Cleaning up a bit, and testing SyncMail. PLEASE DO NOT CHANGE THE ITKIT TARGET SETTI...
[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 setBordered:NO];
190     [textField setDrawsBackground:NO];
191     [textField setFont:[NSFont fontWithName:@"Lucida Grande Bold" size:18]];
192     [textField setTextColor:[NSColor whiteColor]];
193     [textField setCastsShadow:YES];
194     [textField setStringValue:text];
195     [[statusWindow contentView] addSubview:textField];
196
197     [[statusWindow contentView] setNeedsDisplay:YES];
198 }
199
200 /*
201 - (IBAction)foo:(id)sender
202 {
203
204     maxLineHeight = ( ( maxLineHeight > [cdImage size].height ) ? maxLineHeight : [cdImage size].height );
205     
206     totalWidth  = ( ITTSWPADDING + [cdImage size].width + ITTSWSPACING + maxLineWidth + ITTSWPADDING );
207     totalHeight = ( ITTSWPADDING + maxLineHeight + ITTSWPADDING );
208
209     totalWidth  = ( ( totalWidth  > ITTSWMINW ) ? totalWidth  : ITTSWMINW );
210
211
212     [statusWindow setFrame:NSMakeRect(72.0, 72.0, totalWidth, totalHeight)
213                    display:YES];
214
215     textField = [[[ITTextField alloc] initWithFrame:
216         NSMakeRect((24.0 + [cdImage size].width + 32.0), 24.0, maxLineWidth, maxLineHeight)] autorelease];
217     [[statusWindow contentView] addSubview:textField];
218     [textField setBordered:NO];
219     [textField setDrawsBackground:NO];
220     [textField setFont:[NSFont fontWithName:@"Lucida Grande Bold" size:18]];
221     [textField setTextColor:[NSColor whiteColor]];
222     [textField setCastsShadow:YES];
223     [textField setStringValue:text];
224
225
226     [[statusWindow contentView] lockFocus];
227     [cdImage compositeToPoint:NSMakePoint(ITTSWPADDING, ( ITTSWPADDING + ((maxLineHeight - [cdImage size].height) / ITTSWPADDING)) )
228                     operation:NSCompositeSourceOver];
229     [[statusWindow contentView] unlockFocus];
230 }
231 */
232
233 - (IBAction)showStatusWindow:(id)sender
234 {
235 //    [[statusWindow contentView] setNeedsDisplay:YES];
236     [statusWindow setFrame:NSMakeRect( (0.0 - NSWidth([statusWindow frame])),
237                                        SW_BORDER,
238                                        NSWidth([statusWindow frame]),
239                                        NSHeight([statusWindow frame]) )
240                    display:YES];
241     [statusWindow orderFront:self];
242     [statusWindow setFrame:NSMakeRect( SW_BORDER,
243                                        SW_BORDER,
244                                        NSWidth([statusWindow frame]),
245                                        NSHeight([statusWindow frame]) )
246                    display:YES
247                    animate:YES];
248 }
249
250 - (IBAction)hideStatusWindow:(id)sender
251 {
252     [statusWindow orderOut:self];
253 }
254
255 /*************************************************************************/
256 #pragma mark -
257 #pragma mark NSWindow DELEGATE METHODS
258 /*************************************************************************/
259
260 - (void)windowWillMiniaturize:(NSNotification *)note
261 {
262     [[note object] setMiniwindowImage:[NSImage imageNamed:@"ITStatusItem"]];
263 }
264
265
266 @end