tb_select.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. #include "tb_select.h"
  6. #include "tb_menu_window.h"
  7. #include "tb_widgets_listener.h"
  8. #include "tb_language.h"
  9. #include "tb_tempbuffer.h"
  10. #include "tb_sort.h"
  11. namespace tb {
  12. // == Sort callback for sorting items ===================================================
  13. static int select_list_sort_cb(TBSelectItemSource *source, const int *a, const int *b)
  14. {
  15. int value = strcmp(source->GetItemString(*a), source->GetItemString(*b));
  16. return source->GetSort() == TB_SORT_DESCENDING ? -value : value;
  17. }
  18. // == TBSelectList ==============================================
  19. TBSelectList::TBSelectList()
  20. : m_value(-1)
  21. , m_list_is_invalid(false)
  22. , m_scroll_to_current(false)
  23. , m_header_lng_string_id(TBIDC("TBList.header"))
  24. , m_sort_callback(select_list_sort_cb)
  25. {
  26. SetSource(&m_default_source);
  27. SetIsFocusable(true);
  28. SetSkinBg(TBIDC("TBSelectList"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  29. m_container.SetGravity(WIDGET_GRAVITY_ALL);
  30. m_container.SetRect(GetPaddingRect());
  31. AddChild(&m_container);
  32. m_layout.SetGravity(WIDGET_GRAVITY_ALL);
  33. m_layout.SetAxis(AXIS_Y);
  34. m_layout.SetSpacing(0);
  35. m_layout.SetLayoutPosition(LAYOUT_POSITION_LEFT_TOP);
  36. m_layout.SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP);
  37. m_layout.SetLayoutSize(LAYOUT_SIZE_AVAILABLE);
  38. m_container.GetContentRoot()->AddChild(&m_layout);
  39. m_container.SetScrollMode(SCROLL_MODE_Y_AUTO);
  40. m_container.SetAdaptContentSize(true);
  41. }
  42. TBSelectList::~TBSelectList()
  43. {
  44. m_container.GetContentRoot()->RemoveChild(&m_layout);
  45. RemoveChild(&m_container);
  46. SetSource(nullptr);
  47. }
  48. void TBSelectList::OnSourceChanged()
  49. {
  50. InvalidateList();
  51. }
  52. void TBSelectList::OnItemChanged(int index)
  53. {
  54. if (m_list_is_invalid) // We're updating all widgets soon.
  55. return;
  56. TBWidget *old_widget = GetItemWidget(index);
  57. if (!old_widget) // We don't have this widget so we have nothing to update.
  58. return;
  59. // Replace the old widget representing the item, with a new one. Preserve its state.
  60. WIDGET_STATE old_state = old_widget->GetStateRaw();
  61. if (TBWidget *widget = CreateAndAddItemAfter(index, old_widget))
  62. widget->SetStateRaw(old_state);
  63. old_widget->GetParent()->RemoveChild(old_widget);
  64. delete old_widget;
  65. }
  66. void TBSelectList::OnItemAdded(int index)
  67. {
  68. if (m_list_is_invalid) // We're updating all widgets soon.
  69. return;
  70. // Sorting, filtering etc. makes it messy to handle dynamic addition of items.
  71. // Resort to invalidate the entire list (may even be faster anyway)
  72. InvalidateList();
  73. }
  74. void TBSelectList::OnItemRemoved(int index)
  75. {
  76. if (m_list_is_invalid) // We're updating all widgets soon.
  77. return;
  78. // Sorting, filtering etc. makes it messy to handle dynamic addition of items.
  79. // Resort to invalidate the entire list (may even be faster anyway)
  80. InvalidateList();
  81. }
  82. void TBSelectList::OnAllItemsRemoved()
  83. {
  84. InvalidateList();
  85. m_value = -1;
  86. }
  87. void TBSelectList::SetFilter(const char *filter)
  88. {
  89. TBStr new_filter;
  90. if (filter && *filter)
  91. new_filter.Set(filter);
  92. if (m_filter.Equals(new_filter))
  93. return;
  94. m_filter.Set(new_filter);
  95. InvalidateList();
  96. }
  97. void TBSelectList::SetHeaderString(const TBID& id)
  98. {
  99. if (m_header_lng_string_id == id)
  100. return;
  101. m_header_lng_string_id = id;
  102. InvalidateList();
  103. }
  104. void TBSelectList::InvalidateList()
  105. {
  106. if (m_list_is_invalid)
  107. return;
  108. m_list_is_invalid = true;
  109. Invalidate();
  110. }
  111. void TBSelectList::ValidateList()
  112. {
  113. if (!m_list_is_invalid)
  114. return;
  115. m_list_is_invalid = false;
  116. // FIX: Could delete and create only the changed items (faster filter change)
  117. // Remove old items
  118. while (TBWidget *child = m_layout.GetContentRoot()->GetFirstChild())
  119. {
  120. child->GetParent()->RemoveChild(child);
  121. delete child;
  122. }
  123. if (!m_source || !m_source->GetNumItems())
  124. return;
  125. // Create a sorted list of the items we should include using the current filter.
  126. TBTempBuffer sort_buf;
  127. if (!sort_buf.Reserve(m_source->GetNumItems() * sizeof(int)))
  128. return; // Out of memory
  129. int *sorted_index = (int *) sort_buf.GetData();
  130. // Populate the sorted index list
  131. int num_sorted_items = 0;
  132. for (int i = 0; i < m_source->GetNumItems(); i++)
  133. if (m_filter.IsEmpty() || m_source->Filter(i, m_filter))
  134. sorted_index[num_sorted_items++] = i;
  135. // Sort
  136. if (m_source->GetSort() != TB_SORT_NONE)
  137. insertion_sort<TBSelectItemSource*, int>(sorted_index, num_sorted_items, m_source, m_sort_callback);
  138. // Show header if we only show a subset of all items.
  139. // a single space filter is used as a dummy filter
  140. if (!m_filter.IsEmpty() && (!(m_filter.Length() == 1 && m_filter[0] == ' ')))
  141. {
  142. if (TBWidget *widget = new TBTextField())
  143. {
  144. TBStr str;
  145. str.SetFormatted(g_tb_lng->GetString(m_header_lng_string_id), num_sorted_items, m_source->GetNumItems());
  146. widget->SetText(str);
  147. widget->SetSkinBg(TBIDC("TBList.header"));
  148. widget->SetState(WIDGET_STATE_DISABLED, true);
  149. widget->SetGravity(WIDGET_GRAVITY_ALL);
  150. widget->data.SetInt(-1);
  151. m_layout.GetContentRoot()->AddChild(widget);
  152. }
  153. }
  154. // Create new items
  155. for (int i = 0; i < num_sorted_items; i++)
  156. CreateAndAddItemAfter(sorted_index[i], nullptr);
  157. SelectItem(m_value, true);
  158. // FIX: Should not scroll just because we update the list. Only automatically first time!
  159. m_scroll_to_current = true;
  160. }
  161. TBWidget *TBSelectList::CreateAndAddItemAfter(int index, TBWidget *reference)
  162. {
  163. if (TBWidget *widget = m_source->CreateItemWidget(index, this))
  164. {
  165. // Use item data as widget to index lookup
  166. widget->data.SetInt(index);
  167. m_layout.GetContentRoot()->AddChildRelative(widget, WIDGET_Z_REL_AFTER, reference);
  168. return widget;
  169. }
  170. return nullptr;
  171. }
  172. void TBSelectList::SetValue(int value)
  173. {
  174. if (value == m_value)
  175. return;
  176. m_value = value;
  177. SelectItem(m_value, true);
  178. ScrollToSelectedItem();
  179. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  180. if (TBWidget *widget = GetItemWidget(m_value))
  181. ev.ref_id = widget->GetID();
  182. InvokeEvent(ev);
  183. }
  184. TBID TBSelectList::GetItemID(int index) const
  185. {
  186. if (!m_source)
  187. return TBID();
  188. return m_source->GetItemID(index);
  189. }
  190. int TBSelectList::GetNumItems() const
  191. {
  192. if (!m_source)
  193. return 0;
  194. return m_source->GetNumItems();
  195. }
  196. TBID TBSelectList::GetSelectedItemID()
  197. {
  198. if (m_source && m_value >= 0 && m_value < m_source->GetNumItems())
  199. return m_source->GetItemID(m_value);
  200. return TBID();
  201. }
  202. void TBSelectList::SelectItem(int index, bool selected)
  203. {
  204. if (TBWidget *widget = GetItemWidget(index))
  205. {
  206. bool changed = widget->GetState(WIDGET_STATE_SELECTED) != selected;
  207. widget->SetState(WIDGET_STATE_SELECTED, selected);
  208. if (changed)
  209. {
  210. TBWidgetEvent ev(EVENT_TYPE_CUSTOM);
  211. // TBIDC does not register the string with the UI system
  212. TBID refid("select_list_selection_changed");
  213. ev.ref_id = refid;
  214. // forward to delegate
  215. TBWidget::OnEvent(ev);
  216. }
  217. }
  218. }
  219. TBWidget *TBSelectList::GetItemWidget(int index)
  220. {
  221. if (index == -1)
  222. return nullptr;
  223. for (TBWidget *tmp = m_layout.GetContentRoot()->GetFirstChild(); tmp; tmp = tmp->GetNext())
  224. if (tmp->data.GetInt() == index)
  225. return tmp;
  226. return nullptr;
  227. }
  228. void TBSelectList::ScrollToSelectedItem()
  229. {
  230. if (m_list_is_invalid)
  231. {
  232. m_scroll_to_current = true;
  233. return;
  234. }
  235. m_scroll_to_current = false;
  236. if (TBWidget *widget = GetItemWidget(m_value))
  237. m_container.ScrollIntoView(widget->GetRect());
  238. else
  239. m_container.ScrollTo(0, 0);
  240. }
  241. void TBSelectList::OnSkinChanged()
  242. {
  243. m_container.SetRect(GetPaddingRect());
  244. }
  245. void TBSelectList::OnProcess()
  246. {
  247. ValidateList();
  248. }
  249. void TBSelectList::OnProcessAfterChildren()
  250. {
  251. if (m_scroll_to_current)
  252. ScrollToSelectedItem();
  253. }
  254. bool TBSelectList::OnEvent(const TBWidgetEvent &ev)
  255. {
  256. if (ev.type == EVENT_TYPE_CLICK && ev.target->GetParent() == m_layout.GetContentRoot())
  257. {
  258. // SetValue (EVENT_TYPE_CHANGED) might cause something to delete this (f.ex closing
  259. // the dropdown menu. We want to sent another event, so ensure we're still around.
  260. TBWidgetSafePointer this_widget(this);
  261. int index = ev.target->data.GetInt();
  262. if (ev.modifierkeys & TB_SHIFT || ev.modifierkeys & TB_CTRL || ev.modifierkeys & TB_SUPER)
  263. {
  264. if (GetItemSelected(index))
  265. {
  266. SelectItem(index, false);
  267. }
  268. else
  269. {
  270. SetValue(index);
  271. SelectItem(index, true);
  272. }
  273. }
  274. else
  275. {
  276. SelectAllItems(false);
  277. SetValue(index);
  278. }
  279. // If we're still around, invoke the click event too.
  280. if (this_widget.Get())
  281. {
  282. TBSelectList *target_list = this;
  283. // If the parent window is a TBMenuWindow, we will iterate up the event destination
  284. // chain to find the top TBMenuWindow and invoke the event there.
  285. // That way events in submenus will reach the caller properly, and seem like it was
  286. // invoked on the top menu.
  287. TBWindow *window = GetParentWindow();
  288. while (TBMenuWindow *menu_win = TBSafeCast<TBMenuWindow>(window))
  289. {
  290. target_list = menu_win->GetList();
  291. window = menu_win->GetEventDestination()->GetParentWindow();
  292. }
  293. // Invoke the click event on the target list
  294. TBWidgetEvent ev(EVENT_TYPE_CLICK);
  295. if (TBWidget *widget = GetItemWidget(m_value))
  296. ev.ref_id = widget->GetID();
  297. target_list->InvokeEvent(ev);
  298. }
  299. return true;
  300. }
  301. else if (ev.type == EVENT_TYPE_KEY_DOWN)
  302. {
  303. if (ChangeValue(ev.special_key))
  304. return true;
  305. // Give the scroll container a chance to handle the key so it may
  306. // scroll. This matters if the list itself is focused instead of
  307. // some child view of any select item (since that would have passed
  308. // the container already)
  309. if (GetScrollContainer()->OnEvent(ev))
  310. return true;
  311. }
  312. return TBWidget::OnEvent(ev);
  313. }
  314. bool TBSelectList::ChangeValue(SPECIAL_KEY key)
  315. {
  316. if (!m_source || !m_layout.GetContentRoot()->GetFirstChild())
  317. return false;
  318. bool forward;
  319. if (key == TB_KEY_HOME || key == TB_KEY_DOWN)
  320. forward = true;
  321. else if (key == TB_KEY_END || key == TB_KEY_UP)
  322. forward = false;
  323. else
  324. return false;
  325. TBWidget *item_root = m_layout.GetContentRoot();
  326. TBWidget *current = GetItemWidget(m_value);
  327. TBWidget *origin = nullptr;
  328. if (key == TB_KEY_HOME || (!current && key == TB_KEY_DOWN))
  329. current = item_root->GetFirstChild();
  330. else if (key == TB_KEY_END || (!current && key == TB_KEY_UP))
  331. current = item_root->GetLastChild();
  332. else
  333. origin = current;
  334. while (current)
  335. {
  336. if (current != origin && !current->GetDisabled())
  337. break;
  338. current = forward ? current->GetNext() : current->GetPrev();
  339. }
  340. // Select and focus what we found
  341. if (current)
  342. {
  343. SelectAllItems(false);
  344. SetValue(current->data.GetInt());
  345. return true;
  346. }
  347. return false;
  348. }
  349. bool TBSelectList::GetItemSelected(int index)
  350. {
  351. if (TBWidget *widget = GetItemWidget(index))
  352. return widget->GetState(WIDGET_STATE_SELECTED);
  353. return false;
  354. }
  355. void TBSelectList::SelectAllItems(bool select)
  356. {
  357. if (!m_source)
  358. return;
  359. for (int i = 0; i < m_source->GetNumItems(); i++)
  360. {
  361. SelectItem(i, select);
  362. }
  363. if (!select)
  364. m_value = -1;
  365. }
  366. // == TBSelectDropdown ==========================================
  367. TBSelectDropdown::TBSelectDropdown()
  368. : m_value(-1)
  369. {
  370. SetSource(&m_default_source);
  371. SetSkinBg(TBIDC("TBSelectDropdown"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  372. m_arrow.SetSkinBg(TBIDC("TBSelectDropdown.arrow"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  373. GetContentRoot()->AddChild(&m_arrow);
  374. }
  375. TBSelectDropdown::~TBSelectDropdown()
  376. {
  377. GetContentRoot()->RemoveChild(&m_arrow);
  378. SetSource(nullptr);
  379. CloseWindow();
  380. }
  381. void TBSelectDropdown::OnSourceChanged()
  382. {
  383. m_value = -1;
  384. if (m_source && m_source->GetNumItems())
  385. SetValue(0);
  386. }
  387. void TBSelectDropdown::OnItemChanged(int index)
  388. {
  389. }
  390. void TBSelectDropdown::SetValue(int value)
  391. {
  392. if (value == m_value || !m_source)
  393. return;
  394. m_value = value;
  395. if (m_value < 0)
  396. SetText("");
  397. else if (m_value < m_source->GetNumItems())
  398. SetText(m_source->GetItemString(m_value));
  399. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  400. InvokeEvent(ev);
  401. }
  402. TBID TBSelectDropdown::GetSelectedItemID()
  403. {
  404. if (m_source && m_value >= 0 && m_value < m_source->GetNumItems())
  405. return m_source->GetItemID(m_value);
  406. return TBID();
  407. }
  408. void TBSelectDropdown::OpenWindow()
  409. {
  410. if (!m_source || !m_source->GetNumItems() || m_window_pointer.Get())
  411. return;
  412. if (TBMenuWindow *window = new TBMenuWindow(this, TBIDC("TBSelectDropdown.window")))
  413. {
  414. m_window_pointer.Set(window);
  415. window->SetSkinBg(TBIDC("TBSelectDropdown.window"));
  416. window->Show(m_source, TBPopupAlignment(), GetValue());
  417. }
  418. }
  419. void TBSelectDropdown::CloseWindow()
  420. {
  421. if (TBMenuWindow *window = GetMenuIfOpen())
  422. window->Close();
  423. }
  424. TBMenuWindow *TBSelectDropdown::GetMenuIfOpen() const
  425. {
  426. return TBSafeCast<TBMenuWindow>(m_window_pointer.Get());
  427. }
  428. bool TBSelectDropdown::OnEvent(const TBWidgetEvent &ev)
  429. {
  430. if (ev.target == this && ev.type == EVENT_TYPE_CLICK)
  431. {
  432. // Open the menu, or set the value and close it if already open (this will
  433. // happen when clicking by keyboard since that will call click on this button)
  434. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  435. {
  436. TBWidgetSafePointer tmp(this);
  437. int value = menu_window->GetList()->GetValue();
  438. menu_window->Die();
  439. if (tmp.Get())
  440. SetValue(value);
  441. }
  442. else
  443. OpenWindow();
  444. return true;
  445. }
  446. else if (ev.target->GetID() == TBIDC("TBSelectDropdown.window") && ev.type == EVENT_TYPE_CLICK)
  447. {
  448. // Set the value of the clicked item
  449. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  450. SetValue(menu_window->GetList()->GetValue());
  451. return true;
  452. }
  453. else if (ev.target == this && m_source && ev.IsKeyEvent())
  454. {
  455. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  456. {
  457. // Redirect the key strokes to the list
  458. TBWidgetEvent redirected_ev(ev);
  459. return menu_window->GetList()->InvokeEvent(redirected_ev);
  460. }
  461. }
  462. return false;
  463. }
  464. } // namespace tb