Leaked source code of windows server 2003
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.

402 lines
11 KiB

  1. /*************************************************************************
  2. Project: Narrator
  3. Module: getprop.cpp
  4. Author: Charles Oppermann (ChuckOp)
  5. Date: 24 October 1996
  6. Notes: Gets Object Information
  7. Copyright (C) 1996 by Microsoft Corporation. All rights reserved.
  8. See bottom of file for disclaimer
  9. History: Clean up buffer problems : a-anilk
  10. *************************************************************************/
  11. #define STRICT
  12. #include <windows.h>
  13. #include <windowsx.h>
  14. #include <winerror.h>
  15. #include <oleacc.h>
  16. #include "..\Narrator\Narrator.h"
  17. #include "getprop.h"
  18. #define ARRAYSIZE(x) (sizeof(x) / sizeof(*x))
  19. void GetObjectProperty(IAccessible*, long, int, LPTSTR, UINT);
  20. void VariantMyInit(VARIANT *pv);
  21. // --------------------------------------------------------------------------
  22. //
  23. // GetObjectProperty
  24. //
  25. // --------------------------------------------------------------------------
  26. void GetObjectProperty(IAccessible * pobj, LONG idChild, int idProperty,
  27. LPTSTR lpszName, UINT cchName)
  28. {
  29. HRESULT hr=0;
  30. VARIANT varChild;
  31. BSTR bstr;
  32. RECT rc;
  33. VARIANT varResult;
  34. TCHAR bigbuf[4096] = TEXT("");
  35. if (!lpszName || cchName<1) return;
  36. *lpszName = 0;
  37. if ( cchName > ARRAYSIZE(bigbuf) )
  38. cchName = ARRAYSIZE(bigbuf);
  39. //
  40. // Clear out the possible return value objects
  41. //
  42. bstr = NULL;
  43. SetRectEmpty(&rc);
  44. VariantMyInit(&varResult);
  45. //
  46. // Setup the VARIANT child to pass in to the property
  47. //
  48. VariantMyInit(&varChild);
  49. varChild.vt = VT_I4;
  50. varChild.lVal = idChild;
  51. //
  52. // Get the property
  53. //
  54. switch (idProperty)
  55. {
  56. case ID_NAME:
  57. hr = pobj->get_accName(varChild, &bstr);
  58. break;
  59. case ID_DESCRIPTION:
  60. hr = pobj->get_accDescription(varChild, &bstr);
  61. break;
  62. case ID_VALUE:
  63. hr = pobj->get_accValue(varChild, &bstr);
  64. break;
  65. case ID_HELP:
  66. // Future enhancement: Try help file instead if failure
  67. hr = pobj->get_accHelp(varChild, &bstr);
  68. break;
  69. case ID_SHORTCUT:
  70. hr = pobj->get_accKeyboardShortcut(varChild, &bstr);
  71. break;
  72. case ID_DEFAULT:
  73. hr = pobj->get_accDefaultAction(varChild, &bstr);
  74. break;
  75. case ID_ROLE:
  76. hr = pobj->get_accRole(varChild, &varResult);
  77. break;
  78. case ID_STATE:
  79. hr = pobj->get_accState(varChild, &varResult);
  80. break;
  81. case ID_LOCATION:
  82. hr = pobj->accLocation(&rc.left, &rc.top, &rc.right, &rc.bottom, varChild);
  83. break;
  84. case ID_CHILDREN:
  85. // hr = GetObjectChildren(pobj, idChild, lpszName, cchName);
  86. break;
  87. case ID_SELECTION:
  88. // hr = GetObjectSelection(pobj, idChild, lpszName, cchName);
  89. break;
  90. case ID_PARENT:
  91. // hr = GetObjectParent(pobj, idChild, lpszName, cchName);
  92. break;
  93. case ID_WINDOW:
  94. // hr = GetObjectWindow(pobj, lpszName, cchName);
  95. break;
  96. }
  97. // Return if the IAccessible call failed.
  98. if (!SUCCEEDED(hr))
  99. {
  100. #ifdef _DEBUG
  101. // Pass back the error string to be displayed
  102. LPVOID lpMsgBuf;
  103. LPTSTR lpTChar;
  104. int length;
  105. length = FormatMessage(
  106. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  107. NULL,
  108. hr,
  109. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  110. (LPTSTR) &lpMsgBuf,
  111. 0,
  112. NULL
  113. );
  114. lpTChar = (LPTSTR)lpMsgBuf;
  115. // remove the \r\n at the end of the string
  116. if (length > 2)
  117. if (lpTChar[length-2] == '\r')
  118. lpTChar[length-2] = 0;
  119. wsprintf(bigbuf, TEXT("['%s' hr 0x%lx idProperty %d pobj 0x%lx idChild 0x%lx]"), lpMsgBuf,hr, idProperty, pobj, idChild);
  120. bigbuf[cchName-1]=0;
  121. lstrcpy(lpszName, bigbuf);
  122. // Free the buffer.
  123. LocalFree( lpMsgBuf );
  124. #endif
  125. return;
  126. }
  127. //
  128. // Convert it to a display string
  129. //
  130. switch (idProperty)
  131. {
  132. // These are the cases already taken care of.
  133. case ID_SELECTION:
  134. case ID_CHILDREN:
  135. case ID_PARENT:
  136. case ID_WINDOW:
  137. break;
  138. // These are the cases where we got unicode string back
  139. case ID_NAME:
  140. case ID_DESCRIPTION:
  141. case ID_VALUE:
  142. case ID_HELP:
  143. case ID_SHORTCUT:
  144. case ID_DEFAULT:
  145. if (bstr)
  146. {
  147. #ifdef UNICODE
  148. lstrcpyn(bigbuf, bstr, sizeof(bigbuf) / sizeof(TCHAR));
  149. #else
  150. WideCharToMultiByte(CP_ACP, 0, bstr, -1, (LPBYTE)bigbuf, cchName, NULL, NULL);
  151. #endif
  152. SysFreeString(bstr);
  153. }
  154. break;
  155. case ID_LOCATION:
  156. wsprintf(bigbuf, TEXT("{%04d, %04d, %04d, %04d}"), rc.left, rc.top,
  157. rc.left + rc.right, rc.top + rc.bottom);
  158. break;
  159. case ID_ROLE: // Role can be either I4 or BSTR
  160. break;
  161. case ID_STATE: // State can either be I4 or BSTR
  162. if (varResult.vt == VT_BSTR)
  163. {
  164. #ifdef UNICODE
  165. lstrcpyn(bigbuf, varResult.bstrVal, sizeof(bigbuf) / sizeof(TCHAR));
  166. #else
  167. // If we got back a string, use that.
  168. WideCharToMultiByte(CP_ACP, 0, varResult.bstrVal, -1,
  169. bigbuf, cchName, NULL, NULL);
  170. #endif
  171. }
  172. else if (varResult.vt == VT_I4)
  173. {
  174. int iStateBit;
  175. DWORD lStateBits;
  176. LPTSTR lpszT;
  177. UINT cchT;
  178. // We have a mask of standard states. Make a string.
  179. // Separate the states with ",".
  180. lpszT = bigbuf;
  181. for (iStateBit = 0, lStateBits = 1; iStateBit < 32; iStateBit++, (lStateBits <<= 1))
  182. {
  183. if (varResult.lVal & lStateBits)
  184. {
  185. cchT = GetStateText(lStateBits, lpszT, cchName);
  186. // If it is link, say so
  187. // if (lStateBits==STATE_SYSTEM_LINKED) Say(SAY_ALWAYS, lpszT);
  188. lpszT += cchT;
  189. cchName -= cchT;
  190. *lpszT++ = ',';
  191. *lpszT++ = ' ';
  192. }
  193. }
  194. //
  195. // Clip off final ", "
  196. //
  197. if (varResult.lVal)
  198. {
  199. *(lpszT-2) = 0;
  200. *(lpszT-1) = 0;
  201. }
  202. else
  203. GetStateText(0, bigbuf, cchName);
  204. }
  205. VariantClear(&varResult);
  206. break;
  207. default:
  208. DebugBreak();
  209. }
  210. bigbuf[cchName-1]=0;
  211. lstrcpy(lpszName, bigbuf);
  212. return;
  213. }
  214. /*************************************************************************
  215. Function: GetObjectName
  216. Purpose:
  217. Inputs:
  218. Returns:
  219. History:
  220. *************************************************************************/
  221. /* DWORD GetObjectName(LPOBJINFO poiObj,LPTSTR lpszBuf, int cchBuf)
  222. {
  223. BSTR bszName;
  224. IAccessible* pIAcc;
  225. long* pl;
  226. bszName = NULL;
  227. // Get the object out of the struct
  228. pl = poiObj->plObj;
  229. pIAcc =(IAccessible*)pl;
  230. GetObjectProperty(pIAcc, poiObj->varChild.lVal, ID_NAME, lpszBuf, cchBuf);
  231. return(0);
  232. }
  233. */
  234. /*
  235. DWORD GetObjectDescription(LPOBJINFO poiObj,LPTSTR lpszBuf, int cchBuf)
  236. {
  237. DWORD dwRetVal;
  238. BSTR bszDesc;
  239. IAccessible* pIAcc;
  240. long* pl;
  241. bszDesc = NULL;
  242. // Get the object out of the struct
  243. pl = poiObj->plObj;
  244. pIAcc =(IAccessible*)pl;
  245. // Get the object's name
  246. pIAcc->get_accDescription(poiObj->varChild, &bszDesc);
  247. // Did we get name string?
  248. if (bszDesc)
  249. {
  250. // Convert from OLE Unicode
  251. if (WideCharToMultiByte(CP_ACP, 0,
  252. bszDesc,
  253. WC_SEPCHARS, // -1
  254. lpszBuf,
  255. cchBuf,
  256. NULL, NULL))
  257. {
  258. SysFreeString(bszDesc);
  259. dwRetVal = NO_ERROR;
  260. }
  261. else
  262. {
  263. dwRetVal = GetLastError();
  264. }
  265. return(dwRetVal);
  266. }
  267. // Need general failure handling routine
  268. MessageBeep(MB_ICONEXCLAMATION);
  269. return(ERROR_INVALID_FUNCTION);
  270. }
  271. */
  272. /*
  273. DWORD GetObjectValue(LPOBJINFO poiObj, LPTSTR lpszBuf, int cchBuf)
  274. {
  275. DWORD dwRetVal;
  276. BSTR bszValue;
  277. IAccessible* pIAcc;
  278. long* pl;
  279. bszValue = NULL;
  280. // Get the object out of the struct
  281. pl = poiObj->plObj;
  282. pIAcc =(IAccessible*)pl;
  283. // Get the object's name
  284. pIAcc->get_accValue(poiObj->varChild, &bszValue);
  285. // Did we get name string?
  286. if (bszValue)
  287. {
  288. // Convert from OLE Unicode
  289. if (WideCharToMultiByte(CP_ACP, 0,
  290. bszValue,
  291. WC_SEPCHARS, // -1
  292. lpszBuf,
  293. cchBuf,
  294. NULL, NULL))
  295. {
  296. SysFreeString(bszValue);
  297. dwRetVal = NO_ERROR;
  298. }
  299. else
  300. {
  301. dwRetVal = GetLastError();
  302. }
  303. return(dwRetVal);
  304. }
  305. // Need general failure handling routine
  306. MessageBeep(MB_ICONEXCLAMATION);
  307. return(ERROR_INVALID_FUNCTION);
  308. }
  309. */
  310. void VariantMyInit(VARIANT *pv)
  311. {
  312. VariantInit(pv);
  313. pv->lVal=0;
  314. }
  315. /*************************************************************************
  316. THE INFORMATION AND CODE PROVIDED HEREUNDER (COLLECTIVELY REFERRED TO
  317. AS "SOFTWARE") IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER
  318. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  319. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN
  320. NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR
  321. ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL,
  322. CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF
  323. MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE
  324. POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR
  325. LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE
  326. FOREGOING LIMITATION MAY NOT APPLY.
  327. *************************************************************************/