tab_bar.cpp 47 KB

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