~ubuntu-branches/ubuntu/raring/virtualbox-ose/raring

« back to all changes in this revision

Viewing changes to src/VBox/Frontends/VirtualBox/src/platform/darwin/UICocoaDockIconPreview.mm

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-01-30 23:27:25 UTC
  • mfrom: (0.3.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20110130232725-2ouajjd2ggdet0zd
Tags: 4.0.2-dfsg-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add Apport hook.
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Drop *-source packages.
* Drop ubuntu-01-fix-build-gcc45.patch, fixed upstream.
* Drop ubuntu-02-as-needed.patch, added to the Debian package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: UICocoaDockIconPreview.mm 30151 2010-06-10 16:12:33Z 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-2010 Oracle Corporation
 
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
 
 
20
/* VBox includes */
 
21
#include "UICocoaDockIconPreview.h"
 
22
#include "VBoxCocoaHelper.h"
 
23
 
 
24
/* System includes */
 
25
#import <Cocoa/Cocoa.h>
 
26
 
 
27
@interface UIDockTileMonitor: NSView
 
28
{
 
29
    UICocoaDockIconPreviewPrivate *p;
 
30
 
 
31
    NSImageView *mScreenContent;
 
32
    NSImageView *mMonitorGlossy;
 
33
}
 
34
- (id)initWithFrame:(NSRect)frame parent:(UICocoaDockIconPreviewPrivate*)parent;
 
35
- (NSImageView*)screenContent;
 
36
- (void)resize:(NSSize)size;
 
37
@end
 
38
 
 
39
@interface UIDockTileOverlay: NSView
 
40
{
 
41
    UICocoaDockIconPreviewPrivate *p;
 
42
}
 
43
- (id)initWithFrame:(NSRect)frame parent:(UICocoaDockIconPreviewPrivate*)parent;
 
44
@end
 
45
 
 
46
@interface UIDockTile: NSView
 
47
{
 
48
    UICocoaDockIconPreviewPrivate *p;
 
49
 
 
50
    UIDockTileMonitor *mMonitor;
 
51
    NSImageView       *mAppIcon;
 
52
 
 
53
    UIDockTileOverlay *mOverlay;
 
54
}
 
55
- (id)initWithParent:(UICocoaDockIconPreviewPrivate*)parent;
 
56
- (void)destroy;
 
57
- (NSView*)screenContentWithParentView:(NSView*)parentView;
 
58
- (void)cleanup;
 
59
- (void)restoreAppIcon;
 
60
- (void)updateAppIcon;
 
61
- (void)restoreMonitor;
 
62
- (void)updateMonitorWithImage:(CGImageRef)image;
 
63
- (void)resizeMonitor:(NSSize)size;
 
64
@end
 
65
 
 
66
/*
 
67
 * Helper class which allow us to access all members/methods of AbstractDockIconPreviewHelper
 
68
 * from any Cocoa class.
 
69
 */
 
70
class UICocoaDockIconPreviewPrivate: public UIAbstractDockIconPreviewHelper
 
71
{
 
72
public:
 
73
    inline UICocoaDockIconPreviewPrivate(UISession *pSession, const QPixmap& overlayImage)
 
74
      :UIAbstractDockIconPreviewHelper(pSession, overlayImage)
 
75
    {
 
76
        mUIDockTile = [[UIDockTile alloc] initWithParent:this];
 
77
    }
 
78
 
 
79
    inline ~UICocoaDockIconPreviewPrivate()
 
80
    {
 
81
 
 
82
        [mUIDockTile destroy];
 
83
        [mUIDockTile release];
 
84
    }
 
85
 
 
86
    UIDockTile *mUIDockTile;
 
87
};
 
88
 
 
89
/*
 
90
 * Cocoa wrapper for the abstract dock icon preview class
 
91
 */
 
92
UICocoaDockIconPreview::UICocoaDockIconPreview(UISession *pSession, const QPixmap& overlayImage)
 
93
  : UIAbstractDockIconPreview(pSession, overlayImage)
 
94
{
 
95
    CocoaAutoreleasePool pool;
 
96
 
 
97
    d = new UICocoaDockIconPreviewPrivate(pSession, overlayImage);
 
98
}
 
99
 
 
100
UICocoaDockIconPreview::~UICocoaDockIconPreview()
 
101
{
 
102
    CocoaAutoreleasePool pool;
 
103
 
 
104
    delete d;
 
105
}
 
106
 
 
107
void UICocoaDockIconPreview::updateDockOverlay()
 
108
{
 
109
    CocoaAutoreleasePool pool;
 
110
 
 
111
    [d->mUIDockTile updateAppIcon];
 
112
}
 
113
 
 
114
void UICocoaDockIconPreview::updateDockPreview(CGImageRef VMImage)
 
115
{
 
116
    CocoaAutoreleasePool pool;
 
117
 
 
118
    [d->mUIDockTile updateMonitorWithImage:VMImage];
 
119
}
 
120
 
 
121
void UICocoaDockIconPreview::updateDockPreview(UIFrameBuffer *pFrameBuffer)
 
122
{
 
123
    CocoaAutoreleasePool pool;
 
124
 
 
125
    UIAbstractDockIconPreview::updateDockPreview(pFrameBuffer);
 
126
}
 
127
 
 
128
void UICocoaDockIconPreview::setOriginalSize(int width, int height)
 
129
{
 
130
    CocoaAutoreleasePool pool;
 
131
 
 
132
    [d->mUIDockTile resizeMonitor:NSMakeSize(width, height)];
 
133
}
 
134
 
 
135
/*
 
136
 * Class for arranging/updating the layers for the glossy monitor preview.
 
137
 */
 
138
@implementation UIDockTileMonitor;
 
139
- (id)initWithFrame:(NSRect)frame parent:(UICocoaDockIconPreviewPrivate*)parent
 
140
{
 
141
    self = [super initWithFrame:frame];
 
142
 
 
143
    if (self != nil)
 
144
    {
 
145
        p = parent;
 
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];
 
152
        /* The state view */
 
153
        mMonitorGlossy = [[NSImageView alloc] initWithFrame:NSRectFromCGRect(p->flipRect(p->m_monitorRect))];
 
154
        [mMonitorGlossy setImage: ::darwinToNSImageRef(p->m_dockMonitorGlossy)];
 
155
        [self addSubview: mMonitorGlossy];
 
156
    }
 
157
 
 
158
    return self;
 
159
}
 
160
 
 
161
- (void)drawRect:(NSRect)aRect;
 
162
{
 
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];
 
166
}
 
167
 
 
168
- (NSImageView*)screenContent
 
169
{
 
170
    return mScreenContent;
 
171
}
 
172
 
 
173
- (void)resize:(NSSize)size;
 
174
{
 
175
    /* Calculate the new size based on the aspect ratio of the original screen
 
176
       size */
 
177
    float w, h;
 
178
    if (size.width > size.height)
 
179
    {
 
180
        w = p->m_updateRect.size.width;
 
181
        h = ((float)size.height / size.width * p->m_updateRect.size.height);
 
182
    }
 
183
    else
 
184
    {
 
185
        w = ((float)size.width / size.height * p->m_updateRect.size.width);
 
186
        h = p->m_updateRect.size.height;
 
187
    }
 
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)];
 
196
}
 
197
@end
 
198
 
 
199
/*
 
200
 * Simple implementation for the overlay of the OS & the state icon. Is used both
 
201
 * in the application icon & preview mode.
 
202
 */
 
203
@implementation UIDockTileOverlay
 
204
- (id)initWithFrame:(NSRect)frame parent:(UICocoaDockIconPreviewPrivate*)parent
 
205
{
 
206
    self = [super initWithFrame:frame];
 
207
 
 
208
    if (self != nil)
 
209
        p = parent;
 
210
 
 
211
    return self;
 
212
}
 
213
 
 
214
- (void)drawRect:(NSRect)aRect;
 
215
{
 
216
    NSGraphicsContext *nsContext = [NSGraphicsContext currentContext];
 
217
    CGContextRef pCGContext = (CGContextRef)[nsContext graphicsPort];
 
218
    p->drawOverlayIcons (pCGContext);
 
219
}
 
220
@end
 
221
 
 
222
/*
 
223
 * VirtualBox Dock Tile implementation. Manage the switching between the icon
 
224
 * and preview mode & forwards all update request to the appropriate methods.
 
225
 */
 
226
@implementation UIDockTile
 
227
- (id)initWithParent:(UICocoaDockIconPreviewPrivate*)parent
 
228
{
 
229
    self = [super init];
 
230
 
 
231
    if (self != nil)
 
232
    {
 
233
        p = 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];
 
239
        /* The overlay */
 
240
        mOverlay = [[UIDockTileOverlay alloc] initWithFrame:NSRectFromCGRect(p->flipRect (p->m_dockIconRect)) parent:p];
 
241
        [self addSubview: mOverlay];
 
242
    }
 
243
 
 
244
    return self;
 
245
}
 
246
 
 
247
- (void)destroy
 
248
{
 
249
    /* Remove all content from the application dock tile. */
 
250
    [mOverlay removeFromSuperview];
 
251
    [mOverlay release];
 
252
    mOverlay = nil;
 
253
    NSDockTile *dock = [[NSApplication sharedApplication] dockTile];
 
254
    [dock setContentView: nil];
 
255
    /* Cleanup all other resources */
 
256
    [self cleanup];
 
257
}
 
258
 
 
259
- (NSView*)screenContentWithParentView:(NSView*)parentView
 
260
{
 
261
    if (mMonitor != nil)
 
262
    {
 
263
        void *pId = p->currentPreviewWindowId();
 
264
        if (parentView == pId)
 
265
            return [mMonitor screenContent];
 
266
    }
 
267
    return nil;
 
268
}
 
269
 
 
270
- (void)cleanup
 
271
{
 
272
    if (mAppIcon != nil)
 
273
    {
 
274
        [mAppIcon removeFromSuperview];
 
275
        [mAppIcon release];
 
276
        mAppIcon = nil;
 
277
    }
 
278
    if (mMonitor != nil)
 
279
    {
 
280
        [mMonitor removeFromSuperview];
 
281
        [mMonitor release];
 
282
        mMonitor = nil;
 
283
    }
 
284
}
 
285
 
 
286
- (void)restoreAppIcon
 
287
{
 
288
    if (mAppIcon == nil)
 
289
    {
 
290
        [self cleanup];
 
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];
 
294
    }
 
295
}
 
296
 
 
297
- (void)updateAppIcon
 
298
{
 
299
    [self restoreAppIcon];
 
300
    [[[NSApplication sharedApplication] dockTile] display];
 
301
}
 
302
 
 
303
- (void)restoreMonitor
 
304
{
 
305
    if (mMonitor == nil)
 
306
    {
 
307
        p->initPreviewImages();
 
308
        [self cleanup];
 
309
        mMonitor = [[UIDockTileMonitor alloc] initWithFrame:NSRectFromCGRect (p->flipRect (p->m_dockIconRect)) parent:p];
 
310
        [self addSubview: mMonitor positioned:NSWindowBelow relativeTo:mOverlay];
 
311
    }
 
312
}
 
313
 
 
314
- (void)updateMonitorWithImage:(CGImageRef)image
 
315
{
 
316
    [self restoreMonitor];
 
317
    NSImage *nsimage = ::darwinToNSImageRef(image);
 
318
    [[mMonitor screenContent] setImage: nsimage];
 
319
    [nsimage release];
 
320
    [[[NSApplication sharedApplication] dockTile] display];
 
321
}
 
322
 
 
323
- (void)resizeMonitor:(NSSize)size;
 
324
{
 
325
    [self restoreMonitor];
 
326
    [mMonitor resize:size];
 
327
}
 
328
@end
 
329