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. String formatName;
  65. if (ImageFileFormat* format = ImageFileFormat::findImageFormatForStream (*input))
  66. formatName = " " + format->getFormatName();
  67. input = nullptr;
  68. Image image (ImageCache::getFromFile (file));
  69. if (image.isValid())
  70. {
  71. DrawableImage* d = new DrawableImage();
  72. d->setImage (image);
  73. drawable = d;
  74. facts.add (String (image.getWidth()) + " x " + String (image.getHeight()) + formatName);
  75. }
  76. if (totalSize > 0)
  77. facts.add (File::descriptionOfSizeInBytes (totalSize));
  78. }
  79. }
  80. if (drawable == nullptr)
  81. {
  82. ScopedPointer<XmlElement> svg (XmlDocument::parse (file));
  83. if (svg != nullptr)
  84. drawable = Drawable::createFromSVG (*svg);
  85. }
  86. facts.removeEmptyStrings (true);
  87. }
  88. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemPreviewComponent)
  89. };
  90. #endif // __JUCER_FILEPREVIEWCOMPONENT_JUCEHEADER__