multithreading for class Window; usage of window map insted of GWL_USERDATA
[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 <time.h>
46
47
48 #ifndef BTNS_BUTTON
49 #define BTNS_BUTTON TBSTYLE_BUTTON //missing in old mingw headers
50 #define BTNS_SEP TBSTYLE_SEP
51 #define BTNS_NOPREFIX TBSTYLE_NOPREFIX
52 #endif
53 #ifndef TB_HITTEST //missing in mingw headers
54 #define TB_HITTEST (WM_USER+69)
55 #endif
56 #ifndef TB_GETBUTTONINFO //missing in mingw headers
57 #define TB_GETBUTTONINFO (WM_USER+65)
58 #endif
59 #ifndef SFGAO_HIDDEN //SFGAO_GHOSTED wrong defined, SFGAO_HIDDEN missing in mingw headers
60 #define SFGAO_HIDDEN 0x00080000L
61 #undef SFGAO_GHOSTED
62 #define SFGAO_GHOSTED 0x00008000L
63 #endif
64
65
66
67 #ifdef __cplusplus
68
69 #ifdef _MSC_VER
70 #pragma warning(disable: 4786) // disable warnings about too long debug information symbols
71 #endif
72
73 // STL headers for strings and streams
74 #include <string>
75 #include <iostream>
76 using namespace std;
77
78 #if _MSC_VER>=1300 // VS.Net
79 #define _NO_COMUTIL //@@
80 #endif
81
82 #if defined(_MSC_VER) && !defined(_NO_COMUTIL)
83
84 // COM utility headers
85 #include <comdef.h>
86 using namespace _com_util;
87
88 #endif // _MSC_VER && !_NO_COMUTIL
89
90
91 #define for if (0) {} else for
92
93
94 #define BUFFER_LEN 1024
95
96
97 struct CommonControlInit
98 {
99 CommonControlInit(DWORD flags=ICC_LISTVIEW_CLASSES|ICC_TREEVIEW_CLASSES|ICC_BAR_CLASSES|ICC_PROGRESS_CLASS|ICC_COOL_CLASSES)
100 {
101 INITCOMMONCONTROLSEX icc = {sizeof(INITCOMMONCONTROLSEX), flags};
102
103 InitCommonControlsEx(&icc);
104 }
105 };
106
107
108 /// wait cursor
109
110 struct WaitCursor //TODO: integrate with WM_SETCURSOR to enable multithreaded background tasks as program launching
111 {
112 WaitCursor()
113 {
114 _old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
115 }
116
117 ~WaitCursor()
118 {
119 SetCursor(_old_cursor);
120 }
121
122 protected:
123 HCURSOR _old_cursor;
124 };
125
126
127 struct WindowHandle
128 {
129 WindowHandle(HWND hwnd=0)
130 : _hwnd(hwnd) {}
131
132 operator HWND() const {return _hwnd;}
133 HWND* operator&() {return &_hwnd;}
134
135 protected:
136 HWND _hwnd;
137 };
138
139
140 /// critical section wrapper
141
142 struct CritSect : public CRITICAL_SECTION
143 {
144 CritSect()
145 {
146 InitializeCriticalSection(this);
147 }
148
149 ~CritSect()
150 {
151 DeleteCriticalSection(this);
152 }
153 };
154
155
156 /// Lock protects a code section utilizing a critical section
157
158 struct Lock
159 {
160 Lock(CritSect& crit_sect)
161 : _crit_sect(crit_sect)
162 {
163 EnterCriticalSection(&crit_sect);
164 }
165
166 ~Lock()
167 {
168 LeaveCriticalSection(&_crit_sect);
169 }
170
171 protected:
172 CritSect& _crit_sect;
173 };
174
175
176 // window utilities
177
178 struct ClientRect : public RECT
179 {
180 ClientRect(HWND hwnd)
181 {
182 GetClientRect(hwnd, this);
183 }
184
185 operator LPRECT() {return this;}
186 };
187
188 struct WindowRect : public RECT
189 {
190 WindowRect(HWND hwnd)
191 {
192 GetWindowRect(hwnd, this);
193 }
194
195 operator LPRECT() {return this;}
196 };
197
198 struct Point : public POINT
199 {
200 Point(LONG x_, LONG y_)
201 {
202 x = x_;
203 y = y_;
204 }
205
206 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
207 Point(LPARAM lparam)
208 {
209 x = GET_X_LPARAM(lparam);
210 y = GET_Y_LPARAM(lparam);
211 }
212
213 operator LPPOINT() {return this;}
214 };
215
216
217 struct FullScreenParameters
218 {
219 FullScreenParameters()
220 : _mode(FALSE)
221 {
222 }
223
224 BOOL _mode;
225 RECT _orgPos;
226 BOOL _wasZoomed;
227 };
228
229
230 // drawing utilities
231
232 struct PaintCanvas : public PAINTSTRUCT
233 {
234 PaintCanvas(HWND hwnd)
235 : _hwnd(hwnd)
236 {
237 BeginPaint(hwnd, this);
238 }
239
240 ~PaintCanvas()
241 {
242 EndPaint(_hwnd, this);
243 }
244
245 operator HDC() const {return hdc;}
246
247 protected:
248 HWND _hwnd;
249 };
250
251 struct Canvas
252 {
253 Canvas(HDC hdc) : _hdc(hdc) {}
254
255 operator HDC() {return _hdc;}
256
257 protected:
258 HDC _hdc;
259 };
260
261 struct WindowCanvas : public Canvas
262 {
263 WindowCanvas(HWND hwnd)
264 : Canvas(GetDC(hwnd)), _hwnd(hwnd) {}
265
266 ~WindowCanvas() {ReleaseDC(_hwnd, _hdc);}
267
268 protected:
269 HWND _hwnd;
270 };
271
272
273 // double buffering classes
274
275 struct MemCanvas : public Canvas
276 {
277 MemCanvas(HDC hdc=0)
278 : Canvas(CreateCompatibleDC(hdc)) {assert(_hdc);}
279
280 ~MemCanvas() {DeleteDC(_hdc);}
281 };
282
283 struct SelectedBitmap
284 {
285 SelectedBitmap(HDC hdc, HBITMAP hbmp)
286 : _hdc(hdc), _old_hbmp(SelectBitmap(hdc, hbmp)) {}
287
288 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc, _old_hbmp));}
289
290 protected:
291 HDC _hdc;
292 HBITMAP _old_hbmp;
293 };
294
295 struct BufferCanvas : public MemCanvas
296 {
297 BufferCanvas(HDC hdc, int x, int y, int w, int h)
298 : MemCanvas(hdc), _hdctarg(hdc),
299 _x(x), _y(y), _w(w), _h(h),
300 _bmp(_hdc, CreateCompatibleBitmap(hdc, w, h)) {}
301
302 BufferCanvas(HDC hdc, const RECT& rect)
303 : MemCanvas(hdc), _hdctarg(hdc),
304 _x(rect.left), _y(rect.top), _w(rect.right-rect.left), _h(rect.bottom-rect.top),
305 _bmp(_hdc, CreateCompatibleBitmap(hdc, _w, _h)) {}
306
307 protected:
308 HDC _hdctarg;
309 int _x, _y, _w, _h;
310 SelectedBitmap _bmp;
311 };
312
313 struct BufferedCanvas : public BufferCanvas
314 {
315 BufferedCanvas(HDC hdc, int x, int y, int w, int h, DWORD mode=SRCCOPY)
316 : BufferCanvas(hdc, x, y, w, h), _mode(mode) {}
317
318 BufferedCanvas(HDC hdc, const RECT& rect, DWORD mode=SRCCOPY)
319 : BufferCanvas(hdc, rect), _mode(mode) {}
320
321 ~BufferedCanvas() {BitBlt(_hdctarg, _x, _y, _w, _h, _hdc, 0, 0, _mode);}
322
323 DWORD _mode;
324 };
325
326 struct BufferedPaintCanvas : public PaintCanvas, public BufferedCanvas
327 {
328 BufferedPaintCanvas(HWND hwnd)
329 : PaintCanvas(hwnd),
330 BufferedCanvas(PAINTSTRUCT::hdc, 0, 0, rcPaint.right, rcPaint.bottom)
331 {
332 }
333
334 operator HDC() {return BufferedCanvas::_hdc;}
335 };
336
337
338 struct TextColor
339 {
340 TextColor(HDC hdc, COLORREF color)
341 : _hdc(hdc), _old_color(SetTextColor(hdc, color)) {}
342
343 ~TextColor() {SetTextColor(_hdc, _old_color);}
344
345 protected:
346 HDC _hdc;
347 COLORREF _old_color;
348 };
349
350 struct BkMode
351 {
352 BkMode(HDC hdc, int bkmode)
353 : _hdc(hdc), _old_bkmode(SetBkMode(hdc, bkmode)) {}
354
355 ~BkMode() {SetBkMode(_hdc, _old_bkmode);}
356
357 protected:
358 HDC _hdc;
359 COLORREF _old_bkmode;
360 };
361
362 struct FontSelection
363 {
364 FontSelection(HDC hdc, HFONT hFont)
365 : _hdc(hdc), _old_hFont(SelectFont(hdc, hFont)) {}
366
367 ~FontSelection() {SelectFont(_hdc, _old_hFont);}
368
369 protected:
370 HDC _hdc;
371 HFONT _old_hFont;
372 };
373
374 struct BitmapSelection
375 {
376 BitmapSelection(HDC hdc, HBITMAP hBmp)
377 : _hdc(hdc), _old_hBmp(SelectBitmap(hdc, hBmp)) {}
378
379 ~BitmapSelection() {SelectBitmap(_hdc, _old_hBmp);}
380
381 protected:
382 HDC _hdc;
383 HBITMAP _old_hBmp;
384 };
385
386
387 struct String
388 #ifdef UNICODE
389 : public wstring
390 #else
391 : public string
392 #endif
393 {
394 #ifdef UNICODE
395 typedef wstring super;
396 #else
397 typedef string super;
398 #endif
399
400 String() {}
401 String(LPCTSTR s) : super(s) {}
402 String(const String& other) : super(other) {}
403
404 String& operator=(LPCTSTR s) {assign(s); return *this;}
405 operator LPCTSTR() const {return c_str();}
406 };
407
408
409 #endif // __cplusplus
410
411
412 #ifdef __cplusplus
413 extern "C" {
414 #endif
415
416
417 #ifdef _MSC_VER
418 #define LONGLONGARG TEXT("I64")
419 #else
420 #define LONGLONGARG TEXT("L")
421 #endif
422
423
424 #ifndef _tcsrchr
425 #ifdef UNICODE
426 #define _tcsrchr wcsrchr
427 #else
428 #define _tcsrchr strrchr
429 #endif
430 #endif
431
432 #ifndef _stprintf
433 #ifdef UNICODE
434 #define _stprintf wcsprintf
435 #else
436 #define _stprintf sprintf
437 #endif
438 #endif
439
440
441 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
442 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
443 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
444
445
446 // display
447 extern void display_error(HWND hwnd, DWORD error);
448
449 // convert time_t to WIN32 FILETIME
450 extern BOOL time_to_filetime(const time_t* t, FILETIME* ftime);
451
452 // search for windows of a specific classname
453 extern int find_window_class(LPCTSTR classname);
454
455 // create a bitmap from an icon
456 extern HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd);
457
458 // launch a program or document file
459 extern BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow);
460 #ifdef UNICODE
461 extern BOOL launch_fileA(HWND hwnd, LPSTR cmd, UINT nCmdShow);
462 #else
463 #define launch_fileA launch_file
464 #endif
465
466 // call an DLL export like rundll32
467 BOOL RunDLL(HWND hwnd, LPCTSTR dllname, LPCSTR procname, LPCTSTR cmdline, UINT nCmdShow);
468
469
470 #ifdef __cplusplus
471 } // extern "C"
472 #endif
473