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