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.

193 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. static Typeface::Ptr getTypefaceForFontFromLookAndFeel (const Font& font)
  22. {
  23. return LookAndFeel::getDefaultLookAndFeel().getTypefaceForFont (font);
  24. }
  25. using GetTypefaceForFont = Typeface::Ptr (*)(const Font&);
  26. extern GetTypefaceForFont juce_getTypefaceForFont;
  27. //==============================================================================
  28. LookAndFeel::LookAndFeel()
  29. {
  30. /* if this fails it means you're trying to create a LookAndFeel object before
  31. the static Colours have been initialised. That ain't gonna work. It probably
  32. means that you're using a static LookAndFeel object and that your compiler has
  33. decided to intialise it before the Colours class.
  34. */
  35. jassert (Colours::white == Colour (0xffffffff));
  36. juce_getTypefaceForFont = getTypefaceForFontFromLookAndFeel;
  37. }
  38. LookAndFeel::~LookAndFeel()
  39. {
  40. /* This assertion is triggered if you try to delete a LookAndFeel object while something
  41. is still using it!
  42. Reasons may be:
  43. - it's still being used as the default LookAndFeel; or
  44. - it's set as a Component's current lookandfeel; or
  45. - there's a WeakReference to it somewhere else in your code
  46. Generally the fix for this will be to make sure you call
  47. Component::setLookandFeel (nullptr) on any components that were still using
  48. it before you delete it, or call LookAndFeel::setDefaultLookAndFeel (nullptr)
  49. if you had set it up to be the default one. This assertion can also be avoided by
  50. declaring your LookAndFeel object before any of the Components that use it as
  51. the Components will be destroyed before the LookAndFeel.
  52. Deleting a LookAndFeel is unlikely to cause a crash since most things will use a
  53. safe WeakReference to it, but it could cause some unexpected graphical behaviour,
  54. so it's advisable to clear up any references before destroying them!
  55. */
  56. jassert (masterReference.getNumActiveWeakReferences() == 0
  57. || (masterReference.getNumActiveWeakReferences() == 1
  58. && this == &getDefaultLookAndFeel()));
  59. }
  60. //==============================================================================
  61. Colour LookAndFeel::findColour (int colourID) const noexcept
  62. {
  63. const ColourSetting c = { colourID, Colour() };
  64. auto index = colours.indexOf (c);
  65. if (index >= 0)
  66. return colours.getReference (index).colour;
  67. jassertfalse;
  68. return Colours::black;
  69. }
  70. void LookAndFeel::setColour (int colourID, Colour newColour) noexcept
  71. {
  72. const ColourSetting c = { colourID, newColour };
  73. auto index = colours.indexOf (c);
  74. if (index >= 0)
  75. colours.getReference (index).colour = newColour;
  76. else
  77. colours.add (c);
  78. }
  79. bool LookAndFeel::isColourSpecified (const int colourID) const noexcept
  80. {
  81. const ColourSetting c = { colourID, Colour() };
  82. return colours.contains (c);
  83. }
  84. //==============================================================================
  85. LookAndFeel& LookAndFeel::getDefaultLookAndFeel() noexcept
  86. {
  87. return Desktop::getInstance().getDefaultLookAndFeel();
  88. }
  89. void LookAndFeel::setDefaultLookAndFeel (LookAndFeel* newDefaultLookAndFeel) noexcept
  90. {
  91. Desktop::getInstance().setDefaultLookAndFeel (newDefaultLookAndFeel);
  92. }
  93. //==============================================================================
  94. Typeface::Ptr LookAndFeel::getTypefaceForFont (const Font& font)
  95. {
  96. if (font.getTypefaceName() == Font::getDefaultSansSerifFontName())
  97. {
  98. if (defaultTypeface != nullptr)
  99. return defaultTypeface;
  100. if (defaultSans.isNotEmpty())
  101. {
  102. Font f (font);
  103. f.setTypefaceName (defaultSans);
  104. return Typeface::createSystemTypefaceFor (f);
  105. }
  106. }
  107. return Font::getDefaultTypefaceForFont (font);
  108. }
  109. void LookAndFeel::setDefaultSansSerifTypeface (Typeface::Ptr newDefaultTypeface)
  110. {
  111. if (defaultTypeface != newDefaultTypeface)
  112. {
  113. defaultTypeface = newDefaultTypeface;
  114. Typeface::clearTypefaceCache();
  115. }
  116. }
  117. void LookAndFeel::setDefaultSansSerifTypefaceName (const String& newName)
  118. {
  119. if (defaultSans != newName)
  120. {
  121. defaultTypeface.reset();
  122. Typeface::clearTypefaceCache();
  123. defaultSans = newName;
  124. }
  125. }
  126. //==============================================================================
  127. MouseCursor LookAndFeel::getMouseCursorFor (Component& component)
  128. {
  129. auto cursor = component.getMouseCursor();
  130. for (auto* parent = component.getParentComponent();
  131. parent != nullptr && cursor == MouseCursor::ParentCursor;
  132. parent = parent->getParentComponent())
  133. {
  134. cursor = parent->getMouseCursor();
  135. }
  136. return cursor;
  137. }
  138. LowLevelGraphicsContext* LookAndFeel::createGraphicsContext (const Image& imageToRenderOn, const Point<int>& origin,
  139. const RectangleList<int>& initialClip)
  140. {
  141. return new LowLevelGraphicsSoftwareRenderer (imageToRenderOn, origin, initialClip);
  142. }
  143. //==============================================================================
  144. void LookAndFeel::setUsingNativeAlertWindows (bool shouldUseNativeAlerts)
  145. {
  146. useNativeAlertWindows = shouldUseNativeAlerts;
  147. }
  148. bool LookAndFeel::isUsingNativeAlertWindows()
  149. {
  150. #if JUCE_LINUX
  151. return false; // not available currently..
  152. #else
  153. return useNativeAlertWindows;
  154. #endif
  155. }
  156. } // namespace juce