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.

619 lines
20KB

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