work around for missing BTNS_NOPREFIX declaration in mingw commtrl.h
[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
60
61 #ifdef __cplusplus
62
63 #ifdef _MSC_VER
64 #pragma warning(disable: 4786) // disable warnings about too long debug information symbols
65 #endif
66
67 // STL headers for strings and streams
68 #include <string>
69 #include <iostream>
70 using namespace std;
71
72 #if _MSC_VER>=1300 // VS.Net
73 #define _NO_COMUTIL //@@
74 #endif
75
76 #if defined(_MSC_VER) && !defined(_NO_COMUTIL)
77
78 // COM utility headers
79 #include <comdef.h>
80 using namespace _com_util;
81
82 #endif // _MSC_VER && !_NO_COMUTIL
83
84
85 #define for if (0) {} else for
86
87
88 #define BUFFER_LEN 1024
89
90
91 struct CommonControlInit
92 {
93 CommonControlInit(DWORD flags=ICC_LISTVIEW_CLASSES|ICC_TREEVIEW_CLASSES|ICC_BAR_CLASSES|ICC_PROGRESS_CLASS|ICC_COOL_CLASSES)
94 {
95 INITCOMMONCONTROLSEX icc = {sizeof(INITCOMMONCONTROLSEX), flags};
96
97 InitCommonControlsEx(&icc);
98 }
99 };
100
101
102 /// wait cursor
103
104 struct WaitCursor
105 {
106 WaitCursor()
107 {
108 _old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
109 }
110
111 ~WaitCursor()
112 {
113 SetCursor(_old_cursor);
114 }
115
116 protected:
117 HCURSOR _old_cursor;
118 };
119
120
121 // window utilities
122
123 struct ClientRect : public RECT
124 {
125 ClientRect(HWND hwnd)
126 {
127 GetClientRect(hwnd, this);
128 }
129
130 operator LPRECT() {return this;}
131 };
132
133 struct WindowRect : public RECT
134 {
135 WindowRect(HWND hwnd)
136 {
137 GetWindowRect(hwnd, this);
138 }
139
140 operator LPRECT() {return this;}
141 };
142
143 struct Point : public POINT
144 {
145 Point(LONG x_, LONG y_)
146 {
147 x = x_;
148 y = y_;
149 }
150
151 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
152 Point(LPARAM lparam)
153 {
154 x = GET_X_LPARAM(lparam);
155 y = GET_Y_LPARAM(lparam);
156 }
157
158 operator LPPOINT() {return this;}
159 };
160
161
162 struct FullScreenParameters
163 {
164 FullScreenParameters()
165 : _mode(FALSE)
166 {
167 }
168
169 BOOL _mode;
170 RECT _orgPos;
171 BOOL _wasZoomed;
172 };
173
174
175 // drawing utilities
176
177 struct PaintCanvas : public PAINTSTRUCT
178 {
179 PaintCanvas(HWND hwnd)
180 : _hwnd(hwnd)
181 {
182 BeginPaint(hwnd, this);
183 }
184
185 ~PaintCanvas()
186 {
187 EndPaint(_hwnd, this);
188 }
189
190 operator HDC() const {return hdc;}
191
192 protected:
193 HWND _hwnd;
194 };
195
196 struct Canvas
197 {
198 Canvas(HDC hdc) : _hdc(hdc) {}
199
200 operator HDC() {return _hdc;}
201
202 protected:
203 HDC _hdc;
204 };
205
206 struct WindowCanvas : public Canvas
207 {
208 WindowCanvas(HWND hwnd)
209 : Canvas(GetDC(hwnd)), _hwnd(hwnd) {}
210
211 ~WindowCanvas() {ReleaseDC(_hwnd, _hdc);}
212
213 protected:
214 HWND _hwnd;
215 };
216
217
218 // double buffering classes
219
220 struct MemCanvas : public Canvas
221 {
222 MemCanvas(HDC hdc=0)
223 : Canvas(CreateCompatibleDC(hdc)) {assert(_hdc);}
224
225 ~MemCanvas() {DeleteDC(_hdc);}
226 };
227
228 struct SelectedBitmap
229 {
230 SelectedBitmap(HDC hdc, HBITMAP hbmp)
231 : _hdc(hdc), _old_hbmp(SelectBitmap(hdc, hbmp)) {}
232
233 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc, _old_hbmp));}
234
235 protected:
236 HDC _hdc;
237 HBITMAP _old_hbmp;
238 };
239
240 struct BufferCanvas : public MemCanvas
241 {
242 BufferCanvas(HDC hdc, int x, int y, int w, int h)
243 : MemCanvas(hdc), _hdctarg(hdc),
244 _x(x), _y(y), _w(w), _h(h),
245 _bmp(_hdc, CreateCompatibleBitmap(hdc, w, h)) {}
246
247 BufferCanvas(HDC hdc, const RECT& rect)
248 : MemCanvas(hdc), _hdctarg(hdc),
249 _x(rect.left), _y(rect.top), _w(rect.right-rect.left), _h(rect.bottom-rect.top),
250 _bmp(_hdc, CreateCompatibleBitmap(hdc, _w, _h)) {}
251
252 protected:
253 HDC _hdctarg;
254 int _x, _y, _w, _h;
255 SelectedBitmap _bmp;
256 };
257
258 struct BufferedCanvas : public BufferCanvas
259 {
260 BufferedCanvas(HDC hdc, int x, int y, int w, int h, DWORD mode=SRCCOPY)
261 : BufferCanvas(hdc, x, y, w, h), _mode(mode) {}
262
263 BufferedCanvas(HDC hdc, const RECT& rect, DWORD mode=SRCCOPY)
264 : BufferCanvas(hdc, rect), _mode(mode) {}
265
266 ~BufferedCanvas() {BitBlt(_hdctarg, _x, _y, _w, _h, _hdc, 0, 0, _mode);}
267
268 DWORD _mode;
269 };
270
271 struct BufferedPaintCanvas : public PaintCanvas, public BufferedCanvas
272 {
273 BufferedPaintCanvas(HWND hwnd)
274 : PaintCanvas(hwnd),
275 BufferedCanvas(PAINTSTRUCT::hdc, 0, 0, rcPaint.right, rcPaint.bottom)
276 {
277 }
278
279 operator HDC() {return BufferedCanvas::_hdc;}
280 };
281
282
283 struct TextColor
284 {
285 TextColor(HDC hdc, COLORREF color)
286 : _hdc(hdc), _old_color(SetTextColor(hdc, color)) {}
287
288 ~TextColor() {SetTextColor(_hdc, _old_color);}
289
290 protected:
291 HDC _hdc;
292 COLORREF _old_color;
293 };
294
295 struct BkMode
296 {
297 BkMode(HDC hdc, int bkmode)
298 : _hdc(hdc), _old_bkmode(SetBkMode(hdc, bkmode)) {}
299
300 ~BkMode() {SetBkMode(_hdc, _old_bkmode);}
301
302 protected:
303 HDC _hdc;
304 COLORREF _old_bkmode;
305 };
306
307 struct FontSelection
308 {
309 FontSelection(HDC hdc, HFONT hFont)
310 : _hdc(hdc), _old_hFont(SelectFont(hdc, hFont)) {}
311
312 ~FontSelection() {SelectFont(_hdc, _old_hFont);}
313
314 protected:
315 HDC _hdc;
316 HFONT _old_hFont;
317 };
318
319
320 struct String
321 #ifdef UNICODE
322 : public wstring
323 #else
324 : public string
325 #endif
326 {
327 #ifdef UNICODE
328 typedef wstring super;
329 #else
330 typedef string super;
331 #endif
332
333 String() {}
334 String(LPCTSTR s) : super(s) {}
335 String(const String& other) : super(other) {}
336
337 String& operator=(LPCTSTR s) {assign(s); return *this;}
338 operator LPCTSTR() const {return c_str();}
339 };
340
341
342 #endif // __cplusplus
343
344
345 #ifdef __cplusplus
346 extern "C" {
347 #endif
348
349
350 #ifdef _MSC_VER
351 #define LONGLONGARG TEXT("I64")
352 #else
353 #define LONGLONGARG TEXT("L")
354 #endif
355
356
357 #ifndef _tcsrchr
358 #ifdef UNICODE
359 #define _tcsrchr wcsrchr
360 #else
361 #define _tcsrchr strrchr
362 #endif
363 #endif
364
365 #ifndef _stprintf
366 #ifdef UNICODE
367 #define _stprintf wcsprintf
368 #else
369 #define _stprintf sprintf
370 #endif
371 #endif
372
373
374 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
375
376
377 // display
378 extern void display_error(HWND hwnd, DWORD error);
379
380 // convert time_t to WIN32 FILETIME
381 extern BOOL time_to_filetime(const time_t* t, FILETIME* ftime);
382
383 // search for windows of a specific classname
384 extern int find_window_class(LPCTSTR classname);
385
386 // launch a program or document file
387 extern BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow);
388 #ifdef UNICODE
389 extern BOOL launch_fileA(HWND hwnd, LPSTR cmd, UINT nCmdShow);
390 #else
391 #define launch_fileA launch_file
392 #endif
393
394 // create a bitmap from an icon
395 extern HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd);
396
397
398 #ifdef __cplusplus
399 } // extern "C"
400 #endif
401