Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Side by Side Diff: ui/views/corewm/cursor_height_provider_win.cc

Issue 225403022: Fixed positioning of tooltips in multi monitor setups. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Squashed patchset. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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> PixelData;
sky 2014/05/16 17:13:22 uint32 is deprecated. Use uint32_t (same comment a
16 typedef std::map<HCURSOR, int> HeightStorage;
17
18 const uint32 bits_per_uint32 = 32;
sky 2014/05/16 17:13:22 Why not use sizeof(uint32_t) * 8 when you need thi
Mateusz Szymański 2014/05/19 07:43:13 Because uint32 is obviously 32 bits long and if th
19 // All bits are 1 for transparent portion of monochromatic mask.
20 const uint32 transparent_mask = 0xffffffff;
sky 2014/05/16 17:13:22 Use k for constants, eg kTransparentMask.
21 // This is height of default pointer arrow in Windows 7.
22 const int default_height = 20;
23
24 static HeightStorage* cached_heights = nullptr;
sky 2014/05/16 17:13:22 We're not using c++ 11 yet.
25
26 // Extracts the pixel data of provided bitmap
27 PixelData GetBitmapData(HBITMAP handle, const BITMAPINFO& info, HDC hdc) {
28 PixelData data(nullptr);
29 // Masks are monochromatic.
30 DCHECK_EQ(info.bmiHeader.biBitCount, 1);
31 if (info.bmiHeader.biBitCount != 1)
32 return data.Pass();
33
34 const size_t number_of_colors = 2;
35
36 const size_t header_and_palette =
37 sizeof(info.bmiHeader) + number_of_colors * sizeof(RGBQUAD);
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[header_and_palette]));
42 if (header == nullptr)
sky 2014/05/16 17:13:22 Don't null check news. If new fails we'll crash.
43 return data.Pass();
44 memcpy(header.get(), &(info.bmiHeader), sizeof(info.bmiHeader));
45
46 data.reset(new uint32[info.bmiHeader.biSizeImage / sizeof(uint32)]);
47 if (data == nullptr)
sky 2014/05/16 17:13:22 Same comment here about checking for null.
48 return data.Pass();
49
50 int result = GetDIBits(hdc,
sky 2014/05/16 17:13:22 Shouldn't you check the return value? Perhaps rese
Mateusz Szymański 2014/05/19 07:43:13 Nicely spotted, I was sure I did.
51 handle,
52 0,
53 info.bmiHeader.biHeight,
54 data.get(),
55 header.get(),
56 DIB_RGB_COLORS);
57
58 return data.Pass();
59 }
60
61 // Checks if the specifed row is transparent in provided bitmap.
62 bool IsRowTransparent(const PixelData& data, const uint32 row_size,
sky 2014/05/16 17:13:22 nit: when you wrap, one param per line.
63 const uint32 last_byte_mask, const uint32 y) {
64 // Set the padding bits to 1 to make mask matching easier.
65 *(data.get() + (y + 1)* row_size - 1) |= last_byte_mask;
66 for (uint32 i = y * row_size; i < (y + 1) * row_size; ++i) {
67 if (*(data.get() + i) != transparent_mask)
68 return false;
69 }
70 return true;
71 }
72
73 // Gets the vertical offset between specified cursor's hotpoint and it's bottom.
74 //
75 // Gets the cursor image data and extract cursor's visible height.
76 // Based on that get's what should be the vertical offset between cursor's
77 // hot point and the tooltip.
78 int CalculateCursorHeight(HCURSOR cursor_handle) {
79 base::win::ScopedGetDC hdc(NULL);
80
81 ICONINFO icon = {0};
82 GetIconInfo(cursor_handle, &icon);
83
84 BITMAPINFO bitmap_info = {0};
85 bitmap_info.bmiHeader.biSize = sizeof(bitmap_info.bmiHeader);
86 if (GetDIBits(hdc, icon.hbmMask, 0, 0, NULL, &bitmap_info, DIB_RGB_COLORS) ==
87 0)
88 return default_height;
89
90 // Rows are padded to full DWORDs. OR with this mask will set them to 1
91 // to simplify matching with |transparent_mask|.
92 uint32 last_byte_mask = ~0;
93 const unsigned char bits_to_shift = sizeof(last_byte_mask) * 8 -
94 (bitmap_info.bmiHeader.biWidth % bits_per_uint32);
95 if (bits_to_shift != bits_per_uint32) {
96 last_byte_mask = (last_byte_mask << bits_to_shift);
97 } else {
98 last_byte_mask = 0;
99 }
100
101 const uint32 row_size =
102 (bitmap_info.bmiHeader.biWidth + bits_per_uint32 - 1) / bits_per_uint32;
103 PixelData data(GetBitmapData(icon.hbmMask, bitmap_info, hdc));
104 if (data == nullptr)
sky 2014/05/16 17:13:22 no null check
Mateusz Szymański 2014/05/19 10:38:30 NULL here is return value indicating internal erro
105 return default_height;
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 GetCursorHeight() {
124 CURSORINFO cursor = {0};
125 cursor.cbSize = sizeof(cursor);
126 GetCursorInfo(&cursor);
127
128 if (cached_heights == nullptr) {
sky 2014/05/16 17:13:22 nit: no {}
129 cached_heights = new HeightStorage;
130 }
131
132 HeightStorage::const_iterator cached_height =
133 cached_heights->find(cursor.hCursor);
134 if (cached_height != cached_heights->end())
135 return cached_height->second;
136
137 const int height = CalculateCursorHeight(cursor.hCursor);
138 (*cached_heights)[cursor.hCursor] = height;
139
140 return height;
141 }
142
143 } // namespace corewm
144 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698