| 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 "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 using ui::OSExchangeData; | |
| 19 | |
| 20 namespace drag_utils { | |
| 21 | |
| 22 // Maximum width of the link drag image in pixels. | |
| 23 static const int kLinkDragImageMaxWidth = 200; | |
| 24 static const int kLinkDragImageVPadding = 3; | |
| 25 | |
| 26 // File dragging pixel measurements | |
| 27 static const int kFileDragImageMaxWidth = 200; | |
| 28 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; | |
| 29 | |
| 30 void SetURLAndDragImage(const GURL& url, | |
| 31 const string16& title, | |
| 32 const SkBitmap& icon, | |
| 33 OSExchangeData* data) { | |
| 34 DCHECK(url.is_valid() && data); | |
| 35 | |
| 36 data->SetURL(url, title); | |
| 37 | |
| 38 // Create a button to render the drag image for us. | |
| 39 views::TextButton button(NULL, | |
| 40 title.empty() ? UTF8ToUTF16(url.spec()) : title); | |
| 41 button.set_max_width(kLinkDragImageMaxWidth); | |
| 42 if (icon.isNull()) { | |
| 43 button.SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
| 44 IDR_DEFAULT_FAVICON)); | |
| 45 } else { | |
| 46 button.SetIcon(icon); | |
| 47 } | |
| 48 gfx::Size prefsize = button.GetPreferredSize(); | |
| 49 button.SetBounds(0, 0, prefsize.width(), prefsize.height()); | |
| 50 | |
| 51 // Render the image. | |
| 52 gfx::CanvasSkia canvas(prefsize.width(), prefsize.height(), false); | |
| 53 button.PaintButton(&canvas, views::TextButton::PB_FOR_DRAG); | |
| 54 SetDragImageOnDataObject(canvas, prefsize, | |
| 55 gfx::Point(prefsize.width() / 2, prefsize.height() / 2), data); | |
| 56 } | |
| 57 | |
| 58 void CreateDragImageForFile(const FilePath& file_name, | |
| 59 const SkBitmap* icon, | |
| 60 OSExchangeData* data_object) { | |
| 61 DCHECK(icon); | |
| 62 DCHECK(data_object); | |
| 63 | |
| 64 // Set up our text portion | |
| 65 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 66 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont); | |
| 67 | |
| 68 const int width = kFileDragImageMaxWidth; | |
| 69 // Add +2 here to allow room for the halo. | |
| 70 const int height = font.GetHeight() + icon->height() + | |
| 71 kLinkDragImageVPadding + 2; | |
| 72 gfx::CanvasSkia canvas(width, height, false /* translucent */); | |
| 73 | |
| 74 // Paint the icon. | |
| 75 canvas.DrawBitmapInt(*icon, (width - icon->width()) / 2, 0); | |
| 76 | |
| 77 string16 name = file_name.BaseName().LossyDisplayName(); | |
| 78 #if defined(OS_WIN) | |
| 79 // Paint the file name. We inset it one pixel to allow room for the halo. | |
| 80 canvas.DrawStringWithHalo(name, font, kFileDragImageTextColor, SK_ColorWHITE, | |
| 81 1, icon->height() + kLinkDragImageVPadding + 1, | |
| 82 width - 2, font.GetHeight(), | |
| 83 gfx::Canvas::TEXT_ALIGN_CENTER); | |
| 84 #else | |
| 85 canvas.DrawStringInt(name, font, kFileDragImageTextColor, | |
| 86 0, icon->height() + kLinkDragImageVPadding, | |
| 87 width, font.GetHeight(), gfx::Canvas::TEXT_ALIGN_CENTER); | |
| 88 #endif | |
| 89 | |
| 90 SetDragImageOnDataObject(canvas, gfx::Size(width, height), | |
| 91 gfx::Point(width / 2, kLinkDragImageVPadding), | |
| 92 data_object); | |
| 93 } | |
| 94 | |
| 95 void SetDragImageOnDataObject(const gfx::Canvas& canvas, | |
| 96 const gfx::Size& size, | |
| 97 const gfx::Point& cursor_offset, | |
| 98 OSExchangeData* data_object) { | |
| 99 SetDragImageOnDataObject( | |
| 100 canvas.AsCanvasSkia()->ExtractBitmap(), size, cursor_offset, data_object); | |
| 101 } | |
| 102 | |
| 103 } // namespace drag_utils | |
| OLD | NEW |