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
|
#include "CppLexer.h"
#include "FindInFilesWidget.h"
#include "Project.h"
#include "TerminalWrapper.h"
#include <LibCore/CFile.h>
#include <LibGUI/GAboutDialog.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GInputBox.h>
#include <LibGUI/GListView.h>
#include <LibGUI/GMenu.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GMessageBox.h>
#include <LibGUI/GSplitter.h>
#include <LibGUI/GStatusBar.h>
#include <LibGUI/GTabWidget.h>
#include <LibGUI/GTextBox.h>
#include <LibGUI/GTextEditor.h>
#include <LibGUI/GToolBar.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GWindow.h>
#include <stdio.h>
#include <unistd.h>
String g_currently_open_file;
OwnPtr<Project> g_project;
RefPtr<GWindow> g_window;
RefPtr<GListView> g_project_list_view;
RefPtr<GTextEditor> g_text_editor;
GTextEditor& main_editor()
{
ASSERT(g_text_editor);
return *g_text_editor;
}
static void build(TerminalWrapper&);
static void run(TerminalWrapper&);
void open_file(const String&);
int main(int argc, char** argv)
{
GApplication app(argc, argv);
g_window = GWindow::construct();
g_window->set_rect(100, 100, 800, 600);
g_window->set_title("HackStudio");
auto widget = GWidget::construct();
g_window->set_main_widget(widget);
widget->set_fill_with_background_color(true);
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
widget->layout()->set_spacing(0);
if (chdir("/home/anon/little") < 0) {
perror("chdir");
return 1;
}
g_project = Project::load_from_file("little.files");
ASSERT(g_project);
auto toolbar = GToolBar::construct(widget);
auto outer_splitter = GSplitter::construct(Orientation::Horizontal, widget);
g_project_list_view = GListView::construct(outer_splitter);
g_project_list_view->set_model(g_project->model());
g_project_list_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
g_project_list_view->set_preferred_size(200, 0);
auto inner_splitter = GSplitter::construct(Orientation::Vertical, outer_splitter);
g_text_editor = GTextEditor::construct(GTextEditor::MultiLine, inner_splitter);
g_text_editor->set_ruler_visible(true);
auto new_action = GAction::create("New", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GAction&) {
// FIXME: Implement.
});
auto open_action = GCommonActions::make_open_action([&](auto&) {
// FIXME: Implement.
});
auto save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
if (g_currently_open_file.is_empty())
return;
g_text_editor->write_to_file(g_currently_open_file);
});
toolbar->add_action(new_action);
toolbar->add_action(open_action);
toolbar->add_action(save_action);
toolbar->add_separator();
toolbar->add_action(g_text_editor->cut_action());
toolbar->add_action(g_text_editor->copy_action());
toolbar->add_action(g_text_editor->paste_action());
toolbar->add_separator();
toolbar->add_action(g_text_editor->undo_action());
toolbar->add_action(g_text_editor->redo_action());
toolbar->add_separator();
g_project_list_view->on_activation = [&](auto& index) {
auto filename = g_project_list_view->model()->data(index).to_string();
open_file(filename);
};
auto tab_widget = GTabWidget::construct(inner_splitter);
auto find_in_files_widget = FindInFilesWidget::construct(nullptr);
tab_widget->add_widget("Find in files", find_in_files_widget);
auto terminal_wrapper = TerminalWrapper::construct(nullptr);
tab_widget->add_widget("Console", terminal_wrapper);
auto statusbar = GStatusBar::construct(widget);
g_text_editor->on_cursor_change = [&] {
statusbar->set_text(String::format("Line: %d, Column: %d", g_text_editor->cursor().line() + 1, g_text_editor->cursor().column()));
};
g_text_editor->add_custom_context_menu_action(GAction::create(
"Go to line...", { Mod_Ctrl, Key_L }, GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), [&](auto&) {
auto input_box = GInputBox::construct("Line:", "Go to line", g_window);
auto result = input_box->exec();
if (result == GInputBox::ExecOK) {
bool ok;
auto line_number = input_box->text_value().to_uint(ok);
if (ok) {
g_text_editor->set_cursor(line_number - 1, 0);
}
}
},
g_text_editor));
auto menubar = make<GMenuBar>();
auto app_menu = make<GMenu>("HackStudio");
app_menu->add_action(save_action);
app_menu->add_action(GCommonActions::make_quit_action([&](auto&) {
app.quit();
}));
menubar->add_menu(move(app_menu));
auto edit_menu = make<GMenu>("Edit");
edit_menu->add_action(GAction::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
tab_widget->set_active_widget(find_in_files_widget);
find_in_files_widget->focus_textbox_and_select_all();
}));
menubar->add_menu(move(edit_menu));
auto build_action = GAction::create("Build", { Mod_Ctrl, Key_B }, GraphicsBitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
tab_widget->set_active_widget(terminal_wrapper);
build(terminal_wrapper);
});
toolbar->add_action(build_action);
auto run_action = GAction::create("Run", { Mod_Ctrl, Key_R }, GraphicsBitmap::load_from_file("/res/icons/16x16/run.png"), [&](auto&) {
tab_widget->set_active_widget(terminal_wrapper);
run(terminal_wrapper);
});
toolbar->add_action(run_action);
auto build_menu = make<GMenu>("Build");
build_menu->add_action(build_action);
build_menu->add_action(run_action);
menubar->add_menu(move(build_menu));
auto small_icon = GraphicsBitmap::load_from_file("/res/icons/16x16/app-hack-studio.png");
auto help_menu = make<GMenu>("Help");
help_menu->add_action(GAction::create("About", [&](auto&) {
GAboutDialog::show("HackStudio", small_icon, g_window);
}));
menubar->add_menu(move(help_menu));
app.set_menubar(move(menubar));
g_window->set_icon(small_icon);
g_window->show();
return app.exec();
}
void build(TerminalWrapper& wrapper)
{
wrapper.run_command("make");
}
void run(TerminalWrapper& wrapper)
{
wrapper.run_command("make run");
}
struct TextStyle {
Color color;
const Font* font { nullptr };
};
static TextStyle style_for_token_type(CppToken::Type type)
{
switch (type) {
case CppToken::Type::Keyword:
return { Color::Black, &Font::default_bold_fixed_width_font() };
case CppToken::Type::Identifier:
return { Color::Blue };
case CppToken::Type::DoubleQuotedString:
case CppToken::Type::SingleQuotedString:
case CppToken::Type::Number:
return { Color::Red };
case CppToken::Type::PreprocessorStatement:
return { Color::Magenta };
case CppToken::Type::Comment:
return { Color::from_rgb(0x008200) };
default:
return { Color::Black };
}
}
void open_file(const String& filename)
{
auto file = CFile::construct(filename);
if (!file->open(CFile::ReadOnly)) {
GMessageBox::show("Could not open!", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window);
return;
}
auto contents = file->read_all();
g_text_editor->set_text(contents);
g_currently_open_file = filename;
g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
g_project_list_view->update();
if (filename.ends_with(".cpp")) {
CppLexer lexer(contents);
auto tokens = lexer.lex();
Vector<GTextEditor::Span> spans;
for (auto& token : tokens) {
dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column;
GTextEditor::Span span;
span.start = { token.m_start.line, token.m_start.column };
span.end = { token.m_end.line, token.m_end.column };
auto style = style_for_token_type(token.m_type);
span.color = style.color;
span.font = style.font;
spans.append(span);
}
g_text_editor->set_spans(spans);
}
}
|