root/trunk/Views/JVFontPreviewField.m

Revision 3072, 3.5 kB (checked in by timothy, 3 years ago)

Major file system restructuring of the code files.

Line 
1 #import "JVFontPreviewField.h"
2
3 @implementation JVFontPreviewField
4 - (id) initWithCoder:(NSCoder *) coder {
5         self = [super initWithCoder:coder];
6         if( [coder allowsKeyedCoding] ) {
7                 _showPointSize = [coder decodeBoolForKey:@"showPointSize"];
8                 _showFontFace = [coder decodeBoolForKey:@"showFontFace"];
9                 _actualFont = [[coder decodeObjectForKey:@"actualFont"] retain];
10         } else {
11                 [coder decodeValueOfObjCType:@encode( char ) at:&_showPointSize];
12                 [coder decodeValueOfObjCType:@encode( char ) at:&_showFontFace];
13                 _actualFont = [[coder decodeObject] retain];
14         }
15         return self;
16 }
17
18 - (void) encodeWithCoder:(NSCoder *) coder {
19         [super encodeWithCoder:coder];
20         if( [coder allowsKeyedCoding] ) {
21                 [coder encodeBool:_showPointSize forKey:@"showPointSize"];
22                 [coder encodeBool:_showFontFace forKey:@"showFontFace"];
23                 [coder encodeObject:_actualFont forKey:@"actualFont"];
24         } else {
25                 [coder encodeValueOfObjCType:@encode( char ) at:&_showPointSize];
26                 [coder encodeValueOfObjCType:@encode( char ) at:&_showFontFace];
27                 [coder encodeObject:_actualFont];
28         }
29 }
30
31 - (void) dealloc {
32         [_actualFont release];
33         _actualFont = nil;
34         [super dealloc];
35 }
36
37 - (void) selectFont:(id) sender {
38         NSFont *font = [sender convertFont:[self font]];
39
40         if( ! font ) return;
41
42         if( [[self delegate] respondsToSelector:@selector( fontPreviewField:shouldChangeToFont: )] )
43                 if( ! [[self delegate] fontPreviewField:self shouldChangeToFont:font] ) return;
44
45         [self setFont:font];
46
47         if( [[self delegate] respondsToSelector:@selector( fontPreviewField:didChangeToFont: )] )
48                 [[self delegate] fontPreviewField:self didChangeToFont:font];
49 }
50
51 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2
52 #define NSFontPanelStandardModesMask 0xFFFF
53 #define NSFontPanelSizeModeMask 1 << 1
54 #define NSFontPanelFaceModeMask 1 << 0
55 #endif
56
57 - (unsigned int) validModesForFontPanel:(NSFontPanel *) fontPanel {
58         unsigned int ret = NSFontPanelStandardModesMask;
59         if( ! _showPointSize ) ret ^= NSFontPanelSizeModeMask;
60         if( ! _showFontFace ) ret ^= NSFontPanelFaceModeMask;
61         return ret;
62         return 0;
63 }
64
65 - (BOOL) becomeFirstResponder {
66         [[NSFontManager sharedFontManager] setSelectedFont:_actualFont isMultiple:NO];
67         return YES;
68 }
69
70 - (void) setFont:(NSFont *) font {
71         if( ! font ) return;
72
73         [_actualFont autorelease];
74         _actualFont = [font retain];
75
76         [super setFont:[[NSFontManager sharedFontManager] convertFont:font toSize:11.]];
77
78         NSMutableAttributedString *text = nil;
79         if( _showPointSize ) {
80                 text = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %.0f", ( _showFontFace ? [_actualFont displayName] : [_actualFont familyName] ), [_actualFont pointSize]]] autorelease];
81         } else {
82                 text = [[[NSMutableAttributedString alloc] initWithString:( _showFontFace ? [_actualFont displayName] : [_actualFont familyName] )] autorelease];
83         }
84
85         NSMutableParagraphStyle *paraStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
86
87         [paraStyle setMinimumLineHeight:NSHeight( [self bounds] )];
88         [paraStyle setMaximumLineHeight:NSHeight( [self bounds] )];
89         [text addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange( 0, [text length] )];
90
91         [self setObjectValue:text];
92 }
93
94 - (IBAction) chooseFontWithFontPanel:(id) sender {
95         [[NSFontManager sharedFontManager] setAction:@selector( selectFont: )];
96         [[self window] makeFirstResponder:self];
97         [[NSFontManager sharedFontManager] orderFrontFontPanel:nil];
98 }
99
100 - (void) setShowPointSize:(BOOL) show {
101         _showPointSize = show;
102 }
103
104 - (void) setShowFontFace:(BOOL) show {
105         _showFontFace = show;
106 }
107 @end
Note: See TracBrowser for help on using the browser.