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.

123 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_FILEPREVIEWCOMPONENT_JUCEHEADER__
  19. #define __JUCER_FILEPREVIEWCOMPONENT_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. */
  23. class ItemPreviewComponent : public Component
  24. {
  25. public:
  26. ItemPreviewComponent (const File& f)
  27. : file (f)
  28. {
  29. setOpaque (true);
  30. tryToLoadImage();
  31. }
  32. void paint (Graphics& g)
  33. {
  34. IntrojucerLookAndFeel::fillWithBackgroundTexture (*this, g);
  35. if (drawable != nullptr)
  36. {
  37. Rectangle<int> area = RectanglePlacement (RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize)
  38. .appliedTo (drawable->getBounds(), Rectangle<int> (4, 22, getWidth() - 8, getHeight() - 26));
  39. Path p;
  40. p.addRectangle (area);
  41. DropShadow (Colours::black.withAlpha (0.5f), 6, Point<int> (0, 1)).drawForPath (g, p);
  42. g.fillCheckerBoard (area, 24, 24, Colour (0xffffffff), Colour (0xffeeeeee));
  43. g.setOpacity (1.0f);
  44. drawable->drawWithin (g, area.toFloat(), RectanglePlacement::stretchToFit, 1.0f);
  45. }
  46. g.setFont (Font (14.0f, Font::bold));
  47. g.setColour (findColour (mainBackgroundColourId).contrasting());
  48. g.drawMultiLineText (facts.joinIntoString ("\n"), 10, 15, getWidth() - 16);
  49. }
  50. private:
  51. StringArray facts;
  52. File file;
  53. ScopedPointer<Drawable> drawable;
  54. void tryToLoadImage()
  55. {
  56. facts.clear();
  57. facts.add (file.getFullPathName());
  58. drawable = nullptr;
  59. {
  60. ScopedPointer <InputStream> input (file.createInputStream());
  61. if (input != nullptr)
  62. {
  63. const int64 totalSize = input->getTotalLength();
  64. ImageFileFormat* format = ImageFileFormat::findImageFormatForStream (*input);
  65. input = nullptr;
  66. String formatName;
  67. if (format != nullptr)
  68. formatName = " " + format->getFormatName();
  69. Image image (ImageCache::getFromFile (file));
  70. if (image.isValid())
  71. {
  72. DrawableImage* d = new DrawableImage();
  73. d->setImage (image);
  74. drawable = d;
  75. facts.add (String (image.getWidth()) + " x " + String (image.getHeight()) + formatName);
  76. }
  77. if (totalSize > 0)
  78. facts.add (File::descriptionOfSizeInBytes (totalSize));
  79. }
  80. }
  81. if (drawable == nullptr)
  82. {
  83. ScopedPointer<XmlElement> svg (XmlDocument::parse (file));
  84. if (svg != nullptr)
  85. drawable = Drawable::createFromSVG (*svg);
  86. }
  87. facts.removeEmptyStrings (true);
  88. }
  89. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemPreviewComponent);
  90. };
  91. #endif // __JUCER_FILEPREVIEWCOMPONENT_JUCEHEADER__