2
0

tab_container.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /**************************************************************************/
  2. /* tab_container.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_container.h"
  31. #include "scene/gui/box_container.h"
  32. #include "scene/gui/label.h"
  33. #include "scene/gui/texture_rect.h"
  34. int TabContainer::_get_top_margin() const {
  35. int height = 0;
  36. if (tabs_visible && get_tab_count() > 0) {
  37. height = tab_bar->get_minimum_size().height;
  38. }
  39. return height;
  40. }
  41. void TabContainer::gui_input(const Ref<InputEvent> &p_event) {
  42. ERR_FAIL_COND(p_event.is_null());
  43. Ref<InputEventMouseButton> mb = p_event;
  44. Popup *popup = get_popup();
  45. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  46. Point2 pos = mb->get_position();
  47. Size2 size = get_size();
  48. // Click must be on tabs in the tab header area.
  49. if (pos.y > _get_top_margin()) {
  50. return;
  51. }
  52. // Handle menu button.
  53. if (is_layout_rtl()) {
  54. if (popup && pos.x < theme_cache.menu_icon->get_width()) {
  55. emit_signal(SNAME("pre_popup_pressed"));
  56. Vector2 popup_pos = get_screen_position();
  57. popup_pos.y += theme_cache.menu_icon->get_height();
  58. popup->set_position(popup_pos);
  59. popup->popup();
  60. return;
  61. }
  62. } else {
  63. if (popup && pos.x > size.width - theme_cache.menu_icon->get_width()) {
  64. emit_signal(SNAME("pre_popup_pressed"));
  65. Vector2 popup_pos = get_screen_position();
  66. popup_pos.x += size.width - popup->get_size().width;
  67. popup_pos.y += theme_cache.menu_icon->get_height();
  68. popup->set_position(popup_pos);
  69. popup->popup();
  70. return;
  71. }
  72. }
  73. }
  74. Ref<InputEventMouseMotion> mm = p_event;
  75. if (mm.is_valid()) {
  76. Point2 pos = mm->get_position();
  77. Size2 size = get_size();
  78. // Mouse must be on tabs in the tab header area.
  79. if (pos.y > _get_top_margin()) {
  80. if (menu_hovered) {
  81. menu_hovered = false;
  82. queue_redraw();
  83. }
  84. return;
  85. }
  86. if (popup) {
  87. if (is_layout_rtl()) {
  88. if (pos.x <= theme_cache.menu_icon->get_width()) {
  89. if (!menu_hovered) {
  90. menu_hovered = true;
  91. queue_redraw();
  92. return;
  93. }
  94. } else if (menu_hovered) {
  95. menu_hovered = false;
  96. queue_redraw();
  97. }
  98. } else {
  99. if (pos.x >= size.width - theme_cache.menu_icon->get_width()) {
  100. if (!menu_hovered) {
  101. menu_hovered = true;
  102. queue_redraw();
  103. return;
  104. }
  105. } else if (menu_hovered) {
  106. menu_hovered = false;
  107. queue_redraw();
  108. }
  109. }
  110. if (menu_hovered) {
  111. return;
  112. }
  113. }
  114. }
  115. }
  116. void TabContainer::_update_theme_item_cache() {
  117. Container::_update_theme_item_cache();
  118. theme_cache.side_margin = get_theme_constant(SNAME("side_margin"));
  119. theme_cache.panel_style = get_theme_stylebox(SNAME("panel"));
  120. theme_cache.tabbar_style = get_theme_stylebox(SNAME("tabbar_background"));
  121. theme_cache.menu_icon = get_theme_icon(SNAME("menu"));
  122. theme_cache.menu_hl_icon = get_theme_icon(SNAME("menu_highlight"));
  123. // TabBar overrides.
  124. theme_cache.icon_separation = get_theme_constant(SNAME("icon_separation"));
  125. theme_cache.outline_size = get_theme_constant(SNAME("outline_size"));
  126. theme_cache.tab_unselected_style = get_theme_stylebox(SNAME("tab_unselected"));
  127. theme_cache.tab_selected_style = get_theme_stylebox(SNAME("tab_selected"));
  128. theme_cache.tab_disabled_style = get_theme_stylebox(SNAME("tab_disabled"));
  129. theme_cache.increment_icon = get_theme_icon(SNAME("increment"));
  130. theme_cache.increment_hl_icon = get_theme_icon(SNAME("increment_highlight"));
  131. theme_cache.decrement_icon = get_theme_icon(SNAME("decrement"));
  132. theme_cache.decrement_hl_icon = get_theme_icon(SNAME("decrement_highlight"));
  133. theme_cache.drop_mark_icon = get_theme_icon(SNAME("drop_mark"));
  134. theme_cache.drop_mark_color = get_theme_color(SNAME("drop_mark_color"));
  135. theme_cache.font_selected_color = get_theme_color(SNAME("font_selected_color"));
  136. theme_cache.font_unselected_color = get_theme_color(SNAME("font_unselected_color"));
  137. theme_cache.font_disabled_color = get_theme_color(SNAME("font_disabled_color"));
  138. theme_cache.font_outline_color = get_theme_color(SNAME("font_outline_color"));
  139. theme_cache.tab_font = get_theme_font(SNAME("font"));
  140. theme_cache.tab_font_size = get_theme_font_size(SNAME("font_size"));
  141. }
  142. void TabContainer::_notification(int p_what) {
  143. switch (p_what) {
  144. case NOTIFICATION_ENTER_TREE: {
  145. // If some nodes happen to be renamed outside the tree, the tab names need to be updated manually.
  146. if (get_tab_count() > 0) {
  147. _refresh_tab_names();
  148. }
  149. } break;
  150. case NOTIFICATION_READY:
  151. case NOTIFICATION_RESIZED: {
  152. _update_margins();
  153. } break;
  154. case NOTIFICATION_DRAW: {
  155. RID canvas = get_canvas_item();
  156. Size2 size = get_size();
  157. // Draw only the tab area if the header is hidden.
  158. if (!tabs_visible) {
  159. theme_cache.panel_style->draw(canvas, Rect2(0, 0, size.width, size.height));
  160. return;
  161. }
  162. int header_height = _get_top_margin();
  163. // Draw background for the tabbar.
  164. theme_cache.tabbar_style->draw(canvas, Rect2(0, 0, size.width, header_height));
  165. // Draw the background for the tab's content.
  166. theme_cache.panel_style->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
  167. // Draw the popup menu.
  168. if (get_popup()) {
  169. int x = is_layout_rtl() ? 0 : get_size().width - theme_cache.menu_icon->get_width();
  170. if (menu_hovered) {
  171. theme_cache.menu_hl_icon->draw(get_canvas_item(), Point2(x, (header_height - theme_cache.menu_hl_icon->get_height()) / 2));
  172. } else {
  173. theme_cache.menu_icon->draw(get_canvas_item(), Point2(x, (header_height - theme_cache.menu_icon->get_height()) / 2));
  174. }
  175. }
  176. } break;
  177. case NOTIFICATION_TRANSLATION_CHANGED:
  178. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  179. case NOTIFICATION_THEME_CHANGED: {
  180. theme_changing = true;
  181. call_deferred(SNAME("_on_theme_changed")); // Wait until all changed theme.
  182. } break;
  183. }
  184. }
  185. void TabContainer::_on_theme_changed() {
  186. if (!theme_changing) {
  187. return;
  188. }
  189. tab_bar->add_theme_style_override(SNAME("tab_unselected"), theme_cache.tab_unselected_style);
  190. tab_bar->add_theme_style_override(SNAME("tab_selected"), theme_cache.tab_selected_style);
  191. tab_bar->add_theme_style_override(SNAME("tab_disabled"), theme_cache.tab_disabled_style);
  192. tab_bar->add_theme_icon_override(SNAME("increment"), theme_cache.increment_icon);
  193. tab_bar->add_theme_icon_override(SNAME("increment_highlight"), theme_cache.increment_hl_icon);
  194. tab_bar->add_theme_icon_override(SNAME("decrement"), theme_cache.decrement_icon);
  195. tab_bar->add_theme_icon_override(SNAME("decrement_highlight"), theme_cache.decrement_hl_icon);
  196. tab_bar->add_theme_icon_override(SNAME("drop_mark"), theme_cache.drop_mark_icon);
  197. tab_bar->add_theme_color_override(SNAME("drop_mark_color"), theme_cache.drop_mark_color);
  198. tab_bar->add_theme_color_override(SNAME("font_selected_color"), theme_cache.font_selected_color);
  199. tab_bar->add_theme_color_override(SNAME("font_unselected_color"), theme_cache.font_unselected_color);
  200. tab_bar->add_theme_color_override(SNAME("font_disabled_color"), theme_cache.font_disabled_color);
  201. tab_bar->add_theme_color_override(SNAME("font_outline_color"), theme_cache.font_outline_color);
  202. tab_bar->add_theme_font_override(SNAME("font"), theme_cache.tab_font);
  203. tab_bar->add_theme_font_size_override(SNAME("font_size"), theme_cache.tab_font_size);
  204. tab_bar->add_theme_constant_override(SNAME("h_separation"), theme_cache.icon_separation);
  205. tab_bar->add_theme_constant_override(SNAME("outline_size"), theme_cache.outline_size);
  206. _update_margins();
  207. if (get_tab_count() > 0) {
  208. _repaint();
  209. } else {
  210. update_minimum_size();
  211. }
  212. queue_redraw();
  213. theme_changing = false;
  214. }
  215. void TabContainer::_repaint() {
  216. Vector<Control *> controls = _get_tab_controls();
  217. int current = get_current_tab();
  218. for (int i = 0; i < controls.size(); i++) {
  219. Control *c = controls[i];
  220. if (i == current) {
  221. c->show();
  222. c->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  223. if (tabs_visible) {
  224. c->set_offset(SIDE_TOP, _get_top_margin());
  225. }
  226. c->set_offset(SIDE_TOP, c->get_offset(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_TOP));
  227. c->set_offset(SIDE_LEFT, c->get_offset(SIDE_LEFT) + theme_cache.panel_style->get_margin(SIDE_LEFT));
  228. c->set_offset(SIDE_RIGHT, c->get_offset(SIDE_RIGHT) - theme_cache.panel_style->get_margin(SIDE_RIGHT));
  229. c->set_offset(SIDE_BOTTOM, c->get_offset(SIDE_BOTTOM) - theme_cache.panel_style->get_margin(SIDE_BOTTOM));
  230. } else {
  231. c->hide();
  232. }
  233. }
  234. update_minimum_size();
  235. }
  236. void TabContainer::_update_margins() {
  237. int menu_width = theme_cache.menu_icon->get_width();
  238. // Directly check for validity, to avoid errors when quitting.
  239. bool has_popup = popup_obj_id.is_valid();
  240. if (get_tab_count() == 0) {
  241. tab_bar->set_offset(SIDE_LEFT, 0);
  242. tab_bar->set_offset(SIDE_RIGHT, has_popup ? -menu_width : 0);
  243. return;
  244. }
  245. switch (get_tab_alignment()) {
  246. case TabBar::ALIGNMENT_LEFT: {
  247. tab_bar->set_offset(SIDE_LEFT, theme_cache.side_margin);
  248. tab_bar->set_offset(SIDE_RIGHT, has_popup ? -menu_width : 0);
  249. } break;
  250. case TabBar::ALIGNMENT_CENTER: {
  251. tab_bar->set_offset(SIDE_LEFT, 0);
  252. tab_bar->set_offset(SIDE_RIGHT, has_popup ? -menu_width : 0);
  253. } break;
  254. case TabBar::ALIGNMENT_RIGHT: {
  255. tab_bar->set_offset(SIDE_LEFT, 0);
  256. if (has_popup) {
  257. tab_bar->set_offset(SIDE_RIGHT, -menu_width);
  258. return;
  259. }
  260. int first_tab_pos = tab_bar->get_tab_rect(0).position.x;
  261. Rect2 last_tab_rect = tab_bar->get_tab_rect(get_tab_count() - 1);
  262. int total_tabs_width = last_tab_rect.position.x - first_tab_pos + last_tab_rect.size.width;
  263. // Calculate if all the tabs would still fit if the margin was present.
  264. if (get_clip_tabs() && (tab_bar->get_offset_buttons_visible() || (get_tab_count() > 1 && (total_tabs_width + theme_cache.side_margin) > get_size().width))) {
  265. tab_bar->set_offset(SIDE_RIGHT, has_popup ? -menu_width : 0);
  266. } else {
  267. tab_bar->set_offset(SIDE_RIGHT, -theme_cache.side_margin);
  268. }
  269. } break;
  270. case TabBar::ALIGNMENT_MAX:
  271. break; // Can't happen, but silences warning.
  272. }
  273. }
  274. void TabContainer::_on_mouse_exited() {
  275. if (menu_hovered) {
  276. menu_hovered = false;
  277. queue_redraw();
  278. }
  279. }
  280. Vector<Control *> TabContainer::_get_tab_controls() const {
  281. Vector<Control *> controls;
  282. for (int i = 0; i < get_child_count(); i++) {
  283. Control *control = Object::cast_to<Control>(get_child(i));
  284. if (!control || control->is_set_as_top_level() || control == tab_bar || children_removing.has(control)) {
  285. continue;
  286. }
  287. controls.push_back(control);
  288. }
  289. return controls;
  290. }
  291. Variant TabContainer::_get_drag_data_fw(const Point2 &p_point, Control *p_from_control) {
  292. if (!drag_to_rearrange_enabled) {
  293. return Variant();
  294. }
  295. int tab_over = get_tab_idx_at_point(p_point);
  296. if (tab_over < 0) {
  297. return Variant();
  298. }
  299. HBoxContainer *drag_preview = memnew(HBoxContainer);
  300. Ref<Texture2D> icon = get_tab_icon(tab_over);
  301. if (!icon.is_null()) {
  302. TextureRect *tf = memnew(TextureRect);
  303. tf->set_texture(icon);
  304. drag_preview->add_child(tf);
  305. }
  306. Label *label = memnew(Label(get_tab_title(tab_over)));
  307. set_drag_preview(drag_preview);
  308. drag_preview->add_child(label);
  309. Dictionary drag_data;
  310. drag_data["type"] = "tabc_element";
  311. drag_data["tabc_element"] = tab_over;
  312. drag_data["from_path"] = get_path();
  313. return drag_data;
  314. }
  315. bool TabContainer::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const {
  316. if (!drag_to_rearrange_enabled) {
  317. return false;
  318. }
  319. Dictionary d = p_data;
  320. if (!d.has("type")) {
  321. return false;
  322. }
  323. if (String(d["type"]) == "tabc_element") {
  324. NodePath from_path = d["from_path"];
  325. NodePath to_path = get_path();
  326. if (from_path == to_path) {
  327. return true;
  328. } else if (get_tabs_rearrange_group() != -1) {
  329. // Drag and drop between other TabContainers.
  330. Node *from_node = get_node(from_path);
  331. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  332. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  333. return true;
  334. }
  335. }
  336. }
  337. return false;
  338. }
  339. void TabContainer::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) {
  340. if (!drag_to_rearrange_enabled) {
  341. return;
  342. }
  343. Dictionary d = p_data;
  344. if (!d.has("type")) {
  345. return;
  346. }
  347. if (String(d["type"]) == "tabc_element") {
  348. int tab_from_id = d["tabc_element"];
  349. int hover_now = get_tab_idx_at_point(p_point);
  350. NodePath from_path = d["from_path"];
  351. NodePath to_path = get_path();
  352. if (from_path == to_path) {
  353. if (tab_from_id == hover_now) {
  354. return;
  355. }
  356. // Drop the new tab to the left or right depending on where the target tab is being hovered.
  357. if (hover_now != -1) {
  358. Rect2 tab_rect = tab_bar->get_tab_rect(hover_now);
  359. if (is_layout_rtl() ^ (p_point.x <= tab_rect.position.x + tab_rect.size.width / 2)) {
  360. if (hover_now > tab_from_id) {
  361. hover_now -= 1;
  362. }
  363. } else if (tab_from_id > hover_now) {
  364. hover_now += 1;
  365. }
  366. } else {
  367. hover_now = is_layout_rtl() ^ (p_point.x < tab_bar->get_tab_rect(0).position.x) ? 0 : get_tab_count() - 1;
  368. }
  369. move_child(get_tab_control(tab_from_id), get_tab_control(hover_now)->get_index(false));
  370. if (!is_tab_disabled(hover_now)) {
  371. set_current_tab(hover_now);
  372. }
  373. } else if (get_tabs_rearrange_group() != -1) {
  374. // Drag and drop between TabContainers.
  375. Node *from_node = get_node(from_path);
  376. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  377. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  378. // Get the tab properties before they get erased by the child removal.
  379. String tab_title = from_tabc->get_tab_title(tab_from_id);
  380. bool tab_disabled = from_tabc->is_tab_disabled(tab_from_id);
  381. // Drop the new tab to the left or right depending on where the target tab is being hovered.
  382. if (hover_now != -1) {
  383. Rect2 tab_rect = tab_bar->get_tab_rect(hover_now);
  384. if (is_layout_rtl() ^ (p_point.x > tab_rect.position.x + tab_rect.size.width / 2)) {
  385. hover_now += 1;
  386. }
  387. } else {
  388. hover_now = is_layout_rtl() ^ (p_point.x < tab_bar->get_tab_rect(0).position.x) ? 0 : get_tab_count();
  389. }
  390. Control *moving_tabc = from_tabc->get_tab_control(tab_from_id);
  391. from_tabc->remove_child(moving_tabc);
  392. add_child(moving_tabc, true);
  393. set_tab_title(get_tab_count() - 1, tab_title);
  394. set_tab_disabled(get_tab_count() - 1, tab_disabled);
  395. move_child(moving_tabc, get_tab_control(hover_now)->get_index(false));
  396. if (!is_tab_disabled(hover_now)) {
  397. set_current_tab(hover_now);
  398. }
  399. }
  400. }
  401. }
  402. }
  403. void TabContainer::_on_tab_changed(int p_tab) {
  404. call_deferred(SNAME("_repaint"));
  405. emit_signal(SNAME("tab_changed"), p_tab);
  406. }
  407. void TabContainer::_on_tab_selected(int p_tab) {
  408. if (p_tab != get_previous_tab()) {
  409. call_deferred(SNAME("_repaint"));
  410. }
  411. emit_signal(SNAME("tab_selected"), p_tab);
  412. }
  413. void TabContainer::_on_tab_button_pressed(int p_tab) {
  414. emit_signal(SNAME("tab_button_pressed"), p_tab);
  415. }
  416. void TabContainer::_refresh_tab_names() {
  417. Vector<Control *> controls = _get_tab_controls();
  418. for (int i = 0; i < controls.size(); i++) {
  419. if (!controls[i]->has_meta("_tab_name") && String(controls[i]->get_name()) != get_tab_title(i)) {
  420. tab_bar->set_tab_title(i, controls[i]->get_name());
  421. }
  422. }
  423. }
  424. void TabContainer::add_child_notify(Node *p_child) {
  425. Container::add_child_notify(p_child);
  426. if (p_child == tab_bar) {
  427. return;
  428. }
  429. Control *c = Object::cast_to<Control>(p_child);
  430. if (!c || c->is_set_as_top_level()) {
  431. return;
  432. }
  433. c->hide();
  434. tab_bar->add_tab(p_child->get_name());
  435. _update_margins();
  436. if (get_tab_count() == 1) {
  437. queue_redraw();
  438. }
  439. p_child->connect("renamed", callable_mp(this, &TabContainer::_refresh_tab_names));
  440. // TabBar won't emit the "tab_changed" signal when not inside the tree.
  441. if (!is_inside_tree()) {
  442. call_deferred("_repaint");
  443. }
  444. }
  445. void TabContainer::move_child_notify(Node *p_child) {
  446. if (p_child == tab_bar) {
  447. return;
  448. }
  449. Container::move_child_notify(p_child);
  450. Control *c = Object::cast_to<Control>(p_child);
  451. if (c && !c->is_set_as_top_level()) {
  452. int old_idx = -1;
  453. String tab_name = String(c->get_meta("_tab_name", c->get_name()));
  454. // Find the previous tab index of the control.
  455. for (int i = 0; i < get_tab_count(); i++) {
  456. if (get_tab_title(i) == tab_name) {
  457. old_idx = i;
  458. break;
  459. }
  460. }
  461. tab_bar->move_tab(old_idx, get_tab_idx_from_control(c));
  462. }
  463. }
  464. void TabContainer::remove_child_notify(Node *p_child) {
  465. if (p_child == tab_bar) {
  466. return;
  467. }
  468. Container::remove_child_notify(p_child);
  469. Control *c = Object::cast_to<Control>(p_child);
  470. if (!c || c->is_set_as_top_level()) {
  471. return;
  472. }
  473. int idx = get_tab_idx_from_control(c);
  474. // As the child hasn't been removed yet, keep track of it so when the "tab_changed" signal is fired it can be ignored.
  475. children_removing.push_back(c);
  476. tab_bar->remove_tab(idx);
  477. children_removing.erase(c);
  478. _update_margins();
  479. if (get_tab_count() == 0) {
  480. queue_redraw();
  481. }
  482. p_child->remove_meta("_tab_name");
  483. p_child->disconnect("renamed", callable_mp(this, &TabContainer::_refresh_tab_names));
  484. // TabBar won't emit the "tab_changed" signal when not inside the tree.
  485. if (!is_inside_tree()) {
  486. call_deferred("_repaint");
  487. }
  488. }
  489. int TabContainer::get_tab_count() const {
  490. return tab_bar->get_tab_count();
  491. }
  492. void TabContainer::set_current_tab(int p_current) {
  493. tab_bar->set_current_tab(p_current);
  494. }
  495. int TabContainer::get_current_tab() const {
  496. return tab_bar->get_current_tab();
  497. }
  498. int TabContainer::get_previous_tab() const {
  499. return tab_bar->get_previous_tab();
  500. }
  501. Control *TabContainer::get_tab_control(int p_idx) const {
  502. Vector<Control *> controls = _get_tab_controls();
  503. if (p_idx >= 0 && p_idx < controls.size()) {
  504. return controls[p_idx];
  505. } else {
  506. return nullptr;
  507. }
  508. }
  509. Control *TabContainer::get_current_tab_control() const {
  510. return get_tab_control(tab_bar->get_current_tab());
  511. }
  512. int TabContainer::get_tab_idx_at_point(const Point2 &p_point) const {
  513. return tab_bar->get_tab_idx_at_point(p_point);
  514. }
  515. int TabContainer::get_tab_idx_from_control(Control *p_child) const {
  516. ERR_FAIL_NULL_V(p_child, -1);
  517. ERR_FAIL_COND_V(p_child->get_parent() != this, -1);
  518. Vector<Control *> controls = _get_tab_controls();
  519. for (int i = 0; i < controls.size(); i++) {
  520. if (controls[i] == p_child) {
  521. return i;
  522. }
  523. }
  524. return -1;
  525. }
  526. void TabContainer::set_tab_alignment(TabBar::AlignmentMode p_alignment) {
  527. if (tab_bar->get_tab_alignment() == p_alignment) {
  528. return;
  529. }
  530. tab_bar->set_tab_alignment(p_alignment);
  531. _update_margins();
  532. }
  533. TabBar::AlignmentMode TabContainer::get_tab_alignment() const {
  534. return tab_bar->get_tab_alignment();
  535. }
  536. void TabContainer::set_clip_tabs(bool p_clip_tabs) {
  537. tab_bar->set_clip_tabs(p_clip_tabs);
  538. }
  539. bool TabContainer::get_clip_tabs() const {
  540. return tab_bar->get_clip_tabs();
  541. }
  542. void TabContainer::set_tabs_visible(bool p_visible) {
  543. if (p_visible == tabs_visible) {
  544. return;
  545. }
  546. tabs_visible = p_visible;
  547. tab_bar->set_visible(tabs_visible);
  548. Vector<Control *> controls = _get_tab_controls();
  549. for (int i = 0; i < controls.size(); i++) {
  550. Control *c = controls[i];
  551. if (tabs_visible) {
  552. c->set_offset(SIDE_TOP, _get_top_margin());
  553. } else {
  554. c->set_offset(SIDE_TOP, 0);
  555. }
  556. }
  557. queue_redraw();
  558. update_minimum_size();
  559. }
  560. bool TabContainer::are_tabs_visible() const {
  561. return tabs_visible;
  562. }
  563. void TabContainer::set_all_tabs_in_front(bool p_in_front) {
  564. if (p_in_front == all_tabs_in_front) {
  565. return;
  566. }
  567. all_tabs_in_front = p_in_front;
  568. remove_child(tab_bar);
  569. add_child(tab_bar, false, all_tabs_in_front ? INTERNAL_MODE_FRONT : INTERNAL_MODE_BACK);
  570. }
  571. bool TabContainer::is_all_tabs_in_front() const {
  572. return all_tabs_in_front;
  573. }
  574. void TabContainer::set_tab_title(int p_tab, const String &p_title) {
  575. Control *child = get_tab_control(p_tab);
  576. ERR_FAIL_COND(!child);
  577. if (tab_bar->get_tab_title(p_tab) == p_title) {
  578. return;
  579. }
  580. tab_bar->set_tab_title(p_tab, p_title);
  581. if (p_title == child->get_name()) {
  582. child->remove_meta("_tab_name");
  583. } else {
  584. child->set_meta("_tab_name", p_title);
  585. }
  586. _update_margins();
  587. if (!get_clip_tabs()) {
  588. update_minimum_size();
  589. }
  590. }
  591. String TabContainer::get_tab_title(int p_tab) const {
  592. return tab_bar->get_tab_title(p_tab);
  593. }
  594. void TabContainer::set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  595. if (tab_bar->get_tab_icon(p_tab) == p_icon) {
  596. return;
  597. }
  598. tab_bar->set_tab_icon(p_tab, p_icon);
  599. _update_margins();
  600. _repaint();
  601. }
  602. Ref<Texture2D> TabContainer::get_tab_icon(int p_tab) const {
  603. return tab_bar->get_tab_icon(p_tab);
  604. }
  605. void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) {
  606. if (tab_bar->is_tab_disabled(p_tab) == p_disabled) {
  607. return;
  608. }
  609. tab_bar->set_tab_disabled(p_tab, p_disabled);
  610. _update_margins();
  611. if (!get_clip_tabs()) {
  612. update_minimum_size();
  613. }
  614. }
  615. bool TabContainer::is_tab_disabled(int p_tab) const {
  616. return tab_bar->is_tab_disabled(p_tab);
  617. }
  618. void TabContainer::set_tab_hidden(int p_tab, bool p_hidden) {
  619. Control *child = get_tab_control(p_tab);
  620. ERR_FAIL_COND(!child);
  621. if (tab_bar->is_tab_hidden(p_tab) == p_hidden) {
  622. return;
  623. }
  624. tab_bar->set_tab_hidden(p_tab, p_hidden);
  625. child->hide();
  626. _update_margins();
  627. if (!get_clip_tabs()) {
  628. update_minimum_size();
  629. }
  630. call_deferred(SNAME("_repaint"));
  631. }
  632. bool TabContainer::is_tab_hidden(int p_tab) const {
  633. return tab_bar->is_tab_hidden(p_tab);
  634. }
  635. void TabContainer::set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  636. tab_bar->set_tab_button_icon(p_tab, p_icon);
  637. _update_margins();
  638. _repaint();
  639. }
  640. Ref<Texture2D> TabContainer::get_tab_button_icon(int p_tab) const {
  641. return tab_bar->get_tab_button_icon(p_tab);
  642. }
  643. Size2 TabContainer::get_minimum_size() const {
  644. Size2 ms;
  645. if (tabs_visible) {
  646. ms = tab_bar->get_minimum_size();
  647. if (!get_clip_tabs()) {
  648. if (get_popup()) {
  649. ms.x += theme_cache.menu_icon->get_width();
  650. }
  651. if (theme_cache.side_margin > 0 && get_tab_alignment() != TabBar::ALIGNMENT_CENTER &&
  652. (get_tab_alignment() != TabBar::ALIGNMENT_RIGHT || !get_popup())) {
  653. ms.x += theme_cache.side_margin;
  654. }
  655. }
  656. }
  657. Vector<Control *> controls = _get_tab_controls();
  658. Size2 largest_child_min_size;
  659. for (int i = 0; i < controls.size(); i++) {
  660. Control *c = controls[i];
  661. if (!c->is_visible_in_tree() && !use_hidden_tabs_for_min_size) {
  662. continue;
  663. }
  664. Size2 cms = c->get_combined_minimum_size();
  665. largest_child_min_size.x = MAX(largest_child_min_size.x, cms.x);
  666. largest_child_min_size.y = MAX(largest_child_min_size.y, cms.y);
  667. }
  668. ms.y += largest_child_min_size.y;
  669. Size2 panel_ms = theme_cache.panel_style->get_minimum_size();
  670. ms.x = MAX(ms.x, largest_child_min_size.x + panel_ms.x);
  671. ms.y += panel_ms.y;
  672. return ms;
  673. }
  674. void TabContainer::set_popup(Node *p_popup) {
  675. bool had_popup = get_popup();
  676. Popup *popup = Object::cast_to<Popup>(p_popup);
  677. ObjectID popup_id = popup ? popup->get_instance_id() : ObjectID();
  678. if (popup_obj_id == popup_id) {
  679. return;
  680. }
  681. popup_obj_id = popup_id;
  682. if (had_popup != bool(popup)) {
  683. queue_redraw();
  684. _update_margins();
  685. if (!get_clip_tabs()) {
  686. update_minimum_size();
  687. }
  688. }
  689. }
  690. Popup *TabContainer::get_popup() const {
  691. if (popup_obj_id.is_valid()) {
  692. Popup *popup = Object::cast_to<Popup>(ObjectDB::get_instance(popup_obj_id));
  693. if (popup) {
  694. return popup;
  695. } else {
  696. #ifdef DEBUG_ENABLED
  697. ERR_PRINT("Popup assigned to TabContainer is gone!");
  698. #endif
  699. popup_obj_id = ObjectID();
  700. }
  701. }
  702. return nullptr;
  703. }
  704. void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
  705. drag_to_rearrange_enabled = p_enabled;
  706. }
  707. bool TabContainer::get_drag_to_rearrange_enabled() const {
  708. return drag_to_rearrange_enabled;
  709. }
  710. void TabContainer::set_tabs_rearrange_group(int p_group_id) {
  711. tab_bar->set_tabs_rearrange_group(p_group_id);
  712. }
  713. int TabContainer::get_tabs_rearrange_group() const {
  714. return tab_bar->get_tabs_rearrange_group();
  715. }
  716. void TabContainer::set_use_hidden_tabs_for_min_size(bool p_use_hidden_tabs) {
  717. if (use_hidden_tabs_for_min_size == p_use_hidden_tabs) {
  718. return;
  719. }
  720. use_hidden_tabs_for_min_size = p_use_hidden_tabs;
  721. update_minimum_size();
  722. }
  723. bool TabContainer::get_use_hidden_tabs_for_min_size() const {
  724. return use_hidden_tabs_for_min_size;
  725. }
  726. Vector<int> TabContainer::get_allowed_size_flags_horizontal() const {
  727. return Vector<int>();
  728. }
  729. Vector<int> TabContainer::get_allowed_size_flags_vertical() const {
  730. return Vector<int>();
  731. }
  732. void TabContainer::_bind_methods() {
  733. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabContainer::get_tab_count);
  734. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
  735. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabContainer::get_current_tab);
  736. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabContainer::get_previous_tab);
  737. ClassDB::bind_method(D_METHOD("get_current_tab_control"), &TabContainer::get_current_tab_control);
  738. ClassDB::bind_method(D_METHOD("get_tab_control", "tab_idx"), &TabContainer::get_tab_control);
  739. ClassDB::bind_method(D_METHOD("set_tab_alignment", "alignment"), &TabContainer::set_tab_alignment);
  740. ClassDB::bind_method(D_METHOD("get_tab_alignment"), &TabContainer::get_tab_alignment);
  741. ClassDB::bind_method(D_METHOD("set_clip_tabs", "clip_tabs"), &TabContainer::set_clip_tabs);
  742. ClassDB::bind_method(D_METHOD("get_clip_tabs"), &TabContainer::get_clip_tabs);
  743. ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
  744. ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
  745. ClassDB::bind_method(D_METHOD("set_all_tabs_in_front", "is_front"), &TabContainer::set_all_tabs_in_front);
  746. ClassDB::bind_method(D_METHOD("is_all_tabs_in_front"), &TabContainer::is_all_tabs_in_front);
  747. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
  748. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
  749. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon);
  750. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon);
  751. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled);
  752. ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabContainer::is_tab_disabled);
  753. ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabContainer::set_tab_hidden);
  754. ClassDB::bind_method(D_METHOD("is_tab_hidden", "tab_idx"), &TabContainer::is_tab_hidden);
  755. ClassDB::bind_method(D_METHOD("set_tab_button_icon", "tab_idx", "icon"), &TabContainer::set_tab_button_icon);
  756. ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &TabContainer::get_tab_button_icon);
  757. ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabContainer::get_tab_idx_at_point);
  758. ClassDB::bind_method(D_METHOD("get_tab_idx_from_control", "control"), &TabContainer::get_tab_idx_from_control);
  759. ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
  760. ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
  761. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabContainer::set_drag_to_rearrange_enabled);
  762. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabContainer::get_drag_to_rearrange_enabled);
  763. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabContainer::set_tabs_rearrange_group);
  764. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabContainer::get_tabs_rearrange_group);
  765. ClassDB::bind_method(D_METHOD("set_use_hidden_tabs_for_min_size", "enabled"), &TabContainer::set_use_hidden_tabs_for_min_size);
  766. ClassDB::bind_method(D_METHOD("get_use_hidden_tabs_for_min_size"), &TabContainer::get_use_hidden_tabs_for_min_size);
  767. ClassDB::bind_method(D_METHOD("_repaint"), &TabContainer::_repaint);
  768. ClassDB::bind_method(D_METHOD("_on_theme_changed"), &TabContainer::_on_theme_changed);
  769. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  770. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  771. ADD_SIGNAL(MethodInfo("tab_button_pressed", PropertyInfo(Variant::INT, "tab")));
  772. ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
  773. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_alignment", "get_tab_alignment");
  774. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
  775. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_tabs"), "set_clip_tabs", "get_clip_tabs");
  776. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
  777. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "all_tabs_in_front"), "set_all_tabs_in_front", "is_all_tabs_in_front");
  778. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  779. ADD_PROPERTY(PropertyInfo(Variant::INT, "tabs_rearrange_group"), "set_tabs_rearrange_group", "get_tabs_rearrange_group");
  780. 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");
  781. }
  782. TabContainer::TabContainer() {
  783. tab_bar = memnew(TabBar);
  784. SET_DRAG_FORWARDING_GCDU(tab_bar, TabContainer);
  785. add_child(tab_bar, false, INTERNAL_MODE_FRONT);
  786. tab_bar->set_anchors_and_offsets_preset(Control::PRESET_TOP_WIDE);
  787. tab_bar->connect("tab_changed", callable_mp(this, &TabContainer::_on_tab_changed));
  788. tab_bar->connect("tab_selected", callable_mp(this, &TabContainer::_on_tab_selected));
  789. tab_bar->connect("tab_button_pressed", callable_mp(this, &TabContainer::_on_tab_button_pressed));
  790. connect("mouse_exited", callable_mp(this, &TabContainer::_on_mouse_exited));
  791. }