OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 13 #include "ui/gfx/canvas.h" |
14 #include "ui/gfx/font.h" | 14 #include "ui/gfx/font.h" |
| 15 #include "ui/gfx/image/canvas_image_source.h" |
15 #include "ui/gfx/point.h" | 16 #include "ui/gfx/point.h" |
16 #include "ui/gfx/size.h" | 17 #include "ui/gfx/size.h" |
17 | 18 |
18 namespace drag_utils { | 19 namespace drag_utils { |
19 | 20 namespace { |
20 // Maximum width of the link drag image in pixels. | 21 // Maximum width of the link drag image in pixels. |
21 static const int kLinkDragImageVPadding = 3; | 22 const int kLinkDragImageVPadding = 3; |
22 | 23 |
23 // File dragging pixel measurements | 24 // File dragging pixel measurements |
24 static const int kFileDragImageMaxWidth = 200; | 25 const int kFileDragImageMaxWidth = 200; |
25 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; | 26 const SkColor kFileDragImageTextColor = SK_ColorBLACK; |
| 27 |
| 28 gfx::Size GetImageSize(const gfx::ImageSkia& icon) { |
| 29 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 30 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); |
| 31 |
| 32 const int width = kFileDragImageMaxWidth; |
| 33 // Add +2 here to allow room for the halo. |
| 34 const int height = font.GetHeight() + icon.height() + |
| 35 kLinkDragImageVPadding + 2; |
| 36 return gfx::Size(width, height); |
| 37 } |
| 38 |
| 39 class DragImageSource : public gfx::CanvasImageSource { |
| 40 public: |
| 41 DragImageSource(const gfx::ImageSkia& icon, const string16& name) |
| 42 : CanvasImageSource(GetImageSize(icon), false /* translucent */), |
| 43 icon_(icon), |
| 44 name_(name) { |
| 45 } |
| 46 virtual ~DragImageSource() {} |
| 47 |
| 48 // gfx::CanvasImageSource overrides: |
| 49 virtual void Draw(gfx::Canvas* canvas) OVERRIDE { |
| 50 // Set up our text portion |
| 51 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 52 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); |
| 53 |
| 54 int width = size().width(); |
| 55 // Paint the icon. |
| 56 canvas->DrawImageInt(icon_, (width - icon_.width()) / 2, 0); |
| 57 |
| 58 const int flags = gfx::Canvas::TEXT_ALIGN_CENTER; |
| 59 #if defined(OS_WIN) |
| 60 // Paint the file name. We inset it one pixel to allow room for the halo. |
| 61 canvas->DrawStringWithHalo(name_, font, |
| 62 kFileDragImageTextColor, SK_ColorWHITE, |
| 63 1, icon_.height() + kLinkDragImageVPadding + 1, |
| 64 width - 2, font.GetHeight(), flags); |
| 65 #else |
| 66 // NO_SUBPIXEL_RENDERING is required when drawing to a non-opaque canvas. |
| 67 canvas->DrawStringInt(name_, font, |
| 68 kFileDragImageTextColor, |
| 69 0, icon_.height() + kLinkDragImageVPadding, |
| 70 width, font.GetHeight(), |
| 71 flags | gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 72 #endif |
| 73 } |
| 74 |
| 75 private: |
| 76 const gfx::ImageSkia icon_; |
| 77 const string16 name_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(DragImageSource); |
| 80 }; |
| 81 |
| 82 } // namespace |
26 | 83 |
27 void CreateDragImageForFile(const FilePath& file_name, | 84 void CreateDragImageForFile(const FilePath& file_name, |
28 const gfx::ImageSkia* icon, | 85 const gfx::ImageSkia* icon, |
29 ui::OSExchangeData* data_object) { | 86 ui::OSExchangeData* data_object) { |
30 DCHECK(icon); | 87 DCHECK(icon); |
31 DCHECK(data_object); | 88 DCHECK(data_object); |
32 | 89 |
33 // Set up our text portion | 90 string16 name = file_name.BaseName().LossyDisplayName(); |
34 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 91 DragImageSource* source = new DragImageSource(*icon, name); |
35 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); | 92 gfx::Size size = source->size(); |
36 | 93 |
37 const int width = kFileDragImageMaxWidth; | 94 SetDragImageOnDataObject(gfx::ImageSkia(source, size), size, |
38 // Add +2 here to allow room for the halo. | 95 gfx::Point(size.width() / 2, kLinkDragImageVPadding), |
39 const int height = font.GetHeight() + icon->height() + | |
40 kLinkDragImageVPadding + 2; | |
41 gfx::Canvas canvas(gfx::Size(width, height), false /* translucent */); | |
42 | |
43 // Paint the icon. | |
44 canvas.DrawImageInt(*icon, (width - icon->width()) / 2, 0); | |
45 | |
46 string16 name = file_name.BaseName().LossyDisplayName(); | |
47 const int flags = gfx::Canvas::TEXT_ALIGN_CENTER; | |
48 #if defined(OS_WIN) | |
49 // Paint the file name. We inset it one pixel to allow room for the halo. | |
50 canvas.DrawStringWithHalo(name, font, kFileDragImageTextColor, SK_ColorWHITE, | |
51 1, icon->height() + kLinkDragImageVPadding + 1, | |
52 width - 2, font.GetHeight(), flags); | |
53 #else | |
54 // NO_SUBPIXEL_RENDERING is required when drawing to a non-opaque canvas. | |
55 canvas.DrawStringInt(name, font, kFileDragImageTextColor, | |
56 0, icon->height() + kLinkDragImageVPadding, | |
57 width, font.GetHeight(), | |
58 flags | gfx::Canvas::NO_SUBPIXEL_RENDERING); | |
59 #endif | |
60 | |
61 SetDragImageOnDataObject(canvas, gfx::Size(width, height), | |
62 gfx::Point(width / 2, kLinkDragImageVPadding), | |
63 data_object); | 96 data_object); |
64 } | 97 } |
65 | 98 |
66 void SetDragImageOnDataObject(const gfx::Canvas& canvas, | 99 void SetDragImageOnDataObject(const gfx::Canvas& canvas, |
67 const gfx::Size& size, | 100 const gfx::Size& size, |
68 const gfx::Point& cursor_offset, | 101 const gfx::Point& cursor_offset, |
69 ui::OSExchangeData* data_object) { | 102 ui::OSExchangeData* data_object) { |
70 SetDragImageOnDataObject(canvas.ExtractImageSkiaRep(), size, cursor_offset, | 103 SetDragImageOnDataObject(canvas.ExtractImageSkiaRep(), size, cursor_offset, |
71 data_object); | 104 data_object); |
72 } | 105 } |
73 | 106 |
74 } // namespace drag_utils | 107 } // namespace drag_utils |
OLD | NEW |