| OLD | NEW |
| (Empty) |
| 1 // Copyright 2004-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include "omaha/base/window_utils.h" | |
| 17 | |
| 18 #include "base/basictypes.h" | |
| 19 #include "omaha/base/constants.h" | |
| 20 #include "omaha/base/debug.h" | |
| 21 #include "omaha/base/error.h" | |
| 22 #include "omaha/base/logging.h" | |
| 23 #include "omaha/base/string.h" | |
| 24 | |
| 25 namespace omaha { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 struct FindProcessWindowsRecord { | |
| 30 uint32 process_id; | |
| 31 uint32 window_flags; | |
| 32 CSimpleArray<HWND>* windows; | |
| 33 }; | |
| 34 | |
| 35 BOOL CALLBACK FindProcessWindowsEnumProc(HWND hwnd, LPARAM lparam) { | |
| 36 FindProcessWindowsRecord* enum_record = | |
| 37 reinterpret_cast<FindProcessWindowsRecord*>(lparam); | |
| 38 ASSERT1(enum_record); | |
| 39 | |
| 40 DWORD process_id = 0; | |
| 41 ::GetWindowThreadProcessId(hwnd, &process_id); | |
| 42 | |
| 43 // Only count this window if it is in the right process | |
| 44 // and it satisfies all specified window requirements. | |
| 45 if (enum_record->process_id != process_id) { | |
| 46 return true; | |
| 47 } | |
| 48 if ((enum_record->window_flags & kWindowMustBeTopLevel) && | |
| 49 ::GetParent(hwnd)) { | |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 if ((enum_record->window_flags & kWindowMustHaveSysMenu) && | |
| 54 !(GetWindowLong(hwnd, GWL_STYLE) & WS_SYSMENU)) { | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 if ((enum_record->window_flags & kWindowMustBeVisible) && | |
| 59 !::IsWindowVisible(hwnd)) { | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 enum_record->windows->Add(hwnd); | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 bool WindowUtils::FindProcessWindows(uint32 process_id, | |
| 70 uint32 window_flags, | |
| 71 CSimpleArray<HWND>* windows) { | |
| 72 ASSERT1(windows); | |
| 73 windows->RemoveAll(); | |
| 74 FindProcessWindowsRecord enum_record = {0}; | |
| 75 enum_record.process_id = process_id; | |
| 76 enum_record.window_flags = window_flags; | |
| 77 enum_record.windows = windows; | |
| 78 ::EnumWindows(FindProcessWindowsEnumProc, | |
| 79 reinterpret_cast<LPARAM>(&enum_record)); | |
| 80 int num_windows = enum_record.windows->GetSize(); | |
| 81 return num_windows > 0; | |
| 82 } | |
| 83 | |
| 84 void WindowUtils::MakeWindowForeground(HWND wnd) { | |
| 85 if (!IsWindowVisible(wnd)) { | |
| 86 // If the window is hidden and we call SetWindowPos with SWP_SHOWWINDOW | |
| 87 // then the window will be visible. | |
| 88 // If the caller wants it visible they should do it themselves first. | |
| 89 return; | |
| 90 } | |
| 91 if (!SetWindowPos(wnd, | |
| 92 HWND_TOP, | |
| 93 0, | |
| 94 0, | |
| 95 0, | |
| 96 0, | |
| 97 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW)) { | |
| 98 UTIL_LOG(LE, (_T("[WindowUtils::MakeWindowForeground]") | |
| 99 _T("[SetWindowPos failed][0x%08x]"), HRESULTFromLastError())); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 bool WindowUtils::IsMainWindow(HWND wnd) { | |
| 104 return NULL == ::GetParent(wnd) && IsWindowVisible(wnd); | |
| 105 } | |
| 106 | |
| 107 bool WindowUtils::HasSystemMenu(HWND wnd) { | |
| 108 return (GetWindowLong(wnd, GWL_STYLE) & WS_SYSMENU) != 0; | |
| 109 } | |
| 110 | |
| 111 // The system displays the system large icon in the ALT+TAB dialog box. | |
| 112 // We do not need any small icon in the window caption. However, setting | |
| 113 // ICON_BIG has the side effect of the window displaying a scaled down | |
| 114 // version of it in the window caption. We could not find any way to | |
| 115 // hide that icon, including setting the icon to NULL or handling WM_GETICON | |
| 116 // message. | |
| 117 HRESULT WindowUtils::SetWindowIcon(HWND hwnd, WORD icon_id, HICON* hicon) { | |
| 118 ASSERT1(hwnd); | |
| 119 ASSERT1(hicon); | |
| 120 *hicon = NULL; | |
| 121 | |
| 122 const int cx = ::GetSystemMetrics(SM_CXICON); | |
| 123 const int cy = ::GetSystemMetrics(SM_CYICON); | |
| 124 HINSTANCE exe_instance = static_cast<HINSTANCE>(::GetModuleHandle(NULL)); | |
| 125 HICON icon = reinterpret_cast<HICON>(::LoadImage(exe_instance, | |
| 126 MAKEINTRESOURCE(icon_id), | |
| 127 IMAGE_ICON, | |
| 128 cx, | |
| 129 cy, | |
| 130 LR_DEFAULTCOLOR)); | |
| 131 if (!icon) { | |
| 132 HRESULT hr = HRESULTFromLastError(); | |
| 133 CORE_LOG(LE, (_T("[SetWindowIcon - LoadImage failed][0x%x]"), hr)); | |
| 134 return hr; | |
| 135 } | |
| 136 | |
| 137 ::SendMessage(hwnd, | |
| 138 WM_SETICON, | |
| 139 ICON_BIG, | |
| 140 reinterpret_cast<LPARAM>(icon)); | |
| 141 *hicon = icon; | |
| 142 return S_OK; | |
| 143 } | |
| 144 | |
| 145 | |
| 146 } // namespace omaha | |
| 147 | |
| OLD | NEW |