tab_container.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*************************************************************************/
  2. /* tab_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "tab_container.h"
  31. #include "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. }
  39. // Respect the minimum tab height.
  40. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  41. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  42. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  43. int tab_height = MAX(MAX(tab_bg->get_minimum_size().height, tab_fg->get_minimum_size().height), tab_disabled->get_minimum_size().height);
  44. // Font height or higher icon wins.
  45. Ref<Font> font = get_font("font");
  46. int content_height = font->get_height();
  47. Vector<Control *> tabs = _get_tabs();
  48. for (int i = 0; i < tabs.size(); i++) {
  49. Control *c = tabs[i];
  50. if (!c->has_meta("_tab_icon")) {
  51. continue;
  52. }
  53. Ref<Texture> tex = c->get_meta("_tab_icon");
  54. if (!tex.is_valid()) {
  55. continue;
  56. }
  57. content_height = MAX(content_height, tex->get_size().height);
  58. }
  59. return tab_height + content_height;
  60. }
  61. void TabContainer::_gui_input(const Ref<InputEvent> &p_event) {
  62. Ref<InputEventMouseButton> mb = p_event;
  63. Popup *popup = get_popup();
  64. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  65. Point2 pos(mb->get_position().x, mb->get_position().y);
  66. Size2 size = get_size();
  67. // Click must be on tabs in the tab header area.
  68. if (pos.x < tabs_ofs_cache || pos.y > _get_top_margin()) {
  69. return;
  70. }
  71. // Handle menu button.
  72. Ref<Texture> menu = get_icon("menu");
  73. if (popup && pos.x > size.width - menu->get_width()) {
  74. emit_signal("pre_popup_pressed");
  75. Vector2 popup_pos = get_global_position();
  76. popup_pos.x += size.width * get_global_transform().get_scale().x - popup->get_size().width * popup->get_global_transform().get_scale().x;
  77. popup_pos.y += menu->get_height() * get_global_transform().get_scale().y;
  78. popup->set_global_position(popup_pos);
  79. popup->popup();
  80. return;
  81. }
  82. // Do not activate tabs when tabs is empty.
  83. if (get_tab_count() == 0) {
  84. return;
  85. }
  86. Vector<Control *> tabs = _get_tabs();
  87. // Handle navigation buttons.
  88. if (buttons_visible_cache) {
  89. int popup_ofs = 0;
  90. if (popup) {
  91. popup_ofs = menu->get_width();
  92. }
  93. Ref<Texture> increment = get_icon("increment");
  94. Ref<Texture> decrement = get_icon("decrement");
  95. if (pos.x > size.width - increment->get_width() - popup_ofs) {
  96. if (last_tab_cache < tabs.size() - 1) {
  97. first_tab_cache += 1;
  98. update();
  99. }
  100. return;
  101. } else if (pos.x > size.width - increment->get_width() - decrement->get_width() - popup_ofs) {
  102. if (first_tab_cache > 0) {
  103. first_tab_cache -= 1;
  104. update();
  105. }
  106. return;
  107. }
  108. }
  109. // Activate the clicked tab.
  110. pos.x -= tabs_ofs_cache;
  111. for (int i = first_tab_cache; i <= last_tab_cache; i++) {
  112. if (get_tab_hidden(i)) {
  113. continue;
  114. }
  115. int tab_width = _get_tab_width(i);
  116. if (pos.x < tab_width) {
  117. if (!get_tab_disabled(i)) {
  118. set_current_tab(i);
  119. }
  120. break;
  121. }
  122. pos.x -= tab_width;
  123. }
  124. }
  125. Ref<InputEventMouseMotion> mm = p_event;
  126. if (mm.is_valid()) {
  127. Point2 pos(mm->get_position().x, mm->get_position().y);
  128. Size2 size = get_size();
  129. // Mouse must be on tabs in the tab header area.
  130. if (pos.x < tabs_ofs_cache || pos.y > _get_top_margin()) {
  131. if (menu_hovered || highlight_arrow > -1) {
  132. menu_hovered = false;
  133. highlight_arrow = -1;
  134. update();
  135. }
  136. return;
  137. }
  138. Ref<Texture> menu = get_icon("menu");
  139. if (popup) {
  140. if (pos.x >= size.width - menu->get_width()) {
  141. if (!menu_hovered) {
  142. menu_hovered = true;
  143. highlight_arrow = -1;
  144. update();
  145. return;
  146. }
  147. } else if (menu_hovered) {
  148. menu_hovered = false;
  149. update();
  150. }
  151. if (menu_hovered) {
  152. return;
  153. }
  154. }
  155. // Do not activate tabs when tabs is empty.
  156. if ((get_tab_count() == 0 || !buttons_visible_cache) && menu_hovered) {
  157. highlight_arrow = -1;
  158. update();
  159. return;
  160. }
  161. int popup_ofs = 0;
  162. if (popup) {
  163. popup_ofs = menu->get_width();
  164. }
  165. Ref<Texture> increment = get_icon("increment");
  166. Ref<Texture> decrement = get_icon("decrement");
  167. if (pos.x >= size.width - increment->get_width() - popup_ofs) {
  168. if (highlight_arrow != 1) {
  169. highlight_arrow = 1;
  170. update();
  171. }
  172. } else if (pos.x >= size.width - increment->get_width() - decrement->get_width() - popup_ofs) {
  173. if (highlight_arrow != 0) {
  174. highlight_arrow = 0;
  175. update();
  176. }
  177. } else if (highlight_arrow > -1) {
  178. highlight_arrow = -1;
  179. update();
  180. }
  181. }
  182. }
  183. void TabContainer::_notification(int p_what) {
  184. switch (p_what) {
  185. case NOTIFICATION_TRANSLATION_CHANGED: {
  186. minimum_size_changed();
  187. update();
  188. } break;
  189. case NOTIFICATION_RESIZED: {
  190. Vector<Control *> tabs = _get_tabs();
  191. int side_margin = get_constant("side_margin");
  192. Ref<Texture> menu = get_icon("menu");
  193. Ref<Texture> increment = get_icon("increment");
  194. Ref<Texture> decrement = get_icon("decrement");
  195. int header_width = get_size().width - side_margin * 2;
  196. // Find the width of the header area.
  197. Popup *popup = get_popup();
  198. if (popup) {
  199. header_width -= menu->get_width();
  200. }
  201. if (buttons_visible_cache) {
  202. header_width -= increment->get_width() + decrement->get_width();
  203. }
  204. if (popup || buttons_visible_cache) {
  205. header_width += side_margin;
  206. }
  207. // Find the width of all tabs after first_tab_cache.
  208. int all_tabs_width = 0;
  209. for (int i = first_tab_cache; i < tabs.size(); i++) {
  210. int tab_width = _get_tab_width(i);
  211. all_tabs_width += tab_width;
  212. }
  213. // Check if tabs before first_tab_cache would fit into the header area.
  214. for (int i = first_tab_cache - 1; i >= 0; i--) {
  215. int tab_width = _get_tab_width(i);
  216. if (all_tabs_width + tab_width > header_width) {
  217. break;
  218. }
  219. all_tabs_width += tab_width;
  220. first_tab_cache--;
  221. }
  222. } break;
  223. case NOTIFICATION_DRAW: {
  224. RID canvas = get_canvas_item();
  225. Size2 size = get_size();
  226. // Draw only the tab area if the header is hidden.
  227. Ref<StyleBox> panel = get_stylebox("panel");
  228. if (!tabs_visible) {
  229. panel->draw(canvas, Rect2(0, 0, size.width, size.height));
  230. return;
  231. }
  232. Vector<Control *> tabs = _get_tabs();
  233. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  234. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  235. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  236. Ref<Texture> increment = get_icon("increment");
  237. Ref<Texture> increment_hl = get_icon("increment_highlight");
  238. Ref<Texture> decrement = get_icon("decrement");
  239. Ref<Texture> decrement_hl = get_icon("decrement_highlight");
  240. Ref<Texture> menu = get_icon("menu");
  241. Ref<Texture> menu_hl = get_icon("menu_highlight");
  242. Ref<Font> font = get_font("font");
  243. Color font_color_fg = get_color("font_color_fg");
  244. Color font_color_bg = get_color("font_color_bg");
  245. Color font_color_disabled = get_color("font_color_disabled");
  246. int side_margin = get_constant("side_margin");
  247. // Find out start and width of the header area.
  248. int header_x = side_margin;
  249. int header_width = size.width - side_margin * 2;
  250. int header_height = _get_top_margin();
  251. Popup *popup = get_popup();
  252. if (popup) {
  253. header_width -= menu->get_width();
  254. }
  255. // Check if all tabs would fit into the header area.
  256. int all_tabs_width = 0;
  257. for (int i = 0; i < tabs.size(); i++) {
  258. if (get_tab_hidden(i)) {
  259. continue;
  260. }
  261. int tab_width = _get_tab_width(i);
  262. all_tabs_width += tab_width;
  263. if (all_tabs_width > header_width) {
  264. // Not all tabs are visible at the same time - reserve space for navigation buttons.
  265. buttons_visible_cache = true;
  266. header_width -= decrement->get_width() + increment->get_width();
  267. break;
  268. } else {
  269. buttons_visible_cache = false;
  270. }
  271. }
  272. // With buttons, a right side margin does not need to be respected.
  273. if (popup || buttons_visible_cache) {
  274. header_width += side_margin;
  275. }
  276. if (!buttons_visible_cache) {
  277. first_tab_cache = 0;
  278. }
  279. // Go through the visible tabs to find the width they occupy.
  280. all_tabs_width = 0;
  281. Vector<int> tab_widths;
  282. for (int i = first_tab_cache; i < tabs.size(); i++) {
  283. if (get_tab_hidden(i)) {
  284. tab_widths.push_back(0);
  285. continue;
  286. }
  287. int tab_width = _get_tab_width(i);
  288. if (all_tabs_width + tab_width > header_width && tab_widths.size() > 0) {
  289. break;
  290. }
  291. all_tabs_width += tab_width;
  292. tab_widths.push_back(tab_width);
  293. }
  294. // Find the offset at which to draw tabs, according to the alignment.
  295. switch (align) {
  296. case ALIGN_LEFT:
  297. tabs_ofs_cache = header_x;
  298. break;
  299. case ALIGN_CENTER:
  300. tabs_ofs_cache = header_x + (header_width / 2) - (all_tabs_width / 2);
  301. break;
  302. case ALIGN_RIGHT:
  303. tabs_ofs_cache = header_x + header_width - all_tabs_width;
  304. break;
  305. }
  306. if (all_tabs_in_front) {
  307. // Draw the tab area.
  308. panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
  309. }
  310. // Draw unselected tabs in back
  311. int x = 0;
  312. int x_current = 0;
  313. int index = 0;
  314. for (int i = 0; i < tab_widths.size(); i++) {
  315. index = i + first_tab_cache;
  316. if (get_tab_hidden(index)) {
  317. continue;
  318. }
  319. int tab_width = tab_widths[i];
  320. if (index == current) {
  321. x_current = x;
  322. } else if (get_tab_disabled(index)) {
  323. _draw_tab(tab_disabled, font_color_disabled, index, tabs_ofs_cache + x);
  324. } else {
  325. _draw_tab(tab_bg, font_color_bg, index, tabs_ofs_cache + x);
  326. }
  327. x += tab_width;
  328. last_tab_cache = index;
  329. }
  330. if (!all_tabs_in_front) {
  331. // Draw the tab area.
  332. panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
  333. }
  334. // Draw selected tab in front. Only draw selected tab when it's in visible range.
  335. if (tabs.size() > 0 && current - first_tab_cache < tab_widths.size() && current >= first_tab_cache) {
  336. Ref<StyleBox> current_style_box = get_tab_disabled(current) ? tab_disabled : tab_fg;
  337. _draw_tab(current_style_box, font_color_fg, current, tabs_ofs_cache + x_current);
  338. }
  339. // Draw the popup menu.
  340. x = get_size().width;
  341. if (popup) {
  342. x -= menu->get_width();
  343. if (menu_hovered) {
  344. menu_hl->draw(get_canvas_item(), Size2(x, (header_height - menu_hl->get_height()) / 2));
  345. } else {
  346. menu->draw(get_canvas_item(), Size2(x, (header_height - menu->get_height()) / 2));
  347. }
  348. }
  349. // Draw the navigation buttons.
  350. if (buttons_visible_cache) {
  351. x -= increment->get_width();
  352. if (last_tab_cache < tabs.size() - 1) {
  353. draw_texture(highlight_arrow == 1 ? increment_hl : increment, Point2(x, (header_height - increment->get_height()) / 2));
  354. } else {
  355. draw_texture(increment, Point2(x, (header_height - increment->get_height()) / 2), Color(1, 1, 1, 0.5));
  356. }
  357. x -= decrement->get_width();
  358. if (first_tab_cache > 0) {
  359. draw_texture(highlight_arrow == 0 ? decrement_hl : decrement, Point2(x, (header_height - decrement->get_height()) / 2));
  360. } else {
  361. draw_texture(decrement, Point2(x, (header_height - decrement->get_height()) / 2), Color(1, 1, 1, 0.5));
  362. }
  363. }
  364. } break;
  365. case NOTIFICATION_THEME_CHANGED: {
  366. minimum_size_changed();
  367. call_deferred("_on_theme_changed"); // Wait until all changed theme.
  368. } break;
  369. }
  370. }
  371. void TabContainer::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x) {
  372. Control *control = get_tab_control(p_index);
  373. RID canvas = get_canvas_item();
  374. Ref<Font> font = get_font("font");
  375. int icon_text_distance = get_constant("hseparation");
  376. int tab_width = _get_tab_width(p_index);
  377. int header_height = _get_top_margin();
  378. // Draw the tab background.
  379. Rect2 tab_rect(p_x, 0, tab_width, header_height);
  380. p_tab_style->draw(canvas, tab_rect);
  381. // Draw the tab contents.
  382. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
  383. int x_content = tab_rect.position.x + p_tab_style->get_margin(MARGIN_LEFT);
  384. int top_margin = p_tab_style->get_margin(MARGIN_TOP);
  385. int y_center = top_margin + (tab_rect.size.y - p_tab_style->get_minimum_size().y) / 2;
  386. // Draw the tab icon.
  387. if (control->has_meta("_tab_icon")) {
  388. Ref<Texture> icon = control->get_meta("_tab_icon");
  389. if (icon.is_valid()) {
  390. int y = y_center - (icon->get_height() / 2);
  391. icon->draw(canvas, Point2i(x_content, y));
  392. if (text != "") {
  393. x_content += icon->get_width() + icon_text_distance;
  394. }
  395. }
  396. }
  397. // Draw the tab text.
  398. Point2i text_pos(x_content, y_center - (font->get_height() / 2) + font->get_ascent());
  399. font->draw(canvas, text_pos, text, p_font_color);
  400. }
  401. void TabContainer::_on_theme_changed() {
  402. if (get_tab_count() > 0) {
  403. _repaint();
  404. update();
  405. }
  406. }
  407. void TabContainer::_repaint() {
  408. Ref<StyleBox> sb = get_stylebox("panel");
  409. Vector<Control *> tabs = _get_tabs();
  410. for (int i = 0; i < tabs.size(); i++) {
  411. Control *c = tabs[i];
  412. if (i == current) {
  413. c->show();
  414. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  415. if (tabs_visible) {
  416. c->set_margin(MARGIN_TOP, _get_top_margin());
  417. }
  418. c->set_margin(MARGIN_TOP, c->get_margin(MARGIN_TOP) + sb->get_margin(MARGIN_TOP));
  419. c->set_margin(MARGIN_LEFT, c->get_margin(MARGIN_LEFT) + sb->get_margin(MARGIN_LEFT));
  420. c->set_margin(MARGIN_RIGHT, c->get_margin(MARGIN_RIGHT) - sb->get_margin(MARGIN_RIGHT));
  421. c->set_margin(MARGIN_BOTTOM, c->get_margin(MARGIN_BOTTOM) - sb->get_margin(MARGIN_BOTTOM));
  422. } else {
  423. c->hide();
  424. }
  425. }
  426. }
  427. void TabContainer::_on_mouse_exited() {
  428. if (menu_hovered || highlight_arrow > -1) {
  429. menu_hovered = false;
  430. highlight_arrow = -1;
  431. update();
  432. }
  433. }
  434. int TabContainer::_get_tab_width(int p_index) const {
  435. ERR_FAIL_INDEX_V(p_index, get_tab_count(), 0);
  436. Control *control = get_tab_control(p_index);
  437. if (!control || get_tab_hidden(p_index)) {
  438. return 0;
  439. }
  440. // Get the width of the text displayed on the tab.
  441. Ref<Font> font = get_font("font");
  442. String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
  443. int width = font->get_string_size(text).width;
  444. // Add space for a tab icon.
  445. if (control->has_meta("_tab_icon")) {
  446. Ref<Texture> icon = control->get_meta("_tab_icon");
  447. if (icon.is_valid()) {
  448. width += icon->get_width();
  449. if (text != "") {
  450. width += get_constant("hseparation");
  451. }
  452. }
  453. }
  454. // Respect a minimum size.
  455. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  456. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  457. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  458. if (get_tab_disabled(p_index)) {
  459. width += tab_disabled->get_minimum_size().width;
  460. } else if (p_index == current) {
  461. width += tab_fg->get_minimum_size().width;
  462. } else {
  463. width += tab_bg->get_minimum_size().width;
  464. }
  465. return width;
  466. }
  467. Vector<Control *> TabContainer::_get_tabs() const {
  468. Vector<Control *> controls;
  469. for (int i = 0; i < get_child_count(); i++) {
  470. Control *control = Object::cast_to<Control>(get_child(i));
  471. if (!control || control->is_set_as_toplevel()) {
  472. continue;
  473. }
  474. controls.push_back(control);
  475. }
  476. return controls;
  477. }
  478. void TabContainer::_child_renamed_callback() {
  479. update();
  480. }
  481. void TabContainer::add_child_notify(Node *p_child) {
  482. Container::add_child_notify(p_child);
  483. Control *c = Object::cast_to<Control>(p_child);
  484. if (!c || c->is_set_as_toplevel()) {
  485. return;
  486. }
  487. call_deferred("_repaint");
  488. update();
  489. bool first = (get_tab_count() == 1);
  490. if (first) {
  491. current = 0;
  492. previous = 0;
  493. }
  494. p_child->connect("renamed", this, "_child_renamed_callback");
  495. if (first) {
  496. emit_signal("tab_changed", current);
  497. }
  498. }
  499. void TabContainer::move_child_notify(Node *p_child) {
  500. Container::move_child_notify(p_child);
  501. Control *c = Object::cast_to<Control>(p_child);
  502. if (!c || c->is_set_as_toplevel()) {
  503. return;
  504. }
  505. _update_current_tab();
  506. update();
  507. }
  508. int TabContainer::get_tab_count() const {
  509. return _get_tabs().size();
  510. }
  511. void TabContainer::set_current_tab(int p_current) {
  512. ERR_FAIL_INDEX(p_current, get_tab_count());
  513. int pending_previous = current;
  514. current = p_current;
  515. _repaint();
  516. _change_notify("current_tab");
  517. if (pending_previous == current) {
  518. emit_signal("tab_selected", current);
  519. } else {
  520. previous = pending_previous;
  521. emit_signal("tab_selected", current);
  522. emit_signal("tab_changed", current);
  523. }
  524. update();
  525. }
  526. int TabContainer::get_current_tab() const {
  527. return current;
  528. }
  529. int TabContainer::get_previous_tab() const {
  530. return previous;
  531. }
  532. Control *TabContainer::get_tab_control(int p_idx) const {
  533. Vector<Control *> tabs = _get_tabs();
  534. if (p_idx >= 0 && p_idx < tabs.size()) {
  535. return tabs[p_idx];
  536. } else {
  537. return nullptr;
  538. }
  539. }
  540. Control *TabContainer::get_current_tab_control() const {
  541. return get_tab_control(current);
  542. }
  543. void TabContainer::remove_child_notify(Node *p_child) {
  544. Container::remove_child_notify(p_child);
  545. Control *c = Object::cast_to<Control>(p_child);
  546. if (!c || c->is_set_as_toplevel()) {
  547. return;
  548. }
  549. // Defer the call because tab is not yet removed (remove_child_notify is called right before p_child is actually removed).
  550. call_deferred("_update_current_tab");
  551. p_child->disconnect("renamed", this, "_child_renamed_callback");
  552. update();
  553. }
  554. void TabContainer::_update_current_tab() {
  555. int tc = get_tab_count();
  556. if (current >= tc) {
  557. current = tc - 1;
  558. }
  559. if (current < 0) {
  560. current = 0;
  561. } else {
  562. set_current_tab(current);
  563. }
  564. }
  565. Variant TabContainer::get_drag_data(const Point2 &p_point) {
  566. if (!drag_to_rearrange_enabled) {
  567. return Variant();
  568. }
  569. int tab_over = get_tab_idx_at_point(p_point);
  570. if (tab_over < 0) {
  571. return Variant();
  572. }
  573. HBoxContainer *drag_preview = memnew(HBoxContainer);
  574. Ref<Texture> icon = get_tab_icon(tab_over);
  575. if (!icon.is_null()) {
  576. TextureRect *tf = memnew(TextureRect);
  577. tf->set_texture(icon);
  578. drag_preview->add_child(tf);
  579. }
  580. Label *label = memnew(Label(get_tab_title(tab_over)));
  581. drag_preview->add_child(label);
  582. set_drag_preview(drag_preview);
  583. Dictionary drag_data;
  584. drag_data["type"] = "tabc_element";
  585. drag_data["tabc_element"] = tab_over;
  586. drag_data["from_path"] = get_path();
  587. return drag_data;
  588. }
  589. bool TabContainer::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  590. if (!drag_to_rearrange_enabled) {
  591. return false;
  592. }
  593. Dictionary d = p_data;
  594. if (!d.has("type")) {
  595. return false;
  596. }
  597. if (String(d["type"]) == "tabc_element") {
  598. NodePath from_path = d["from_path"];
  599. NodePath to_path = get_path();
  600. if (from_path == to_path) {
  601. return true;
  602. } else if (get_tabs_rearrange_group() != -1) {
  603. // drag and drop between other TabContainers
  604. Node *from_node = get_node(from_path);
  605. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  606. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  607. return true;
  608. }
  609. }
  610. }
  611. return false;
  612. }
  613. void TabContainer::drop_data(const Point2 &p_point, const Variant &p_data) {
  614. if (!drag_to_rearrange_enabled) {
  615. return;
  616. }
  617. int hover_now = get_tab_idx_at_point(p_point);
  618. Dictionary d = p_data;
  619. if (!d.has("type")) {
  620. return;
  621. }
  622. if (String(d["type"]) == "tabc_element") {
  623. int tab_from_id = d["tabc_element"];
  624. NodePath from_path = d["from_path"];
  625. NodePath to_path = get_path();
  626. if (from_path == to_path) {
  627. if (hover_now < 0) {
  628. hover_now = get_tab_count() - 1;
  629. }
  630. move_child(get_tab_control(tab_from_id), get_tab_control(hover_now)->get_index());
  631. set_current_tab(hover_now);
  632. } else if (get_tabs_rearrange_group() != -1) {
  633. // drag and drop between TabContainers
  634. Node *from_node = get_node(from_path);
  635. TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
  636. if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  637. Control *moving_tabc = from_tabc->get_tab_control(tab_from_id);
  638. from_tabc->remove_child(moving_tabc);
  639. add_child(moving_tabc);
  640. if (hover_now < 0) {
  641. hover_now = get_tab_count() - 1;
  642. }
  643. move_child(moving_tabc, get_tab_control(hover_now)->get_index());
  644. set_current_tab(hover_now);
  645. emit_signal("tab_changed", hover_now);
  646. }
  647. }
  648. }
  649. update();
  650. }
  651. int TabContainer::get_tab_idx_at_point(const Point2 &p_point) const {
  652. if (get_tab_count() == 0) {
  653. return -1;
  654. }
  655. // must be on tabs in the tab header area.
  656. if (p_point.x < tabs_ofs_cache || p_point.y > _get_top_margin()) {
  657. return -1;
  658. }
  659. Size2 size = get_size();
  660. int right_ofs = 0;
  661. Popup *popup = get_popup();
  662. if (popup) {
  663. Ref<Texture> menu = get_icon("menu");
  664. right_ofs += menu->get_width();
  665. }
  666. if (buttons_visible_cache) {
  667. Ref<Texture> increment = get_icon("increment");
  668. Ref<Texture> decrement = get_icon("decrement");
  669. right_ofs += increment->get_width() + decrement->get_width();
  670. }
  671. if (p_point.x > size.width - right_ofs) {
  672. return -1;
  673. }
  674. // get the tab at the point
  675. Vector<Control *> tabs = _get_tabs();
  676. int px = p_point.x;
  677. px -= tabs_ofs_cache;
  678. for (int i = first_tab_cache; i <= last_tab_cache; i++) {
  679. int tab_width = _get_tab_width(i);
  680. if (px < tab_width) {
  681. return i;
  682. }
  683. px -= tab_width;
  684. }
  685. return -1;
  686. }
  687. void TabContainer::set_tab_align(TabAlign p_align) {
  688. ERR_FAIL_INDEX(p_align, 3);
  689. align = p_align;
  690. update();
  691. _change_notify("tab_align");
  692. }
  693. TabContainer::TabAlign TabContainer::get_tab_align() const {
  694. return align;
  695. }
  696. void TabContainer::set_tabs_visible(bool p_visible) {
  697. if (p_visible == tabs_visible) {
  698. return;
  699. }
  700. tabs_visible = p_visible;
  701. Vector<Control *> tabs = _get_tabs();
  702. for (int i = 0; i < tabs.size(); i++) {
  703. Control *c = tabs[i];
  704. if (p_visible) {
  705. c->set_margin(MARGIN_TOP, _get_top_margin());
  706. } else {
  707. c->set_margin(MARGIN_TOP, 0);
  708. }
  709. }
  710. update();
  711. minimum_size_changed();
  712. }
  713. bool TabContainer::are_tabs_visible() const {
  714. return tabs_visible;
  715. }
  716. void TabContainer::set_all_tabs_in_front(bool p_in_front) {
  717. if (p_in_front == all_tabs_in_front) {
  718. return;
  719. }
  720. all_tabs_in_front = p_in_front;
  721. update();
  722. }
  723. bool TabContainer::is_all_tabs_in_front() const {
  724. return all_tabs_in_front;
  725. }
  726. void TabContainer::set_tab_title(int p_tab, const String &p_title) {
  727. Control *child = get_tab_control(p_tab);
  728. ERR_FAIL_COND(!child);
  729. child->set_meta("_tab_name", p_title);
  730. update();
  731. }
  732. String TabContainer::get_tab_title(int p_tab) const {
  733. Control *child = get_tab_control(p_tab);
  734. ERR_FAIL_COND_V(!child, "");
  735. if (child->has_meta("_tab_name")) {
  736. return child->get_meta("_tab_name");
  737. } else {
  738. return child->get_name();
  739. }
  740. }
  741. void TabContainer::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) {
  742. Control *child = get_tab_control(p_tab);
  743. ERR_FAIL_COND(!child);
  744. child->set_meta("_tab_icon", p_icon);
  745. update();
  746. }
  747. Ref<Texture> TabContainer::get_tab_icon(int p_tab) const {
  748. Control *child = get_tab_control(p_tab);
  749. ERR_FAIL_COND_V(!child, Ref<Texture>());
  750. if (child->has_meta("_tab_icon")) {
  751. return child->get_meta("_tab_icon");
  752. } else {
  753. return Ref<Texture>();
  754. }
  755. }
  756. void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) {
  757. Control *child = get_tab_control(p_tab);
  758. ERR_FAIL_COND(!child);
  759. child->set_meta("_tab_disabled", p_disabled);
  760. update();
  761. }
  762. bool TabContainer::get_tab_disabled(int p_tab) const {
  763. Control *child = get_tab_control(p_tab);
  764. ERR_FAIL_COND_V(!child, false);
  765. if (child->has_meta("_tab_disabled")) {
  766. return child->get_meta("_tab_disabled");
  767. } else {
  768. return false;
  769. }
  770. }
  771. void TabContainer::set_tab_hidden(int p_tab, bool p_hidden) {
  772. Control *child = get_tab_control(p_tab);
  773. ERR_FAIL_COND(!child);
  774. child->set_meta("_tab_hidden", p_hidden);
  775. update();
  776. for (int i = 0; i < get_tab_count(); i++) {
  777. int try_tab = (p_tab + 1 + i) % get_tab_count();
  778. if (get_tab_disabled(try_tab) || get_tab_hidden(try_tab)) {
  779. continue;
  780. }
  781. set_current_tab(try_tab);
  782. return;
  783. }
  784. //assumed no other tab can be switched to, just hide
  785. child->hide();
  786. }
  787. bool TabContainer::get_tab_hidden(int p_tab) const {
  788. Control *child = get_tab_control(p_tab);
  789. ERR_FAIL_COND_V(!child, false);
  790. if (child->has_meta("_tab_hidden")) {
  791. return child->get_meta("_tab_hidden");
  792. } else {
  793. return false;
  794. }
  795. }
  796. void TabContainer::get_translatable_strings(List<String> *p_strings) const {
  797. Vector<Control *> tabs = _get_tabs();
  798. for (int i = 0; i < tabs.size(); i++) {
  799. Control *c = tabs[i];
  800. if (!c->has_meta("_tab_name")) {
  801. continue;
  802. }
  803. String name = c->get_meta("_tab_name");
  804. if (name != "") {
  805. p_strings->push_back(name);
  806. }
  807. }
  808. }
  809. Size2 TabContainer::get_minimum_size() const {
  810. Size2 ms;
  811. Vector<Control *> tabs = _get_tabs();
  812. for (int i = 0; i < tabs.size(); i++) {
  813. Control *c = tabs[i];
  814. if (!c->is_visible_in_tree() && !use_hidden_tabs_for_min_size) {
  815. continue;
  816. }
  817. Size2 cms = c->get_combined_minimum_size();
  818. ms.x = MAX(ms.x, cms.x);
  819. ms.y = MAX(ms.y, cms.y);
  820. }
  821. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  822. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  823. Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
  824. Ref<Font> font = get_font("font");
  825. if (tabs_visible) {
  826. ms.y += MAX(MAX(tab_bg->get_minimum_size().y, tab_fg->get_minimum_size().y), tab_disabled->get_minimum_size().y);
  827. ms.y += font->get_height();
  828. }
  829. Ref<StyleBox> sb = get_stylebox("panel");
  830. ms += sb->get_minimum_size();
  831. return ms;
  832. }
  833. void TabContainer::set_popup(Node *p_popup) {
  834. ERR_FAIL_NULL(p_popup);
  835. Popup *popup = Object::cast_to<Popup>(p_popup);
  836. popup_obj_id = popup ? popup->get_instance_id() : 0;
  837. update();
  838. }
  839. Popup *TabContainer::get_popup() const {
  840. if (popup_obj_id) {
  841. Popup *popup = Object::cast_to<Popup>(ObjectDB::get_instance(popup_obj_id));
  842. if (popup) {
  843. return popup;
  844. } else {
  845. #ifdef DEBUG_ENABLED
  846. ERR_PRINT("Popup assigned to TabContainer is gone!");
  847. #endif
  848. popup_obj_id = 0;
  849. }
  850. }
  851. return nullptr;
  852. }
  853. void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
  854. drag_to_rearrange_enabled = p_enabled;
  855. }
  856. bool TabContainer::get_drag_to_rearrange_enabled() const {
  857. return drag_to_rearrange_enabled;
  858. }
  859. void TabContainer::set_tabs_rearrange_group(int p_group_id) {
  860. tabs_rearrange_group = p_group_id;
  861. }
  862. int TabContainer::get_tabs_rearrange_group() const {
  863. return tabs_rearrange_group;
  864. }
  865. void TabContainer::set_use_hidden_tabs_for_min_size(bool p_use_hidden_tabs) {
  866. use_hidden_tabs_for_min_size = p_use_hidden_tabs;
  867. }
  868. bool TabContainer::get_use_hidden_tabs_for_min_size() const {
  869. return use_hidden_tabs_for_min_size;
  870. }
  871. void TabContainer::_bind_methods() {
  872. ClassDB::bind_method(D_METHOD("_gui_input"), &TabContainer::_gui_input);
  873. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabContainer::get_tab_count);
  874. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
  875. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabContainer::get_current_tab);
  876. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabContainer::get_previous_tab);
  877. ClassDB::bind_method(D_METHOD("get_current_tab_control"), &TabContainer::get_current_tab_control);
  878. ClassDB::bind_method(D_METHOD("get_tab_control", "tab_idx"), &TabContainer::get_tab_control);
  879. ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &TabContainer::set_tab_align);
  880. ClassDB::bind_method(D_METHOD("get_tab_align"), &TabContainer::get_tab_align);
  881. ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
  882. ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
  883. ClassDB::bind_method(D_METHOD("set_all_tabs_in_front", "is_front"), &TabContainer::set_all_tabs_in_front);
  884. ClassDB::bind_method(D_METHOD("is_all_tabs_in_front"), &TabContainer::is_all_tabs_in_front);
  885. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
  886. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
  887. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon);
  888. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon);
  889. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled);
  890. ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &TabContainer::get_tab_disabled);
  891. ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabContainer::set_tab_hidden);
  892. ClassDB::bind_method(D_METHOD("get_tab_hidden", "tab_idx"), &TabContainer::get_tab_hidden);
  893. ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabContainer::get_tab_idx_at_point);
  894. ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
  895. ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
  896. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabContainer::set_drag_to_rearrange_enabled);
  897. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabContainer::get_drag_to_rearrange_enabled);
  898. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabContainer::set_tabs_rearrange_group);
  899. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabContainer::get_tabs_rearrange_group);
  900. ClassDB::bind_method(D_METHOD("set_use_hidden_tabs_for_min_size", "enabled"), &TabContainer::set_use_hidden_tabs_for_min_size);
  901. ClassDB::bind_method(D_METHOD("get_use_hidden_tabs_for_min_size"), &TabContainer::get_use_hidden_tabs_for_min_size);
  902. ClassDB::bind_method(D_METHOD("_child_renamed_callback"), &TabContainer::_child_renamed_callback);
  903. ClassDB::bind_method(D_METHOD("_repaint"), &TabContainer::_repaint);
  904. ClassDB::bind_method(D_METHOD("_on_theme_changed"), &TabContainer::_on_theme_changed);
  905. ClassDB::bind_method(D_METHOD("_on_mouse_exited"), &TabContainer::_on_mouse_exited);
  906. ClassDB::bind_method(D_METHOD("_update_current_tab"), &TabContainer::_update_current_tab);
  907. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  908. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  909. ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
  910. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
  911. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
  912. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
  913. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "all_tabs_in_front"), "set_all_tabs_in_front", "is_all_tabs_in_front");
  914. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  915. 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");
  916. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  917. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  918. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  919. }
  920. TabContainer::TabContainer() {
  921. first_tab_cache = 0;
  922. last_tab_cache = 0;
  923. buttons_visible_cache = false;
  924. menu_hovered = false;
  925. highlight_arrow = -1;
  926. tabs_ofs_cache = 0;
  927. current = 0;
  928. previous = 0;
  929. align = ALIGN_CENTER;
  930. tabs_visible = true;
  931. popup_obj_id = 0;
  932. all_tabs_in_front = false;
  933. drag_to_rearrange_enabled = false;
  934. tabs_rearrange_group = -1;
  935. use_hidden_tabs_for_min_size = false;
  936. connect("mouse_exited", this, "_on_mouse_exited");
  937. }