ComboBox.cpp 9.3 KB

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