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.

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