tb_select.cpp 15 KB

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