2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "BitmapInfo.h"
33 #include "SystemInfo.h"
35 #include <wtf/OwnPtr.h>
36 #include <wtf/PassOwnPtr.h>
44 static PassRefPtr<SharedCursor> createSharedCursor(Image* img, const IntPoint& hotSpot)
46 RefPtr<SharedCursor> impl;
48 IntPoint effectiveHotSpot = determineHotSpot(img, hotSpot);
49 static bool doAlpha = windowsVersion() >= WindowsXP;
50 BitmapInfo cursorImage = BitmapInfo::create(IntSize(img->width(), img->height()));
53 HDC workingDC = CreateCompatibleDC(dc);
55 OwnPtr<HBITMAP> hCursor = adoptPtr(CreateDIBSection(dc, (BITMAPINFO *)&cursorImage, DIB_RGB_COLORS, 0, 0, 0));
58 img->getHBITMAP(hCursor.get());
59 HBITMAP hOldBitmap = (HBITMAP)SelectObject(workingDC, hCursor.get());
60 SetBkMode(workingDC, TRANSPARENT);
61 SelectObject(workingDC, hOldBitmap);
63 Vector<unsigned char, 128> maskBits;
64 maskBits.fill(0xff, (img->width() + 7) / 8 * img->height());
65 OwnPtr<HBITMAP> hMask = adoptPtr(CreateBitmap(img->width(), img->height(), 1, 1, maskBits.data()));
69 ii.xHotspot = effectiveHotSpot.x();
70 ii.yHotspot = effectiveHotSpot.y();
71 ii.hbmMask = hMask.get();
72 ii.hbmColor = hCursor.get();
74 impl = SharedCursor::create(CreateIconIndirect(&ii));
76 // Platform doesn't support alpha blended cursors, so we need
77 // to create the mask manually
78 HDC andMaskDC = CreateCompatibleDC(dc);
79 HDC xorMaskDC = CreateCompatibleDC(dc);
80 OwnPtr<HBITMAP> hCursor = adoptPtr(CreateDIBSection(dc, &cursorImage, DIB_RGB_COLORS, 0, 0, 0));
82 img->getHBITMAP(hCursor.get());
84 GetObject(hCursor.get(), sizeof(BITMAP), &cursor);
85 OwnPtr<HBITMAP> andMask = adoptPtr(CreateBitmap(cursor.bmWidth, cursor.bmHeight, 1, 1, NULL));
86 OwnPtr<HBITMAP> xorMask = adoptPtr(CreateCompatibleBitmap(dc, cursor.bmWidth, cursor.bmHeight));
87 HBITMAP oldCursor = (HBITMAP)SelectObject(workingDC, hCursor.get());
88 HBITMAP oldAndMask = (HBITMAP)SelectObject(andMaskDC, andMask.get());
89 HBITMAP oldXorMask = (HBITMAP)SelectObject(xorMaskDC, xorMask.get());
91 SetBkColor(workingDC, RGB(0,0,0));
92 BitBlt(andMaskDC, 0, 0, cursor.bmWidth, cursor.bmHeight, workingDC, 0, 0, SRCCOPY);
94 SetBkColor(xorMaskDC, RGB(255, 255, 255));
95 SetTextColor(xorMaskDC, RGB(255, 255, 255));
96 BitBlt(xorMaskDC, 0, 0, cursor.bmWidth, cursor.bmHeight, andMaskDC, 0, 0, SRCCOPY);
97 BitBlt(xorMaskDC, 0, 0, cursor.bmWidth, cursor.bmHeight, workingDC, 0,0, SRCAND);
99 SelectObject(workingDC, oldCursor);
100 SelectObject(andMaskDC, oldAndMask);
101 SelectObject(xorMaskDC, oldXorMask);
105 icon.xHotspot = effectiveHotSpot.x();
106 icon.yHotspot = effectiveHotSpot.y();
107 icon.hbmMask = andMask.get();
108 icon.hbmColor = xorMask.get();
109 impl = SharedCursor::create(CreateIconIndirect(&icon));
117 return impl.release();
120 static PassRefPtr<SharedCursor> loadSharedCursor(HINSTANCE hInstance, LPCWSTR lpCursorName)
122 return SharedCursor::create(::LoadCursorW(hInstance, lpCursorName));
125 static PassRefPtr<SharedCursor> loadCursorByName(char* name, int x, int y)
127 IntPoint hotSpot(x, y);
128 RefPtr<Image> cursorImage(Image::loadPlatformResource(name));
129 if (cursorImage && !cursorImage->isNull())
130 return createSharedCursor(cursorImage.get(), hotSpot);
131 return loadSharedCursor(0, IDC_ARROW);
134 void Cursor::ensurePlatformCursor() const
136 if (m_platformCursor)
140 case Cursor::Pointer:
142 case Cursor::ContextMenu:
147 case Cursor::Grabbing:
148 m_platformCursor = loadSharedCursor(0, IDC_ARROW);
151 m_platformCursor = loadSharedCursor(0, IDC_CROSS);
154 m_platformCursor = loadSharedCursor(0, IDC_HAND);
157 m_platformCursor = loadSharedCursor(0, IDC_IBEAM);
160 m_platformCursor = loadSharedCursor(0, IDC_WAIT);
163 m_platformCursor = loadSharedCursor(0, IDC_HELP);
166 m_platformCursor = loadSharedCursor(0, IDC_SIZEALL);
168 case Cursor::MiddlePanning:
169 m_platformCursor = loadCursorByName("panIcon", 8, 8);
171 case Cursor::EastResize:
172 m_platformCursor = loadSharedCursor(0, IDC_SIZEWE);
174 case Cursor::EastPanning:
175 m_platformCursor = loadCursorByName("panEastCursor", 7, 7);
177 case Cursor::NorthResize:
178 m_platformCursor = loadSharedCursor(0, IDC_SIZENS);
180 case Cursor::NorthPanning:
181 m_platformCursor = loadCursorByName("panNorthCursor", 7, 7);
183 case Cursor::NorthEastResize:
184 m_platformCursor = loadSharedCursor(0, IDC_SIZENESW);
186 case Cursor::NorthEastPanning:
187 m_platformCursor = loadCursorByName("panNorthEastCursor", 7, 7);
189 case Cursor::NorthWestResize:
190 m_platformCursor = loadSharedCursor(0, IDC_SIZENWSE);
192 case Cursor::NorthWestPanning:
193 m_platformCursor = loadCursorByName("panNorthWestCursor", 7, 7);
195 case Cursor::SouthResize:
196 m_platformCursor = loadSharedCursor(0, IDC_SIZENS);
198 case Cursor::SouthPanning:
199 m_platformCursor = loadCursorByName("panSouthCursor", 7, 7);
201 case Cursor::SouthEastResize:
202 m_platformCursor = loadSharedCursor(0, IDC_SIZENWSE);
204 case Cursor::SouthEastPanning:
205 m_platformCursor = loadCursorByName("panSouthEastCursor", 7, 7);
207 case Cursor::SouthWestResize:
208 m_platformCursor = loadSharedCursor(0, IDC_SIZENESW);
210 case Cursor::SouthWestPanning:
211 m_platformCursor = loadCursorByName("panSouthWestCursor", 7, 7);
213 case Cursor::WestResize:
214 m_platformCursor = loadSharedCursor(0, IDC_SIZEWE);
216 case Cursor::NorthSouthResize:
217 m_platformCursor = loadSharedCursor(0, IDC_SIZENS);
219 case Cursor::EastWestResize:
220 m_platformCursor = loadSharedCursor(0, IDC_SIZEWE);
222 case Cursor::WestPanning:
223 m_platformCursor = loadCursorByName("panWestCursor", 7, 7);
225 case Cursor::NorthEastSouthWestResize:
226 m_platformCursor = loadSharedCursor(0, IDC_SIZENESW);
228 case Cursor::NorthWestSouthEastResize:
229 m_platformCursor = loadSharedCursor(0, IDC_SIZENWSE);
231 case Cursor::ColumnResize:
232 // FIXME: Windows does not have a standard column resize cursor <rdar://problem/5018591>
233 m_platformCursor = loadSharedCursor(0, IDC_SIZEWE);
235 case Cursor::RowResize:
236 // FIXME: Windows does not have a standard row resize cursor <rdar://problem/5018591>
237 m_platformCursor = loadSharedCursor(0, IDC_SIZENS);
239 case Cursor::VerticalText:
240 m_platformCursor = loadCursorByName("verticalTextCursor", 7, 7);
242 case Cursor::Progress:
243 m_platformCursor = loadSharedCursor(0, IDC_APPSTARTING);
246 case Cursor::NotAllowed:
247 m_platformCursor = loadSharedCursor(0, IDC_NO);
250 m_platformCursor = loadCursorByName("zoomInCursor", 7, 7);
252 case Cursor::ZoomOut:
253 m_platformCursor = loadCursorByName("zoomOutCursor", 7, 7);
256 m_platformCursor = createSharedCursor(m_image.get(), m_hotSpot);
259 ASSERT_NOT_REACHED();
260 m_platformCursor = loadSharedCursor(0, IDC_ARROW);
265 SharedCursor::~SharedCursor()
267 DestroyIcon(m_nativeCursor);
270 Cursor::Cursor(const Cursor& other)
271 : m_type(other.m_type)
272 , m_image(other.m_image)
273 , m_hotSpot(other.m_hotSpot)
274 , m_platformCursor(other.m_platformCursor)
278 Cursor& Cursor::operator=(const Cursor& other)
280 m_type = other.m_type;
281 m_image = other.m_image;
282 m_hotSpot = other.m_hotSpot;
283 m_platformCursor = other.m_platformCursor;
291 } // namespace WebCore