Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
8.8 KiB

  1. // File: util.cpp
  2. #include "precomp.h"
  3. #include "resource.h"
  4. #include <oprahcom.h>
  5. #include <strutil.h>
  6. #include "dirutil.h"
  7. /* F I N D S Z C O M B O */
  8. /*-------------------------------------------------------------------------
  9. %%Function: FindSzCombo
  10. Find the item that matches at least the first part of the string
  11. from the names in the combo box list.
  12. Returns the item index or -1 if not found.
  13. -------------------------------------------------------------------------*/
  14. int FindSzCombo(HWND hwnd, LPCTSTR pszSrc, LPTSTR pszResult)
  15. {
  16. int cch = lstrlen(pszSrc);
  17. if (0 == cch)
  18. return -1;
  19. TCHAR szBuff[CCHMAXSZ];
  20. lstrcpy(szBuff, pszSrc);
  21. CharUpperBuff(szBuff, CCHMAX(szBuff));
  22. COMBOBOXEXITEM cbi;
  23. ClearStruct(&cbi);
  24. cbi.mask = CBEIF_TEXT;
  25. cbi.pszText = pszResult;
  26. for ( ; ; cbi.iItem++)
  27. {
  28. cbi.cchTextMax = CCHMAX(szBuff);
  29. if (0 == SendMessage(hwnd, CBEM_GETITEM, 0, (LPARAM) &cbi))
  30. return -1;
  31. TCHAR szTemp[CCHMAXSZ];
  32. lstrcpy(szTemp, pszResult);
  33. CharUpperBuff(szTemp, CCHMAX(szTemp));
  34. if (0 == _StrCmpN(szBuff, szTemp, cch))
  35. return (int)(cbi.iItem);
  36. }
  37. }
  38. // Atl defines a function AtlWaitWithMessageLoop
  39. // We are not linking with ATL, but when we start,
  40. // this function can be removed
  41. HRESULT WaitWithMessageLoop(HANDLE hEvent)
  42. {
  43. DWORD dwRet;
  44. MSG msg;
  45. HRESULT hr = S_OK;
  46. while(1)
  47. {
  48. dwRet = MsgWaitForMultipleObjects(1, &hEvent, FALSE, INFINITE, QS_ALLINPUT);
  49. if (dwRet == WAIT_OBJECT_0)
  50. return S_OK; // The event was signaled
  51. if (dwRet != WAIT_OBJECT_0 + 1)
  52. return E_FAIL; // Something else happened
  53. // There is one or more window message available. Dispatch them
  54. while(PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
  55. {
  56. TranslateMessage(&msg);
  57. DispatchMessage(&msg);
  58. if (WaitForSingleObject(hEvent, 0) == WAIT_OBJECT_0)
  59. return S_OK; // Event is now signaled.
  60. }
  61. }
  62. return hr;
  63. }
  64. /* A U T O C O M P L E T E C O M B O */
  65. /*-------------------------------------------------------------------------
  66. %%Function: AutoCompleteCombo
  67. Update the current edit text with the suggestion and select it.
  68. -------------------------------------------------------------------------*/
  69. VOID AutoCompleteCombo(HWND hwnd, LPCTSTR psz)
  70. {
  71. HWND hwndEdit = (HWND) SendMessage(hwnd, CBEM_GETEDITCONTROL, 0, 0);
  72. if (NULL != hwndEdit)
  73. {
  74. AutoCompleteEdit(hwndEdit, psz);
  75. }
  76. }
  77. VOID AutoCompleteEdit(HWND hwndEdit, LPCTSTR psz)
  78. {
  79. const int cchLast = 0x7FFF;
  80. int cch = GetWindowTextLength(hwndEdit);
  81. Edit_SetSel(hwndEdit, cchLast, cchLast);
  82. Edit_ReplaceSel(hwndEdit, psz);
  83. Edit_SetSel(hwndEdit, cch, cchLast);
  84. }
  85. /* F G E T D E F A U L T S E R V E R */
  86. /*-------------------------------------------------------------------------
  87. %%Function: FGetDefaultServer
  88. -------------------------------------------------------------------------*/
  89. BOOL FGetDefaultServer(LPTSTR pszServer, UINT cchMax)
  90. {
  91. RegEntry ulsKey(ISAPI_CLIENT_KEY, HKEY_CURRENT_USER);
  92. LPTSTR psz = ulsKey.GetString(REGVAL_SERVERNAME);
  93. if (FEmptySz(psz))
  94. return FALSE;
  95. lstrcpyn(pszServer, psz, cchMax);
  96. return TRUE;
  97. }
  98. /* F C R E A T E I L S N A M E */
  99. /*-------------------------------------------------------------------------
  100. %%Function: FCreateIlsName
  101. Combine the server and email names to form an ILS name.
  102. Return TRUE if the result fit in the buffer.
  103. -------------------------------------------------------------------------*/
  104. BOOL FCreateIlsName(LPTSTR pszDest, LPCTSTR pszEmail, int cchMax)
  105. {
  106. ASSERT(NULL != pszDest);
  107. TCHAR szServer[MAX_PATH];
  108. if (!FGetDefaultServer(szServer, CCHMAX(szServer)))
  109. return FALSE;
  110. if (FEmptySz(pszEmail))
  111. {
  112. WARNING_OUT(("FCreateIlsName: Null email name?"));
  113. return FALSE;
  114. }
  115. int cch = lstrlen(szServer);
  116. lstrcpyn(pszDest, szServer, cchMax);
  117. if (cch >= (cchMax-2))
  118. return FALSE;
  119. pszDest += cch;
  120. *pszDest++ = _T('/');
  121. cchMax -= (cch+1);
  122. lstrcpyn(pszDest, pszEmail, cchMax);
  123. return (lstrlen(pszEmail) < cchMax);
  124. }
  125. /* D I S P L A Y M S G */
  126. /*-------------------------------------------------------------------------
  127. %%Function: DisplayMsg
  128. Display a message with the standard title.
  129. -------------------------------------------------------------------------*/
  130. int DisplayMsg(HWND hwndParent, LPCTSTR pszMsg, UINT uType)
  131. {
  132. TCHAR szTitle[MAX_PATH];
  133. FLoadString(IDS_MSGBOX_TITLE, szTitle, CCHMAX(szTitle));
  134. return ::MessageBox(hwndParent, pszMsg, szTitle, uType);
  135. }
  136. int DisplayMsgId(HWND hwndParent, UINT id)
  137. {
  138. TCHAR szMsg[CCHMAXSZ];
  139. if (!FLoadString(id, szMsg, CCHMAX(szMsg)))
  140. return IDOK;
  141. return DisplayMsg(hwndParent, szMsg,
  142. MB_ICONINFORMATION | MB_SETFOREGROUND | MB_OK);
  143. }
  144. VOID DisplayMsgErr(HWND hwndParent, UINT id, PVOID pv)
  145. {
  146. TCHAR szFormat[CCHMAXSZ];
  147. if (!FLoadString(id, szFormat, CCHMAX(szFormat)))
  148. return;
  149. TCHAR szMsg[CCHMAXSZ*2];
  150. wsprintf(szMsg, szFormat, pv);
  151. ASSERT(lstrlen(szMsg) < CCHMAX(szMsg));
  152. DisplayMsg(hwndParent, szMsg, MB_OK | MB_SETFOREGROUND | MB_ICONERROR);
  153. }
  154. VOID DisplayMsgErr(HWND hwndParent, UINT id)
  155. {
  156. TCHAR szFormat[CCHMAXSZ];
  157. if (FLoadString(id, szFormat, CCHMAX(szFormat)))
  158. {
  159. DisplayMsg(hwndParent, szFormat, MB_OK | MB_SETFOREGROUND | MB_ICONERROR);
  160. }
  161. }
  162. /* A D D T O O L T I P */
  163. /*-------------------------------------------------------------------------
  164. %%Function: AddToolTip
  165. Add a tooltip to a control.
  166. -------------------------------------------------------------------------*/
  167. VOID AddToolTip(HWND hwndParent, HWND hwndCtrl, UINT_PTR idMsg)
  168. {
  169. if (NULL == hwndCtrl)
  170. return;
  171. HWND hwnd = ::CreateWindowEx(0, TOOLTIPS_CLASS, NULL, 0,
  172. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  173. hwndParent, NULL, ::GetInstanceHandle(), NULL);
  174. if (NULL == hwnd)
  175. return;
  176. TOOLINFO ti;
  177. ti.cbSize = sizeof(TOOLINFO);
  178. ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
  179. ti.hwnd = hwndParent;
  180. ti.hinst = ::GetInstanceHandle();
  181. ti.uId = (UINT_PTR) hwndCtrl; // Note: subclassing the window!
  182. ti.lpszText = (LPTSTR) idMsg;
  183. ::SendMessage(hwnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
  184. }
  185. /* C R E A T E S T A T I C T E X T */
  186. /*-------------------------------------------------------------------------
  187. %%Function: CreateStaticText
  188. -------------------------------------------------------------------------*/
  189. HWND CreateStaticText(HWND hwndParent, INT_PTR id)
  190. {
  191. HWND hwndCtrl = ::CreateWindowEx(0, g_cszStatic, NULL,
  192. WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
  193. hwndParent, (HMENU) id, ::GetInstanceHandle(), NULL);
  194. if (NULL == hwndCtrl)
  195. return NULL;
  196. // Set the font:
  197. ::SendMessage(hwndCtrl, WM_SETFONT, (WPARAM) GetDefaultFont(), 0);
  198. TCHAR sz[CCHMAXSZ];
  199. if (FLoadString(PtrToInt((LPVOID)id), sz, CCHMAX(sz)))
  200. {
  201. ::SetWindowText(hwndCtrl, sz);
  202. }
  203. return hwndCtrl;
  204. }
  205. /* C R E A T E B U T T O N */
  206. /*-------------------------------------------------------------------------
  207. %%Function: CreateButton
  208. -------------------------------------------------------------------------*/
  209. HWND CreateButton(HWND hwndParent, int ids, INT_PTR id)
  210. {
  211. TCHAR sz[CCHMAXSZ];
  212. if (!FLoadString(ids, sz, CCHMAX(sz)))
  213. return NULL;
  214. HWND hwndCtrl = CreateWindow(g_cszButton, sz,
  215. WS_CHILD | WS_VISIBLE | WS_TABSTOP |
  216. WS_CLIPCHILDREN | BS_PUSHBUTTON,
  217. 0, 0, 0, 0,
  218. hwndParent, (HMENU) id,
  219. ::GetInstanceHandle(), NULL);
  220. if (NULL != hwndCtrl)
  221. {
  222. ::SendMessage(hwndCtrl, WM_SETFONT, (WPARAM) GetDefaultFont(), 0);
  223. }
  224. return hwndCtrl;
  225. }
  226. bool IsValid_e164_Char( TCHAR t )
  227. {
  228. bool bRet = false;
  229. switch( t )
  230. {
  231. case _T('0'):
  232. case _T('1'):
  233. case _T('2'):
  234. case _T('3'):
  235. case _T('4'):
  236. case _T('5'):
  237. case _T('6'):
  238. case _T('7'):
  239. case _T('8'):
  240. case _T('9'):
  241. case _T('#'):
  242. case _T('*'):
  243. case _T(','):
  244. bRet = true;
  245. }
  246. return bRet;
  247. }
  248. HRESULT ExtractAddress( DWORD dwAddrType, LPTSTR szAddress, LPTSTR szExtractedAddr, int cchMax )
  249. {
  250. HRESULT hr = S_OK;
  251. LPTSTR pchRead;
  252. LPTSTR pchWrite;
  253. if( szAddress && szExtractedAddr && ( cchMax > 0 ) )
  254. {
  255. switch( dwAddrType )
  256. {
  257. case NM_ADDR_UNKNOWN:
  258. case NM_ADDR_IP:
  259. case NM_ADDR_MACHINENAME:
  260. case NM_ADDR_ULS:
  261. case NM_ADDR_ALIAS_ID:
  262. lstrcpyn( szExtractedAddr, szAddress, cchMax );
  263. break;
  264. // THese are phone numbers, yank the non-telephone num keys...
  265. case NM_ADDR_PSTN:
  266. case NM_ADDR_H323_GATEWAY:
  267. case NM_ADDR_ALIAS_E164:
  268. {
  269. pchRead = szAddress;
  270. pchWrite = szExtractedAddr;
  271. while( *pchRead != NULL )
  272. {
  273. if( IsValid_e164_Char( *pchRead ) )
  274. {
  275. // REVIEW: Is this rite for unicode??
  276. *pchWrite = *pchRead;
  277. pchWrite = CharNext(pchWrite);
  278. }
  279. pchRead = CharNext( pchRead );
  280. }
  281. // This will copy the NULL termination...
  282. *pchWrite = *pchRead;
  283. }
  284. break;
  285. default:
  286. hr = E_FAIL;
  287. break;
  288. }
  289. }
  290. else
  291. {
  292. hr = E_INVALIDARG;
  293. }
  294. return hr;
  295. }
  296. bool IsValidAddress( DWORD dwAddrType, LPTSTR szAddr )
  297. {
  298. bool bRet = false;
  299. if( szAddr && szAddr[0] )
  300. {
  301. bRet = true;
  302. }
  303. return bRet;
  304. }