tab_container.cpp 33 KB

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