1
/* $Id: UICocoaDockIconPreview.mm 30151 2010-06-10 16:12:33Z vboxsync $ */
4
* VBox frontends: Qt GUI ("VirtualBox"):
5
* Cocoa helper for the dock icon preview
9
* Copyright (C) 2009-2010 Oracle Corporation
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.
21
#include "UICocoaDockIconPreview.h"
22
#include "VBoxCocoaHelper.h"
25
#import <Cocoa/Cocoa.h>
27
@interface UIDockTileMonitor: NSView
29
UICocoaDockIconPreviewPrivate *p;
31
NSImageView *mScreenContent;
32
NSImageView *mMonitorGlossy;
34
- (id)initWithFrame:(NSRect)frame parent:(UICocoaDockIconPreviewPrivate*)parent;
35
- (NSImageView*)screenContent;
36
- (void)resize:(NSSize)size;
39
@interface UIDockTileOverlay: NSView
41
UICocoaDockIconPreviewPrivate *p;
43
- (id)initWithFrame:(NSRect)frame parent:(UICocoaDockIconPreviewPrivate*)parent;
46
@interface UIDockTile: NSView
48
UICocoaDockIconPreviewPrivate *p;
50
UIDockTileMonitor *mMonitor;
51
NSImageView *mAppIcon;
53
UIDockTileOverlay *mOverlay;
55
- (id)initWithParent:(UICocoaDockIconPreviewPrivate*)parent;
57
- (NSView*)screenContentWithParentView:(NSView*)parentView;
59
- (void)restoreAppIcon;
60
- (void)updateAppIcon;
61
- (void)restoreMonitor;
62
- (void)updateMonitorWithImage:(CGImageRef)image;
63
- (void)resizeMonitor:(NSSize)size;
67
* Helper class which allow us to access all members/methods of AbstractDockIconPreviewHelper
68
* from any Cocoa class.
70
class UICocoaDockIconPreviewPrivate: public UIAbstractDockIconPreviewHelper
73
inline UICocoaDockIconPreviewPrivate(UISession *pSession, const QPixmap& overlayImage)
74
:UIAbstractDockIconPreviewHelper(pSession, overlayImage)
76
mUIDockTile = [[UIDockTile alloc] initWithParent:this];
79
inline ~UICocoaDockIconPreviewPrivate()
82
[mUIDockTile destroy];
83
[mUIDockTile release];
86
UIDockTile *mUIDockTile;
90
* Cocoa wrapper for the abstract dock icon preview class
92
UICocoaDockIconPreview::UICocoaDockIconPreview(UISession *pSession, const QPixmap& overlayImage)
93
: UIAbstractDockIconPreview(pSession, overlayImage)
95
CocoaAutoreleasePool pool;
97
d = new UICocoaDockIconPreviewPrivate(pSession, overlayImage);
100
UICocoaDockIconPreview::~UICocoaDockIconPreview()
102
CocoaAutoreleasePool pool;
107
void UICocoaDockIconPreview::updateDockOverlay()
109
CocoaAutoreleasePool pool;
111
[d->mUIDockTile updateAppIcon];
114
void UICocoaDockIconPreview::updateDockPreview(CGImageRef VMImage)
116
CocoaAutoreleasePool pool;
118
[d->mUIDockTile updateMonitorWithImage:VMImage];
121
void UICocoaDockIconPreview::updateDockPreview(UIFrameBuffer *pFrameBuffer)
123
CocoaAutoreleasePool pool;
125
UIAbstractDockIconPreview::updateDockPreview(pFrameBuffer);
128
void UICocoaDockIconPreview::setOriginalSize(int width, int height)
130
CocoaAutoreleasePool pool;
132
[d->mUIDockTile resizeMonitor:NSMakeSize(width, height)];
136
* Class for arranging/updating the layers for the glossy monitor preview.
138
@implementation UIDockTileMonitor;
139
- (id)initWithFrame:(NSRect)frame parent:(UICocoaDockIconPreviewPrivate*)parent
141
self = [super initWithFrame:frame];
146
/* The screen content view */
147
mScreenContent = [[NSImageView alloc] initWithFrame:NSRectFromCGRect(p->flipRect(p->m_updateRect))];
148
// [mScreenContent setImageAlignment: NSImageAlignCenter];
149
[mScreenContent setImageAlignment: NSImageAlignTop| NSImageAlignLeft];
150
[mScreenContent setImageScaling: NSScaleToFit];
151
[self addSubview: mScreenContent];
153
mMonitorGlossy = [[NSImageView alloc] initWithFrame:NSRectFromCGRect(p->flipRect(p->m_monitorRect))];
154
[mMonitorGlossy setImage: ::darwinToNSImageRef(p->m_dockMonitorGlossy)];
155
[self addSubview: mMonitorGlossy];
161
- (void)drawRect:(NSRect)aRect;
163
NSImage *dockMonitor = ::darwinToNSImageRef(p->m_dockMonitor);
164
[dockMonitor drawInRect:NSRectFromCGRect(p->flipRect(p->m_monitorRect)) fromRect:aRect operation:NSCompositeSourceOver fraction:1.0];
165
[dockMonitor release];
168
- (NSImageView*)screenContent
170
return mScreenContent;
173
- (void)resize:(NSSize)size;
175
/* Calculate the new size based on the aspect ratio of the original screen
178
if (size.width > size.height)
180
w = p->m_updateRect.size.width;
181
h = ((float)size.height / size.width * p->m_updateRect.size.height);
185
w = ((float)size.width / size.height * p->m_updateRect.size.width);
186
h = p->m_updateRect.size.height;
188
CGRect r = (p->flipRect (p->centerRectTo (CGRectMake (0, 0, (int)w, (int)h), p->m_updateRect)));
189
r.origin.x = (int)r.origin.x;
190
r.origin.y = (int)r.origin.y;
191
r.size.width = (int)r.size.width;
192
r.size.height = (int)r.size.height;
193
// printf("gui %f %f %f %f\n", r.origin.x, r.origin.y, r.size.width, r.size.height);
194
/* Center within the update rect */
195
[mScreenContent setFrame:NSRectFromCGRect (r)];
200
* Simple implementation for the overlay of the OS & the state icon. Is used both
201
* in the application icon & preview mode.
203
@implementation UIDockTileOverlay
204
- (id)initWithFrame:(NSRect)frame parent:(UICocoaDockIconPreviewPrivate*)parent
206
self = [super initWithFrame:frame];
214
- (void)drawRect:(NSRect)aRect;
216
NSGraphicsContext *nsContext = [NSGraphicsContext currentContext];
217
CGContextRef pCGContext = (CGContextRef)[nsContext graphicsPort];
218
p->drawOverlayIcons (pCGContext);
223
* VirtualBox Dock Tile implementation. Manage the switching between the icon
224
* and preview mode & forwards all update request to the appropriate methods.
226
@implementation UIDockTile
227
- (id)initWithParent:(UICocoaDockIconPreviewPrivate*)parent
234
/* Add self as the content view of the dock tile */
235
NSDockTile *dock = [[NSApplication sharedApplication] dockTile];
236
[dock setContentView: self];
237
/* App icon is default */
238
[self restoreAppIcon];
240
mOverlay = [[UIDockTileOverlay alloc] initWithFrame:NSRectFromCGRect(p->flipRect (p->m_dockIconRect)) parent:p];
241
[self addSubview: mOverlay];
249
/* Remove all content from the application dock tile. */
250
[mOverlay removeFromSuperview];
253
NSDockTile *dock = [[NSApplication sharedApplication] dockTile];
254
[dock setContentView: nil];
255
/* Cleanup all other resources */
259
- (NSView*)screenContentWithParentView:(NSView*)parentView
263
void *pId = p->currentPreviewWindowId();
264
if (parentView == pId)
265
return [mMonitor screenContent];
274
[mAppIcon removeFromSuperview];
280
[mMonitor removeFromSuperview];
286
- (void)restoreAppIcon
291
mAppIcon = [[NSImageView alloc] initWithFrame:NSRectFromCGRect (p->flipRect (p->m_dockIconRect))];
292
[mAppIcon setImage: [NSImage imageNamed:@"NSApplicationIcon"]];
293
[self addSubview: mAppIcon positioned:NSWindowBelow relativeTo:mOverlay];
297
- (void)updateAppIcon
299
[self restoreAppIcon];
300
[[[NSApplication sharedApplication] dockTile] display];
303
- (void)restoreMonitor
307
p->initPreviewImages();
309
mMonitor = [[UIDockTileMonitor alloc] initWithFrame:NSRectFromCGRect (p->flipRect (p->m_dockIconRect)) parent:p];
310
[self addSubview: mMonitor positioned:NSWindowBelow relativeTo:mOverlay];
314
- (void)updateMonitorWithImage:(CGImageRef)image
316
[self restoreMonitor];
317
NSImage *nsimage = ::darwinToNSImageRef(image);
318
[[mMonitor screenContent] setImage: nsimage];
320
[[[NSApplication sharedApplication] dockTile] display];
323
- (void)resizeMonitor:(NSSize)size;
325
[self restoreMonitor];
326
[mMonitor resize:size];