2019-10-21 18:46:55 +02:00
|
|
|
#include "Project.h"
|
2019-10-21 20:17:32 +02:00
|
|
|
#include "TerminalWrapper.h"
|
2019-10-21 18:46:55 +02:00
|
|
|
#include <LibCore/CFile.h>
|
2019-10-21 22:13:20 +02:00
|
|
|
#include <LibGUI/GAboutDialog.h>
|
2019-10-21 19:03:09 +02:00
|
|
|
#include <LibGUI/GAction.h>
|
2019-10-21 18:46:55 +02:00
|
|
|
#include <LibGUI/GApplication.h>
|
2019-10-23 20:54:41 +02:00
|
|
|
#include <LibGUI/GButton.h>
|
2019-10-21 18:46:55 +02:00
|
|
|
#include <LibGUI/GBoxLayout.h>
|
2019-10-21 19:03:09 +02:00
|
|
|
#include <LibGUI/GInputBox.h>
|
2019-10-21 18:46:55 +02:00
|
|
|
#include <LibGUI/GListView.h>
|
2019-10-21 22:13:20 +02:00
|
|
|
#include <LibGUI/GMenu.h>
|
|
|
|
#include <LibGUI/GMenuBar.h>
|
2019-10-21 18:46:55 +02:00
|
|
|
#include <LibGUI/GMessageBox.h>
|
|
|
|
#include <LibGUI/GSplitter.h>
|
|
|
|
#include <LibGUI/GStatusBar.h>
|
2019-10-23 19:52:34 +02:00
|
|
|
#include <LibGUI/GTabWidget.h>
|
2019-10-23 20:54:41 +02:00
|
|
|
#include <LibGUI/GTextBox.h>
|
2019-10-21 18:46:55 +02:00
|
|
|
#include <LibGUI/GTextEditor.h>
|
|
|
|
#include <LibGUI/GToolBar.h>
|
|
|
|
#include <LibGUI/GWidget.h>
|
|
|
|
#include <LibGUI/GWindow.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-10-22 21:38:58 +02:00
|
|
|
String g_currently_open_file;
|
2019-10-23 20:54:41 +02:00
|
|
|
OwnPtr<Project> g_project;
|
|
|
|
RefPtr<GWindow> g_window;
|
|
|
|
RefPtr<GListView> g_project_list_view;
|
|
|
|
RefPtr<GTextEditor> g_text_editor;
|
2019-10-22 21:38:58 +02:00
|
|
|
|
2019-10-22 22:15:43 +02:00
|
|
|
static void build(TerminalWrapper&);
|
2019-10-22 22:18:46 +02:00
|
|
|
static void run(TerminalWrapper&);
|
2019-10-23 20:54:41 +02:00
|
|
|
static NonnullRefPtr<GWidget> build_find_in_files_widget();
|
|
|
|
static void open_file(const String&);
|
2019-10-22 22:15:43 +02:00
|
|
|
|
2019-10-21 18:46:55 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
GApplication app(argc, argv);
|
|
|
|
|
2019-10-23 20:54:41 +02:00
|
|
|
g_window = GWindow::construct();
|
|
|
|
g_window->set_rect(100, 100, 800, 600);
|
|
|
|
g_window->set_title("HackStudio");
|
2019-10-21 18:46:55 +02:00
|
|
|
|
|
|
|
auto widget = GWidget::construct();
|
2019-10-23 20:54:41 +02:00
|
|
|
g_window->set_main_widget(widget);
|
2019-10-21 18:46:55 +02:00
|
|
|
|
|
|
|
widget->set_fill_with_background_color(true);
|
|
|
|
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
|
|
|
|
widget->layout()->set_spacing(0);
|
|
|
|
|
2019-10-22 21:38:58 +02:00
|
|
|
if (chdir("/home/anon/little") < 0) {
|
2019-10-21 18:46:55 +02:00
|
|
|
perror("chdir");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-10-23 20:54:41 +02:00
|
|
|
g_project = Project::load_from_file("little.files");
|
|
|
|
ASSERT(g_project);
|
2019-10-21 18:46:55 +02:00
|
|
|
|
|
|
|
auto toolbar = GToolBar::construct(widget);
|
|
|
|
|
2019-10-21 20:17:32 +02:00
|
|
|
auto outer_splitter = GSplitter::construct(Orientation::Horizontal, widget);
|
2019-10-23 20:54:41 +02:00
|
|
|
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);
|
2019-10-21 18:46:55 +02:00
|
|
|
|
2019-10-21 20:17:32 +02:00
|
|
|
auto inner_splitter = GSplitter::construct(Orientation::Vertical, outer_splitter);
|
2019-10-23 20:54:41 +02:00
|
|
|
g_text_editor = GTextEditor::construct(GTextEditor::MultiLine, inner_splitter);
|
|
|
|
g_text_editor->set_ruler_visible(true);
|
|
|
|
|
|
|
|
g_project_list_view->on_activation = [&](auto& index) {
|
|
|
|
auto filename = g_project_list_view->model()->data(index).to_string();
|
|
|
|
open_file(filename);
|
2019-10-21 18:46:55 +02:00
|
|
|
};
|
|
|
|
|
2019-10-23 19:52:34 +02:00
|
|
|
auto tab_widget = GTabWidget::construct(inner_splitter);
|
|
|
|
|
2019-10-23 20:54:41 +02:00
|
|
|
tab_widget->add_widget("Find in files", build_find_in_files_widget());
|
|
|
|
|
2019-10-23 19:52:34 +02:00
|
|
|
auto terminal_wrapper = TerminalWrapper::construct(nullptr);
|
|
|
|
tab_widget->add_widget("Console", terminal_wrapper);
|
2019-10-21 20:17:32 +02:00
|
|
|
|
2019-10-21 18:46:55 +02:00
|
|
|
auto statusbar = GStatusBar::construct(widget);
|
|
|
|
|
2019-10-23 20:54:41 +02:00
|
|
|
g_text_editor->on_cursor_change = [&] {
|
|
|
|
statusbar->set_text(String::format("Line: %d, Column: %d", g_text_editor->cursor().line(), g_text_editor->cursor().column()));
|
2019-10-21 18:50:24 +02:00
|
|
|
};
|
|
|
|
|
2019-10-23 20:54:41 +02:00
|
|
|
g_text_editor->add_custom_context_menu_action(GAction::create(
|
2019-10-21 22:13:20 +02:00
|
|
|
"Go to line...", { Mod_Ctrl, Key_L }, GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), [&](auto&) {
|
2019-10-23 20:54:41 +02:00
|
|
|
auto input_box = GInputBox::construct("Line:", "Go to line", g_window);
|
2019-10-21 22:13:20 +02:00
|
|
|
auto result = input_box->exec();
|
|
|
|
if (result == GInputBox::ExecOK) {
|
|
|
|
bool ok;
|
|
|
|
auto line_number = input_box->text_value().to_uint(ok);
|
|
|
|
if (ok) {
|
2019-10-23 20:54:41 +02:00
|
|
|
g_text_editor->set_cursor(line_number - 1, 0);
|
2019-10-21 22:13:20 +02:00
|
|
|
}
|
2019-10-21 19:03:09 +02:00
|
|
|
}
|
2019-10-21 22:13:20 +02:00
|
|
|
},
|
2019-10-23 20:54:41 +02:00
|
|
|
g_text_editor));
|
2019-10-21 22:13:20 +02:00
|
|
|
|
|
|
|
auto menubar = make<GMenuBar>();
|
|
|
|
auto app_menu = make<GMenu>("HackStudio");
|
2019-10-22 22:25:05 +02:00
|
|
|
app_menu->add_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;
|
2019-10-23 20:54:41 +02:00
|
|
|
g_text_editor->write_to_file(g_currently_open_file);
|
2019-10-22 22:25:05 +02:00
|
|
|
}));
|
2019-10-21 22:13:20 +02:00
|
|
|
app_menu->add_action(GCommonActions::make_quit_action([&](auto&) {
|
|
|
|
app.quit();
|
|
|
|
}));
|
|
|
|
menubar->add_menu(move(app_menu));
|
|
|
|
|
2019-10-22 22:15:43 +02:00
|
|
|
auto build_menu = make<GMenu>("Build");
|
|
|
|
build_menu->add_action(GAction::create("Build", { Mod_Ctrl, Key_B }, [&](auto&) {
|
|
|
|
build(terminal_wrapper);
|
|
|
|
}));
|
2019-10-22 22:18:46 +02:00
|
|
|
build_menu->add_action(GAction::create("Run", { Mod_Ctrl, Key_R }, [&](auto&) {
|
|
|
|
run(terminal_wrapper);
|
|
|
|
}));
|
2019-10-22 22:15:43 +02:00
|
|
|
menubar->add_menu(move(build_menu));
|
|
|
|
|
2019-10-21 22:13:20 +02:00
|
|
|
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&) {
|
2019-10-23 20:54:41 +02:00
|
|
|
GAboutDialog::show("HackStudio", small_icon, g_window);
|
2019-10-21 22:13:20 +02:00
|
|
|
}));
|
|
|
|
menubar->add_menu(move(help_menu));
|
|
|
|
|
|
|
|
app.set_menubar(move(menubar));
|
|
|
|
|
2019-10-23 20:54:41 +02:00
|
|
|
g_window->set_icon(small_icon);
|
2019-10-21 19:03:09 +02:00
|
|
|
|
2019-10-23 20:54:41 +02:00
|
|
|
g_window->show();
|
2019-10-21 18:46:55 +02:00
|
|
|
return app.exec();
|
|
|
|
}
|
2019-10-22 22:15:43 +02:00
|
|
|
|
|
|
|
void build(TerminalWrapper& wrapper)
|
|
|
|
{
|
|
|
|
wrapper.run_command("make");
|
|
|
|
}
|
2019-10-22 22:18:46 +02:00
|
|
|
|
|
|
|
void run(TerminalWrapper& wrapper)
|
|
|
|
{
|
|
|
|
wrapper.run_command("make run");
|
|
|
|
}
|
2019-10-23 20:54:41 +02:00
|
|
|
|
|
|
|
struct FilenameAndLineNumber {
|
|
|
|
String filename;
|
|
|
|
int line_number { -1 };
|
|
|
|
};
|
|
|
|
|
|
|
|
class SearchResultsModel final : public GModel {
|
|
|
|
public:
|
|
|
|
explicit SearchResultsModel(const Vector<FilenameAndLineNumber>&& matches)
|
|
|
|
: m_matches(move(matches))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int row_count(const GModelIndex& = GModelIndex()) const override { return m_matches.size(); }
|
|
|
|
virtual int column_count(const GModelIndex& = GModelIndex()) const override { return 1; }
|
|
|
|
virtual GVariant data(const GModelIndex& index, Role role = Role::Display) const override
|
|
|
|
{
|
|
|
|
if (role == Role::Display) {
|
|
|
|
auto& match = m_matches.at(index.row());
|
|
|
|
return String::format("%s:%d", match.filename.characters(), match.line_number);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
virtual void update() override {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Vector<FilenameAndLineNumber> m_matches;
|
|
|
|
};
|
|
|
|
|
|
|
|
static RefPtr<SearchResultsModel> find_in_files(const StringView& text)
|
|
|
|
{
|
|
|
|
Vector<FilenameAndLineNumber> matches;
|
|
|
|
g_project->for_each_text_file([&](auto& file) {
|
|
|
|
auto matches_in_file = file.find(text);
|
|
|
|
for (int match : matches_in_file) {
|
|
|
|
matches.append({ file.name(), match });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return adopt(*new SearchResultsModel(move(matches)));
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<GWidget> build_find_in_files_widget()
|
|
|
|
{
|
|
|
|
auto widget = GWidget::construct();
|
|
|
|
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
|
|
|
|
auto textbox = GTextBox::construct(widget);
|
|
|
|
textbox->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
|
|
|
textbox->set_preferred_size(0, 20);
|
|
|
|
auto button = GButton::construct("Find in files", widget);
|
|
|
|
button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
|
|
|
button->set_preferred_size(0, 20);
|
|
|
|
|
|
|
|
auto result_view = GListView::construct(widget);
|
|
|
|
|
|
|
|
result_view->on_activation = [result_view](auto& index) {
|
|
|
|
auto match_string = result_view->model()->data(index).to_string();
|
|
|
|
auto parts = match_string.split(':');
|
|
|
|
ASSERT(parts.size() == 2);
|
|
|
|
bool ok;
|
|
|
|
int line_number = parts[1].to_int(ok);
|
|
|
|
ASSERT(ok);
|
|
|
|
open_file(parts[0]);
|
|
|
|
g_text_editor->set_cursor(line_number - 1, 0);
|
|
|
|
g_text_editor->set_focus(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
button->on_click = [textbox, result_view = result_view.ptr()](auto&) {
|
|
|
|
auto results_model = find_in_files(textbox->text());
|
|
|
|
result_view->set_model(results_model);
|
|
|
|
};
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
g_text_editor->set_text(file->read_all());
|
|
|
|
g_currently_open_file = filename;
|
|
|
|
g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
|
|
|
|
g_project_list_view->update();
|
|
|
|
}
|