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.

139 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: ImagesDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Displays image files.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics
  26. exporters: xcode_mac, vs2019, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: ImagesDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. //==============================================================================
  36. class ImagesDemo : public Component,
  37. public FileBrowserListener
  38. {
  39. public:
  40. ImagesDemo()
  41. {
  42. setOpaque (true);
  43. imageList.setDirectory (File::getSpecialLocation (File::userPicturesDirectory), true, true);
  44. directoryThread.startThread (1);
  45. fileTree.addListener (this);
  46. fileTree.setColour (TreeView::backgroundColourId, Colours::grey);
  47. addAndMakeVisible (fileTree);
  48. addAndMakeVisible (resizerBar);
  49. addAndMakeVisible (imagePreview);
  50. // we have to set up our StretchableLayoutManager so it know the limits and preferred sizes of it's contents
  51. stretchableManager.setItemLayout (0, // for the fileTree
  52. -0.1, -0.9, // must be between 50 pixels and 90% of the available space
  53. -0.3); // and its preferred size is 30% of the total available space
  54. stretchableManager.setItemLayout (1, // for the resize bar
  55. 5, 5, 5); // hard limit to 5 pixels
  56. stretchableManager.setItemLayout (2, // for the imagePreview
  57. -0.1, -0.9, // size must be between 50 pixels and 90% of the available space
  58. -0.7); // and its preferred size is 70% of the total available space
  59. setSize (500, 500);
  60. }
  61. ~ImagesDemo() override
  62. {
  63. fileTree.removeListener (this);
  64. }
  65. void paint (Graphics& g) override
  66. {
  67. g.fillAll (Colours::white);
  68. }
  69. void resized() override
  70. {
  71. auto r = getLocalBounds().reduced (4);
  72. // make a list of two of our child components that we want to reposition
  73. Component* comps[] = { &fileTree, &resizerBar, &imagePreview };
  74. // this will position the 3 components, one above the other, to fit
  75. // vertically into the rectangle provided.
  76. stretchableManager.layOutComponents (comps, 3,
  77. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  78. true, true);
  79. }
  80. private:
  81. WildcardFileFilter imagesWildcardFilter { "*.jpeg;*.jpg;*.png;*.gif", "*", "Image File Filter"};
  82. TimeSliceThread directoryThread { "Image File Scanner Thread" };
  83. DirectoryContentsList imageList { &imagesWildcardFilter, directoryThread };
  84. FileTreeComponent fileTree { imageList };
  85. ImageComponent imagePreview;
  86. StretchableLayoutManager stretchableManager;
  87. StretchableLayoutResizerBar resizerBar { &stretchableManager, 1, false };
  88. void selectionChanged() override
  89. {
  90. // we're only really interested in when the selection changes, regardless of if it was
  91. // clicked or not so we'll only override this method
  92. auto selectedFile = fileTree.getSelectedFile();
  93. if (selectedFile.existsAsFile())
  94. imagePreview.setImage (ImageCache::getFromFile (selectedFile));
  95. // the image cache is a handy way to load images from files or directly from memory and
  96. // will keep them hanging around for a few seconds in case they are requested elsewhere
  97. }
  98. void fileClicked (const File&, const MouseEvent&) override {}
  99. void fileDoubleClicked (const File&) override {}
  100. void browserRootChanged (const File&) override {}
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ImagesDemo)
  102. };