summaryrefslogtreecommitdiff
path: root/Applications/Spreadsheet/SpreadsheetWidget.cpp
blob: 57056558c1078c64e03e782a21f8d78991280e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (c) 2020, the SerenityOS developers.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "SpreadsheetWidget.h"
#include "CellSyntaxHighlighter.h"
#include "HelpWindow.h"
#include "LibGUI/InputBox.h"
#include <LibCore/File.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/FilePicker.h>
#include <LibGUI/Label.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Splitter.h>
#include <LibGUI/TabWidget.h>
#include <LibGUI/TextEditor.h>
#include <LibGfx/FontDatabase.h>
#include <string.h>

namespace Spreadsheet {

SpreadsheetWidget::SpreadsheetWidget(NonnullRefPtrVector<Sheet>&& sheets, bool should_add_sheet_if_empty)
    : m_workbook(make<Workbook>(move(sheets)))
{
    set_fill_with_background_color(true);
    set_layout<GUI::VerticalBoxLayout>().set_margins({ 2, 2, 2, 2 });
    auto& container = add<GUI::VerticalSplitter>();

    auto& top_bar = container.add<GUI::Frame>();
    top_bar.set_layout<GUI::HorizontalBoxLayout>().set_spacing(1);
    top_bar.set_fixed_height(26);
    auto& current_cell_label = top_bar.add<GUI::Label>("");
    current_cell_label.set_fixed_width(50);

    auto& help_button = top_bar.add<GUI::Button>("🛈");
    help_button.set_fixed_size(20, 20);
    help_button.on_click = [&](auto) {
        auto docs = m_selected_view->sheet().gather_documentation();
        auto help_window = HelpWindow::the();
        help_window->set_docs(move(docs));
        help_window->show();
    };

    auto& cell_value_editor = top_bar.add<GUI::TextEditor>(GUI::TextEditor::Type::SingleLine);
    cell_value_editor.set_font(Gfx::FontDatabase::default_fixed_width_font());
    cell_value_editor.set_scrollbars_enabled(false);

    cell_value_editor.set_syntax_highlighter(make<CellSyntaxHighlighter>());
    cell_value_editor.set_enabled(false);
    current_cell_label.set_enabled(false);

    m_tab_widget = container.add<GUI::TabWidget>();
    m_tab_widget->set_tab_position(GUI::TabWidget::TabPosition::Bottom);

    m_cell_value_editor = cell_value_editor;
    m_current_cell_label = current_cell_label;
    m_inline_documentation_window = GUI::Window::construct(window());
    m_inline_documentation_window->set_rect(m_cell_value_editor->rect().translated(0, m_cell_value_editor->height() + 7).inflated(6, 6));
    m_inline_documentation_window->set_window_type(GUI::WindowType::Tooltip);
    m_inline_documentation_window->set_resizable(false);
    auto& inline_widget = m_inline_documentation_window->set_main_widget<GUI::Frame>();
    inline_widget.set_fill_with_background_color(true);
    inline_widget.set_layout<GUI::VerticalBoxLayout>().set_margins({ 4, 4, 4, 4 });
    inline_widget.set_frame_shape(Gfx::FrameShape::Box);
    m_inline_documentation_label = inline_widget.add<GUI::Label>();
    m_inline_documentation_label->set_fill_with_background_color(true);
    m_inline_documentation_label->set_autosize(false);
    m_inline_documentation_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);

    if (!m_workbook->has_sheets() && should_add_sheet_if_empty)
        m_workbook->add_sheet("Sheet 1");

    m_tab_context_menu = GUI::Menu::construct();
    auto rename_action = GUI::Action::create("Rename...", [this](auto&) {
        ASSERT(m_tab_context_menu_sheet_view);

        auto& sheet = m_tab_context_menu_sheet_view->sheet();
        String new_name;
        if (GUI::InputBox::show(new_name, window(), String::formatted("New name for '{}'", sheet.name()), "Rename sheet") == GUI::Dialog::ExecOK) {
            sheet.set_name(new_name);
            sheet.update();
            m_tab_widget->set_tab_title(static_cast<GUI::Widget&>(*m_tab_context_menu_sheet_view), new_name);
        }
    });
    m_tab_context_menu->add_action(rename_action);
    m_tab_context_menu->add_action(GUI::Action::create("Add new sheet...", [this](auto&) {
        String name;
        if (GUI::InputBox::show(name, window(), "Name for new sheet", "Create sheet") == GUI::Dialog::ExecOK) {
            NonnullRefPtrVector<Sheet> new_sheets;
            new_sheets.append(m_workbook->add_sheet(name));
            setup_tabs(move(new_sheets));
        }
    }));

    setup_tabs(m_workbook->sheets());
}

void SpreadsheetWidget::resize_event(GUI::ResizeEvent& event)
{
    GUI::Widget::resize_event(event);
    if (m_inline_documentation_window && m_cell_value_editor && window())
        m_inline_documentation_window->set_rect(m_cell_value_editor->screen_relative_rect().translated(0, m_cell_value_editor->height() + 7).inflated(6, 6));
}

void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
{
    RefPtr<GUI::Widget> first_tab_widget;
    for (auto& sheet : new_sheets) {
        auto& tab = m_tab_widget->add_tab<SpreadsheetView>(sheet.name(), sheet);
        if (!first_tab_widget)
            first_tab_widget = &tab;
    }

    auto change = [&](auto& selected_widget) {
        if (m_selected_view) {
            m_selected_view->on_selection_changed = nullptr;
            m_selected_view->on_selection_dropped = nullptr;
        };
        m_selected_view = &static_cast<SpreadsheetView&>(selected_widget);
        m_selected_view->on_selection_changed = [&](Vector<Position>&& selection) {
            if (selection.is_empty()) {
                m_current_cell_label->set_enabled(false);
                m_current_cell_label->set_text({});
                m_cell_value_editor->on_change = nullptr;
                m_cell_value_editor->on_focusin = nullptr;
                m_cell_value_editor->on_focusout = nullptr;
                m_cell_value_editor->set_text("");
                m_cell_value_editor->set_enabled(false);
                return;
            }

            if (selection.size() == 1) {
                auto& position = selection.first();
                StringBuilder builder;
                builder.append(position.column);
                builder.appendff("{}", position.row);
                m_current_cell_label->set_enabled(true);
                m_current_cell_label->set_text(builder.string_view());

                auto& cell = m_selected_view->sheet().ensure(position);
                m_cell_value_editor->on_change = nullptr;
                m_cell_value_editor->set_text(cell.source());
                m_cell_value_editor->on_change = [&] {
                    auto text = m_cell_value_editor->text();
                    // FIXME: Lines?
                    auto offset = m_cell_value_editor->cursor().column();
                    try_generate_tip_for_input_expression(text, offset);
                    cell.set_data(move(text));
                    m_selected_view->sheet().update();
                    update();
                };
                m_cell_value_editor->set_enabled(true);
                static_cast<CellSyntaxHighlighter*>(const_cast<GUI::SyntaxHighlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(&cell);
                return;
            }

            // There are many cells selected, change all of them.
            StringBuilder builder;
            builder.appendff("<{}>", selection.size());
            m_current_cell_label->set_enabled(true);
            m_current_cell_label->set_text(builder.string_view());

            Vector<Cell*> cells;
            for (auto& position : selection)
                cells.append(&m_selected_view->sheet().ensure(position));

            auto first_cell = cells.first();
            m_cell_value_editor->on_change = nullptr;
            m_cell_value_editor->set_text("");
            m_should_change_selected_cells = false;
            m_cell_value_editor->on_focusin = [this] { m_should_change_selected_cells = true; };
            m_cell_value_editor->on_focusout = [this] { m_should_change_selected_cells = false; };
            m_cell_value_editor->on_change = [cells = move(cells), this] {
                if (m_should_change_selected_cells) {
                    auto text = m_cell_value_editor->text();
                    // FIXME: Lines?
                    auto offset = m_cell_value_editor->cursor().column();
                    try_generate_tip_for_input_expression(text, offset);
                    for (auto* cell : cells)
                        cell->set_data(text);
                    m_selected_view->sheet().update();
                    update();
                }
            };
            m_cell_value_editor->set_enabled(true);
            static_cast<CellSyntaxHighlighter*>(const_cast<GUI::SyntaxHighlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(first_cell);
        };
        m_selected_view->on_selection_dropped = [&]() {
            m_cell_value_editor->set_enabled(false);
            static_cast<CellSyntaxHighlighter*>(const_cast<GUI::SyntaxHighlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(nullptr);
            m_cell_value_editor->set_text("");
            m_current_cell_label->set_enabled(false);
            m_current_cell_label->set_text("");
        };
    };

    if (first_tab_widget)
        change(*first_tab_widget);

    m_tab_widget->on_change = [change = move(change)](auto& selected_widget) {
        change(selected_widget);
    };

    m_tab_widget->on_context_menu_request = [&](auto& widget, auto& event) {
        m_tab_context_menu_sheet_view = widget;
        m_tab_context_menu->popup(event.screen_position());
    };
}

void SpreadsheetWidget::try_generate_tip_for_input_expression(StringView source, size_t cursor_offset)
{
    m_inline_documentation_window->set_rect(m_cell_value_editor->screen_relative_rect().translated(0, m_cell_value_editor->height() + 7).inflated(6, 6));
    if (!m_selected_view || !source.starts_with('=')) {
        m_inline_documentation_window->hide();
        return;
    }
    auto maybe_function_and_argument = get_function_and_argument_index(source.substring_view(0, cursor_offset));
    if (!maybe_function_and_argument.has_value()) {
        m_inline_documentation_window->hide();
        return;
    }

    auto& [name, index] = maybe_function_and_argument.value();
    auto& sheet = m_selected_view->sheet();
    auto text = sheet.generate_inline_documentation_for(name, index);
    if (text.is_empty()) {
        m_inline_documentation_window->hide();
    } else {
        m_inline_documentation_label->set_text(move(text));
        m_inline_documentation_window->show();
    }
}

void SpreadsheetWidget::save(const StringView& filename)
{
    auto result = m_workbook->save(filename);
    if (result.is_error())
        GUI::MessageBox::show_error(window(), result.error());
}

void SpreadsheetWidget::load(const StringView& filename)
{
    auto result = m_workbook->load(filename);
    if (result.is_error()) {
        GUI::MessageBox::show_error(window(), result.error());
        return;
    }

    m_tab_widget->on_change = nullptr;
    m_cell_value_editor->on_change = nullptr;
    m_current_cell_label->set_text("");
    m_should_change_selected_cells = false;
    while (auto* widget = m_tab_widget->active_widget()) {
        m_tab_widget->remove_tab(*widget);
    }

    setup_tabs(m_workbook->sheets());
}

bool SpreadsheetWidget::request_close()
{
    if (!m_workbook->dirty())
        return true;

    auto result = GUI::MessageBox::show(window(), "The spreadsheet has been modified. Would you like to save?", "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);

    if (result == GUI::MessageBox::ExecYes) {
        if (current_filename().is_empty()) {
            String name = "workbook";
            Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), name, "sheets");
            if (!save_path.has_value())
                return false;

            save(save_path.value());
        } else {
            save(current_filename());
        }
        return true;
    }

    if (result == GUI::MessageBox::ExecNo)
        return true;

    return false;
}

void SpreadsheetWidget::add_sheet()
{
    StringBuilder name;
    name.append("Sheet");
    name.appendff(" {}", m_workbook->sheets().size() + 1);

    NonnullRefPtrVector<Sheet> new_sheets;
    new_sheets.append(m_workbook->add_sheet(name.string_view()));
    setup_tabs(move(new_sheets));
}

void SpreadsheetWidget::add_sheet(NonnullRefPtr<Sheet>&& sheet)
{
    ASSERT(m_workbook == &sheet->workbook());

    NonnullRefPtrVector<Sheet> new_sheets;
    new_sheets.append(move(sheet));
    m_workbook->sheets().append(new_sheets);
    setup_tabs(new_sheets);
}

void SpreadsheetWidget::set_filename(const String& filename)
{
    if (m_workbook->set_filename(filename)) {
        StringBuilder builder;
        builder.append("Spreadsheet - ");
        builder.append(current_filename());

        window()->set_title(builder.string_view());
        window()->update();
    }
}

SpreadsheetWidget::~SpreadsheetWidget()
{
}
}