tab_container.cpp 30 KB

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