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