Windows NT 4.0 source code leak
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.

306 lines
5.9 KiB

4 years ago
  1. /***
  2. *macmain.cpp
  3. *
  4. * Copyright (C) 1992-1994, Microsoft Corporation. All Rights Reserved.
  5. *
  6. *Purpose:
  7. * This module is the main entry point for the sample IDispatch calculator
  8. * server, dispcalc.
  9. *
  10. *Implementation Notes:
  11. *
  12. *****************************************************************************/
  13. #include "dispcalc.h"
  14. #include <stdio.h>
  15. extern "C" {
  16. Boolean g_fInitOle = false;
  17. #ifndef _PPCMAC
  18. Boolean g_fInitLibraryManager = false;
  19. #endif //!_PPCMAC
  20. }
  21. Boolean g_fQuit = false;
  22. void Init(void);
  23. void EventLoop(void);
  24. void AdjustMenus(void);
  25. void DoEvent(EventRecord *pevent);
  26. void DoMenuCommand(long menuResult);
  27. void Quit(void);
  28. #ifndef _MSC_VER
  29. #ifndef ConstStr255Param
  30. #define ConstStr255Param StringPtr
  31. #endif
  32. #endif
  33. void Fatal(ConstStr255Param);
  34. Boolean
  35. IsAppWindow(WindowPtr window)
  36. {
  37. return (window == nil)
  38. ? false : ((WindowPeek)window)->windowKind == userKind;
  39. }
  40. Boolean
  41. IsDAWindow(WindowPtr window)
  42. {
  43. return (window == nil)
  44. ? false : (((WindowPeek)window)->windowKind < 0);
  45. }
  46. void
  47. main()
  48. {
  49. Init();
  50. EventLoop();
  51. }
  52. void
  53. EventLoop()
  54. {
  55. short sItem;
  56. DialogPtr pdlg;
  57. EventRecord event;
  58. RgnHandle cursorRgn;
  59. cursorRgn = NewRgn();
  60. while(1){
  61. if(WaitNextEvent(everyEvent, &event, MAXLONG, cursorRgn)){
  62. if(FrontWindow() != nil
  63. && event.what != diskEvt
  64. && (event.what != keyDown || (event.modifiers & cmdKey) == 0)
  65. && IsDialogEvent(&event))
  66. {
  67. if(DialogSelect(&event, &pdlg, &sItem)){
  68. // REVIEW: replace following with an assertion
  69. if(pdlg != g_pcalc->m_pdlg)
  70. Debugger();
  71. g_pcalc->m_arith.ButtonPush(sItem);
  72. }
  73. }else{
  74. DoEvent(&event);
  75. }
  76. }
  77. if (g_fQuit)
  78. Quit();
  79. }
  80. }
  81. void
  82. DoEvent(EventRecord *pevent)
  83. {
  84. char key;
  85. short part;
  86. WindowPtr window;
  87. switch(pevent->what){
  88. case mouseDown:
  89. part = FindWindow(pevent->where, &window);
  90. switch(part){
  91. case inMenuBar:
  92. AdjustMenus();
  93. DoMenuCommand(MenuSelect(pevent->where));
  94. break;
  95. case inSysWindow: /* let the system handle the mouseDown */
  96. SystemClick(pevent, window);
  97. break;
  98. case inContent:
  99. if(window != FrontWindow()){
  100. SelectWindow(window);
  101. }
  102. break;
  103. case inDrag:
  104. DragWindow(window, pevent->where, &qd.screenBits.bounds);
  105. break;
  106. }
  107. break;
  108. case keyDown:
  109. case autoKey: /* check for menukey equivalents */
  110. key = (char)(pevent->message & charCodeMask);
  111. if(pevent->modifiers & cmdKey){ /* Command key down */
  112. if(pevent->what == keyDown){
  113. /* enable/disable/check menu items properly */
  114. AdjustMenus();
  115. DoMenuCommand(MenuKey(key));
  116. }
  117. }
  118. break;
  119. case kHighLevelEvent:
  120. AEProcessAppleEvent(pevent);
  121. break;
  122. }
  123. }
  124. void
  125. Enable(MenuHandle hmenu, short sItem, Boolean fEnable)
  126. {
  127. if(fEnable)
  128. EnableItem(hmenu, sItem);
  129. else
  130. DisableItem(hmenu, sItem);
  131. }
  132. void
  133. AdjustMenus()
  134. {
  135. Boolean fIsDA;
  136. MenuHandle hmenu;
  137. fIsDA = IsDAWindow(FrontWindow());
  138. /* we can allow desk accessories to be closed from the menu */
  139. hmenu = GetMHandle(mFile);
  140. Enable(hmenu, iClose, fIsDA);
  141. hmenu = GetMHandle(mEdit);
  142. Enable(hmenu, iUndo, fIsDA);
  143. Enable(hmenu, iCut, fIsDA);
  144. Enable(hmenu, iCopy, fIsDA);
  145. Enable(hmenu, iPaste, fIsDA);
  146. Enable(hmenu, iClear, fIsDA);
  147. }
  148. void
  149. DoMenuCommand(long menuResult)
  150. {
  151. short menuID; /* the resource ID of the selected menu */
  152. short menuItem; /* the item number of the selected menu */
  153. Str255 daName;
  154. menuID = HiWord(menuResult);
  155. menuItem = LoWord(menuResult);
  156. switch(menuID){
  157. case mApple:
  158. switch(menuItem){
  159. case iAbout: /* bring up alert for About */
  160. Alert(rAboutAlert, nil);
  161. break;
  162. default:
  163. GetMenuItemText(GetMHandle(mApple), menuItem, daName);
  164. OpenDeskAcc(daName);
  165. break;
  166. }
  167. break;
  168. case mFile:
  169. switch(menuItem){
  170. case iQuit:
  171. Quit();
  172. break;
  173. }
  174. break;
  175. case mEdit:
  176. SystemEdit(menuItem-1);
  177. break;
  178. }
  179. HiliteMenu(0);
  180. }
  181. #if defined(_MSC_VER)
  182. OSErr pascal
  183. #else
  184. pascal OSErr
  185. #endif
  186. RemoteLowLevelEvt(AppleEvent theAppEvt, AppleEvent reply, long HandlerRefCon)
  187. {
  188. long cb;
  189. OSErr err;
  190. DescType descType;
  191. EventRecord event;
  192. UNUSED(reply);
  193. UNUSED(HandlerRefCon);
  194. err = AEGetKeyPtr(
  195. &theAppEvt,
  196. keyDirectObject,
  197. typeWildCard,
  198. &descType,
  199. (Ptr)&event, sizeof(event), &cb);
  200. if(err != noErr)
  201. return err;
  202. DoEvent(&event);
  203. return noErr;
  204. }
  205. void
  206. Init()
  207. {
  208. Handle menuBar;
  209. MaxApplZone();
  210. InitGraf((Ptr)&qd.thePort);
  211. InitFonts();
  212. InitWindows();
  213. InitMenus();
  214. TEInit();
  215. InitDialogs(nil);
  216. InitCursor();
  217. FlushEvents(everyEvent, 0);
  218. #ifndef _PPCMAC
  219. if (InitOleManager(0) != NOERROR)
  220. Fatal((ConstStr255Param)"\pCould not initialize OLE Applet");
  221. g_fInitLibraryManager = true;
  222. #endif //!_PPCMAC
  223. if(InitOle() != NOERROR)
  224. Fatal((ConstStr255Param)"\pUnable to Initialize Ole");
  225. g_fInitOle = true;
  226. if(AEInstallEventHandler('OLE2', 'EVNT', (EventHandlerProcPtr)RemoteLowLevelEvt, 0, false) != noErr)
  227. Fatal((ConstStr255Param)"\pUnable to install handler");
  228. if((g_pcalc->m_pdlg = GetNewDialog(rCalc, nil, (WindowPtr)-1)) == nil)
  229. Fatal((ConstStr255Param)"\pUnable to create dialog");
  230. if((menuBar = GetNewMBar(rMenuBar)) == nil)
  231. Fatal((ConstStr255Param)"\pUnable to load menu bar");
  232. SetMenuBar(menuBar);
  233. DisposHandle(menuBar);
  234. AddResMenu(GetMHandle(mApple), 'DRVR'); /* add DA names to Apple menu */
  235. DrawMenuBar();
  236. }
  237. void
  238. Quit()
  239. {
  240. if(g_fInitOle)
  241. UninitOle();
  242. #ifndef _PPCMAC
  243. if(g_fInitLibraryManager)
  244. UninitOleManager(); // clean up applet
  245. #endif //_PPCMAC
  246. ExitToShell();
  247. }
  248. /* display fatal error alert, and exit */
  249. void
  250. Fatal(ConstStr255Param msg)
  251. {
  252. SetCursor(&qd.arrow);
  253. ParamText(msg, (ConstStr255Param)"\p", (ConstStr255Param)"\p", (ConstStr255Param)"\p");
  254. Alert(rUserAlert, nil);
  255. Quit();
  256. }