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
21 // Explorer clone, lean version
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(), ...
47 #include <malloc.h> // for alloca()
49 #include <stdlib.h> // for _MAX_DIR, ...
50 #include <stdio.h> // for sprintf()
55 #define _MAX_FNAME 256
56 #define _MAX_DIR _MAX_FNAME
57 #define _MAX_EXT _MAX_FNAME
67 #define for if (0) {} else for
69 #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
72 #define BUFFER_LEN 1024
75 extern void _log_(LPCTSTR txt
);
77 #define LOG(txt) _log_(txt)
81 #define LONGLONGARG TEXT("I64")
83 #define LONGLONGARG TEXT("L")
89 #define _tcsrchr wcsrchr
91 #define _tcsrchr strrchr
97 #define _stprintf wcsprintf
99 #define _stprintf sprintf
103 #define U2A(s, d, l) WideCharToMultiByte(CP_ACP, 0, s, -1, d, l, NULL, NULL)
104 #define U2nA(s, d, l) WideCharToMultiByte(CP_ACP, 0, s, l, d, l, NULL, NULL)
105 #define A2U(s, d, l) MultiByteToWideChar(CP_ACP, 0, s, -1, d, l)
106 #define A2nU(s, d, l) MultiByteToWideChar(CP_ACP, 0, s, l, d, l)
109 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
110 #define FILE_ATTRIBUTE_ENCRYPTED 0x00000040
111 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
112 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
113 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
117 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
118 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
119 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
120 #define Window_SetIcon(hwnd, type, hicon) (HICON)SendMessage(hwnd, WM_SETICON, type, (LPARAM)(hicon))
123 // center window in respect to its parent window
124 extern void CenterWindow(HWND hwnd
);
126 // move window into visibility
127 extern void MoveVisible(HWND hwnd
);
129 // display error message
130 extern void display_error(HWND hwnd
, DWORD error
);
132 // convert time_t to WIN32 FILETIME
133 extern BOOL
time_to_filetime(const time_t* t
, FILETIME
* ftime
);
135 // search for windows of a specific classname
136 extern int find_window_class(LPCTSTR classname
);
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
);
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 /// initialization of windows common controls
190 struct CommonControlInit
192 CommonControlInit(DWORD flags
=ICC_LISTVIEW_CLASSES
|ICC_TREEVIEW_CLASSES
|ICC_BAR_CLASSES
|ICC_PROGRESS_CLASS
|ICC_COOL_CLASSES
)
194 INITCOMMONCONTROLSEX icc
= {sizeof(INITCOMMONCONTROLSEX
), flags
};
196 InitCommonControlsEx(&icc
);
203 struct WaitCursor
///@todo integrate with WM_SETCURSOR to enable multithreaded background tasks as program launching
207 _old_cursor
= SetCursor(LoadCursor(0, IDC_WAIT
));
212 SetCursor(_old_cursor
);
220 /// base of all structures storing a window handle
223 WindowHandle(HWND hwnd
=0)
226 operator HWND() const {return _hwnd
;}
227 HWND
* operator&() {return &_hwnd
;}
234 /// locally hide a window
235 struct HiddenWindow
: public WindowHandle
237 HiddenWindow(HWND hwnd
)
240 SetWindowPos(hwnd
, 0, 0, 0, 0, 0, SWP_HIDEWINDOW
|SWP_NOREDRAW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
245 SetWindowPos(_hwnd
, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
250 /// critical section wrapper
252 struct CritSect
: public CRITICAL_SECTION
256 InitializeCriticalSection(this);
261 DeleteCriticalSection(this);
266 /// Lock protects a code section utilizing a critical section
270 Lock(CritSect
& crit_sect
)
271 : _crit_sect(crit_sect
)
273 EnterCriticalSection(&crit_sect
);
278 LeaveCriticalSection(&_crit_sect
);
282 CritSect
& _crit_sect
;
286 /// Thread base class
293 _hThread
= INVALID_HANDLE_VALUE
;
300 CloseHandle(_hThread
);
307 _hThread
= CreateThread(NULL
, 0, ThreadProc
, this, 0, NULL
);
315 Lock
lock(_crit_sect
);
319 // wait for finishing
320 WaitForSingleObject(_hThread
, INFINITE
);
324 virtual int Run() = 0;
326 bool is_alive() const {return _alive
;}
331 static DWORD WINAPI
ThreadProc(void* para
);
340 /// ClientRect retreives the client area rectangle of a window.
341 struct ClientRect
: public RECT
343 ClientRect(HWND hwnd
)
345 GetClientRect(hwnd
, this);
348 operator LPRECT() {return this;}
350 POINT
& pos() {return *(LPPOINT
)this;}
353 /// ClientRect retreives the window rectangle of a window.
354 struct WindowRect
: public RECT
356 WindowRect(HWND hwnd
)
358 GetWindowRect(hwnd
, this);
361 operator LPRECT() {return this;}
363 POINT
& pos() {return *(LPPOINT
)this;}
366 /// PointL encapsulates the POINT structure into a C++ object.
367 struct Point
: public POINT
369 Point(LONG x_
, LONG y_
)
375 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
378 x
= GET_X_LPARAM(lparam
);
379 y
= GET_Y_LPARAM(lparam
);
382 operator LPPOINT() {return this;}
386 /// transform coordinates in a RECT from client to screen coordiantes
387 inline void ClientToScreen(HWND hwnd
, RECT
* prect
)
388 {::ClientToScreen(hwnd
,(LPPOINT
)&prect
->left
); ::ClientToScreen(hwnd
,(LPPOINT
)&prect
->right
);}
390 /// transform coordinates in a RECT from screen to client coordiantes
391 inline void ScreenToClient(HWND hwnd
, RECT
* prect
)
392 {::ScreenToClient(hwnd
,(LPPOINT
)&prect
->left
); ::ScreenToClient(hwnd
,(LPPOINT
)&prect
->right
);}
397 /// PaintCanvas is a encapsulation of device contexts managed by BeginPaint()/EndPaint().
398 struct PaintCanvas
: public PAINTSTRUCT
400 PaintCanvas(HWND hwnd
)
403 BeginPaint(hwnd
, this);
408 EndPaint(_hwnd
, this);
411 operator HDC() const {return hdc
;}
417 /// Canvas is a encapsulation of device contexts.
420 Canvas(HDC hdc
) : _hdc(hdc
) {}
422 operator HDC() {return _hdc
;}
428 /// WindowCanvas is a encapsulation of client area device contexts.
429 struct WindowCanvas
: public Canvas
431 WindowCanvas(HWND hwnd
)
432 : Canvas(GetDC(hwnd
)), _hwnd(hwnd
) {}
434 ~WindowCanvas() {ReleaseDC(_hwnd
, _hdc
);}
441 // double buffering classes
443 /// Memory Canvas creates and destroys memory devoce contexts.
444 struct MemCanvas
: public Canvas
447 : Canvas(CreateCompatibleDC(hdc
)) {assert(_hdc
);}
449 ~MemCanvas() {DeleteDC(_hdc
);}
452 /// SelectedBitmap is used to localy select bitmaps into device contexts.
453 struct SelectedBitmap
455 SelectedBitmap(HDC hdc
, HBITMAP hbmp
)
456 : _hdc(hdc
), _old_hbmp(SelectBitmap(hdc
, hbmp
)) {}
458 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc
, _old_hbmp
));}
465 /// BufferCanvas manages offscreen bitmaps selected into memory device contexts.
466 struct BufferCanvas
: public MemCanvas
468 BufferCanvas(HDC hdc
, int x
, int y
, int w
, int h
)
469 : MemCanvas(hdc
), _hdctarg(hdc
),
470 _x(x
), _y(y
), _w(w
), _h(h
),
471 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, w
, h
)) {}
473 BufferCanvas(HDC hdc
, const RECT
& rect
)
474 : MemCanvas(hdc
), _hdctarg(hdc
),
475 _x(rect
.left
), _y(rect
.top
), _w(rect
.right
-rect
.left
), _h(rect
.bottom
-rect
.top
),
476 _bmp(_hdc
, CreateCompatibleBitmap(hdc
, _w
, _h
)) {}
484 /// BufferedCanvas enables double buffering for a device context.
485 struct BufferedCanvas
: public BufferCanvas
487 BufferedCanvas(HDC hdc
, int x
, int y
, int w
, int h
, DWORD mode
=SRCCOPY
)
488 : BufferCanvas(hdc
, x
, y
, w
, h
), _mode(mode
) {}
490 BufferedCanvas(HDC hdc
, const RECT
& rect
, DWORD mode
=SRCCOPY
)
491 : BufferCanvas(hdc
, rect
), _mode(mode
) {}
493 ~BufferedCanvas() {BitBlt(_hdctarg
, _x
, _y
, _w
, _h
, _hdc
, 0, 0, _mode
);}
498 /// BufferedPaintCanvas extends PaintCanvas for double buffering.
499 struct BufferedPaintCanvas
: public PaintCanvas
, public BufferedCanvas
501 BufferedPaintCanvas(HWND hwnd
)
503 BufferedCanvas(PAINTSTRUCT::hdc
, 0, 0, rcPaint
.right
, rcPaint
.bottom
)
507 operator HDC() {return BufferedCanvas::_hdc
;}
511 /// TextColor locally selects a text color for drawing.
514 TextColor(HDC hdc
, COLORREF color
)
515 : _hdc(hdc
), _old_color(SetTextColor(hdc
, color
)) {}
517 ~TextColor() {SetTextColor(_hdc
, _old_color
);}
524 /// BkMode locally sets the background mode for drawing.
527 BkMode(HDC hdc
, int bkmode
)
528 : _hdc(hdc
), _old_bkmode(SetBkMode(hdc
, bkmode
)) {}
530 ~BkMode() {SetBkMode(_hdc
, _old_bkmode
);}
534 COLORREF _old_bkmode
;
537 /// FontSelection locally selects a font for drawing.
540 FontSelection(HDC hdc
, HFONT hFont
)
541 : _hdc(hdc
), _old_hFont(SelectFont(hdc
, hFont
)) {}
543 ~FontSelection() {SelectFont(_hdc
, _old_hFont
);}
550 /// BitmapSelection locally selects a bitmap into a device context.
551 struct BitmapSelection
553 BitmapSelection(HDC hdc
, HBITMAP hBmp
)
554 : _hdc(hdc
), _old_hBmp(SelectBitmap(hdc
, hBmp
)) {}
556 ~BitmapSelection() {SelectBitmap(_hdc
, _old_hBmp
);}
563 /// BrushSelection locally selects a brush into a device context.
564 struct BrushSelection
566 BrushSelection(HDC hdc
, HBRUSH hBrush
)
567 : _hdc(hdc
), _old_hBrush(SelectBrush(hdc
, hBrush
)) {}
569 ~BrushSelection() {SelectBrush(_hdc
, _old_hBrush
);}
581 : _hmenu(CreatePopupMenu())
587 operator HMENU() {return _hmenu
;}
589 void Append(UINT id
, LPCTSTR str
, UINT flags
=MF_STRING
)
591 AppendMenu(_hmenu
, flags
, id
, str
);
594 int TrackPopupMenu(HWND hwnd
, const POINT
& pt
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
, LPTPMPARAMS tpm
=NULL
) {
595 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, tpm
);
598 int PopupContextMenu(HWND hwnd
, POINTS pos
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
) {
599 POINT pt
; POINTSTOPOINT(pt
, pos
);
600 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
603 int TrackPopupMenu(HWND hwnd
, POINTS pos
, UINT flags
=TPM_LEFTBUTTON
|TPM_RIGHTBUTTON
) {
604 POINT pt
; POINTSTOPOINT(pt
, pos
);
605 ClientToScreen(hwnd
, &pt
);
606 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
609 int TrackPopupMenuAtCursor(HWND hwnd
, UINT flags
=TPM_LEFTBUTTON
) {
610 POINT pt
; GetCursorPos(&pt
);
611 return TrackPopupMenuEx(_hmenu
, flags
, pt
.x
, pt
.y
, hwnd
, NULL
);
614 int TrackPopupMenuAtPos(HWND hwnd
, DWORD pos
, UINT flags
=TPM_LEFTBUTTON
) {
615 return TrackPopupMenuEx(_hmenu
, flags
, GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
), hwnd
, NULL
);
623 /// string class for convenience
632 typedef wstring super
;
634 typedef string super
;
638 String(LPCTSTR s
) {if (s
) super::assign(s
);}
639 String(LPCTSTR s
, int l
) : super(s
, l
) {}
640 String(const super
& other
) : super(other
) {}
641 String(const String
& other
) : super(other
) {}
644 String(LPCSTR s
) {assign(s
);}
645 String(LPCSTR s
, int l
) {assign(s
, l
);}
646 String(const string
& other
) {assign(other
.c_str());}
647 String
& operator=(LPCSTR s
) {assign(s
); return *this;}
648 void assign(LPCSTR s
) {if (s
) {TCHAR b
[BUFFER_LEN
]; super::assign(b
, MultiByteToWideChar(CP_ACP
, 0, s
, -1, b
, BUFFER_LEN
));} else erase();}
649 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();}
651 String(LPCWSTR s
) {assign(s
);}
652 String(LPCWSTR s
, int l
) {assign(s
, l
);}
653 String(const wstring
& other
) {assign(other
.c_str());}
654 String
& operator=(LPCWSTR s
) {assign(s
); return *this;}
655 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();}
656 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();}
659 String
& operator=(LPCTSTR s
) {if (s
) super::assign(s
); else erase(); return *this;}
660 String
& operator=(const super
& s
) {super::assign(s
); return *this;}
661 void assign(LPCTSTR s
) {super::assign(s
);}
662 void assign(LPCTSTR s
, int l
) {super::assign(s
, l
);}
664 operator LPCTSTR() const {return c_str();}
666 String
& printf(LPCTSTR fmt
, ...)
672 super::assign(b
, _vstprintf(b
, fmt
, l
));
678 String
& vprintf(LPCTSTR fmt
, va_list l
)
682 super::assign(b
, _vstprintf(b
, fmt
, l
));
687 String
& appendf(LPCTSTR fmt
, ...)
693 super::append(b
, _vstprintf(b
, fmt
, l
));
699 String
& vappendf(LPCTSTR fmt
, va_list l
)
703 super::append(b
, _vstprintf(b
, fmt
, l
));
710 struct FmtString
: public String
712 FmtString(LPCTSTR fmt
, ...)
729 int l
= wcslen(s
) + 1;
730 _str
= (LPSTR
) malloc(2*l
);
732 if (WideCharToMultiByte(CP_ACP
, 0, s
, -1, _str
, 2*l
, 0, 0) <= 0)
741 operator LPCSTR() {return _str
;}
747 #define UNC(x) ((LPCWSTR)(x))
751 #define ANS(x) ((LPCSTR)(x))
757 int l
= strlen(s
) + 1;
758 _str
= (LPWSTR
) malloc(2*l
);
760 if (MultiByteToWideChar(CP_ACP
, 0, s
, -1, _str
, l
) <= 0)
769 operator LPCWSTR() {return _str
;}
778 /// link dynamicly to functions by using GetModuleHandle() and GetProcAddress()
779 template<typename FCT
> struct DynamicFct
781 DynamicFct(LPCTSTR moduleName
, UINT ordinal
)
783 HMODULE hModule
= GetModuleHandle(moduleName
);
785 _fct
= (FCT
) GetProcAddress(hModule
, (LPCSTR
)ordinal
);
788 DynamicFct(LPCTSTR moduleName
, LPCSTR name
)
790 HMODULE hModule
= GetModuleHandle(moduleName
);
792 _fct
= (FCT
) GetProcAddress(hModule
, name
);
795 FCT
operator*() const {return _fct
;}
796 operator bool() const {return _fct
? true: false;}
803 /// link dynamicly to functions by using LoadLibrary() and GetProcAddress()
804 template<typename FCT
> struct DynamicLoadLibFct
806 DynamicLoadLibFct(LPCTSTR moduleName
, UINT ordinal
)
808 _hModule
= LoadLibrary(moduleName
);
810 _fct
= (FCT
) GetProcAddress(_hModule
, (LPCSTR
)ordinal
);
813 DynamicLoadLibFct(LPCTSTR moduleName
, LPCSTR name
)
815 _hModule
= LoadLibrary(moduleName
);
817 _fct
= (FCT
) GetProcAddress(_hModule
, name
);
822 FreeLibrary(_hModule
);
825 FCT
operator*() const {return _fct
;}
826 operator bool() const {return _fct
? true: false;}
838 Context(const char* ctx
)
845 Context(const char* ctx
, LPCSTR obj
)
853 Context(const char* ctx
, LPCWSTR obj
)
861 Context(const Context
& other
)
876 String
toString() const;
877 String
getStackTrace() const;
882 static Context
& current() {return *s_current
;}
887 static Context
* s_current
; ///@todo use TLS
888 static Context s_main
;
891 #define CONTEXT_OBJ __ctx__._obj
892 #define CONTEXT(c) Context __ctx__(c)
893 #define CURRENT_CONTEXT Context::current()
894 #define OBJ_CONTEXT(c, o) Context __ctx__(c, o);
899 #define OBJ_CONTEXT(c, o) ;
904 #endif // __cplusplus