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.

621 lines
20KB

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