tab_container.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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/theme/theme_db.h"
  32. Rect2 TabContainer::_get_tab_rect() const {
  33. Rect2 rect;
  34. if (tabs_visible && get_tab_count() > 0) {
  35. rect = Rect2(theme_cache.tabbar_style->get_offset(), tab_bar->get_size());
  36. rect.position.x += is_layout_rtl() ? theme_cache.menu_icon->get_width() : theme_cache.side_margin;
  37. }
  38. return rect;
  39. }
  40. int TabContainer::_get_tab_height() const {
  41. int height = 0;
  42. if (tabs_visible && get_tab_count() > 0) {
  43. height = tab_bar->get_minimum_size().height + theme_cache.tabbar_style->get_margin(SIDE_TOP) + theme_cache.tabbar_style->get_margin(SIDE_BOTTOM);
  44. }
  45. return height;
  46. }
  47. void TabContainer::gui_input(const Ref<InputEvent> &p_event) {
  48. ERR_FAIL_COND(p_event.is_null());
  49. Ref<InputEventMouseButton> mb = p_event;
  50. Popup *popup = get_popup();
  51. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  52. Point2 pos = mb->get_position();
  53. real_t content_height = get_size().height - _get_tab_height();
  54. Rect2 popup_rect = _get_tab_rect();
  55. popup_rect.position.x += is_layout_rtl() ? -theme_cache.menu_icon->get_width() : popup_rect.size.x;
  56. popup_rect.position.y += tabs_position == POSITION_BOTTOM ? content_height : 0;
  57. popup_rect.size.x = theme_cache.menu_icon->get_width();
  58. // Click must be on tabs in the tab header area.
  59. if (!tabs_visible || pos.y < popup_rect.position.y || pos.y >= popup_rect.position.y + popup_rect.size.y) {
  60. return;
  61. }
  62. // Handle menu button.
  63. if (popup) {
  64. if (popup_rect.has_point(pos)) {
  65. emit_signal(SNAME("pre_popup_pressed"));
  66. Vector2 popup_pos = get_screen_position();
  67. popup_pos.x += popup_rect.position.x + (is_layout_rtl() ? 0 : popup_rect.size.x - popup->get_size().width);
  68. popup_pos.y += popup_rect.position.y + popup_rect.size.y / 2.0;
  69. if (tabs_position == POSITION_BOTTOM) {
  70. popup_pos.y -= popup->get_size().height;
  71. popup_pos.y -= theme_cache.menu_icon->get_height() / 2.0;
  72. } else {
  73. popup_pos.y += theme_cache.menu_icon->get_height() / 2.0;
  74. }
  75. popup->set_position(popup_pos);
  76. popup->popup();
  77. return;
  78. }
  79. }
  80. }
  81. Ref<InputEventMouseMotion> mm = p_event;
  82. if (mm.is_valid()) {
  83. Point2 pos = mm->get_position();
  84. real_t content_height = get_size().height - _get_tab_height();
  85. Rect2 popup_rect = _get_tab_rect();
  86. popup_rect.position.x += is_layout_rtl() ? -theme_cache.menu_icon->get_width() : popup_rect.size.x;
  87. popup_rect.position.y += tabs_position == POSITION_BOTTOM ? content_height : 0;
  88. popup_rect.size.x = theme_cache.menu_icon->get_width();
  89. // Mouse must be on tabs in the tab header area.
  90. if (!tabs_visible || pos.y < popup_rect.position.y || pos.y >= popup_rect.position.y + popup_rect.size.y) {
  91. if (menu_hovered) {
  92. menu_hovered = false;
  93. queue_redraw();
  94. }
  95. return;
  96. }
  97. if (popup) {
  98. if (popup_rect.has_point(pos)) {
  99. if (!menu_hovered) {
  100. menu_hovered = true;
  101. queue_redraw();
  102. return;
  103. }
  104. } else if (menu_hovered) {
  105. menu_hovered = false;
  106. queue_redraw();
  107. }
  108. if (menu_hovered) {
  109. return;
  110. }
  111. }
  112. }
  113. }
  114. void TabContainer::_notification(int p_what) {
  115. switch (p_what) {
  116. case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
  117. tab_panels.clear();
  118. } break;
  119. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  120. RID ae = get_accessibility_element();
  121. ERR_FAIL_COND(ae.is_null());
  122. int tab_index = 0;
  123. int tab_cur = tab_bar->get_current_tab();
  124. for (int i = 0; i < get_child_count(); i++) {
  125. Node *child_node = get_child(i);
  126. Window *child_wnd = Object::cast_to<Window>(child_node);
  127. if (child_wnd && !child_wnd->is_embedded()) {
  128. continue;
  129. }
  130. if (child_node->is_part_of_edited_scene()) {
  131. continue;
  132. }
  133. Control *control = as_sortable_control(child_node, SortableVisibilityMode::IGNORE);
  134. if (!control || control == tab_bar || children_removing.has(control)) {
  135. DisplayServer::get_singleton()->accessibility_update_add_child(ae, child_node->get_accessibility_element());
  136. } else {
  137. if (!tab_panels.has(child_node)) {
  138. tab_panels[child_node] = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_TAB_PANEL);
  139. }
  140. RID panel = tab_panels[child_node];
  141. RID tab = tab_bar->get_tab_accessibility_element(tab_index);
  142. DisplayServer::get_singleton()->accessibility_update_add_related_controls(tab, panel);
  143. DisplayServer::get_singleton()->accessibility_update_add_related_labeled_by(panel, tab);
  144. DisplayServer::get_singleton()->accessibility_update_set_flag(panel, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, tab_index != tab_cur);
  145. DisplayServer::get_singleton()->accessibility_update_add_child(panel, child_node->get_accessibility_element());
  146. tab_index++;
  147. }
  148. }
  149. } break;
  150. case NOTIFICATION_ENTER_TREE: {
  151. // If some nodes happen to be renamed outside the tree, the tab names need to be updated manually.
  152. if (get_tab_count() > 0) {
  153. _refresh_tab_names();
  154. }
  155. if (setup_current_tab >= -1) {
  156. set_current_tab(setup_current_tab);
  157. setup_current_tab = -2;
  158. }
  159. } break;
  160. case NOTIFICATION_READY:
  161. case NOTIFICATION_RESIZED: {
  162. _update_margins();
  163. } break;
  164. case NOTIFICATION_DRAW: {
  165. RID canvas = get_canvas_item();
  166. Size2 size = get_size();
  167. Rect2 tabbar_rect = _get_tab_rect();
  168. // Draw only the tab area if the header is hidden.
  169. if (!tabs_visible) {
  170. theme_cache.panel_style->draw(canvas, Rect2(0, 0, size.width, size.height));
  171. return;
  172. }
  173. int header_height = _get_tab_height();
  174. int header_voffset = int(tabs_position == POSITION_BOTTOM) * (size.height - header_height);
  175. // Draw background for the tabbar.
  176. theme_cache.tabbar_style->draw(canvas, Rect2(0, header_voffset, size.width, header_height));
  177. // Draw the background for the tab's content.
  178. theme_cache.panel_style->draw(canvas, Rect2(0, int(tabs_position == POSITION_TOP) * header_height, size.width, size.height - header_height));
  179. // Draw the popup menu.
  180. if (get_popup()) {
  181. int x = is_layout_rtl() ? tabbar_rect.position.x - theme_cache.menu_icon->get_width() : tabbar_rect.position.x + tabbar_rect.size.x;
  182. header_voffset += tabbar_rect.position.y;
  183. if (menu_hovered) {
  184. theme_cache.menu_hl_icon->draw(get_canvas_item(), Point2(x, header_voffset + (tabbar_rect.size.y - theme_cache.menu_hl_icon->get_height()) / 2));
  185. } else {
  186. theme_cache.menu_icon->draw(get_canvas_item(), Point2(x, header_voffset + (tabbar_rect.size.y - theme_cache.menu_icon->get_height()) / 2));
  187. }
  188. }
  189. } break;
  190. case NOTIFICATION_VISIBILITY_CHANGED: {
  191. if (!is_visible()) {
  192. return;
  193. }
  194. updating_visibility = true;
  195. // As the visibility change notification will be triggered for all children soon after,
  196. // beat it to the punch and make sure that the correct node is the only one visible first.
  197. // Otherwise, it can prevent a tab change done right before this container was made visible.
  198. Vector<Control *> controls = _get_tab_controls();
  199. int current = setup_current_tab > -2 ? setup_current_tab : get_current_tab();
  200. for (int i = 0; i < controls.size(); i++) {
  201. controls[i]->set_visible(i == current);
  202. }
  203. updating_visibility = false;
  204. } break;
  205. case NOTIFICATION_TRANSLATION_CHANGED:
  206. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  207. case NOTIFICATION_THEME_CHANGED: {
  208. theme_changing = true;
  209. callable_mp(this, &TabContainer::_on_theme_changed).call_deferred(); // Wait until all changed theme.
  210. } break;
  211. }
  212. }
  213. void TabContainer::_on_theme_changed() {
  214. if (!theme_changing) {
  215. return;
  216. }
  217. tab_bar->begin_bulk_theme_override();
  218. tab_bar->add_theme_style_override(SNAME("tab_unselected"), theme_cache.tab_unselected_style);
  219. tab_bar->add_theme_style_override(SNAME("tab_hovered"), theme_cache.tab_hovered_style);
  220. tab_bar->add_theme_style_override(SNAME("tab_selected"), theme_cache.tab_selected_style);
  221. tab_bar->add_theme_style_override(SNAME("tab_disabled"), theme_cache.tab_disabled_style);
  222. tab_bar->add_theme_style_override(SNAME("tab_focus"), theme_cache.tab_focus_style);
  223. tab_bar->add_theme_icon_override(SNAME("increment"), theme_cache.increment_icon);
  224. tab_bar->add_theme_icon_override(SNAME("increment_highlight"), theme_cache.increment_hl_icon);
  225. tab_bar->add_theme_icon_override(SNAME("decrement"), theme_cache.decrement_icon);
  226. tab_bar->add_theme_icon_override(SNAME("decrement_highlight"), theme_cache.decrement_hl_icon);
  227. tab_bar->add_theme_icon_override(SNAME("drop_mark"), theme_cache.drop_mark_icon);
  228. tab_bar->add_theme_color_override(SNAME("drop_mark_color"), theme_cache.drop_mark_color);
  229. tab_bar->add_theme_color_override(SNAME("font_selected_color"), theme_cache.font_selected_color);
  230. tab_bar->add_theme_color_override(SNAME("font_hovered_color"), theme_cache.font_hovered_color);
  231. tab_bar->add_theme_color_override(SNAME("font_unselected_color"), theme_cache.font_unselected_color);
  232. tab_bar->add_theme_color_override(SNAME("font_disabled_color"), theme_cache.font_disabled_color);
  233. tab_bar->add_theme_color_override(SNAME("font_outline_color"), theme_cache.font_outline_color);
  234. tab_bar->add_theme_font_override(SceneStringName(font), theme_cache.tab_font);
  235. tab_bar->add_theme_font_size_override(SceneStringName(font_size), theme_cache.tab_font_size);
  236. tab_bar->add_theme_constant_override(SNAME("h_separation"), theme_cache.icon_separation);
  237. tab_bar->add_theme_constant_override(SNAME("tab_separation"), theme_cache.tab_separation);
  238. tab_bar->add_theme_constant_override(SNAME("icon_max_width"), theme_cache.icon_max_width);
  239. tab_bar->add_theme_constant_override(SNAME("outline_size"), theme_cache.outline_size);
  240. tab_bar->end_bulk_theme_override();
  241. _update_margins();
  242. if (get_tab_count() > 0) {
  243. _repaint();
  244. } else {
  245. update_minimum_size();
  246. }
  247. queue_redraw();
  248. theme_changing = false;
  249. }
  250. void TabContainer::_repaint() {
  251. Vector<Control *> controls = _get_tab_controls();
  252. int current = get_current_tab();
  253. float top_margin = theme_cache.tabbar_style->get_margin(SIDE_TOP);
  254. float bottom_margin = theme_cache.tabbar_style->get_margin(SIDE_BOTTOM);
  255. // Move the TabBar to the top or bottom.
  256. // Don't change the left and right offsets since the TabBar will resize and may change tab offset.
  257. if (tabs_position == POSITION_BOTTOM) {
  258. tab_bar->set_anchor_and_offset(SIDE_BOTTOM, 1.0, -bottom_margin);
  259. tab_bar->set_anchor_and_offset(SIDE_TOP, 1.0, top_margin - _get_tab_height());
  260. } else {
  261. tab_bar->set_anchor_and_offset(SIDE_TOP, 0.0, top_margin);
  262. tab_bar->set_anchor_and_offset(SIDE_BOTTOM, 0.0, _get_tab_height() - bottom_margin);
  263. }
  264. updating_visibility = true;
  265. for (int i = 0; i < controls.size(); i++) {
  266. Control *c = controls[i];
  267. if (i == current) {
  268. c->show();
  269. c->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  270. if (tabs_visible) {
  271. if (tabs_position == POSITION_BOTTOM) {
  272. c->set_offset(SIDE_BOTTOM, -_get_tab_height());
  273. } else {
  274. c->set_offset(SIDE_TOP, _get_tab_height());
  275. }
  276. }
  277. c->set_offset(SIDE_TOP, c->get_offset(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_TOP));
  278. c->set_offset(SIDE_LEFT, c->get_offset(SIDE_LEFT) + theme_cache.panel_style->get_margin(SIDE_LEFT));
  279. c->set_offset(SIDE_RIGHT, c->get_offset(SIDE_RIGHT) - theme_cache.panel_style->get_margin(SIDE_RIGHT));
  280. c->set_offset(SIDE_BOTTOM, c->get_offset(SIDE_BOTTOM) - theme_cache.panel_style->get_margin(SIDE_BOTTOM));
  281. } else {
  282. c->hide();
  283. }
  284. }
  285. updating_visibility = false;
  286. update_minimum_size();
  287. }
  288. void TabContainer::_update_margins() {
  289. // Directly check for validity, to avoid errors when quitting.
  290. bool has_popup = popup_obj_id.is_valid();
  291. int left_margin = theme_cache.tabbar_style->get_margin(SIDE_LEFT);
  292. int right_margin = theme_cache.tabbar_style->get_margin(SIDE_RIGHT);
  293. if (is_layout_rtl()) {
  294. SWAP(left_margin, right_margin);
  295. }
  296. if (has_popup) {
  297. right_margin += theme_cache.menu_icon->get_width();
  298. }
  299. if (get_tab_count() == 0) {
  300. tab_bar->set_offset(SIDE_LEFT, left_margin);
  301. tab_bar->set_offset(SIDE_RIGHT, -right_margin);
  302. return;
  303. }
  304. switch (get_tab_alignment()) {
  305. case TabBar::ALIGNMENT_LEFT: {
  306. tab_bar->set_offset(SIDE_LEFT, left_margin + theme_cache.side_margin);
  307. tab_bar->set_offset(SIDE_RIGHT, -right_margin);
  308. } break;
  309. case TabBar::ALIGNMENT_CENTER: {
  310. tab_bar->set_offset(SIDE_LEFT, left_margin);
  311. tab_bar->set_offset(SIDE_RIGHT, -right_margin);
  312. } break;
  313. case TabBar::ALIGNMENT_RIGHT: {
  314. tab_bar->set_offset(SIDE_LEFT, left_margin);
  315. if (has_popup) {
  316. tab_bar->set_offset(SIDE_RIGHT, -right_margin);
  317. return;
  318. }
  319. int first_tab_pos = tab_bar->get_tab_rect(0).position.x;
  320. Rect2 last_tab_rect = tab_bar->get_tab_rect(get_tab_count() - 1);
  321. int total_tabs_width = left_margin + right_margin + last_tab_rect.position.x - first_tab_pos + last_tab_rect.size.width;
  322. // Calculate if all the tabs would still fit if the margin was present.
  323. if (get_clip_tabs() && (tab_bar->get_offset_buttons_visible() || (get_tab_count() > 1 && (total_tabs_width + theme_cache.side_margin) > get_size().width))) {
  324. tab_bar->set_offset(SIDE_RIGHT, -right_margin);
  325. } else {
  326. tab_bar->set_offset(SIDE_RIGHT, -right_margin - theme_cache.side_margin);
  327. }
  328. } break;
  329. case TabBar::ALIGNMENT_MAX:
  330. break; // Can't happen, but silences warning.
  331. }
  332. }
  333. void TabContainer::_on_mouse_exited() {
  334. if (menu_hovered) {
  335. menu_hovered = false;
  336. queue_redraw();
  337. }
  338. }
  339. Vector<Control *> TabContainer::_get_tab_controls() const {
  340. Vector<Control *> controls;
  341. for (int i = 0; i < get_child_count(); i++) {
  342. Control *control = as_sortable_control(get_child(i), SortableVisibilityMode::IGNORE);
  343. if (!control || control == tab_bar || children_removing.has(control)) {
  344. continue;
  345. }
  346. controls.push_back(control);
  347. }
  348. return controls;
  349. }
  350. Variant TabContainer::_get_drag_data_fw(const Point2 &p_point, Control *p_from_control) {
  351. if (!drag_to_rearrange_enabled) {
  352. return Variant();
  353. }
  354. return tab_bar->_handle_get_drag_data("tab_container_tab", p_point);
  355. }
  356. bool TabContainer::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const {
  357. if (!drag_to_rearrange_enabled) {
  358. return false;
  359. }
  360. return tab_bar->_handle_can_drop_data("tab_container_tab", p_point, p_data);
  361. }
  362. void TabContainer::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) {
  363. if (!drag_to_rearrange_enabled) {
  364. return;
  365. }
  366. return tab_bar->_handle_drop_data("tab_container_tab", p_point, p_data, callable_mp(this, &TabContainer::_drag_move_tab), callable_mp(this, &TabContainer::_drag_move_tab_from));
  367. }
  368. void TabContainer::_drag_move_tab(int p_from_index, int p_to_index) {
  369. move_child(get_tab_control(p_from_index), get_tab_control(p_to_index)->get_index(false));
  370. }
  371. void TabContainer::_drag_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index) {
  372. Node *parent = p_from_tabbar->get_parent();
  373. if (!parent) {
  374. return;
  375. }
  376. TabContainer *from_tab_container = Object::cast_to<TabContainer>(parent);
  377. if (!from_tab_container) {
  378. return;
  379. }
  380. move_tab_from_tab_container(from_tab_container, p_from_index, p_to_index);
  381. }
  382. void TabContainer::move_tab_from_tab_container(TabContainer *p_from, int p_from_index, int p_to_index) {
  383. ERR_FAIL_NULL(p_from);
  384. ERR_FAIL_INDEX(p_from_index, p_from->get_tab_count());
  385. ERR_FAIL_INDEX(p_to_index, get_tab_count() + 1);
  386. // Get the tab properties before they get erased by the child removal.
  387. String tab_title = p_from->get_tab_title(p_from_index);
  388. String tab_tooltip = p_from->get_tab_tooltip(p_from_index);
  389. Ref<Texture2D> tab_icon = p_from->get_tab_icon(p_from_index);
  390. Ref<Texture2D> tab_button_icon = p_from->get_tab_button_icon(p_from_index);
  391. bool tab_disabled = p_from->is_tab_disabled(p_from_index);
  392. bool tab_hidden = p_from->is_tab_hidden(p_from_index);
  393. Variant tab_metadata = p_from->get_tab_metadata(p_from_index);
  394. int tab_icon_max_width = p_from->get_tab_bar()->get_tab_icon_max_width(p_from_index);
  395. Control *moving_tabc = p_from->get_tab_control(p_from_index);
  396. p_from->remove_child(moving_tabc);
  397. add_child(moving_tabc, true);
  398. if (p_to_index < 0 || p_to_index > get_tab_count() - 1) {
  399. p_to_index = get_tab_count() - 1;
  400. }
  401. move_child(moving_tabc, get_tab_control(p_to_index)->get_index(false));
  402. set_tab_title(p_to_index, tab_title);
  403. set_tab_tooltip(p_to_index, tab_tooltip);
  404. set_tab_icon(p_to_index, tab_icon);
  405. set_tab_button_icon(p_to_index, tab_button_icon);
  406. set_tab_disabled(p_to_index, tab_disabled);
  407. set_tab_hidden(p_to_index, tab_hidden);
  408. set_tab_metadata(p_to_index, tab_metadata);
  409. get_tab_bar()->set_tab_icon_max_width(p_to_index, tab_icon_max_width);
  410. if (!is_tab_disabled(p_to_index)) {
  411. set_current_tab(p_to_index);
  412. }
  413. }
  414. void TabContainer::_on_tab_clicked(int p_tab) {
  415. emit_signal(SNAME("tab_clicked"), p_tab);
  416. }
  417. void TabContainer::_on_tab_hovered(int p_tab) {
  418. emit_signal(SNAME("tab_hovered"), p_tab);
  419. }
  420. void TabContainer::_on_tab_changed(int p_tab) {
  421. callable_mp(this, &TabContainer::_repaint).call_deferred();
  422. queue_redraw();
  423. emit_signal(SNAME("tab_changed"), p_tab);
  424. }
  425. void TabContainer::_on_tab_selected(int p_tab) {
  426. if (p_tab != get_previous_tab()) {
  427. callable_mp(this, &TabContainer::_repaint).call_deferred();
  428. }
  429. emit_signal(SNAME("tab_selected"), p_tab);
  430. }
  431. void TabContainer::_on_tab_button_pressed(int p_tab) {
  432. emit_signal(SNAME("tab_button_pressed"), p_tab);
  433. }
  434. void TabContainer::_on_active_tab_rearranged(int p_tab) {
  435. emit_signal(SNAME("active_tab_rearranged"), p_tab);
  436. }
  437. void TabContainer::_on_tab_visibility_changed(Control *p_child) {
  438. if (updating_visibility) {
  439. return;
  440. }
  441. int tab_index = get_tab_idx_from_control(p_child);
  442. if (tab_index == -1) {
  443. return;
  444. }
  445. // Only allow one tab to be visible.
  446. bool made_visible = p_child->is_visible();
  447. updating_visibility = true;
  448. if (!made_visible && get_current_tab() == tab_index) {
  449. if (get_deselect_enabled() || get_tab_count() == 0) {
  450. // Deselect.
  451. set_current_tab(-1);
  452. } else if (get_tab_count() == 1) {
  453. // Only tab, cannot deselect.
  454. p_child->show();
  455. } else {
  456. // Set a different tab to be the current tab.
  457. bool selected = select_next_available();
  458. if (!selected) {
  459. selected = select_previous_available();
  460. }
  461. if (!selected) {
  462. // No available tabs, deselect.
  463. set_current_tab(-1);
  464. }
  465. }
  466. } else if (made_visible && get_current_tab() != tab_index) {
  467. set_current_tab(tab_index);
  468. }
  469. updating_visibility = false;
  470. }
  471. void TabContainer::_refresh_tab_indices() {
  472. Vector<Control *> controls = _get_tab_controls();
  473. for (int i = 0; i < controls.size(); i++) {
  474. controls[i]->set_meta("_tab_index", i);
  475. }
  476. }
  477. void TabContainer::_refresh_tab_names() {
  478. Vector<Control *> controls = _get_tab_controls();
  479. for (int i = 0; i < controls.size(); i++) {
  480. if (!controls[i]->has_meta("_tab_name") && String(controls[i]->get_name()) != get_tab_title(i)) {
  481. tab_bar->set_tab_title(i, controls[i]->get_name());
  482. }
  483. }
  484. }
  485. void TabContainer::add_child_notify(Node *p_child) {
  486. Container::add_child_notify(p_child);
  487. if (p_child == tab_bar) {
  488. return;
  489. }
  490. Control *c = as_sortable_control(p_child, SortableVisibilityMode::IGNORE);
  491. if (!c) {
  492. return;
  493. }
  494. c->hide();
  495. tab_bar->add_tab(p_child->get_name());
  496. c->set_meta("_tab_index", tab_bar->get_tab_count() - 1);
  497. _update_margins();
  498. if (get_tab_count() == 1) {
  499. queue_redraw();
  500. }
  501. queue_accessibility_update();
  502. p_child->connect("renamed", callable_mp(this, &TabContainer::_refresh_tab_names));
  503. p_child->connect(SceneStringName(visibility_changed), callable_mp(this, &TabContainer::_on_tab_visibility_changed).bind(c));
  504. // TabBar won't emit the "tab_changed" signal when not inside the tree.
  505. if (!is_inside_tree()) {
  506. callable_mp(this, &TabContainer::_repaint).call_deferred();
  507. }
  508. }
  509. void TabContainer::move_child_notify(Node *p_child) {
  510. Container::move_child_notify(p_child);
  511. if (p_child == tab_bar) {
  512. return;
  513. }
  514. Control *c = as_sortable_control(p_child, SortableVisibilityMode::IGNORE);
  515. if (c) {
  516. tab_bar->move_tab(c->get_meta("_tab_index"), get_tab_idx_from_control(c));
  517. }
  518. _refresh_tab_indices();
  519. queue_accessibility_update();
  520. }
  521. void TabContainer::remove_child_notify(Node *p_child) {
  522. Container::remove_child_notify(p_child);
  523. if (tab_panels.has(p_child)) {
  524. DisplayServer::get_singleton()->accessibility_free_element(tab_panels[p_child]);
  525. tab_panels.erase(p_child);
  526. }
  527. if (p_child == tab_bar) {
  528. return;
  529. }
  530. Control *c = as_sortable_control(p_child, SortableVisibilityMode::IGNORE);
  531. if (!c) {
  532. return;
  533. }
  534. int idx = get_tab_idx_from_control(c);
  535. // As the child hasn't been removed yet, keep track of it so when the "tab_changed" signal is fired it can be ignored.
  536. children_removing.push_back(c);
  537. tab_bar->remove_tab(idx);
  538. _refresh_tab_indices();
  539. children_removing.erase(c);
  540. _update_margins();
  541. if (get_tab_count() == 0) {
  542. queue_redraw();
  543. }
  544. queue_accessibility_update();
  545. p_child->remove_meta("_tab_index");
  546. p_child->remove_meta("_tab_name");
  547. p_child->disconnect("renamed", callable_mp(this, &TabContainer::_refresh_tab_names));
  548. p_child->disconnect(SceneStringName(visibility_changed), callable_mp(this, &TabContainer::_on_tab_visibility_changed));
  549. // TabBar won't emit the "tab_changed" signal when not inside the tree.
  550. if (!is_inside_tree()) {
  551. callable_mp(this, &TabContainer::_repaint).call_deferred();
  552. }
  553. }
  554. TabBar *TabContainer::get_tab_bar() const {
  555. return tab_bar;
  556. }
  557. int TabContainer::get_tab_count() const {
  558. return tab_bar->get_tab_count();
  559. }
  560. void TabContainer::set_current_tab(int p_current) {
  561. if (!is_inside_tree()) {
  562. setup_current_tab = p_current;
  563. return;
  564. }
  565. tab_bar->set_current_tab(p_current);
  566. }
  567. int TabContainer::get_current_tab() const {
  568. return tab_bar->get_current_tab();
  569. }
  570. int TabContainer::get_previous_tab() const {
  571. return tab_bar->get_previous_tab();
  572. }
  573. bool TabContainer::select_previous_available() {
  574. return tab_bar->select_previous_available();
  575. }
  576. bool TabContainer::select_next_available() {
  577. return tab_bar->select_next_available();
  578. }
  579. void TabContainer::set_deselect_enabled(bool p_enabled) {
  580. tab_bar->set_deselect_enabled(p_enabled);
  581. }
  582. bool TabContainer::get_deselect_enabled() const {
  583. return tab_bar->get_deselect_enabled();
  584. }
  585. Control *TabContainer::get_tab_control(int p_idx) const {
  586. Vector<Control *> controls = _get_tab_controls();
  587. if (p_idx >= 0 && p_idx < controls.size()) {
  588. return controls[p_idx];
  589. } else {
  590. return nullptr;
  591. }
  592. }
  593. Control *TabContainer::get_current_tab_control() const {
  594. return get_tab_control(tab_bar->get_current_tab());
  595. }
  596. int TabContainer::get_tab_idx_at_point(const Point2 &p_point) const {
  597. return tab_bar->get_tab_idx_at_point(p_point);
  598. }
  599. int TabContainer::get_tab_idx_from_control(Control *p_child) const {
  600. ERR_FAIL_NULL_V(p_child, -1);
  601. ERR_FAIL_COND_V(p_child->get_parent() != this, -1);
  602. Vector<Control *> controls = _get_tab_controls();
  603. for (int i = 0; i < controls.size(); i++) {
  604. if (controls[i] == p_child) {
  605. return i;
  606. }
  607. }
  608. return -1;
  609. }
  610. void TabContainer::set_tab_alignment(TabBar::AlignmentMode p_alignment) {
  611. if (tab_bar->get_tab_alignment() == p_alignment) {
  612. return;
  613. }
  614. tab_bar->set_tab_alignment(p_alignment);
  615. _update_margins();
  616. }
  617. TabBar::AlignmentMode TabContainer::get_tab_alignment() const {
  618. return tab_bar->get_tab_alignment();
  619. }
  620. void TabContainer::set_tabs_position(TabPosition p_tabs_position) {
  621. ERR_FAIL_INDEX(p_tabs_position, POSITION_MAX);
  622. if (p_tabs_position == tabs_position) {
  623. return;
  624. }
  625. tabs_position = p_tabs_position;
  626. tab_bar->set_tab_style_v_flip(tabs_position == POSITION_BOTTOM);
  627. callable_mp(this, &TabContainer::_repaint).call_deferred();
  628. queue_redraw();
  629. }
  630. TabContainer::TabPosition TabContainer::get_tabs_position() const {
  631. return tabs_position;
  632. }
  633. void TabContainer::set_tab_focus_mode(Control::FocusMode p_focus_mode) {
  634. tab_bar->set_focus_mode(p_focus_mode);
  635. }
  636. Control::FocusMode TabContainer::get_tab_focus_mode() const {
  637. return tab_bar->get_focus_mode();
  638. }
  639. void TabContainer::set_clip_tabs(bool p_clip_tabs) {
  640. tab_bar->set_clip_tabs(p_clip_tabs);
  641. }
  642. bool TabContainer::get_clip_tabs() const {
  643. return tab_bar->get_clip_tabs();
  644. }
  645. void TabContainer::set_tabs_visible(bool p_visible) {
  646. if (p_visible == tabs_visible) {
  647. return;
  648. }
  649. tabs_visible = p_visible;
  650. tab_bar->set_visible(tabs_visible);
  651. callable_mp(this, &TabContainer::_repaint).call_deferred();
  652. queue_redraw();
  653. }
  654. bool TabContainer::are_tabs_visible() const {
  655. return tabs_visible;
  656. }
  657. void TabContainer::set_all_tabs_in_front(bool p_in_front) {
  658. if (p_in_front == all_tabs_in_front) {
  659. return;
  660. }
  661. all_tabs_in_front = p_in_front;
  662. remove_child(tab_bar);
  663. add_child(tab_bar, false, all_tabs_in_front ? INTERNAL_MODE_FRONT : INTERNAL_MODE_BACK);
  664. }
  665. bool TabContainer::is_all_tabs_in_front() const {
  666. return all_tabs_in_front;
  667. }
  668. void TabContainer::set_tab_title(int p_tab, const String &p_title) {
  669. Control *child = get_tab_control(p_tab);
  670. ERR_FAIL_NULL(child);
  671. if (tab_bar->get_tab_title(p_tab) == p_title) {
  672. return;
  673. }
  674. tab_bar->set_tab_title(p_tab, p_title);
  675. if (p_title == child->get_name()) {
  676. child->remove_meta("_tab_name");
  677. } else {
  678. child->set_meta("_tab_name", p_title);
  679. }
  680. _repaint();
  681. queue_redraw();
  682. }
  683. String TabContainer::get_tab_title(int p_tab) const {
  684. return tab_bar->get_tab_title(p_tab);
  685. }
  686. void TabContainer::set_tab_tooltip(int p_tab, const String &p_tooltip) {
  687. tab_bar->set_tab_tooltip(p_tab, p_tooltip);
  688. }
  689. String TabContainer::get_tab_tooltip(int p_tab) const {
  690. return tab_bar->get_tab_tooltip(p_tab);
  691. }
  692. void TabContainer::set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  693. if (tab_bar->get_tab_icon(p_tab) == p_icon) {
  694. return;
  695. }
  696. tab_bar->set_tab_icon(p_tab, p_icon);
  697. _update_margins();
  698. _repaint();
  699. queue_redraw();
  700. }
  701. Ref<Texture2D> TabContainer::get_tab_icon(int p_tab) const {
  702. return tab_bar->get_tab_icon(p_tab);
  703. }
  704. void TabContainer::set_tab_icon_max_width(int p_tab, int p_width) {
  705. if (tab_bar->get_tab_icon_max_width(p_tab) == p_width) {
  706. return;
  707. }
  708. tab_bar->set_tab_icon_max_width(p_tab, p_width);
  709. _update_margins();
  710. _repaint();
  711. queue_redraw();
  712. }
  713. int TabContainer::get_tab_icon_max_width(int p_tab) const {
  714. return tab_bar->get_tab_icon_max_width(p_tab);
  715. }
  716. void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) {
  717. if (tab_bar->is_tab_disabled(p_tab) == p_disabled) {
  718. return;
  719. }
  720. tab_bar->set_tab_disabled(p_tab, p_disabled);
  721. _update_margins();
  722. if (!get_clip_tabs()) {
  723. update_minimum_size();
  724. }
  725. }
  726. bool TabContainer::is_tab_disabled(int p_tab) const {
  727. return tab_bar->is_tab_disabled(p_tab);
  728. }
  729. void TabContainer::set_tab_hidden(int p_tab, bool p_hidden) {
  730. Control *child = get_tab_control(p_tab);
  731. ERR_FAIL_NULL(child);
  732. if (tab_bar->is_tab_hidden(p_tab) == p_hidden) {
  733. return;
  734. }
  735. tab_bar->set_tab_hidden(p_tab, p_hidden);
  736. child->hide();
  737. _update_margins();
  738. if (!get_clip_tabs()) {
  739. update_minimum_size();
  740. }
  741. callable_mp(this, &TabContainer::_repaint).call_deferred();
  742. }
  743. bool TabContainer::is_tab_hidden(int p_tab) const {
  744. return tab_bar->is_tab_hidden(p_tab);
  745. }
  746. void TabContainer::set_tab_metadata(int p_tab, const Variant &p_metadata) {
  747. tab_bar->set_tab_metadata(p_tab, p_metadata);
  748. }
  749. Variant TabContainer::get_tab_metadata(int p_tab) const {
  750. return tab_bar->get_tab_metadata(p_tab);
  751. }
  752. void TabContainer::set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  753. tab_bar->set_tab_button_icon(p_tab, p_icon);
  754. _update_margins();
  755. _repaint();
  756. }
  757. Ref<Texture2D> TabContainer::get_tab_button_icon(int p_tab) const {
  758. return tab_bar->get_tab_button_icon(p_tab);
  759. }
  760. Size2 TabContainer::get_minimum_size() const {
  761. Size2 ms;
  762. if (tabs_visible) {
  763. ms = tab_bar->get_minimum_size();
  764. ms.width += theme_cache.tabbar_style->get_margin(SIDE_LEFT) + theme_cache.tabbar_style->get_margin(SIDE_RIGHT);
  765. ms.height += theme_cache.tabbar_style->get_margin(SIDE_TOP) + theme_cache.tabbar_style->get_margin(SIDE_BOTTOM);
  766. if (!get_clip_tabs()) {
  767. if (get_popup()) {
  768. ms.width += theme_cache.menu_icon->get_width();
  769. }
  770. if (theme_cache.side_margin > 0 && get_tab_alignment() != TabBar::ALIGNMENT_CENTER &&
  771. (get_tab_alignment() != TabBar::ALIGNMENT_RIGHT || !get_popup())) {
  772. ms.width += theme_cache.side_margin;
  773. }
  774. }
  775. }
  776. Vector<Control *> controls = _get_tab_controls();
  777. Size2 largest_child_min_size;
  778. for (int i = 0; i < controls.size(); i++) {
  779. Control *c = controls[i];
  780. if (!c->is_visible() && !use_hidden_tabs_for_min_size) {
  781. continue;
  782. }
  783. Size2 cms = c->get_combined_minimum_size();
  784. largest_child_min_size = largest_child_min_size.max(cms);
  785. }
  786. ms.height += largest_child_min_size.height;
  787. Size2 panel_ms = theme_cache.panel_style->get_minimum_size();
  788. ms.width = MAX(ms.width, largest_child_min_size.width + panel_ms.width);
  789. ms.height += panel_ms.height;
  790. return ms;
  791. }
  792. void TabContainer::set_popup(Node *p_popup) {
  793. bool had_popup = get_popup();
  794. Popup *popup = Object::cast_to<Popup>(p_popup);
  795. ObjectID popup_id = popup ? popup->get_instance_id() : ObjectID();
  796. if (popup_obj_id == popup_id) {
  797. return;
  798. }
  799. popup_obj_id = popup_id;
  800. if (had_popup != bool(popup)) {
  801. queue_redraw();
  802. _update_margins();
  803. if (!get_clip_tabs()) {
  804. update_minimum_size();
  805. }
  806. }
  807. }
  808. Popup *TabContainer::get_popup() const {
  809. if (popup_obj_id.is_valid()) {
  810. Popup *popup = ObjectDB::get_instance<Popup>(popup_obj_id);
  811. if (popup) {
  812. return popup;
  813. } else {
  814. #ifdef DEBUG_ENABLED
  815. ERR_PRINT("Popup assigned to TabContainer is gone!");
  816. #endif
  817. popup_obj_id = ObjectID();
  818. }
  819. }
  820. return nullptr;
  821. }
  822. void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
  823. drag_to_rearrange_enabled = p_enabled;
  824. }
  825. bool TabContainer::get_drag_to_rearrange_enabled() const {
  826. return drag_to_rearrange_enabled;
  827. }
  828. void TabContainer::set_tabs_rearrange_group(int p_group_id) {
  829. tab_bar->set_tabs_rearrange_group(p_group_id);
  830. }
  831. int TabContainer::get_tabs_rearrange_group() const {
  832. return tab_bar->get_tabs_rearrange_group();
  833. }
  834. void TabContainer::set_use_hidden_tabs_for_min_size(bool p_use_hidden_tabs) {
  835. if (use_hidden_tabs_for_min_size == p_use_hidden_tabs) {
  836. return;
  837. }
  838. use_hidden_tabs_for_min_size = p_use_hidden_tabs;
  839. update_minimum_size();
  840. }
  841. bool TabContainer::get_use_hidden_tabs_for_min_size() const {
  842. return use_hidden_tabs_for_min_size;
  843. }
  844. Vector<int> TabContainer::get_allowed_size_flags_horizontal() const {
  845. return Vector<int>();
  846. }
  847. Vector<int> TabContainer::get_allowed_size_flags_vertical() const {
  848. return Vector<int>();
  849. }
  850. void TabContainer::_bind_methods() {
  851. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabContainer::get_tab_count);
  852. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
  853. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabContainer::get_current_tab);
  854. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabContainer::get_previous_tab);
  855. ClassDB::bind_method(D_METHOD("select_previous_available"), &TabContainer::select_previous_available);
  856. ClassDB::bind_method(D_METHOD("select_next_available"), &TabContainer::select_next_available);
  857. ClassDB::bind_method(D_METHOD("get_current_tab_control"), &TabContainer::get_current_tab_control);
  858. ClassDB::bind_method(D_METHOD("get_tab_bar"), &TabContainer::get_tab_bar);
  859. ClassDB::bind_method(D_METHOD("get_tab_control", "tab_idx"), &TabContainer::get_tab_control);
  860. ClassDB::bind_method(D_METHOD("set_tab_alignment", "alignment"), &TabContainer::set_tab_alignment);
  861. ClassDB::bind_method(D_METHOD("get_tab_alignment"), &TabContainer::get_tab_alignment);
  862. ClassDB::bind_method(D_METHOD("set_tabs_position", "tabs_position"), &TabContainer::set_tabs_position);
  863. ClassDB::bind_method(D_METHOD("get_tabs_position"), &TabContainer::get_tabs_position);
  864. ClassDB::bind_method(D_METHOD("set_clip_tabs", "clip_tabs"), &TabContainer::set_clip_tabs);
  865. ClassDB::bind_method(D_METHOD("get_clip_tabs"), &TabContainer::get_clip_tabs);
  866. ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
  867. ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
  868. ClassDB::bind_method(D_METHOD("set_all_tabs_in_front", "is_front"), &TabContainer::set_all_tabs_in_front);
  869. ClassDB::bind_method(D_METHOD("is_all_tabs_in_front"), &TabContainer::is_all_tabs_in_front);
  870. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
  871. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
  872. ClassDB::bind_method(D_METHOD("set_tab_tooltip", "tab_idx", "tooltip"), &TabContainer::set_tab_tooltip);
  873. ClassDB::bind_method(D_METHOD("get_tab_tooltip", "tab_idx"), &TabContainer::get_tab_tooltip);
  874. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon);
  875. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon);
  876. ClassDB::bind_method(D_METHOD("set_tab_icon_max_width", "tab_idx", "width"), &TabContainer::set_tab_icon_max_width);
  877. ClassDB::bind_method(D_METHOD("get_tab_icon_max_width", "tab_idx"), &TabContainer::get_tab_icon_max_width);
  878. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled);
  879. ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabContainer::is_tab_disabled);
  880. ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabContainer::set_tab_hidden);
  881. ClassDB::bind_method(D_METHOD("is_tab_hidden", "tab_idx"), &TabContainer::is_tab_hidden);
  882. ClassDB::bind_method(D_METHOD("set_tab_metadata", "tab_idx", "metadata"), &TabContainer::set_tab_metadata);
  883. ClassDB::bind_method(D_METHOD("get_tab_metadata", "tab_idx"), &TabContainer::get_tab_metadata);
  884. ClassDB::bind_method(D_METHOD("set_tab_button_icon", "tab_idx", "icon"), &TabContainer::set_tab_button_icon);
  885. ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &TabContainer::get_tab_button_icon);
  886. ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabContainer::get_tab_idx_at_point);
  887. ClassDB::bind_method(D_METHOD("get_tab_idx_from_control", "control"), &TabContainer::get_tab_idx_from_control);
  888. ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
  889. ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
  890. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabContainer::set_drag_to_rearrange_enabled);
  891. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabContainer::get_drag_to_rearrange_enabled);
  892. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabContainer::set_tabs_rearrange_group);
  893. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabContainer::get_tabs_rearrange_group);
  894. ClassDB::bind_method(D_METHOD("set_use_hidden_tabs_for_min_size", "enabled"), &TabContainer::set_use_hidden_tabs_for_min_size);
  895. ClassDB::bind_method(D_METHOD("get_use_hidden_tabs_for_min_size"), &TabContainer::get_use_hidden_tabs_for_min_size);
  896. ClassDB::bind_method(D_METHOD("set_tab_focus_mode", "focus_mode"), &TabContainer::set_tab_focus_mode);
  897. ClassDB::bind_method(D_METHOD("get_tab_focus_mode"), &TabContainer::get_tab_focus_mode);
  898. ClassDB::bind_method(D_METHOD("set_deselect_enabled", "enabled"), &TabContainer::set_deselect_enabled);
  899. ClassDB::bind_method(D_METHOD("get_deselect_enabled"), &TabContainer::get_deselect_enabled);
  900. ADD_SIGNAL(MethodInfo("active_tab_rearranged", PropertyInfo(Variant::INT, "idx_to")));
  901. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  902. ADD_SIGNAL(MethodInfo("tab_clicked", PropertyInfo(Variant::INT, "tab")));
  903. ADD_SIGNAL(MethodInfo("tab_hovered", PropertyInfo(Variant::INT, "tab")));
  904. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  905. ADD_SIGNAL(MethodInfo("tab_button_pressed", PropertyInfo(Variant::INT, "tab")));
  906. ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
  907. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_alignment", "get_tab_alignment");
  908. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1"), "set_current_tab", "get_current_tab");
  909. ADD_PROPERTY(PropertyInfo(Variant::INT, "tabs_position", PROPERTY_HINT_ENUM, "Top,Bottom"), "set_tabs_position", "get_tabs_position");
  910. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_tabs"), "set_clip_tabs", "get_clip_tabs");
  911. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
  912. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "all_tabs_in_front"), "set_all_tabs_in_front", "is_all_tabs_in_front");
  913. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  914. ADD_PROPERTY(PropertyInfo(Variant::INT, "tabs_rearrange_group"), "set_tabs_rearrange_group", "get_tabs_rearrange_group");
  915. 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");
  916. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_tab_focus_mode", "get_tab_focus_mode");
  917. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_enabled"), "set_deselect_enabled", "get_deselect_enabled");
  918. ADD_CLASS_DEPENDENCY("TabBar");
  919. BIND_ENUM_CONSTANT(POSITION_TOP);
  920. BIND_ENUM_CONSTANT(POSITION_BOTTOM);
  921. BIND_ENUM_CONSTANT(POSITION_MAX);
  922. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabContainer, side_margin);
  923. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabContainer, tab_separation);
  924. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabContainer, panel_style, "panel");
  925. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabContainer, tabbar_style, "tabbar_background");
  926. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabContainer, menu_icon, "menu");
  927. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabContainer, menu_hl_icon, "menu_highlight");
  928. // TabBar overrides.
  929. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabContainer, icon_separation);
  930. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabContainer, icon_max_width);
  931. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabContainer, tab_unselected_style, "tab_unselected");
  932. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabContainer, tab_hovered_style, "tab_hovered");
  933. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabContainer, tab_selected_style, "tab_selected");
  934. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabContainer, tab_disabled_style, "tab_disabled");
  935. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabContainer, tab_focus_style, "tab_focus");
  936. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabContainer, increment_icon, "increment");
  937. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabContainer, increment_hl_icon, "increment_highlight");
  938. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabContainer, decrement_icon, "decrement");
  939. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabContainer, decrement_hl_icon, "decrement_highlight");
  940. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabContainer, drop_mark_icon, "drop_mark");
  941. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabContainer, drop_mark_color);
  942. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabContainer, font_selected_color);
  943. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabContainer, font_hovered_color);
  944. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabContainer, font_unselected_color);
  945. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabContainer, font_disabled_color);
  946. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabContainer, font_outline_color);
  947. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT, TabContainer, tab_font, "font");
  948. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT_SIZE, TabContainer, tab_font_size, "font_size");
  949. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabContainer, outline_size);
  950. }
  951. TabContainer::TabContainer() {
  952. tab_bar = memnew(TabBar);
  953. SET_DRAG_FORWARDING_GCDU(tab_bar, TabContainer);
  954. add_child(tab_bar, false, INTERNAL_MODE_FRONT);
  955. tab_bar->set_use_parent_material(true);
  956. tab_bar->set_anchors_and_offsets_preset(Control::PRESET_TOP_WIDE);
  957. tab_bar->connect("tab_changed", callable_mp(this, &TabContainer::_on_tab_changed));
  958. tab_bar->connect("tab_clicked", callable_mp(this, &TabContainer::_on_tab_clicked));
  959. tab_bar->connect("tab_hovered", callable_mp(this, &TabContainer::_on_tab_hovered));
  960. tab_bar->connect("tab_selected", callable_mp(this, &TabContainer::_on_tab_selected));
  961. tab_bar->connect("tab_button_pressed", callable_mp(this, &TabContainer::_on_tab_button_pressed));
  962. tab_bar->connect("active_tab_rearranged", callable_mp(this, &TabContainer::_on_active_tab_rearranged));
  963. connect(SceneStringName(mouse_exited), callable_mp(this, &TabContainer::_on_mouse_exited));
  964. }