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 "ui/views/drag_utils.h" | 5 #include "ui/base/dragdrop/drag_utils.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
11 #include "grit/ui_resources.h" | |
12 #include "ui/base/dragdrop/os_exchange_data.h" | 11 #include "ui/base/dragdrop/os_exchange_data.h" |
13 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
14 #include "ui/gfx/canvas_skia.h" | 13 #include "ui/gfx/canvas_skia.h" |
15 #include "ui/gfx/font.h" | 14 #include "ui/gfx/font.h" |
16 #include "ui/views/controls/button/text_button.h" | 15 #include "ui/gfx/point.h" |
| 16 #include "ui/gfx/size.h" |
17 | 17 |
18 namespace drag_utils { | 18 namespace drag_utils { |
19 | 19 |
20 // Maximum width of the link drag image in pixels. | 20 // Maximum width of the link drag image in pixels. |
21 static const int kLinkDragImageMaxWidth = 200; | |
22 static const int kLinkDragImageVPadding = 3; | 21 static const int kLinkDragImageVPadding = 3; |
23 | 22 |
24 // File dragging pixel measurements | 23 // File dragging pixel measurements |
25 static const int kFileDragImageMaxWidth = 200; | 24 static const int kFileDragImageMaxWidth = 200; |
26 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; | 25 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; |
27 | 26 |
28 void SetURLAndDragImage(const GURL& url, | |
29 const string16& title, | |
30 const SkBitmap& icon, | |
31 ui::OSExchangeData* data) { | |
32 DCHECK(url.is_valid() && data); | |
33 | |
34 data->SetURL(url, title); | |
35 | |
36 // Create a button to render the drag image for us. | |
37 views::TextButton button(NULL, | |
38 title.empty() ? UTF8ToUTF16(url.spec()) : title); | |
39 button.set_max_width(kLinkDragImageMaxWidth); | |
40 if (icon.isNull()) { | |
41 button.SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
42 IDR_DEFAULT_FAVICON)); | |
43 } else { | |
44 button.SetIcon(icon); | |
45 } | |
46 gfx::Size prefsize = button.GetPreferredSize(); | |
47 button.SetBounds(0, 0, prefsize.width(), prefsize.height()); | |
48 | |
49 // Render the image. | |
50 gfx::CanvasSkia canvas(prefsize, false); | |
51 button.PaintButton(&canvas, views::TextButton::PB_FOR_DRAG); | |
52 SetDragImageOnDataObject(canvas, prefsize, | |
53 gfx::Point(prefsize.width() / 2, prefsize.height() / 2), data); | |
54 } | |
55 | |
56 void CreateDragImageForFile(const FilePath& file_name, | 27 void CreateDragImageForFile(const FilePath& file_name, |
57 const SkBitmap* icon, | 28 const SkBitmap* icon, |
58 ui::OSExchangeData* data_object) { | 29 ui::OSExchangeData* data_object) { |
59 DCHECK(icon); | 30 DCHECK(icon); |
60 DCHECK(data_object); | 31 DCHECK(data_object); |
61 | 32 |
62 // Set up our text portion | 33 // Set up our text portion |
63 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 34 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
64 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); | 35 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); |
65 | 36 |
(...skipping 26 matching lines...) Expand all Loading... |
92 | 63 |
93 void SetDragImageOnDataObject(const gfx::Canvas& canvas, | 64 void SetDragImageOnDataObject(const gfx::Canvas& canvas, |
94 const gfx::Size& size, | 65 const gfx::Size& size, |
95 const gfx::Point& cursor_offset, | 66 const gfx::Point& cursor_offset, |
96 ui::OSExchangeData* data_object) { | 67 ui::OSExchangeData* data_object) { |
97 SetDragImageOnDataObject( | 68 SetDragImageOnDataObject( |
98 canvas.AsCanvasSkia()->ExtractBitmap(), size, cursor_offset, data_object); | 69 canvas.AsCanvasSkia()->ExtractBitmap(), size, cursor_offset, data_object); |
99 } | 70 } |
100 | 71 |
101 } // namespace drag_utils | 72 } // namespace drag_utils |
OLD | NEW |