ComboBox.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Button.h>
  7. #include <LibGUI/ComboBox.h>
  8. #include <LibGUI/Desktop.h>
  9. #include <LibGUI/ListView.h>
  10. #include <LibGUI/Model.h>
  11. #include <LibGUI/Scrollbar.h>
  12. #include <LibGUI/TextBox.h>
  13. #include <LibGUI/Window.h>
  14. REGISTER_WIDGET(GUI, ComboBox)
  15. namespace GUI {
  16. class ComboBoxEditor final : public TextEditor {
  17. C_OBJECT(ComboBoxEditor);
  18. public:
  19. Function<void(int delta)> on_mousewheel;
  20. private:
  21. ComboBoxEditor()
  22. : TextEditor(TextEditor::SingleLine)
  23. {
  24. }
  25. virtual void mousewheel_event(MouseEvent& event) override
  26. {
  27. if (!is_focused())
  28. set_focus(true);
  29. if (on_mousewheel)
  30. on_mousewheel(event.wheel_delta());
  31. }
  32. };
  33. ComboBox::ComboBox()
  34. {
  35. REGISTER_STRING_PROPERTY("placeholder", editor_placeholder, set_editor_placeholder);
  36. REGISTER_BOOL_PROPERTY("model_only", only_allow_values_from_model, set_only_allow_values_from_model);
  37. set_min_width(32);
  38. set_fixed_height(22);
  39. m_editor = add<ComboBoxEditor>();
  40. m_editor->set_frame_thickness(0);
  41. m_editor->on_return_pressed = [this] {
  42. if (on_return_pressed)
  43. on_return_pressed();
  44. };
  45. m_editor->on_up_pressed = [this] {
  46. navigate(AbstractView::CursorMovement::Up);
  47. };
  48. m_editor->on_down_pressed = [this] {
  49. navigate(AbstractView::CursorMovement::Down);
  50. };
  51. m_editor->on_pageup_pressed = [this] {
  52. navigate(AbstractView::CursorMovement::PageUp);
  53. };
  54. m_editor->on_pagedown_pressed = [this] {
  55. navigate(AbstractView::CursorMovement::PageDown);
  56. };
  57. m_editor->on_mousewheel = [this](int delta) {
  58. // Since we can only show one item at a time we don't want to
  59. // skip any. So just move one item at a time.
  60. navigate_relative(delta > 0 ? 1 : -1);
  61. };
  62. m_editor->on_mousedown = [this] {
  63. if (only_allow_values_from_model())
  64. m_open_button->click();
  65. };
  66. m_open_button = add<Button>();
  67. m_open_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png"));
  68. m_open_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
  69. m_open_button->on_click = [this](auto) {
  70. if (m_list_window->is_visible())
  71. close();
  72. else
  73. open();
  74. };
  75. m_list_window = add<Window>(window());
  76. m_list_window->set_frameless(true);
  77. m_list_window->set_accessory(true);
  78. m_list_window->on_active_input_change = [this](bool is_active_input) {
  79. if (!is_active_input) {
  80. m_open_button->set_enabled(false);
  81. close();
  82. }
  83. m_open_button->set_enabled(true);
  84. };
  85. m_list_view = m_list_window->set_main_widget<ListView>();
  86. m_list_view->horizontal_scrollbar().set_visible(false);
  87. m_list_view->set_alternating_row_colors(false);
  88. m_list_view->set_hover_highlighting(true);
  89. m_list_view->set_frame_thickness(1);
  90. m_list_view->set_frame_shadow(Gfx::FrameShadow::Plain);
  91. m_list_view->on_selection = [this](auto& index) {
  92. VERIFY(model());
  93. m_list_view->set_activates_on_selection(true);
  94. if (m_updating_model)
  95. selection_updated(index);
  96. };
  97. m_list_view->on_activation = [this](auto& index) {
  98. deferred_invoke([this, index](auto&) {
  99. selection_updated(index);
  100. if (on_change)
  101. on_change(m_editor->text(), index);
  102. });
  103. m_list_view->set_activates_on_selection(false);
  104. close();
  105. };
  106. m_list_view->on_escape_pressed = [this] {
  107. close();
  108. };
  109. }
  110. ComboBox::~ComboBox()
  111. {
  112. }
  113. void ComboBox::set_editor_placeholder(const StringView& placeholder)
  114. {
  115. m_editor->set_placeholder(placeholder);
  116. }
  117. const String& ComboBox::editor_placeholder() const
  118. {
  119. return m_editor->placeholder();
  120. }
  121. void ComboBox::navigate(AbstractView::CursorMovement cursor_movement)
  122. {
  123. auto previous_selected = m_list_view->cursor_index();
  124. m_list_view->move_cursor(cursor_movement, AbstractView::SelectionUpdate::Set);
  125. auto current_selected = m_list_view->cursor_index();
  126. selection_updated(current_selected);
  127. if (previous_selected.row() != current_selected.row() && on_change)
  128. on_change(m_editor->text(), current_selected);
  129. }
  130. void ComboBox::navigate_relative(int delta)
  131. {
  132. auto previous_selected = m_list_view->cursor_index();
  133. m_list_view->move_cursor_relative(delta, AbstractView::SelectionUpdate::Set);
  134. auto current_selected = m_list_view->cursor_index();
  135. selection_updated(current_selected);
  136. if (previous_selected.row() != current_selected.row() && on_change)
  137. on_change(m_editor->text(), current_selected);
  138. }
  139. void ComboBox::selection_updated(const ModelIndex& index)
  140. {
  141. if (index.is_valid())
  142. m_selected_index = index;
  143. else
  144. m_selected_index.clear();
  145. auto new_value = index.data().to_string();
  146. m_editor->set_text(new_value);
  147. if (!m_only_allow_values_from_model)
  148. m_editor->select_all();
  149. }
  150. void ComboBox::resize_event(ResizeEvent& event)
  151. {
  152. Widget::resize_event(event);
  153. int button_height = event.size().height() - frame_thickness() * 2;
  154. int button_width = 15;
  155. m_open_button->set_relative_rect(width() - button_width - frame_thickness(), frame_thickness(), button_width, button_height);
  156. auto editor_rect = frame_inner_rect();
  157. editor_rect.set_width(editor_rect.width() - button_width);
  158. m_editor->set_relative_rect(editor_rect);
  159. }
  160. void ComboBox::set_model(NonnullRefPtr<Model> model)
  161. {
  162. TemporaryChange change(m_updating_model, true);
  163. m_selected_index.clear();
  164. m_list_view->set_model(move(model));
  165. }
  166. void ComboBox::set_selected_index(size_t index)
  167. {
  168. if (!m_list_view->model())
  169. return;
  170. TemporaryChange change(m_updating_model, true);
  171. m_list_view->set_cursor(m_list_view->model()->index(index, 0), AbstractView::SelectionUpdate::Set);
  172. }
  173. size_t ComboBox::selected_index() const
  174. {
  175. return m_selected_index.has_value() ? m_selected_index.value().row() : 0;
  176. }
  177. void ComboBox::select_all()
  178. {
  179. m_editor->select_all();
  180. }
  181. void ComboBox::open()
  182. {
  183. if (!model())
  184. return;
  185. auto my_screen_rect = screen_relative_rect();
  186. int longest_item_width = 0;
  187. for (int i = 0; i < model()->row_count(); ++i) {
  188. auto index = model()->index(i);
  189. auto item_text = index.data().to_string();
  190. longest_item_width = max(longest_item_width, m_list_view->font().width(item_text));
  191. }
  192. Gfx::IntSize size {
  193. max(width(), longest_item_width + m_list_view->width_occupied_by_vertical_scrollbar() + m_list_view->frame_thickness() * 2 + m_list_view->horizontal_padding()),
  194. model()->row_count() * m_list_view->item_height() + m_list_view->frame_thickness() * 2
  195. };
  196. auto taskbar_height = GUI::Desktop::the().taskbar_height();
  197. // NOTE: This is so the combobox bottom edge exactly fits the taskbar's
  198. // top edge - the value was found through trial and error though.
  199. auto offset = 8;
  200. Gfx::IntRect list_window_rect { my_screen_rect.bottom_left(), size };
  201. list_window_rect.intersect(Desktop::the().rect().shrunken(0, taskbar_height + offset));
  202. m_editor->set_focus(true);
  203. if (m_selected_index.has_value()) {
  204. // Don't set m_updating_model to true here because we only want to
  205. // change the list view's selected item without triggering a change to it.
  206. m_list_view->set_cursor(m_selected_index.value(), AbstractView::SelectionUpdate::Set);
  207. }
  208. // Set the minimum minimum height of the list window to the height of three
  209. // items or the row count, whichever is smaller, plus the frame thickness.
  210. // This prevents the list from becoming infinitesimally small when pushed
  211. // up against the screen edge.
  212. m_list_window->set_minimum_size(1, min(3, model()->row_count()) * m_list_view->item_height() + m_list_view->frame_thickness() * 2);
  213. m_list_window->set_rect(list_window_rect);
  214. m_list_window->show();
  215. }
  216. void ComboBox::close()
  217. {
  218. m_list_window->hide();
  219. }
  220. String ComboBox::text() const
  221. {
  222. return m_editor->text();
  223. }
  224. void ComboBox::set_text(const String& text)
  225. {
  226. m_editor->set_text(text);
  227. }
  228. void ComboBox::set_only_allow_values_from_model(bool b)
  229. {
  230. if (m_only_allow_values_from_model == b)
  231. return;
  232. m_only_allow_values_from_model = b;
  233. m_editor->set_mode(m_only_allow_values_from_model ? TextEditor::DisplayOnly : TextEditor::Editable);
  234. }
  235. Model* ComboBox::model()
  236. {
  237. return m_list_view->model();
  238. }
  239. const Model* ComboBox::model() const
  240. {
  241. return m_list_view->model();
  242. }
  243. int ComboBox::model_column() const
  244. {
  245. return m_list_view->model_column();
  246. }
  247. void ComboBox::set_model_column(int column)
  248. {
  249. m_list_view->set_model_column(column);
  250. }
  251. }