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! :^)
This commit is contained in:
Andreas Kling 2019-08-21 21:30:20 +02:00
parent 0c72371ad9
commit bb74832dd6
Notes: sideshowbarker 2024-07-19 12:34:12 +09:00
2 changed files with 36 additions and 0 deletions

View file

@ -4,11 +4,13 @@
#include <LibCore/CFile.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GFilePicker.h>
#include <LibGUI/GFontDatabase.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GMessageBox.h>
#include <LibGUI/GStatusBar.h>
#include <LibGUI/GTextBox.h>
#include <LibGUI/GTextEditor.h>
#include <LibGUI/GToolBar.h>
@ -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<GBoxLayout>(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] {

View file

@ -7,6 +7,8 @@
#include <LibGUI/GWidget.h>
#include <LibGUI/GWindow.h>
class GButton;
class GTextBox;
class GTextEditor;
class TextEditorWidget final : public GWidget {
@ -26,4 +28,7 @@ private:
RefPtr<GAction> m_open_action;
RefPtr<GAction> m_save_action;
RefPtr<GAction> m_save_as_action;
GTextBox* m_find_textbox { nullptr };
GButton* m_find_button { nullptr };
};