tab_bar.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*************************************************************************/
  2. /* tab_bar.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_bar.h"
  31. #include "core/object/message_queue.h"
  32. #include "core/string/translation.h"
  33. #include "scene/gui/box_container.h"
  34. #include "scene/gui/label.h"
  35. #include "scene/gui/texture_rect.h"
  36. Size2 TabBar::get_minimum_size() const {
  37. Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
  38. Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
  39. Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
  40. int y_margin = MAX(MAX(tab_unselected->get_minimum_size().height, tab_selected->get_minimum_size().height), tab_disabled->get_minimum_size().height);
  41. Size2 ms(0, 0);
  42. for (int i = 0; i < tabs.size(); i++) {
  43. Ref<Texture2D> tex = tabs[i].icon;
  44. if (tex.is_valid()) {
  45. ms.height = MAX(ms.height, tex->get_size().height);
  46. if (tabs[i].text != "") {
  47. ms.width += get_theme_constant(SNAME("hseparation"));
  48. }
  49. }
  50. ms.width += Math::ceil(tabs[i].text_buf->get_size().x);
  51. ms.height = MAX(ms.height, tabs[i].text_buf->get_size().y + y_margin);
  52. if (tabs[i].disabled) {
  53. ms.width += tab_disabled->get_minimum_size().width;
  54. } else if (current == i) {
  55. ms.width += tab_selected->get_minimum_size().width;
  56. } else {
  57. ms.width += tab_unselected->get_minimum_size().width;
  58. }
  59. if (tabs[i].right_button.is_valid()) {
  60. Ref<Texture2D> rb = tabs[i].right_button;
  61. Size2 bms = rb->get_size();
  62. bms.width += get_theme_constant(SNAME("hseparation"));
  63. ms.width += bms.width;
  64. ms.height = MAX(bms.height + tab_unselected->get_minimum_size().height, ms.height);
  65. }
  66. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current)) {
  67. Ref<Texture2D> cb = get_theme_icon(SNAME("close"));
  68. Size2 bms = cb->get_size();
  69. bms.width += get_theme_constant(SNAME("hseparation"));
  70. ms.width += bms.width;
  71. ms.height = MAX(bms.height + tab_unselected->get_minimum_size().height, ms.height);
  72. }
  73. }
  74. if (clip_tabs) {
  75. ms.width = 0;
  76. }
  77. return ms;
  78. }
  79. void TabBar::gui_input(const Ref<InputEvent> &p_event) {
  80. ERR_FAIL_COND(p_event.is_null());
  81. Ref<InputEventMouseMotion> mm = p_event;
  82. if (mm.is_valid()) {
  83. Point2 pos = mm->get_position();
  84. if (buttons_visible) {
  85. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  86. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  87. if (is_layout_rtl()) {
  88. if (pos.x < decr->get_width()) {
  89. if (highlight_arrow != 1) {
  90. highlight_arrow = 1;
  91. update();
  92. }
  93. } else if (pos.x < incr->get_width() + decr->get_width()) {
  94. if (highlight_arrow != 0) {
  95. highlight_arrow = 0;
  96. update();
  97. }
  98. } else if (highlight_arrow != -1) {
  99. highlight_arrow = -1;
  100. update();
  101. }
  102. } else {
  103. int limit_minus_buttons = get_size().width - incr->get_width() - decr->get_width();
  104. if (pos.x > limit_minus_buttons + decr->get_width()) {
  105. if (highlight_arrow != 1) {
  106. highlight_arrow = 1;
  107. update();
  108. }
  109. } else if (pos.x > limit_minus_buttons) {
  110. if (highlight_arrow != 0) {
  111. highlight_arrow = 0;
  112. update();
  113. }
  114. } else if (highlight_arrow != -1) {
  115. highlight_arrow = -1;
  116. update();
  117. }
  118. }
  119. }
  120. _update_hover();
  121. return;
  122. }
  123. Ref<InputEventMouseButton> mb = p_event;
  124. if (mb.is_valid()) {
  125. if (mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_WHEEL_UP && !mb->is_command_pressed()) {
  126. if (scrolling_enabled && buttons_visible) {
  127. if (offset > 0) {
  128. offset--;
  129. update();
  130. }
  131. }
  132. }
  133. if (mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_WHEEL_DOWN && !mb->is_command_pressed()) {
  134. if (scrolling_enabled && buttons_visible) {
  135. if (missing_right) {
  136. offset++;
  137. _ensure_no_over_offset(); // Avoid overreaching when scrolling fast.
  138. update();
  139. }
  140. }
  141. }
  142. if (rb_pressing && !mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  143. if (rb_hover != -1) {
  144. // pressed
  145. emit_signal(SNAME("tab_rmb_clicked"), rb_hover);
  146. }
  147. rb_pressing = false;
  148. update();
  149. }
  150. if (cb_pressing && !mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  151. if (cb_hover != -1) {
  152. // pressed
  153. emit_signal(SNAME("tab_close_pressed"), cb_hover);
  154. }
  155. cb_pressing = false;
  156. update();
  157. }
  158. if (mb->is_pressed() && (mb->get_button_index() == MOUSE_BUTTON_LEFT || (select_with_rmb && mb->get_button_index() == MOUSE_BUTTON_RIGHT))) {
  159. // clicks
  160. Point2 pos = mb->get_position();
  161. if (buttons_visible) {
  162. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  163. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  164. if (is_layout_rtl()) {
  165. if (pos.x < decr->get_width()) {
  166. if (missing_right) {
  167. offset++;
  168. update();
  169. }
  170. return;
  171. } else if (pos.x < incr->get_width() + decr->get_width()) {
  172. if (offset > 0) {
  173. offset--;
  174. update();
  175. }
  176. return;
  177. }
  178. } else {
  179. int limit = get_size().width - incr->get_width() - decr->get_width();
  180. if (pos.x > limit + decr->get_width()) {
  181. if (missing_right) {
  182. offset++;
  183. update();
  184. }
  185. return;
  186. } else if (pos.x > limit) {
  187. if (offset > 0) {
  188. offset--;
  189. update();
  190. }
  191. return;
  192. }
  193. }
  194. }
  195. if (tabs.is_empty()) {
  196. // Return early if there are no actual tabs to handle input for.
  197. return;
  198. }
  199. int found = -1;
  200. for (int i = offset; i <= max_drawn_tab; i++) {
  201. if (tabs[i].rb_rect.has_point(pos)) {
  202. rb_pressing = true;
  203. update();
  204. return;
  205. }
  206. if (tabs[i].cb_rect.has_point(pos)) {
  207. cb_pressing = true;
  208. update();
  209. return;
  210. }
  211. if (pos.x >= get_tab_rect(i).position.x && pos.x < get_tab_rect(i).position.x + tabs[i].size_cache) {
  212. if (!tabs[i].disabled) {
  213. found = i;
  214. }
  215. break;
  216. }
  217. }
  218. if (found != -1) {
  219. set_current_tab(found);
  220. emit_signal(SNAME("tab_clicked"), found);
  221. }
  222. }
  223. }
  224. }
  225. void TabBar::_shape(int p_tab) {
  226. Ref<Font> font = get_theme_font(SNAME("font"));
  227. int font_size = get_theme_font_size(SNAME("font_size"));
  228. tabs.write[p_tab].xl_text = atr(tabs[p_tab].text);
  229. tabs.write[p_tab].text_buf->clear();
  230. tabs.write[p_tab].text_buf->set_width(-1);
  231. if (tabs[p_tab].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  232. tabs.write[p_tab].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  233. } else {
  234. tabs.write[p_tab].text_buf->set_direction((TextServer::Direction)tabs[p_tab].text_direction);
  235. }
  236. tabs.write[p_tab].text_buf->add_string(tabs.write[p_tab].xl_text, font, font_size, tabs[p_tab].opentype_features, (tabs[p_tab].language != "") ? tabs[p_tab].language : TranslationServer::get_singleton()->get_tool_locale());
  237. }
  238. void TabBar::_notification(int p_what) {
  239. switch (p_what) {
  240. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  241. _update_cache();
  242. update();
  243. } break;
  244. case NOTIFICATION_THEME_CHANGED:
  245. case NOTIFICATION_TRANSLATION_CHANGED: {
  246. for (int i = 0; i < tabs.size(); ++i) {
  247. _shape(i);
  248. }
  249. _update_cache();
  250. minimum_size_changed();
  251. update();
  252. } break;
  253. case NOTIFICATION_RESIZED: {
  254. _update_cache();
  255. _ensure_no_over_offset();
  256. ensure_tab_visible(current);
  257. } break;
  258. case NOTIFICATION_DRAW: {
  259. _update_cache();
  260. RID ci = get_canvas_item();
  261. Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
  262. Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
  263. Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
  264. Color font_selected_color = get_theme_color(SNAME("font_selected_color"));
  265. Color font_unselected_color = get_theme_color(SNAME("font_unselected_color"));
  266. Color font_disabled_color = get_theme_color(SNAME("font_disabled_color"));
  267. Ref<Texture2D> close = get_theme_icon(SNAME("close"));
  268. Color font_outline_color = get_theme_color(SNAME("font_outline_color"));
  269. int outline_size = get_theme_constant(SNAME("outline_size"));
  270. Vector2 size = get_size();
  271. bool rtl = is_layout_rtl();
  272. int h = get_size().height;
  273. int w = 0;
  274. int mw = 0;
  275. for (int i = 0; i < tabs.size(); i++) {
  276. tabs.write[i].ofs_cache = mw;
  277. mw += get_tab_width(i);
  278. }
  279. if (tab_align == ALIGN_CENTER) {
  280. w = (get_size().width - mw) / 2;
  281. } else if (tab_align == ALIGN_RIGHT) {
  282. w = get_size().width - mw;
  283. }
  284. if (w < 0) {
  285. w = 0;
  286. }
  287. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  288. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  289. Ref<Texture2D> incr_hl = get_theme_icon(SNAME("increment_highlight"));
  290. Ref<Texture2D> decr_hl = get_theme_icon(SNAME("decrement_highlight"));
  291. int limit = get_size().width;
  292. int limit_minus_buttons = get_size().width - incr->get_width() - decr->get_width();
  293. missing_right = false;
  294. for (int i = offset; i < tabs.size(); i++) {
  295. tabs.write[i].ofs_cache = w;
  296. int lsize = tabs[i].size_cache;
  297. Ref<StyleBox> sb;
  298. Color col;
  299. if (tabs[i].disabled) {
  300. sb = tab_disabled;
  301. col = font_disabled_color;
  302. } else if (i == current) {
  303. sb = tab_selected;
  304. col = font_selected_color;
  305. } else {
  306. sb = tab_unselected;
  307. col = font_unselected_color;
  308. }
  309. int new_width = w + lsize;
  310. if (new_width > limit || (i < tabs.size() - 1 && new_width > limit_minus_buttons)) { // For the last tab, we accept if the tab covers the buttons.
  311. max_drawn_tab = i - 1;
  312. missing_right = true;
  313. break;
  314. } else {
  315. max_drawn_tab = i;
  316. }
  317. Rect2 sb_rect;
  318. if (rtl) {
  319. sb_rect = Rect2(size.width - w - tabs[i].size_cache, 0, tabs[i].size_cache, h);
  320. } else {
  321. sb_rect = Rect2(w, 0, tabs[i].size_cache, h);
  322. }
  323. sb->draw(ci, sb_rect);
  324. w += sb->get_margin(SIDE_LEFT);
  325. Size2i sb_ms = sb->get_minimum_size();
  326. Ref<Texture2D> icon = tabs[i].icon;
  327. if (icon.is_valid()) {
  328. if (rtl) {
  329. icon->draw(ci, Point2i(size.width - w - icon->get_width(), sb->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - icon->get_height()) / 2));
  330. } else {
  331. icon->draw(ci, Point2i(w, sb->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - icon->get_height()) / 2));
  332. }
  333. if (tabs[i].text != "") {
  334. w += icon->get_width() + get_theme_constant(SNAME("hseparation"));
  335. }
  336. }
  337. if (rtl) {
  338. Vector2 text_pos = Point2i(size.width - w - tabs[i].text_buf->get_size().x, sb->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - tabs[i].text_buf->get_size().y) / 2);
  339. if (outline_size > 0 && font_outline_color.a > 0) {
  340. tabs[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  341. }
  342. tabs[i].text_buf->draw(ci, text_pos, col);
  343. } else {
  344. Vector2 text_pos = Point2i(w, sb->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - tabs[i].text_buf->get_size().y) / 2);
  345. if (outline_size > 0 && font_outline_color.a > 0) {
  346. tabs[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  347. }
  348. tabs[i].text_buf->draw(ci, text_pos, col);
  349. }
  350. w += tabs[i].size_text;
  351. if (tabs[i].right_button.is_valid()) {
  352. Ref<StyleBox> style = get_theme_stylebox(SNAME("close_bg_highlight"));
  353. Ref<Texture2D> rb = tabs[i].right_button;
  354. w += get_theme_constant(SNAME("hseparation"));
  355. Rect2 rb_rect;
  356. rb_rect.size = style->get_minimum_size() + rb->get_size();
  357. if (rtl) {
  358. rb_rect.position.x = size.width - w - rb_rect.size.x;
  359. } else {
  360. rb_rect.position.x = w;
  361. }
  362. rb_rect.position.y = sb->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (rb_rect.size.y)) / 2;
  363. if (rb_hover == i) {
  364. if (rb_pressing) {
  365. get_theme_stylebox(SNAME("button_pressed"))->draw(ci, rb_rect);
  366. } else {
  367. style->draw(ci, rb_rect);
  368. }
  369. }
  370. if (rtl) {
  371. rb->draw(ci, Point2i(size.width - w - rb_rect.size.x + style->get_margin(SIDE_LEFT), rb_rect.position.y + style->get_margin(SIDE_TOP)));
  372. } else {
  373. rb->draw(ci, Point2i(w + style->get_margin(SIDE_LEFT), rb_rect.position.y + style->get_margin(SIDE_TOP)));
  374. }
  375. w += rb->get_width();
  376. tabs.write[i].rb_rect = rb_rect;
  377. }
  378. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current)) {
  379. Ref<StyleBox> style = get_theme_stylebox(SNAME("close_bg_highlight"));
  380. Ref<Texture2D> cb = close;
  381. w += get_theme_constant(SNAME("hseparation"));
  382. Rect2 cb_rect;
  383. cb_rect.size = style->get_minimum_size() + cb->get_size();
  384. if (rtl) {
  385. cb_rect.position.x = size.width - w - cb_rect.size.x;
  386. } else {
  387. cb_rect.position.x = w;
  388. }
  389. cb_rect.position.y = sb->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (cb_rect.size.y)) / 2;
  390. if (!tabs[i].disabled && cb_hover == i) {
  391. if (cb_pressing) {
  392. get_theme_stylebox(SNAME("close_bg_pressed"))->draw(ci, cb_rect);
  393. } else {
  394. style->draw(ci, cb_rect);
  395. }
  396. }
  397. if (rtl) {
  398. cb->draw(ci, Point2i(size.width - w - cb_rect.size.x + style->get_margin(SIDE_LEFT), cb_rect.position.y + style->get_margin(SIDE_TOP)));
  399. } else {
  400. cb->draw(ci, Point2i(w + style->get_margin(SIDE_LEFT), cb_rect.position.y + style->get_margin(SIDE_TOP)));
  401. }
  402. w += cb->get_width();
  403. tabs.write[i].cb_rect = cb_rect;
  404. }
  405. w += sb->get_margin(SIDE_RIGHT);
  406. }
  407. if (offset > 0 || missing_right) {
  408. int vofs = (get_size().height - incr->get_size().height) / 2;
  409. if (rtl) {
  410. if (missing_right) {
  411. draw_texture(highlight_arrow == 1 ? decr_hl : decr, Point2(0, vofs));
  412. } else {
  413. draw_texture(decr, Point2(0, vofs), Color(1, 1, 1, 0.5));
  414. }
  415. if (offset > 0) {
  416. draw_texture(highlight_arrow == 0 ? incr_hl : incr, Point2(incr->get_size().width, vofs));
  417. } else {
  418. draw_texture(incr, Point2(incr->get_size().width, vofs), Color(1, 1, 1, 0.5));
  419. }
  420. } else {
  421. if (offset > 0) {
  422. draw_texture(highlight_arrow == 0 ? decr_hl : decr, Point2(limit_minus_buttons, vofs));
  423. } else {
  424. draw_texture(decr, Point2(limit_minus_buttons, vofs), Color(1, 1, 1, 0.5));
  425. }
  426. if (missing_right) {
  427. draw_texture(highlight_arrow == 1 ? incr_hl : incr, Point2(limit_minus_buttons + decr->get_size().width, vofs));
  428. } else {
  429. draw_texture(incr, Point2(limit_minus_buttons + decr->get_size().width, vofs), Color(1, 1, 1, 0.5));
  430. }
  431. }
  432. buttons_visible = true;
  433. } else {
  434. buttons_visible = false;
  435. }
  436. } break;
  437. }
  438. }
  439. int TabBar::get_tab_count() const {
  440. return tabs.size();
  441. }
  442. void TabBar::set_current_tab(int p_current) {
  443. if (current == p_current) {
  444. return;
  445. }
  446. ERR_FAIL_INDEX(p_current, get_tab_count());
  447. previous = current;
  448. current = p_current;
  449. _update_cache();
  450. update();
  451. emit_signal(SNAME("tab_changed"), p_current);
  452. }
  453. int TabBar::get_current_tab() const {
  454. return current;
  455. }
  456. int TabBar::get_previous_tab() const {
  457. return previous;
  458. }
  459. int TabBar::get_hovered_tab() const {
  460. return hover;
  461. }
  462. int TabBar::get_tab_offset() const {
  463. return offset;
  464. }
  465. bool TabBar::get_offset_buttons_visible() const {
  466. return buttons_visible;
  467. }
  468. void TabBar::set_tab_title(int p_tab, const String &p_title) {
  469. ERR_FAIL_INDEX(p_tab, tabs.size());
  470. tabs.write[p_tab].text = p_title;
  471. _shape(p_tab);
  472. update();
  473. minimum_size_changed();
  474. }
  475. String TabBar::get_tab_title(int p_tab) const {
  476. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  477. return tabs[p_tab].text;
  478. }
  479. void TabBar::set_tab_text_direction(int p_tab, Control::TextDirection p_text_direction) {
  480. ERR_FAIL_INDEX(p_tab, tabs.size());
  481. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  482. if (tabs[p_tab].text_direction != p_text_direction) {
  483. tabs.write[p_tab].text_direction = p_text_direction;
  484. _shape(p_tab);
  485. update();
  486. }
  487. }
  488. Control::TextDirection TabBar::get_tab_text_direction(int p_tab) const {
  489. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Control::TEXT_DIRECTION_INHERITED);
  490. return tabs[p_tab].text_direction;
  491. }
  492. void TabBar::clear_tab_opentype_features(int p_tab) {
  493. ERR_FAIL_INDEX(p_tab, tabs.size());
  494. tabs.write[p_tab].opentype_features.clear();
  495. _shape(p_tab);
  496. update();
  497. }
  498. void TabBar::set_tab_opentype_feature(int p_tab, const String &p_name, int p_value) {
  499. ERR_FAIL_INDEX(p_tab, tabs.size());
  500. int32_t tag = TS->name_to_tag(p_name);
  501. if (!tabs[p_tab].opentype_features.has(tag) || (int)tabs[p_tab].opentype_features[tag] != p_value) {
  502. tabs.write[p_tab].opentype_features[tag] = p_value;
  503. _shape(p_tab);
  504. update();
  505. }
  506. }
  507. int TabBar::get_tab_opentype_feature(int p_tab, const String &p_name) const {
  508. ERR_FAIL_INDEX_V(p_tab, tabs.size(), -1);
  509. int32_t tag = TS->name_to_tag(p_name);
  510. if (!tabs[p_tab].opentype_features.has(tag)) {
  511. return -1;
  512. }
  513. return tabs[p_tab].opentype_features[tag];
  514. }
  515. void TabBar::set_tab_language(int p_tab, const String &p_language) {
  516. ERR_FAIL_INDEX(p_tab, tabs.size());
  517. if (tabs[p_tab].language != p_language) {
  518. tabs.write[p_tab].language = p_language;
  519. _shape(p_tab);
  520. update();
  521. }
  522. }
  523. String TabBar::get_tab_language(int p_tab) const {
  524. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  525. return tabs[p_tab].language;
  526. }
  527. void TabBar::set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  528. ERR_FAIL_INDEX(p_tab, tabs.size());
  529. tabs.write[p_tab].icon = p_icon;
  530. update();
  531. minimum_size_changed();
  532. }
  533. Ref<Texture2D> TabBar::get_tab_icon(int p_tab) const {
  534. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
  535. return tabs[p_tab].icon;
  536. }
  537. void TabBar::set_tab_disabled(int p_tab, bool p_disabled) {
  538. ERR_FAIL_INDEX(p_tab, tabs.size());
  539. tabs.write[p_tab].disabled = p_disabled;
  540. update();
  541. }
  542. bool TabBar::get_tab_disabled(int p_tab) const {
  543. ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
  544. return tabs[p_tab].disabled;
  545. }
  546. void TabBar::set_tab_right_button(int p_tab, const Ref<Texture2D> &p_right_button) {
  547. ERR_FAIL_INDEX(p_tab, tabs.size());
  548. tabs.write[p_tab].right_button = p_right_button;
  549. _update_cache();
  550. update();
  551. minimum_size_changed();
  552. }
  553. Ref<Texture2D> TabBar::get_tab_right_button(int p_tab) const {
  554. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
  555. return tabs[p_tab].right_button;
  556. }
  557. void TabBar::_update_hover() {
  558. if (!is_inside_tree()) {
  559. return;
  560. }
  561. const Point2 &pos = get_local_mouse_position();
  562. // test hovering to display right or close button
  563. int hover_now = -1;
  564. int hover_buttons = -1;
  565. for (int i = offset; i < tabs.size(); i++) {
  566. Rect2 rect = get_tab_rect(i);
  567. if (rect.has_point(pos)) {
  568. hover_now = i;
  569. }
  570. if (tabs[i].rb_rect.has_point(pos)) {
  571. rb_hover = i;
  572. cb_hover = -1;
  573. hover_buttons = i;
  574. break;
  575. } else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) {
  576. cb_hover = i;
  577. rb_hover = -1;
  578. hover_buttons = i;
  579. break;
  580. }
  581. }
  582. if (hover != hover_now) {
  583. hover = hover_now;
  584. emit_signal(SNAME("tab_hovered"), hover);
  585. }
  586. if (hover_buttons == -1) { // no hover
  587. rb_hover = hover_buttons;
  588. cb_hover = hover_buttons;
  589. }
  590. }
  591. void TabBar::_update_cache() {
  592. Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
  593. Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
  594. Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
  595. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  596. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  597. int limit_minus_buttons = get_size().width - incr->get_width() - decr->get_width();
  598. int w = 0;
  599. int mw = 0;
  600. int size_fixed = 0;
  601. int count_resize = 0;
  602. for (int i = 0; i < tabs.size(); i++) {
  603. tabs.write[i].ofs_cache = mw;
  604. tabs.write[i].size_cache = get_tab_width(i);
  605. tabs.write[i].size_text = Math::ceil(tabs[i].text_buf->get_size().x);
  606. tabs.write[i].text_buf->set_width(-1);
  607. mw += tabs[i].size_cache;
  608. if (tabs[i].size_cache <= min_width || i == current) {
  609. size_fixed += tabs[i].size_cache;
  610. } else {
  611. count_resize++;
  612. }
  613. }
  614. int m_width = min_width;
  615. if (count_resize > 0) {
  616. m_width = MAX((limit_minus_buttons - size_fixed) / count_resize, min_width);
  617. }
  618. for (int i = offset; i < tabs.size(); i++) {
  619. Ref<StyleBox> sb;
  620. if (tabs[i].disabled) {
  621. sb = tab_disabled;
  622. } else if (i == current) {
  623. sb = tab_selected;
  624. } else {
  625. sb = tab_unselected;
  626. }
  627. int lsize = tabs[i].size_cache;
  628. int slen = tabs[i].size_text;
  629. if (min_width > 0 && mw > limit_minus_buttons && i != current) {
  630. if (lsize > m_width) {
  631. slen = m_width - (sb->get_margin(SIDE_LEFT) + sb->get_margin(SIDE_RIGHT));
  632. if (tabs[i].icon.is_valid()) {
  633. slen -= tabs[i].icon->get_width();
  634. slen -= get_theme_constant(SNAME("hseparation"));
  635. }
  636. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current)) {
  637. Ref<Texture2D> cb = get_theme_icon(SNAME("close"));
  638. slen -= cb->get_width();
  639. slen -= get_theme_constant(SNAME("hseparation"));
  640. }
  641. slen = MAX(slen, 1);
  642. lsize = m_width;
  643. }
  644. }
  645. tabs.write[i].ofs_cache = w;
  646. tabs.write[i].size_cache = lsize;
  647. tabs.write[i].size_text = slen;
  648. tabs.write[i].text_buf->set_width(slen);
  649. w += lsize;
  650. }
  651. }
  652. void TabBar::_on_mouse_exited() {
  653. rb_hover = -1;
  654. cb_hover = -1;
  655. hover = -1;
  656. highlight_arrow = -1;
  657. update();
  658. }
  659. void TabBar::add_tab(const String &p_str, const Ref<Texture2D> &p_icon) {
  660. Tab t;
  661. t.text = p_str;
  662. t.xl_text = atr(p_str);
  663. t.text_buf.instantiate();
  664. t.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  665. t.text_buf->add_string(t.xl_text, get_theme_font(SNAME("font")), get_theme_font_size(SNAME("font_size")), Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  666. t.icon = p_icon;
  667. t.disabled = false;
  668. t.ofs_cache = 0;
  669. t.size_cache = 0;
  670. tabs.push_back(t);
  671. _update_cache();
  672. call_deferred(SNAME("_update_hover"));
  673. update();
  674. minimum_size_changed();
  675. }
  676. void TabBar::clear_tabs() {
  677. tabs.clear();
  678. current = 0;
  679. previous = 0;
  680. call_deferred(SNAME("_update_hover"));
  681. update();
  682. }
  683. void TabBar::remove_tab(int p_idx) {
  684. ERR_FAIL_INDEX(p_idx, tabs.size());
  685. tabs.remove(p_idx);
  686. if (current >= p_idx) {
  687. current--;
  688. }
  689. _update_cache();
  690. call_deferred(SNAME("_update_hover"));
  691. update();
  692. minimum_size_changed();
  693. if (current < 0) {
  694. current = 0;
  695. previous = 0;
  696. }
  697. if (current >= tabs.size()) {
  698. current = tabs.size() - 1;
  699. }
  700. _ensure_no_over_offset();
  701. }
  702. Variant TabBar::get_drag_data(const Point2 &p_point) {
  703. if (!drag_to_rearrange_enabled) {
  704. return Variant();
  705. }
  706. int tab_over = get_tab_idx_at_point(p_point);
  707. if (tab_over < 0) {
  708. return Variant();
  709. }
  710. HBoxContainer *drag_preview = memnew(HBoxContainer);
  711. if (!tabs[tab_over].icon.is_null()) {
  712. TextureRect *tf = memnew(TextureRect);
  713. tf->set_texture(tabs[tab_over].icon);
  714. drag_preview->add_child(tf);
  715. }
  716. Label *label = memnew(Label(tabs[tab_over].xl_text));
  717. drag_preview->add_child(label);
  718. if (!tabs[tab_over].right_button.is_null()) {
  719. TextureRect *tf = memnew(TextureRect);
  720. tf->set_texture(tabs[tab_over].right_button);
  721. drag_preview->add_child(tf);
  722. }
  723. set_drag_preview(drag_preview);
  724. Dictionary drag_data;
  725. drag_data["type"] = "tab_element";
  726. drag_data["tab_element"] = tab_over;
  727. drag_data["from_path"] = get_path();
  728. return drag_data;
  729. }
  730. bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  731. if (!drag_to_rearrange_enabled) {
  732. return false;
  733. }
  734. Dictionary d = p_data;
  735. if (!d.has("type")) {
  736. return false;
  737. }
  738. if (String(d["type"]) == "tab_element") {
  739. NodePath from_path = d["from_path"];
  740. NodePath to_path = get_path();
  741. if (from_path == to_path) {
  742. return true;
  743. } else if (get_tabs_rearrange_group() != -1) {
  744. // drag and drop between other TabBars
  745. Node *from_node = get_node(from_path);
  746. TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
  747. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  748. return true;
  749. }
  750. }
  751. }
  752. return false;
  753. }
  754. void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) {
  755. if (!drag_to_rearrange_enabled) {
  756. return;
  757. }
  758. int hover_now = get_tab_idx_at_point(p_point);
  759. Dictionary d = p_data;
  760. if (!d.has("type")) {
  761. return;
  762. }
  763. if (String(d["type"]) == "tab_element") {
  764. int tab_from_id = d["tab_element"];
  765. NodePath from_path = d["from_path"];
  766. NodePath to_path = get_path();
  767. if (from_path == to_path) {
  768. if (hover_now < 0) {
  769. hover_now = get_tab_count() - 1;
  770. }
  771. move_tab(tab_from_id, hover_now);
  772. emit_signal(SNAME("active_tab_rearranged"), hover_now);
  773. set_current_tab(hover_now);
  774. } else if (get_tabs_rearrange_group() != -1) {
  775. // drag and drop between Tabs
  776. Node *from_node = get_node(from_path);
  777. TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
  778. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  779. if (tab_from_id >= from_tabs->get_tab_count()) {
  780. return;
  781. }
  782. Tab moving_tab = from_tabs->tabs[tab_from_id];
  783. if (hover_now < 0) {
  784. hover_now = get_tab_count();
  785. }
  786. tabs.insert(hover_now, moving_tab);
  787. from_tabs->remove_tab(tab_from_id);
  788. set_current_tab(hover_now);
  789. emit_signal(SNAME("tab_changed"), hover_now);
  790. _update_cache();
  791. }
  792. }
  793. }
  794. update();
  795. }
  796. int TabBar::get_tab_idx_at_point(const Point2 &p_point) const {
  797. int hover_now = -1;
  798. for (int i = offset; i <= max_drawn_tab; i++) {
  799. Rect2 rect = get_tab_rect(i);
  800. if (rect.has_point(p_point)) {
  801. hover_now = i;
  802. }
  803. }
  804. return hover_now;
  805. }
  806. void TabBar::set_tab_align(TabAlign p_align) {
  807. ERR_FAIL_INDEX(p_align, ALIGN_MAX);
  808. tab_align = p_align;
  809. update();
  810. }
  811. TabBar::TabAlign TabBar::get_tab_align() const {
  812. return tab_align;
  813. }
  814. void TabBar::set_clip_tabs(bool p_clip_tabs) {
  815. if (clip_tabs == p_clip_tabs) {
  816. return;
  817. }
  818. clip_tabs = p_clip_tabs;
  819. update();
  820. minimum_size_changed();
  821. }
  822. bool TabBar::get_clip_tabs() const {
  823. return clip_tabs;
  824. }
  825. void TabBar::move_tab(int from, int to) {
  826. if (from == to) {
  827. return;
  828. }
  829. ERR_FAIL_INDEX(from, tabs.size());
  830. ERR_FAIL_INDEX(to, tabs.size());
  831. Tab tab_from = tabs[from];
  832. tabs.remove(from);
  833. tabs.insert(to, tab_from);
  834. _update_cache();
  835. update();
  836. }
  837. int TabBar::get_tab_width(int p_idx) const {
  838. ERR_FAIL_INDEX_V(p_idx, tabs.size(), 0);
  839. Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
  840. Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
  841. Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
  842. int x = 0;
  843. Ref<Texture2D> tex = tabs[p_idx].icon;
  844. if (tex.is_valid()) {
  845. x += tex->get_width();
  846. if (tabs[p_idx].text != "") {
  847. x += get_theme_constant(SNAME("hseparation"));
  848. }
  849. }
  850. x += Math::ceil(tabs[p_idx].text_buf->get_size().x);
  851. if (tabs[p_idx].disabled) {
  852. x += tab_disabled->get_minimum_size().width;
  853. } else if (current == p_idx) {
  854. x += tab_selected->get_minimum_size().width;
  855. } else {
  856. x += tab_unselected->get_minimum_size().width;
  857. }
  858. if (tabs[p_idx].right_button.is_valid()) {
  859. Ref<Texture2D> rb = tabs[p_idx].right_button;
  860. x += rb->get_width();
  861. x += get_theme_constant(SNAME("hseparation"));
  862. }
  863. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx == current)) {
  864. Ref<Texture2D> cb = get_theme_icon(SNAME("close"));
  865. x += cb->get_width();
  866. x += get_theme_constant(SNAME("hseparation"));
  867. }
  868. return x;
  869. }
  870. void TabBar::_ensure_no_over_offset() {
  871. if (!is_inside_tree()) {
  872. return;
  873. }
  874. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  875. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  876. int limit = get_size().width;
  877. int limit_minus_buttons = get_size().width - incr->get_width() - decr->get_width();
  878. while (offset > 0) {
  879. int total_w = 0;
  880. for (int i = offset - 1; i < tabs.size(); i++) {
  881. total_w += tabs[i].size_cache;
  882. }
  883. if ((buttons_visible && total_w < limit_minus_buttons) || total_w < limit) { // For the last tab, we accept if the tab covers the buttons.
  884. offset--;
  885. update();
  886. } else {
  887. break;
  888. }
  889. }
  890. }
  891. void TabBar::ensure_tab_visible(int p_idx) {
  892. if (!is_inside_tree()) {
  893. return;
  894. }
  895. if (tabs.size() == 0) {
  896. return;
  897. }
  898. ERR_FAIL_INDEX(p_idx, tabs.size());
  899. if (p_idx == offset) {
  900. return;
  901. }
  902. if (p_idx < offset) {
  903. offset = p_idx;
  904. update();
  905. return;
  906. }
  907. int prev_offset = offset;
  908. Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
  909. Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
  910. int limit = get_size().width;
  911. int limit_minus_buttons = get_size().width - incr->get_width() - decr->get_width();
  912. for (int i = offset; i <= p_idx; i++) {
  913. int total_w = tabs[i].ofs_cache + tabs[i].size_cache;
  914. if (total_w > limit || (buttons_visible && total_w > limit_minus_buttons)) {
  915. offset++;
  916. }
  917. }
  918. if (prev_offset != offset) {
  919. update();
  920. }
  921. }
  922. Rect2 TabBar::get_tab_rect(int p_tab) const {
  923. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Rect2());
  924. if (is_layout_rtl()) {
  925. return Rect2(get_size().width - tabs[p_tab].ofs_cache - tabs[p_tab].size_cache, 0, tabs[p_tab].size_cache, get_size().height);
  926. } else {
  927. return Rect2(tabs[p_tab].ofs_cache, 0, tabs[p_tab].size_cache, get_size().height);
  928. }
  929. }
  930. void TabBar::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) {
  931. ERR_FAIL_INDEX(p_policy, CLOSE_BUTTON_MAX);
  932. cb_displaypolicy = p_policy;
  933. update();
  934. }
  935. TabBar::CloseButtonDisplayPolicy TabBar::get_tab_close_display_policy() const {
  936. return cb_displaypolicy;
  937. }
  938. void TabBar::set_min_width(int p_width) {
  939. min_width = p_width;
  940. }
  941. void TabBar::set_scrolling_enabled(bool p_enabled) {
  942. scrolling_enabled = p_enabled;
  943. }
  944. bool TabBar::get_scrolling_enabled() const {
  945. return scrolling_enabled;
  946. }
  947. void TabBar::set_drag_to_rearrange_enabled(bool p_enabled) {
  948. drag_to_rearrange_enabled = p_enabled;
  949. }
  950. bool TabBar::get_drag_to_rearrange_enabled() const {
  951. return drag_to_rearrange_enabled;
  952. }
  953. void TabBar::set_tabs_rearrange_group(int p_group_id) {
  954. tabs_rearrange_group = p_group_id;
  955. }
  956. int TabBar::get_tabs_rearrange_group() const {
  957. return tabs_rearrange_group;
  958. }
  959. void TabBar::set_select_with_rmb(bool p_enabled) {
  960. select_with_rmb = p_enabled;
  961. }
  962. bool TabBar::get_select_with_rmb() const {
  963. return select_with_rmb;
  964. }
  965. void TabBar::_bind_methods() {
  966. ClassDB::bind_method(D_METHOD("_update_hover"), &TabBar::_update_hover);
  967. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabBar::get_tab_count);
  968. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabBar::set_current_tab);
  969. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabBar::get_current_tab);
  970. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabBar::get_previous_tab);
  971. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabBar::set_tab_title);
  972. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabBar::get_tab_title);
  973. ClassDB::bind_method(D_METHOD("set_tab_text_direction", "tab_idx", "direction"), &TabBar::set_tab_text_direction);
  974. ClassDB::bind_method(D_METHOD("get_tab_text_direction", "tab_idx"), &TabBar::get_tab_text_direction);
  975. ClassDB::bind_method(D_METHOD("set_tab_opentype_feature", "tab_idx", "tag", "values"), &TabBar::set_tab_opentype_feature);
  976. ClassDB::bind_method(D_METHOD("get_tab_opentype_feature", "tab_idx", "tag"), &TabBar::get_tab_opentype_feature);
  977. ClassDB::bind_method(D_METHOD("clear_tab_opentype_features", "tab_idx"), &TabBar::clear_tab_opentype_features);
  978. ClassDB::bind_method(D_METHOD("set_tab_language", "tab_idx", "language"), &TabBar::set_tab_language);
  979. ClassDB::bind_method(D_METHOD("get_tab_language", "tab_idx"), &TabBar::get_tab_language);
  980. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabBar::set_tab_icon);
  981. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabBar::get_tab_icon);
  982. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabBar::set_tab_disabled);
  983. ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &TabBar::get_tab_disabled);
  984. ClassDB::bind_method(D_METHOD("remove_tab", "tab_idx"), &TabBar::remove_tab);
  985. ClassDB::bind_method(D_METHOD("add_tab", "title", "icon"), &TabBar::add_tab, DEFVAL(""), DEFVAL(Ref<Texture2D>()));
  986. ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &TabBar::set_tab_align);
  987. ClassDB::bind_method(D_METHOD("get_tab_align"), &TabBar::get_tab_align);
  988. ClassDB::bind_method(D_METHOD("set_clip_tabs", "clip_tabs"), &TabBar::set_clip_tabs);
  989. ClassDB::bind_method(D_METHOD("get_clip_tabs"), &TabBar::get_clip_tabs);
  990. ClassDB::bind_method(D_METHOD("get_tab_offset"), &TabBar::get_tab_offset);
  991. ClassDB::bind_method(D_METHOD("get_offset_buttons_visible"), &TabBar::get_offset_buttons_visible);
  992. ClassDB::bind_method(D_METHOD("ensure_tab_visible", "idx"), &TabBar::ensure_tab_visible);
  993. ClassDB::bind_method(D_METHOD("get_tab_rect", "tab_idx"), &TabBar::get_tab_rect);
  994. ClassDB::bind_method(D_METHOD("move_tab", "from", "to"), &TabBar::move_tab);
  995. ClassDB::bind_method(D_METHOD("set_tab_close_display_policy", "policy"), &TabBar::set_tab_close_display_policy);
  996. ClassDB::bind_method(D_METHOD("get_tab_close_display_policy"), &TabBar::get_tab_close_display_policy);
  997. ClassDB::bind_method(D_METHOD("set_scrolling_enabled", "enabled"), &TabBar::set_scrolling_enabled);
  998. ClassDB::bind_method(D_METHOD("get_scrolling_enabled"), &TabBar::get_scrolling_enabled);
  999. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabBar::set_drag_to_rearrange_enabled);
  1000. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabBar::get_drag_to_rearrange_enabled);
  1001. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabBar::set_tabs_rearrange_group);
  1002. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabBar::get_tabs_rearrange_group);
  1003. ClassDB::bind_method(D_METHOD("set_select_with_rmb", "enabled"), &TabBar::set_select_with_rmb);
  1004. ClassDB::bind_method(D_METHOD("get_select_with_rmb"), &TabBar::get_select_with_rmb);
  1005. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  1006. ADD_SIGNAL(MethodInfo("tab_rmb_clicked", PropertyInfo(Variant::INT, "tab")));
  1007. ADD_SIGNAL(MethodInfo("tab_close_pressed", PropertyInfo(Variant::INT, "tab")));
  1008. ADD_SIGNAL(MethodInfo("tab_hovered", PropertyInfo(Variant::INT, "tab")));
  1009. ADD_SIGNAL(MethodInfo("active_tab_rearranged", PropertyInfo(Variant::INT, "idx_to")));
  1010. ADD_SIGNAL(MethodInfo("tab_clicked", PropertyInfo(Variant::INT, "tab")));
  1011. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
  1012. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
  1013. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_tabs"), "set_clip_tabs", "get_clip_tabs");
  1014. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_close_display_policy", PROPERTY_HINT_ENUM, "Show Never,Show Active Only,Show Always"), "set_tab_close_display_policy", "get_tab_close_display_policy");
  1015. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrolling_enabled"), "set_scrolling_enabled", "get_scrolling_enabled");
  1016. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  1017. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  1018. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  1019. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  1020. BIND_ENUM_CONSTANT(ALIGN_MAX);
  1021. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_NEVER);
  1022. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ACTIVE_ONLY);
  1023. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ALWAYS);
  1024. BIND_ENUM_CONSTANT(CLOSE_BUTTON_MAX);
  1025. }
  1026. TabBar::TabBar() {
  1027. connect("mouse_exited", callable_mp(this, &TabBar::_on_mouse_exited));
  1028. }