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.

273 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceDemoHeader.h"
  18. //==============================================================================
  19. /** The Note class contains text editor used to display and edit the note's contents and will
  20. also listen to changes in the text and mark the FileBasedDocument as 'dirty'. This 'dirty'
  21. flag is used to promt the user to save the note when it is closed.
  22. */
  23. class Note : public Component,
  24. public FileBasedDocument,
  25. private TextEditor::Listener
  26. {
  27. public:
  28. Note (const String& name, const String& contents)
  29. : FileBasedDocument (".jnote", "*.jnote",
  30. "Browse for note to load",
  31. "Choose file to save note to"),
  32. textValueObject (contents)
  33. {
  34. // we need to use an separate Value object as our text source so it doesn't get marked
  35. // as changed immediately
  36. setName (name);
  37. editor.setMultiLine (true);
  38. editor.setReturnKeyStartsNewLine (true);
  39. editor.getTextValue().referTo (textValueObject);
  40. addAndMakeVisible (editor);
  41. editor.addListener (this);
  42. }
  43. ~Note()
  44. {
  45. editor.removeListener (this);
  46. }
  47. void resized() override
  48. {
  49. editor.setBounds (getLocalBounds());
  50. }
  51. String getDocumentTitle() override
  52. {
  53. return getName();
  54. }
  55. Result loadDocument (const File& file) override
  56. {
  57. editor.setText (file.loadFileAsString());
  58. return Result::ok();
  59. }
  60. Result saveDocument (const File& file) override
  61. {
  62. // attempt to save the contents into the given file
  63. FileOutputStream os (file);
  64. if (os.openedOk())
  65. os.writeText (editor.getText(), false, false);
  66. return Result::ok();
  67. }
  68. File getLastDocumentOpened() override
  69. {
  70. // not interested in this for now
  71. return File();
  72. }
  73. void setLastDocumentOpened (const File& /*file*/) override
  74. {
  75. // not interested in this for now
  76. }
  77. #if JUCE_MODAL_LOOPS_PERMITTED
  78. File getSuggestedSaveAsFile (const File&) override
  79. {
  80. return File::getSpecialLocation (File::userDesktopDirectory).getChildFile (getName()).withFileExtension ("jnote");
  81. }
  82. #endif
  83. private:
  84. Value textValueObject;
  85. TextEditor editor;
  86. void textEditorTextChanged (TextEditor& ed) override
  87. {
  88. // let our FileBasedDocument know we've changed
  89. if (&ed == &editor)
  90. changed();
  91. }
  92. void textEditorReturnKeyPressed (TextEditor&) override {}
  93. void textEditorEscapeKeyPressed (TextEditor&) override {}
  94. void textEditorFocusLost (TextEditor&) override {}
  95. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Note)
  96. };
  97. //==============================================================================
  98. /** Simple MultiDocumentPanel that just tries to save our notes when they are closed.
  99. */
  100. class DemoMultiDocumentPanel : public MultiDocumentPanel
  101. {
  102. public:
  103. DemoMultiDocumentPanel()
  104. {
  105. }
  106. ~DemoMultiDocumentPanel()
  107. {
  108. closeAllDocuments (true);
  109. }
  110. bool tryToCloseDocument (Component* component) override
  111. {
  112. #if JUCE_MODAL_LOOPS_PERMITTED
  113. if (Note* note = dynamic_cast<Note*> (component))
  114. return note->saveIfNeededAndUserAgrees() != FileBasedDocument::failedToWriteToFile;
  115. #endif
  116. return true;
  117. }
  118. private:
  119. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoMultiDocumentPanel)
  120. };
  121. //==============================================================================
  122. /** Simple multi-document panel that manages a number of notes that you can store to files.
  123. By default this will look for notes saved to the desktop and load them up.
  124. */
  125. class MDIDemo : public Component,
  126. public FileDragAndDropTarget,
  127. private Button::Listener
  128. {
  129. public:
  130. MDIDemo()
  131. {
  132. setOpaque (true);
  133. showInTabsButton.setButtonText ("Show with tabs");
  134. showInTabsButton.setToggleState (false, dontSendNotification);
  135. showInTabsButton.addListener (this);
  136. addAndMakeVisible (showInTabsButton);
  137. addNoteButton.setButtonText ("Create a new note");
  138. addNoteButton.addListener (this);
  139. addAndMakeVisible (addNoteButton);
  140. addAndMakeVisible (multiDocumentPanel);
  141. multiDocumentPanel.setBackgroundColour (Colours::transparentBlack);
  142. updateLayoutMode();
  143. addNote ("Notes Demo", "You can drag-and-drop text files onto this page to open them as notes..");
  144. addExistingNotes();
  145. }
  146. ~MDIDemo()
  147. {
  148. addNoteButton.removeListener (this);
  149. showInTabsButton.removeListener (this);
  150. }
  151. void paint (Graphics& g) override
  152. {
  153. fillStandardDemoBackground (g);
  154. }
  155. void resized() override
  156. {
  157. Rectangle<int> area (getLocalBounds());
  158. Rectangle<int> buttonArea (area.removeFromTop (28).reduced (2));
  159. addNoteButton.setBounds (buttonArea.removeFromRight (150));
  160. showInTabsButton.setBounds (buttonArea);
  161. multiDocumentPanel.setBounds (area);
  162. }
  163. bool isInterestedInFileDrag (const StringArray&) override
  164. {
  165. return true;
  166. }
  167. void filesDropped (const StringArray& filenames, int /* x */, int /* y */) override
  168. {
  169. Array<File> files;
  170. for (int i = 0; i < filenames.size(); ++i)
  171. files.add (File (filenames[i]));
  172. createNotesForFiles (files);
  173. }
  174. void createNotesForFiles (const Array<File>& files)
  175. {
  176. for (int i = 0; i < files.size(); ++i)
  177. {
  178. const File file (files[i]);
  179. String content = file.loadFileAsString();
  180. if (content.length() > 20000)
  181. content = "Too long!";
  182. addNote (file.getFileName(), content);
  183. }
  184. }
  185. private:
  186. ToggleButton showInTabsButton;
  187. TextButton addNoteButton;
  188. DemoMultiDocumentPanel multiDocumentPanel;
  189. void updateLayoutMode()
  190. {
  191. multiDocumentPanel.setLayoutMode (showInTabsButton.getToggleState() ? MultiDocumentPanel::MaximisedWindowsWithTabs
  192. : MultiDocumentPanel::FloatingWindows);
  193. }
  194. void addNote (const String& name, const String& content)
  195. {
  196. Note* newNote = new Note (name, content);
  197. newNote->setSize (200, 200);
  198. multiDocumentPanel.addDocument (newNote, Colours::lightblue.withAlpha (0.6f), true);
  199. }
  200. void addExistingNotes()
  201. {
  202. Array<File> files;
  203. File::getSpecialLocation (File::userDesktopDirectory).findChildFiles (files, File::findFiles, false, "*.jnote");
  204. createNotesForFiles (files);
  205. }
  206. void buttonClicked (Button* b) override
  207. {
  208. if (b == &showInTabsButton)
  209. updateLayoutMode();
  210. else if (b == &addNoteButton)
  211. addNote (String ("Note ") + String (multiDocumentPanel.getNumDocuments() + 1), "Hello World!");
  212. }
  213. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MDIDemo)
  214. };
  215. // This static object will register this demo type in a global list of demos..
  216. static JuceDemoType<MDIDemo> demo ("10 Components: MDI");