OLD | NEW |
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> | 7 #include <objidl.h> |
8 #include <shlobj.h> | 8 #include <shlobj.h> |
9 #include <shobjidl.h> | 9 #include <shobjidl.h> |
10 | 10 |
11 #include "app/gfx/canvas.h" | 11 #include "app/gfx/canvas.h" |
12 #include "app/gfx/font.h" | |
13 #include "app/l10n_util.h" | |
14 #include "app/os_exchange_data.h" | 12 #include "app/os_exchange_data.h" |
15 #include "app/resource_bundle.h" | |
16 #include "base/file_util.h" | |
17 #include "base/gfx/gdi_util.h" | 13 #include "base/gfx/gdi_util.h" |
18 #include "base/gfx/point.h" | |
19 #include "base/string_util.h" | |
20 #include "googleurl/src/gurl.h" | |
21 #include "grit/app_resources.h" | |
22 #include "views/controls/button/text_button.h" | |
23 | 14 |
24 namespace drag_utils { | 15 namespace drag_utils { |
25 | 16 |
26 // Maximum width of the link drag image in pixels. | |
27 static const int kLinkDragImageMaxWidth = 200; | |
28 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 | |
35 // File dragging pixel measurements | |
36 static const int kFileDragImageMaxWidth = 200; | |
37 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; | |
38 | |
39 static void SetDragImageOnDataObject(HBITMAP hbitmap, | 17 static void SetDragImageOnDataObject(HBITMAP hbitmap, |
40 int width, | 18 int width, |
41 int height, | 19 int height, |
42 int cursor_offset_x, | 20 int cursor_offset_x, |
43 int cursor_offset_y, | 21 int cursor_offset_y, |
44 IDataObject* data_object) { | 22 IDataObject* data_object) { |
45 IDragSourceHelper* helper = NULL; | 23 IDragSourceHelper* helper = NULL; |
46 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, | 24 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, |
47 IID_IDragSourceHelper, reinterpret_cast<LPVOID*>(&helper)); | 25 IID_IDragSourceHelper, reinterpret_cast<LPVOID*>(&helper)); |
48 if (SUCCEEDED(rv)) { | 26 if (SUCCEEDED(rv)) { |
(...skipping 24 matching lines...) Expand all Loading... |
73 HGDIOBJ old_object = SelectObject(compatible_dc, bitmap); | 51 HGDIOBJ old_object = SelectObject(compatible_dc, bitmap); |
74 BitBlt(compatible_dc, 0, 0, width, height, | 52 BitBlt(compatible_dc, 0, 0, width, height, |
75 canvas.getTopPlatformDevice().getBitmapDC(), | 53 canvas.getTopPlatformDevice().getBitmapDC(), |
76 0, 0, SRCCOPY); | 54 0, 0, SRCCOPY); |
77 SelectObject(compatible_dc, old_object); | 55 SelectObject(compatible_dc, old_object); |
78 ReleaseDC(NULL, compatible_dc); | 56 ReleaseDC(NULL, compatible_dc); |
79 ReleaseDC(NULL, screen_dc); | 57 ReleaseDC(NULL, screen_dc); |
80 return bitmap; | 58 return bitmap; |
81 } | 59 } |
82 | 60 |
83 void SetURLAndDragImage(const GURL& url, | |
84 const std::wstring& title, | |
85 const SkBitmap& icon, | |
86 OSExchangeData* data) { | |
87 DCHECK(url.is_valid() && data); | |
88 | |
89 data->SetURL(url, title); | |
90 | |
91 // Create a button to render the drag image for us. | |
92 views::TextButton button(NULL, | |
93 title.empty() ? UTF8ToWide(url.spec()) : title); | |
94 button.set_max_width(kLinkDragImageMaxWidth); | |
95 if (icon.isNull()) { | |
96 button.SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
97 IDR_DEFAULT_FAVICON)); | |
98 } else { | |
99 button.SetIcon(icon); | |
100 } | |
101 gfx::Size prefsize = button.GetPreferredSize(); | |
102 button.SetBounds(0, 0, prefsize.width(), prefsize.height()); | |
103 | |
104 // Render the image. | |
105 gfx::Canvas canvas(prefsize.width(), prefsize.height(), false); | |
106 button.Paint(&canvas, true); | |
107 SetDragImageOnDataObject(canvas, prefsize.width(), prefsize.height(), | |
108 prefsize.width() / 2, prefsize.height() / 2, | |
109 data); | |
110 } | |
111 | |
112 void CreateDragImageForFile(const std::wstring& file_name, | |
113 SkBitmap* icon, | |
114 IDataObject* data_object) { | |
115 DCHECK(icon); | |
116 DCHECK(data_object); | |
117 | |
118 // Set up our text portion | |
119 const std::wstring& name = file_util::GetFilenameFromPath(file_name); | |
120 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
121 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); | |
122 | |
123 const int width = kFileDragImageMaxWidth; | |
124 // Add +2 here to allow room for the halo. | |
125 const int height = font.height() + icon->height() + | |
126 kLinkDragImageVPadding + 2; | |
127 gfx::Canvas canvas(width, height, false /* translucent */); | |
128 | |
129 // Paint the icon. | |
130 canvas.DrawBitmapInt(*icon, (width - icon->width()) / 2, 0); | |
131 | |
132 // Paint the file name. We inset it one pixel to allow room for the halo. | |
133 canvas.DrawStringWithHalo(name, font, kFileDragImageTextColor, SK_ColorWHITE, | |
134 1, icon->height() + kLinkDragImageVPadding + 1, | |
135 width - 2, font.height(), | |
136 gfx::Canvas::TEXT_ALIGN_CENTER); | |
137 | |
138 SetDragImageOnDataObject(canvas, width, height, width / 2, | |
139 kLinkDragImageVPadding, data_object); | |
140 } | |
141 | |
142 void SetDragImageOnDataObject(const gfx::Canvas& canvas, | 61 void SetDragImageOnDataObject(const gfx::Canvas& canvas, |
143 int width, | 62 int width, |
144 int height, | 63 int height, |
145 int cursor_x_offset, | 64 int cursor_x_offset, |
146 int cursor_y_offset, | 65 int cursor_y_offset, |
147 IDataObject* data_object) { | 66 OSExchangeData* data_object) { |
148 DCHECK(data_object && width > 0 && height > 0); | 67 DCHECK(data_object && width > 0 && height > 0); |
149 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. | 68 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. |
150 HBITMAP bitmap = CreateBitmapFromCanvas(canvas, width, height); | 69 HBITMAP bitmap = CreateBitmapFromCanvas(canvas, width, height); |
151 | 70 |
152 // Attach 'bitmap' to the data_object. | 71 // Attach 'bitmap' to the data_object. |
153 SetDragImageOnDataObject(bitmap, width, height, | 72 SetDragImageOnDataObject(bitmap, width, height, |
154 cursor_x_offset, | 73 cursor_x_offset, |
155 cursor_y_offset, | 74 cursor_y_offset, |
156 data_object); | 75 data_object); |
157 } | 76 } |
158 | 77 |
159 } // namespace drag_utils | 78 } // namespace drag_utils |
OLD | NEW |