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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/corewm/cursor_height_provider_win.cc
diff --git a/ui/views/corewm/cursor_height_provider_win.cc b/ui/views/corewm/cursor_height_provider_win.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b48cbdffc1cfb3c2ee2b68f52f432e72a5e2ebb2
--- /dev/null
+++ b/ui/views/corewm/cursor_height_provider_win.cc
@@ -0,0 +1,144 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/views/corewm/cursor_height_provider_win.h"
+
+#include <windows.h>
+#include <map>
+
+#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/win/scoped_hdc.h"
+
+namespace {
+typedef scoped_ptr<uint32> PixelData;
sky 2014/05/16 17:13:22 uint32 is deprecated. Use uint32_t (same comment a
+typedef std::map<HCURSOR, int> HeightStorage;
+
+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
+// All bits are 1 for transparent portion of monochromatic mask.
+const uint32 transparent_mask = 0xffffffff;
sky 2014/05/16 17:13:22 Use k for constants, eg kTransparentMask.
+// This is height of default pointer arrow in Windows 7.
+const int default_height = 20;
+
+static HeightStorage* cached_heights = nullptr;
sky 2014/05/16 17:13:22 We're not using c++ 11 yet.
+
+// Extracts the pixel data of provided bitmap
+PixelData GetBitmapData(HBITMAP handle, const BITMAPINFO& info, HDC hdc) {
+ PixelData data(nullptr);
+ // Masks are monochromatic.
+ DCHECK_EQ(info.bmiHeader.biBitCount, 1);
+ if (info.bmiHeader.biBitCount != 1)
+ return data.Pass();
+
+ const size_t number_of_colors = 2;
+
+ const size_t header_and_palette =
+ sizeof(info.bmiHeader) + number_of_colors * sizeof(RGBQUAD);
+ // When getting pixel data palette is appended to memory pointed by
+ // BITMAPINFO passed so allocate additional memory to store additional data.
+ scoped_ptr<BITMAPINFO> header(
+ reinterpret_cast<BITMAPINFO*>(new char[header_and_palette]));
+ if (header == nullptr)
sky 2014/05/16 17:13:22 Don't null check news. If new fails we'll crash.
+ return data.Pass();
+ memcpy(header.get(), &(info.bmiHeader), sizeof(info.bmiHeader));
+
+ data.reset(new uint32[info.bmiHeader.biSizeImage / sizeof(uint32)]);
+ if (data == nullptr)
sky 2014/05/16 17:13:22 Same comment here about checking for null.
+ return data.Pass();
+
+ 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.
+ handle,
+ 0,
+ info.bmiHeader.biHeight,
+ data.get(),
+ header.get(),
+ DIB_RGB_COLORS);
+
+ return data.Pass();
+}
+
+// Checks if the specifed row is transparent in provided bitmap.
+bool IsRowTransparent(const PixelData& data, const uint32 row_size,
sky 2014/05/16 17:13:22 nit: when you wrap, one param per line.
+ const uint32 last_byte_mask, const uint32 y) {
+ // Set the padding bits to 1 to make mask matching easier.
+ *(data.get() + (y + 1)* row_size - 1) |= last_byte_mask;
+ for (uint32 i = y * row_size; i < (y + 1) * row_size; ++i) {
+ if (*(data.get() + i) != transparent_mask)
+ return false;
+ }
+ return true;
+}
+
+// Gets the vertical offset between specified cursor's hotpoint and it's bottom.
+//
+// Gets the cursor image data and extract cursor's visible height.
+// Based on that get's what should be the vertical offset between cursor's
+// hot point and the tooltip.
+int CalculateCursorHeight(HCURSOR cursor_handle) {
+ base::win::ScopedGetDC hdc(NULL);
+
+ ICONINFO icon = {0};
+ GetIconInfo(cursor_handle, &icon);
+
+ BITMAPINFO bitmap_info = {0};
+ bitmap_info.bmiHeader.biSize = sizeof(bitmap_info.bmiHeader);
+ if (GetDIBits(hdc, icon.hbmMask, 0, 0, NULL, &bitmap_info, DIB_RGB_COLORS) ==
+ 0)
+ return default_height;
+
+ // Rows are padded to full DWORDs. OR with this mask will set them to 1
+ // to simplify matching with |transparent_mask|.
+ uint32 last_byte_mask = ~0;
+ const unsigned char bits_to_shift = sizeof(last_byte_mask) * 8 -
+ (bitmap_info.bmiHeader.biWidth % bits_per_uint32);
+ if (bits_to_shift != bits_per_uint32) {
+ last_byte_mask = (last_byte_mask << bits_to_shift);
+ } else {
+ last_byte_mask = 0;
+ }
+
+ const uint32 row_size =
+ (bitmap_info.bmiHeader.biWidth + bits_per_uint32 - 1) / bits_per_uint32;
+ PixelData data(GetBitmapData(icon.hbmMask, bitmap_info, hdc));
+ 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
+ return default_height;
+
+ const int cursor_height = GetSystemMetrics(SM_CYCURSOR);
+ int i = bitmap_info.bmiHeader.biHeight - cursor_height;
+ for (; i < bitmap_info.bmiHeader.biHeight; ++i) {
+ if (!IsRowTransparent(data, row_size, last_byte_mask, i)) {
+ i--;
+ break;
+ }
+ }
+ return bitmap_info.bmiHeader.biHeight - i - icon.yHotspot;
+}
+
+} // namespace
+
+namespace views {
+namespace corewm {
+
+int GetCursorHeight() {
+ CURSORINFO cursor = {0};
+ cursor.cbSize = sizeof(cursor);
+ GetCursorInfo(&cursor);
+
+ if (cached_heights == nullptr) {
sky 2014/05/16 17:13:22 nit: no {}
+ cached_heights = new HeightStorage;
+ }
+
+ HeightStorage::const_iterator cached_height =
+ cached_heights->find(cursor.hCursor);
+ if (cached_height != cached_heights->end())
+ return cached_height->second;
+
+ const int height = CalculateCursorHeight(cursor.hCursor);
+ (*cached_heights)[cursor.hCursor] = height;
+
+ return height;
+}
+
+} // namespace corewm
+} // namespace views

Powered by Google App Engine
This is Rietveld 408576698