2 * Copyright 2003 Martin Fuchs
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.
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.
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
25 // Martin Fuchs, 23.07.2003
29 // standard windows headers
30 #define WIN32_LEAN_AND_MEAN
31 #define WIN32_EXTRA_LEAN
40 #include <windowsx.h> // for SelectBrush(), ListBox_SetSel(), SubclassWindow(), ...
43 #include <malloc.h> // for alloca()
45 #include <stdlib.h> // for _MAX_DIR, ...
46 #include <stdio.h> // for sprintf()
51 #define _MAX_FNAME 256
52 #define _MAX_DIR _MAX_FNAME
53 #define _MAX_EXT _MAX_FNAME
63 #define for if (0) {} else for
66 #define BUFFER_LEN 1024
69 extern void _log_(LPCTSTR txt
);
71 #define LOG(txt) _log_(txt)
75 #define LONGLONGARG TEXT("I64")
77 #define LONGLONGARG TEXT("L")
83 #define _tcsrchr wcsrchr
85 #define _tcsrchr strrchr
91 #define _stprintf wcsprintf
93 #define _stprintf sprintf
100 extern void _wsplitpath(const WCHAR
* path
, WCHAR
* drv
, WCHAR
* dir
, WCHAR
* name
, WCHAR
* ext
);
102 extern void _splitpath(const CHAR
* path
, CHAR
* drv
, CHAR
* dir
, CHAR
* name
, CHAR
* ext
);
106 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
107 #define FILE_ATTRIBUTE_ENCRYPTED 0x00000040
108 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
109 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
110 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
114 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
115 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
116 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
117 #define Window_SetIcon(hwnd, type, hicon) (HICON)SendMessage(hwnd, WM_SETICON, type, (LPARAM)(hicon))
120 // center window in respect to its parent window
121 extern void CenterWindow(HWND hwnd
);
123 // move window into visibility
124 extern void MoveVisible(HWND hwnd
);
126 // display error message
127 extern void display_error(HWND hwnd
, DWORD error
);
129 // convert time_t to WIN32 FILETIME
130 extern BOOL
time_to_filetime(const time_t* t
, FILETIME
* ftime
);
132 // search for windows of a specific classname
133 extern int find_window_class(LPCTSTR classname
);
135 // create a bitmap from an icon
136 extern HBITMAP
create_bitmap_from_icon(HICON hIcon
, HBRUSH hbrush_bkgnd
, HDC hdc_wnd
);
138 // launch a program or document file
139 extern BOOL
launch_file(HWND hwnd
, LPCTSTR cmd
, UINT nCmdShow
);
141 extern BOOL
launch_fileA(HWND hwnd
, LPSTR cmd
, UINT nCmdShow
);
143 #define launch_fileA launch_file
146 // call an DLL export like rundll32
147 BOOL
RunDLL(HWND hwnd
, LPCTSTR dllname
, LPCSTR procname
, LPCTSTR cmdline
, UINT nCmdShow
);
158 #pragma warning(disable: 4786) // disable warnings about too long debug information symbols
161 // STL headers for strings and streams
166 #if _MSC_VER>=1300 // VS.Net
167 #define _NO_COMUTIL //@@
170 #if defined(_MSC_VER) && !defined(_NO_COMUTIL)
172 // COM utility headers
174 using namespace _com_util
;
176 #endif // _MSC_VER && !_NO_COMUTIL
179 /// initialization of windows common controls
180 struct CommonControlInit
182 CommonControlInit(DWORD flags
=ICC_LISTVIEW_CLASSES
|ICC_TREEVIEW_CLASSES
|ICC_BAR_CLASSES
|ICC_PROGRESS_CLASS
|ICC_COOL_CLASSES
)
184 INITCOMMONCONTROLSEX icc
= {sizeof(INITCOMMONCONTROLSEX
), flags
};
186 InitCommonControlsEx(&icc
);
193 struct WaitCursor
///@todo integrate with WM_SETCURSOR to enable multithreaded background tasks as program launching
197 _old_cursor
= SetCursor(LoadCursor(0, IDC_WAIT
));
202 SetCursor(_old_cursor
);
210 /// base of all structures storing a window handle
213 WindowHandle(HWND hwnd
=0)
216 operator HWND() const {return _hwnd
;}
217 HWND
* operator&() {return &_hwnd
;}
224 /// locally hide a window
225 struct HiddenWindow
: public WindowHandle
227 HiddenWindow(HWND hwnd
)
230 SetWindowPos(hwnd
, 0, 0, 0, 0, 0, SWP_HIDEWINDOW
|SWP_NOREDRAW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
235 SetWindowPos(_hwnd
, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
240 /// critical section wrapper
242 struct CritSect
: public CRITICAL_SECTION
246 InitializeCriticalSection(this);
251 DeleteCriticalSection(this);
256 /// Lock protects a code section utilizing a critical section
260 Lock(CritSect
& crit_sect
)
261 : _crit_sect(crit_sect
)
263 EnterCriticalSection(&crit_sect
);
268 LeaveCriticalSection(&_crit_sect
);
272 CritSect
& _crit_sect
;
276 /// Thread base class
283 _hThread
= INVALID_HANDLE_VALUE
;
290 CloseHandle(_hThread
);
297 _hThread
= CreateThread(NULL
, 0, ThreadProc
, this, 0, NULL
);
305 Lock
lock(_crit_sect
);
309 // wait for finishing
310 WaitForSingleObject(_hThread
, INFINITE
);
314 virtual int Run() = 0;
316 bool is_alive() const {return _alive
;}
321 static DWORD WINAPI
ThreadProc(void* para
);
330 /// ClientRect retreives the client area rectangle of a window.
331 struct ClientRect
: public RECT
333 ClientRect(HWND hwnd
)
335 GetClientRect(hwnd
, this);
338 operator LPRECT() {return this;}
340 POINT
& pos() {return *(LPPOINT
)this;}
343 /// ClientRect retreives the window rectangle of a window.
344 struct WindowRect
: public RECT
346 WindowRect(HWND hwnd
)
348 GetWindowRect(hwnd
, this);
351 operator LPRECT() {return this;}
353 POINT
& pos() {return *(LPPOINT
)this;}
356 /// PointL encapsulates the POINT structure into a C++ object.
357 struct Point
: public POINT
359 Point(LONG x_
, LONG y_
)
365 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
368 x
= GET_X_LPARAM(lparam
);
369 y
= GET_Y_LPARAM(lparam
);
372 operator LPPOINT() {return this;}
376 /// transform coordinates in a RECT from client to screen coordiantes
377 inline void ClientToScreen(HWND hwnd
, RECT
* prect
)
378 {::ClientToScreen(hwnd
,(LPPOINT
)&prect
->left
); ::ClientToScreen(hwnd
,(LPPOINT
)&prect
->right
);}
380 /// transform coordinates in a RECT from screen to client coordiantes
381 inline void ScreenToClient(HWND hwnd
, RECT
* prect
)
382 {::ScreenToClient(hwnd
,(LPPOINT
)&prect
->left
); ::ScreenToClient(hwnd
,(LPPOINT
)&prect
->right
);}
385 /// structure containing information about full screen display of the frame window
386 struct FullScreenParameters
388 FullScreenParameters()
401 /// PaintCanvas is a encapsulation of device contexts managed by BeginPaint()/EndPaint().
402 struct PaintCanvas
: public PAINTSTRUCT
404 PaintCanvas(HWND hwnd
)
407 BeginPaint(hwnd
, this);
412 EndPaint(_hwnd
, this);
415 operator HDC() const {return hdc
;}
421 /// Canvas is a encapsulation of device contexts.
424 Canvas(HDC hdc
) : _hdc(hdc
) {}
426 operator HDC() {return _hdc
;}
432 /// WindowCanvas is a encapsulation of client area device contexts.
433 struct WindowCanvas
: public Canvas
435 WindowCanvas(HWND hwnd
)
436 : Canvas(GetDC(hwnd
)), _hwnd(hwnd
) {}
438 ~WindowCanvas() {ReleaseDC(_hwnd
, _hdc
);}
445 // double buffering classes
447 /// Memory Canvas creates and destroys memory devoce contexts.
448 struct MemCanvas
: public Canvas
451 : Canvas(CreateCompatibleDC(hdc
)) {assert(_hdc
);}
453 ~MemCanvas() {DeleteDC(_hdc
);}
456 /// SelectedBitmap is used to localy select bitmaps into device contexts.
457 struct SelectedBitmap
459 SelectedBitmap(HDC hdc
, HBITMAP hbmp
)
460 : _hdc(hdc
), _old_hbmp(SelectBitmap(hdc
, hbmp
)) {}
462 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc
, _old_hbmp
));}
469 /// BufferCanvas manages offscreen bitmaps selected into memory device contexts.
470 struct BufferCanvas
: public MemCanvas
472 BufferCanvas(HDC hdc
, int x
, int y
, int w
, int h
)
473 : MemCanvas(hdc
), _hdctarg(hdc
),
474 _x(x
), _y(y
), _w(w
), _h(h
),
475 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, w
, h
)) {}
477 BufferCanvas(HDC hdc
, const RECT
& rect
)
478 : MemCanvas(hdc
), _hdctarg(hdc
),
479 _x(rect
.left
), _y(rect
.top
), _w(rect
.right
-rect
.left
), _h(rect
.bottom
-rect
.top
),
480 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, _w
, _h
)) {}
488 /// BufferedCanvas enables double buffering for a device context.
489 struct BufferedCanvas
: public BufferCanvas
491 BufferedCanvas(HDC hdc
, int x
, int y
, int w
, int h
, DWORD mode
=SRCCOPY
)
492 : BufferCanvas(hdc
, x
, y
, w
, h
), _mode(mode
) {}
494 BufferedCanvas(HDC hdc
, const RECT
& rect
, DWORD mode
=SRCCOPY
)
495 : BufferCanvas(hdc
, rect
), _mode(mode
) {}
497 ~BufferedCanvas() {BitBlt(_hdctarg
, _x
, _y
, _w
, _h
, _hdc
, 0, 0, _mode
);}
502 /// BufferedPaintCanvas extends PaintCanvas for double buffering.
503 struct BufferedPaintCanvas
: public PaintCanvas
, public BufferedCanvas
505 BufferedPaintCanvas(HWND hwnd
)
507 BufferedCanvas(PAINTSTRUCT::hdc
, 0, 0, rcPaint
.right
, rcPaint
.bottom
)
511 operator HDC() {return BufferedCanvas::_hdc
;}
515 /// TextColor locally selects a text color for drawing.
518 TextColor(HDC hdc
, COLORREF color
)
519 : _hdc(hdc
), _old_color(SetTextColor(hdc
, color
)) {}
521 ~TextColor() {SetTextColor(_hdc
, _old_color
);}
528 /// TextColor locally sets the background mode for drawing.
531 BkMode(HDC hdc
, int bkmode
)
532 : _hdc(hdc
), _old_bkmode(SetBkMode(hdc
, bkmode
)) {}
534 ~BkMode() {SetBkMode(_hdc
, _old_bkmode
);}
538 COLORREF _old_bkmode
;
541 /// TextColor locally selects a font for drawing.
544 FontSelection(HDC hdc
, HFONT hFont
)
545 : _hdc(hdc
), _old_hFont(SelectFont(hdc
, hFont
)) {}
547 ~FontSelection() {SelectFont(_hdc
, _old_hFont
);}
554 /// TextColor locally selects a bitmap into a device context.
555 struct BitmapSelection
557 BitmapSelection(HDC hdc
, HBITMAP hBmp
)
558 : _hdc(hdc
), _old_hBmp(SelectBitmap(hdc
, hBmp
)) {}
560 ~BitmapSelection() {SelectBitmap(_hdc
, _old_hBmp
);}
568 /// string class for convenience
577 typedef wstring super
;
579 typedef string super
;
583 String(LPCTSTR s
) {if (s
) super::assign(s
);}
584 String(LPCTSTR s
, int l
) : super(s
, l
) {}
585 String(const super
& other
) : super(other
) {}
586 String(const String
& other
) : super(other
) {}
589 String(LPCSTR s
) {assign(s
);}
590 String(LPCSTR s
, int l
) {assign(s
, l
);}
591 String(const string
& other
) {assign(other
.c_str());}
592 String
& operator=(LPCSTR s
) {assign(s
); return *this;}
593 void assign(LPCSTR s
) {if (s
) {TCHAR b
[BUFFER_LEN
]; super::assign(b
, MultiByteToWideChar(CP_ACP
, 0, s
, -1, b
, BUFFER_LEN
));} else erase();}
594 void assign(LPCSTR s
, int l
) {if (s
) {TCHAR b
[BUFFER_LEN
]; super::assign(b
, MultiByteToWideChar(CP_ACP
, 0, s
, l
, b
, BUFFER_LEN
));} else erase();}
596 String(LPCWSTR s
) {assign(s
);}
597 String(LPCWSTR s
, int l
) {assign(s
, l
);}
598 String(const wstring
& other
) {assign(other
.c_str());}
599 String
& operator=(LPCWSTR s
) {assign(s
); return *this;}
600 void assign(LPCWSTR s
) {if (s
) {char b
[BUFFER_LEN
]; super::assign(b
, WideCharToMultiByte(CP_ACP
, 0, s
, -1, b
, BUFFER_LEN
, 0, 0));} else erase();}
601 void assign(LPCWSTR s
, int l
) {if (s
) {char b
[BUFFER_LEN
]; super::assign(b
, WideCharToMultiByte(CP_ACP
, 0, s
, l
, b
, BUFFER_LEN
, 0, 0));} else erase();}
604 String
& operator=(LPCTSTR s
) {if (s
) super::assign(s
); else erase(); return *this;}
605 String
& operator=(const super
& s
) {super::assign(s
); return *this;}
607 operator LPCTSTR() const {return c_str();}
609 String
& printf(LPCTSTR fmt
, ...)
615 super::assign(b
, _vstprintf(b
, fmt
, l
));
621 String
& vprintf(LPCTSTR fmt
, va_list l
)
625 super::assign(b
, _vstprintf(b
, fmt
, l
));
630 String
& appendf(LPCTSTR fmt
, ...)
636 super::append(b
, _vstprintf(b
, fmt
, l
));
642 String
& vappendf(LPCTSTR fmt
, va_list l
)
646 super::append(b
, _vstprintf(b
, fmt
, l
));
653 struct FmtString
: public String
655 FmtString(LPCTSTR fmt
, ...)
672 int l
= wcslen(s
) + 1;
673 _str
= (LPSTR
) malloc(2*l
);
675 if (WideCharToMultiByte(CP_ACP
, 0, s
, -1, _str
, 2*l
, 0, 0) <= 0)
684 operator LPCSTR() {return _str
;}
690 #define UNC(x) ((LPCWSTR)(x))
694 #define ANS(x) ((LPCSTR)(x))
700 int l
= strlen(s
) + 1;
701 _str
= (LPWSTR
) malloc(2*l
);
703 if (MultiByteToWideChar(CP_ACP
, 0, s
, -1, _str
, l
) <= 0)
712 operator LPCWSTR() {return _str
;}
721 /// link dynamicly to functions by using GetModuleHandle() and GetProcAddress()
722 template<typename FCT
> struct DynamicFct
724 DynamicFct(LPCTSTR moduleName
, UINT ordinal
)
726 HMODULE hModule
= GetModuleHandle(moduleName
);
728 _fct
= (FCT
) GetProcAddress(hModule
, (LPCSTR
)ordinal
);
731 DynamicFct(LPCTSTR moduleName
, LPCSTR name
)
733 HMODULE hModule
= GetModuleHandle(moduleName
);
735 _fct
= (FCT
) GetProcAddress(hModule
, name
);
738 FCT
operator*() const {return _fct
;}
739 operator bool() const {return _fct
? true: false;}
746 /// link dynamicly to functions by using LoadLibrary() and GetProcAddress()
747 template<typename FCT
> struct DynamicLoadLibFct
749 DynamicLoadLibFct(LPCTSTR moduleName
, UINT ordinal
)
751 _hModule
= LoadLibrary(moduleName
);
753 _fct
= (FCT
) GetProcAddress(_hModule
, (LPCSTR
)ordinal
);
756 DynamicLoadLibFct(LPCTSTR moduleName
, LPCSTR name
)
758 _hModule
= LoadLibrary(moduleName
);
760 _fct
= (FCT
) GetProcAddress(_hModule
, name
);
765 FreeLibrary(_hModule
);
768 FCT
operator*() const {return _fct
;}
769 operator bool() const {return _fct
? true: false;}
779 Context(const char* ctx
)
786 Context(const char* ctx
, LPCSTR obj
)
794 Context(const char* ctx
, LPCWSTR obj
)
802 Context(const Context
& other
)
817 String
toString() const;
818 String
getStackTrace() const;
823 static Context
& current() {return *s_current
;}
828 static Context
* s_current
; ///@todo use TLS
829 static Context s_main
;
832 #define CONTEXT_OBJ __ctx__._obj
833 #define CONTEXT(c) Context __ctx__(c)
834 #define CURRENT_CONTEXT Context::current()
835 #define OBJ_CONTEXT(c, o) Context __ctx__(c, o);
838 #endif // __cplusplus