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.

774 lines
28KB

  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. #include "jucer_Application.h"
  19. #include "jucer_AppearanceSettings.h"
  20. namespace AppearanceColours
  21. {
  22. struct ColourInfo
  23. {
  24. const char* name;
  25. uint32 colourID;
  26. bool mustBeOpaque;
  27. bool applyToEditorOnly;
  28. };
  29. static const ColourInfo colours[] =
  30. {
  31. { "Main Window Bkgd", mainBackgroundColourId, true, false },
  32. { "Treeview Highlight", treeviewHighlightColourId, false, false },
  33. { "Code Background", CodeEditorComponent::backgroundColourId, true, false },
  34. { "Line Number Bkgd", CodeEditorComponent::lineNumberBackgroundId, false, false },
  35. { "Line Numbers", CodeEditorComponent::lineNumberTextId, false, false },
  36. { "Plain Text", CodeEditorComponent::defaultTextColourId, false, false },
  37. { "Selected Text Bkgd", CodeEditorComponent::highlightColourId, false, false },
  38. { "Caret", CaretComponent::caretColourId, false, true }
  39. };
  40. enum
  41. {
  42. numColours = sizeof (AppearanceColours::colours) / sizeof (AppearanceColours::colours[0])
  43. };
  44. }
  45. //==============================================================================
  46. AppearanceSettings::AppearanceSettings (bool updateAppWhenChanged)
  47. : settings ("COLOUR_SCHEME")
  48. {
  49. IntrojucerLookAndFeel lf;
  50. for (int i = 0; i < AppearanceColours::numColours; ++i)
  51. getColourValue (AppearanceColours::colours[i].name) = lf.findColour (AppearanceColours::colours[i].colourID).toString();
  52. CodeDocument doc;
  53. CPlusPlusCodeTokeniser tokeniser;
  54. CodeEditorComponent editor (doc, &tokeniser);
  55. const CodeEditorComponent::ColourScheme cs (editor.getColourScheme());
  56. for (int i = cs.types.size(); --i >= 0;)
  57. {
  58. CodeEditorComponent::ColourScheme::TokenType& t = cs.types.getReference(i);
  59. getColourValue (t.name) = t.colour.toString();
  60. }
  61. getCodeFontValue() = getDefaultCodeFont().toString();
  62. if (updateAppWhenChanged)
  63. settings.addListener (this);
  64. }
  65. File AppearanceSettings::getSchemesFolder()
  66. {
  67. File f (getGlobalProperties().getFile().getSiblingFile ("Schemes"));
  68. f.createDirectory();
  69. return f;
  70. }
  71. void AppearanceSettings::writeDefaultSchemeFile (const String& xmlString, const String& name)
  72. {
  73. const File file (getSchemesFolder().getChildFile (name).withFileExtension (getSchemeFileSuffix()));
  74. AppearanceSettings settings (false);
  75. ScopedPointer<XmlElement> xml (XmlDocument::parse (xmlString));
  76. if (xml != nullptr)
  77. settings.readFromXML (*xml);
  78. settings.writeToFile (file);
  79. }
  80. void AppearanceSettings::refreshPresetSchemeList()
  81. {
  82. writeDefaultSchemeFile (BinaryData::colourscheme_dark_xml, "Default (Dark)");
  83. writeDefaultSchemeFile (BinaryData::colourscheme_light_xml, "Default (Light)");
  84. Array<File> newSchemes;
  85. getSchemesFolder().findChildFiles (newSchemes, File::findFiles, false, String ("*") + getSchemeFileSuffix());
  86. if (newSchemes != presetSchemeFiles)
  87. {
  88. presetSchemeFiles.swapWithArray (newSchemes);
  89. commandManager->commandStatusChanged();
  90. }
  91. }
  92. StringArray AppearanceSettings::getPresetSchemes()
  93. {
  94. StringArray s;
  95. for (int i = 0; i < presetSchemeFiles.size(); ++i)
  96. s.add (presetSchemeFiles.getReference(i).getFileNameWithoutExtension());
  97. return s;
  98. }
  99. void AppearanceSettings::selectPresetScheme (int index)
  100. {
  101. readFromFile (presetSchemeFiles [index]);
  102. }
  103. bool AppearanceSettings::readFromXML (const XmlElement& xml)
  104. {
  105. if (xml.hasTagName (settings.getType().toString()))
  106. {
  107. const ValueTree newSettings (ValueTree::fromXml (xml));
  108. // we'll manually copy across the new properties to the existing tree so that
  109. // any open editors will be kept up to date..
  110. settings.copyPropertiesFrom (newSettings, nullptr);
  111. for (int i = settings.getNumChildren(); --i >= 0;)
  112. {
  113. ValueTree c (settings.getChild (i));
  114. const ValueTree newValue (newSettings.getChildWithProperty (Ids::name, c.getProperty (Ids::name)));
  115. if (newValue.isValid())
  116. c.copyPropertiesFrom (newValue, nullptr);
  117. }
  118. return true;
  119. }
  120. return false;
  121. }
  122. bool AppearanceSettings::readFromFile (const File& file)
  123. {
  124. const ScopedPointer<XmlElement> xml (XmlDocument::parse (file));
  125. return xml != nullptr && readFromXML (*xml);
  126. }
  127. bool AppearanceSettings::writeToFile (const File& file) const
  128. {
  129. const ScopedPointer<XmlElement> xml (settings.createXml());
  130. return xml != nullptr && xml->writeToFile (file, String::empty);
  131. }
  132. Font AppearanceSettings::getDefaultCodeFont()
  133. {
  134. return Font (Font::getDefaultMonospacedFontName(), Font::getDefaultStyle(), 13.0f);
  135. }
  136. StringArray AppearanceSettings::getColourNames() const
  137. {
  138. StringArray s;
  139. for (int i = 0; i < settings.getNumChildren(); ++i)
  140. {
  141. const ValueTree c (settings.getChild(i));
  142. if (c.hasType ("COLOUR"))
  143. s.add (c [Ids::name]);
  144. }
  145. return s;
  146. }
  147. void AppearanceSettings::updateColourScheme()
  148. {
  149. applyToLookAndFeel (LookAndFeel::getDefaultLookAndFeel());
  150. IntrojucerApp::getApp().mainWindowList.sendLookAndFeelChange();
  151. }
  152. void AppearanceSettings::applyToLookAndFeel (LookAndFeel& lf) const
  153. {
  154. for (int i = 0; i < AppearanceColours::numColours; ++i)
  155. {
  156. Colour col;
  157. if (getColour (AppearanceColours::colours[i].name, col))
  158. {
  159. if (AppearanceColours::colours[i].mustBeOpaque)
  160. col = Colours::white.overlaidWith (col);
  161. if (! AppearanceColours::colours[i].applyToEditorOnly)
  162. lf.setColour (AppearanceColours::colours[i].colourID, col);
  163. }
  164. }
  165. lf.setColour (ScrollBar::thumbColourId,
  166. IntrojucerLookAndFeel::getScrollbarColourForBackground (lf.findColour (mainBackgroundColourId)));
  167. }
  168. void AppearanceSettings::applyToCodeEditor (CodeEditorComponent& editor) const
  169. {
  170. CodeEditorComponent::ColourScheme cs (editor.getColourScheme());
  171. for (int i = cs.types.size(); --i >= 0;)
  172. {
  173. CodeEditorComponent::ColourScheme::TokenType& t = cs.types.getReference(i);
  174. getColour (t.name, t.colour);
  175. }
  176. editor.setColourScheme (cs);
  177. editor.setFont (getCodeFont());
  178. for (int i = 0; i < AppearanceColours::numColours; ++i)
  179. {
  180. if (AppearanceColours::colours[i].applyToEditorOnly)
  181. {
  182. Colour col;
  183. if (getColour (AppearanceColours::colours[i].name, col))
  184. editor.setColour (AppearanceColours::colours[i].colourID, col);
  185. }
  186. }
  187. editor.setColour (ScrollBar::thumbColourId,
  188. IntrojucerLookAndFeel::getScrollbarColourForBackground (editor.findColour (CodeEditorComponent::backgroundColourId)));
  189. }
  190. Font AppearanceSettings::getCodeFont() const
  191. {
  192. const String fontString (settings [Ids::font].toString());
  193. if (fontString.isEmpty())
  194. return getDefaultCodeFont();
  195. return Font::fromString (fontString);
  196. }
  197. Value AppearanceSettings::getCodeFontValue()
  198. {
  199. return settings.getPropertyAsValue (Ids::font, nullptr);
  200. }
  201. Value AppearanceSettings::getColourValue (const String& colourName)
  202. {
  203. ValueTree c (settings.getChildWithProperty (Ids::name, colourName));
  204. if (! c.isValid())
  205. {
  206. c = ValueTree ("COLOUR");
  207. c.setProperty (Ids::name, colourName, nullptr);
  208. settings.addChild (c, -1, nullptr);
  209. }
  210. return c.getPropertyAsValue (Ids::colour, nullptr);
  211. }
  212. bool AppearanceSettings::getColour (const String& name, Colour& result) const
  213. {
  214. const ValueTree colour (settings.getChildWithProperty (Ids::name, name));
  215. if (colour.isValid())
  216. {
  217. result = Colour::fromString (colour [Ids::colour].toString());
  218. return true;
  219. }
  220. return false;
  221. }
  222. //==============================================================================
  223. struct AppearanceEditor
  224. {
  225. class FontScanPanel : public Component,
  226. private Timer
  227. {
  228. public:
  229. FontScanPanel()
  230. {
  231. fontsToScan = Font::findAllTypefaceNames();
  232. startTimer (1);
  233. }
  234. void paint (Graphics& g)
  235. {
  236. g.fillAll (Colours::darkgrey);
  237. g.setFont (14.0f);
  238. g.setColour (Colours::white);
  239. g.drawFittedText ("Scanning for fonts..", getLocalBounds(), Justification::centred, 2);
  240. const int size = 30;
  241. getLookAndFeel().drawSpinningWaitAnimation (g, Colours::white, (getWidth() - size) / 2, getHeight() / 2 - 50, size, size);
  242. }
  243. void timerCallback()
  244. {
  245. repaint();
  246. if (fontsToScan.size() == 0)
  247. {
  248. getAppSettings().monospacedFontNames = fontsFound;
  249. DialogWindow* w = findParentComponentOfClass<DialogWindow>();
  250. if (w != nullptr)
  251. w->setContentOwned (new EditorPanel(), false);
  252. }
  253. else
  254. {
  255. if (isMonospacedTypeface (fontsToScan[0]))
  256. fontsFound.add (fontsToScan[0]);
  257. fontsToScan.remove (0);
  258. }
  259. }
  260. // A rather hacky trick to select only the fixed-pitch fonts..
  261. // This is unfortunately a bit slow, but will work on all platforms.
  262. static bool isMonospacedTypeface (const String& name)
  263. {
  264. const Font font (name, 20.0f, Font::plain);
  265. const int width = font.getStringWidth ("....");
  266. return width == font.getStringWidth ("WWWW")
  267. && width == font.getStringWidth ("0000")
  268. && width == font.getStringWidth ("1111")
  269. && width == font.getStringWidth ("iiii");
  270. }
  271. StringArray fontsToScan, fontsFound;
  272. };
  273. //==============================================================================
  274. class EditorPanel : public Component,
  275. private ButtonListener
  276. {
  277. public:
  278. EditorPanel()
  279. : loadButton ("Load Scheme..."),
  280. saveButton ("Save Scheme...")
  281. {
  282. rebuildProperties();
  283. addAndMakeVisible (&panel);
  284. loadButton.setColour (TextButton::buttonColourId, Colours::lightgrey.withAlpha (0.5f));
  285. saveButton.setColour (TextButton::buttonColourId, Colours::lightgrey.withAlpha (0.5f));
  286. loadButton.setColour (TextButton::textColourOffId, Colours::white);
  287. saveButton.setColour (TextButton::textColourOffId, Colours::white);
  288. addAndMakeVisible (&loadButton);
  289. addAndMakeVisible (&saveButton);
  290. loadButton.addListener (this);
  291. saveButton.addListener (this);
  292. }
  293. void rebuildProperties()
  294. {
  295. AppearanceSettings& scheme = getAppSettings().appearance;
  296. Array <PropertyComponent*> props;
  297. Value fontValue (scheme.getCodeFontValue());
  298. props.add (FontNameValueSource::createProperty ("Code Editor Font", fontValue));
  299. props.add (FontSizeValueSource::createProperty ("Font Size", fontValue));
  300. const StringArray colourNames (scheme.getColourNames());
  301. for (int i = 0; i < colourNames.size(); ++i)
  302. props.add (new ColourPropertyComponent (nullptr, colourNames[i],
  303. scheme.getColourValue (colourNames[i]),
  304. Colours::white, false));
  305. panel.clear();
  306. panel.addProperties (props);
  307. }
  308. void resized()
  309. {
  310. Rectangle<int> r (getLocalBounds());
  311. panel.setBounds (r.removeFromTop (getHeight() - 28).reduced (4, 2));
  312. loadButton.setBounds (r.removeFromLeft (getWidth() / 2).reduced (10, 4));
  313. saveButton.setBounds (r.reduced (10, 3));
  314. }
  315. private:
  316. PropertyPanel panel;
  317. TextButton loadButton, saveButton;
  318. void buttonClicked (Button* b)
  319. {
  320. if (b == &loadButton)
  321. loadScheme();
  322. else
  323. saveScheme();
  324. }
  325. void saveScheme()
  326. {
  327. FileChooser fc ("Select a file in which to save this colour-scheme...",
  328. getAppSettings().appearance.getSchemesFolder()
  329. .getNonexistentChildFile ("Scheme", AppearanceSettings::getSchemeFileSuffix()),
  330. AppearanceSettings::getSchemeFileWildCard());
  331. if (fc.browseForFileToSave (true))
  332. {
  333. File file (fc.getResult().withFileExtension (AppearanceSettings::getSchemeFileSuffix()));
  334. getAppSettings().appearance.writeToFile (file);
  335. getAppSettings().appearance.refreshPresetSchemeList();
  336. }
  337. }
  338. void loadScheme()
  339. {
  340. FileChooser fc ("Please select a colour-scheme file to load...",
  341. getAppSettings().appearance.getSchemesFolder(),
  342. AppearanceSettings::getSchemeFileWildCard());
  343. if (fc.browseForFileToOpen())
  344. {
  345. if (getAppSettings().appearance.readFromFile (fc.getResult()))
  346. rebuildProperties();
  347. }
  348. }
  349. JUCE_DECLARE_NON_COPYABLE (EditorPanel)
  350. };
  351. //==============================================================================
  352. class FontNameValueSource : public ValueSourceFilter
  353. {
  354. public:
  355. FontNameValueSource (const Value& source) : ValueSourceFilter (source) {}
  356. var getValue() const
  357. {
  358. return Font::fromString (sourceValue.toString()).getTypefaceName();
  359. }
  360. void setValue (const var& newValue)
  361. {
  362. Font font (Font::fromString (sourceValue.toString()));
  363. font.setTypefaceName (newValue.toString().isEmpty() ? Font::getDefaultMonospacedFontName()
  364. : newValue.toString());
  365. sourceValue = font.toString();
  366. }
  367. static ChoicePropertyComponent* createProperty (const String& title, const Value& value)
  368. {
  369. StringArray fontNames = getAppSettings().monospacedFontNames;
  370. Array<var> values;
  371. values.add (Font::getDefaultMonospacedFontName());
  372. values.add (var());
  373. for (int i = 0; i < fontNames.size(); ++i)
  374. values.add (fontNames[i]);
  375. StringArray names;
  376. names.add ("<Default Monospaced>");
  377. names.add (String::empty);
  378. names.addArray (getAppSettings().monospacedFontNames);
  379. return new ChoicePropertyComponent (Value (new FontNameValueSource (value)),
  380. title, names, values);
  381. }
  382. };
  383. //==============================================================================
  384. class FontSizeValueSource : public ValueSourceFilter
  385. {
  386. public:
  387. FontSizeValueSource (const Value& source) : ValueSourceFilter (source) {}
  388. var getValue() const
  389. {
  390. return Font::fromString (sourceValue.toString()).getHeight();
  391. }
  392. void setValue (const var& newValue)
  393. {
  394. sourceValue = Font::fromString (sourceValue.toString()).withHeight (newValue).toString();
  395. }
  396. static PropertyComponent* createProperty (const String& title, const Value& value)
  397. {
  398. return new SliderPropertyComponent (Value (new FontSizeValueSource (value)),
  399. title, 5.0, 40.0, 0.1, 0.5);
  400. }
  401. };
  402. };
  403. void AppearanceSettings::showEditorWindow (ScopedPointer<Component>& ownerPointer)
  404. {
  405. if (ownerPointer != nullptr)
  406. {
  407. ownerPointer->toFront (true);
  408. }
  409. else
  410. {
  411. Component* content;
  412. if (getAppSettings().monospacedFontNames.size() == 0)
  413. content = new AppearanceEditor::FontScanPanel();
  414. else
  415. content = new AppearanceEditor::EditorPanel();
  416. const int width = 350;
  417. new FloatingToolWindow ("Appearance Settings",
  418. "colourSchemeEditorPos",
  419. content, ownerPointer,
  420. width, 500,
  421. width, 200, width, 1000);
  422. }
  423. }
  424. //==============================================================================
  425. IntrojucerLookAndFeel::IntrojucerLookAndFeel()
  426. {
  427. setColour (mainBackgroundColourId, Colour::greyLevel (0.8f));
  428. setColour (treeviewHighlightColourId, Colour (0x401111ee));
  429. setColour (TextButton::buttonColourId, Colour (0xffeeeeff));
  430. setColour (ScrollBar::thumbColourId,
  431. getScrollbarColourForBackground (findColour (mainBackgroundColourId)));
  432. }
  433. Colour IntrojucerLookAndFeel::getScrollbarColourForBackground (const Colour& background)
  434. {
  435. return background.contrasting().withAlpha (0.13f);
  436. }
  437. Rectangle<int> IntrojucerLookAndFeel::getPropertyComponentContentPosition (PropertyComponent& component)
  438. {
  439. if (component.findParentComponentOfClass<AppearanceEditor::EditorPanel>() != nullptr)
  440. return component.getLocalBounds().reduced (1).removeFromRight (component.getWidth() / 2);
  441. return LookAndFeel::getPropertyComponentContentPosition (component);
  442. }
  443. int IntrojucerLookAndFeel::getTabButtonOverlap (int /*tabDepth*/) { return -1; }
  444. int IntrojucerLookAndFeel::getTabButtonSpaceAroundImage() { return 1; }
  445. int IntrojucerLookAndFeel::getTabButtonBestWidth (TabBarButton&, int /*tabDepth*/) { return 120; }
  446. void IntrojucerLookAndFeel::createTabTextLayout (const TabBarButton& button, const Rectangle<int>& textArea, GlyphArrangement& textLayout)
  447. {
  448. Font font (textArea.getHeight() * 0.5f);
  449. font.setUnderline (button.hasKeyboardFocus (false));
  450. textLayout.addFittedText (font, button.getButtonText().trim(),
  451. (float) textArea.getX(), (float) textArea.getY(), (float) textArea.getWidth(), (float) textArea.getHeight(),
  452. Justification::centred, 1);
  453. }
  454. Colour IntrojucerLookAndFeel::getTabBackgroundColour (TabBarButton& button)
  455. {
  456. const Colour normalBkg (button.findColour (mainBackgroundColourId));
  457. Colour bkg (normalBkg.contrasting (0.15f));
  458. if (button.isFrontTab())
  459. bkg = bkg.overlaidWith (Colours::yellow.withAlpha (0.5f));
  460. return bkg;
  461. }
  462. void IntrojucerLookAndFeel::drawTabButton (TabBarButton& button, Graphics& g, bool isMouseOver, bool isMouseDown)
  463. {
  464. const Rectangle<int> activeArea (button.getActiveArea());
  465. const Colour bkg (getTabBackgroundColour (button));
  466. g.setGradientFill (ColourGradient (bkg.brighter (0.1f), 0, (float) activeArea.getY(),
  467. bkg.darker (0.1f), 0, (float) activeArea.getBottom(), false));
  468. g.fillRect (activeArea);
  469. g.setColour (button.findColour (mainBackgroundColourId).darker (0.3f));
  470. g.drawRect (activeArea);
  471. GlyphArrangement textLayout;
  472. createTabTextLayout (button, button.getTextArea(), textLayout);
  473. const float alpha = button.isEnabled() ? ((isMouseOver || isMouseDown) ? 1.0f : 0.8f) : 0.3f;
  474. g.setColour (bkg.contrasting().withMultipliedAlpha (alpha));
  475. textLayout.draw (g);
  476. }
  477. Rectangle<int> IntrojucerLookAndFeel::getTabButtonExtraComponentBounds (const TabBarButton& button, Rectangle<int>& textArea, Component& comp)
  478. {
  479. GlyphArrangement textLayout;
  480. createTabTextLayout (button, textArea, textLayout);
  481. const int textWidth = (int) textLayout.getBoundingBox (0, -1, false).getWidth();
  482. const int extraSpace = jmax (0, textArea.getWidth() - (textWidth + comp.getWidth())) / 2;
  483. textArea.removeFromRight (extraSpace);
  484. textArea.removeFromLeft (extraSpace);
  485. return textArea.removeFromRight (comp.getWidth());
  486. }
  487. void IntrojucerLookAndFeel::drawStretchableLayoutResizerBar (Graphics& g, int /*w*/, int /*h*/, bool /*isVerticalBar*/, bool isMouseOver, bool isMouseDragging)
  488. {
  489. if (isMouseOver || isMouseDragging)
  490. g.fillAll (Colours::yellow.withAlpha (0.4f));
  491. }
  492. void IntrojucerLookAndFeel::drawScrollbar (Graphics& g, ScrollBar& scrollbar, int x, int y, int width, int height,
  493. bool isScrollbarVertical, int thumbStartPosition, int thumbSize,
  494. bool isMouseOver, bool isMouseDown)
  495. {
  496. Path thumbPath;
  497. if (thumbSize > 0)
  498. {
  499. const float thumbIndent = (isScrollbarVertical ? width : height) * 0.25f;
  500. const float thumbIndentx2 = thumbIndent * 2.0f;
  501. if (isScrollbarVertical)
  502. thumbPath.addRoundedRectangle (x + thumbIndent, thumbStartPosition + thumbIndent,
  503. width - thumbIndentx2, thumbSize - thumbIndentx2, (width - thumbIndentx2) * 0.5f);
  504. else
  505. thumbPath.addRoundedRectangle (thumbStartPosition + thumbIndent, y + thumbIndent,
  506. thumbSize - thumbIndentx2, height - thumbIndentx2, (height - thumbIndentx2) * 0.5f);
  507. }
  508. Colour thumbCol (scrollbar.findColour (ScrollBar::thumbColourId, true));
  509. if (isMouseOver || isMouseDown)
  510. thumbCol = thumbCol.withMultipliedAlpha (2.0f);
  511. g.setColour (thumbCol);
  512. g.fillPath (thumbPath);
  513. g.setColour (thumbCol.contrasting ((isMouseOver || isMouseDown) ? 0.2f : 0.1f));
  514. g.strokePath (thumbPath, PathStrokeType (1.0f));
  515. }
  516. static Range<float> getBrightnessRange (const Image& im)
  517. {
  518. float minB = 1.0f, maxB = 0;
  519. const int w = im.getWidth();
  520. const int h = im.getHeight();
  521. for (int y = 0; y < h; ++y)
  522. {
  523. for (int x = 0; x < w; ++x)
  524. {
  525. const float b = im.getPixelAt (x, y).getBrightness();
  526. minB = jmin (minB, b);
  527. maxB = jmax (maxB, b);
  528. }
  529. }
  530. return Range<float> (minB, maxB);
  531. }
  532. void IntrojucerLookAndFeel::fillWithBackgroundTexture (Graphics& g)
  533. {
  534. const Colour bkg (findColour (mainBackgroundColourId));
  535. if (backgroundTextureBaseColour != bkg)
  536. {
  537. backgroundTextureBaseColour = bkg;
  538. const Image original (ImageCache::getFromMemory (BinaryData::background_tile_png,
  539. BinaryData::background_tile_pngSize));
  540. const int w = original.getWidth();
  541. const int h = original.getHeight();
  542. backgroundTexture = Image (Image::RGB, w, h, false);
  543. const Range<float> brightnessRange (getBrightnessRange (original));
  544. const float brightnessOffset = (brightnessRange.getStart() + brightnessRange.getEnd()) / 2.0f;
  545. const float brightnessScale = 0.025f / brightnessRange.getLength();
  546. const float bkgB = bkg.getBrightness();
  547. for (int y = 0; y < h; ++y)
  548. {
  549. for (int x = 0; x < w; ++x)
  550. {
  551. const float b = (original.getPixelAt (x, y).getBrightness() - brightnessOffset) * brightnessScale;
  552. backgroundTexture.setPixelAt (x, y, bkg.withBrightness (jlimit (0.0f, 1.0f, bkgB + b)));
  553. }
  554. }
  555. }
  556. g.setTiledImageFill (backgroundTexture, 0, 0, 1.0f);
  557. g.fillAll();
  558. }
  559. void IntrojucerLookAndFeel::fillWithBackgroundTexture (Component& c, Graphics& g)
  560. {
  561. dynamic_cast<IntrojucerLookAndFeel&> (c.getLookAndFeel()).fillWithBackgroundTexture (g);
  562. }
  563. void IntrojucerLookAndFeel::drawConcertinaPanelHeader (Graphics& g, const Rectangle<int>& area,
  564. bool isMouseOver, bool /*isMouseDown*/,
  565. ConcertinaPanel&, Component& panel)
  566. {
  567. const Colour bkg (findColour (mainBackgroundColourId));
  568. g.setGradientFill (ColourGradient (Colours::white.withAlpha (isMouseOver ? 0.4f : 0.2f), 0, (float) area.getY(),
  569. Colours::darkgrey.withAlpha (0.2f), 0, (float) area.getBottom(), false));
  570. g.fillAll();
  571. g.setColour (bkg.contrasting().withAlpha (0.04f));
  572. g.fillRect (area.withHeight (1));
  573. g.fillRect (area.withTop (area.getBottom() - 1));
  574. g.setColour (bkg.contrasting());
  575. g.setFont (Font (area.getHeight() * 0.6f).boldened());
  576. g.drawFittedText (panel.getName(), 4, 0, area.getWidth() - 6, area.getHeight(), Justification::centredLeft, 1);
  577. }
  578. void IntrojucerLookAndFeel::drawButtonBackground (Graphics& g,
  579. Button& button,
  580. const Colour& backgroundColour,
  581. bool isMouseOverButton,
  582. bool isButtonDown)
  583. {
  584. const bool flatOnLeft = button.isConnectedOnLeft();
  585. const bool flatOnRight = button.isConnectedOnRight();
  586. const bool flatOnTop = button.isConnectedOnTop();
  587. const bool flatOnBottom = button.isConnectedOnBottom();
  588. const float width = (float) button.getWidth();
  589. const float height = (float) button.getHeight();
  590. const float x = 0.5f;
  591. const float y = 0.5f;
  592. const float w = width - 1.0f;
  593. const float h = height - 1.0f;
  594. const float cornerSize = 4.0f;
  595. Colour baseColour (backgroundColour.withMultipliedSaturation (button.hasKeyboardFocus (true)
  596. ? 1.3f : 0.9f)
  597. .withMultipliedAlpha (button.isEnabled() ? 0.9f : 0.5f));
  598. if (isButtonDown) baseColour = baseColour.contrasting (0.2f);
  599. else if (isMouseOverButton) baseColour = baseColour.contrasting (0.1f);
  600. const float mainBrightness = baseColour.getBrightness();
  601. const float mainAlpha = baseColour.getFloatAlpha();
  602. Path outline;
  603. outline.addRoundedRectangle (x, y, w, h, cornerSize, cornerSize,
  604. ! (flatOnLeft || flatOnTop),
  605. ! (flatOnRight || flatOnTop),
  606. ! (flatOnLeft || flatOnBottom),
  607. ! (flatOnRight || flatOnBottom));
  608. g.setGradientFill (ColourGradient (baseColour.brighter (0.2f), 0.0f, 0.0f,
  609. baseColour.darker (0.25f), 0.0f, height, false));
  610. g.fillPath (outline);
  611. g.setColour (Colours::white.withAlpha (0.4f * mainAlpha * mainBrightness * mainBrightness));
  612. g.strokePath (outline, PathStrokeType (1.0f), AffineTransform::translation (0.0f, 1.0f)
  613. .scaled (1.0f, (h - 1.6f) / h));
  614. g.setColour (Colours::black.withAlpha (0.4f * mainAlpha));
  615. g.strokePath (outline, PathStrokeType (1.0f));
  616. }