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.

247 lines
7.5KB

  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 "../JuceDemoHeader.h"
  20. //==============================================================================
  21. /** The Note class contains text editor used to display and edit the note's contents and will
  22. also listen to changes in the text and mark the FileBasedDocument as 'dirty'. This 'dirty'
  23. flag is used to promt the user to save the note when it is closed.
  24. */
  25. class Note : public Component,
  26. public FileBasedDocument
  27. {
  28. public:
  29. Note (const String& name, const String& contents)
  30. : FileBasedDocument (".jnote", "*.jnote",
  31. "Browse for note to load",
  32. "Choose file to save note to"),
  33. textValueObject (contents)
  34. {
  35. // we need to use an separate Value object as our text source so it doesn't get marked
  36. // as changed immediately
  37. setName (name);
  38. editor.setMultiLine (true);
  39. editor.setReturnKeyStartsNewLine (true);
  40. editor.getTextValue().referTo (textValueObject);
  41. addAndMakeVisible (editor);
  42. editor.onTextChange = [this] { changed(); };
  43. }
  44. void resized() override
  45. {
  46. editor.setBounds (getLocalBounds());
  47. }
  48. String getDocumentTitle() override
  49. {
  50. return getName();
  51. }
  52. Result loadDocument (const File& file) override
  53. {
  54. editor.setText (file.loadFileAsString());
  55. return Result::ok();
  56. }
  57. Result saveDocument (const File& file) override
  58. {
  59. // attempt to save the contents into the given file
  60. FileOutputStream os (file);
  61. if (os.openedOk())
  62. os.writeText (editor.getText(), false, false);
  63. return Result::ok();
  64. }
  65. File getLastDocumentOpened() override
  66. {
  67. // not interested in this for now
  68. return {};
  69. }
  70. void setLastDocumentOpened (const File& /*file*/) override
  71. {
  72. // not interested in this for now
  73. }
  74. #if JUCE_MODAL_LOOPS_PERMITTED
  75. File getSuggestedSaveAsFile (const File&) override
  76. {
  77. return File::getSpecialLocation (File::userDesktopDirectory).getChildFile (getName()).withFileExtension ("jnote");
  78. }
  79. #endif
  80. private:
  81. Value textValueObject;
  82. TextEditor editor;
  83. void lookAndFeelChanged() override
  84. {
  85. editor.applyFontToAllText (editor.getFont());
  86. }
  87. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Note)
  88. };
  89. //==============================================================================
  90. /** Simple MultiDocumentPanel that just tries to save our notes when they are closed.
  91. */
  92. class DemoMultiDocumentPanel : public MultiDocumentPanel
  93. {
  94. public:
  95. DemoMultiDocumentPanel()
  96. {
  97. }
  98. ~DemoMultiDocumentPanel()
  99. {
  100. closeAllDocuments (true);
  101. }
  102. bool tryToCloseDocument (Component* component) override
  103. {
  104. #if JUCE_MODAL_LOOPS_PERMITTED
  105. if (Note* note = dynamic_cast<Note*> (component))
  106. return note->saveIfNeededAndUserAgrees() != FileBasedDocument::failedToWriteToFile;
  107. #else
  108. ignoreUnused (component);
  109. #endif
  110. return true;
  111. }
  112. private:
  113. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoMultiDocumentPanel)
  114. };
  115. //==============================================================================
  116. /** Simple multi-document panel that manages a number of notes that you can store to files.
  117. By default this will look for notes saved to the desktop and load them up.
  118. */
  119. class MDIDemo : public Component,
  120. public FileDragAndDropTarget
  121. {
  122. public:
  123. MDIDemo()
  124. {
  125. setOpaque (true);
  126. showInTabsButton.setButtonText ("Show with tabs");
  127. showInTabsButton.setToggleState (false, dontSendNotification);
  128. showInTabsButton.onClick = [this] { updateLayoutMode(); };
  129. addAndMakeVisible (showInTabsButton);
  130. addNoteButton.setButtonText ("Create a new note");
  131. addNoteButton.onClick = [this] { addNote (String ("Note ") + String (multiDocumentPanel.getNumDocuments() + 1), "Hello World!"); };
  132. addAndMakeVisible (addNoteButton);
  133. addAndMakeVisible (multiDocumentPanel);
  134. multiDocumentPanel.setBackgroundColour (Colours::transparentBlack);
  135. updateLayoutMode();
  136. addNote ("Notes Demo", "You can drag-and-drop text files onto this page to open them as notes..");
  137. addExistingNotes();
  138. }
  139. void paint (Graphics& g) override
  140. {
  141. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  142. }
  143. void resized() override
  144. {
  145. Rectangle<int> area (getLocalBounds());
  146. Rectangle<int> buttonArea (area.removeFromTop (28).reduced (2));
  147. addNoteButton.setBounds (buttonArea.removeFromRight (150));
  148. showInTabsButton.setBounds (buttonArea);
  149. multiDocumentPanel.setBounds (area);
  150. }
  151. bool isInterestedInFileDrag (const StringArray&) override
  152. {
  153. return true;
  154. }
  155. void filesDropped (const StringArray& filenames, int /* x */, int /* y */) override
  156. {
  157. Array<File> files;
  158. for (int i = 0; i < filenames.size(); ++i)
  159. files.add (File (filenames[i]));
  160. createNotesForFiles (files);
  161. }
  162. void createNotesForFiles (const Array<File>& files)
  163. {
  164. for (auto& file : files)
  165. {
  166. auto content = file.loadFileAsString();
  167. if (content.length() > 20000)
  168. content = "Too long!";
  169. addNote (file.getFileName(), content);
  170. }
  171. }
  172. private:
  173. ToggleButton showInTabsButton;
  174. TextButton addNoteButton;
  175. DemoMultiDocumentPanel multiDocumentPanel;
  176. void updateLayoutMode()
  177. {
  178. multiDocumentPanel.setLayoutMode (showInTabsButton.getToggleState() ? MultiDocumentPanel::MaximisedWindowsWithTabs
  179. : MultiDocumentPanel::FloatingWindows);
  180. }
  181. void addNote (const String& name, const String& content)
  182. {
  183. Note* newNote = new Note (name, content);
  184. newNote->setSize (200, 200);
  185. multiDocumentPanel.addDocument (newNote, Colours::lightblue.withAlpha (0.6f), true);
  186. }
  187. void addExistingNotes()
  188. {
  189. createNotesForFiles (File::getSpecialLocation (File::userDesktopDirectory)
  190. .findChildFiles (File::findFiles, false, "*.jnote"));
  191. }
  192. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MDIDemo)
  193. };
  194. // This static object will register this demo type in a global list of demos..
  195. static JuceDemoType<MDIDemo> demo ("10 Components: MDI");