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.

140 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - 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, vs2022, 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 final : 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 (Thread::Priority::background);
  45. fileTree.setTitle ("Files");
  46. fileTree.addListener (this);
  47. fileTree.setColour (TreeView::backgroundColourId, Colours::grey);
  48. addAndMakeVisible (fileTree);
  49. addAndMakeVisible (resizerBar);
  50. addAndMakeVisible (imagePreview);
  51. // we have to set up our StretchableLayoutManager so it know the limits and preferred sizes of it's contents
  52. stretchableManager.setItemLayout (0, // for the fileTree
  53. -0.1, -0.9, // must be between 50 pixels and 90% of the available space
  54. -0.3); // and its preferred size is 30% of the total available space
  55. stretchableManager.setItemLayout (1, // for the resize bar
  56. 5, 5, 5); // hard limit to 5 pixels
  57. stretchableManager.setItemLayout (2, // for the imagePreview
  58. -0.1, -0.9, // size must be between 50 pixels and 90% of the available space
  59. -0.7); // and its preferred size is 70% of the total available space
  60. setSize (500, 500);
  61. }
  62. ~ImagesDemo() override
  63. {
  64. fileTree.removeListener (this);
  65. }
  66. void paint (Graphics& g) override
  67. {
  68. g.fillAll (Colours::white);
  69. }
  70. void resized() override
  71. {
  72. auto r = getLocalBounds().reduced (4);
  73. // make a list of two of our child components that we want to reposition
  74. Component* comps[] = { &fileTree, &resizerBar, &imagePreview };
  75. // this will position the 3 components, one above the other, to fit
  76. // vertically into the rectangle provided.
  77. stretchableManager.layOutComponents (comps, 3,
  78. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  79. true, true);
  80. }
  81. private:
  82. WildcardFileFilter imagesWildcardFilter { "*.jpeg;*.jpg;*.png;*.gif", "*", "Image File Filter"};
  83. TimeSliceThread directoryThread { "Image File Scanner Thread" };
  84. DirectoryContentsList imageList { &imagesWildcardFilter, directoryThread };
  85. FileTreeComponent fileTree { imageList };
  86. ImageComponent imagePreview;
  87. StretchableLayoutManager stretchableManager;
  88. StretchableLayoutResizerBar resizerBar { &stretchableManager, 1, false };
  89. void selectionChanged() override
  90. {
  91. // we're only really interested in when the selection changes, regardless of if it was
  92. // clicked or not so we'll only override this method
  93. auto selectedFile = fileTree.getSelectedFile();
  94. if (selectedFile.existsAsFile())
  95. imagePreview.setImage (ImageCache::getFromFile (selectedFile));
  96. // the image cache is a handy way to load images from files or directly from memory and
  97. // will keep them hanging around for a few seconds in case they are requested elsewhere
  98. }
  99. void fileClicked (const File&, const MouseEvent&) override {}
  100. void fileDoubleClicked (const File&) override {}
  101. void browserRootChanged (const File&) override {}
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ImagesDemo)
  103. };