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.

472 lines
15KB

  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. };
  28. static const ColourInfo colours[] =
  29. {
  30. { "Main Window Bkgd", mainBackgroundColourId, true },
  31. { "Treeview Highlight", treeviewHighlightColourId, false },
  32. { "Code Background", CodeEditorComponent::backgroundColourId, true },
  33. { "Line Number Bkgd", CodeEditorComponent::lineNumberBackgroundId, false },
  34. { "Line Numbers", CodeEditorComponent::lineNumberTextId, false },
  35. { "Plain Text", CodeEditorComponent::defaultTextColourId, false },
  36. { "Selected Text Bkgd", CodeEditorComponent::highlightColourId, false },
  37. { "Caret", CaretComponent::caretColourId, false }
  38. };
  39. }
  40. //==============================================================================
  41. AppearanceSettings::AppearanceSettings()
  42. : settings ("COLOUR_SCHEME")
  43. {
  44. IntrojucerLookAndFeel lf;
  45. for (int i = 0; i < sizeof (AppearanceColours::colours) / sizeof (AppearanceColours::colours[0]); ++i)
  46. getColourValue (AppearanceColours::colours[i].name) = lf.findColour (AppearanceColours::colours[i].colourID).toString();
  47. CodeDocument doc;
  48. CPlusPlusCodeTokeniser tokeniser;
  49. CodeEditorComponent editor (doc, &tokeniser);
  50. const CodeEditorComponent::ColourScheme cs (editor.getColourScheme());
  51. for (int i = cs.types.size(); --i >= 0;)
  52. {
  53. CodeEditorComponent::ColourScheme::TokenType& t = cs.types.getReference(i);
  54. getColourValue (t.name) = t.colour.toString();
  55. }
  56. Font f (editor.getFont());
  57. f.setTypefaceName (f.getTypeface()->getName());
  58. getCodeFontValue() = f.toString();
  59. settings.addListener (this);
  60. }
  61. File AppearanceSettings::getSchemesFolder()
  62. {
  63. File f (getAppProperties().getFile().getSiblingFile ("Colour Schemes"));
  64. f.createDirectory();
  65. return f;
  66. }
  67. void AppearanceSettings::refreshPresetSchemeList()
  68. {
  69. const File defaultSchemeFile (getSchemesFolder().getChildFile ("Default").withFileExtension (getSchemeFileSuffix()));
  70. if (! defaultSchemeFile.exists())
  71. AppearanceSettings().writeToFile (defaultSchemeFile);
  72. Array<File> newSchemes;
  73. getSchemesFolder().findChildFiles (newSchemes, File::findFiles, false, String ("*") + getSchemeFileSuffix());
  74. if (newSchemes != presetSchemeFiles)
  75. {
  76. presetSchemeFiles.swapWithArray (newSchemes);
  77. commandManager->commandStatusChanged();
  78. }
  79. }
  80. StringArray AppearanceSettings::getPresetSchemes()
  81. {
  82. StringArray s;
  83. for (int i = 0; i < presetSchemeFiles.size(); ++i)
  84. s.add (presetSchemeFiles.getReference(i).getFileNameWithoutExtension());
  85. return s;
  86. }
  87. void AppearanceSettings::selectPresetScheme (int index)
  88. {
  89. readFromFile (presetSchemeFiles [index]);
  90. }
  91. bool AppearanceSettings::readFromXML (const XmlElement& xml)
  92. {
  93. if (xml.hasTagName (settings.getType().toString()))
  94. {
  95. const ValueTree newSettings (ValueTree::fromXml (xml));
  96. // we'll manually copy across the new properties to the existing tree so that
  97. // any open editors will be kept up to date..
  98. settings.copyPropertiesFrom (newSettings, nullptr);
  99. for (int i = settings.getNumChildren(); --i >= 0;)
  100. {
  101. ValueTree c (settings.getChild (i));
  102. const ValueTree newValue (newSettings.getChildWithProperty (Ids::name, c.getProperty (Ids::name)));
  103. if (newValue.isValid())
  104. c.copyPropertiesFrom (newValue, nullptr);
  105. }
  106. return true;
  107. }
  108. return false;
  109. }
  110. bool AppearanceSettings::readFromFile (const File& file)
  111. {
  112. const ScopedPointer<XmlElement> xml (XmlDocument::parse (file));
  113. return xml != nullptr && readFromXML (*xml);
  114. }
  115. bool AppearanceSettings::writeToFile (const File& file) const
  116. {
  117. const ScopedPointer<XmlElement> xml (settings.createXml());
  118. return xml != nullptr && xml->writeToFile (file, String::empty);
  119. }
  120. StringArray AppearanceSettings::getColourNames() const
  121. {
  122. StringArray s;
  123. for (int i = 0; i < settings.getNumChildren(); ++i)
  124. {
  125. const ValueTree c (settings.getChild(i));
  126. if (c.hasType ("COLOUR"))
  127. s.add (c [Ids::name]);
  128. }
  129. return s;
  130. }
  131. void AppearanceSettings::updateColourScheme()
  132. {
  133. applyToLookAndFeel (LookAndFeel::getDefaultLookAndFeel());
  134. JucerApplication::getApp().mainWindowList.sendLookAndFeelChange();
  135. }
  136. void AppearanceSettings::applyToLookAndFeel (LookAndFeel& lf) const
  137. {
  138. for (int i = 0; i < sizeof (AppearanceColours::colours) / sizeof (AppearanceColours::colours[0]); ++i)
  139. {
  140. Colour col;
  141. if (getColour (AppearanceColours::colours[i].name, col))
  142. {
  143. if (AppearanceColours::colours[i].mustBeOpaque)
  144. col = Colours::white.overlaidWith (col);
  145. lf.setColour (AppearanceColours::colours[i].colourID, col);
  146. }
  147. }
  148. }
  149. void AppearanceSettings::applyToCodeEditor (CodeEditorComponent& editor) const
  150. {
  151. CodeEditorComponent::ColourScheme cs (editor.getColourScheme());
  152. for (int i = cs.types.size(); --i >= 0;)
  153. {
  154. CodeEditorComponent::ColourScheme::TokenType& t = cs.types.getReference(i);
  155. getColour (t.name, t.colour);
  156. }
  157. editor.setColourScheme (cs);
  158. editor.setFont (getCodeFont());
  159. }
  160. Font AppearanceSettings::getCodeFont() const
  161. {
  162. const String fontString (settings [Ids::font].toString());
  163. if (fontString.isEmpty())
  164. {
  165. #if JUCE_MAC
  166. Font font (13.0f);
  167. font.setTypefaceName ("Menlo");
  168. #else
  169. Font font (10.0f);
  170. font.setTypefaceName (Font::getDefaultMonospacedFontName());
  171. #endif
  172. return font;
  173. }
  174. return Font::fromString (fontString);
  175. }
  176. Value AppearanceSettings::getCodeFontValue()
  177. {
  178. return settings.getPropertyAsValue (Ids::font, nullptr);
  179. }
  180. Value AppearanceSettings::getColourValue (const String& colourName)
  181. {
  182. ValueTree c (settings.getChildWithProperty (Ids::name, colourName));
  183. if (! c.isValid())
  184. {
  185. c = ValueTree ("COLOUR");
  186. c.setProperty (Ids::name, colourName, nullptr);
  187. settings.addChild (c, -1, nullptr);
  188. }
  189. return c.getPropertyAsValue (Ids::colour, nullptr);
  190. }
  191. bool AppearanceSettings::getColour (const String& name, Colour& result) const
  192. {
  193. const ValueTree colour (settings.getChildWithProperty (Ids::name, name));
  194. if (colour.isValid())
  195. {
  196. result = Colour::fromString (colour [Ids::colour].toString());
  197. return true;
  198. }
  199. return false;
  200. }
  201. //==============================================================================
  202. struct AppearanceEditor
  203. {
  204. class Window : public DialogWindow
  205. {
  206. public:
  207. Window() : DialogWindow ("Appearance Settings", Colours::black, true, true)
  208. {
  209. setUsingNativeTitleBar (true);
  210. setContentOwned (new EditorPanel(), false);
  211. setResizable (true, true);
  212. const int width = 350;
  213. setResizeLimits (width, 200, width, 1000);
  214. String windowState (getAppProperties().getValue (getWindowPosName()));
  215. if (windowState.isNotEmpty())
  216. restoreWindowStateFromString (windowState);
  217. else
  218. centreAroundComponent (Component::getCurrentlyFocusedComponent(), width, 500);
  219. setVisible (true);
  220. }
  221. ~Window()
  222. {
  223. getAppProperties().setValue (getWindowPosName(), getWindowStateAsString());
  224. }
  225. void closeButtonPressed()
  226. {
  227. JucerApplication::getApp().appearanceEditorWindow = nullptr;
  228. }
  229. private:
  230. static const char* getWindowPosName() { return "colourSchemeEditorPos"; }
  231. JUCE_DECLARE_NON_COPYABLE (Window);
  232. };
  233. //==============================================================================
  234. class EditorPanel : public Component,
  235. private Button::Listener
  236. {
  237. public:
  238. EditorPanel()
  239. : loadButton ("Load Scheme..."),
  240. saveButton ("Save Scheme...")
  241. {
  242. setOpaque (true);
  243. rebuildProperties();
  244. addAndMakeVisible (&panel);
  245. loadButton.setColour (TextButton::buttonColourId, Colours::grey);
  246. saveButton.setColour (TextButton::buttonColourId, Colours::grey);
  247. addAndMakeVisible (&loadButton);
  248. addAndMakeVisible (&saveButton);
  249. loadButton.addListener (this);
  250. saveButton.addListener (this);
  251. }
  252. void rebuildProperties()
  253. {
  254. AppearanceSettings& scheme = getAppSettings().appearance;
  255. Array <PropertyComponent*> props;
  256. Value fontValue (scheme.getCodeFontValue());
  257. props.add (FontNameValueSource::createProperty ("Code Editor Font", fontValue));
  258. props.add (FontSizeValueSource::createProperty ("Font Size", fontValue));
  259. const StringArray colourNames (scheme.getColourNames());
  260. for (int i = 0; i < colourNames.size(); ++i)
  261. props.add (new ColourPropertyComponent (nullptr, colourNames[i],
  262. scheme.getColourValue (colourNames[i]),
  263. Colours::white, false));
  264. panel.clear();
  265. panel.addProperties (props);
  266. }
  267. void paint (Graphics& g)
  268. {
  269. g.fillAll (Colours::black);
  270. }
  271. void resized()
  272. {
  273. Rectangle<int> r (getLocalBounds());
  274. panel.setBounds (r.removeFromTop (getHeight() - 26).reduced (4, 3));
  275. loadButton.setBounds (r.removeFromLeft (getWidth() / 2).reduced (10, 3));
  276. saveButton.setBounds (r.reduced (10, 3));
  277. }
  278. private:
  279. PropertyPanel panel;
  280. TextButton loadButton, saveButton;
  281. void buttonClicked (Button* b)
  282. {
  283. if (b == &loadButton)
  284. loadScheme();
  285. else
  286. saveScheme();
  287. }
  288. void saveScheme()
  289. {
  290. FileChooser fc ("Select a file in which to save this colour-scheme...",
  291. getAppSettings().appearance.getSchemesFolder().getNonexistentChildFile ("Scheme", ".editorscheme"),
  292. "*.editorscheme");
  293. if (fc.browseForFileToSave (true))
  294. {
  295. File file (fc.getResult().withFileExtension (".editorscheme"));
  296. getAppSettings().appearance.writeToFile (file);
  297. getAppSettings().appearance.refreshPresetSchemeList();
  298. }
  299. }
  300. void loadScheme()
  301. {
  302. FileChooser fc ("Please select a colour-scheme file to load...",
  303. getAppSettings().appearance.getSchemesFolder(),
  304. "*.editorscheme");
  305. if (fc.browseForFileToOpen())
  306. {
  307. if (getAppSettings().appearance.readFromFile (fc.getResult()))
  308. rebuildProperties();
  309. }
  310. }
  311. JUCE_DECLARE_NON_COPYABLE (EditorPanel);
  312. };
  313. //==============================================================================
  314. class FontNameValueSource : public ValueSourceFilter
  315. {
  316. public:
  317. FontNameValueSource (const Value& source) : ValueSourceFilter (source) {}
  318. var getValue() const
  319. {
  320. return Font::fromString (sourceValue.toString()).getTypefaceName();
  321. }
  322. void setValue (const var& newValue)
  323. {
  324. Font font (Font::fromString (sourceValue.toString()));
  325. font.setTypefaceName (newValue.toString());
  326. sourceValue = font.toString();
  327. }
  328. static ChoicePropertyComponent* createProperty (const String& title, const Value& value)
  329. {
  330. const StringArray& fontNames = getAppSettings().getFontNames();
  331. Array<var> values;
  332. for (int i = 0; i < fontNames.size(); ++i)
  333. values.add (fontNames[i]);
  334. return new ChoicePropertyComponent (Value (new FontNameValueSource (value)),
  335. title, fontNames, values);
  336. }
  337. };
  338. //==============================================================================
  339. class FontSizeValueSource : public ValueSourceFilter
  340. {
  341. public:
  342. FontSizeValueSource (const Value& source) : ValueSourceFilter (source) {}
  343. var getValue() const
  344. {
  345. return Font::fromString (sourceValue.toString()).getHeight();
  346. }
  347. void setValue (const var& newValue)
  348. {
  349. sourceValue = Font::fromString (sourceValue.toString()).withHeight (newValue).toString();
  350. }
  351. static PropertyComponent* createProperty (const String& title, const Value& value)
  352. {
  353. return new SliderPropertyComponent (Value (new FontSizeValueSource (value)),
  354. title, 5.0, 40.0, 0.1, 0.5);
  355. }
  356. };
  357. };
  358. Component* AppearanceSettings::createEditorWindow()
  359. {
  360. return new AppearanceEditor::Window();
  361. }
  362. //==============================================================================
  363. IntrojucerLookAndFeel::IntrojucerLookAndFeel()
  364. {
  365. setColour (mainBackgroundColourId, Colour::greyLevel (0.8f));
  366. setColour (treeviewHighlightColourId, Colour (0x401111ee));
  367. }
  368. Rectangle<int> IntrojucerLookAndFeel::getPropertyComponentContentPosition (PropertyComponent& component)
  369. {
  370. if (component.findParentComponentOfClass<AppearanceEditor::EditorPanel>() != nullptr)
  371. return component.getLocalBounds().reduced (1, 1).removeFromRight (component.getWidth() / 2);
  372. return LookAndFeel::getPropertyComponentContentPosition (component);
  373. }