Serenity Operating System
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
27#include <LibGUI/Button.h>
28#include <LibGUI/ComboBox.h>
29#include <LibGUI/Desktop.h>
30#include <LibGUI/ListView.h>
31#include <LibGUI/Model.h>
32#include <LibGUI/ScrollBar.h>
33#include <LibGUI/TextBox.h>
34#include <LibGUI/Window.h>
35
36namespace GUI {
37
38ComboBox::ComboBox()
39{
40 m_editor = add<TextBox>();
41 m_editor->on_change = [this] {
42 if (on_change)
43 on_change(m_editor->text(), m_list_view->selection().first());
44 };
45 m_editor->on_return_pressed = [this] {
46 if (on_return_pressed)
47 on_return_pressed();
48 };
49 m_open_button = add<Button>();
50 m_open_button->set_focusable(false);
51 m_open_button->set_text("\xc3\xb7");
52 m_open_button->on_click = [this](auto&) {
53 if (m_list_window->is_visible())
54 close();
55 else
56 open();
57 };
58
59 m_list_window = add<Window>();
60 // FIXME: This is obviously not a tooltip window, but it's the closest thing to what we want atm.
61 m_list_window->set_window_type(WindowType::Tooltip);
62
63 m_list_view = ListView::construct();
64 m_list_view->horizontal_scrollbar().set_visible(false);
65 m_list_window->set_main_widget(m_list_view);
66
67 m_list_view->on_selection = [this](auto& index) {
68 ASSERT(model());
69 auto new_value = model()->data(index).to_string();
70 m_editor->set_text(new_value);
71 m_editor->select_all();
72 close();
73 deferred_invoke([this, index](auto&) {
74 if (on_change)
75 on_change(m_editor->text(), index);
76 });
77 };
78}
79
80ComboBox::~ComboBox()
81{
82}
83
84void ComboBox::resize_event(ResizeEvent& event)
85{
86 int frame_thickness = m_editor->frame_thickness();
87 int button_height = event.size().height() - frame_thickness * 2;
88 int button_width = 15;
89 m_open_button->set_relative_rect(width() - button_width - frame_thickness, frame_thickness, button_width, button_height);
90 m_editor->set_relative_rect(0, 0, width(), height());
91}
92
93void ComboBox::set_model(NonnullRefPtr<Model> model)
94{
95 m_list_view->set_model(move(model));
96}
97
98void ComboBox::select_all()
99{
100 m_editor->select_all();
101}
102
103void ComboBox::open()
104{
105 if (!model())
106 return;
107
108 auto my_screen_rect = screen_relative_rect();
109
110 int longest_item_width = 0;
111 for (int i = 0; i < model()->row_count(); ++i) {
112 auto index = model()->index(i);
113 auto item_text = model()->data(index).to_string();
114 longest_item_width = max(longest_item_width, m_list_view->font().width(item_text));
115 }
116 Gfx::Size size {
117 max(width(), longest_item_width + m_list_view->width_occupied_by_vertical_scrollbar() + m_list_view->frame_thickness() * 2 + m_list_view->horizontal_padding()),
118 model()->row_count() * m_list_view->item_height() + m_list_view->frame_thickness() * 2
119 };
120
121 Gfx::Rect list_window_rect { my_screen_rect.bottom_left(), size };
122 list_window_rect.intersect(Desktop::the().rect().shrunken(0, 128));
123
124 m_list_window->set_rect(list_window_rect);
125 m_list_window->show();
126}
127
128void ComboBox::close()
129{
130 m_list_window->hide();
131 m_editor->set_focus(true);
132}
133
134String ComboBox::text() const
135{
136 return m_editor->text();
137}
138
139void ComboBox::set_text(const String& text)
140{
141 m_editor->set_text(text);
142}
143
144void ComboBox::set_only_allow_values_from_model(bool b)
145{
146 if (m_only_allow_values_from_model == b)
147 return;
148 m_only_allow_values_from_model = b;
149 m_editor->set_readonly(m_only_allow_values_from_model);
150}
151
152Model* ComboBox::model()
153{
154 return m_list_view->model();
155}
156
157const Model* ComboBox::model() const
158{
159 return m_list_view->model();
160}
161
162int ComboBox::model_column() const
163{
164 return m_list_view->model_column();
165}
166
167void ComboBox::set_model_column(int column)
168{
169 m_list_view->set_model_column(column);
170}
171
172}