tab_bar.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  1. /*************************************************************************/
  2. /* tab_bar.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/object/message_queue.h"
  32. #include "core/string/translation.h"
  33. #include "scene/gui/box_container.h"
  34. #include "scene/gui/label.h"
  35. #include "scene/gui/texture_rect.h"
  36. #include "scene/main/viewport.h"
  37. Size2 TabBar::get_minimum_size() const {
  38. Size2 ms;
  39. if (tabs.is_empty()) {
  40. return ms;
  41. }
  42. Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
  43. Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
  44. Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
  45. Ref<StyleBox> button_highlight = get_theme_stylebox(SNAME("button_highlight"));
  46. Ref<Texture2D> close = get_theme_icon(SNAME("close"));
  47. int hseparation = get_theme_constant(SNAME("h_separation"));
  48. int y_margin = MAX(MAX(tab_unselected->get_minimum_size().height, tab_selected->get_minimum_size().height), tab_disabled->get_minimum_size().height);
  49. for (int i = 0; i < tabs.size(); i++) {
  50. if (tabs[i].hidden) {
  51. continue;
  52. }
  53. int ofs = ms.width;
  54. Ref<StyleBox> style;
  55. if (tabs[i].disabled) {
  56. style = tab_disabled;
  57. } else if (current == i) {
  58. style = tab_selected;
  59. } else {
  60. style = tab_unselected;
  61. }
  62. ms.width += style->get_minimum_size().width;
  63. Ref<Texture2D> tex = tabs[i].icon;
  64. if (tex.is_valid()) {
  65. ms.height = MAX(ms.height, tex->get_size().height + y_margin);
  66. ms.width += tex->get_size().width + hseparation;
  67. }
  68. if (!tabs[i].text.is_empty()) {
  69. ms.width += tabs[i].size_text + hseparation;
  70. }
  71. ms.height = MAX(ms.height, tabs[i].text_buf->get_size().y + y_margin);
  72. bool close_visible = cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current);
  73. if (tabs[i].right_button.is_valid()) {
  74. Ref<Texture2D> rb = tabs[i].right_button;
  75. if (close_visible) {
  76. ms.width += button_highlight->get_minimum_size().width + rb->get_width();
  77. } else {
  78. ms.width += button_highlight->get_margin(SIDE_LEFT) + rb->get_width() + hseparation;
  79. }
  80. ms.height = MAX(ms.height, rb->get_height() + y_margin);
  81. }
  82. if (close_visible) {
  83. ms.width += button_highlight->get_margin(SIDE_LEFT) + close->get_width() + hseparation;
  84. ms.height = MAX(ms.height, close->get_height() + y_margin);
  85. }
  86. if (ms.width - ofs > style->get_minimum_size().width) {
  87. ms.width -= hseparation;
  88. }
  89. }
  90. if (clip_tabs) {
  91. ms.width = 0;
  92. }
  93. return ms;
  94. }
  95. void TabBar::gui_input(const Ref<InputEvent> &p_event) {
  96. ERR_FAIL_COND(p_event.is_null());
  97. Ref<InputEventMouseMotion> mm = p_event;
  98. if (mm.is_valid()) {
  99. Point2 pos = mm->get_position();
  100. if (buttons_visible) {
  101. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  102. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  103. if (is_layout_rtl()) {
  104. if (pos.x < decr->get_width()) {
  105. if (highlight_arrow != 1) {
  106. highlight_arrow = 1;
  107. queue_redraw();
  108. }
  109. } else if (pos.x < incr->get_width() + decr->get_width()) {
  110. if (highlight_arrow != 0) {
  111. highlight_arrow = 0;
  112. queue_redraw();
  113. }
  114. } else if (highlight_arrow != -1) {
  115. highlight_arrow = -1;
  116. queue_redraw();
  117. }
  118. } else {
  119. int limit_minus_buttons = get_size().width - incr->get_width() - decr->get_width();
  120. if (pos.x > limit_minus_buttons + decr->get_width()) {
  121. if (highlight_arrow != 1) {
  122. highlight_arrow = 1;
  123. queue_redraw();
  124. }
  125. } else if (pos.x > limit_minus_buttons) {
  126. if (highlight_arrow != 0) {
  127. highlight_arrow = 0;
  128. queue_redraw();
  129. }
  130. } else if (highlight_arrow != -1) {
  131. highlight_arrow = -1;
  132. queue_redraw();
  133. }
  134. }
  135. }
  136. if (get_viewport()->gui_is_dragging() && can_drop_data(pos, get_viewport()->gui_get_drag_data())) {
  137. dragging_valid_tab = true;
  138. queue_redraw();
  139. }
  140. _update_hover();
  141. return;
  142. }
  143. Ref<InputEventMouseButton> mb = p_event;
  144. if (mb.is_valid()) {
  145. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP && !mb->is_command_pressed()) {
  146. if (scrolling_enabled && buttons_visible) {
  147. if (offset > 0) {
  148. offset--;
  149. _update_cache();
  150. queue_redraw();
  151. }
  152. }
  153. }
  154. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN && !mb->is_command_pressed()) {
  155. if (scrolling_enabled && buttons_visible) {
  156. if (missing_right && offset < tabs.size()) {
  157. offset++;
  158. _update_cache();
  159. queue_redraw();
  160. }
  161. }
  162. }
  163. if (rb_pressing && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  164. if (rb_hover != -1) {
  165. emit_signal(SNAME("tab_button_pressed"), rb_hover);
  166. }
  167. rb_pressing = false;
  168. queue_redraw();
  169. }
  170. if (cb_pressing && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  171. if (cb_hover != -1) {
  172. emit_signal(SNAME("tab_close_pressed"), cb_hover);
  173. }
  174. cb_pressing = false;
  175. queue_redraw();
  176. }
  177. if (mb->is_pressed() && (mb->get_button_index() == MouseButton::LEFT || (select_with_rmb && mb->get_button_index() == MouseButton::RIGHT))) {
  178. Point2 pos = mb->get_position();
  179. if (buttons_visible) {
  180. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  181. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  182. if (is_layout_rtl()) {
  183. if (pos.x < decr->get_width()) {
  184. if (missing_right) {
  185. offset++;
  186. _update_cache();
  187. queue_redraw();
  188. }
  189. return;
  190. } else if (pos.x < incr->get_width() + decr->get_width()) {
  191. if (offset > 0) {
  192. offset--;
  193. _update_cache();
  194. queue_redraw();
  195. }
  196. return;
  197. }
  198. } else {
  199. int limit = get_size().width - incr->get_width() - decr->get_width();
  200. if (pos.x > limit + decr->get_width()) {
  201. if (missing_right) {
  202. offset++;
  203. _update_cache();
  204. queue_redraw();
  205. }
  206. return;
  207. } else if (pos.x > limit) {
  208. if (offset > 0) {
  209. offset--;
  210. _update_cache();
  211. queue_redraw();
  212. }
  213. return;
  214. }
  215. }
  216. }
  217. if (tabs.is_empty()) {
  218. // Return early if there are no actual tabs to handle input for.
  219. return;
  220. }
  221. int found = -1;
  222. for (int i = offset; i <= max_drawn_tab; i++) {
  223. if (tabs[i].hidden) {
  224. continue;
  225. }
  226. if (tabs[i].rb_rect.has_point(pos)) {
  227. rb_pressing = true;
  228. queue_redraw();
  229. return;
  230. }
  231. if (tabs[i].cb_rect.has_point(pos) && (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current))) {
  232. cb_pressing = true;
  233. queue_redraw();
  234. return;
  235. }
  236. if (pos.x >= get_tab_rect(i).position.x && pos.x < get_tab_rect(i).position.x + tabs[i].size_cache) {
  237. if (!tabs[i].disabled) {
  238. found = i;
  239. }
  240. break;
  241. }
  242. }
  243. if (found != -1) {
  244. set_current_tab(found);
  245. if (mb->get_button_index() == MouseButton::RIGHT) {
  246. // Right mouse button clicked.
  247. emit_signal(SNAME("tab_rmb_clicked"), found);
  248. }
  249. emit_signal(SNAME("tab_clicked"), found);
  250. }
  251. }
  252. }
  253. }
  254. void TabBar::_shape(int p_tab) {
  255. Ref<Font> font = get_theme_font(SNAME("font"));
  256. int font_size = get_theme_font_size(SNAME("font_size"));
  257. tabs.write[p_tab].xl_text = atr(tabs[p_tab].text);
  258. tabs.write[p_tab].text_buf->clear();
  259. tabs.write[p_tab].text_buf->set_width(-1);
  260. if (tabs[p_tab].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  261. tabs.write[p_tab].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  262. } else {
  263. tabs.write[p_tab].text_buf->set_direction((TextServer::Direction)tabs[p_tab].text_direction);
  264. }
  265. tabs.write[p_tab].text_buf->add_string(tabs[p_tab].xl_text, font, font_size, tabs[p_tab].language);
  266. }
  267. void TabBar::_notification(int p_what) {
  268. switch (p_what) {
  269. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  270. queue_redraw();
  271. } break;
  272. case NOTIFICATION_THEME_CHANGED:
  273. case NOTIFICATION_TRANSLATION_CHANGED: {
  274. for (int i = 0; i < tabs.size(); ++i) {
  275. _shape(i);
  276. }
  277. [[fallthrough]];
  278. }
  279. case NOTIFICATION_RESIZED: {
  280. int ofs_old = offset;
  281. int max_old = max_drawn_tab;
  282. _update_cache();
  283. _ensure_no_over_offset();
  284. if (scroll_to_selected && (offset != ofs_old || max_drawn_tab != max_old)) {
  285. ensure_tab_visible(current);
  286. }
  287. } break;
  288. case NOTIFICATION_DRAG_END: {
  289. if (dragging_valid_tab) {
  290. dragging_valid_tab = false;
  291. queue_redraw();
  292. }
  293. } break;
  294. case NOTIFICATION_DRAW: {
  295. if (tabs.is_empty()) {
  296. return;
  297. }
  298. Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
  299. Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
  300. Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
  301. Color font_selected_color = get_theme_color(SNAME("font_selected_color"));
  302. Color font_unselected_color = get_theme_color(SNAME("font_unselected_color"));
  303. Color font_disabled_color = get_theme_color(SNAME("font_disabled_color"));
  304. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  305. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  306. bool rtl = is_layout_rtl();
  307. Vector2 size = get_size();
  308. int limit_minus_buttons = size.width - incr->get_width() - decr->get_width();
  309. int ofs = tabs[offset].ofs_cache;
  310. // Draw unselected tabs in the back.
  311. for (int i = offset; i <= max_drawn_tab; i++) {
  312. if (tabs[i].hidden) {
  313. continue;
  314. }
  315. if (i != current) {
  316. Ref<StyleBox> sb;
  317. Color col;
  318. if (tabs[i].disabled) {
  319. sb = tab_disabled;
  320. col = font_disabled_color;
  321. } else if (i == current) {
  322. sb = tab_selected;
  323. col = font_selected_color;
  324. } else {
  325. sb = tab_unselected;
  326. col = font_unselected_color;
  327. }
  328. _draw_tab(sb, col, i, rtl ? size.width - ofs - tabs[i].size_cache : ofs);
  329. }
  330. ofs += tabs[i].size_cache;
  331. }
  332. // Draw selected tab in the front, but only if it's visible.
  333. if (current >= offset && current <= max_drawn_tab && !tabs[current].hidden) {
  334. Ref<StyleBox> sb = tabs[current].disabled ? tab_disabled : tab_selected;
  335. float x = rtl ? size.width - tabs[current].ofs_cache - tabs[current].size_cache : tabs[current].ofs_cache;
  336. _draw_tab(sb, font_selected_color, current, x);
  337. }
  338. if (buttons_visible) {
  339. Ref<Texture2D> incr_hl = get_theme_icon(SNAME("increment_highlight"));
  340. Ref<Texture2D> decr_hl = get_theme_icon(SNAME("decrement_highlight"));
  341. int vofs = (size.height - incr->get_size().height) / 2;
  342. if (rtl) {
  343. if (missing_right) {
  344. draw_texture(highlight_arrow == 1 ? decr_hl : decr, Point2(0, vofs));
  345. } else {
  346. draw_texture(decr, Point2(0, vofs), Color(1, 1, 1, 0.5));
  347. }
  348. if (offset > 0) {
  349. draw_texture(highlight_arrow == 0 ? incr_hl : incr, Point2(incr->get_size().width, vofs));
  350. } else {
  351. draw_texture(incr, Point2(incr->get_size().width, vofs), Color(1, 1, 1, 0.5));
  352. }
  353. } else {
  354. if (offset > 0) {
  355. draw_texture(highlight_arrow == 0 ? decr_hl : decr, Point2(limit_minus_buttons, vofs));
  356. } else {
  357. draw_texture(decr, Point2(limit_minus_buttons, vofs), Color(1, 1, 1, 0.5));
  358. }
  359. if (missing_right) {
  360. draw_texture(highlight_arrow == 1 ? incr_hl : incr, Point2(limit_minus_buttons + decr->get_size().width, vofs));
  361. } else {
  362. draw_texture(incr, Point2(limit_minus_buttons + decr->get_size().width, vofs), Color(1, 1, 1, 0.5));
  363. }
  364. }
  365. }
  366. if (dragging_valid_tab) {
  367. int x;
  368. int tab_hover = get_hovered_tab();
  369. if (tab_hover != -1) {
  370. Rect2 tab_rect = get_tab_rect(tab_hover);
  371. x = tab_rect.position.x;
  372. if (get_local_mouse_position().x > x + tab_rect.size.width / 2) {
  373. x += tab_rect.size.width;
  374. }
  375. } else {
  376. if (rtl ^ (get_local_mouse_position().x < get_tab_rect(0).position.x)) {
  377. x = get_tab_rect(0).position.x;
  378. if (rtl) {
  379. x += get_tab_rect(0).size.width;
  380. }
  381. } else {
  382. Rect2 tab_rect = get_tab_rect(get_tab_count() - 1);
  383. x = tab_rect.position.x;
  384. if (!rtl) {
  385. x += tab_rect.size.width;
  386. }
  387. }
  388. }
  389. Ref<Texture2D> drop_mark = get_theme_icon(SNAME("drop_mark"));
  390. Color drop_mark_color = get_theme_color(SNAME("drop_mark_color"));
  391. drop_mark->draw(get_canvas_item(), Point2(x - drop_mark->get_width() / 2, (size.height - drop_mark->get_height()) / 2), drop_mark_color);
  392. }
  393. } break;
  394. }
  395. }
  396. void TabBar::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x) {
  397. RID ci = get_canvas_item();
  398. bool rtl = is_layout_rtl();
  399. Color font_outline_color = get_theme_color(SNAME("font_outline_color"));
  400. int outline_size = get_theme_constant(SNAME("outline_size"));
  401. int hseparation = get_theme_constant(SNAME("h_separation"));
  402. Rect2 sb_rect = Rect2(p_x, 0, tabs[p_index].size_cache, get_size().height);
  403. p_tab_style->draw(ci, sb_rect);
  404. p_x += rtl ? tabs[p_index].size_cache - p_tab_style->get_margin(SIDE_LEFT) : p_tab_style->get_margin(SIDE_LEFT);
  405. Size2i sb_ms = p_tab_style->get_minimum_size();
  406. // Draw the icon.
  407. Ref<Texture2D> icon = tabs[p_index].icon;
  408. if (icon.is_valid()) {
  409. icon->draw(ci, Point2i(rtl ? p_x - icon->get_width() : p_x, p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - icon->get_height()) / 2));
  410. p_x = rtl ? p_x - icon->get_width() - hseparation : p_x + icon->get_width() + hseparation;
  411. }
  412. // Draw the text.
  413. if (!tabs[p_index].text.is_empty()) {
  414. Point2i text_pos = Point2i(rtl ? p_x - tabs[p_index].size_text : p_x,
  415. p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - tabs[p_index].text_buf->get_size().y) / 2);
  416. if (outline_size > 0 && font_outline_color.a > 0) {
  417. tabs[p_index].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  418. }
  419. tabs[p_index].text_buf->draw(ci, text_pos, p_font_color);
  420. p_x = rtl ? p_x - tabs[p_index].size_text - hseparation : p_x + tabs[p_index].size_text + hseparation;
  421. }
  422. // Draw and calculate rect of the right button.
  423. if (tabs[p_index].right_button.is_valid()) {
  424. Ref<StyleBox> style = get_theme_stylebox(SNAME("button_highlight"));
  425. Ref<Texture2D> rb = tabs[p_index].right_button;
  426. Rect2 rb_rect;
  427. rb_rect.size = style->get_minimum_size() + rb->get_size();
  428. rb_rect.position.x = rtl ? p_x - rb_rect.size.width : p_x;
  429. rb_rect.position.y = p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (rb_rect.size.y)) / 2;
  430. tabs.write[p_index].rb_rect = rb_rect;
  431. if (rb_hover == p_index) {
  432. if (rb_pressing) {
  433. get_theme_stylebox(SNAME("button_pressed"))->draw(ci, rb_rect);
  434. } else {
  435. style->draw(ci, rb_rect);
  436. }
  437. }
  438. rb->draw(ci, Point2i(rb_rect.position.x + style->get_margin(SIDE_LEFT), rb_rect.position.y + style->get_margin(SIDE_TOP)));
  439. p_x = rtl ? rb_rect.position.x : rb_rect.position.x + rb_rect.size.width;
  440. }
  441. // Draw and calculate rect of the close button.
  442. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_index == current)) {
  443. Ref<StyleBox> style = get_theme_stylebox(SNAME("button_highlight"));
  444. Ref<Texture2D> cb = get_theme_icon(SNAME("close"));
  445. Rect2 cb_rect;
  446. cb_rect.size = style->get_minimum_size() + cb->get_size();
  447. cb_rect.position.x = rtl ? p_x - cb_rect.size.width : p_x;
  448. cb_rect.position.y = p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (cb_rect.size.y)) / 2;
  449. tabs.write[p_index].cb_rect = cb_rect;
  450. if (!tabs[p_index].disabled && cb_hover == p_index) {
  451. if (cb_pressing) {
  452. get_theme_stylebox(SNAME("button_pressed"))->draw(ci, cb_rect);
  453. } else {
  454. style->draw(ci, cb_rect);
  455. }
  456. }
  457. cb->draw(ci, Point2i(cb_rect.position.x + style->get_margin(SIDE_LEFT), cb_rect.position.y + style->get_margin(SIDE_TOP)));
  458. }
  459. }
  460. void TabBar::set_tab_count(int p_count) {
  461. if (p_count == tabs.size()) {
  462. return;
  463. }
  464. ERR_FAIL_COND(p_count < 0);
  465. tabs.resize(p_count);
  466. if (p_count == 0) {
  467. offset = 0;
  468. max_drawn_tab = 0;
  469. current = 0;
  470. previous = 0;
  471. } else {
  472. offset = MIN(offset, p_count - 1);
  473. max_drawn_tab = MIN(max_drawn_tab, p_count - 1);
  474. current = MIN(current, p_count - 1);
  475. _update_cache();
  476. _ensure_no_over_offset();
  477. if (scroll_to_selected) {
  478. ensure_tab_visible(current);
  479. }
  480. }
  481. queue_redraw();
  482. update_minimum_size();
  483. notify_property_list_changed();
  484. }
  485. int TabBar::get_tab_count() const {
  486. return tabs.size();
  487. }
  488. void TabBar::set_current_tab(int p_current) {
  489. ERR_FAIL_INDEX(p_current, get_tab_count());
  490. previous = current;
  491. current = p_current;
  492. if (current == previous) {
  493. emit_signal(SNAME("tab_selected"), current);
  494. return;
  495. }
  496. emit_signal(SNAME("tab_selected"), current);
  497. _update_cache();
  498. if (scroll_to_selected) {
  499. ensure_tab_visible(current);
  500. }
  501. queue_redraw();
  502. emit_signal(SNAME("tab_changed"), p_current);
  503. }
  504. int TabBar::get_current_tab() const {
  505. return current;
  506. }
  507. int TabBar::get_previous_tab() const {
  508. return previous;
  509. }
  510. int TabBar::get_hovered_tab() const {
  511. return hover;
  512. }
  513. int TabBar::get_tab_offset() const {
  514. return offset;
  515. }
  516. bool TabBar::get_offset_buttons_visible() const {
  517. return buttons_visible;
  518. }
  519. void TabBar::set_tab_title(int p_tab, const String &p_title) {
  520. ERR_FAIL_INDEX(p_tab, tabs.size());
  521. if (tabs[p_tab].text == p_title) {
  522. return;
  523. }
  524. tabs.write[p_tab].text = p_title;
  525. _shape(p_tab);
  526. _update_cache();
  527. _ensure_no_over_offset();
  528. if (scroll_to_selected) {
  529. ensure_tab_visible(current);
  530. }
  531. queue_redraw();
  532. update_minimum_size();
  533. }
  534. String TabBar::get_tab_title(int p_tab) const {
  535. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  536. return tabs[p_tab].text;
  537. }
  538. void TabBar::set_tab_text_direction(int p_tab, Control::TextDirection p_text_direction) {
  539. ERR_FAIL_INDEX(p_tab, tabs.size());
  540. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  541. if (tabs[p_tab].text_direction != p_text_direction) {
  542. tabs.write[p_tab].text_direction = p_text_direction;
  543. _shape(p_tab);
  544. queue_redraw();
  545. }
  546. }
  547. Control::TextDirection TabBar::get_tab_text_direction(int p_tab) const {
  548. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Control::TEXT_DIRECTION_INHERITED);
  549. return tabs[p_tab].text_direction;
  550. }
  551. void TabBar::set_tab_language(int p_tab, const String &p_language) {
  552. ERR_FAIL_INDEX(p_tab, tabs.size());
  553. if (tabs[p_tab].language != p_language) {
  554. tabs.write[p_tab].language = p_language;
  555. _shape(p_tab);
  556. _update_cache();
  557. _ensure_no_over_offset();
  558. if (scroll_to_selected) {
  559. ensure_tab_visible(current);
  560. }
  561. queue_redraw();
  562. update_minimum_size();
  563. }
  564. }
  565. String TabBar::get_tab_language(int p_tab) const {
  566. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  567. return tabs[p_tab].language;
  568. }
  569. void TabBar::set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  570. ERR_FAIL_INDEX(p_tab, tabs.size());
  571. if (tabs[p_tab].icon == p_icon) {
  572. return;
  573. }
  574. tabs.write[p_tab].icon = p_icon;
  575. _update_cache();
  576. _ensure_no_over_offset();
  577. if (scroll_to_selected) {
  578. ensure_tab_visible(current);
  579. }
  580. queue_redraw();
  581. update_minimum_size();
  582. }
  583. Ref<Texture2D> TabBar::get_tab_icon(int p_tab) const {
  584. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
  585. return tabs[p_tab].icon;
  586. }
  587. void TabBar::set_tab_disabled(int p_tab, bool p_disabled) {
  588. ERR_FAIL_INDEX(p_tab, tabs.size());
  589. if (tabs[p_tab].disabled == p_disabled) {
  590. return;
  591. }
  592. tabs.write[p_tab].disabled = p_disabled;
  593. _update_cache();
  594. _ensure_no_over_offset();
  595. if (scroll_to_selected) {
  596. ensure_tab_visible(current);
  597. }
  598. queue_redraw();
  599. update_minimum_size();
  600. }
  601. bool TabBar::is_tab_disabled(int p_tab) const {
  602. ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
  603. return tabs[p_tab].disabled;
  604. }
  605. void TabBar::set_tab_hidden(int p_tab, bool p_hidden) {
  606. ERR_FAIL_INDEX(p_tab, tabs.size());
  607. if (tabs[p_tab].hidden == p_hidden) {
  608. return;
  609. }
  610. tabs.write[p_tab].hidden = p_hidden;
  611. _update_cache();
  612. _ensure_no_over_offset();
  613. if (scroll_to_selected) {
  614. ensure_tab_visible(current);
  615. }
  616. queue_redraw();
  617. update_minimum_size();
  618. }
  619. bool TabBar::is_tab_hidden(int p_tab) const {
  620. ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
  621. return tabs[p_tab].hidden;
  622. }
  623. void TabBar::set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  624. ERR_FAIL_INDEX(p_tab, tabs.size());
  625. if (tabs[p_tab].right_button == p_icon) {
  626. return;
  627. }
  628. tabs.write[p_tab].right_button = p_icon;
  629. _update_cache();
  630. _ensure_no_over_offset();
  631. if (scroll_to_selected) {
  632. ensure_tab_visible(current);
  633. }
  634. queue_redraw();
  635. update_minimum_size();
  636. }
  637. Ref<Texture2D> TabBar::get_tab_button_icon(int p_tab) const {
  638. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
  639. return tabs[p_tab].right_button;
  640. }
  641. void TabBar::_update_hover() {
  642. if (!is_inside_tree()) {
  643. return;
  644. }
  645. ERR_FAIL_COND(tabs.is_empty());
  646. const Point2 &pos = get_local_mouse_position();
  647. // Test hovering to display right or close button.
  648. int hover_now = -1;
  649. int hover_buttons = -1;
  650. for (int i = offset; i <= max_drawn_tab; i++) {
  651. if (tabs[i].hidden) {
  652. continue;
  653. }
  654. Rect2 rect = get_tab_rect(i);
  655. if (rect.has_point(pos)) {
  656. hover_now = i;
  657. }
  658. if (tabs[i].rb_rect.has_point(pos)) {
  659. rb_hover = i;
  660. cb_hover = -1;
  661. hover_buttons = i;
  662. } else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) {
  663. cb_hover = i;
  664. rb_hover = -1;
  665. hover_buttons = i;
  666. }
  667. if (hover_buttons != -1) {
  668. queue_redraw();
  669. break;
  670. }
  671. }
  672. if (hover != hover_now) {
  673. hover = hover_now;
  674. if (hover != -1) {
  675. emit_signal(SNAME("tab_hovered"), hover);
  676. }
  677. }
  678. if (hover_buttons == -1) { // No hover.
  679. int rb_hover_old = rb_hover;
  680. int cb_hover_old = cb_hover;
  681. rb_hover = hover_buttons;
  682. cb_hover = hover_buttons;
  683. if (rb_hover != rb_hover_old || cb_hover != cb_hover_old) {
  684. queue_redraw();
  685. }
  686. }
  687. }
  688. void TabBar::_update_cache() {
  689. if (tabs.is_empty()) {
  690. buttons_visible = false;
  691. return;
  692. }
  693. Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
  694. Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
  695. Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
  696. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  697. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  698. int limit = get_size().width;
  699. int limit_minus_buttons = limit - incr->get_width() - decr->get_width();
  700. int w = 0;
  701. max_drawn_tab = tabs.size() - 1;
  702. for (int i = 0; i < tabs.size(); i++) {
  703. tabs.write[i].text_buf->set_width(-1);
  704. tabs.write[i].size_text = Math::ceil(tabs[i].text_buf->get_size().x);
  705. tabs.write[i].size_cache = get_tab_width(i);
  706. if (max_width > 0 && tabs[i].size_cache > max_width) {
  707. int size_textless = tabs[i].size_cache - tabs[i].size_text;
  708. int mw = MAX(size_textless, max_width);
  709. tabs.write[i].size_text = MAX(mw - size_textless, 1);
  710. tabs.write[i].text_buf->set_width(tabs[i].size_text);
  711. tabs.write[i].size_cache = size_textless + tabs[i].size_text;
  712. }
  713. if (i < offset || i > max_drawn_tab) {
  714. tabs.write[i].ofs_cache = 0;
  715. continue;
  716. }
  717. tabs.write[i].ofs_cache = w;
  718. if (tabs[i].hidden) {
  719. continue;
  720. }
  721. w += tabs[i].size_cache;
  722. // Check if all tabs would fit inside the area.
  723. if (clip_tabs && i > offset && (w > limit || (offset > 0 && w > limit_minus_buttons))) {
  724. tabs.write[i].ofs_cache = 0;
  725. w -= tabs[i].size_cache;
  726. max_drawn_tab = i - 1;
  727. while (w > limit_minus_buttons && max_drawn_tab > offset) {
  728. tabs.write[max_drawn_tab].ofs_cache = 0;
  729. if (!tabs[max_drawn_tab].hidden) {
  730. w -= tabs[max_drawn_tab].size_cache;
  731. }
  732. max_drawn_tab--;
  733. }
  734. }
  735. }
  736. missing_right = max_drawn_tab < tabs.size() - 1;
  737. buttons_visible = offset > 0 || missing_right;
  738. if (tab_alignment == ALIGNMENT_LEFT) {
  739. _update_hover();
  740. return;
  741. }
  742. if (tab_alignment == ALIGNMENT_CENTER) {
  743. w = ((buttons_visible ? limit_minus_buttons : limit) - w) / 2;
  744. } else if (tab_alignment == ALIGNMENT_RIGHT) {
  745. w = (buttons_visible ? limit_minus_buttons : limit) - w;
  746. }
  747. for (int i = offset; i <= max_drawn_tab; i++) {
  748. tabs.write[i].ofs_cache = w;
  749. if (!tabs[i].hidden) {
  750. w += tabs[i].size_cache;
  751. }
  752. }
  753. _update_hover();
  754. }
  755. void TabBar::_on_mouse_exited() {
  756. rb_hover = -1;
  757. cb_hover = -1;
  758. hover = -1;
  759. highlight_arrow = -1;
  760. dragging_valid_tab = false;
  761. queue_redraw();
  762. }
  763. void TabBar::add_tab(const String &p_str, const Ref<Texture2D> &p_icon) {
  764. Tab t;
  765. t.text = p_str;
  766. t.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  767. t.icon = p_icon;
  768. tabs.push_back(t);
  769. _shape(tabs.size() - 1);
  770. _update_cache();
  771. if (scroll_to_selected) {
  772. ensure_tab_visible(current);
  773. }
  774. queue_redraw();
  775. update_minimum_size();
  776. if (tabs.size() == 1 && is_inside_tree()) {
  777. emit_signal(SNAME("tab_changed"), 0);
  778. }
  779. }
  780. void TabBar::clear_tabs() {
  781. if (tabs.is_empty()) {
  782. return;
  783. }
  784. tabs.clear();
  785. offset = 0;
  786. max_drawn_tab = 0;
  787. current = 0;
  788. previous = 0;
  789. queue_redraw();
  790. update_minimum_size();
  791. notify_property_list_changed();
  792. }
  793. void TabBar::remove_tab(int p_idx) {
  794. ERR_FAIL_INDEX(p_idx, tabs.size());
  795. tabs.remove_at(p_idx);
  796. bool is_tab_changing = current == p_idx && !tabs.is_empty();
  797. if (current >= p_idx && current > 0) {
  798. current--;
  799. }
  800. if (tabs.is_empty()) {
  801. offset = 0;
  802. max_drawn_tab = 0;
  803. previous = 0;
  804. } else {
  805. offset = MIN(offset, tabs.size() - 1);
  806. max_drawn_tab = MIN(max_drawn_tab, tabs.size() - 1);
  807. _update_cache();
  808. _ensure_no_over_offset();
  809. if (scroll_to_selected) {
  810. ensure_tab_visible(current);
  811. }
  812. }
  813. queue_redraw();
  814. update_minimum_size();
  815. notify_property_list_changed();
  816. if (is_tab_changing && is_inside_tree()) {
  817. emit_signal(SNAME("tab_changed"), current);
  818. }
  819. }
  820. Variant TabBar::get_drag_data(const Point2 &p_point) {
  821. if (!drag_to_rearrange_enabled) {
  822. return Control::get_drag_data(p_point); // Allow stuff like TabContainer to override it.
  823. }
  824. int tab_over = get_tab_idx_at_point(p_point);
  825. if (tab_over < 0) {
  826. return Variant();
  827. }
  828. HBoxContainer *drag_preview = memnew(HBoxContainer);
  829. if (!tabs[tab_over].icon.is_null()) {
  830. TextureRect *tf = memnew(TextureRect);
  831. tf->set_texture(tabs[tab_over].icon);
  832. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  833. drag_preview->add_child(tf);
  834. }
  835. Label *label = memnew(Label(tabs[tab_over].xl_text));
  836. drag_preview->add_child(label);
  837. set_drag_preview(drag_preview);
  838. Dictionary drag_data;
  839. drag_data["type"] = "tab_element";
  840. drag_data["tab_element"] = tab_over;
  841. drag_data["from_path"] = get_path();
  842. return drag_data;
  843. }
  844. bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  845. if (!drag_to_rearrange_enabled) {
  846. return Control::can_drop_data(p_point, p_data); // Allow stuff like TabContainer to override it.
  847. }
  848. Dictionary d = p_data;
  849. if (!d.has("type")) {
  850. return false;
  851. }
  852. if (String(d["type"]) == "tab_element") {
  853. NodePath from_path = d["from_path"];
  854. NodePath to_path = get_path();
  855. if (from_path == to_path) {
  856. return true;
  857. } else if (get_tabs_rearrange_group() != -1) {
  858. // Drag and drop between other TabBars.
  859. Node *from_node = get_node(from_path);
  860. TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
  861. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  862. return true;
  863. }
  864. }
  865. }
  866. return false;
  867. }
  868. void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) {
  869. if (!drag_to_rearrange_enabled) {
  870. Control::drop_data(p_point, p_data); // Allow stuff like TabContainer to override it.
  871. return;
  872. }
  873. Dictionary d = p_data;
  874. if (!d.has("type")) {
  875. return;
  876. }
  877. if (String(d["type"]) == "tab_element") {
  878. int tab_from_id = d["tab_element"];
  879. int hover_now = get_tab_idx_at_point(p_point);
  880. NodePath from_path = d["from_path"];
  881. NodePath to_path = get_path();
  882. if (from_path == to_path) {
  883. if (tab_from_id == hover_now) {
  884. return;
  885. }
  886. // Drop the new tab to the left or right depending on where the target tab is being hovered.
  887. if (hover_now != -1) {
  888. Rect2 tab_rect = get_tab_rect(hover_now);
  889. if (is_layout_rtl() ^ (p_point.x <= tab_rect.position.x + tab_rect.size.width / 2)) {
  890. if (hover_now > tab_from_id) {
  891. hover_now -= 1;
  892. }
  893. } else if (tab_from_id > hover_now) {
  894. hover_now += 1;
  895. }
  896. } else {
  897. hover_now = is_layout_rtl() ^ (p_point.x < get_tab_rect(0).position.x) ? 0 : get_tab_count() - 1;
  898. }
  899. move_tab(tab_from_id, hover_now);
  900. if (!is_tab_disabled(hover_now)) {
  901. emit_signal(SNAME("active_tab_rearranged"), hover_now);
  902. set_current_tab(hover_now);
  903. }
  904. } else if (get_tabs_rearrange_group() != -1) {
  905. // Drag and drop between Tabs.
  906. Node *from_node = get_node(from_path);
  907. TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
  908. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  909. if (tab_from_id >= from_tabs->get_tab_count()) {
  910. return;
  911. }
  912. // Drop the new tab to the left or right depending on where the target tab is being hovered.
  913. if (hover_now != -1) {
  914. Rect2 tab_rect = get_tab_rect(hover_now);
  915. if (is_layout_rtl() ^ (p_point.x > tab_rect.position.x + tab_rect.size.width / 2)) {
  916. hover_now += 1;
  917. }
  918. } else {
  919. hover_now = is_layout_rtl() ^ (p_point.x < get_tab_rect(0).position.x) ? 0 : get_tab_count();
  920. }
  921. Tab moving_tab = from_tabs->tabs[tab_from_id];
  922. from_tabs->remove_tab(tab_from_id);
  923. tabs.insert(hover_now, moving_tab);
  924. if (tabs.size() > 1) {
  925. if (current >= hover_now) {
  926. current++;
  927. }
  928. if (previous >= hover_now) {
  929. previous++;
  930. }
  931. }
  932. if (!is_tab_disabled(hover_now)) {
  933. set_current_tab(hover_now);
  934. } else {
  935. _update_cache();
  936. queue_redraw();
  937. }
  938. update_minimum_size();
  939. if (tabs.size() == 1) {
  940. emit_signal(SNAME("tab_selected"), 0);
  941. emit_signal(SNAME("tab_changed"), 0);
  942. }
  943. }
  944. }
  945. }
  946. }
  947. int TabBar::get_tab_idx_at_point(const Point2 &p_point) const {
  948. int hover_now = -1;
  949. for (int i = offset; i <= max_drawn_tab; i++) {
  950. Rect2 rect = get_tab_rect(i);
  951. if (rect.has_point(p_point)) {
  952. hover_now = i;
  953. }
  954. }
  955. return hover_now;
  956. }
  957. void TabBar::set_tab_alignment(AlignmentMode p_alignment) {
  958. ERR_FAIL_INDEX(p_alignment, ALIGNMENT_MAX);
  959. if (tab_alignment == p_alignment) {
  960. return;
  961. }
  962. tab_alignment = p_alignment;
  963. _update_cache();
  964. queue_redraw();
  965. }
  966. TabBar::AlignmentMode TabBar::get_tab_alignment() const {
  967. return tab_alignment;
  968. }
  969. void TabBar::set_clip_tabs(bool p_clip_tabs) {
  970. if (clip_tabs == p_clip_tabs) {
  971. return;
  972. }
  973. clip_tabs = p_clip_tabs;
  974. if (!clip_tabs) {
  975. offset = 0;
  976. max_drawn_tab = 0;
  977. }
  978. _update_cache();
  979. if (scroll_to_selected) {
  980. ensure_tab_visible(current);
  981. }
  982. queue_redraw();
  983. update_minimum_size();
  984. }
  985. bool TabBar::get_clip_tabs() const {
  986. return clip_tabs;
  987. }
  988. void TabBar::move_tab(int p_from, int p_to) {
  989. if (p_from == p_to) {
  990. return;
  991. }
  992. ERR_FAIL_INDEX(p_from, tabs.size());
  993. ERR_FAIL_INDEX(p_to, tabs.size());
  994. Tab tab_from = tabs[p_from];
  995. tabs.remove_at(p_from);
  996. tabs.insert(p_to, tab_from);
  997. if (current == p_from) {
  998. current = p_to;
  999. } else if (current > p_from && current <= p_to) {
  1000. current--;
  1001. } else if (current < p_from && current >= p_to) {
  1002. current++;
  1003. }
  1004. if (previous == p_from) {
  1005. previous = p_to;
  1006. } else if (previous > p_from && previous >= p_to) {
  1007. previous--;
  1008. } else if (previous < p_from && previous <= p_to) {
  1009. previous++;
  1010. }
  1011. _update_cache();
  1012. _ensure_no_over_offset();
  1013. if (scroll_to_selected) {
  1014. ensure_tab_visible(current);
  1015. }
  1016. queue_redraw();
  1017. notify_property_list_changed();
  1018. }
  1019. int TabBar::get_tab_width(int p_idx) const {
  1020. ERR_FAIL_INDEX_V(p_idx, tabs.size(), 0);
  1021. Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
  1022. Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
  1023. Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
  1024. int hseparation = get_theme_constant(SNAME("h_separation"));
  1025. Ref<StyleBox> style;
  1026. if (tabs[p_idx].disabled) {
  1027. style = tab_disabled;
  1028. } else if (current == p_idx) {
  1029. style = tab_selected;
  1030. } else {
  1031. style = tab_unselected;
  1032. }
  1033. int x = style->get_minimum_size().width;
  1034. Ref<Texture2D> tex = tabs[p_idx].icon;
  1035. if (tex.is_valid()) {
  1036. x += tex->get_width() + hseparation;
  1037. }
  1038. if (!tabs[p_idx].text.is_empty()) {
  1039. x += tabs[p_idx].size_text + hseparation;
  1040. }
  1041. bool close_visible = cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx == current);
  1042. if (tabs[p_idx].right_button.is_valid()) {
  1043. Ref<StyleBox> btn_style = get_theme_stylebox(SNAME("button_highlight"));
  1044. Ref<Texture2D> rb = tabs[p_idx].right_button;
  1045. if (close_visible) {
  1046. x += btn_style->get_minimum_size().width + rb->get_width();
  1047. } else {
  1048. x += btn_style->get_margin(SIDE_LEFT) + rb->get_width() + hseparation;
  1049. }
  1050. }
  1051. if (close_visible) {
  1052. Ref<StyleBox> btn_style = get_theme_stylebox(SNAME("button_highlight"));
  1053. Ref<Texture2D> cb = get_theme_icon(SNAME("close"));
  1054. x += btn_style->get_margin(SIDE_LEFT) + cb->get_width() + hseparation;
  1055. }
  1056. if (x > style->get_minimum_size().width) {
  1057. x -= hseparation;
  1058. }
  1059. return x;
  1060. }
  1061. void TabBar::_ensure_no_over_offset() {
  1062. if (!is_inside_tree() || !buttons_visible) {
  1063. return;
  1064. }
  1065. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  1066. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  1067. int limit_minus_buttons = get_size().width - incr->get_width() - decr->get_width();
  1068. int prev_offset = offset;
  1069. int total_w = tabs[max_drawn_tab].ofs_cache + tabs[max_drawn_tab].size_cache - tabs[offset].ofs_cache;
  1070. for (int i = offset; i > 0; i--) {
  1071. if (tabs[i - 1].hidden) {
  1072. continue;
  1073. }
  1074. total_w += tabs[i - 1].size_cache;
  1075. if (total_w < limit_minus_buttons) {
  1076. offset--;
  1077. } else {
  1078. break;
  1079. }
  1080. }
  1081. if (prev_offset != offset) {
  1082. _update_cache();
  1083. queue_redraw();
  1084. }
  1085. }
  1086. void TabBar::ensure_tab_visible(int p_idx) {
  1087. if (!is_inside_tree() || !buttons_visible) {
  1088. return;
  1089. }
  1090. ERR_FAIL_INDEX(p_idx, tabs.size());
  1091. if (tabs[p_idx].hidden || (p_idx >= offset && p_idx <= max_drawn_tab)) {
  1092. return;
  1093. }
  1094. if (p_idx < offset) {
  1095. offset = p_idx;
  1096. _update_cache();
  1097. queue_redraw();
  1098. return;
  1099. }
  1100. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  1101. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  1102. int limit_minus_buttons = get_size().width - incr->get_width() - decr->get_width();
  1103. int total_w = tabs[max_drawn_tab].ofs_cache - tabs[offset].ofs_cache;
  1104. for (int i = max_drawn_tab; i <= p_idx; i++) {
  1105. if (tabs[i].hidden) {
  1106. continue;
  1107. }
  1108. total_w += tabs[i].size_cache;
  1109. }
  1110. int prev_offset = offset;
  1111. for (int i = offset; i < p_idx; i++) {
  1112. if (tabs[i].hidden) {
  1113. continue;
  1114. }
  1115. if (total_w > limit_minus_buttons) {
  1116. total_w -= tabs[i].size_cache;
  1117. offset++;
  1118. } else {
  1119. break;
  1120. }
  1121. }
  1122. if (prev_offset != offset) {
  1123. _update_cache();
  1124. queue_redraw();
  1125. }
  1126. }
  1127. Rect2 TabBar::get_tab_rect(int p_tab) const {
  1128. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Rect2());
  1129. if (is_layout_rtl()) {
  1130. return Rect2(get_size().width - tabs[p_tab].ofs_cache - tabs[p_tab].size_cache, 0, tabs[p_tab].size_cache, get_size().height);
  1131. } else {
  1132. return Rect2(tabs[p_tab].ofs_cache, 0, tabs[p_tab].size_cache, get_size().height);
  1133. }
  1134. }
  1135. void TabBar::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) {
  1136. ERR_FAIL_INDEX(p_policy, CLOSE_BUTTON_MAX);
  1137. if (cb_displaypolicy == p_policy) {
  1138. return;
  1139. }
  1140. cb_displaypolicy = p_policy;
  1141. _update_cache();
  1142. _ensure_no_over_offset();
  1143. if (scroll_to_selected) {
  1144. ensure_tab_visible(current);
  1145. }
  1146. queue_redraw();
  1147. update_minimum_size();
  1148. }
  1149. TabBar::CloseButtonDisplayPolicy TabBar::get_tab_close_display_policy() const {
  1150. return cb_displaypolicy;
  1151. }
  1152. void TabBar::set_max_tab_width(int p_width) {
  1153. ERR_FAIL_COND(p_width < 0);
  1154. if (max_width == p_width) {
  1155. return;
  1156. }
  1157. max_width = p_width;
  1158. _update_cache();
  1159. _ensure_no_over_offset();
  1160. if (scroll_to_selected) {
  1161. ensure_tab_visible(current);
  1162. }
  1163. queue_redraw();
  1164. update_minimum_size();
  1165. }
  1166. int TabBar::get_max_tab_width() const {
  1167. return max_width;
  1168. }
  1169. void TabBar::set_scrolling_enabled(bool p_enabled) {
  1170. scrolling_enabled = p_enabled;
  1171. }
  1172. bool TabBar::get_scrolling_enabled() const {
  1173. return scrolling_enabled;
  1174. }
  1175. void TabBar::set_drag_to_rearrange_enabled(bool p_enabled) {
  1176. drag_to_rearrange_enabled = p_enabled;
  1177. }
  1178. bool TabBar::get_drag_to_rearrange_enabled() const {
  1179. return drag_to_rearrange_enabled;
  1180. }
  1181. void TabBar::set_tabs_rearrange_group(int p_group_id) {
  1182. tabs_rearrange_group = p_group_id;
  1183. }
  1184. int TabBar::get_tabs_rearrange_group() const {
  1185. return tabs_rearrange_group;
  1186. }
  1187. void TabBar::set_scroll_to_selected(bool p_enabled) {
  1188. scroll_to_selected = p_enabled;
  1189. if (p_enabled) {
  1190. ensure_tab_visible(current);
  1191. }
  1192. }
  1193. bool TabBar::get_scroll_to_selected() const {
  1194. return scroll_to_selected;
  1195. }
  1196. void TabBar::set_select_with_rmb(bool p_enabled) {
  1197. select_with_rmb = p_enabled;
  1198. }
  1199. bool TabBar::get_select_with_rmb() const {
  1200. return select_with_rmb;
  1201. }
  1202. bool TabBar::_set(const StringName &p_name, const Variant &p_value) {
  1203. Vector<String> components = String(p_name).split("/", true, 2);
  1204. if (components.size() >= 2 && components[0].begins_with("tab_") && components[0].trim_prefix("tab_").is_valid_int()) {
  1205. int tab_index = components[0].trim_prefix("tab_").to_int();
  1206. String property = components[1];
  1207. if (property == "title") {
  1208. set_tab_title(tab_index, p_value);
  1209. return true;
  1210. } else if (property == "icon") {
  1211. set_tab_icon(tab_index, p_value);
  1212. return true;
  1213. } else if (components[1] == "disabled") {
  1214. set_tab_disabled(tab_index, p_value);
  1215. return true;
  1216. }
  1217. }
  1218. return false;
  1219. }
  1220. bool TabBar::_get(const StringName &p_name, Variant &r_ret) const {
  1221. Vector<String> components = String(p_name).split("/", true, 2);
  1222. if (components.size() >= 2 && components[0].begins_with("tab_") && components[0].trim_prefix("tab_").is_valid_int()) {
  1223. int tab_index = components[0].trim_prefix("tab_").to_int();
  1224. String property = components[1];
  1225. if (property == "title") {
  1226. r_ret = get_tab_title(tab_index);
  1227. return true;
  1228. } else if (property == "icon") {
  1229. r_ret = get_tab_icon(tab_index);
  1230. return true;
  1231. } else if (components[1] == "disabled") {
  1232. r_ret = is_tab_disabled(tab_index);
  1233. return true;
  1234. }
  1235. }
  1236. return false;
  1237. }
  1238. void TabBar::_get_property_list(List<PropertyInfo> *p_list) const {
  1239. for (int i = 0; i < tabs.size(); i++) {
  1240. p_list->push_back(PropertyInfo(Variant::STRING, vformat("tab_%d/title", i)));
  1241. PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("tab_%d/icon", i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D");
  1242. pi.usage &= ~(get_tab_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0);
  1243. p_list->push_back(pi);
  1244. pi = PropertyInfo(Variant::BOOL, vformat("tab_%d/disabled", i));
  1245. pi.usage &= ~(!is_tab_disabled(i) ? PROPERTY_USAGE_STORAGE : 0);
  1246. p_list->push_back(pi);
  1247. }
  1248. }
  1249. void TabBar::_bind_methods() {
  1250. ClassDB::bind_method(D_METHOD("set_tab_count", "count"), &TabBar::set_tab_count);
  1251. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabBar::get_tab_count);
  1252. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabBar::set_current_tab);
  1253. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabBar::get_current_tab);
  1254. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabBar::get_previous_tab);
  1255. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabBar::set_tab_title);
  1256. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabBar::get_tab_title);
  1257. ClassDB::bind_method(D_METHOD("set_tab_text_direction", "tab_idx", "direction"), &TabBar::set_tab_text_direction);
  1258. ClassDB::bind_method(D_METHOD("get_tab_text_direction", "tab_idx"), &TabBar::get_tab_text_direction);
  1259. ClassDB::bind_method(D_METHOD("set_tab_language", "tab_idx", "language"), &TabBar::set_tab_language);
  1260. ClassDB::bind_method(D_METHOD("get_tab_language", "tab_idx"), &TabBar::get_tab_language);
  1261. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabBar::set_tab_icon);
  1262. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabBar::get_tab_icon);
  1263. ClassDB::bind_method(D_METHOD("set_tab_button_icon", "tab_idx", "icon"), &TabBar::set_tab_button_icon);
  1264. ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &TabBar::get_tab_button_icon);
  1265. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabBar::set_tab_disabled);
  1266. ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabBar::is_tab_disabled);
  1267. ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabBar::set_tab_hidden);
  1268. ClassDB::bind_method(D_METHOD("is_tab_hidden", "tab_idx"), &TabBar::is_tab_hidden);
  1269. ClassDB::bind_method(D_METHOD("remove_tab", "tab_idx"), &TabBar::remove_tab);
  1270. ClassDB::bind_method(D_METHOD("add_tab", "title", "icon"), &TabBar::add_tab, DEFVAL(""), DEFVAL(Ref<Texture2D>()));
  1271. ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabBar::get_tab_idx_at_point);
  1272. ClassDB::bind_method(D_METHOD("set_tab_alignment", "alignment"), &TabBar::set_tab_alignment);
  1273. ClassDB::bind_method(D_METHOD("get_tab_alignment"), &TabBar::get_tab_alignment);
  1274. ClassDB::bind_method(D_METHOD("set_clip_tabs", "clip_tabs"), &TabBar::set_clip_tabs);
  1275. ClassDB::bind_method(D_METHOD("get_clip_tabs"), &TabBar::get_clip_tabs);
  1276. ClassDB::bind_method(D_METHOD("get_tab_offset"), &TabBar::get_tab_offset);
  1277. ClassDB::bind_method(D_METHOD("get_offset_buttons_visible"), &TabBar::get_offset_buttons_visible);
  1278. ClassDB::bind_method(D_METHOD("ensure_tab_visible", "idx"), &TabBar::ensure_tab_visible);
  1279. ClassDB::bind_method(D_METHOD("get_tab_rect", "tab_idx"), &TabBar::get_tab_rect);
  1280. ClassDB::bind_method(D_METHOD("move_tab", "from", "to"), &TabBar::move_tab);
  1281. ClassDB::bind_method(D_METHOD("set_tab_close_display_policy", "policy"), &TabBar::set_tab_close_display_policy);
  1282. ClassDB::bind_method(D_METHOD("get_tab_close_display_policy"), &TabBar::get_tab_close_display_policy);
  1283. ClassDB::bind_method(D_METHOD("set_max_tab_width", "width"), &TabBar::set_max_tab_width);
  1284. ClassDB::bind_method(D_METHOD("get_max_tab_width"), &TabBar::get_max_tab_width);
  1285. ClassDB::bind_method(D_METHOD("set_scrolling_enabled", "enabled"), &TabBar::set_scrolling_enabled);
  1286. ClassDB::bind_method(D_METHOD("get_scrolling_enabled"), &TabBar::get_scrolling_enabled);
  1287. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabBar::set_drag_to_rearrange_enabled);
  1288. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabBar::get_drag_to_rearrange_enabled);
  1289. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabBar::set_tabs_rearrange_group);
  1290. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabBar::get_tabs_rearrange_group);
  1291. ClassDB::bind_method(D_METHOD("set_scroll_to_selected", "enabled"), &TabBar::set_scroll_to_selected);
  1292. ClassDB::bind_method(D_METHOD("get_scroll_to_selected"), &TabBar::get_scroll_to_selected);
  1293. ClassDB::bind_method(D_METHOD("set_select_with_rmb", "enabled"), &TabBar::set_select_with_rmb);
  1294. ClassDB::bind_method(D_METHOD("get_select_with_rmb"), &TabBar::get_select_with_rmb);
  1295. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  1296. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  1297. ADD_SIGNAL(MethodInfo("tab_clicked", PropertyInfo(Variant::INT, "tab")));
  1298. ADD_SIGNAL(MethodInfo("tab_rmb_clicked", PropertyInfo(Variant::INT, "tab")));
  1299. ADD_SIGNAL(MethodInfo("tab_close_pressed", PropertyInfo(Variant::INT, "tab")));
  1300. ADD_SIGNAL(MethodInfo("tab_button_pressed", PropertyInfo(Variant::INT, "tab")));
  1301. ADD_SIGNAL(MethodInfo("tab_hovered", PropertyInfo(Variant::INT, "tab")));
  1302. ADD_SIGNAL(MethodInfo("active_tab_rearranged", PropertyInfo(Variant::INT, "idx_to")));
  1303. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
  1304. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_alignment", "get_tab_alignment");
  1305. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_tabs"), "set_clip_tabs", "get_clip_tabs");
  1306. 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");
  1307. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_tab_width", PROPERTY_HINT_RANGE, "0,99999,1,suffix:px"), "set_max_tab_width", "get_max_tab_width");
  1308. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrolling_enabled"), "set_scrolling_enabled", "get_scrolling_enabled");
  1309. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  1310. ADD_PROPERTY(PropertyInfo(Variant::INT, "tabs_rearrange_group"), "set_tabs_rearrange_group", "get_tabs_rearrange_group");
  1311. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_to_selected"), "set_scroll_to_selected", "get_scroll_to_selected");
  1312. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_with_rmb"), "set_select_with_rmb", "get_select_with_rmb");
  1313. ADD_ARRAY_COUNT("Tabs", "tab_count", "set_tab_count", "get_tab_count", "tab_");
  1314. BIND_ENUM_CONSTANT(ALIGNMENT_LEFT);
  1315. BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
  1316. BIND_ENUM_CONSTANT(ALIGNMENT_RIGHT);
  1317. BIND_ENUM_CONSTANT(ALIGNMENT_MAX);
  1318. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_NEVER);
  1319. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ACTIVE_ONLY);
  1320. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ALWAYS);
  1321. BIND_ENUM_CONSTANT(CLOSE_BUTTON_MAX);
  1322. }
  1323. TabBar::TabBar() {
  1324. set_size(Size2(get_size().width, get_minimum_size().height));
  1325. connect("mouse_exited", callable_mp(this, &TabBar::_on_mouse_exited));
  1326. }