tab_container.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*************************************************************************/
  2. /* tab_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "tab_container.h"
  31. #include "core/message_queue.h"
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/gui/texture_rect.h"
  35. int TabContainer::_get_top_margin() const {
  36. if (!tabs_visible)
  37. return 0;
  38. // Respect the minimum tab height.
  39. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  40. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  41. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  42. int tab_height = MAX(MAX(tab_bg->get_minimum_size().height, tab_fg->get_minimum_size().height), tab_disabled->get_minimum_size().height);
  43. // Font height or higher icon wins.
  44. Ref<Font> font = get_font("font");
  45. int content_height = font->get_height();
  46. Vector<Control *> tabs = _get_tabs();
  47. for (int i = 0; i < tabs.size(); i++) {
  48. Control *c = tabs[i];
  49. if (!c->has_meta("_tab_icon"))
  50. continue;
  51. Ref<Texture> tex = c->get_meta("_tab_icon");
  52. if (!tex.is_valid())
  53. continue;
  54. content_height = MAX(content_height, tex->get_size().height);
  55. }
  56. return tab_height + content_height;
  57. }
  58. void TabContainer::_gui_input(const Ref<InputEvent> &p_event) {
  59. Ref<InputEventMouseButton> mb = p_event;
  60. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  61. Point2 pos(mb->get_position().x, mb->get_position().y);
  62. Size2 size = get_size();
  63. // Click must be on tabs in the tab header area.
  64. if (pos.x < tabs_ofs_cache || pos.y > _get_top_margin())
  65. return;
  66. // Handle menu button.
  67. Ref<Texture> menu = get_icon("menu");
  68. if (popup && pos.x > size.width - menu->get_width()) {
  69. emit_signal("pre_popup_pressed");
  70. Vector2 popup_pos = get_global_position();
  71. popup_pos.x += size.width - popup->get_size().width;
  72. popup_pos.y += menu->get_height();
  73. popup->set_global_position(popup_pos);
  74. popup->popup();
  75. return;
  76. }
  77. // Do not activate tabs when tabs is empty
  78. if (get_tab_count() == 0)
  79. return;
  80. Vector<Control *> tabs = _get_tabs();
  81. // Handle navigation buttons.
  82. if (buttons_visible_cache) {
  83. int popup_ofs = 0;
  84. if (popup) {
  85. popup_ofs = menu->get_width();
  86. }
  87. Ref<Texture> increment = get_icon("increment");
  88. Ref<Texture> decrement = get_icon("decrement");
  89. if (pos.x > size.width - increment->get_width() - popup_ofs) {
  90. if (last_tab_cache < tabs.size() - 1) {
  91. first_tab_cache += 1;
  92. update();
  93. }
  94. return;
  95. } else if (pos.x > size.width - increment->get_width() - decrement->get_width() - popup_ofs) {
  96. if (first_tab_cache > 0) {
  97. first_tab_cache -= 1;
  98. update();
  99. }
  100. return;
  101. }
  102. }
  103. // Activate the clicked tab.
  104. pos.x -= tabs_ofs_cache;
  105. for (int i = first_tab_cache; i <= last_tab_cache; i++) {
  106. int tab_width = _get_tab_width(i);
  107. if (pos.x < tab_width) {
  108. if (!get_tab_disabled(i)) {
  109. set_current_tab(i);
  110. }
  111. break;
  112. }
  113. pos.x -= tab_width;
  114. }
  115. }
  116. }
  117. void TabContainer::_notification(int p_what) {
  118. switch (p_what) {
  119. case NOTIFICATION_TRANSLATION_CHANGED: {
  120. minimum_size_changed();
  121. update();
  122. } break;
  123. case NOTIFICATION_RESIZED: {
  124. Vector<Control *> tabs = _get_tabs();
  125. int side_margin = get_constant("side_margin");
  126. Ref<Texture> menu = get_icon("menu");
  127. Ref<Texture> increment = get_icon("increment");
  128. Ref<Texture> decrement = get_icon("decrement");
  129. int header_width = get_size().width - side_margin * 2;
  130. // Find the width of the header area.
  131. if (popup)
  132. header_width -= menu->get_width();
  133. if (buttons_visible_cache)
  134. header_width -= increment->get_width() + decrement->get_width();
  135. if (popup || buttons_visible_cache)
  136. header_width += side_margin;
  137. // Find the width of all tabs after first_tab_cache.
  138. int all_tabs_width = 0;
  139. for (int i = first_tab_cache; i < tabs.size(); i++) {
  140. int tab_width = _get_tab_width(i);
  141. all_tabs_width += tab_width;
  142. }
  143. // Check if tabs before first_tab_cache would fit into the header area.
  144. for (int i = first_tab_cache - 1; i >= 0; i--) {
  145. int tab_width = _get_tab_width(i);
  146. if (all_tabs_width + tab_width > header_width)
  147. break;
  148. all_tabs_width += tab_width;
  149. first_tab_cache--;
  150. }
  151. } break;
  152. case NOTIFICATION_DRAW: {
  153. RID canvas = get_canvas_item();
  154. Size2 size = get_size();
  155. // Draw only the tab area if the header is hidden.
  156. Ref<StyleBox> panel = get_stylebox("panel");
  157. if (!tabs_visible) {
  158. panel->draw(canvas, Rect2(0, 0, size.width, size.height));
  159. return;
  160. }
  161. Vector<Control *> tabs = _get_tabs();
  162. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  163. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  164. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  165. Ref<Texture> increment = get_icon("increment");
  166. Ref<Texture> decrement = get_icon("decrement");
  167. Ref<Texture> menu = get_icon("menu");
  168. Ref<Texture> menu_hl = get_icon("menu_hl");
  169. Ref<Font> font = get_font("font");
  170. Color font_color_fg = get_color("font_color_fg");
  171. Color font_color_bg = get_color("font_color_bg");
  172. Color font_color_disabled = get_color("font_color_disabled");
  173. int side_margin = get_constant("side_margin");
  174. int icon_text_distance = get_constant("hseparation");
  175. // Find out start and width of the header area.
  176. int header_x = side_margin;
  177. int header_width = size.width - side_margin * 2;
  178. int header_height = _get_top_margin();
  179. if (popup)
  180. header_width -= menu->get_width();
  181. // Check if all tabs would fit into the header area.
  182. int all_tabs_width = 0;
  183. for (int i = 0; i < tabs.size(); i++) {
  184. int tab_width = _get_tab_width(i);
  185. all_tabs_width += tab_width;
  186. if (all_tabs_width > header_width) {
  187. // Not all tabs are visible at the same time - reserve space for navigation buttons.
  188. buttons_visible_cache = true;
  189. header_width -= decrement->get_width() + increment->get_width();
  190. break;
  191. } else {
  192. buttons_visible_cache = false;
  193. }
  194. }
  195. // With buttons, a right side margin does not need to be respected.
  196. if (popup || buttons_visible_cache) {
  197. header_width += side_margin;
  198. }
  199. if (!buttons_visible_cache) {
  200. first_tab_cache = 0;
  201. }
  202. // Go through the visible tabs to find the width they occupy.
  203. all_tabs_width = 0;
  204. Vector<int> tab_widths;
  205. for (int i = first_tab_cache; i < tabs.size(); i++) {
  206. int tab_width = _get_tab_width(i);
  207. if (all_tabs_width + tab_width > header_width && tab_widths.size() > 0)
  208. break;
  209. all_tabs_width += tab_width;
  210. tab_widths.push_back(tab_width);
  211. }
  212. // Find the offset at which to draw tabs, according to the alignment.
  213. switch (align) {
  214. case ALIGN_LEFT:
  215. tabs_ofs_cache = header_x;
  216. break;
  217. case ALIGN_CENTER:
  218. tabs_ofs_cache = header_x + (header_width / 2) - (all_tabs_width / 2);
  219. break;
  220. case ALIGN_RIGHT:
  221. tabs_ofs_cache = header_x + header_width - all_tabs_width;
  222. break;
  223. }
  224. // Draw the tab area.
  225. panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
  226. // Draw all visible tabs.
  227. int x = 0;
  228. for (int i = 0; i < tab_widths.size(); i++) {
  229. Ref<StyleBox> tab_style;
  230. Color font_color;
  231. if (get_tab_disabled(i + first_tab_cache)) {
  232. tab_style = tab_disabled;
  233. font_color = font_color_disabled;
  234. } else if (i + first_tab_cache == current) {
  235. tab_style = tab_fg;
  236. font_color = font_color_fg;
  237. } else {
  238. tab_style = tab_bg;
  239. font_color = font_color_bg;
  240. }
  241. // Draw the tab background.
  242. int tab_width = tab_widths[i];
  243. Rect2 tab_rect(tabs_ofs_cache + x, 0, tab_width, header_height);
  244. tab_style->draw(canvas, tab_rect);
  245. // Draw the tab contents.
  246. Control *control = Object::cast_to<Control>(tabs[i + first_tab_cache]);
  247. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(control->get_name());
  248. int x_content = tab_rect.position.x + tab_style->get_margin(MARGIN_LEFT);
  249. int top_margin = tab_style->get_margin(MARGIN_TOP);
  250. int y_center = top_margin + (tab_rect.size.y - tab_style->get_minimum_size().y) / 2;
  251. // Draw the tab icon.
  252. if (control->has_meta("_tab_icon")) {
  253. Ref<Texture> icon = control->get_meta("_tab_icon");
  254. if (icon.is_valid()) {
  255. int y = y_center - (icon->get_height() / 2);
  256. icon->draw(canvas, Point2i(x_content, y));
  257. if (text != "")
  258. x_content += icon->get_width() + icon_text_distance;
  259. }
  260. }
  261. // Draw the tab text.
  262. Point2i text_pos(x_content, y_center - (font->get_height() / 2) + font->get_ascent());
  263. font->draw(canvas, text_pos, text, font_color);
  264. x += tab_width;
  265. last_tab_cache = i + first_tab_cache;
  266. }
  267. // Draw the popup menu.
  268. x = get_size().width;
  269. if (popup) {
  270. x -= menu->get_width();
  271. if (mouse_x_cache > x)
  272. menu_hl->draw(get_canvas_item(), Size2(x, (header_height - menu_hl->get_height()) / 2));
  273. else
  274. menu->draw(get_canvas_item(), Size2(x, (header_height - menu->get_height()) / 2));
  275. }
  276. // Draw the navigation buttons.
  277. if (buttons_visible_cache) {
  278. int y_center = header_height / 2;
  279. x -= increment->get_width();
  280. increment->draw(canvas,
  281. Point2(x, y_center - (increment->get_height() / 2)),
  282. Color(1, 1, 1, last_tab_cache < tabs.size() - 1 ? 1.0 : 0.5));
  283. x -= decrement->get_width();
  284. decrement->draw(canvas,
  285. Point2(x, y_center - (decrement->get_height() / 2)),
  286. Color(1, 1, 1, first_tab_cache > 0 ? 1.0 : 0.5));
  287. }
  288. } break;
  289. case NOTIFICATION_THEME_CHANGED: {
  290. minimum_size_changed();
  291. call_deferred("_on_theme_changed"); //wait until all changed theme
  292. } break;
  293. }
  294. }
  295. void TabContainer::_on_theme_changed() {
  296. if (get_tab_count() > 0) {
  297. set_current_tab(get_current_tab());
  298. }
  299. }
  300. int TabContainer::_get_tab_width(int p_index) const {
  301. ERR_FAIL_INDEX_V(p_index, get_tab_count(), 0);
  302. Control *control = Object::cast_to<Control>(_get_tabs()[p_index]);
  303. if (!control || control->is_set_as_toplevel())
  304. return 0;
  305. // Get the width of the text displayed on the tab.
  306. Ref<Font> font = get_font("font");
  307. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(control->get_name());
  308. int width = font->get_string_size(text).width;
  309. // Add space for a tab icon.
  310. if (control->has_meta("_tab_icon")) {
  311. Ref<Texture> icon = control->get_meta("_tab_icon");
  312. if (icon.is_valid()) {
  313. width += icon->get_width();
  314. if (text != "")
  315. width += get_constant("hseparation");
  316. }
  317. }
  318. // Respect a minimum size.
  319. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  320. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  321. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  322. if (get_tab_disabled(p_index)) {
  323. width += tab_disabled->get_minimum_size().width;
  324. } else if (p_index == current) {
  325. width += tab_fg->get_minimum_size().width;
  326. } else {
  327. width += tab_bg->get_minimum_size().width;
  328. }
  329. return width;
  330. }
  331. Vector<Control *> TabContainer::_get_tabs() const {
  332. Vector<Control *> controls;
  333. for (int i = 0; i < get_child_count(); i++) {
  334. Control *control = Object::cast_to<Control>(get_child(i));
  335. if (!control || control->is_toplevel_control())
  336. continue;
  337. controls.push_back(control);
  338. }
  339. return controls;
  340. }
  341. void TabContainer::_child_renamed_callback() {
  342. update();
  343. }
  344. void TabContainer::add_child_notify(Node *p_child) {
  345. Container::add_child_notify(p_child);
  346. Control *c = Object::cast_to<Control>(p_child);
  347. if (!c)
  348. return;
  349. if (c->is_set_as_toplevel())
  350. return;
  351. bool first = false;
  352. if (get_tab_count() != 1)
  353. c->hide();
  354. else {
  355. c->show();
  356. //call_deferred("set_current_tab",0);
  357. first = true;
  358. current = 0;
  359. previous = 0;
  360. }
  361. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  362. if (tabs_visible)
  363. c->set_margin(MARGIN_TOP, _get_top_margin());
  364. Ref<StyleBox> sb = get_stylebox("panel");
  365. c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
  366. c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
  367. c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
  368. c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
  369. update();
  370. p_child->connect("renamed", this, "_child_renamed_callback");
  371. if (first)
  372. emit_signal("tab_changed", current);
  373. }
  374. int TabContainer::get_tab_count() const {
  375. return _get_tabs().size();
  376. }
  377. void TabContainer::set_current_tab(int p_current) {
  378. ERR_FAIL_INDEX(p_current, get_tab_count());
  379. int pending_previous = current;
  380. current = p_current;
  381. Ref<StyleBox> sb = get_stylebox("panel");
  382. Vector<Control *> tabs = _get_tabs();
  383. for (int i = 0; i < tabs.size(); i++) {
  384. Control *c = tabs[i];
  385. if (i == current) {
  386. c->show();
  387. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  388. if (tabs_visible)
  389. c->set_margin(MARGIN_TOP, _get_top_margin());
  390. c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
  391. c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
  392. c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
  393. c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
  394. } else
  395. c->hide();
  396. }
  397. _change_notify("current_tab");
  398. if (pending_previous == current)
  399. emit_signal("tab_selected", current);
  400. else {
  401. previous = pending_previous;
  402. emit_signal("tab_selected", current);
  403. emit_signal("tab_changed", current);
  404. }
  405. update();
  406. }
  407. int TabContainer::get_current_tab() const {
  408. return current;
  409. }
  410. int TabContainer::get_previous_tab() const {
  411. return previous;
  412. }
  413. Control *TabContainer::get_tab_control(int p_idx) const {
  414. Vector<Control *> tabs = _get_tabs();
  415. if (p_idx >= 0 && p_idx < tabs.size())
  416. return tabs[p_idx];
  417. else
  418. return NULL;
  419. }
  420. Control *TabContainer::get_current_tab_control() const {
  421. Vector<Control *> tabs = _get_tabs();
  422. if (current >= 0 && current < tabs.size())
  423. return tabs[current];
  424. else
  425. return NULL;
  426. }
  427. void TabContainer::remove_child_notify(Node *p_child) {
  428. Container::remove_child_notify(p_child);
  429. call_deferred("_update_current_tab");
  430. p_child->disconnect("renamed", this, "_child_renamed_callback");
  431. update();
  432. }
  433. void TabContainer::_update_current_tab() {
  434. int tc = get_tab_count();
  435. if (current >= tc)
  436. current = tc - 1;
  437. if (current < 0)
  438. current = 0;
  439. else
  440. set_current_tab(current);
  441. }
  442. Variant TabContainer::get_drag_data(const Point2 &p_point) {
  443. if (!drag_to_rearrange_enabled)
  444. return Variant();
  445. int tab_over = get_tab_idx_at_point(p_point);
  446. if (tab_over < 0)
  447. return Variant();
  448. HBoxContainer *drag_preview = memnew(HBoxContainer);
  449. Ref<Texture> icon = get_tab_icon(tab_over);
  450. if (!icon.is_null()) {
  451. TextureRect *tf = memnew(TextureRect);
  452. tf->set_texture(icon);
  453. drag_preview->add_child(tf);
  454. }
  455. Label *label = memnew(Label(get_tab_title(tab_over)));
  456. drag_preview->add_child(label);
  457. set_drag_preview(drag_preview);
  458. Dictionary drag_data;
  459. drag_data["type"] = "tabc_element";
  460. drag_data["tabc_element"] = tab_over;
  461. drag_data["from_path"] = get_path();
  462. return drag_data;
  463. }
  464. bool TabContainer::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  465. if (!drag_to_rearrange_enabled)
  466. return false;
  467. Dictionary d = p_data;
  468. if (!d.has("type"))
  469. return false;
  470. if (String(d["type"]) == "tabc_element") {
  471. NodePath from_path = d["from_path"];
  472. NodePath to_path = get_path();
  473. if (from_path == to_path) {
  474. return true;
  475. } else if (get_tabs_rearrange_group() != -1) {
  476. // drag and drop between other TabContainers
  477. Node *from_node = get_node(from_path);
  478. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  479. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  480. return true;
  481. }
  482. }
  483. }
  484. return false;
  485. }
  486. void TabContainer::drop_data(const Point2 &p_point, const Variant &p_data) {
  487. if (!drag_to_rearrange_enabled)
  488. return;
  489. int hover_now = get_tab_idx_at_point(p_point);
  490. Dictionary d = p_data;
  491. if (!d.has("type"))
  492. return;
  493. if (String(d["type"]) == "tabc_element") {
  494. int tab_from_id = d["tabc_element"];
  495. NodePath from_path = d["from_path"];
  496. NodePath to_path = get_path();
  497. if (from_path == to_path) {
  498. if (hover_now < 0)
  499. hover_now = get_tab_count() - 1;
  500. move_child(get_tab_control(tab_from_id), hover_now);
  501. set_current_tab(hover_now);
  502. } else if (get_tabs_rearrange_group() != -1) {
  503. // drag and drop between TabContainers
  504. Node *from_node = get_node(from_path);
  505. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  506. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  507. Control *moving_tabc = from_tabc->get_tab_control(tab_from_id);
  508. from_tabc->remove_child(moving_tabc);
  509. add_child(moving_tabc);
  510. if (hover_now < 0)
  511. hover_now = get_tab_count() - 1;
  512. move_child(moving_tabc, hover_now);
  513. set_current_tab(hover_now);
  514. emit_signal("tab_changed", hover_now);
  515. }
  516. }
  517. }
  518. update();
  519. }
  520. int TabContainer::get_tab_idx_at_point(const Point2 &p_point) const {
  521. if (get_tab_count() == 0)
  522. return -1;
  523. // must be on tabs in the tab header area.
  524. if (p_point.x < tabs_ofs_cache || p_point.y > _get_top_margin())
  525. return -1;
  526. Size2 size = get_size();
  527. int right_ofs = 0;
  528. if (popup) {
  529. Ref<Texture> menu = get_icon("menu");
  530. right_ofs += menu->get_width();
  531. }
  532. if (buttons_visible_cache) {
  533. Ref<Texture> increment = get_icon("increment");
  534. Ref<Texture> decrement = get_icon("decrement");
  535. right_ofs += increment->get_width() + decrement->get_width();
  536. }
  537. if (p_point.x > size.width - right_ofs) {
  538. return -1;
  539. }
  540. // get the tab at the point
  541. Vector<Control *> tabs = _get_tabs();
  542. int px = p_point.x;
  543. px -= tabs_ofs_cache;
  544. for (int i = first_tab_cache; i <= last_tab_cache; i++) {
  545. int tab_width = _get_tab_width(i);
  546. if (px < tab_width) {
  547. return i;
  548. }
  549. px -= tab_width;
  550. }
  551. return -1;
  552. }
  553. void TabContainer::set_tab_align(TabAlign p_align) {
  554. ERR_FAIL_INDEX(p_align, 3);
  555. align = p_align;
  556. update();
  557. _change_notify("tab_align");
  558. }
  559. TabContainer::TabAlign TabContainer::get_tab_align() const {
  560. return align;
  561. }
  562. void TabContainer::set_tabs_visible(bool p_visibe) {
  563. if (p_visibe == tabs_visible)
  564. return;
  565. tabs_visible = p_visibe;
  566. Vector<Control *> tabs = _get_tabs();
  567. for (int i = 0; i < tabs.size(); i++) {
  568. Control *c = tabs[i];
  569. if (p_visibe)
  570. c->set_margin(MARGIN_TOP, _get_top_margin());
  571. else
  572. c->set_margin(MARGIN_TOP, 0);
  573. }
  574. update();
  575. }
  576. bool TabContainer::are_tabs_visible() const {
  577. return tabs_visible;
  578. }
  579. Control *TabContainer::_get_tab(int p_idx) const {
  580. return get_tab_control(p_idx);
  581. }
  582. void TabContainer::set_tab_title(int p_tab, const String &p_title) {
  583. Control *child = _get_tab(p_tab);
  584. ERR_FAIL_COND(!child);
  585. child->set_meta("_tab_name", p_title);
  586. update();
  587. }
  588. String TabContainer::get_tab_title(int p_tab) const {
  589. Control *child = _get_tab(p_tab);
  590. ERR_FAIL_COND_V(!child, "");
  591. if (child->has_meta("_tab_name"))
  592. return child->get_meta("_tab_name");
  593. else
  594. return child->get_name();
  595. }
  596. void TabContainer::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) {
  597. Control *child = _get_tab(p_tab);
  598. ERR_FAIL_COND(!child);
  599. child->set_meta("_tab_icon", p_icon);
  600. update();
  601. }
  602. Ref<Texture> TabContainer::get_tab_icon(int p_tab) const {
  603. Control *child = _get_tab(p_tab);
  604. ERR_FAIL_COND_V(!child, Ref<Texture>());
  605. if (child->has_meta("_tab_icon"))
  606. return child->get_meta("_tab_icon");
  607. else
  608. return Ref<Texture>();
  609. }
  610. void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) {
  611. Control *child = _get_tab(p_tab);
  612. ERR_FAIL_COND(!child);
  613. child->set_meta("_tab_disabled", p_disabled);
  614. update();
  615. }
  616. bool TabContainer::get_tab_disabled(int p_tab) const {
  617. Control *child = _get_tab(p_tab);
  618. ERR_FAIL_COND_V(!child, false);
  619. if (child->has_meta("_tab_disabled"))
  620. return child->get_meta("_tab_disabled");
  621. else
  622. return false;
  623. }
  624. void TabContainer::get_translatable_strings(List<String> *p_strings) const {
  625. Vector<Control *> tabs = _get_tabs();
  626. for (int i = 0; i < tabs.size(); i++) {
  627. Control *c = tabs[i];
  628. if (!c->has_meta("_tab_name"))
  629. continue;
  630. String name = c->get_meta("_tab_name");
  631. if (name != "")
  632. p_strings->push_back(name);
  633. }
  634. }
  635. Size2 TabContainer::get_minimum_size() const {
  636. Size2 ms;
  637. Vector<Control *> tabs = _get_tabs();
  638. for (int i = 0; i < tabs.size(); i++) {
  639. Control *c = tabs[i];
  640. if (!c->is_visible_in_tree())
  641. continue;
  642. Size2 cms = c->get_combined_minimum_size();
  643. ms.x = MAX(ms.x, cms.x);
  644. ms.y = MAX(ms.y, cms.y);
  645. }
  646. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  647. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  648. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  649. Ref<Font> font = get_font("font");
  650. ms.y += MAX(MAX(tab_bg->get_minimum_size().y, tab_fg->get_minimum_size().y), tab_disabled->get_minimum_size().y);
  651. ms.y += font->get_height();
  652. Ref<StyleBox> sb = get_stylebox("panel");
  653. ms += sb->get_minimum_size();
  654. return ms;
  655. }
  656. void TabContainer::set_popup(Node *p_popup) {
  657. ERR_FAIL_NULL(p_popup);
  658. popup = Object::cast_to<Popup>(p_popup);
  659. update();
  660. }
  661. Popup *TabContainer::get_popup() const {
  662. return popup;
  663. }
  664. void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
  665. drag_to_rearrange_enabled = p_enabled;
  666. }
  667. bool TabContainer::get_drag_to_rearrange_enabled() const {
  668. return drag_to_rearrange_enabled;
  669. }
  670. void TabContainer::set_tabs_rearrange_group(int p_group_id) {
  671. tabs_rearrange_group = p_group_id;
  672. }
  673. int TabContainer::get_tabs_rearrange_group() const {
  674. return tabs_rearrange_group;
  675. }
  676. void TabContainer::_bind_methods() {
  677. ClassDB::bind_method(D_METHOD("_gui_input"), &TabContainer::_gui_input);
  678. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabContainer::get_tab_count);
  679. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
  680. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabContainer::get_current_tab);
  681. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabContainer::get_previous_tab);
  682. ClassDB::bind_method(D_METHOD("get_current_tab_control"), &TabContainer::get_current_tab_control);
  683. ClassDB::bind_method(D_METHOD("get_tab_control", "idx"), &TabContainer::get_tab_control);
  684. ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &TabContainer::set_tab_align);
  685. ClassDB::bind_method(D_METHOD("get_tab_align"), &TabContainer::get_tab_align);
  686. ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
  687. ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
  688. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
  689. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
  690. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon);
  691. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon);
  692. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled);
  693. ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &TabContainer::get_tab_disabled);
  694. ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
  695. ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
  696. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabContainer::set_drag_to_rearrange_enabled);
  697. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabContainer::get_drag_to_rearrange_enabled);
  698. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabContainer::set_tabs_rearrange_group);
  699. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabContainer::get_tabs_rearrange_group);
  700. ClassDB::bind_method(D_METHOD("_child_renamed_callback"), &TabContainer::_child_renamed_callback);
  701. ClassDB::bind_method(D_METHOD("_on_theme_changed"), &TabContainer::_on_theme_changed);
  702. ClassDB::bind_method(D_METHOD("_update_current_tab"), &TabContainer::_update_current_tab);
  703. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  704. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  705. ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
  706. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
  707. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
  708. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
  709. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  710. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  711. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  712. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  713. }
  714. TabContainer::TabContainer() {
  715. first_tab_cache = 0;
  716. last_tab_cache = 0;
  717. buttons_visible_cache = false;
  718. tabs_ofs_cache = 0;
  719. current = 0;
  720. previous = 0;
  721. mouse_x_cache = 0;
  722. align = ALIGN_CENTER;
  723. tabs_visible = true;
  724. popup = NULL;
  725. drag_to_rearrange_enabled = false;
  726. tabs_rearrange_group = -1;
  727. }