ComboBox.cpp 7.7 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_frame_thickness(0);
  56. m_editor->on_return_pressed = [this] {
  57. if (on_return_pressed)
  58. on_return_pressed();
  59. };
  60. m_editor->on_up_pressed = [this] {
  61. m_list_view->move_cursor(AbstractView::CursorMovement::Up, AbstractView::SelectionUpdate::Set);
  62. };
  63. m_editor->on_down_pressed = [this] {
  64. m_list_view->move_cursor(AbstractView::CursorMovement::Down, AbstractView::SelectionUpdate::Set);
  65. };
  66. m_editor->on_pageup_pressed = [this] {
  67. m_list_view->move_cursor(AbstractView::CursorMovement::PageUp, AbstractView::SelectionUpdate::Set);
  68. };
  69. m_editor->on_pagedown_pressed = [this] {
  70. m_list_view->move_cursor(AbstractView::CursorMovement::PageDown, AbstractView::SelectionUpdate::Set);
  71. };
  72. m_editor->on_mousewheel = [this](int delta) {
  73. m_list_view->move_cursor_relative(delta, AbstractView::SelectionUpdate::Set);
  74. };
  75. m_editor->on_mousedown = [this] {
  76. if (only_allow_values_from_model())
  77. m_open_button->click();
  78. };
  79. m_open_button = add<ControlBoxButton>(ControlBoxButton::DownArrow);
  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 = [this](auto& index) {
  104. ASSERT(model());
  105. m_list_view->set_activates_on_selection(true);
  106. auto new_value = index.data().to_string();
  107. m_editor->set_text(new_value);
  108. if (!m_only_allow_values_from_model)
  109. m_editor->select_all();
  110. };
  111. m_list_view->on_activation = [this](auto& index) {
  112. deferred_invoke([this, index](auto&) {
  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::resize_event(ResizeEvent& event)
  127. {
  128. Widget::resize_event(event);
  129. int button_height = event.size().height() - frame_thickness() * 2;
  130. int button_width = 15;
  131. m_open_button->set_relative_rect(width() - button_width - frame_thickness(), frame_thickness(), button_width, button_height);
  132. auto editor_rect = frame_inner_rect();
  133. editor_rect.set_width(editor_rect.width() - button_width);
  134. m_editor->set_relative_rect(editor_rect);
  135. }
  136. void ComboBox::set_model(NonnullRefPtr<Model> model)
  137. {
  138. m_list_view->set_model(move(model));
  139. }
  140. void ComboBox::set_selected_index(size_t index)
  141. {
  142. if (!m_list_view->model())
  143. return;
  144. m_list_view->set_cursor(m_list_view->model()->index(index, 0), AbstractView::SelectionUpdate::Set);
  145. }
  146. size_t ComboBox::selected_index() const
  147. {
  148. return m_list_view->cursor_index().row();
  149. }
  150. void ComboBox::select_all()
  151. {
  152. m_editor->select_all();
  153. }
  154. void ComboBox::open()
  155. {
  156. if (!model())
  157. return;
  158. auto my_screen_rect = screen_relative_rect();
  159. int longest_item_width = 0;
  160. for (int i = 0; i < model()->row_count(); ++i) {
  161. auto index = model()->index(i);
  162. auto item_text = index.data().to_string();
  163. longest_item_width = max(longest_item_width, m_list_view->font().width(item_text));
  164. }
  165. Gfx::IntSize size {
  166. max(width(), longest_item_width + m_list_view->width_occupied_by_vertical_scrollbar() + m_list_view->frame_thickness() * 2 + m_list_view->horizontal_padding()),
  167. model()->row_count() * m_list_view->item_height() + m_list_view->frame_thickness() * 2
  168. };
  169. auto taskbar_height = GUI::Desktop::the().taskbar_height();
  170. auto menubar_height = GUI::Desktop::the().menubar_height();
  171. // NOTE: This is so the combobox bottom edge exactly fits the taskbar's
  172. // top edge - the value was found through trial and error though.
  173. auto offset = 8;
  174. Gfx::IntRect list_window_rect { my_screen_rect.bottom_left(), size };
  175. list_window_rect.intersect(Desktop::the().rect().shrunken(0, taskbar_height + menubar_height + offset));
  176. m_editor->set_has_visible_list(true);
  177. m_editor->set_focus(true);
  178. m_list_window->set_rect(list_window_rect);
  179. m_list_window->show();
  180. }
  181. void ComboBox::close()
  182. {
  183. m_list_window->hide();
  184. m_editor->set_has_visible_list(false);
  185. m_editor->set_focus(true);
  186. }
  187. String ComboBox::text() const
  188. {
  189. return m_editor->text();
  190. }
  191. void ComboBox::set_text(const String& text)
  192. {
  193. m_editor->set_text(text);
  194. }
  195. void ComboBox::set_only_allow_values_from_model(bool b)
  196. {
  197. if (m_only_allow_values_from_model == b)
  198. return;
  199. m_only_allow_values_from_model = b;
  200. m_editor->set_mode(m_only_allow_values_from_model ? TextEditor::DisplayOnly : TextEditor::Editable);
  201. }
  202. Model* ComboBox::model()
  203. {
  204. return m_list_view->model();
  205. }
  206. const Model* ComboBox::model() const
  207. {
  208. return m_list_view->model();
  209. }
  210. int ComboBox::model_column() const
  211. {
  212. return m_list_view->model_column();
  213. }
  214. void ComboBox::set_model_column(int column)
  215. {
  216. m_list_view->set_model_column(column);
  217. }
  218. }