ComboBox.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. 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 (on_mousewheel)
  47. on_mousewheel(event.wheel_delta());
  48. }
  49. };
  50. ComboBox::ComboBox()
  51. {
  52. m_editor = add<ComboBoxEditor>();
  53. m_editor->on_change = [this] {
  54. if (on_change)
  55. on_change(m_editor->text(), m_list_view->selection().first());
  56. };
  57. m_editor->on_return_pressed = [this] {
  58. if (on_return_pressed)
  59. on_return_pressed();
  60. };
  61. m_open_button = add<Button>();
  62. m_open_button->set_focusable(false);
  63. m_open_button->set_text("\xE2\xAC\x87"); // DOWNWARDS BLACK ARROW
  64. m_open_button->on_click = [this](auto) {
  65. if (m_list_window->is_visible())
  66. close();
  67. else
  68. open();
  69. };
  70. m_list_window = add<Window>();
  71. m_list_window->set_frameless(true);
  72. m_list_view = m_list_window->set_main_widget<ListView>();
  73. m_list_view->horizontal_scrollbar().set_visible(false);
  74. m_list_view->on_selection = [this](auto& index) {
  75. ASSERT(model());
  76. auto new_value = model()->data(index).to_string();
  77. m_editor->set_text(new_value);
  78. if (!m_only_allow_values_from_model)
  79. m_editor->select_all();
  80. close();
  81. deferred_invoke([this, index](auto&) {
  82. if (on_change)
  83. on_change(m_editor->text(), index);
  84. });
  85. };
  86. m_editor->on_mousewheel = [this](int delta) {
  87. m_list_view->move_selection(delta);
  88. };
  89. }
  90. ComboBox::~ComboBox()
  91. {
  92. }
  93. void ComboBox::resize_event(ResizeEvent& event)
  94. {
  95. int frame_thickness = m_editor->frame_thickness();
  96. int button_height = event.size().height() - frame_thickness * 2;
  97. int button_width = 15;
  98. m_open_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness, button_width, button_height);
  99. m_editor->set_relative_rect(0, 0, width(), height());
  100. }
  101. void ComboBox::set_model(NonnullRefPtr<Model> model)
  102. {
  103. m_list_view->set_model(move(model));
  104. }
  105. void ComboBox::set_selected_index(size_t index)
  106. {
  107. auto model = this->m_list_view->model();
  108. auto model_index = model->index(index, 0);
  109. if (model->is_valid(model_index))
  110. this->m_list_view->selection().set(model_index);
  111. }
  112. void ComboBox::select_all()
  113. {
  114. m_editor->select_all();
  115. }
  116. void ComboBox::open()
  117. {
  118. if (!model())
  119. return;
  120. auto my_screen_rect = screen_relative_rect();
  121. int longest_item_width = 0;
  122. for (int i = 0; i < model()->row_count(); ++i) {
  123. auto index = model()->index(i);
  124. auto item_text = model()->data(index).to_string();
  125. longest_item_width = max(longest_item_width, m_list_view->font().width(item_text));
  126. }
  127. Gfx::Size size {
  128. max(width(), longest_item_width + m_list_view->width_occupied_by_vertical_scrollbar() + m_list_view->frame_thickness() * 2 + m_list_view->horizontal_padding()),
  129. model()->row_count() * m_list_view->item_height() + m_list_view->frame_thickness() * 2
  130. };
  131. Gfx::Rect list_window_rect { my_screen_rect.bottom_left(), size };
  132. list_window_rect.intersect(Desktop::the().rect().shrunken(0, 128));
  133. m_list_window->set_rect(list_window_rect);
  134. m_list_window->show();
  135. }
  136. void ComboBox::close()
  137. {
  138. m_list_window->hide();
  139. m_editor->set_focus(true);
  140. }
  141. String ComboBox::text() const
  142. {
  143. return m_editor->text();
  144. }
  145. void ComboBox::set_text(const String& text)
  146. {
  147. m_editor->set_text(text);
  148. }
  149. void ComboBox::set_only_allow_values_from_model(bool b)
  150. {
  151. if (m_only_allow_values_from_model == b)
  152. return;
  153. m_only_allow_values_from_model = b;
  154. m_editor->set_readonly(m_only_allow_values_from_model);
  155. }
  156. Model* ComboBox::model()
  157. {
  158. return m_list_view->model();
  159. }
  160. const Model* ComboBox::model() const
  161. {
  162. return m_list_view->model();
  163. }
  164. int ComboBox::model_column() const
  165. {
  166. return m_list_view->model_column();
  167. }
  168. void ComboBox::set_model_column(int column)
  169. {
  170. m_list_view->set_model_column(column);
  171. }
  172. }