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/base/dragdrop/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 "ui/base/dragdrop/os_exchange_data.h" | 11 #include "ui/base/dragdrop/os_exchange_data.h" |
12 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
13 #include "ui/gfx/canvas_skia.h" | 13 #include "ui/gfx/canvas.h" |
14 #include "ui/gfx/font.h" | 14 #include "ui/gfx/font.h" |
15 #include "ui/gfx/point.h" | 15 #include "ui/gfx/point.h" |
16 #include "ui/gfx/size.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 kLinkDragImageVPadding = 3; | 21 static const int kLinkDragImageVPadding = 3; |
22 | 22 |
23 // File dragging pixel measurements | 23 // File dragging pixel measurements |
24 static const int kFileDragImageMaxWidth = 200; | 24 static const int kFileDragImageMaxWidth = 200; |
25 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; | 25 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; |
26 | 26 |
27 void CreateDragImageForFile(const FilePath& file_name, | 27 void CreateDragImageForFile(const FilePath& file_name, |
28 const SkBitmap* icon, | 28 const SkBitmap* icon, |
29 ui::OSExchangeData* data_object) { | 29 ui::OSExchangeData* data_object) { |
30 DCHECK(icon); | 30 DCHECK(icon); |
31 DCHECK(data_object); | 31 DCHECK(data_object); |
32 | 32 |
33 // Set up our text portion | 33 // Set up our text portion |
34 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 34 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
35 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); | 35 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); |
36 | 36 |
37 const int width = kFileDragImageMaxWidth; | 37 const int width = kFileDragImageMaxWidth; |
38 // Add +2 here to allow room for the halo. | 38 // Add +2 here to allow room for the halo. |
39 const int height = font.GetHeight() + icon->height() + | 39 const int height = font.GetHeight() + icon->height() + |
40 kLinkDragImageVPadding + 2; | 40 kLinkDragImageVPadding + 2; |
41 gfx::CanvasSkia canvas(gfx::Size(width, height), false /* translucent */); | 41 gfx::Canvas canvas(gfx::Size(width, height), false /* translucent */); |
42 | 42 |
43 // Paint the icon. | 43 // Paint the icon. |
44 canvas.DrawBitmapInt(*icon, (width - icon->width()) / 2, 0); | 44 canvas.DrawBitmapInt(*icon, (width - icon->width()) / 2, 0); |
45 | 45 |
46 string16 name = file_name.BaseName().LossyDisplayName(); | 46 string16 name = file_name.BaseName().LossyDisplayName(); |
47 #if defined(OS_WIN) | 47 #if defined(OS_WIN) |
48 // Paint the file name. We inset it one pixel to allow room for the halo. | 48 // Paint the file name. We inset it one pixel to allow room for the halo. |
49 canvas.DrawStringWithHalo(name, font, kFileDragImageTextColor, SK_ColorWHITE, | 49 canvas.DrawStringWithHalo(name, font, kFileDragImageTextColor, SK_ColorWHITE, |
50 1, icon->height() + kLinkDragImageVPadding + 1, | 50 1, icon->height() + kLinkDragImageVPadding + 1, |
51 width - 2, font.GetHeight(), | 51 width - 2, font.GetHeight(), |
52 gfx::Canvas::TEXT_ALIGN_CENTER); | 52 gfx::Canvas::TEXT_ALIGN_CENTER); |
53 #else | 53 #else |
54 canvas.DrawStringInt(name, font, kFileDragImageTextColor, | 54 canvas.DrawStringInt(name, font, kFileDragImageTextColor, |
55 0, icon->height() + kLinkDragImageVPadding, | 55 0, icon->height() + kLinkDragImageVPadding, |
56 width, font.GetHeight(), gfx::Canvas::TEXT_ALIGN_CENTER); | 56 width, font.GetHeight(), gfx::Canvas::TEXT_ALIGN_CENTER); |
57 #endif | 57 #endif |
58 | 58 |
59 SetDragImageOnDataObject(canvas, gfx::Size(width, height), | 59 SetDragImageOnDataObject(canvas, gfx::Size(width, height), |
60 gfx::Point(width / 2, kLinkDragImageVPadding), | 60 gfx::Point(width / 2, kLinkDragImageVPadding), |
61 data_object); | 61 data_object); |
62 } | 62 } |
63 | 63 |
64 void SetDragImageOnDataObject(const gfx::Canvas& canvas, | 64 void SetDragImageOnDataObject(const gfx::Canvas& canvas, |
65 const gfx::Size& size, | 65 const gfx::Size& size, |
66 const gfx::Point& cursor_offset, | 66 const gfx::Point& cursor_offset, |
67 ui::OSExchangeData* data_object) { | 67 ui::OSExchangeData* data_object) { |
68 SetDragImageOnDataObject( | 68 SetDragImageOnDataObject(canvas.ExtractBitmap(), size, cursor_offset, |
69 canvas.AsCanvasSkia()->ExtractBitmap(), size, cursor_offset, data_object); | 69 data_object); |
70 } | 70 } |
71 | 71 |
72 } // namespace drag_utils | 72 } // namespace drag_utils |
OLD | NEW |