OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/button_drag_utils.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "googleurl/src/gurl.h" |
| 9 #include "grit/ui_resources.h" |
| 10 #include "ui/base/dragdrop/drag_utils.h" |
| 11 #include "ui/base/dragdrop/os_exchange_data.h" |
| 12 #include "ui/base/resource/resource_bundle.h" |
| 13 #include "ui/gfx/canvas_skia.h" |
| 14 #include "ui/views/controls/button/text_button.h" |
| 15 |
| 16 namespace button_drag_utils { |
| 17 |
| 18 // Maximum width of the link drag image in pixels. |
| 19 static const int kLinkDragImageMaxWidth = 200; |
| 20 |
| 21 void SetURLAndDragImage(const GURL& url, |
| 22 const string16& title, |
| 23 const SkBitmap& icon, |
| 24 ui::OSExchangeData* data) { |
| 25 DCHECK(url.is_valid() && data); |
| 26 |
| 27 data->SetURL(url, title); |
| 28 |
| 29 // Create a button to render the drag image for us. |
| 30 views::TextButton button(NULL, |
| 31 title.empty() ? UTF8ToUTF16(url.spec()) : title); |
| 32 button.set_max_width(kLinkDragImageMaxWidth); |
| 33 if (icon.isNull()) { |
| 34 button.SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 35 IDR_DEFAULT_FAVICON)); |
| 36 } else { |
| 37 button.SetIcon(icon); |
| 38 } |
| 39 gfx::Size prefsize = button.GetPreferredSize(); |
| 40 button.SetBounds(0, 0, prefsize.width(), prefsize.height()); |
| 41 |
| 42 // Render the image. |
| 43 gfx::CanvasSkia canvas(prefsize, false); |
| 44 button.PaintButton(&canvas, views::TextButton::PB_FOR_DRAG); |
| 45 drag_utils::SetDragImageOnDataObject(canvas, prefsize, |
| 46 gfx::Point(prefsize.width() / 2, prefsize.height() / 2), data); |
| 47 } |
| 48 |
| 49 } // namespace button_drag_utils |
OLD | NEW |