EditorWrapper.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "HackStudio.h"
  29. #include <LibGUI/Action.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/BoxLayout.h>
  32. #include <LibGUI/InputBox.h>
  33. #include <LibGUI/Label.h>
  34. #include <LibGfx/Font.h>
  35. #include <LibGfx/FontDatabase.h>
  36. #include <LibGfx/Palette.h>
  37. namespace HackStudio {
  38. EditorWrapper::EditorWrapper()
  39. {
  40. set_layout<GUI::VerticalBoxLayout>();
  41. auto& label_wrapper = add<GUI::Widget>();
  42. label_wrapper.set_fixed_height(14);
  43. label_wrapper.set_fill_with_background_color(true);
  44. label_wrapper.set_layout<GUI::HorizontalBoxLayout>();
  45. label_wrapper.layout()->set_margins({ 2, 0, 2, 0 });
  46. m_filename_label = label_wrapper.add<GUI::Label>("(Untitled)");
  47. m_filename_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  48. m_filename_label->set_fixed_height(14);
  49. m_cursor_label = label_wrapper.add<GUI::Label>("(Cursor)");
  50. m_cursor_label->set_text_alignment(Gfx::TextAlignment::CenterRight);
  51. m_cursor_label->set_fixed_height(14);
  52. m_editor = add<Editor>();
  53. m_editor->set_ruler_visible(true);
  54. m_editor->set_automatic_indentation_enabled(true);
  55. m_editor->on_cursor_change = [this] {
  56. m_cursor_label->set_text(String::formatted("Line: {}, Column: {}", m_editor->cursor().line() + 1, m_editor->cursor().column()));
  57. };
  58. m_editor->on_focus = [this] {
  59. set_current_editor_wrapper(this);
  60. };
  61. m_editor->on_open = [](String path) {
  62. open_file(path);
  63. };
  64. }
  65. EditorWrapper::~EditorWrapper()
  66. {
  67. }
  68. void EditorWrapper::set_editor_has_focus(Badge<Editor>, bool focus)
  69. {
  70. m_filename_label->set_font(focus ? Gfx::FontDatabase::default_bold_font() : Gfx::FontDatabase::default_font());
  71. }
  72. LanguageClient& EditorWrapper::language_client() { return m_editor->language_client(); }
  73. void EditorWrapper::set_mode_displayable()
  74. {
  75. editor().set_mode(GUI::TextEditor::Editable);
  76. editor().set_background_role(Gfx::ColorRole::Base);
  77. editor().set_palette(GUI::Application::the()->palette());
  78. }
  79. void EditorWrapper::set_mode_non_displayable()
  80. {
  81. editor().set_mode(GUI::TextEditor::ReadOnly);
  82. editor().set_background_role(Gfx::ColorRole::InactiveSelection);
  83. auto palette = editor().palette();
  84. palette.set_color(Gfx::ColorRole::BaseText, Color::from_rgb(0xffffff));
  85. editor().set_palette(palette);
  86. editor().document().set_text("The contents of this file could not be displayed. Is it a binary file?");
  87. }
  88. }