-
Notifications
You must be signed in to change notification settings - Fork 2
/
qcocoasegmentedbutton.mm
282 lines (208 loc) · 6.14 KB
/
qcocoasegmentedbutton.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "qcocoasegmentedbutton.h"
#include <QIcon>
#include <QMenu>
#import <AppKit/NSSegmentedControl.h>
#import <AppKit/NSSegmentedCell.h>
#import <AppKit/NSFont.h>
// QCocoaSegmentedButtonTarget
@interface QCocoaSegmentedButtonTarget: NSObject
{
QCocoaSegmentedButton *mRealTarget;
}
-(id)initWithObject1:(QCocoaSegmentedButton*)object;
-(IBAction)segControlClicked:(id)sender;
@end
@implementation QCocoaSegmentedButtonTarget
-(id)initWithObject1:(QCocoaSegmentedButton*)object
{
self = [super init];
mRealTarget = object;
return self;
}
-(IBAction)segControlClicked:(id)sender
{
mRealTarget->onClicked([sender selectedSegment]);
}
@end
// QCocoaSegmentedButtonPrivate
class QCocoaSegmentedButtonPrivate : public QObject
{
protected:
QPointer<QCocoaSegmentedButton> parent;
NSSegmentedControl *control;
public:
QCocoaSegmentedButtonPrivate(NSSegmentedControl *pControl, QCocoaSegmentedButton *pParent)
: QObject(pParent), parent(pParent), control(pControl)
{
}
~QCocoaSegmentedButtonPrivate()
{
[control release];
}
void updateSize()
{
[control sizeToFit];
NSRect frame = [control frame];
[control setFrame:frame];
parent->setFixedSize(NSWidth(frame), NSHeight(frame));
}
friend class QCocoaSegmentedButton;
};
// see: http://stackoverflow.com/questions/1203698/show-nssegmentedcontrol-menu-when-segment-clicked-despite-having-set-action
@interface SegmentedCellWithoutMenuDelay : NSSegmentedCell
{
}
- (SEL)action;
@end
@implementation SegmentedCellWithoutMenuDelay
- (SEL)action
{
//this allows connected menu to popup instantly (because no action is returned for menu button)
NSMenu *m = [self menuForSegment: [self selectedSegment]];
return (m ? nil : [super action]);
}
@end
QCocoaSegmentedButton::QCocoaSegmentedButton(QWidget *pParent /*= 0*/)
: QCocoaWidget(pParent)
{
QMacAutoReleasePool pool;
NSSegmentedControl *segControl = [[NSSegmentedControl alloc] init];
pimpl = new QCocoaSegmentedButtonPrivate(segControl, this);
[segControl setFont: [NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize: NSControlSizeSmall]]];
[segControl sizeToFit];
setCustomCellWithoutMenuDelay();
QCocoaSegmentedButtonTarget *bt = [[QCocoaSegmentedButtonTarget alloc] initWithObject1:this];
[segControl setTarget:bt];
[segControl setAction:@selector(segControlClicked:)];
setupLayout(segControl);
NSRect initFrame = [segControl frame];
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setFixedSize(NSWidth(initFrame), NSHeight(initFrame));
}
void QCocoaSegmentedButton::setCustomCellWithoutMenuDelay()
{
NSFont *font = [pimpl->control font]; // remember font
[pimpl->control setCell: [[SegmentedCellWithoutMenuDelay alloc] init]];
[pimpl->control setFont:font]; // restore
}
QCocoaSegmentedButton::~QCocoaSegmentedButton()
{
}
QSize QCocoaSegmentedButton::sizeHint() const
{
Q_ASSERT(pimpl);
if (!pimpl)
return QSize();
NSRect frame = [pimpl->control frame];
return QSize(frame.size.width, frame.size.height);
}
void QCocoaSegmentedButton::setSegmentCount(int count)
{
[pimpl->control setSegmentCount:count];
}
void QCocoaSegmentedButton::setSegmentStyle(SegmentStyle style)
{
[pimpl->control setSegmentStyle:(NSSegmentStyle)style];
}
void QCocoaSegmentedButton::setTitle(int iSegment, const QString &strTitle)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
QMacAutoReleasePool pool;
QString s(strTitle);
[pimpl->control setLabel: s.remove('&').toNSString() forSegment: iSegment];
pimpl->updateSize();
}
void QCocoaSegmentedButton::setToolTip(int iSegment, const QString &strTip)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
QMacAutoReleasePool pool;
[[pimpl->control cell] setToolTip: strTip.toNSString() forSegment: iSegment];
}
void QCocoaSegmentedButton::setSegmentIcon(int iSegment, const QIcon& icon)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
NSImage *nsImage = QCocoaIcon::imageFromQIcon(icon);
if (nsImage)
{
[pimpl->control setImage: nsImage forSegment: iSegment];
[nsImage release];
}
pimpl->updateSize();
}
void QCocoaSegmentedButton::setSegmentIcon(int iSegment, QCocoaIcon::StandardIcon icon)
{
NSImage *nsImage = QCocoaIcon::standardIcon(icon);
if (nsImage)
[pimpl->control setImage: nsImage forSegment: iSegment];
pimpl->updateSize();
}
void QCocoaSegmentedButton::setSegmentMenu(int iSegment, QMenu *menu)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
if (menu && pimpl->control)
[pimpl->control setMenu:menu->toNSMenu() forSegment:iSegment];
}
void QCocoaSegmentedButton::setEnabled(int iSegment, bool fEnabled)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
[[pimpl->control cell] setEnabled: fEnabled forSegment: iSegment];
}
void QCocoaSegmentedButton::animateClick(int iSegment)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
[pimpl->control setSelectedSegment: iSegment];
[[pimpl->control cell] performClick: pimpl->control];
}
void QCocoaSegmentedButton::onClicked(int iSegment)
{
emit clicked(iSegment, false);
}
void QCocoaSegmentedButton::setChecked(int nIndex, bool bChecked)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
if (bChecked)
[pimpl->control setSelectedSegment:nIndex];
}
bool QCocoaSegmentedButton::isChecked(int nIndex) const
{
Q_ASSERT(pimpl);
if (!pimpl)
return false;
return [pimpl->control selectedSegment] == nIndex;
}
void QCocoaSegmentedButton::setFixedWidth(int nSegment, int nWidth)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
[pimpl->control setWidth:nWidth forSegment:nSegment];
}
int QCocoaSegmentedButton::segmentWidth(int nSegment) const
{
return [pimpl->control widthForSegment:nSegment];
}
int QCocoaSegmentedButton::segmentCount() const
{
return [pimpl->control segmentCount];
}
void QCocoaSegmentedButton::setTrackingMode(SegmentSwitchTracking mode)
{
Q_ASSERT(pimpl);
if (!pimpl)
return;
[[pimpl->control cell] setTrackingMode: (NSSegmentSwitchTracking)mode];
}