OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/views/drag_utils.h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "base/logging.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "googleurl/src/gurl.h" | |
11 #include "grit/ui_resources.h" | |
12 #include "ui/base/dragdrop/os_exchange_data.h" | |
13 #include "ui/base/resource/resource_bundle.h" | |
14 #include "ui/gfx/canvas_skia.h" | |
15 #include "ui/gfx/font.h" | |
16 #include "ui/views/controls/button/text_button.h" | |
17 | |
18 namespace drag_utils { | |
19 | |
20 // Maximum width of the link drag image in pixels. | |
21 static const int kLinkDragImageMaxWidth = 200; | |
22 static const int kLinkDragImageVPadding = 3; | |
23 | |
24 // File dragging pixel measurements | |
25 static const int kFileDragImageMaxWidth = 200; | |
26 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; | |
27 | |
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, | |
57 const SkBitmap* icon, | |
58 ui::OSExchangeData* data_object) { | |
59 DCHECK(icon); | |
60 DCHECK(data_object); | |
61 | |
62 // Set up our text portion | |
63 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
64 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); | |
65 | |
66 const int width = kFileDragImageMaxWidth; | |
67 // Add +2 here to allow room for the halo. | |
68 const int height = font.GetHeight() + icon->height() + | |
69 kLinkDragImageVPadding + 2; | |
70 gfx::CanvasSkia canvas(gfx::Size(width, height), false /* translucent */); | |
71 | |
72 // Paint the icon. | |
73 canvas.DrawBitmapInt(*icon, (width - icon->width()) / 2, 0); | |
74 | |
75 string16 name = file_name.BaseName().LossyDisplayName(); | |
76 #if defined(OS_WIN) | |
77 // Paint the file name. We inset it one pixel to allow room for the halo. | |
78 canvas.DrawStringWithHalo(name, font, kFileDragImageTextColor, SK_ColorWHITE, | |
79 1, icon->height() + kLinkDragImageVPadding + 1, | |
80 width - 2, font.GetHeight(), | |
81 gfx::Canvas::TEXT_ALIGN_CENTER); | |
82 #else | |
83 canvas.DrawStringInt(name, font, kFileDragImageTextColor, | |
84 0, icon->height() + kLinkDragImageVPadding, | |
85 width, font.GetHeight(), gfx::Canvas::TEXT_ALIGN_CENTER); | |
86 #endif | |
87 | |
88 SetDragImageOnDataObject(canvas, gfx::Size(width, height), | |
89 gfx::Point(width / 2, kLinkDragImageVPadding), | |
90 data_object); | |
91 } | |
92 | |
93 void SetDragImageOnDataObject(const gfx::Canvas& canvas, | |
94 const gfx::Size& size, | |
95 const gfx::Point& cursor_offset, | |
96 ui::OSExchangeData* data_object) { | |
97 SetDragImageOnDataObject( | |
98 canvas.AsCanvasSkia()->ExtractBitmap(), size, cursor_offset, data_object); | |
99 } | |
100 | |
101 } // namespace drag_utils | |
OLD | NEW |