ProjectFile.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "ProjectFile.h"
  27. #include <LibCore/File.h>
  28. #include <string.h>
  29. namespace HackStudio {
  30. ProjectFile::ProjectFile(const String& name)
  31. : m_name(name)
  32. {
  33. }
  34. GUI::TextDocument& ProjectFile::document() const
  35. {
  36. if (!m_document) {
  37. m_document = CodeDocument::create(m_name);
  38. auto file_or_error = Core::File::open(m_name, Core::File::ReadOnly);
  39. if (file_or_error.is_error()) {
  40. warnln("Couldn't open '{}': {}", m_name, file_or_error.error());
  41. // This is okay though, we'll just go with an empty document and create the file when saving.
  42. } else {
  43. auto& file = *file_or_error.value();
  44. m_document->set_text(file.read_all());
  45. }
  46. }
  47. return *m_document;
  48. }
  49. int ProjectFile::vertical_scroll_value() const
  50. {
  51. return m_vertical_scroll_value;
  52. }
  53. void ProjectFile::vertical_scroll_value(int vertical_scroll_value)
  54. {
  55. m_vertical_scroll_value = vertical_scroll_value;
  56. }
  57. int ProjectFile::horizontal_scroll_value() const
  58. {
  59. return m_horizontal_scroll_value;
  60. }
  61. void ProjectFile::horizontal_scroll_value(int horizontal_scroll_value)
  62. {
  63. m_horizontal_scroll_value = horizontal_scroll_value;
  64. }
  65. }