tb_atomic_widgets.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. //
  6. // Copyright (c) 2016-2017, THUNDERBEAST GAMES LLC All rights reserved
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #include "tb_widgets_reader.h"
  27. #include "tb_widgets_common.h"
  28. #include "tb_node_tree.h"
  29. #include "tb_system.h"
  30. #include "tb_atomic_widgets.h"
  31. #include "tb_editfield.h"
  32. #include "tb_menu_window.h"
  33. #include "tb_select.h"
  34. #include <math.h>
  35. namespace tb {
  36. // == TBColorWidget =======================================
  37. TBColorWidget::TBColorWidget() : color_(), alpha_ ( 1.0f)
  38. {
  39. }
  40. void TBColorWidget::SetColor ( const char *name )
  41. {
  42. if ( name )
  43. color_.SetFromString(name, strlen(name));
  44. InvalidateSkinStates();
  45. Invalidate();
  46. }
  47. void TBColorWidget::SetColor(float r, float g, float b, float a)
  48. {
  49. color_.Set(TBColor(r, g, b, a));
  50. InvalidateSkinStates();
  51. Invalidate();
  52. }
  53. void TBColorWidget::SetAlpha ( float value )
  54. {
  55. if ( value < 0.0 || value > 1.0 )
  56. {
  57. alpha_ = 1.0;
  58. return;
  59. }
  60. alpha_ = value;
  61. InvalidateSkinStates();
  62. Invalidate();
  63. }
  64. void TBColorWidget::OnPaint(const PaintProps &paint_props)
  65. {
  66. TBRect local_rect = GetRect();
  67. local_rect.x = 0;
  68. local_rect.y = 0;
  69. float old_opacity = g_renderer->GetOpacity();
  70. g_renderer->SetOpacity(alpha_);
  71. g_renderer->DrawRectFill(local_rect, color_);
  72. g_renderer->SetOpacity(old_opacity);
  73. }
  74. void TBColorWidget::OnInflate(const INFLATE_INFO &info)
  75. {
  76. if (const char *colr = info.node->GetValueString("color", nullptr))
  77. SetColor(colr);
  78. TBWidget::OnInflate(info);
  79. }
  80. TB_WIDGET_FACTORY(TBColorWidget, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  81. // == TBColorWheel =======================================
  82. TBColorWheel::TBColorWheel() :
  83. markerx_(128),
  84. markery_(128),
  85. markercolor_(),
  86. hue_(0.0),
  87. saturation_(0.0)
  88. {
  89. }
  90. void TBColorWheel::OnPaint(const PaintProps &paint_props)
  91. {
  92. TBWidget::OnPaint(paint_props); // draw the widget stuff
  93. TBRect local_rect ( 0,0,4,4 ); // AND draw a marker where we clicked.
  94. local_rect.x = markerx_ - 2;
  95. local_rect.y = markery_ - 2;
  96. g_renderer->DrawRect( local_rect, markercolor_);
  97. local_rect.x -= 1;
  98. local_rect.y -= 1;
  99. local_rect.w += 2;
  100. local_rect.h += 2;
  101. g_renderer->DrawRect( local_rect, markercolor_); // draw double box
  102. }
  103. bool TBColorWheel::OnEvent(const TBWidgetEvent &ev)
  104. {
  105. if (ev.target == this && ev.type == EVENT_TYPE_CLICK)
  106. {
  107. SetMarkerX ( ev.target_x );
  108. SetMarkerY ( ev.target_y );
  109. CalcHueSaturation( markerx_, markery_ );
  110. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  111. InvokeEvent(ev);
  112. }
  113. return TBWidget::OnEvent(ev);
  114. }
  115. void TBColorWheel::SetHueSaturation ( float hue, float saturation )
  116. {
  117. // suppose to set the marker position to match HS here
  118. hue_ = hue * 360.0;
  119. saturation_ = saturation * 128.0;
  120. Invalidate();
  121. }
  122. void TBColorWheel::CalcHueSaturation ( int rawx, int rawy )
  123. {
  124. TBRect rect = GetRect();
  125. int centerx = rect.w / 2;
  126. int centery = rect.h / 2;
  127. float X1 = rawx;
  128. float Y1 = rawy;
  129. float X2 = centerx;
  130. float Y2 = centery;
  131. float angle = 0.0;
  132. float xd = X2-X1;
  133. float yd = Y2-Y1;
  134. float dx = sqrt(xd * xd + yd * yd);
  135. // angle in degrees
  136. angle = atan2(Y2 - Y1, X2 - X1) * 180 / 3.14159265358979323846;
  137. if (angle < 0) angle += 360.0;
  138. // if the distance > 128, can we calculate the line point at 128 and set the marker there?
  139. if( dx > 128.0 ) dx = 128.0; // limit value
  140. saturation_ = dx;
  141. hue_ = angle;
  142. }
  143. void TBColorWheel::SetMarkerX ( int value )
  144. {
  145. markerx_ = value;
  146. }
  147. void TBColorWheel::SetMarkerY ( int value )
  148. {
  149. markery_ = value;
  150. }
  151. void TBColorWheel::SetMarkerColor ( const char *name )
  152. {
  153. if ( name )
  154. markercolor_.SetFromString(name, strlen(name));
  155. Invalidate();
  156. }
  157. void TBColorWheel::OnInflate(const INFLATE_INFO &info)
  158. {
  159. if (const char *colr = info.node->GetValueString("color", nullptr))
  160. SetMarkerColor(colr);
  161. TBWidget::OnInflate(info);
  162. }
  163. TB_WIDGET_FACTORY(TBColorWheel, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  164. // == TBBarGraph =======================================
  165. TBBarGraph::TBBarGraph() : color_(255,255,255,255), m_value (0.0), m_axis(AXIS_X), m_margin(0)
  166. {
  167. SetSkinBg(TBIDC("background_solid"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  168. }
  169. void TBBarGraph::SetColor ( const char *name )
  170. {
  171. if ( name )
  172. color_.SetFromString(name, strlen(name));
  173. InvalidateSkinStates();
  174. Invalidate();
  175. }
  176. void TBBarGraph::SetColor(float r, float g, float b, float a)
  177. {
  178. color_.Set(TBColor(r, g, b, a));
  179. InvalidateSkinStates();
  180. Invalidate();
  181. }
  182. void TBBarGraph::OnPaint(const PaintProps &paint_props)
  183. {
  184. TBRect local_rect = GetRect();
  185. local_rect.x = 0;
  186. local_rect.y = 0;
  187. if ( m_axis == AXIS_X ) // horizontal bar
  188. {
  189. double w1 = (double)local_rect.w * ( m_value / 100.0 );
  190. local_rect.w = (int)w1;
  191. if ( m_margin > 0 && m_margin < (local_rect.h/2)-2)
  192. {
  193. local_rect.h -= (m_margin *2);
  194. local_rect.y += m_margin;
  195. }
  196. }
  197. else if ( m_axis == AXIS_Y ) // vertical bar
  198. {
  199. double h1 = (double)local_rect.h * ( m_value / 100.0 );
  200. local_rect.y = local_rect.h - (int)h1;
  201. local_rect.h = (int)h1;
  202. if ( m_margin > 0 && m_margin < (local_rect.w/2)-2 )
  203. {
  204. local_rect.w -= (m_margin*2);
  205. local_rect.x += m_margin;
  206. }
  207. }
  208. g_renderer->DrawRectFill(local_rect, color_);
  209. }
  210. void TBBarGraph::OnInflate(const INFLATE_INFO &info)
  211. {
  212. if (const char *colr = info.node->GetValueString("color", nullptr))
  213. SetColor(colr);
  214. if ( const char *axis = info.node->GetValueString("axis", "x") )
  215. SetAxis(*axis == 'x' ? AXIS_X : AXIS_Y);
  216. if (info.sync_type == TBValue::TYPE_FLOAT)
  217. SetValueDouble(info.node->GetValueFloat("value", 0));
  218. SetMargin( (unsigned)info.node->GetValueInt("margin", 0 ) );
  219. TBWidget::OnInflate(info);
  220. }
  221. void TBBarGraph::SetValueDouble(double value)
  222. {
  223. value = CLAMP(value, 0.0, 100.0);
  224. if (value == m_value)
  225. return;
  226. m_value = value;
  227. InvalidateSkinStates();
  228. Invalidate();
  229. }
  230. void TBBarGraph::SetAxis(AXIS axis)
  231. {
  232. m_axis = axis;
  233. InvalidateSkinStates();
  234. Invalidate();
  235. }
  236. TB_WIDGET_FACTORY(TBBarGraph, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  237. // == TBPromptWindow =======================================
  238. TBPromptWindow::TBPromptWindow(TBWidget *target, TBID id)
  239. : m_target(target)
  240. {
  241. TBWidgetListener::AddGlobalListener(this);
  242. SetID(id);
  243. }
  244. TBPromptWindow::~TBPromptWindow()
  245. {
  246. TBWidgetListener::RemoveGlobalListener(this);
  247. if (TBWidget *dimmer = m_dimmer.Get())
  248. {
  249. dimmer->GetParent()->RemoveChild(dimmer);
  250. delete dimmer;
  251. }
  252. }
  253. bool TBPromptWindow::Show(const char *title, const char *message,
  254. const char *preset, int dimmer,
  255. int width, int height)
  256. {
  257. TBWidget *target = m_target.Get();
  258. if (!target)
  259. return false;
  260. TBWidget *root = target->GetParentRoot();
  261. const char *source = "TBLayout: axis: y, distribution: gravity, position: left\n"
  262. " TBLayout: axis: y, distribution: gravity, distribution-position: left\n"
  263. " TBTextField: id: 1, gravity: left right\n"
  264. " font: size: 14dp\n"
  265. " TBEditField: id: 2, multiline: 0, styling: 0, adapt-to-content: 0, gravity: left right\n"
  266. " font: size: 14dp\n"
  267. " TBSeparator: gravity: left right\n"
  268. " TBLayout: distribution: gravity\n"
  269. " TBButton: text: \" OK \", id: \"TBPromptWindow.ok\"\n"
  270. " font: size: 14dp\n"
  271. " TBButton: text: \"Cancel\", id: \"TBPromptWindow.cancel\"\n"
  272. " font: size: 14dp\n";
  273. if (!g_widgets_reader->LoadData(GetContentRoot(), source))
  274. return false;
  275. SetText(title);
  276. TBTextField *editfield = GetWidgetByIDAndType<TBTextField>(UIPROMPTMESSAGEID);
  277. editfield->SetText(message);
  278. editfield->SetSkinBg("");
  279. TBEditField *stringfield = GetWidgetByIDAndType<TBEditField>(UIPROMPTEDITID);
  280. if (preset)
  281. stringfield->SetText(preset);
  282. TBRect rect;
  283. // Size to fit content. This will use the default size of the textfield.
  284. if (width == 0 || height == 0)
  285. {
  286. ResizeToFitContent();
  287. rect = GetRect();
  288. }
  289. else
  290. {
  291. SetSize(width, height);
  292. rect = GetRect();
  293. }
  294. // Create background dimmer
  295. if (dimmer != 0)
  296. {
  297. if (TBDimmer *dimmer = new TBDimmer)
  298. {
  299. root->AddChild(dimmer);
  300. m_dimmer.Set(dimmer);
  301. }
  302. }
  303. // Center and size to the new height
  304. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  305. SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  306. root->AddChild(this);
  307. return true;
  308. }
  309. bool TBPromptWindow::OnEvent(const TBWidgetEvent &ev)
  310. {
  311. if (ev.type == EVENT_TYPE_CLICK && ev.target->IsOfType<TBButton>())
  312. {
  313. TBWidgetSafePointer this_widget(this);
  314. // Invoke the click on the target
  315. TBWidgetEvent target_ev(EVENT_TYPE_CLICK);
  316. target_ev.ref_id = ev.target->GetID();
  317. InvokeEvent(target_ev);
  318. // If target got deleted, close
  319. if (this_widget.Get())
  320. Close();
  321. return true;
  322. }
  323. else if (ev.type == EVENT_TYPE_KEY_DOWN && ev.special_key == TB_KEY_ESC)
  324. {
  325. TBWidgetEvent click_ev(EVENT_TYPE_CLICK);
  326. m_close_button.InvokeEvent(click_ev);
  327. return true;
  328. }
  329. return TBWindow::OnEvent(ev);
  330. }
  331. void TBPromptWindow::OnDie()
  332. {
  333. if (TBWidget *dimmer = m_dimmer.Get())
  334. dimmer->Die();
  335. }
  336. void TBPromptWindow::OnWidgetDelete(TBWidget *widget)
  337. {
  338. // If the target widget is deleted, close!
  339. if (!m_target.Get())
  340. Close();
  341. }
  342. bool TBPromptWindow::OnWidgetDying(TBWidget *widget)
  343. {
  344. // If the target widget or an ancestor of it is dying, close!
  345. if (widget == m_target.Get() || widget->IsAncestorOf(m_target.Get()))
  346. Close();
  347. return false;
  348. }
  349. // == TBFinderWindow =======================================
  350. TBFinderWindow::TBFinderWindow(TBWidget *target, TBID id)
  351. : m_target(target),
  352. rightMenuParent(NULL),
  353. rightMenuChild(NULL)
  354. {
  355. TBWidgetListener::AddGlobalListener(this);
  356. SetID(id);
  357. }
  358. TBFinderWindow::~TBFinderWindow()
  359. {
  360. TBWidgetListener::RemoveGlobalListener(this);
  361. if (TBWidget *dimmer = m_dimmer.Get())
  362. {
  363. dimmer->GetParent()->RemoveChild(dimmer);
  364. delete dimmer;
  365. }
  366. rightMenuParent=NULL;
  367. rightMenuChild=NULL;
  368. }
  369. bool TBFinderWindow::Show(const char *title,
  370. const char *preset, int dimmer,
  371. int width, int height)
  372. {
  373. TBWidget *target = m_target.Get();
  374. if (!target)
  375. return false;
  376. TBWidget *root = target->GetParentRoot();
  377. const char *source =
  378. "TBLayout: axis: y, size: available, position: gravity, distribution: gravity\n"
  379. " lp: min-width: 512dp, min-height: 500dp\n"
  380. " TBLayout: distribution: gravity, distribution-position: left\n"
  381. " TBEditField: id: 1, multiline: 0, styling: 0, adapt-to-content: 0, gravity: left right\n"
  382. " tooltip current folder location\n"
  383. " font: size: 14dp\n"
  384. " TBButton\n"
  385. " lp: height: 28dp, width: 28dp\n"
  386. " skin TBButton.uniformflat\n"
  387. " TBSkinImage: skin: FolderUp, id: up_image\n"
  388. " id 2\n"
  389. " tooltip Go up one folder\n"
  390. " TBWidget\n"
  391. " lp: height: 28dp, width: 28dp\n"
  392. " TBButton\n"
  393. " lp: height: 28dp, width: 28dp\n"
  394. " skin TBButton.uniformflat\n"
  395. " TBSkinImage: skin: BookmarkIcon, id: book_image\n"
  396. " id 3\n"
  397. " tooltip Bookmark current folder\n"
  398. " TBButton\n"
  399. " lp: height: 28dp, width: 28dp\n"
  400. " skin TBButton.uniformflat\n"
  401. " TBSkinImage: skin: FolderAdd, id: create_image\n"
  402. " id 4\n"
  403. " tooltip Create a new folder \n"
  404. " TBLayout: distribution: gravity, distribution-position: left\n"
  405. " TBLayout: axis: x, distribution: gravity\n"
  406. " TBSelectList: id: 5, gravity: all\n"
  407. " lp: max-width: 160dp, min-width: 160dp\n"
  408. " font: size: 14dp\n"
  409. " TBSelectList: id: 6, gravity: all\n"
  410. " font: size: 14dp\n"
  411. " TBLayout: distribution: gravity\n"
  412. " TBEditField: id: 7, multiline: 0, styling: 0, adapt-to-content: 0, gravity: left right\n"
  413. " tooltip name of the wanted file\n"
  414. " font: size: 14dp\n"
  415. " TBLayout: distribution: gravity\n"
  416. " TBButton: text: \" OK \", id: 8\n"
  417. " font: size: 14dp\n"
  418. " TBButton: text: \"Cancel\", id: 9\n"
  419. " font: size: 14dp\n";
  420. if (!g_widgets_reader->LoadData(GetContentRoot(), source))
  421. return false;
  422. SetText(title);
  423. TBRect rect;
  424. // Size to fit content. This will use the default size of the textfield.
  425. if (width == 0 || height == 0)
  426. {
  427. ResizeToFitContent();
  428. rect = GetRect();
  429. }
  430. else
  431. {
  432. SetSize(width, height);
  433. rect = GetRect();
  434. }
  435. // Create background dimmer
  436. if (dimmer != 0)
  437. {
  438. if (TBDimmer *dimmer = new TBDimmer)
  439. {
  440. root->AddChild(dimmer);
  441. m_dimmer.Set(dimmer);
  442. }
  443. }
  444. // Center and size to the new height
  445. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  446. SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  447. root->AddChild(this);
  448. return true;
  449. }
  450. bool TBFinderWindow::OnEvent(const TBWidgetEvent &ev)
  451. {
  452. if (ev.type == EVENT_TYPE_CLICK )
  453. {
  454. if ( ev.target->IsOfType<TBButton>())
  455. {
  456. TBWidgetSafePointer this_widget(this);
  457. // Invoke the click on the target
  458. TBWidgetEvent target_ev(EVENT_TYPE_CLICK);
  459. target_ev.ref_id = ev.target->GetID();
  460. InvokeEvent(target_ev);
  461. // these are internal buttons that do not close the finder window!
  462. bool isbuttons = (ev.target->GetID() == UIFINDERUPBUTTONID
  463. || ev.target->GetID() == UIFINDERBOOKBUTTONID
  464. || ev.target->GetID() == UIFINDERFOLDERBUTTONID );
  465. // If target got deleted, close
  466. if (this_widget.Get() && !isbuttons )
  467. Close();
  468. return true;
  469. }
  470. else if ( ev.target->GetID() == TBIDC("popupmenu"))
  471. {
  472. if (ev.ref_id == TBIDC("delete"))
  473. {
  474. // send EVENT_TYPE_CUSTOM, were gonna used cached information to send info how we got here
  475. if(rightMenuParent)
  476. {
  477. TBWidgetEvent custom_ev(EVENT_TYPE_CUSTOM);
  478. custom_ev.target = rightMenuParent; // were want to operate on this list
  479. custom_ev.ref_id = rightMenuChild?rightMenuChild->GetID():ev.ref_id; // on this entry
  480. custom_ev.special_key = TB_KEY_DELETE; // and what we wanna do to it
  481. rightMenuParent->InvokeEvent(custom_ev); // forward to delegate
  482. rightMenuChild = NULL; // clear the cached values
  483. rightMenuParent = NULL;
  484. }
  485. }
  486. else
  487. return false;
  488. return true;
  489. }
  490. }
  491. else if (ev.type == EVENT_TYPE_KEY_DOWN && ev.special_key == TB_KEY_ESC)
  492. {
  493. TBWidgetEvent click_ev(EVENT_TYPE_CLICK);
  494. m_close_button.InvokeEvent(click_ev);
  495. return true;
  496. }
  497. else if (ev.type == EVENT_TYPE_CONTEXT_MENU || ev.type == EVENT_TYPE_RIGHT_POINTER_UP ) // want to embed popup menu on TBSelectList id: 5
  498. {
  499. rightMenuChild = ev.target; // save for later, this is where we started
  500. rightMenuParent = FindParentList(ev.target); // save for later, omg why is this so hard!
  501. if ( rightMenuParent && rightMenuParent->GetID() == UIFINDERBOOKLISTID ) // if we clicked in bookmark list take action!
  502. {
  503. TBPoint pos_in_root(ev.target_x, ev.target_y);
  504. if (TBMenuWindow *menu = new TBMenuWindow(rightMenuParent, TBIDC("popupmenu")))
  505. {
  506. TBGenericStringItemSource *source = menu->GetList()->GetDefaultSource();
  507. source->AddItem(new TBGenericStringItem("delete", TBIDC("delete")));
  508. menu->Show(source, TBPopupAlignment(pos_in_root), -1);
  509. }
  510. return true;
  511. }
  512. }
  513. return TBWindow::OnEvent(ev);
  514. }
  515. void TBFinderWindow::OnDie()
  516. {
  517. if (TBWidget *dimmer = m_dimmer.Get())
  518. dimmer->Die();
  519. }
  520. void TBFinderWindow::OnWidgetDelete(TBWidget *widget)
  521. {
  522. // If the target widget is deleted, close!
  523. if (!m_target.Get())
  524. Close();
  525. }
  526. bool TBFinderWindow::OnWidgetDying(TBWidget *widget)
  527. {
  528. // If the target widget or an ancestor of it is dying, close!
  529. if (widget == m_target.Get() || widget->IsAncestorOf(m_target.Get()))
  530. Close();
  531. return false;
  532. }
  533. TBWidget *TBFinderWindow::FindParentList( TBWidget *widget) // utility for dealing with menus.
  534. {
  535. TBWidget *tmp = widget;
  536. while (tmp)
  537. {
  538. if ( tmp->IsOfType<TBSelectList>() ) return tmp;
  539. tmp = tmp->GetParent();
  540. }
  541. return NULL;
  542. }
  543. // == TBPulldownMenu ==========================================
  544. TBPulldownMenu::TBPulldownMenu()
  545. : m_value(-1)
  546. {
  547. SetSource(&m_default_source);
  548. SetSkinBg(TBIDC("TBSelectDropdown"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  549. }
  550. TBPulldownMenu::~TBPulldownMenu()
  551. {
  552. SetSource(nullptr);
  553. CloseWindow();
  554. }
  555. void TBPulldownMenu::OnSourceChanged()
  556. {
  557. m_value = -1;
  558. m_valueid = TBID();
  559. if (m_source && m_source->GetNumItems())
  560. SetValue(0);
  561. }
  562. void TBPulldownMenu::SetValue(int value)
  563. {
  564. if (!m_source)
  565. return;
  566. m_value = value;
  567. m_valueid = GetSelectedItemID();
  568. InvokeModifiedEvent( m_valueid );
  569. }
  570. TBID TBPulldownMenu::GetSelectedItemID()
  571. {
  572. if (m_source && m_value >= 0 && m_value < m_source->GetNumItems())
  573. return m_source->GetItemID(m_value);
  574. return TBID();
  575. }
  576. void TBPulldownMenu::InvokeModifiedEvent( TBID entryid )
  577. {
  578. TBWidgetEvent ev( EVENT_TYPE_CHANGED);
  579. // TBIDC does not register the TBID with the UI system, so do it this way
  580. ev.target = this; // who am I
  581. ev.ref_id = entryid; // id of whom we clicked
  582. TBWidget::OnEvent(ev); // forward to delegate
  583. }
  584. void TBPulldownMenu::OpenWindow()
  585. {
  586. if (!m_source || !m_source->GetNumItems() || m_window_pointer.Get())
  587. return;
  588. if (TBMenuWindow *window = new TBMenuWindow(this, TBIDC("TBPulldownMenu.menu")))
  589. {
  590. m_window_pointer.Set(window);
  591. window->SetSkinBg(TBIDC("TBSelectDropdown.window"));
  592. window->Show(m_source, TBPopupAlignment());
  593. }
  594. }
  595. void TBPulldownMenu::CloseWindow()
  596. {
  597. if (TBMenuWindow *window = GetMenuIfOpen())
  598. window->Close();
  599. }
  600. TBMenuWindow *TBPulldownMenu::GetMenuIfOpen() const
  601. {
  602. return TBSafeCast<TBMenuWindow>(m_window_pointer.Get());
  603. }
  604. bool TBPulldownMenu::OnEvent(const TBWidgetEvent &ev)
  605. {
  606. if ( ev.target->IsOfType<TBButton>() && ev.type == EVENT_TYPE_CLICK)
  607. {
  608. // Open the menu, or set the value and close it if already open (this will
  609. // happen when clicking by keyboard since that will call click on this button)
  610. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  611. {
  612. TBWidgetSafePointer tmp(this);
  613. int value = menu_window->GetList()->GetValue();
  614. menu_window->Die();
  615. if (tmp.Get())
  616. SetValue(value);
  617. }
  618. else
  619. OpenWindow();
  620. return true;
  621. }
  622. else if (ev.target->GetID() == TBIDC("TBPulldownMenu.menu") && ev.type == EVENT_TYPE_CLICK )
  623. {
  624. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  625. SetValue(menu_window->GetList()->GetValue());
  626. return true;
  627. }
  628. else if (ev.target == this && m_source && ev.IsKeyEvent())
  629. {
  630. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  631. {
  632. // Redirect the key strokes to the list
  633. TBWidgetEvent redirected_ev(ev);
  634. return menu_window->GetList()->InvokeEvent(redirected_ev);
  635. }
  636. }
  637. return false;
  638. }
  639. TB_WIDGET_FACTORY( TBPulldownMenu, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  640. extern void ReadItems(TBNode *node, TBGenericStringItemSource *target_source); // blind function that lives in tb_widgets_reader.cpp
  641. void TBPulldownMenu::OnInflate(const INFLATE_INFO &info)
  642. {
  643. // Read items (if there is any) into the default source
  644. ReadItems(info.node, GetDefaultSource());
  645. TBWidget::OnInflate(info);
  646. }
  647. }; // namespace tb