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.

124 lines
4.7KB

  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. class ImagesDemo : public Component,
  22. public FileBrowserListener
  23. {
  24. public:
  25. ImagesDemo()
  26. : imagesWildcardFilter ("*.jpeg;*.jpg;*.png;*.gif", "*", "Image File Filter"),
  27. directoryThread ("Image File Scanner Thread"),
  28. imageList (&imagesWildcardFilter, directoryThread),
  29. fileTree (imageList),
  30. resizerBar (&stretchableManager, 1, false)
  31. {
  32. setOpaque (true);
  33. imageList.setDirectory (File::getSpecialLocation (File::userPicturesDirectory), true, true);
  34. directoryThread.startThread (1);
  35. fileTree.addListener (this);
  36. fileTree.setColour (TreeView::backgroundColourId, Colours::grey);
  37. addAndMakeVisible (fileTree);
  38. addAndMakeVisible (resizerBar);
  39. addAndMakeVisible (imagePreview);
  40. // we have to set up our StretchableLayoutManager so it know the limits and preferred sizes of it's contents
  41. stretchableManager.setItemLayout (0, // for the fileTree
  42. -0.1, -0.9, // must be between 50 pixels and 90% of the available space
  43. -0.3); // and its preferred size is 30% of the total available space
  44. stretchableManager.setItemLayout (1, // for the resize bar
  45. 5, 5, 5); // hard limit to 5 pixels
  46. stretchableManager.setItemLayout (2, // for the imagePreview
  47. -0.1, -0.9, // size must be between 50 pixels and 90% of the available space
  48. -0.7); // and its preferred size is 70% of the total available space
  49. }
  50. ~ImagesDemo()
  51. {
  52. fileTree.removeListener (this);
  53. }
  54. void paint (Graphics& g) override
  55. {
  56. g.fillAll (Colours::white);
  57. }
  58. void resized() override
  59. {
  60. auto r = getLocalBounds().reduced (4);
  61. // make a list of two of our child components that we want to reposition
  62. Component* comps[] = { &fileTree, &resizerBar, &imagePreview };
  63. // this will position the 3 components, one above the other, to fit
  64. // vertically into the rectangle provided.
  65. stretchableManager.layOutComponents (comps, 3,
  66. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  67. true, true);
  68. }
  69. private:
  70. WildcardFileFilter imagesWildcardFilter;
  71. TimeSliceThread directoryThread;
  72. DirectoryContentsList imageList;
  73. FileTreeComponent fileTree;
  74. ImageComponent imagePreview;
  75. StretchableLayoutManager stretchableManager;
  76. StretchableLayoutResizerBar resizerBar;
  77. void selectionChanged() override
  78. {
  79. // we're only really interested in when the selection changes, regardless of if it was
  80. // clicked or not so we'll only override this method
  81. const File selectedFile (fileTree.getSelectedFile());
  82. if (selectedFile.existsAsFile())
  83. imagePreview.setImage (ImageCache::getFromFile (selectedFile));
  84. // the image cahce is a handly way to load images from files or directly from memory and
  85. // will keep them hanging around for a few seconds in case they are requested elsewhere
  86. }
  87. void fileClicked (const File&, const MouseEvent&) override {}
  88. void fileDoubleClicked (const File&) override {}
  89. void browserRootChanged (const File&) override {}
  90. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ImagesDemo)
  91. };
  92. // This static object will register this demo type in a global list of demos..
  93. static JuceDemoType<ImagesDemo> demo ("20 Graphics: Image formats");