| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 "chrome/browser/drag_utils.h" | |
| 6 | |
| 7 #include <objidl.h> | |
| 8 #include <shlobj.h> | |
| 9 #include <shobjidl.h> | |
| 10 | |
| 11 #include "app/gfx/chrome_canvas.h" | |
| 12 #include "app/gfx/chrome_font.h" | |
| 13 #include "app/l10n_util.h" | |
| 14 #include "app/os_exchange_data.h" | |
| 15 #include "app/resource_bundle.h" | |
| 16 #include "base/file_util.h" | |
| 17 #include "base/gfx/gdi_util.h" | |
| 18 #include "base/gfx/point.h" | |
| 19 #include "base/string_util.h" | |
| 20 #include "chrome/browser/views/bookmark_bar_view.h" | |
| 21 #include "chrome/common/win_util.h" | |
| 22 #include "googleurl/src/gurl.h" | |
| 23 #include "grit/theme_resources.h" | |
| 24 #include "views/controls/button/text_button.h" | |
| 25 | |
| 26 namespace drag_utils { | |
| 27 | |
| 28 // Maximum width of the link drag image in pixels. | |
| 29 static const int kLinkDragImageMaxWidth = 200; | |
| 30 static const int kLinkDragImageVPadding = 3; | |
| 31 static const int kLinkDragImageVSpacing = 2; | |
| 32 static const int kLinkDragImageHPadding = 4; | |
| 33 static const SkColor kLinkDragImageBGColor = SkColorSetRGB(131, 146, 171); | |
| 34 //static const SkColor kLinkDragImageBGColor = SkColorSetRGB(195, 217, 255); | |
| 35 static const SkColor kLinkDragImageTextColor = SK_ColorBLACK; | |
| 36 | |
| 37 // File dragging pixel measurements | |
| 38 static const int kFileDragImageMaxWidth = 200; | |
| 39 static const SkColor kFileDragImageTextColor = SK_ColorBLACK; | |
| 40 | |
| 41 static void SetDragImageOnDataObject(HBITMAP hbitmap, | |
| 42 int width, | |
| 43 int height, | |
| 44 int cursor_offset_x, | |
| 45 int cursor_offset_y, | |
| 46 IDataObject* data_object) { | |
| 47 IDragSourceHelper* helper = NULL; | |
| 48 HRESULT rv = CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, | |
| 49 IID_IDragSourceHelper, reinterpret_cast<LPVOID*>(&helper)); | |
| 50 if (SUCCEEDED(rv)) { | |
| 51 SHDRAGIMAGE sdi; | |
| 52 sdi.sizeDragImage.cx = width; | |
| 53 sdi.sizeDragImage.cy = height; | |
| 54 sdi.crColorKey = 0xFFFFFFFF; | |
| 55 sdi.hbmpDragImage = hbitmap; | |
| 56 sdi.ptOffset.x = cursor_offset_x; | |
| 57 sdi.ptOffset.y = cursor_offset_y; | |
| 58 helper->InitializeFromBitmap(&sdi, data_object); | |
| 59 } | |
| 60 }; | |
| 61 | |
| 62 // Blit the contents of the canvas to a new HBITMAP. It is the caller's | |
| 63 // responsibility to release the |bits| buffer. | |
| 64 static HBITMAP CreateBitmapFromCanvas(const ChromeCanvas& canvas, | |
| 65 int width, | |
| 66 int height) { | |
| 67 HDC screen_dc = GetDC(NULL); | |
| 68 BITMAPINFOHEADER header; | |
| 69 gfx::CreateBitmapHeader(width, height, &header); | |
| 70 void* bits; | |
| 71 HBITMAP bitmap = | |
| 72 CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&header), | |
| 73 DIB_RGB_COLORS, &bits, NULL, 0); | |
| 74 HDC compatible_dc = CreateCompatibleDC(screen_dc); | |
| 75 HGDIOBJ old_object = SelectObject(compatible_dc, bitmap); | |
| 76 BitBlt(compatible_dc, 0, 0, width, height, | |
| 77 canvas.getTopPlatformDevice().getBitmapDC(), | |
| 78 0, 0, SRCCOPY); | |
| 79 SelectObject(compatible_dc, old_object); | |
| 80 ReleaseDC(NULL, compatible_dc); | |
| 81 ReleaseDC(NULL, screen_dc); | |
| 82 return bitmap; | |
| 83 } | |
| 84 | |
| 85 void SetURLAndDragImage(const GURL& url, | |
| 86 const std::wstring& title, | |
| 87 const SkBitmap& icon, | |
| 88 OSExchangeData* data) { | |
| 89 DCHECK(url.is_valid() && data); | |
| 90 | |
| 91 data->SetURL(url, title); | |
| 92 | |
| 93 // Create a button to render the drag image for us. | |
| 94 views::TextButton button(NULL, | |
| 95 title.empty() ? UTF8ToWide(url.spec()) : title); | |
| 96 button.set_max_width(BookmarkBarView::kMaxButtonWidth); | |
| 97 if (icon.isNull()) { | |
| 98 button.SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
| 99 IDR_DEFAULT_FAVICON)); | |
| 100 } else { | |
| 101 button.SetIcon(icon); | |
| 102 } | |
| 103 gfx::Size prefsize = button.GetPreferredSize(); | |
| 104 button.SetBounds(0, 0, prefsize.width(), prefsize.height()); | |
| 105 | |
| 106 // Render the image. | |
| 107 ChromeCanvas canvas(prefsize.width(), prefsize.height(), false); | |
| 108 button.Paint(&canvas, true); | |
| 109 SetDragImageOnDataObject(canvas, prefsize.width(), prefsize.height(), | |
| 110 prefsize.width() / 2, prefsize.height() / 2, | |
| 111 data); | |
| 112 } | |
| 113 | |
| 114 void CreateDragImageForFile(const std::wstring& file_name, | |
| 115 SkBitmap* icon, | |
| 116 IDataObject* data_object) { | |
| 117 DCHECK(icon); | |
| 118 DCHECK(data_object); | |
| 119 | |
| 120 // Set up our text portion | |
| 121 const std::wstring& name = file_util::GetFilenameFromPath(file_name); | |
| 122 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 123 ChromeFont font = rb.GetFont(ResourceBundle::BaseFont); | |
| 124 | |
| 125 const int width = kFileDragImageMaxWidth; | |
| 126 // Add +2 here to allow room for the halo. | |
| 127 const int height = font.height() + icon->height() + | |
| 128 kLinkDragImageVPadding + 2; | |
| 129 ChromeCanvas canvas(width, height, false /* translucent */); | |
| 130 | |
| 131 // Paint the icon. | |
| 132 canvas.DrawBitmapInt(*icon, (width - icon->width()) / 2, 0); | |
| 133 | |
| 134 // Paint the file name. We inset it one pixel to allow room for the halo. | |
| 135 canvas.DrawStringWithHalo(name, font, kFileDragImageTextColor, SK_ColorWHITE, | |
| 136 1, icon->height() + kLinkDragImageVPadding + 1, | |
| 137 width - 2, font.height(), | |
| 138 ChromeCanvas::TEXT_ALIGN_CENTER); | |
| 139 | |
| 140 SetDragImageOnDataObject(canvas, width, height, width / 2, | |
| 141 kLinkDragImageVPadding, data_object); | |
| 142 } | |
| 143 | |
| 144 void SetDragImageOnDataObject(const ChromeCanvas& canvas, | |
| 145 int width, | |
| 146 int height, | |
| 147 int cursor_x_offset, | |
| 148 int cursor_y_offset, | |
| 149 IDataObject* data_object) { | |
| 150 DCHECK(data_object && width > 0 && height > 0); | |
| 151 // SetDragImageOnDataObject(HBITMAP) takes ownership of the bitmap. | |
| 152 HBITMAP bitmap = CreateBitmapFromCanvas(canvas, width, height); | |
| 153 | |
| 154 // Attach 'bitmap' to the data_object. | |
| 155 SetDragImageOnDataObject(bitmap, width, height, | |
| 156 cursor_x_offset, | |
| 157 cursor_y_offset, | |
| 158 data_object); | |
| 159 } | |
| 160 | |
| 161 } // namespace drag_utils | |
| OLD | NEW |