| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "app/win/win_util.h" | |
| 6 | |
| 7 #include <commdlg.h> | |
| 8 #include <shellapi.h> | |
| 9 | |
| 10 #include <algorithm> | |
| 11 | |
| 12 #include "base/base_switches.h" | |
| 13 #include "base/command_line.h" | |
| 14 #include "base/file_util.h" | |
| 15 #include "base/i18n/rtl.h" | |
| 16 #include "base/logging.h" | |
| 17 #include "base/scoped_handle.h" | |
| 18 #include "base/string_util.h" | |
| 19 #include "base/win/scoped_gdi_object.h" | |
| 20 #include "base/win/scoped_hdc.h" | |
| 21 #include "base/win/win_util.h" | |
| 22 #include "gfx/codec/png_codec.h" | |
| 23 #include "gfx/font.h" | |
| 24 #include "gfx/gdi_util.h" | |
| 25 #include "ui/base/l10n/l10n_util.h" | |
| 26 #include "ui/base/l10n/l10n_util_win.h" | |
| 27 | |
| 28 namespace app { | |
| 29 namespace win { | |
| 30 | |
| 31 const int kAutoHideTaskbarThicknessPx = 2; | |
| 32 | |
| 33 string16 FormatSystemTime(const SYSTEMTIME& time, const string16& format) { | |
| 34 // If the format string is empty, just use the default format. | |
| 35 LPCTSTR format_ptr = NULL; | |
| 36 if (!format.empty()) | |
| 37 format_ptr = format.c_str(); | |
| 38 | |
| 39 int buffer_size = GetTimeFormat(LOCALE_USER_DEFAULT, NULL, &time, format_ptr, | |
| 40 NULL, 0); | |
| 41 | |
| 42 string16 output; | |
| 43 GetTimeFormat(LOCALE_USER_DEFAULT, NULL, &time, format_ptr, | |
| 44 WriteInto(&output, buffer_size), buffer_size); | |
| 45 | |
| 46 return output; | |
| 47 } | |
| 48 | |
| 49 string16 FormatSystemDate(const SYSTEMTIME& date, const string16& format) { | |
| 50 // If the format string is empty, just use the default format. | |
| 51 LPCTSTR format_ptr = NULL; | |
| 52 if (!format.empty()) | |
| 53 format_ptr = format.c_str(); | |
| 54 | |
| 55 int buffer_size = GetDateFormat(LOCALE_USER_DEFAULT, NULL, &date, format_ptr, | |
| 56 NULL, 0); | |
| 57 | |
| 58 string16 output; | |
| 59 GetDateFormat(LOCALE_USER_DEFAULT, NULL, &date, format_ptr, | |
| 60 WriteInto(&output, buffer_size), buffer_size); | |
| 61 | |
| 62 return output; | |
| 63 } | |
| 64 | |
| 65 bool ConvertToLongPath(const string16& short_path, | |
| 66 string16* long_path) { | |
| 67 wchar_t long_path_buf[MAX_PATH]; | |
| 68 DWORD return_value = GetLongPathName(short_path.c_str(), long_path_buf, | |
| 69 MAX_PATH); | |
| 70 if (return_value != 0 && return_value < MAX_PATH) { | |
| 71 *long_path = long_path_buf; | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 bool IsDoubleClick(const POINT& origin, | |
| 79 const POINT& current, | |
| 80 DWORD elapsed_time) { | |
| 81 // The CXDOUBLECLK and CYDOUBLECLK system metrics describe the width and | |
| 82 // height of a rectangle around the origin position, inside of which clicks | |
| 83 // within the double click time are considered double clicks. | |
| 84 return (elapsed_time <= GetDoubleClickTime()) && | |
| 85 (abs(current.x - origin.x) <= (GetSystemMetrics(SM_CXDOUBLECLK) / 2)) && | |
| 86 (abs(current.y - origin.y) <= (GetSystemMetrics(SM_CYDOUBLECLK) / 2)); | |
| 87 } | |
| 88 | |
| 89 bool IsDrag(const POINT& origin, const POINT& current) { | |
| 90 // The CXDRAG and CYDRAG system metrics describe the width and height of a | |
| 91 // rectangle around the origin position, inside of which motion is not | |
| 92 // considered a drag. | |
| 93 return (abs(current.x - origin.x) > (GetSystemMetrics(SM_CXDRAG) / 2)) || | |
| 94 (abs(current.y - origin.y) > (GetSystemMetrics(SM_CYDRAG) / 2)); | |
| 95 } | |
| 96 | |
| 97 bool EdgeHasTopmostAutoHideTaskbar(UINT edge, HMONITOR monitor) { | |
| 98 APPBARDATA taskbar_data = { 0 }; | |
| 99 taskbar_data.cbSize = sizeof APPBARDATA; | |
| 100 taskbar_data.uEdge = edge; | |
| 101 HWND taskbar = reinterpret_cast<HWND>(SHAppBarMessage(ABM_GETAUTOHIDEBAR, | |
| 102 &taskbar_data)); | |
| 103 return ::IsWindow(taskbar) && (monitor != NULL) && | |
| 104 (MonitorFromWindow(taskbar, MONITOR_DEFAULTTONULL) == monitor) && | |
| 105 (GetWindowLong(taskbar, GWL_EXSTYLE) & WS_EX_TOPMOST); | |
| 106 } | |
| 107 | |
| 108 HANDLE GetSectionFromProcess(HANDLE section, HANDLE process, bool read_only) { | |
| 109 HANDLE valid_section = NULL; | |
| 110 DWORD access = STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ; | |
| 111 if (!read_only) | |
| 112 access |= FILE_MAP_WRITE; | |
| 113 DuplicateHandle(process, section, GetCurrentProcess(), &valid_section, access, | |
| 114 FALSE, 0); | |
| 115 return valid_section; | |
| 116 } | |
| 117 | |
| 118 HANDLE GetSectionForProcess(HANDLE section, HANDLE process, bool read_only) { | |
| 119 HANDLE valid_section = NULL; | |
| 120 DWORD access = STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ; | |
| 121 if (!read_only) | |
| 122 access |= FILE_MAP_WRITE; | |
| 123 DuplicateHandle(GetCurrentProcess(), section, process, &valid_section, access, | |
| 124 FALSE, 0); | |
| 125 return valid_section; | |
| 126 } | |
| 127 | |
| 128 void EnsureRectIsVisibleInRect(const gfx::Rect& parent_rect, | |
| 129 gfx::Rect* child_rect, | |
| 130 int padding) { | |
| 131 DCHECK(child_rect); | |
| 132 | |
| 133 // We use padding here because it allows some of the original web page to | |
| 134 // bleed through around the edges. | |
| 135 int twice_padding = padding * 2; | |
| 136 | |
| 137 // FIRST, clamp width and height so we don't open child windows larger than | |
| 138 // the containing parent. | |
| 139 if (child_rect->width() > (parent_rect.width() + twice_padding)) | |
| 140 child_rect->set_width(std::max(0, parent_rect.width() - twice_padding)); | |
| 141 if (child_rect->height() > parent_rect.height() + twice_padding) | |
| 142 child_rect->set_height(std::max(0, parent_rect.height() - twice_padding)); | |
| 143 | |
| 144 // SECOND, clamp x,y position to padding,padding so we don't position child | |
| 145 // windows in hyperspace. | |
| 146 // TODO(mpcomplete): I don't see what the second check in each 'if' does that | |
| 147 // isn't handled by the LAST set of 'ifs'. Maybe we can remove it. | |
| 148 if (child_rect->x() < parent_rect.x() || | |
| 149 child_rect->x() > parent_rect.right()) { | |
| 150 child_rect->set_x(parent_rect.x() + padding); | |
| 151 } | |
| 152 if (child_rect->y() < parent_rect.y() || | |
| 153 child_rect->y() > parent_rect.bottom()) { | |
| 154 child_rect->set_y(parent_rect.y() + padding); | |
| 155 } | |
| 156 | |
| 157 // LAST, nudge the window back up into the client area if its x,y position is | |
| 158 // within the parent bounds but its width/height place it off-screen. | |
| 159 if (child_rect->bottom() > parent_rect.bottom()) | |
| 160 child_rect->set_y(parent_rect.bottom() - child_rect->height() - padding); | |
| 161 if (child_rect->right() > parent_rect.right()) | |
| 162 child_rect->set_x(parent_rect.right() - child_rect->width() - padding); | |
| 163 } | |
| 164 | |
| 165 gfx::Rect GetMonitorBoundsForRect(const gfx::Rect& rect) { | |
| 166 RECT p_rect = rect.ToRECT(); | |
| 167 HMONITOR monitor = MonitorFromRect(&p_rect, MONITOR_DEFAULTTONEAREST); | |
| 168 if (monitor) { | |
| 169 MONITORINFO mi = {0}; | |
| 170 mi.cbSize = sizeof(mi); | |
| 171 GetMonitorInfo(monitor, &mi); | |
| 172 return gfx::Rect(mi.rcWork); | |
| 173 } | |
| 174 NOTREACHED(); | |
| 175 return gfx::Rect(); | |
| 176 } | |
| 177 | |
| 178 bool IsNumPadDigit(int key_code, bool extended_key) { | |
| 179 if (key_code >= VK_NUMPAD0 && key_code <= VK_NUMPAD9) | |
| 180 return true; | |
| 181 | |
| 182 // Check for num pad keys without NumLock. | |
| 183 // Note: there is no easy way to know if a the key that was pressed comes from | |
| 184 // the num pad or the rest of the keyboard. Investigating how | |
| 185 // TranslateMessage() generates the WM_KEYCHAR from an | |
| 186 // ALT + <NumPad sequences> it appears it looks at the extended key flag | |
| 187 // (which is on if the key pressed comes from one of the 3 clusters to | |
| 188 // the left of the numeric keypad). So we use it as well. | |
| 189 return !extended_key && | |
| 190 ((key_code >= VK_PRIOR && key_code <= VK_DOWN) || // All keys but 5 | |
| 191 // and 0. | |
| 192 (key_code == VK_CLEAR) || // Key 5. | |
| 193 (key_code == VK_INSERT)); // Key 0. | |
| 194 } | |
| 195 | |
| 196 void GrabWindowSnapshot(HWND window_handle, | |
| 197 std::vector<unsigned char>* png_representation) { | |
| 198 // Create a memory DC that's compatible with the window. | |
| 199 HDC window_hdc = GetWindowDC(window_handle); | |
| 200 base::win::ScopedHDC mem_hdc(CreateCompatibleDC(window_hdc)); | |
| 201 | |
| 202 // Create a DIB that's the same size as the window. | |
| 203 RECT content_rect = {0, 0, 0, 0}; | |
| 204 ::GetWindowRect(window_handle, &content_rect); | |
| 205 content_rect.right++; // Match what PrintWindow wants. | |
| 206 int width = content_rect.right - content_rect.left; | |
| 207 int height = content_rect.bottom - content_rect.top; | |
| 208 BITMAPINFOHEADER hdr; | |
| 209 gfx::CreateBitmapHeader(width, height, &hdr); | |
| 210 unsigned char *bit_ptr = NULL; | |
| 211 base::win::ScopedBitmap bitmap( | |
| 212 CreateDIBSection(mem_hdc, | |
| 213 reinterpret_cast<BITMAPINFO*>(&hdr), | |
| 214 DIB_RGB_COLORS, | |
| 215 reinterpret_cast<void **>(&bit_ptr), | |
| 216 NULL, 0)); | |
| 217 | |
| 218 SelectObject(mem_hdc, bitmap); | |
| 219 // Clear the bitmap to white (so that rounded corners on windows | |
| 220 // show up on a white background, and strangely-shaped windows | |
| 221 // look reasonable). Not capturing an alpha mask saves a | |
| 222 // bit of space. | |
| 223 PatBlt(mem_hdc, 0, 0, width, height, WHITENESS); | |
| 224 // Grab a copy of the window | |
| 225 // First, see if PrintWindow is defined (it's not in Windows 2000). | |
| 226 typedef BOOL (WINAPI *PrintWindowPointer)(HWND, HDC, UINT); | |
| 227 PrintWindowPointer print_window = | |
| 228 reinterpret_cast<PrintWindowPointer>( | |
| 229 GetProcAddress(GetModuleHandle(L"User32.dll"), "PrintWindow")); | |
| 230 | |
| 231 // If PrintWindow is defined, use it. It will work on partially | |
| 232 // obscured windows, and works better for out of process sub-windows. | |
| 233 // Otherwise grab the bits we can get with BitBlt; it's better | |
| 234 // than nothing and will work fine in the average case (window is | |
| 235 // completely on screen). | |
| 236 if (print_window) | |
| 237 (*print_window)(window_handle, mem_hdc, 0); | |
| 238 else | |
| 239 BitBlt(mem_hdc, 0, 0, width, height, window_hdc, 0, 0, SRCCOPY); | |
| 240 | |
| 241 // We now have a copy of the window contents in a DIB, so | |
| 242 // encode it into a useful format for posting to the bug report | |
| 243 // server. | |
| 244 gfx::PNGCodec::Encode(bit_ptr, gfx::PNGCodec::FORMAT_BGRA, | |
| 245 width, height, width * 4, true, | |
| 246 png_representation); | |
| 247 | |
| 248 ReleaseDC(window_handle, window_hdc); | |
| 249 } | |
| 250 | |
| 251 bool IsWindowActive(HWND hwnd) { | |
| 252 WINDOWINFO info; | |
| 253 return ::GetWindowInfo(hwnd, &info) && | |
| 254 ((info.dwWindowStatus & WS_ACTIVECAPTION) != 0); | |
| 255 } | |
| 256 | |
| 257 bool IsReservedName(const string16& filename) { | |
| 258 // This list is taken from the MSDN article "Naming a file" | |
| 259 // http://msdn2.microsoft.com/en-us/library/aa365247(VS.85).aspx | |
| 260 // I also added clock$ because GetSaveFileName seems to consider it as a | |
| 261 // reserved name too. | |
| 262 static const wchar_t* const known_devices[] = { | |
| 263 L"con", L"prn", L"aux", L"nul", L"com1", L"com2", L"com3", L"com4", L"com5", | |
| 264 L"com6", L"com7", L"com8", L"com9", L"lpt1", L"lpt2", L"lpt3", L"lpt4", | |
| 265 L"lpt5", L"lpt6", L"lpt7", L"lpt8", L"lpt9", L"clock$" | |
| 266 }; | |
| 267 string16 filename_lower = StringToLowerASCII(filename); | |
| 268 | |
| 269 for (int i = 0; i < arraysize(known_devices); ++i) { | |
| 270 // Exact match. | |
| 271 if (filename_lower == known_devices[i]) | |
| 272 return true; | |
| 273 // Starts with "DEVICE.". | |
| 274 if (filename_lower.find(string16(known_devices[i]) + L".") == 0) | |
| 275 return true; | |
| 276 } | |
| 277 | |
| 278 static const wchar_t* const magic_names[] = { | |
| 279 // These file names are used by the "Customize folder" feature of the shell. | |
| 280 L"desktop.ini", | |
| 281 L"thumbs.db", | |
| 282 }; | |
| 283 | |
| 284 for (int i = 0; i < arraysize(magic_names); ++i) { | |
| 285 if (filename_lower == magic_names[i]) | |
| 286 return true; | |
| 287 } | |
| 288 | |
| 289 return false; | |
| 290 } | |
| 291 | |
| 292 // In addition to passing the RTL flags to ::MessageBox if we are running in an | |
| 293 // RTL locale, we need to make sure that LTR strings are rendered correctly by | |
| 294 // adding the appropriate Unicode directionality marks. | |
| 295 int MessageBox(HWND hwnd, | |
| 296 const string16& text, | |
| 297 const string16& caption, | |
| 298 UINT flags) { | |
| 299 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoMessageBox)) | |
| 300 return IDOK; | |
| 301 | |
| 302 UINT actual_flags = flags; | |
| 303 if (base::i18n::IsRTL()) | |
| 304 actual_flags |= MB_RIGHT | MB_RTLREADING; | |
| 305 | |
| 306 string16 localized_text = text; | |
| 307 base::i18n::AdjustStringForLocaleDirection(&localized_text); | |
| 308 const wchar_t* text_ptr = localized_text.c_str(); | |
| 309 | |
| 310 string16 localized_caption = caption; | |
| 311 base::i18n::AdjustStringForLocaleDirection(&localized_caption); | |
| 312 const wchar_t* caption_ptr = localized_caption.c_str(); | |
| 313 | |
| 314 return ::MessageBox(hwnd, text_ptr, caption_ptr, actual_flags); | |
| 315 } | |
| 316 | |
| 317 gfx::Font GetWindowTitleFont() { | |
| 318 NONCLIENTMETRICS ncm; | |
| 319 base::win::GetNonClientMetrics(&ncm); | |
| 320 l10n_util::AdjustUIFont(&(ncm.lfCaptionFont)); | |
| 321 base::win::ScopedHFONT caption_font(CreateFontIndirect(&(ncm.lfCaptionFont))); | |
| 322 return gfx::Font(caption_font); | |
| 323 } | |
| 324 | |
| 325 } // namespace win | |
| 326 } // namespace app | |
| OLD | NEW |