Add UMA reports for Linux nacl_helper startup status
[chromium-blink-merge.git] / views / drag_utils.cc
blob7d417c2948ee0423b9081f8fea699acc64f60239
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.
5 #include "views/drag_utils.h"
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 "views/controls/button/text_button.h"
18 using ui::OSExchangeData;
20 namespace drag_utils {
22 // Maximum width of the link drag image in pixels.
23 static const int kLinkDragImageMaxWidth = 200;
24 static const int kLinkDragImageVPadding = 3;
26 // File dragging pixel measurements
27 static const int kFileDragImageMaxWidth = 200;
28 static const SkColor kFileDragImageTextColor = SK_ColorBLACK;
30 void SetURLAndDragImage(const GURL& url,
31 const string16& title,
32 const SkBitmap& icon,
33 OSExchangeData* data) {
34 DCHECK(url.is_valid() && data);
36 data->SetURL(url, title);
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);
48 gfx::Size prefsize = button.GetPreferredSize();
49 button.SetBounds(0, 0, prefsize.width(), prefsize.height());
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);
58 void CreateDragImageForFile(const FilePath& file_name,
59 const SkBitmap* icon,
60 OSExchangeData* data_object) {
61 DCHECK(icon);
62 DCHECK(data_object);
64 // Set up our text portion
65 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
66 gfx::Font font = rb.GetFont(ResourceBundle::BaseFont);
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 */);
74 // Paint the icon.
75 canvas.DrawBitmapInt(*icon, (width - icon->width()) / 2, 0);
77 std::wstring name = UTF16ToWide(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(WideToUTF16Hack(name), font, kFileDragImageTextColor,
86 0, icon->height() + kLinkDragImageVPadding,
87 width, font.GetHeight(), gfx::Canvas::TEXT_ALIGN_CENTER);
88 #endif
90 SetDragImageOnDataObject(canvas, gfx::Size(width, height),
91 gfx::Point(width / 2, kLinkDragImageVPadding),
92 data_object);
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);
103 } // namespace drag_utils