compatibility changes for correct start menu subentries on Windows 9x
[reactos.git] / reactos / subsys / system / explorer / utility / utility.h
1 /*
2 * Copyright 2003 Martin Fuchs
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 //
21 // Explorer clone
22 //
23 // utility.h
24 //
25 // Martin Fuchs, 23.07.2003
26 //
27
28
29 // standard windows headers
30 #define WIN32_LEAN_AND_MEAN
31 #define WIN32_EXTRA_LEAN
32 #include <windows.h>
33
34 // Unicode support
35 #ifdef UNICODE
36 #define _UNICODE
37 #endif
38 #include <tchar.h>
39
40 #include <windowsx.h> // for SelectBrush(), ListBox_SetSel(), SubclassWindow(), ...
41 #include <commctrl.h>
42
43 #include <malloc.h> // for alloca()
44 #include <assert.h>
45 #include <stdlib.h> // for _MAX_DIR, ...
46 #include <stdio.h> // for sprintf()
47 #include <time.h>
48
49 #ifndef _MAX_PATH
50 #define _MAX_DRIVE 3
51 #define _MAX_FNAME 256
52 #define _MAX_DIR _MAX_FNAME
53 #define _MAX_EXT _MAX_FNAME
54 #define _MAX_PATH 260
55 #endif
56
57
58 #ifdef __cplusplus
59
60 #ifdef _MSC_VER
61 #pragma warning(disable: 4786) // disable warnings about too long debug information symbols
62 #endif
63
64 // STL headers for strings and streams
65 #include <string>
66 #include <iostream>
67 using namespace std;
68
69 #if _MSC_VER>=1300 // VS.Net
70 #define _NO_COMUTIL //@@
71 #endif
72
73 #if defined(_MSC_VER) && !defined(_NO_COMUTIL)
74
75 // COM utility headers
76 #include <comdef.h>
77 using namespace _com_util;
78
79 #endif // _MSC_VER && !_NO_COMUTIL
80
81
82 #define for if (0) {} else for
83
84
85 #define BUFFER_LEN 1024
86
87
88 struct CommonControlInit
89 {
90 CommonControlInit(DWORD flags=ICC_LISTVIEW_CLASSES|ICC_TREEVIEW_CLASSES|ICC_BAR_CLASSES|ICC_PROGRESS_CLASS|ICC_COOL_CLASSES)
91 {
92 INITCOMMONCONTROLSEX icc = {sizeof(INITCOMMONCONTROLSEX), flags};
93
94 InitCommonControlsEx(&icc);
95 }
96 };
97
98
99 /// wait cursor
100
101 struct WaitCursor //TODO: integrate with WM_SETCURSOR to enable multithreaded background tasks as program launching
102 {
103 WaitCursor()
104 {
105 _old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
106 }
107
108 ~WaitCursor()
109 {
110 SetCursor(_old_cursor);
111 }
112
113 protected:
114 HCURSOR _old_cursor;
115 };
116
117
118 struct WindowHandle
119 {
120 WindowHandle(HWND hwnd=0)
121 : _hwnd(hwnd) {}
122
123 operator HWND() const {return _hwnd;}
124 HWND* operator&() {return &_hwnd;}
125
126 protected:
127 HWND _hwnd;
128 };
129
130
131 /// critical section wrapper
132
133 struct CritSect : public CRITICAL_SECTION
134 {
135 CritSect()
136 {
137 InitializeCriticalSection(this);
138 }
139
140 ~CritSect()
141 {
142 DeleteCriticalSection(this);
143 }
144 };
145
146
147 /// Lock protects a code section utilizing a critical section
148
149 struct Lock
150 {
151 Lock(CritSect& crit_sect)
152 : _crit_sect(crit_sect)
153 {
154 EnterCriticalSection(&crit_sect);
155 }
156
157 ~Lock()
158 {
159 LeaveCriticalSection(&_crit_sect);
160 }
161
162 protected:
163 CritSect& _crit_sect;
164 };
165
166
167 // window utilities
168
169 struct ClientRect : public RECT
170 {
171 ClientRect(HWND hwnd)
172 {
173 GetClientRect(hwnd, this);
174 }
175
176 operator LPRECT() {return this;}
177 };
178
179 struct WindowRect : public RECT
180 {
181 WindowRect(HWND hwnd)
182 {
183 GetWindowRect(hwnd, this);
184 }
185
186 operator LPRECT() {return this;}
187 };
188
189 struct Point : public POINT
190 {
191 Point(LONG x_, LONG y_)
192 {
193 x = x_;
194 y = y_;
195 }
196
197 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
198 Point(LPARAM lparam)
199 {
200 x = GET_X_LPARAM(lparam);
201 y = GET_Y_LPARAM(lparam);
202 }
203
204 operator LPPOINT() {return this;}
205 };
206
207
208 struct FullScreenParameters
209 {
210 FullScreenParameters()
211 : _mode(FALSE)
212 {
213 }
214
215 BOOL _mode;
216 RECT _orgPos;
217 BOOL _wasZoomed;
218 };
219
220
221 // drawing utilities
222
223 struct PaintCanvas : public PAINTSTRUCT
224 {
225 PaintCanvas(HWND hwnd)
226 : _hwnd(hwnd)
227 {
228 BeginPaint(hwnd, this);
229 }
230
231 ~PaintCanvas()
232 {
233 EndPaint(_hwnd, this);
234 }
235
236 operator HDC() const {return hdc;}
237
238 protected:
239 HWND _hwnd;
240 };
241
242 struct Canvas
243 {
244 Canvas(HDC hdc) : _hdc(hdc) {}
245
246 operator HDC() {return _hdc;}
247
248 protected:
249 HDC _hdc;
250 };
251
252 struct WindowCanvas : public Canvas
253 {
254 WindowCanvas(HWND hwnd)
255 : Canvas(GetDC(hwnd)), _hwnd(hwnd) {}
256
257 ~WindowCanvas() {ReleaseDC(_hwnd, _hdc);}
258
259 protected:
260 HWND _hwnd;
261 };
262
263
264 // double buffering classes
265
266 struct MemCanvas : public Canvas
267 {
268 MemCanvas(HDC hdc=0)
269 : Canvas(CreateCompatibleDC(hdc)) {assert(_hdc);}
270
271 ~MemCanvas() {DeleteDC(_hdc);}
272 };
273
274 struct SelectedBitmap
275 {
276 SelectedBitmap(HDC hdc, HBITMAP hbmp)
277 : _hdc(hdc), _old_hbmp(SelectBitmap(hdc, hbmp)) {}
278
279 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc, _old_hbmp));}
280
281 protected:
282 HDC _hdc;
283 HBITMAP _old_hbmp;
284 };
285
286 struct BufferCanvas : public MemCanvas
287 {
288 BufferCanvas(HDC hdc, int x, int y, int w, int h)
289 : MemCanvas(hdc), _hdctarg(hdc),
290 _x(x), _y(y), _w(w), _h(h),
291 _bmp(_hdc, CreateCompatibleBitmap(hdc, w, h)) {}
292
293 BufferCanvas(HDC hdc, const RECT& rect)
294 : MemCanvas(hdc), _hdctarg(hdc),
295 _x(rect.left), _y(rect.top), _w(rect.right-rect.left), _h(rect.bottom-rect.top),
296 _bmp(_hdc, CreateCompatibleBitmap(hdc, _w, _h)) {}
297
298 protected:
299 HDC _hdctarg;
300 int _x, _y, _w, _h;
301 SelectedBitmap _bmp;
302 };
303
304 struct BufferedCanvas : public BufferCanvas
305 {
306 BufferedCanvas(HDC hdc, int x, int y, int w, int h, DWORD mode=SRCCOPY)
307 : BufferCanvas(hdc, x, y, w, h), _mode(mode) {}
308
309 BufferedCanvas(HDC hdc, const RECT& rect, DWORD mode=SRCCOPY)
310 : BufferCanvas(hdc, rect), _mode(mode) {}
311
312 ~BufferedCanvas() {BitBlt(_hdctarg, _x, _y, _w, _h, _hdc, 0, 0, _mode);}
313
314 DWORD _mode;
315 };
316
317 struct BufferedPaintCanvas : public PaintCanvas, public BufferedCanvas
318 {
319 BufferedPaintCanvas(HWND hwnd)
320 : PaintCanvas(hwnd),
321 BufferedCanvas(PAINTSTRUCT::hdc, 0, 0, rcPaint.right, rcPaint.bottom)
322 {
323 }
324
325 operator HDC() {return BufferedCanvas::_hdc;}
326 };
327
328
329 struct TextColor
330 {
331 TextColor(HDC hdc, COLORREF color)
332 : _hdc(hdc), _old_color(SetTextColor(hdc, color)) {}
333
334 ~TextColor() {SetTextColor(_hdc, _old_color);}
335
336 protected:
337 HDC _hdc;
338 COLORREF _old_color;
339 };
340
341 struct BkMode
342 {
343 BkMode(HDC hdc, int bkmode)
344 : _hdc(hdc), _old_bkmode(SetBkMode(hdc, bkmode)) {}
345
346 ~BkMode() {SetBkMode(_hdc, _old_bkmode);}
347
348 protected:
349 HDC _hdc;
350 COLORREF _old_bkmode;
351 };
352
353 struct FontSelection
354 {
355 FontSelection(HDC hdc, HFONT hFont)
356 : _hdc(hdc), _old_hFont(SelectFont(hdc, hFont)) {}
357
358 ~FontSelection() {SelectFont(_hdc, _old_hFont);}
359
360 protected:
361 HDC _hdc;
362 HFONT _old_hFont;
363 };
364
365 struct BitmapSelection
366 {
367 BitmapSelection(HDC hdc, HBITMAP hBmp)
368 : _hdc(hdc), _old_hBmp(SelectBitmap(hdc, hBmp)) {}
369
370 ~BitmapSelection() {SelectBitmap(_hdc, _old_hBmp);}
371
372 protected:
373 HDC _hdc;
374 HBITMAP _old_hBmp;
375 };
376
377
378 struct String
379 #ifdef UNICODE
380 : public wstring
381 #else
382 : public string
383 #endif
384 {
385 #ifdef UNICODE
386 typedef wstring super;
387 #else
388 typedef string super;
389 #endif
390
391 String() {}
392 String(LPCTSTR s) : super(s) {}
393 String(const String& other) : super(other) {}
394
395 String& operator=(LPCTSTR s) {assign(s); return *this;}
396 operator LPCTSTR() const {return c_str();}
397 };
398
399
400 /// link dynamicly to functions by using GetModuleHandle() and GetProcAddress()
401 template<typename FCT> struct DynamicFct
402 {
403 DynamicFct(LPCTSTR moduleName, UINT ordinal)
404 {
405 HMODULE hModule = GetModuleHandle(moduleName);
406
407 _fct = (FCT) GetProcAddress(hModule, (LPCSTR)ordinal);
408 }
409
410 FCT operator*() const {return _fct;}
411 operator bool() const {return _fct? true: false;}
412
413 protected:
414 FCT _fct;
415 };
416
417
418 /// link dynamicly to functions by using LoadLibrary() and GetProcAddress()
419 template<typename FCT> struct DynamicLoadLibFct
420 {
421 DynamicLoadLibFct(LPCTSTR moduleName, UINT ordinal)
422 {
423 _hModule = LoadLibrary(moduleName);
424
425 _fct = (FCT) GetProcAddress(_hModule, (LPCSTR)ordinal);
426 }
427
428 ~DynamicLoadLibFct()
429 {
430 FreeLibrary(_hModule);
431 }
432
433 FCT operator*() const {return _fct;}
434 operator bool() const {return _fct? true: false;}
435
436 protected:
437 HMODULE _hModule;
438 FCT _fct;
439 };
440
441 #endif // __cplusplus
442
443
444 #ifdef __cplusplus
445 extern "C" {
446 #endif
447
448
449 #ifdef _MSC_VER
450 #define LONGLONGARG TEXT("I64")
451 #else
452 #define LONGLONGARG TEXT("L")
453 #endif
454
455
456 #ifndef _tcsrchr
457 #ifdef UNICODE
458 #define _tcsrchr wcsrchr
459 #else
460 #define _tcsrchr strrchr
461 #endif
462 #endif
463
464 #ifndef _stprintf
465 #ifdef UNICODE
466 #define _stprintf wcsprintf
467 #else
468 #define _stprintf sprintf
469 #endif
470 #endif
471
472
473 #ifdef __WINE__
474 #ifdef UNICODE
475 extern void _wsplitpath(const WCHAR* path, WCHAR* drv, WCHAR* dir, WCHAR* name, WCHAR* ext);
476 #else
477 extern void _splitpath(const CHAR* path, CHAR* drv, CHAR* dir, CHAR* name, CHAR* ext);
478 #endif
479 #endif
480
481 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
482 #define FILE_ATTRIBUTE_ENCRYPTED 0x00000040
483 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
484 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
485 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
486 #endif
487
488
489 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
490 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
491 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
492
493
494 // display
495 extern void display_error(HWND hwnd, DWORD error);
496
497 // convert time_t to WIN32 FILETIME
498 extern BOOL time_to_filetime(const time_t* t, FILETIME* ftime);
499
500 // search for windows of a specific classname
501 extern int find_window_class(LPCTSTR classname);
502
503 // create a bitmap from an icon
504 extern HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd);
505
506 // launch a program or document file
507 extern BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow);
508 #ifdef UNICODE
509 extern BOOL launch_fileA(HWND hwnd, LPSTR cmd, UINT nCmdShow);
510 #else
511 #define launch_fileA launch_file
512 #endif
513
514 // call an DLL export like rundll32
515 BOOL RunDLL(HWND hwnd, LPCTSTR dllname, LPCSTR procname, LPCTSTR cmdline, UINT nCmdShow);
516
517
518 #ifdef __cplusplus
519 } // extern "C"
520 #endif
521