1 | /* $Id: CocoaDockIconPreview.mm 24377 2009-11-05 11:18:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
5 | * Cocoa helper for the dock icon preview
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /* VBox includes */
|
---|
25 | #include "CocoaDockIconPreview.h"
|
---|
26 | #include "VBoxCocoaHelper.h"
|
---|
27 |
|
---|
28 | /* System includes */
|
---|
29 | #import <Cocoa/Cocoa.h>
|
---|
30 |
|
---|
31 | @interface DockTileMonitor: NSView
|
---|
32 | {
|
---|
33 | CocoaDockIconPreviewPrivate *p;
|
---|
34 |
|
---|
35 | NSImageView *mScreenContent;
|
---|
36 | NSImageView *mMonitorGlossy;
|
---|
37 | }
|
---|
38 | - (id)initWithFrame:(NSRect)frame parent:(CocoaDockIconPreviewPrivate*)parent;
|
---|
39 | - (NSImageView*)screenContent;
|
---|
40 | - (void)resize:(NSSize)size;
|
---|
41 | @end
|
---|
42 |
|
---|
43 | @interface DockTileOverlay: NSView
|
---|
44 | {
|
---|
45 | CocoaDockIconPreviewPrivate *p;
|
---|
46 | }
|
---|
47 | - (id)initWithFrame:(NSRect)frame parent:(CocoaDockIconPreviewPrivate*)parent;
|
---|
48 | @end
|
---|
49 |
|
---|
50 | @interface DockTile: NSView
|
---|
51 | {
|
---|
52 | CocoaDockIconPreviewPrivate *p;
|
---|
53 |
|
---|
54 | DockTileMonitor *mMonitor;
|
---|
55 | NSImageView *mAppIcon;
|
---|
56 |
|
---|
57 | DockTileOverlay *mOverlay;
|
---|
58 | }
|
---|
59 | - (id)initWithParent:(CocoaDockIconPreviewPrivate*)parent;
|
---|
60 | - (NSView*)screenContent;
|
---|
61 | - (void)cleanup;
|
---|
62 | - (void)restoreAppIcon;
|
---|
63 | - (void)updateAppIcon;
|
---|
64 | - (void)restoreMonitor;
|
---|
65 | - (void)updateMonitorWithImage:(CGImageRef)image;
|
---|
66 | - (void)resizeMonitor:(NSSize)size;
|
---|
67 | @end
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Helper class which allow us to access all members/methods of AbstractDockIconPreviewHelper
|
---|
71 | * from any Cocoa class.
|
---|
72 | */
|
---|
73 | class CocoaDockIconPreviewPrivate: public AbstractDockIconPreviewHelper
|
---|
74 | {
|
---|
75 | public:
|
---|
76 | inline CocoaDockIconPreviewPrivate (VBoxConsoleWnd *aMainWnd, const QPixmap& aOverlayImage)
|
---|
77 | :AbstractDockIconPreviewHelper (aMainWnd, aOverlayImage)
|
---|
78 | {
|
---|
79 | mDockTile = [[DockTile alloc] initWithParent:this];
|
---|
80 | }
|
---|
81 |
|
---|
82 | inline ~CocoaDockIconPreviewPrivate()
|
---|
83 | {
|
---|
84 | [mDockTile release];
|
---|
85 | }
|
---|
86 |
|
---|
87 | DockTile *mDockTile;
|
---|
88 | };
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Cocoa wrapper for the abstract dock icon preview class
|
---|
92 | */
|
---|
93 | CocoaDockIconPreview::CocoaDockIconPreview (VBoxConsoleWnd *aMainWnd, const QPixmap& aOverlayImage)
|
---|
94 | : AbstractDockIconPreview (aMainWnd, aOverlayImage)
|
---|
95 | {
|
---|
96 | CocoaAutoreleasePool pool;
|
---|
97 |
|
---|
98 | d = new CocoaDockIconPreviewPrivate (aMainWnd, aOverlayImage);
|
---|
99 | }
|
---|
100 |
|
---|
101 | CocoaDockIconPreview::~CocoaDockIconPreview()
|
---|
102 | {
|
---|
103 | CocoaAutoreleasePool pool;
|
---|
104 |
|
---|
105 | delete d;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void CocoaDockIconPreview::updateDockOverlay()
|
---|
109 | {
|
---|
110 | CocoaAutoreleasePool pool;
|
---|
111 |
|
---|
112 | [d->mDockTile updateAppIcon];
|
---|
113 | }
|
---|
114 |
|
---|
115 | void CocoaDockIconPreview::updateDockPreview (CGImageRef aVMImage)
|
---|
116 | {
|
---|
117 | CocoaAutoreleasePool pool;
|
---|
118 |
|
---|
119 | [d->mDockTile updateMonitorWithImage:aVMImage];
|
---|
120 | }
|
---|
121 |
|
---|
122 | void CocoaDockIconPreview::updateDockPreview (VBoxFrameBuffer *aFrameBuffer)
|
---|
123 | {
|
---|
124 | CocoaAutoreleasePool pool;
|
---|
125 |
|
---|
126 | AbstractDockIconPreview::updateDockPreview (aFrameBuffer);
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | void CocoaDockIconPreview::setOriginalSize (int aWidth, int aHeight)
|
---|
131 | {
|
---|
132 | CocoaAutoreleasePool pool;
|
---|
133 |
|
---|
134 | [d->mDockTile resizeMonitor:NSMakeSize (aWidth, aHeight)];
|
---|
135 | }
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * Class for arranging/updating the layers for the glossy monitor preview.
|
---|
139 | */
|
---|
140 | @implementation DockTileMonitor;
|
---|
141 | - (id)initWithFrame:(NSRect)frame parent:(CocoaDockIconPreviewPrivate*)parent
|
---|
142 | {
|
---|
143 | self = [super initWithFrame:frame];
|
---|
144 |
|
---|
145 | if (self != nil)
|
---|
146 | {
|
---|
147 | p = parent;
|
---|
148 | /* The screen content view */
|
---|
149 | mScreenContent = [[NSImageView alloc] initWithFrame:NSRectFromCGRect (p->flipRect (p->mUpdateRect))];
|
---|
150 | // [mScreenContent setImageAlignment: NSImageAlignCenter];
|
---|
151 | [mScreenContent setImageAlignment: NSImageAlignTop| NSImageAlignLeft];
|
---|
152 | [mScreenContent setImageScaling: NSScaleToFit];
|
---|
153 | [self addSubview: mScreenContent];
|
---|
154 | /* The state view */
|
---|
155 | mMonitorGlossy = [[NSImageView alloc] initWithFrame:NSRectFromCGRect (p->flipRect (p->mMonitorRect))];
|
---|
156 | [mMonitorGlossy setImage: darwinCGImageToNSImage (p->mDockMonitorGlossy)];
|
---|
157 | [self addSubview: mMonitorGlossy];
|
---|
158 | }
|
---|
159 |
|
---|
160 | return self;
|
---|
161 | }
|
---|
162 |
|
---|
163 | - (void)drawRect:(NSRect)aRect;
|
---|
164 | {
|
---|
165 | NSImage *dockMonitor = darwinCGImageToNSImage (p->mDockMonitor);
|
---|
166 | [dockMonitor drawInRect:NSRectFromCGRect (p->flipRect (p->mMonitorRect)) fromRect:aRect operation:NSCompositeSourceOver fraction:1.0];
|
---|
167 | [dockMonitor release];
|
---|
168 | }
|
---|
169 |
|
---|
170 | - (NSImageView*)screenContent
|
---|
171 | {
|
---|
172 | return mScreenContent;
|
---|
173 | }
|
---|
174 |
|
---|
175 | - (void)resize:(NSSize)size;
|
---|
176 | {
|
---|
177 | /* Calculate the new size based on the aspect ratio of the original screen
|
---|
178 | size */
|
---|
179 | float w, h;
|
---|
180 | if (size.width > size.height)
|
---|
181 | {
|
---|
182 | w = p->mUpdateRect.size.width;
|
---|
183 | h = ((float)size.height / size.width * p->mUpdateRect.size.height);
|
---|
184 | }
|
---|
185 | else
|
---|
186 | {
|
---|
187 | w = ((float)size.width / size.height * p->mUpdateRect.size.width);
|
---|
188 | h = p->mUpdateRect.size.height;
|
---|
189 | }
|
---|
190 | CGRect r = (p->flipRect (p->centerRectTo (CGRectMake (0, 0, (int)w, (int)h), p->mUpdateRect)));
|
---|
191 | r.origin.x = (int)r.origin.x;
|
---|
192 | r.origin.y = (int)r.origin.y;
|
---|
193 | r.size.width = (int)r.size.width;
|
---|
194 | r.size.height = (int)r.size.height;
|
---|
195 | // printf("gui %f %f %f %f\n", r.origin.x, r.origin.y, r.size.width, r.size.height);
|
---|
196 | /* Center within the update rect */
|
---|
197 | [mScreenContent setFrame:NSRectFromCGRect (r)];
|
---|
198 | }
|
---|
199 | @end
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * Simple implementation for the overlay of the OS & the state icon. Is used both
|
---|
203 | * in the application icon & preview mode.
|
---|
204 | */
|
---|
205 | @implementation DockTileOverlay
|
---|
206 | - (id)initWithFrame:(NSRect)frame parent:(CocoaDockIconPreviewPrivate*)parent
|
---|
207 | {
|
---|
208 | self = [super initWithFrame:frame];
|
---|
209 |
|
---|
210 | if (self != nil)
|
---|
211 | p = parent;
|
---|
212 |
|
---|
213 | return self;
|
---|
214 | }
|
---|
215 |
|
---|
216 | - (void)drawRect:(NSRect)aRect;
|
---|
217 | {
|
---|
218 | NSGraphicsContext *nsContext = [NSGraphicsContext currentContext];
|
---|
219 | CGContextRef pCGContext = (CGContextRef)[nsContext graphicsPort];
|
---|
220 | p->drawOverlayIcons (pCGContext);
|
---|
221 | }
|
---|
222 | @end
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * VirtualBox Dock Tile implementation. Manage the switching between the icon
|
---|
226 | * and preview mode & forwards all update request to the appropriate methods.
|
---|
227 | */
|
---|
228 | @implementation DockTile
|
---|
229 | - (id)initWithParent:(CocoaDockIconPreviewPrivate*)parent
|
---|
230 | {
|
---|
231 | self = [super init];
|
---|
232 |
|
---|
233 | if (self != nil)
|
---|
234 | {
|
---|
235 | p = parent;
|
---|
236 | /* Add self as the content view of the dock tile */
|
---|
237 | NSDockTile *dock = [[NSApplication sharedApplication] dockTile];
|
---|
238 | [dock setContentView: self];
|
---|
239 | /* App icon is default */
|
---|
240 | [self restoreAppIcon];
|
---|
241 | /* The overlay */
|
---|
242 | mOverlay = [[DockTileOverlay alloc] initWithFrame:NSRectFromCGRect(p->flipRect (p->mDockIconRect)) parent:p];
|
---|
243 | [self addSubview: mOverlay];
|
---|
244 | }
|
---|
245 |
|
---|
246 | return self;
|
---|
247 | }
|
---|
248 |
|
---|
249 | - (NSView*)screenContent
|
---|
250 | {
|
---|
251 | return [mMonitor screenContent];
|
---|
252 | }
|
---|
253 |
|
---|
254 | - (void)cleanup
|
---|
255 | {
|
---|
256 | if (mAppIcon != nil)
|
---|
257 | {
|
---|
258 | [mAppIcon removeFromSuperview];
|
---|
259 | [mAppIcon release];
|
---|
260 | mAppIcon = nil;
|
---|
261 | }
|
---|
262 | if (mMonitor != nil)
|
---|
263 | {
|
---|
264 | [mMonitor removeFromSuperview];
|
---|
265 | [mMonitor release];
|
---|
266 | mMonitor = nil;
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | - (void)restoreAppIcon
|
---|
271 | {
|
---|
272 | if (mAppIcon == nil)
|
---|
273 | {
|
---|
274 | [self cleanup];
|
---|
275 | mAppIcon = [[NSImageView alloc] initWithFrame:NSRectFromCGRect (p->flipRect (p->mDockIconRect))];
|
---|
276 | [mAppIcon setImage: [NSImage imageNamed:@"NSApplicationIcon"]];
|
---|
277 | [self addSubview: mAppIcon positioned:NSWindowBelow relativeTo:mOverlay];
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | - (void)updateAppIcon
|
---|
282 | {
|
---|
283 | [self restoreAppIcon];
|
---|
284 | [[[NSApplication sharedApplication] dockTile] display];
|
---|
285 | }
|
---|
286 |
|
---|
287 | - (void)restoreMonitor
|
---|
288 | {
|
---|
289 | if (mMonitor == nil)
|
---|
290 | {
|
---|
291 | p->initPreviewImages();
|
---|
292 | [self cleanup];
|
---|
293 | mMonitor = [[DockTileMonitor alloc] initWithFrame:NSRectFromCGRect (p->flipRect (p->mDockIconRect)) parent:p];
|
---|
294 | [self addSubview: mMonitor positioned:NSWindowBelow relativeTo:mOverlay];
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | - (void)updateMonitorWithImage:(CGImageRef)image
|
---|
299 | {
|
---|
300 | [self restoreMonitor];
|
---|
301 | NSImage *nsimage = darwinCGImageToNSImage (image);
|
---|
302 | [[mMonitor screenContent] setImage: nsimage];
|
---|
303 | [nsimage release];
|
---|
304 | [[[NSApplication sharedApplication] dockTile] display];
|
---|
305 | }
|
---|
306 |
|
---|
307 | - (void)resizeMonitor:(NSSize)size;
|
---|
308 | {
|
---|
309 | [mMonitor resize:size];
|
---|
310 | }
|
---|
311 | @end
|
---|
312 |
|
---|