utility.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2003, 2004, 2005 Martin Fuchs
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 
00020  //
00021  // Explorer clone
00022  //
00023  // utility.h
00024  //
00025  // Martin Fuchs, 23.07.2003
00026  //
00027 
00028 #ifdef __cplusplus
00029 
00030 #ifdef _MSC_VER
00031 #pragma warning(disable: 4786)  // disable warnings about too long debug information symbols
00032 #endif
00033 
00034  // STL headers for strings and streams
00035 #include <string>
00036 #include <iostream>
00037 using namespace std;
00038 
00039 #endif /* __cplusplus */
00040 
00041 
00042  // standard windows headers
00043 #define WIN32_LEAN_AND_MEAN
00044 #define WIN32_EXTRA_LEAN
00045 #include <windows.h>
00046 
00047  // Unicode support
00048 #if defined(UNICODE) && !defined(_UNICODE)
00049 #define _UNICODE
00050 #endif
00051 #include <tchar.h>
00052 
00053 #include <windowsx.h>   // for SelectBrush(), ListBox_SetSel(), SubclassWindow(), ...
00054 #include <commctrl.h>
00055 
00056 #ifndef _MSC_VER
00057 #include <objbase.h>
00058 #endif
00059 #include <oleauto.h>    // for VARIANT
00060 
00061 #include <malloc.h>     // for alloca()
00062 #include <assert.h>
00063 #include <stdlib.h>     // for _MAX_DIR, ...
00064 #include <stdio.h>      // for sprintf()
00065 #include <time.h>
00066 
00067 #ifndef _MAX_PATH
00068 #define _MAX_DRIVE  3
00069 #define _MAX_FNAME  256
00070 #define _MAX_DIR    _MAX_FNAME
00071 #define _MAX_EXT    _MAX_FNAME
00072 #define _MAX_PATH   260
00073 #endif
00074 
00075 #define W_VER_NT 0  // constant for HIWORD(GetVersion())>>14
00076 
00077 
00078 #ifdef __cplusplus
00079 extern "C" {
00080 #endif
00081 
00082 
00083 #define for if (0) {} else for
00084 
00085 #ifdef _countof
00086 #define COUNTOF _countof
00087 #else
00088 #define COUNTOF(x)  (sizeof(x)/sizeof(x[0]))
00089 #endif
00090 
00091 
00092 #define BUFFER_LEN              2048
00093 
00094 
00095 extern void _log_(LPCTSTR txt);
00096 
00097 #define LOG(txt) _log_(txt)
00098 
00099 
00100 #ifdef _MSC_VER
00101 #define LONGLONGARG TEXT("I64")
00102 #else
00103 #define LONGLONGARG TEXT("L")
00104 #endif
00105 
00106 
00107 #ifndef _tcsrchr
00108 #ifdef UNICODE
00109 #define _tcsrchr wcsrchr
00110 #else
00111 #define _tcsrchr strrchr
00112 #endif
00113 #endif
00114 
00115 #ifndef _stprintf
00116 #ifdef UNICODE
00117 #define _stprintf wcsprintf
00118 #else
00119 #define _stprintf sprintf
00120 #endif
00121 #endif
00122 
00123 #define U2A(s, d, l) WideCharToMultiByte(CP_ACP, 0, s, -1, d, l, NULL, NULL)
00124 #define U2nA(s, d, l) WideCharToMultiByte(CP_ACP, 0, s, l, d, l, NULL, NULL)
00125 #define A2U(s, d, l) MultiByteToWideChar(CP_ACP, 0, s, -1, d, l)
00126 #define A2nU(s, d, l) MultiByteToWideChar(CP_ACP, 0, s, l, d, l)
00127 
00128 
00129 #ifdef __WINE__
00130 #ifdef UNICODE
00131 extern void _wsplitpath(const WCHAR* path, WCHAR* drv, WCHAR* dir, WCHAR* name, WCHAR* ext);
00132 #else
00133 extern void _splitpath(const CHAR* path, CHAR* drv, CHAR* dir, CHAR* name, CHAR* ext);
00134 #endif
00135 #define _tcsnicmp strncasecmp
00136 #define _tcsicoll strcasecmp
00137 #endif
00138 
00139 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
00140 #define FILE_ATTRIBUTE_ENCRYPTED            0x00000040
00141 #define FILE_ATTRIBUTE_SPARSE_FILE          0x00000200
00142 #define FILE_ATTRIBUTE_REPARSE_POINT        0x00000400
00143 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED  0x00002000
00144 #endif
00145 
00146 
00147 #define SetDlgCtrlID(hwnd, id) SetWindowLong(hwnd, GWL_ID, id)
00148 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLong(hwnd, GWL_STYLE, val)
00149 #define SetWindowExStyle(h, val) (DWORD)SetWindowLong(hwnd, GWL_EXSTYLE, val)
00150 #define Window_SetIcon(hwnd, type, hicon) (HICON)SendMessage(hwnd, WM_SETICON, type, (LPARAM)(hicon))
00151 
00152 
00153  // center window in respect to its parent window
00154 extern void CenterWindow(HWND hwnd);
00155 
00156  // move window into visibility
00157 extern void MoveVisible(HWND hwnd);
00158 
00159  // display error message
00160 extern void display_error(HWND hwnd, DWORD error);
00161 
00162  // convert time_t to WIN32 FILETIME
00163 extern BOOL time_to_filetime(const time_t* t, FILETIME* ftime);
00164 
00165  // search for windows of a specific classname
00166 extern int find_window_class(LPCTSTR classname);
00167 
00168  // create a directory with all missing parent directories
00169 BOOL RecursiveCreateDirectory(LPCTSTR path_in);
00170 
00171  // read DWORD value from registry
00172 DWORD RegGetDWORDValue(HKEY root, LPCTSTR path, LPCTSTR valueName, DWORD def);
00173 
00174  // write DWORD value to registry
00175 BOOL RegSetDWORDValue(HKEY root, LPCTSTR path, LPCTSTR valueName, DWORD value);
00176 
00177  // test for existing directory
00178 BOOL exists_path(LPCTSTR path);
00179 
00180 
00181 #ifdef __cplusplus
00182 } // extern "C"
00183 #endif
00184 
00185 
00186  // secure CRT functions
00187 #ifdef __STDC_WANT_SECURE_LIB__ // for VS 2005: _MSC_VER>=1400
00188 
00189 #define _stprintf_s1 _stprintf_s
00190 #define _stprintf_s2 _stprintf_s
00191 
00192 #else   // __STDC_WANT_SECURE_LIB__
00193 
00194 #define strcpy_s(d, l, s) strcpy(d, s)
00195 #define _tcscpy_s(d, l, s) _tcscpy(d, s)
00196 #define wcsncpy_s(d, l, s, n) wcsncpy(d, s, n)
00197 #define _stprintf_s1(b, l, f, p1) _stprintf(b, f, p1)
00198 #define _stprintf_s2(b, l, f, p1,p2) _stprintf(b, f, p1,p2)
00199 #define _tsplitpath_s(f, d,dl, p,pl, n,nl, e,el) _tsplitpath(f, d, p, n, e)
00200 
00201 #endif  // __STDC_WANT_SECURE_LIB__
00202 
00203 
00204 #ifdef __cplusplus
00205 
00206  // containers
00207 #include <map>
00208 #include <set>
00209 #include <list>
00210 #include <stack>
00211 #include <vector>
00212 
00213 
00214 /* not necessary with correct include file order for comdef.h ("<MS PSDK>\include" path first) */
00215 #if _MSC_VER>=1300  // VS.Net
00216 #define _NO_COMUTIL
00217 #endif
00218 
00219 
00220 #if defined(_MSC_VER) && !defined(_NO_COMUTIL)
00221 
00222  // COM utility headers
00223 #include <comdef.h>
00224 using namespace _com_util;
00225 
00226 #endif  // _MSC_VER && !_NO_COMUTIL
00227 
00228 
00229  // launch a program or document file
00230 extern BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow=SW_SHOWNORMAL, LPCTSTR parameters=NULL);
00231 #ifdef UNICODE
00232 extern BOOL launch_fileA(HWND hwnd, LPSTR cmd, UINT nCmdShow=SW_SHOWNORMAL, LPCSTR parameters=NULL);
00233 #else
00234 #define launch_fileA launch_file
00235 #endif
00236 
00237  // call an DLL export like rundll32
00238 extern BOOL RunDLL(HWND hwnd, LPCTSTR dllname, LPCSTR procname, LPCTSTR cmdline, UINT nCmdShow);
00239 
00240  // launch control panel applet
00241 extern BOOL launch_cpanel(HWND hwnd, LPCTSTR applet);
00242 
00243 
00244  /// initialization of windows common controls
00245 struct CommonControlInit
00246 {
00247     CommonControlInit(DWORD flags=ICC_LISTVIEW_CLASSES|ICC_TREEVIEW_CLASSES|ICC_BAR_CLASSES|ICC_PROGRESS_CLASS|ICC_COOL_CLASSES)
00248     {
00249         INITCOMMONCONTROLSEX icc = {sizeof(INITCOMMONCONTROLSEX), flags};
00250 
00251         InitCommonControlsEx(&icc);
00252     }
00253 };
00254 
00255 
00256  /// wait cursor
00257 
00258 struct WaitCursor   ///@todo integrate with WM_SETCURSOR to enable multithreaded background tasks as program launching
00259 {
00260     WaitCursor()
00261     {
00262         _old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
00263     }
00264 
00265     ~WaitCursor()
00266     {
00267         SetCursor(_old_cursor);
00268     }
00269 
00270 protected:
00271     HCURSOR _old_cursor;
00272 };
00273 
00274 
00275  /// base of all structures storing a window handle
00276 struct WindowHandle
00277 {
00278     WindowHandle(HWND hwnd=0)
00279      :  _hwnd(hwnd) {}
00280 
00281     operator HWND() const {return _hwnd;}
00282     HWND* operator&() {return &_hwnd;}
00283 
00284 protected:
00285     HWND    _hwnd;
00286 };
00287 
00288 
00289  /// locally hide a window
00290 struct HiddenWindow : public WindowHandle
00291 {
00292     HiddenWindow(HWND hwnd)
00293      :  WindowHandle(IsWindowVisible(hwnd)? hwnd: 0)
00294     {
00295         if (_hwnd)
00296             SetWindowPos(_hwnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW|SWP_NOREDRAW|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER);
00297     }
00298 
00299     ~HiddenWindow()
00300     {
00301         if (_hwnd)
00302             SetWindowPos(_hwnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER);
00303     }
00304 };
00305 
00306 
00307  /// critical section wrapper
00308 
00309 struct CritSect : public CRITICAL_SECTION
00310 {
00311     CritSect()
00312     {
00313         InitializeCriticalSection(this);
00314     }
00315 
00316     ~CritSect()
00317     {
00318         DeleteCriticalSection(this);
00319     }
00320 };
00321 
00322 
00323  /// Lock protects a code section utilizing a critical section
00324 
00325 struct Lock
00326 {
00327     Lock(CritSect& crit_sect)
00328      :  _crit_sect(crit_sect)
00329     {
00330         EnterCriticalSection(&crit_sect);
00331     }
00332 
00333     ~Lock()
00334     {
00335         LeaveCriticalSection(&_crit_sect);
00336     }
00337 
00338 protected:
00339     CritSect&   _crit_sect;
00340 };
00341 
00342 
00343  /// Thread base class
00344 
00345 struct Thread
00346 {
00347     Thread()
00348      :  _alive(false),
00349         _destroy(false)
00350     {
00351         _hThread = INVALID_HANDLE_VALUE;
00352         _evtFinish = CreateEvent(NULL, TRUE, FALSE, NULL);
00353     }
00354 
00355     virtual ~Thread()
00356     {
00357         Stop();
00358 
00359         CloseHandle(_evtFinish);
00360         CloseHandle(_hThread);
00361 
00362         if (_destroy)
00363             delete this;
00364     }
00365 
00366     void Start()
00367     {
00368         if (!_alive) {
00369             _alive = true;
00370             _hThread = CreateThread(NULL, 0, ThreadProc, this, 0, NULL);
00371         }
00372     }
00373 
00374     void Stop()
00375     {
00376         SetEvent(_evtFinish);
00377 
00378         if (_alive) {
00379             {
00380             Lock lock(_crit_sect);
00381             _alive = false;
00382             }
00383 
00384              // wait for finishing
00385             WaitForSingleObject(_hThread, INFINITE);
00386         }
00387     }
00388 
00389     virtual int Run() = 0;
00390 
00391     bool    is_alive() const {return _alive;}
00392 
00393     CritSect _crit_sect;
00394 
00395 protected:
00396     static DWORD WINAPI ThreadProc(void* para);
00397 
00398     HANDLE  _hThread;
00399     HANDLE  _evtFinish;
00400     bool    _alive;
00401     bool    _destroy;
00402 };
00403 
00404 
00405  // window utilities
00406 
00407  /// ClientRect retreives the client area rectangle of a window.
00408 struct ClientRect : public RECT
00409 {
00410     ClientRect(HWND hwnd)
00411     {
00412         GetClientRect(hwnd, this);
00413     }
00414 
00415     operator LPRECT() {return this;}
00416 
00417     POINT& pos() {return *(LPPOINT)this;}
00418 };
00419 
00420  /// ClientRect retreives the window rectangle of a window.
00421 struct WindowRect : public RECT
00422 {
00423     WindowRect(HWND hwnd)
00424     {
00425         GetWindowRect(hwnd, this);
00426     }
00427 
00428     operator LPRECT() {return this;}
00429 
00430     POINT& pos() {return *(LPPOINT)this;}
00431 };
00432 
00433  /// PointL encapsulates the POINT structure into a C++ object.
00434 struct Point : public POINT
00435 {
00436     Point(LONG x_, LONG y_)
00437     {
00438         x = x_;
00439         y = y_;
00440     }
00441 
00442      // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages
00443     Point(LPARAM lparam)
00444     {
00445         x = GET_X_LPARAM(lparam);
00446         y = GET_Y_LPARAM(lparam);
00447     }
00448 
00449     operator LPPOINT() {return this;}
00450 };
00451 
00452 
00453  /// transform coordinates in a RECT from client to screen coordiantes
00454 inline void ClientToScreen(HWND hwnd, RECT* prect)
00455  {::ClientToScreen(hwnd,(LPPOINT)&prect->left); ::ClientToScreen(hwnd,(LPPOINT)&prect->right);}
00456 
00457  /// transform coordinates in a RECT from screen to client coordiantes
00458 inline void ScreenToClient(HWND hwnd, RECT* prect)
00459  {::ScreenToClient(hwnd,(LPPOINT)&prect->left); ::ScreenToClient(hwnd,(LPPOINT)&prect->right);}
00460 
00461 
00462  /// structure containing information about full screen display of the frame window
00463 struct FullScreenParameters
00464 {
00465     FullScreenParameters()
00466      :  _mode(FALSE)
00467     {
00468     }
00469 
00470     BOOL    _mode;
00471     RECT    _orgPos;
00472     BOOL    _wasZoomed;
00473 };
00474 
00475 
00476  // drawing utilities
00477 
00478  /// PaintCanvas is a encapsulation of device contexts managed by BeginPaint()/EndPaint().
00479 struct PaintCanvas : public PAINTSTRUCT
00480 {
00481     PaintCanvas(HWND hwnd)
00482      :  _hwnd(hwnd)
00483     {
00484         BeginPaint(hwnd, this);
00485     }
00486 
00487     ~PaintCanvas()
00488     {
00489         EndPaint(_hwnd, this);
00490     }
00491 
00492     operator HDC() const {return hdc;}
00493 
00494 protected:
00495     HWND    _hwnd;
00496 };
00497 
00498  /// Canvas is a encapsulation of device contexts.
00499 struct Canvas
00500 {
00501     Canvas(HDC hdc) : _hdc(hdc) {}
00502 
00503     operator HDC() {return _hdc;}
00504 
00505 protected:
00506     HDC _hdc;
00507 };
00508 
00509  /// WindowCanvas is a encapsulation of client area device contexts.
00510 struct WindowCanvas : public Canvas
00511 {
00512     WindowCanvas(HWND hwnd)
00513      :  Canvas(GetDC(hwnd)), _hwnd(hwnd) {}
00514 
00515     ~WindowCanvas() {ReleaseDC(_hwnd, _hdc);}
00516 
00517 protected:
00518     HWND    _hwnd;
00519 };
00520 
00521 
00522  // double buffering classes
00523 
00524  /// Memory Canvas creates and destroys memory devoce contexts.
00525 struct MemCanvas : public Canvas
00526 {
00527     MemCanvas(HDC hdc=0)
00528      :  Canvas(CreateCompatibleDC(hdc)) {assert(_hdc);}
00529 
00530     ~MemCanvas() {DeleteDC(_hdc);}
00531 };
00532 
00533  /// SelectedBitmap is used to localy select bitmaps into device contexts.
00534 struct SelectedBitmap
00535 {
00536     SelectedBitmap(HDC hdc, HBITMAP hbmp)
00537      :  _hdc(hdc), _old_hbmp(SelectBitmap(hdc, hbmp)) {}
00538 
00539     ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc, _old_hbmp));}
00540 
00541 protected:
00542     HDC     _hdc;
00543     HBITMAP _old_hbmp;
00544 };
00545 
00546  /// BufferCanvas manages offscreen bitmaps selected into memory device contexts.
00547 struct BufferCanvas : public MemCanvas
00548 {
00549     BufferCanvas(HDC hdc, int x, int y, int w, int h)
00550      :  MemCanvas(hdc), _hdctarg(hdc),
00551         _x(x), _y(y), _w(w), _h(h),
00552         _bmp(_hdc, CreateCompatibleBitmap(hdc, w, h)) {}
00553 
00554     BufferCanvas(HDC hdc, const RECT& rect)
00555      :  MemCanvas(hdc), _hdctarg(hdc),
00556         _x(rect.left), _y(rect.top), _w(rect.right-rect.left), _h(rect.bottom-rect.top),
00557         _bmp(_hdc, CreateCompatibleBitmap(hdc, _w, _h)) {}
00558 
00559 protected:
00560     HDC     _hdctarg;
00561     int     _x, _y, _w, _h;
00562     SelectedBitmap _bmp;
00563 };
00564 
00565  /// BufferedCanvas enables double buffering for a device context.
00566 struct BufferedCanvas : public BufferCanvas
00567 {
00568     BufferedCanvas(HDC hdc, int x, int y, int w, int h, DWORD mode=SRCCOPY)
00569      :  BufferCanvas(hdc, x, y, w, h), _mode(mode) {}
00570 
00571     BufferedCanvas(HDC hdc, const RECT& rect, DWORD mode=SRCCOPY)
00572      :  BufferCanvas(hdc, rect), _mode(mode) {}
00573 
00574     ~BufferedCanvas() {BitBlt(_hdctarg, _x, _y, _w, _h, _hdc, 0, 0, _mode);}
00575 
00576     DWORD   _mode;
00577 };
00578 
00579  /// BufferedPaintCanvas extends PaintCanvas for double buffering.
00580 struct BufferedPaintCanvas : public PaintCanvas, public BufferedCanvas
00581 {
00582     BufferedPaintCanvas(HWND hwnd)
00583      :  PaintCanvas(hwnd),
00584         BufferedCanvas(PAINTSTRUCT::hdc, 0, 0, rcPaint.right, rcPaint.bottom)
00585     {
00586     }
00587 
00588     operator HDC() {return BufferedCanvas::_hdc;}
00589 };
00590 
00591 
00592  /// TextColor locally selects a text color for drawing.
00593 struct TextColor
00594 {
00595     TextColor(HDC hdc, COLORREF color)
00596      : _hdc(hdc), _old_color(SetTextColor(hdc, color)) {}
00597 
00598     ~TextColor() {SetTextColor(_hdc, _old_color);}
00599 
00600 protected:
00601     HDC      _hdc;
00602     COLORREF _old_color;
00603 };
00604 
00605  /// BkMode locally sets the background mode for drawing.
00606 struct BkMode
00607 {
00608     BkMode(HDC hdc, int bkmode)
00609      : _hdc(hdc), _old_bkmode(SetBkMode(hdc, bkmode)) {}
00610 
00611     ~BkMode() {SetBkMode(_hdc, _old_bkmode);}
00612 
00613 protected:
00614     HDC      _hdc;
00615     COLORREF _old_bkmode;
00616 };
00617 
00618  /// FontSelection locally selects a font for drawing.
00619 struct FontSelection
00620 {
00621     FontSelection(HDC hdc, HFONT hFont)
00622      : _hdc(hdc), _old_hFont(SelectFont(hdc, hFont)) {}
00623 
00624     ~FontSelection() {SelectFont(_hdc, _old_hFont);}
00625 
00626 protected:
00627     HDC     _hdc;
00628     HFONT   _old_hFont;
00629 };
00630 
00631  /// BitmapSelection locally selects a bitmap into a device context.
00632 struct BitmapSelection
00633 {
00634     BitmapSelection(HDC hdc, HBITMAP hBmp)
00635      : _hdc(hdc), _old_hBmp(SelectBitmap(hdc, hBmp)) {}
00636 
00637     ~BitmapSelection() {SelectBitmap(_hdc, _old_hBmp);}
00638 
00639 protected:
00640     HDC     _hdc;
00641     HBITMAP _old_hBmp;
00642 };
00643 
00644  /// BrushSelection locally selects a brush into a device context.
00645 struct BrushSelection
00646 {
00647     BrushSelection(HDC hdc, HBRUSH hBrush)
00648      : _hdc(hdc), _old_hBrush(SelectBrush(hdc, hBrush)) {}
00649 
00650     ~BrushSelection() {SelectBrush(_hdc, _old_hBrush);}
00651 
00652 protected:
00653     HDC     _hdc;
00654     HBRUSH  _old_hBrush;
00655 };
00656 
00657 
00658  /// Popup Menus
00659 struct PopupMenu
00660 {
00661     PopupMenu()
00662      :  _hmenu(CreatePopupMenu())
00663     {
00664     }
00665 
00666     ~PopupMenu()
00667     {
00668         DestroyMenu(_hmenu);
00669     }
00670 
00671     PopupMenu(UINT nid);
00672 
00673     operator HMENU() {return _hmenu;}
00674 
00675     void Append(UINT id, LPCTSTR str, UINT flags=MF_STRING)
00676     {
00677         AppendMenu(_hmenu, flags, id, str);
00678     }
00679 
00680     int TrackPopupMenu(HWND hwnd, const POINT& pt, UINT flags=TPM_LEFTBUTTON|TPM_RIGHTBUTTON, LPTPMPARAMS tpm=NULL) {
00681      return TrackPopupMenuEx(_hmenu, flags, pt.x, pt.y, hwnd, tpm);
00682     }
00683 
00684     int PopupContextMenu(HWND hwnd, POINTS pos, UINT flags=TPM_LEFTBUTTON|TPM_RIGHTBUTTON) {
00685      POINT pt;
00686      pt.x = pos.x;
00687      pt.y = pos.y;
00688      return TrackPopupMenuEx(_hmenu, flags, pt.x, pt.y, hwnd, NULL);
00689     }
00690 
00691     int TrackPopupMenu(HWND hwnd, POINTS pos, UINT flags=TPM_LEFTBUTTON|TPM_RIGHTBUTTON) {
00692      POINT pt;
00693      pt.x = pos.x;
00694      pt.y = pos.y;
00695      ClientToScreen(hwnd, &pt);
00696      return TrackPopupMenuEx(_hmenu, flags, pt.x, pt.y, hwnd, NULL);
00697     }
00698 
00699     int TrackPopupMenuAtCursor(HWND hwnd, UINT flags=TPM_LEFTBUTTON) {
00700      POINT pt; GetCursorPos(&pt);
00701      return TrackPopupMenuEx(_hmenu, flags, pt.x, pt.y, hwnd, NULL);
00702     }
00703 
00704     int TrackPopupMenuAtPos(HWND hwnd, DWORD pos, UINT flags=TPM_LEFTBUTTON) {
00705      return TrackPopupMenuEx(_hmenu, flags, GET_X_LPARAM(pos), GET_Y_LPARAM(pos), hwnd, NULL);
00706     }
00707 
00708 protected:
00709     HMENU _hmenu;
00710 };
00711 
00712 
00713 struct Variant : public VARIANT
00714 {
00715     Variant() {VariantInit(this);}
00716     Variant(const VARIANT& var);
00717     Variant(const VARIANT* var);
00718     ~Variant();
00719 
00720     operator long() const;
00721     operator bool() const;
00722     operator VARIANT_BOOL() const;
00723     operator IDispatch*() const;
00724 };
00725 
00726 
00727 struct BStr
00728 {
00729     BStr()
00730     {
00731         _p = NULL;
00732     }
00733 
00734     BStr(const BSTR s)
00735     {
00736         _p = SysAllocString(s);
00737     }
00738 
00739     BStr(LPCSTR s)
00740     {
00741         WCHAR b[BUFFER_LEN];
00742 
00743         if (s)
00744             _p = SysAllocStringLen(b, MultiByteToWideChar(CP_ACP, 0, s, -1, b, BUFFER_LEN)-1);
00745         else
00746             _p = NULL;
00747     }
00748 
00749     BStr(LPCWSTR s)
00750     {
00751         _p = SysAllocString(s);
00752     }
00753 
00754     BStr(const VARIANT& var)
00755      :  _p(NULL)
00756     {
00757         assign(var);
00758     }
00759 
00760     ~BStr()
00761     {
00762         SysFreeString(_p);
00763     }
00764 
00765     void assign(BSTR s);
00766     void assign(const VARIANT& var);
00767 
00768     operator BSTR() const
00769     {
00770         return _p? _p: (BSTR)L"";
00771     }
00772 
00773     int length() const
00774     {
00775         return _p? wcslen(_p): 0;
00776     }
00777 
00778 protected:
00779     BSTR    _p;
00780 };
00781 
00782 
00783  /// string class for TCHAR strings
00784 struct String
00785 #ifdef UNICODE
00786  : public wstring
00787 #else
00788  : public string
00789 #endif
00790 {
00791 #ifdef UNICODE
00792     typedef wstring super;
00793 #else
00794     typedef string super;
00795 #endif
00796 
00797     String() {}
00798 
00799     String(LPCTSTR s) {if (s) super::assign(s);}
00800     String(LPCTSTR s, int l) : super(s, l) {}
00801 
00802     String(const super& other) : super(other) {}
00803     String(const String& other) : super(other) {}
00804 
00805 #ifdef UNICODE
00806     String(LPCSTR s) {assign(s);}
00807     String(LPCSTR s, int l) {assign(s, l);}
00808     String(const string& other) {assign(other.c_str());}
00809     String& operator=(LPCSTR s) {assign(s); return *this;}
00810     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();}
00811     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();}
00812     void assign(const BStr& s) {int l = s.length(); super::assign(s, l);}
00813 #else
00814     String(LPCWSTR s) {assign(s);}
00815     String(LPCWSTR s, int l) {assign(s, l);}
00816     String(const wstring& other) {assign(other.c_str());}
00817     String& operator=(LPCWSTR s) {assign(s); return *this;}
00818     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();}
00819     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();}
00820     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();}
00821 #endif
00822     String(const BStr& s) {assign(s);}
00823     String& operator=(const BStr& s) {assign(s); return *this;}
00824 
00825     String& operator=(LPCTSTR s) {if (s) super::assign(s); else erase(); return *this;}
00826     String& operator=(const super& s) {super::assign(s); return *this;}
00827     void assign(LPCTSTR s) {super::assign(s);}
00828     void assign(LPCTSTR s, int l) {super::assign(s, l);}
00829 
00830     operator LPCTSTR() const {return c_str();}
00831 
00832 #ifdef UNICODE
00833     operator string() const {char b[BUFFER_LEN]; return string(b, WideCharToMultiByte(CP_ACP, 0, c_str(), -1, b, BUFFER_LEN, 0, 0)-1);}
00834 #else
00835     operator wstring() const {WCHAR b[BUFFER_LEN]; return wstring(b, MultiByteToWideChar(CP_ACP, 0, c_str(), -1, b, BUFFER_LEN)-1);}
00836 #endif
00837 
00838     LPTSTR str() {return (LPTSTR)data();}   /// return modifyable character string pointer
00839 
00840     String& printf(LPCTSTR fmt, ...)
00841     {
00842         va_list l;
00843         TCHAR b[BUFFER_LEN];
00844 
00845         va_start(l, fmt);
00846         super::assign(b, _vsntprintf(b, COUNTOF(b), fmt, l));
00847         va_end(l);
00848 
00849         return *this;
00850     }
00851 
00852     String& vprintf(LPCTSTR fmt, va_list l)
00853     {
00854         TCHAR b[BUFFER_LEN];
00855 
00856         super::assign(b, _vsntprintf(b, COUNTOF(b), fmt, l));
00857 
00858         return *this;
00859     }
00860 
00861     String& appendf(LPCTSTR fmt, ...)
00862     {
00863         va_list l;
00864         TCHAR b[BUFFER_LEN];
00865 
00866         va_start(l, fmt);
00867         super::append(b, _vsntprintf(b, COUNTOF(b), fmt, l));
00868         va_end(l);
00869 
00870         return *this;
00871     }
00872 
00873     String& vappendf(LPCTSTR fmt, va_list l)
00874     {
00875         TCHAR b[BUFFER_LEN];
00876 
00877         super::append(b, _vsntprintf(b, COUNTOF(b), fmt, l));
00878 
00879         return *this;
00880     }
00881 
00882     void toLower()
00883     {
00884         if (!empty())
00885             CharLower(str());
00886     }
00887 };
00888 
00889 #define _STRING_DEFINED
00890 
00891 
00892 struct FmtString : public String
00893 {
00894     FmtString(LPCTSTR fmt, ...)
00895     {
00896         va_list l;
00897 
00898         va_start(l, fmt);
00899         vprintf(fmt, l);
00900         va_end(l);
00901     }
00902 };
00903 
00904 
00905 #ifdef UNICODE
00906 
00907 struct ANS
00908 {
00909     ANS(LPCWSTR s)
00910     {
00911         int l = wcslen(s) + 1;
00912         _str = (LPSTR) malloc(2*l);
00913 
00914         if (WideCharToMultiByte(CP_ACP, 0, s, -1, _str, 2*l, 0, 0) <= 0)
00915             *_str = '\0';
00916     }
00917 
00918     ~ANS()
00919     {
00920         free(_str);
00921     }
00922 
00923     operator LPCSTR() {return _str;}
00924 
00925 protected:
00926     LPSTR   _str;
00927 };
00928 
00929 #define UNC(x) ((LPCWSTR)(x))
00930 
00931 #else
00932 
00933 #define ANS(x) ((LPCSTR)(x))
00934 
00935 struct UNC
00936 {
00937     UNC(LPCSTR s)
00938     {
00939         int l = strlen(s) + 1;
00940         _str = (LPWSTR) malloc(2*l);
00941 
00942         if (_str && MultiByteToWideChar(CP_ACP, 0, s, -1, _str, l) <= 0)
00943             *_str = '\0';
00944     }
00945 
00946     ~UNC()
00947     {
00948         free(_str);
00949     }
00950 
00951     operator LPCWSTR() {return _str;}
00952 
00953 protected:
00954     LPWSTR  _str;
00955 };
00956 
00957 #endif
00958 
00959 
00960  // determine windows version string
00961 String get_windows_version_str();
00962 
00963 
00964  /// link dynamicly to functions by using GetModuleHandle() and GetProcAddress()
00965 template<typename FCT> struct DynamicFct
00966 {
00967     DynamicFct(LPCTSTR moduleName, UINT ordinal)
00968     {
00969         HMODULE hModule = GetModuleHandle(moduleName);
00970 
00971         _fct = (FCT) GetProcAddress(hModule, (LPCSTR)ordinal);
00972     }
00973 
00974     DynamicFct(LPCTSTR moduleName, LPCSTR name)
00975     {
00976         HMODULE hModule = GetModuleHandle(moduleName);
00977 
00978         _fct = (FCT) GetProcAddress(hModule, name);
00979     }
00980 
00981     FCT operator*() const {return _fct;}
00982     operator bool() const {return _fct? true: false;}
00983 
00984 protected:
00985     FCT _fct;
00986 };
00987 
00988 
00989  /// link dynamicly to functions by using LoadLibrary() and GetProcAddress()
00990 template<typename FCT> struct DynamicLoadLibFct
00991 {
00992     DynamicLoadLibFct(LPCTSTR moduleName, UINT ordinal)
00993     {
00994         _hModule = LoadLibrary(moduleName);
00995 
00996         _fct = (FCT) GetProcAddress(_hModule, (LPCSTR)ordinal);
00997     }
00998 
00999     DynamicLoadLibFct(LPCTSTR moduleName, LPCSTR name)
01000     {
01001         _hModule = LoadLibrary(moduleName);
01002 
01003         _fct = (FCT) GetProcAddress(_hModule, name);
01004     }
01005 
01006     ~DynamicLoadLibFct()
01007     {
01008         FreeLibrary(_hModule);
01009     }
01010 
01011     FCT operator*() const {return _fct;}
01012     operator bool() const {return _fct? true: false;}
01013 
01014 protected:
01015     HMODULE _hModule;
01016     FCT _fct;
01017 };
01018 
01019 
01020 struct Context
01021 {
01022     Context(const char* ctx)
01023      :  _ctx(ctx)
01024     {
01025         _last = s_current;
01026         s_current = this;
01027     }
01028 
01029     Context(const char* ctx, LPCSTR obj)
01030      :  _ctx(ctx),
01031         _obj(obj)
01032     {
01033         _last = s_current;
01034         s_current = this;
01035     }
01036 
01037     Context(const char* ctx, LPCWSTR obj)
01038      :  _ctx(ctx),
01039         _obj(obj)
01040     {
01041         _last = s_current;
01042         s_current = this;
01043     }
01044 
01045     Context(const Context& other)
01046      :  _ctx(other._ctx),
01047         _obj(other._obj)
01048     {
01049         _last = NULL;
01050     }
01051 
01052     ~Context()
01053     {
01054         if (_last) {
01055             s_current = _last;
01056             _last = NULL;
01057         }
01058     }
01059 
01060     String toString() const;
01061     String getStackTrace() const;
01062 
01063     const char* _ctx;
01064     String  _obj;
01065 
01066     static Context& current() {return *s_current;}
01067 
01068 protected:
01069     Context* _last;
01070 
01071     static Context* s_current;  ///@todo use TLS
01072     static Context  s_main;
01073 };
01074 
01075 #define CONTEXT_OBJ __ctx__._obj
01076 #define CONTEXT(c) Context __ctx__(c)
01077 #define CURRENT_CONTEXT Context::current()
01078 #define OBJ_CONTEXT(c, o) Context __ctx__(c, o)
01079 
01080 
01081 extern bool SplitFileSysURL(LPCTSTR url, String& dir_out, String& fname_out);
01082 
01083 
01084 #endif // __cplusplus

ROS Explorer Source Code Documentation
generated on 02.01.2013 by doxygen

ROS Explorer Homepage