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.

660 lines
21KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_SourceCodeEditor.h"
  19. #include "../Application/jucer_Application.h"
  20. #include "../Application/jucer_OpenDocumentManager.h"
  21. //==============================================================================
  22. SourceCodeDocument::SourceCodeDocument (Project* p, const File& f)
  23. : modDetector (f), project (p)
  24. {
  25. }
  26. CodeDocument& SourceCodeDocument::getCodeDocument()
  27. {
  28. if (codeDoc == nullptr)
  29. {
  30. codeDoc = new CodeDocument();
  31. reloadInternal();
  32. codeDoc->clearUndoHistory();
  33. }
  34. return *codeDoc;
  35. }
  36. Component* SourceCodeDocument::createEditor()
  37. {
  38. SourceCodeEditor* e = new SourceCodeEditor (this, getCodeDocument());
  39. applyLastState (*(e->editor));
  40. return e;
  41. }
  42. void SourceCodeDocument::reloadFromFile()
  43. {
  44. getCodeDocument();
  45. reloadInternal();
  46. }
  47. void SourceCodeDocument::reloadInternal()
  48. {
  49. jassert (codeDoc != nullptr);
  50. modDetector.updateHash();
  51. codeDoc->applyChanges (getFile().loadFileAsString());
  52. codeDoc->setSavePoint();
  53. }
  54. static bool writeCodeDocToFile (const File& file, CodeDocument& doc)
  55. {
  56. TemporaryFile temp (file);
  57. {
  58. FileOutputStream fo (temp.getFile());
  59. if (! (fo.openedOk() && doc.writeToStream (fo)))
  60. return false;
  61. }
  62. return temp.overwriteTargetFileWithTemporary();
  63. }
  64. bool SourceCodeDocument::save()
  65. {
  66. if (writeCodeDocToFile (getFile(), getCodeDocument()))
  67. {
  68. getCodeDocument().setSavePoint();
  69. modDetector.updateHash();
  70. return true;
  71. }
  72. return false;
  73. }
  74. bool SourceCodeDocument::saveAs()
  75. {
  76. FileChooser fc (TRANS("Save As..."), getFile(), "*");
  77. if (! fc.browseForFileToSave (true))
  78. return true;
  79. return writeCodeDocToFile (fc.getResult(), getCodeDocument());
  80. }
  81. void SourceCodeDocument::updateLastState (CodeEditorComponent& editor)
  82. {
  83. lastState = new CodeEditorComponent::State (editor);
  84. }
  85. void SourceCodeDocument::applyLastState (CodeEditorComponent& editor) const
  86. {
  87. if (lastState != nullptr)
  88. lastState->restoreState (editor);
  89. }
  90. //==============================================================================
  91. SourceCodeEditor::SourceCodeEditor (OpenDocumentManager::Document* doc, CodeDocument& codeDocument)
  92. : DocumentEditorComponent (doc)
  93. {
  94. GenericCodeEditorComponent* ed = nullptr;
  95. const File file (document->getFile());
  96. if (fileNeedsCppSyntaxHighlighting (file))
  97. {
  98. ed = new CppCodeEditorComponent (file, codeDocument);
  99. }
  100. else
  101. {
  102. CodeTokeniser* tokeniser = nullptr;
  103. if (file.hasFileExtension ("xml;svg"))
  104. {
  105. static XmlTokeniser xmlTokeniser;
  106. tokeniser = &xmlTokeniser;
  107. }
  108. if (file.hasFileExtension ("lua"))
  109. {
  110. static LuaTokeniser luaTokeniser;
  111. tokeniser = &luaTokeniser;
  112. }
  113. ed = new GenericCodeEditorComponent (file, codeDocument, tokeniser);
  114. }
  115. setEditor (ed);
  116. }
  117. SourceCodeEditor::SourceCodeEditor (OpenDocumentManager::Document* doc, GenericCodeEditorComponent* ed)
  118. : DocumentEditorComponent (doc)
  119. {
  120. setEditor (ed);
  121. }
  122. SourceCodeEditor::~SourceCodeEditor()
  123. {
  124. if (editor != nullptr)
  125. editor->getDocument().removeListener (this);
  126. getAppSettings().appearance.settings.removeListener (this);
  127. if (SourceCodeDocument* doc = dynamic_cast<SourceCodeDocument*> (getDocument()))
  128. doc->updateLastState (*editor);
  129. }
  130. void SourceCodeEditor::setEditor (GenericCodeEditorComponent* newEditor)
  131. {
  132. if (editor != nullptr)
  133. editor->getDocument().removeListener (this);
  134. addAndMakeVisible (editor = newEditor);
  135. editor->setFont (AppearanceSettings::getDefaultCodeFont());
  136. editor->setTabSize (4, true);
  137. updateColourScheme();
  138. getAppSettings().appearance.settings.addListener (this);
  139. editor->getDocument().addListener (this);
  140. }
  141. void SourceCodeEditor::scrollToKeepRangeOnScreen (Range<int> range)
  142. {
  143. const int space = jmin (10, editor->getNumLinesOnScreen() / 3);
  144. const CodeDocument::Position start (editor->getDocument(), range.getStart());
  145. const CodeDocument::Position end (editor->getDocument(), range.getEnd());
  146. editor->scrollToKeepLinesOnScreen (Range<int> (start.getLineNumber() - space, end.getLineNumber() + space));
  147. }
  148. void SourceCodeEditor::highlight (Range<int> range, bool cursorAtStart)
  149. {
  150. scrollToKeepRangeOnScreen (range);
  151. if (cursorAtStart)
  152. {
  153. editor->moveCaretTo (CodeDocument::Position (editor->getDocument(), range.getEnd()), false);
  154. editor->moveCaretTo (CodeDocument::Position (editor->getDocument(), range.getStart()), true);
  155. }
  156. else
  157. {
  158. editor->setHighlightedRegion (range);
  159. }
  160. }
  161. void SourceCodeEditor::resized()
  162. {
  163. editor->setBounds (getLocalBounds());
  164. }
  165. void SourceCodeEditor::updateColourScheme() { getAppSettings().appearance.applyToCodeEditor (*editor); }
  166. void SourceCodeEditor::checkSaveState()
  167. {
  168. setEditedState (getDocument()->needsSaving());
  169. }
  170. void SourceCodeEditor::valueTreePropertyChanged (ValueTree&, const Identifier&) { updateColourScheme(); }
  171. void SourceCodeEditor::valueTreeChildAdded (ValueTree&, ValueTree&) { updateColourScheme(); }
  172. void SourceCodeEditor::valueTreeChildRemoved (ValueTree&, ValueTree&, int) { updateColourScheme(); }
  173. void SourceCodeEditor::valueTreeChildOrderChanged (ValueTree&, int, int) { updateColourScheme(); }
  174. void SourceCodeEditor::valueTreeParentChanged (ValueTree&) { updateColourScheme(); }
  175. void SourceCodeEditor::valueTreeRedirected (ValueTree&) { updateColourScheme(); }
  176. void SourceCodeEditor::codeDocumentTextInserted (const String&, int) { checkSaveState(); }
  177. void SourceCodeEditor::codeDocumentTextDeleted (int, int) { checkSaveState(); }
  178. //==============================================================================
  179. GenericCodeEditorComponent::GenericCodeEditorComponent (const File& f, CodeDocument& codeDocument,
  180. CodeTokeniser* tokeniser)
  181. : CodeEditorComponent (codeDocument, tokeniser), file (f)
  182. {
  183. setCommandManager (&ProjucerApplication::getCommandManager());
  184. }
  185. GenericCodeEditorComponent::~GenericCodeEditorComponent() {}
  186. enum
  187. {
  188. showInFinderID = 0x2fe821e3,
  189. insertComponentID = 0x2fe821e4
  190. };
  191. void GenericCodeEditorComponent::addPopupMenuItems (PopupMenu& menu, const MouseEvent* e)
  192. {
  193. menu.addItem (showInFinderID,
  194. #if JUCE_MAC
  195. "Reveal " + file.getFileName() + " in Finder");
  196. #else
  197. "Reveal " + file.getFileName() + " in Explorer");
  198. #endif
  199. menu.addSeparator();
  200. CodeEditorComponent::addPopupMenuItems (menu, e);
  201. }
  202. void GenericCodeEditorComponent::performPopupMenuAction (int menuItemID)
  203. {
  204. if (menuItemID == showInFinderID)
  205. file.revealToUser();
  206. else
  207. CodeEditorComponent::performPopupMenuAction (menuItemID);
  208. }
  209. void GenericCodeEditorComponent::getAllCommands (Array <CommandID>& commands)
  210. {
  211. CodeEditorComponent::getAllCommands (commands);
  212. const CommandID ids[] = { CommandIDs::showFindPanel,
  213. CommandIDs::findSelection,
  214. CommandIDs::findNext,
  215. CommandIDs::findPrevious };
  216. commands.addArray (ids, numElementsInArray (ids));
  217. }
  218. void GenericCodeEditorComponent::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  219. {
  220. const bool anythingSelected = isHighlightActive();
  221. switch (commandID)
  222. {
  223. case CommandIDs::showFindPanel:
  224. result.setInfo (TRANS ("Find"), TRANS ("Searches for text in the current document."), "Editing", 0);
  225. result.defaultKeypresses.add (KeyPress ('f', ModifierKeys::commandModifier, 0));
  226. break;
  227. case CommandIDs::findSelection:
  228. result.setInfo (TRANS ("Find Selection"), TRANS ("Searches for the currently selected text."), "Editing", 0);
  229. result.setActive (anythingSelected);
  230. result.defaultKeypresses.add (KeyPress ('l', ModifierKeys::commandModifier, 0));
  231. break;
  232. case CommandIDs::findNext:
  233. result.setInfo (TRANS ("Find Next"), TRANS ("Searches for the next occurrence of the current search-term."), "Editing", 0);
  234. result.defaultKeypresses.add (KeyPress ('g', ModifierKeys::commandModifier, 0));
  235. break;
  236. case CommandIDs::findPrevious:
  237. result.setInfo (TRANS ("Find Previous"), TRANS ("Searches for the previous occurrence of the current search-term."), "Editing", 0);
  238. result.defaultKeypresses.add (KeyPress ('g', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0));
  239. result.defaultKeypresses.add (KeyPress ('d', ModifierKeys::commandModifier, 0));
  240. break;
  241. default:
  242. CodeEditorComponent::getCommandInfo (commandID, result);
  243. break;
  244. }
  245. }
  246. bool GenericCodeEditorComponent::perform (const InvocationInfo& info)
  247. {
  248. switch (info.commandID)
  249. {
  250. case CommandIDs::showFindPanel: showFindPanel(); return true;
  251. case CommandIDs::findSelection: findSelection(); return true;
  252. case CommandIDs::findNext: findNext (true, true); return true;
  253. case CommandIDs::findPrevious: findNext (false, false); return true;
  254. default: break;
  255. }
  256. return CodeEditorComponent::perform (info);
  257. }
  258. void GenericCodeEditorComponent::addListener (GenericCodeEditorComponent::Listener* listener)
  259. {
  260. listeners.add (listener);
  261. }
  262. void GenericCodeEditorComponent::removeListener (GenericCodeEditorComponent::Listener* listener)
  263. {
  264. listeners.remove (listener);
  265. }
  266. //==============================================================================
  267. class GenericCodeEditorComponent::FindPanel : public Component,
  268. private TextEditor::Listener,
  269. private ButtonListener
  270. {
  271. public:
  272. FindPanel()
  273. : caseButton ("Case-sensitive"),
  274. findPrev ("<"),
  275. findNext (">")
  276. {
  277. editor.setColour (CaretComponent::caretColourId, Colours::black);
  278. addAndMakeVisible (editor);
  279. label.setText ("Find:", dontSendNotification);
  280. label.setColour (Label::textColourId, Colours::white);
  281. label.attachToComponent (&editor, false);
  282. addAndMakeVisible (caseButton);
  283. caseButton.setColour (ToggleButton::textColourId, Colours::white);
  284. caseButton.setToggleState (isCaseSensitiveSearch(), dontSendNotification);
  285. caseButton.addListener (this);
  286. findPrev.setConnectedEdges (Button::ConnectedOnRight);
  287. findNext.setConnectedEdges (Button::ConnectedOnLeft);
  288. addAndMakeVisible (findPrev);
  289. addAndMakeVisible (findNext);
  290. setWantsKeyboardFocus (false);
  291. setFocusContainer (true);
  292. findPrev.setWantsKeyboardFocus (false);
  293. findNext.setWantsKeyboardFocus (false);
  294. editor.setText (getSearchString());
  295. editor.addListener (this);
  296. }
  297. void setCommandManager (ApplicationCommandManager* cm)
  298. {
  299. findPrev.setCommandToTrigger (cm, CommandIDs::findPrevious, true);
  300. findNext.setCommandToTrigger (cm, CommandIDs::findNext, true);
  301. }
  302. void paint (Graphics& g) override
  303. {
  304. Path outline;
  305. outline.addRoundedRectangle (1.0f, 1.0f, getWidth() - 2.0f, getHeight() - 2.0f, 8.0f);
  306. g.setColour (Colours::black.withAlpha (0.6f));
  307. g.fillPath (outline);
  308. g.setColour (Colours::white.withAlpha (0.8f));
  309. g.strokePath (outline, PathStrokeType (1.0f));
  310. }
  311. void resized() override
  312. {
  313. int y = 30;
  314. editor.setBounds (10, y, getWidth() - 20, 24);
  315. y += 30;
  316. caseButton.setBounds (10, y, getWidth() / 2 - 10, 22);
  317. findNext.setBounds (getWidth() - 40, y, 30, 22);
  318. findPrev.setBounds (getWidth() - 70, y, 30, 22);
  319. }
  320. void buttonClicked (Button*) override
  321. {
  322. setCaseSensitiveSearch (caseButton.getToggleState());
  323. }
  324. void textEditorTextChanged (TextEditor&) override
  325. {
  326. setSearchString (editor.getText());
  327. if (GenericCodeEditorComponent* ed = getOwner())
  328. ed->findNext (true, false);
  329. }
  330. void textEditorFocusLost (TextEditor&) override {}
  331. void textEditorReturnKeyPressed (TextEditor&) override
  332. {
  333. ProjucerApplication::getCommandManager().invokeDirectly (CommandIDs::findNext, true);
  334. }
  335. void textEditorEscapeKeyPressed (TextEditor&) override
  336. {
  337. if (GenericCodeEditorComponent* ed = getOwner())
  338. ed->hideFindPanel();
  339. }
  340. GenericCodeEditorComponent* getOwner() const
  341. {
  342. return findParentComponentOfClass <GenericCodeEditorComponent>();
  343. }
  344. TextEditor editor;
  345. Label label;
  346. ToggleButton caseButton;
  347. TextButton findPrev, findNext;
  348. };
  349. void GenericCodeEditorComponent::resized()
  350. {
  351. CodeEditorComponent::resized();
  352. if (findPanel != nullptr)
  353. {
  354. findPanel->setSize (jmin (260, getWidth() - 32), 100);
  355. findPanel->setTopRightPosition (getWidth() - 16, 8);
  356. }
  357. }
  358. void GenericCodeEditorComponent::showFindPanel()
  359. {
  360. if (findPanel == nullptr)
  361. {
  362. findPanel = new FindPanel();
  363. findPanel->setCommandManager (&ProjucerApplication::getCommandManager());
  364. addAndMakeVisible (findPanel);
  365. resized();
  366. }
  367. if (findPanel != nullptr)
  368. {
  369. findPanel->editor.grabKeyboardFocus();
  370. findPanel->editor.selectAll();
  371. }
  372. }
  373. void GenericCodeEditorComponent::hideFindPanel()
  374. {
  375. findPanel = nullptr;
  376. }
  377. void GenericCodeEditorComponent::findSelection()
  378. {
  379. const String selected (getTextInRange (getHighlightedRegion()));
  380. if (selected.isNotEmpty())
  381. {
  382. setSearchString (selected);
  383. findNext (true, true);
  384. }
  385. }
  386. void GenericCodeEditorComponent::findNext (bool forwards, bool skipCurrentSelection)
  387. {
  388. const Range<int> highlight (getHighlightedRegion());
  389. const CodeDocument::Position startPos (getDocument(), skipCurrentSelection ? highlight.getEnd()
  390. : highlight.getStart());
  391. int lineNum = startPos.getLineNumber();
  392. int linePos = startPos.getIndexInLine();
  393. const int totalLines = getDocument().getNumLines();
  394. const String searchText (getSearchString());
  395. const bool caseSensitive = isCaseSensitiveSearch();
  396. for (int linesToSearch = totalLines; --linesToSearch >= 0;)
  397. {
  398. String line (getDocument().getLine (lineNum));
  399. int index;
  400. if (forwards)
  401. {
  402. index = caseSensitive ? line.indexOf (linePos, searchText)
  403. : line.indexOfIgnoreCase (linePos, searchText);
  404. }
  405. else
  406. {
  407. if (linePos >= 0)
  408. line = line.substring (0, linePos);
  409. index = caseSensitive ? line.lastIndexOf (searchText)
  410. : line.lastIndexOfIgnoreCase (searchText);
  411. }
  412. if (index >= 0)
  413. {
  414. const CodeDocument::Position p (getDocument(), lineNum, index);
  415. selectRegion (p, p.movedBy (searchText.length()));
  416. break;
  417. }
  418. if (forwards)
  419. {
  420. linePos = 0;
  421. lineNum = (lineNum + 1) % totalLines;
  422. }
  423. else
  424. {
  425. if (--lineNum < 0)
  426. lineNum = totalLines - 1;
  427. linePos = -1;
  428. }
  429. }
  430. }
  431. void GenericCodeEditorComponent::handleEscapeKey()
  432. {
  433. CodeEditorComponent::handleEscapeKey();
  434. hideFindPanel();
  435. }
  436. void GenericCodeEditorComponent::editorViewportPositionChanged()
  437. {
  438. CodeEditorComponent::editorViewportPositionChanged();
  439. listeners.call (&Listener::codeEditorViewportMoved, *this);
  440. }
  441. //==============================================================================
  442. static CPlusPlusCodeTokeniser cppTokeniser;
  443. CppCodeEditorComponent::CppCodeEditorComponent (const File& f, CodeDocument& doc)
  444. : GenericCodeEditorComponent (f, doc, &cppTokeniser)
  445. {
  446. }
  447. CppCodeEditorComponent::~CppCodeEditorComponent() {}
  448. void CppCodeEditorComponent::handleReturnKey()
  449. {
  450. GenericCodeEditorComponent::handleReturnKey();
  451. CodeDocument::Position pos (getCaretPos());
  452. String blockIndent, lastLineIndent;
  453. CodeHelpers::getIndentForCurrentBlock (pos, getTabString (getTabSize()), blockIndent, lastLineIndent);
  454. const String remainderOfBrokenLine (pos.getLineText());
  455. const int numLeadingWSChars = CodeHelpers::getLeadingWhitespace (remainderOfBrokenLine).length();
  456. if (numLeadingWSChars > 0)
  457. getDocument().deleteSection (pos, pos.movedBy (numLeadingWSChars));
  458. if (remainderOfBrokenLine.trimStart().startsWithChar ('}'))
  459. insertTextAtCaret (blockIndent);
  460. else
  461. insertTextAtCaret (lastLineIndent);
  462. const String previousLine (pos.movedByLines (-1).getLineText());
  463. const String trimmedPreviousLine (previousLine.trim());
  464. if ((trimmedPreviousLine.startsWith ("if ")
  465. || trimmedPreviousLine.startsWith ("if(")
  466. || trimmedPreviousLine.startsWith ("for ")
  467. || trimmedPreviousLine.startsWith ("for(")
  468. || trimmedPreviousLine.startsWith ("while(")
  469. || trimmedPreviousLine.startsWith ("while "))
  470. && trimmedPreviousLine.endsWithChar (')'))
  471. {
  472. insertTabAtCaret();
  473. }
  474. }
  475. void CppCodeEditorComponent::insertTextAtCaret (const String& newText)
  476. {
  477. if (getHighlightedRegion().isEmpty())
  478. {
  479. const CodeDocument::Position pos (getCaretPos());
  480. if ((newText == "{" || newText == "}")
  481. && pos.getLineNumber() > 0
  482. && pos.getLineText().trim().isEmpty())
  483. {
  484. moveCaretToStartOfLine (true);
  485. String blockIndent, lastLineIndent;
  486. if (CodeHelpers::getIndentForCurrentBlock (pos, getTabString (getTabSize()), blockIndent, lastLineIndent))
  487. {
  488. GenericCodeEditorComponent::insertTextAtCaret (blockIndent);
  489. if (newText == "{")
  490. insertTabAtCaret();
  491. }
  492. }
  493. }
  494. GenericCodeEditorComponent::insertTextAtCaret (newText);
  495. }
  496. void CppCodeEditorComponent::addPopupMenuItems (PopupMenu& menu, const MouseEvent* e)
  497. {
  498. GenericCodeEditorComponent::addPopupMenuItems (menu, e);
  499. menu.addSeparator();
  500. menu.addItem (insertComponentID, TRANS("Insert code for a new Component class..."));
  501. }
  502. void CppCodeEditorComponent::performPopupMenuAction (int menuItemID)
  503. {
  504. if (menuItemID == insertComponentID)
  505. insertComponentClass();
  506. GenericCodeEditorComponent::performPopupMenuAction (menuItemID);
  507. }
  508. void CppCodeEditorComponent::insertComponentClass()
  509. {
  510. AlertWindow aw (TRANS ("Insert a new Component class"),
  511. TRANS ("Please enter a name for the new class"),
  512. AlertWindow::NoIcon, nullptr);
  513. const char* classNameField = "Class Name";
  514. aw.addTextEditor (classNameField, String(), String(), false);
  515. aw.addButton (TRANS ("Insert Code"), 1, KeyPress (KeyPress::returnKey));
  516. aw.addButton (TRANS ("Cancel"), 0, KeyPress (KeyPress::escapeKey));
  517. while (aw.runModalLoop() != 0)
  518. {
  519. const String className (aw.getTextEditorContents (classNameField).trim());
  520. if (className == CodeHelpers::makeValidIdentifier (className, false, true, false))
  521. {
  522. String code (BinaryData::jucer_InlineComponentTemplate_h);
  523. code = code.replace ("COMPONENTCLASS", className);
  524. insertTextAtCaret (code);
  525. break;
  526. }
  527. }
  528. }