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.

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