OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | 5 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
11 #include "base/memory/scoped_nsobject.h" | 11 #include "base/memory/scoped_nsobject.h" |
12 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
13 | 13 |
14 namespace browser { | 14 namespace browser { |
jonathan.backer
2011/11/14 13:45:02
Maybe move this below? No sense nesting an anonymo
| |
15 | 15 |
16 namespace { | |
16 gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window, | 17 gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window, |
17 std::vector<unsigned char>* png_representation) { | 18 std::vector<unsigned char>* png_representation, |
19 CGRect windowRect) { | |
sky
2011/11/14 16:23:09
const CGRect&
| |
18 png_representation->clear(); | 20 png_representation->clear(); |
19 | 21 |
20 // Make sure to grab the "window frame" view so we get current tab + | 22 // Make sure to grab the "window frame" view so we get current tab + |
21 // tabstrip. | 23 // tabstrip. |
22 NSView* view = [[window contentView] superview]; | 24 NSView* view = [[window contentView] superview]; |
23 base::mac::ScopedCFTypeRef<CGImageRef> windowSnapshot(CGWindowListCreateImage( | 25 base::mac::ScopedCFTypeRef<CGImageRef> windowSnapshot(CGWindowListCreateImage( |
24 CGRectNull, kCGWindowListOptionIncludingWindow, | 26 windowRect, kCGWindowListOptionIncludingWindow, |
25 [[view window] windowNumber], kCGWindowImageBoundsIgnoreFraming)); | 27 [[view window] windowNumber], kCGWindowImageBoundsIgnoreFraming)); |
26 if (CGImageGetWidth(windowSnapshot) <= 0) | 28 if (CGImageGetWidth(windowSnapshot) <= 0) |
27 return gfx::Rect(); | 29 return gfx::Rect(); |
28 | 30 |
29 scoped_nsobject<NSBitmapImageRep> rep( | 31 scoped_nsobject<NSBitmapImageRep> rep( |
30 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]); | 32 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]); |
31 NSData* data = [rep representationUsingType:NSPNGFileType properties:nil]; | 33 NSData* data = [rep representationUsingType:NSPNGFileType properties:nil]; |
32 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); | 34 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); |
33 NSUInteger length = [data length]; | 35 NSUInteger length = [data length]; |
34 if (buf == NULL || length == 0) | 36 if (buf == NULL || length == 0) |
35 return gfx::Rect(); | 37 return gfx::Rect(); |
36 | 38 |
37 png_representation->assign(buf, buf + length); | 39 png_representation->assign(buf, buf + length); |
38 DCHECK(!png_representation->empty()); | 40 DCHECK(!png_representation->empty()); |
39 | 41 |
40 return gfx::Rect(static_cast<int>([rep pixelsWide]), | 42 return gfx::Rect(static_cast<int>([rep pixelsWide]), |
41 static_cast<int>([rep pixelsHigh])); | 43 static_cast<int>([rep pixelsHigh])); |
42 } | 44 } |
43 | 45 |
46 } // namespace | |
47 | |
48 gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window, | |
49 std::vector<unsigned char>* png_representation) { | |
50 return GrabWindowSnapshot(window, png_representation, CGRectNull); | |
jonathan.backer
2011/11/14 13:45:02
Maybe document that CGRectNull means to grab the w
| |
51 } | |
52 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | |
jonathan.backer
2011/11/14 13:45:02
Bad merge?
| |
53 | |
54 gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window, | |
55 std::vector<unsigned char>* png_representation, | |
56 const gfx::Rect snapshot_bounds) { | |
57 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | |
58 gfx::Rect screen_bounds = gfx::Rect(NSRectToCGRect([screen frame])); | |
59 gfx::Rect window_bounds = gfx::Rect(NSRectToCGRect([window frame])); | |
60 | |
61 // Flip window coordinates based on the primary screen. | |
62 window_bounds.set_y( | |
63 screen_bounds.height() - window_bounds.y() - window_bounds.height()); | |
64 | |
65 // Convert snapshot bounds relative to window into bounds relative to | |
66 // screen. | |
67 gfx::Rect screen_snapshot_bounds = gfx::Rect( | |
68 window_bounds.origin().Add(snapshot_bounds.origin()), | |
69 snapshot_bounds.size()); | |
70 | |
71 DCHECK(screen_snapshot_bounds.right() <= window_bounds.right()); | |
72 DCHECK(screen_snapshot_bounds.bottom() <= window_bounds.bottom()); | |
73 | |
74 CGRect windowRect = screen_snapshot_bounds.ToCGRect(); | |
75 return GrabWindowSnapshot(window, png_representation, windowRect); | |
76 } | |
77 | |
44 } // namespace browser | 78 } // namespace browser |
OLD | NEW |