tb_select.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. ev.ref_id = TBIDC("select_list_selection_changed");
  212. // forward to delegate
  213. TBWidget::OnEvent(ev);
  214. }
  215. }
  216. }
  217. TBWidget *TBSelectList::GetItemWidget(int index)
  218. {
  219. if (index == -1)
  220. return nullptr;
  221. for (TBWidget *tmp = m_layout.GetContentRoot()->GetFirstChild(); tmp; tmp = tmp->GetNext())
  222. if (tmp->data.GetInt() == index)
  223. return tmp;
  224. return nullptr;
  225. }
  226. void TBSelectList::ScrollToSelectedItem()
  227. {
  228. if (m_list_is_invalid)
  229. {
  230. m_scroll_to_current = true;
  231. return;
  232. }
  233. m_scroll_to_current = false;
  234. if (TBWidget *widget = GetItemWidget(m_value))
  235. m_container.ScrollIntoView(widget->GetRect());
  236. else
  237. m_container.ScrollTo(0, 0);
  238. }
  239. void TBSelectList::OnSkinChanged()
  240. {
  241. m_container.SetRect(GetPaddingRect());
  242. }
  243. void TBSelectList::OnProcess()
  244. {
  245. ValidateList();
  246. }
  247. void TBSelectList::OnProcessAfterChildren()
  248. {
  249. if (m_scroll_to_current)
  250. ScrollToSelectedItem();
  251. }
  252. bool TBSelectList::OnEvent(const TBWidgetEvent &ev)
  253. {
  254. if (ev.type == EVENT_TYPE_CLICK && ev.target->GetParent() == m_layout.GetContentRoot())
  255. {
  256. // SetValue (EVENT_TYPE_CHANGED) might cause something to delete this (f.ex closing
  257. // the dropdown menu. We want to sent another event, so ensure we're still around.
  258. TBWidgetSafePointer this_widget(this);
  259. int index = ev.target->data.GetInt();
  260. if (ev.modifierkeys & TB_SHIFT || ev.modifierkeys & TB_CTRL || ev.modifierkeys & TB_SUPER)
  261. {
  262. if (GetItemSelected(index))
  263. {
  264. SelectItem(index, false);
  265. }
  266. else
  267. {
  268. SetValue(index);
  269. }
  270. }
  271. else
  272. {
  273. SelectAllItems(false);
  274. SetValue(index);
  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_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. SelectAllItems(false);
  341. SetValue(current->data.GetInt());
  342. return true;
  343. }
  344. return false;
  345. }
  346. bool TBSelectList::GetItemSelected(int index)
  347. {
  348. if (TBWidget *widget = GetItemWidget(index))
  349. return widget->GetState(WIDGET_STATE_SELECTED);
  350. return false;
  351. }
  352. void TBSelectList::SelectAllItems(bool select)
  353. {
  354. if (!m_source)
  355. return;
  356. for (int i = 0; i < m_source->GetNumItems(); i++)
  357. {
  358. SelectItem(i, select);
  359. }
  360. if (!select)
  361. m_value = -1;
  362. }
  363. // == TBSelectDropdown ==========================================
  364. TBSelectDropdown::TBSelectDropdown()
  365. : m_value(-1)
  366. {
  367. SetSource(&m_default_source);
  368. SetSkinBg(TBIDC("TBSelectDropdown"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  369. m_arrow.SetSkinBg(TBIDC("TBSelectDropdown.arrow"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  370. GetContentRoot()->AddChild(&m_arrow);
  371. }
  372. TBSelectDropdown::~TBSelectDropdown()
  373. {
  374. GetContentRoot()->RemoveChild(&m_arrow);
  375. SetSource(nullptr);
  376. CloseWindow();
  377. }
  378. void TBSelectDropdown::OnSourceChanged()
  379. {
  380. m_value = -1;
  381. if (m_source && m_source->GetNumItems())
  382. SetValue(0);
  383. }
  384. void TBSelectDropdown::OnItemChanged(int index)
  385. {
  386. }
  387. void TBSelectDropdown::SetValue(int value)
  388. {
  389. if (value == m_value || !m_source)
  390. return;
  391. m_value = value;
  392. if (m_value < 0)
  393. SetText("");
  394. else if (m_value < m_source->GetNumItems())
  395. SetText(m_source->GetItemString(m_value));
  396. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  397. InvokeEvent(ev);
  398. }
  399. TBID TBSelectDropdown::GetSelectedItemID()
  400. {
  401. if (m_source && m_value >= 0 && m_value < m_source->GetNumItems())
  402. return m_source->GetItemID(m_value);
  403. return TBID();
  404. }
  405. void TBSelectDropdown::OpenWindow()
  406. {
  407. if (!m_source || !m_source->GetNumItems() || m_window_pointer.Get())
  408. return;
  409. if (TBMenuWindow *window = new TBMenuWindow(this, TBIDC("TBSelectDropdown.window")))
  410. {
  411. m_window_pointer.Set(window);
  412. window->SetSkinBg(TBIDC("TBSelectDropdown.window"));
  413. window->Show(m_source, TBPopupAlignment(), GetValue());
  414. }
  415. }
  416. void TBSelectDropdown::CloseWindow()
  417. {
  418. if (TBMenuWindow *window = GetMenuIfOpen())
  419. window->Close();
  420. }
  421. TBMenuWindow *TBSelectDropdown::GetMenuIfOpen() const
  422. {
  423. return TBSafeCast<TBMenuWindow>(m_window_pointer.Get());
  424. }
  425. bool TBSelectDropdown::OnEvent(const TBWidgetEvent &ev)
  426. {
  427. if (ev.target == this && ev.type == EVENT_TYPE_CLICK)
  428. {
  429. // Open the menu, or set the value and close it if already open (this will
  430. // happen when clicking by keyboard since that will call click on this button)
  431. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  432. {
  433. TBWidgetSafePointer tmp(this);
  434. int value = menu_window->GetList()->GetValue();
  435. menu_window->Die();
  436. if (tmp.Get())
  437. SetValue(value);
  438. }
  439. else
  440. OpenWindow();
  441. return true;
  442. }
  443. else if (ev.target->GetID() == TBIDC("TBSelectDropdown.window") && ev.type == EVENT_TYPE_CLICK)
  444. {
  445. // Set the value of the clicked item
  446. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  447. SetValue(menu_window->GetList()->GetValue());
  448. return true;
  449. }
  450. else if (ev.target == this && m_source && ev.IsKeyEvent())
  451. {
  452. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  453. {
  454. // Redirect the key strokes to the list
  455. TBWidgetEvent redirected_ev(ev);
  456. return menu_window->GetList()->InvokeEvent(redirected_ev);
  457. }
  458. }
  459. return false;
  460. }
  461. } // namespace tb