1/* -*- mode:objc; coding:utf-8; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*- */
2/*
3  Copyright (c) 2003-2005 MacUIM contributors, All rights reserved.
4
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions
7  are met:
8
9  1. Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11  2. Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in the
13     documentation and/or other materials provided with the distribution.
14  3. Neither the name of authors nor the names of its contributors
15     may be used to endorse or promote products derived from this software
16     without specific prior written permission.
17
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
22  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  SUCH DAMAGE.
29*/
30
31#import "ModeTipsPanel.h"
32
33@implementation ModeTipsPanel
34
35- (void)initView
36{
37  NSView *content;
38
39  modeTipsView = [[ModeTipsView alloc] initWithFrame:[self frame]];
40  image = nil;
41
42  content = [[self contentView] retain];
43  [content removeFromSuperview];
44  [content release];
45
46  [self setContentView:modeTipsView];
47}
48
49- (ModeTipsView *)view
50{
51  return modeTipsView;
52}
53
54- (BOOL)canBecomeMainWindow
55{
56  return YES;
57}
58
59- (BOOL)canBecomeKeyWindow
60{
61  return YES;
62}
63
64- (void)showLabels:(NSArray *)labels
65{
66  [self createImage:labels];
67  [modeTipsView setImage:image];
68  [self setAlphaValue:1.0];
69  [modeTipsView setNeedsDisplay:YES];
70}
71
72- (void)createImage:(NSArray *)labels
73{
74  NSView *view = [self contentView];
75
76  if ([labels count] <= 1) {
77    [self setContentSize:NSMakeSize(kModeTipsWidth, kModeTipsHeight)];
78    [view setFrameSize:NSMakeSize(kModeTipsWidth, [view frame].size.height)];
79    image = [[[NSImage alloc] initWithSize:NSMakeSize(kModeTipsWidth, kModeTipsHeight)]
80              autorelease];
81  }
82  else if ([labels count] == 2) {
83    [self setContentSize:NSMakeSize(kModeTipsWidth2, kModeTipsHeight)];
84    [view setFrameSize:NSMakeSize(kModeTipsWidth2, [view frame].size.height)];
85    image = [[[NSImage alloc] initWithSize:NSMakeSize(kModeTipsWidth2, kModeTipsHeight)]
86              autorelease];
87  } else {
88    [self setContentSize:NSMakeSize(kModeTipsWidth3, kModeTipsHeight)];
89    [view setFrameSize:NSMakeSize(kModeTipsWidth3, [view frame].size.height)];
90    image = [[[NSImage alloc] initWithSize:NSMakeSize(kModeTipsWidth3, kModeTipsHeight)]
91              autorelease];
92  }
93
94  [self renderFrame:labels];
95  [self renderText:labels];
96}
97
98- (void)renderFrame:(NSArray *)labels
99{
100  NSBezierPath *framePath;
101  NSColor *color;
102
103  color = [NSColor blackColor];
104
105  [image lockFocus];
106
107  if ([labels count] <= 1) {
108    framePath =
109      [NSBezierPath bezierPathWithRect:
110        NSMakeRect(0.5, 0.5,
111                   kModeTipsWidth - 1.0, kModeTipsHeight - 1.0)];
112    [[NSColor whiteColor] set];
113    [framePath fill];
114
115    [color set];
116    [framePath stroke];
117  }
118  else if ([labels count] == 2) {
119    framePath =
120      [NSBezierPath bezierPathWithRect:
121        NSMakeRect(0.5, 0.5,
122                   kModeTipsWidth2 - 1.0, kModeTipsHeight - 1.0)];
123    [[NSColor whiteColor] set];
124    [framePath fill];
125
126    [color set];
127    [framePath stroke];
128
129    [[NSGraphicsContext currentContext] setShouldAntialias:NO];
130    framePath = [NSBezierPath bezierPath];
131    [framePath moveToPoint:NSMakePoint(kModeTipsWidth2 / 2.0, 0.5)];
132    [framePath lineToPoint:NSMakePoint(kModeTipsWidth2 / 2.0, 0.5 + kModeTipsHeight - 1.0)];
133    [color set];
134    [framePath stroke];
135    [[NSGraphicsContext currentContext] setShouldAntialias:YES];
136  } else {
137    framePath =
138      [NSBezierPath bezierPathWithRect:
139        NSMakeRect(0.5, 0.5,
140                   kModeTipsWidth3 - 1.0, kModeTipsHeight - 1.0)];
141    [[NSColor whiteColor] set];
142    [framePath fill];
143    [color set];
144    [framePath stroke];
145
146
147    [[NSGraphicsContext currentContext] setShouldAntialias:NO];
148    framePath = [NSBezierPath bezierPath];
149    [framePath moveToPoint:NSMakePoint(kModeTipsWidth3 / 3.0, 0.5)];
150    [framePath lineToPoint:NSMakePoint(kModeTipsWidth3 / 3.0, 0.5 + kModeTipsHeight - 1.0)];
151    [color set];
152    [framePath stroke];
153
154    framePath = [NSBezierPath bezierPath];
155    [framePath moveToPoint:NSMakePoint(kModeTipsWidth3 * 2.0 / 3.0, 0.5)];
156    [framePath lineToPoint:NSMakePoint(kModeTipsWidth3 * 2.0 / 3.0, 0.5 + kModeTipsHeight - 1.0)];
157    [color set];
158    [framePath stroke];
159    [[NSGraphicsContext currentContext] setShouldAntialias:YES];
160  }
161
162  [image unlockFocus];
163}
164
165- (void)renderText:(NSArray *)labels
166{
167  NSMutableAttributedString *text;
168  NSMutableString *label = nil;
169  NSColor *color;
170  int i;
171
172  color = [NSColor blackColor];
173
174  [image lockFocus];
175
176  [[NSGraphicsContext currentContext] setShouldAntialias:YES];
177
178  if ([labels count] < 1)
179    label = [[NSMutableString alloc] initWithString:@"?"];
180
181  i = 0;
182  do {
183    if (!label)
184      label = [[NSMutableString alloc] initWithString:[labels objectAtIndex:i]];
185
186    if ([labels count] <= 1) {
187      text = [[NSAttributedString alloc]
188              initWithString:label
189                  attributes:[NSDictionary dictionaryWithObjectsAndKeys:
190                    [NSFont boldSystemFontOfSize:12],
191                    NSFontAttributeName,
192                    color,
193                    NSForegroundColorAttributeName,
194                    nil]];
195      [text drawAtPoint:NSMakePoint(0.5 + 0.5 +
196                                    (kModeTipsWidth - 1.0 -
197                                     ceil([text size].width + 0.5)) / 2.0,
198                                    (kModeTipsHeight - ceil([text size].height)) / 2.0)];
199    }
200    else if ([labels count] == 2) {
201      text = [[NSAttributedString alloc]
202              initWithString:label
203                  attributes:[NSDictionary dictionaryWithObjectsAndKeys:
204                    [NSFont boldSystemFontOfSize:11],
205                    NSFontAttributeName,
206                    color,
207                    NSForegroundColorAttributeName,
208                    nil]];
209      [text drawAtPoint:NSMakePoint(0.5 + 0.5 +
210                                    (i > 0 ? (kModeTipsWidth2 - 1.0) / 2.0 : 0)
211                                    + (kModeTipsWidth2 / 2.0 - 0.5 -
212                                     ceil([text size].width + 0.5)) / 2.0,
213                                    (kModeTipsHeight - ceil([text size].height)) / 2.0)];
214    } else {
215      text = [[NSAttributedString alloc]
216              initWithString:label
217                  attributes:[NSDictionary dictionaryWithObjectsAndKeys:
218                    [NSFont boldSystemFontOfSize:11],
219                    NSFontAttributeName,
220                    color,
221                    NSForegroundColorAttributeName,
222                    nil]];
223      if (i == 0) {
224      [text drawAtPoint:NSMakePoint(0.5 + 0.5 +
225                                    0
226                                    + (kModeTipsWidth3 / 3.0 - 0.5 -
227                                     ceil([text size].width + 0.5)) / 2.0,
228                                    (kModeTipsHeight - ceil([text size].height)) / 2.0)];
229      } else if (i == 1) {
230      [text drawAtPoint:NSMakePoint(0.5 + 0.5 +
231                                    kModeTipsWidth3 / 3.0
232                                    + (kModeTipsWidth3 / 3.0 - 0.5 -
233                                     ceil([text size].width + 0.5)) / 2.0,
234                                    (kModeTipsHeight - ceil([text size].height)) / 2.0)];
235      } else if (i == 2) {
236      [text drawAtPoint:NSMakePoint(0.5 + 0.5 +
237                                    kModeTipsWidth3 * 2.0 / 3.0
238                                    + (kModeTipsWidth3 / 3.0 - 0.5 -
239                                     ceil([text size].width + 0.5)) / 2.0,
240                                    (kModeTipsHeight - ceil([text size].height)) / 2.0)];
241      }
242
243    }
244
245    [text release];
246    [label release];
247    label = nil;
248
249  } while (++i < [labels count]);
250
251  [image unlockFocus];
252}
253
254@end
255