tb_select.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. SelectItem(m_value, false);
  177. m_value = value;
  178. SelectItem(m_value, true);
  179. ScrollToSelectedItem();
  180. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  181. if (TBWidget *widget = GetItemWidget(m_value))
  182. ev.ref_id = widget->GetID();
  183. InvokeEvent(ev);
  184. }
  185. TBID TBSelectList::GetSelectedItemID()
  186. {
  187. if (m_source && m_value >= 0 && m_value < m_source->GetNumItems())
  188. return m_source->GetItemID(m_value);
  189. return TBID();
  190. }
  191. void TBSelectList::SelectItem(int index, bool selected)
  192. {
  193. if (TBWidget *widget = GetItemWidget(index))
  194. widget->SetState(WIDGET_STATE_SELECTED, selected);
  195. }
  196. TBWidget *TBSelectList::GetItemWidget(int index)
  197. {
  198. if (index == -1)
  199. return nullptr;
  200. for (TBWidget *tmp = m_layout.GetContentRoot()->GetFirstChild(); tmp; tmp = tmp->GetNext())
  201. if (tmp->data.GetInt() == index)
  202. return tmp;
  203. return nullptr;
  204. }
  205. void TBSelectList::ScrollToSelectedItem()
  206. {
  207. if (m_list_is_invalid)
  208. {
  209. m_scroll_to_current = true;
  210. return;
  211. }
  212. m_scroll_to_current = false;
  213. if (TBWidget *widget = GetItemWidget(m_value))
  214. m_container.ScrollIntoView(widget->GetRect());
  215. else
  216. m_container.ScrollTo(0, 0);
  217. }
  218. void TBSelectList::OnSkinChanged()
  219. {
  220. m_container.SetRect(GetPaddingRect());
  221. }
  222. void TBSelectList::OnProcess()
  223. {
  224. ValidateList();
  225. }
  226. void TBSelectList::OnProcessAfterChildren()
  227. {
  228. if (m_scroll_to_current)
  229. ScrollToSelectedItem();
  230. }
  231. bool TBSelectList::OnEvent(const TBWidgetEvent &ev)
  232. {
  233. if (ev.type == EVENT_TYPE_CLICK && ev.target->GetParent() == m_layout.GetContentRoot())
  234. {
  235. // SetValue (EVENT_TYPE_CHANGED) might cause something to delete this (f.ex closing
  236. // the dropdown menu. We want to sent another event, so ensure we're still around.
  237. TBWidgetSafePointer this_widget(this);
  238. int index = ev.target->data.GetInt();
  239. SetValue(index);
  240. // If we're still around, invoke the click event too.
  241. if (this_widget.Get())
  242. {
  243. TBSelectList *target_list = this;
  244. // If the parent window is a TBMenuWindow, we will iterate up the event destination
  245. // chain to find the top TBMenuWindow and invoke the event there.
  246. // That way events in submenus will reach the caller properly, and seem like it was
  247. // invoked on the top menu.
  248. TBWindow *window = GetParentWindow();
  249. while (TBMenuWindow *menu_win = TBSafeCast<TBMenuWindow>(window))
  250. {
  251. target_list = menu_win->GetList();
  252. window = menu_win->GetEventDestination()->GetParentWindow();
  253. }
  254. // Invoke the click event on the target list
  255. TBWidgetEvent ev(EVENT_TYPE_CLICK);
  256. if (TBWidget *widget = GetItemWidget(m_value))
  257. ev.ref_id = widget->GetID();
  258. target_list->InvokeEvent(ev);
  259. }
  260. return true;
  261. }
  262. else if (ev.type == EVENT_TYPE_KEY_DOWN)
  263. {
  264. if (ChangeValue(ev.special_key))
  265. return true;
  266. // Give the scroll container a chance to handle the key so it may
  267. // scroll. This matters if the list itself is focused instead of
  268. // some child view of any select item (since that would have passed
  269. // the container already)
  270. if (GetScrollContainer()->OnEvent(ev))
  271. return true;
  272. }
  273. return TBWidget::OnEvent(ev);
  274. }
  275. bool TBSelectList::ChangeValue(SPECIAL_KEY key)
  276. {
  277. if (!m_source || !m_layout.GetContentRoot()->GetFirstChild())
  278. return false;
  279. bool forward;
  280. if (key == TB_KEY_HOME || key == TB_KEY_DOWN)
  281. forward = true;
  282. else if (key == TB_KEY_END || key == TB_KEY_UP)
  283. forward = false;
  284. else
  285. return false;
  286. TBWidget *item_root = m_layout.GetContentRoot();
  287. TBWidget *current = GetItemWidget(m_value);
  288. TBWidget *origin = nullptr;
  289. if (key == TB_KEY_HOME || (!current && key == TB_KEY_DOWN))
  290. current = item_root->GetFirstChild();
  291. else if (key == TB_KEY_END || (!current && key == TB_KEY_UP))
  292. current = item_root->GetLastChild();
  293. else
  294. origin = current;
  295. while (current)
  296. {
  297. if (current != origin && !current->GetDisabled())
  298. break;
  299. current = forward ? current->GetNext() : current->GetPrev();
  300. }
  301. // Select and focus what we found
  302. if (current)
  303. {
  304. SetValue(current->data.GetInt());
  305. return true;
  306. }
  307. return false;
  308. }
  309. // == TBSelectDropdown ==========================================
  310. TBSelectDropdown::TBSelectDropdown()
  311. : m_value(-1)
  312. {
  313. SetSource(&m_default_source);
  314. SetSkinBg(TBIDC("TBSelectDropdown"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  315. m_arrow.SetSkinBg(TBIDC("TBSelectDropdown.arrow"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  316. GetContentRoot()->AddChild(&m_arrow);
  317. }
  318. TBSelectDropdown::~TBSelectDropdown()
  319. {
  320. GetContentRoot()->RemoveChild(&m_arrow);
  321. SetSource(nullptr);
  322. CloseWindow();
  323. }
  324. void TBSelectDropdown::OnSourceChanged()
  325. {
  326. m_value = -1;
  327. if (m_source && m_source->GetNumItems())
  328. SetValue(0);
  329. }
  330. void TBSelectDropdown::OnItemChanged(int index)
  331. {
  332. }
  333. void TBSelectDropdown::SetValue(int value)
  334. {
  335. if (value == m_value || !m_source)
  336. return;
  337. m_value = value;
  338. if (m_value < 0)
  339. SetText("");
  340. else if (m_value < m_source->GetNumItems())
  341. SetText(m_source->GetItemString(m_value));
  342. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  343. InvokeEvent(ev);
  344. }
  345. TBID TBSelectDropdown::GetSelectedItemID()
  346. {
  347. if (m_source && m_value >= 0 && m_value < m_source->GetNumItems())
  348. return m_source->GetItemID(m_value);
  349. return TBID();
  350. }
  351. void TBSelectDropdown::OpenWindow()
  352. {
  353. if (!m_source || !m_source->GetNumItems() || m_window_pointer.Get())
  354. return;
  355. if (TBMenuWindow *window = new TBMenuWindow(this, TBIDC("TBSelectDropdown.window")))
  356. {
  357. m_window_pointer.Set(window);
  358. window->SetSkinBg(TBIDC("TBSelectDropdown.window"));
  359. window->Show(m_source, TBPopupAlignment(), GetValue());
  360. }
  361. }
  362. void TBSelectDropdown::CloseWindow()
  363. {
  364. if (TBMenuWindow *window = GetMenuIfOpen())
  365. window->Close();
  366. }
  367. TBMenuWindow *TBSelectDropdown::GetMenuIfOpen() const
  368. {
  369. return TBSafeCast<TBMenuWindow>(m_window_pointer.Get());
  370. }
  371. bool TBSelectDropdown::OnEvent(const TBWidgetEvent &ev)
  372. {
  373. if (ev.target == this && ev.type == EVENT_TYPE_CLICK)
  374. {
  375. // Open the menu, or set the value and close it if already open (this will
  376. // happen when clicking by keyboard since that will call click on this button)
  377. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  378. {
  379. TBWidgetSafePointer tmp(this);
  380. int value = menu_window->GetList()->GetValue();
  381. menu_window->Die();
  382. if (tmp.Get())
  383. SetValue(value);
  384. }
  385. else
  386. OpenWindow();
  387. return true;
  388. }
  389. else if (ev.target->GetID() == TBIDC("TBSelectDropdown.window") && ev.type == EVENT_TYPE_CLICK)
  390. {
  391. // Set the value of the clicked item
  392. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  393. SetValue(menu_window->GetList()->GetValue());
  394. return true;
  395. }
  396. else if (ev.target == this && m_source && ev.IsKeyEvent())
  397. {
  398. if (TBMenuWindow *menu_window = GetMenuIfOpen())
  399. {
  400. // Redirect the key strokes to the list
  401. TBWidgetEvent redirected_ev(ev);
  402. return menu_window->GetList()->InvokeEvent(redirected_ev);
  403. }
  404. }
  405. return false;
  406. }
  407. }; // namespace tb