2
0

tab_container.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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_RESIZED: {
  120. Vector<Control *> tabs = _get_tabs();
  121. int side_margin = get_constant("side_margin");
  122. Ref<Texture> menu = get_icon("menu");
  123. Ref<Texture> increment = get_icon("increment");
  124. Ref<Texture> decrement = get_icon("decrement");
  125. int header_width = get_size().width - side_margin * 2;
  126. // Find the width of the header area.
  127. if (popup)
  128. header_width -= menu->get_width();
  129. if (buttons_visible_cache)
  130. header_width -= increment->get_width() + decrement->get_width();
  131. if (popup || buttons_visible_cache)
  132. header_width += side_margin;
  133. // Find the width of all tabs after first_tab_cache.
  134. int all_tabs_width = 0;
  135. for (int i = first_tab_cache; i < tabs.size(); i++) {
  136. int tab_width = _get_tab_width(i);
  137. all_tabs_width += tab_width;
  138. }
  139. // Check if tabs before first_tab_cache would fit into the header area.
  140. for (int i = first_tab_cache - 1; i >= 0; i--) {
  141. int tab_width = _get_tab_width(i);
  142. if (all_tabs_width + tab_width > header_width)
  143. break;
  144. all_tabs_width += tab_width;
  145. first_tab_cache--;
  146. }
  147. } break;
  148. case NOTIFICATION_DRAW: {
  149. RID canvas = get_canvas_item();
  150. Size2 size = get_size();
  151. // Draw only the tab area if the header is hidden.
  152. Ref<StyleBox> panel = get_stylebox("panel");
  153. if (!tabs_visible) {
  154. panel->draw(canvas, Rect2(0, 0, size.width, size.height));
  155. return;
  156. }
  157. Vector<Control *> tabs = _get_tabs();
  158. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  159. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  160. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  161. Ref<Texture> increment = get_icon("increment");
  162. Ref<Texture> decrement = get_icon("decrement");
  163. Ref<Texture> menu = get_icon("menu");
  164. Ref<Texture> menu_hl = get_icon("menu_hl");
  165. Ref<Font> font = get_font("font");
  166. Color font_color_fg = get_color("font_color_fg");
  167. Color font_color_bg = get_color("font_color_bg");
  168. Color font_color_disabled = get_color("font_color_disabled");
  169. int side_margin = get_constant("side_margin");
  170. int icon_text_distance = get_constant("hseparation");
  171. // Find out start and width of the header area.
  172. int header_x = side_margin;
  173. int header_width = size.width - side_margin * 2;
  174. int header_height = _get_top_margin();
  175. if (popup)
  176. header_width -= menu->get_width();
  177. // Check if all tabs would fit into the header area.
  178. int all_tabs_width = 0;
  179. for (int i = 0; i < tabs.size(); i++) {
  180. int tab_width = _get_tab_width(i);
  181. all_tabs_width += tab_width;
  182. if (all_tabs_width > header_width) {
  183. // Not all tabs are visible at the same time - reserve space for navigation buttons.
  184. buttons_visible_cache = true;
  185. header_width -= decrement->get_width() + increment->get_width();
  186. break;
  187. } else {
  188. buttons_visible_cache = false;
  189. }
  190. }
  191. // With buttons, a right side margin does not need to be respected.
  192. if (popup || buttons_visible_cache) {
  193. header_width += side_margin;
  194. }
  195. if (!buttons_visible_cache) {
  196. first_tab_cache = 0;
  197. }
  198. // Go through the visible tabs to find the width they occupy.
  199. all_tabs_width = 0;
  200. Vector<int> tab_widths;
  201. for (int i = first_tab_cache; i < tabs.size(); i++) {
  202. int tab_width = _get_tab_width(i);
  203. if (all_tabs_width + tab_width > header_width && tab_widths.size() > 0)
  204. break;
  205. all_tabs_width += tab_width;
  206. tab_widths.push_back(tab_width);
  207. }
  208. // Find the offset at which to draw tabs, according to the alignment.
  209. switch (align) {
  210. case ALIGN_LEFT:
  211. tabs_ofs_cache = header_x;
  212. break;
  213. case ALIGN_CENTER:
  214. tabs_ofs_cache = header_x + (header_width / 2) - (all_tabs_width / 2);
  215. break;
  216. case ALIGN_RIGHT:
  217. tabs_ofs_cache = header_x + header_width - all_tabs_width;
  218. break;
  219. }
  220. // Draw the tab area.
  221. panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
  222. // Draw all visible tabs.
  223. int x = 0;
  224. for (int i = 0; i < tab_widths.size(); i++) {
  225. Ref<StyleBox> tab_style;
  226. Color font_color;
  227. if (get_tab_disabled(i + first_tab_cache)) {
  228. tab_style = tab_disabled;
  229. font_color = font_color_disabled;
  230. } else if (i + first_tab_cache == current) {
  231. tab_style = tab_fg;
  232. font_color = font_color_fg;
  233. } else {
  234. tab_style = tab_bg;
  235. font_color = font_color_bg;
  236. }
  237. // Draw the tab background.
  238. int tab_width = tab_widths[i];
  239. Rect2 tab_rect(tabs_ofs_cache + x, 0, tab_width, header_height);
  240. tab_style->draw(canvas, tab_rect);
  241. // Draw the tab contents.
  242. Control *control = Object::cast_to<Control>(tabs[i + first_tab_cache]);
  243. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(control->get_name());
  244. int x_content = tab_rect.position.x + tab_style->get_margin(MARGIN_LEFT);
  245. int top_margin = tab_style->get_margin(MARGIN_TOP);
  246. int y_center = top_margin + (tab_rect.size.y - tab_style->get_minimum_size().y) / 2;
  247. // Draw the tab icon.
  248. if (control->has_meta("_tab_icon")) {
  249. Ref<Texture> icon = control->get_meta("_tab_icon");
  250. if (icon.is_valid()) {
  251. int y = y_center - (icon->get_height() / 2);
  252. icon->draw(canvas, Point2i(x_content, y));
  253. if (text != "")
  254. x_content += icon->get_width() + icon_text_distance;
  255. }
  256. }
  257. // Draw the tab text.
  258. Point2i text_pos(x_content, y_center - (font->get_height() / 2) + font->get_ascent());
  259. font->draw(canvas, text_pos, text, font_color);
  260. x += tab_width;
  261. last_tab_cache = i + first_tab_cache;
  262. }
  263. // Draw the popup menu.
  264. x = get_size().width;
  265. if (popup) {
  266. x -= menu->get_width();
  267. if (mouse_x_cache > x)
  268. menu_hl->draw(get_canvas_item(), Size2(x, (header_height - menu_hl->get_height()) / 2));
  269. else
  270. menu->draw(get_canvas_item(), Size2(x, (header_height - menu->get_height()) / 2));
  271. }
  272. // Draw the navigation buttons.
  273. if (buttons_visible_cache) {
  274. int y_center = header_height / 2;
  275. x -= increment->get_width();
  276. increment->draw(canvas,
  277. Point2(x, y_center - (increment->get_height() / 2)),
  278. Color(1, 1, 1, last_tab_cache < tabs.size() - 1 ? 1.0 : 0.5));
  279. x -= decrement->get_width();
  280. decrement->draw(canvas,
  281. Point2(x, y_center - (decrement->get_height() / 2)),
  282. Color(1, 1, 1, first_tab_cache > 0 ? 1.0 : 0.5));
  283. }
  284. } break;
  285. case NOTIFICATION_THEME_CHANGED: {
  286. minimum_size_changed();
  287. call_deferred("_on_theme_changed"); //wait until all changed theme
  288. } break;
  289. }
  290. }
  291. void TabContainer::_on_theme_changed() {
  292. if (get_tab_count() > 0) {
  293. set_current_tab(get_current_tab());
  294. }
  295. }
  296. int TabContainer::_get_tab_width(int p_index) const {
  297. ERR_FAIL_INDEX_V(p_index, get_tab_count(), 0);
  298. Control *control = Object::cast_to<Control>(_get_tabs()[p_index]);
  299. if (!control || control->is_set_as_toplevel())
  300. return 0;
  301. // Get the width of the text displayed on the tab.
  302. Ref<Font> font = get_font("font");
  303. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(control->get_name());
  304. int width = font->get_string_size(text).width;
  305. // Add space for a tab icon.
  306. if (control->has_meta("_tab_icon")) {
  307. Ref<Texture> icon = control->get_meta("_tab_icon");
  308. if (icon.is_valid()) {
  309. width += icon->get_width();
  310. if (text != "")
  311. width += get_constant("hseparation");
  312. }
  313. }
  314. // Respect a minimum size.
  315. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  316. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  317. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  318. if (get_tab_disabled(p_index)) {
  319. width += tab_disabled->get_minimum_size().width;
  320. } else if (p_index == current) {
  321. width += tab_fg->get_minimum_size().width;
  322. } else {
  323. width += tab_bg->get_minimum_size().width;
  324. }
  325. return width;
  326. }
  327. Vector<Control *> TabContainer::_get_tabs() const {
  328. Vector<Control *> controls;
  329. for (int i = 0; i < get_child_count(); i++) {
  330. Control *control = Object::cast_to<Control>(get_child(i));
  331. if (!control || control->is_toplevel_control())
  332. continue;
  333. controls.push_back(control);
  334. }
  335. return controls;
  336. }
  337. void TabContainer::_child_renamed_callback() {
  338. update();
  339. }
  340. void TabContainer::add_child_notify(Node *p_child) {
  341. Container::add_child_notify(p_child);
  342. Control *c = Object::cast_to<Control>(p_child);
  343. if (!c)
  344. return;
  345. if (c->is_set_as_toplevel())
  346. return;
  347. bool first = false;
  348. if (get_tab_count() != 1)
  349. c->hide();
  350. else {
  351. c->show();
  352. //call_deferred("set_current_tab",0);
  353. first = true;
  354. current = 0;
  355. previous = 0;
  356. }
  357. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  358. if (tabs_visible)
  359. c->set_margin(MARGIN_TOP, _get_top_margin());
  360. Ref<StyleBox> sb = get_stylebox("panel");
  361. c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
  362. c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
  363. c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
  364. c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
  365. update();
  366. p_child->connect("renamed", this, "_child_renamed_callback");
  367. if (first)
  368. emit_signal("tab_changed", current);
  369. }
  370. int TabContainer::get_tab_count() const {
  371. return _get_tabs().size();
  372. }
  373. void TabContainer::set_current_tab(int p_current) {
  374. ERR_FAIL_INDEX(p_current, get_tab_count());
  375. int pending_previous = current;
  376. current = p_current;
  377. Ref<StyleBox> sb = get_stylebox("panel");
  378. Vector<Control *> tabs = _get_tabs();
  379. for (int i = 0; i < tabs.size(); i++) {
  380. Control *c = tabs[i];
  381. if (i == current) {
  382. c->show();
  383. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  384. if (tabs_visible)
  385. c->set_margin(MARGIN_TOP, _get_top_margin());
  386. c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
  387. c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
  388. c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
  389. c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
  390. } else
  391. c->hide();
  392. }
  393. _change_notify("current_tab");
  394. if (pending_previous == current)
  395. emit_signal("tab_selected", current);
  396. else {
  397. previous = pending_previous;
  398. emit_signal("tab_selected", current);
  399. emit_signal("tab_changed", current);
  400. }
  401. update();
  402. }
  403. int TabContainer::get_current_tab() const {
  404. return current;
  405. }
  406. int TabContainer::get_previous_tab() const {
  407. return previous;
  408. }
  409. Control *TabContainer::get_tab_control(int p_idx) const {
  410. Vector<Control *> tabs = _get_tabs();
  411. if (p_idx >= 0 && p_idx < tabs.size())
  412. return tabs[p_idx];
  413. else
  414. return NULL;
  415. }
  416. Control *TabContainer::get_current_tab_control() const {
  417. Vector<Control *> tabs = _get_tabs();
  418. if (current >= 0 && current < tabs.size())
  419. return tabs[current];
  420. else
  421. return NULL;
  422. }
  423. void TabContainer::remove_child_notify(Node *p_child) {
  424. Container::remove_child_notify(p_child);
  425. call_deferred("_update_current_tab");
  426. p_child->disconnect("renamed", this, "_child_renamed_callback");
  427. update();
  428. }
  429. void TabContainer::_update_current_tab() {
  430. int tc = get_tab_count();
  431. if (current >= tc)
  432. current = tc - 1;
  433. if (current < 0)
  434. current = 0;
  435. else
  436. set_current_tab(current);
  437. }
  438. Variant TabContainer::get_drag_data(const Point2 &p_point) {
  439. if (!drag_to_rearrange_enabled)
  440. return Variant();
  441. int tab_over = get_tab_idx_at_point(p_point);
  442. if (tab_over < 0)
  443. return Variant();
  444. HBoxContainer *drag_preview = memnew(HBoxContainer);
  445. Ref<Texture> icon = get_tab_icon(tab_over);
  446. if (!icon.is_null()) {
  447. TextureRect *tf = memnew(TextureRect);
  448. tf->set_texture(icon);
  449. drag_preview->add_child(tf);
  450. }
  451. Label *label = memnew(Label(get_tab_title(tab_over)));
  452. drag_preview->add_child(label);
  453. set_drag_preview(drag_preview);
  454. Dictionary drag_data;
  455. drag_data["type"] = "tabc_element";
  456. drag_data["tabc_element"] = tab_over;
  457. drag_data["from_path"] = get_path();
  458. return drag_data;
  459. }
  460. bool TabContainer::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  461. if (!drag_to_rearrange_enabled)
  462. return false;
  463. Dictionary d = p_data;
  464. if (!d.has("type"))
  465. return false;
  466. if (String(d["type"]) == "tabc_element") {
  467. NodePath from_path = d["from_path"];
  468. NodePath to_path = get_path();
  469. if (from_path == to_path) {
  470. return true;
  471. } else if (get_tabs_rearrange_group() != -1) {
  472. // drag and drop between other TabContainers
  473. Node *from_node = get_node(from_path);
  474. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  475. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  476. return true;
  477. }
  478. }
  479. }
  480. return false;
  481. }
  482. void TabContainer::drop_data(const Point2 &p_point, const Variant &p_data) {
  483. if (!drag_to_rearrange_enabled)
  484. return;
  485. int hover_now = get_tab_idx_at_point(p_point);
  486. Dictionary d = p_data;
  487. if (!d.has("type"))
  488. return;
  489. if (String(d["type"]) == "tabc_element") {
  490. int tab_from_id = d["tabc_element"];
  491. NodePath from_path = d["from_path"];
  492. NodePath to_path = get_path();
  493. if (from_path == to_path) {
  494. if (hover_now < 0)
  495. hover_now = get_tab_count() - 1;
  496. move_child(get_tab_control(tab_from_id), hover_now);
  497. set_current_tab(hover_now);
  498. } else if (get_tabs_rearrange_group() != -1) {
  499. // drag and drop between TabContainers
  500. Node *from_node = get_node(from_path);
  501. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  502. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  503. Control *moving_tabc = from_tabc->get_tab_control(tab_from_id);
  504. from_tabc->remove_child(moving_tabc);
  505. add_child(moving_tabc);
  506. if (hover_now < 0)
  507. hover_now = get_tab_count() - 1;
  508. move_child(moving_tabc, hover_now);
  509. set_current_tab(hover_now);
  510. emit_signal("tab_changed", hover_now);
  511. }
  512. }
  513. }
  514. update();
  515. }
  516. int TabContainer::get_tab_idx_at_point(const Point2 &p_point) const {
  517. if (get_tab_count() == 0)
  518. return -1;
  519. // must be on tabs in the tab header area.
  520. if (p_point.x < tabs_ofs_cache || p_point.y > _get_top_margin())
  521. return -1;
  522. Size2 size = get_size();
  523. int right_ofs = 0;
  524. if (popup) {
  525. Ref<Texture> menu = get_icon("menu");
  526. right_ofs += menu->get_width();
  527. }
  528. if (buttons_visible_cache) {
  529. Ref<Texture> increment = get_icon("increment");
  530. Ref<Texture> decrement = get_icon("decrement");
  531. right_ofs += increment->get_width() + decrement->get_width();
  532. }
  533. if (p_point.x > size.width - right_ofs) {
  534. return -1;
  535. }
  536. // get the tab at the point
  537. Vector<Control *> tabs = _get_tabs();
  538. int px = p_point.x;
  539. px -= tabs_ofs_cache;
  540. for (int i = first_tab_cache; i <= last_tab_cache; i++) {
  541. int tab_width = _get_tab_width(i);
  542. if (px < tab_width) {
  543. return i;
  544. }
  545. px -= tab_width;
  546. }
  547. return -1;
  548. }
  549. void TabContainer::set_tab_align(TabAlign p_align) {
  550. ERR_FAIL_INDEX(p_align, 3);
  551. align = p_align;
  552. update();
  553. _change_notify("tab_align");
  554. }
  555. TabContainer::TabAlign TabContainer::get_tab_align() const {
  556. return align;
  557. }
  558. void TabContainer::set_tabs_visible(bool p_visibe) {
  559. if (p_visibe == tabs_visible)
  560. return;
  561. tabs_visible = p_visibe;
  562. Vector<Control *> tabs = _get_tabs();
  563. for (int i = 0; i < tabs.size(); i++) {
  564. Control *c = tabs[i];
  565. if (p_visibe)
  566. c->set_margin(MARGIN_TOP, _get_top_margin());
  567. else
  568. c->set_margin(MARGIN_TOP, 0);
  569. }
  570. update();
  571. }
  572. bool TabContainer::are_tabs_visible() const {
  573. return tabs_visible;
  574. }
  575. Control *TabContainer::_get_tab(int p_idx) const {
  576. return get_tab_control(p_idx);
  577. }
  578. void TabContainer::set_tab_title(int p_tab, const String &p_title) {
  579. Control *child = _get_tab(p_tab);
  580. ERR_FAIL_COND(!child);
  581. child->set_meta("_tab_name", p_title);
  582. }
  583. String TabContainer::get_tab_title(int p_tab) const {
  584. Control *child = _get_tab(p_tab);
  585. ERR_FAIL_COND_V(!child, "");
  586. if (child->has_meta("_tab_name"))
  587. return child->get_meta("_tab_name");
  588. else
  589. return child->get_name();
  590. }
  591. void TabContainer::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) {
  592. Control *child = _get_tab(p_tab);
  593. ERR_FAIL_COND(!child);
  594. child->set_meta("_tab_icon", p_icon);
  595. }
  596. Ref<Texture> TabContainer::get_tab_icon(int p_tab) const {
  597. Control *child = _get_tab(p_tab);
  598. ERR_FAIL_COND_V(!child, Ref<Texture>());
  599. if (child->has_meta("_tab_icon"))
  600. return child->get_meta("_tab_icon");
  601. else
  602. return Ref<Texture>();
  603. }
  604. void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) {
  605. Control *child = _get_tab(p_tab);
  606. ERR_FAIL_COND(!child);
  607. child->set_meta("_tab_disabled", p_disabled);
  608. update();
  609. }
  610. bool TabContainer::get_tab_disabled(int p_tab) const {
  611. Control *child = _get_tab(p_tab);
  612. ERR_FAIL_COND_V(!child, false);
  613. if (child->has_meta("_tab_disabled"))
  614. return child->get_meta("_tab_disabled");
  615. else
  616. return false;
  617. }
  618. void TabContainer::get_translatable_strings(List<String> *p_strings) const {
  619. Vector<Control *> tabs = _get_tabs();
  620. for (int i = 0; i < tabs.size(); i++) {
  621. Control *c = tabs[i];
  622. if (!c->has_meta("_tab_name"))
  623. continue;
  624. String name = c->get_meta("_tab_name");
  625. if (name != "")
  626. p_strings->push_back(name);
  627. }
  628. }
  629. Size2 TabContainer::get_minimum_size() const {
  630. Size2 ms;
  631. Vector<Control *> tabs = _get_tabs();
  632. for (int i = 0; i < tabs.size(); i++) {
  633. Control *c = tabs[i];
  634. if (!c->is_visible_in_tree())
  635. continue;
  636. Size2 cms = c->get_combined_minimum_size();
  637. ms.x = MAX(ms.x, cms.x);
  638. ms.y = MAX(ms.y, cms.y);
  639. }
  640. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  641. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  642. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  643. Ref<Font> font = get_font("font");
  644. ms.y += MAX(MAX(tab_bg->get_minimum_size().y, tab_fg->get_minimum_size().y), tab_disabled->get_minimum_size().y);
  645. ms.y += font->get_height();
  646. Ref<StyleBox> sb = get_stylebox("panel");
  647. ms += sb->get_minimum_size();
  648. return ms;
  649. }
  650. void TabContainer::set_popup(Node *p_popup) {
  651. ERR_FAIL_NULL(p_popup);
  652. popup = Object::cast_to<Popup>(p_popup);
  653. update();
  654. }
  655. Popup *TabContainer::get_popup() const {
  656. return popup;
  657. }
  658. void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
  659. drag_to_rearrange_enabled = p_enabled;
  660. }
  661. bool TabContainer::get_drag_to_rearrange_enabled() const {
  662. return drag_to_rearrange_enabled;
  663. }
  664. void TabContainer::set_tabs_rearrange_group(int p_group_id) {
  665. tabs_rearrange_group = p_group_id;
  666. }
  667. int TabContainer::get_tabs_rearrange_group() const {
  668. return tabs_rearrange_group;
  669. }
  670. void TabContainer::_bind_methods() {
  671. ClassDB::bind_method(D_METHOD("_gui_input"), &TabContainer::_gui_input);
  672. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabContainer::get_tab_count);
  673. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
  674. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabContainer::get_current_tab);
  675. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabContainer::get_previous_tab);
  676. ClassDB::bind_method(D_METHOD("get_current_tab_control"), &TabContainer::get_current_tab_control);
  677. ClassDB::bind_method(D_METHOD("get_tab_control", "idx"), &TabContainer::get_tab_control);
  678. ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &TabContainer::set_tab_align);
  679. ClassDB::bind_method(D_METHOD("get_tab_align"), &TabContainer::get_tab_align);
  680. ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
  681. ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
  682. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
  683. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
  684. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon);
  685. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon);
  686. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled);
  687. ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &TabContainer::get_tab_disabled);
  688. ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
  689. ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
  690. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabContainer::set_drag_to_rearrange_enabled);
  691. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabContainer::get_drag_to_rearrange_enabled);
  692. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabContainer::set_tabs_rearrange_group);
  693. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabContainer::get_tabs_rearrange_group);
  694. ClassDB::bind_method(D_METHOD("_child_renamed_callback"), &TabContainer::_child_renamed_callback);
  695. ClassDB::bind_method(D_METHOD("_on_theme_changed"), &TabContainer::_on_theme_changed);
  696. ClassDB::bind_method(D_METHOD("_update_current_tab"), &TabContainer::_update_current_tab);
  697. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  698. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  699. ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
  700. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
  701. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
  702. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
  703. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  704. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  705. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  706. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  707. }
  708. TabContainer::TabContainer() {
  709. first_tab_cache = 0;
  710. last_tab_cache = 0;
  711. buttons_visible_cache = false;
  712. tabs_ofs_cache = 0;
  713. current = 0;
  714. previous = 0;
  715. mouse_x_cache = 0;
  716. align = ALIGN_CENTER;
  717. tabs_visible = true;
  718. popup = NULL;
  719. drag_to_rearrange_enabled = false;
  720. tabs_rearrange_group = -1;
  721. }