ComboBox.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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_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_focusable(false);
  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. int frame_thickness = m_editor->frame_thickness();
  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. m_editor->set_relative_rect(0, 0, width(), height());
  133. }
  134. void ComboBox::set_model(NonnullRefPtr<Model> model)
  135. {
  136. m_list_view->set_model(move(model));
  137. }
  138. void ComboBox::set_selected_index(size_t index)
  139. {
  140. if (!m_list_view->model())
  141. return;
  142. m_list_view->set_cursor(m_list_view->model()->index(index, 0), AbstractView::SelectionUpdate::Set);
  143. }
  144. size_t ComboBox::selected_index() const
  145. {
  146. return m_list_view->cursor_index().row();
  147. }
  148. void ComboBox::select_all()
  149. {
  150. m_editor->select_all();
  151. }
  152. void ComboBox::open()
  153. {
  154. if (!model())
  155. return;
  156. auto my_screen_rect = screen_relative_rect();
  157. int longest_item_width = 0;
  158. for (int i = 0; i < model()->row_count(); ++i) {
  159. auto index = model()->index(i);
  160. auto item_text = index.data().to_string();
  161. longest_item_width = max(longest_item_width, m_list_view->font().width(item_text));
  162. }
  163. Gfx::IntSize size {
  164. max(width(), longest_item_width + m_list_view->width_occupied_by_vertical_scrollbar() + m_list_view->frame_thickness() * 2 + m_list_view->horizontal_padding()),
  165. model()->row_count() * m_list_view->item_height() + m_list_view->frame_thickness() * 2
  166. };
  167. auto taskbar_height = GUI::Desktop::the().taskbar_height();
  168. auto menubar_height = GUI::Desktop::the().menubar_height();
  169. // NOTE: This is so the combobox bottom edge exactly fits the taskbar's
  170. // top edge - the value was found through trial and error though.
  171. auto offset = 8;
  172. Gfx::IntRect list_window_rect { my_screen_rect.bottom_left(), size };
  173. list_window_rect.intersect(Desktop::the().rect().shrunken(0, taskbar_height + menubar_height + offset));
  174. m_editor->set_has_visible_list(true);
  175. m_editor->set_focus(true);
  176. m_list_window->set_rect(list_window_rect);
  177. m_list_window->show();
  178. }
  179. void ComboBox::close()
  180. {
  181. m_list_window->hide();
  182. m_editor->set_has_visible_list(false);
  183. m_editor->set_focus(true);
  184. }
  185. String ComboBox::text() const
  186. {
  187. return m_editor->text();
  188. }
  189. void ComboBox::set_text(const String& text)
  190. {
  191. m_editor->set_text(text);
  192. }
  193. void ComboBox::set_only_allow_values_from_model(bool b)
  194. {
  195. if (m_only_allow_values_from_model == b)
  196. return;
  197. m_only_allow_values_from_model = b;
  198. m_editor->set_mode(m_only_allow_values_from_model ? TextEditor::DisplayOnly : TextEditor::Editable);
  199. }
  200. Model* ComboBox::model()
  201. {
  202. return m_list_view->model();
  203. }
  204. const Model* ComboBox::model() const
  205. {
  206. return m_list_view->model();
  207. }
  208. int ComboBox::model_column() const
  209. {
  210. return m_list_view->model_column();
  211. }
  212. void ComboBox::set_model_column(int column)
  213. {
  214. m_list_view->set_model_column(column);
  215. }
  216. }