-
Notifications
You must be signed in to change notification settings - Fork 1
/
IGResizableComboBox.m
363 lines (299 loc) · 10.6 KB
/
IGResizableComboBox.m
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*********************************************************************************
© Copyright 2010-2011, Isaac Greenspan
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*********************************************************************************/
//
// IGResizableComboBox.m
//
#import "IGResizableComboBox.h"
#define RESIZE_HANDLE_HEIGHT 7.0
#define RESIZE_HANDLE_IMAGE_HEIGHT 1.0
#define RESIZE_HANDLE_IMAGE_WIDTH 30.0
#pragma mark -
#pragma mark Private helper class's interface
// this interface-only section avoids the "may not respond to selector" warning
// in IGResizableComboBoxPopUpContentView's forwardingTargetForSelector:
@interface NSView (forwardingTargetForSelector)
- (id) forwardingTargetForSelector:(SEL)selector;
@end
@interface IGResizableComboBoxPopUpContentView : NSView {
IGResizableComboBox *theComboBox;
NSScrollView *theScrollView;
CGFloat draggingBasisY;
BOOL draggingNow;
}
@property(retain) IGResizableComboBox *theComboBox;
@property(retain) NSScrollView *theScrollView;
//<#methods#>
@end
@interface IGResizableComboBoxPopUpHandleImageView : NSImageView {
}
//<#methods#>
@end
#pragma mark -
#pragma mark main class implementation
@implementation IGResizableComboBox
@synthesize isPopUpOpen;
- (id)initWithFrame:(NSRect)frame {
// NSLog(@"initWithFrame:");
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
[self awakeFromNib];
[self setNumberOfVisibleItemsAutosaveName:nil];
isHandleDrawn = NO;
}
return self;
}
- (void)awakeFromNib
{
// NSLog(@"awakeFromNib");
[self setIsPopUpOpen:NO];
// NSLog(@"setting notifications...");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willPopUp:)
name:NSComboBoxWillPopUpNotification
object:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willDismiss:)
name:NSComboBoxWillDismissNotification
object:self];
// NSLog(@"done.");
}
- (NSWindow *)comboBoxPopUpWindow
{
NSWindow *child = nil;
if ([self isPopUpOpen]) {
for (child in [[self window] childWindows]) {
if ([[child className] isEqualToString:@"NSComboBoxWindow"]) {
break;
}
}
}
return child;
}
- (BOOL)isPopUpBelow
{
if ([self isPopUpOpen]) {
NSRect popUpFrame = [[self comboBoxPopUpWindow] frame];
CGFloat popUpTopInScreenY = popUpFrame.origin.y + popUpFrame.size.height;
NSPoint selfOriginInSelf = [self bounds].origin;
NSPoint selfOriginInWindow = [self convertPoint:selfOriginInSelf
toView:nil];
CGFloat selfOriginInScreenY = [[self window] convertBaseToScreen:selfOriginInWindow].y;
return (popUpTopInScreenY < selfOriginInScreenY);
} else {
return NO;
}
}
- (BOOL)isPopUpAbove
{
return [self isPopUpOpen] && ![self isPopUpBelow];
}
- (void)drawHandle {
// NSUInteger numberOfDisplayedLines = MIN([self numberOfVisibleItems],MAX(1,[self numberOfItems]));
NSWindow *child = [self comboBoxPopUpWindow];
if (child) {
NSRect windowFrame = [child frame];
NSScrollView *scrollView = [child contentView];
NSRect scrollViewFrame = [scrollView frame];
NSRect handleImageViewFrame;
windowFrame.size.height += RESIZE_HANDLE_HEIGHT;
if ([self isPopUpAbove]) {
// the pop-up is above
handleImageViewFrame = NSMakeRect(0.0, windowFrame.size.height - RESIZE_HANDLE_HEIGHT, // TODO: fix
windowFrame.size.width, RESIZE_HANDLE_HEIGHT);
} else {
// the pop-up is not above
windowFrame.origin.y -= RESIZE_HANDLE_HEIGHT;
scrollViewFrame.origin.y += RESIZE_HANDLE_HEIGHT;
handleImageViewFrame = NSMakeRect(0.0, 0.0,
windowFrame.size.width, RESIZE_HANDLE_HEIGHT);
}
[child setFrame:windowFrame display:YES];
innerView = [[IGResizableComboBoxPopUpContentView alloc] initWithFrame:scrollViewFrame];
[innerView setTheComboBox:self];
[innerView setTheScrollView:scrollView];
[child setContentView:innerView];
[innerView addSubview:scrollView];
[scrollView setFrame:scrollViewFrame];
IGResizableComboBoxPopUpHandleImageView *handleImageView;
handleImageView = [[[IGResizableComboBoxPopUpHandleImageView alloc]
initWithFrame:handleImageViewFrame]
autorelease];
NSImage *image = [[[NSImage alloc]
initWithSize:NSMakeSize(windowFrame.size.width, RESIZE_HANDLE_HEIGHT)]
autorelease];
[image lockFocus];
[[NSColor controlShadowColor] set];
[NSBezierPath fillRect:NSMakeRect((windowFrame.size.width - RESIZE_HANDLE_IMAGE_WIDTH)/2,
(RESIZE_HANDLE_HEIGHT - RESIZE_HANDLE_IMAGE_HEIGHT)/2,
RESIZE_HANDLE_IMAGE_WIDTH,
RESIZE_HANDLE_IMAGE_HEIGHT)];
[[NSColor headerColor] set];
[NSBezierPath strokeLineFromPoint:NSMakePoint(0.0, 0.0)
toPoint:NSMakePoint(windowFrame.size.width, 0.0)];
[NSBezierPath strokeLineFromPoint:NSMakePoint(0.0, RESIZE_HANDLE_HEIGHT)
toPoint:NSMakePoint(windowFrame.size.width, RESIZE_HANDLE_HEIGHT)];
[image unlockFocus];
[handleImageView setImage:image];
[innerView addSubview:handleImageView];
}
isHandleDrawn = YES;
}
- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
[super drawRect:dirtyRect];
if ([self isPopUpOpen] && !isHandleDrawn) {
[self drawHandle];
} else {
}
}
- (void)setNumberOfVisibleItems:(NSInteger)visibleItems
{
[super setNumberOfVisibleItems:visibleItems];
if (numberOfVisibleItemsAutosaveName) {
[[NSUserDefaults standardUserDefaults] setInteger:visibleItems
forKey:numberOfVisibleItemsAutosaveName];
}
}
- (void)setNumberOfVisibleItemsAutosaveName:(NSString *)name
{
// NSLog(@"changing combobox length autosave name from '%@' to '%@'",numberOfVisibleItemsAutosaveName,name);
[numberOfVisibleItemsAutosaveName release];
numberOfVisibleItemsAutosaveName = [name copy];
[self setNumberOfVisibleItems:[[NSUserDefaults standardUserDefaults]
integerForKey:numberOfVisibleItemsAutosaveName]];
}
- (void)willPopUp:(NSNotification *)notification
{
// NSLog(@"willPopUp:");
[self setIsPopUpOpen:YES];
}
- (void)willDismiss:(NSNotification *)notification
{
NSScrollView *scrollView = [innerView theScrollView];
[[innerView window] setContentView:scrollView];
// NSLog(@"willDismiss:");
[self setIsPopUpOpen:NO];
isHandleDrawn = NO;
}
@end
#pragma mark -
#pragma mark Private helper class's implementation
@implementation IGResizableComboBoxPopUpContentView
@synthesize theComboBox;
@synthesize theScrollView;
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
draggingBasisY = 0.0;
draggingNow = NO;
[self setTheComboBox:nil];
[self setTheScrollView:nil];
}
return self;
}
// to allow catching of calls that should go to our inner view
- (id)forwardingTargetForSelector:(SEL)aSelector
{
if (aSelector == @selector(verticalScroller)) {
return theScrollView;
}
if ([super respondsToSelector:@selector(forwardingTargetForSelector:)]) {
return [super forwardingTargetForSelector:aSelector];
} else {
[self doesNotRecognizeSelector:aSelector];
return nil;
}
}
- (void)mouseDown:(NSEvent *)theEvent
{
draggingBasisY = [NSEvent mouseLocation].y;
draggingNow = YES;
[[NSCursor resizeUpDownCursor] push];
}
- (void)mouseUp:(NSEvent *)theEvent
{
draggingBasisY = 0.0;
draggingNow = NO;
[NSCursor pop];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
CGFloat newY = [NSEvent mouseLocation].y;
CGFloat realItemHeight = [theComboBox itemHeight] + [theComboBox intercellSpacing].height;
if (fabs(draggingBasisY - newY) > realItemHeight/2) {
// if we're more than half-way to a change of one item height, then we actually resize
BOOL isAbove = [theComboBox isPopUpAbove];
CGFloat delta_y;
if (isAbove) {
// the pop-up is above; don't let the y position move below a minimum of height 1 line
delta_y = realItemHeight * MIN([theComboBox numberOfVisibleItems] - 1,
round((draggingBasisY - newY) / realItemHeight));
} else {
// the pop-up is not above; don't let the y position move above a minimum of height 1 line
delta_y = realItemHeight * MAX(1 - [theComboBox numberOfVisibleItems],
round((draggingBasisY - newY) / realItemHeight));
}
draggingBasisY -= delta_y;
NSWindow *popup = [self window];
NSRect windowFrame = [popup frame];
CGFloat previousHeight = windowFrame.size.height;
if (isAbove) {
// the pop-up is above
windowFrame.size.height = MAX(windowFrame.size.height - delta_y,RESIZE_HANDLE_HEIGHT + realItemHeight);
IGResizableComboBoxPopUpHandleImageView *handleImageView = nil;
for (NSView *aView in [self subviews]) {
if ([aView isKindOfClass:[IGResizableComboBoxPopUpHandleImageView class]]) {
handleImageView = (IGResizableComboBoxPopUpHandleImageView *)aView;
}
}
if (handleImageView) {
NSRect handleImageViewFrame = [handleImageView frame];
handleImageViewFrame.origin.y += windowFrame.size.height - previousHeight;
[handleImageView setFrame:handleImageViewFrame];
}
} else {
// the pop-up is not above
windowFrame.size.height = MAX(windowFrame.size.height + delta_y,RESIZE_HANDLE_HEIGHT + realItemHeight);
windowFrame.origin.y -= windowFrame.size.height - previousHeight;
}
[popup setFrame:windowFrame display:YES];
NSInteger newNumberOfVisibleItems = round((windowFrame.size.height - RESIZE_HANDLE_HEIGHT)/realItemHeight);
[theComboBox setNumberOfVisibleItems:newNumberOfVisibleItems];
}
}
@end
@implementation IGResizableComboBoxPopUpHandleImageView
- (void)mouseDown:(NSEvent *)theEvent
{
[[self superview] mouseDown:theEvent];
}
- (void)mouseUp:(NSEvent *)theEvent
{
[[self superview] mouseUp:theEvent];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
[[self superview] mouseDragged:theEvent];
}
@end