tb_select.cpp 12 KB

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