1/* 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "DragImage.h" 28 29#include "Font.h" 30#include "FontCache.h" 31#include "FontDescription.h" 32#include "FontSelector.h" 33#include "GraphicsContext.h" 34#include "HWndDC.h" 35#include "Image.h" 36#include "KURL.h" 37#include "StringTruncator.h" 38#include "TextRun.h" 39#include "WebCoreTextRenderer.h" 40#include <wtf/RetainPtr.h> 41 42#include <windows.h> 43 44namespace WebCore { 45 46HBITMAP allocImage(HDC, IntSize, PlatformGraphicsContext** targetRef); 47void deallocContext(PlatformGraphicsContext* target); 48 49IntSize dragImageSize(DragImageRef image) 50{ 51 if (!image) 52 return IntSize(); 53 BITMAP b; 54 GetObject(image, sizeof(BITMAP), &b); 55 return IntSize(b.bmWidth, b.bmHeight); 56} 57 58void deleteDragImage(DragImageRef image) 59{ 60 if (image) 61 ::DeleteObject(image); 62} 63 64DragImageRef dissolveDragImageToFraction(DragImageRef image, float) 65{ 66 //We don't do this on windows as the dragimage is blended by the OS 67 return image; 68} 69 70DragImageRef createDragImageIconForCachedImageFilename(const String& filename) 71{ 72 SHFILEINFO shfi = {0}; 73 String fname = filename; 74 if (FAILED(SHGetFileInfo(static_cast<LPCWSTR>(fname.charactersWithNullTermination()), FILE_ATTRIBUTE_NORMAL, 75 &shfi, sizeof(shfi), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES))) 76 return 0; 77 78 ICONINFO iconInfo; 79 if (!GetIconInfo(shfi.hIcon, &iconInfo)) { 80 DestroyIcon(shfi.hIcon); 81 return 0; 82 } 83 84 DestroyIcon(shfi.hIcon); 85 DeleteObject(iconInfo.hbmMask); 86 87 return iconInfo.hbmColor; 88} 89 90const float DragLabelBorderX = 4; 91// Keep border_y in synch with DragController::LinkDragBorderInset. 92const float DragLabelBorderY = 2; 93const float DragLabelRadius = 5; 94const float LabelBorderYOffset = 2; 95 96const float MinDragLabelWidthBeforeClip = 120; 97const float MaxDragLabelWidth = 200; 98const float MaxDragLabelStringWidth = (MaxDragLabelWidth - 2 * DragLabelBorderX); 99 100const float DragLinkLabelFontsize = 11; 101const float DragLinkUrlFontSize = 10; 102 103static Font dragLabelFont(int size, bool bold, FontRenderingMode renderingMode) 104{ 105 Font result; 106#if !OS(WINCE) 107 NONCLIENTMETRICS metrics; 108 metrics.cbSize = sizeof(metrics); 109 SystemParametersInfo(SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0); 110 111 FontDescription description; 112 description.setWeight(bold ? FontWeightBold : FontWeightNormal); 113 description.setOneFamily(metrics.lfSmCaptionFont.lfFaceName); 114 description.setSpecifiedSize((float)size); 115 description.setComputedSize((float)size); 116 description.setRenderingMode(renderingMode); 117 result = Font(description, 0, 0); 118 result.update(0); 119#endif 120 return result; 121} 122 123DragImageRef createDragImageForLink(KURL& url, const String& inLabel, FontRenderingMode fontRenderingMode) 124{ 125 // This is more or less an exact match for the Mac OS X code. 126 127 const Font* labelFont; 128 const Font* urlFont; 129 FontCachePurgePreventer fontCachePurgePreventer; 130 131 if (fontRenderingMode == AlternateRenderingMode) { 132 static const Font alternateRenderingModeLabelFont = dragLabelFont(DragLinkLabelFontsize, true, AlternateRenderingMode); 133 static const Font alternateRenderingModeURLFont = dragLabelFont(DragLinkUrlFontSize, false, AlternateRenderingMode); 134 labelFont = &alternateRenderingModeLabelFont; 135 urlFont = &alternateRenderingModeURLFont; 136 } else { 137 static const Font normalRenderingModeLabelFont = dragLabelFont(DragLinkLabelFontsize, true, NormalRenderingMode); 138 static const Font normalRenderingModeURLFont = dragLabelFont(DragLinkUrlFontSize, false, NormalRenderingMode); 139 labelFont = &normalRenderingModeLabelFont; 140 urlFont = &normalRenderingModeURLFont; 141 } 142 143 bool drawURLString = true; 144 bool clipURLString = false; 145 bool clipLabelString = false; 146 147 String urlString = url.string(); 148 String label = inLabel; 149 if (label.isEmpty()) { 150 drawURLString = false; 151 label = urlString; 152 } 153 154 // First step in drawing the link drag image width. 155 TextRun labelRun(label.impl()); 156 TextRun urlRun(urlString.impl()); 157 IntSize labelSize(labelFont->width(labelRun), labelFont->fontMetrics().ascent() + labelFont->fontMetrics().descent()); 158 159 if (labelSize.width() > MaxDragLabelStringWidth) { 160 labelSize.setWidth(MaxDragLabelStringWidth); 161 clipLabelString = true; 162 } 163 164 IntSize urlStringSize; 165 IntSize imageSize(labelSize.width() + DragLabelBorderX * 2, labelSize.height() + DragLabelBorderY * 2); 166 167 if (drawURLString) { 168 urlStringSize.setWidth(urlFont->width(urlRun)); 169 urlStringSize.setHeight(urlFont->fontMetrics().ascent() + urlFont->fontMetrics().descent()); 170 imageSize.setHeight(imageSize.height() + urlStringSize.height()); 171 if (urlStringSize.width() > MaxDragLabelStringWidth) { 172 imageSize.setWidth(MaxDragLabelWidth); 173 clipURLString = true; 174 } else 175 imageSize.setWidth(std::max(labelSize.width(), urlStringSize.width()) + DragLabelBorderX * 2); 176 } 177 178 // We now know how big the image needs to be, so we create and 179 // fill the background 180 HBITMAP image = 0; 181 HWndDC dc(0); 182 HDC workingDC = CreateCompatibleDC(dc); 183 if (!workingDC) 184 return 0; 185 186 PlatformGraphicsContext* contextRef; 187 image = allocImage(workingDC, imageSize, &contextRef); 188 if (!image) { 189 DeleteDC(workingDC); 190 return 0; 191 } 192 193 SelectObject(workingDC, image); 194 GraphicsContext context(contextRef); 195 // On Mac alpha is {0.7, 0.7, 0.7, 0.8}, however we can't control alpha 196 // for drag images on win, so we use 1 197 static const Color backgroundColor(140, 140, 140); 198 static const IntSize radii(DragLabelRadius, DragLabelRadius); 199 IntRect rect(0, 0, imageSize.width(), imageSize.height()); 200 context.fillRoundedRect(rect, radii, radii, radii, radii, backgroundColor, ColorSpaceDeviceRGB); 201 202 // Draw the text 203 static const Color topColor(0, 0, 0, 255); // original alpha = 0.75 204 static const Color bottomColor(255, 255, 255, 127); // original alpha = 0.5 205 if (drawURLString) { 206 if (clipURLString) 207 urlString = StringTruncator::rightTruncate(urlString, imageSize.width() - (DragLabelBorderX * 2.0f), *urlFont, StringTruncator::EnableRoundingHacks); 208 IntPoint textPos(DragLabelBorderX, imageSize.height() - (LabelBorderYOffset + urlFont->fontMetrics().descent())); 209 WebCoreDrawDoubledTextAtPoint(context, urlString, textPos, *urlFont, topColor, bottomColor); 210 } 211 212 if (clipLabelString) 213 label = StringTruncator::rightTruncate(label, imageSize.width() - (DragLabelBorderX * 2.0f), *labelFont, StringTruncator::EnableRoundingHacks); 214 215 IntPoint textPos(DragLabelBorderX, DragLabelBorderY + labelFont->pixelSize()); 216 WebCoreDrawDoubledTextAtPoint(context, label, textPos, *labelFont, topColor, bottomColor); 217 218 deallocContext(contextRef); 219 DeleteDC(workingDC); 220 return image; 221} 222 223} 224