Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: views/drag_utils.cc

Issue 113954: Refactors drag_utils. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "views/drag_utils.h" 5 #include "views/drag_utils.h"
6 6
7 #include <objidl.h>
8 #include <shlobj.h>
9 #include <shobjidl.h>
10
11 #include "app/gfx/canvas.h" 7 #include "app/gfx/canvas.h"
12 #include "app/gfx/font.h" 8 #include "app/gfx/font.h"
13 #include "app/l10n_util.h" 9 #include "app/l10n_util.h"
14 #include "app/os_exchange_data.h" 10 #include "app/os_exchange_data.h"
15 #include "app/resource_bundle.h" 11 #include "app/resource_bundle.h"
16 #include "base/file_util.h" 12 #include "base/file_util.h"
17 #include "base/gfx/gdi_util.h" 13 #include "base/logging.h"
18 #include "base/gfx/point.h"
19 #include "base/string_util.h" 14 #include "base/string_util.h"
20 #include "googleurl/src/gurl.h" 15 #include "googleurl/src/gurl.h"
21 #include "grit/app_resources.h" 16 #include "grit/app_resources.h"
22 #include "views/controls/button/text_button.h" 17 #include "views/controls/button/text_button.h"
23 18
24 namespace drag_utils { 19 namespace drag_utils {
25 20
26 // Maximum width of the link drag image in pixels. 21 // Maximum width of the link drag image in pixels.
27 static const int kLinkDragImageMaxWidth = 200; 22 static const int kLinkDragImageMaxWidth = 200;
28 static const int kLinkDragImageVPadding = 3; 23 static const int kLinkDragImageVPadding = 3;
29 static const int kLinkDragImageVSpacing = 2;
30 static const int kLinkDragImageHPadding = 4;
31 static const SkColor kLinkDragImageBGColor = SkColorSetRGB(131, 146, 171);
32 //static const SkColor kLinkDragImageBGColor = SkColorSetRGB(195, 217, 255);
33 static const SkColor kLinkDragImageTextColor = SK_ColorBLACK;
34 24
35 // File dragging pixel measurements 25 // File dragging pixel measurements
36 static const int kFileDragImageMaxWidth = 200; 26 static const int kFileDragImageMaxWidth = 200;
37 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; 27 static const SkColor kFileDragImageTextColor = SK_ColorBLACK;
38 28
39 static void SetDragImageOnDataObject(HBITMAP hbitmap,
40 int width,
41 int height,
42 int cursor_offset_x,
43 int cursor_offset_y,
44 IDataObject* data_object) {
45 IDragSourceHelper* helper = NULL;
46 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER,
47 IID_IDragSourceHelper, reinterpret_cast<LPVOID*>(&helper));
48 if (SUCCEEDED(rv)) {
49 SHDRAGIMAGE sdi;
50 sdi.sizeDragImage.cx = width;
51 sdi.sizeDragImage.cy = height;
52 sdi.crColorKey = 0xFFFFFFFF;
53 sdi.hbmpDragImage = hbitmap;
54 sdi.ptOffset.x = cursor_offset_x;
55 sdi.ptOffset.y = cursor_offset_y;
56 helper->InitializeFromBitmap(&sdi, data_object);
57 }
58 };
59
60 // Blit the contents of the canvas to a new HBITMAP. It is the caller's
61 // responsibility to release the |bits| buffer.
62 static HBITMAP CreateBitmapFromCanvas(const gfx::Canvas& canvas,
63 int width,
64 int height) {
65 HDC screen_dc = GetDC(NULL);
66 BITMAPINFOHEADER header;
67 gfx::CreateBitmapHeader(width, height, &header);
68 void* bits;
69 HBITMAP bitmap =
70 CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&header),
71 DIB_RGB_COLORS, &bits, NULL, 0);
72 HDC compatible_dc = CreateCompatibleDC(screen_dc);
73 HGDIOBJ old_object = SelectObject(compatible_dc, bitmap);
74 BitBlt(compatible_dc, 0, 0, width, height,
75 canvas.getTopPlatformDevice().getBitmapDC(),
76 0, 0, SRCCOPY);
77 SelectObject(compatible_dc, old_object);
78 ReleaseDC(NULL, compatible_dc);
79 ReleaseDC(NULL, screen_dc);
80 return bitmap;
81 }
82
83 void SetURLAndDragImage(const GURL& url, 29 void SetURLAndDragImage(const GURL& url,
84 const std::wstring& title, 30 const std::wstring& title,
85 const SkBitmap& icon, 31 const SkBitmap& icon,
86 OSExchangeData* data) { 32 OSExchangeData* data) {
87 DCHECK(url.is_valid() && data); 33 DCHECK(url.is_valid() && data);
88 34
89 data->SetURL(url, title); 35 data->SetURL(url, title);
90 36
91 // Create a button to render the drag image for us. 37 // Create a button to render the drag image for us.
92 views::TextButton button(NULL, 38 views::TextButton button(NULL,
(...skipping 11 matching lines...) Expand all
104 // Render the image. 50 // Render the image.
105 gfx::Canvas canvas(prefsize.width(), prefsize.height(), false); 51 gfx::Canvas canvas(prefsize.width(), prefsize.height(), false);
106 button.Paint(&canvas, true); 52 button.Paint(&canvas, true);
107 SetDragImageOnDataObject(canvas, prefsize.width(), prefsize.height(), 53 SetDragImageOnDataObject(canvas, prefsize.width(), prefsize.height(),
108 prefsize.width() / 2, prefsize.height() / 2, 54 prefsize.width() / 2, prefsize.height() / 2,
109 data); 55 data);
110 } 56 }
111 57
112 void CreateDragImageForFile(const std::wstring& file_name, 58 void CreateDragImageForFile(const std::wstring& file_name,
113 SkBitmap* icon, 59 SkBitmap* icon,
114 IDataObject* data_object) { 60 OSExchangeData* data_object) {
115 DCHECK(icon); 61 DCHECK(icon);
116 DCHECK(data_object); 62 DCHECK(data_object);
117 63
118 // Set up our text portion 64 // Set up our text portion
119 const std::wstring& name = file_util::GetFilenameFromPath(file_name);
120 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 65 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
121 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); 66 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont);
122 67
123 const int width = kFileDragImageMaxWidth; 68 const int width = kFileDragImageMaxWidth;
124 // Add +2 here to allow room for the halo. 69 // Add +2 here to allow room for the halo.
125 const int height = font.height() + icon->height() + 70 const int height = font.height() + icon->height() +
126 kLinkDragImageVPadding + 2; 71 kLinkDragImageVPadding + 2;
127 gfx::Canvas canvas(width, height, false /* translucent */); 72 gfx::Canvas canvas(width, height, false /* translucent */);
128 73
129 // Paint the icon. 74 // Paint the icon.
130 canvas.DrawBitmapInt(*icon, (width - icon->width()) / 2, 0); 75 canvas.DrawBitmapInt(*icon, (width - icon->width()) / 2, 0);
131 76
132 // Paint the file name. We inset it one pixel to allow room for the halo. 77 // Paint the file name. We inset it one pixel to allow room for the halo.
78 #if defined(OS_WIN)
79 const std::wstring& name = file_util::GetFilenameFromPath(file_name);
133 canvas.DrawStringWithHalo(name, font, kFileDragImageTextColor, SK_ColorWHITE, 80 canvas.DrawStringWithHalo(name, font, kFileDragImageTextColor, SK_ColorWHITE,
134 1, icon->height() + kLinkDragImageVPadding + 1, 81 1, icon->height() + kLinkDragImageVPadding + 1,
135 width - 2, font.height(), 82 width - 2, font.height(),
136 gfx::Canvas::TEXT_ALIGN_CENTER); 83 gfx::Canvas::TEXT_ALIGN_CENTER);
84 #else
85 NOTIMPLEMENTED();
86 #endif
137 87
138 SetDragImageOnDataObject(canvas, width, height, width / 2, 88 SetDragImageOnDataObject(canvas, width, height, width / 2,
139 kLinkDragImageVPadding, data_object); 89 kLinkDragImageVPadding, data_object);
140 } 90 }
141 91
142 void SetDragImageOnDataObject(const gfx::Canvas& canvas,
143 int width,
144 int height,
145 int cursor_x_offset,
146 int cursor_y_offset,
147 IDataObject* data_object) {
148 DCHECK(data_object && width > 0 && height > 0);
149 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap.
150 HBITMAP bitmap = CreateBitmapFromCanvas(canvas, width, height);
151
152 // Attach 'bitmap' to the data_object.
153 SetDragImageOnDataObject(bitmap, width, height,
154 cursor_x_offset,
155 cursor_y_offset,
156 data_object);
157 }
158
159 } // namespace drag_utils 92 } // namespace drag_utils
OLDNEW
« no previous file with comments | « views/drag_utils.h ('k') | views/drag_utils_gtk.cc » ('j') | views/drag_utils_gtk.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698