Keyboard Events(Chapter 19 of Cocoa Programming for Mac OS X)
1 #import "BigLetterView.h"
2
3
4 @implementation BigLetterView
5
6 - (id)initWithFrame:(NSRect)frameRect
7 {
8 if(![super initWithFrame:frameRect])
9 {
10 return nil;
11 }
12
13 NSLog(@"initializing view");
14 bgColor = [[NSColor yellowColor] retain];
15 string = @" ";
16 return self;
17 }
18
19 - (void)dealloc
20 {
21 [bgColor release];
22 [string release];
23 [super dealloc];
24 }
25
26 - (void)drawRect:(NSRect)dirtyRect
27 {
28 NSRect bounds = [self bounds];
29 [bgColor set];
30 [NSBezierPath fillRect:bounds];
31
32 if([[self window] firstResponder] == self)
33 {
34 [[NSColor keyboardFocusIndicatorColor] set];
35 [NSBezierPath setDefaultLineWidth:4.0];
36 [NSBezierPath strokeRect:bounds];
37 }
38 }
39
40 - (BOOL)isOpaque
41 {
42 return YES;
43 }
44
45 - (BOOL)acceptsFirstResponder
46 {
47 NSLog(@"Accepting");
48 return YES;
49 }
50
51 - (BOOL)resignFirstResponder
52 {
53 NSLog(@"Resigning");
54 [self setNeedsDisplay:YES];
55 return YES;
56 }
57
58 - (BOOL)becomeFirstResponder
59 {
60 NSLog(@"Becoming");
61 [self setNeedsDisplay:YES];
62 return YES;
63 }
64
65 - (void)keyDown:(NSEvent *)theEvent
66 {
67 [self interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
68 }
69
70 - (void)insertText:(NSString *)input
71 {
72 [self setString:input];
73 }
74
75 - (void)insertTab:(id)sender
76 {
77 [[self window] selectKeyViewPrecedingView:self];
78 }
79
80 - (void)deleteBackward:(id)sender
81 {
82 [self setString:@" "];
83 }
84
85 #pragma mark Accessors
86
87 - (void)setBgColor:(NSColor *)c
88 {
89 [c retain];
90 [bgColor release];
91 bgColor = c;
92 [self setNeedsDisplay:YES];
93 }
94
95 - (NSColor *)bgColor
96 {
97 return bgColor;
98 }
99
100 - (void)setString:(NSString *)c
101 {
102 c = [c copy];
103 [string release];
104 string = c;
105 NSLog(@"The string is now %@", string);
106 }
107
108 - (NSString *)string
109 {
110 return string;
111 }
112
113 @end
2
3
4 @implementation BigLetterView
5
6 - (id)initWithFrame:(NSRect)frameRect
7 {
8 if(![super initWithFrame:frameRect])
9 {
10 return nil;
11 }
12
13 NSLog(@"initializing view");
14 bgColor = [[NSColor yellowColor] retain];
15 string = @" ";
16 return self;
17 }
18
19 - (void)dealloc
20 {
21 [bgColor release];
22 [string release];
23 [super dealloc];
24 }
25
26 - (void)drawRect:(NSRect)dirtyRect
27 {
28 NSRect bounds = [self bounds];
29 [bgColor set];
30 [NSBezierPath fillRect:bounds];
31
32 if([[self window] firstResponder] == self)
33 {
34 [[NSColor keyboardFocusIndicatorColor] set];
35 [NSBezierPath setDefaultLineWidth:4.0];
36 [NSBezierPath strokeRect:bounds];
37 }
38 }
39
40 - (BOOL)isOpaque
41 {
42 return YES;
43 }
44
45 - (BOOL)acceptsFirstResponder
46 {
47 NSLog(@"Accepting");
48 return YES;
49 }
50
51 - (BOOL)resignFirstResponder
52 {
53 NSLog(@"Resigning");
54 [self setNeedsDisplay:YES];
55 return YES;
56 }
57
58 - (BOOL)becomeFirstResponder
59 {
60 NSLog(@"Becoming");
61 [self setNeedsDisplay:YES];
62 return YES;
63 }
64
65 - (void)keyDown:(NSEvent *)theEvent
66 {
67 [self interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
68 }
69
70 - (void)insertText:(NSString *)input
71 {
72 [self setString:input];
73 }
74
75 - (void)insertTab:(id)sender
76 {
77 [[self window] selectKeyViewPrecedingView:self];
78 }
79
80 - (void)deleteBackward:(id)sender
81 {
82 [self setString:@" "];
83 }
84
85 #pragma mark Accessors
86
87 - (void)setBgColor:(NSColor *)c
88 {
89 [c retain];
90 [bgColor release];
91 bgColor = c;
92 [self setNeedsDisplay:YES];
93 }
94
95 - (NSColor *)bgColor
96 {
97 return bgColor;
98 }
99
100 - (void)setString:(NSString *)c
101 {
102 c = [c copy];
103 [string release];
104 string = c;
105 NSLog(@"The string is now %@", string);
106 }
107
108 - (NSString *)string
109 {
110 return string;
111 }
112
113 @end