ComboBox.cpp 10.0 KB

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