-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathNSButtonCell.m
executable file
·1178 lines (960 loc) · 36.5 KB
/
NSButtonCell.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd
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. */
#import <AppKit/NSButtonCell.h>
#import <AppKit/NSApplication.h>
#import <AppKit/NSGraphics.h>
#import <AppKit/NSAttributedString.h>
#import <AppKit/NSStringDrawing.h>
#import <AppKit/NSGraphicsContext.h>
#import <AppKit/NSFont.h>
#import <AppKit/NSColor.h>
#import <AppKit/NSImage.h>
#import <AppKit/NSParagraphStyle.h>
#import <AppKit/NSWindow.h>
#import <AppKit/NSGraphicsStyle.h>
#import <AppKit/NSStringDrawer.h>
#import <AppKit/NSControl.h>
#import <AppKit/NSMatrix.h>
#import <Foundation/NSKeyedArchiver.h>
#import <AppKit/NSButtonImageSource.h>
#import <AppKit/NSComboBoxCell.h>
#import <AppKit/NSPopUpButtonCell.h>
#import <AppKit/NSSound.h>
#import <AppKit/NSRaise.h>
@implementation NSButtonCell
// Margin between an image and the Button interior borders
static const float kImageMargin = 2.;
-(void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:_titleOrAttributedTitle forKey:@"NSButtonCell title"];
[coder encodeObject:_alternateTitle forKey:@"NSButtonCell alternateTitle"];
[coder encodeInt:_imagePosition forKey:@"NSButtonCell imagePosition"];
[coder encodeInt:_highlightsBy forKey:@"NSButtonCell highlightsBy"];
[coder encodeInt:_showsStateBy forKey:@"NSButtonCell showsStateBy"];
[coder encodeBool:_isTransparent forKey:@"NSButtonCell transparent"];
[coder encodeBool:_imageDimsWhenDisabled forKey:@"NSButtonCell imageDimsWhenDisabled"];
[coder encodeObject:_alternateImage forKey:@"NSButtonCell alternateImage"];
[coder encodeObject:_keyEquivalent forKey:@"NSButtonCell keyEquivalent"];
[coder encodeInt:_keyEquivalentModifierMask forKey:@"NSButtonCell keyEquivalentModifierMask"];
}
-initWithCoder:(NSCoder *)coder {
[super initWithCoder:coder];
if([coder allowsKeyedCoding]){
NSKeyedUnarchiver *keyed=(NSKeyedUnarchiver *)coder;
unsigned flags=[keyed decodeIntForKey:@"NSButtonFlags"];
unsigned flags2=[keyed decodeIntForKey:@"NSButtonFlags2"];
id check;
_titleOrAttributedTitle=[[keyed decodeObjectForKey:@"NSContents"] retain];
_alternateTitle=[[keyed decodeObjectForKey:@"NSAlternateContents"] retain];
_imagePosition=NSNoImage;
if((flags&0x00480000)==0x00400000)
_imagePosition=NSImageOnly;
else if((flags&0x00480000)==0x00480000)
_imagePosition=NSImageOverlaps;
else if((flags&0x00380000)==0x00380000)
_imagePosition=NSImageLeft;
else if((flags&0x00380000)==0x00280000)
_imagePosition=NSImageRight;
else if((flags&0x00380000)==0x00180000)
_imagePosition=NSImageBelow;
else if((flags&0x00380000)==0x00080000)
_imagePosition=NSImageAbove;
// bits 6 and 7 in flags2, but not in order
switch((flags2>>6)&0x3){
case 0:
_imageScaling=NSImageScaleNone;
break;
case 1:
_imageScaling=NSImageScaleProportionallyUpOrDown;
break;
case 2:
_imageScaling=NSImageScaleProportionallyDown;
break;
case 3:
_imageScaling=NSImageScaleAxesIndependently;
break;
}
_highlightsBy=0;
_showsStateBy=0;
if(flags&0x80000000)
_highlightsBy|=NSPushInCellMask;
if(flags&0x40000000)
_showsStateBy|=NSContentsCellMask;
if(flags&0x20000000)
_showsStateBy|=NSChangeBackgroundCellMask;
if(flags&0x10000000)
_showsStateBy|=NSChangeGrayCellMask;
if(flags&0x08000000)
_highlightsBy|=NSContentsCellMask;
if(flags&0x04000000)
_highlightsBy|=NSChangeBackgroundCellMask;
if(flags&0x02000000)
_highlightsBy|=NSChangeGrayCellMask;
_isBordered=(flags&0x00800000)?YES:NO; // err, this flag is in NSCell too
_bezelStyle=(flags2&0x7)|(flags2&0x20>>2);
if (_bezelStyle==0) // this is how textured buttons are encoded by IB
_bezelStyle=NSTexturedSquareBezelStyle;
if (_bezelStyle==3)
_bezelStyle=NSTexturedRoundedBezelStyle;
if (_bezelStyle==4)
_bezelStyle=NSRoundRectBezelStyle;
_isTransparent=(flags&0x00008000)?YES:NO;
_imageDimsWhenDisabled=(flags&0x00002000)?NO:YES;
_showsBorderOnlyWhileMouseInside=(flags2&0x8)?YES:NO;
check=[keyed decodeObjectForKey:@"NSAlternateImage"];
if([check isKindOfClass:[NSImage class]])
_alternateImage=[check retain];
else if([check isKindOfClass:[NSButtonImageSource class]]){
[_image release];
_image=[[check normalImage] retain];
_alternateImage=[[check alternateImage] retain];
}
else
_alternateImage=nil;
/* _normalImage is a private ivar in Apple's AppKit. Third party library BGHUDAppKit uses it to
figure out what kind of standard button is being drawn. We emulate it for BGHUDAppKit. */
_normalImage=[_image retain];
_keyEquivalent=[[keyed decodeObjectForKey:@"NSKeyEquivalent"] retain];
_keyEquivalentModifierMask=flags2>>8;
[self setIntValue:_state]; // make the int value of NSButtonCell to be
// in synch with the bare _state of NSCell
}
else {
[NSException raise:NSInvalidArgumentException format:@"%@ can not initWithCoder:%@",isa,[coder class]];
}
return self;
}
-initTextCell:(NSString *)string {
[super initTextCell:string];
_titleOrAttributedTitle=[string copy];
_alternateTitle=@"";
_imagePosition=NSNoImage;
_highlightsBy=NSPushInCellMask;
_showsStateBy=0;
_isTransparent=NO;
_imageDimsWhenDisabled=NO;
_alternateImage=nil;
_keyEquivalent=@"";
_keyEquivalentModifierMask=0;
_showsBorderOnlyWhileMouseInside=NO;
[self setBordered:YES];
[self setBezeled:YES];
[self setAlignment:NSCenterTextAlignment];
[self setObjectValue:[NSNumber numberWithBool:NO]];
return self;
}
-initImageCell:(NSImage *)image {
[super initImageCell:image];
_titleOrAttributedTitle=@""; // empty string, not nil
_imagePosition=NSImageOnly;
[self setObjectValue:[NSNumber numberWithBool:NO]];
return self;
}
-init {
return [self initTextCell:@"Button"];
}
-(void)dealloc {
[_titleOrAttributedTitle release];
[_normalImage release];
[_alternateTitle release];
[_alternateImage release];
[_keyEquivalent release];
[_sound release];
[_keyEquivalentFont release];
[_backgroundColor release];
[super dealloc];
}
-copyWithZone:(NSZone *)zone {
NSButtonCell *result=[super copyWithZone:zone];
result->_titleOrAttributedTitle=[_titleOrAttributedTitle copy];
result->_alternateTitle =[_alternateTitle copy];
result->_alternateImage=[_alternateImage retain];
result->_keyEquivalent=[_keyEquivalent copy];
result->_sound=[_sound retain];
result->_keyEquivalentFont=[_keyEquivalentFont retain];
result->_backgroundColor=[_backgroundColor retain];
return result;
}
-(BOOL)isTransparent {
return _isTransparent;
}
-(NSString *)keyEquivalent {
return _keyEquivalent;
}
-(NSCellImagePosition)imagePosition {
return _imagePosition;
}
-(NSString *)title {
if([_titleOrAttributedTitle isKindOfClass:[NSAttributedString class]])
return [_titleOrAttributedTitle string];
else
return _titleOrAttributedTitle;
}
-(NSString *)alternateTitle {
return _alternateTitle;
}
-(NSImage *)alternateImage {
return _alternateImage;
}
-(NSAttributedString *)attributedTitle {
if([_titleOrAttributedTitle isKindOfClass:[NSAttributedString class]])
return _titleOrAttributedTitle;
else {
NSMutableDictionary *attributes=[NSMutableDictionary dictionary];
NSMutableParagraphStyle *paraStyle=[[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
NSFont *font=[self font];
if(font!=nil)
[attributes setObject:font forKey:NSFontAttributeName];
[paraStyle setLineBreakMode:_lineBreakMode];
[paraStyle setAlignment:_textAlignment];
[attributes setObject:paraStyle forKey:NSParagraphStyleAttributeName];
if([self isEnabled])
[attributes setObject:[NSColor controlTextColor]
forKey:NSForegroundColorAttributeName];
else
[attributes setObject:[NSColor disabledControlTextColor]
forKey:NSForegroundColorAttributeName];
return [[[NSAttributedString alloc] initWithString:[self title] attributes:attributes] autorelease];
}
}
-(NSAttributedString *)attributedAlternateTitle {
NSMutableDictionary *attributes=[NSMutableDictionary dictionary];
NSMutableParagraphStyle *paraStyle=[[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
NSFont *font=[self font];
if(font!=nil)
[attributes setObject:font forKey:NSFontAttributeName];
[paraStyle setLineBreakMode:_lineBreakMode];
[paraStyle setAlignment:_textAlignment];
[attributes setObject:paraStyle forKey:NSParagraphStyleAttributeName];
if([self isEnabled])
[attributes setObject:[NSColor controlTextColor]
forKey:NSForegroundColorAttributeName];
else
[attributes setObject:[NSColor disabledControlTextColor]
forKey:NSForegroundColorAttributeName];
return [[[NSAttributedString alloc] initWithString:[self alternateTitle] attributes:attributes] autorelease];
}
-(int)highlightsBy {
return _highlightsBy;
}
-(int)showsStateBy {
return _showsStateBy;
}
-(BOOL)imageDimsWhenDisabled {
return _imageDimsWhenDisabled;
}
-(unsigned)keyEquivalentModifierMask {
return _keyEquivalentModifierMask;
}
-(NSBezelStyle)bezelStyle {
return _bezelStyle;
}
-(BOOL)showsBorderOnlyWhileMouseInside {
return _showsBorderOnlyWhileMouseInside;
}
-(NSSound *)sound {
return _sound;
}
-(NSGradientType)gradientType {
return _gradientType;
}
-(NSImageScaling)imageScaling {
return _imageScaling;
}
-(BOOL)isOpaque {
if(_bezelStyle==NSDisclosureBezelStyle)
return NO;
if(_bezelStyle==NSTexturedSquareBezelStyle)
return NO;
if(_bezelStyle==NSTexturedRoundedBezelStyle)
return NO;
if(_bezelStyle==NSShadowlessSquareBezelStyle)
return NO;
if(_bezelStyle==NSRecessedBezelStyle)
return NO;
return ![self isTransparent] && [self isBordered];
}
-(NSFont *)keyEquivalentFont {
return _keyEquivalentFont;
}
-(NSColor *)backgroundColor {
return _backgroundColor;
}
-(void)getPeriodicDelay:(float *)delay interval:(float *)interval {
*delay=_periodicDelay;
*interval=_periodicInterval;
}
-(int)state {
return [self intValue];
}
-(void)setTransparent:(BOOL)flag {
_isTransparent=flag;
}
-(void)setKeyEquivalent:(NSString *)keyEquivalent {
keyEquivalent=[keyEquivalent copy];
[_keyEquivalent release];
_keyEquivalent=keyEquivalent;
}
-(void)setImagePosition:(NSCellImagePosition)position {
_imagePosition=position;
}
-(void)setTitle:(NSString *)title {
title=[title copy];
[_titleOrAttributedTitle release];
_titleOrAttributedTitle=title;
}
-(void)setAlternateTitle:(NSString *)title {
title=[title copy];
[_alternateTitle release];
_alternateTitle=title;
}
-(void)setAlternateImage:(NSImage *)image {
image=[image retain];
[_alternateImage release];
_alternateImage=image;
}
-(void)setAttributedTitle:(NSAttributedString *)title {
title=[title copy];
[_titleOrAttributedTitle release];
_titleOrAttributedTitle=title;
}
-(void)setAttributedAlternateTitle:(NSAttributedString *)title {
NSUnimplementedMethod();
}
-(void)setHighlightsBy:(int)type {
_highlightsBy=type;
}
-(void)setShowsStateBy:(int)type {
_showsStateBy=type;
}
-(void)setImageDimsWhenDisabled:(BOOL)flag {
_imageDimsWhenDisabled=flag;
}
-(void)setKeyEquivalentModifierMask:(unsigned)mask {
_keyEquivalentModifierMask=mask;
}
-(void)setState:(int)value {
[self setIntValue:value];
}
-(void)setNextState {
[self setIntValue:[self nextState]];
}
-(void)setObjectValue:(id <NSCopying>)value {
if ([(id)value respondsToSelector:@selector(intValue)])
[super setState:[(NSNumber *)value intValue]];
else
[super setState:0];
[[self controlView] willChangeValueForKey:@"objectValue"];
[_objectValue release];
_objectValue = [[NSNumber numberWithInt:[super state]] retain];
[[self controlView] didChangeValueForKey:@"objectValue"];
if( [ [self controlView] respondsToSelector:@selector(updateCell:)] )
[(NSControl *)[self controlView] updateCell:self];
}
-(void)setBezelStyle:(NSBezelStyle)bezelStyle {
_bezelStyle = bezelStyle;
}
-(void)setButtonType:(NSButtonType)buttonType {
switch (buttonType)
{
case NSMomentaryLightButton:
_highlightsBy = NSChangeBackgroundCellMask;
_showsStateBy = NSNoCellMask;
_imageDimsWhenDisabled = YES;
break;
case NSMomentaryPushInButton:
_highlightsBy = NSPushInCellMask|NSChangeGrayCellMask;
_showsStateBy = NSNoCellMask;
_imageDimsWhenDisabled = YES;
break;
case NSMomentaryChangeButton:
_highlightsBy = NSContentsCellMask;
_showsStateBy = NSNoCellMask;
_imageDimsWhenDisabled = YES;
break;
case NSPushOnPushOffButton:
_highlightsBy = NSPushInCellMask|NSChangeGrayCellMask;
_showsStateBy = NSChangeBackgroundCellMask;
_imageDimsWhenDisabled = YES;
break;
case NSOnOffButton:
_highlightsBy = NSChangeBackgroundCellMask|NSChangeGrayCellMask;
_showsStateBy = NSChangeBackgroundCellMask|NSChangeGrayCellMask;
_imageDimsWhenDisabled = YES;
break;
case NSToggleButton:
_highlightsBy = NSPushInCellMask|NSContentsCellMask;
_showsStateBy = NSContentsCellMask;
_imageDimsWhenDisabled = YES;
break;
case NSSwitchButton:
_highlightsBy = NSContentsCellMask;
_showsStateBy = NSContentsCellMask;
_imagePosition = NSImageLeft;
_imageDimsWhenDisabled = NO;
[self setImage:[NSImage imageNamed:@"NSSwitch"]];
[self setAlternateImage:[NSImage imageNamed:@"NSHighlightedSwitch"]];
[self setAlignment:NSLeftTextAlignment];
[self setBordered:NO];
[self setBezeled:NO];
break;
case NSRadioButton:
_highlightsBy = NSContentsCellMask;
_showsStateBy = NSContentsCellMask;
_imagePosition = NSImageLeft;
_imageDimsWhenDisabled = NO;
[self setImage:[NSImage imageNamed:@"NSRadioButton"]];
[self setAlternateImage:[NSImage imageNamed:@"NSHighlightedRadioButton"]];
[self setAlignment:NSLeftTextAlignment];
[self setBordered:NO];
[self setBezeled:NO];
break;
}
[(NSControl *)[self controlView] updateCell:self];
}
-(void)setShowsBorderOnlyWhileMouseInside:(BOOL)show {
_showsBorderOnlyWhileMouseInside=show;
}
-(void)setSound:(NSSound *)sound {
sound=[sound retain];
[_sound release];
_sound=sound;
}
-(void)setGradientType:(NSGradientType)value {
_gradientType=value;
}
-(void)setBackgroundColor:(NSColor *)value {
value=[value copy];
[_backgroundColor release];
_backgroundColor=value;
}
-(void)setImageScaling:(NSImageScaling)value {
_imageScaling=value;
}
-(void)setKeyEquivalentFont:(NSFont *)value {
value=[value retain];
[_keyEquivalentFont release];
_keyEquivalentFont=value;
}
-(void)setKeyEquivalentFont:(NSString *)value size:(CGFloat)size {
NSFont *font=[NSFont fontWithName:value size:size];
[self setKeyEquivalentFont:font];
}
-(void)setPeriodicDelay:(float)delay interval:(float)interval {
_periodicDelay=delay;
_periodicInterval=interval;
}
-(NSAttributedString *)titleForHighlight {
if((([self highlightsBy]&NSContentsCellMask) && [self isHighlighted]) ||
(([self showsStateBy]&NSContentsCellMask) && [self state])){
NSAttributedString *result=[self attributedAlternateTitle];
if([result length]>0)
return result;
}
return [self attributedTitle];
}
-(NSImage *)imageForHighlight {
if(_bezelStyle==NSDisclosureBezelStyle){
if((([self highlightsBy]&NSContentsCellMask) && [self isHighlighted]))
return [NSImage imageNamed:@"NSButtonCell_disclosure_highlighted"];
else if([self state])
return [NSImage imageNamed:@"NSButtonCell_disclosure_selected"];
else
return [NSImage imageNamed:@"NSButtonCell_disclosure_normal"];
return nil;
}
else {
if((([self highlightsBy]&NSContentsCellMask) && [self isHighlighted]) ||
(([self showsStateBy]&NSContentsCellMask) && [self state]))
return [self alternateImage];
return [self image];
}
}
-(NSRect)imageRectForBounds:(NSRect)rect {
// Make sure we use the same image as will be drawn!
NSImage *image = [self imageForHighlight];
NSSize imageSize= NSMakeSize(0,0);
if (image != nil) {
BOOL enabled = [self isEnabled] ? YES : ![self imageDimsWhenDisabled];
BOOL mixed = [self state] == NSMixedState;
imageSize = [[[self controlView] graphicsStyle] sizeOfButtonImage: image
enabled: enabled
mixed: mixed];
}
return NSMakeRect(rect.origin.x, rect.origin.y, imageSize.width, imageSize.height);
}
-(BOOL)isVisuallyHighlighted {
return ((([self highlightsBy]&NSChangeGrayCellMask) && [self isHighlighted]) ||
(([self showsStateBy]&NSChangeGrayCellMask) && [self state]));
}
-(NSRect)getControlSizeAdjustment: (BOOL)flipped
{
/*
Aqua Push Buttons actually have a frame much larger than told by IB to make room for shadows and whatnot
So we have to compensate for this when drawing simpler buttons.
There is probably a way to streamline this, make NSPopUpButtonCell draw itself for starters
NSGraphicsStyle should probably do this adjustment too
*/
NSRect frame = { { 0, 0 }, { 0, 0 } };
if ([self isKindOfClass:[NSComboBoxCell class]])
{
switch (_controlSize)
{
case NSRegularControlSize:
frame.size.width = 2;
frame.size.height = 1;
frame.origin.x = 1;
break;
case NSSmallControlSize:
frame.size.width = 4;
frame.size.height = 8;
frame.origin.x = 2;
frame.origin.y = 6;
break;
case NSMiniControlSize:
frame.size.width = 6;
frame.size.height = 4;
frame.origin.x = 3;
frame.origin.y = 4;
break;
}
}
else if ([self isKindOfClass:[NSPopUpButtonCell class]])
{
switch (_controlSize)
{
case NSRegularControlSize:
frame.size.width = 2;
frame.size.height = 1;
frame.origin.x = 1;
break;
case NSSmallControlSize:
frame.size.width = 4;
frame.size.height = 3;
frame.origin.x = 2;
frame.origin.y = 3;
break;
case NSMiniControlSize:
// Mini controls don't need adjusting they're small enough already.
break;
}
}
else if((_bezelStyle==NSRoundedBezelStyle) && (_highlightsBy&NSPushInCellMask) && (_highlightsBy&NSChangeGrayCellMask) && (_showsStateBy==NSNoCellMask))
{
switch (_controlSize)
{
default:
frame.size.width = 10 - _controlSize*2;
frame.size.height = 10 - _controlSize*2;
frame.origin.x = 5 - _controlSize;
frame.origin.y = flipped ? _controlSize*2 - 3 : 7 - _controlSize*2;
break;
case NSMiniControlSize:
break;
}
}
else if(_bezelStyle==NSRegularSquareBezelStyle){
}
return frame;
}
- (void)_drawTexturedBezelWithFrame:(NSRect)frame
{
BOOL highlighted=[self isHighlighted];
BOOL pressed=[self state] && ([self showsStateBy] & NSChangeBackgroundCellMask);
BOOL renderDarkenBg=NO, renderOutlineShadow=NO;
//CGFloat topGray=0.76, bottomGray=0.98, strokeGray=0.4;
CGFloat topGray=0.98, bottomGray=0.76, strokeGray=0.4;
if (pressed) {
topGray=0.4;
bottomGray=0.30;
}
renderDarkenBg=highlighted;
renderOutlineShadow=highlighted || pressed;
CGContextRef ctx=[[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(ctx);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextSetFillColorSpace(ctx, colorSpace);
CGContextSetStrokeColorSpace(ctx, colorSpace);
CGColorSpaceRelease(colorSpace);
frame = NSInsetRect(frame, 1.5, 1.5);
const CGFloat rounding=1.5;
const CGFloat baseY = floor(frame.origin.y);
const CGFloat maxY = baseY + frame.size.height - 1.0;
CGRect r = CGRectMake(floor(frame.origin.x), baseY, ceil(frame.size.width), 1.0);
while (r.origin.y <= maxY) {
CGFloat g = bottomGray + (topGray - bottomGray) * ((r.origin.y - baseY) / (maxY - baseY));
CGFloat components[4] = { g, g, g, 1.0 };
CGContextSetFillColor(ctx, components);
if (r.origin.y < baseY+1.0f || r.origin.y > maxY-1.0f) {
CGContextFillRect(ctx, CGRectMake(r.origin.x+1.0f, r.origin.y, r.size.width-2.0f, r.size.height));
} else {
CGContextFillRect(ctx, r);
}
r.origin.y += 1.0f;
}
const CGFloat lx = floor(frame.origin.x) + 0.5f;
const CGFloat rx = floor(frame.origin.x + frame.size.width) + 0.5f;
const CGFloat ty = floor(frame.origin.y) + 0.5f;
const CGFloat by = floor(frame.origin.y + frame.size.height) + 0.5f;
const CGFloat rlx = lx + rounding;
const CGFloat rrx = rx - rounding;
const CGFloat rty = ty + rounding;
const CGFloat rby = by - rounding;
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, rlx, ty);
CGContextAddLineToPoint(ctx, rrx, ty);
CGContextAddLineToPoint(ctx, rx, rty);
CGContextAddLineToPoint(ctx, rx, rby);
CGContextAddLineToPoint(ctx, rrx, by);
CGContextAddLineToPoint(ctx, rlx, by);
CGContextAddLineToPoint(ctx, lx, rby);
CGContextAddLineToPoint(ctx, lx, rty);
CGContextClosePath(ctx);
CGFloat components[4] = { strokeGray, strokeGray, strokeGray, 1.0 };
CGContextSetStrokeColor(ctx, components);
CGContextSetLineWidth(ctx, 1.0);
if (renderDarkenBg) {
components[0] = components[1] = components[2] = 0.0;
components[3] = 0.15;
CGContextSetFillColor(ctx, components);
CGContextDrawPath(ctx, kCGPathFillStroke);
} else {
CGContextStrokePath(ctx);
}
if (renderOutlineShadow) { // a small interior shadow within the button's outline
const CGFloat ins = 0.4f;
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, lx+ins, ty+ins);
CGContextAddLineToPoint(ctx, rx-ins, ty+ins);
CGContextAddLineToPoint(ctx, rx-ins, by-ins);
CGContextAddLineToPoint(ctx, lx+ins, by-ins);
CGContextClosePath(ctx);
components[0] = components[1] = components[2] = 0.0;
components[3] = 0.3;
CGContextSetStrokeColor(ctx, components);
CGContextSetLineWidth(ctx, 0.9);
CGContextStrokePath(ctx);
}
CGContextRestoreGState(ctx);
}
static void drawRoundedBezel(CGContextRef context,CGRect frame){
CGFloat radius=frame.size.height/2;
CGContextBeginPath(context);
CGContextAddArc(context,CGRectGetMaxX(frame)-radius,CGRectGetMinY(frame)+radius,radius,M_PI_2,M_PI_2*3,YES);
CGContextAddArc(context,CGRectGetMinX(frame)+radius,CGRectGetMinY(frame)+radius,radius,M_PI_2*3,M_PI_2,YES);
CGContextClosePath(context);
CGContextFillPath(context);
}
-(void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView {
BOOL defaulted=([[controlView window] defaultButtonCell] == self);
NSRect adjustment = [self getControlSizeAdjustment:[controlView isFlipped] ];
frame.size.width -= adjustment.size.width;
frame.size.height -= adjustment.size.height;
frame.origin.x += adjustment.origin.x;
frame.origin.y += adjustment.origin.y;
switch(_bezelStyle){
case NSDisclosureBezelStyle:
break;
case NSRegularSquareBezelStyle:
if([self isBordered]){
BOOL highlighted=(([self highlightsBy]&NSPushInCellMask) && [self isHighlighted]);
float topGray=highlighted?0.8:0.9;
float bottomGray=highlighted?0.7:0.8;
NSRect top=frame,bottom=frame;
top.size.height=floor(frame.size.height/2);
bottom.size.height=ceil(frame.size.height/2);
if([controlView isFlipped])
bottom.origin.y+=top.size.height;
else
top.origin.y+=bottom.size.height;
[[NSColor colorWithCalibratedWhite:topGray alpha:1] set];
NSRectFill(top);
[[NSColor colorWithCalibratedWhite:bottomGray alpha:1] set];
NSRectFill(bottom);
[[NSColor lightGrayColor] set];
NSFrameRectWithWidth(frame,1);
}
break;
case NSTexturedSquareBezelStyle:
case NSTexturedRoundedBezelStyle:
case NSShadowlessSquareBezelStyle:
if ([self isBordered]) {
[self _drawTexturedBezelWithFrame:frame];
}
break;
case NSRecessedBezelStyle:;
if([self isBordered] && [self isVisuallyHighlighted]){
CGContextRef context=[[NSGraphicsContext currentContext] graphicsPort];
frame.size.height--;
frame.origin.y+=[controlView isFlipped]?1:0;
[[NSColor lightGrayColor] setFill];
drawRoundedBezel(context,frame);
frame.origin.y+=[controlView isFlipped]?-1:1;
[[NSColor darkGrayColor] setFill];
drawRoundedBezel(context,frame);
frame.origin.y+=[controlView isFlipped]?0:-1;
frame.size.height++;
frame=CGRectInset(frame,1,1.5);
[[NSColor grayColor] setFill];
drawRoundedBezel(context,frame);
}
break;
default:
if(![self isBordered]){
[[_controlView graphicsStyle] drawUnborderedButtonInRect:frame defaulted:defaulted];
}
else {
if(([self highlightsBy]&NSPushInCellMask) && [self isHighlighted])
[[_controlView graphicsStyle] drawPushButtonPressedInRect:frame];
else if([self isVisuallyHighlighted])
[[_controlView graphicsStyle] drawPushButtonHighlightedInRect:frame];
else
[[_controlView graphicsStyle] drawPushButtonNormalInRect:frame defaulted:defaulted];
}
break;
}
}
-(void)drawImage:(NSImage *)image withFrame:(NSRect)rect inView:(NSView *)controlView {
BOOL enabled=[self isEnabled]?YES:![self imageDimsWhenDisabled];
BOOL mixed=([self state]==NSMixedState)?YES:NO;
CGContextRef ctx=[[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx,rect.origin.x,rect.origin.y);
if([controlView isFlipped]){
CGContextTranslateCTM(ctx,0,rect.size.height);
CGContextScaleCTM(ctx,1,-1);
}
[[controlView graphicsStyle] drawButtonImage:image inRect:NSMakeRect(0,0,rect.size.width,rect.size.height) enabled:enabled mixed:mixed];
CGContextRestoreGState(ctx);
}
-(NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)titleRect inView:(NSView *)controlView {
[title _clipAndDrawInRect:titleRect];
BOOL drawDottedRect=NO;
if([[controlView window] firstResponder]==controlView){
if([controlView isKindOfClass:[NSMatrix class]]){
NSMatrix *matrix=(NSMatrix *)controlView;
drawDottedRect=([matrix keyCell]==self)?YES:NO;
}
else if([controlView isKindOfClass:[NSControl class]]){
NSControl *control=(NSControl *)controlView;
drawDottedRect=([control selectedCell]==self)?YES:NO;
}
}
if(drawDottedRect)
NSDottedFrameRect(NSInsetRect(titleRect,1,1));
return titleRect; //FIXME: wrong value
}
// This function is duplicated in NSImageCell, consolidate
static NSSize scaledImageSizeInFrameSize(NSSize imageSize,NSSize frameSize,NSImageScaling scaling){
switch(scaling){
case NSImageScaleProportionallyDown:{
float xscale=frameSize.width/imageSize.width;
float yscale=frameSize.height/imageSize.height;
float scale=MIN(1.0,MIN(xscale,yscale));
imageSize.width*=scale;
imageSize.height*=scale;
return imageSize;
}
case NSImageScaleAxesIndependently:
return frameSize;
case NSImageScaleProportionallyUpOrDown:{
float xscale=frameSize.width/imageSize.width;
float yscale=frameSize.height/imageSize.height;
float scale=MIN(xscale,yscale);
imageSize.width*=scale;
imageSize.height*=scale;
return imageSize;
}
default:
case NSImageScaleNone:
return imageSize;
}
}
-(void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)controlView {
/* This method gets the original button frame. We have to compensate for borders.
There is some duplication of rect calculation which can be split out
*/
BOOL defaulted=([[controlView window] defaultButtonCell] == self);
NSRect adjustment = [self getControlSizeAdjustment:[controlView isFlipped] ];
frame.size.width -= adjustment.size.width;
frame.size.height -= adjustment.size.height;
frame.origin.x += adjustment.origin.x;
frame.origin.y += adjustment.origin.y;
if(_bezelStyle==NSDisclosureBezelStyle)
;
else if(![self isBordered]){
if(defaulted)
frame = NSInsetRect(frame,1,1);
}
else {
frame=NSInsetRect(frame,2,2);
}
NSAttributedString *title=[self titleForHighlight];
NSImage *image=[self imageForHighlight];
BOOL enabled=[self isEnabled]?YES:![self imageDimsWhenDisabled];
BOOL mixed=([self state]==NSMixedState)?YES:NO;
NSRect imageRect = [self imageRectForBounds: frame];
NSSize imageSize=imageRect.size;
NSPoint imageOrigin=imageRect.origin;
NSSize titleSize=[title size];
NSRect titleRect=[self titleRectForBounds:frame];
BOOL drawImage=YES,drawTitle=YES;
NSCellImagePosition imagePosition=[self imagePosition];
if([self isTransparent])
return;
// it doesnt actually change the image pos in the button but it draws like this
if([self bezelStyle]==NSDisclosureBezelStyle)
imagePosition=NSImageOnly;
imageSize=scaledImageSizeInFrameSize(imageSize,frame.size,[self imageScaling]);
imageOrigin.x+=floor((frame.size.width-imageSize.width)/2);
imageOrigin.y+=floor((frame.size.height-imageSize.height)/2);
titleRect.origin.y+=floor((titleRect.size.height-titleSize.height)/2);
titleRect.size.height=titleSize.height;
switch(imagePosition){
case NSNoImage: