OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2009 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 #ifndef CHROME_COMMON_WIN_UTIL_H_ | |
6 #define CHROME_COMMON_WIN_UTIL_H_ | |
7 | |
8 #include <objbase.h> | |
9 | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "app/gfx/chrome_font.h" | |
14 #include "base/fix_wp64.h" | |
15 #include "base/gfx/rect.h" | |
16 #include "base/scoped_handle.h" | |
17 | |
18 class FilePath; | |
19 | |
20 namespace win_util { | |
21 | |
22 // Import ScopedHandle and friends into this namespace for backwards | |
23 // compatibility. TODO(darin): clean this up! | |
24 using ::ScopedHandle; | |
25 using ::ScopedFindFileHandle; | |
26 using ::ScopedHDC; | |
27 using ::ScopedBitmap; | |
28 using ::ScopedHRGN; | |
29 | |
30 // Simple scoped memory releaser class for COM allocated memory. | |
31 // Example: | |
32 // CoMemReleaser<ITEMIDLIST> file_item; | |
33 // SHGetSomeInfo(&file_item, ...); | |
34 // ... | |
35 // return; <-- memory released | |
36 template<typename T> | |
37 class CoMemReleaser { | |
38 public: | |
39 explicit CoMemReleaser() : mem_ptr_(NULL) {} | |
40 | |
41 ~CoMemReleaser() { | |
42 if (mem_ptr_) | |
43 CoTaskMemFree(mem_ptr_); | |
44 } | |
45 | |
46 T** operator&() { | |
47 return &mem_ptr_; | |
48 } | |
49 | |
50 operator T*() { | |
51 return mem_ptr_; | |
52 } | |
53 | |
54 private: | |
55 T* mem_ptr_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(CoMemReleaser); | |
58 }; | |
59 | |
60 // Initializes COM in the constructor (STA), and uninitializes COM in the | |
61 // destructor. | |
62 class ScopedCOMInitializer { | |
63 public: | |
64 ScopedCOMInitializer() : hr_(CoInitialize(NULL)) { | |
65 } | |
66 | |
67 ScopedCOMInitializer::~ScopedCOMInitializer() { | |
68 if (SUCCEEDED(hr_)) | |
69 CoUninitialize(); | |
70 } | |
71 | |
72 // Returns the error code from CoInitialize(NULL) | |
73 // (called in constructor) | |
74 inline HRESULT error_code() const { | |
75 return hr_; | |
76 } | |
77 | |
78 protected: | |
79 HRESULT hr_; | |
80 | |
81 private: | |
82 DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer); | |
83 }; | |
84 | |
85 // Creates a string interpretation of the time of day represented by the given | |
86 // SYSTEMTIME that's appropriate for the user's default locale. | |
87 // Format can be an empty string (for the default format), or a "format picture" | |
88 // as specified in the Windows documentation for GetTimeFormat(). | |
89 std::wstring FormatSystemTime(const SYSTEMTIME& time, | |
90 const std::wstring& format); | |
91 | |
92 // Creates a string interpretation of the date represented by the given | |
93 // SYSTEMTIME that's appropriate for the user's default locale. | |
94 // Format can be an empty string (for the default format), or a "format picture" | |
95 // as specified in the Windows documentation for GetDateFormat(). | |
96 std::wstring FormatSystemDate(const SYSTEMTIME& date, | |
97 const std::wstring& format); | |
98 | |
99 // Returns the long path name given a short path name. A short path name | |
100 // is a path that follows the 8.3 convention and has ~x in it. If the | |
101 // path is already a long path name, the function returns the current | |
102 // path without modification. | |
103 bool ConvertToLongPath(const std::wstring& short_path, std::wstring* long_path); | |
104 | |
105 // Returns true if the current point is close enough to the origin point in | |
106 // space and time that it would be considered a double click. | |
107 bool IsDoubleClick(const POINT& origin, | |
108 const POINT& current, | |
109 DWORD elapsed_time); | |
110 | |
111 // Returns true if the current point is far enough from the origin that it | |
112 // would be considered a drag. | |
113 bool IsDrag(const POINT& origin, const POINT& current); | |
114 | |
115 // Returns true if we are on Windows Vista and composition is enabled | |
116 bool ShouldUseVistaFrame(); | |
117 | |
118 // Open or run a file via the Windows shell. In the event that there is no | |
119 // default application registered for the file specified by 'full_path', | |
120 // ask the user, via the Windows "Open With" dialog, for an application to use | |
121 // if 'ask_for_app' is true. | |
122 // Returns 'true' on successful open, 'false' otherwise. | |
123 bool OpenItemViaShell(const FilePath& full_path, bool ask_for_app); | |
124 | |
125 // The download manager now writes the alternate data stream with the | |
126 // zone on all downloads. This function is equivalent to OpenItemViaShell | |
127 // without showing the zone warning dialog. | |
128 bool OpenItemViaShellNoZoneCheck(const FilePath& full_path, | |
129 bool ask_for_app); | |
130 | |
131 // Ask the user, via the Windows "Open With" dialog, for an application to use | |
132 // to open the file specified by 'full_path'. | |
133 // Returns 'true' on successful open, 'false' otherwise. | |
134 bool OpenItemWithExternalApp(const std::wstring& full_path); | |
135 | |
136 // Set up a filter for a Save/Open dialog, which will consist of |file_ext| file | |
137 // extensions (internally separated by semicolons), |ext_desc| as the text | |
138 // descriptions of the |file_ext| types (optional), and (optionally) the default | |
139 // 'All Files' view. The purpose of the filter is to show only files of a | |
140 // particular type in a Windows Save/Open dialog box. The resulting filter is | |
141 // returned. The filters created here are: | |
142 // 1. only files that have 'file_ext' as their extension | |
143 // 2. all files (only added if 'include_all_files' is true) | |
144 // Example: | |
145 // file_ext: { "*.txt", "*.htm;*.html" } | |
146 // ext_desc: { "Text Document" } | |
147 // returned: "Text Document\0*.txt\0HTML Document\0*.htm;*.html\0" | |
148 // "All Files\0*.*\0\0" (in one big string) | |
149 // If a description is not provided for a file extension, it will be retrieved | |
150 // from the registry. If the file extension does not exist in the registry, it | |
151 // will be omitted from the filter, as it is likely a bogus extension. | |
152 std::wstring FormatFilterForExtensions( | |
153 const std::vector<std::wstring>& file_ext, | |
154 const std::vector<std::wstring>& ext_desc, | |
155 bool include_all_files); | |
156 | |
157 // Prompt the user for location to save a file. 'suggested_name' is a full path | |
158 // that gives the dialog box a hint as to how to initialize itself. | |
159 // For example, a 'suggested_name' of: | |
160 // "C:\Documents and Settings\jojo\My Documents\picture.png" | |
161 // will start the dialog in the "C:\Documents and Settings\jojo\My Documents\" | |
162 // directory, and filter for .png file types. | |
163 // 'owner' is the window to which the dialog box is modal, NULL for a modeless | |
164 // dialog box. | |
165 // On success, returns true and 'final_name' contains the full path of the file | |
166 // that the user chose. On error, returns false, and 'final_name' is not | |
167 // modified. | |
168 // NOTE: DO NOT CALL THIS FUNCTION DIRECTLY! Instead use the helper objects in | |
169 // browser/shell_dialogs.cc to do this asynchronously on a different | |
170 // thread so that the app isn't jankified if the Windows shell dialog | |
171 // takes a long time to display. | |
172 bool SaveFileAs(HWND owner, | |
173 const std::wstring& suggested_name, | |
174 std::wstring* final_name); | |
175 | |
176 // Prompt the user for location to save a file. | |
177 // Callers should provide the filter string, and also a filter index. | |
178 // The parameter |index| indicates the initial index of filter description | |
179 // and filter pattern for the dialog box. If |index| is zero or greater than | |
180 // the number of total filter types, the system uses the first filter in the | |
181 // |filter| buffer. |index| is used to specify the initial selected extension, | |
182 // and when done contains the extension the user chose. The parameter | |
183 // |final_name| returns the file name which contains the drive designator, | |
184 // path, file name, and extension of the user selected file name. |def_ext| is | |
185 // the default extension to give to the file if the user did not enter an | |
186 // extension. If |ignore_suggested_ext| is true, any file extension contained in | |
187 // |suggested_name| will not be used to generate the file name. This is useful | |
188 // in the case of saving web pages, where we know the extension type already and | |
189 // where |suggested_name| may contain a '.' character as a valid part of the | |
190 // name, thus confusing our extension detection code. | |
191 bool SaveFileAsWithFilter(HWND owner, | |
192 const std::wstring& suggested_name, | |
193 const std::wstring& filter, | |
194 const std::wstring& def_ext, | |
195 bool ignore_suggested_ext, | |
196 unsigned* index, | |
197 std::wstring* final_name); | |
198 | |
199 // This function takes the output of a SaveAs dialog: a filename, a filter and | |
200 // the extension originally suggested to the user (shown in the dialog box) and | |
201 // returns back the filename with the appropriate extension tacked on. For | |
202 // example, if you pass in 'foo' as filename with filter '*.jpg' this function | |
203 // will return 'foo.jpg'. It respects MIME types, so if you pass in 'foo.jpeg' | |
204 // with filer '*.jpg' it will return 'foo.jpeg' (will not append .jpg). | |
205 // |filename| should contain the filename selected in the SaveAs dialog box and | |
206 // may include the path, |filter_selected| should be '*.something', for example | |
207 // '*.*' or it can be blank (which is treated as *.*). |suggested_ext| should | |
208 // contain the extension without the dot (.) in front, for example 'jpg'. | |
209 std::wstring AppendExtensionIfNeeded(const std::wstring& filename, | |
210 const std::wstring& filter_selected, | |
211 const std::wstring& suggested_ext); | |
212 | |
213 // If the window does not fit on the default monitor, it is moved and possibly | |
214 // resized appropriately. | |
215 void AdjustWindowToFit(HWND hwnd); | |
216 | |
217 // Sizes the window to have a client or window size (depending on the value of | |
218 // |pref_is_client|) of pref, then centers the window over parent, ensuring the | |
219 // window fits on screen. | |
220 void CenterAndSizeWindow(HWND parent, HWND window, const SIZE& pref, | |
221 bool pref_is_client); | |
222 | |
223 // Returns true if edge |edge| (one of ABE_LEFT, TOP, RIGHT, or BOTTOM) of | |
224 // monitor |monitor| has an auto-hiding taskbar that's always-on-top. | |
225 bool EdgeHasTopmostAutoHideTaskbar(UINT edge, HMONITOR monitor); | |
226 | |
227 // Duplicates a section handle from another process to the current process. | |
228 // Returns the new valid handle if the function succeed. NULL otherwise. | |
229 HANDLE GetSectionFromProcess(HANDLE section, HANDLE process, bool read_only); | |
230 | |
231 // Returns true if the specified window is the current active top window or one | |
232 // of its children. | |
233 bool DoesWindowBelongToActiveWindow(HWND window); | |
234 | |
235 // Adjusts the value of |child_rect| if necessary to ensure that it is | |
236 // completely visible within |parent_rect|. | |
237 void EnsureRectIsVisibleInRect(const gfx::Rect& parent_rect, | |
238 gfx::Rect* child_rect, | |
239 int padding); | |
240 | |
241 // Ensures that the child window stays within the boundaries of the parent | |
242 // before setting its bounds. If |parent_window| is NULL, the bounds of the | |
243 // parent are assumed to be the bounds of the monitor that |child_window| is | |
244 // nearest to. If |child_window| isn't visible yet and |insert_after_window| | |
245 // is non-NULL and visible, the monitor |insert_after_window| is on is used | |
246 // as the parent bounds instead. | |
247 void SetChildBounds(HWND child_window, HWND parent_window, | |
248 HWND insert_after_window, const gfx::Rect& bounds, | |
249 int padding, unsigned long flags); | |
250 | |
251 // Returns the bounds for the monitor that contains the largest area of | |
252 // intersection with the specified rectangle. | |
253 gfx::Rect GetMonitorBoundsForRect(const gfx::Rect& rect); | |
254 | |
255 // Returns true if the virtual key code is a digit coming from the numeric | |
256 // keypad (with or without NumLock on). |extended_key| should be set to the | |
257 // extended key flag specified in the WM_KEYDOWN/UP where the |key_code| | |
258 // originated. | |
259 bool IsNumPadDigit(int key_code, bool extended_key); | |
260 | |
261 // Grabs a snapshot of the designated window and stores a PNG representation | |
262 // into a byte vector. | |
263 void GrabWindowSnapshot(HWND window_handle, | |
264 std::vector<unsigned char>* png_representation); | |
265 | |
266 // Returns whether the specified window is the current active window. | |
267 bool IsWindowActive(HWND hwnd); | |
268 | |
269 // Returns whether the specified file name is a reserved name on windows. | |
270 // This includes names like "com2.zip" (which correspond to devices) and | |
271 // desktop.ini and thumbs.db which have special meaning to the windows shell. | |
272 bool IsReservedName(const std::wstring& filename); | |
273 | |
274 // Returns whether the specified extension is automatically integrated into the | |
275 // windows shell. | |
276 bool IsShellIntegratedExtension(const std::wstring& eextension); | |
277 | |
278 // A wrapper around Windows' MessageBox function. Using a Chrome specific | |
279 // MessageBox function allows us to control certain RTL locale flags so that | |
280 // callers don't have to worry about adding these flags when running in a | |
281 // right-to-left locale. | |
282 int MessageBox(HWND hwnd, | |
283 const std::wstring& text, | |
284 const std::wstring& caption, | |
285 UINT flags); | |
286 | |
287 // Returns the system set window title font. | |
288 ChromeFont GetWindowTitleFont(); | |
289 | |
290 // The thickness of an auto-hide taskbar in pixels. | |
291 extern const int kAutoHideTaskbarThicknessPx; | |
292 | |
293 } // namespace win_util | |
294 | |
295 #endif // CHROME_COMMON_WIN_UTIL_H_ | |
OLD | NEW |