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.2KB

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