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.

513 lines
15 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: win2mac.cpp
  7. //
  8. // Contents: All functions that are not implemented for the Macintosh
  9. // Stubs are coded so that we can link cleanly.
  10. //
  11. //----------------------------------------------------------------------------
  12. #ifndef PEGASUS
  13. #include "_common.h"
  14. #include <mbstring.h>
  15. #include "stdio.h"
  16. #if defined(MACPORT) && defined(UNICODE)
  17. #include <macname1.h>
  18. #include <quickdraw.h>
  19. #include <macname2.h>
  20. AssertData
  21. //----------------------------------------------------------------------------
  22. //
  23. // Mac wrapper functions
  24. //
  25. //----------------------------------------------------------------------------
  26. //----------------------------------------------------------------------------
  27. //
  28. // Function: MacCLSIDFromProgID
  29. //
  30. //----------------------------------------------------------------------------
  31. #undef CLSIDFromProgID
  32. STDAPI MacCLSIDFromProgID (LPCWSTR lpszProgID, LPCLSID lpclsid)
  33. {
  34. CStrIn str(lpszProgID);
  35. return CLSIDFromProgID((LPOLESTR)str, lpclsid);
  36. }
  37. //----------------------------------------------------------------------------
  38. //
  39. // Function: MacCoTaskMemAlloc
  40. //
  41. //----------------------------------------------------------------------------
  42. STDAPI_(LPVOID) MacCoTaskMemAlloc(ULONG cb)
  43. {
  44. IMalloc *pIMalloc = NULL;
  45. HRESULT hr;
  46. HGLOBAL pMem = NULL;
  47. hr = CoGetMalloc ( MEMCTX_TASK, &pIMalloc);
  48. if (hr)
  49. {
  50. goto Cleanup;
  51. }
  52. pMem = pIMalloc->Alloc(cb);
  53. Cleanup:
  54. if( pIMalloc )
  55. pIMalloc->Release();
  56. return (LPVOID)pMem;
  57. }
  58. //----------------------------------------------------------------------------
  59. //
  60. // Function: MacCoTaskMemFree
  61. //
  62. //----------------------------------------------------------------------------
  63. STDAPI_(void) MacCoTaskMemFree(LPVOID pv)
  64. {
  65. IMalloc *pIMalloc = NULL;
  66. HRESULT hr;
  67. HGLOBAL pMem = NULL;
  68. hr = CoGetMalloc ( MEMCTX_TASK, &pIMalloc);
  69. if (hr)
  70. {
  71. goto Cleanup;
  72. }
  73. pIMalloc->Free(pv);
  74. Cleanup:
  75. if( pIMalloc )
  76. pIMalloc->Release();
  77. }
  78. //----------------------------------------------------------------------------
  79. //
  80. // Function: MacCoTaskMemRealloc
  81. //
  82. //----------------------------------------------------------------------------
  83. STDAPI_(LPVOID) MacCoTaskMemRealloc(LPVOID pv, ULONG cb)
  84. {
  85. IMalloc *pIMalloc = NULL;
  86. HRESULT hr;
  87. HGLOBAL pMem = NULL;
  88. hr = CoGetMalloc ( MEMCTX_TASK, &pIMalloc);
  89. if (hr)
  90. {
  91. goto Cleanup;
  92. }
  93. pMem = pIMalloc->Realloc(pv,cb);
  94. Cleanup:
  95. if( pIMalloc )
  96. pIMalloc->Release();
  97. return (LPVOID)pMem;
  98. }
  99. //----------------------------------------------------------------------------
  100. //
  101. // Function: MacGetCurrentObject
  102. //
  103. //----------------------------------------------------------------------------
  104. HGDIOBJ WINAPI MacGetCurrentObject(HDC hdc, // handle of device context
  105. UINT uObjectType) // object-type identifier
  106. {
  107. // GetCurrentObject is not supported under wlm, so we always simulate failure
  108. // by returning NULL
  109. return NULL;
  110. }
  111. //----------------------------------------------------------------------------
  112. // NOTE WELL: GetDoubleClickTime() maps to GetDblTime() in the VC++ 4.0 header
  113. // file MSDEV\MAC\INCLUDE\MACOS\EVENTS.H. The only problem is that
  114. // GetDblTime() returns the count in "Ticks" (1/60th second) and
  115. // we want milliseconds (0.001 sec). This should be fixed when the
  116. // bug in the VC4 header file is fixed. - DAS 1/16/96
  117. //----------------------------------------------------------------------------
  118. UINT MacGetDoubleClickTime()
  119. {
  120. return MulDiv(100,GetDblTime(),6);
  121. }
  122. //----------------------------------------------------------------------------
  123. //
  124. // Function: MacGetMetaFileBitsEx
  125. //
  126. //----------------------------------------------------------------------------
  127. UINT WINAPI MacGetMetaFileBitsEx(HMETAFILE hmf, UINT nSize, LPVOID lpvData )
  128. {
  129. Assert (0 && "GetMetaFileBitsEx is not implemented for Macintosh");
  130. return NULL;
  131. }
  132. //----------------------------------------------------------------------------
  133. //
  134. // Function: MacIsValidCodePage
  135. //
  136. //----------------------------------------------------------------------------
  137. WINBASEAPI BOOL WINAPI MacIsValidCodePage(UINT CodePage)
  138. {
  139. return TRUE;
  140. }
  141. //----------------------------------------------------------------------------
  142. //
  143. // Function: MacOleDraw
  144. //
  145. //----------------------------------------------------------------------------
  146. #undef OleDraw
  147. STDAPI MacOleDraw(
  148. IUnknown * pUnk, //Points to the view object to be drawn
  149. DWORD dwAspect, //Specifies how the object is to be represented
  150. HDC hdcDraw, //Identifies the device context on which to draw
  151. LPCRECT lprcBounds //Specifies the rectangle in which the object is drawn
  152. )
  153. {
  154. Rect rect;
  155. HRESULT ret;
  156. GrafPtr grafptr = CheckoutPort(hdcDraw, CA_NONE);
  157. Assert(grafptr);
  158. rect.top = lprcBounds->top;
  159. rect.bottom = lprcBounds->bottom;
  160. rect.left = lprcBounds->left;
  161. rect.right = lprcBounds->right;
  162. ret = OleDraw(pUnk, dwAspect, grafptr, &rect);
  163. CheckinPort(hdcDraw, CA_ALL);
  164. return ret;
  165. }
  166. //----------------------------------------------------------------------------
  167. //
  168. // Function: MacProgIDFromCLSID
  169. //
  170. //----------------------------------------------------------------------------
  171. #undef ProgIDFromCLSID
  172. STDAPI MacProgIDFromCLSID (REFCLSID clsid, LPWSTR FAR* lplpszProgID)
  173. {
  174. CStrIn str(*lplpszProgID);
  175. return ProgIDFromCLSID(clsid, (LPSTR FAR*)&str);
  176. }
  177. //-------------------------------------------------------------------------
  178. //
  179. // Function: MacRichEditWndProc
  180. //
  181. // Synopsis: This is the Mac WndProc callback function.
  182. //
  183. // Arguments: HWND hwnd,
  184. // UINT msg,
  185. // WPARAM wparam,
  186. // LPARAM lparam
  187. //
  188. // Returns: LRESULT
  189. //
  190. // Notes: The function processes the messages, then often calls
  191. // the normal PC callback function (RichEditANSIWndProc);
  192. //
  193. //
  194. //-------------------------------------------------------------------------
  195. LRESULT CALLBACK MacRichEditWndProc(
  196. HWND hwnd,
  197. UINT msg,
  198. WPARAM wparam,
  199. LPARAM lparam)
  200. {
  201. TRACEBEGINPARAM(TRCSUBSYSHOST, TRCSCOPEINTERN, "MacRichEditWndProc", msg);
  202. switch( msg )
  203. {
  204. case WM_MACINTOSH:
  205. {
  206. if (LOWORD(wparam) == WLM_SETMENUBAR)
  207. return TRUE; // dont change the menu bar
  208. }
  209. break;
  210. case WM_LBUTTONDOWN:
  211. case WM_LBUTTONUP:
  212. case WM_LBUTTONDBLCLK:
  213. {
  214. MacSimulateMouseButtons(msg,wparam);
  215. }
  216. break;
  217. case WM_KEYDOWN:
  218. case WM_KEYUP:
  219. {
  220. MacSimulateKey(msg,wparam);
  221. }
  222. break;
  223. case WM_SIZE:
  224. //case WM_SETFOCUS:
  225. //case WM_SYSCOLORCHANGE:
  226. //case WM_MOVE:
  227. //case WM_MACINTOSH:
  228. {
  229. return DefWindowProc(hwnd, msg, wparam, lparam);
  230. }
  231. break;
  232. case WM_CHAR:
  233. {
  234. return RichEditWndProc(hwnd, msg, wparam, lparam);
  235. }
  236. break;
  237. default:
  238. return RichEditANSIWndProc(hwnd, msg, wparam, lparam);
  239. }
  240. return RichEditANSIWndProc(hwnd, msg, wparam, lparam);
  241. }
  242. //----------------------------------------------------------------------------
  243. //
  244. // Function: MacSelectPalette
  245. //
  246. //----------------------------------------------------------------------------
  247. HPALETTE WINAPI MacSelectPalette(HDC hdc, HPALETTE hpal, BOOL bForceBackground)
  248. {
  249. if (hpal)
  250. return ::SelectPalette(hdc,hpal,bForceBackground);
  251. else
  252. return NULL;
  253. }
  254. //----------------------------------------------------------------------------
  255. //
  256. // Function: MacportSetCursor
  257. //
  258. //----------------------------------------------------------------------------
  259. HCURSOR MacportSetCursor(HCURSOR hCursor)
  260. {
  261. if (hCursor)
  262. return SetCursor(hCursor);
  263. else
  264. {
  265. ObscureCursor();
  266. return NULL;
  267. }
  268. }
  269. //----------------------------------------------------------------------------
  270. //
  271. // Function: MacSetMetaFileBitsEx
  272. //
  273. //----------------------------------------------------------------------------
  274. HMETAFILE WINAPI MacSetMetaFileBitsEx(UINT nSize,CONST BYTE * lpData )
  275. {
  276. Assert (0 && "SetMetaFileBitsEx is not implemented for Macintosh");
  277. return NULL;
  278. }
  279. //-------------------------------------------------------------------------
  280. //
  281. // Function: MacSimulateKey
  282. //
  283. // Synopsis: Simulates menu enabler keys for the mac
  284. //
  285. // Arguments: [msg]
  286. // [wParam]
  287. //
  288. // Returns: UINT
  289. //
  290. // Notes: The key is changed to accommodate the mac.
  291. //
  292. //
  293. //-------------------------------------------------------------------------
  294. UINT MacSimulateKey (UINT& msg, WPARAM& wParam)
  295. {
  296. BYTE rgbKeyState[256];
  297. GetKeyboardState(rgbKeyState);
  298. if (rgbKeyState[VK_CONTROL])
  299. {
  300. rgbKeyState[VK_CONTROL] = 0;
  301. }
  302. SetKeyboardState(rgbKeyState);
  303. return msg;
  304. }
  305. //-------------------------------------------------------------------------
  306. //
  307. // Function: MacSimulateMouseButtons
  308. //
  309. // Synopsis: Simulates the right and middle mouse Windows buttons.
  310. //
  311. // Arguments: [msg]
  312. // [wParam]
  313. //
  314. // Returns: UINT
  315. //
  316. // Notes: The right mouse is simulated by CTRL (or COMMAND) click. The
  317. // middle mouse is simulated by SHIFT click. Because CTRL is already
  318. // in use the CTRL click is simulated using the OPTION key.
  319. //
  320. // The command key is used the same as the control key because
  321. // WLM likes to pick up the CTRL mouse as a user break. This makes
  322. // debugging the right mouse simulation very difficult.
  323. //
  324. //-------------------------------------------------------------------------
  325. //-------------------------------------------------------------------------
  326. typedef struct tagUISim
  327. {
  328. UINT msg;
  329. UINT wParam;
  330. BYTE control; // Value for VK_CONTROL key state
  331. BYTE menu; // Value for VK_MENU key state
  332. } UISim;
  333. //-------------------------------------------------------------------------
  334. UINT MacSimulateMouseButtons (UINT& msg, WPARAM& wParam)
  335. {
  336. BYTE rgbKeyState[256];
  337. WORD stateIndex = 0;
  338. UISim UISim[] = // 8 4 2 1
  339. { // cmd shift ctrl option
  340. WM_LBUTTONDOWN, MK_LBUTTON, 0x00, 0x00, // - - - -
  341. WM_LBUTTONDOWN, MK_LBUTTON|MK_CONTROL, 0x80, 0x00, // - - - x
  342. WM_RBUTTONDOWN, MK_RBUTTON, 0x00, 0x00, // - - x -
  343. WM_RBUTTONDOWN, MK_RBUTTON|MK_CONTROL, 0x80, 0x00, // - - x x
  344. WM_MBUTTONDOWN, MK_MBUTTON, 0x00, 0x00, // - x - -
  345. WM_MBUTTONDOWN, MK_MBUTTON|MK_CONTROL, 0x80, 0x00, // - x - x
  346. WM_RBUTTONDOWN, MK_RBUTTON|MK_MBUTTON, 0x00, 0x00, // - x x -
  347. WM_RBUTTONDOWN, MK_RBUTTON|MK_MBUTTON|MK_CONTROL, 0x80, 0x00, // - x x x
  348. WM_LBUTTONDOWN, MK_LBUTTON, 0x00, 0x10, // x - - -
  349. WM_LBUTTONDOWN, MK_LBUTTON|MK_CONTROL, 0x80, 0x10, // x - - x
  350. WM_RBUTTONDOWN, MK_RBUTTON, 0x00, 0x10, // x - x -
  351. WM_RBUTTONDOWN, MK_RBUTTON|MK_CONTROL, 0x80, 0x10, // x - x x
  352. WM_MBUTTONDOWN, MK_MBUTTON, 0x00, 0x10, // x x - -
  353. WM_MBUTTONDOWN, MK_MBUTTON|MK_CONTROL, 0x80, 0x10, // x x - x
  354. WM_RBUTTONDOWN, MK_RBUTTON|MK_MBUTTON, 0x00, 0x10, // x x x -
  355. WM_RBUTTONDOWN, MK_RBUTTON|MK_MBUTTON|MK_CONTROL, 0x80, 0x10 // x x x x
  356. };
  357. // Determine which keys were pressed, and clean out the state variables
  358. GetKeyboardState(rgbKeyState);
  359. if (rgbKeyState[VK_OPTION])
  360. {
  361. rgbKeyState[VK_OPTION] = 0; // Clear key state
  362. stateIndex |= 0x01; // Set option key bit in index
  363. }
  364. if (rgbKeyState[VK_CONTROL])
  365. {
  366. rgbKeyState[VK_CONTROL] = 0; // Clear key state
  367. stateIndex |= 0x02; // Set control key bit in index
  368. }
  369. if (rgbKeyState[VK_COMMAND]) // Use command key like control key due to WLM debug issues
  370. {
  371. rgbKeyState[VK_COMMAND] = 0; // Clear key state
  372. stateIndex |= 0x08; // Set command key bit in index
  373. }
  374. if (rgbKeyState[VK_SHIFT])
  375. {
  376. rgbKeyState[VK_SHIFT] = 0; // Clear key state
  377. stateIndex |= 0x04; // Set shift key bit in index
  378. }
  379. // Now set the return values
  380. if (stateIndex) // Only do this is the mouse is being simulated
  381. {
  382. msg = (msg - WM_LBUTTONDOWN) + UISim[stateIndex].msg;
  383. wParam = UISim[stateIndex].wParam;
  384. rgbKeyState[VK_CONTROL] = UISim[stateIndex].control;
  385. rgbKeyState[VK_MENU] = UISim[stateIndex].menu;
  386. SetKeyboardState(rgbKeyState);
  387. }
  388. return msg;
  389. }
  390. //----------------------------------------------------------------------------
  391. //
  392. // Function: MacSysAllocStringLen
  393. //
  394. //----------------------------------------------------------------------------
  395. STDAPI_(BSTR) MacSysAllocStringLen(LPCWSTR lpStringW, UINT lenChars)
  396. {
  397. TRACEBEGIN(TRCSUBSYSWRAP, TRCSCOPEINTERN, "SysAllocStringLenMac");
  398. int lenStrBytes;
  399. LPSTR lpStringMB;
  400. if ((lpStringW) && (*lpStringW != NULL))
  401. {
  402. lenStrBytes = MsoWideCharToMultiByte(CP_ACP,0,lpStringW,lenChars,NULL,NULL,NULL,NULL);
  403. lpStringMB = (LPSTR)CoTaskMemAlloc( lenStrBytes + sizeof(INT) );
  404. memcpy(lpStringMB, &lenStrBytes, sizeof(INT)); //copy BSTR lenghth in integer before BSTR
  405. lpStringMB += sizeof(INT);
  406. MsoWideCharToMultiByte(CP_ACP,0,lpStringW,lenChars,lpStringMB,lenStrBytes,NULL,NULL);
  407. }
  408. else
  409. {
  410. // note that in every case so far used on RichEdit the first parm is NULL
  411. // so no way to determine how big to make the buffer
  412. // therefore making it the lenghth of a unicode buffer - max size it could be for mbcs
  413. lenStrBytes = lenChars*sizeof(WCHAR);
  414. lpStringMB = (LPSTR)CoTaskMemAlloc( lenStrBytes + sizeof(INT) );
  415. // not sure if this should be lenChars or lenStrBytes
  416. // note that lenStrBytes is wchar lenghth - the max length it could be for mbcs
  417. // memcpy(lpStringMB, &lenStrBytes, sizeof(INT)); //copy BSTR lenghth in integer before BSTR
  418. memcpy(lpStringMB, &lenChars, sizeof(INT)); //copy BSTR lenghth in integer before BSTR
  419. lpStringMB += sizeof(INT);
  420. }
  421. return (BSTR)lpStringMB;
  422. }
  423. //----------------------------------------------------------------------------
  424. //
  425. // Function: MacWordSwapLong
  426. //
  427. //----------------------------------------------------------------------------
  428. ULONG MacWordSwapLong ( ULONG ul)
  429. {
  430. WORD w1,w2;
  431. w1 = (WORD)ul;
  432. w2 = (WORD)(ul>>16);
  433. return (((ULONG)w1)<<16) | w2;
  434. }
  435. #endif //MACPORT
  436. #endif