2 * Copyright 2003, 2004 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(), ...
46 #include <oleauto.h> // for VARIANT
48 #include <malloc.h> // for alloca()
50 #include <stdlib.h> // for _MAX_DIR, ...
51 #include <stdio.h> // for sprintf()
56 #define _MAX_FNAME 256
57 #define _MAX_DIR _MAX_FNAME
58 #define _MAX_EXT _MAX_FNAME
68 #define for if (0) {} else for
70 #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
73 #define BUFFER_LEN 2048
76 extern void _log_(LPCTSTR txt
);
78 #define LOG(txt) _log_(txt)
82 #define LONGLONGARG TEXT("I64")
84 #define LONGLONGARG TEXT("L")
90 #define _tcsrchr wcsrchr
92 #define _tcsrchr strrchr
98 #define _stprintf wcsprintf
100 #define _stprintf sprintf
104 #define U2A(s, d, l) WideCharToMultiByte(CP_ACP, 0, s, -1, d, l, NULL, NULL)
105 #define U2nA(s, d, l) WideCharToMultiByte(CP_ACP, 0, s, l, d, l, NULL, NULL)
106 #define A2U(s, d, l) MultiByteToWideChar(CP_ACP, 0, s, -1, d, l)
107 #define A2nU(s, d, l) MultiByteToWideChar(CP_ACP, 0, s, l, d, l)
112 extern void _wsplitpath(const WCHAR
* path
, WCHAR
* drv
, WCHAR
* dir
, WCHAR
* name
, WCHAR
* ext
);
114 extern void _splitpath(const CHAR
* path
, CHAR
* drv
, CHAR
* dir
, CHAR
* name
, CHAR
* ext
);
116 #define _tcsnicmp strncasecmp
117 #define _tcsicoll strcasecmp
120 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
121 #define FILE_ATTRIBUTE_ENCRYPTED 0x00000040
122 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
123 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
124 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
128 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
129 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
130 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
131 #define Window_SetIcon(hwnd, type, hicon) (HICON)SendMessage(hwnd, WM_SETICON, type, (LPARAM)(hicon))
134 // center window in respect to its parent window
135 extern void CenterWindow(HWND hwnd
);
137 // move window into visibility
138 extern void MoveVisible(HWND hwnd
);
140 // display error message
141 extern void display_error(HWND hwnd
, DWORD error
);
143 // convert time_t to WIN32 FILETIME
144 extern BOOL
time_to_filetime(const time_t* t
, FILETIME
* ftime
);
146 // search for windows of a specific classname
147 extern int find_window_class(LPCTSTR classname
);
149 // create a directory with all missing parent directories
150 BOOL
RecursiveCreateDirectory(LPCTSTR path_in
);
161 #pragma warning(disable: 4786) // disable warnings about too long debug information symbols
164 // STL headers for strings and streams
176 #if _MSC_VER>=1300 // VS.Net
177 #define _NO_COMUTIL //@@
180 #if defined(_MSC_VER) && !defined(_NO_COMUTIL)
182 // COM utility headers
184 using namespace _com_util
;
186 #endif // _MSC_VER && !_NO_COMUTIL
189 // launch a program or document file
190 extern BOOL
launch_file(HWND hwnd
, LPCTSTR cmd
, UINT nCmdShow
, LPCTSTR parameters
=NULL
);
192 extern BOOL
launch_fileA(HWND hwnd
, LPSTR cmd
, UINT nCmdShow
, LPCSTR parameters
=NULL
);
194 #define launch_fileA launch_file
197 // call an DLL export like rundll32
198 extern BOOL
RunDLL(HWND hwnd
, LPCTSTR dllname
, LPCSTR procname
, LPCTSTR cmdline
, UINT nCmdShow
);
200 // launch control panel applet
201 extern BOOL
launch_cpanel(HWND hwnd
, LPCTSTR applet
);
204 /// initialization of windows common controls
205 struct CommonControlInit
207 CommonControlInit(DWORD flags
=ICC_LISTVIEW_CLASSES
|ICC_TREEVIEW_CLASSES
|ICC_BAR_CLASSES
|ICC_PROGRESS_CLASS
|ICC_COOL_CLASSES
)
209 INITCOMMONCONTROLSEX icc
= {sizeof(INITCOMMONCONTROLSEX
), flags
};
211 InitCommonControlsEx(&icc
);
218 struct WaitCursor
///@todo integrate with WM_SETCURSOR to enable multithreaded background tasks as program launching
222 _old_cursor
= SetCursor(LoadCursor(0, IDC_WAIT
));
227 SetCursor(_old_cursor
);
235 /// base of all structures storing a window handle
238 WindowHandle(HWND hwnd
=0)
241 operator HWND() const {return _hwnd
;}
242 HWND
* operator&() {return &_hwnd
;}
249 /// locally hide a window
250 struct HiddenWindow
: public WindowHandle
252 HiddenWindow(HWND hwnd
)
255 SetWindowPos(hwnd
, 0, 0, 0, 0, 0, SWP_HIDEWINDOW
|SWP_NOREDRAW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
260 SetWindowPos(_hwnd
, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
265 /// critical section wrapper
267 struct CritSect
: public CRITICAL_SECTION
271 InitializeCriticalSection(this);
276 DeleteCriticalSection(this);
281 /// Lock protects a code section utilizing a critical section
285 Lock(CritSect
& crit_sect
)
286 : _crit_sect(crit_sect
)
288 EnterCriticalSection(&crit_sect
);
293 LeaveCriticalSection(&_crit_sect
);
297 CritSect
& _crit_sect
;
301 /// Thread base class
308 _hThread
= INVALID_HANDLE_VALUE
;
315 CloseHandle(_hThread
);
322 _hThread
= CreateThread(NULL
, 0, ThreadProc
, this, 0, NULL
);
330 Lock
lock(_crit_sect
);
334 // wait for finishing
335 WaitForSingleObject(_hThread
, INFINITE
);
339 virtual int Run() = 0;
341 bool is_alive() const {return _alive
;}
346 static DWORD WINAPI
ThreadProc(void* para
);
355 /// ClientRect retreives the client area rectangle of a window.
356 struct ClientRect
: public RECT
358 ClientRect(HWND hwnd
)
360 GetClientRect(hwnd
, this);
363 operator LPRECT() {return this;}
365 POINT
& pos() {return *(LPPOINT
)this;}
368 /// ClientRect retreives the window rectangle of a window.
369 struct WindowRect
: public RECT
371 WindowRect(HWND hwnd
)
373 GetWindowRect(hwnd
, this);
376 operator LPRECT() {return this;}
378 POINT
& pos() {return *(LPPOINT
)this;}
381 /// PointL encapsulates the POINT structure into a C++ object.
382 struct Point
: public POINT
384 Point(LONG x_
, LONG y_
)
390 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
393 x
= GET_X_LPARAM(lparam
);
394 y
= GET_Y_LPARAM(lparam
);
397 operator LPPOINT() {return this;}
401 /// transform coordinates in a RECT from client to screen coordiantes
402 inline void ClientToScreen(HWND hwnd
, RECT
* prect
)
403 {::ClientToScreen(hwnd
,(LPPOINT
)&prect
->left
); ::ClientToScreen(hwnd
,(LPPOINT
)&prect
->right
);}
405 /// transform coordinates in a RECT from screen to client coordiantes
406 inline void ScreenToClient(HWND hwnd
, RECT
* prect
)
407 {::ScreenToClient(hwnd
,(LPPOINT
)&prect
->left
); ::ScreenToClient(hwnd
,(LPPOINT
)&prect
->right
);}
410 /// structure containing information about full screen display of the frame window
411 struct FullScreenParameters
413 FullScreenParameters()
426 /// PaintCanvas is a encapsulation of device contexts managed by BeginPaint()/EndPaint().
427 struct PaintCanvas
: public PAINTSTRUCT
429 PaintCanvas(HWND hwnd
)
432 BeginPaint(hwnd
, this);
437 EndPaint(_hwnd
, this);
440 operator HDC() const {return hdc
;}
446 /// Canvas is a encapsulation of device contexts.
449 Canvas(HDC hdc
) : _hdc(hdc
) {}
451 operator HDC() {return _hdc
;}
457 /// WindowCanvas is a encapsulation of client area device contexts.
458 struct WindowCanvas
: public Canvas
460 WindowCanvas(HWND hwnd
)
461 : Canvas(GetDC(hwnd
)), _hwnd(hwnd
) {}
463 ~WindowCanvas() {ReleaseDC(_hwnd
, _hdc
);}
470 // double buffering classes
472 /// Memory Canvas creates and destroys memory devoce contexts.
473 struct MemCanvas
: public Canvas
476 : Canvas(CreateCompatibleDC(hdc
)) {assert(_hdc
);}
478 ~MemCanvas() {DeleteDC(_hdc
);}
481 /// SelectedBitmap is used to localy select bitmaps into device contexts.
482 struct SelectedBitmap
484 SelectedBitmap(HDC hdc
, HBITMAP hbmp
)
485 : _hdc(hdc
), _old_hbmp(SelectBitmap(hdc
, hbmp
)) {}
487 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc
, _old_hbmp
));}
494 /// BufferCanvas manages offscreen bitmaps selected into memory device contexts.
495 struct BufferCanvas
: public MemCanvas
497 BufferCanvas(HDC hdc
, int x
, int y
, int w
, int h
)
498 : MemCanvas(hdc
), _hdctarg(hdc
),
499 _x(x
), _y(y
), _w(w
), _h(h
),
500 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, w
, h
)) {}
502 BufferCanvas(HDC hdc
, const RECT
& rect
)
503 : MemCanvas(hdc
), _hdctarg(hdc
),
504 _x(rect
.left
), _y(rect
.top
), _w(rect
.right
-rect
.left
), _h(rect
.bottom
-rect
.top
),
505 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, _w
, _h
)) {}
513 /// BufferedCanvas enables double buffering for a device context.
514 struct BufferedCanvas
: public BufferCanvas
516 BufferedCanvas(HDC hdc
, int x
, int y
, int w
, int h
, DWORD mode
=SRCCOPY
)
517 : BufferCanvas(hdc
, x
, y
, w
, h
), _mode(mode
) {}
519 BufferedCanvas(HDC hdc
, const RECT
& rect
, DWORD mode
=SRCCOPY
)
520 : BufferCanvas(hdc
, rect
), _mode(mode
) {}
522 ~BufferedCanvas() {BitBlt(_hdctarg
, _x
, _y
, _w
, _h
, _hdc
, 0, 0, _mode
);}
527 /// BufferedPaintCanvas extends PaintCanvas for double buffering.
528 struct BufferedPaintCanvas
: public PaintCanvas
, public BufferedCanvas
530 BufferedPaintCanvas(HWND hwnd
)
532 BufferedCanvas(PAINTSTRUCT::hdc
, 0, 0, rcPaint
.right
, rcPaint
.bottom
)
536 operator HDC() {return BufferedCanvas::_hdc
;}
540 /// TextColor locally selects a text color for drawing.
543 TextColor(HDC hdc
, COLORREF color
)
544 : _hdc(hdc
), _old_color(SetTextColor(hdc
, color
)) {}
546 ~TextColor() {SetTextColor(_hdc
, _old_color
);}
553 /// BkMode locally sets the background mode for drawing.
556 BkMode(HDC hdc
, int bkmode
)
557 : _hdc(hdc
), _old_bkmode(SetBkMode(hdc
, bkmode
)) {}
559 ~BkMode() {SetBkMode(_hdc
, _old_bkmode
);}
563 COLORREF _old_bkmode
;
566 /// FontSelection locally selects a font for drawing.
569 FontSelection(HDC hdc
, HFONT hFont
)
570 : _hdc(hdc
), _old_hFont(SelectFont(hdc
, hFont
)) {}
572 ~FontSelection() {SelectFont(_hdc
, _old_hFont
);}
579 /// BitmapSelection locally selects a bitmap into a device context.
580 struct BitmapSelection
582 BitmapSelection(HDC hdc
, HBITMAP hBmp
)
583 : _hdc(hdc
), _old_hBmp(SelectBitmap(hdc
, hBmp
)) {}
585 ~BitmapSelection() {SelectBitmap(_hdc
, _old_hBmp
);}
592 /// BrushSelection locally selects a brush into a device context.
593 struct BrushSelection
595 BrushSelection(HDC hdc
, HBRUSH hBrush
)
596 : _hdc(hdc
), _old_hBrush(SelectBrush(hdc
, hBrush
)) {}
598 ~BrushSelection() {SelectBrush(_hdc
, _old_hBrush
);}
610 : _hmenu(CreatePopupMenu())
616 operator HMENU() {return _hmenu
;}
618 void Append(UINT id
, LPCTSTR str
, UINT flags
=MF_STRING
)
620 AppendMenu(_hmenu
, flags
, id
, str
);
623 int TrackPopupMenu(HWND hwnd
, const POINT
& pt
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
, LPTPMPARAMS tpm
=NULL
) {
624 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, tpm
);
627 int PopupContextMenu(HWND hwnd
, POINTS pos
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
) {
628 POINT pt
; POINTSTOPOINT(pt
, pos
);
629 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
632 int TrackPopupMenu(HWND hwnd
, POINTS pos
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
) {
633 POINT pt
; POINTSTOPOINT(pt
, pos
);
634 ClientToScreen(hwnd
, &pt
);
635 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
638 int TrackPopupMenuAtCursor(HWND hwnd
, UINT flags
=TPM_LEFTBUTTON
) {
639 POINT pt
; GetCursorPos(&pt
);
640 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
643 int TrackPopupMenuAtPos(HWND hwnd
, DWORD pos
, UINT flags
=TPM_LEFTBUTTON
) {
644 return TrackPopupMenuEx(_hmenu
, flags
, GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
), hwnd
, NULL
);
652 struct Variant
: public VARIANT
654 Variant() {VariantInit(this);}
655 Variant(const VARIANT
& var
);
656 Variant(const VARIANT
* var
);
659 operator long() const;
660 operator bool() const;
661 operator VARIANT_BOOL() const;
662 operator IDispatch
*() const;
675 _p
= SysAllocString(s
);
683 _p
= SysAllocStringLen(b
, MultiByteToWideChar(CP_ACP
, 0, s
, -1, b
, BUFFER_LEN
)-1);
690 _p
= SysAllocString(s
);
693 BStr(const VARIANT
& var
)
705 void assign(const VARIANT
& var
);
707 operator BSTR() const
709 return _p
? _p
: (BSTR
)L
"";
714 return _p
? wcslen(_p
): 0;
722 /// string class for TCHAR strings
731 typedef wstring super
;
733 typedef string super
;
737 String(LPCTSTR s
) {if (s
) super::assign(s
);}
738 String(LPCTSTR s
, int l
) : super(s
, l
) {}
739 String(const super
& other
) : super(other
) {}
740 String(const String
& other
) : super(other
) {}
743 String(LPCSTR s
) {assign(s
);}
744 String(LPCSTR s
, int l
) {assign(s
, l
);}
745 String(const string
& other
) {assign(other
.c_str());}
746 String
& operator=(LPCSTR s
) {assign(s
); return *this;}
747 void assign(LPCSTR s
) {if (s
) {TCHAR b
[BUFFER_LEN
]; super::assign(b
, MultiByteToWideChar(CP_ACP
, 0, s
, -1, b
, BUFFER_LEN
)-1);} else erase();}
748 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();}
749 void assign(const BStr
& s
) {int l
= s
.length(); super::assign(s
, l
);}
751 String(LPCWSTR s
) {assign(s
);}
752 String(LPCWSTR s
, int l
) {assign(s
, l
);}
753 String(const wstring
& other
) {assign(other
.c_str());}
754 String
& operator=(LPCWSTR s
) {assign(s
); return *this;}
755 void assign(LPCWSTR s
) {if (s
) {char b
[BUFFER_LEN
]; super::assign(b
, WideCharToMultiByte(CP_ACP
, 0, s
, -1, b
, BUFFER_LEN
, 0, 0)-1);} else erase();}
756 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();}
757 void assign(const BStr
& s
) {int l
= s
.length(); if (l
) {char b
[BUFFER_LEN
]; super::assign(b
, WideCharToMultiByte(CP_ACP
, 0, s
, l
, b
, BUFFER_LEN
, 0, 0));} else erase();}
759 String(const BStr
& s
) {assign(s
);}
760 String
& operator=(const BStr
& s
) {assign(s
); return *this;}
762 String
& operator=(LPCTSTR s
) {if (s
) super::assign(s
); else erase(); return *this;}
763 String
& operator=(const super
& s
) {super::assign(s
); return *this;}
764 void assign(LPCTSTR s
) {super::assign(s
);}
765 void assign(LPCTSTR s
, int l
) {super::assign(s
, l
);}
767 operator LPCTSTR() const {return c_str();}
770 operator string() const {char b
[BUFFER_LEN
]; return string(b
, WideCharToMultiByte(CP_ACP
, 0, c_str(), -1, b
, BUFFER_LEN
, 0, 0)-1);}
772 operator wstring() const {WCHAR b
[BUFFER_LEN
]; return wstring(b
, MultiByteToWideChar(CP_ACP
, 0, c_str(), -1, b
, BUFFER_LEN
)-1);}
775 String
& printf(LPCTSTR fmt
, ...)
781 super::assign(b
, _vstprintf(b
, fmt
, l
));
787 String
& vprintf(LPCTSTR fmt
, va_list l
)
791 super::assign(b
, _vstprintf(b
, fmt
, l
));
796 String
& appendf(LPCTSTR fmt
, ...)
802 super::append(b
, _vstprintf(b
, fmt
, l
));
808 String
& vappendf(LPCTSTR fmt
, va_list l
)
812 super::append(b
, _vstprintf(b
, fmt
, l
));
818 #define _STRING_DEFINED
821 struct FmtString
: public String
823 FmtString(LPCTSTR fmt
, ...)
840 int l
= wcslen(s
) + 1;
841 _str
= (LPSTR
) malloc(2*l
);
843 if (WideCharToMultiByte(CP_ACP
, 0, s
, -1, _str
, 2*l
, 0, 0) <= 0)
852 operator LPCSTR() {return _str
;}
858 #define UNC(x) ((LPCWSTR)(x))
862 #define ANS(x) ((LPCSTR)(x))
868 int l
= strlen(s
) + 1;
869 _str
= (LPWSTR
) malloc(2*l
);
871 if (MultiByteToWideChar(CP_ACP
, 0, s
, -1, _str
, l
) <= 0)
880 operator LPCWSTR() {return _str
;}
889 /// link dynamicly to functions by using GetModuleHandle() and GetProcAddress()
890 template<typename FCT
> struct DynamicFct
892 DynamicFct(LPCTSTR moduleName
, UINT ordinal
)
894 HMODULE hModule
= GetModuleHandle(moduleName
);
896 _fct
= (FCT
) GetProcAddress(hModule
, (LPCSTR
)ordinal
);
899 DynamicFct(LPCTSTR moduleName
, LPCSTR name
)
901 HMODULE hModule
= GetModuleHandle(moduleName
);
903 _fct
= (FCT
) GetProcAddress(hModule
, name
);
906 FCT
operator*() const {return _fct
;}
907 operator bool() const {return _fct
? true: false;}
914 /// link dynamicly to functions by using LoadLibrary() and GetProcAddress()
915 template<typename FCT
> struct DynamicLoadLibFct
917 DynamicLoadLibFct(LPCTSTR moduleName
, UINT ordinal
)
919 _hModule
= LoadLibrary(moduleName
);
921 _fct
= (FCT
) GetProcAddress(_hModule
, (LPCSTR
)ordinal
);
924 DynamicLoadLibFct(LPCTSTR moduleName
, LPCSTR name
)
926 _hModule
= LoadLibrary(moduleName
);
928 _fct
= (FCT
) GetProcAddress(_hModule
, name
);
933 FreeLibrary(_hModule
);
936 FCT
operator*() const {return _fct
;}
937 operator bool() const {return _fct
? true: false;}
947 Context(const char* ctx
)
954 Context(const char* ctx
, LPCSTR obj
)
962 Context(const char* ctx
, LPCWSTR obj
)
970 Context(const Context
& other
)
985 String
toString() const;
986 String
getStackTrace() const;
991 static Context
& current() {return *s_current
;}
996 static Context
* s_current
; ///@todo use TLS
997 static Context s_main
;
1000 #define CONTEXT_OBJ __ctx__._obj
1001 #define CONTEXT(c) Context __ctx__(c)
1002 #define CURRENT_CONTEXT Context::current()
1003 #define OBJ_CONTEXT(c, o) Context __ctx__(c, o);
1006 #endif // __cplusplus