From bb74832dd6348021120ea88c9d34e26a9104209c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 21 Aug 2019 21:30:20 +0200 Subject: [PATCH] TextEditor: Add a search bar that allows you to search for text If the text is not found, we show a friendly message box that says we didn't find the text! :^) --- Applications/TextEditor/TextEditorWidget.cpp | 31 ++++++++++++++++++++ Applications/TextEditor/TextEditorWidget.h | 5 ++++ 2 files changed, 36 insertions(+) diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp index 537959135b..85941be96c 100644 --- a/Applications/TextEditor/TextEditorWidget.cpp +++ b/Applications/TextEditor/TextEditorWidget.cpp @@ -4,11 +4,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include @@ -21,6 +23,35 @@ TextEditorWidget::TextEditorWidget() m_editor = new GTextEditor(GTextEditor::MultiLine, this); m_editor->set_ruler_visible(true); m_editor->set_automatic_indentation_enabled(true); + + auto* find_widget = new GWidget(this); + find_widget->set_fill_with_background_color(true); + find_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); + find_widget->set_preferred_size(0, 22); + find_widget->set_layout(make(Orientation::Horizontal)); + find_widget->layout()->set_margins({ 2, 2, 2, 2 }); + + m_find_textbox = new GTextBox(find_widget); + + m_find_button = new GButton("Find", find_widget); + m_find_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); + m_find_button->set_preferred_size(100, 0); + + m_find_button->on_click = [this](auto&) { + auto needle = m_find_textbox->text(); + auto found_range = m_editor->find(needle, m_editor->normalized_selection().end()); + dbg() << "find(\"" << needle << "\") returned " << found_range; + if (found_range.is_valid()) { + m_editor->set_selection(found_range); + } else { + GMessageBox::show( + String::format("Not found: \"%s\"", needle.characters()), + "Not found", + GMessageBox::Type::Information, + GMessageBox::InputType::OK, window()); + } + }; + auto* statusbar = new GStatusBar(this); m_editor->on_cursor_change = [statusbar, this] { diff --git a/Applications/TextEditor/TextEditorWidget.h b/Applications/TextEditor/TextEditorWidget.h index 7b4a1f5b2e..c8e7c1e28d 100644 --- a/Applications/TextEditor/TextEditorWidget.h +++ b/Applications/TextEditor/TextEditorWidget.h @@ -7,6 +7,8 @@ #include #include +class GButton; +class GTextBox; class GTextEditor; class TextEditorWidget final : public GWidget { @@ -26,4 +28,7 @@ private: RefPtr m_open_action; RefPtr m_save_action; RefPtr m_save_as_action; + + GTextBox* m_find_textbox { nullptr }; + GButton* m_find_button { nullptr }; };