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 "EditorWrapper.h"
28#include "Editor.h"
29#include <LibGUI/Action.h>
30#include <LibGUI/BoxLayout.h>
31#include <LibGUI/InputBox.h>
32#include <LibGUI/Label.h>
33#include <LibGfx/Font.h>
34
35extern RefPtr<EditorWrapper> g_current_editor_wrapper;
36extern Function<void(String)> g_open_file;
37
38EditorWrapper::EditorWrapper()
39{
40 set_layout<GUI::VerticalBoxLayout>();
41
42 auto& label_wrapper = add<GUI::Widget>();
43 label_wrapper.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
44 label_wrapper.set_preferred_size(0, 14);
45 label_wrapper.set_fill_with_background_color(true);
46 label_wrapper.set_layout<GUI::HorizontalBoxLayout>();
47 label_wrapper.layout()->set_margins({ 2, 0, 2, 0 });
48
49 m_filename_label = label_wrapper.add<GUI::Label>("(Untitled)");
50 m_filename_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
51 m_filename_label->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
52 m_filename_label->set_preferred_size(0, 14);
53
54 m_cursor_label = label_wrapper.add<GUI::Label>("(Cursor)");
55 m_cursor_label->set_text_alignment(Gfx::TextAlignment::CenterRight);
56 m_cursor_label->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
57 m_cursor_label->set_preferred_size(0, 14);
58
59 m_editor = add<Editor>();
60 m_editor->set_ruler_visible(true);
61 m_editor->set_line_wrapping_enabled(true);
62 m_editor->set_automatic_indentation_enabled(true);
63
64 m_editor->on_cursor_change = [this] {
65 m_cursor_label->set_text(String::format("Line: %d, Column: %d", m_editor->cursor().line() + 1, m_editor->cursor().column()));
66 };
67
68 m_editor->on_focus = [this] {
69 g_current_editor_wrapper = this;
70 };
71
72 m_editor->on_open = [](String path) {
73 g_open_file(path);
74 };
75}
76
77EditorWrapper::~EditorWrapper()
78{
79}
80
81void EditorWrapper::set_editor_has_focus(Badge<Editor>, bool focus)
82{
83 m_filename_label->set_font(focus ? Gfx::Font::default_bold_font() : Gfx::Font::default_font());
84}