EditorWrapper.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "EditorWrapper.h"
  27. #include "Editor.h"
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/BoxLayout.h>
  30. #include <LibGUI/InputBox.h>
  31. #include <LibGUI/Label.h>
  32. #include <LibGfx/Font.h>
  33. extern RefPtr<EditorWrapper> g_current_editor_wrapper;
  34. extern Function<void(String)> g_open_file;
  35. EditorWrapper::EditorWrapper()
  36. {
  37. set_layout<GUI::VerticalBoxLayout>();
  38. auto& label_wrapper = add<GUI::Widget>();
  39. label_wrapper.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  40. label_wrapper.set_preferred_size(0, 14);
  41. label_wrapper.set_fill_with_background_color(true);
  42. label_wrapper.set_layout<GUI::HorizontalBoxLayout>();
  43. label_wrapper.layout()->set_margins({ 2, 0, 2, 0 });
  44. m_filename_label = label_wrapper.add<GUI::Label>("(Untitled)");
  45. m_filename_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  46. m_filename_label->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  47. m_filename_label->set_preferred_size(0, 14);
  48. m_cursor_label = label_wrapper.add<GUI::Label>("(Cursor)");
  49. m_cursor_label->set_text_alignment(Gfx::TextAlignment::CenterRight);
  50. m_cursor_label->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  51. m_cursor_label->set_preferred_size(0, 14);
  52. m_editor = add<Editor>();
  53. m_editor->set_ruler_visible(true);
  54. m_editor->set_line_wrapping_enabled(true);
  55. m_editor->set_automatic_indentation_enabled(true);
  56. m_editor->on_cursor_change = [this] {
  57. m_cursor_label->set_text(String::format("Line: %d, Column: %d", m_editor->cursor().line() + 1, m_editor->cursor().column()));
  58. };
  59. m_editor->on_focus = [this] {
  60. g_current_editor_wrapper = this;
  61. };
  62. m_editor->on_open = [this](String path) {
  63. g_open_file(path);
  64. };
  65. }
  66. EditorWrapper::~EditorWrapper()
  67. {
  68. }
  69. void EditorWrapper::set_editor_has_focus(Badge<Editor>, bool focus)
  70. {
  71. m_filename_label->set_font(focus ? Gfx::Font::default_bold_font() : Gfx::Font::default_font());
  72. }