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.

1209 lines
45KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../../jucer_Headers.h"
  20. #include "../../Application/jucer_AppearanceSettings.h"
  21. #include "../../Application/jucer_GlobalPreferences.h"
  22. #include "../../Application/jucer_Application.h"
  23. #include "jucer_JucerDocumentEditor.h"
  24. #include "jucer_TestComponent.h"
  25. #include "../jucer_ObjectTypes.h"
  26. #include "jucer_ComponentLayoutPanel.h"
  27. #include "jucer_PaintRoutinePanel.h"
  28. #include "jucer_ResourceEditorPanel.h"
  29. #include "../properties/jucer_ComponentTextProperty.h"
  30. #include "../properties/jucer_ComponentChoiceProperty.h"
  31. #include "../ui/jucer_JucerCommandIDs.h"
  32. //==============================================================================
  33. class ExtraMethodsList : public PropertyComponent,
  34. public ListBoxModel,
  35. public ChangeListener
  36. {
  37. public:
  38. ExtraMethodsList (JucerDocument& doc)
  39. : PropertyComponent ("extra callbacks", 250),
  40. document (doc)
  41. {
  42. addAndMakeVisible (listBox = new ListBox (String(), this));
  43. listBox->setRowHeight (22);
  44. document.addChangeListener (this);
  45. }
  46. ~ExtraMethodsList()
  47. {
  48. document.removeChangeListener (this);
  49. }
  50. int getNumRows() override
  51. {
  52. return methods.size();
  53. }
  54. void paintListBoxItem (int row, Graphics& g, int width, int height, bool rowIsSelected) override
  55. {
  56. if (row < 0 || row >= getNumRows())
  57. return;
  58. if (rowIsSelected)
  59. {
  60. g.fillAll (findColour (TextEditor::highlightColourId));
  61. g.setColour (findColour (defaultHighlightedTextColourId));
  62. }
  63. else
  64. {
  65. g.setColour (findColour (defaultTextColourId));
  66. }
  67. g.setFont (height * 0.6f);
  68. g.drawText (returnValues [row] + " " + baseClasses [row] + "::" + methods [row],
  69. 30, 0, width - 32, height,
  70. Justification::centredLeft, true);
  71. getLookAndFeel().drawTickBox (g, *this, 6, 2, 18, 18, document.isOptionalMethodEnabled (methods [row]), true, false, false);
  72. }
  73. void listBoxItemClicked (int row, const MouseEvent& e) override
  74. {
  75. if (row < 0 || row >= getNumRows())
  76. return;
  77. if (e.x < 30)
  78. document.setOptionalMethodEnabled (methods [row],
  79. ! document.isOptionalMethodEnabled (methods [row]));
  80. }
  81. void paint (Graphics& g) override
  82. {
  83. g.fillAll (Colours::white);
  84. }
  85. void resized() override
  86. {
  87. listBox->setBounds (getLocalBounds());
  88. }
  89. void refresh() override
  90. {
  91. baseClasses.clear();
  92. returnValues.clear();
  93. methods.clear();
  94. initialContents.clear();
  95. document.getOptionalMethods (baseClasses, returnValues, methods, initialContents);
  96. listBox->updateContent();
  97. listBox->repaint();
  98. }
  99. void changeListenerCallback (ChangeBroadcaster*) override
  100. {
  101. refresh();
  102. }
  103. private:
  104. JucerDocument& document;
  105. ScopedPointer<ListBox> listBox;
  106. StringArray baseClasses, returnValues, methods, initialContents;
  107. };
  108. //==============================================================================
  109. class ClassPropertiesPanel : public Component,
  110. private ChangeListener
  111. {
  112. public:
  113. ClassPropertiesPanel (JucerDocument& doc)
  114. : document (doc)
  115. {
  116. addAndMakeVisible (panel1);
  117. addAndMakeVisible (panel2);
  118. Array <PropertyComponent*> props;
  119. props.add (new ComponentClassNameProperty (doc));
  120. props.add (new TemplateFileProperty (doc));
  121. props.add (new ComponentCompNameProperty (doc));
  122. props.add (new ComponentParentClassesProperty (doc));
  123. props.add (new ComponentConstructorParamsProperty (doc));
  124. props.add (new ComponentInitialisersProperty (doc));
  125. props.add (new ComponentInitialSizeProperty (doc, true));
  126. props.add (new ComponentInitialSizeProperty (doc, false));
  127. props.add (new FixedSizeProperty (doc));
  128. panel1.addSection ("General class settings", props);
  129. Array <PropertyComponent*> props2;
  130. props2.add (new ExtraMethodsList (doc));
  131. panel2.addSection ("Extra callback methods to generate", props2);
  132. doc.addExtraClassProperties (panel1);
  133. doc.addChangeListener (this);
  134. }
  135. ~ClassPropertiesPanel()
  136. {
  137. document.removeChangeListener (this);
  138. }
  139. void resized() override
  140. {
  141. int pw = jmin (getWidth() / 2 - 20, 350);
  142. panel1.setBounds (10, 6, pw, getHeight() - 12);
  143. panel2.setBounds (panel1.getRight() + 20, panel1.getY(), pw, panel1.getHeight());
  144. }
  145. void paint (Graphics& g) override
  146. {
  147. g.fillAll (findColour (secondaryBackgroundColourId));
  148. }
  149. void changeListenerCallback (ChangeBroadcaster*) override
  150. {
  151. panel1.refreshAll();
  152. panel2.refreshAll();
  153. }
  154. private:
  155. JucerDocument& document;
  156. PropertyPanel panel1, panel2;
  157. //==============================================================================
  158. class ComponentClassNameProperty : public ComponentTextProperty <Component>
  159. {
  160. public:
  161. ComponentClassNameProperty (JucerDocument& doc)
  162. : ComponentTextProperty <Component> ("Class name", 128, false, 0, doc)
  163. {}
  164. void setText (const String& newText) override { document.setClassName (newText); }
  165. String getText() const override { return document.getClassName(); }
  166. };
  167. //==============================================================================
  168. class ComponentCompNameProperty : public ComponentTextProperty <Component>
  169. {
  170. public:
  171. ComponentCompNameProperty (JucerDocument& doc)
  172. : ComponentTextProperty <Component> ("Component name", 200, false, 0, doc)
  173. {}
  174. void setText (const String& newText) override { document.setComponentName (newText); }
  175. String getText() const override { return document.getComponentName(); }
  176. };
  177. //==============================================================================
  178. class ComponentParentClassesProperty : public ComponentTextProperty <Component>
  179. {
  180. public:
  181. ComponentParentClassesProperty (JucerDocument& doc)
  182. : ComponentTextProperty <Component> ("Parent classes", 512, false, 0, doc)
  183. {}
  184. void setText (const String& newText) override { document.setParentClasses (newText); }
  185. String getText() const override { return document.getParentClassString(); }
  186. };
  187. //==============================================================================
  188. class ComponentConstructorParamsProperty : public ComponentTextProperty <Component>
  189. {
  190. public:
  191. ComponentConstructorParamsProperty (JucerDocument& doc)
  192. : ComponentTextProperty <Component> ("Constructor params", 2048, false, 0, doc)
  193. {}
  194. void setText (const String& newText) override { document.setConstructorParams (newText); }
  195. String getText() const override { return document.getConstructorParams(); }
  196. };
  197. //==============================================================================
  198. class ComponentInitialisersProperty : public ComponentTextProperty <Component>
  199. {
  200. public:
  201. ComponentInitialisersProperty (JucerDocument& doc)
  202. : ComponentTextProperty <Component> ("Member initialisers", 16384, true, 0, doc)
  203. {
  204. preferredHeight = 24 * 3;
  205. }
  206. void setText (const String& newText) override { document.setVariableInitialisers (newText); }
  207. String getText() const override { return document.getVariableInitialisers(); }
  208. };
  209. //==============================================================================
  210. class ComponentInitialSizeProperty : public ComponentTextProperty <Component>
  211. {
  212. public:
  213. ComponentInitialSizeProperty (JucerDocument& doc, const bool isWidth_)
  214. : ComponentTextProperty <Component> (isWidth_ ? "Initial width"
  215. : "Initial height",
  216. 10, false, 0, doc),
  217. isWidth (isWidth_)
  218. {}
  219. void setText (const String& newText) override
  220. {
  221. if (isWidth)
  222. document.setInitialSize (newText.getIntValue(), document.getInitialHeight());
  223. else
  224. document.setInitialSize (document.getInitialWidth(), newText.getIntValue());
  225. }
  226. String getText() const override
  227. {
  228. return String (isWidth ? document.getInitialWidth()
  229. : document.getInitialHeight());
  230. }
  231. private:
  232. const bool isWidth;
  233. };
  234. //==============================================================================
  235. class FixedSizeProperty : public ComponentChoiceProperty <Component>
  236. {
  237. public:
  238. FixedSizeProperty (JucerDocument& doc)
  239. : ComponentChoiceProperty <Component> ("Fixed size", 0, doc)
  240. {
  241. choices.add ("Resize component to fit workspace");
  242. choices.add ("Keep component size fixed");
  243. }
  244. void setIndex (int newIndex) { document.setFixedSize (newIndex != 0); }
  245. int getIndex() const { return document.isFixedSize() ? 1 : 0; }
  246. };
  247. //==============================================================================
  248. class TemplateFileProperty : public ComponentTextProperty <Component>
  249. {
  250. public:
  251. TemplateFileProperty (JucerDocument& doc)
  252. : ComponentTextProperty <Component> ("Template file", 2048, false, 0, doc)
  253. {}
  254. void setText (const String& newText) override { document.setTemplateFile (newText); }
  255. String getText() const override { return document.getTemplateFile(); }
  256. };
  257. };
  258. static const Colour tabColour (Colour (0xff888888));
  259. static SourceCodeEditor* createCodeEditor (const File& file, SourceCodeDocument& sourceCodeDoc)
  260. {
  261. return new SourceCodeEditor (&sourceCodeDoc,
  262. new CppCodeEditorComponent (file, sourceCodeDoc.getCodeDocument()));
  263. }
  264. //==============================================================================
  265. JucerDocumentEditor::JucerDocumentEditor (JucerDocument* const doc)
  266. : document (doc),
  267. tabbedComponent (doc)
  268. {
  269. setOpaque (true);
  270. if (document != nullptr)
  271. {
  272. setSize (document->getInitialWidth(),
  273. document->getInitialHeight());
  274. addAndMakeVisible (tabbedComponent);
  275. tabbedComponent.setOutline (0);
  276. tabbedComponent.addTab ("Class", tabColour, new ClassPropertiesPanel (*document), true);
  277. if (document->getComponentLayout() != nullptr)
  278. tabbedComponent.addTab ("Subcomponents", tabColour,
  279. compLayoutPanel = new ComponentLayoutPanel (*document, *document->getComponentLayout()), true);
  280. tabbedComponent.addTab ("Resources", tabColour, new ResourceEditorPanel (*document), true);
  281. tabbedComponent.addTab ("Code", tabColour, createCodeEditor (document->getCppFile(),
  282. document->getCppDocument()), true);
  283. updateTabs();
  284. tabbedComponent.setCurrentTabIndex (1);
  285. document->addChangeListener (this);
  286. resized();
  287. refreshPropertiesPanel();
  288. changeListenerCallback (nullptr);
  289. }
  290. }
  291. JucerDocumentEditor::~JucerDocumentEditor()
  292. {
  293. tabbedComponent.clearTabs();
  294. }
  295. void JucerDocumentEditor::refreshPropertiesPanel() const
  296. {
  297. for (int i = tabbedComponent.getNumTabs(); --i >= 0;)
  298. {
  299. if (ComponentLayoutPanel* layoutPanel = dynamic_cast<ComponentLayoutPanel*> (tabbedComponent.getTabContentComponent (i)))
  300. {
  301. if (layoutPanel->isVisible())
  302. layoutPanel->updatePropertiesList();
  303. }
  304. else
  305. {
  306. if (PaintRoutinePanel* pr = dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (i)))
  307. if (pr->isVisible())
  308. pr->updatePropertiesList();
  309. }
  310. }
  311. }
  312. void JucerDocumentEditor::updateTabs()
  313. {
  314. const StringArray paintRoutineNames (document->getPaintRoutineNames());
  315. for (int i = tabbedComponent.getNumTabs(); --i >= 0;)
  316. {
  317. if (dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (i)) != 0
  318. && ! paintRoutineNames.contains (tabbedComponent.getTabNames() [i]))
  319. {
  320. tabbedComponent.removeTab (i);
  321. }
  322. }
  323. for (int i = 0; i < document->getNumPaintRoutines(); ++i)
  324. {
  325. if (! tabbedComponent.getTabNames().contains (paintRoutineNames [i]))
  326. {
  327. int index, numPaintRoutinesSeen = 0;
  328. for (index = 1; index < tabbedComponent.getNumTabs(); ++index)
  329. {
  330. if (dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (index)) != nullptr)
  331. {
  332. if (++numPaintRoutinesSeen == i)
  333. {
  334. ++index;
  335. break;
  336. }
  337. }
  338. }
  339. if (numPaintRoutinesSeen == 0)
  340. index = document->getComponentLayout() != nullptr ? 2 : 1;
  341. tabbedComponent.addTab (paintRoutineNames[i], tabColour,
  342. new PaintRoutinePanel (*document,
  343. *document->getPaintRoutine (i),
  344. this), true, index);
  345. }
  346. }
  347. }
  348. //==============================================================================
  349. void JucerDocumentEditor::paint (Graphics& g)
  350. {
  351. g.fillAll (findColour (backgroundColourId));
  352. }
  353. void JucerDocumentEditor::resized()
  354. {
  355. tabbedComponent.setBounds (getLocalBounds().withTrimmedLeft (12));
  356. }
  357. void JucerDocumentEditor::changeListenerCallback (ChangeBroadcaster*)
  358. {
  359. setName (document->getClassName());
  360. updateTabs();
  361. }
  362. //==============================================================================
  363. ApplicationCommandTarget* JucerDocumentEditor::getNextCommandTarget()
  364. {
  365. return findFirstTargetParentComponent();
  366. }
  367. ComponentLayout* JucerDocumentEditor::getCurrentLayout() const
  368. {
  369. if (ComponentLayoutPanel* panel = dynamic_cast<ComponentLayoutPanel*> (tabbedComponent.getCurrentContentComponent()))
  370. return &(panel->layout);
  371. return nullptr;
  372. }
  373. PaintRoutine* JucerDocumentEditor::getCurrentPaintRoutine() const
  374. {
  375. if (PaintRoutinePanel* panel = dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getCurrentContentComponent()))
  376. return &(panel->getPaintRoutine());
  377. return nullptr;
  378. }
  379. void JucerDocumentEditor::showLayout()
  380. {
  381. if (getCurrentLayout() == nullptr)
  382. {
  383. for (int i = 0; i < tabbedComponent.getNumTabs(); ++i)
  384. {
  385. if (dynamic_cast<ComponentLayoutPanel*> (tabbedComponent.getTabContentComponent (i)) != nullptr)
  386. {
  387. tabbedComponent.setCurrentTabIndex (i);
  388. break;
  389. }
  390. }
  391. }
  392. }
  393. void JucerDocumentEditor::showGraphics (PaintRoutine* routine)
  394. {
  395. if (getCurrentPaintRoutine() != routine || routine == 0)
  396. {
  397. for (int i = 0; i < tabbedComponent.getNumTabs(); ++i)
  398. {
  399. if (PaintRoutinePanel* pr = dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getTabContentComponent (i)))
  400. {
  401. if (routine == &(pr->getPaintRoutine()) || routine == nullptr)
  402. {
  403. tabbedComponent.setCurrentTabIndex (i);
  404. break;
  405. }
  406. }
  407. }
  408. }
  409. }
  410. //==============================================================================
  411. void JucerDocumentEditor::setViewportToLastPos (Viewport* vp, EditingPanelBase& editor)
  412. {
  413. vp->setViewPosition (lastViewportX, lastViewportY);
  414. editor.setZoom (currentZoomLevel);
  415. }
  416. void JucerDocumentEditor::storeLastViewportPos (Viewport* vp, EditingPanelBase& editor)
  417. {
  418. lastViewportX = vp->getViewPositionX();
  419. lastViewportY = vp->getViewPositionY();
  420. currentZoomLevel = editor.getZoom();
  421. }
  422. void JucerDocumentEditor::setZoom (double scale)
  423. {
  424. scale = jlimit (1.0 / 4.0, 32.0, scale);
  425. if (EditingPanelBase* panel = dynamic_cast<EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  426. panel->setZoom (scale);
  427. }
  428. double JucerDocumentEditor::getZoom() const
  429. {
  430. if (EditingPanelBase* panel = dynamic_cast<EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  431. return panel->getZoom();
  432. return 1.0;
  433. }
  434. static double snapToIntegerZoom (double zoom)
  435. {
  436. if (zoom >= 1.0)
  437. return (double) (int) (zoom + 0.5);
  438. return 1.0 / (int) (1.0 / zoom + 0.5);
  439. }
  440. void JucerDocumentEditor::addElement (const int index)
  441. {
  442. if (PaintRoutinePanel* const panel = dynamic_cast<PaintRoutinePanel*> (tabbedComponent.getCurrentContentComponent()))
  443. {
  444. PaintRoutine* const currentPaintRoutine = & (panel->getPaintRoutine());
  445. const Rectangle<int> area (panel->getComponentArea());
  446. document->beginTransaction();
  447. PaintElement* e = ObjectTypes::createNewElement (index, currentPaintRoutine);
  448. e->setInitialBounds (area.getWidth(), area.getHeight());
  449. e = currentPaintRoutine->addNewElement (e, -1, true);
  450. if (e != nullptr)
  451. {
  452. const int randomness = jmin (80, area.getWidth() / 2, area.getHeight() / 2);
  453. int x = area.getX() + area.getWidth() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  454. int y = area.getY() + area.getHeight() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  455. x = document->snapPosition (x);
  456. y = document->snapPosition (y);
  457. panel->xyToTargetXY (x, y);
  458. Rectangle<int> r (e->getCurrentBounds (area));
  459. r.setPosition (x, y);
  460. e->setCurrentBounds (r, area, true);
  461. currentPaintRoutine->getSelectedElements().selectOnly (e);
  462. }
  463. document->beginTransaction();
  464. }
  465. }
  466. void JucerDocumentEditor::addComponent (const int index)
  467. {
  468. showLayout();
  469. if (ComponentLayoutPanel* const panel = dynamic_cast<ComponentLayoutPanel*> (tabbedComponent.getCurrentContentComponent()))
  470. {
  471. const Rectangle<int> area (panel->getComponentArea());
  472. document->beginTransaction ("Add new " + ObjectTypes::componentTypeHandlers [index]->getTypeName());
  473. const int randomness = jmin (80, area.getWidth() / 2, area.getHeight() / 2);
  474. int x = area.getWidth() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  475. int y = area.getHeight() / 2 + Random::getSystemRandom().nextInt (randomness) - randomness / 2;
  476. x = document->snapPosition (x);
  477. y = document->snapPosition (y);
  478. panel->xyToTargetXY (x, y);
  479. if (Component* newOne = panel->layout.addNewComponent (ObjectTypes::componentTypeHandlers [index], x, y))
  480. panel->layout.getSelectedSet().selectOnly (newOne);
  481. document->beginTransaction();
  482. }
  483. }
  484. //==============================================================================
  485. bool JucerDocumentEditor::isSomethingSelected() const
  486. {
  487. if (ComponentLayout* layout = getCurrentLayout())
  488. return layout->getSelectedSet().getNumSelected() > 0;
  489. if (PaintRoutine* routine = getCurrentPaintRoutine())
  490. return routine->getSelectedElements().getNumSelected() > 0;
  491. return false;
  492. }
  493. //==============================================================================
  494. void JucerDocumentEditor::getAllCommands (Array <CommandID>& commands)
  495. {
  496. const CommandID ids[] =
  497. {
  498. JucerCommandIDs::test,
  499. JucerCommandIDs::toFront,
  500. JucerCommandIDs::toBack,
  501. JucerCommandIDs::group,
  502. JucerCommandIDs::ungroup,
  503. JucerCommandIDs::bringBackLostItems,
  504. JucerCommandIDs::enableSnapToGrid,
  505. JucerCommandIDs::showGrid,
  506. JucerCommandIDs::editCompLayout,
  507. JucerCommandIDs::editCompGraphics,
  508. JucerCommandIDs::zoomIn,
  509. JucerCommandIDs::zoomOut,
  510. JucerCommandIDs::zoomNormal,
  511. JucerCommandIDs::spaceBarDrag,
  512. JucerCommandIDs::compOverlay0,
  513. JucerCommandIDs::compOverlay33,
  514. JucerCommandIDs::compOverlay66,
  515. JucerCommandIDs::compOverlay100,
  516. StandardApplicationCommandIDs::undo,
  517. StandardApplicationCommandIDs::redo,
  518. StandardApplicationCommandIDs::cut,
  519. StandardApplicationCommandIDs::copy,
  520. StandardApplicationCommandIDs::paste,
  521. StandardApplicationCommandIDs::del,
  522. StandardApplicationCommandIDs::selectAll,
  523. StandardApplicationCommandIDs::deselectAll
  524. };
  525. commands.addArray (ids, numElementsInArray (ids));
  526. for (int i = 0; i < ObjectTypes::numComponentTypes; ++i)
  527. commands.add (JucerCommandIDs::newComponentBase + i);
  528. for (int i = 0; i < ObjectTypes::numElementTypes; ++i)
  529. commands.add (JucerCommandIDs::newElementBase + i);
  530. }
  531. void JucerDocumentEditor::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  532. {
  533. ComponentLayout* const currentLayout = getCurrentLayout();
  534. PaintRoutine* const currentPaintRoutine = getCurrentPaintRoutine();
  535. const int cmd = ModifierKeys::commandModifier;
  536. const int shift = ModifierKeys::shiftModifier;
  537. if (commandID >= JucerCommandIDs::newComponentBase
  538. && commandID < JucerCommandIDs::newComponentBase + ObjectTypes::numComponentTypes)
  539. {
  540. const int index = commandID - JucerCommandIDs::newComponentBase;
  541. result.setInfo ("New " + ObjectTypes::componentTypeHandlers [index]->getTypeName(),
  542. "Creates a new " + ObjectTypes::componentTypeHandlers [index]->getTypeName(),
  543. CommandCategories::editing, 0);
  544. return;
  545. }
  546. if (commandID >= JucerCommandIDs::newElementBase
  547. && commandID < JucerCommandIDs::newElementBase + ObjectTypes::numElementTypes)
  548. {
  549. const int index = commandID - JucerCommandIDs::newElementBase;
  550. result.setInfo (String ("New ") + ObjectTypes::elementTypeNames [index],
  551. String ("Adds a new ") + ObjectTypes::elementTypeNames [index],
  552. CommandCategories::editing, 0);
  553. result.setActive (currentPaintRoutine != nullptr);
  554. return;
  555. }
  556. switch (commandID)
  557. {
  558. case JucerCommandIDs::toFront:
  559. result.setInfo (TRANS("Bring to front"), TRANS("Brings the currently selected component to the front."), CommandCategories::editing, 0);
  560. result.setActive (isSomethingSelected());
  561. result.defaultKeypresses.add (KeyPress ('f', cmd, 0));
  562. break;
  563. case JucerCommandIDs::toBack:
  564. result.setInfo (TRANS("Send to back"), TRANS("Sends the currently selected component to the back."), CommandCategories::editing, 0);
  565. result.setActive (isSomethingSelected());
  566. result.defaultKeypresses.add (KeyPress ('b', cmd, 0));
  567. break;
  568. case JucerCommandIDs::group:
  569. result.setInfo (TRANS("Group selected items"), TRANS("Turns the currently selected elements into a single group object."), CommandCategories::editing, 0);
  570. result.setActive (currentPaintRoutine != nullptr && currentPaintRoutine->getSelectedElements().getNumSelected() > 1);
  571. result.defaultKeypresses.add (KeyPress ('k', cmd, 0));
  572. break;
  573. case JucerCommandIDs::ungroup:
  574. result.setInfo (TRANS("Ungroup selected items"), TRANS("Turns the currently selected elements into a single group object."), CommandCategories::editing, 0);
  575. result.setActive (currentPaintRoutine != nullptr
  576. && currentPaintRoutine->getSelectedElements().getNumSelected() == 1
  577. && currentPaintRoutine->getSelectedElements().getSelectedItem (0)->getTypeName() == "Group");
  578. result.defaultKeypresses.add (KeyPress ('k', cmd | shift, 0));
  579. break;
  580. case JucerCommandIDs::test:
  581. result.setInfo (TRANS("Test component..."), TRANS("Runs the current component interactively."), CommandCategories::view, 0);
  582. result.defaultKeypresses.add (KeyPress ('t', cmd, 0));
  583. break;
  584. case JucerCommandIDs::enableSnapToGrid:
  585. result.setInfo (TRANS("Enable snap-to-grid"), TRANS("Toggles whether components' positions are aligned to a grid."), CommandCategories::view, 0);
  586. result.setTicked (document != nullptr && document->isSnapActive (false));
  587. result.defaultKeypresses.add (KeyPress ('g', cmd, 0));
  588. break;
  589. case JucerCommandIDs::showGrid:
  590. result.setInfo (TRANS("Show snap-to-grid"), TRANS("Toggles whether the snapping grid is displayed on-screen."), CommandCategories::view, 0);
  591. result.setTicked (document != nullptr && document->isSnapShown());
  592. result.defaultKeypresses.add (KeyPress ('g', cmd | shift, 0));
  593. break;
  594. case JucerCommandIDs::editCompLayout:
  595. result.setInfo (TRANS("Edit sub-component layout"), TRANS("Switches to the sub-component editor view."), CommandCategories::view, 0);
  596. result.setTicked (currentLayout != nullptr);
  597. result.defaultKeypresses.add (KeyPress ('n', cmd, 0));
  598. break;
  599. case JucerCommandIDs::editCompGraphics:
  600. result.setInfo (TRANS("Edit background graphics"), TRANS("Switches to the background graphics editor view."), CommandCategories::view, 0);
  601. result.setTicked (currentPaintRoutine != nullptr);
  602. result.defaultKeypresses.add (KeyPress ('m', cmd, 0));
  603. break;
  604. case JucerCommandIDs::bringBackLostItems:
  605. result.setInfo (TRANS("Retrieve offscreen items"), TRANS("Moves any items that are lost beyond the edges of the screen back to the centre."), CommandCategories::editing, 0);
  606. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  607. result.defaultKeypresses.add (KeyPress ('m', cmd, 0));
  608. break;
  609. case JucerCommandIDs::zoomIn:
  610. result.setInfo (TRANS("Zoom in"), TRANS("Zooms in on the current component."), CommandCategories::editing, 0);
  611. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  612. result.defaultKeypresses.add (KeyPress (']', cmd, 0));
  613. break;
  614. case JucerCommandIDs::zoomOut:
  615. result.setInfo (TRANS("Zoom out"), TRANS("Zooms out on the current component."), CommandCategories::editing, 0);
  616. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  617. result.defaultKeypresses.add (KeyPress ('[', cmd, 0));
  618. break;
  619. case JucerCommandIDs::zoomNormal:
  620. result.setInfo (TRANS("Zoom to 100%"), TRANS("Restores the zoom level to normal."), CommandCategories::editing, 0);
  621. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  622. result.defaultKeypresses.add (KeyPress ('1', cmd, 0));
  623. break;
  624. case JucerCommandIDs::spaceBarDrag:
  625. result.setInfo (TRANS("Scroll while dragging mouse"), TRANS("When held down, this key lets you scroll around by dragging with the mouse."),
  626. CommandCategories::view, ApplicationCommandInfo::wantsKeyUpDownCallbacks);
  627. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  628. result.defaultKeypresses.add (KeyPress (KeyPress::spaceKey, 0, 0));
  629. break;
  630. case JucerCommandIDs::compOverlay0:
  631. case JucerCommandIDs::compOverlay33:
  632. case JucerCommandIDs::compOverlay66:
  633. case JucerCommandIDs::compOverlay100:
  634. {
  635. int amount = 0, num = 0;
  636. if (commandID == JucerCommandIDs::compOverlay33)
  637. {
  638. amount = 33;
  639. num = 1;
  640. }
  641. else if (commandID == JucerCommandIDs::compOverlay66)
  642. {
  643. amount = 66;
  644. num = 2;
  645. }
  646. else if (commandID == JucerCommandIDs::compOverlay100)
  647. {
  648. amount = 100;
  649. num = 3;
  650. }
  651. result.defaultKeypresses.add (KeyPress ('2' + num, cmd, 0));
  652. int currentAmount = 0;
  653. if (document != nullptr && document->getComponentOverlayOpacity() > 0.9f)
  654. currentAmount = 100;
  655. else if (document != nullptr && document->getComponentOverlayOpacity() > 0.6f)
  656. currentAmount = 66;
  657. else if (document != nullptr && document->getComponentOverlayOpacity() > 0.3f)
  658. currentAmount = 33;
  659. result.setInfo (commandID == JucerCommandIDs::compOverlay0
  660. ? TRANS("No component overlay")
  661. : TRANS("Overlay with opacity of 123%").replace ("123", String (amount)),
  662. TRANS("Changes the opacity of the components that are shown over the top of the graphics editor."),
  663. CommandCategories::view, 0);
  664. result.setActive (currentPaintRoutine != nullptr && document->getComponentLayout() != nullptr);
  665. result.setTicked (amount == currentAmount);
  666. }
  667. break;
  668. case StandardApplicationCommandIDs::undo:
  669. result.setInfo (TRANS ("Undo"), TRANS ("Undo"), "Editing", 0);
  670. result.setActive (document != nullptr && document->getUndoManager().canUndo());
  671. result.defaultKeypresses.add (KeyPress ('z', cmd, 0));
  672. break;
  673. case StandardApplicationCommandIDs::redo:
  674. result.setInfo (TRANS ("Redo"), TRANS ("Redo"), "Editing", 0);
  675. result.setActive (document != nullptr && document->getUndoManager().canRedo());
  676. result.defaultKeypresses.add (KeyPress ('z', cmd | shift, 0));
  677. break;
  678. case StandardApplicationCommandIDs::cut:
  679. result.setInfo (TRANS ("Cut"), String(), "Editing", 0);
  680. result.setActive (isSomethingSelected());
  681. result.defaultKeypresses.add (KeyPress ('x', cmd, 0));
  682. break;
  683. case StandardApplicationCommandIDs::copy:
  684. result.setInfo (TRANS ("Copy"), String(), "Editing", 0);
  685. result.setActive (isSomethingSelected());
  686. result.defaultKeypresses.add (KeyPress ('c', cmd, 0));
  687. break;
  688. case StandardApplicationCommandIDs::paste:
  689. {
  690. result.setInfo (TRANS ("Paste"), String(), "Editing", 0);
  691. result.defaultKeypresses.add (KeyPress ('v', cmd, 0));
  692. bool canPaste = false;
  693. ScopedPointer<XmlElement> doc (XmlDocument::parse (SystemClipboard::getTextFromClipboard()));
  694. if (doc != nullptr)
  695. {
  696. if (doc->hasTagName (ComponentLayout::clipboardXmlTag))
  697. canPaste = (currentLayout != nullptr);
  698. else if (doc->hasTagName (PaintRoutine::clipboardXmlTag))
  699. canPaste = (currentPaintRoutine != nullptr);
  700. }
  701. result.setActive (canPaste);
  702. }
  703. break;
  704. case StandardApplicationCommandIDs::del:
  705. result.setInfo (TRANS ("Delete"), String(), "Editing", 0);
  706. result.setActive (isSomethingSelected());
  707. break;
  708. case StandardApplicationCommandIDs::selectAll:
  709. result.setInfo (TRANS ("Select All"), String(), "Editing", 0);
  710. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  711. result.defaultKeypresses.add (KeyPress ('a', cmd, 0));
  712. break;
  713. case StandardApplicationCommandIDs::deselectAll:
  714. result.setInfo (TRANS ("Deselect All"), String(), "Editing", 0);
  715. result.setActive (currentPaintRoutine != nullptr || currentLayout != nullptr);
  716. result.defaultKeypresses.add (KeyPress ('d', cmd, 0));
  717. break;
  718. default:
  719. break;
  720. }
  721. }
  722. bool JucerDocumentEditor::perform (const InvocationInfo& info)
  723. {
  724. ComponentLayout* const currentLayout = getCurrentLayout();
  725. PaintRoutine* const currentPaintRoutine = getCurrentPaintRoutine();
  726. document->beginTransaction();
  727. if (info.commandID >= JucerCommandIDs::newComponentBase
  728. && info.commandID < JucerCommandIDs::newComponentBase + ObjectTypes::numComponentTypes)
  729. {
  730. addComponent (info.commandID - JucerCommandIDs::newComponentBase);
  731. return true;
  732. }
  733. if (info.commandID >= JucerCommandIDs::newElementBase
  734. && info.commandID < JucerCommandIDs::newElementBase + ObjectTypes::numElementTypes)
  735. {
  736. addElement (info.commandID - JucerCommandIDs::newElementBase);
  737. return true;
  738. }
  739. switch (info.commandID)
  740. {
  741. case StandardApplicationCommandIDs::undo:
  742. document->getUndoManager().undo();
  743. document->dispatchPendingMessages();
  744. break;
  745. case StandardApplicationCommandIDs::redo:
  746. document->getUndoManager().redo();
  747. document->dispatchPendingMessages();
  748. break;
  749. case JucerCommandIDs::test:
  750. TestComponent::showInDialogBox (*document);
  751. break;
  752. case JucerCommandIDs::enableSnapToGrid:
  753. document->setSnappingGrid (document->getSnappingGridSize(),
  754. ! document->isSnapActive (false),
  755. document->isSnapShown());
  756. break;
  757. case JucerCommandIDs::showGrid:
  758. document->setSnappingGrid (document->getSnappingGridSize(),
  759. document->isSnapActive (false),
  760. ! document->isSnapShown());
  761. break;
  762. case JucerCommandIDs::editCompLayout:
  763. showLayout();
  764. break;
  765. case JucerCommandIDs::editCompGraphics:
  766. showGraphics (0);
  767. break;
  768. case JucerCommandIDs::zoomIn: setZoom (snapToIntegerZoom (getZoom() * 2.0)); break;
  769. case JucerCommandIDs::zoomOut: setZoom (snapToIntegerZoom (getZoom() / 2.0)); break;
  770. case JucerCommandIDs::zoomNormal: setZoom (1.0); break;
  771. case JucerCommandIDs::spaceBarDrag:
  772. if (EditingPanelBase* panel = dynamic_cast<EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  773. panel->dragKeyHeldDown (info.isKeyDown);
  774. break;
  775. case JucerCommandIDs::compOverlay0:
  776. case JucerCommandIDs::compOverlay33:
  777. case JucerCommandIDs::compOverlay66:
  778. case JucerCommandIDs::compOverlay100:
  779. {
  780. int amount = 0;
  781. if (info.commandID == JucerCommandIDs::compOverlay33)
  782. amount = 33;
  783. else if (info.commandID == JucerCommandIDs::compOverlay66)
  784. amount = 66;
  785. else if (info.commandID == JucerCommandIDs::compOverlay100)
  786. amount = 100;
  787. document->setComponentOverlayOpacity (amount * 0.01f);
  788. }
  789. break;
  790. case JucerCommandIDs::bringBackLostItems:
  791. if (EditingPanelBase* panel = dynamic_cast<EditingPanelBase*> (tabbedComponent.getCurrentContentComponent()))
  792. {
  793. int w = panel->getComponentArea().getWidth();
  794. int h = panel->getComponentArea().getHeight();
  795. if (currentPaintRoutine != nullptr)
  796. currentPaintRoutine->bringLostItemsBackOnScreen (panel->getComponentArea());
  797. else if (currentLayout != nullptr)
  798. currentLayout->bringLostItemsBackOnScreen (w, h);
  799. }
  800. break;
  801. case JucerCommandIDs::toFront:
  802. if (currentLayout != nullptr)
  803. currentLayout->selectedToFront();
  804. else if (currentPaintRoutine != nullptr)
  805. currentPaintRoutine->selectedToFront();
  806. break;
  807. case JucerCommandIDs::toBack:
  808. if (currentLayout != nullptr)
  809. currentLayout->selectedToBack();
  810. else if (currentPaintRoutine != nullptr)
  811. currentPaintRoutine->selectedToBack();
  812. break;
  813. case JucerCommandIDs::group:
  814. if (currentPaintRoutine != nullptr)
  815. currentPaintRoutine->groupSelected();
  816. break;
  817. case JucerCommandIDs::ungroup:
  818. if (currentPaintRoutine != nullptr)
  819. currentPaintRoutine->ungroupSelected();
  820. break;
  821. case StandardApplicationCommandIDs::cut:
  822. if (currentLayout != nullptr)
  823. {
  824. currentLayout->copySelectedToClipboard();
  825. currentLayout->deleteSelected();
  826. }
  827. else if (currentPaintRoutine != nullptr)
  828. {
  829. currentPaintRoutine->copySelectedToClipboard();
  830. currentPaintRoutine->deleteSelected();
  831. }
  832. break;
  833. case StandardApplicationCommandIDs::copy:
  834. if (currentLayout != nullptr)
  835. currentLayout->copySelectedToClipboard();
  836. else if (currentPaintRoutine != nullptr)
  837. currentPaintRoutine->copySelectedToClipboard();
  838. break;
  839. case StandardApplicationCommandIDs::paste:
  840. {
  841. if (ScopedPointer<XmlElement> doc = XmlDocument::parse (SystemClipboard::getTextFromClipboard()))
  842. {
  843. if (doc->hasTagName (ComponentLayout::clipboardXmlTag))
  844. {
  845. if (currentLayout != nullptr)
  846. currentLayout->paste();
  847. }
  848. else if (doc->hasTagName (PaintRoutine::clipboardXmlTag))
  849. {
  850. if (currentPaintRoutine != nullptr)
  851. currentPaintRoutine->paste();
  852. }
  853. }
  854. }
  855. break;
  856. case StandardApplicationCommandIDs::del:
  857. if (currentLayout != nullptr)
  858. currentLayout->deleteSelected();
  859. else if (currentPaintRoutine != nullptr)
  860. currentPaintRoutine->deleteSelected();
  861. break;
  862. case StandardApplicationCommandIDs::selectAll:
  863. if (currentLayout != nullptr)
  864. currentLayout->selectAll();
  865. else if (currentPaintRoutine != nullptr)
  866. currentPaintRoutine->selectAll();
  867. break;
  868. case StandardApplicationCommandIDs::deselectAll:
  869. if (currentLayout != nullptr)
  870. {
  871. currentLayout->getSelectedSet().deselectAll();
  872. }
  873. else if (currentPaintRoutine != nullptr)
  874. {
  875. currentPaintRoutine->getSelectedElements().deselectAll();
  876. currentPaintRoutine->getSelectedPoints().deselectAll();
  877. }
  878. break;
  879. default:
  880. return false;
  881. }
  882. document->beginTransaction();
  883. return true;
  884. }
  885. bool JucerDocumentEditor::keyPressed (const KeyPress& key)
  886. {
  887. if (key.isKeyCode (KeyPress::deleteKey) || key.isKeyCode (KeyPress::backspaceKey))
  888. {
  889. ProjucerApplication::getCommandManager().invokeDirectly (StandardApplicationCommandIDs::del, true);
  890. return true;
  891. }
  892. return false;
  893. }
  894. JucerDocumentEditor* JucerDocumentEditor::getActiveDocumentHolder()
  895. {
  896. ApplicationCommandInfo info (0);
  897. return dynamic_cast<JucerDocumentEditor*> (ProjucerApplication::getCommandManager()
  898. .getTargetForCommand (JucerCommandIDs::editCompLayout, info));
  899. }
  900. Image JucerDocumentEditor::createComponentLayerSnapshot() const
  901. {
  902. if (compLayoutPanel != nullptr)
  903. return compLayoutPanel->createComponentSnapshot();
  904. return {};
  905. }
  906. const int gridSnapMenuItemBase = 0x8723620;
  907. const int snapSizes[] = { 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32 };
  908. void createGUIEditorMenu (PopupMenu& menu)
  909. {
  910. ApplicationCommandManager* commandManager = &ProjucerApplication::getCommandManager();
  911. menu.addCommandItem (commandManager, JucerCommandIDs::editCompLayout);
  912. menu.addCommandItem (commandManager, JucerCommandIDs::editCompGraphics);
  913. menu.addSeparator();
  914. PopupMenu newComps;
  915. for (int i = 0; i < ObjectTypes::numComponentTypes; ++i)
  916. newComps.addCommandItem (commandManager, JucerCommandIDs::newComponentBase + i);
  917. menu.addSubMenu ("Add new component", newComps);
  918. PopupMenu newElements;
  919. for (int i = 0; i < ObjectTypes::numElementTypes; ++i)
  920. newElements.addCommandItem (commandManager, JucerCommandIDs::newElementBase + i);
  921. menu.addSubMenu ("Add new graphic element", newElements);
  922. menu.addSeparator();
  923. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  924. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  925. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  926. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  927. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::selectAll);
  928. menu.addCommandItem (commandManager, StandardApplicationCommandIDs::deselectAll);
  929. menu.addSeparator();
  930. menu.addCommandItem (commandManager, JucerCommandIDs::toFront);
  931. menu.addCommandItem (commandManager, JucerCommandIDs::toBack);
  932. menu.addSeparator();
  933. menu.addCommandItem (commandManager, JucerCommandIDs::group);
  934. menu.addCommandItem (commandManager, JucerCommandIDs::ungroup);
  935. menu.addSeparator();
  936. menu.addCommandItem (commandManager, JucerCommandIDs::bringBackLostItems);
  937. menu.addSeparator();
  938. menu.addCommandItem (commandManager, JucerCommandIDs::showGrid);
  939. menu.addCommandItem (commandManager, JucerCommandIDs::enableSnapToGrid);
  940. JucerDocumentEditor* holder = JucerDocumentEditor::getActiveDocumentHolder();
  941. {
  942. const int currentSnapSize = holder != nullptr ? holder->getDocument()->getSnappingGridSize() : -1;
  943. PopupMenu m;
  944. for (int i = 0; i < numElementsInArray (snapSizes); ++i)
  945. m.addItem (gridSnapMenuItemBase + i, String (snapSizes[i]) + " pixels",
  946. true, snapSizes[i] == currentSnapSize);
  947. menu.addSubMenu ("Grid size", m, currentSnapSize >= 0);
  948. }
  949. menu.addSeparator();
  950. menu.addCommandItem (commandManager, JucerCommandIDs::zoomIn);
  951. menu.addCommandItem (commandManager, JucerCommandIDs::zoomOut);
  952. menu.addCommandItem (commandManager, JucerCommandIDs::zoomNormal);
  953. menu.addSeparator();
  954. menu.addCommandItem (commandManager, JucerCommandIDs::test);
  955. menu.addSeparator();
  956. {
  957. PopupMenu overlays;
  958. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay0);
  959. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay33);
  960. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay66);
  961. overlays.addCommandItem (commandManager, JucerCommandIDs::compOverlay100);
  962. menu.addSubMenu ("Component Overlay", overlays, holder != nullptr);
  963. }
  964. }
  965. void handleGUIEditorMenuCommand (int menuItemID)
  966. {
  967. if (auto* ed = JucerDocumentEditor::getActiveDocumentHolder())
  968. {
  969. int gridIndex = menuItemID - gridSnapMenuItemBase;
  970. if (isPositiveAndBelow (gridIndex, numElementsInArray (snapSizes)))
  971. {
  972. auto& doc = *ed->getDocument();
  973. doc.setSnappingGrid (snapSizes [gridIndex],
  974. doc.isSnapActive (false),
  975. doc.isSnapShown());
  976. }
  977. }
  978. }
  979. void registerGUIEditorCommands()
  980. {
  981. JucerDocumentEditor dh (nullptr);
  982. ProjucerApplication::getCommandManager().registerAllCommandsForTarget (&dh);
  983. }