tb_atomic_widgets.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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 "tb_tab_container.h"
  35. #include "image/tb_image_widget.h"
  36. #include <math.h>
  37. namespace tb {
  38. // == TBColorWidget =======================================
  39. TBColorWidget::TBColorWidget() : color_(), alpha_ ( 1.0f)
  40. {
  41. }
  42. void TBColorWidget::SetColor ( const char *name )
  43. {
  44. if ( name )
  45. color_.SetFromString(name, strlen(name));
  46. InvalidateSkinStates();
  47. Invalidate();
  48. }
  49. void TBColorWidget::SetColor(float r, float g, float b, float a)
  50. {
  51. color_.Set(TBColor(r, g, b, a));
  52. InvalidateSkinStates();
  53. Invalidate();
  54. }
  55. void TBColorWidget::SetAlpha ( float value )
  56. {
  57. if ( value < 0.0 || value > 1.0 )
  58. {
  59. alpha_ = 1.0;
  60. return;
  61. }
  62. alpha_ = value;
  63. InvalidateSkinStates();
  64. Invalidate();
  65. }
  66. void TBColorWidget::OnPaint(const PaintProps &paint_props)
  67. {
  68. TBRect local_rect = GetRect();
  69. local_rect.x = 0;
  70. local_rect.y = 0;
  71. float old_opacity = g_renderer->GetOpacity();
  72. g_renderer->SetOpacity(alpha_);
  73. g_renderer->DrawRectFill(local_rect, color_);
  74. g_renderer->SetOpacity(old_opacity);
  75. }
  76. void TBColorWidget::OnInflate(const INFLATE_INFO &info)
  77. {
  78. if (const char *colr = info.node->GetValueString("color", nullptr))
  79. SetColor(colr);
  80. TBWidget::OnInflate(info);
  81. }
  82. TB_WIDGET_FACTORY(TBColorWidget, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  83. // == TBColorWheel =======================================
  84. TBColorWheel::TBColorWheel() :
  85. markerx_(128),
  86. markery_(128),
  87. markercolor_(),
  88. hue_(0.0),
  89. saturation_(0.0)
  90. {
  91. }
  92. void TBColorWheel::OnPaint(const PaintProps &paint_props)
  93. {
  94. TBWidget::OnPaint(paint_props); // draw the widget stuff
  95. TBRect local_rect ( 0,0,4,4 ); // AND draw a marker where we clicked.
  96. local_rect.x = markerx_ - 2;
  97. local_rect.y = markery_ - 2;
  98. g_renderer->DrawRect( local_rect, markercolor_);
  99. local_rect.x -= 1;
  100. local_rect.y -= 1;
  101. local_rect.w += 2;
  102. local_rect.h += 2;
  103. g_renderer->DrawRect( local_rect, markercolor_); // draw double box
  104. }
  105. bool TBColorWheel::OnEvent(const TBWidgetEvent &ev)
  106. {
  107. if (ev.target == this && ev.type == EVENT_TYPE_CLICK)
  108. {
  109. SetMarkerX ( ev.target_x );
  110. SetMarkerY ( ev.target_y );
  111. CalcHueSaturation( markerx_, markery_ );
  112. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  113. InvokeEvent(ev);
  114. }
  115. return TBWidget::OnEvent(ev);
  116. }
  117. void TBColorWheel::SetHueSaturation ( float hue, float saturation )
  118. {
  119. // suppose to set the marker position to match HS here
  120. hue_ = hue * 360.0;
  121. saturation_ = saturation * 128.0;
  122. Invalidate();
  123. }
  124. void TBColorWheel::CalcHueSaturation ( int rawx, int rawy )
  125. {
  126. TBRect rect = GetRect();
  127. int centerx = rect.w / 2;
  128. int centery = rect.h / 2;
  129. float X1 = rawx;
  130. float Y1 = rawy;
  131. float X2 = centerx;
  132. float Y2 = centery;
  133. float angle = 0.0;
  134. float xd = X2-X1;
  135. float yd = Y2-Y1;
  136. float dx = sqrt(xd * xd + yd * yd);
  137. // angle in degrees
  138. angle = atan2(Y2 - Y1, X2 - X1) * 180 / 3.14159265358979323846;
  139. if (angle < 0) angle += 360.0;
  140. // if the distance > 128, can we calculate the line point at 128 and set the marker there?
  141. if( dx > 128.0 ) dx = 128.0; // limit value
  142. saturation_ = dx;
  143. hue_ = angle;
  144. }
  145. void TBColorWheel::SetMarkerX ( int value )
  146. {
  147. markerx_ = value;
  148. }
  149. void TBColorWheel::SetMarkerY ( int value )
  150. {
  151. markery_ = value;
  152. }
  153. void TBColorWheel::SetMarkerColor ( const char *name )
  154. {
  155. if ( name )
  156. markercolor_.SetFromString(name, strlen(name));
  157. Invalidate();
  158. }
  159. void TBColorWheel::OnInflate(const INFLATE_INFO &info)
  160. {
  161. if (const char *colr = info.node->GetValueString("color", nullptr))
  162. SetMarkerColor(colr);
  163. TBWidget::OnInflate(info);
  164. }
  165. TB_WIDGET_FACTORY(TBColorWheel, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  166. // == TBBarGraph =======================================
  167. TBBarGraph::TBBarGraph() : color_(255,255,255,255), m_value (0.0), m_axis(AXIS_X), m_margin(0)
  168. {
  169. SetSkinBg(TBIDC("background_solid"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  170. }
  171. void TBBarGraph::SetColor ( const char *name )
  172. {
  173. if ( name )
  174. color_.SetFromString(name, strlen(name));
  175. InvalidateSkinStates();
  176. Invalidate();
  177. }
  178. void TBBarGraph::SetColor(float r, float g, float b, float a)
  179. {
  180. color_.Set(TBColor(r, g, b, a));
  181. InvalidateSkinStates();
  182. Invalidate();
  183. }
  184. void TBBarGraph::OnPaint(const PaintProps &paint_props)
  185. {
  186. TBRect local_rect = GetRect();
  187. local_rect.x = 0;
  188. local_rect.y = 0;
  189. if ( m_axis == AXIS_X ) // horizontal bar
  190. {
  191. double w1 = (double)local_rect.w * ( m_value / 100.0 );
  192. local_rect.w = (int)w1;
  193. if ( m_margin > 0 && m_margin < (local_rect.h/2)-2)
  194. {
  195. local_rect.h -= (m_margin *2);
  196. local_rect.y += m_margin;
  197. }
  198. }
  199. else if ( m_axis == AXIS_Y ) // vertical bar
  200. {
  201. double h1 = (double)local_rect.h * ( m_value / 100.0 );
  202. local_rect.y = local_rect.h - (int)h1;
  203. local_rect.h = (int)h1;
  204. if ( m_margin > 0 && m_margin < (local_rect.w/2)-2 )
  205. {
  206. local_rect.w -= (m_margin*2);
  207. local_rect.x += m_margin;
  208. }
  209. }
  210. g_renderer->DrawRectFill(local_rect, color_);
  211. }
  212. void TBBarGraph::OnInflate(const INFLATE_INFO &info)
  213. {
  214. if (const char *colr = info.node->GetValueString("color", nullptr))
  215. SetColor(colr);
  216. if ( const char *axis = info.node->GetValueString("axis", "x") )
  217. SetAxis(*axis == 'x' ? AXIS_X : AXIS_Y);
  218. if (info.sync_type == TBValue::TYPE_FLOAT)
  219. SetValueDouble(info.node->GetValueFloat("value", 0));
  220. SetMargin( (unsigned)info.node->GetValueInt("margin", 0 ) );
  221. TBWidget::OnInflate(info);
  222. }
  223. void TBBarGraph::SetValueDouble(double value)
  224. {
  225. value = CLAMP(value, 0.0, 100.0);
  226. if (value == m_value)
  227. return;
  228. m_value = value;
  229. InvalidateSkinStates();
  230. Invalidate();
  231. }
  232. void TBBarGraph::SetAxis(AXIS axis)
  233. {
  234. m_axis = axis;
  235. InvalidateSkinStates();
  236. Invalidate();
  237. }
  238. TB_WIDGET_FACTORY(TBBarGraph, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  239. // == TBPromptWindow =======================================
  240. TBPromptWindow::TBPromptWindow(TBWidget *target, TBID id)
  241. : m_target(target)
  242. {
  243. TBWidgetListener::AddGlobalListener(this);
  244. SetID(id);
  245. }
  246. TBPromptWindow::~TBPromptWindow()
  247. {
  248. TBWidgetListener::RemoveGlobalListener(this);
  249. if (TBWidget *dimmer = m_dimmer.Get())
  250. {
  251. dimmer->GetParent()->RemoveChild(dimmer);
  252. delete dimmer;
  253. }
  254. }
  255. bool TBPromptWindow::Show(const char *title, const char *message,
  256. const char *preset, int dimmer,
  257. int width, int height)
  258. {
  259. TBWidget *target = m_target.Get();
  260. if (!target)
  261. return false;
  262. TBWidget *root = target->GetParentRoot();
  263. const char *source = "TBLayout: axis: y, distribution: gravity, position: left\n"
  264. " TBLayout: axis: y, distribution: gravity, distribution-position: left\n"
  265. " TBTextField: id: 1, gravity: left right\n"
  266. " font: size: 14dp\n"
  267. " TBEditField: id: 2, multiline: 0, styling: 0, adapt-to-content: 0, gravity: left right\n"
  268. " font: size: 14dp\n"
  269. " TBSeparator: gravity: left right\n"
  270. " TBLayout: distribution: gravity\n"
  271. " TBButton: text: \" OK \", id: \"TBPromptWindow.ok\"\n"
  272. " font: size: 14dp\n"
  273. " TBButton: text: \"Cancel\", id: \"TBPromptWindow.cancel\"\n"
  274. " font: size: 14dp\n";
  275. if (!g_widgets_reader->LoadData(GetContentRoot(), source))
  276. return false;
  277. SetText(title);
  278. TBTextField *editfield = GetWidgetByIDAndType<TBTextField>(UIPROMPTMESSAGEID);
  279. editfield->SetText(message);
  280. editfield->SetSkinBg("");
  281. TBEditField *stringfield = GetWidgetByIDAndType<TBEditField>(UIPROMPTEDITID);
  282. if (preset)
  283. stringfield->SetText(preset);
  284. TBRect rect;
  285. // Size to fit content. This will use the default size of the textfield.
  286. if (width == 0 || height == 0)
  287. {
  288. ResizeToFitContent();
  289. rect = GetRect();
  290. }
  291. else
  292. {
  293. SetSize(width, height);
  294. rect = GetRect();
  295. }
  296. // Create background dimmer
  297. if (dimmer != 0)
  298. {
  299. if (TBDimmer *dimmer = new TBDimmer)
  300. {
  301. root->AddChild(dimmer);
  302. m_dimmer.Set(dimmer);
  303. }
  304. }
  305. // Center and size to the new height
  306. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  307. SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  308. root->AddChild(this);
  309. return true;
  310. }
  311. bool TBPromptWindow::OnEvent(const TBWidgetEvent &ev)
  312. {
  313. if (ev.type == EVENT_TYPE_CLICK && ev.target->IsOfType<TBButton>())
  314. {
  315. TBWidgetSafePointer this_widget(this);
  316. // Invoke the click on the target
  317. TBWidgetEvent target_ev(EVENT_TYPE_CLICK);
  318. target_ev.ref_id = ev.target->GetID();
  319. InvokeEvent(target_ev);
  320. // If target got deleted, close
  321. if (this_widget.Get())
  322. Close();
  323. return true;
  324. }
  325. else if (ev.type == EVENT_TYPE_KEY_DOWN && ev.special_key == TB_KEY_ESC)
  326. {
  327. TBWidgetEvent click_ev(EVENT_TYPE_CLICK);
  328. m_close_button.InvokeEvent(click_ev);
  329. return true;
  330. }
  331. return TBWindow::OnEvent(ev);
  332. }
  333. void TBPromptWindow::OnDie()
  334. {
  335. if (TBWidget *dimmer = m_dimmer.Get())
  336. dimmer->Die();
  337. }
  338. void TBPromptWindow::OnWidgetDelete(TBWidget *widget)
  339. {
  340. // If the target widget is deleted, close!
  341. if (!m_target.Get())
  342. Close();
  343. }
  344. bool TBPromptWindow::OnWidgetDying(TBWidget *widget)
  345. {
  346. // If the target widget or an ancestor of it is dying, close!
  347. if (widget == m_target.Get() || widget->IsAncestorOf(m_target.Get()))
  348. Close();
  349. return false;
  350. }
  351. // == TBFinderWindow =======================================
  352. TBFinderWindow::TBFinderWindow(TBWidget *target, TBID id)
  353. : m_target(target),
  354. rightMenuParent(NULL),
  355. rightMenuChild(NULL)
  356. {
  357. TBWidgetListener::AddGlobalListener(this);
  358. SetID(id);
  359. }
  360. TBFinderWindow::~TBFinderWindow()
  361. {
  362. TBWidgetListener::RemoveGlobalListener(this);
  363. if (TBWidget *dimmer = m_dimmer.Get())
  364. {
  365. dimmer->GetParent()->RemoveChild(dimmer);
  366. delete dimmer;
  367. }
  368. rightMenuParent=NULL;
  369. rightMenuChild=NULL;
  370. }
  371. bool TBFinderWindow::Show(const char *title,
  372. const char *preset, int dimmer,
  373. int width, int height)
  374. {
  375. TBWidget *target = m_target.Get();
  376. if (!target)
  377. return false;
  378. TBWidget *root = target->GetParentRoot();
  379. const char *source =
  380. "TBLayout: axis: y, size: available, position: gravity, distribution: gravity\n"
  381. " lp: min-width: 512dp, min-height: 500dp\n"
  382. " TBLayout: distribution: gravity, distribution-position: left\n"
  383. " TBEditField: id: 1, multiline: 0, styling: 0, adapt-to-content: 0, gravity: left right\n"
  384. " tooltip current folder location\n"
  385. " font: size: 14dp\n"
  386. " TBButton\n"
  387. " lp: height: 28dp, width: 28dp\n"
  388. " skin TBButton.uniformflat\n"
  389. " TBSkinImage: skin: FolderUp, id: up_image\n"
  390. " id 2\n"
  391. " tooltip Go up one folder\n"
  392. " TBWidget\n"
  393. " lp: height: 28dp, width: 28dp\n"
  394. " TBButton\n"
  395. " lp: height: 28dp, width: 28dp\n"
  396. " skin TBButton.uniformflat\n"
  397. " TBSkinImage: skin: BookmarkIcon, id: book_image\n"
  398. " id 3\n"
  399. " tooltip Bookmark current folder\n"
  400. " TBButton\n"
  401. " lp: height: 28dp, width: 28dp\n"
  402. " skin TBButton.uniformflat\n"
  403. " TBSkinImage: skin: FolderAdd, id: create_image\n"
  404. " id 4\n"
  405. " tooltip Create a new folder \n"
  406. " TBLayout: distribution: gravity, distribution-position: left\n"
  407. " TBLayout: axis: x, distribution: gravity\n"
  408. " TBSelectList: id: 5, gravity: all\n"
  409. " lp: max-width: 160dp, min-width: 160dp\n"
  410. " font: size: 14dp\n"
  411. " TBSelectList: id: 6, gravity: all\n"
  412. " font: size: 14dp\n"
  413. " TBLayout: distribution: gravity\n"
  414. " TBEditField: id: 7, multiline: 0, styling: 0, adapt-to-content: 0, gravity: left right\n"
  415. " tooltip name of the wanted file\n"
  416. " font: size: 14dp\n"
  417. " TBLayout: distribution: gravity\n"
  418. " TBButton: text: \" OK \", id: 8\n"
  419. " font: size: 14dp\n"
  420. " TBButton: text: \"Cancel\", id: 9\n"
  421. " font: size: 14dp\n";
  422. if (!g_widgets_reader->LoadData(GetContentRoot(), source))
  423. return false;
  424. SetText(title);
  425. TBRect rect;
  426. // Size to fit content. This will use the default size of the textfield.
  427. if (width == 0 || height == 0)
  428. {
  429. ResizeToFitContent();
  430. rect = GetRect();
  431. }
  432. else
  433. {
  434. SetSize(width, height);
  435. rect = GetRect();
  436. }
  437. // Create background dimmer
  438. if (dimmer != 0)
  439. {
  440. if (TBDimmer *dimmer = new TBDimmer)
  441. {
  442. root->AddChild(dimmer);
  443. m_dimmer.Set(dimmer);
  444. }
  445. }
  446. // Center and size to the new height
  447. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  448. SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  449. root->AddChild(this);
  450. return true;
  451. }
  452. bool TBFinderWindow::OnEvent(const TBWidgetEvent &ev)
  453. {
  454. if (ev.type == EVENT_TYPE_CLICK )
  455. {
  456. if ( ev.target->IsOfType<TBButton>())
  457. {
  458. TBWidgetSafePointer this_widget(this);
  459. // Invoke the click on the target
  460. TBWidgetEvent target_ev(EVENT_TYPE_CLICK);
  461. target_ev.ref_id = ev.target->GetID();
  462. InvokeEvent(target_ev);
  463. // these are internal buttons that do not close the finder window!
  464. bool isbuttons = (ev.target->GetID() == UIFINDERUPBUTTONID
  465. || ev.target->GetID() == UIFINDERBOOKBUTTONID
  466. || ev.target->GetID() == UIFINDERFOLDERBUTTONID );
  467. // If target got deleted, close
  468. if (this_widget.Get() && !isbuttons )
  469. Close();
  470. return true;
  471. }
  472. else if ( ev.target->GetID() == TBIDC("popupmenu"))
  473. {
  474. if (ev.ref_id == TBIDC("delete"))
  475. {
  476. // send EVENT_TYPE_CUSTOM, were gonna used cached information to send info how we got here
  477. if(rightMenuParent)
  478. {
  479. TBWidgetEvent custom_ev(EVENT_TYPE_CUSTOM);
  480. custom_ev.target = rightMenuParent; // were want to operate on this list
  481. custom_ev.ref_id = rightMenuChild?rightMenuChild->GetID():ev.ref_id; // on this entry
  482. custom_ev.special_key = TB_KEY_DELETE; // and what we wanna do to it
  483. rightMenuParent->InvokeEvent(custom_ev); // forward to delegate
  484. rightMenuChild = NULL; // clear the cached values
  485. rightMenuParent = NULL;
  486. }
  487. }
  488. else
  489. return false;
  490. return true;
  491. }
  492. }
  493. else if (ev.type == EVENT_TYPE_KEY_DOWN && ev.special_key == TB_KEY_ESC)
  494. {
  495. TBWidgetEvent click_ev(EVENT_TYPE_CLICK);
  496. m_close_button.InvokeEvent(click_ev);
  497. return true;
  498. }
  499. else if (ev.type == EVENT_TYPE_CONTEXT_MENU || ev.type == EVENT_TYPE_RIGHT_POINTER_UP ) // want to embed popup menu on TBSelectList id: 5
  500. {
  501. rightMenuChild = ev.target; // save for later, this is where we started
  502. rightMenuParent = FindParentList(ev.target); // save for later, omg why is this so hard!
  503. if ( rightMenuParent && rightMenuParent->GetID() == UIFINDERBOOKLISTID ) // if we clicked in bookmark list take action!
  504. {
  505. TBPoint pos_in_root(ev.target_x, ev.target_y);
  506. if (TBMenuWindow *menu = new TBMenuWindow(rightMenuParent, TBIDC("popupmenu")))
  507. {
  508. TBGenericStringItemSource *source = menu->GetList()->GetDefaultSource();
  509. source->AddItem(new TBGenericStringItem("delete", TBIDC("delete")));
  510. menu->Show(source, TBPopupAlignment(pos_in_root), -1);
  511. }
  512. return true;
  513. }
  514. }
  515. return TBWindow::OnEvent(ev);
  516. }
  517. void TBFinderWindow::OnDie()
  518. {
  519. if (TBWidget *dimmer = m_dimmer.Get())
  520. dimmer->Die();
  521. }
  522. void TBFinderWindow::OnWidgetDelete(TBWidget *widget)
  523. {
  524. // If the target widget is deleted, close!
  525. if (!m_target.Get())
  526. Close();
  527. }
  528. bool TBFinderWindow::OnWidgetDying(TBWidget *widget)
  529. {
  530. // If the target widget or an ancestor of it is dying, close!
  531. if (widget == m_target.Get() || widget->IsAncestorOf(m_target.Get()))
  532. Close();
  533. return false;
  534. }
  535. TBWidget *TBFinderWindow::FindParentList( TBWidget *widget) // utility for dealing with menus.
  536. {
  537. TBWidget *tmp = widget;
  538. while (tmp)
  539. {
  540. if ( tmp->IsOfType<TBSelectList>() ) return tmp;
  541. tmp = tmp->GetParent();
  542. }
  543. return NULL;
  544. }
  545. // == TBPulldownMenu ==========================================
  546. TBPulldownMenu::TBPulldownMenu()
  547. : m_value(-1)
  548. {
  549. SetSource(&m_default_source);
  550. SetSkinBg(TBIDC("TBSelectDropdown"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  551. }
  552. TBPulldownMenu::~TBPulldownMenu()
  553. {
  554. SetSource(nullptr);
  555. CloseWindow();
  556. }
  557. void TBPulldownMenu::OnSourceChanged()
  558. {
  559. m_value = -1;
  560. m_valueid = TBID();
  561. if (m_source && m_source->GetNumItems())
  562. SetValue(0);
  563. }
  564. void TBPulldownMenu::SetValue(int value)
  565. {
  566. if (!m_source)
  567. return;
  568. m_value = value;
  569. m_valueid = GetSelectedItemID();
  570. InvokeModifiedEvent( m_valueid );
  571. }
  572. TBID TBPulldownMenu::GetSelectedItemID()
  573. {
  574. if (m_source && m_value >= 0 && m_value < m_source->GetNumItems())
  575. return m_source->GetItemID(m_value);
  576. return TBID();
  577. }
  578. void TBPulldownMenu::InvokeModifiedEvent( TBID entryid )
  579. {
  580. TBWidgetEvent ev( EVENT_TYPE_CHANGED);
  581. // TBIDC does not register the TBID with the UI system, so do it this way
  582. ev.target = this; // who am I
  583. ev.ref_id = entryid; // id of whom we clicked
  584. TBWidget::OnEvent(ev); // forward to delegate
  585. }
  586. void TBPulldownMenu::OpenWindow()
  587. {
  588. if (!m_source || !m_source->GetNumItems() || m_window_pointer.Get())
  589. return;
  590. if (TBMenuWindow *window = new TBMenuWindow(this, TBIDC("TBPulldownMenu.menu")))
  591. {
  592. m_window_pointer.Set(window);
  593. window->SetSkinBg(TBIDC("TBSelectDropdown.window"));
  594. window->Show(m_source, TBPopupAlignment());
  595. }
  596. }
  597. void TBPulldownMenu::CloseWindow()
  598. {
  599. if (TBMenuWindow *window = GetMenuIfOpen())
  600. window->Close();
  601. }
  602. TBMenuWindow *TBPulldownMenu::GetMenuIfOpen() const
  603. {
  604. return TBSafeCast<TBMenuWindow>(m_window_pointer.Get());
  605. }
  606. bool TBPulldownMenu::OnEvent(const TBWidgetEvent &ev)
  607. {
  608. if ( ev.target->IsOfType<TBButton>() && ev.type == EVENT_TYPE_CLICK)
  609. {
  610. // Open the menu, or set the value and close it if already open (this will
  611. // happen when clicking by keyboard since that will call click on this button)
  612. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  613. {
  614. TBWidgetSafePointer tmp(this);
  615. int value = menu_window->GetList()->GetValue();
  616. menu_window->Die();
  617. if (tmp.Get())
  618. SetValue(value);
  619. }
  620. else
  621. OpenWindow();
  622. return true;
  623. }
  624. else if (ev.target->GetID() == TBIDC("TBPulldownMenu.menu") && ev.type == EVENT_TYPE_CLICK )
  625. {
  626. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  627. SetValue(menu_window->GetList()->GetValue());
  628. return true;
  629. }
  630. else if (ev.target == this && m_source && ev.IsKeyEvent())
  631. {
  632. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  633. {
  634. // Redirect the key strokes to the list
  635. TBWidgetEvent redirected_ev(ev);
  636. return menu_window->GetList()->InvokeEvent(redirected_ev);
  637. }
  638. }
  639. return false;
  640. }
  641. TB_WIDGET_FACTORY( TBPulldownMenu, TBValue::TYPE_INT, WIDGET_Z_TOP) {}
  642. extern void ReadItems(TBNode *node, TBGenericStringItemSource *target_source); // blind function that lives in tb_widgets_reader.cpp
  643. void TBPulldownMenu::OnInflate(const INFLATE_INFO &info)
  644. {
  645. // Read items (if there is any) into the default source
  646. ReadItems(info.node, GetDefaultSource());
  647. TBWidget::OnInflate(info);
  648. }
  649. // == TBDockWindow ==========================================
  650. TBDockWindow::TBDockWindow( TBStr title, TBWidget *contentptr, int minwidth, int minheight ) : TBWindow()
  651. {
  652. SetID(TBID(title));
  653. m_mover.AddChild(&m_redock_button);
  654. m_redock_button.SetSkinBg(TBIDC("TBWindow.redock"));
  655. if ( m_redock_button.GetSkinBgElement() == NULL )
  656. m_redock_button.SetSkinBg(TBIDC("arrow.down")); // fallback skin for editor
  657. m_redock_button.SetIsFocusable(false);
  658. m_redock_button.SetID(TBIDC("TBWindow.redock"));
  659. m_redock_button.SetVisibilility(WIDGET_VISIBILITY_INVISIBLE);
  660. SetText(title);
  661. TBStr uilayout; // undock host ui layout
  662. uilayout.SetFormatted ( "TBLayout: axis: y, distribution: gravity\n\tlp: min-width: %ddp, min-height: %ddp\n\tTBLayout: id: \"undocklayout\", distribution: gravity\n", minwidth, minheight );
  663. g_widgets_reader->LoadData(this, uilayout );
  664. TBLayout *lo1 = GetWidgetByIDAndType<TBLayout>(TBID("undocklayout"));
  665. lo1->AddChild(contentptr); // jam it into the window before the pointer gets stale
  666. ResizeToFitContent();
  667. }
  668. TBDockWindow::~TBDockWindow()
  669. {
  670. if (m_resizer.GetParent()) RemoveChild(&m_resizer);
  671. if (m_mover.GetParent()) RemoveChild(&m_mover);
  672. if (m_close_button.GetParent()) m_mover.RemoveChild(&m_close_button);
  673. if (m_redock_button.GetParent()) m_mover.RemoveChild(&m_redock_button);
  674. m_mover.RemoveChild(&m_textfield);
  675. }
  676. void TBDockWindow::SetDockOrigin( TBID dockid )
  677. {
  678. m_dockid = dockid;
  679. if ( m_dockid > 0 ) //enable/show the (re)dock button is a return id is specified
  680. {
  681. m_redock_button.SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
  682. }
  683. }
  684. TBWidget *TBDockWindow::GetEventDestination()
  685. {
  686. if ( m_dockid > 0 ) // if the origin is specified, send events back to there while the content is undocked
  687. { // so the original event handlers will still function.
  688. return GetParentRoot(true)->GetWidgetByID(m_dockid);
  689. }
  690. return GetParent();
  691. }
  692. void TBDockWindow::Show( TBWidget *host, int xpos, int ypos )
  693. {
  694. if ( host )
  695. {
  696. host->AddChild(this);
  697. SetPosition( TBPoint( xpos, ypos) );
  698. }
  699. }
  700. TBWidget *TBDockWindow::GetDockContent()
  701. {
  702. TBLayout *lo1 = GetWidgetByIDAndType<TBLayout>(TBID("undocklayout"));
  703. if ( lo1 )
  704. return lo1->GetChildFromIndex(0);
  705. }
  706. bool TBDockWindow::HasDockContent()
  707. {
  708. return ( GetDockContent() != NULL );
  709. }
  710. void TBDockWindow::Redock ()
  711. {
  712. if ( m_dockid > 0 ) // see if there is a dock point id specified
  713. {
  714. TBWidget *mytarget = GetParentRoot(true)->GetWidgetByID(m_dockid);
  715. if (mytarget)
  716. Dock (mytarget);
  717. }
  718. }
  719. void TBDockWindow::Dock ( TBWidget *target )
  720. {
  721. if ( !HasDockContent() ) return;
  722. if ( target ) // what kind of widget is it?
  723. {
  724. TBStr mystr;
  725. TBWidget *mypage = NULL;
  726. TBLayout *lo1 = NULL;
  727. TBTabContainer *tc1 = NULL;
  728. if ( tc1 = TBSafeCast<TBTabContainer>( target ) ) // handle TBTabContainer
  729. {
  730. mystr = GetText();
  731. mypage = GetDockContent();
  732. if (mypage)
  733. {
  734. TBButton *tbb = new TBButton();
  735. tbb->SetID (TBID(mystr));
  736. tbb->SetText(mystr);
  737. mypage->GetParent()->RemoveChild(mypage);
  738. tc1->GetContentRoot()->AddChild(mypage);
  739. tc1->GetTabLayout()->AddChild(tbb);
  740. tc1->Invalidate();
  741. tc1->SetCurrentPage(tc1->GetNumPages()-1);
  742. Close();
  743. }
  744. }
  745. }
  746. }
  747. bool TBDockWindow::OnEvent(const TBWidgetEvent &ev)
  748. {
  749. if (ev.target == &m_close_button)
  750. {
  751. if (ev.type == EVENT_TYPE_CLICK)
  752. Close();
  753. return true;
  754. }
  755. if (ev.target == &m_redock_button)
  756. {
  757. if (ev.type == EVENT_TYPE_CLICK)
  758. Redock();
  759. return true;
  760. }
  761. return TBWidget::OnEvent(ev);
  762. }
  763. void TBDockWindow::OnResized(int old_w, int old_h)
  764. {
  765. // Apply gravity on children
  766. TBWidget::OnResized(old_w, old_h);
  767. // Manually move our own decoration children
  768. // FIX: Put a layout in the TBMover so we can add things there nicely.
  769. int title_height = GetTitleHeight();
  770. if ( m_axis == AXIS_Y ) // default
  771. {
  772. m_mover.SetRect(TBRect(0, 0, GetRect().w, title_height));
  773. PreferredSize ps = m_resizer.GetPreferredSize();
  774. m_resizer.SetRect(TBRect(GetRect().w - ps.pref_w, GetRect().h - ps.pref_h, ps.pref_w, ps.pref_h));
  775. TBRect mover_rect = m_mover.GetPaddingRect();
  776. int button_size = mover_rect.h;
  777. m_close_button.SetRect(TBRect(mover_rect.x + mover_rect.w - button_size, mover_rect.y, button_size, button_size));
  778. if (m_settings & WINDOW_SETTINGS_CLOSE_BUTTON)
  779. mover_rect.w -= button_size;
  780. // add redock button to header
  781. m_redock_button.SetRect(TBRect(mover_rect.x + mover_rect.w - button_size, mover_rect.y, button_size, button_size));
  782. if (m_settings & WINDOW_SETTINGS_CLOSE_BUTTON)
  783. mover_rect.w -= button_size;
  784. m_textfield.SetRect(mover_rect);
  785. }
  786. else if ( m_axis == AXIS_X ) // rotated sideways
  787. {
  788. m_mover.SetRect(TBRect(0, 0, title_height, GetRect().h ));
  789. PreferredSize ps = m_resizer.GetPreferredSize();
  790. m_resizer.SetRect(TBRect(GetRect().w - ps.pref_w, GetRect().h - ps.pref_h, ps.pref_w, ps.pref_h));
  791. TBRect mover_rect = m_mover.GetPaddingRect();
  792. int button_size = mover_rect.w;
  793. m_close_button.SetRect(TBRect(mover_rect.x + 1, mover_rect.y + 1, button_size, button_size));
  794. if (m_settings & WINDOW_SETTINGS_CLOSE_BUTTON)
  795. mover_rect.y += button_size;
  796. m_redock_button.SetRect(TBRect(mover_rect.x + 1, mover_rect.y + 1, button_size, button_size));
  797. if (m_settings & WINDOW_SETTINGS_CLOSE_BUTTON)
  798. mover_rect.y -= button_size;
  799. m_textfield.SetRect(TBRect(mover_rect.x + 5, mover_rect.y + mover_rect.h - button_size, button_size - 1, button_size));
  800. }
  801. }
  802. // == MultiList Support ==========================================
  803. void MultiItem::AddColumn (TBStr widgettype, TBStr string, int width )
  804. {
  805. TBValue *new_str = colStr_.GetArray()->AddValue();
  806. new_str->SetString(string.CStr(), TBValue::SET_NEW_COPY );
  807. TBValue *new_widget = colWidget_.GetArray()->AddValue();
  808. new_widget->SetString( widgettype.CStr(), TBValue::SET_NEW_COPY );
  809. TBValue *new_width = colWidth_.GetArray()->AddValue();
  810. new_width->SetInt(width);
  811. }
  812. const char* MultiItem::GetColumnStr( int col )
  813. {
  814. const char* strx = colStr_.GetArray()->GetValue(col)->GetString();
  815. return strx;
  816. }
  817. const char* MultiItem::GetColumnWidget( int col )
  818. {
  819. const char* strx = colWidget_.GetArray()->GetValue(col)->GetString();
  820. return strx;
  821. }
  822. int MultiItem::GetColumnWidth( int col )
  823. {
  824. return colWidth_.GetArray()->GetValue(col)->GetInt();
  825. }
  826. int MultiItem::GetColumnHeight()
  827. {
  828. return colHeight_;
  829. }
  830. int MultiItem::GetNumColumns()
  831. {
  832. return colStr_.GetArrayLength();
  833. }
  834. bool MultiItemSource::Filter(int index, const char *filter)
  835. {
  836. // Override this method so we can return hits for our extra data too.
  837. if (TBSelectItemSource::Filter(index, filter))
  838. return true;
  839. // do this on ALL of the columns?
  840. return false;
  841. }
  842. TBWidget *MultiItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  843. {
  844. if (TBLayout *layout = new MultiItemWidget( GetItem(index), this, viewer, index))
  845. return layout;
  846. return NULL;
  847. }
  848. MultiItemWidget::MultiItemWidget(MultiItem *item, MultiItemSource *source, TBSelectItemViewer *source_viewer, int index)
  849. {
  850. SetSkinBg(TBIDC("TBSelectItem"));
  851. SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  852. SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP);
  853. SetPaintOverflowFadeout(false);
  854. if (item) // unpack the MultiItem recipe.
  855. {
  856. int col = 0;
  857. int numcols = item->GetNumColumns();
  858. int prefheight = item->GetColumnHeight();
  859. for ( col = 0; col < numcols; col++ )
  860. {
  861. TBStr widget;
  862. const char *widgetx = item->GetColumnWidget(col); // what are we making?
  863. if (widgetx) widget = TBStr(widgetx); // lets not deal with char *s
  864. if ( widget.Equals("TEXTC") ) // TextField center justified text
  865. {
  866. TBTextField *tfc = new TBTextField();
  867. tfc->SetTextAlign(TB_TEXT_ALIGN::TB_TEXT_ALIGN_CENTER);
  868. tfc->SetSqueezable(true);
  869. tfc->SetIgnoreInput(true);
  870. tfc->SetText( item->GetColumnStr(col));
  871. LayoutParams *lpx = new LayoutParams();
  872. lpx->SetWidth(item->GetColumnWidth(col));
  873. if ( prefheight > 0) lpx->SetHeight( prefheight);
  874. tfc->SetLayoutParams(*lpx);
  875. AddChild (tfc);
  876. }
  877. else if ( widget.Equals("TEXTR") ) // TextField right justified text
  878. {
  879. TBTextField *tfr = new TBTextField();
  880. tfr->SetTextAlign(TB_TEXT_ALIGN::TB_TEXT_ALIGN_RIGHT);
  881. tfr->SetSqueezable(true);
  882. tfr->SetIgnoreInput(true);
  883. tfr->SetText( item->GetColumnStr(col));
  884. LayoutParams *lpx = new LayoutParams();
  885. lpx->SetWidth(item->GetColumnWidth(col));
  886. if ( prefheight > 0) lpx->SetHeight( prefheight);
  887. tfr->SetLayoutParams(*lpx);
  888. AddChild (tfr);
  889. }
  890. else if ( widget.Equals("IMAGE") ) // ImageWidget + filename
  891. {
  892. TBImageWidget *ti = new TBImageWidget();
  893. ti->SetImage(item->GetColumnStr(col));
  894. ti->SetIgnoreInput(true);
  895. LayoutParams *lpx = new LayoutParams();
  896. lpx->SetWidth(item->GetColumnWidth(col));
  897. if ( prefheight > 0) lpx->SetHeight( prefheight);
  898. ti->SetLayoutParams(*lpx);
  899. AddChild (ti);
  900. }
  901. else if ( widget.Equals("COLOR") ) // ColorWidget
  902. {
  903. TBColorWidget *cw = new TBColorWidget();
  904. cw->SetColor(item->GetColumnStr(col));
  905. cw->SetIgnoreInput(true);
  906. cw->SetGravity(WIDGET_GRAVITY_ALL);
  907. LayoutParams *lpx = new LayoutParams();
  908. lpx->SetWidth(item->GetColumnWidth(col));
  909. if ( prefheight > 0) lpx->SetHeight( prefheight);
  910. cw->SetLayoutParams(*lpx);
  911. AddChild(cw);
  912. }
  913. else if ( widget.Equals("ICON") ) // SkinImage + skinname
  914. {
  915. const char *skin = item->GetColumnStr(col);
  916. if (skin)
  917. {
  918. TBID skinn = TBID(skin);
  919. TBSkinImage *si = new TBSkinImage(skinn);
  920. si->SetIgnoreInput(true);
  921. LayoutParams *lpx = new LayoutParams();
  922. lpx->SetWidth(item->GetColumnWidth(col));
  923. if (prefheight > 0) lpx->SetHeight(prefheight);
  924. si->SetLayoutParams(*lpx);
  925. AddChild (si);
  926. }
  927. }
  928. else // the default is TextField, left justified text
  929. {
  930. TBTextField *tfl = new TBTextField();
  931. tfl->SetTextAlign(TB_TEXT_ALIGN::TB_TEXT_ALIGN_LEFT);
  932. tfl->SetSqueezable(true);
  933. tfl->SetIgnoreInput(true);
  934. tfl->SetText( item->GetColumnStr(col));
  935. LayoutParams *lpx = new LayoutParams();
  936. lpx->SetWidth(item->GetColumnWidth(col));
  937. if ( prefheight > 0) lpx->SetHeight(prefheight);
  938. tfl->SetLayoutParams(*lpx);
  939. AddChild (tfl);
  940. }
  941. }
  942. }
  943. }
  944. bool MultiItemWidget::OnEvent(const TBWidgetEvent &ev)
  945. {
  946. // if there are active widgets (buttons, checkbox) handle the events here
  947. return TBLayout::OnEvent(ev);
  948. }
  949. }; // namespace tb