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. call_deferred("_on_theme_changed"); //wait until all changed theme
  287. } break;
  288. }
  289. }
  290. void TabContainer::_on_theme_changed() {
  291. if (get_tab_count() > 0) {
  292. set_current_tab(get_current_tab());
  293. }
  294. }
  295. int TabContainer::_get_tab_width(int p_index) const {
  296. ERR_FAIL_INDEX_V(p_index, get_tab_count(), 0);
  297. Control *control = Object::cast_to<Control>(_get_tabs()[p_index]);
  298. if (!control || control->is_set_as_toplevel())
  299. return 0;
  300. // Get the width of the text displayed on the tab.
  301. Ref<Font> font = get_font("font");
  302. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(control->get_name());
  303. int width = font->get_string_size(text).width;
  304. // Add space for a tab icon.
  305. if (control->has_meta("_tab_icon")) {
  306. Ref<Texture> icon = control->get_meta("_tab_icon");
  307. if (icon.is_valid()) {
  308. width += icon->get_width();
  309. if (text != "")
  310. width += get_constant("hseparation");
  311. }
  312. }
  313. // Respect a minimum size.
  314. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  315. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  316. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  317. if (get_tab_disabled(p_index)) {
  318. width += tab_disabled->get_minimum_size().width;
  319. } else if (p_index == current) {
  320. width += tab_fg->get_minimum_size().width;
  321. } else {
  322. width += tab_bg->get_minimum_size().width;
  323. }
  324. return width;
  325. }
  326. Vector<Control *> TabContainer::_get_tabs() const {
  327. Vector<Control *> controls;
  328. for (int i = 0; i < get_child_count(); i++) {
  329. Control *control = Object::cast_to<Control>(get_child(i));
  330. if (!control || control->is_toplevel_control())
  331. continue;
  332. controls.push_back(control);
  333. }
  334. return controls;
  335. }
  336. void TabContainer::_child_renamed_callback() {
  337. update();
  338. }
  339. void TabContainer::add_child_notify(Node *p_child) {
  340. Container::add_child_notify(p_child);
  341. Control *c = Object::cast_to<Control>(p_child);
  342. if (!c)
  343. return;
  344. if (c->is_set_as_toplevel())
  345. return;
  346. bool first = false;
  347. if (get_tab_count() != 1)
  348. c->hide();
  349. else {
  350. c->show();
  351. //call_deferred("set_current_tab",0);
  352. first = true;
  353. current = 0;
  354. previous = 0;
  355. }
  356. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  357. if (tabs_visible)
  358. c->set_margin(MARGIN_TOP, _get_top_margin());
  359. Ref<StyleBox> sb = get_stylebox("panel");
  360. c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
  361. c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
  362. c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
  363. c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
  364. update();
  365. p_child->connect("renamed", this, "_child_renamed_callback");
  366. if (first)
  367. emit_signal("tab_changed", current);
  368. }
  369. int TabContainer::get_tab_count() const {
  370. return _get_tabs().size();
  371. }
  372. void TabContainer::set_current_tab(int p_current) {
  373. ERR_FAIL_INDEX(p_current, get_tab_count());
  374. int pending_previous = current;
  375. current = p_current;
  376. Ref<StyleBox> sb = get_stylebox("panel");
  377. Vector<Control *> tabs = _get_tabs();
  378. for (int i = 0; i < tabs.size(); i++) {
  379. Control *c = tabs[i];
  380. if (i == current) {
  381. c->show();
  382. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  383. if (tabs_visible)
  384. c->set_margin(MARGIN_TOP, _get_top_margin());
  385. c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
  386. c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
  387. c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
  388. c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
  389. } else
  390. c->hide();
  391. }
  392. _change_notify("current_tab");
  393. if (pending_previous == current)
  394. emit_signal("tab_selected", current);
  395. else {
  396. previous = pending_previous;
  397. emit_signal("tab_selected", current);
  398. emit_signal("tab_changed", current);
  399. }
  400. update();
  401. }
  402. int TabContainer::get_current_tab() const {
  403. return current;
  404. }
  405. int TabContainer::get_previous_tab() const {
  406. return previous;
  407. }
  408. Control *TabContainer::get_tab_control(int p_idx) const {
  409. Vector<Control *> tabs = _get_tabs();
  410. if (p_idx >= 0 && p_idx < tabs.size())
  411. return tabs[p_idx];
  412. else
  413. return NULL;
  414. }
  415. Control *TabContainer::get_current_tab_control() const {
  416. Vector<Control *> tabs = _get_tabs();
  417. if (current >= 0 && current < tabs.size())
  418. return tabs[current];
  419. else
  420. return NULL;
  421. }
  422. void TabContainer::remove_child_notify(Node *p_child) {
  423. Container::remove_child_notify(p_child);
  424. call_deferred("_update_current_tab");
  425. p_child->disconnect("renamed", this, "_child_renamed_callback");
  426. update();
  427. }
  428. void TabContainer::_update_current_tab() {
  429. int tc = get_tab_count();
  430. if (current >= tc)
  431. current = tc - 1;
  432. if (current < 0)
  433. current = 0;
  434. else
  435. set_current_tab(current);
  436. }
  437. Variant TabContainer::get_drag_data(const Point2 &p_point) {
  438. if (!drag_to_rearrange_enabled)
  439. return Variant();
  440. int tab_over = get_tab_idx_at_point(p_point);
  441. if (tab_over < 0)
  442. return Variant();
  443. HBoxContainer *drag_preview = memnew(HBoxContainer);
  444. Ref<Texture> icon = get_tab_icon(tab_over);
  445. if (!icon.is_null()) {
  446. TextureRect *tf = memnew(TextureRect);
  447. tf->set_texture(icon);
  448. drag_preview->add_child(tf);
  449. }
  450. Label *label = memnew(Label(get_tab_title(tab_over)));
  451. drag_preview->add_child(label);
  452. set_drag_preview(drag_preview);
  453. Dictionary drag_data;
  454. drag_data["type"] = "tabc_element";
  455. drag_data["tabc_element"] = tab_over;
  456. drag_data["from_path"] = get_path();
  457. return drag_data;
  458. }
  459. bool TabContainer::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  460. if (!drag_to_rearrange_enabled)
  461. return false;
  462. Dictionary d = p_data;
  463. if (!d.has("type"))
  464. return false;
  465. if (String(d["type"]) == "tabc_element") {
  466. NodePath from_path = d["from_path"];
  467. NodePath to_path = get_path();
  468. if (from_path == to_path) {
  469. return true;
  470. } else if (get_tabs_rearrange_group() != -1) {
  471. // drag and drop between other TabContainers
  472. Node *from_node = get_node(from_path);
  473. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  474. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  475. return true;
  476. }
  477. }
  478. }
  479. return false;
  480. }
  481. void TabContainer::drop_data(const Point2 &p_point, const Variant &p_data) {
  482. if (!drag_to_rearrange_enabled)
  483. return;
  484. int hover_now = get_tab_idx_at_point(p_point);
  485. Dictionary d = p_data;
  486. if (!d.has("type"))
  487. return;
  488. if (String(d["type"]) == "tabc_element") {
  489. int tab_from_id = d["tabc_element"];
  490. NodePath from_path = d["from_path"];
  491. NodePath to_path = get_path();
  492. if (from_path == to_path) {
  493. if (hover_now < 0)
  494. hover_now = get_tab_count() - 1;
  495. move_child(get_tab_control(tab_from_id), hover_now);
  496. set_current_tab(hover_now);
  497. } else if (get_tabs_rearrange_group() != -1) {
  498. // drag and drop between TabContainers
  499. Node *from_node = get_node(from_path);
  500. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  501. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  502. Control *moving_tabc = from_tabc->get_tab_control(tab_from_id);
  503. from_tabc->remove_child(moving_tabc);
  504. add_child(moving_tabc);
  505. if (hover_now < 0)
  506. hover_now = get_tab_count() - 1;
  507. move_child(moving_tabc, hover_now);
  508. set_current_tab(hover_now);
  509. emit_signal("tab_changed", hover_now);
  510. }
  511. }
  512. }
  513. update();
  514. }
  515. int TabContainer::get_tab_idx_at_point(const Point2 &p_point) const {
  516. if (get_tab_count() == 0)
  517. return -1;
  518. // must be on tabs in the tab header area.
  519. if (p_point.x < tabs_ofs_cache || p_point.y > _get_top_margin())
  520. return -1;
  521. Size2 size = get_size();
  522. int right_ofs = 0;
  523. if (popup) {
  524. Ref<Texture> menu = get_icon("menu");
  525. right_ofs += menu->get_width();
  526. }
  527. if (buttons_visible_cache) {
  528. Ref<Texture> increment = get_icon("increment");
  529. Ref<Texture> decrement = get_icon("decrement");
  530. right_ofs += increment->get_width() + decrement->get_width();
  531. }
  532. if (p_point.x > size.width - right_ofs) {
  533. return -1;
  534. }
  535. // get the tab at the point
  536. Vector<Control *> tabs = _get_tabs();
  537. int px = p_point.x;
  538. px -= tabs_ofs_cache;
  539. for (int i = first_tab_cache; i <= last_tab_cache; i++) {
  540. int tab_width = _get_tab_width(i);
  541. if (px < tab_width) {
  542. return i;
  543. }
  544. px -= tab_width;
  545. }
  546. return -1;
  547. }
  548. void TabContainer::set_tab_align(TabAlign p_align) {
  549. ERR_FAIL_INDEX(p_align, 3);
  550. align = p_align;
  551. update();
  552. _change_notify("tab_align");
  553. }
  554. TabContainer::TabAlign TabContainer::get_tab_align() const {
  555. return align;
  556. }
  557. void TabContainer::set_tabs_visible(bool p_visibe) {
  558. if (p_visibe == tabs_visible)
  559. return;
  560. tabs_visible = p_visibe;
  561. Vector<Control *> tabs = _get_tabs();
  562. for (int i = 0; i < tabs.size(); i++) {
  563. Control *c = tabs[i];
  564. if (p_visibe)
  565. c->set_margin(MARGIN_TOP, _get_top_margin());
  566. else
  567. c->set_margin(MARGIN_TOP, 0);
  568. }
  569. update();
  570. }
  571. bool TabContainer::are_tabs_visible() const {
  572. return tabs_visible;
  573. }
  574. Control *TabContainer::_get_tab(int p_idx) const {
  575. return get_tab_control(p_idx);
  576. }
  577. void TabContainer::set_tab_title(int p_tab, const String &p_title) {
  578. Control *child = _get_tab(p_tab);
  579. ERR_FAIL_COND(!child);
  580. child->set_meta("_tab_name", p_title);
  581. }
  582. String TabContainer::get_tab_title(int p_tab) const {
  583. Control *child = _get_tab(p_tab);
  584. ERR_FAIL_COND_V(!child, "");
  585. if (child->has_meta("_tab_name"))
  586. return child->get_meta("_tab_name");
  587. else
  588. return child->get_name();
  589. }
  590. void TabContainer::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) {
  591. Control *child = _get_tab(p_tab);
  592. ERR_FAIL_COND(!child);
  593. child->set_meta("_tab_icon", p_icon);
  594. }
  595. Ref<Texture> TabContainer::get_tab_icon(int p_tab) const {
  596. Control *child = _get_tab(p_tab);
  597. ERR_FAIL_COND_V(!child, Ref<Texture>());
  598. if (child->has_meta("_tab_icon"))
  599. return child->get_meta("_tab_icon");
  600. else
  601. return Ref<Texture>();
  602. }
  603. void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) {
  604. Control *child = _get_tab(p_tab);
  605. ERR_FAIL_COND(!child);
  606. child->set_meta("_tab_disabled", p_disabled);
  607. update();
  608. }
  609. bool TabContainer::get_tab_disabled(int p_tab) const {
  610. Control *child = _get_tab(p_tab);
  611. ERR_FAIL_COND_V(!child, false);
  612. if (child->has_meta("_tab_disabled"))
  613. return child->get_meta("_tab_disabled");
  614. else
  615. return false;
  616. }
  617. void TabContainer::get_translatable_strings(List<String> *p_strings) const {
  618. Vector<Control *> tabs = _get_tabs();
  619. for (int i = 0; i < tabs.size(); i++) {
  620. Control *c = tabs[i];
  621. if (!c->has_meta("_tab_name"))
  622. continue;
  623. String name = c->get_meta("_tab_name");
  624. if (name != "")
  625. p_strings->push_back(name);
  626. }
  627. }
  628. Size2 TabContainer::get_minimum_size() const {
  629. Size2 ms;
  630. Vector<Control *> tabs = _get_tabs();
  631. for (int i = 0; i < tabs.size(); i++) {
  632. Control *c = tabs[i];
  633. if (!c->is_visible_in_tree())
  634. continue;
  635. Size2 cms = c->get_combined_minimum_size();
  636. ms.x = MAX(ms.x, cms.x);
  637. ms.y = MAX(ms.y, cms.y);
  638. }
  639. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  640. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  641. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  642. Ref<Font> font = get_font("font");
  643. ms.y += MAX(MAX(tab_bg->get_minimum_size().y, tab_fg->get_minimum_size().y), tab_disabled->get_minimum_size().y);
  644. ms.y += font->get_height();
  645. Ref<StyleBox> sb = get_stylebox("panel");
  646. ms += sb->get_minimum_size();
  647. return ms;
  648. }
  649. void TabContainer::set_popup(Node *p_popup) {
  650. ERR_FAIL_NULL(p_popup);
  651. popup = Object::cast_to<Popup>(p_popup);
  652. update();
  653. }
  654. Popup *TabContainer::get_popup() const {
  655. return popup;
  656. }
  657. void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
  658. drag_to_rearrange_enabled = p_enabled;
  659. }
  660. bool TabContainer::get_drag_to_rearrange_enabled() const {
  661. return drag_to_rearrange_enabled;
  662. }
  663. void TabContainer::set_tabs_rearrange_group(int p_group_id) {
  664. tabs_rearrange_group = p_group_id;
  665. }
  666. int TabContainer::get_tabs_rearrange_group() const {
  667. return tabs_rearrange_group;
  668. }
  669. void TabContainer::_bind_methods() {
  670. ClassDB::bind_method(D_METHOD("_gui_input"), &TabContainer::_gui_input);
  671. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabContainer::get_tab_count);
  672. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
  673. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabContainer::get_current_tab);
  674. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabContainer::get_previous_tab);
  675. ClassDB::bind_method(D_METHOD("get_current_tab_control"), &TabContainer::get_current_tab_control);
  676. ClassDB::bind_method(D_METHOD("get_tab_control", "idx"), &TabContainer::get_tab_control);
  677. ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &TabContainer::set_tab_align);
  678. ClassDB::bind_method(D_METHOD("get_tab_align"), &TabContainer::get_tab_align);
  679. ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
  680. ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
  681. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
  682. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
  683. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon);
  684. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon);
  685. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled);
  686. ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &TabContainer::get_tab_disabled);
  687. ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
  688. ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
  689. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabContainer::set_drag_to_rearrange_enabled);
  690. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabContainer::get_drag_to_rearrange_enabled);
  691. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabContainer::set_tabs_rearrange_group);
  692. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabContainer::get_tabs_rearrange_group);
  693. ClassDB::bind_method(D_METHOD("_child_renamed_callback"), &TabContainer::_child_renamed_callback);
  694. ClassDB::bind_method(D_METHOD("_on_theme_changed"), &TabContainer::_on_theme_changed);
  695. ClassDB::bind_method(D_METHOD("_update_current_tab"), &TabContainer::_update_current_tab);
  696. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  697. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  698. ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
  699. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
  700. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
  701. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
  702. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  703. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  704. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  705. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  706. }
  707. TabContainer::TabContainer() {
  708. first_tab_cache = 0;
  709. last_tab_cache = 0;
  710. buttons_visible_cache = false;
  711. tabs_ofs_cache = 0;
  712. current = 0;
  713. previous = 0;
  714. mouse_x_cache = 0;
  715. align = ALIGN_CENTER;
  716. tabs_visible = true;
  717. popup = NULL;
  718. drag_to_rearrange_enabled = false;
  719. tabs_rearrange_group = -1;
  720. }