Qt 4.8
qt_mac.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <private/qt_mac_p.h>
43 #include <private/qpixmap_mac_p.h>
44 #include <private/qnativeimage_p.h>
45 #include <qdebug.h>
46 
48 #ifdef QT_MAC_USE_COCOA
49 static CTFontRef CopyCTThemeFont(ThemeFontID themeID)
50 {
51  CTFontUIFontType ctID = HIThemeGetUIFontType(themeID);
52  return CTFontCreateUIFontForLanguage(ctID, 0, 0);
53 }
54 #endif
55 
56 QFont qfontForThemeFont(ThemeFontID themeID)
57 {
58 #ifndef QT_MAC_USE_COCOA
59  static const ScriptCode Script = smRoman;
60  Str255 f_name;
61  SInt16 f_size;
62  Style f_style;
63  GetThemeFont(themeID, Script, f_name, &f_size, &f_style);
64  return QFont(qt_mac_from_pascal_string(f_name), f_size,
65  (f_style & ::bold) ? QFont::Bold : QFont::Normal,
66  (bool)(f_style & ::italic));
67 #else
68  QCFType<CTFontRef> ctfont = CopyCTThemeFont(themeID);
69  QString familyName = QCFString(CTFontCopyFamilyName(ctfont));
70  QCFType<CFDictionaryRef> dict = CTFontCopyTraits(ctfont);
71  CFNumberRef num = static_cast<CFNumberRef>(CFDictionaryGetValue(dict, kCTFontWeightTrait));
72  float fW;
73  CFNumberGetValue(num, kCFNumberFloat32Type, &fW);
74  QFont::Weight wght = fW > 0. ? QFont::Bold : QFont::Normal;
75  num = static_cast<CFNumberRef>(CFDictionaryGetValue(dict, kCTFontSlantTrait));
76  CFNumberGetValue(num, kCFNumberFloatType, &fW);
77  bool italic = (fW != 0.0);
78  return QFont(familyName, CTFontGetSize(ctfont), wght, italic);
79 #endif
80 }
81 
82 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
83 static QColor qcolorFromCGColor(CGColorRef cgcolor)
84 {
85  QColor pc;
86  CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(cgcolor));
87  const CGFloat *components = CGColorGetComponents(cgcolor);
88  if (model == kCGColorSpaceModelRGB) {
89  pc.setRgbF(components[0], components[1], components[2], components[3]);
90  } else if (model == kCGColorSpaceModelCMYK) {
91  pc.setCmykF(components[0], components[1], components[2], components[3]);
92  } else if (model == kCGColorSpaceModelMonochrome) {
93  pc.setRgbF(components[0], components[0], components[0], components[1]);
94  } else {
95  // Colorspace we can't deal with.
96  qWarning("Qt: qcolorFromCGColor: cannot convert from colorspace model: %d", model);
97  Q_ASSERT(false);
98  }
99  return pc;
100 }
101 
102 static inline QColor leopardBrush(ThemeBrush brush)
103 {
104  QCFType<CGColorRef> cgClr = 0;
105  HIThemeBrushCreateCGColor(brush, &cgClr);
106  return qcolorFromCGColor(cgClr);
107 }
108 #endif
109 
110 QColor qcolorForTheme(ThemeBrush brush)
111 {
112 #ifndef QT_MAC_USE_COCOA
113 # if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
115  return leopardBrush(brush);
116  } else
117 # endif
118  {
119  RGBColor rgbcolor;
120  GetThemeBrushAsColor(brush, 32, true, &rgbcolor);
121  return QColor(rgbcolor.red / 256, rgbcolor.green / 256, rgbcolor.blue / 256);
122  }
123 #else
124  return leopardBrush(brush);
125 #endif
126 }
127 
128 QColor qcolorForThemeTextColor(ThemeTextColor themeColor)
129 {
130 #ifdef Q_OS_MAC32
131  RGBColor c;
132  GetThemeTextColor(themeColor, 32, true, &c);
133  QColor color = QColor(c.red / 256, c.green / 256, c.blue / 256);
134  return color;
135 #else
136  // There is no equivalent to GetThemeTextColor in 64-bit and it was rather bad that
137  // I didn't file a request to implement this for Snow Leopard. So, in the meantime
138  // I've encoded the values from the GetThemeTextColor. This is not exactly ideal
139  // as if someone really wants to mess with themeing, these colors will be wrong.
140  // It also means that we need to make sure the values for differences between
141  // OS releases (and it will be likely that we are a step behind.)
142  switch (themeColor) {
143  case kThemeTextColorAlertActive:
144  case kThemeTextColorTabFrontActive:
145  case kThemeTextColorBevelButtonActive:
146  case kThemeTextColorListView:
147  case kThemeTextColorPlacardActive:
148  case kThemeTextColorPopupButtonActive:
149  case kThemeTextColorPopupLabelActive:
150  case kThemeTextColorPushButtonActive:
151  return Qt::black;
152  case kThemeTextColorAlertInactive:
153  case kThemeTextColorDialogInactive:
154  case kThemeTextColorPlacardInactive:
155  return QColor(69, 69, 69, 255);
156  case kThemeTextColorPopupButtonInactive:
157  case kThemeTextColorPopupLabelInactive:
158  case kThemeTextColorPushButtonInactive:
159  case kThemeTextColorTabFrontInactive:
160  case kThemeTextColorBevelButtonInactive:
161  return QColor(127, 127, 127, 255);
162  default: {
163  QNativeImage nativeImage(16,16, QNativeImage::systemFormat());
164  CGRect cgrect = CGRectMake(0, 0, 16, 16);
165  HIThemeSetTextFill(themeColor, 0, nativeImage.cg, kHIThemeOrientationNormal);
166  CGContextFillRect(nativeImage.cg, cgrect);
167  QColor color = nativeImage.image.pixel(0,0);
168  return QColor(nativeImage.image.pixel(0 , 0));
169  }
170  }
171 #endif
172 }
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setRgbF(qreal r, qreal g, qreal b, qreal a=1.0)
Sets the color channels of this color to r (red), g (green), b (blue) and a (alpha, transparency).
Definition: qcolor.cpp:954
Weight
Qt uses a weighting scale from 0 to 99 similar to, but not the same as, the scales used in Windows or...
Definition: qfont.h:103
static QImage::Format systemFormat()
static QColor qcolorFromCGColor(CGColorRef cgcolor)
Definition: qt_mac.cpp:83
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QRgb pixel(int x, int y) const
Returns the color of the pixel at coordinates (x, y).
Definition: qimage.cpp:4240
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a=1.0)
Sets the color to CMYK values, c (cyan), m (magenta), y (yellow), k (black), and a (alpha-channel...
Definition: qcolor.cpp:2276
Q_CORE_EXPORT void qWarning(const char *,...)
QFont qfontForThemeFont(ThemeFontID themeID)
Definition: qt_mac.cpp:56
struct CGRect CGRect
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
QColor qcolorForThemeTextColor(ThemeTextColor themeColor)
Definition: qt_mac.cpp:128
QString qt_mac_from_pascal_string(const Str255 pstr)
Definition: qcore_mac.cpp:120
static QColor leopardBrush(ThemeBrush brush)
Definition: qt_mac.cpp:102
QColor qcolorForTheme(ThemeBrush brush)
Definition: qt_mac.cpp:110
static const MacVersion MacintoshVersion
the version of the Macintosh operating system on which the application is run (Mac only)...
Definition: qglobal.h:1646
float CGFloat