tab_bar.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. /**************************************************************************/
  2. /* tab_bar.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "tab_bar.h"
  31. #include "scene/gui/box_container.h"
  32. #include "scene/gui/label.h"
  33. #include "scene/gui/texture_rect.h"
  34. #include "scene/main/viewport.h"
  35. #include "scene/theme/theme_db.h"
  36. Size2 TabBar::get_minimum_size() const {
  37. Size2 ms;
  38. if (tabs.is_empty()) {
  39. return ms;
  40. }
  41. int y_margin = MAX(MAX(MAX(theme_cache.tab_unselected_style->get_minimum_size().height, theme_cache.tab_hovered_style->get_minimum_size().height), theme_cache.tab_selected_style->get_minimum_size().height), theme_cache.tab_disabled_style->get_minimum_size().height);
  42. for (int i = 0; i < tabs.size(); i++) {
  43. if (tabs[i].hidden) {
  44. continue;
  45. }
  46. int ofs = ms.width;
  47. Ref<StyleBox> style;
  48. if (tabs[i].disabled) {
  49. style = theme_cache.tab_disabled_style;
  50. } else if (current == i) {
  51. style = theme_cache.tab_selected_style;
  52. } else if (hover == i) {
  53. style = theme_cache.tab_hovered_style;
  54. } else {
  55. style = theme_cache.tab_unselected_style;
  56. }
  57. ms.width += style->get_minimum_size().width;
  58. if (tabs[i].icon.is_valid()) {
  59. const Size2 icon_size = _get_tab_icon_size(i);
  60. ms.height = MAX(ms.height, icon_size.height + y_margin);
  61. ms.width += icon_size.width + theme_cache.h_separation;
  62. }
  63. if (!tabs[i].text.is_empty()) {
  64. ms.width += tabs[i].size_text + theme_cache.h_separation;
  65. }
  66. ms.height = MAX(ms.height, tabs[i].text_buf->get_size().y + y_margin);
  67. bool close_visible = cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current);
  68. if (tabs[i].right_button.is_valid()) {
  69. Ref<Texture2D> rb = tabs[i].right_button;
  70. if (close_visible) {
  71. ms.width += theme_cache.button_hl_style->get_minimum_size().width + rb->get_width();
  72. } else {
  73. ms.width += theme_cache.button_hl_style->get_margin(SIDE_LEFT) + rb->get_width() + theme_cache.h_separation;
  74. }
  75. ms.height = MAX(ms.height, rb->get_height() + y_margin);
  76. }
  77. if (close_visible) {
  78. ms.width += theme_cache.button_hl_style->get_margin(SIDE_LEFT) + theme_cache.close_icon->get_width() + theme_cache.h_separation;
  79. ms.height = MAX(ms.height, theme_cache.close_icon->get_height() + y_margin);
  80. }
  81. if (ms.width - ofs > style->get_minimum_size().width) {
  82. ms.width -= theme_cache.h_separation;
  83. }
  84. }
  85. if (clip_tabs) {
  86. ms.width = 0;
  87. }
  88. return ms;
  89. }
  90. void TabBar::gui_input(const Ref<InputEvent> &p_event) {
  91. ERR_FAIL_COND(p_event.is_null());
  92. Ref<InputEventMouseMotion> mm = p_event;
  93. if (mm.is_valid()) {
  94. Point2 pos = mm->get_position();
  95. if (buttons_visible) {
  96. if (is_layout_rtl()) {
  97. if (pos.x < theme_cache.decrement_icon->get_width()) {
  98. if (highlight_arrow != 1) {
  99. highlight_arrow = 1;
  100. queue_redraw();
  101. }
  102. } else if (pos.x < theme_cache.increment_icon->get_width() + theme_cache.decrement_icon->get_width()) {
  103. if (highlight_arrow != 0) {
  104. highlight_arrow = 0;
  105. queue_redraw();
  106. }
  107. } else if (highlight_arrow != -1) {
  108. highlight_arrow = -1;
  109. queue_redraw();
  110. }
  111. } else {
  112. int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  113. if (pos.x > limit_minus_buttons + theme_cache.decrement_icon->get_width()) {
  114. if (highlight_arrow != 1) {
  115. highlight_arrow = 1;
  116. queue_redraw();
  117. }
  118. } else if (pos.x > limit_minus_buttons) {
  119. if (highlight_arrow != 0) {
  120. highlight_arrow = 0;
  121. queue_redraw();
  122. }
  123. } else if (highlight_arrow != -1) {
  124. highlight_arrow = -1;
  125. queue_redraw();
  126. }
  127. }
  128. }
  129. if (get_viewport()->gui_is_dragging() && can_drop_data(pos, get_viewport()->gui_get_drag_data())) {
  130. dragging_valid_tab = true;
  131. queue_redraw();
  132. }
  133. if (!tabs.is_empty()) {
  134. _update_hover();
  135. }
  136. return;
  137. }
  138. Ref<InputEventMouseButton> mb = p_event;
  139. if (mb.is_valid()) {
  140. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP && !mb->is_command_or_control_pressed()) {
  141. if (scrolling_enabled && buttons_visible) {
  142. if (offset > 0) {
  143. offset--;
  144. _update_cache();
  145. queue_redraw();
  146. }
  147. }
  148. }
  149. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN && !mb->is_command_or_control_pressed()) {
  150. if (scrolling_enabled && buttons_visible) {
  151. if (missing_right && offset < tabs.size()) {
  152. offset++;
  153. _update_cache();
  154. queue_redraw();
  155. }
  156. }
  157. }
  158. if (rb_pressing && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  159. if (rb_hover != -1) {
  160. emit_signal(SNAME("tab_button_pressed"), rb_hover);
  161. }
  162. rb_pressing = false;
  163. queue_redraw();
  164. }
  165. if (cb_pressing && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  166. if (cb_hover != -1) {
  167. emit_signal(SNAME("tab_close_pressed"), cb_hover);
  168. }
  169. cb_pressing = false;
  170. queue_redraw();
  171. }
  172. if (mb->is_pressed() && (mb->get_button_index() == MouseButton::LEFT || (select_with_rmb && mb->get_button_index() == MouseButton::RIGHT))) {
  173. Point2 pos = mb->get_position();
  174. if (buttons_visible) {
  175. if (is_layout_rtl()) {
  176. if (pos.x < theme_cache.decrement_icon->get_width()) {
  177. if (missing_right) {
  178. offset++;
  179. _update_cache();
  180. queue_redraw();
  181. }
  182. return;
  183. } else if (pos.x < theme_cache.increment_icon->get_width() + theme_cache.decrement_icon->get_width()) {
  184. if (offset > 0) {
  185. offset--;
  186. _update_cache();
  187. queue_redraw();
  188. }
  189. return;
  190. }
  191. } else {
  192. int limit = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  193. if (pos.x > limit + theme_cache.decrement_icon->get_width()) {
  194. if (missing_right) {
  195. offset++;
  196. _update_cache();
  197. queue_redraw();
  198. }
  199. return;
  200. } else if (pos.x > limit) {
  201. if (offset > 0) {
  202. offset--;
  203. _update_cache();
  204. queue_redraw();
  205. }
  206. return;
  207. }
  208. }
  209. }
  210. if (tabs.is_empty()) {
  211. // Return early if there are no actual tabs to handle input for.
  212. return;
  213. }
  214. int found = -1;
  215. for (int i = offset; i <= max_drawn_tab; i++) {
  216. if (tabs[i].hidden) {
  217. continue;
  218. }
  219. if (tabs[i].rb_rect.has_point(pos)) {
  220. rb_pressing = true;
  221. _update_hover();
  222. queue_redraw();
  223. return;
  224. }
  225. if (tabs[i].cb_rect.has_point(pos) && (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current))) {
  226. cb_pressing = true;
  227. _update_hover();
  228. queue_redraw();
  229. return;
  230. }
  231. if (pos.x >= get_tab_rect(i).position.x && pos.x < get_tab_rect(i).position.x + tabs[i].size_cache) {
  232. if (!tabs[i].disabled) {
  233. found = i;
  234. }
  235. break;
  236. }
  237. }
  238. if (found != -1) {
  239. if (deselect_enabled && get_current_tab() == found) {
  240. set_current_tab(-1);
  241. } else {
  242. set_current_tab(found);
  243. }
  244. if (mb->get_button_index() == MouseButton::RIGHT) {
  245. // Right mouse button clicked.
  246. emit_signal(SNAME("tab_rmb_clicked"), found);
  247. }
  248. emit_signal(SNAME("tab_clicked"), found);
  249. }
  250. }
  251. }
  252. if (p_event->is_pressed()) {
  253. Input *input = Input::get_singleton();
  254. Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
  255. Ref<InputEventJoypadButton> joypadbutton_event = p_event;
  256. bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
  257. if (p_event->is_action("ui_right", true)) {
  258. if (is_joypad_event) {
  259. if (!input->is_action_just_pressed("ui_right", true)) {
  260. return;
  261. }
  262. set_process_internal(true);
  263. }
  264. if (is_layout_rtl() ? select_previous_available() : select_next_available()) {
  265. accept_event();
  266. }
  267. } else if (p_event->is_action("ui_left", true)) {
  268. if (is_joypad_event) {
  269. if (!input->is_action_just_pressed("ui_left", true)) {
  270. return;
  271. }
  272. set_process_internal(true);
  273. }
  274. if (is_layout_rtl() ? select_next_available() : select_previous_available()) {
  275. accept_event();
  276. }
  277. }
  278. }
  279. }
  280. String TabBar::get_tooltip(const Point2 &p_pos) const {
  281. int tab_idx = get_tab_idx_at_point(p_pos);
  282. if (tab_idx < 0) {
  283. return Control::get_tooltip(p_pos);
  284. }
  285. if (tabs[tab_idx].tooltip.is_empty() && tabs[tab_idx].truncated) {
  286. return tabs[tab_idx].text;
  287. }
  288. return tabs[tab_idx].tooltip;
  289. }
  290. void TabBar::_shape(int p_tab) {
  291. tabs.write[p_tab].text_buf->clear();
  292. tabs.write[p_tab].text_buf->set_width(-1);
  293. if (tabs[p_tab].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  294. tabs.write[p_tab].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  295. } else {
  296. tabs.write[p_tab].text_buf->set_direction((TextServer::Direction)tabs[p_tab].text_direction);
  297. }
  298. tabs.write[p_tab].text_buf->add_string(atr(tabs[p_tab].text), theme_cache.font, theme_cache.font_size, tabs[p_tab].language);
  299. }
  300. void TabBar::_notification(int p_what) {
  301. switch (p_what) {
  302. case NOTIFICATION_ENTER_TREE: {
  303. if (scroll_to_selected) {
  304. ensure_tab_visible(current);
  305. }
  306. // Set initialized even if no tabs were set.
  307. initialized = true;
  308. } break;
  309. case NOTIFICATION_INTERNAL_PROCESS: {
  310. Input *input = Input::get_singleton();
  311. if (input->is_action_just_released("ui_left") || input->is_action_just_released("ui_right")) {
  312. gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  313. set_process_internal(false);
  314. return;
  315. }
  316. gamepad_event_delay_ms -= get_process_delta_time();
  317. if (gamepad_event_delay_ms <= 0) {
  318. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  319. if (input->is_action_pressed("ui_right")) {
  320. is_layout_rtl() ? select_previous_available() : select_next_available();
  321. }
  322. if (input->is_action_pressed("ui_left")) {
  323. is_layout_rtl() ? select_next_available() : select_previous_available();
  324. }
  325. }
  326. } break;
  327. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  328. queue_redraw();
  329. } break;
  330. case NOTIFICATION_THEME_CHANGED:
  331. case NOTIFICATION_TRANSLATION_CHANGED: {
  332. for (int i = 0; i < tabs.size(); ++i) {
  333. _shape(i);
  334. }
  335. queue_redraw();
  336. update_minimum_size();
  337. [[fallthrough]];
  338. }
  339. case NOTIFICATION_RESIZED: {
  340. int ofs_old = offset;
  341. int max_old = max_drawn_tab;
  342. _update_cache();
  343. _ensure_no_over_offset();
  344. if (scroll_to_selected && (offset != ofs_old || max_drawn_tab != max_old)) {
  345. ensure_tab_visible(current);
  346. }
  347. } break;
  348. case NOTIFICATION_DRAG_END: {
  349. if (dragging_valid_tab) {
  350. dragging_valid_tab = false;
  351. queue_redraw();
  352. }
  353. } break;
  354. case NOTIFICATION_DRAW: {
  355. bool rtl = is_layout_rtl();
  356. Vector2 size = get_size();
  357. if (tabs.is_empty()) {
  358. // Draw the drop indicator where the first tab would be if there are no tabs.
  359. if (dragging_valid_tab) {
  360. int x = rtl ? size.x : 0;
  361. theme_cache.drop_mark_icon->draw(get_canvas_item(), Point2(x - (theme_cache.drop_mark_icon->get_width() / 2), (size.height - theme_cache.drop_mark_icon->get_height()) / 2), theme_cache.drop_mark_color);
  362. }
  363. return;
  364. }
  365. int limit_minus_buttons = size.width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  366. int ofs = tabs[offset].ofs_cache;
  367. // Draw unselected tabs in the back.
  368. for (int i = offset; i <= max_drawn_tab; i++) {
  369. if (tabs[i].hidden) {
  370. continue;
  371. }
  372. if (i != current) {
  373. Ref<StyleBox> sb;
  374. Color col;
  375. if (tabs[i].disabled) {
  376. sb = theme_cache.tab_disabled_style;
  377. col = theme_cache.font_disabled_color;
  378. } else if (i == hover) {
  379. sb = theme_cache.tab_hovered_style;
  380. col = theme_cache.font_hovered_color;
  381. } else {
  382. sb = theme_cache.tab_unselected_style;
  383. col = theme_cache.font_unselected_color;
  384. }
  385. _draw_tab(sb, col, i, rtl ? size.width - ofs - tabs[i].size_cache : ofs, false);
  386. }
  387. ofs += tabs[i].size_cache;
  388. }
  389. // Draw selected tab in the front, but only if it's visible.
  390. if (current >= offset && current <= max_drawn_tab && !tabs[current].hidden) {
  391. Ref<StyleBox> sb = tabs[current].disabled ? theme_cache.tab_disabled_style : theme_cache.tab_selected_style;
  392. float x = rtl ? size.width - tabs[current].ofs_cache - tabs[current].size_cache : tabs[current].ofs_cache;
  393. _draw_tab(sb, theme_cache.font_selected_color, current, x, has_focus());
  394. }
  395. if (buttons_visible) {
  396. int vofs = (size.height - theme_cache.increment_icon->get_size().height) / 2;
  397. if (rtl) {
  398. if (missing_right) {
  399. draw_texture(highlight_arrow == 1 ? theme_cache.decrement_hl_icon : theme_cache.decrement_icon, Point2(0, vofs));
  400. } else {
  401. draw_texture(theme_cache.decrement_icon, Point2(0, vofs), Color(1, 1, 1, 0.5));
  402. }
  403. if (offset > 0) {
  404. draw_texture(highlight_arrow == 0 ? theme_cache.increment_hl_icon : theme_cache.increment_icon, Point2(theme_cache.increment_icon->get_size().width, vofs));
  405. } else {
  406. draw_texture(theme_cache.increment_icon, Point2(theme_cache.increment_icon->get_size().width, vofs), Color(1, 1, 1, 0.5));
  407. }
  408. } else {
  409. if (offset > 0) {
  410. draw_texture(highlight_arrow == 0 ? theme_cache.decrement_hl_icon : theme_cache.decrement_icon, Point2(limit_minus_buttons, vofs));
  411. } else {
  412. draw_texture(theme_cache.decrement_icon, Point2(limit_minus_buttons, vofs), Color(1, 1, 1, 0.5));
  413. }
  414. if (missing_right) {
  415. draw_texture(highlight_arrow == 1 ? theme_cache.increment_hl_icon : theme_cache.increment_icon, Point2(limit_minus_buttons + theme_cache.decrement_icon->get_size().width, vofs));
  416. } else {
  417. draw_texture(theme_cache.increment_icon, Point2(limit_minus_buttons + theme_cache.decrement_icon->get_size().width, vofs), Color(1, 1, 1, 0.5));
  418. }
  419. }
  420. }
  421. if (dragging_valid_tab) {
  422. int x;
  423. int tab_hover = get_hovered_tab();
  424. if (tab_hover != -1) {
  425. Rect2 tab_rect = get_tab_rect(tab_hover);
  426. x = tab_rect.position.x;
  427. if (get_local_mouse_position().x > x + tab_rect.size.width / 2) {
  428. x += tab_rect.size.width;
  429. }
  430. } else {
  431. if (rtl ^ (get_local_mouse_position().x < get_tab_rect(0).position.x)) {
  432. x = get_tab_rect(0).position.x;
  433. if (rtl) {
  434. x += get_tab_rect(0).size.width;
  435. }
  436. } else {
  437. Rect2 tab_rect = get_tab_rect(get_tab_count() - 1);
  438. x = tab_rect.position.x;
  439. if (!rtl) {
  440. x += tab_rect.size.width;
  441. }
  442. }
  443. }
  444. theme_cache.drop_mark_icon->draw(get_canvas_item(), Point2(x - theme_cache.drop_mark_icon->get_width() / 2, (size.height - theme_cache.drop_mark_icon->get_height()) / 2), theme_cache.drop_mark_color);
  445. }
  446. } break;
  447. }
  448. }
  449. void TabBar::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x, bool p_focus) {
  450. RID ci = get_canvas_item();
  451. bool rtl = is_layout_rtl();
  452. Rect2 sb_rect = Rect2(p_x, 0, tabs[p_index].size_cache, get_size().height);
  453. if (tab_style_v_flip) {
  454. draw_set_transform(Point2(0.0, p_tab_style->get_draw_rect(sb_rect).size.y), 0.0, Size2(1.0, -1.0));
  455. }
  456. p_tab_style->draw(ci, sb_rect);
  457. if (tab_style_v_flip) {
  458. draw_set_transform(Point2(), 0.0, Size2(1.0, 1.0));
  459. }
  460. if (p_focus) {
  461. Ref<StyleBox> focus_style = theme_cache.tab_focus_style;
  462. focus_style->draw(ci, sb_rect);
  463. }
  464. p_x += rtl ? tabs[p_index].size_cache - p_tab_style->get_margin(SIDE_LEFT) : p_tab_style->get_margin(SIDE_LEFT);
  465. Size2i sb_ms = p_tab_style->get_minimum_size();
  466. // Draw the icon.
  467. Ref<Texture2D> icon = tabs[p_index].icon;
  468. if (icon.is_valid()) {
  469. const Size2 icon_size = _get_tab_icon_size(p_index);
  470. const Point2 icon_pos = Point2i(rtl ? p_x - icon_size.width : p_x, p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - icon_size.height) / 2);
  471. icon->draw_rect(ci, Rect2(icon_pos, icon_size));
  472. p_x = rtl ? p_x - icon_size.width - theme_cache.h_separation : p_x + icon_size.width + theme_cache.h_separation;
  473. }
  474. // Draw the text.
  475. if (!tabs[p_index].text.is_empty()) {
  476. Point2i text_pos = Point2i(rtl ? p_x - tabs[p_index].size_text : p_x,
  477. p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - tabs[p_index].text_buf->get_size().y) / 2);
  478. if (theme_cache.outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  479. tabs[p_index].text_buf->draw_outline(ci, text_pos, theme_cache.outline_size, theme_cache.font_outline_color);
  480. }
  481. tabs[p_index].text_buf->draw(ci, text_pos, p_font_color);
  482. p_x = rtl ? p_x - tabs[p_index].size_text - theme_cache.h_separation : p_x + tabs[p_index].size_text + theme_cache.h_separation;
  483. }
  484. // Draw and calculate rect of the right button.
  485. if (tabs[p_index].right_button.is_valid()) {
  486. Ref<StyleBox> style = theme_cache.button_hl_style;
  487. Ref<Texture2D> rb = tabs[p_index].right_button;
  488. Rect2 rb_rect;
  489. rb_rect.size = style->get_minimum_size() + rb->get_size();
  490. rb_rect.position.x = rtl ? p_x - rb_rect.size.width : p_x;
  491. rb_rect.position.y = p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (rb_rect.size.y)) / 2;
  492. tabs.write[p_index].rb_rect = rb_rect;
  493. if (rb_hover == p_index) {
  494. if (rb_pressing) {
  495. theme_cache.button_pressed_style->draw(ci, rb_rect);
  496. } else {
  497. style->draw(ci, rb_rect);
  498. }
  499. }
  500. rb->draw(ci, Point2i(rb_rect.position.x + style->get_margin(SIDE_LEFT), rb_rect.position.y + style->get_margin(SIDE_TOP)));
  501. p_x = rtl ? rb_rect.position.x : rb_rect.position.x + rb_rect.size.width;
  502. } else {
  503. tabs.write[p_index].rb_rect = Rect2();
  504. }
  505. // Draw and calculate rect of the close button.
  506. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_index == current)) {
  507. Ref<StyleBox> style = theme_cache.button_hl_style;
  508. Ref<Texture2D> cb = theme_cache.close_icon;
  509. Rect2 cb_rect;
  510. cb_rect.size = style->get_minimum_size() + cb->get_size();
  511. cb_rect.position.x = rtl ? p_x - cb_rect.size.width : p_x;
  512. cb_rect.position.y = p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (cb_rect.size.y)) / 2;
  513. tabs.write[p_index].cb_rect = cb_rect;
  514. if (!tabs[p_index].disabled && cb_hover == p_index) {
  515. if (cb_pressing) {
  516. theme_cache.button_pressed_style->draw(ci, cb_rect);
  517. } else {
  518. style->draw(ci, cb_rect);
  519. }
  520. }
  521. cb->draw(ci, Point2i(cb_rect.position.x + style->get_margin(SIDE_LEFT), cb_rect.position.y + style->get_margin(SIDE_TOP)));
  522. } else {
  523. tabs.write[p_index].cb_rect = Rect2();
  524. }
  525. }
  526. void TabBar::set_tab_count(int p_count) {
  527. if (p_count == tabs.size()) {
  528. return;
  529. }
  530. ERR_FAIL_COND(p_count < 0);
  531. tabs.resize(p_count);
  532. if (p_count == 0) {
  533. offset = 0;
  534. max_drawn_tab = 0;
  535. current = -1;
  536. previous = -1;
  537. } else {
  538. offset = MIN(offset, p_count - 1);
  539. max_drawn_tab = MIN(max_drawn_tab, p_count - 1);
  540. current = MIN(current, p_count - 1);
  541. // Fix range if unable to deselect.
  542. if (current == -1 && !_can_deselect()) {
  543. current = 0;
  544. }
  545. _update_cache();
  546. _ensure_no_over_offset();
  547. if (scroll_to_selected) {
  548. ensure_tab_visible(current);
  549. }
  550. }
  551. if (!initialized) {
  552. initialized = true;
  553. if (queued_current != CURRENT_TAB_UNINITIALIZED && queued_current != current) {
  554. set_current_tab(queued_current);
  555. }
  556. }
  557. queue_redraw();
  558. update_minimum_size();
  559. notify_property_list_changed();
  560. }
  561. int TabBar::get_tab_count() const {
  562. return tabs.size();
  563. }
  564. void TabBar::set_current_tab(int p_current) {
  565. if (p_current == -1) {
  566. // An index of -1 is only valid if deselecting is enabled or there are no valid tabs.
  567. ERR_FAIL_COND_MSG(!_can_deselect(), "Cannot deselect tabs, deselection is not enabled.");
  568. } else {
  569. if (!initialized && p_current >= get_tab_count()) {
  570. queued_current = p_current;
  571. return;
  572. }
  573. ERR_FAIL_INDEX(p_current, get_tab_count());
  574. }
  575. previous = current;
  576. current = p_current;
  577. if (current == previous) {
  578. emit_signal(SNAME("tab_selected"), current);
  579. return;
  580. }
  581. emit_signal(SNAME("tab_selected"), current);
  582. _update_cache();
  583. if (scroll_to_selected) {
  584. ensure_tab_visible(current);
  585. }
  586. queue_redraw();
  587. emit_signal(SNAME("tab_changed"), p_current);
  588. }
  589. int TabBar::get_current_tab() const {
  590. return current;
  591. }
  592. int TabBar::get_previous_tab() const {
  593. return previous;
  594. }
  595. int TabBar::get_hovered_tab() const {
  596. return hover;
  597. }
  598. bool TabBar::select_previous_available() {
  599. const int offset_end = (get_current_tab() + 1);
  600. for (int i = 1; i < offset_end; i++) {
  601. int target_tab = get_current_tab() - i;
  602. if (target_tab < 0) {
  603. target_tab += get_tab_count();
  604. }
  605. if (!is_tab_disabled(target_tab) && !is_tab_hidden(target_tab)) {
  606. set_current_tab(target_tab);
  607. return true;
  608. }
  609. }
  610. return false;
  611. }
  612. bool TabBar::select_next_available() {
  613. const int offset_end = (get_tab_count() - get_current_tab());
  614. for (int i = 1; i < offset_end; i++) {
  615. int target_tab = (get_current_tab() + i) % get_tab_count();
  616. if (!is_tab_disabled(target_tab) && !is_tab_hidden(target_tab)) {
  617. set_current_tab(target_tab);
  618. return true;
  619. }
  620. }
  621. return false;
  622. }
  623. void TabBar::set_tab_offset(int p_offset) {
  624. ERR_FAIL_INDEX(p_offset, tabs.size());
  625. offset = p_offset;
  626. _update_cache();
  627. queue_redraw();
  628. }
  629. int TabBar::get_tab_offset() const {
  630. return offset;
  631. }
  632. bool TabBar::get_offset_buttons_visible() const {
  633. return buttons_visible;
  634. }
  635. void TabBar::set_tab_title(int p_tab, const String &p_title) {
  636. ERR_FAIL_INDEX(p_tab, tabs.size());
  637. if (tabs[p_tab].text == p_title) {
  638. return;
  639. }
  640. tabs.write[p_tab].text = p_title;
  641. _shape(p_tab);
  642. _update_cache();
  643. _ensure_no_over_offset();
  644. if (scroll_to_selected) {
  645. ensure_tab_visible(current);
  646. }
  647. queue_redraw();
  648. update_minimum_size();
  649. }
  650. String TabBar::get_tab_title(int p_tab) const {
  651. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  652. return tabs[p_tab].text;
  653. }
  654. void TabBar::set_tab_tooltip(int p_tab, const String &p_tooltip) {
  655. ERR_FAIL_INDEX(p_tab, tabs.size());
  656. tabs.write[p_tab].tooltip = p_tooltip;
  657. }
  658. String TabBar::get_tab_tooltip(int p_tab) const {
  659. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  660. return tabs[p_tab].tooltip;
  661. }
  662. void TabBar::set_tab_text_direction(int p_tab, Control::TextDirection p_text_direction) {
  663. ERR_FAIL_INDEX(p_tab, tabs.size());
  664. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  665. if (tabs[p_tab].text_direction != p_text_direction) {
  666. tabs.write[p_tab].text_direction = p_text_direction;
  667. _shape(p_tab);
  668. queue_redraw();
  669. }
  670. }
  671. Control::TextDirection TabBar::get_tab_text_direction(int p_tab) const {
  672. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Control::TEXT_DIRECTION_INHERITED);
  673. return tabs[p_tab].text_direction;
  674. }
  675. void TabBar::set_tab_language(int p_tab, const String &p_language) {
  676. ERR_FAIL_INDEX(p_tab, tabs.size());
  677. if (tabs[p_tab].language != p_language) {
  678. tabs.write[p_tab].language = p_language;
  679. _shape(p_tab);
  680. _update_cache();
  681. _ensure_no_over_offset();
  682. if (scroll_to_selected) {
  683. ensure_tab_visible(current);
  684. }
  685. queue_redraw();
  686. update_minimum_size();
  687. }
  688. }
  689. String TabBar::get_tab_language(int p_tab) const {
  690. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  691. return tabs[p_tab].language;
  692. }
  693. void TabBar::set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  694. ERR_FAIL_INDEX(p_tab, tabs.size());
  695. if (tabs[p_tab].icon == p_icon) {
  696. return;
  697. }
  698. tabs.write[p_tab].icon = p_icon;
  699. _update_cache();
  700. _ensure_no_over_offset();
  701. if (scroll_to_selected) {
  702. ensure_tab_visible(current);
  703. }
  704. queue_redraw();
  705. update_minimum_size();
  706. }
  707. Ref<Texture2D> TabBar::get_tab_icon(int p_tab) const {
  708. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
  709. return tabs[p_tab].icon;
  710. }
  711. void TabBar::set_tab_icon_max_width(int p_tab, int p_width) {
  712. ERR_FAIL_INDEX(p_tab, tabs.size());
  713. if (tabs[p_tab].icon_max_width == p_width) {
  714. return;
  715. }
  716. tabs.write[p_tab].icon_max_width = p_width;
  717. _update_cache();
  718. _ensure_no_over_offset();
  719. if (scroll_to_selected) {
  720. ensure_tab_visible(current);
  721. }
  722. queue_redraw();
  723. update_minimum_size();
  724. }
  725. int TabBar::get_tab_icon_max_width(int p_tab) const {
  726. ERR_FAIL_INDEX_V(p_tab, tabs.size(), 0);
  727. return tabs[p_tab].icon_max_width;
  728. }
  729. void TabBar::set_tab_disabled(int p_tab, bool p_disabled) {
  730. ERR_FAIL_INDEX(p_tab, tabs.size());
  731. if (tabs[p_tab].disabled == p_disabled) {
  732. return;
  733. }
  734. tabs.write[p_tab].disabled = p_disabled;
  735. _update_cache();
  736. _ensure_no_over_offset();
  737. if (scroll_to_selected) {
  738. ensure_tab_visible(current);
  739. }
  740. queue_redraw();
  741. update_minimum_size();
  742. }
  743. bool TabBar::is_tab_disabled(int p_tab) const {
  744. ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
  745. return tabs[p_tab].disabled;
  746. }
  747. void TabBar::set_tab_hidden(int p_tab, bool p_hidden) {
  748. ERR_FAIL_INDEX(p_tab, tabs.size());
  749. if (tabs[p_tab].hidden == p_hidden) {
  750. return;
  751. }
  752. tabs.write[p_tab].hidden = p_hidden;
  753. _update_cache();
  754. _ensure_no_over_offset();
  755. if (scroll_to_selected) {
  756. ensure_tab_visible(current);
  757. }
  758. queue_redraw();
  759. update_minimum_size();
  760. }
  761. bool TabBar::is_tab_hidden(int p_tab) const {
  762. ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
  763. return tabs[p_tab].hidden;
  764. }
  765. void TabBar::set_tab_metadata(int p_tab, const Variant &p_metadata) {
  766. ERR_FAIL_INDEX(p_tab, tabs.size());
  767. tabs.write[p_tab].metadata = p_metadata;
  768. }
  769. Variant TabBar::get_tab_metadata(int p_tab) const {
  770. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Variant());
  771. return tabs[p_tab].metadata;
  772. }
  773. void TabBar::set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  774. ERR_FAIL_INDEX(p_tab, tabs.size());
  775. if (tabs[p_tab].right_button == p_icon) {
  776. return;
  777. }
  778. tabs.write[p_tab].right_button = p_icon;
  779. _update_cache();
  780. _ensure_no_over_offset();
  781. if (scroll_to_selected) {
  782. ensure_tab_visible(current);
  783. }
  784. queue_redraw();
  785. update_minimum_size();
  786. }
  787. Ref<Texture2D> TabBar::get_tab_button_icon(int p_tab) const {
  788. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
  789. return tabs[p_tab].right_button;
  790. }
  791. void TabBar::_update_hover() {
  792. if (!is_inside_tree()) {
  793. return;
  794. }
  795. ERR_FAIL_COND(tabs.is_empty());
  796. const Point2 &pos = get_local_mouse_position();
  797. // Test hovering to display right or close button.
  798. int hover_now = -1;
  799. int hover_buttons = -1;
  800. for (int i = offset; i <= max_drawn_tab; i++) {
  801. if (tabs[i].hidden) {
  802. continue;
  803. }
  804. Rect2 rect = get_tab_rect(i);
  805. if (rect.has_point(pos)) {
  806. hover_now = i;
  807. }
  808. if (tabs[i].rb_rect.has_point(pos)) {
  809. rb_hover = i;
  810. cb_hover = -1;
  811. hover_buttons = i;
  812. } else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) {
  813. cb_hover = i;
  814. rb_hover = -1;
  815. hover_buttons = i;
  816. }
  817. if (hover_buttons != -1) {
  818. queue_redraw();
  819. break;
  820. }
  821. }
  822. if (hover != hover_now) {
  823. hover = hover_now;
  824. if (hover != -1) {
  825. emit_signal(SNAME("tab_hovered"), hover);
  826. }
  827. _update_cache();
  828. queue_redraw();
  829. }
  830. if (hover_buttons == -1) { // No hover.
  831. int rb_hover_old = rb_hover;
  832. int cb_hover_old = cb_hover;
  833. rb_hover = hover_buttons;
  834. cb_hover = hover_buttons;
  835. if (rb_hover != rb_hover_old || cb_hover != cb_hover_old) {
  836. queue_redraw();
  837. }
  838. }
  839. }
  840. void TabBar::_update_cache(bool p_update_hover) {
  841. if (tabs.is_empty()) {
  842. buttons_visible = false;
  843. return;
  844. }
  845. int limit = get_size().width;
  846. int limit_minus_buttons = limit - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  847. int w = 0;
  848. max_drawn_tab = tabs.size() - 1;
  849. for (int i = 0; i < tabs.size(); i++) {
  850. tabs.write[i].text_buf->set_width(-1);
  851. tabs.write[i].size_text = Math::ceil(tabs[i].text_buf->get_size().x);
  852. tabs.write[i].size_cache = get_tab_width(i);
  853. tabs.write[i].truncated = max_width > 0 && tabs[i].size_cache > max_width;
  854. if (tabs[i].truncated) {
  855. int size_textless = tabs[i].size_cache - tabs[i].size_text;
  856. int mw = MAX(size_textless, max_width);
  857. tabs.write[i].size_text = MAX(mw - size_textless, 1);
  858. tabs.write[i].text_buf->set_width(tabs[i].size_text);
  859. tabs.write[i].size_cache = size_textless + tabs[i].size_text;
  860. }
  861. if (i < offset || i > max_drawn_tab) {
  862. tabs.write[i].ofs_cache = 0;
  863. continue;
  864. }
  865. tabs.write[i].ofs_cache = w;
  866. if (tabs[i].hidden) {
  867. continue;
  868. }
  869. w += tabs[i].size_cache;
  870. // Check if all tabs would fit inside the area.
  871. if (clip_tabs && i > offset && (w > limit || (offset > 0 && w > limit_minus_buttons))) {
  872. tabs.write[i].ofs_cache = 0;
  873. w -= tabs[i].size_cache;
  874. max_drawn_tab = i - 1;
  875. while (w > limit_minus_buttons && max_drawn_tab > offset) {
  876. tabs.write[max_drawn_tab].ofs_cache = 0;
  877. if (!tabs[max_drawn_tab].hidden) {
  878. w -= tabs[max_drawn_tab].size_cache;
  879. }
  880. max_drawn_tab--;
  881. }
  882. }
  883. }
  884. missing_right = max_drawn_tab < tabs.size() - 1;
  885. buttons_visible = offset > 0 || missing_right;
  886. if (tab_alignment == ALIGNMENT_LEFT) {
  887. if (p_update_hover) {
  888. _update_hover();
  889. }
  890. return;
  891. }
  892. if (tab_alignment == ALIGNMENT_CENTER) {
  893. w = ((buttons_visible ? limit_minus_buttons : limit) - w) / 2;
  894. } else if (tab_alignment == ALIGNMENT_RIGHT) {
  895. w = (buttons_visible ? limit_minus_buttons : limit) - w;
  896. }
  897. for (int i = offset; i <= max_drawn_tab; i++) {
  898. tabs.write[i].ofs_cache = w;
  899. if (!tabs[i].hidden) {
  900. w += tabs[i].size_cache;
  901. }
  902. }
  903. if (p_update_hover) {
  904. _update_hover();
  905. }
  906. }
  907. void TabBar::_on_mouse_exited() {
  908. rb_hover = -1;
  909. cb_hover = -1;
  910. hover = -1;
  911. highlight_arrow = -1;
  912. dragging_valid_tab = false;
  913. _update_cache(false);
  914. queue_redraw();
  915. }
  916. void TabBar::add_tab(const String &p_str, const Ref<Texture2D> &p_icon) {
  917. Tab t;
  918. t.text = p_str;
  919. t.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  920. t.icon = p_icon;
  921. tabs.push_back(t);
  922. _shape(tabs.size() - 1);
  923. _update_cache();
  924. if (scroll_to_selected) {
  925. ensure_tab_visible(current);
  926. }
  927. queue_redraw();
  928. update_minimum_size();
  929. if (tabs.size() == 1) {
  930. if (is_inside_tree()) {
  931. set_current_tab(0);
  932. } else {
  933. current = 0;
  934. previous = -1;
  935. }
  936. }
  937. }
  938. void TabBar::clear_tabs() {
  939. if (tabs.is_empty()) {
  940. return;
  941. }
  942. tabs.clear();
  943. offset = 0;
  944. max_drawn_tab = 0;
  945. current = -1;
  946. previous = -1;
  947. queue_redraw();
  948. update_minimum_size();
  949. notify_property_list_changed();
  950. }
  951. void TabBar::remove_tab(int p_idx) {
  952. ERR_FAIL_INDEX(p_idx, tabs.size());
  953. tabs.remove_at(p_idx);
  954. bool is_tab_changing = current == p_idx;
  955. if (current >= p_idx && current > 0) {
  956. current--;
  957. }
  958. if (previous >= p_idx && previous > 0) {
  959. previous--;
  960. }
  961. if (tabs.is_empty()) {
  962. offset = 0;
  963. max_drawn_tab = 0;
  964. current = -1;
  965. previous = -1;
  966. } else {
  967. if (current != -1) {
  968. // Try to change to a valid tab if possible (without firing the `tab_selected` signal).
  969. for (int i = current; i < tabs.size(); i++) {
  970. if (!is_tab_disabled(i) && !is_tab_hidden(i)) {
  971. current = i;
  972. break;
  973. }
  974. }
  975. // If nothing, try backwards.
  976. if (is_tab_disabled(current) || is_tab_hidden(current)) {
  977. for (int i = current - 1; i >= 0; i--) {
  978. if (!is_tab_disabled(i) && !is_tab_hidden(i)) {
  979. current = i;
  980. break;
  981. }
  982. }
  983. }
  984. // If still no valid tab, deselect.
  985. if (is_tab_disabled(current) || is_tab_hidden(current)) {
  986. current = -1;
  987. }
  988. }
  989. offset = MIN(offset, tabs.size() - 1);
  990. max_drawn_tab = MIN(max_drawn_tab, tabs.size() - 1);
  991. _update_cache();
  992. _ensure_no_over_offset();
  993. if (scroll_to_selected) {
  994. ensure_tab_visible(current);
  995. }
  996. }
  997. queue_redraw();
  998. update_minimum_size();
  999. notify_property_list_changed();
  1000. if (is_tab_changing && is_inside_tree()) {
  1001. emit_signal(SNAME("tab_changed"), current);
  1002. }
  1003. }
  1004. Variant TabBar::get_drag_data(const Point2 &p_point) {
  1005. if (!drag_to_rearrange_enabled) {
  1006. return Control::get_drag_data(p_point); // Allow stuff like TabContainer to override it.
  1007. }
  1008. return _handle_get_drag_data("tab_bar_tab", p_point);
  1009. }
  1010. bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  1011. if (!drag_to_rearrange_enabled) {
  1012. return Control::can_drop_data(p_point, p_data); // Allow stuff like TabContainer to override it.
  1013. }
  1014. return _handle_can_drop_data("tab_bar_tab", p_point, p_data);
  1015. }
  1016. void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) {
  1017. if (!drag_to_rearrange_enabled) {
  1018. Control::drop_data(p_point, p_data); // Allow stuff like TabContainer to override it.
  1019. return;
  1020. }
  1021. _handle_drop_data("tab_bar_tab", p_point, p_data, callable_mp(this, &TabBar::move_tab), callable_mp(this, &TabBar::_move_tab_from));
  1022. }
  1023. Variant TabBar::_handle_get_drag_data(const String &p_type, const Point2 &p_point) {
  1024. int tab_over = get_tab_idx_at_point(p_point);
  1025. if (tab_over < 0) {
  1026. return Variant();
  1027. }
  1028. HBoxContainer *drag_preview = memnew(HBoxContainer);
  1029. if (tabs[tab_over].icon.is_valid()) {
  1030. const Size2 icon_size = _get_tab_icon_size(tab_over);
  1031. TextureRect *tf = memnew(TextureRect);
  1032. tf->set_texture(tabs[tab_over].icon);
  1033. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1034. tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1035. tf->set_custom_minimum_size(icon_size);
  1036. drag_preview->add_child(tf);
  1037. }
  1038. Label *label = memnew(Label(get_tab_title(tab_over)));
  1039. label->set_auto_translate_mode(get_auto_translate_mode()); // Reflect how the title is displayed.
  1040. drag_preview->add_child(label);
  1041. set_drag_preview(drag_preview);
  1042. Dictionary drag_data;
  1043. drag_data["type"] = p_type;
  1044. drag_data["tab_index"] = tab_over;
  1045. drag_data["from_path"] = get_path();
  1046. return drag_data;
  1047. }
  1048. bool TabBar::_handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const {
  1049. Dictionary d = p_data;
  1050. if (!d.has("type")) {
  1051. return false;
  1052. }
  1053. if (String(d["type"]) == p_type) {
  1054. NodePath from_path = d["from_path"];
  1055. NodePath to_path = get_path();
  1056. if (from_path == to_path) {
  1057. return true;
  1058. } else if (get_tabs_rearrange_group() != -1) {
  1059. // Drag and drop between other TabBars.
  1060. Node *from_node = get_node(from_path);
  1061. TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
  1062. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  1063. return true;
  1064. }
  1065. }
  1066. }
  1067. return false;
  1068. }
  1069. void TabBar::_handle_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data, const Callable &p_move_tab_callback, const Callable &p_move_tab_from_other_callback) {
  1070. Dictionary d = p_data;
  1071. if (!d.has("type")) {
  1072. return;
  1073. }
  1074. if (String(d["type"]) == p_type) {
  1075. int tab_from_id = d["tab_index"];
  1076. int hover_now = get_tab_idx_at_point(p_point);
  1077. NodePath from_path = d["from_path"];
  1078. NodePath to_path = get_path();
  1079. if (from_path == to_path) {
  1080. if (tab_from_id == hover_now) {
  1081. return;
  1082. }
  1083. // Drop the new tab to the left or right depending on where the target tab is being hovered.
  1084. if (hover_now != -1) {
  1085. Rect2 tab_rect = get_tab_rect(hover_now);
  1086. if (is_layout_rtl() ^ (p_point.x <= tab_rect.position.x + tab_rect.size.width / 2)) {
  1087. if (hover_now > tab_from_id) {
  1088. hover_now -= 1;
  1089. }
  1090. } else if (tab_from_id > hover_now) {
  1091. hover_now += 1;
  1092. }
  1093. } else {
  1094. int x = tabs.is_empty() ? 0 : get_tab_rect(0).position.x;
  1095. hover_now = is_layout_rtl() ^ (p_point.x < x) ? 0 : get_tab_count() - 1;
  1096. }
  1097. p_move_tab_callback.call(tab_from_id, hover_now);
  1098. if (!is_tab_disabled(hover_now)) {
  1099. emit_signal(SNAME("active_tab_rearranged"), hover_now);
  1100. set_current_tab(hover_now);
  1101. }
  1102. } else if (get_tabs_rearrange_group() != -1) {
  1103. // Drag and drop between Tabs.
  1104. Node *from_node = get_node(from_path);
  1105. TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
  1106. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  1107. if (tab_from_id >= from_tabs->get_tab_count()) {
  1108. return;
  1109. }
  1110. // Drop the new tab to the left or right depending on where the target tab is being hovered.
  1111. if (hover_now != -1) {
  1112. Rect2 tab_rect = get_tab_rect(hover_now);
  1113. if (is_layout_rtl() ^ (p_point.x > tab_rect.position.x + tab_rect.size.width / 2)) {
  1114. hover_now += 1;
  1115. }
  1116. } else {
  1117. hover_now = tabs.is_empty() || (is_layout_rtl() ^ (p_point.x < get_tab_rect(0).position.x)) ? 0 : get_tab_count();
  1118. }
  1119. p_move_tab_from_other_callback.call(from_tabs, tab_from_id, hover_now);
  1120. }
  1121. }
  1122. }
  1123. }
  1124. void TabBar::_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index) {
  1125. Tab moving_tab = p_from_tabbar->tabs[p_from_index];
  1126. p_from_tabbar->remove_tab(p_from_index);
  1127. tabs.insert(p_to_index, moving_tab);
  1128. if (tabs.size() > 1) {
  1129. if (current >= p_to_index) {
  1130. current++;
  1131. }
  1132. if (previous >= p_to_index) {
  1133. previous++;
  1134. }
  1135. }
  1136. if (!is_tab_disabled(p_to_index)) {
  1137. set_current_tab(p_to_index);
  1138. } else {
  1139. _update_cache();
  1140. queue_redraw();
  1141. }
  1142. update_minimum_size();
  1143. }
  1144. int TabBar::get_tab_idx_at_point(const Point2 &p_point) const {
  1145. int hover_now = -1;
  1146. if (!tabs.is_empty()) {
  1147. for (int i = offset; i <= max_drawn_tab; i++) {
  1148. Rect2 rect = get_tab_rect(i);
  1149. if (rect.has_point(p_point)) {
  1150. hover_now = i;
  1151. }
  1152. }
  1153. }
  1154. return hover_now;
  1155. }
  1156. void TabBar::set_tab_alignment(AlignmentMode p_alignment) {
  1157. ERR_FAIL_INDEX(p_alignment, ALIGNMENT_MAX);
  1158. if (tab_alignment == p_alignment) {
  1159. return;
  1160. }
  1161. tab_alignment = p_alignment;
  1162. _update_cache();
  1163. queue_redraw();
  1164. }
  1165. TabBar::AlignmentMode TabBar::get_tab_alignment() const {
  1166. return tab_alignment;
  1167. }
  1168. void TabBar::set_clip_tabs(bool p_clip_tabs) {
  1169. if (clip_tabs == p_clip_tabs) {
  1170. return;
  1171. }
  1172. clip_tabs = p_clip_tabs;
  1173. if (!clip_tabs) {
  1174. offset = 0;
  1175. max_drawn_tab = 0;
  1176. }
  1177. _update_cache();
  1178. if (scroll_to_selected) {
  1179. ensure_tab_visible(current);
  1180. }
  1181. queue_redraw();
  1182. update_minimum_size();
  1183. }
  1184. bool TabBar::get_clip_tabs() const {
  1185. return clip_tabs;
  1186. }
  1187. void TabBar::set_tab_style_v_flip(bool p_tab_style_v_flip) {
  1188. tab_style_v_flip = p_tab_style_v_flip;
  1189. }
  1190. void TabBar::move_tab(int p_from, int p_to) {
  1191. if (p_from == p_to) {
  1192. return;
  1193. }
  1194. ERR_FAIL_INDEX(p_from, tabs.size());
  1195. ERR_FAIL_INDEX(p_to, tabs.size());
  1196. Tab tab_from = tabs[p_from];
  1197. tabs.remove_at(p_from);
  1198. tabs.insert(p_to, tab_from);
  1199. if (current == p_from) {
  1200. current = p_to;
  1201. } else if (current > p_from && current <= p_to) {
  1202. current--;
  1203. } else if (current < p_from && current >= p_to) {
  1204. current++;
  1205. }
  1206. if (previous == p_from) {
  1207. previous = p_to;
  1208. } else if (previous > p_from && previous <= p_to) {
  1209. previous--;
  1210. } else if (previous < p_from && previous >= p_to) {
  1211. previous++;
  1212. }
  1213. _update_cache();
  1214. _ensure_no_over_offset();
  1215. if (scroll_to_selected) {
  1216. ensure_tab_visible(current);
  1217. }
  1218. queue_redraw();
  1219. notify_property_list_changed();
  1220. }
  1221. int TabBar::get_tab_width(int p_idx) const {
  1222. ERR_FAIL_INDEX_V(p_idx, tabs.size(), 0);
  1223. Ref<StyleBox> style;
  1224. if (tabs[p_idx].disabled) {
  1225. style = theme_cache.tab_disabled_style;
  1226. } else if (current == p_idx) {
  1227. style = theme_cache.tab_selected_style;
  1228. // Always pick the widest style between hovered and unselected, to avoid an infinite loop when switching tabs with the mouse.
  1229. } else if (theme_cache.tab_hovered_style->get_minimum_size().width > theme_cache.tab_unselected_style->get_minimum_size().width) {
  1230. style = theme_cache.tab_hovered_style;
  1231. } else {
  1232. style = theme_cache.tab_unselected_style;
  1233. }
  1234. int x = style->get_minimum_size().width;
  1235. if (tabs[p_idx].icon.is_valid()) {
  1236. const Size2 icon_size = _get_tab_icon_size(p_idx);
  1237. x += icon_size.width + theme_cache.h_separation;
  1238. }
  1239. if (!tabs[p_idx].text.is_empty()) {
  1240. x += tabs[p_idx].size_text + theme_cache.h_separation;
  1241. }
  1242. bool close_visible = cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx == current);
  1243. if (tabs[p_idx].right_button.is_valid()) {
  1244. Ref<StyleBox> btn_style = theme_cache.button_hl_style;
  1245. Ref<Texture2D> rb = tabs[p_idx].right_button;
  1246. if (close_visible) {
  1247. x += btn_style->get_minimum_size().width + rb->get_width();
  1248. } else {
  1249. x += btn_style->get_margin(SIDE_LEFT) + rb->get_width() + theme_cache.h_separation;
  1250. }
  1251. }
  1252. if (close_visible) {
  1253. Ref<StyleBox> btn_style = theme_cache.button_hl_style;
  1254. Ref<Texture2D> cb = theme_cache.close_icon;
  1255. x += btn_style->get_margin(SIDE_LEFT) + cb->get_width() + theme_cache.h_separation;
  1256. }
  1257. if (x > style->get_minimum_size().width) {
  1258. x -= theme_cache.h_separation;
  1259. }
  1260. return x;
  1261. }
  1262. Size2 TabBar::_get_tab_icon_size(int p_index) const {
  1263. ERR_FAIL_INDEX_V(p_index, tabs.size(), Size2());
  1264. const TabBar::Tab &tab = tabs[p_index];
  1265. Size2 icon_size = tab.icon->get_size();
  1266. int icon_max_width = 0;
  1267. if (theme_cache.icon_max_width > 0) {
  1268. icon_max_width = theme_cache.icon_max_width;
  1269. }
  1270. if (tab.icon_max_width > 0 && (icon_max_width == 0 || tab.icon_max_width < icon_max_width)) {
  1271. icon_max_width = tab.icon_max_width;
  1272. }
  1273. if (icon_max_width > 0 && icon_size.width > icon_max_width) {
  1274. icon_size.height = icon_size.height * icon_max_width / icon_size.width;
  1275. icon_size.width = icon_max_width;
  1276. }
  1277. return icon_size;
  1278. }
  1279. void TabBar::_ensure_no_over_offset() {
  1280. if (!is_inside_tree() || !buttons_visible) {
  1281. return;
  1282. }
  1283. int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  1284. int prev_offset = offset;
  1285. int total_w = tabs[max_drawn_tab].ofs_cache + tabs[max_drawn_tab].size_cache - tabs[offset].ofs_cache;
  1286. for (int i = offset; i > 0; i--) {
  1287. if (tabs[i - 1].hidden) {
  1288. continue;
  1289. }
  1290. total_w += tabs[i - 1].size_cache;
  1291. if (total_w < limit_minus_buttons) {
  1292. offset--;
  1293. } else {
  1294. break;
  1295. }
  1296. }
  1297. if (prev_offset != offset) {
  1298. _update_cache();
  1299. queue_redraw();
  1300. }
  1301. }
  1302. bool TabBar::_can_deselect() const {
  1303. if (deselect_enabled) {
  1304. return true;
  1305. }
  1306. // All tabs must be disabled or hidden.
  1307. for (const Tab &tab : tabs) {
  1308. if (!tab.disabled && !tab.hidden) {
  1309. return false;
  1310. }
  1311. }
  1312. return true;
  1313. }
  1314. void TabBar::ensure_tab_visible(int p_idx) {
  1315. if (p_idx == -1 || !is_inside_tree() || !buttons_visible) {
  1316. return;
  1317. }
  1318. ERR_FAIL_INDEX(p_idx, tabs.size());
  1319. if (tabs[p_idx].hidden || (p_idx >= offset && p_idx <= max_drawn_tab)) {
  1320. return;
  1321. }
  1322. if (p_idx < offset) {
  1323. offset = p_idx;
  1324. _update_cache();
  1325. queue_redraw();
  1326. return;
  1327. }
  1328. int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  1329. int total_w = tabs[max_drawn_tab].ofs_cache - tabs[offset].ofs_cache;
  1330. for (int i = max_drawn_tab; i <= p_idx; i++) {
  1331. if (tabs[i].hidden) {
  1332. continue;
  1333. }
  1334. total_w += tabs[i].size_cache;
  1335. }
  1336. int prev_offset = offset;
  1337. for (int i = offset; i < p_idx; i++) {
  1338. if (tabs[i].hidden) {
  1339. continue;
  1340. }
  1341. if (total_w > limit_minus_buttons) {
  1342. total_w -= tabs[i].size_cache;
  1343. offset++;
  1344. } else {
  1345. break;
  1346. }
  1347. }
  1348. if (prev_offset != offset) {
  1349. _update_cache();
  1350. queue_redraw();
  1351. }
  1352. }
  1353. Rect2 TabBar::get_tab_rect(int p_tab) const {
  1354. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Rect2());
  1355. if (is_layout_rtl()) {
  1356. return Rect2(get_size().width - tabs[p_tab].ofs_cache - tabs[p_tab].size_cache, 0, tabs[p_tab].size_cache, get_size().height);
  1357. } else {
  1358. return Rect2(tabs[p_tab].ofs_cache, 0, tabs[p_tab].size_cache, get_size().height);
  1359. }
  1360. }
  1361. void TabBar::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) {
  1362. ERR_FAIL_INDEX(p_policy, CLOSE_BUTTON_MAX);
  1363. if (cb_displaypolicy == p_policy) {
  1364. return;
  1365. }
  1366. cb_displaypolicy = p_policy;
  1367. _update_cache();
  1368. _ensure_no_over_offset();
  1369. if (scroll_to_selected) {
  1370. ensure_tab_visible(current);
  1371. }
  1372. queue_redraw();
  1373. update_minimum_size();
  1374. }
  1375. TabBar::CloseButtonDisplayPolicy TabBar::get_tab_close_display_policy() const {
  1376. return cb_displaypolicy;
  1377. }
  1378. void TabBar::set_max_tab_width(int p_width) {
  1379. ERR_FAIL_COND(p_width < 0);
  1380. if (max_width == p_width) {
  1381. return;
  1382. }
  1383. max_width = p_width;
  1384. _update_cache();
  1385. _ensure_no_over_offset();
  1386. if (scroll_to_selected) {
  1387. ensure_tab_visible(current);
  1388. }
  1389. queue_redraw();
  1390. update_minimum_size();
  1391. }
  1392. int TabBar::get_max_tab_width() const {
  1393. return max_width;
  1394. }
  1395. void TabBar::set_scrolling_enabled(bool p_enabled) {
  1396. scrolling_enabled = p_enabled;
  1397. }
  1398. bool TabBar::get_scrolling_enabled() const {
  1399. return scrolling_enabled;
  1400. }
  1401. void TabBar::set_drag_to_rearrange_enabled(bool p_enabled) {
  1402. drag_to_rearrange_enabled = p_enabled;
  1403. }
  1404. bool TabBar::get_drag_to_rearrange_enabled() const {
  1405. return drag_to_rearrange_enabled;
  1406. }
  1407. void TabBar::set_tabs_rearrange_group(int p_group_id) {
  1408. tabs_rearrange_group = p_group_id;
  1409. }
  1410. int TabBar::get_tabs_rearrange_group() const {
  1411. return tabs_rearrange_group;
  1412. }
  1413. void TabBar::set_scroll_to_selected(bool p_enabled) {
  1414. scroll_to_selected = p_enabled;
  1415. if (p_enabled) {
  1416. ensure_tab_visible(current);
  1417. }
  1418. }
  1419. bool TabBar::get_scroll_to_selected() const {
  1420. return scroll_to_selected;
  1421. }
  1422. void TabBar::set_select_with_rmb(bool p_enabled) {
  1423. select_with_rmb = p_enabled;
  1424. }
  1425. bool TabBar::get_select_with_rmb() const {
  1426. return select_with_rmb;
  1427. }
  1428. void TabBar::set_deselect_enabled(bool p_enabled) {
  1429. if (deselect_enabled == p_enabled) {
  1430. return;
  1431. }
  1432. deselect_enabled = p_enabled;
  1433. if (!deselect_enabled && current == -1 && !tabs.is_empty()) {
  1434. select_next_available();
  1435. }
  1436. }
  1437. bool TabBar::get_deselect_enabled() const {
  1438. return deselect_enabled;
  1439. }
  1440. void TabBar::_bind_methods() {
  1441. ClassDB::bind_method(D_METHOD("set_tab_count", "count"), &TabBar::set_tab_count);
  1442. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabBar::get_tab_count);
  1443. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabBar::set_current_tab);
  1444. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabBar::get_current_tab);
  1445. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabBar::get_previous_tab);
  1446. ClassDB::bind_method(D_METHOD("select_previous_available"), &TabBar::select_previous_available);
  1447. ClassDB::bind_method(D_METHOD("select_next_available"), &TabBar::select_next_available);
  1448. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabBar::set_tab_title);
  1449. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabBar::get_tab_title);
  1450. ClassDB::bind_method(D_METHOD("set_tab_tooltip", "tab_idx", "tooltip"), &TabBar::set_tab_tooltip);
  1451. ClassDB::bind_method(D_METHOD("get_tab_tooltip", "tab_idx"), &TabBar::get_tab_tooltip);
  1452. ClassDB::bind_method(D_METHOD("set_tab_text_direction", "tab_idx", "direction"), &TabBar::set_tab_text_direction);
  1453. ClassDB::bind_method(D_METHOD("get_tab_text_direction", "tab_idx"), &TabBar::get_tab_text_direction);
  1454. ClassDB::bind_method(D_METHOD("set_tab_language", "tab_idx", "language"), &TabBar::set_tab_language);
  1455. ClassDB::bind_method(D_METHOD("get_tab_language", "tab_idx"), &TabBar::get_tab_language);
  1456. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabBar::set_tab_icon);
  1457. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabBar::get_tab_icon);
  1458. ClassDB::bind_method(D_METHOD("set_tab_icon_max_width", "tab_idx", "width"), &TabBar::set_tab_icon_max_width);
  1459. ClassDB::bind_method(D_METHOD("get_tab_icon_max_width", "tab_idx"), &TabBar::get_tab_icon_max_width);
  1460. ClassDB::bind_method(D_METHOD("set_tab_button_icon", "tab_idx", "icon"), &TabBar::set_tab_button_icon);
  1461. ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &TabBar::get_tab_button_icon);
  1462. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabBar::set_tab_disabled);
  1463. ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabBar::is_tab_disabled);
  1464. ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabBar::set_tab_hidden);
  1465. ClassDB::bind_method(D_METHOD("is_tab_hidden", "tab_idx"), &TabBar::is_tab_hidden);
  1466. ClassDB::bind_method(D_METHOD("set_tab_metadata", "tab_idx", "metadata"), &TabBar::set_tab_metadata);
  1467. ClassDB::bind_method(D_METHOD("get_tab_metadata", "tab_idx"), &TabBar::get_tab_metadata);
  1468. ClassDB::bind_method(D_METHOD("remove_tab", "tab_idx"), &TabBar::remove_tab);
  1469. ClassDB::bind_method(D_METHOD("add_tab", "title", "icon"), &TabBar::add_tab, DEFVAL(""), DEFVAL(Ref<Texture2D>()));
  1470. ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabBar::get_tab_idx_at_point);
  1471. ClassDB::bind_method(D_METHOD("set_tab_alignment", "alignment"), &TabBar::set_tab_alignment);
  1472. ClassDB::bind_method(D_METHOD("get_tab_alignment"), &TabBar::get_tab_alignment);
  1473. ClassDB::bind_method(D_METHOD("set_clip_tabs", "clip_tabs"), &TabBar::set_clip_tabs);
  1474. ClassDB::bind_method(D_METHOD("get_clip_tabs"), &TabBar::get_clip_tabs);
  1475. ClassDB::bind_method(D_METHOD("get_tab_offset"), &TabBar::get_tab_offset);
  1476. ClassDB::bind_method(D_METHOD("get_offset_buttons_visible"), &TabBar::get_offset_buttons_visible);
  1477. ClassDB::bind_method(D_METHOD("ensure_tab_visible", "idx"), &TabBar::ensure_tab_visible);
  1478. ClassDB::bind_method(D_METHOD("get_tab_rect", "tab_idx"), &TabBar::get_tab_rect);
  1479. ClassDB::bind_method(D_METHOD("move_tab", "from", "to"), &TabBar::move_tab);
  1480. ClassDB::bind_method(D_METHOD("set_tab_close_display_policy", "policy"), &TabBar::set_tab_close_display_policy);
  1481. ClassDB::bind_method(D_METHOD("get_tab_close_display_policy"), &TabBar::get_tab_close_display_policy);
  1482. ClassDB::bind_method(D_METHOD("set_max_tab_width", "width"), &TabBar::set_max_tab_width);
  1483. ClassDB::bind_method(D_METHOD("get_max_tab_width"), &TabBar::get_max_tab_width);
  1484. ClassDB::bind_method(D_METHOD("set_scrolling_enabled", "enabled"), &TabBar::set_scrolling_enabled);
  1485. ClassDB::bind_method(D_METHOD("get_scrolling_enabled"), &TabBar::get_scrolling_enabled);
  1486. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabBar::set_drag_to_rearrange_enabled);
  1487. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabBar::get_drag_to_rearrange_enabled);
  1488. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabBar::set_tabs_rearrange_group);
  1489. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabBar::get_tabs_rearrange_group);
  1490. ClassDB::bind_method(D_METHOD("set_scroll_to_selected", "enabled"), &TabBar::set_scroll_to_selected);
  1491. ClassDB::bind_method(D_METHOD("get_scroll_to_selected"), &TabBar::get_scroll_to_selected);
  1492. ClassDB::bind_method(D_METHOD("set_select_with_rmb", "enabled"), &TabBar::set_select_with_rmb);
  1493. ClassDB::bind_method(D_METHOD("get_select_with_rmb"), &TabBar::get_select_with_rmb);
  1494. ClassDB::bind_method(D_METHOD("set_deselect_enabled", "enabled"), &TabBar::set_deselect_enabled);
  1495. ClassDB::bind_method(D_METHOD("get_deselect_enabled"), &TabBar::get_deselect_enabled);
  1496. ClassDB::bind_method(D_METHOD("clear_tabs"), &TabBar::clear_tabs);
  1497. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  1498. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  1499. ADD_SIGNAL(MethodInfo("tab_clicked", PropertyInfo(Variant::INT, "tab")));
  1500. ADD_SIGNAL(MethodInfo("tab_rmb_clicked", PropertyInfo(Variant::INT, "tab")));
  1501. ADD_SIGNAL(MethodInfo("tab_close_pressed", PropertyInfo(Variant::INT, "tab")));
  1502. ADD_SIGNAL(MethodInfo("tab_button_pressed", PropertyInfo(Variant::INT, "tab")));
  1503. ADD_SIGNAL(MethodInfo("tab_hovered", PropertyInfo(Variant::INT, "tab")));
  1504. ADD_SIGNAL(MethodInfo("active_tab_rearranged", PropertyInfo(Variant::INT, "idx_to")));
  1505. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1"), "set_current_tab", "get_current_tab");
  1506. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_alignment", "get_tab_alignment");
  1507. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_tabs"), "set_clip_tabs", "get_clip_tabs");
  1508. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_close_display_policy", PROPERTY_HINT_ENUM, "Show Never,Show Active Only,Show Always"), "set_tab_close_display_policy", "get_tab_close_display_policy");
  1509. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_tab_width", PROPERTY_HINT_RANGE, "0,99999,1,suffix:px"), "set_max_tab_width", "get_max_tab_width");
  1510. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrolling_enabled"), "set_scrolling_enabled", "get_scrolling_enabled");
  1511. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  1512. ADD_PROPERTY(PropertyInfo(Variant::INT, "tabs_rearrange_group"), "set_tabs_rearrange_group", "get_tabs_rearrange_group");
  1513. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_to_selected"), "set_scroll_to_selected", "get_scroll_to_selected");
  1514. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_with_rmb"), "set_select_with_rmb", "get_select_with_rmb");
  1515. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_enabled"), "set_deselect_enabled", "get_deselect_enabled");
  1516. ADD_ARRAY_COUNT("Tabs", "tab_count", "set_tab_count", "get_tab_count", "tab_");
  1517. BIND_ENUM_CONSTANT(ALIGNMENT_LEFT);
  1518. BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
  1519. BIND_ENUM_CONSTANT(ALIGNMENT_RIGHT);
  1520. BIND_ENUM_CONSTANT(ALIGNMENT_MAX);
  1521. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_NEVER);
  1522. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ACTIVE_ONLY);
  1523. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ALWAYS);
  1524. BIND_ENUM_CONSTANT(CLOSE_BUTTON_MAX);
  1525. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, h_separation);
  1526. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, icon_max_width);
  1527. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_unselected_style, "tab_unselected");
  1528. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_hovered_style, "tab_hovered");
  1529. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_selected_style, "tab_selected");
  1530. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_disabled_style, "tab_disabled");
  1531. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_focus_style, "tab_focus");
  1532. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, increment_icon, "increment");
  1533. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, increment_hl_icon, "increment_highlight");
  1534. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, decrement_icon, "decrement");
  1535. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, decrement_hl_icon, "decrement_highlight");
  1536. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, drop_mark_icon, "drop_mark");
  1537. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, drop_mark_color);
  1538. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_selected_color);
  1539. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_hovered_color);
  1540. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_unselected_color);
  1541. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_disabled_color);
  1542. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_outline_color);
  1543. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, TabBar, font);
  1544. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, TabBar, font_size);
  1545. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, outline_size);
  1546. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, close_icon, "close");
  1547. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, button_pressed_style, "button_pressed");
  1548. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, button_hl_style, "button_highlight");
  1549. Tab defaults(true);
  1550. base_property_helper.set_prefix("tab_");
  1551. base_property_helper.set_array_length_getter(&TabBar::get_tab_count);
  1552. base_property_helper.register_property(PropertyInfo(Variant::STRING, "title"), defaults.text, &TabBar::set_tab_title, &TabBar::get_tab_title);
  1553. base_property_helper.register_property(PropertyInfo(Variant::STRING, "tooltip"), defaults.tooltip, &TabBar::set_tab_tooltip, &TabBar::get_tab_tooltip);
  1554. base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &TabBar::set_tab_icon, &TabBar::get_tab_icon);
  1555. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &TabBar::set_tab_disabled, &TabBar::is_tab_disabled);
  1556. PropertyListHelper::register_base_helper(&base_property_helper);
  1557. }
  1558. TabBar::TabBar() {
  1559. set_size(Size2(get_size().width, get_minimum_size().height));
  1560. set_focus_mode(FOCUS_ALL);
  1561. connect(SceneStringName(mouse_exited), callable_mp(this, &TabBar::_on_mouse_exited));
  1562. property_helper.setup_for_instance(base_property_helper, this);
  1563. }