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* cached_heights = NULL; |
| 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) |
| 97 last_byte_mask = (last_byte_mask << bits_to_shift); |
| 98 else |
| 99 last_byte_mask = 0; |
| 100 |
| 101 const uint32_t row_size = |
| 102 (bitmap_info.bmiHeader.biWidth + kBitsPeruint32 - 1) / kBitsPeruint32; |
| 103 PixelData data(GetBitmapData(icon.hbmMask, bitmap_info, hdc)); |
| 104 if (data == NULL) |
| 105 return kDefaultHeight; |
| 106 |
| 107 const int cursor_height = GetSystemMetrics(SM_CYCURSOR); |
| 108 int i = bitmap_info.bmiHeader.biHeight - cursor_height; |
| 109 for (; i < bitmap_info.bmiHeader.biHeight; ++i) { |
| 110 if (!IsRowTransparent(data, row_size, last_byte_mask, i)) { |
| 111 i--; |
| 112 break; |
| 113 } |
| 114 } |
| 115 return bitmap_info.bmiHeader.biHeight - i - icon.yHotspot; |
| 116 } |
| 117 |
| 118 } // namespace |
| 119 |
| 120 namespace views { |
| 121 namespace corewm { |
| 122 |
| 123 int GetCurrentCursorVisibleHeight() { |
| 124 CURSORINFO cursor = {0}; |
| 125 cursor.cbSize = sizeof(cursor); |
| 126 GetCursorInfo(&cursor); |
| 127 |
| 128 if (cached_heights == NULL) |
| 129 cached_heights = new HeightStorage; |
| 130 |
| 131 HeightStorage::const_iterator cached_height = |
| 132 cached_heights->find(cursor.hCursor); |
| 133 if (cached_height != cached_heights->end()) |
| 134 return cached_height->second; |
| 135 |
| 136 const int height = CalculateCursorHeight(cursor.hCursor); |
| 137 (*cached_heights)[cursor.hCursor] = height; |
| 138 |
| 139 return height; |
| 140 } |
| 141 |
| 142 } // namespace corewm |
| 143 } // namespace views |
OLD | NEW |