tab_bar.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. /**************************************************************************/
  2. /* tab_bar.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/string/translation.h"
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/gui/texture_rect.h"
  35. #include "scene/main/viewport.h"
  36. #include "scene/theme/theme_db.h"
  37. Size2 TabBar::get_minimum_size() const {
  38. Size2 ms;
  39. if (tabs.is_empty()) {
  40. return ms;
  41. }
  42. int y_margin = MAX(MAX(MAX(theme_cache.tab_unselected_style->get_minimum_size().height, theme_cache.tab_hovered_style->get_minimum_size().height), theme_cache.tab_selected_style->get_minimum_size().height), theme_cache.tab_disabled_style->get_minimum_size().height);
  43. for (int i = 0; i < tabs.size(); i++) {
  44. if (tabs[i].hidden) {
  45. continue;
  46. }
  47. int ofs = ms.width;
  48. Ref<StyleBox> style;
  49. if (tabs[i].disabled) {
  50. style = theme_cache.tab_disabled_style;
  51. } else if (current == i) {
  52. style = theme_cache.tab_selected_style;
  53. } else if (hover == i) {
  54. style = theme_cache.tab_hovered_style;
  55. } else {
  56. style = theme_cache.tab_unselected_style;
  57. }
  58. ms.width += style->get_minimum_size().width;
  59. if (tabs[i].icon.is_valid()) {
  60. const Size2 icon_size = _get_tab_icon_size(i);
  61. ms.height = MAX(ms.height, icon_size.height + y_margin);
  62. ms.width += icon_size.width + theme_cache.h_separation;
  63. }
  64. if (!tabs[i].text.is_empty()) {
  65. ms.width += tabs[i].size_text + theme_cache.h_separation;
  66. }
  67. ms.height = MAX(ms.height, tabs[i].text_buf->get_size().y + y_margin);
  68. bool close_visible = cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current);
  69. if (tabs[i].right_button.is_valid()) {
  70. Ref<Texture2D> rb = tabs[i].right_button;
  71. if (close_visible) {
  72. ms.width += theme_cache.button_hl_style->get_minimum_size().width + rb->get_width();
  73. } else {
  74. ms.width += theme_cache.button_hl_style->get_margin(SIDE_LEFT) + rb->get_width() + theme_cache.h_separation;
  75. }
  76. ms.height = MAX(ms.height, rb->get_height() + y_margin);
  77. }
  78. if (close_visible) {
  79. ms.width += theme_cache.button_hl_style->get_margin(SIDE_LEFT) + theme_cache.close_icon->get_width() + theme_cache.h_separation;
  80. ms.height = MAX(ms.height, theme_cache.close_icon->get_height() + y_margin);
  81. }
  82. if (ms.width - ofs > style->get_minimum_size().width) {
  83. ms.width -= theme_cache.h_separation;
  84. }
  85. }
  86. if (clip_tabs) {
  87. ms.width = 0;
  88. }
  89. return ms;
  90. }
  91. void TabBar::gui_input(const Ref<InputEvent> &p_event) {
  92. ERR_FAIL_COND(p_event.is_null());
  93. Ref<InputEventMouseMotion> mm = p_event;
  94. if (mm.is_valid()) {
  95. Point2 pos = mm->get_position();
  96. if (buttons_visible) {
  97. if (is_layout_rtl()) {
  98. if (pos.x < theme_cache.decrement_icon->get_width()) {
  99. if (highlight_arrow != 1) {
  100. highlight_arrow = 1;
  101. queue_redraw();
  102. }
  103. } else if (pos.x < theme_cache.increment_icon->get_width() + theme_cache.decrement_icon->get_width()) {
  104. if (highlight_arrow != 0) {
  105. highlight_arrow = 0;
  106. queue_redraw();
  107. }
  108. } else if (highlight_arrow != -1) {
  109. highlight_arrow = -1;
  110. queue_redraw();
  111. }
  112. } else {
  113. int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  114. if (pos.x > limit_minus_buttons + theme_cache.decrement_icon->get_width()) {
  115. if (highlight_arrow != 1) {
  116. highlight_arrow = 1;
  117. queue_redraw();
  118. }
  119. } else if (pos.x > limit_minus_buttons) {
  120. if (highlight_arrow != 0) {
  121. highlight_arrow = 0;
  122. queue_redraw();
  123. }
  124. } else if (highlight_arrow != -1) {
  125. highlight_arrow = -1;
  126. queue_redraw();
  127. }
  128. }
  129. }
  130. if (get_viewport()->gui_is_dragging() && can_drop_data(pos, get_viewport()->gui_get_drag_data())) {
  131. dragging_valid_tab = true;
  132. queue_redraw();
  133. }
  134. if (!tabs.is_empty()) {
  135. _update_hover();
  136. }
  137. return;
  138. }
  139. Ref<InputEventMouseButton> mb = p_event;
  140. if (mb.is_valid()) {
  141. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP && !mb->is_command_or_control_pressed()) {
  142. if (scrolling_enabled && buttons_visible) {
  143. if (offset > 0) {
  144. offset--;
  145. _update_cache();
  146. queue_redraw();
  147. }
  148. }
  149. }
  150. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN && !mb->is_command_or_control_pressed()) {
  151. if (scrolling_enabled && buttons_visible) {
  152. if (missing_right && offset < tabs.size()) {
  153. offset++;
  154. _update_cache();
  155. queue_redraw();
  156. }
  157. }
  158. }
  159. if (rb_pressing && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  160. if (rb_hover != -1) {
  161. emit_signal(SNAME("tab_button_pressed"), rb_hover);
  162. }
  163. rb_pressing = false;
  164. queue_redraw();
  165. }
  166. if (cb_pressing && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  167. if (cb_hover != -1) {
  168. emit_signal(SNAME("tab_close_pressed"), cb_hover);
  169. }
  170. cb_pressing = false;
  171. queue_redraw();
  172. }
  173. if (mb->is_pressed() && (mb->get_button_index() == MouseButton::LEFT || (select_with_rmb && mb->get_button_index() == MouseButton::RIGHT))) {
  174. Point2 pos = mb->get_position();
  175. if (buttons_visible) {
  176. if (is_layout_rtl()) {
  177. if (pos.x < theme_cache.decrement_icon->get_width()) {
  178. if (missing_right) {
  179. offset++;
  180. _update_cache();
  181. queue_redraw();
  182. }
  183. return;
  184. } else if (pos.x < theme_cache.increment_icon->get_width() + theme_cache.decrement_icon->get_width()) {
  185. if (offset > 0) {
  186. offset--;
  187. _update_cache();
  188. queue_redraw();
  189. }
  190. return;
  191. }
  192. } else {
  193. int limit = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  194. if (pos.x > limit + theme_cache.decrement_icon->get_width()) {
  195. if (missing_right) {
  196. offset++;
  197. _update_cache();
  198. queue_redraw();
  199. }
  200. return;
  201. } else if (pos.x > limit) {
  202. if (offset > 0) {
  203. offset--;
  204. _update_cache();
  205. queue_redraw();
  206. }
  207. return;
  208. }
  209. }
  210. }
  211. if (tabs.is_empty()) {
  212. // Return early if there are no actual tabs to handle input for.
  213. return;
  214. }
  215. int found = -1;
  216. for (int i = offset; i <= max_drawn_tab; i++) {
  217. if (tabs[i].hidden) {
  218. continue;
  219. }
  220. if (tabs[i].rb_rect.has_point(pos)) {
  221. rb_pressing = true;
  222. _update_hover();
  223. queue_redraw();
  224. return;
  225. }
  226. if (tabs[i].cb_rect.has_point(pos) && (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current))) {
  227. cb_pressing = true;
  228. _update_hover();
  229. queue_redraw();
  230. return;
  231. }
  232. if (pos.x >= get_tab_rect(i).position.x && pos.x < get_tab_rect(i).position.x + tabs[i].size_cache) {
  233. if (!tabs[i].disabled) {
  234. found = i;
  235. }
  236. break;
  237. }
  238. }
  239. if (found != -1) {
  240. if (deselect_enabled && get_current_tab() == found) {
  241. set_current_tab(-1);
  242. } else {
  243. set_current_tab(found);
  244. }
  245. if (mb->get_button_index() == MouseButton::RIGHT) {
  246. // Right mouse button clicked.
  247. emit_signal(SNAME("tab_rmb_clicked"), found);
  248. }
  249. emit_signal(SNAME("tab_clicked"), found);
  250. }
  251. }
  252. }
  253. if (p_event->is_pressed()) {
  254. Input *input = Input::get_singleton();
  255. Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
  256. Ref<InputEventJoypadButton> joypadbutton_event = p_event;
  257. bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
  258. if (p_event->is_action("ui_right", true)) {
  259. if (is_joypad_event) {
  260. if (!input->is_action_just_pressed("ui_right", true)) {
  261. return;
  262. }
  263. set_process_internal(true);
  264. }
  265. if (is_layout_rtl() ? select_previous_available() : select_next_available()) {
  266. accept_event();
  267. }
  268. } else if (p_event->is_action("ui_left", true)) {
  269. if (is_joypad_event) {
  270. if (!input->is_action_just_pressed("ui_left", true)) {
  271. return;
  272. }
  273. set_process_internal(true);
  274. }
  275. if (is_layout_rtl() ? select_next_available() : select_previous_available()) {
  276. accept_event();
  277. }
  278. }
  279. }
  280. }
  281. String TabBar::get_tooltip(const Point2 &p_pos) const {
  282. int tab_idx = get_tab_idx_at_point(p_pos);
  283. if (tab_idx < 0) {
  284. return Control::get_tooltip(p_pos);
  285. }
  286. if (tabs[tab_idx].tooltip.is_empty() && tabs[tab_idx].truncated) {
  287. return tabs[tab_idx].text;
  288. }
  289. return tabs[tab_idx].tooltip;
  290. }
  291. void TabBar::_shape(int p_tab) {
  292. tabs.write[p_tab].text_buf->clear();
  293. tabs.write[p_tab].text_buf->set_width(-1);
  294. if (tabs[p_tab].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  295. tabs.write[p_tab].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  296. } else {
  297. tabs.write[p_tab].text_buf->set_direction((TextServer::Direction)tabs[p_tab].text_direction);
  298. }
  299. tabs.write[p_tab].text_buf->add_string(atr(tabs[p_tab].text), theme_cache.font, theme_cache.font_size, tabs[p_tab].language);
  300. }
  301. void TabBar::_notification(int p_what) {
  302. switch (p_what) {
  303. case NOTIFICATION_ENTER_TREE: {
  304. if (scroll_to_selected) {
  305. ensure_tab_visible(current);
  306. }
  307. // Set initialized even if no tabs were set.
  308. initialized = true;
  309. } break;
  310. case NOTIFICATION_INTERNAL_PROCESS: {
  311. Input *input = Input::get_singleton();
  312. if (input->is_action_just_released("ui_left") || input->is_action_just_released("ui_right")) {
  313. gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  314. set_process_internal(false);
  315. return;
  316. }
  317. gamepad_event_delay_ms -= get_process_delta_time();
  318. if (gamepad_event_delay_ms <= 0) {
  319. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  320. if (input->is_action_pressed("ui_right")) {
  321. is_layout_rtl() ? select_previous_available() : select_next_available();
  322. }
  323. if (input->is_action_pressed("ui_left")) {
  324. is_layout_rtl() ? select_next_available() : select_previous_available();
  325. }
  326. }
  327. } break;
  328. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  329. queue_redraw();
  330. } break;
  331. case NOTIFICATION_THEME_CHANGED:
  332. case NOTIFICATION_TRANSLATION_CHANGED: {
  333. for (int i = 0; i < tabs.size(); ++i) {
  334. _shape(i);
  335. }
  336. queue_redraw();
  337. update_minimum_size();
  338. [[fallthrough]];
  339. }
  340. case NOTIFICATION_RESIZED: {
  341. int ofs_old = offset;
  342. int max_old = max_drawn_tab;
  343. _update_cache();
  344. _ensure_no_over_offset();
  345. if (scroll_to_selected && (offset != ofs_old || max_drawn_tab != max_old)) {
  346. ensure_tab_visible(current);
  347. }
  348. } break;
  349. case NOTIFICATION_DRAG_END: {
  350. if (dragging_valid_tab) {
  351. dragging_valid_tab = false;
  352. queue_redraw();
  353. }
  354. } break;
  355. case NOTIFICATION_DRAW: {
  356. bool rtl = is_layout_rtl();
  357. Vector2 size = get_size();
  358. if (tabs.is_empty()) {
  359. // Draw the drop indicator where the first tab would be if there are no tabs.
  360. if (dragging_valid_tab) {
  361. int x = rtl ? size.x : 0;
  362. theme_cache.drop_mark_icon->draw(get_canvas_item(), Point2(x - (theme_cache.drop_mark_icon->get_width() / 2), (size.height - theme_cache.drop_mark_icon->get_height()) / 2), theme_cache.drop_mark_color);
  363. }
  364. return;
  365. }
  366. int limit_minus_buttons = size.width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  367. int ofs = tabs[offset].ofs_cache;
  368. // Draw unselected tabs in the back.
  369. for (int i = offset; i <= max_drawn_tab; i++) {
  370. if (tabs[i].hidden) {
  371. continue;
  372. }
  373. if (i != current) {
  374. Ref<StyleBox> sb;
  375. Color col;
  376. if (tabs[i].disabled) {
  377. sb = theme_cache.tab_disabled_style;
  378. col = theme_cache.font_disabled_color;
  379. } else if (i == hover) {
  380. sb = theme_cache.tab_hovered_style;
  381. col = theme_cache.font_hovered_color;
  382. } else {
  383. sb = theme_cache.tab_unselected_style;
  384. col = theme_cache.font_unselected_color;
  385. }
  386. _draw_tab(sb, col, i, rtl ? size.width - ofs - tabs[i].size_cache : ofs, false);
  387. }
  388. ofs += tabs[i].size_cache;
  389. }
  390. // Draw selected tab in the front, but only if it's visible.
  391. if (current >= offset && current <= max_drawn_tab && !tabs[current].hidden) {
  392. Ref<StyleBox> sb = tabs[current].disabled ? theme_cache.tab_disabled_style : theme_cache.tab_selected_style;
  393. float x = rtl ? size.width - tabs[current].ofs_cache - tabs[current].size_cache : tabs[current].ofs_cache;
  394. _draw_tab(sb, theme_cache.font_selected_color, current, x, has_focus());
  395. }
  396. if (buttons_visible) {
  397. int vofs = (size.height - theme_cache.increment_icon->get_size().height) / 2;
  398. if (rtl) {
  399. if (missing_right) {
  400. draw_texture(highlight_arrow == 1 ? theme_cache.decrement_hl_icon : theme_cache.decrement_icon, Point2(0, vofs));
  401. } else {
  402. draw_texture(theme_cache.decrement_icon, Point2(0, vofs), Color(1, 1, 1, 0.5));
  403. }
  404. if (offset > 0) {
  405. draw_texture(highlight_arrow == 0 ? theme_cache.increment_hl_icon : theme_cache.increment_icon, Point2(theme_cache.increment_icon->get_size().width, vofs));
  406. } else {
  407. draw_texture(theme_cache.increment_icon, Point2(theme_cache.increment_icon->get_size().width, vofs), Color(1, 1, 1, 0.5));
  408. }
  409. } else {
  410. if (offset > 0) {
  411. draw_texture(highlight_arrow == 0 ? theme_cache.decrement_hl_icon : theme_cache.decrement_icon, Point2(limit_minus_buttons, vofs));
  412. } else {
  413. draw_texture(theme_cache.decrement_icon, Point2(limit_minus_buttons, vofs), Color(1, 1, 1, 0.5));
  414. }
  415. if (missing_right) {
  416. draw_texture(highlight_arrow == 1 ? theme_cache.increment_hl_icon : theme_cache.increment_icon, Point2(limit_minus_buttons + theme_cache.decrement_icon->get_size().width, vofs));
  417. } else {
  418. draw_texture(theme_cache.increment_icon, Point2(limit_minus_buttons + theme_cache.decrement_icon->get_size().width, vofs), Color(1, 1, 1, 0.5));
  419. }
  420. }
  421. }
  422. if (dragging_valid_tab) {
  423. int x;
  424. int tab_hover = get_hovered_tab();
  425. if (tab_hover != -1) {
  426. Rect2 tab_rect = get_tab_rect(tab_hover);
  427. x = tab_rect.position.x;
  428. if (get_local_mouse_position().x > x + tab_rect.size.width / 2) {
  429. x += tab_rect.size.width;
  430. }
  431. } else {
  432. if (rtl ^ (get_local_mouse_position().x < get_tab_rect(0).position.x)) {
  433. x = get_tab_rect(0).position.x;
  434. if (rtl) {
  435. x += get_tab_rect(0).size.width;
  436. }
  437. } else {
  438. Rect2 tab_rect = get_tab_rect(get_tab_count() - 1);
  439. x = tab_rect.position.x;
  440. if (!rtl) {
  441. x += tab_rect.size.width;
  442. }
  443. }
  444. }
  445. theme_cache.drop_mark_icon->draw(get_canvas_item(), Point2(x - theme_cache.drop_mark_icon->get_width() / 2, (size.height - theme_cache.drop_mark_icon->get_height()) / 2), theme_cache.drop_mark_color);
  446. }
  447. } break;
  448. }
  449. }
  450. void TabBar::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x, bool p_focus) {
  451. RID ci = get_canvas_item();
  452. bool rtl = is_layout_rtl();
  453. Rect2 sb_rect = Rect2(p_x, 0, tabs[p_index].size_cache, get_size().height);
  454. if (tab_style_v_flip) {
  455. draw_set_transform(Point2(0.0, p_tab_style->get_draw_rect(sb_rect).size.y), 0.0, Size2(1.0, -1.0));
  456. }
  457. p_tab_style->draw(ci, sb_rect);
  458. if (tab_style_v_flip) {
  459. draw_set_transform(Point2(), 0.0, Size2(1.0, 1.0));
  460. }
  461. if (p_focus) {
  462. Ref<StyleBox> focus_style = theme_cache.tab_focus_style;
  463. focus_style->draw(ci, sb_rect);
  464. }
  465. p_x += rtl ? tabs[p_index].size_cache - p_tab_style->get_margin(SIDE_LEFT) : p_tab_style->get_margin(SIDE_LEFT);
  466. Size2i sb_ms = p_tab_style->get_minimum_size();
  467. // Draw the icon.
  468. Ref<Texture2D> icon = tabs[p_index].icon;
  469. if (icon.is_valid()) {
  470. const Size2 icon_size = _get_tab_icon_size(p_index);
  471. const Point2 icon_pos = Point2i(rtl ? p_x - icon_size.width : p_x, p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - icon_size.height) / 2);
  472. icon->draw_rect(ci, Rect2(icon_pos, icon_size));
  473. p_x = rtl ? p_x - icon_size.width - theme_cache.h_separation : p_x + icon_size.width + theme_cache.h_separation;
  474. }
  475. // Draw the text.
  476. if (!tabs[p_index].text.is_empty()) {
  477. Point2i text_pos = Point2i(rtl ? p_x - tabs[p_index].size_text : p_x,
  478. p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - tabs[p_index].text_buf->get_size().y) / 2);
  479. if (theme_cache.outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  480. tabs[p_index].text_buf->draw_outline(ci, text_pos, theme_cache.outline_size, theme_cache.font_outline_color);
  481. }
  482. tabs[p_index].text_buf->draw(ci, text_pos, p_font_color);
  483. p_x = rtl ? p_x - tabs[p_index].size_text - theme_cache.h_separation : p_x + tabs[p_index].size_text + theme_cache.h_separation;
  484. }
  485. // Draw and calculate rect of the right button.
  486. if (tabs[p_index].right_button.is_valid()) {
  487. Ref<StyleBox> style = theme_cache.button_hl_style;
  488. Ref<Texture2D> rb = tabs[p_index].right_button;
  489. Rect2 rb_rect;
  490. rb_rect.size = style->get_minimum_size() + rb->get_size();
  491. rb_rect.position.x = rtl ? p_x - rb_rect.size.width : p_x;
  492. rb_rect.position.y = p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (rb_rect.size.y)) / 2;
  493. tabs.write[p_index].rb_rect = rb_rect;
  494. if (rb_hover == p_index) {
  495. if (rb_pressing) {
  496. theme_cache.button_pressed_style->draw(ci, rb_rect);
  497. } else {
  498. style->draw(ci, rb_rect);
  499. }
  500. }
  501. rb->draw(ci, Point2i(rb_rect.position.x + style->get_margin(SIDE_LEFT), rb_rect.position.y + style->get_margin(SIDE_TOP)));
  502. p_x = rtl ? rb_rect.position.x : rb_rect.position.x + rb_rect.size.width;
  503. } else {
  504. tabs.write[p_index].rb_rect = Rect2();
  505. }
  506. // Draw and calculate rect of the close button.
  507. if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_index == current)) {
  508. Ref<StyleBox> style = theme_cache.button_hl_style;
  509. Ref<Texture2D> cb = theme_cache.close_icon;
  510. Rect2 cb_rect;
  511. cb_rect.size = style->get_minimum_size() + cb->get_size();
  512. cb_rect.position.x = rtl ? p_x - cb_rect.size.width : p_x;
  513. cb_rect.position.y = p_tab_style->get_margin(SIDE_TOP) + ((sb_rect.size.y - sb_ms.y) - (cb_rect.size.y)) / 2;
  514. tabs.write[p_index].cb_rect = cb_rect;
  515. if (!tabs[p_index].disabled && cb_hover == p_index) {
  516. if (cb_pressing) {
  517. theme_cache.button_pressed_style->draw(ci, cb_rect);
  518. } else {
  519. style->draw(ci, cb_rect);
  520. }
  521. }
  522. cb->draw(ci, Point2i(cb_rect.position.x + style->get_margin(SIDE_LEFT), cb_rect.position.y + style->get_margin(SIDE_TOP)));
  523. } else {
  524. tabs.write[p_index].cb_rect = Rect2();
  525. }
  526. }
  527. void TabBar::set_tab_count(int p_count) {
  528. if (p_count == tabs.size()) {
  529. return;
  530. }
  531. ERR_FAIL_COND(p_count < 0);
  532. tabs.resize(p_count);
  533. if (p_count == 0) {
  534. offset = 0;
  535. max_drawn_tab = 0;
  536. current = -1;
  537. previous = -1;
  538. } else {
  539. offset = MIN(offset, p_count - 1);
  540. max_drawn_tab = MIN(max_drawn_tab, p_count - 1);
  541. current = MIN(current, p_count - 1);
  542. // Fix range if unable to deselect.
  543. if (current == -1 && !_can_deselect()) {
  544. current = 0;
  545. }
  546. _update_cache();
  547. _ensure_no_over_offset();
  548. if (scroll_to_selected) {
  549. ensure_tab_visible(current);
  550. }
  551. }
  552. if (!initialized) {
  553. initialized = true;
  554. if (queued_current != CURRENT_TAB_UNINITIALIZED && queued_current != current) {
  555. set_current_tab(queued_current);
  556. }
  557. }
  558. queue_redraw();
  559. update_minimum_size();
  560. notify_property_list_changed();
  561. }
  562. int TabBar::get_tab_count() const {
  563. return tabs.size();
  564. }
  565. void TabBar::set_current_tab(int p_current) {
  566. if (p_current == -1) {
  567. // An index of -1 is only valid if deselecting is enabled or there are no valid tabs.
  568. ERR_FAIL_COND_MSG(!_can_deselect(), "Cannot deselect tabs, deselection is not enabled.");
  569. } else {
  570. if (!initialized && p_current >= get_tab_count()) {
  571. queued_current = p_current;
  572. return;
  573. }
  574. ERR_FAIL_INDEX(p_current, get_tab_count());
  575. }
  576. previous = current;
  577. current = p_current;
  578. if (current == previous) {
  579. emit_signal(SNAME("tab_selected"), current);
  580. return;
  581. }
  582. emit_signal(SNAME("tab_selected"), current);
  583. _update_cache();
  584. if (scroll_to_selected) {
  585. ensure_tab_visible(current);
  586. }
  587. queue_redraw();
  588. emit_signal(SNAME("tab_changed"), p_current);
  589. }
  590. int TabBar::get_current_tab() const {
  591. return current;
  592. }
  593. int TabBar::get_previous_tab() const {
  594. return previous;
  595. }
  596. int TabBar::get_hovered_tab() const {
  597. return hover;
  598. }
  599. bool TabBar::select_previous_available() {
  600. const int offset_end = (get_current_tab() + 1);
  601. for (int i = 1; i < offset_end; i++) {
  602. int target_tab = get_current_tab() - i;
  603. if (target_tab < 0) {
  604. target_tab += get_tab_count();
  605. }
  606. if (!is_tab_disabled(target_tab) && !is_tab_hidden(target_tab)) {
  607. set_current_tab(target_tab);
  608. return true;
  609. }
  610. }
  611. return false;
  612. }
  613. bool TabBar::select_next_available() {
  614. const int offset_end = (get_tab_count() - get_current_tab());
  615. for (int i = 1; i < offset_end; i++) {
  616. int target_tab = (get_current_tab() + i) % get_tab_count();
  617. if (!is_tab_disabled(target_tab) && !is_tab_hidden(target_tab)) {
  618. set_current_tab(target_tab);
  619. return true;
  620. }
  621. }
  622. return false;
  623. }
  624. void TabBar::set_tab_offset(int p_offset) {
  625. ERR_FAIL_INDEX(p_offset, tabs.size());
  626. offset = p_offset;
  627. _update_cache();
  628. queue_redraw();
  629. }
  630. int TabBar::get_tab_offset() const {
  631. return offset;
  632. }
  633. bool TabBar::get_offset_buttons_visible() const {
  634. return buttons_visible;
  635. }
  636. void TabBar::set_tab_title(int p_tab, const String &p_title) {
  637. ERR_FAIL_INDEX(p_tab, tabs.size());
  638. if (tabs[p_tab].text == p_title) {
  639. return;
  640. }
  641. tabs.write[p_tab].text = p_title;
  642. _shape(p_tab);
  643. _update_cache();
  644. _ensure_no_over_offset();
  645. if (scroll_to_selected) {
  646. ensure_tab_visible(current);
  647. }
  648. queue_redraw();
  649. update_minimum_size();
  650. }
  651. String TabBar::get_tab_title(int p_tab) const {
  652. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  653. return tabs[p_tab].text;
  654. }
  655. void TabBar::set_tab_tooltip(int p_tab, const String &p_tooltip) {
  656. ERR_FAIL_INDEX(p_tab, tabs.size());
  657. tabs.write[p_tab].tooltip = p_tooltip;
  658. }
  659. String TabBar::get_tab_tooltip(int p_tab) const {
  660. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  661. return tabs[p_tab].tooltip;
  662. }
  663. void TabBar::set_tab_text_direction(int p_tab, Control::TextDirection p_text_direction) {
  664. ERR_FAIL_INDEX(p_tab, tabs.size());
  665. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  666. if (tabs[p_tab].text_direction != p_text_direction) {
  667. tabs.write[p_tab].text_direction = p_text_direction;
  668. _shape(p_tab);
  669. queue_redraw();
  670. }
  671. }
  672. Control::TextDirection TabBar::get_tab_text_direction(int p_tab) const {
  673. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Control::TEXT_DIRECTION_INHERITED);
  674. return tabs[p_tab].text_direction;
  675. }
  676. void TabBar::set_tab_language(int p_tab, const String &p_language) {
  677. ERR_FAIL_INDEX(p_tab, tabs.size());
  678. if (tabs[p_tab].language != p_language) {
  679. tabs.write[p_tab].language = p_language;
  680. _shape(p_tab);
  681. _update_cache();
  682. _ensure_no_over_offset();
  683. if (scroll_to_selected) {
  684. ensure_tab_visible(current);
  685. }
  686. queue_redraw();
  687. update_minimum_size();
  688. }
  689. }
  690. String TabBar::get_tab_language(int p_tab) const {
  691. ERR_FAIL_INDEX_V(p_tab, tabs.size(), "");
  692. return tabs[p_tab].language;
  693. }
  694. void TabBar::set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  695. ERR_FAIL_INDEX(p_tab, tabs.size());
  696. if (tabs[p_tab].icon == p_icon) {
  697. return;
  698. }
  699. tabs.write[p_tab].icon = p_icon;
  700. _update_cache();
  701. _ensure_no_over_offset();
  702. if (scroll_to_selected) {
  703. ensure_tab_visible(current);
  704. }
  705. queue_redraw();
  706. update_minimum_size();
  707. }
  708. Ref<Texture2D> TabBar::get_tab_icon(int p_tab) const {
  709. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
  710. return tabs[p_tab].icon;
  711. }
  712. void TabBar::set_tab_icon_max_width(int p_tab, int p_width) {
  713. ERR_FAIL_INDEX(p_tab, tabs.size());
  714. if (tabs[p_tab].icon_max_width == p_width) {
  715. return;
  716. }
  717. tabs.write[p_tab].icon_max_width = p_width;
  718. _update_cache();
  719. _ensure_no_over_offset();
  720. if (scroll_to_selected) {
  721. ensure_tab_visible(current);
  722. }
  723. queue_redraw();
  724. update_minimum_size();
  725. }
  726. int TabBar::get_tab_icon_max_width(int p_tab) const {
  727. ERR_FAIL_INDEX_V(p_tab, tabs.size(), 0);
  728. return tabs[p_tab].icon_max_width;
  729. }
  730. void TabBar::set_tab_disabled(int p_tab, bool p_disabled) {
  731. ERR_FAIL_INDEX(p_tab, tabs.size());
  732. if (tabs[p_tab].disabled == p_disabled) {
  733. return;
  734. }
  735. tabs.write[p_tab].disabled = p_disabled;
  736. _update_cache();
  737. _ensure_no_over_offset();
  738. if (scroll_to_selected) {
  739. ensure_tab_visible(current);
  740. }
  741. queue_redraw();
  742. update_minimum_size();
  743. }
  744. bool TabBar::is_tab_disabled(int p_tab) const {
  745. ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
  746. return tabs[p_tab].disabled;
  747. }
  748. void TabBar::set_tab_hidden(int p_tab, bool p_hidden) {
  749. ERR_FAIL_INDEX(p_tab, tabs.size());
  750. if (tabs[p_tab].hidden == p_hidden) {
  751. return;
  752. }
  753. tabs.write[p_tab].hidden = p_hidden;
  754. _update_cache();
  755. _ensure_no_over_offset();
  756. if (scroll_to_selected) {
  757. ensure_tab_visible(current);
  758. }
  759. queue_redraw();
  760. update_minimum_size();
  761. }
  762. bool TabBar::is_tab_hidden(int p_tab) const {
  763. ERR_FAIL_INDEX_V(p_tab, tabs.size(), false);
  764. return tabs[p_tab].hidden;
  765. }
  766. void TabBar::set_tab_metadata(int p_tab, const Variant &p_metadata) {
  767. ERR_FAIL_INDEX(p_tab, tabs.size());
  768. tabs.write[p_tab].metadata = p_metadata;
  769. }
  770. Variant TabBar::get_tab_metadata(int p_tab) const {
  771. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Variant());
  772. return tabs[p_tab].metadata;
  773. }
  774. void TabBar::set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon) {
  775. ERR_FAIL_INDEX(p_tab, tabs.size());
  776. if (tabs[p_tab].right_button == p_icon) {
  777. return;
  778. }
  779. tabs.write[p_tab].right_button = p_icon;
  780. _update_cache();
  781. _ensure_no_over_offset();
  782. if (scroll_to_selected) {
  783. ensure_tab_visible(current);
  784. }
  785. queue_redraw();
  786. update_minimum_size();
  787. }
  788. Ref<Texture2D> TabBar::get_tab_button_icon(int p_tab) const {
  789. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture2D>());
  790. return tabs[p_tab].right_button;
  791. }
  792. void TabBar::_update_hover() {
  793. if (!is_inside_tree()) {
  794. return;
  795. }
  796. ERR_FAIL_COND(tabs.is_empty());
  797. const Point2 &pos = get_local_mouse_position();
  798. // Test hovering to display right or close button.
  799. int hover_now = -1;
  800. int hover_buttons = -1;
  801. for (int i = offset; i <= max_drawn_tab; i++) {
  802. if (tabs[i].hidden) {
  803. continue;
  804. }
  805. Rect2 rect = get_tab_rect(i);
  806. if (rect.has_point(pos)) {
  807. hover_now = i;
  808. }
  809. if (tabs[i].rb_rect.has_point(pos)) {
  810. rb_hover = i;
  811. cb_hover = -1;
  812. hover_buttons = i;
  813. } else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) {
  814. cb_hover = i;
  815. rb_hover = -1;
  816. hover_buttons = i;
  817. }
  818. if (hover_buttons != -1) {
  819. queue_redraw();
  820. break;
  821. }
  822. }
  823. if (hover != hover_now) {
  824. hover = hover_now;
  825. if (hover != -1) {
  826. emit_signal(SNAME("tab_hovered"), hover);
  827. }
  828. _update_cache();
  829. queue_redraw();
  830. }
  831. if (hover_buttons == -1) { // No hover.
  832. int rb_hover_old = rb_hover;
  833. int cb_hover_old = cb_hover;
  834. rb_hover = hover_buttons;
  835. cb_hover = hover_buttons;
  836. if (rb_hover != rb_hover_old || cb_hover != cb_hover_old) {
  837. queue_redraw();
  838. }
  839. }
  840. }
  841. void TabBar::_update_cache(bool p_update_hover) {
  842. if (tabs.is_empty()) {
  843. buttons_visible = false;
  844. return;
  845. }
  846. int limit = get_size().width;
  847. int limit_minus_buttons = limit - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  848. int w = 0;
  849. max_drawn_tab = tabs.size() - 1;
  850. for (int i = 0; i < tabs.size(); i++) {
  851. tabs.write[i].text_buf->set_width(-1);
  852. tabs.write[i].size_text = Math::ceil(tabs[i].text_buf->get_size().x);
  853. tabs.write[i].size_cache = get_tab_width(i);
  854. tabs.write[i].truncated = max_width > 0 && tabs[i].size_cache > max_width;
  855. if (tabs[i].truncated) {
  856. int size_textless = tabs[i].size_cache - tabs[i].size_text;
  857. int mw = MAX(size_textless, max_width);
  858. tabs.write[i].size_text = MAX(mw - size_textless, 1);
  859. tabs.write[i].text_buf->set_width(tabs[i].size_text);
  860. tabs.write[i].size_cache = size_textless + tabs[i].size_text;
  861. }
  862. if (i < offset || i > max_drawn_tab) {
  863. tabs.write[i].ofs_cache = 0;
  864. continue;
  865. }
  866. tabs.write[i].ofs_cache = w;
  867. if (tabs[i].hidden) {
  868. continue;
  869. }
  870. w += tabs[i].size_cache;
  871. // Check if all tabs would fit inside the area.
  872. if (clip_tabs && i > offset && (w > limit || (offset > 0 && w > limit_minus_buttons))) {
  873. tabs.write[i].ofs_cache = 0;
  874. w -= tabs[i].size_cache;
  875. max_drawn_tab = i - 1;
  876. while (w > limit_minus_buttons && max_drawn_tab > offset) {
  877. tabs.write[max_drawn_tab].ofs_cache = 0;
  878. if (!tabs[max_drawn_tab].hidden) {
  879. w -= tabs[max_drawn_tab].size_cache;
  880. }
  881. max_drawn_tab--;
  882. }
  883. }
  884. }
  885. missing_right = max_drawn_tab < tabs.size() - 1;
  886. buttons_visible = offset > 0 || missing_right;
  887. if (tab_alignment == ALIGNMENT_LEFT) {
  888. if (p_update_hover) {
  889. _update_hover();
  890. }
  891. return;
  892. }
  893. if (tab_alignment == ALIGNMENT_CENTER) {
  894. w = ((buttons_visible ? limit_minus_buttons : limit) - w) / 2;
  895. } else if (tab_alignment == ALIGNMENT_RIGHT) {
  896. w = (buttons_visible ? limit_minus_buttons : limit) - w;
  897. }
  898. for (int i = offset; i <= max_drawn_tab; i++) {
  899. tabs.write[i].ofs_cache = w;
  900. if (!tabs[i].hidden) {
  901. w += tabs[i].size_cache;
  902. }
  903. }
  904. if (p_update_hover) {
  905. _update_hover();
  906. }
  907. }
  908. void TabBar::_on_mouse_exited() {
  909. rb_hover = -1;
  910. cb_hover = -1;
  911. hover = -1;
  912. highlight_arrow = -1;
  913. dragging_valid_tab = false;
  914. _update_cache(false);
  915. queue_redraw();
  916. }
  917. void TabBar::add_tab(const String &p_str, const Ref<Texture2D> &p_icon) {
  918. Tab t;
  919. t.text = p_str;
  920. t.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  921. t.icon = p_icon;
  922. tabs.push_back(t);
  923. _shape(tabs.size() - 1);
  924. _update_cache();
  925. if (scroll_to_selected) {
  926. ensure_tab_visible(current);
  927. }
  928. queue_redraw();
  929. update_minimum_size();
  930. if (tabs.size() == 1) {
  931. if (is_inside_tree()) {
  932. set_current_tab(0);
  933. } else {
  934. current = 0;
  935. previous = -1;
  936. }
  937. }
  938. }
  939. void TabBar::clear_tabs() {
  940. if (tabs.is_empty()) {
  941. return;
  942. }
  943. tabs.clear();
  944. offset = 0;
  945. max_drawn_tab = 0;
  946. current = -1;
  947. previous = -1;
  948. queue_redraw();
  949. update_minimum_size();
  950. notify_property_list_changed();
  951. }
  952. void TabBar::remove_tab(int p_idx) {
  953. ERR_FAIL_INDEX(p_idx, tabs.size());
  954. tabs.remove_at(p_idx);
  955. bool is_tab_changing = current == p_idx;
  956. if (current >= p_idx && current > 0) {
  957. current--;
  958. }
  959. if (previous >= p_idx && previous > 0) {
  960. previous--;
  961. }
  962. if (tabs.is_empty()) {
  963. offset = 0;
  964. max_drawn_tab = 0;
  965. current = -1;
  966. previous = -1;
  967. } else {
  968. if (current != -1) {
  969. // Try to change to a valid tab if possible (without firing the `tab_selected` signal).
  970. for (int i = current; i < tabs.size(); i++) {
  971. if (!is_tab_disabled(i) && !is_tab_hidden(i)) {
  972. current = i;
  973. break;
  974. }
  975. }
  976. // If nothing, try backwards.
  977. if (is_tab_disabled(current) || is_tab_hidden(current)) {
  978. for (int i = current - 1; i >= 0; i--) {
  979. if (!is_tab_disabled(i) && !is_tab_hidden(i)) {
  980. current = i;
  981. break;
  982. }
  983. }
  984. }
  985. // If still no valid tab, deselect.
  986. if (is_tab_disabled(current) || is_tab_hidden(current)) {
  987. current = -1;
  988. }
  989. }
  990. offset = MIN(offset, tabs.size() - 1);
  991. max_drawn_tab = MIN(max_drawn_tab, tabs.size() - 1);
  992. _update_cache();
  993. _ensure_no_over_offset();
  994. if (scroll_to_selected) {
  995. ensure_tab_visible(current);
  996. }
  997. }
  998. queue_redraw();
  999. update_minimum_size();
  1000. notify_property_list_changed();
  1001. if (is_tab_changing && is_inside_tree()) {
  1002. emit_signal(SNAME("tab_changed"), current);
  1003. }
  1004. }
  1005. Variant TabBar::get_drag_data(const Point2 &p_point) {
  1006. if (!drag_to_rearrange_enabled) {
  1007. return Control::get_drag_data(p_point); // Allow stuff like TabContainer to override it.
  1008. }
  1009. return _handle_get_drag_data("tab_bar_tab", p_point);
  1010. }
  1011. bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  1012. if (!drag_to_rearrange_enabled) {
  1013. return Control::can_drop_data(p_point, p_data); // Allow stuff like TabContainer to override it.
  1014. }
  1015. return _handle_can_drop_data("tab_bar_tab", p_point, p_data);
  1016. }
  1017. void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) {
  1018. if (!drag_to_rearrange_enabled) {
  1019. Control::drop_data(p_point, p_data); // Allow stuff like TabContainer to override it.
  1020. return;
  1021. }
  1022. _handle_drop_data("tab_bar_tab", p_point, p_data, callable_mp(this, &TabBar::move_tab), callable_mp(this, &TabBar::_move_tab_from));
  1023. }
  1024. Variant TabBar::_handle_get_drag_data(const String &p_type, const Point2 &p_point) {
  1025. int tab_over = get_tab_idx_at_point(p_point);
  1026. if (tab_over < 0) {
  1027. return Variant();
  1028. }
  1029. HBoxContainer *drag_preview = memnew(HBoxContainer);
  1030. if (!tabs[tab_over].icon.is_null()) {
  1031. const Size2 icon_size = _get_tab_icon_size(tab_over);
  1032. TextureRect *tf = memnew(TextureRect);
  1033. tf->set_texture(tabs[tab_over].icon);
  1034. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1035. tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1036. tf->set_custom_minimum_size(icon_size);
  1037. drag_preview->add_child(tf);
  1038. }
  1039. Label *label = memnew(Label(get_tab_title(tab_over)));
  1040. label->set_auto_translate_mode(get_auto_translate_mode()); // Reflect how the title is displayed.
  1041. drag_preview->add_child(label);
  1042. set_drag_preview(drag_preview);
  1043. Dictionary drag_data;
  1044. drag_data["type"] = p_type;
  1045. drag_data["tab_index"] = tab_over;
  1046. drag_data["from_path"] = get_path();
  1047. return drag_data;
  1048. }
  1049. bool TabBar::_handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const {
  1050. Dictionary d = p_data;
  1051. if (!d.has("type")) {
  1052. return false;
  1053. }
  1054. if (String(d["type"]) == p_type) {
  1055. NodePath from_path = d["from_path"];
  1056. NodePath to_path = get_path();
  1057. if (from_path == to_path) {
  1058. return true;
  1059. } else if (get_tabs_rearrange_group() != -1) {
  1060. // Drag and drop between other TabBars.
  1061. Node *from_node = get_node(from_path);
  1062. TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
  1063. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  1064. return true;
  1065. }
  1066. }
  1067. }
  1068. return false;
  1069. }
  1070. void TabBar::_handle_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data, const Callable &p_move_tab_callback, const Callable &p_move_tab_from_other_callback) {
  1071. Dictionary d = p_data;
  1072. if (!d.has("type")) {
  1073. return;
  1074. }
  1075. if (String(d["type"]) == p_type) {
  1076. int tab_from_id = d["tab_index"];
  1077. int hover_now = get_tab_idx_at_point(p_point);
  1078. NodePath from_path = d["from_path"];
  1079. NodePath to_path = get_path();
  1080. if (from_path == to_path) {
  1081. if (tab_from_id == hover_now) {
  1082. return;
  1083. }
  1084. // Drop the new tab to the left or right depending on where the target tab is being hovered.
  1085. if (hover_now != -1) {
  1086. Rect2 tab_rect = get_tab_rect(hover_now);
  1087. if (is_layout_rtl() ^ (p_point.x <= tab_rect.position.x + tab_rect.size.width / 2)) {
  1088. if (hover_now > tab_from_id) {
  1089. hover_now -= 1;
  1090. }
  1091. } else if (tab_from_id > hover_now) {
  1092. hover_now += 1;
  1093. }
  1094. } else {
  1095. int x = tabs.is_empty() ? 0 : get_tab_rect(0).position.x;
  1096. hover_now = is_layout_rtl() ^ (p_point.x < x) ? 0 : get_tab_count() - 1;
  1097. }
  1098. p_move_tab_callback.call(tab_from_id, hover_now);
  1099. if (!is_tab_disabled(hover_now)) {
  1100. emit_signal(SNAME("active_tab_rearranged"), hover_now);
  1101. set_current_tab(hover_now);
  1102. }
  1103. } else if (get_tabs_rearrange_group() != -1) {
  1104. // Drag and drop between Tabs.
  1105. Node *from_node = get_node(from_path);
  1106. TabBar *from_tabs = Object::cast_to<TabBar>(from_node);
  1107. if (from_tabs && from_tabs->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
  1108. if (tab_from_id >= from_tabs->get_tab_count()) {
  1109. return;
  1110. }
  1111. // Drop the new tab to the left or right depending on where the target tab is being hovered.
  1112. if (hover_now != -1) {
  1113. Rect2 tab_rect = get_tab_rect(hover_now);
  1114. if (is_layout_rtl() ^ (p_point.x > tab_rect.position.x + tab_rect.size.width / 2)) {
  1115. hover_now += 1;
  1116. }
  1117. } else {
  1118. hover_now = tabs.is_empty() || (is_layout_rtl() ^ (p_point.x < get_tab_rect(0).position.x)) ? 0 : get_tab_count();
  1119. }
  1120. p_move_tab_from_other_callback.call(from_tabs, tab_from_id, hover_now);
  1121. }
  1122. }
  1123. }
  1124. }
  1125. void TabBar::_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index) {
  1126. Tab moving_tab = p_from_tabbar->tabs[p_from_index];
  1127. p_from_tabbar->remove_tab(p_from_index);
  1128. tabs.insert(p_to_index, moving_tab);
  1129. if (tabs.size() > 1) {
  1130. if (current >= p_to_index) {
  1131. current++;
  1132. }
  1133. if (previous >= p_to_index) {
  1134. previous++;
  1135. }
  1136. }
  1137. if (!is_tab_disabled(p_to_index)) {
  1138. set_current_tab(p_to_index);
  1139. } else {
  1140. _update_cache();
  1141. queue_redraw();
  1142. }
  1143. update_minimum_size();
  1144. }
  1145. int TabBar::get_tab_idx_at_point(const Point2 &p_point) const {
  1146. int hover_now = -1;
  1147. if (!tabs.is_empty()) {
  1148. for (int i = offset; i <= max_drawn_tab; i++) {
  1149. Rect2 rect = get_tab_rect(i);
  1150. if (rect.has_point(p_point)) {
  1151. hover_now = i;
  1152. }
  1153. }
  1154. }
  1155. return hover_now;
  1156. }
  1157. void TabBar::set_tab_alignment(AlignmentMode p_alignment) {
  1158. ERR_FAIL_INDEX(p_alignment, ALIGNMENT_MAX);
  1159. if (tab_alignment == p_alignment) {
  1160. return;
  1161. }
  1162. tab_alignment = p_alignment;
  1163. _update_cache();
  1164. queue_redraw();
  1165. }
  1166. TabBar::AlignmentMode TabBar::get_tab_alignment() const {
  1167. return tab_alignment;
  1168. }
  1169. void TabBar::set_clip_tabs(bool p_clip_tabs) {
  1170. if (clip_tabs == p_clip_tabs) {
  1171. return;
  1172. }
  1173. clip_tabs = p_clip_tabs;
  1174. if (!clip_tabs) {
  1175. offset = 0;
  1176. max_drawn_tab = 0;
  1177. }
  1178. _update_cache();
  1179. if (scroll_to_selected) {
  1180. ensure_tab_visible(current);
  1181. }
  1182. queue_redraw();
  1183. update_minimum_size();
  1184. }
  1185. bool TabBar::get_clip_tabs() const {
  1186. return clip_tabs;
  1187. }
  1188. void TabBar::set_tab_style_v_flip(bool p_tab_style_v_flip) {
  1189. tab_style_v_flip = p_tab_style_v_flip;
  1190. }
  1191. void TabBar::move_tab(int p_from, int p_to) {
  1192. if (p_from == p_to) {
  1193. return;
  1194. }
  1195. ERR_FAIL_INDEX(p_from, tabs.size());
  1196. ERR_FAIL_INDEX(p_to, tabs.size());
  1197. Tab tab_from = tabs[p_from];
  1198. tabs.remove_at(p_from);
  1199. tabs.insert(p_to, tab_from);
  1200. if (current == p_from) {
  1201. current = p_to;
  1202. } else if (current > p_from && current <= p_to) {
  1203. current--;
  1204. } else if (current < p_from && current >= p_to) {
  1205. current++;
  1206. }
  1207. if (previous == p_from) {
  1208. previous = p_to;
  1209. } else if (previous > p_from && previous <= p_to) {
  1210. previous--;
  1211. } else if (previous < p_from && previous >= p_to) {
  1212. previous++;
  1213. }
  1214. _update_cache();
  1215. _ensure_no_over_offset();
  1216. if (scroll_to_selected) {
  1217. ensure_tab_visible(current);
  1218. }
  1219. queue_redraw();
  1220. notify_property_list_changed();
  1221. }
  1222. int TabBar::get_tab_width(int p_idx) const {
  1223. ERR_FAIL_INDEX_V(p_idx, tabs.size(), 0);
  1224. Ref<StyleBox> style;
  1225. if (tabs[p_idx].disabled) {
  1226. style = theme_cache.tab_disabled_style;
  1227. } else if (current == p_idx) {
  1228. style = theme_cache.tab_selected_style;
  1229. // Use the unselected style's width if the hovered one is shorter, to avoid an infinite loop when switching tabs with the mouse.
  1230. } else if (hover == p_idx && theme_cache.tab_hovered_style->get_minimum_size().width >= theme_cache.tab_unselected_style->get_minimum_size().width) {
  1231. style = theme_cache.tab_hovered_style;
  1232. } else {
  1233. style = theme_cache.tab_unselected_style;
  1234. }
  1235. int x = style->get_minimum_size().width;
  1236. if (tabs[p_idx].icon.is_valid()) {
  1237. const Size2 icon_size = _get_tab_icon_size(p_idx);
  1238. x += icon_size.width + theme_cache.h_separation;
  1239. }
  1240. if (!tabs[p_idx].text.is_empty()) {
  1241. x += tabs[p_idx].size_text + theme_cache.h_separation;
  1242. }
  1243. bool close_visible = cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx == current);
  1244. if (tabs[p_idx].right_button.is_valid()) {
  1245. Ref<StyleBox> btn_style = theme_cache.button_hl_style;
  1246. Ref<Texture2D> rb = tabs[p_idx].right_button;
  1247. if (close_visible) {
  1248. x += btn_style->get_minimum_size().width + rb->get_width();
  1249. } else {
  1250. x += btn_style->get_margin(SIDE_LEFT) + rb->get_width() + theme_cache.h_separation;
  1251. }
  1252. }
  1253. if (close_visible) {
  1254. Ref<StyleBox> btn_style = theme_cache.button_hl_style;
  1255. Ref<Texture2D> cb = theme_cache.close_icon;
  1256. x += btn_style->get_margin(SIDE_LEFT) + cb->get_width() + theme_cache.h_separation;
  1257. }
  1258. if (x > style->get_minimum_size().width) {
  1259. x -= theme_cache.h_separation;
  1260. }
  1261. return x;
  1262. }
  1263. Size2 TabBar::_get_tab_icon_size(int p_index) const {
  1264. ERR_FAIL_INDEX_V(p_index, tabs.size(), Size2());
  1265. const TabBar::Tab &tab = tabs[p_index];
  1266. Size2 icon_size = tab.icon->get_size();
  1267. int icon_max_width = 0;
  1268. if (theme_cache.icon_max_width > 0) {
  1269. icon_max_width = theme_cache.icon_max_width;
  1270. }
  1271. if (tab.icon_max_width > 0 && (icon_max_width == 0 || tab.icon_max_width < icon_max_width)) {
  1272. icon_max_width = tab.icon_max_width;
  1273. }
  1274. if (icon_max_width > 0 && icon_size.width > icon_max_width) {
  1275. icon_size.height = icon_size.height * icon_max_width / icon_size.width;
  1276. icon_size.width = icon_max_width;
  1277. }
  1278. return icon_size;
  1279. }
  1280. void TabBar::_ensure_no_over_offset() {
  1281. if (!is_inside_tree() || !buttons_visible) {
  1282. return;
  1283. }
  1284. int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  1285. int prev_offset = offset;
  1286. int total_w = tabs[max_drawn_tab].ofs_cache + tabs[max_drawn_tab].size_cache - tabs[offset].ofs_cache;
  1287. for (int i = offset; i > 0; i--) {
  1288. if (tabs[i - 1].hidden) {
  1289. continue;
  1290. }
  1291. total_w += tabs[i - 1].size_cache;
  1292. if (total_w < limit_minus_buttons) {
  1293. offset--;
  1294. } else {
  1295. break;
  1296. }
  1297. }
  1298. if (prev_offset != offset) {
  1299. _update_cache();
  1300. queue_redraw();
  1301. }
  1302. }
  1303. bool TabBar::_can_deselect() const {
  1304. if (deselect_enabled) {
  1305. return true;
  1306. }
  1307. // All tabs must be disabled or hidden.
  1308. for (const Tab &tab : tabs) {
  1309. if (!tab.disabled && !tab.hidden) {
  1310. return false;
  1311. }
  1312. }
  1313. return true;
  1314. }
  1315. void TabBar::ensure_tab_visible(int p_idx) {
  1316. if (p_idx == -1 || !is_inside_tree() || !buttons_visible) {
  1317. return;
  1318. }
  1319. ERR_FAIL_INDEX(p_idx, tabs.size());
  1320. if (tabs[p_idx].hidden || (p_idx >= offset && p_idx <= max_drawn_tab)) {
  1321. return;
  1322. }
  1323. if (p_idx < offset) {
  1324. offset = p_idx;
  1325. _update_cache();
  1326. queue_redraw();
  1327. return;
  1328. }
  1329. int limit_minus_buttons = get_size().width - theme_cache.increment_icon->get_width() - theme_cache.decrement_icon->get_width();
  1330. int total_w = tabs[max_drawn_tab].ofs_cache - tabs[offset].ofs_cache;
  1331. for (int i = max_drawn_tab; i <= p_idx; i++) {
  1332. if (tabs[i].hidden) {
  1333. continue;
  1334. }
  1335. total_w += tabs[i].size_cache;
  1336. }
  1337. int prev_offset = offset;
  1338. for (int i = offset; i < p_idx; i++) {
  1339. if (tabs[i].hidden) {
  1340. continue;
  1341. }
  1342. if (total_w > limit_minus_buttons) {
  1343. total_w -= tabs[i].size_cache;
  1344. offset++;
  1345. } else {
  1346. break;
  1347. }
  1348. }
  1349. if (prev_offset != offset) {
  1350. _update_cache();
  1351. queue_redraw();
  1352. }
  1353. }
  1354. Rect2 TabBar::get_tab_rect(int p_tab) const {
  1355. ERR_FAIL_INDEX_V(p_tab, tabs.size(), Rect2());
  1356. if (is_layout_rtl()) {
  1357. return Rect2(get_size().width - tabs[p_tab].ofs_cache - tabs[p_tab].size_cache, 0, tabs[p_tab].size_cache, get_size().height);
  1358. } else {
  1359. return Rect2(tabs[p_tab].ofs_cache, 0, tabs[p_tab].size_cache, get_size().height);
  1360. }
  1361. }
  1362. void TabBar::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) {
  1363. ERR_FAIL_INDEX(p_policy, CLOSE_BUTTON_MAX);
  1364. if (cb_displaypolicy == p_policy) {
  1365. return;
  1366. }
  1367. cb_displaypolicy = p_policy;
  1368. _update_cache();
  1369. _ensure_no_over_offset();
  1370. if (scroll_to_selected) {
  1371. ensure_tab_visible(current);
  1372. }
  1373. queue_redraw();
  1374. update_minimum_size();
  1375. }
  1376. TabBar::CloseButtonDisplayPolicy TabBar::get_tab_close_display_policy() const {
  1377. return cb_displaypolicy;
  1378. }
  1379. void TabBar::set_max_tab_width(int p_width) {
  1380. ERR_FAIL_COND(p_width < 0);
  1381. if (max_width == p_width) {
  1382. return;
  1383. }
  1384. max_width = p_width;
  1385. _update_cache();
  1386. _ensure_no_over_offset();
  1387. if (scroll_to_selected) {
  1388. ensure_tab_visible(current);
  1389. }
  1390. queue_redraw();
  1391. update_minimum_size();
  1392. }
  1393. int TabBar::get_max_tab_width() const {
  1394. return max_width;
  1395. }
  1396. void TabBar::set_scrolling_enabled(bool p_enabled) {
  1397. scrolling_enabled = p_enabled;
  1398. }
  1399. bool TabBar::get_scrolling_enabled() const {
  1400. return scrolling_enabled;
  1401. }
  1402. void TabBar::set_drag_to_rearrange_enabled(bool p_enabled) {
  1403. drag_to_rearrange_enabled = p_enabled;
  1404. }
  1405. bool TabBar::get_drag_to_rearrange_enabled() const {
  1406. return drag_to_rearrange_enabled;
  1407. }
  1408. void TabBar::set_tabs_rearrange_group(int p_group_id) {
  1409. tabs_rearrange_group = p_group_id;
  1410. }
  1411. int TabBar::get_tabs_rearrange_group() const {
  1412. return tabs_rearrange_group;
  1413. }
  1414. void TabBar::set_scroll_to_selected(bool p_enabled) {
  1415. scroll_to_selected = p_enabled;
  1416. if (p_enabled) {
  1417. ensure_tab_visible(current);
  1418. }
  1419. }
  1420. bool TabBar::get_scroll_to_selected() const {
  1421. return scroll_to_selected;
  1422. }
  1423. void TabBar::set_select_with_rmb(bool p_enabled) {
  1424. select_with_rmb = p_enabled;
  1425. }
  1426. bool TabBar::get_select_with_rmb() const {
  1427. return select_with_rmb;
  1428. }
  1429. void TabBar::set_deselect_enabled(bool p_enabled) {
  1430. if (deselect_enabled == p_enabled) {
  1431. return;
  1432. }
  1433. deselect_enabled = p_enabled;
  1434. if (!deselect_enabled && current == -1 && !tabs.is_empty()) {
  1435. select_next_available();
  1436. }
  1437. }
  1438. bool TabBar::get_deselect_enabled() const {
  1439. return deselect_enabled;
  1440. }
  1441. void TabBar::_bind_methods() {
  1442. ClassDB::bind_method(D_METHOD("set_tab_count", "count"), &TabBar::set_tab_count);
  1443. ClassDB::bind_method(D_METHOD("get_tab_count"), &TabBar::get_tab_count);
  1444. ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabBar::set_current_tab);
  1445. ClassDB::bind_method(D_METHOD("get_current_tab"), &TabBar::get_current_tab);
  1446. ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabBar::get_previous_tab);
  1447. ClassDB::bind_method(D_METHOD("select_previous_available"), &TabBar::select_previous_available);
  1448. ClassDB::bind_method(D_METHOD("select_next_available"), &TabBar::select_next_available);
  1449. ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabBar::set_tab_title);
  1450. ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabBar::get_tab_title);
  1451. ClassDB::bind_method(D_METHOD("set_tab_tooltip", "tab_idx", "tooltip"), &TabBar::set_tab_tooltip);
  1452. ClassDB::bind_method(D_METHOD("get_tab_tooltip", "tab_idx"), &TabBar::get_tab_tooltip);
  1453. ClassDB::bind_method(D_METHOD("set_tab_text_direction", "tab_idx", "direction"), &TabBar::set_tab_text_direction);
  1454. ClassDB::bind_method(D_METHOD("get_tab_text_direction", "tab_idx"), &TabBar::get_tab_text_direction);
  1455. ClassDB::bind_method(D_METHOD("set_tab_language", "tab_idx", "language"), &TabBar::set_tab_language);
  1456. ClassDB::bind_method(D_METHOD("get_tab_language", "tab_idx"), &TabBar::get_tab_language);
  1457. ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabBar::set_tab_icon);
  1458. ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabBar::get_tab_icon);
  1459. ClassDB::bind_method(D_METHOD("set_tab_icon_max_width", "tab_idx", "width"), &TabBar::set_tab_icon_max_width);
  1460. ClassDB::bind_method(D_METHOD("get_tab_icon_max_width", "tab_idx"), &TabBar::get_tab_icon_max_width);
  1461. ClassDB::bind_method(D_METHOD("set_tab_button_icon", "tab_idx", "icon"), &TabBar::set_tab_button_icon);
  1462. ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &TabBar::get_tab_button_icon);
  1463. ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabBar::set_tab_disabled);
  1464. ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabBar::is_tab_disabled);
  1465. ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabBar::set_tab_hidden);
  1466. ClassDB::bind_method(D_METHOD("is_tab_hidden", "tab_idx"), &TabBar::is_tab_hidden);
  1467. ClassDB::bind_method(D_METHOD("set_tab_metadata", "tab_idx", "metadata"), &TabBar::set_tab_metadata);
  1468. ClassDB::bind_method(D_METHOD("get_tab_metadata", "tab_idx"), &TabBar::get_tab_metadata);
  1469. ClassDB::bind_method(D_METHOD("remove_tab", "tab_idx"), &TabBar::remove_tab);
  1470. ClassDB::bind_method(D_METHOD("add_tab", "title", "icon"), &TabBar::add_tab, DEFVAL(""), DEFVAL(Ref<Texture2D>()));
  1471. ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabBar::get_tab_idx_at_point);
  1472. ClassDB::bind_method(D_METHOD("set_tab_alignment", "alignment"), &TabBar::set_tab_alignment);
  1473. ClassDB::bind_method(D_METHOD("get_tab_alignment"), &TabBar::get_tab_alignment);
  1474. ClassDB::bind_method(D_METHOD("set_clip_tabs", "clip_tabs"), &TabBar::set_clip_tabs);
  1475. ClassDB::bind_method(D_METHOD("get_clip_tabs"), &TabBar::get_clip_tabs);
  1476. ClassDB::bind_method(D_METHOD("get_tab_offset"), &TabBar::get_tab_offset);
  1477. ClassDB::bind_method(D_METHOD("get_offset_buttons_visible"), &TabBar::get_offset_buttons_visible);
  1478. ClassDB::bind_method(D_METHOD("ensure_tab_visible", "idx"), &TabBar::ensure_tab_visible);
  1479. ClassDB::bind_method(D_METHOD("get_tab_rect", "tab_idx"), &TabBar::get_tab_rect);
  1480. ClassDB::bind_method(D_METHOD("move_tab", "from", "to"), &TabBar::move_tab);
  1481. ClassDB::bind_method(D_METHOD("set_tab_close_display_policy", "policy"), &TabBar::set_tab_close_display_policy);
  1482. ClassDB::bind_method(D_METHOD("get_tab_close_display_policy"), &TabBar::get_tab_close_display_policy);
  1483. ClassDB::bind_method(D_METHOD("set_max_tab_width", "width"), &TabBar::set_max_tab_width);
  1484. ClassDB::bind_method(D_METHOD("get_max_tab_width"), &TabBar::get_max_tab_width);
  1485. ClassDB::bind_method(D_METHOD("set_scrolling_enabled", "enabled"), &TabBar::set_scrolling_enabled);
  1486. ClassDB::bind_method(D_METHOD("get_scrolling_enabled"), &TabBar::get_scrolling_enabled);
  1487. ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabBar::set_drag_to_rearrange_enabled);
  1488. ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabBar::get_drag_to_rearrange_enabled);
  1489. ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabBar::set_tabs_rearrange_group);
  1490. ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabBar::get_tabs_rearrange_group);
  1491. ClassDB::bind_method(D_METHOD("set_scroll_to_selected", "enabled"), &TabBar::set_scroll_to_selected);
  1492. ClassDB::bind_method(D_METHOD("get_scroll_to_selected"), &TabBar::get_scroll_to_selected);
  1493. ClassDB::bind_method(D_METHOD("set_select_with_rmb", "enabled"), &TabBar::set_select_with_rmb);
  1494. ClassDB::bind_method(D_METHOD("get_select_with_rmb"), &TabBar::get_select_with_rmb);
  1495. ClassDB::bind_method(D_METHOD("set_deselect_enabled", "enabled"), &TabBar::set_deselect_enabled);
  1496. ClassDB::bind_method(D_METHOD("get_deselect_enabled"), &TabBar::get_deselect_enabled);
  1497. ClassDB::bind_method(D_METHOD("clear_tabs"), &TabBar::clear_tabs);
  1498. ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
  1499. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
  1500. ADD_SIGNAL(MethodInfo("tab_clicked", PropertyInfo(Variant::INT, "tab")));
  1501. ADD_SIGNAL(MethodInfo("tab_rmb_clicked", PropertyInfo(Variant::INT, "tab")));
  1502. ADD_SIGNAL(MethodInfo("tab_close_pressed", PropertyInfo(Variant::INT, "tab")));
  1503. ADD_SIGNAL(MethodInfo("tab_button_pressed", PropertyInfo(Variant::INT, "tab")));
  1504. ADD_SIGNAL(MethodInfo("tab_hovered", PropertyInfo(Variant::INT, "tab")));
  1505. ADD_SIGNAL(MethodInfo("active_tab_rearranged", PropertyInfo(Variant::INT, "idx_to")));
  1506. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1"), "set_current_tab", "get_current_tab");
  1507. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_alignment", "get_tab_alignment");
  1508. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_tabs"), "set_clip_tabs", "get_clip_tabs");
  1509. 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");
  1510. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_tab_width", PROPERTY_HINT_RANGE, "0,99999,1,suffix:px"), "set_max_tab_width", "get_max_tab_width");
  1511. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrolling_enabled"), "set_scrolling_enabled", "get_scrolling_enabled");
  1512. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
  1513. ADD_PROPERTY(PropertyInfo(Variant::INT, "tabs_rearrange_group"), "set_tabs_rearrange_group", "get_tabs_rearrange_group");
  1514. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_to_selected"), "set_scroll_to_selected", "get_scroll_to_selected");
  1515. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_with_rmb"), "set_select_with_rmb", "get_select_with_rmb");
  1516. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_enabled"), "set_deselect_enabled", "get_deselect_enabled");
  1517. ADD_ARRAY_COUNT("Tabs", "tab_count", "set_tab_count", "get_tab_count", "tab_");
  1518. BIND_ENUM_CONSTANT(ALIGNMENT_LEFT);
  1519. BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
  1520. BIND_ENUM_CONSTANT(ALIGNMENT_RIGHT);
  1521. BIND_ENUM_CONSTANT(ALIGNMENT_MAX);
  1522. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_NEVER);
  1523. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ACTIVE_ONLY);
  1524. BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ALWAYS);
  1525. BIND_ENUM_CONSTANT(CLOSE_BUTTON_MAX);
  1526. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, h_separation);
  1527. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, icon_max_width);
  1528. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_unselected_style, "tab_unselected");
  1529. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_hovered_style, "tab_hovered");
  1530. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_selected_style, "tab_selected");
  1531. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_disabled_style, "tab_disabled");
  1532. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, tab_focus_style, "tab_focus");
  1533. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, increment_icon, "increment");
  1534. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, increment_hl_icon, "increment_highlight");
  1535. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, decrement_icon, "decrement");
  1536. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, decrement_hl_icon, "decrement_highlight");
  1537. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, drop_mark_icon, "drop_mark");
  1538. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, drop_mark_color);
  1539. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_selected_color);
  1540. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_hovered_color);
  1541. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_unselected_color);
  1542. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_disabled_color);
  1543. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TabBar, font_outline_color);
  1544. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, TabBar, font);
  1545. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, TabBar, font_size);
  1546. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, outline_size);
  1547. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TabBar, close_icon, "close");
  1548. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, button_pressed_style, "button_pressed");
  1549. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TabBar, button_hl_style, "button_highlight");
  1550. Tab defaults(true);
  1551. base_property_helper.set_prefix("tab_");
  1552. base_property_helper.set_array_length_getter(&TabBar::get_tab_count);
  1553. base_property_helper.register_property(PropertyInfo(Variant::STRING, "title"), defaults.text, &TabBar::set_tab_title, &TabBar::get_tab_title);
  1554. base_property_helper.register_property(PropertyInfo(Variant::STRING, "tooltip"), defaults.tooltip, &TabBar::set_tab_tooltip, &TabBar::get_tab_tooltip);
  1555. base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &TabBar::set_tab_icon, &TabBar::get_tab_icon);
  1556. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &TabBar::set_tab_disabled, &TabBar::is_tab_disabled);
  1557. PropertyListHelper::register_base_helper(&base_property_helper);
  1558. }
  1559. TabBar::TabBar() {
  1560. set_size(Size2(get_size().width, get_minimum_size().height));
  1561. set_focus_mode(FOCUS_ALL);
  1562. connect(SceneStringName(mouse_exited), callable_mp(this, &TabBar::_on_mouse_exited));
  1563. property_helper.setup_for_instance(base_property_helper, this);
  1564. }