Progress Check In
[ITKit.git] / Showcase / Controller.m
1 #import "Controller.h"
2 #import "ITTransientStatusWindow.h"
3 #import "ITTextField.h"
4
5 #define SW_PAD    24.0
6 #define SW_SPACE  24.0
7 #define SW_MINW   211.0
8 #define SW_BORDER 32.0
9 #define SW_IMAGE  @"Library"
10
11 @interface Controller (ITStatusItemSupport)
12 - (void)createStatusItem;
13 - (void)removeStatusItem;
14 @end
15
16
17 @implementation Controller
18
19 - (void)awakeFromNib
20 {
21     [self createStatusItem];
22     [testTextField setCastsShadow:YES];
23     statusWindow = [ITTransientStatusWindow sharedWindow];
24 //  [tabView setAllowsDragging:YES];
25 }
26
27 /*************************************************************************/
28 #pragma mark -
29 #pragma mark ITStatusItem SUPPORT
30 /*************************************************************************/
31
32 - (void)createStatusItem
33 {
34     statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
35                                               withLength:NSVariableStatusItemLength];
36
37     if ( [showImageCheckBox state] == NSOnState ) {
38         [statusItem setImage:[NSImage imageNamed:@"ITStatusItem"]];
39     }
40
41     if ( [useInvertedCheckBox state] == NSOnState ) {
42         [statusItem setAlternateImage:[NSImage imageNamed:@"ITStatusItemInv"]];
43     }
44
45     if ( [showTitleCheckBox state] == NSOnState ) {
46         [statusItem setTitle:@"ITStatusItem"];
47     }
48
49     [statusItem setMenu:statusItemMenu];
50 }
51
52 - (void)removeStatusItem
53 {
54     [[statusItem statusBar] removeStatusItem:statusItem];
55     [statusItem autorelease];
56     statusItem = nil;
57 }
58
59 - (IBAction)toggleStatusItem:(id)sender
60 {
61     if ( [sender state] == NSOnState ) {
62         [self createStatusItem];
63         [showImageCheckBox   setEnabled:YES];
64         [showTitleCheckBox   setEnabled:YES];
65         [useInvertedCheckBox setEnabled:YES];
66     } else {
67         [self removeStatusItem];
68         [showImageCheckBox   setEnabled:NO];
69         [useInvertedCheckBox setEnabled:NO];
70         [showTitleCheckBox   setEnabled:NO];
71     }
72 }
73
74 - (IBAction)toggleImage:(id)sender
75 {
76     if ( [sender state] == NSOnState ) {
77         [statusItem setImage:[NSImage imageNamed:@"ITStatusItem"]];
78         [statusItem setAlternateImage:[NSImage imageNamed:@"ITStatusItemInv"]];
79         [useInvertedCheckBox setEnabled:YES];
80         [useInvertedCheckBox setState:NSOnState];
81     } else {
82         [statusItem setImage:nil];
83         [statusItem setAlternateImage:nil];
84         [useInvertedCheckBox setEnabled:NO];
85         [useInvertedCheckBox setState:NSOffState];
86     }
87 }
88
89 - (IBAction)toggleInvertedImage:(id)sender
90 {
91     if ( [sender state] == NSOnState ) {
92         [statusItem setAlternateImage:[NSImage imageNamed:@"ITStatusItemInv"]];
93     } else {
94         [statusItem setAlternateImage:nil];
95     }
96 }
97
98 - (IBAction)toggleTitle:(id)sender
99 {
100     if ( [sender state] == NSOnState ) {
101         [statusItem setTitle:@"ITStatusItem"];
102     } else {
103         [statusItem setTitle:nil];
104     }
105 }
106
107
108 /*************************************************************************/
109 #pragma mark -
110 #pragma mark ITTextView SUPPORT
111 /*************************************************************************/
112
113 - (IBAction)toggleCastsShadow:(id)sender
114 {
115     [testTextField setCastsShadow:([sender state] == NSOnState)];
116 }
117
118
119 /*************************************************************************/
120 #pragma mark -
121 #pragma mark ITTransientStatusWindow SUPPORT
122 /*************************************************************************/
123
124 - (IBAction)buildStatusWindow:(id)sender
125 {
126     NSImageView *imageView    = nil;
127     ITTextField *textField    = nil;
128     NSImage     *image        = [NSImage imageNamed:SW_IMAGE];
129     NSRect       imageRect;
130     NSRect       textRect;   
131
132     float imageWidth    = 0.0;
133     float imageHeight   = 0.0;
134     float textWidth     = 0.0;
135     float textHeight    = 0.0;
136     float contentHeight = 0.0;
137     float windowWidth   = 0.0;
138     float windowHeight  = 0.0;
139
140     NSString     *text     = [swSampleTextView string];
141     NSArray      *lines    = [text componentsSeparatedByString:@"\n"];
142     id                    oneLine  = nil;
143     NSEnumerator *lineEnum = [lines objectEnumerator];
144
145     NSFont *font           = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
146     NSDictionary *attr     = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
147     
148     // Get image width and height.
149     imageWidth  = [image size].width;
150     imageHeight = [image size].height;
151     
152     // Iterate over each line to get text width and height
153     while ( oneLine = [lineEnum nextObject] ) {
154         // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
155         float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
156         // Add the height of this line to the total text height
157         textHeight += [oneLine sizeWithAttributes:attr].height;
158         // If this line wider than the last one, set it as the text width.
159         textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
160     }
161     
162     // Add 4.0 to the final textHeight to accomodate the shadow.
163     textHeight += 4.0;
164     
165     // Set the content height to the greater of the text and image heights.
166     contentHeight = ( ( imageHeight > textHeight ) ? imageHeight : textHeight );
167     
168     // Setup the Window, and remove all its contentview's subviews.
169     windowWidth  = ( SW_PAD + imageWidth + SW_SPACE + textWidth + SW_PAD );
170     windowHeight = ( SW_PAD + contentHeight + SW_PAD );
171     [statusWindow setFrame:NSMakeRect(SW_BORDER, SW_BORDER, windowWidth, windowHeight) display:YES];
172     [[[statusWindow contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
173     
174     // Setup, position, fill, and add the image view to the content view.
175     imageRect = NSMakeRect( SW_PAD,
176                             (SW_PAD + ((contentHeight - imageHeight) / 2)),
177                             imageWidth,
178                             imageHeight );
179     imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
180     [imageView setImage:image];
181     [[statusWindow contentView] addSubview:imageView];
182     
183     // Setup, position, fill, and add the text view to the content view.
184     textRect = NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
185                            (SW_PAD + ((contentHeight - textHeight) / 2)),
186                            textWidth,
187                            textHeight);
188     textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
189     [textField setEditable:NO];
190     [textField setSelectable:NO];
191     [textField setBordered:NO];
192     [textField setDrawsBackground:NO];
193     [textField setFont:[NSFont fontWithName:@"Lucida Grande Bold" size:18]];
194     [textField setTextColor:[NSColor whiteColor]];
195     [textField setCastsShadow:YES];
196     [textField setStringValue:text];
197     [[statusWindow contentView] addSubview:textField];
198
199     [[statusWindow contentView] setNeedsDisplay:YES];
200 }
201
202 - (IBAction)showStatusWindow:(id)sender
203 {
204 //    [[statusWindow contentView] setNeedsDisplay:YES];
205     [statusWindow setFrame:NSMakeRect( (0.0 - NSWidth([statusWindow frame])),
206                                        SW_BORDER,
207                                        NSWidth([statusWindow frame]),
208                                        NSHeight([statusWindow frame]) )
209                    display:YES];
210     [statusWindow orderFront:self];
211     [statusWindow setFrame:NSMakeRect( SW_BORDER,
212                                        SW_BORDER,
213                                        NSWidth([statusWindow frame]),
214                                        NSHeight([statusWindow frame]) )
215                    display:YES
216                    animate:YES];
217 }
218
219 - (IBAction)hideStatusWindow:(id)sender
220 {
221     [statusWindow orderOut:self];
222 }
223
224 - (IBAction)setRotation:(id)sender
225 {
226     [statusWindow setRotation:([sender floatValue] * (pi / 180))];
227 }
228
229
230 /*************************************************************************/
231 #pragma mark -
232 #pragma mark NSWindow DELEGATE METHODS
233 /*************************************************************************/
234
235 - (void)windowWillMiniaturize:(NSNotification *)note
236 {
237     [[note object] setMiniwindowImage:[NSImage imageNamed:@"ITStatusItem"]];
238 }
239
240
241 @end