-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqcocoabutton.mm
198 lines (149 loc) · 4.65 KB
/
qcocoabutton.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "qcocoabutton.h"
#include "qcocoabutton_p.h"
#import <AppKit/NSButton.h>
#import <AppKit/NSButtonCell.h>
#import <AppKit/NSFont.h>
#import <AppKit/NSMenu.h>
#import <AppKit/NSApplication.h>
QCocoaButton::QCocoaButton(QWidget *parent)
: QCocoaWidget(parent)
{
pimpl.reset(new CocoaButtonPrivate(this));
setupLayout(pimpl->getNSButton());
// calling a virtual function updateSize from a constructor is a bad idea, it's now done in the showEvent method
}
QCocoaButton::QCocoaButton(QWidget *parent, CocoaButtonPrivate *customPrivate)
: QCocoaWidget(parent)
{
pimpl.reset(customPrivate);
setupLayout(pimpl->getNSButton());
}
void QCocoaButton::showEvent(QShowEvent *event)
{
Q_UNUSED(event);
setBezelStyle(_bezelStyle); // init default bezel style, will call updateSize
}
QCocoaButton::~QCocoaButton()
{
}
void QCocoaButton::setBezelStyle(BezelStyle bezelStyle)
{
_bezelStyle = bezelStyle;
switch(bezelStyle) {
case QCocoaButton::Disclosure:
case QCocoaButton::Circular:
case QCocoaButton::Inline:
case QCocoaButton::RoundedDisclosure:
case QCocoaButton::HelpButton:
[pimpl->getNSButton() setTitle:@""];
default:
break;
}
NSFont* font = 0;
switch(bezelStyle) {
case QCocoaButton::RoundRect:
font = [NSFont fontWithName:@"Lucida Grande" size:12];
break;
case QCocoaButton::Recessed:
font = [NSFont fontWithName:@"Lucida Grande Bold" size:12];
break;
case QCocoaButton::Inline:
font = [NSFont boldSystemFontOfSize:[NSFont systemFontSizeForControlSize:NSControlSizeSmall]];
break;
default:
font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSControlSizeRegular]];
break;
}
[pimpl->getNSButton() setFont:font];
switch(bezelStyle) {
case QCocoaButton::Recessed:
[pimpl->getNSButton() setButtonType:NSPushOnPushOffButton];
case QCocoaButton::Disclosure:
[pimpl->getNSButton() setButtonType:NSOnOffButton];
default:
[pimpl->getNSButton() setButtonType:NSMomentaryPushInButton];
}
[pimpl->getNSButton() setBezelStyle:(NSBezelStyle)bezelStyle];
pimpl->updateSize();
}
void QCocoaButton::setText(const QString &text)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
if (pimpl->isSpecialButton())
return;
QMacAutoReleasePool pool;
[pimpl->getNSButton() setTitle: text.toNSString()];
//[pimpl->nsButton setAlternateTitle:fromQString(text)]; // Set it for accessibility reasons as alternative title
pimpl->updateSize();
}
void QCocoaButton::setIcon(const QIcon &image)
{
QMacAutoReleasePool pool;
if (pimpl->isSpecialButton())
return;
NSImage *img = QCocoaIcon::imageFromQIcon(image);
if (img)
pimpl->setImage(img);
pimpl->updateSize();
}
void QCocoaButton::setIcon(QCocoaIcon::StandardIcon icon)
{
QMacAutoReleasePool pool;
NSImage *img = QCocoaIcon::standardIcon(icon);
if (img)
pimpl->setImage(img);
pimpl->updateSize();
}
void QCocoaButton::setIconPosition(IconPosition pos)
{
NSCellImagePosition nsPos = NSNoImage;
if (pos == IconOnly)
nsPos = NSImageOnly;
else if (pos == IconLeft)
nsPos = NSImageLeft;
else if (pos == IconRight)
nsPos = NSImageRight;
else if (pos == IconBelow)
nsPos = NSImageBelow;
else if (pos == IconAbove)
nsPos = NSImageAbove;
else if (pos == IconOverlaps)
nsPos = NSImageOverlaps;
[pimpl->getNSButton() setImagePosition:nsPos];
pimpl->updateSize();
}
void QCocoaButton::setChecked(bool checked)
{
[pimpl->getNSButton() setState:checked];
}
void QCocoaButton::setCheckable(bool checkable)
{
const NSInteger cellMask = checkable ? NSChangeBackgroundCellMask : NSNoCellMask;
[[pimpl->getNSButton() cell] setShowsStateBy:cellMask];
}
void QCocoaButton::setToolTip(const QString& strTip)
{
QMacAutoReleasePool pool;
[pimpl->getNSButton() setToolTip: strTip.toNSString()];
}
void QCocoaButton::setMenu(QMenu *menu)
{
NSMenu *m = menu->toNSMenu();
NSEvent *event = [[NSApplication sharedApplication] currentEvent];
[NSMenu popUpContextMenu:m withEvent:event forView:(NSButton *)pimpl->getNSButton()];
}
bool QCocoaButton::isChecked()
{
return [pimpl->getNSButton() state];
}
QSize QCocoaButton::sizeHint() const
{
NSRect frame = [pimpl->getNSButton() frame];
return QSize(frame.size.width, frame.size.height);
}
QAbstractButton *QCocoaButton::abstractButton()
{
return reinterpret_cast<QAbstractButton *>(this);
}