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.

249 lines
8.8KB

  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(), true, false, false,
  29. "*", String(), "(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::showMessageBoxAsync (AlertWindow::WarningIcon,
  95. "Couldn't load the file!",
  96. r.getErrorMessage());
  97. }
  98. }
  99. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MovieComponentWithFileBrowser)
  100. };
  101. //==============================================================================
  102. class VideoDemo : public Component,
  103. public DragAndDropContainer,
  104. private Button::Listener,
  105. private FileBrowserListener
  106. {
  107. public:
  108. VideoDemo()
  109. : moviesWildcardFilter ("*", "*", "Movies File Filter"),
  110. directoryThread ("Movie File Scanner Thread"),
  111. movieList (&moviesWildcardFilter, directoryThread),
  112. fileTree (movieList),
  113. resizerBar (&stretchableManager, 1, false)
  114. {
  115. setOpaque (true);
  116. movieList.setDirectory (File::getSpecialLocation (File::userMoviesDirectory), true, true);
  117. directoryThread.startThread (1);
  118. fileTree.addListener (this);
  119. fileTree.setColour (TreeView::backgroundColourId, Colours::lightgrey);
  120. addAndMakeVisible (fileTree);
  121. addAndMakeVisible (resizerBar);
  122. loadLeftButton.setButtonText ("Load Left");
  123. loadRightButton.setButtonText ("Load Right");
  124. loadLeftButton.addListener (this);
  125. loadRightButton.addListener (this);
  126. addAndMakeVisible (loadLeftButton);
  127. addAndMakeVisible (loadRightButton);
  128. addAndMakeVisible (movieCompLeft);
  129. addAndMakeVisible (movieCompRight);
  130. // we have to set up our StretchableLayoutManager so it know the limits and preferred sizes of it's contents
  131. stretchableManager.setItemLayout (0, // for the fileTree
  132. -0.1, -0.9, // must be between 50 pixels and 90% of the available space
  133. -0.3); // and its preferred size is 30% of the total available space
  134. stretchableManager.setItemLayout (1, // for the resize bar
  135. 5, 5, 5); // hard limit to 5 pixels
  136. stretchableManager.setItemLayout (2, // for the movie components
  137. -0.1, -0.9, // size must be between 50 pixels and 90% of the available space
  138. -0.7); // and its preferred size is 70% of the total available space
  139. }
  140. ~VideoDemo()
  141. {
  142. loadLeftButton.removeListener (this);
  143. loadRightButton.removeListener (this);
  144. fileTree.removeListener (this);
  145. }
  146. void paint (Graphics& g) override
  147. {
  148. fillStandardDemoBackground (g);
  149. }
  150. void resized() override
  151. {
  152. // make a list of two of our child components that we want to reposition
  153. Component* comps[] = { &fileTree, &resizerBar, nullptr };
  154. // this will position the 3 components, one above the other, to fit
  155. // vertically into the rectangle provided.
  156. stretchableManager.layOutComponents (comps, 3,
  157. 0, 0, getWidth(), getHeight(),
  158. true, true);
  159. // now position out two video components in the space that's left
  160. Rectangle<int> area (getLocalBounds().removeFromBottom (getHeight() - resizerBar.getBottom()));
  161. {
  162. Rectangle<int> buttonArea (area.removeFromTop (30));
  163. loadLeftButton.setBounds (buttonArea.removeFromLeft (buttonArea.getWidth() / 2).reduced (5));
  164. loadRightButton.setBounds (buttonArea.reduced (5));
  165. }
  166. movieCompLeft.setBounds (area.removeFromLeft (area.getWidth() / 2).reduced (5));
  167. movieCompRight.setBounds (area.reduced (5));
  168. }
  169. private:
  170. WildcardFileFilter moviesWildcardFilter;
  171. TimeSliceThread directoryThread;
  172. DirectoryContentsList movieList;
  173. FileTreeComponent fileTree;
  174. StretchableLayoutManager stretchableManager;
  175. StretchableLayoutResizerBar resizerBar;
  176. TextButton loadLeftButton, loadRightButton;
  177. MovieComponentWithFileBrowser movieCompLeft, movieCompRight;
  178. void buttonClicked (Button* button) override
  179. {
  180. if (button == &loadLeftButton)
  181. movieCompLeft.setFile (fileTree.getSelectedFile (0));
  182. else if (button == &loadRightButton)
  183. movieCompRight.setFile (fileTree.getSelectedFile (0));
  184. }
  185. void selectionChanged() override
  186. {
  187. // we're just going to update the drag description of out tree so that rows can be dragged onto the file players
  188. fileTree.setDragAndDropDescription (fileTree.getSelectedFile().getFullPathName());
  189. }
  190. void fileClicked (const File&, const MouseEvent&) override {}
  191. void fileDoubleClicked (const File&) override {}
  192. void browserRootChanged (const File&) override {}
  193. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VideoDemo)
  194. };
  195. // This static object will register this demo type in a global list of demos..
  196. static JuceDemoType<VideoDemo> demo ("29 Graphics: Video Playback");
  197. #endif