OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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/corewm/cursor_height_provider_win.h" | |
6 | |
7 #include <windows.h> | |
8 #include <map> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/win/scoped_hdc.h" | |
13 | |
14 namespace { | |
15 typedef scoped_ptr<uint32_t> PixelData; | |
16 typedef std::map<HCURSOR, int> HeightStorage; | |
17 | |
18 const uint32_t kBitsPeruint32 = sizeof(uint32_t) * 8; | |
19 // All bits are 1 for transparent portion of monochromatic mask. | |
20 const uint32_t kTransparentMask = 0xffffffff; | |
21 // This is height of default pointer arrow in Windows 7. | |
22 const int kDefaultHeight = 20; | |
23 // Masks are monochromatic. | |
24 const size_t kNumberOfColors = 2; | |
25 const size_t KHeaderAndPalette = | |
26 sizeof(BITMAPINFOHEADER) + kNumberOfColors * sizeof(RGBQUAD); | |
27 | |
28 static HeightStorage* g_cached_heights = NULL; | |
sky
2014/05/19 16:12:46
g_cached_heights -> cached_heights (g_ is for glob
| |
29 | |
30 // Extracts the pixel data of provided bitmap | |
31 PixelData GetBitmapData(HBITMAP handle, const BITMAPINFO& info, HDC hdc) { | |
32 PixelData data; | |
33 // Masks are monochromatic. | |
34 DCHECK_EQ(info.bmiHeader.biBitCount, 1); | |
35 if (info.bmiHeader.biBitCount != 1) | |
36 return data.Pass(); | |
37 | |
38 // When getting pixel data palette is appended to memory pointed by | |
39 // BITMAPINFO passed so allocate additional memory to store additional data. | |
40 scoped_ptr<BITMAPINFO> header( | |
41 reinterpret_cast<BITMAPINFO*>(new char[KHeaderAndPalette])); | |
42 memcpy(header.get(), &(info.bmiHeader), sizeof(info.bmiHeader)); | |
43 | |
44 data.reset(new uint32_t[info.bmiHeader.biSizeImage / sizeof(uint32_t)]); | |
45 | |
46 int result = GetDIBits(hdc, | |
47 handle, | |
48 0, | |
49 info.bmiHeader.biHeight, | |
50 data.get(), | |
51 header.get(), | |
52 DIB_RGB_COLORS); | |
53 | |
54 if (result == 0) | |
55 data.reset(); | |
56 | |
57 return data.Pass(); | |
58 } | |
59 | |
60 // Checks if the specifed row is transparent in provided bitmap. | |
61 bool IsRowTransparent(const PixelData& data, | |
62 const uint32_t row_size, | |
63 const uint32_t last_byte_mask, | |
64 const uint32_t y) { | |
65 // Set the padding bits to 1 to make mask matching easier. | |
66 *(data.get() + (y + 1) * row_size - 1) |= last_byte_mask; | |
67 for (uint32_t i = y * row_size; i < (y + 1) * row_size; ++i) { | |
68 if (*(data.get() + i) != kTransparentMask) | |
69 return false; | |
70 } | |
71 return true; | |
72 } | |
73 | |
74 // Gets the vertical offset between specified cursor's hotpoint and it's bottom. | |
75 // | |
76 // Gets the cursor image data and extract cursor's visible height. | |
77 // Based on that get's what should be the vertical offset between cursor's | |
78 // hot point and the tooltip. | |
79 int CalculateCursorHeight(HCURSOR cursor_handle) { | |
80 base::win::ScopedGetDC hdc(NULL); | |
81 | |
82 ICONINFO icon = {0}; | |
83 GetIconInfo(cursor_handle, &icon); | |
84 | |
85 BITMAPINFO bitmap_info = {0}; | |
86 bitmap_info.bmiHeader.biSize = sizeof(bitmap_info.bmiHeader); | |
87 if (GetDIBits(hdc, icon.hbmMask, 0, 0, NULL, &bitmap_info, DIB_RGB_COLORS) == | |
88 0) | |
89 return kDefaultHeight; | |
90 | |
91 // Rows are padded to full DWORDs. OR with this mask will set them to 1 | |
92 // to simplify matching with |transparent_mask|. | |
93 uint32_t last_byte_mask = ~0; | |
94 const unsigned char bits_to_shift = sizeof(last_byte_mask) * 8 - | |
95 (bitmap_info.bmiHeader.biWidth % kBitsPeruint32); | |
96 if (bits_to_shift != kBitsPeruint32) { | |
sky
2014/05/19 16:12:46
nit: no {}
| |
97 last_byte_mask = (last_byte_mask << bits_to_shift); | |
98 } else { | |
99 last_byte_mask = 0; | |
100 } | |
101 | |
102 const uint32_t row_size = | |
103 (bitmap_info.bmiHeader.biWidth + kBitsPeruint32 - 1) / kBitsPeruint32; | |
104 PixelData data(GetBitmapData(icon.hbmMask, bitmap_info, hdc)); | |
105 if (data == NULL) | |
106 return kDefaultHeight; | |
107 | |
108 const int cursor_height = GetSystemMetrics(SM_CYCURSOR); | |
109 int i = bitmap_info.bmiHeader.biHeight - cursor_height; | |
110 for (; i < bitmap_info.bmiHeader.biHeight; ++i) { | |
111 if (!IsRowTransparent(data, row_size, last_byte_mask, i)) { | |
112 i--; | |
sky
2014/05/19 16:12:46
Might you set i to -1 here?
Mateusz Szymański
2014/05/20 09:49:52
Yes, I believe I can and I believe that would be c
sky
2014/05/20 17:19:23
Might that mean you're going to return a cursor he
Mateusz Szymański
2014/05/21 08:32:22
Note that the value returned is
return bitmap_info
| |
113 break; | |
114 } | |
115 } | |
116 return bitmap_info.bmiHeader.biHeight - i - icon.yHotspot; | |
117 } | |
118 | |
119 } // namespace | |
120 | |
121 namespace views { | |
122 namespace corewm { | |
123 | |
124 int GetCurrentCursorVisibleHeight() { | |
125 CURSORINFO cursor = {0}; | |
126 cursor.cbSize = sizeof(cursor); | |
127 GetCursorInfo(&cursor); | |
128 | |
129 if (g_cached_heights == NULL) | |
130 g_cached_heights = new HeightStorage; | |
131 | |
132 HeightStorage::const_iterator cached_height = | |
133 g_cached_heights->find(cursor.hCursor); | |
134 if (cached_height != g_cached_heights->end()) | |
135 return cached_height->second; | |
136 | |
137 const int height = CalculateCursorHeight(cursor.hCursor); | |
138 (*g_cached_heights)[cursor.hCursor] = height; | |
139 | |
140 return height; | |
141 } | |
142 | |
143 } // namespace corewm | |
144 } // namespace views | |
OLD | NEW |