ComboBox.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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->selection().first());
  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_selection(-1);
  66. };
  67. m_editor->on_down_pressed = [this] {
  68. m_list_view->move_selection(1);
  69. };
  70. m_editor->on_pageup_pressed = [this] {
  71. m_list_view->move_selection(-m_list_view->selection().first().row());
  72. };
  73. m_editor->on_pagedown_pressed = [this] {
  74. if (model())
  75. m_list_view->move_selection((model()->row_count() - 1) - m_list_view->selection().first().row());
  76. };
  77. m_editor->on_mousewheel = [this](int delta) {
  78. m_list_view->move_selection(delta);
  79. };
  80. m_editor->on_mousedown = [this] {
  81. if (only_allow_values_from_model())
  82. m_open_button->click();
  83. };
  84. m_open_button = add<ControlBoxButton>(ControlBoxButton::DownArrow);
  85. m_open_button->set_focusable(false);
  86. m_open_button->on_click = [this](auto) {
  87. if (m_list_window->is_visible())
  88. close();
  89. else
  90. open();
  91. };
  92. m_list_window = add<Window>(window());
  93. m_list_window->set_frameless(true);
  94. m_list_window->set_accessory(true);
  95. m_list_window->on_active_input_change = [this](bool is_active_input) {
  96. if (!is_active_input) {
  97. m_open_button->set_enabled(false);
  98. close();
  99. }
  100. m_open_button->set_enabled(true);
  101. };
  102. m_list_view = m_list_window->set_main_widget<ListView>();
  103. m_list_view->horizontal_scrollbar().set_visible(false);
  104. m_list_view->set_alternating_row_colors(false);
  105. m_list_view->set_hover_highlighting(true);
  106. m_list_view->set_frame_thickness(1);
  107. m_list_view->set_frame_shadow(Gfx::FrameShadow::Plain);
  108. m_list_view->on_selection = [this](auto& index) {
  109. ASSERT(model());
  110. m_list_view->set_activates_on_selection(true);
  111. auto new_value = model()->data(index).to_string();
  112. m_editor->set_text(new_value);
  113. if (!m_only_allow_values_from_model)
  114. m_editor->select_all();
  115. deferred_invoke([this, index](auto&) {
  116. if (on_change)
  117. on_change(m_editor->text(), index);
  118. });
  119. };
  120. m_list_view->on_activation = [this](auto&) {
  121. m_list_view->set_activates_on_selection(false);
  122. close();
  123. };
  124. m_list_view->on_escape_pressed = [this] {
  125. close();
  126. };
  127. }
  128. ComboBox::~ComboBox()
  129. {
  130. }
  131. void ComboBox::resize_event(ResizeEvent& event)
  132. {
  133. int frame_thickness = m_editor->frame_thickness();
  134. int button_height = event.size().height() - frame_thickness * 2;
  135. int button_width = 15;
  136. m_open_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness, button_width, button_height);
  137. m_editor->set_relative_rect(0, 0, width(), height());
  138. }
  139. void ComboBox::set_model(NonnullRefPtr<Model> model)
  140. {
  141. m_list_view->set_model(move(model));
  142. }
  143. void ComboBox::set_selected_index(size_t index)
  144. {
  145. auto model = this->m_list_view->model();
  146. auto model_index = model->index(index, 0);
  147. if (model->is_valid(model_index))
  148. this->m_list_view->selection().set(model_index);
  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 = model()->data(index).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. Gfx::IntRect list_window_rect { my_screen_rect.bottom_left(), size };
  170. list_window_rect.intersect(Desktop::the().rect().shrunken(0, 128));
  171. if (m_list_view->hover_highlighting())
  172. m_list_view->set_last_valid_hovered_index({});
  173. m_editor->set_has_visible_list(true);
  174. m_editor->set_focus(true);
  175. m_list_window->set_rect(list_window_rect);
  176. m_list_window->show();
  177. }
  178. void ComboBox::close()
  179. {
  180. m_list_window->hide();
  181. m_editor->set_has_visible_list(false);
  182. m_editor->set_focus(true);
  183. }
  184. String ComboBox::text() const
  185. {
  186. return m_editor->text();
  187. }
  188. void ComboBox::set_text(const String& text)
  189. {
  190. m_editor->set_text(text);
  191. }
  192. void ComboBox::set_only_allow_values_from_model(bool b)
  193. {
  194. if (m_only_allow_values_from_model == b)
  195. return;
  196. m_only_allow_values_from_model = b;
  197. m_editor->set_mode(m_only_allow_values_from_model ? TextEditor::DisplayOnly : TextEditor::Editable);
  198. }
  199. Model* ComboBox::model()
  200. {
  201. return m_list_view->model();
  202. }
  203. const Model* ComboBox::model() const
  204. {
  205. return m_list_view->model();
  206. }
  207. int ComboBox::model_column() const
  208. {
  209. return m_list_view->model_column();
  210. }
  211. void ComboBox::set_model_column(int column)
  212. {
  213. m_list_view->set_model_column(column);
  214. }
  215. }