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.

137 lines
5.3KB

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