2 * Copyright 2003, 2004, 2005 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
81 #define _tcsrchr wcsrchr
83 #define _tcsrchr strrchr
89 #define _stprintf wcsprintf
91 #define _stprintf sprintf
95 #define U2A(s, d, l) WideCharToMultiByte(CP_ACP, 0, s, -1, d, l, NULL, NULL)
96 #define U2nA(s, d, l) WideCharToMultiByte(CP_ACP, 0, s, l, d, l, NULL, NULL)
97 #define A2U(s, d, l) MultiByteToWideChar(CP_ACP, 0, s, -1, d, l)
98 #define A2nU(s, d, l) MultiByteToWideChar(CP_ACP, 0, s, l, d, l)
103 extern void _wsplitpath(const WCHAR
* path
, WCHAR
* drv
, WCHAR
* dir
, WCHAR
* name
, WCHAR
* ext
);
105 extern void _splitpath(const CHAR
* path
, CHAR
* drv
, CHAR
* dir
, CHAR
* name
, CHAR
* ext
);
107 #define _tcsnicmp strncasecmp
108 #define _tcsicoll strcasecmp
111 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
112 #define FILE_ATTRIBUTE_ENCRYPTED 0x00000040
113 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
114 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
115 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
119 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
120 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
121 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
122 #define Window_SetIcon(hwnd, type, hicon) (HICON)SendMessage(hwnd, WM_SETICON, type, (LPARAM)(hicon))
125 // center window in respect to its parent window
126 extern void CenterWindow(HWND hwnd
);
128 // move window into visibility
129 extern void MoveVisible(HWND hwnd
);
131 // display error message
132 extern void display_error(HWND hwnd
, DWORD error
);
134 // convert time_t to WIN32 FILETIME
135 extern BOOL
time_to_filetime(const time_t* t
, FILETIME
* ftime
);
137 // search for windows of a specific classname
138 extern int find_window_class(LPCTSTR classname
);
140 // create a directory with all missing parent directories
141 BOOL
RecursiveCreateDirectory(LPCTSTR path_in
);
143 // test for existing directory
144 BOOL
exists_path(LPCTSTR path
);
155 #pragma warning(disable: 4786) // disable warnings about too long debug information symbols
158 // STL headers for strings and streams
171 #if _MSC_VER>=1300 // VS.Net
172 #define _NO_COMUTIL //@@
175 #if defined(_MSC_VER) && !defined(_NO_COMUTIL)
177 // COM utility headers
179 using namespace _com_util
;
181 #endif // _MSC_VER && !_NO_COMUTIL
184 // launch a program or document file
185 extern BOOL
launch_file(HWND hwnd
, LPCTSTR cmd
, UINT nCmdShow
=SW_SHOWNORMAL
, LPCTSTR parameters
=NULL
);
187 extern BOOL
launch_fileA(HWND hwnd
, LPSTR cmd
, UINT nCmdShow
=SW_SHOWNORMAL
, LPCSTR parameters
=NULL
);
189 #define launch_fileA launch_file
192 // call an DLL export like rundll32
193 extern BOOL
RunDLL(HWND hwnd
, LPCTSTR dllname
, LPCSTR procname
, LPCTSTR cmdline
, UINT nCmdShow
);
195 // launch control panel applet
196 extern BOOL
launch_cpanel(HWND hwnd
, LPCTSTR applet
);
199 /// initialization of windows common controls
200 struct CommonControlInit
202 CommonControlInit(DWORD flags
=ICC_LISTVIEW_CLASSES
|ICC_TREEVIEW_CLASSES
|ICC_BAR_CLASSES
|ICC_PROGRESS_CLASS
|ICC_COOL_CLASSES
)
204 INITCOMMONCONTROLSEX icc
= {sizeof(INITCOMMONCONTROLSEX
), flags
};
206 InitCommonControlsEx(&icc
);
213 struct WaitCursor
///@todo integrate with WM_SETCURSOR to enable multithreaded background tasks as program launching
217 _old_cursor
= SetCursor(LoadCursor(0, IDC_WAIT
));
222 SetCursor(_old_cursor
);
230 /// base of all structures storing a window handle
233 WindowHandle(HWND hwnd
=0)
236 operator HWND() const {return _hwnd
;}
237 HWND
* operator&() {return &_hwnd
;}
244 /// locally hide a window
245 struct HiddenWindow
: public WindowHandle
247 HiddenWindow(HWND hwnd
)
248 : WindowHandle(IsWindowVisible(hwnd
)? hwnd
: 0)
251 SetWindowPos(_hwnd
, 0, 0, 0, 0, 0, SWP_HIDEWINDOW
|SWP_NOREDRAW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
257 SetWindowPos(_hwnd
, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
262 /// critical section wrapper
264 struct CritSect
: public CRITICAL_SECTION
268 InitializeCriticalSection(this);
273 DeleteCriticalSection(this);
278 /// Lock protects a code section utilizing a critical section
282 Lock(CritSect
& crit_sect
)
283 : _crit_sect(crit_sect
)
285 EnterCriticalSection(&crit_sect
);
290 LeaveCriticalSection(&_crit_sect
);
294 CritSect
& _crit_sect
;
298 /// Thread base class
306 _hThread
= INVALID_HANDLE_VALUE
;
307 _evtFinish
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
314 CloseHandle(_evtFinish
);
315 CloseHandle(_hThread
);
325 _hThread
= CreateThread(NULL
, 0, ThreadProc
, this, 0, NULL
);
331 SetEvent(_evtFinish
);
335 Lock
lock(_crit_sect
);
339 // wait for finishing
340 WaitForSingleObject(_hThread
, INFINITE
);
344 virtual int Run() = 0;
346 bool is_alive() const {return _alive
;}
351 static DWORD WINAPI
ThreadProc(void* para
);
362 /// ClientRect retreives the client area rectangle of a window.
363 struct ClientRect
: public RECT
365 ClientRect(HWND hwnd
)
367 GetClientRect(hwnd
, this);
370 operator LPRECT() {return this;}
372 POINT
& pos() {return *(LPPOINT
)this;}
375 /// ClientRect retreives the window rectangle of a window.
376 struct WindowRect
: public RECT
378 WindowRect(HWND hwnd
)
380 GetWindowRect(hwnd
, this);
383 operator LPRECT() {return this;}
385 POINT
& pos() {return *(LPPOINT
)this;}
388 /// PointL encapsulates the POINT structure into a C++ object.
389 struct Point
: public POINT
391 Point(LONG x_
, LONG y_
)
397 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
400 x
= GET_X_LPARAM(lparam
);
401 y
= GET_Y_LPARAM(lparam
);
404 operator LPPOINT() {return this;}
408 /// transform coordinates in a RECT from client to screen coordiantes
409 inline void ClientToScreen(HWND hwnd
, RECT
* prect
)
410 {::ClientToScreen(hwnd
,(LPPOINT
)&prect
->left
); ::ClientToScreen(hwnd
,(LPPOINT
)&prect
->right
);}
412 /// transform coordinates in a RECT from screen to client coordiantes
413 inline void ScreenToClient(HWND hwnd
, RECT
* prect
)
414 {::ScreenToClient(hwnd
,(LPPOINT
)&prect
->left
); ::ScreenToClient(hwnd
,(LPPOINT
)&prect
->right
);}
417 /// structure containing information about full screen display of the frame window
418 struct FullScreenParameters
420 FullScreenParameters()
433 /// PaintCanvas is a encapsulation of device contexts managed by BeginPaint()/EndPaint().
434 struct PaintCanvas
: public PAINTSTRUCT
436 PaintCanvas(HWND hwnd
)
439 BeginPaint(hwnd
, this);
444 EndPaint(_hwnd
, this);
447 operator HDC() const {return hdc
;}
453 /// Canvas is a encapsulation of device contexts.
456 Canvas(HDC hdc
) : _hdc(hdc
) {}
458 operator HDC() {return _hdc
;}
464 /// WindowCanvas is a encapsulation of client area device contexts.
465 struct WindowCanvas
: public Canvas
467 WindowCanvas(HWND hwnd
)
468 : Canvas(GetDC(hwnd
)), _hwnd(hwnd
) {}
470 ~WindowCanvas() {ReleaseDC(_hwnd
, _hdc
);}
477 // double buffering classes
479 /// Memory Canvas creates and destroys memory devoce contexts.
480 struct MemCanvas
: public Canvas
483 : Canvas(CreateCompatibleDC(hdc
)) {assert(_hdc
);}
485 ~MemCanvas() {DeleteDC(_hdc
);}
488 /// SelectedBitmap is used to localy select bitmaps into device contexts.
489 struct SelectedBitmap
491 SelectedBitmap(HDC hdc
, HBITMAP hbmp
)
492 : _hdc(hdc
), _old_hbmp(SelectBitmap(hdc
, hbmp
)) {}
494 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc
, _old_hbmp
));}
501 /// BufferCanvas manages offscreen bitmaps selected into memory device contexts.
502 struct BufferCanvas
: public MemCanvas
504 BufferCanvas(HDC hdc
, int x
, int y
, int w
, int h
)
505 : MemCanvas(hdc
), _hdctarg(hdc
),
506 _x(x
), _y(y
), _w(w
), _h(h
),
507 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, w
, h
)) {}
509 BufferCanvas(HDC hdc
, const RECT
& rect
)
510 : MemCanvas(hdc
), _hdctarg(hdc
),
511 _x(rect
.left
), _y(rect
.top
), _w(rect
.right
-rect
.left
), _h(rect
.bottom
-rect
.top
),
512 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, _w
, _h
)) {}
520 /// BufferedCanvas enables double buffering for a device context.
521 struct BufferedCanvas
: public BufferCanvas
523 BufferedCanvas(HDC hdc
, int x
, int y
, int w
, int h
, DWORD mode
=SRCCOPY
)
524 : BufferCanvas(hdc
, x
, y
, w
, h
), _mode(mode
) {}
526 BufferedCanvas(HDC hdc
, const RECT
& rect
, DWORD mode
=SRCCOPY
)
527 : BufferCanvas(hdc
, rect
), _mode(mode
) {}
529 ~BufferedCanvas() {BitBlt(_hdctarg
, _x
, _y
, _w
, _h
, _hdc
, 0, 0, _mode
);}
534 /// BufferedPaintCanvas extends PaintCanvas for double buffering.
535 struct BufferedPaintCanvas
: public PaintCanvas
, public BufferedCanvas
537 BufferedPaintCanvas(HWND hwnd
)
539 BufferedCanvas(PAINTSTRUCT::hdc
, 0, 0, rcPaint
.right
, rcPaint
.bottom
)
543 operator HDC() {return BufferedCanvas::_hdc
;}
547 /// TextColor locally selects a text color for drawing.
550 TextColor(HDC hdc
, COLORREF color
)
551 : _hdc(hdc
), _old_color(SetTextColor(hdc
, color
)) {}
553 ~TextColor() {SetTextColor(_hdc
, _old_color
);}
560 /// BkMode locally sets the background mode for drawing.
563 BkMode(HDC hdc
, int bkmode
)
564 : _hdc(hdc
), _old_bkmode(SetBkMode(hdc
, bkmode
)) {}
566 ~BkMode() {SetBkMode(_hdc
, _old_bkmode
);}
570 COLORREF _old_bkmode
;
573 /// FontSelection locally selects a font for drawing.
576 FontSelection(HDC hdc
, HFONT hFont
)
577 : _hdc(hdc
), _old_hFont(SelectFont(hdc
, hFont
)) {}
579 ~FontSelection() {SelectFont(_hdc
, _old_hFont
);}
586 /// BitmapSelection locally selects a bitmap into a device context.
587 struct BitmapSelection
589 BitmapSelection(HDC hdc
, HBITMAP hBmp
)
590 : _hdc(hdc
), _old_hBmp(SelectBitmap(hdc
, hBmp
)) {}
592 ~BitmapSelection() {SelectBitmap(_hdc
, _old_hBmp
);}
599 /// BrushSelection locally selects a brush into a device context.
600 struct BrushSelection
602 BrushSelection(HDC hdc
, HBRUSH hBrush
)
603 : _hdc(hdc
), _old_hBrush(SelectBrush(hdc
, hBrush
)) {}
605 ~BrushSelection() {SelectBrush(_hdc
, _old_hBrush
);}
617 : _hmenu(CreatePopupMenu())
623 operator HMENU() {return _hmenu
;}
625 void Append(UINT id
, LPCTSTR str
, UINT flags
=MF_STRING
)
627 AppendMenu(_hmenu
, flags
, id
, str
);
630 int TrackPopupMenu(HWND hwnd
, const POINT
& pt
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
, LPTPMPARAMS tpm
=NULL
) {
631 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, tpm
);
634 int PopupContextMenu(HWND hwnd
, POINTS pos
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
) {
635 POINT pt
; POINTSTOPOINT(pt
, pos
);
636 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
639 int TrackPopupMenu(HWND hwnd
, POINTS pos
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
) {
640 POINT pt
; POINTSTOPOINT(pt
, pos
);
641 ClientToScreen(hwnd
, &pt
);
642 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
645 int TrackPopupMenuAtCursor(HWND hwnd
, UINT flags
=TPM_LEFTBUTTON
) {
646 POINT pt
; GetCursorPos(&pt
);
647 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
650 int TrackPopupMenuAtPos(HWND hwnd
, DWORD pos
, UINT flags
=TPM_LEFTBUTTON
) {
651 return TrackPopupMenuEx(_hmenu
, flags
, GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
), hwnd
, NULL
);
659 struct Variant
: public VARIANT
661 Variant() {VariantInit(this);}
662 Variant(const VARIANT
& var
);
663 Variant(const VARIANT
* var
);
666 operator long() const;
667 operator bool() const;
668 operator VARIANT_BOOL() const;
669 operator IDispatch
*() const;
682 _p
= SysAllocString(s
);
690 _p
= SysAllocStringLen(b
, MultiByteToWideChar(CP_ACP
, 0, s
, -1, b
, BUFFER_LEN
)-1);
697 _p
= SysAllocString(s
);
700 BStr(const VARIANT
& var
)
712 void assign(const VARIANT
& var
);
714 operator BSTR() const
716 return _p
? _p
: (BSTR
)L
"";
721 return _p
? wcslen(_p
): 0;
729 /// string class for TCHAR strings
738 typedef wstring super
;
740 typedef string super
;
745 String(LPCTSTR s
) {if (s
) super::assign(s
);}
746 String(LPCTSTR s
, int l
) : super(s
, l
) {}
748 String(const super
& other
) : super(other
) {}
749 String(const String
& other
) : super(other
) {}
752 String(LPCSTR s
) {assign(s
);}
753 String(LPCSTR s
, int l
) {assign(s
, l
);}
754 String(const string
& other
) {assign(other
.c_str());}
755 String
& operator=(LPCSTR s
) {assign(s
); return *this;}
756 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();}
757 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();}
758 void assign(const BStr
& s
) {int l
= s
.length(); super::assign(s
, l
);}
760 String(LPCWSTR s
) {assign(s
);}
761 String(LPCWSTR s
, int l
) {assign(s
, l
);}
762 String(const wstring
& other
) {assign(other
.c_str());}
763 String
& operator=(LPCWSTR s
) {assign(s
); return *this;}
764 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();}
765 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();}
766 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();}
768 String(const BStr
& s
) {assign(s
);}
769 String
& operator=(const BStr
& s
) {assign(s
); return *this;}
771 String
& operator=(LPCTSTR s
) {if (s
) super::assign(s
); else erase(); return *this;}
772 String
& operator=(const super
& s
) {super::assign(s
); return *this;}
773 void assign(LPCTSTR s
) {super::assign(s
);}
774 void assign(LPCTSTR s
, int l
) {super::assign(s
, l
);}
776 operator LPCTSTR() const {return c_str();}
779 operator string() const {char b
[BUFFER_LEN
]; return string(b
, WideCharToMultiByte(CP_ACP
, 0, c_str(), -1, b
, BUFFER_LEN
, 0, 0)-1);}
781 operator wstring() const {WCHAR b
[BUFFER_LEN
]; return wstring(b
, MultiByteToWideChar(CP_ACP
, 0, c_str(), -1, b
, BUFFER_LEN
)-1);}
784 String
& printf(LPCTSTR fmt
, ...)
790 super::assign(b
, _vstprintf(b
, fmt
, l
));
796 String
& vprintf(LPCTSTR fmt
, va_list l
)
800 super::assign(b
, _vstprintf(b
, fmt
, l
));
805 String
& appendf(LPCTSTR fmt
, ...)
811 super::append(b
, _vstprintf(b
, fmt
, l
));
817 String
& vappendf(LPCTSTR fmt
, va_list l
)
821 super::append(b
, _vstprintf(b
, fmt
, l
));
827 #define _STRING_DEFINED
830 struct FmtString
: public String
832 FmtString(LPCTSTR fmt
, ...)
849 int l
= wcslen(s
) + 1;
850 _str
= (LPSTR
) malloc(2*l
);
852 if (WideCharToMultiByte(CP_ACP
, 0, s
, -1, _str
, 2*l
, 0, 0) <= 0)
861 operator LPCSTR() {return _str
;}
867 #define UNC(x) ((LPCWSTR)(x))
871 #define ANS(x) ((LPCSTR)(x))
877 int l
= strlen(s
) + 1;
878 _str
= (LPWSTR
) malloc(2*l
);
880 if (MultiByteToWideChar(CP_ACP
, 0, s
, -1, _str
, l
) <= 0)
889 operator LPCWSTR() {return _str
;}
898 // determine windows version string
899 //@@String get_windows_version_str();
902 /// link dynamicly to functions by using GetModuleHandle() and GetProcAddress()
903 template<typename FCT
> struct DynamicFct
905 DynamicFct(LPCTSTR moduleName
, UINT ordinal
)
907 HMODULE hModule
= GetModuleHandle(moduleName
);
909 _fct
= (FCT
) GetProcAddress(hModule
, (LPCSTR
)ordinal
);
912 DynamicFct(LPCTSTR moduleName
, LPCSTR name
)
914 HMODULE hModule
= GetModuleHandle(moduleName
);
916 _fct
= (FCT
) GetProcAddress(hModule
, name
);
919 FCT
operator*() const {return _fct
;}
920 operator bool() const {return _fct
? true: false;}
927 /// link dynamicly to functions by using LoadLibrary() and GetProcAddress()
928 template<typename FCT
> struct DynamicLoadLibFct
930 DynamicLoadLibFct(LPCTSTR moduleName
, UINT ordinal
)
932 _hModule
= LoadLibrary(moduleName
);
934 _fct
= (FCT
) GetProcAddress(_hModule
, (LPCSTR
)ordinal
);
937 DynamicLoadLibFct(LPCTSTR moduleName
, LPCSTR name
)
939 _hModule
= LoadLibrary(moduleName
);
941 _fct
= (FCT
) GetProcAddress(_hModule
, name
);
946 FreeLibrary(_hModule
);
949 FCT
operator*() const {return _fct
;}
950 operator bool() const {return _fct
? true: false;}
960 Context(const char* ctx
)
967 Context(const char* ctx
, LPCSTR obj
)
975 Context(const char* ctx
, LPCWSTR obj
)
983 Context(const Context
& other
)
998 String
toString() const;
999 String
getStackTrace() const;
1004 static Context
& current() {return *s_current
;}
1009 static Context
* s_current
; ///@todo use TLS
1010 static Context s_main
;
1013 #define CONTEXT_OBJ __ctx__._obj
1014 #define CONTEXT(c) Context __ctx__(c)
1015 #define CURRENT_CONTEXT Context::current()
1016 #define OBJ_CONTEXT(c, o) Context __ctx__(c, o)
1019 extern bool SplitFileSysURL(LPCTSTR url
, String
& dir_out
, String
& fname_out
);
1022 #endif // __cplusplus