EditorWrapper.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "EditorWrapper.h"
  2. #include "Editor.h"
  3. #include <LibGUI/GAction.h>
  4. #include <LibGUI/GBoxLayout.h>
  5. #include <LibGUI/GInputBox.h>
  6. #include <LibGUI/GLabel.h>
  7. extern RefPtr<EditorWrapper> g_current_editor_wrapper;
  8. EditorWrapper::EditorWrapper(GWidget* parent)
  9. : GWidget(parent)
  10. {
  11. set_layout(make<GBoxLayout>(Orientation::Vertical));
  12. auto label_wrapper = GWidget::construct(this);
  13. label_wrapper->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  14. label_wrapper->set_preferred_size(0, 16);
  15. label_wrapper->set_fill_with_background_color(true);
  16. label_wrapper->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  17. m_filename_label = GLabel::construct("(Untitled)", label_wrapper);
  18. m_filename_label->set_font(Font::default_bold_font());
  19. m_filename_label->set_text_alignment(TextAlignment::CenterLeft);
  20. m_filename_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  21. m_filename_label->set_preferred_size(0, 16);
  22. m_cursor_label = GLabel::construct("(Cursor)", label_wrapper);
  23. m_cursor_label->set_text_alignment(TextAlignment::CenterRight);
  24. m_cursor_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  25. m_cursor_label->set_preferred_size(0, 16);
  26. m_editor = Editor::construct(this);
  27. m_editor->set_ruler_visible(true);
  28. m_editor->set_line_wrapping_enabled(true);
  29. m_editor->set_automatic_indentation_enabled(true);
  30. m_editor->on_cursor_change = [this] {
  31. m_cursor_label->set_text(String::format("Line: %d, Column: %d", m_editor->cursor().line() + 1, m_editor->cursor().column()));
  32. };
  33. m_editor->on_focus = [this] {
  34. g_current_editor_wrapper = this;
  35. };
  36. m_editor->add_custom_context_menu_action(GAction::create(
  37. "Go to line...", { Mod_Ctrl, Key_L }, GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](auto&) {
  38. auto input_box = GInputBox::construct("Line:", "Go to line", window());
  39. auto result = input_box->exec();
  40. if (result == GInputBox::ExecOK) {
  41. bool ok;
  42. auto line_number = input_box->text_value().to_uint(ok);
  43. if (ok) {
  44. m_editor->set_cursor(line_number - 1, 0);
  45. }
  46. }
  47. },
  48. m_editor));
  49. }
  50. EditorWrapper::~EditorWrapper()
  51. {
  52. }