The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_SourceCodeEditor.h"
  19. #include "../Application/jucer_OpenDocumentManager.h"
  20. //==============================================================================
  21. SourceCodeEditor::SourceCodeEditor (OpenDocumentManager::Document* document_)
  22. : DocumentEditorComponent (document_)
  23. {
  24. }
  25. SourceCodeEditor::~SourceCodeEditor()
  26. {
  27. getAppSettings().appearance.settings.removeListener (this);
  28. SourceCodeDocument* doc = dynamic_cast <SourceCodeDocument*> (getDocument());
  29. if (doc != nullptr)
  30. doc->updateLastState (*editor);
  31. }
  32. void SourceCodeEditor::createEditor (CodeDocument& codeDocument)
  33. {
  34. if (document->getFile().hasFileExtension (sourceOrHeaderFileExtensions))
  35. setEditor (new CppCodeEditorComponent (codeDocument));
  36. else
  37. setEditor (new CodeEditorComponent (codeDocument, nullptr));
  38. }
  39. void SourceCodeEditor::setEditor (CodeEditorComponent* newEditor)
  40. {
  41. addAndMakeVisible (editor = newEditor);
  42. #if JUCE_MAC
  43. Font font (13.0f);
  44. font.setTypefaceName ("Menlo");
  45. #else
  46. Font font (12.0f);
  47. font.setTypefaceName (Font::getDefaultMonospacedFontName());
  48. #endif
  49. editor->setFont (font);
  50. editor->setTabSize (4, true);
  51. updateColourScheme();
  52. getAppSettings().appearance.settings.addListener (this);
  53. }
  54. void SourceCodeEditor::highlightLine (int lineNum, int characterIndex)
  55. {
  56. if (lineNum <= editor->getFirstLineOnScreen()
  57. || lineNum >= editor->getFirstLineOnScreen() + editor->getNumLinesOnScreen() - 1)
  58. {
  59. editor->scrollToLine (jmax (0, jmin (lineNum - editor->getNumLinesOnScreen() / 3,
  60. editor->getDocument().getNumLines() - editor->getNumLinesOnScreen())));
  61. }
  62. editor->moveCaretTo (CodeDocument::Position (&editor->getDocument(), lineNum - 1, characterIndex), false);
  63. }
  64. void SourceCodeEditor::resized()
  65. {
  66. editor->setBounds (getLocalBounds());
  67. }
  68. void SourceCodeEditor::updateColourScheme() { getAppSettings().appearance.applyToCodeEditor (*editor); }
  69. void SourceCodeEditor::valueTreePropertyChanged (ValueTree&, const Identifier&) { updateColourScheme(); }
  70. void SourceCodeEditor::valueTreeChildAdded (ValueTree&, ValueTree&) { updateColourScheme(); }
  71. void SourceCodeEditor::valueTreeChildRemoved (ValueTree&, ValueTree&) { updateColourScheme(); }
  72. void SourceCodeEditor::valueTreeChildOrderChanged (ValueTree&) { updateColourScheme(); }
  73. void SourceCodeEditor::valueTreeParentChanged (ValueTree&) { updateColourScheme(); }
  74. void SourceCodeEditor::valueTreeRedirected (ValueTree&) { updateColourScheme(); }
  75. //==============================================================================
  76. SourceCodeDocument::SourceCodeDocument (Project* project_, const File& file_)
  77. : modDetector (file_), project (project_)
  78. {
  79. }
  80. CodeDocument& SourceCodeDocument::getCodeDocument()
  81. {
  82. if (codeDoc == nullptr)
  83. {
  84. codeDoc = new CodeDocument();
  85. reloadInternal();
  86. }
  87. return *codeDoc;
  88. }
  89. Component* SourceCodeDocument::createEditor()
  90. {
  91. SourceCodeEditor* e = new SourceCodeEditor (this);
  92. e->createEditor (getCodeDocument());
  93. applyLastState (*(e->editor));
  94. return e;
  95. }
  96. void SourceCodeDocument::reloadFromFile()
  97. {
  98. getCodeDocument();
  99. reloadInternal();
  100. }
  101. void SourceCodeDocument::reloadInternal()
  102. {
  103. jassert (codeDoc != nullptr);
  104. modDetector.updateHash();
  105. ScopedPointer <InputStream> in (modDetector.getFile().createInputStream());
  106. if (in != nullptr)
  107. codeDoc->loadFromStream (*in);
  108. }
  109. bool SourceCodeDocument::save()
  110. {
  111. TemporaryFile temp (modDetector.getFile());
  112. {
  113. FileOutputStream fo (temp.getFile());
  114. if (! (fo.openedOk() && getCodeDocument().writeToStream (fo)))
  115. return false;
  116. }
  117. if (! temp.overwriteTargetFileWithTemporary())
  118. return false;
  119. getCodeDocument().setSavePoint();
  120. modDetector.updateHash();
  121. return true;
  122. }
  123. void SourceCodeDocument::updateLastState (CodeEditorComponent& editor)
  124. {
  125. lastState = new CodeEditorComponent::State (editor);
  126. }
  127. void SourceCodeDocument::applyLastState (CodeEditorComponent& editor) const
  128. {
  129. if (lastState != nullptr)
  130. lastState->restoreState (editor);
  131. }