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
);
104 #define _tcsnicmp strncasecmp
105 #define _tcsicoll strcasecmp
108 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
109 #define FILE_ATTRIBUTE_ENCRYPTED 0x00000040
110 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
111 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
112 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
116 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
117 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
118 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
119 #define Window_SetIcon(hwnd, type, hicon) (HICON)SendMessage(hwnd, WM_SETICON, type, (LPARAM)(hicon))
122 // center window in respect to its parent window
123 extern void CenterWindow(HWND hwnd
);
125 // move window into visibility
126 extern void MoveVisible(HWND hwnd
);
128 // display error message
129 extern void display_error(HWND hwnd
, DWORD error
);
131 // convert time_t to WIN32 FILETIME
132 extern BOOL
time_to_filetime(const time_t* t
, FILETIME
* ftime
);
134 // search for windows of a specific classname
135 extern int find_window_class(LPCTSTR classname
);
137 // launch a program or document file
138 extern BOOL
launch_file(HWND hwnd
, LPCTSTR cmd
, UINT nCmdShow
);
140 extern BOOL
launch_fileA(HWND hwnd
, LPSTR cmd
, UINT nCmdShow
);
142 #define launch_fileA launch_file
145 // call an DLL export like rundll32
146 BOOL
RunDLL(HWND hwnd
, LPCTSTR dllname
, LPCSTR procname
, LPCTSTR cmdline
, UINT nCmdShow
);
148 // create a directory with all missing parent directories
149 BOOL
RecursiveCreateDirectory(LPCTSTR path_in
);
160 #pragma warning(disable: 4786) // disable warnings about too long debug information symbols
163 // STL headers for strings and streams
173 #if _MSC_VER>=1300 // VS.Net
174 #define _NO_COMUTIL //@@
177 #if defined(_MSC_VER) && !defined(_NO_COMUTIL)
179 // COM utility headers
181 using namespace _com_util
;
183 #endif // _MSC_VER && !_NO_COMUTIL
186 /// initialization of windows common controls
187 struct CommonControlInit
189 CommonControlInit(DWORD flags
=ICC_LISTVIEW_CLASSES
|ICC_TREEVIEW_CLASSES
|ICC_BAR_CLASSES
|ICC_PROGRESS_CLASS
|ICC_COOL_CLASSES
)
191 INITCOMMONCONTROLSEX icc
= {sizeof(INITCOMMONCONTROLSEX
), flags
};
193 InitCommonControlsEx(&icc
);
200 struct WaitCursor
///@todo integrate with WM_SETCURSOR to enable multithreaded background tasks as program launching
204 _old_cursor
= SetCursor(LoadCursor(0, IDC_WAIT
));
209 SetCursor(_old_cursor
);
217 /// base of all structures storing a window handle
220 WindowHandle(HWND hwnd
=0)
223 operator HWND() const {return _hwnd
;}
224 HWND
* operator&() {return &_hwnd
;}
231 /// locally hide a window
232 struct HiddenWindow
: public WindowHandle
234 HiddenWindow(HWND hwnd
)
237 SetWindowPos(hwnd
, 0, 0, 0, 0, 0, SWP_HIDEWINDOW
|SWP_NOREDRAW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
242 SetWindowPos(_hwnd
, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
247 /// critical section wrapper
249 struct CritSect
: public CRITICAL_SECTION
253 InitializeCriticalSection(this);
258 DeleteCriticalSection(this);
263 /// Lock protects a code section utilizing a critical section
267 Lock(CritSect
& crit_sect
)
268 : _crit_sect(crit_sect
)
270 EnterCriticalSection(&crit_sect
);
275 LeaveCriticalSection(&_crit_sect
);
279 CritSect
& _crit_sect
;
283 /// Thread base class
290 _hThread
= INVALID_HANDLE_VALUE
;
297 CloseHandle(_hThread
);
304 _hThread
= CreateThread(NULL
, 0, ThreadProc
, this, 0, NULL
);
312 Lock
lock(_crit_sect
);
316 // wait for finishing
317 WaitForSingleObject(_hThread
, INFINITE
);
321 virtual int Run() = 0;
323 bool is_alive() const {return _alive
;}
328 static DWORD WINAPI
ThreadProc(void* para
);
337 /// ClientRect retreives the client area rectangle of a window.
338 struct ClientRect
: public RECT
340 ClientRect(HWND hwnd
)
342 GetClientRect(hwnd
, this);
345 operator LPRECT() {return this;}
347 POINT
& pos() {return *(LPPOINT
)this;}
350 /// ClientRect retreives the window rectangle of a window.
351 struct WindowRect
: public RECT
353 WindowRect(HWND hwnd
)
355 GetWindowRect(hwnd
, this);
358 operator LPRECT() {return this;}
360 POINT
& pos() {return *(LPPOINT
)this;}
363 /// PointL encapsulates the POINT structure into a C++ object.
364 struct Point
: public POINT
366 Point(LONG x_
, LONG y_
)
372 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
375 x
= GET_X_LPARAM(lparam
);
376 y
= GET_Y_LPARAM(lparam
);
379 operator LPPOINT() {return this;}
383 /// transform coordinates in a RECT from client to screen coordiantes
384 inline void ClientToScreen(HWND hwnd
, RECT
* prect
)
385 {::ClientToScreen(hwnd
,(LPPOINT
)&prect
->left
); ::ClientToScreen(hwnd
,(LPPOINT
)&prect
->right
);}
387 /// transform coordinates in a RECT from screen to client coordiantes
388 inline void ScreenToClient(HWND hwnd
, RECT
* prect
)
389 {::ScreenToClient(hwnd
,(LPPOINT
)&prect
->left
); ::ScreenToClient(hwnd
,(LPPOINT
)&prect
->right
);}
392 /// structure containing information about full screen display of the frame window
393 struct FullScreenParameters
395 FullScreenParameters()
408 /// PaintCanvas is a encapsulation of device contexts managed by BeginPaint()/EndPaint().
409 struct PaintCanvas
: public PAINTSTRUCT
411 PaintCanvas(HWND hwnd
)
414 BeginPaint(hwnd
, this);
419 EndPaint(_hwnd
, this);
422 operator HDC() const {return hdc
;}
428 /// Canvas is a encapsulation of device contexts.
431 Canvas(HDC hdc
) : _hdc(hdc
) {}
433 operator HDC() {return _hdc
;}
439 /// WindowCanvas is a encapsulation of client area device contexts.
440 struct WindowCanvas
: public Canvas
442 WindowCanvas(HWND hwnd
)
443 : Canvas(GetDC(hwnd
)), _hwnd(hwnd
) {}
445 ~WindowCanvas() {ReleaseDC(_hwnd
, _hdc
);}
452 // double buffering classes
454 /// Memory Canvas creates and destroys memory devoce contexts.
455 struct MemCanvas
: public Canvas
458 : Canvas(CreateCompatibleDC(hdc
)) {assert(_hdc
);}
460 ~MemCanvas() {DeleteDC(_hdc
);}
463 /// SelectedBitmap is used to localy select bitmaps into device contexts.
464 struct SelectedBitmap
466 SelectedBitmap(HDC hdc
, HBITMAP hbmp
)
467 : _hdc(hdc
), _old_hbmp(SelectBitmap(hdc
, hbmp
)) {}
469 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc
, _old_hbmp
));}
476 /// BufferCanvas manages offscreen bitmaps selected into memory device contexts.
477 struct BufferCanvas
: public MemCanvas
479 BufferCanvas(HDC hdc
, int x
, int y
, int w
, int h
)
480 : MemCanvas(hdc
), _hdctarg(hdc
),
481 _x(x
), _y(y
), _w(w
), _h(h
),
482 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, w
, h
)) {}
484 BufferCanvas(HDC hdc
, const RECT
& rect
)
485 : MemCanvas(hdc
), _hdctarg(hdc
),
486 _x(rect
.left
), _y(rect
.top
), _w(rect
.right
-rect
.left
), _h(rect
.bottom
-rect
.top
),
487 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, _w
, _h
)) {}
495 /// BufferedCanvas enables double buffering for a device context.
496 struct BufferedCanvas
: public BufferCanvas
498 BufferedCanvas(HDC hdc
, int x
, int y
, int w
, int h
, DWORD mode
=SRCCOPY
)
499 : BufferCanvas(hdc
, x
, y
, w
, h
), _mode(mode
) {}
501 BufferedCanvas(HDC hdc
, const RECT
& rect
, DWORD mode
=SRCCOPY
)
502 : BufferCanvas(hdc
, rect
), _mode(mode
) {}
504 ~BufferedCanvas() {BitBlt(_hdctarg
, _x
, _y
, _w
, _h
, _hdc
, 0, 0, _mode
);}
509 /// BufferedPaintCanvas extends PaintCanvas for double buffering.
510 struct BufferedPaintCanvas
: public PaintCanvas
, public BufferedCanvas
512 BufferedPaintCanvas(HWND hwnd
)
514 BufferedCanvas(PAINTSTRUCT::hdc
, 0, 0, rcPaint
.right
, rcPaint
.bottom
)
518 operator HDC() {return BufferedCanvas::_hdc
;}
522 /// TextColor locally selects a text color for drawing.
525 TextColor(HDC hdc
, COLORREF color
)
526 : _hdc(hdc
), _old_color(SetTextColor(hdc
, color
)) {}
528 ~TextColor() {SetTextColor(_hdc
, _old_color
);}
535 /// BkMode locally sets the background mode for drawing.
538 BkMode(HDC hdc
, int bkmode
)
539 : _hdc(hdc
), _old_bkmode(SetBkMode(hdc
, bkmode
)) {}
541 ~BkMode() {SetBkMode(_hdc
, _old_bkmode
);}
545 COLORREF _old_bkmode
;
548 /// FontSelection locally selects a font for drawing.
551 FontSelection(HDC hdc
, HFONT hFont
)
552 : _hdc(hdc
), _old_hFont(SelectFont(hdc
, hFont
)) {}
554 ~FontSelection() {SelectFont(_hdc
, _old_hFont
);}
561 /// BitmapSelection locally selects a bitmap into a device context.
562 struct BitmapSelection
564 BitmapSelection(HDC hdc
, HBITMAP hBmp
)
565 : _hdc(hdc
), _old_hBmp(SelectBitmap(hdc
, hBmp
)) {}
567 ~BitmapSelection() {SelectBitmap(_hdc
, _old_hBmp
);}
574 /// BrushSelection locally selects a brush into a device context.
575 struct BrushSelection
577 BrushSelection(HDC hdc
, HBRUSH hBrush
)
578 : _hdc(hdc
), _old_hBrush(SelectBrush(hdc
, hBrush
)) {}
580 ~BrushSelection() {SelectBrush(_hdc
, _old_hBrush
);}
592 : _hmenu(CreatePopupMenu())
598 operator HMENU() {return _hmenu
;}
600 void Append(UINT id
, LPCTSTR str
, UINT flags
=MF_STRING
)
602 AppendMenu(_hmenu
, flags
, id
, str
);
605 int TrackPopupMenu(HWND hwnd
, const POINT
& pt
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
, LPTPMPARAMS tpm
=NULL
) {
606 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, tpm
);
609 int PopupContextMenu(HWND hwnd
, POINTS pos
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
) {
610 POINT pt
; POINTSTOPOINT(pt
, pos
);
611 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
614 int TrackPopupMenu(HWND hwnd
, POINTS pos
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
) {
615 POINT pt
; POINTSTOPOINT(pt
, pos
);
616 ClientToScreen(hwnd
, &pt
);
617 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
620 int TrackPopupMenuAtCursor(HWND hwnd
, UINT flags
=TPM_LEFTBUTTON
) {
621 POINT pt
; GetCursorPos(&pt
);
622 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
625 int TrackPopupMenuAtPos(HWND hwnd
, DWORD pos
, UINT flags
=TPM_LEFTBUTTON
) {
626 return TrackPopupMenuEx(_hmenu
, flags
, GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
), hwnd
, NULL
);
634 /// string class for convenience
643 typedef wstring super
;
645 typedef string super
;
649 String(LPCTSTR s
) {if (s
) super::assign(s
);}
650 String(LPCTSTR s
, int l
) : super(s
, l
) {}
651 String(const super
& other
) : super(other
) {}
652 String(const String
& other
) : super(other
) {}
655 String(LPCSTR s
) {assign(s
);}
656 String(LPCSTR s
, int l
) {assign(s
, l
);}
657 String(const string
& other
) {assign(other
.c_str());}
658 String
& operator=(LPCSTR s
) {assign(s
); return *this;}
659 void assign(LPCSTR s
) {if (s
) {TCHAR b
[BUFFER_LEN
]; super::assign(b
, MultiByteToWideChar(CP_ACP
, 0, s
, -1, b
, BUFFER_LEN
));} else erase();}
660 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();}
662 String(LPCWSTR s
) {assign(s
);}
663 String(LPCWSTR s
, int l
) {assign(s
, l
);}
664 String(const wstring
& other
) {assign(other
.c_str());}
665 String
& operator=(LPCWSTR s
) {assign(s
); return *this;}
666 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();}
667 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();}
670 String
& operator=(LPCTSTR s
) {if (s
) super::assign(s
); else erase(); return *this;}
671 String
& operator=(const super
& s
) {super::assign(s
); return *this;}
672 void assign(LPCTSTR s
) {super::assign(s
);}
673 void assign(LPCTSTR s
, int l
) {super::assign(s
, l
);}
675 operator LPCTSTR() const {return c_str();}
677 String
& printf(LPCTSTR fmt
, ...)
683 super::assign(b
, _vstprintf(b
, fmt
, l
));
689 String
& vprintf(LPCTSTR fmt
, va_list l
)
693 super::assign(b
, _vstprintf(b
, fmt
, l
));
698 String
& appendf(LPCTSTR fmt
, ...)
704 super::append(b
, _vstprintf(b
, fmt
, l
));
710 String
& vappendf(LPCTSTR fmt
, va_list l
)
714 super::append(b
, _vstprintf(b
, fmt
, l
));
721 struct FmtString
: public String
723 FmtString(LPCTSTR fmt
, ...)
740 int l
= wcslen(s
) + 1;
741 _str
= (LPSTR
) malloc(2*l
);
743 if (WideCharToMultiByte(CP_ACP
, 0, s
, -1, _str
, 2*l
, 0, 0) <= 0)
752 operator LPCSTR() {return _str
;}
758 #define UNC(x) ((LPCWSTR)(x))
762 #define ANS(x) ((LPCSTR)(x))
768 int l
= strlen(s
) + 1;
769 _str
= (LPWSTR
) malloc(2*l
);
771 if (MultiByteToWideChar(CP_ACP
, 0, s
, -1, _str
, l
) <= 0)
780 operator LPCWSTR() {return _str
;}
789 /// link dynamicly to functions by using GetModuleHandle() and GetProcAddress()
790 template<typename FCT
> struct DynamicFct
792 DynamicFct(LPCTSTR moduleName
, UINT ordinal
)
794 HMODULE hModule
= GetModuleHandle(moduleName
);
796 _fct
= (FCT
) GetProcAddress(hModule
, (LPCSTR
)ordinal
);
799 DynamicFct(LPCTSTR moduleName
, LPCSTR name
)
801 HMODULE hModule
= GetModuleHandle(moduleName
);
803 _fct
= (FCT
) GetProcAddress(hModule
, name
);
806 FCT
operator*() const {return _fct
;}
807 operator bool() const {return _fct
? true: false;}
814 /// link dynamicly to functions by using LoadLibrary() and GetProcAddress()
815 template<typename FCT
> struct DynamicLoadLibFct
817 DynamicLoadLibFct(LPCTSTR moduleName
, UINT ordinal
)
819 _hModule
= LoadLibrary(moduleName
);
821 _fct
= (FCT
) GetProcAddress(_hModule
, (LPCSTR
)ordinal
);
824 DynamicLoadLibFct(LPCTSTR moduleName
, LPCSTR name
)
826 _hModule
= LoadLibrary(moduleName
);
828 _fct
= (FCT
) GetProcAddress(_hModule
, name
);
833 FreeLibrary(_hModule
);
836 FCT
operator*() const {return _fct
;}
837 operator bool() const {return _fct
? true: false;}
847 Context(const char* ctx
)
854 Context(const char* ctx
, LPCSTR obj
)
862 Context(const char* ctx
, LPCWSTR obj
)
870 Context(const Context
& other
)
885 String
toString() const;
886 String
getStackTrace() const;
891 static Context
& current() {return *s_current
;}
896 static Context
* s_current
; ///@todo use TLS
897 static Context s_main
;
900 #define CONTEXT_OBJ __ctx__._obj
901 #define CONTEXT(c) Context __ctx__(c)
902 #define CURRENT_CONTEXT Context::current()
903 #define OBJ_CONTEXT(c, o) Context __ctx__(c, o);
906 #endif // __cplusplus