tab_container.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*************************************************************************/
  2. /* tab_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "tab_container.h"
  30. #include "message_queue.h"
  31. int TabContainer::_get_top_margin() const {
  32. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  33. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  34. Ref<Font> font = get_font("font");
  35. int h = MAX( tab_bg->get_minimum_size().height,tab_fg->get_minimum_size().height);
  36. // h+=MIN( get_constant("label_valign_fg"), get_constant("label_valign_bg") );
  37. int ch = font->get_height();;
  38. for(int i=0;i<get_child_count();i++) {
  39. Control *c = get_child(i)->cast_to<Control>();
  40. if (!c)
  41. continue;
  42. if (c->is_set_as_toplevel())
  43. continue;
  44. if (!c->has_meta("_tab_icon"))
  45. continue;
  46. Ref<Texture> tex = c->get_meta("_tab_icon");
  47. if (!tex.is_valid())
  48. continue;
  49. ch = MAX( ch, tex->get_size().height );
  50. }
  51. h+=ch;
  52. return h;
  53. }
  54. void TabContainer::_input_event(const InputEvent& p_event) {
  55. if (p_event.type==InputEvent::MOUSE_BUTTON &&
  56. p_event.mouse_button.pressed &&
  57. p_event.mouse_button.button_index==BUTTON_LEFT) {
  58. // clicks
  59. Point2 pos( p_event.mouse_button.x, p_event.mouse_button.y );
  60. int top_margin = _get_top_margin();
  61. if (pos.y>top_margin)
  62. return; // no click (too far down)
  63. if (pos.x<tabs_ofs_cache)
  64. return; // no click (too far left)
  65. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  66. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  67. Ref<Font> font = get_font("font");
  68. Ref<Texture> incr = get_icon("increment");
  69. Ref<Texture> decr = get_icon("decrement");
  70. pos.x-=tabs_ofs_cache;
  71. int idx=0;
  72. int found=-1;
  73. bool rightroom=false;
  74. for(int i=0;i<get_child_count();i++) {
  75. Control *c = get_child(i)->cast_to<Control>();
  76. if (!c)
  77. continue;
  78. if (c->is_set_as_toplevel())
  79. continue;
  80. if (idx<tab_display_ofs) {
  81. idx++;
  82. continue;
  83. }
  84. if (idx>last_tab_cache) {
  85. rightroom=true;
  86. break;
  87. }
  88. String s = c->has_meta("_tab_title")?String(XL_MESSAGE(String(c->get_meta("_tab_title")))):String(c->get_name());
  89. int tab_width=font->get_string_size(s).width;
  90. if (c->has_meta("_tab_icon")) {
  91. Ref<Texture> icon = c->get_meta("_tab_icon");
  92. if (icon.is_valid()) {
  93. tab_width+=icon->get_width();
  94. if (s!="")
  95. tab_width+=get_constant("hseparation");
  96. }
  97. }
  98. if (idx==current) {
  99. tab_width+=tab_fg->get_minimum_size().width;
  100. } else {
  101. tab_width+=tab_bg->get_minimum_size().width;
  102. }
  103. if (pos.x < tab_width) {
  104. found=idx;
  105. break;
  106. }
  107. pos.x-=tab_width;
  108. idx++;
  109. }
  110. if (buttons_visible_cache) {
  111. if (p_event.mouse_button.x>get_size().width-incr->get_width()) {
  112. if (rightroom) {
  113. tab_display_ofs+=1;
  114. update();
  115. }
  116. } else if (p_event.mouse_button.x>get_size().width-incr->get_width()-decr->get_width()) {
  117. if (tab_display_ofs>0) {
  118. tab_display_ofs-=1;
  119. update();
  120. }
  121. }
  122. }
  123. if (found!=-1) {
  124. set_current_tab(found);
  125. }
  126. }
  127. }
  128. void TabContainer::_notification(int p_what) {
  129. switch(p_what) {
  130. case NOTIFICATION_DRAW: {
  131. RID ci = get_canvas_item();
  132. Ref<StyleBox> panel = get_stylebox("panel");
  133. Size2 size = get_size();
  134. if (!tabs_visible) {
  135. panel->draw(ci, Rect2( 0, 0, size.width, size.height));
  136. return;
  137. }
  138. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  139. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  140. Ref<Texture> incr = get_icon("increment");
  141. Ref<Texture> incr_hl = get_icon("increment_hilite");
  142. Ref<Texture> decr = get_icon("decrement");
  143. Ref<Texture> decr_hl = get_icon("decrement_hilite");
  144. Ref<Font> font = get_font("font");
  145. Color color_fg = get_color("font_color_fg");
  146. Color color_bg = get_color("font_color_bg");
  147. int side_margin = get_constant("side_margin");
  148. int top_margin = _get_top_margin();
  149. int label_valign_fg = get_constant("label_valign_fg");
  150. int label_valign_bg = get_constant("label_valign_bg");
  151. Size2 top_size = Size2( size.width, top_margin );
  152. int w=0;
  153. int idx=0;
  154. for(int i=0;i<get_child_count();i++) {
  155. Control *c = get_child(i)->cast_to<Control>();
  156. if (!c)
  157. continue;
  158. if (c->is_set_as_toplevel())
  159. continue;
  160. String s = c->has_meta("_tab_title")?String(XL_MESSAGE(String(c->get_meta("_tab_title")))):String(c->get_name());
  161. w+=font->get_string_size(s).width;
  162. if (c->has_meta("_tab_icon")) {
  163. Ref<Texture> icon = c->get_meta("_tab_icon");
  164. if (icon.is_valid()) {
  165. w+=icon->get_width();
  166. if (s!="")
  167. w+=get_constant("hseparation");
  168. }
  169. }
  170. if (idx==current) {
  171. w+=tab_fg->get_minimum_size().width;
  172. } else {
  173. w+=tab_bg->get_minimum_size().width;
  174. }
  175. idx++;
  176. }
  177. int ofs;
  178. int limit=get_size().width;
  179. if (w<=get_size().width) {
  180. switch(align) {
  181. case ALIGN_LEFT: ofs = side_margin; break;
  182. case ALIGN_CENTER: ofs = (int(top_size.width) - w)/2; break;
  183. case ALIGN_RIGHT: ofs = int(top_size.width) - w - side_margin; break;
  184. };
  185. tab_display_ofs=0;
  186. buttons_visible_cache=false;
  187. } else {
  188. ofs=0;
  189. limit-=incr->get_width()+decr->get_width();
  190. buttons_visible_cache=true;
  191. }
  192. tabs_ofs_cache=ofs;
  193. last_tab_cache=-1;
  194. idx=0;
  195. bool notdone=false;
  196. for(int i=0;i<get_child_count();i++) {
  197. Control *c = get_child(i)->cast_to<Control>();
  198. if (!c)
  199. continue;
  200. if (c->is_set_as_toplevel())
  201. continue;
  202. if (idx<tab_display_ofs) {
  203. idx++;
  204. continue;
  205. }
  206. String s = c->has_meta("_tab_title")?String(c->get_meta("_tab_title")):String(c->get_name());
  207. int w=font->get_string_size(s).width;
  208. Ref<Texture> icon;
  209. if (c->has_meta("_tab_icon")) {
  210. icon = c->get_meta("_tab_icon");
  211. if (icon.is_valid()) {
  212. w+=icon->get_width();
  213. if (s!="")
  214. w+=get_constant("hseparation");
  215. }
  216. }
  217. Ref<StyleBox> sb;
  218. int va;
  219. Color col;
  220. if (idx==current) {
  221. sb=tab_fg;
  222. va=label_valign_fg;
  223. col=color_fg;
  224. } else {
  225. sb=tab_bg;
  226. va=label_valign_bg;
  227. col=color_bg;
  228. }
  229. Size2i sb_ms = sb->get_minimum_size();
  230. Rect2 sb_rect = Rect2( ofs, 0, w+sb_ms.width, top_margin);
  231. if (sb_ms.width+w+ofs > limit) {
  232. notdone=true;
  233. break;
  234. }
  235. sb->draw(ci, sb_rect );
  236. Point2i lpos = sb_rect.pos;
  237. lpos.x+=sb->get_margin(MARGIN_LEFT);
  238. if (icon.is_valid()) {
  239. icon->draw(ci, Point2i( lpos.x, sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-icon->get_height())/2 ) );
  240. if (s!="")
  241. lpos.x+=icon->get_width()+get_constant("hseparation");
  242. }
  243. font->draw(ci, Point2i( lpos.x, sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-font->get_height())/2+font->get_ascent() ), s, col );
  244. ofs+=sb_ms.x+w;
  245. last_tab_cache=idx;
  246. /*
  247. int sb_mw = sb->get_minimum_size().width;
  248. int font_ofs = sb_mw / 2;
  249. Rect2i rect =Rect2( ofs, 0, w+sb_mw, top_margin);
  250. rect.size
  251. sb->draw(ci,rect);
  252. rect.y+=va;
  253. rect.height+=va;
  254. int font_y = (rect.height - font->get_height())/2;
  255. font->draw(ci, Point2( ofs+font_ofs, va+font_y ), s, col );
  256. */
  257. idx++;
  258. }
  259. if (buttons_visible_cache) {
  260. int vofs = (top_margin-incr->get_height())/2;
  261. decr->draw(ci,Point2(limit,vofs),Color(1,1,1,tab_display_ofs==0?0.5:1.0));
  262. incr->draw(ci,Point2(limit+incr->get_width(),vofs),Color(1,1,1,notdone?1.0:0.5));
  263. }
  264. panel->draw(ci, Rect2( 0, top_size.height, size.width, size.height-top_size.height));
  265. } break;
  266. }
  267. }
  268. void TabContainer::_child_renamed_callback() {
  269. update();
  270. }
  271. void TabContainer::add_child_notify(Node *p_child) {
  272. Control *c = p_child->cast_to<Control>();
  273. if (!c)
  274. return;
  275. if (c->is_set_as_toplevel())
  276. return;
  277. bool first=false;
  278. if (get_tab_count()!=1)
  279. c->hide();
  280. else {
  281. c->show();
  282. //call_deferred("set_current_tab",0);
  283. first=true;
  284. current=0;
  285. }
  286. c->set_area_as_parent_rect();
  287. if (tabs_visible)
  288. c->set_margin(MARGIN_TOP,_get_top_margin());
  289. Ref<StyleBox> sb = get_stylebox("panel");
  290. for(int i=0;i<4;i++)
  291. c->set_margin(Margin(i),c->get_margin(Margin(i))+sb->get_margin(Margin(i)));
  292. update();
  293. p_child->connect("renamed", this,"_child_renamed_callback");
  294. if(first)
  295. emit_signal("tab_changed",current);
  296. }
  297. int TabContainer::get_tab_count() const {
  298. int count=0;
  299. for(int i=0;i<get_child_count();i++) {
  300. Control *c = get_child(i)->cast_to<Control>();
  301. if (!c)
  302. continue;
  303. count++;
  304. }
  305. return count;
  306. }
  307. void TabContainer::set_current_tab(int p_current) {
  308. ERR_FAIL_INDEX( p_current, get_tab_count() );
  309. //printf("DEBUG %p: set_current_tab to %i\n", this, p_current);
  310. current=p_current;
  311. int idx=0;
  312. Ref<StyleBox> sb=get_stylebox("panel");
  313. for(int i=0;i<get_child_count();i++) {
  314. Control *c = get_child(i)->cast_to<Control>();
  315. if (!c)
  316. continue;
  317. if (c->is_set_as_toplevel())
  318. continue;
  319. if (idx==current) {
  320. c->show();
  321. c->set_area_as_parent_rect();
  322. if (tabs_visible)
  323. c->set_margin(MARGIN_TOP,_get_top_margin());
  324. for(int i=0;i<4;i++)
  325. c->set_margin(Margin(i),c->get_margin(Margin(i))+sb->get_margin(Margin(i)));
  326. } else
  327. c->hide();
  328. idx++;
  329. }
  330. _change_notify("current_tab");
  331. emit_signal("tab_changed",current);
  332. update();
  333. }
  334. int TabContainer::get_current_tab() const {
  335. return current;
  336. }
  337. void TabContainer::remove_child_notify(Node *p_child) {
  338. int tc = get_tab_count();
  339. // bool set_curent=false;
  340. if (current==tc-1) {
  341. current--;
  342. if (current<0)
  343. current=0;
  344. else {
  345. call_deferred("set_current_tab",current);
  346. }
  347. }
  348. p_child->disconnect("renamed", this,"_child_renamed_callback");
  349. update();
  350. }
  351. void TabContainer::set_tab_align(TabAlign p_align) {
  352. ERR_FAIL_INDEX(p_align,3);
  353. align=p_align;
  354. update();
  355. _change_notify("tab_align");
  356. }
  357. TabContainer::TabAlign TabContainer::get_tab_align() const {
  358. return align;
  359. }
  360. void TabContainer::set_tabs_visible(bool p_visibe) {
  361. if (p_visibe==tabs_visible)
  362. return;
  363. tabs_visible=p_visibe;
  364. for(int i=0;i<get_child_count();i++) {
  365. Control *c = get_child(i)->cast_to<Control>();
  366. if (!c)
  367. continue;
  368. if (p_visibe)
  369. c->set_margin(MARGIN_TOP,_get_top_margin());
  370. else
  371. c->set_margin(MARGIN_TOP,0);
  372. }
  373. update();
  374. }
  375. bool TabContainer::are_tabs_visible() const {
  376. return tabs_visible;
  377. }
  378. Control *TabContainer::_get_tab(int p_idx) const {
  379. int idx=0;
  380. for(int i=0;i<get_child_count();i++) {
  381. Control *c = get_child(i)->cast_to<Control>();
  382. if (!c)
  383. continue;
  384. if (c->is_set_as_toplevel())
  385. continue;
  386. if (idx==p_idx)
  387. return c;
  388. idx++;
  389. }
  390. return NULL;
  391. }
  392. void TabContainer::set_tab_title(int p_tab,const String& p_title) {
  393. Control *child = _get_tab(p_tab);
  394. ERR_FAIL_COND(!child);
  395. child->set_meta("_tab_name",p_title);
  396. }
  397. String TabContainer::get_tab_title(int p_tab) const{
  398. Control *child = _get_tab(p_tab);
  399. ERR_FAIL_COND_V(!child,"");
  400. if (child->has_meta("_tab_name"))
  401. return child->get_meta("_tab_name");
  402. else
  403. return child->get_name();
  404. }
  405. void TabContainer::set_tab_icon(int p_tab,const Ref<Texture>& p_icon){
  406. Control *child = _get_tab(p_tab);
  407. ERR_FAIL_COND(!child);
  408. child->set_meta("_tab_icon",p_icon);
  409. }
  410. Ref<Texture> TabContainer::get_tab_icon(int p_tab) const{
  411. Control *child = _get_tab(p_tab);
  412. ERR_FAIL_COND_V(!child,Ref<Texture>());
  413. if (child->has_meta("_tab_icon"))
  414. return child->get_meta("_tab_icon");
  415. else
  416. return Ref<Texture>();
  417. }
  418. void TabContainer::get_translatable_strings(List<String> *p_strings) const {
  419. for(int i=0;i<get_child_count();i++) {
  420. Control *c = get_child(i)->cast_to<Control>();
  421. if (!c)
  422. continue;
  423. if (c->is_set_as_toplevel())
  424. continue;
  425. if (!c->has_meta("_tab_name"))
  426. continue;
  427. String name = c->get_meta("_tab_name");
  428. if (name!="")
  429. p_strings->push_back(name);
  430. }
  431. }
  432. void TabContainer::_bind_methods() {
  433. ObjectTypeDB::bind_method(_MD("_input_event"),&TabContainer::_input_event);
  434. ObjectTypeDB::bind_method(_MD("get_tab_count"),&TabContainer::get_tab_count);
  435. ObjectTypeDB::bind_method(_MD("set_current_tab","tab_idx"),&TabContainer::set_current_tab);
  436. ObjectTypeDB::bind_method(_MD("get_current_tab"),&TabContainer::get_current_tab);
  437. ObjectTypeDB::bind_method(_MD("set_tab_align","align"),&TabContainer::set_tab_align);
  438. ObjectTypeDB::bind_method(_MD("get_tab_align"),&TabContainer::get_tab_align);
  439. ObjectTypeDB::bind_method(_MD("set_tabs_visible","visible"),&TabContainer::set_tabs_visible);
  440. ObjectTypeDB::bind_method(_MD("are_tabs_visible"),&TabContainer::are_tabs_visible);
  441. ObjectTypeDB::bind_method(_MD("set_tab_title","tab_idx","title"),&TabContainer::set_tab_title);
  442. ObjectTypeDB::bind_method(_MD("get_tab_title","tab_idx"),&TabContainer::get_tab_title);
  443. ObjectTypeDB::bind_method(_MD("set_tab_icon","tab_idx","icon:Texture"),&TabContainer::set_tab_icon);
  444. ObjectTypeDB::bind_method(_MD("get_tab_icon:Texture","tab_idx"),&TabContainer::get_tab_icon);
  445. ObjectTypeDB::bind_method(_MD("_child_renamed_callback"),&TabContainer::_child_renamed_callback);
  446. ADD_SIGNAL(MethodInfo("tab_changed",PropertyInfo(Variant::INT,"tab")));
  447. ADD_PROPERTY( PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM,"Left,Center,Right"), _SCS("set_tab_align"), _SCS("get_tab_align") );
  448. ADD_PROPERTY( PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE,"-1,4096,1",PROPERTY_USAGE_EDITOR), _SCS("set_current_tab"), _SCS("get_current_tab") );
  449. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "tabs_visible"), _SCS("set_tabs_visible"), _SCS("are_tabs_visible") );
  450. }
  451. TabContainer::TabContainer() {
  452. tab_display_ofs=0;
  453. buttons_visible_cache=false;
  454. tabs_ofs_cache=0;
  455. current=0;
  456. mouse_x_cache=0;
  457. align=ALIGN_CENTER;
  458. tabs_visible=true;
  459. }