tab_container.cpp 31 KB

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