set TTN_ALWAYS for quicklaunch icons
[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 // window utilities
128
129 struct ClientRect : public RECT
130 {
131 ClientRect(HWND hwnd)
132 {
133 GetClientRect(hwnd, this);
134 }
135
136 operator LPRECT() {return this;}
137 };
138
139 struct WindowRect : public RECT
140 {
141 WindowRect(HWND hwnd)
142 {
143 GetWindowRect(hwnd, this);
144 }
145
146 operator LPRECT() {return this;}
147 };
148
149 struct Point : public POINT
150 {
151 Point(LONG x_, LONG y_)
152 {
153 x = x_;
154 y = y_;
155 }
156
157 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
158 Point(LPARAM lparam)
159 {
160 x = GET_X_LPARAM(lparam);
161 y = GET_Y_LPARAM(lparam);
162 }
163
164 operator LPPOINT() {return this;}
165 };
166
167
168 struct FullScreenParameters
169 {
170 FullScreenParameters()
171 : _mode(FALSE)
172 {
173 }
174
175 BOOL _mode;
176 RECT _orgPos;
177 BOOL _wasZoomed;
178 };
179
180
181 // drawing utilities
182
183 struct PaintCanvas : public PAINTSTRUCT
184 {
185 PaintCanvas(HWND hwnd)
186 : _hwnd(hwnd)
187 {
188 BeginPaint(hwnd, this);
189 }
190
191 ~PaintCanvas()
192 {
193 EndPaint(_hwnd, this);
194 }
195
196 operator HDC() const {return hdc;}
197
198 protected:
199 HWND _hwnd;
200 };
201
202 struct Canvas
203 {
204 Canvas(HDC hdc) : _hdc(hdc) {}
205
206 operator HDC() {return _hdc;}
207
208 protected:
209 HDC _hdc;
210 };
211
212 struct WindowCanvas : public Canvas
213 {
214 WindowCanvas(HWND hwnd)
215 : Canvas(GetDC(hwnd)), _hwnd(hwnd) {}
216
217 ~WindowCanvas() {ReleaseDC(_hwnd, _hdc);}
218
219 protected:
220 HWND _hwnd;
221 };
222
223
224 // double buffering classes
225
226 struct MemCanvas : public Canvas
227 {
228 MemCanvas(HDC hdc=0)
229 : Canvas(CreateCompatibleDC(hdc)) {assert(_hdc);}
230
231 ~MemCanvas() {DeleteDC(_hdc);}
232 };
233
234 struct SelectedBitmap
235 {
236 SelectedBitmap(HDC hdc, HBITMAP hbmp)
237 : _hdc(hdc), _old_hbmp(SelectBitmap(hdc, hbmp)) {}
238
239 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc, _old_hbmp));}
240
241 protected:
242 HDC _hdc;
243 HBITMAP _old_hbmp;
244 };
245
246 struct BufferCanvas : public MemCanvas
247 {
248 BufferCanvas(HDC hdc, int x, int y, int w, int h)
249 : MemCanvas(hdc), _hdctarg(hdc),
250 _x(x), _y(y), _w(w), _h(h),
251 _bmp(_hdc, CreateCompatibleBitmap(hdc, w, h)) {}
252
253 BufferCanvas(HDC hdc, const RECT& rect)
254 : MemCanvas(hdc), _hdctarg(hdc),
255 _x(rect.left), _y(rect.top), _w(rect.right-rect.left), _h(rect.bottom-rect.top),
256 _bmp(_hdc, CreateCompatibleBitmap(hdc, _w, _h)) {}
257
258 protected:
259 HDC _hdctarg;
260 int _x, _y, _w, _h;
261 SelectedBitmap _bmp;
262 };
263
264 struct BufferedCanvas : public BufferCanvas
265 {
266 BufferedCanvas(HDC hdc, int x, int y, int w, int h, DWORD mode=SRCCOPY)
267 : BufferCanvas(hdc, x, y, w, h), _mode(mode) {}
268
269 BufferedCanvas(HDC hdc, const RECT& rect, DWORD mode=SRCCOPY)
270 : BufferCanvas(hdc, rect), _mode(mode) {}
271
272 ~BufferedCanvas() {BitBlt(_hdctarg, _x, _y, _w, _h, _hdc, 0, 0, _mode);}
273
274 DWORD _mode;
275 };
276
277 struct BufferedPaintCanvas : public PaintCanvas, public BufferedCanvas
278 {
279 BufferedPaintCanvas(HWND hwnd)
280 : PaintCanvas(hwnd),
281 BufferedCanvas(PAINTSTRUCT::hdc, 0, 0, rcPaint.right, rcPaint.bottom)
282 {
283 }
284
285 operator HDC() {return BufferedCanvas::_hdc;}
286 };
287
288
289 struct TextColor
290 {
291 TextColor(HDC hdc, COLORREF color)
292 : _hdc(hdc), _old_color(SetTextColor(hdc, color)) {}
293
294 ~TextColor() {SetTextColor(_hdc, _old_color);}
295
296 protected:
297 HDC _hdc;
298 COLORREF _old_color;
299 };
300
301 struct BkMode
302 {
303 BkMode(HDC hdc, int bkmode)
304 : _hdc(hdc), _old_bkmode(SetBkMode(hdc, bkmode)) {}
305
306 ~BkMode() {SetBkMode(_hdc, _old_bkmode);}
307
308 protected:
309 HDC _hdc;
310 COLORREF _old_bkmode;
311 };
312
313 struct FontSelection
314 {
315 FontSelection(HDC hdc, HFONT hFont)
316 : _hdc(hdc), _old_hFont(SelectFont(hdc, hFont)) {}
317
318 ~FontSelection() {SelectFont(_hdc, _old_hFont);}
319
320 protected:
321 HDC _hdc;
322 HFONT _old_hFont;
323 };
324
325 struct BitmapSelection
326 {
327 BitmapSelection(HDC hdc, HBITMAP hBmp)
328 : _hdc(hdc), _old_hBmp(SelectBitmap(hdc, hBmp)) {}
329
330 ~BitmapSelection() {SelectBitmap(_hdc, _old_hBmp);}
331
332 protected:
333 HDC _hdc;
334 HBITMAP _old_hBmp;
335 };
336
337
338 struct String
339 #ifdef UNICODE
340 : public wstring
341 #else
342 : public string
343 #endif
344 {
345 #ifdef UNICODE
346 typedef wstring super;
347 #else
348 typedef string super;
349 #endif
350
351 String() {}
352 String(LPCTSTR s) : super(s) {}
353 String(const String& other) : super(other) {}
354
355 String& operator=(LPCTSTR s) {assign(s); return *this;}
356 operator LPCTSTR() const {return c_str();}
357 };
358
359
360 #endif // __cplusplus
361
362
363 #ifdef __cplusplus
364 extern "C" {
365 #endif
366
367
368 #ifdef _MSC_VER
369 #define LONGLONGARG TEXT("I64")
370 #else
371 #define LONGLONGARG TEXT("L")
372 #endif
373
374
375 #ifndef _tcsrchr
376 #ifdef UNICODE
377 #define _tcsrchr wcsrchr
378 #else
379 #define _tcsrchr strrchr
380 #endif
381 #endif
382
383 #ifndef _stprintf
384 #ifdef UNICODE
385 #define _stprintf wcsprintf
386 #else
387 #define _stprintf sprintf
388 #endif
389 #endif
390
391
392 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
393 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
394 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
395
396
397 // display
398 extern void display_error(HWND hwnd, DWORD error);
399
400 // convert time_t to WIN32 FILETIME
401 extern BOOL time_to_filetime(const time_t* t, FILETIME* ftime);
402
403 // search for windows of a specific classname
404 extern int find_window_class(LPCTSTR classname);
405
406 // create a bitmap from an icon
407 extern HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd);
408
409 // launch a program or document file
410 extern BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow);
411 #ifdef UNICODE
412 extern BOOL launch_fileA(HWND hwnd, LPSTR cmd, UINT nCmdShow);
413 #else
414 #define launch_fileA launch_file
415 #endif
416
417 // call an DLL export like rundll32
418 BOOL RunDLL(HWND hwnd, LPCTSTR dllname, LPCSTR procname, LPCTSTR cmdline, UINT nCmdShow);
419
420
421 #ifdef __cplusplus
422 } // extern "C"
423 #endif
424