tab_container.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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 (get_tab_disabled(index)) {
  309. _draw_tab(tab_disabled, font_color_disabled, index, tabs_ofs_cache + x);
  310. } else if (index == current) {
  311. x_current = 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. _draw_tab(tab_fg, font_color_fg, current, tabs_ofs_cache + x_current);
  325. }
  326. // Draw the popup menu.
  327. x = get_size().width;
  328. if (popup) {
  329. x -= menu->get_width();
  330. if (menu_hovered)
  331. menu_hl->draw(get_canvas_item(), Size2(x, (header_height - menu_hl->get_height()) / 2));
  332. else
  333. menu->draw(get_canvas_item(), Size2(x, (header_height - menu->get_height()) / 2));
  334. }
  335. // Draw the navigation buttons.
  336. if (buttons_visible_cache) {
  337. x -= increment->get_width();
  338. if (last_tab_cache < tabs.size() - 1) {
  339. draw_texture(highlight_arrow == 1 ? increment_hl : increment, Point2(x, (header_height - increment->get_height()) / 2));
  340. } else {
  341. draw_texture(increment, Point2(x, (header_height - increment->get_height()) / 2), Color(1, 1, 1, 0.5));
  342. }
  343. x -= decrement->get_width();
  344. if (first_tab_cache > 0) {
  345. draw_texture(highlight_arrow == 0 ? decrement_hl : decrement, Point2(x, (header_height - decrement->get_height()) / 2));
  346. } else {
  347. draw_texture(decrement, Point2(x, (header_height - decrement->get_height()) / 2), Color(1, 1, 1, 0.5));
  348. }
  349. }
  350. } break;
  351. case NOTIFICATION_THEME_CHANGED: {
  352. minimum_size_changed();
  353. call_deferred("_on_theme_changed"); // Wait until all changed theme.
  354. } break;
  355. }
  356. }
  357. void TabContainer::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x) {
  358. Vector<Control *> tabs = _get_tabs();
  359. RID canvas = get_canvas_item();
  360. Ref<Font> font = get_font("font");
  361. int icon_text_distance = get_constant("hseparation");
  362. int tab_width = _get_tab_width(p_index);
  363. int header_height = _get_top_margin();
  364. // Draw the tab background.
  365. Rect2 tab_rect(p_x, 0, tab_width, header_height);
  366. p_tab_style->draw(canvas, tab_rect);
  367. // Draw the tab contents.
  368. Control *control = Object::cast_to<Control>(tabs[p_index]);
  369. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
  370. int x_content = tab_rect.position.x + p_tab_style->get_margin(MARGIN_LEFT);
  371. int top_margin = p_tab_style->get_margin(MARGIN_TOP);
  372. int y_center = top_margin + (tab_rect.size.y - p_tab_style->get_minimum_size().y) / 2;
  373. // Draw the tab icon.
  374. if (control->has_meta("_tab_icon")) {
  375. Ref<Texture> icon = control->get_meta("_tab_icon");
  376. if (icon.is_valid()) {
  377. int y = y_center - (icon->get_height() / 2);
  378. icon->draw(canvas, Point2i(x_content, y));
  379. if (text != "") {
  380. x_content += icon->get_width() + icon_text_distance;
  381. }
  382. }
  383. }
  384. // Draw the tab text.
  385. Point2i text_pos(x_content, y_center - (font->get_height() / 2) + font->get_ascent());
  386. font->draw(canvas, text_pos, text, p_font_color);
  387. }
  388. void TabContainer::_on_theme_changed() {
  389. if (get_tab_count() > 0) {
  390. _repaint();
  391. update();
  392. }
  393. }
  394. void TabContainer::_repaint() {
  395. Ref<StyleBox> sb = get_stylebox("panel");
  396. Vector<Control *> tabs = _get_tabs();
  397. for (int i = 0; i < tabs.size(); i++) {
  398. Control *c = tabs[i];
  399. if (i == current) {
  400. c->show();
  401. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  402. if (tabs_visible) {
  403. c->set_margin(MARGIN_TOP, _get_top_margin());
  404. }
  405. c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
  406. c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
  407. c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
  408. c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
  409. } else {
  410. c->hide();
  411. }
  412. }
  413. }
  414. void TabContainer::_on_mouse_exited() {
  415. if (menu_hovered || highlight_arrow > -1) {
  416. menu_hovered = false;
  417. highlight_arrow = -1;
  418. update();
  419. }
  420. }
  421. int TabContainer::_get_tab_width(int p_index) const {
  422. ERR_FAIL_INDEX_V(p_index, get_tab_count(), 0);
  423. Control *control = Object::cast_to<Control>(_get_tabs()[p_index]);
  424. if (!control || control->is_set_as_toplevel() || get_tab_hidden(p_index))
  425. return 0;
  426. // Get the width of the text displayed on the tab.
  427. Ref<Font> font = get_font("font");
  428. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
  429. int width = font->get_string_size(text).width;
  430. // Add space for a tab icon.
  431. if (control->has_meta("_tab_icon")) {
  432. Ref<Texture> icon = control->get_meta("_tab_icon");
  433. if (icon.is_valid()) {
  434. width += icon->get_width();
  435. if (text != "")
  436. width += get_constant("hseparation");
  437. }
  438. }
  439. // Respect a minimum size.
  440. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  441. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  442. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  443. if (get_tab_disabled(p_index)) {
  444. width += tab_disabled->get_minimum_size().width;
  445. } else if (p_index == current) {
  446. width += tab_fg->get_minimum_size().width;
  447. } else {
  448. width += tab_bg->get_minimum_size().width;
  449. }
  450. return width;
  451. }
  452. Vector<Control *> TabContainer::_get_tabs() const {
  453. Vector<Control *> controls;
  454. for (int i = 0; i < get_child_count(); i++) {
  455. Control *control = Object::cast_to<Control>(get_child(i));
  456. if (!control || control->is_toplevel_control())
  457. continue;
  458. controls.push_back(control);
  459. }
  460. return controls;
  461. }
  462. void TabContainer::_child_renamed_callback() {
  463. update();
  464. }
  465. void TabContainer::add_child_notify(Node *p_child) {
  466. Container::add_child_notify(p_child);
  467. Control *c = Object::cast_to<Control>(p_child);
  468. if (!c)
  469. return;
  470. if (c->is_set_as_toplevel())
  471. return;
  472. bool first = false;
  473. if (get_tab_count() != 1)
  474. c->hide();
  475. else {
  476. c->show();
  477. //call_deferred("set_current_tab",0);
  478. first = true;
  479. current = 0;
  480. previous = 0;
  481. }
  482. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  483. if (tabs_visible)
  484. c->set_margin(MARGIN_TOP, _get_top_margin());
  485. Ref<StyleBox> sb = get_stylebox("panel");
  486. c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
  487. c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
  488. c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
  489. c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
  490. update();
  491. p_child->connect("renamed", this, "_child_renamed_callback");
  492. if (first)
  493. emit_signal("tab_changed", current);
  494. }
  495. int TabContainer::get_tab_count() const {
  496. return _get_tabs().size();
  497. }
  498. void TabContainer::set_current_tab(int p_current) {
  499. ERR_FAIL_INDEX(p_current, get_tab_count());
  500. int pending_previous = current;
  501. current = p_current;
  502. _repaint();
  503. _change_notify("current_tab");
  504. if (pending_previous == current)
  505. emit_signal("tab_selected", current);
  506. else {
  507. previous = pending_previous;
  508. emit_signal("tab_selected", current);
  509. emit_signal("tab_changed", current);
  510. }
  511. update();
  512. }
  513. int TabContainer::get_current_tab() const {
  514. return current;
  515. }
  516. int TabContainer::get_previous_tab() const {
  517. return previous;
  518. }
  519. Control *TabContainer::get_tab_control(int p_idx) const {
  520. Vector<Control *> tabs = _get_tabs();
  521. if (p_idx >= 0 && p_idx < tabs.size())
  522. return tabs[p_idx];
  523. else
  524. return NULL;
  525. }
  526. Control *TabContainer::get_current_tab_control() const {
  527. Vector<Control *> tabs = _get_tabs();
  528. if (current >= 0 && current < tabs.size())
  529. return tabs[current];
  530. else
  531. return NULL;
  532. }
  533. void TabContainer::remove_child_notify(Node *p_child) {
  534. Container::remove_child_notify(p_child);
  535. if (!Object::cast_to<Control>(p_child)) {
  536. return;
  537. }
  538. call_deferred("_update_current_tab");
  539. p_child->disconnect("renamed", this, "_child_renamed_callback");
  540. update();
  541. }
  542. void TabContainer::_update_current_tab() {
  543. int tc = get_tab_count();
  544. if (current >= tc)
  545. current = tc - 1;
  546. if (current < 0)
  547. current = 0;
  548. else
  549. set_current_tab(current);
  550. }
  551. Variant TabContainer::get_drag_data(const Point2 &p_point) {
  552. if (!drag_to_rearrange_enabled)
  553. return Variant();
  554. int tab_over = get_tab_idx_at_point(p_point);
  555. if (tab_over < 0)
  556. return Variant();
  557. HBoxContainer *drag_preview = memnew(HBoxContainer);
  558. Ref<Texture> icon = get_tab_icon(tab_over);
  559. if (!icon.is_null()) {
  560. TextureRect *tf = memnew(TextureRect);
  561. tf->set_texture(icon);
  562. drag_preview->add_child(tf);
  563. }
  564. Label *label = memnew(Label(get_tab_title(tab_over)));
  565. drag_preview->add_child(label);
  566. set_drag_preview(drag_preview);
  567. Dictionary drag_data;
  568. drag_data["type"] = "tabc_element";
  569. drag_data["tabc_element"] = tab_over;
  570. drag_data["from_path"] = get_path();
  571. return drag_data;
  572. }
  573. bool TabContainer::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  574. if (!drag_to_rearrange_enabled)
  575. return false;
  576. Dictionary d = p_data;
  577. if (!d.has("type"))
  578. return false;
  579. if (String(d["type"]) == "tabc_element") {
  580. NodePath from_path = d["from_path"];
  581. NodePath to_path = get_path();
  582. if (from_path == to_path) {
  583. return true;
  584. } else if (get_tabs_rearrange_group() != -1) {
  585. // drag and drop between other TabContainers
  586. Node *from_node = get_node(from_path);
  587. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  588. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  589. return true;
  590. }
  591. }
  592. }
  593. return false;
  594. }
  595. void TabContainer::drop_data(const Point2 &p_point, const Variant &p_data) {
  596. if (!drag_to_rearrange_enabled)
  597. return;
  598. int hover_now = get_tab_idx_at_point(p_point);
  599. Dictionary d = p_data;
  600. if (!d.has("type"))
  601. return;
  602. if (String(d["type"]) == "tabc_element") {
  603. int tab_from_id = d["tabc_element"];
  604. NodePath from_path = d["from_path"];
  605. NodePath to_path = get_path();
  606. if (from_path == to_path) {
  607. if (hover_now < 0)
  608. hover_now = get_tab_count() - 1;
  609. move_child(get_tab_control(tab_from_id), hover_now);
  610. set_current_tab(hover_now);
  611. } else if (get_tabs_rearrange_group() != -1) {
  612. // drag and drop between TabContainers
  613. Node *from_node = get_node(from_path);
  614. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  615. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  616. Control *moving_tabc = from_tabc->get_tab_control(tab_from_id);
  617. from_tabc->remove_child(moving_tabc);
  618. add_child(moving_tabc);
  619. if (hover_now < 0)
  620. hover_now = get_tab_count() - 1;
  621. move_child(moving_tabc, hover_now);
  622. set_current_tab(hover_now);
  623. emit_signal("tab_changed", hover_now);
  624. }
  625. }
  626. }
  627. update();
  628. }
  629. int TabContainer::get_tab_idx_at_point(const Point2 &p_point) const {
  630. if (get_tab_count() == 0)
  631. return -1;
  632. // must be on tabs in the tab header area.
  633. if (p_point.x < tabs_ofs_cache || p_point.y > _get_top_margin())
  634. return -1;
  635. Size2 size = get_size();
  636. int right_ofs = 0;
  637. Popup *popup = get_popup();
  638. if (popup) {
  639. Ref<Texture> menu = get_icon("menu");
  640. right_ofs += menu->get_width();
  641. }
  642. if (buttons_visible_cache) {
  643. Ref<Texture> increment = get_icon("increment");
  644. Ref<Texture> decrement = get_icon("decrement");
  645. right_ofs += increment->get_width() + decrement->get_width();
  646. }
  647. if (p_point.x > size.width - right_ofs) {
  648. return -1;
  649. }
  650. // get the tab at the point
  651. Vector<Control *> tabs = _get_tabs();
  652. int px = p_point.x;
  653. px -= tabs_ofs_cache;
  654. for (int i = first_tab_cache; i <= last_tab_cache; i++) {
  655. int tab_width = _get_tab_width(i);
  656. if (px < tab_width) {
  657. return i;
  658. }
  659. px -= tab_width;
  660. }
  661. return -1;
  662. }
  663. void TabContainer::set_tab_align(TabAlign p_align) {
  664. ERR_FAIL_INDEX(p_align, 3);
  665. align = p_align;
  666. update();
  667. _change_notify("tab_align");
  668. }
  669. TabContainer::TabAlign TabContainer::get_tab_align() const {
  670. return align;
  671. }
  672. void TabContainer::set_tabs_visible(bool p_visible) {
  673. if (p_visible == tabs_visible)
  674. return;
  675. tabs_visible = p_visible;
  676. Vector<Control *> tabs = _get_tabs();
  677. for (int i = 0; i < tabs.size(); i++) {
  678. Control *c = tabs[i];
  679. if (p_visible)
  680. c->set_margin(MARGIN_TOP, _get_top_margin());
  681. else
  682. c->set_margin(MARGIN_TOP, 0);
  683. }
  684. update();
  685. minimum_size_changed();
  686. }
  687. bool TabContainer::are_tabs_visible() const {
  688. return tabs_visible;
  689. }
  690. void TabContainer::set_all_tabs_in_front(bool p_in_front) {
  691. if (p_in_front == all_tabs_in_front) {
  692. return;
  693. }
  694. all_tabs_in_front = p_in_front;
  695. update();
  696. }
  697. bool TabContainer::is_all_tabs_in_front() const {
  698. return all_tabs_in_front;
  699. }
  700. Control *TabContainer::_get_tab(int p_idx) const {
  701. return get_tab_control(p_idx);
  702. }
  703. void TabContainer::set_tab_title(int p_tab, const String &p_title) {
  704. Control *child = _get_tab(p_tab);
  705. ERR_FAIL_COND(!child);
  706. child->set_meta("_tab_name", p_title);
  707. update();
  708. }
  709. String TabContainer::get_tab_title(int p_tab) const {
  710. Control *child = _get_tab(p_tab);
  711. ERR_FAIL_COND_V(!child, "");
  712. if (child->has_meta("_tab_name"))
  713. return child->get_meta("_tab_name");
  714. else
  715. return child->get_name();
  716. }
  717. void TabContainer::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) {
  718. Control *child = _get_tab(p_tab);
  719. ERR_FAIL_COND(!child);
  720. child->set_meta("_tab_icon", p_icon);
  721. update();
  722. }
  723. Ref<Texture> TabContainer::get_tab_icon(int p_tab) const {
  724. Control *child = _get_tab(p_tab);
  725. ERR_FAIL_COND_V(!child, Ref<Texture>());
  726. if (child->has_meta("_tab_icon"))
  727. return child->get_meta("_tab_icon");
  728. else
  729. return Ref<Texture>();
  730. }
  731. void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) {
  732. Control *child = _get_tab(p_tab);
  733. ERR_FAIL_COND(!child);
  734. child->set_meta("_tab_disabled", p_disabled);
  735. update();
  736. }
  737. bool TabContainer::get_tab_disabled(int p_tab) const {
  738. Control *child = _get_tab(p_tab);
  739. ERR_FAIL_COND_V(!child, false);
  740. if (child->has_meta("_tab_disabled"))
  741. return child->get_meta("_tab_disabled");
  742. else
  743. return false;
  744. }
  745. void TabContainer::set_tab_hidden(int p_tab, bool p_hidden) {
  746. Control *child = _get_tab(p_tab);
  747. ERR_FAIL_COND(!child);
  748. child->set_meta("_tab_hidden", p_hidden);
  749. update();
  750. for (int i = 0; i < get_tab_count(); i++) {
  751. int try_tab = (p_tab + 1 + i) % get_tab_count();
  752. if (get_tab_disabled(try_tab) || get_tab_hidden(try_tab)) {
  753. continue;
  754. }
  755. set_current_tab(try_tab);
  756. return;
  757. }
  758. //assumed no other tab can be switched to, just hide
  759. child->hide();
  760. }
  761. bool TabContainer::get_tab_hidden(int p_tab) const {
  762. Control *child = _get_tab(p_tab);
  763. ERR_FAIL_COND_V(!child, false);
  764. if (child->has_meta("_tab_hidden"))
  765. return child->get_meta("_tab_hidden");
  766. else
  767. return false;
  768. }
  769. void TabContainer::get_translatable_strings(List<String> *p_strings) const {
  770. Vector<Control *> tabs = _get_tabs();
  771. for (int i = 0; i < tabs.size(); i++) {
  772. Control *c = tabs[i];
  773. if (!c->has_meta("_tab_name"))
  774. continue;
  775. String name = c->get_meta("_tab_name");
  776. if (name != "")
  777. p_strings->push_back(name);
  778. }
  779. }
  780. Size2 TabContainer::get_minimum_size() const {
  781. Size2 ms;
  782. Vector<Control *> tabs = _get_tabs();
  783. for (int i = 0; i < tabs.size(); i++) {
  784. Control *c = tabs[i];
  785. if (!c->is_visible_in_tree() && !use_hidden_tabs_for_min_size)
  786. continue;
  787. Size2 cms = c->get_combined_minimum_size();
  788. ms.x = MAX(ms.x, cms.x);
  789. ms.y = MAX(ms.y, cms.y);
  790. }
  791. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  792. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  793. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  794. Ref<Font> font = get_font("font");
  795. if (tabs_visible) {
  796. ms.y += MAX(MAX(tab_bg->get_minimum_size().y, tab_fg->get_minimum_size().y), tab_disabled->get_minimum_size().y);
  797. ms.y += font->get_height();
  798. }
  799. Ref<StyleBox> sb = get_stylebox("panel");
  800. ms += sb->get_minimum_size();
  801. return ms;
  802. }
  803. void TabContainer::set_popup(Node *p_popup) {
  804. ERR_FAIL_NULL(p_popup);
  805. Popup *popup = Object::cast_to<Popup>(p_popup);
  806. popup_obj_id = popup ? popup->get_instance_id() : 0;
  807. update();
  808. }
  809. Popup *TabContainer::get_popup() const {
  810. if (popup_obj_id) {
  811. Popup *popup = Object::cast_to<Popup>(ObjectDB::get_instance(popup_obj_id));
  812. if (popup) {
  813. return popup;
  814. } else {
  815. #ifdef DEBUG_ENABLED
  816. ERR_PRINT("Popup assigned to TabContainer is gone!");
  817. #endif
  818. popup_obj_id = 0;
  819. }
  820. }
  821. return NULL;
  822. }
  823. void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
  824. drag_to_rearrange_enabled = p_enabled;
  825. }
  826. bool TabContainer::get_drag_to_rearrange_enabled() const {
  827. return drag_to_rearrange_enabled;
  828. }
  829. void TabContainer::set_tabs_rearrange_group(int p_group_id) {
  830. tabs_rearrange_group = p_group_id;
  831. }
  832. int TabContainer::get_tabs_rearrange_group() const {
  833. return tabs_rearrange_group;
  834. }
  835. void TabContainer::set_use_hidden_tabs_for_min_size(bool p_use_hidden_tabs) {
  836. use_hidden_tabs_for_min_size = p_use_hidden_tabs;
  837. }
  838. bool TabContainer::get_use_hidden_tabs_for_min_size() const {
  839. return use_hidden_tabs_for_min_size;
  840. }
  841. void TabContainer::_bind_methods() {
  842. ClassDB::bind_method(D_METHOD("_gui_input"), &TabContainer::_gui_input);
  843. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabContainer::get_tab_count);
  844. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
  845. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabContainer::get_current_tab);
  846. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabContainer::get_previous_tab);
  847. ClassDB::bind_method(D_METHOD("get_current_tab_control"), &TabContainer::get_current_tab_control);
  848. ClassDB::bind_method(D_METHOD("get_tab_control", "tab_idx"), &TabContainer::get_tab_control);
  849. ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &TabContainer::set_tab_align);
  850. ClassDB::bind_method(D_METHOD("get_tab_align"), &TabContainer::get_tab_align);
  851. ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
  852. ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
  853. ClassDB::bind_method(D_METHOD("set_all_tabs_in_front", "is_front"), &TabContainer::set_all_tabs_in_front);
  854. ClassDB::bind_method(D_METHOD("is_all_tabs_in_front"), &TabContainer::is_all_tabs_in_front);
  855. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
  856. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
  857. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon);
  858. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon);
  859. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled);
  860. ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &TabContainer::get_tab_disabled);
  861. ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
  862. ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
  863. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabContainer::set_drag_to_rearrange_enabled);
  864. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabContainer::get_drag_to_rearrange_enabled);
  865. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabContainer::set_tabs_rearrange_group);
  866. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabContainer::get_tabs_rearrange_group);
  867. ClassDB::bind_method(D_METHOD("set_use_hidden_tabs_for_min_size", "enabled"), &TabContainer::set_use_hidden_tabs_for_min_size);
  868. ClassDB::bind_method(D_METHOD("get_use_hidden_tabs_for_min_size"), &TabContainer::get_use_hidden_tabs_for_min_size);
  869. ClassDB::bind_method(D_METHOD("_child_renamed_callback"), &TabContainer::_child_renamed_callback);
  870. ClassDB::bind_method(D_METHOD("_on_theme_changed"), &TabContainer::_on_theme_changed);
  871. ClassDB::bind_method(D_METHOD("_on_mouse_exited"), &TabContainer::_on_mouse_exited);
  872. ClassDB::bind_method(D_METHOD("_update_current_tab"), &TabContainer::_update_current_tab);
  873. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  874. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  875. ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
  876. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
  877. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
  878. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
  879. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "all_tabs_in_front"), "set_all_tabs_in_front", "is_all_tabs_in_front");
  880. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  881. 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");
  882. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  883. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  884. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  885. }
  886. TabContainer::TabContainer() {
  887. first_tab_cache = 0;
  888. last_tab_cache = 0;
  889. buttons_visible_cache = false;
  890. menu_hovered = false;
  891. highlight_arrow = -1;
  892. tabs_ofs_cache = 0;
  893. current = 0;
  894. previous = 0;
  895. align = ALIGN_CENTER;
  896. tabs_visible = true;
  897. popup_obj_id = 0;
  898. all_tabs_in_front = false;
  899. drag_to_rearrange_enabled = false;
  900. tabs_rearrange_group = -1;
  901. use_hidden_tabs_for_min_size = false;
  902. connect("mouse_exited", this, "_on_mouse_exited");
  903. }