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