tab_container.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*************************************************************************/
  2. /* tab_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_container.h"
  31. #include "core/message_queue.h"
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/gui/texture_rect.h"
  35. int TabContainer::_get_top_margin() const {
  36. if (!tabs_visible)
  37. return 0;
  38. // Respect the minimum tab height.
  39. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  40. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  41. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  42. int tab_height = MAX(MAX(tab_bg->get_minimum_size().height, tab_fg->get_minimum_size().height), tab_disabled->get_minimum_size().height);
  43. // Font height or higher icon wins.
  44. Ref<Font> font = get_font("font");
  45. int content_height = font->get_height();
  46. Vector<Control *> tabs = _get_tabs();
  47. for (int i = 0; i < tabs.size(); i++) {
  48. Control *c = tabs[i];
  49. if (!c->has_meta("_tab_icon"))
  50. continue;
  51. Ref<Texture> tex = c->get_meta("_tab_icon");
  52. if (!tex.is_valid())
  53. continue;
  54. content_height = MAX(content_height, tex->get_size().height);
  55. }
  56. return tab_height + content_height;
  57. }
  58. void TabContainer::_gui_input(const Ref<InputEvent> &p_event) {
  59. Ref<InputEventMouseButton> mb = p_event;
  60. Popup *popup = get_popup();
  61. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  62. Point2 pos(mb->get_position().x, mb->get_position().y);
  63. Size2 size = get_size();
  64. // Click must be on tabs in the tab header area.
  65. if (pos.x < tabs_ofs_cache || pos.y > _get_top_margin())
  66. return;
  67. // Handle menu button.
  68. Ref<Texture> menu = get_icon("menu");
  69. if (popup && pos.x > size.width - menu->get_width()) {
  70. emit_signal("pre_popup_pressed");
  71. Vector2 popup_pos = get_global_position();
  72. popup_pos.x += size.width * get_global_transform().get_scale().x - popup->get_size().width * popup->get_global_transform().get_scale().x;
  73. popup_pos.y += menu->get_height() * get_global_transform().get_scale().y;
  74. popup->set_global_position(popup_pos);
  75. popup->popup();
  76. return;
  77. }
  78. // Do not activate tabs when tabs is empty.
  79. if (get_tab_count() == 0)
  80. return;
  81. Vector<Control *> tabs = _get_tabs();
  82. // Handle navigation buttons.
  83. if (buttons_visible_cache) {
  84. int popup_ofs = 0;
  85. if (popup) {
  86. popup_ofs = menu->get_width();
  87. }
  88. Ref<Texture> increment = get_icon("increment");
  89. Ref<Texture> decrement = get_icon("decrement");
  90. if (pos.x > size.width - increment->get_width() - popup_ofs) {
  91. if (last_tab_cache < tabs.size() - 1) {
  92. first_tab_cache += 1;
  93. update();
  94. }
  95. return;
  96. } else if (pos.x > size.width - increment->get_width() - decrement->get_width() - popup_ofs) {
  97. if (first_tab_cache > 0) {
  98. first_tab_cache -= 1;
  99. update();
  100. }
  101. return;
  102. }
  103. }
  104. // Activate the clicked tab.
  105. pos.x -= tabs_ofs_cache;
  106. for (int i = first_tab_cache; i <= last_tab_cache; i++) {
  107. if (get_tab_hidden(i)) {
  108. continue;
  109. }
  110. int tab_width = _get_tab_width(i);
  111. if (pos.x < tab_width) {
  112. if (!get_tab_disabled(i)) {
  113. set_current_tab(i);
  114. }
  115. break;
  116. }
  117. pos.x -= tab_width;
  118. }
  119. }
  120. Ref<InputEventMouseMotion> mm = p_event;
  121. if (mm.is_valid()) {
  122. Point2 pos(mm->get_position().x, mm->get_position().y);
  123. Size2 size = get_size();
  124. // Mouse must be on tabs in the tab header area.
  125. if (pos.x < tabs_ofs_cache || pos.y > _get_top_margin()) {
  126. if (menu_hovered || highlight_arrow > -1) {
  127. menu_hovered = false;
  128. highlight_arrow = -1;
  129. update();
  130. }
  131. return;
  132. }
  133. Ref<Texture> menu = get_icon("menu");
  134. if (popup) {
  135. if (pos.x >= size.width - menu->get_width()) {
  136. if (!menu_hovered) {
  137. menu_hovered = true;
  138. highlight_arrow = -1;
  139. update();
  140. return;
  141. }
  142. } else if (menu_hovered) {
  143. menu_hovered = false;
  144. update();
  145. }
  146. if (menu_hovered) {
  147. return;
  148. }
  149. }
  150. // Do not activate tabs when tabs is empty.
  151. if ((get_tab_count() == 0 || !buttons_visible_cache) && menu_hovered) {
  152. highlight_arrow = -1;
  153. update();
  154. return;
  155. }
  156. int popup_ofs = 0;
  157. if (popup) {
  158. popup_ofs = menu->get_width();
  159. }
  160. Ref<Texture> increment = get_icon("increment");
  161. Ref<Texture> decrement = get_icon("decrement");
  162. if (pos.x >= size.width - increment->get_width() - popup_ofs) {
  163. if (highlight_arrow != 1) {
  164. highlight_arrow = 1;
  165. update();
  166. }
  167. } else if (pos.x >= size.width - increment->get_width() - decrement->get_width() - popup_ofs) {
  168. if (highlight_arrow != 0) {
  169. highlight_arrow = 0;
  170. update();
  171. }
  172. } else if (highlight_arrow > -1) {
  173. highlight_arrow = -1;
  174. update();
  175. }
  176. }
  177. }
  178. void TabContainer::_notification(int p_what) {
  179. switch (p_what) {
  180. case NOTIFICATION_TRANSLATION_CHANGED: {
  181. minimum_size_changed();
  182. update();
  183. } break;
  184. case NOTIFICATION_RESIZED: {
  185. Vector<Control *> tabs = _get_tabs();
  186. int side_margin = get_constant("side_margin");
  187. Ref<Texture> menu = get_icon("menu");
  188. Ref<Texture> increment = get_icon("increment");
  189. Ref<Texture> decrement = get_icon("decrement");
  190. int header_width = get_size().width - side_margin * 2;
  191. // Find the width of the header area.
  192. Popup *popup = get_popup();
  193. if (popup)
  194. header_width -= menu->get_width();
  195. if (buttons_visible_cache)
  196. header_width -= increment->get_width() + decrement->get_width();
  197. if (popup || buttons_visible_cache)
  198. header_width += side_margin;
  199. // Find the width of all tabs after first_tab_cache.
  200. int all_tabs_width = 0;
  201. for (int i = first_tab_cache; i < tabs.size(); i++) {
  202. int tab_width = _get_tab_width(i);
  203. all_tabs_width += tab_width;
  204. }
  205. // Check if tabs before first_tab_cache would fit into the header area.
  206. for (int i = first_tab_cache - 1; i >= 0; i--) {
  207. int tab_width = _get_tab_width(i);
  208. if (all_tabs_width + tab_width > header_width)
  209. break;
  210. all_tabs_width += tab_width;
  211. first_tab_cache--;
  212. }
  213. } break;
  214. case NOTIFICATION_DRAW: {
  215. RID canvas = get_canvas_item();
  216. Size2 size = get_size();
  217. // Draw only the tab area if the header is hidden.
  218. Ref<StyleBox> panel = get_stylebox("panel");
  219. if (!tabs_visible) {
  220. panel->draw(canvas, Rect2(0, 0, size.width, size.height));
  221. return;
  222. }
  223. Vector<Control *> tabs = _get_tabs();
  224. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  225. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  226. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  227. Ref<Texture> increment = get_icon("increment");
  228. Ref<Texture> increment_hl = get_icon("increment_highlight");
  229. Ref<Texture> decrement = get_icon("decrement");
  230. Ref<Texture> decrement_hl = get_icon("decrement_highlight");
  231. Ref<Texture> menu = get_icon("menu");
  232. Ref<Texture> menu_hl = get_icon("menu_highlight");
  233. Ref<Font> font = get_font("font");
  234. Color font_color_fg = get_color("font_color_fg");
  235. Color font_color_bg = get_color("font_color_bg");
  236. Color font_color_disabled = get_color("font_color_disabled");
  237. int side_margin = get_constant("side_margin");
  238. // Find out start and width of the header area.
  239. int header_x = side_margin;
  240. int header_width = size.width - side_margin * 2;
  241. int header_height = _get_top_margin();
  242. Popup *popup = get_popup();
  243. if (popup)
  244. header_width -= menu->get_width();
  245. // Check if all tabs would fit into the header area.
  246. int all_tabs_width = 0;
  247. for (int i = 0; i < tabs.size(); i++) {
  248. if (get_tab_hidden(i)) {
  249. continue;
  250. }
  251. int tab_width = _get_tab_width(i);
  252. all_tabs_width += tab_width;
  253. if (all_tabs_width > header_width) {
  254. // Not all tabs are visible at the same time - reserve space for navigation buttons.
  255. buttons_visible_cache = true;
  256. header_width -= decrement->get_width() + increment->get_width();
  257. break;
  258. } else {
  259. buttons_visible_cache = false;
  260. }
  261. }
  262. // With buttons, a right side margin does not need to be respected.
  263. if (popup || buttons_visible_cache) {
  264. header_width += side_margin;
  265. }
  266. if (!buttons_visible_cache) {
  267. first_tab_cache = 0;
  268. }
  269. // Go through the visible tabs to find the width they occupy.
  270. all_tabs_width = 0;
  271. Vector<int> tab_widths;
  272. for (int i = first_tab_cache; i < tabs.size(); i++) {
  273. if (get_tab_hidden(i)) {
  274. continue;
  275. }
  276. int tab_width = _get_tab_width(i);
  277. if (all_tabs_width + tab_width > header_width && tab_widths.size() > 0)
  278. break;
  279. all_tabs_width += tab_width;
  280. tab_widths.push_back(tab_width);
  281. }
  282. // Find the offset at which to draw tabs, according to the alignment.
  283. switch (align) {
  284. case ALIGN_LEFT:
  285. tabs_ofs_cache = header_x;
  286. break;
  287. case ALIGN_CENTER:
  288. tabs_ofs_cache = header_x + (header_width / 2) - (all_tabs_width / 2);
  289. break;
  290. case ALIGN_RIGHT:
  291. tabs_ofs_cache = header_x + header_width - all_tabs_width;
  292. break;
  293. }
  294. if (all_tabs_in_front) {
  295. // Draw the tab area.
  296. panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
  297. }
  298. // Draw unselected tabs in back
  299. int x = 0;
  300. int x_current = 0;
  301. int index = 0;
  302. for (int i = 0; i < tab_widths.size(); i++) {
  303. index = i + first_tab_cache;
  304. if (get_tab_hidden(index)) {
  305. continue;
  306. }
  307. int tab_width = tab_widths[i];
  308. if (index == current) {
  309. x_current = x;
  310. } else if (get_tab_disabled(index)) {
  311. _draw_tab(tab_disabled, font_color_disabled, index, tabs_ofs_cache + x);
  312. } else {
  313. _draw_tab(tab_bg, font_color_bg, index, tabs_ofs_cache + x);
  314. }
  315. x += tab_width;
  316. last_tab_cache = index;
  317. }
  318. if (!all_tabs_in_front) {
  319. // Draw the tab area.
  320. panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
  321. }
  322. // Draw selected tab in front. Only draw selected tab when it's in visible range.
  323. if (tabs.size() > 0 && current - first_tab_cache < tab_widths.size() && current >= first_tab_cache) {
  324. Ref<StyleBox> current_style_box = get_tab_disabled(current) ? tab_disabled : tab_fg;
  325. _draw_tab(current_style_box, font_color_fg, current, tabs_ofs_cache + x_current);
  326. }
  327. // Draw the popup menu.
  328. x = get_size().width;
  329. if (popup) {
  330. x -= menu->get_width();
  331. if (menu_hovered)
  332. menu_hl->draw(get_canvas_item(), Size2(x, (header_height - menu_hl->get_height()) / 2));
  333. else
  334. menu->draw(get_canvas_item(), Size2(x, (header_height - menu->get_height()) / 2));
  335. }
  336. // Draw the navigation buttons.
  337. if (buttons_visible_cache) {
  338. x -= increment->get_width();
  339. if (last_tab_cache < tabs.size() - 1) {
  340. draw_texture(highlight_arrow == 1 ? increment_hl : increment, Point2(x, (header_height - increment->get_height()) / 2));
  341. } else {
  342. draw_texture(increment, Point2(x, (header_height - increment->get_height()) / 2), Color(1, 1, 1, 0.5));
  343. }
  344. x -= decrement->get_width();
  345. if (first_tab_cache > 0) {
  346. draw_texture(highlight_arrow == 0 ? decrement_hl : decrement, Point2(x, (header_height - decrement->get_height()) / 2));
  347. } else {
  348. draw_texture(decrement, Point2(x, (header_height - decrement->get_height()) / 2), Color(1, 1, 1, 0.5));
  349. }
  350. }
  351. } break;
  352. case NOTIFICATION_THEME_CHANGED: {
  353. minimum_size_changed();
  354. call_deferred("_on_theme_changed"); // Wait until all changed theme.
  355. } break;
  356. }
  357. }
  358. void TabContainer::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x) {
  359. Vector<Control *> tabs = _get_tabs();
  360. RID canvas = get_canvas_item();
  361. Ref<Font> font = get_font("font");
  362. int icon_text_distance = get_constant("hseparation");
  363. int tab_width = _get_tab_width(p_index);
  364. int header_height = _get_top_margin();
  365. // Draw the tab background.
  366. Rect2 tab_rect(p_x, 0, tab_width, header_height);
  367. p_tab_style->draw(canvas, tab_rect);
  368. // Draw the tab contents.
  369. Control *control = Object::cast_to<Control>(tabs[p_index]);
  370. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
  371. int x_content = tab_rect.position.x + p_tab_style->get_margin(MARGIN_LEFT);
  372. int top_margin = p_tab_style->get_margin(MARGIN_TOP);
  373. int y_center = top_margin + (tab_rect.size.y - p_tab_style->get_minimum_size().y) / 2;
  374. // Draw the tab icon.
  375. if (control->has_meta("_tab_icon")) {
  376. Ref<Texture> icon = control->get_meta("_tab_icon");
  377. if (icon.is_valid()) {
  378. int y = y_center - (icon->get_height() / 2);
  379. icon->draw(canvas, Point2i(x_content, y));
  380. if (text != "") {
  381. x_content += icon->get_width() + icon_text_distance;
  382. }
  383. }
  384. }
  385. // Draw the tab text.
  386. Point2i text_pos(x_content, y_center - (font->get_height() / 2) + font->get_ascent());
  387. font->draw(canvas, text_pos, text, p_font_color);
  388. }
  389. void TabContainer::_on_theme_changed() {
  390. if (get_tab_count() > 0) {
  391. _repaint();
  392. update();
  393. }
  394. }
  395. void TabContainer::_repaint() {
  396. Ref<StyleBox> sb = get_stylebox("panel");
  397. Vector<Control *> tabs = _get_tabs();
  398. for (int i = 0; i < tabs.size(); i++) {
  399. Control *c = tabs[i];
  400. if (i == current) {
  401. c->show();
  402. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  403. if (tabs_visible) {
  404. c->set_margin(MARGIN_TOP, _get_top_margin());
  405. }
  406. c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
  407. c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
  408. c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
  409. c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
  410. } else {
  411. c->hide();
  412. }
  413. }
  414. }
  415. void TabContainer::_on_mouse_exited() {
  416. if (menu_hovered || highlight_arrow > -1) {
  417. menu_hovered = false;
  418. highlight_arrow = -1;
  419. update();
  420. }
  421. }
  422. int TabContainer::_get_tab_width(int p_index) const {
  423. ERR_FAIL_INDEX_V(p_index, get_tab_count(), 0);
  424. Control *control = get_tab_control(p_index);
  425. if (!control || get_tab_hidden(p_index)) {
  426. return 0;
  427. }
  428. // Get the width of the text displayed on the tab.
  429. Ref<Font> font = get_font("font");
  430. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
  431. int width = font->get_string_size(text).width;
  432. // Add space for a tab icon.
  433. if (control->has_meta("_tab_icon")) {
  434. Ref<Texture> icon = control->get_meta("_tab_icon");
  435. if (icon.is_valid()) {
  436. width += icon->get_width();
  437. if (text != "")
  438. width += get_constant("hseparation");
  439. }
  440. }
  441. // Respect a minimum size.
  442. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  443. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  444. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  445. if (get_tab_disabled(p_index)) {
  446. width += tab_disabled->get_minimum_size().width;
  447. } else if (p_index == current) {
  448. width += tab_fg->get_minimum_size().width;
  449. } else {
  450. width += tab_bg->get_minimum_size().width;
  451. }
  452. return width;
  453. }
  454. Vector<Control *> TabContainer::_get_tabs() const {
  455. Vector<Control *> controls;
  456. for (int i = 0; i < get_child_count(); i++) {
  457. Control *control = Object::cast_to<Control>(get_child(i));
  458. if (!control || control->is_toplevel_control())
  459. continue;
  460. controls.push_back(control);
  461. }
  462. return controls;
  463. }
  464. void TabContainer::_child_renamed_callback() {
  465. update();
  466. }
  467. void TabContainer::add_child_notify(Node *p_child) {
  468. Container::add_child_notify(p_child);
  469. Control *c = Object::cast_to<Control>(p_child);
  470. if (!c)
  471. return;
  472. if (c->is_set_as_toplevel())
  473. return;
  474. bool first = false;
  475. if (get_tab_count() != 1)
  476. c->hide();
  477. else {
  478. c->show();
  479. //call_deferred("set_current_tab",0);
  480. first = true;
  481. current = 0;
  482. previous = 0;
  483. }
  484. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  485. if (tabs_visible)
  486. c->set_margin(MARGIN_TOP, _get_top_margin());
  487. Ref<StyleBox> sb = get_stylebox("panel");
  488. c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
  489. c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
  490. c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
  491. c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
  492. update();
  493. p_child->connect("renamed", this, "_child_renamed_callback");
  494. if (first)
  495. emit_signal("tab_changed", current);
  496. }
  497. int TabContainer::get_tab_count() const {
  498. return _get_tabs().size();
  499. }
  500. void TabContainer::set_current_tab(int p_current) {
  501. ERR_FAIL_INDEX(p_current, get_tab_count());
  502. int pending_previous = current;
  503. current = p_current;
  504. _repaint();
  505. _change_notify("current_tab");
  506. if (pending_previous == current)
  507. emit_signal("tab_selected", current);
  508. else {
  509. previous = pending_previous;
  510. emit_signal("tab_selected", current);
  511. emit_signal("tab_changed", current);
  512. }
  513. update();
  514. }
  515. int TabContainer::get_current_tab() const {
  516. return current;
  517. }
  518. int TabContainer::get_previous_tab() const {
  519. return previous;
  520. }
  521. Control *TabContainer::get_tab_control(int p_idx) const {
  522. Vector<Control *> tabs = _get_tabs();
  523. if (p_idx >= 0 && p_idx < tabs.size())
  524. return tabs[p_idx];
  525. else
  526. return NULL;
  527. }
  528. Control *TabContainer::get_current_tab_control() const {
  529. Vector<Control *> tabs = _get_tabs();
  530. if (current >= 0 && current < tabs.size())
  531. return tabs[current];
  532. else
  533. return NULL;
  534. }
  535. void TabContainer::remove_child_notify(Node *p_child) {
  536. Container::remove_child_notify(p_child);
  537. if (!Object::cast_to<Control>(p_child)) {
  538. return;
  539. }
  540. call_deferred("_update_current_tab");
  541. p_child->disconnect("renamed", this, "_child_renamed_callback");
  542. update();
  543. }
  544. void TabContainer::_update_current_tab() {
  545. int tc = get_tab_count();
  546. if (current >= tc)
  547. current = tc - 1;
  548. if (current < 0)
  549. current = 0;
  550. else
  551. set_current_tab(current);
  552. }
  553. Variant TabContainer::get_drag_data(const Point2 &p_point) {
  554. if (!drag_to_rearrange_enabled)
  555. return Variant();
  556. int tab_over = get_tab_idx_at_point(p_point);
  557. if (tab_over < 0)
  558. return Variant();
  559. HBoxContainer *drag_preview = memnew(HBoxContainer);
  560. Ref<Texture> icon = get_tab_icon(tab_over);
  561. if (!icon.is_null()) {
  562. TextureRect *tf = memnew(TextureRect);
  563. tf->set_texture(icon);
  564. drag_preview->add_child(tf);
  565. }
  566. Label *label = memnew(Label(get_tab_title(tab_over)));
  567. drag_preview->add_child(label);
  568. set_drag_preview(drag_preview);
  569. Dictionary drag_data;
  570. drag_data["type"] = "tabc_element";
  571. drag_data["tabc_element"] = tab_over;
  572. drag_data["from_path"] = get_path();
  573. return drag_data;
  574. }
  575. bool TabContainer::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  576. if (!drag_to_rearrange_enabled)
  577. return false;
  578. Dictionary d = p_data;
  579. if (!d.has("type"))
  580. return false;
  581. if (String(d["type"]) == "tabc_element") {
  582. NodePath from_path = d["from_path"];
  583. NodePath to_path = get_path();
  584. if (from_path == to_path) {
  585. return true;
  586. } else if (get_tabs_rearrange_group() != -1) {
  587. // drag and drop between other TabContainers
  588. Node *from_node = get_node(from_path);
  589. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  590. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  591. return true;
  592. }
  593. }
  594. }
  595. return false;
  596. }
  597. void TabContainer::drop_data(const Point2 &p_point, const Variant &p_data) {
  598. if (!drag_to_rearrange_enabled)
  599. return;
  600. int hover_now = get_tab_idx_at_point(p_point);
  601. Dictionary d = p_data;
  602. if (!d.has("type"))
  603. return;
  604. if (String(d["type"]) == "tabc_element") {
  605. int tab_from_id = d["tabc_element"];
  606. NodePath from_path = d["from_path"];
  607. NodePath to_path = get_path();
  608. if (from_path == to_path) {
  609. if (hover_now < 0)
  610. hover_now = get_tab_count() - 1;
  611. move_child(get_tab_control(tab_from_id), get_tab_control(hover_now)->get_index());
  612. set_current_tab(hover_now);
  613. } else if (get_tabs_rearrange_group() != -1) {
  614. // drag and drop between TabContainers
  615. Node *from_node = get_node(from_path);
  616. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  617. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  618. Control *moving_tabc = from_tabc->get_tab_control(tab_from_id);
  619. from_tabc->remove_child(moving_tabc);
  620. add_child(moving_tabc);
  621. if (hover_now < 0)
  622. hover_now = get_tab_count() - 1;
  623. move_child(moving_tabc, get_tab_control(hover_now)->get_index());
  624. set_current_tab(hover_now);
  625. emit_signal("tab_changed", hover_now);
  626. }
  627. }
  628. }
  629. update();
  630. }
  631. int TabContainer::get_tab_idx_at_point(const Point2 &p_point) const {
  632. if (get_tab_count() == 0)
  633. return -1;
  634. // must be on tabs in the tab header area.
  635. if (p_point.x < tabs_ofs_cache || p_point.y > _get_top_margin())
  636. return -1;
  637. Size2 size = get_size();
  638. int right_ofs = 0;
  639. Popup *popup = get_popup();
  640. if (popup) {
  641. Ref<Texture> menu = get_icon("menu");
  642. right_ofs += menu->get_width();
  643. }
  644. if (buttons_visible_cache) {
  645. Ref<Texture> increment = get_icon("increment");
  646. Ref<Texture> decrement = get_icon("decrement");
  647. right_ofs += increment->get_width() + decrement->get_width();
  648. }
  649. if (p_point.x > size.width - right_ofs) {
  650. return -1;
  651. }
  652. // get the tab at the point
  653. Vector<Control *> tabs = _get_tabs();
  654. int px = p_point.x;
  655. px -= tabs_ofs_cache;
  656. for (int i = first_tab_cache; i <= last_tab_cache; i++) {
  657. int tab_width = _get_tab_width(i);
  658. if (px < tab_width) {
  659. return i;
  660. }
  661. px -= tab_width;
  662. }
  663. return -1;
  664. }
  665. void TabContainer::set_tab_align(TabAlign p_align) {
  666. ERR_FAIL_INDEX(p_align, 3);
  667. align = p_align;
  668. update();
  669. _change_notify("tab_align");
  670. }
  671. TabContainer::TabAlign TabContainer::get_tab_align() const {
  672. return align;
  673. }
  674. void TabContainer::set_tabs_visible(bool p_visible) {
  675. if (p_visible == tabs_visible)
  676. return;
  677. tabs_visible = p_visible;
  678. Vector<Control *> tabs = _get_tabs();
  679. for (int i = 0; i < tabs.size(); i++) {
  680. Control *c = tabs[i];
  681. if (p_visible)
  682. c->set_margin(MARGIN_TOP, _get_top_margin());
  683. else
  684. c->set_margin(MARGIN_TOP, 0);
  685. }
  686. update();
  687. minimum_size_changed();
  688. }
  689. bool TabContainer::are_tabs_visible() const {
  690. return tabs_visible;
  691. }
  692. void TabContainer::set_all_tabs_in_front(bool p_in_front) {
  693. if (p_in_front == all_tabs_in_front) {
  694. return;
  695. }
  696. all_tabs_in_front = p_in_front;
  697. update();
  698. }
  699. bool TabContainer::is_all_tabs_in_front() const {
  700. return all_tabs_in_front;
  701. }
  702. Control *TabContainer::_get_tab(int p_idx) const {
  703. return get_tab_control(p_idx);
  704. }
  705. void TabContainer::set_tab_title(int p_tab, const String &p_title) {
  706. Control *child = _get_tab(p_tab);
  707. ERR_FAIL_COND(!child);
  708. child->set_meta("_tab_name", p_title);
  709. update();
  710. }
  711. String TabContainer::get_tab_title(int p_tab) const {
  712. Control *child = _get_tab(p_tab);
  713. ERR_FAIL_COND_V(!child, "");
  714. if (child->has_meta("_tab_name"))
  715. return child->get_meta("_tab_name");
  716. else
  717. return child->get_name();
  718. }
  719. void TabContainer::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) {
  720. Control *child = _get_tab(p_tab);
  721. ERR_FAIL_COND(!child);
  722. child->set_meta("_tab_icon", p_icon);
  723. update();
  724. }
  725. Ref<Texture> TabContainer::get_tab_icon(int p_tab) const {
  726. Control *child = _get_tab(p_tab);
  727. ERR_FAIL_COND_V(!child, Ref<Texture>());
  728. if (child->has_meta("_tab_icon"))
  729. return child->get_meta("_tab_icon");
  730. else
  731. return Ref<Texture>();
  732. }
  733. void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) {
  734. Control *child = _get_tab(p_tab);
  735. ERR_FAIL_COND(!child);
  736. child->set_meta("_tab_disabled", p_disabled);
  737. update();
  738. }
  739. bool TabContainer::get_tab_disabled(int p_tab) const {
  740. Control *child = _get_tab(p_tab);
  741. ERR_FAIL_COND_V(!child, false);
  742. if (child->has_meta("_tab_disabled"))
  743. return child->get_meta("_tab_disabled");
  744. else
  745. return false;
  746. }
  747. void TabContainer::set_tab_hidden(int p_tab, bool p_hidden) {
  748. Control *child = _get_tab(p_tab);
  749. ERR_FAIL_COND(!child);
  750. child->set_meta("_tab_hidden", p_hidden);
  751. update();
  752. for (int i = 0; i < get_tab_count(); i++) {
  753. int try_tab = (p_tab + 1 + i) % get_tab_count();
  754. if (get_tab_disabled(try_tab) || get_tab_hidden(try_tab)) {
  755. continue;
  756. }
  757. set_current_tab(try_tab);
  758. return;
  759. }
  760. //assumed no other tab can be switched to, just hide
  761. child->hide();
  762. }
  763. bool TabContainer::get_tab_hidden(int p_tab) const {
  764. Control *child = _get_tab(p_tab);
  765. ERR_FAIL_COND_V(!child, false);
  766. if (child->has_meta("_tab_hidden"))
  767. return child->get_meta("_tab_hidden");
  768. else
  769. return false;
  770. }
  771. void TabContainer::get_translatable_strings(List<String> *p_strings) const {
  772. Vector<Control *> tabs = _get_tabs();
  773. for (int i = 0; i < tabs.size(); i++) {
  774. Control *c = tabs[i];
  775. if (!c->has_meta("_tab_name"))
  776. continue;
  777. String name = c->get_meta("_tab_name");
  778. if (name != "")
  779. p_strings->push_back(name);
  780. }
  781. }
  782. Size2 TabContainer::get_minimum_size() const {
  783. Size2 ms;
  784. Vector<Control *> tabs = _get_tabs();
  785. for (int i = 0; i < tabs.size(); i++) {
  786. Control *c = tabs[i];
  787. if (!c->is_visible_in_tree() && !use_hidden_tabs_for_min_size)
  788. continue;
  789. Size2 cms = c->get_combined_minimum_size();
  790. ms.x = MAX(ms.x, cms.x);
  791. ms.y = MAX(ms.y, cms.y);
  792. }
  793. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  794. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  795. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  796. Ref<Font> font = get_font("font");
  797. if (tabs_visible) {
  798. ms.y += MAX(MAX(tab_bg->get_minimum_size().y, tab_fg->get_minimum_size().y), tab_disabled->get_minimum_size().y);
  799. ms.y += font->get_height();
  800. }
  801. Ref<StyleBox> sb = get_stylebox("panel");
  802. ms += sb->get_minimum_size();
  803. return ms;
  804. }
  805. void TabContainer::set_popup(Node *p_popup) {
  806. ERR_FAIL_NULL(p_popup);
  807. Popup *popup = Object::cast_to<Popup>(p_popup);
  808. popup_obj_id = popup ? popup->get_instance_id() : 0;
  809. update();
  810. }
  811. Popup *TabContainer::get_popup() const {
  812. if (popup_obj_id) {
  813. Popup *popup = Object::cast_to<Popup>(ObjectDB::get_instance(popup_obj_id));
  814. if (popup) {
  815. return popup;
  816. } else {
  817. #ifdef DEBUG_ENABLED
  818. ERR_PRINT("Popup assigned to TabContainer is gone!");
  819. #endif
  820. popup_obj_id = 0;
  821. }
  822. }
  823. return NULL;
  824. }
  825. void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
  826. drag_to_rearrange_enabled = p_enabled;
  827. }
  828. bool TabContainer::get_drag_to_rearrange_enabled() const {
  829. return drag_to_rearrange_enabled;
  830. }
  831. void TabContainer::set_tabs_rearrange_group(int p_group_id) {
  832. tabs_rearrange_group = p_group_id;
  833. }
  834. int TabContainer::get_tabs_rearrange_group() const {
  835. return tabs_rearrange_group;
  836. }
  837. void TabContainer::set_use_hidden_tabs_for_min_size(bool p_use_hidden_tabs) {
  838. use_hidden_tabs_for_min_size = p_use_hidden_tabs;
  839. }
  840. bool TabContainer::get_use_hidden_tabs_for_min_size() const {
  841. return use_hidden_tabs_for_min_size;
  842. }
  843. void TabContainer::_bind_methods() {
  844. ClassDB::bind_method(D_METHOD("_gui_input"), &TabContainer::_gui_input);
  845. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabContainer::get_tab_count);
  846. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
  847. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabContainer::get_current_tab);
  848. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabContainer::get_previous_tab);
  849. ClassDB::bind_method(D_METHOD("get_current_tab_control"), &TabContainer::get_current_tab_control);
  850. ClassDB::bind_method(D_METHOD("get_tab_control", "tab_idx"), &TabContainer::get_tab_control);
  851. ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &TabContainer::set_tab_align);
  852. ClassDB::bind_method(D_METHOD("get_tab_align"), &TabContainer::get_tab_align);
  853. ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
  854. ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
  855. ClassDB::bind_method(D_METHOD("set_all_tabs_in_front", "is_front"), &TabContainer::set_all_tabs_in_front);
  856. ClassDB::bind_method(D_METHOD("is_all_tabs_in_front"), &TabContainer::is_all_tabs_in_front);
  857. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
  858. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
  859. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon);
  860. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon);
  861. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled);
  862. ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &TabContainer::get_tab_disabled);
  863. ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
  864. ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
  865. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabContainer::set_drag_to_rearrange_enabled);
  866. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabContainer::get_drag_to_rearrange_enabled);
  867. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabContainer::set_tabs_rearrange_group);
  868. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabContainer::get_tabs_rearrange_group);
  869. ClassDB::bind_method(D_METHOD("set_use_hidden_tabs_for_min_size", "enabled"), &TabContainer::set_use_hidden_tabs_for_min_size);
  870. ClassDB::bind_method(D_METHOD("get_use_hidden_tabs_for_min_size"), &TabContainer::get_use_hidden_tabs_for_min_size);
  871. ClassDB::bind_method(D_METHOD("_child_renamed_callback"), &TabContainer::_child_renamed_callback);
  872. ClassDB::bind_method(D_METHOD("_on_theme_changed"), &TabContainer::_on_theme_changed);
  873. ClassDB::bind_method(D_METHOD("_on_mouse_exited"), &TabContainer::_on_mouse_exited);
  874. ClassDB::bind_method(D_METHOD("_update_current_tab"), &TabContainer::_update_current_tab);
  875. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  876. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  877. ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
  878. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
  879. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
  880. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
  881. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "all_tabs_in_front"), "set_all_tabs_in_front", "is_all_tabs_in_front");
  882. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  883. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hidden_tabs_for_min_size"), "set_use_hidden_tabs_for_min_size", "get_use_hidden_tabs_for_min_size");
  884. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  885. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  886. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  887. }
  888. TabContainer::TabContainer() {
  889. first_tab_cache = 0;
  890. last_tab_cache = 0;
  891. buttons_visible_cache = false;
  892. menu_hovered = false;
  893. highlight_arrow = -1;
  894. tabs_ofs_cache = 0;
  895. current = 0;
  896. previous = 0;
  897. align = ALIGN_CENTER;
  898. tabs_visible = true;
  899. popup_obj_id = 0;
  900. all_tabs_in_front = false;
  901. drag_to_rearrange_enabled = false;
  902. tabs_rearrange_group = -1;
  903. use_hidden_tabs_for_min_size = false;
  904. connect("mouse_exited", this, "_on_mouse_exited");
  905. }