ComboBox.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/ComboBox.h>
  27. #include <LibGUI/ControlBoxButton.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. namespace GUI {
  35. class ComboBoxEditor final : public TextEditor {
  36. C_OBJECT(ComboBoxEditor);
  37. public:
  38. Function<void(int delta)> on_mousewheel;
  39. private:
  40. ComboBoxEditor()
  41. : TextEditor(TextEditor::SingleLine)
  42. {
  43. }
  44. virtual void mousewheel_event(MouseEvent& event) override
  45. {
  46. if (!is_focused())
  47. set_focus(true);
  48. if (on_mousewheel)
  49. on_mousewheel(event.wheel_delta());
  50. }
  51. };
  52. ComboBox::ComboBox()
  53. {
  54. m_editor = add<ComboBoxEditor>();
  55. m_editor->set_has_open_button(true);
  56. m_editor->on_change = [this] {
  57. if (on_change)
  58. on_change(m_editor->text(), m_list_view->cursor_index());
  59. };
  60. m_editor->on_return_pressed = [this] {
  61. if (on_return_pressed)
  62. on_return_pressed();
  63. };
  64. m_editor->on_up_pressed = [this] {
  65. m_list_view->move_cursor(AbstractView::CursorMovement::Up, AbstractView::SelectionUpdate::Set);
  66. };
  67. m_editor->on_down_pressed = [this] {
  68. m_list_view->move_cursor(AbstractView::CursorMovement::Down, AbstractView::SelectionUpdate::Set);
  69. };
  70. m_editor->on_pageup_pressed = [this] {
  71. m_list_view->move_cursor(AbstractView::CursorMovement::PageUp, AbstractView::SelectionUpdate::Set);
  72. };
  73. m_editor->on_pagedown_pressed = [this] {
  74. m_list_view->move_cursor(AbstractView::CursorMovement::PageDown, AbstractView::SelectionUpdate::Set);
  75. };
  76. m_editor->on_mousewheel = [this](int delta) {
  77. m_list_view->move_cursor_relative(delta, AbstractView::SelectionUpdate::Set);
  78. };
  79. m_editor->on_mousedown = [this] {
  80. if (only_allow_values_from_model())
  81. m_open_button->click();
  82. };
  83. m_open_button = add<ControlBoxButton>(ControlBoxButton::DownArrow);
  84. m_open_button->set_focusable(false);
  85. m_open_button->on_click = [this](auto) {
  86. if (m_list_window->is_visible())
  87. close();
  88. else
  89. open();
  90. };
  91. m_list_window = add<Window>(window());
  92. m_list_window->set_frameless(true);
  93. m_list_window->set_accessory(true);
  94. m_list_window->on_active_input_change = [this](bool is_active_input) {
  95. if (!is_active_input) {
  96. m_open_button->set_enabled(false);
  97. close();
  98. }
  99. m_open_button->set_enabled(true);
  100. };
  101. m_list_view = m_list_window->set_main_widget<ListView>();
  102. m_list_view->horizontal_scrollbar().set_visible(false);
  103. m_list_view->set_alternating_row_colors(false);
  104. m_list_view->set_hover_highlighting(true);
  105. m_list_view->set_frame_thickness(1);
  106. m_list_view->set_frame_shadow(Gfx::FrameShadow::Plain);
  107. m_list_view->on_selection = [this](auto& index) {
  108. ASSERT(model());
  109. m_list_view->set_activates_on_selection(true);
  110. auto new_value = index.data().to_string();
  111. m_editor->set_text(new_value);
  112. if (!m_only_allow_values_from_model)
  113. m_editor->select_all();
  114. deferred_invoke([this, index](auto&) {
  115. if (on_change)
  116. on_change(m_editor->text(), index);
  117. });
  118. };
  119. m_list_view->on_activation = [this](auto&) {
  120. m_list_view->set_activates_on_selection(false);
  121. close();
  122. };
  123. m_list_view->on_escape_pressed = [this] {
  124. close();
  125. };
  126. }
  127. ComboBox::~ComboBox()
  128. {
  129. }
  130. void ComboBox::resize_event(ResizeEvent& event)
  131. {
  132. int frame_thickness = m_editor->frame_thickness();
  133. int button_height = event.size().height() - frame_thickness * 2;
  134. int button_width = 15;
  135. m_open_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness, button_width, button_height);
  136. m_editor->set_relative_rect(0, 0, width(), height());
  137. }
  138. void ComboBox::set_model(NonnullRefPtr<Model> model)
  139. {
  140. m_list_view->set_model(move(model));
  141. }
  142. void ComboBox::set_selected_index(size_t index)
  143. {
  144. if (!m_list_view->model())
  145. return;
  146. m_list_view->set_cursor(m_list_view->model()->index(index, 0), AbstractView::SelectionUpdate::Set);
  147. }
  148. size_t ComboBox::selected_index() const
  149. {
  150. return m_list_view->cursor_index().row();
  151. }
  152. void ComboBox::select_all()
  153. {
  154. m_editor->select_all();
  155. }
  156. void ComboBox::open()
  157. {
  158. if (!model())
  159. return;
  160. auto my_screen_rect = screen_relative_rect();
  161. int longest_item_width = 0;
  162. for (int i = 0; i < model()->row_count(); ++i) {
  163. auto index = model()->index(i);
  164. auto item_text = index.data().to_string();
  165. longest_item_width = max(longest_item_width, m_list_view->font().width(item_text));
  166. }
  167. Gfx::IntSize size {
  168. max(width(), longest_item_width + m_list_view->width_occupied_by_vertical_scrollbar() + m_list_view->frame_thickness() * 2 + m_list_view->horizontal_padding()),
  169. model()->row_count() * m_list_view->item_height() + m_list_view->frame_thickness() * 2
  170. };
  171. auto taskbar_height = GUI::Desktop::the().taskbar_height();
  172. auto menubar_height = GUI::Desktop::the().menubar_height();
  173. // NOTE: This is so the combobox bottom edge exactly fits the taskbar's
  174. // top edge - the value was found through trial and error though.
  175. auto offset = 8;
  176. Gfx::IntRect list_window_rect { my_screen_rect.bottom_left(), size };
  177. list_window_rect.intersect(Desktop::the().rect().shrunken(0, taskbar_height + menubar_height + offset));
  178. m_editor->set_has_visible_list(true);
  179. m_editor->set_focus(true);
  180. m_list_window->set_rect(list_window_rect);
  181. m_list_window->show();
  182. }
  183. void ComboBox::close()
  184. {
  185. m_list_window->hide();
  186. m_editor->set_has_visible_list(false);
  187. m_editor->set_focus(true);
  188. }
  189. String ComboBox::text() const
  190. {
  191. return m_editor->text();
  192. }
  193. void ComboBox::set_text(const String& text)
  194. {
  195. m_editor->set_text(text);
  196. }
  197. void ComboBox::set_only_allow_values_from_model(bool b)
  198. {
  199. if (m_only_allow_values_from_model == b)
  200. return;
  201. m_only_allow_values_from_model = b;
  202. m_editor->set_mode(m_only_allow_values_from_model ? TextEditor::DisplayOnly : TextEditor::Editable);
  203. }
  204. Model* ComboBox::model()
  205. {
  206. return m_list_view->model();
  207. }
  208. const Model* ComboBox::model() const
  209. {
  210. return m_list_view->model();
  211. }
  212. int ComboBox::model_column() const
  213. {
  214. return m_list_view->model_column();
  215. }
  216. void ComboBox::set_model_column(int column)
  217. {
  218. m_list_view->set_model_column(column);
  219. }
  220. }