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.

253 lines
9.0KB

  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. #if JUCE_QUICKTIME || JUCE_DIRECTSHOW
  19. //==============================================================================
  20. // so that we can easily have two video windows each with a file browser, wrap this up as a class..
  21. class MovieComponentWithFileBrowser : public Component,
  22. public DragAndDropTarget,
  23. private FilenameComponentListener
  24. {
  25. public:
  26. MovieComponentWithFileBrowser()
  27. : isDragOver (false),
  28. fileChooser ("movie", File::nonexistent, true, false, false,
  29. "*", String::empty, "(choose a video file to play)")
  30. {
  31. addAndMakeVisible (videoComp);
  32. addAndMakeVisible (fileChooser);
  33. fileChooser.addListener (this);
  34. fileChooser.setBrowseButtonText ("browse");
  35. }
  36. void setFile (const File& file)
  37. {
  38. fileChooser.setCurrentFile (file, true);
  39. }
  40. void paintOverChildren (Graphics& g)
  41. {
  42. if (isDragOver)
  43. {
  44. g.setColour (Colours::red);
  45. g.drawRect (fileChooser.getBounds(), 2);
  46. }
  47. }
  48. void resized()
  49. {
  50. videoComp.setBoundsWithCorrectAspectRatio (Rectangle<int> (0, 0, getWidth(), getHeight() - 30),
  51. Justification::centred);
  52. fileChooser.setBounds (0, getHeight() - 24, getWidth(), 24);
  53. }
  54. bool isInterestedInDragSource (const SourceDetails&) { return true; }
  55. void itemDragEnter (const SourceDetails&)
  56. {
  57. isDragOver = true;
  58. repaint();
  59. }
  60. void itemDragExit (const SourceDetails&)
  61. {
  62. isDragOver = false;
  63. repaint();
  64. }
  65. void itemDropped (const SourceDetails& dragSourceDetails)
  66. {
  67. setFile (dragSourceDetails.description.toString());
  68. isDragOver = false;
  69. repaint();
  70. }
  71. private:
  72. #if JUCE_QUICKTIME
  73. QuickTimeMovieComponent videoComp;
  74. #elif JUCE_DIRECTSHOW
  75. DirectShowComponent videoComp;
  76. #endif
  77. bool isDragOver;
  78. FilenameComponent fileChooser;
  79. void filenameComponentChanged (FilenameComponent*) override
  80. {
  81. // this is called when the user changes the filename in the file chooser box
  82. #if JUCE_QUICKTIME
  83. if (videoComp.loadMovie (fileChooser.getCurrentFile(), true))
  84. #elif JUCE_DIRECTSHOW
  85. if (videoComp.loadMovie (fileChooser.getCurrentFile()))
  86. #endif
  87. {
  88. // loaded the file ok, so let's start it playing..
  89. videoComp.play();
  90. resized(); // update to reflect the video's aspect ratio
  91. }
  92. else
  93. {
  94. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  95. "Couldn't load the file!",
  96. #if JUCE_QUICKTIME
  97. "Sorry, QuickTime didn't manage to load that file!");
  98. #elif JUCE_DIRECTSHOW
  99. "Sorry, DirectShow didn't manage to load that file!");
  100. #endif
  101. }
  102. }
  103. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MovieComponentWithFileBrowser)
  104. };
  105. //==============================================================================
  106. class VideoDemo : public Component,
  107. public DragAndDropContainer,
  108. private Button::Listener,
  109. private FileBrowserListener
  110. {
  111. public:
  112. VideoDemo()
  113. : moviesWildcardFilter ("*", "*", "Movies File Filter"),
  114. directoryThread ("Movie File Scanner Thread"),
  115. movieList (&moviesWildcardFilter, directoryThread),
  116. fileTree (movieList),
  117. resizerBar (&stretchableManager, 1, false)
  118. {
  119. setOpaque (true);
  120. movieList.setDirectory (File::getSpecialLocation (File::userMoviesDirectory), true, true);
  121. directoryThread.startThread (1);
  122. fileTree.addListener (this);
  123. fileTree.setColour (TreeView::backgroundColourId, Colours::lightgrey);
  124. addAndMakeVisible (fileTree);
  125. addAndMakeVisible (resizerBar);
  126. loadLeftButton.setButtonText ("Load Left");
  127. loadRightButton.setButtonText ("Load Right");
  128. loadLeftButton.addListener (this);
  129. loadRightButton.addListener (this);
  130. addAndMakeVisible (loadLeftButton);
  131. addAndMakeVisible (loadRightButton);
  132. addAndMakeVisible (movieCompLeft);
  133. addAndMakeVisible (movieCompRight);
  134. // we have to set up our StretchableLayoutManager so it know the limits and preferred sizes of it's contents
  135. stretchableManager.setItemLayout (0, // for the fileTree
  136. -0.1, -0.9, // must be between 50 pixels and 90% of the available space
  137. -0.3); // and its preferred size is 30% of the total available space
  138. stretchableManager.setItemLayout (1, // for the resize bar
  139. 5, 5, 5); // hard limit to 5 pixels
  140. stretchableManager.setItemLayout (2, // for the movie components
  141. -0.1, -0.9, // size must be between 50 pixels and 90% of the available space
  142. -0.7); // and its preferred size is 70% of the total available space
  143. }
  144. ~VideoDemo()
  145. {
  146. loadLeftButton.removeListener (this);
  147. loadRightButton.removeListener (this);
  148. fileTree.removeListener (this);
  149. }
  150. void paint (Graphics& g) override
  151. {
  152. fillTiledBackground (g);
  153. }
  154. void resized() override
  155. {
  156. // make a list of two of our child components that we want to reposition
  157. Component* comps[] = { &fileTree, &resizerBar, nullptr };
  158. // this will position the 3 components, one above the other, to fit
  159. // vertically into the rectangle provided.
  160. stretchableManager.layOutComponents (comps, 3,
  161. 0, 0, getWidth(), getHeight(),
  162. true, true);
  163. // now position out two video components in the space that's left
  164. Rectangle<int> area (getLocalBounds().removeFromBottom (getHeight() - resizerBar.getBottom()));
  165. {
  166. Rectangle<int> buttonArea (area.removeFromTop (30));
  167. loadLeftButton.setBounds (buttonArea.removeFromLeft (buttonArea.getWidth() / 2).reduced (5));
  168. loadRightButton.setBounds (buttonArea.reduced (5));
  169. }
  170. movieCompLeft.setBounds (area.removeFromLeft (area.getWidth() / 2).reduced (5));
  171. movieCompRight.setBounds (area.reduced (5));
  172. }
  173. private:
  174. WildcardFileFilter moviesWildcardFilter;
  175. TimeSliceThread directoryThread;
  176. DirectoryContentsList movieList;
  177. FileTreeComponent fileTree;
  178. StretchableLayoutManager stretchableManager;
  179. StretchableLayoutResizerBar resizerBar;
  180. TextButton loadLeftButton, loadRightButton;
  181. MovieComponentWithFileBrowser movieCompLeft, movieCompRight;
  182. void buttonClicked (Button* button) override
  183. {
  184. if (button == &loadLeftButton)
  185. movieCompLeft.setFile (fileTree.getSelectedFile (0));
  186. else if (button == &loadRightButton)
  187. movieCompRight.setFile (fileTree.getSelectedFile (0));
  188. }
  189. void selectionChanged() override
  190. {
  191. // we're just going to update the drag description of out tree so that rows can be dragged onto the file players
  192. fileTree.setDragAndDropDescription (fileTree.getSelectedFile().getFullPathName());
  193. }
  194. void fileClicked (const File&, const MouseEvent&) override {}
  195. void fileDoubleClicked (const File&) override {}
  196. void browserRootChanged (const File&) override {}
  197. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VideoDemo)
  198. };
  199. // This static object will register this demo type in a global list of demos..
  200. static JuceDemoType<VideoDemo> demo ("29 Graphics: Video Playback");
  201. #endif