| 1 |
/* |
| 2 |
Copyright (c) 2002 Robert Rainwater |
| 3 |
Copyright (c) 2002 Robert Rainwater |
| 4 |
Contributors: Justin Frankel, Fritz Elfert, and Sunil Kamath |
| 5 |
|
| 6 |
This software is provided 'as-is', without any express or implied |
| 7 |
warranty. In no event will the authors be held liable for any damages |
| 8 |
arising from the use of this software. |
| 9 |
|
| 10 |
Permission is granted to anyone to use this software for any purpose, |
| 11 |
including commercial applications, and to alter it and redistribute it |
| 12 |
freely, subject to the following restrictions: |
| 13 |
|
| 14 |
1. The origin of this software must not be misrepresented; you must not |
| 15 |
claim that you wrote the original software. If you use this software |
| 16 |
in a product, an acknowledgment in the product documentation would be |
| 17 |
appreciated but is not required. |
| 18 |
2. Altered source versions must be plainly marked as such, and must not be |
| 19 |
misrepresented as being the original software. |
| 20 |
3. This notice may not be removed or altered from any source distribution. |
| 21 |
|
| 22 |
Unicode support by Jim Park -- 08/20/2007 |
| 23 |
|
| 24 |
*/ |
| 25 |
#ifndef UTILS_H |
| 26 |
#define UTILS_H |
| 27 |
#include "resource.h" |
| 28 |
#include "toolbar.h" |
| 29 |
|
| 30 |
#ifdef COUNTOF |
| 31 |
#undef COUNTOF |
| 32 |
#endif |
| 33 |
#define COUNTOF(a) (sizeof(a)/sizeof(a[0])) |
| 34 |
|
| 35 |
#if defined(_MSC_VER) && _MSC_VER >= 1200 |
| 36 |
EXTERN_C IMAGE_DOS_HEADER __ImageBase; |
| 37 |
#define HINST_THISCOMPONENT ( (HINSTANCE) &__ImageBase ) |
| 38 |
#define HINST_APPLICATION HINST_THISCOMPONENT |
| 39 |
#else |
| 40 |
#define HINST_APPLICATION ( (HINSTANCE) GetModuleHandle(NULL) ) |
| 41 |
#endif |
| 42 |
|
| 43 |
#define MRU_LIST_SIZE 5 |
| 44 |
#define MRU_DISPLAY_LENGTH 40 |
| 45 |
#define SYMSETNAME_MAXLEN 40 |
| 46 |
|
| 47 |
template<class T> inline T API_cast(INT_PTR(CALLBACK*p1)(HWND,UINT,WPARAM,LPARAM)) { return (DLGPROC) p1; } // DLGPROC in really old SDKs return BOOL |
| 48 |
|
| 49 |
void* MemAllocZI(SIZE_T cb); |
| 50 |
void MemSafeFree(void*mem); |
| 51 |
#define MemAlloc MemAllocZI |
| 52 |
#define MemFree MemSafeFree |
| 53 |
HMODULE LoadSysLibrary(LPCSTR Mod); |
| 54 |
FARPROC GetSysProcAddr(LPCSTR Mod, LPCSTR FuncName); |
| 55 |
bool WriteUTF16LEBOM(HANDLE hFile); |
| 56 |
|
| 57 |
BOOL InitCCExHelper(UINT icc); |
| 58 |
static inline BOOL InitCCEx(UINT icc) { return icc > 0xff ? InitCCExHelper(icc) : true; } |
| 59 |
static inline UINT SizeOfStruct(const TOOLINFO&x) { return FIELD_OFFSET(TOOLINFO, lParam); } |
| 60 |
static inline UINT SizeOfStruct(const OPENFILENAME&x) { return sizeof(void*) < 8 ? 76 : sizeof(x); } |
| 61 |
UINT GetScreenBPP(HWND hWnd = NULL); |
| 62 |
|
| 63 |
BOOL ShellExecuteWithErrorBox(HWND hWnd, LPCTSTR File, LPCTSTR Parameters = NULL); |
| 64 |
inline BOOL ShellExecuteSilent(HWND hWnd, LPCTSTR File) { return ShellExecuteWithErrorBox(NULL, File); } |
| 65 |
void FreeSpawn(PROCESS_INFORMATION *pPI, HANDLE hRd, HANDLE hWr); |
| 66 |
BOOL InitSpawn(STARTUPINFO &si, HANDLE &hRd, HANDLE &hWr); |
| 67 |
|
| 68 |
typedef BYTE PACKEDCMDID_T; |
| 69 |
#define PACKCMDID(id) ( PACKEDCMDID_T((id) - IDM_CMDBASE) ) |
| 70 |
#define UNPACKCMDID(id) ( IDM_CMDBASE + (id) ) |
| 71 |
|
| 72 |
int SetArgv(const TCHAR *cmdLine, TCHAR ***argv); |
| 73 |
void SetTitle(HWND hwnd,const TCHAR *substr); |
| 74 |
void PlayAppSoundAsync(LPCSTR SoundName, int MBFallback = -1); |
| 75 |
void CopyToClipboard(HWND hwnd); |
| 76 |
void InitializeLogWindow(); |
| 77 |
void ReleaseLogWindow(); |
| 78 |
enum LOGCOLOR { LC_SUCCESS, LC_WARNING, LC_ERROR, LC_SYSCOLOR }; |
| 79 |
void SetLogColor(enum LOGCOLOR lc); |
| 80 |
void ClearLog(); |
| 81 |
void LogMessage(HWND hwnd,const TCHAR *str); |
| 82 |
void ErrorMessage(HWND hwnd,const TCHAR *str); |
| 83 |
void CenterOnParent(HWND hwnd); |
| 84 |
void SetDialogFocus(HWND hDlg, HWND hCtl); // Use this and not SetFocus()! |
| 85 |
#define DlgRet(hDlg, val) ( SetWindowLongPtr((hDlg), DWLP_MSGRESULT, (val)) | TRUE ) |
| 86 |
HWND GetComboEdit(HWND hCB); |
| 87 |
#define DisableItems(hwnd) EnableDisableItems(((void)(hwnd), 0)) |
| 88 |
#define EnableItems(hwnd) EnableDisableItems(((void)(hwnd), 1)) |
| 89 |
void EnableDisableItems(int on); |
| 90 |
bool OpenRegSettingsKey(HKEY &hKey, bool create = false); |
| 91 |
#define CreateRegSettingsKey(refhkey) OpenRegSettingsKey((refhkey), true) |
| 92 |
DWORD ReadRegSettingDW(LPCTSTR name, const DWORD defval); |
| 93 |
void RestoreWindowPos(HWND hwnd); |
| 94 |
void SaveWindowPos(HWND hwnd); |
| 95 |
void ResetObjects(); |
| 96 |
void ResetSymbols(); |
| 97 |
int InitBranding(); |
| 98 |
void InitTooltips(HWND h); |
| 99 |
void DestroyTooltips(); |
| 100 |
void AddTip(HWND hWnd, LPCTSTR lpszToolTip); |
| 101 |
LRESULT SetTooltipText(HWND hWnd, UINT_PTR Id, LPCTSTR Text); |
| 102 |
void UpdateCloseButtonTooltip(); |
| 103 |
void ShowDocs(); |
| 104 |
void RestoreCompressor(); |
| 105 |
void SaveCompressor(); |
| 106 |
void SetCompressorStats(); |
| 107 |
|
| 108 |
BOOL PopMRUFile(TCHAR* fname); |
| 109 |
void PushMRUFile(TCHAR* fname); |
| 110 |
void BuildMRUMenus(); |
| 111 |
void LoadMRUFile(int position); |
| 112 |
void ClearMRUList(); |
| 113 |
|
| 114 |
struct FSPath { |
| 115 |
template<class T> static inline bool IsAgnosticSeparator(const T c) { return '\\' == c || '/' == c; } |
| 116 |
template<class T> static T* FindLastComponent(T*p) // Note: Returns "" for "dir\" |
| 117 |
{ |
| 118 |
for (T *sep = 0, *start = p;; ++p) |
| 119 |
if (!*p) |
| 120 |
return sep ? ++sep : start; |
| 121 |
else if (IsAgnosticSeparator(*p)) |
| 122 |
sep = p; |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
template<class T> UINT StrLenT(const T*s) { return sizeof(T) > 1 ? lstrlenW((WCHAR*) s) : lstrlenA((CHAR*) s) ; } |
| 127 |
template<class T> T AsciiLoCh(T ch) { return ch >= 'A' && ch <= 'Z' ? (ch | 32) : ch; } |
| 128 |
|
| 129 |
template<class A, class B> int AsciiCmpNI(const A*a, const B*b, SIZE_T len) |
| 130 |
{ |
| 131 |
int cmp = 0; |
| 132 |
for (SIZE_T i = 0;; ++i) |
| 133 |
if (i == len || (cmp = AsciiLoCh(a[i]) - AsciiLoCh(b[i])) || !a[i]) |
| 134 |
return cmp; |
| 135 |
} |
| 136 |
template<class A, class B> inline bool AsciiMatchNI(const A*a, const B*b, SIZE_T c) { return !AsciiCmpNI(a, b, c); } |
| 137 |
template<class A, class B> bool AsciiMatchPrefixI(const A*s, const B*p) { return AsciiMatchNI(s, p, StrLenT(p)); } |
| 138 |
|
| 139 |
template<class T> HKEY GetRegRootKey(const T*Str) |
| 140 |
{ |
| 141 |
if (AsciiMatchPrefixI(Str, "HKEY_CLASSES_ROOT") || AsciiMatchPrefixI(Str, "HKCR")) return HKEY_CLASSES_ROOT; |
| 142 |
if (AsciiMatchPrefixI(Str, "HKEY_CURRENT_USER") || AsciiMatchPrefixI(Str, "HKCU")) return HKEY_CURRENT_USER; |
| 143 |
if (AsciiMatchPrefixI(Str, "HKEY_LOCAL_MACHINE") || AsciiMatchPrefixI(Str, "HKLM")) return HKEY_LOCAL_MACHINE; |
| 144 |
if (AsciiMatchPrefixI(Str, "HKEY_USERS") || AsciiMatchPrefixI(Str, "HKU")) return HKEY_USERS; |
| 145 |
return (HKEY) NULL; |
| 146 |
} |
| 147 |
DWORD RegOpenKeyForReading(HKEY hRoot, LPCTSTR SubKey, HKEY*pKey); |
| 148 |
|
| 149 |
bool FileExists(const TCHAR *fname); |
| 150 |
bool OpenUrlInDefaultBrowser(HWND hwnd, LPCSTR Url); |
| 151 |
|
| 152 |
HMENU FindSubMenu(HMENU hMenu, UINT uId); |
| 153 |
static inline UINT GetMenuDropAlignment() { return GetSystemMetrics(SM_MENUDROPALIGNMENT) ? TPM_RIGHTALIGN : TPM_LEFTALIGN; } |
| 154 |
|
| 155 |
typedef enum { CFF_RAWSIZE = 0x00, CFF_DPIPT = 0x01, CFF_DPIFROMHWND = 0x02 } CREATEFONTFLAGS; |
| 156 |
HFONT CreateFontHelper(INT_PTR Data, int Height, DWORD p1, LPCTSTR Face); |
| 157 |
inline HFONT CreateFont(INT_PTR Data, WORD Flags, int Height, WORD Weight, BYTE PitchAndFamily, BYTE CharSet, LPCTSTR Face) |
| 158 |
{ |
| 159 |
DWORD packed = MAKELONG(MAKEWORD(Weight>>2, Flags), MAKEWORD(CharSet, PitchAndFamily)); |
| 160 |
return CreateFontHelper(Data, Height, packed, Face); |
| 161 |
} |
| 162 |
inline HFONT CreateFontPt(HWND hWndDPI, int Height, WORD Weight, BYTE PitchAndFamily, BYTE CharSet, LPCTSTR Face) |
| 163 |
{ |
| 164 |
return CreateFont((INT_PTR) hWndDPI, CFF_DPIFROMHWND|CFF_DPIPT, Height, Weight, PitchAndFamily, CharSet, Face); |
| 165 |
} |
| 166 |
BOOL FontExists(LPCTSTR Face); |
| 167 |
BOOL FillRectColor(HDC hDC, const RECT &Rect, COLORREF Color); |
| 168 |
BOOL DrawHorzGradient(HDC hDC, LONG l, LONG t, LONG r, LONG b, COLORREF c1, COLORREF c2); |
| 169 |
inline long RectW(const RECT&r) { return r.right - r.left; } |
| 170 |
inline long RectH(const RECT&r) { return r.bottom - r.top; } |
| 171 |
long DlgUnitToPixelX(HWND hDlg, long x); |
| 172 |
long DlgUnitToPixelY(HWND hDlg, long y); |
| 173 |
UINT DpiGetForMonitor(HWND hWnd); |
| 174 |
UINT DpiGetForWindow(HWND hWnd); |
| 175 |
int DpiScaleY(HWND hWnd, int Val); |
| 176 |
|
| 177 |
void DrawGripper(HWND hWnd, HDC hDC, const RECT&r); |
| 178 |
static inline void GetGripperPos(HWND hwnd, RECT&r) |
| 179 |
{ |
| 180 |
GetClientRect(hwnd, &r); |
| 181 |
r.left = r.right - GetSystemMetrics(SM_CXVSCROLL); |
| 182 |
r.top = r.bottom - GetSystemMetrics(SM_CYVSCROLL); |
| 183 |
} |
| 184 |
|
| 185 |
bool RicheditHasSelection(HWND hRE); |
| 186 |
HRESULT RicheditFreeze(void*pITextDocument, SIZE_T Freeze); |
| 187 |
|
| 188 |
void EnableUICommand(UINT Id, INT_PTR Enabled); |
| 189 |
|
| 190 |
#endif |