Audio plugin host https://kx.studio/carla
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.

juce_LookAndFeel.cpp 6.5KB

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