home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / macintosh-c / macc-carbon-demos-nonbinhex.sit / macc-carbon-demos-nonbinhex / chap26-demo / MLTEMenus.c < prev    next >
C/C++ Source or Header  |  2001-05-31  |  43KB  |  1,530 lines

  1. // *******************************************************************************************
  2. // MTLETextEditor.h                                                        CLASSIC EVENT MODEL
  3. // *******************************************************************************************
  4. //
  5. // This program demonstrates the use of the Multilingual Text Engine API to create a basic
  6. // multi-styled text editor.  New documents created by the program are created and saved as
  7. // Textension ('txtn') documents.  Existing 'TEXT' documents and Unicode ('utxt') documents
  8. // are saved in the original format.  In the case of 'TEXT' documents, style information is
  9. // saved in a 'styl' resource. 
  10. //
  11. // The program utilises the following resources:
  12. //
  13. // •    A 'plst' resource.
  14. //
  15. // •    An 'MBAR' resource, and 'MENU' resources for Apple, File, Edit, Size, Style, Colour,
  16. //        and Justification (preload, non-purgeable).  
  17. //
  18. // •    A 'WIND' resource (purgeable) (initially not visible).  
  19. //
  20. // •    A 'STR ' resource (purgeable) containing the "missing application name" string, which is
  21. //        copied to all document files created by the program.
  22. //
  23. // •    'STR#' resources (purgeable) containing error strings, the application's name (for
  24. //        certain Navigation Services functions), and strings for the Edit menu Undo and Redo
  25. //        items.
  26. //
  27. // •    A 'kind' resource (purgeable) describing file types, which is used by Navigation 
  28. //        Services to    build the native file types section of the Show pop-up menu in the Open
  29. //        dialog box.
  30. //
  31. // •    An 'open' resource (purgeable) containing the file type list for the Open dialog box.
  32. //
  33. // •    The 'BNDL' resource (non-purgeable), 'FREF' resources (non-purgeable), signature
  34. //        resource (non-purgeable), and icon family resources (purgeable), required to support the
  35. //        built application.
  36. //
  37. // •    A 'SIZE' resource with the acceptSuspendResumeEvents, canBackground, 
  38. //        doesActivateOnFGSwitch, and isHighLevelEventAware flags set.
  39. //
  40. // *******************************************************************************************
  41.  
  42. // ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
  43.  
  44. #include <Carbon.h>
  45.  
  46. // …………………………………………………………………………………………………………………………………………………………………………………………………………………………… defines
  47.  
  48. #define rMenubar                                    128
  49. #define mAppleApplication                    128
  50. #define  iAbout                                        1
  51. #define mFile                                            129
  52. #define  iNew                                            1
  53. #define  iOpen                                        2
  54. #define  iClose                                        4
  55. #define  iSave                                        5
  56. #define  iSaveAs                                    6
  57. #define  iRevert                                    7
  58. #define  iPageSetup                                9
  59. #define  iPrint                                        10
  60. #define  iQuit                                        12
  61. #define mEdit                                            130
  62. #define  iUndo                                        1
  63. #define  iRedo                                        2
  64. #define  iCut                                            4
  65. #define  iCopy                                        5
  66. #define  iPaste                                        6
  67. #define  iClear                                        7
  68. #define  iSelectAll                                8
  69. #define mFont                                            131
  70. #define mSize                                            132
  71. #define  iTwelve                                    4
  72. #define mStyle                                        133
  73. #define  iPlain                                        1
  74. #define  iBold                                        3
  75. #define  iUnderline                                5
  76. #define mColour                                        134
  77. #define  iBlack                                        4
  78. #define  iColourPicker                        6
  79. #define mJustification                        135
  80. #define  iDefault                                    1
  81. #define  iLeft                                        2
  82. #define  iForceFull                                6
  83. #define mWindow                                        136
  84. #define mFirstHierarchical                160
  85.  
  86. #define rNewWindow                                128
  87. #define rAboutDialog                            128
  88. #define rErrorStrings                            128
  89. #define  eInstallHandler                    1000
  90. #define  eMaxWindows                            1001
  91. #define  eCantFindFinderProcess        1002
  92. #define rMiscellaneousStrings            129
  93. #define  sApplicationName                    1
  94. #define rOpenResource                            128
  95.  
  96. #define kMaxWindows                                8
  97. #define kOpen                                            0
  98. #define kPrint                                        1
  99. #define kFileCreator              'bbJk'
  100. #define MAX_UINT32                                0xFFFFFFFF
  101. #define MIN(a,b)                                     ((a) < (b) ? (a) : (b))
  102. #define topLeft(r)                                (((Point *) &(r))[0])
  103.  
  104. #define kATSUCGContextTag                    32767L
  105.  
  106. // …………………………………………………………………………………………………………………………………………………………………………………………… function prototypes
  107.  
  108. void            main                                                    (void);
  109. void            doPreliminaries                                (void);
  110. void            doInitialiseMTLE                            (void);
  111. void            doInstallAEHandlers                        (void);
  112. void            eventLoop                                            (void);
  113. UInt32        doGetSleepTime                                (void);
  114. void            doIdle                                                (void);
  115. void            doEvents                                            (EventRecord *);
  116. void            doMouseDown                                        (EventRecord *);
  117. void            doBringFinderToFront                    (void);
  118. OSStatus    doFindProcess                                    (OSType,OSType,ProcessSerialNumber *);
  119. void            doActivate                                        (EventRecord *);
  120. void            doUpdate                                            (EventRecord *);
  121. Boolean        isApplicationWindow                        (WindowRef,TXNObject *);
  122. void            doAboutDialog                                    (void);
  123. void            doSynchroniseFiles                        (void);
  124. OSStatus    openAppEventHandler                        (AppleEvent *,AppleEvent *,SInt32);
  125. OSStatus    reopenAppEventHandler                    (AppleEvent *,AppleEvent *,SInt32);
  126. OSStatus    openAndPrintDocsEventHandler    (AppleEvent *,AppleEvent *,SInt32);
  127. OSStatus    quitAppEventHandler                        (AppleEvent *,AppleEvent *,SInt32);
  128. OSStatus    doHasGotRequiredParams                (AppleEvent *);
  129. void            doErrorAlert                                    (SInt16);
  130. void            doCopyPString                                    (Str255,Str255);
  131. void            doConcatPStrings                            (Str255,Str255);
  132.  
  133. void            doEnableDisableMenus                    (Boolean);
  134. void            doAdjustAndPrepareMenus                (void);
  135. void            doAdjustFileMenu                            (MenuRef,WindowRef);
  136. void            doAdjustEditMenu                            (MenuRef,WindowRef);
  137. void            doPrepareFontMenu                            (WindowRef);
  138. void            doPrepareSizeMenu                            (MenuRef,WindowRef);
  139. void            doPrepareStyleMenu                        (MenuRef,WindowRef);
  140. void            doPrepareColourMenu                        (MenuRef,WindowRef);
  141. Boolean        isEqualRGB                                        (RGBColor *,RGBColor *);
  142. void            doPrepareJustificationMenu        (MenuRef,WindowRef);
  143. void            doMenuChoice                                    (SInt32);
  144. void            doFileMenuChoice                            (MenuItemIndex,WindowRef);
  145. void            doEditMenuChoice                            (MenuItemIndex,WindowRef);
  146. void            doFontMenuChoice                            (MenuID,MenuItemIndex,WindowRef);
  147. void            doSizeMenuChoice                            (MenuItemIndex,WindowRef);
  148. void            doStyleMenuChoice                            (MenuItemIndex,WindowRef);
  149. void            doColourMenuChoice                        (MenuItemIndex,WindowRef);
  150. void            doJustificationMenuChoice            (MenuItemIndex,WindowRef);
  151.  
  152. OSStatus    doNewCommand                                    (void);
  153. OSStatus    doOpenCommand                                    (void);
  154. OSStatus    doCloseCommand                                (NavAskSaveChangesAction);
  155. OSStatus    doSaveCommand                                    (void);
  156. OSStatus    doSaveAsCommand                                (void);
  157. OSStatus    doRevertCommand                                (void);
  158. OSStatus    doQuitCommand                                    (NavAskSaveChangesAction);
  159. OSStatus    doNewDocWindow                                (WindowRef *,FSSpec *,TXNFileType);
  160. OSStatus    doOpenFile                                        (FSSpec,OSType);
  161. void            doCloseWindow                                    (WindowRef,TXNObject);
  162. OSStatus    doWriteFile                                        (WindowRef,Boolean);
  163. OSStatus    doCopyResources                                (FSSpec,TXNFileType,Boolean);
  164. OSStatus    doCopyAResource                                (ResType,SInt16,SInt16,SInt16);
  165. void            navEventFunction                            (NavEventCallbackMessage,NavCBRecPtr,
  166.                                                                                  NavCallBackUserData);
  167.  
  168. // *******************************************************************************************
  169. // MLTETextEditor.c
  170. // *******************************************************************************************
  171.  
  172. // ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
  173.  
  174. #include "MLTETextEditor.h"
  175.  
  176. // …………………………………………………………………………………………………………………………………………………………………………………………………… global variables
  177.  
  178. SInt16                        gAppResFileRefNum;
  179. Boolean                        gRunningOnX = false;
  180. Boolean                        gDone;
  181. TXNFontMenuObject    gTXNFontMenuObject;
  182. RgnHandle                    gCursorRgnHdl;
  183. extern SInt16            gCurrentNumberOfWindows;
  184.  
  185. // ************************************************************************************** main
  186.  
  187. void  main(void)
  188. {
  189.     MenuBarHandle    menubarHdl;
  190.     SInt32                response;
  191.     MenuRef                menuRef;
  192.     OSStatus            osStatus = noErr;
  193.  
  194.     // ……………………………………………………………………………………………………………………………………………………………………………………………… do preliminaries
  195.  
  196.     doPreliminaries();
  197.  
  198.     // ………………………………………………………………………………………… save application's resource file file reference number
  199.  
  200.     gAppResFileRefNum = CurResFile();
  201.  
  202.     // ……………………………………………………………………………………………………………………………………………………………………… set up menu bar and menus
  203.     
  204.     menubarHdl = GetNewMBar(rMenubar);
  205.     if(menubarHdl == NULL)
  206.         doErrorAlert(MemError());
  207.     SetMenuBar(menubarHdl);
  208.  
  209.     CreateStandardWindowMenu(0,&menuRef);
  210.     SetMenuID(menuRef,mWindow);
  211.     InsertMenu(menuRef,0);
  212.     DeleteMenuItem(menuRef,1);
  213.  
  214.     Gestalt(gestaltMenuMgrAttr,&response);
  215.     if(response & gestaltMenuMgrAquaLayoutMask)
  216.     {
  217.         menuRef = GetMenuRef(mFile);
  218.         if(menuRef != NULL)
  219.         {
  220.             DeleteMenuItem(menuRef,iQuit);
  221.             DeleteMenuItem(menuRef,iQuit - 1);
  222.         }
  223.  
  224.         gRunningOnX = true;
  225.     }
  226.  
  227.     // ……………………………………………………………………………………………………………… build hierarchical font menu and draw menu bar
  228.  
  229.     menuRef = GetMenuRef(mFont);
  230.     osStatus = TXNNewFontMenuObject(menuRef,mFont,mFirstHierarchical,&gTXNFontMenuObject);
  231.     if(osStatus != noErr)
  232.         doErrorAlert(osStatus);
  233.  
  234.     DrawMenuBar();
  235.  
  236.     // ……………………………………………………………………………………………………………………………………… install required Apple event handlers
  237.  
  238.     doInstallAEHandlers();
  239.  
  240.     // ……………………………………………………………………………………………………………………………………………………………………………………………… enter event loop
  241.  
  242.     eventLoop();
  243. }
  244.  
  245. // *************************************************************************** doPreliminaries
  246.  
  247. void  doPreliminaries(void)
  248. {
  249.     MoreMasterPointers(960);
  250.     InitCursor();
  251.     FlushEvents(everyEvent,0);
  252.  
  253.     doInitialiseMTLE();
  254. }
  255.  
  256. // ************************************************************************** doInitializeMTLE
  257.  
  258. void  doInitialiseMTLE(void)
  259. {
  260.     TXNMacOSPreferredFontDescription    defaultFont[1];
  261.     OSStatus                                                    osStatus = noErr;
  262.     SInt16                                                        fontID;
  263.  
  264.     GetFNum("\pNew York",&fontID);
  265.  
  266.     defaultFont[0].fontID            = fontID;    
  267.     defaultFont[0].pointSize    = 0x000C0000;
  268.     defaultFont[0].fontStyle    = kTXNDefaultFontStyle;
  269.     defaultFont[0].encoding        = kTXNSystemDefaultEncoding;
  270.  
  271.     osStatus = TXNInitTextension(&defaultFont[0],1,kTXNWantMoviesMask);
  272.     if(osStatus != noErr)
  273.         doErrorAlert(osStatus);
  274. }
  275.  
  276. // *********************************************************************** doInstallAEHandlers
  277.  
  278. void  doInstallAEHandlers(void)
  279. {
  280.     OSStatus    osStatus = noErr;
  281.  
  282.     osStatus = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  283.                                                     NewAEEventHandlerUPP((AEEventHandlerProcPtr) openAppEventHandler),
  284.                                                     0L,false);
  285.     if(osStatus != noErr)    doErrorAlert(eInstallHandler);
  286.  
  287.     osStatus = AEInstallEventHandler(kCoreEventClass,kAEReopenApplication,
  288.                                                     NewAEEventHandlerUPP((AEEventHandlerProcPtr) reopenAppEventHandler),
  289.                                                     0L,false);
  290.     if(osStatus != noErr)    doErrorAlert(eInstallHandler);
  291.  
  292.     osStatus = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  293.                                     NewAEEventHandlerUPP((AEEventHandlerProcPtr) openAndPrintDocsEventHandler),
  294.                                                     kOpen,false);
  295.     if(osStatus != noErr)    doErrorAlert(eInstallHandler);
  296.  
  297.     osStatus = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
  298.                                     NewAEEventHandlerUPP((AEEventHandlerProcPtr) openAndPrintDocsEventHandler),
  299.                                                     kPrint,false);
  300.     if(osStatus != noErr)    doErrorAlert(eInstallHandler);
  301.  
  302.     osStatus = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  303.                                                     NewAEEventHandlerUPP((AEEventHandlerProcPtr) quitAppEventHandler),
  304.                                                     0L,false);
  305.     if(osStatus != noErr)    doErrorAlert(eInstallHandler);
  306. }
  307.  
  308. // ********************************************************************************* eventLoop
  309.  
  310. void  eventLoop(void)
  311. {
  312.     EventRecord eventStructure;
  313.  
  314.     gDone = false;
  315.     gCursorRgnHdl = NewRgn();
  316.  
  317.     while(!gDone)
  318.     {
  319.         if(WaitNextEvent(everyEvent,&eventStructure,doGetSleepTime(),gCursorRgnHdl))
  320.             doEvents(&eventStructure);
  321.         else
  322.         {
  323.             if(eventStructure.what == nullEvent)
  324.             {
  325.                 doIdle();
  326.                 doSynchroniseFiles();
  327.             }
  328.         }
  329.     }
  330. }
  331.  
  332. // **************************************************************************** doGetSleepTime
  333.  
  334. UInt32  doGetSleepTime(void)
  335. {
  336.     WindowRef    windowRef;
  337.     UInt32        sleepTime;
  338.     TXNObject    txnObject = NULL;
  339.  
  340.     windowRef = FrontWindow();
  341.  
  342.     if(isApplicationWindow(windowRef,&txnObject))
  343.         sleepTime = TXNGetSleepTicks(txnObject);
  344.     else
  345.         sleepTime = GetCaretTime();
  346.  
  347.     return sleepTime;
  348. }
  349.  
  350. // ************************************************************************************ doIdle
  351.  
  352. void  doIdle(void)
  353. {
  354.     WindowRef    windowRef;
  355.     TXNObject    txnObject = NULL;
  356.  
  357.     windowRef = FrontWindow();
  358.     if(isApplicationWindow(windowRef,&txnObject))
  359.     {
  360.         if(TXNGetChangeCount(txnObject))
  361.             SetWindowModified(windowRef,true);
  362.     }
  363. }
  364.  
  365. // ********************************************************************************** doEvents
  366.  
  367. void    doEvents(EventRecord *eventStrucPtr)
  368. {
  369.     WindowRef    windowRef;
  370.     TXNObject    txnObject = NULL;
  371.  
  372.     switch(eventStrucPtr->what)
  373.     {
  374.         case kHighLevelEvent:
  375.             AEProcessAppleEvent(eventStrucPtr);
  376.             break;
  377.  
  378.         case mouseDown:
  379.             doMouseDown(eventStrucPtr);
  380.             break;
  381.     
  382.         case keyDown:
  383.             if(eventStrucPtr->modifiers & cmdKey)
  384.             {
  385.                 doAdjustAndPrepareMenus();
  386.                 doMenuChoice(MenuEvent(eventStrucPtr));
  387.             }
  388.             break;
  389.  
  390.         case updateEvt:
  391.             doUpdate(eventStrucPtr);
  392.             break;
  393.  
  394.         case activateEvt:
  395.             doActivate(eventStrucPtr);
  396.             break;
  397.  
  398.         case osEvt:
  399.             switch((eventStrucPtr->message >> 24) & 0x000000FF)
  400.             {
  401.                 case suspendResumeMessage:
  402.                     if(eventStrucPtr->message & resumeFlag)
  403.                         SetThemeCursor(kThemeArrowCursor);
  404.                     break;
  405.                     
  406.                 case mouseMovedMessage:
  407.                     windowRef = FrontWindow();
  408.                     if(isApplicationWindow(windowRef,&txnObject))
  409.                         TXNAdjustCursor(txnObject,gCursorRgnHdl);
  410.             }
  411.             break;
  412.     }
  413. }
  414.  
  415. // ******************************************************************************* doMouseDown
  416.  
  417. void    doMouseDown(EventRecord *eventStrucPtr)
  418. {
  419.     WindowRef                windowRef;
  420.     WindowPartCode    partCode;
  421.     OSStatus                osStatus    = noErr;
  422.     TXNObject             txnObject = NULL;
  423.     Boolean                    handled        = false;
  424.     SInt32                    itemSelected;
  425.  
  426.     partCode = FindWindow(eventStrucPtr->where,&windowRef);
  427.  
  428.     switch(partCode)
  429.     {
  430.         case inMenuBar:
  431.             doAdjustAndPrepareMenus();
  432.             doMenuChoice(MenuSelect(eventStrucPtr->where));
  433.             break;
  434.  
  435.         case inContent:
  436.             if(windowRef != FrontWindow())
  437.                 SelectWindow(windowRef);
  438.             else
  439.             {
  440.                 if(isApplicationWindow(windowRef,&txnObject))
  441.                     TXNClick(txnObject,eventStrucPtr);
  442.             }  
  443.             break;
  444.  
  445.         case inGoAway:
  446.             if(TrackGoAway(windowRef,eventStrucPtr->where))
  447.                 doCloseCommand(kNavSaveChangesClosingDocument);
  448.             break;
  449.  
  450.         case inProxyIcon:
  451.             osStatus = TrackWindowProxyDrag(windowRef,eventStrucPtr->where);
  452.             if(osStatus == errUserWantsToDragWindow)
  453.                 handled = false;
  454.             else if(osStatus == noErr)
  455.                 handled = true;
  456.  
  457.         case inDrag:
  458.             if(!handled)
  459.             {
  460.                 if(IsWindowPathSelectClick(windowRef,eventStrucPtr))
  461.                 {
  462.                     if(WindowPathSelect(windowRef,NULL,&itemSelected) == noErr)
  463.                     {
  464.                         if(LoWord(itemSelected) > 1)
  465.                             doBringFinderToFront();
  466.                     }
  467.  
  468.                     handled = true;
  469.                 }
  470.             }
  471.             if(!handled)
  472.                 DragWindow(windowRef,eventStrucPtr->where,NULL);
  473.  
  474.             if(isApplicationWindow(windowRef,&txnObject))
  475.                 TXNAdjustCursor(txnObject,gCursorRgnHdl);
  476.  
  477.             break;
  478.  
  479.         case inGrow:
  480.             if(isApplicationWindow(windowRef,&txnObject))
  481.             {
  482.                 TXNGrowWindow(txnObject,eventStrucPtr);
  483.                 TXNAdjustCursor(txnObject,gCursorRgnHdl);
  484.             }
  485.             break;
  486.  
  487.         case inZoomIn:
  488.         case inZoomOut:
  489.             if(TrackBox(windowRef,eventStrucPtr->where,partCode))
  490.             {
  491.                 if(isApplicationWindow(windowRef,&txnObject))
  492.                 {
  493.                     TXNZoomWindow(txnObject,partCode);
  494.                     TXNAdjustCursor(txnObject,gCursorRgnHdl);
  495.                 }
  496.             }
  497.             break;
  498.     }
  499. }
  500.  
  501. // ********************************************************************** doBringFinderToFront
  502.  
  503. void  doBringFinderToFront(void)
  504. {
  505.     ProcessSerialNumber    finderProcess;
  506.  
  507.     if(doFindProcess('MACS','FNDR',&finderProcess) == noErr)
  508.         SetFrontProcess(&finderProcess);
  509.     else
  510.         doErrorAlert(eCantFindFinderProcess);
  511. }
  512.  
  513. // ***************************************************************************** doFindProcess
  514.  
  515. OSStatus  doFindProcess(OSType creator,OSType type,ProcessSerialNumber *outProcSerNo)
  516. {    
  517.     ProcessSerialNumber    procSerialNo;
  518.     ProcessInfoRec            procInfoStruc;
  519.     OSStatus                        osStatus = noErr;
  520.  
  521.     procSerialNo.highLongOfPSN = 0;
  522.     procSerialNo.lowLongOfPSN  = kNoProcess;
  523.  
  524.     procInfoStruc.processInfoLength    = sizeof(ProcessInfoRec);
  525.     procInfoStruc.processName                = NULL;
  526.     procInfoStruc.processAppSpec        = NULL;
  527.     procInfoStruc.processLocation        = NULL;
  528.  
  529.     while(true)
  530.     {
  531.         osStatus = GetNextProcess(&procSerialNo);
  532.         if(osStatus != noErr)
  533.             break;
  534.  
  535.         osStatus = GetProcessInformation(&procSerialNo,&procInfoStruc);
  536.         if(osStatus != noErr)
  537.             break;
  538.         if((procInfoStruc.processSignature == creator) && (procInfoStruc.processType == type))
  539.             break;
  540.     }
  541.  
  542.     *outProcSerNo = procSerialNo;
  543.  
  544.     return osStatus;
  545. }
  546.  
  547. // ******************************************************************************** doActivate
  548.  
  549. void  doActivate(EventRecord *eventStrucPtr)
  550. {
  551.     WindowRef        windowRef;
  552.     TXNObject        txnObject = NULL;
  553.     Boolean            becomingActive;
  554.     TXNFrameID     txnFrameID = 0;
  555.  
  556.     windowRef = (WindowRef) eventStrucPtr->message;
  557.  
  558.     if(isApplicationWindow(windowRef,&txnObject))
  559.     {
  560.         becomingActive = ((eventStrucPtr->modifiers & activeFlag) == activeFlag);
  561.         GetWindowProperty(windowRef,kFileCreator,'tFRM',sizeof(TXNFrameID),NULL,&txnFrameID);
  562.         
  563.         if(becomingActive)
  564.             TXNActivate(txnObject,txnFrameID,becomingActive);
  565.         else
  566.             TXNActivate(txnObject,txnFrameID,becomingActive);
  567.  
  568.         TXNFocus(txnObject,becomingActive);
  569.     }
  570. }
  571.  
  572. // ********************************************************************************** doUpdate
  573.  
  574. void  doUpdate(EventRecord *eventStrucPtr)
  575. {
  576.     WindowRef    windowRef;
  577.     GrafPtr        oldPort;
  578.     TXNObject    txnObject = NULL;
  579.  
  580.     windowRef = (WindowRef) eventStrucPtr->message;
  581.  
  582.     GetPort(&oldPort);
  583.     SetPortWindowPort(windowRef);
  584.  
  585.     if(isApplicationWindow(windowRef,&txnObject))
  586.         TXNUpdate(txnObject);
  587.  
  588.     SetPort(oldPort);
  589. }
  590.  
  591. // *********************************************************************** isApplicationWindow
  592.  
  593. Boolean  isApplicationWindow(WindowRef windowRef,TXNObject *txnObject)
  594. {
  595.     OSStatus osStatus = noErr;
  596.  
  597.     osStatus = GetWindowProperty(windowRef,kFileCreator,'tOBJ',sizeof(TXNObject),NULL,
  598.                                                              txnObject);
  599.  
  600.     return (windowRef != NULL) && (GetWindowKind(windowRef) == kApplicationWindowKind);
  601. }
  602.  
  603. // ***************************************************************************** doAboutDialog
  604.  
  605. void  doAboutDialog(void)
  606. {
  607.     DialogRef    dialogRef;
  608.     SInt16        itemHit;
  609.  
  610.     dialogRef = GetNewDialog(rAboutDialog,NULL,(WindowRef) -1);
  611.     ModalDialog(NULL,&itemHit);
  612.     DisposeDialog(dialogRef);
  613. }
  614.  
  615. // ************************************************************************ doSynchroniseFiles
  616.  
  617. void  doSynchroniseFiles(void)
  618. {
  619.     UInt32                currentTicks;
  620.     WindowRef            windowRef;
  621.     static UInt32    nextSynchTicks = 0;
  622.     OSStatus            hasNoAliasHdl = noErr;
  623.     Boolean                aliasChanged;
  624.     AliasHandle        aliasHdl = NULL;
  625.     FSSpec                newFSSpec;
  626.     OSStatus            osStatus = noErr;
  627.     SInt16                trashVRefNum;
  628.     SInt32                trashDirID;
  629.     TXNObject            txnObject = NULL;
  630.  
  631.     currentTicks = TickCount();
  632.     windowRef    = FrontWindow();
  633.  
  634.     if(currentTicks > nextSynchTicks)
  635.     {
  636.         while(windowRef != NULL)
  637.         {
  638.             hasNoAliasHdl = GetWindowProperty(windowRef,kFileCreator,'tALH',sizeof(AliasHandle),
  639.                                                                                 NULL,&aliasHdl);
  640.             if(hasNoAliasHdl)
  641.                 break;
  642.  
  643.             aliasChanged = false;
  644.             ResolveAlias(NULL,aliasHdl,&newFSSpec,&aliasChanged);
  645.             if(aliasChanged)
  646.             {
  647.                 SetWindowProperty(windowRef,kFileCreator,'FiSp',sizeof(FSSpec),&newFSSpec);
  648.                 SetWTitle(windowRef,newFSSpec.name);
  649.             }
  650.  
  651.             osStatus = FindFolder(kUserDomain,kTrashFolderType,kDontCreateFolder,
  652.                                                         &trashVRefNum,&trashDirID);
  653.  
  654.             if(osStatus == noErr)
  655.             {
  656.                 do
  657.                 {
  658.                     if(newFSSpec.parID == fsRtParID)
  659.                         break;
  660.  
  661.                     if((newFSSpec.vRefNum == trashVRefNum) && (newFSSpec.parID == trashDirID))
  662.                     {
  663.                         GetWindowProperty(windowRef,kFileCreator,'tOBJ',sizeof(TXNObject),NULL,
  664.                                                             &txnObject);
  665.                         TXNDeleteObject(txnObject);
  666.                         DisposeWindow(windowRef);
  667.                         gCurrentNumberOfWindows --;
  668.                         break;
  669.                     }
  670.                 } while(FSMakeFSSpec(newFSSpec.vRefNum,newFSSpec.parID,"\p",&newFSSpec) == noErr);
  671.             }
  672.  
  673.             windowRef = GetNextWindow(windowRef);
  674.         }
  675.  
  676.         nextSynchTicks = currentTicks + 15;
  677.     }
  678. }
  679.  
  680. // *********************************************************************** openAppEventHandler
  681.  
  682. OSStatus  openAppEventHandler(AppleEvent *appEvent,AppleEvent *reply,SInt32 handlerRefCon)
  683. {
  684.     OSStatus    osStatus = noErr;
  685.  
  686.     osStatus = doHasGotRequiredParams(appEvent);
  687.     if(osStatus == noErr)
  688.         osStatus = doNewCommand();
  689.  
  690.     return osStatus;
  691. }
  692.  
  693. // ********************************************************************* reopenAppEventHandler
  694.  
  695. OSStatus  reopenAppEventHandler(AppleEvent *appEvent,AppleEvent *reply,
  696.                                                                 SInt32 handlerRefCon)
  697. {
  698.     OSStatus    osStatus = noErr;
  699.  
  700.     osStatus = doHasGotRequiredParams(appEvent);
  701.     if(osStatus == noErr)
  702.         if(!FrontWindow())
  703.             osStatus = doNewCommand();
  704.  
  705.     return osStatus;
  706. }
  707.  
  708. // ************************************************************** openAndPrintDocsEventHandler
  709.  
  710. OSStatus  openAndPrintDocsEventHandler(AppleEvent *appEvent,AppleEvent *reply,
  711.                                                                              SInt32 handlerRefcon)
  712. {
  713.     FSSpec            fileSpec;
  714.     AEDescList    docList;
  715.     OSStatus        osStatus, ignoreErr;
  716.     SInt32            index, numberOfItems;
  717.     Size                actualSize;
  718.     AEKeyword        keyWord;
  719.     DescType        returnedType;
  720.     FInfo                fileInfo;
  721.     TXNObject        txnObject;
  722.  
  723.     osStatus = AEGetParamDesc(appEvent,keyDirectObject,typeAEList,&docList);
  724.  
  725.     if(osStatus == noErr)
  726.     {
  727.         osStatus = doHasGotRequiredParams(appEvent);
  728.         if(osStatus == noErr)
  729.         {
  730.             osStatus = AECountItems(&docList,&numberOfItems);
  731.             if(osStatus == noErr)
  732.             {
  733.                 for(index=1;index<=numberOfItems;index++)
  734.                 {
  735.                     osStatus = AEGetNthPtr(&docList,index,typeFSS,&keyWord,&returnedType,
  736.                                                                  &fileSpec,sizeof(fileSpec),&actualSize);
  737.                     if(osStatus == noErr)
  738.                     {
  739.                         osStatus = FSpGetFInfo(&fileSpec,&fileInfo);
  740.                         if(osStatus == noErr)
  741.                         {
  742.                             if(osStatus = doOpenFile(fileSpec,fileInfo.fdType))
  743.                                 doErrorAlert(osStatus);
  744.                             
  745.                             if(osStatus == noErr && handlerRefcon == kPrint)
  746.                             {
  747.                                 if(isApplicationWindow(FrontWindow(),&txnObject))
  748.                                 {
  749.                                     if(osStatus = TXNPrint(txnObject))
  750.                                         doErrorAlert(osStatus);
  751.                                         
  752.                                     if(osStatus = doCloseCommand(kNavSaveChangesOther))
  753.                                         doErrorAlert(osStatus);
  754.                                 }
  755.                             }
  756.                         }
  757.                     }
  758.                     else
  759.                         doErrorAlert(osStatus);
  760.                 }
  761.             }
  762.         }
  763.         else
  764.             doErrorAlert(osStatus);
  765.  
  766.         ignoreErr = AEDisposeDesc(&docList);
  767.     }
  768.     else
  769.         doErrorAlert(osStatus);
  770.  
  771.     return osStatus;
  772. }
  773.  
  774. // *********************************************************************** quitAppEventHandler
  775.  
  776. OSStatus  quitAppEventHandler(AppleEvent *appEvent,AppleEvent *reply,SInt32 handlerRefcon)
  777. {
  778.     OSStatus    osStatus = noErr;
  779.  
  780.     osStatus = doHasGotRequiredParams(appEvent);
  781.     if(osStatus == noErr)
  782.     {
  783.         while(FrontWindow())
  784.         {
  785.             osStatus = doCloseCommand(kNavSaveChangesQuittingApplication);
  786.  
  787.             if(osStatus != noErr && osStatus != kNavAskSaveChangesCancel)
  788.                 doErrorAlert(osStatus);
  789.             if(osStatus == kNavAskSaveChangesCancel)
  790.                 return noErr;
  791.         }
  792.     }
  793.  
  794.     gDone = true;
  795.  
  796.     return osStatus;
  797. }
  798.  
  799. // ******************************************************************** doHasGotRequiredParams
  800.  
  801. OSStatus  doHasGotRequiredParams(AppleEvent *appEvent)
  802. {
  803.     DescType    returnedType;
  804.     Size            actualSize;
  805.     OSStatus    osStatus = noErr;
  806.  
  807.     osStatus = AEGetAttributePtr(appEvent,keyMissedKeywordAttr,typeWildCard,&returnedType,
  808.                                                              NULL,0,&actualSize);
  809.     if(osStatus == errAEDescNotFound)
  810.         osStatus = noErr;
  811.     else if(osStatus == noErr)
  812.         osStatus = errAEParamMissed;
  813.  
  814.     return osStatus;
  815. }
  816.  
  817. // ****************************************************************************** doErrorAlert
  818.  
  819. void  doErrorAlert(SInt16 errorCode)
  820. {
  821.     Str255    errorString, theString;
  822.     SInt16    itemHit;
  823.  
  824.     if(errorCode == kATSUFontsMatched)
  825.         return;
  826.  
  827.     if(errorCode == eInstallHandler)
  828.         GetIndString(errorString,rErrorStrings,1);
  829.     else if(errorCode == eMaxWindows)
  830.         GetIndString(errorString,rErrorStrings,2);
  831.     else if(errorCode == eCantFindFinderProcess)
  832.         GetIndString(errorString,rErrorStrings,3);
  833.     else
  834.     {
  835.         GetIndString(errorString,rErrorStrings,4);
  836.         NumToString((SInt32) errorCode,theString);
  837.         doConcatPStrings(errorString,theString);
  838.     }
  839.  
  840.     if(errorCode != memFullErr)
  841.         StandardAlert(kAlertCautionAlert,errorString,NULL,NULL,&itemHit);
  842.     else
  843.     {
  844.         StandardAlert(kAlertStopAlert,errorString,NULL,NULL,&itemHit);
  845.         ExitToShell();
  846.     }
  847. }
  848.  
  849. // ***************************************************************************** doCopyPString
  850.  
  851. void  doCopyPString(Str255 sourceString,Str255 destinationString)
  852. {
  853.     SInt16    stringLength;
  854.  
  855.     stringLength = sourceString[0];
  856.     BlockMove(sourceString + 1,destinationString + 1,stringLength);
  857.     destinationString[0] = stringLength;
  858. }
  859.  
  860. // ************************************************************************** doConcatPStrings
  861.  
  862. void  doConcatPStrings(Str255 targetString,Str255 appendString)
  863. {
  864.     SInt16    appendLength;
  865.  
  866.     appendLength = MIN(appendString[0],255 - targetString[0]);
  867.  
  868.     if(appendLength > 0)
  869.     {
  870.         BlockMoveData(appendString+1,targetString+targetString[0]+1,(SInt32) appendLength);
  871.         targetString[0] += appendLength;
  872.     }
  873. }
  874.  
  875. // *******************************************************************************************
  876. // MLTEMenus.c
  877. // *******************************************************************************************
  878.  
  879. // ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
  880.  
  881. #include "MLTETextEditor.h"
  882.  
  883. // …………………………………………………………………………………………………………………………………………………………………………………………………… global variables
  884.  
  885. RGBColor                                    gCurrentColourPickerColour = { 0x0000,0x0000,0x0000 };
  886. extern SInt16                            gCurrentNumberOfWindows;
  887. extern TXNFontMenuObject    gTXNFontMenuObject;
  888. extern Boolean                        gDone;
  889.  
  890. // ********************************************************************** doEnableDisableMenus
  891.  
  892. void  doEnableDisableMenus(Boolean enableMenus)
  893. {
  894.     if(enableMenus)
  895.     {
  896.         EnableMenuItem(GetMenuRef(mEdit),0);
  897.         EnableMenuItem(GetMenuRef(mFont),0);
  898.         EnableMenuItem(GetMenuRef(mSize),0);
  899.         EnableMenuItem(GetMenuRef(mStyle),0);
  900.         EnableMenuItem(GetMenuRef(mColour),0);
  901.         EnableMenuItem(GetMenuRef(mJustification),0);
  902.         EnableMenuItem(GetMenuRef(mWindow),0);
  903.     }
  904.     else
  905.     {
  906.         DisableMenuItem(GetMenuRef(mEdit),0);
  907.         DisableMenuItem(GetMenuRef(mFont),0);
  908.         DisableMenuItem(GetMenuRef(mSize),0);
  909.         DisableMenuItem(GetMenuRef(mStyle),0);
  910.         DisableMenuItem(GetMenuRef(mColour),0);
  911.         DisableMenuItem(GetMenuRef(mJustification),0);
  912.         DisableMenuItem(GetMenuRef(mWindow),0);
  913.     }
  914. }
  915.  
  916. // ******************************************************************* doAdjustAndPrepareMenus
  917.  
  918. void  doAdjustAndPrepareMenus(void)
  919. {
  920.     WindowRef    windowRef;
  921.     
  922.     windowRef = FrontWindow();
  923.  
  924.     doAdjustFileMenu(GetMenuRef(mFile),windowRef);
  925.     doAdjustEditMenu(GetMenuRef(mEdit),windowRef);
  926.     doPrepareFontMenu(windowRef);
  927.     doPrepareSizeMenu(GetMenuRef(mSize),windowRef);
  928.     doPrepareStyleMenu(GetMenuRef(mStyle),windowRef);
  929.     doPrepareColourMenu(GetMenuRef(mColour),windowRef);
  930.     doPrepareJustificationMenu(GetMenuRef(mJustification),windowRef);
  931.  
  932.     DrawMenuBar();
  933. }
  934.  
  935. // ************************************************************************** doAdjustFileMenu
  936.  
  937. void  doAdjustFileMenu(MenuRef menuRef,WindowRef windowRef)
  938. {
  939.     TXNObject    txnObject = NULL;
  940.  
  941.     if(gCurrentNumberOfWindows <= kMaxWindows)
  942.     {
  943.         EnableMenuItem(menuRef,iNew);
  944.         EnableMenuItem(menuRef,iOpen);
  945.     }
  946.     else
  947.     {
  948.         DisableMenuItem(menuRef,iNew);
  949.         DisableMenuItem(menuRef,iOpen);
  950.     }
  951.  
  952.     if(isApplicationWindow(windowRef,&txnObject)) 
  953.     {
  954.         EnableMenuItem(menuRef,iClose);
  955.  
  956.         if(TXNGetChangeCount(txnObject))
  957.         {
  958.             EnableMenuItem(menuRef,iSave);
  959.             EnableMenuItem(menuRef,iRevert);
  960.         }
  961.         else
  962.         {
  963.             DisableMenuItem(menuRef,iSave);
  964.             DisableMenuItem(menuRef,iRevert);
  965.         }
  966.  
  967.         if(TXNDataSize(txnObject))
  968.         {
  969.             EnableMenuItem(menuRef,iSaveAs);
  970.             EnableMenuItem(menuRef,iPageSetup);
  971.             EnableMenuItem(menuRef,iPrint);    
  972.         }
  973.         else
  974.         {
  975.             DisableMenuItem(menuRef,iSaveAs);
  976.             DisableMenuItem(menuRef,iPageSetup);
  977.             DisableMenuItem(menuRef,iPrint);    
  978.         }
  979.     }
  980.     else
  981.     {
  982.         DisableMenuItem(menuRef,iClose);
  983.         DisableMenuItem(menuRef,iSave);
  984.         DisableMenuItem(menuRef,iSaveAs);
  985.         DisableMenuItem(menuRef,iRevert);
  986.         DisableMenuItem(menuRef,iPageSetup);
  987.         DisableMenuItem(menuRef,iPrint);
  988.     }
  989. }
  990.  
  991. // ************************************************************************** doAdjustEditMenu
  992.  
  993. void  doAdjustEditMenu(MenuRef menuRef,WindowRef windowRef)
  994. {
  995.     TXNObject            txnObject = NULL;
  996.     SInt16                menuItem;
  997.     Str255                itemText;
  998.     TXNActionKey    actionKey;
  999.  
  1000.     if(isApplicationWindow(windowRef,&txnObject)) 
  1001.     {
  1002.         // ……………………………………………………………………………………………………………………………………………………………………………………… disable all items
  1003.  
  1004.         for(menuItem = iUndo;menuItem <= iSelectAll;menuItem ++)
  1005.             DisableMenuItem(menuRef,menuItem); 
  1006.  
  1007.         // ………………………………………………………………………………………………………… undo and redo default - can't undo, can't redo
  1008.  
  1009.         GetIndString(itemText,130,1);
  1010.         SetMenuItemText(menuRef,iUndo,itemText);    
  1011.         GetIndString(itemText,130,2);
  1012.         SetMenuItemText(menuRef,iRedo,itemText);
  1013.  
  1014.         // ……………………………………………………………………………………………………… if undoable, enable undo item and set item text
  1015.  
  1016.         if(TXNCanUndo(txnObject,&actionKey))
  1017.         {
  1018.             EnableMenuItem(menuRef,iUndo);
  1019.             
  1020.             if((actionKey < kTXNTypingAction) || (actionKey > kTXNMoveAction))
  1021.                 actionKey = -1;
  1022.             
  1023.             GetIndString(itemText,130,2 * actionKey + 5);
  1024.                 SetMenuItemText(menuRef,iUndo,itemText);
  1025.         }
  1026.  
  1027.         // ……………………………………………………………………………………………………… if redoable, enable redo item and set item text
  1028.  
  1029.         if(TXNCanRedo(txnObject,&actionKey))
  1030.         {
  1031.             EnableMenuItem(menuRef,iRedo);
  1032.             
  1033.             if((actionKey < kTXNTypingAction) || (actionKey > kTXNMoveAction))
  1034.                 actionKey = -1;
  1035.             
  1036.             GetIndString(itemText,130,2 * actionKey + 6);
  1037.             SetMenuItemText(menuRef,iRedo,itemText);
  1038.         }
  1039.  
  1040.         // ………………………………………………………………………………………… if there is a selection, enable cut, copy, and clear
  1041.  
  1042.         if(!TXNIsSelectionEmpty(txnObject))
  1043.         {
  1044.             EnableMenuItem(menuRef,iCut);
  1045.             EnableMenuItem(menuRef,iCopy);
  1046.             EnableMenuItem(menuRef,iClear);
  1047.         }
  1048.  
  1049.         // ………………………………………………………………………………………………………………………………………… if scrap is pastable, enable paste
  1050.  
  1051.         if(TXNIsScrapPastable())
  1052.             EnableMenuItem(menuRef,iPaste);
  1053.  
  1054.         // ………………………………………………………………………………………………… if any characters in TXNObject, enable select all
  1055.  
  1056.         if(TXNDataSize(txnObject))
  1057.             EnableMenuItem(menuRef,iSelectAll);
  1058.     }
  1059. }
  1060.  
  1061. // ************************************************************************* doPrepareFontMenu
  1062.  
  1063. void  doPrepareFontMenu(WindowRef windowRef)
  1064. {
  1065.     TXNObject    txnObject = NULL;
  1066.  
  1067.     if(isApplicationWindow(windowRef,&txnObject)) 
  1068.         TXNPrepareFontMenu(txnObject,gTXNFontMenuObject);
  1069. }
  1070.  
  1071. // ************************************************************************* doPrepareSizeMenu
  1072.  
  1073. void  doPrepareSizeMenu(MenuRef menuRef,WindowRef windowRef)
  1074. {
  1075.     TXNObject                        txnObject = NULL;
  1076.     static Fixed                itemSizes[8] = { 0x00090000,0x000A0000,0x000B0000,0x000C0000,
  1077.                                                                              0x000E0000,0x00120000,0x00180000,0x00240000 };
  1078.     TXNContinuousFlags    txnContinuousFlags = 0;
  1079.     TXNTypeAttributes        txnTypeAttributes;
  1080.     OSStatus                        osStatus = noErr;
  1081.     SInt16                            menuItem;
  1082.  
  1083.     if(isApplicationWindow(windowRef,&txnObject)) 
  1084.     {
  1085.         txnTypeAttributes.tag                            = kTXNQDFontSizeAttribute;
  1086.         txnTypeAttributes.size                        = kTXNFontSizeAttributeSize;
  1087.         txnTypeAttributes.data.dataValue    = 0;
  1088.  
  1089.         osStatus = TXNGetContinuousTypeAttributes(txnObject,&txnContinuousFlags,1,
  1090.                                                                                             &txnTypeAttributes);
  1091.         if(osStatus == noErr)
  1092.         {
  1093.             for(menuItem = 1;menuItem < 8;menuItem ++)
  1094.             {
  1095.                 CheckMenuItem(menuRef,menuItem,(txnContinuousFlags & kTXNSizeContinuousMask) &&
  1096.                                             (txnTypeAttributes.data.dataValue == itemSizes[menuItem - 1]));
  1097.             }
  1098.         }
  1099.     }
  1100. }
  1101.  
  1102. // ************************************************************************ doPrepareStyleMenu
  1103.  
  1104. void  doPrepareStyleMenu(MenuRef menuRef,WindowRef windowRef)
  1105. {
  1106.     TXNObject                        txnObject = NULL;
  1107.     TXNContinuousFlags    txnContinuousFlags = 0;
  1108.     TXNTypeAttributes        txnTypeAttributes;
  1109.     OSStatus                        osStatus = noErr;
  1110.     SInt16                            menuItem;
  1111.  
  1112.     if(isApplicationWindow(windowRef,&txnObject)) 
  1113.     {
  1114.         txnTypeAttributes.tag                            = kTXNQDFontStyleAttribute;
  1115.         txnTypeAttributes.size                        = kTXNQDFontStyleAttributeSize;
  1116.         txnTypeAttributes.data.dataValue    = 0;
  1117.  
  1118.         osStatus = TXNGetContinuousTypeAttributes(txnObject,&txnContinuousFlags,1,
  1119.                                                                                             &txnTypeAttributes);
  1120.         if(osStatus == noErr)
  1121.         {
  1122.             CheckMenuItem(menuRef,iPlain,(txnContinuousFlags & kTXNStyleContinuousMask) &&
  1123.                                         (txnTypeAttributes.data.dataValue == normal));
  1124.  
  1125.             for(menuItem = iBold;menuItem <= iUnderline;menuItem ++)
  1126.             {
  1127.                 CheckMenuItem(menuRef,menuItem,(txnContinuousFlags & kTXNStyleContinuousMask) &&
  1128.                                             (txnTypeAttributes.data.dataValue & (1 << (menuItem - iBold))));
  1129.             }
  1130.         }
  1131.     }
  1132. }
  1133.  
  1134. // *********************************************************************** doPrepareColourMenu
  1135.  
  1136. void  doPrepareColourMenu(MenuRef menuRef,WindowRef windowRef)
  1137. {
  1138.     TXNObject                        txnObject = NULL;
  1139.     TXNContinuousFlags    txnContinuousFlags = 0;
  1140.     TXNTypeAttributes        txnTypeAttributes;
  1141.     RGBColor                        attributesColour;
  1142.     OSStatus                        osStatus = noErr;
  1143.     SInt16                            menuItem;
  1144.     RGBColor                        itemColours[4] = { { 0xFFFF,0x0000,0x0000 },{ 0x0000,0x8888,0x0000 },
  1145.                                                                                  { 0x0000,0x0000,0xFFFF },{ 0x0000,0x0000,0x0000 } };
  1146.  
  1147.     if(isApplicationWindow(windowRef,&txnObject)) 
  1148.     {
  1149.         txnTypeAttributes.tag                        = kTXNQDFontColorAttribute;
  1150.         txnTypeAttributes.size                    = kTXNQDFontColorAttributeSize;
  1151.         txnTypeAttributes.data.dataPtr    = &attributesColour;
  1152.  
  1153.         osStatus = TXNGetContinuousTypeAttributes(txnObject,&txnContinuousFlags,1,
  1154.                                                                                             &txnTypeAttributes);
  1155.         if(osStatus == noErr)
  1156.         {
  1157.             for(menuItem = 1;menuItem < 5;menuItem ++)
  1158.             {
  1159.                 CheckMenuItem(menuRef,menuItem,(txnContinuousFlags & kTXNColorContinuousMask) &&
  1160.                                             (isEqualRGB(&attributesColour,&itemColours[menuItem - 1])));
  1161.             }
  1162.         }
  1163.     }
  1164. }
  1165.  
  1166. // ******************************************************************************** isEqualRGB
  1167.  
  1168. Boolean  isEqualRGB(RGBColor *attributesColour,RGBColor *itemColour)
  1169. {
  1170.     return (attributesColour->red        == itemColour->red && 
  1171.                     attributesColour->green    == itemColour->green &&
  1172.                     attributesColour->blue    == itemColour->blue);
  1173. }
  1174.  
  1175. // **************************************************************** doPrepareJustificationMenu
  1176.  
  1177. void  doPrepareJustificationMenu (MenuRef menuRef,WindowRef windowRef)
  1178. {
  1179.     TXNObject                txnObject = NULL;
  1180.     static UInt32        itemJustifications[6] = { kTXNFlushDefault,kTXNFlushLeft,kTXNFlushRight,
  1181.                                                                                         kTXNCenter,kTXNFullJust,kTXNForceFullJust};
  1182.     TXNControlTag        txnControlTag[1];
  1183.     TXNControlData    txnControlData[1];
  1184.     SInt16                    menuItem;
  1185.     OSStatus                osStatus = noErr;
  1186.  
  1187.     if(isApplicationWindow(windowRef,&txnObject)) 
  1188.     {
  1189.         txnControlTag[0]                    = kTXNJustificationTag ;
  1190.         txnControlData[0].uValue    = 0;
  1191.  
  1192.         osStatus = TXNGetTXNObjectControls(txnObject,1,txnControlTag,txnControlData);
  1193.  
  1194.         if(osStatus == noErr)
  1195.         {
  1196.             for(menuItem = iDefault;menuItem <= iForceFull;menuItem ++ )
  1197.                 CheckMenuItem(menuRef,menuItem,(txnControlData[0].uValue == 
  1198.                                             itemJustifications[menuItem - 1]));
  1199.         }
  1200.     }
  1201. }
  1202.  
  1203. // ****************************************************************************** doMenuChoice
  1204.  
  1205. void  doMenuChoice(SInt32 menuChoice)
  1206. {
  1207.     MenuID                menuID;
  1208.     MenuItemIndex    menuItem;
  1209.     OSStatus            osStatus = noErr;
  1210.     WindowRef            windowRef;
  1211.     TXNObject            txnObject = NULL;
  1212.  
  1213.     windowRef = FrontWindow();
  1214.  
  1215.     menuID = HiWord(menuChoice);
  1216.     menuItem = LoWord(menuChoice);
  1217.  
  1218.     if(menuID == 0)
  1219.         return;
  1220.  
  1221.     switch(menuID)
  1222.     {
  1223.         case mAppleApplication:
  1224.             if(menuItem == iAbout)
  1225.                 doAboutDialog();
  1226.             break;
  1227.             
  1228.         case mFile:
  1229.             doFileMenuChoice(menuItem,windowRef);
  1230.             break;
  1231.  
  1232.         case mEdit:
  1233.             doEditMenuChoice(menuItem,windowRef);
  1234.             break;
  1235.  
  1236.         case mFont:
  1237.             doFontMenuChoice(menuID,menuItem,windowRef);            
  1238.             break;
  1239.  
  1240.         case mSize:
  1241.             doSizeMenuChoice(menuItem,windowRef);
  1242.             break;
  1243.  
  1244.         case mStyle:
  1245.             doStyleMenuChoice(menuItem,windowRef);
  1246.             break;
  1247.  
  1248.         case mColour:
  1249.             doColourMenuChoice(menuItem,windowRef);
  1250.             break;
  1251.  
  1252.         case mJustification:
  1253.             doJustificationMenuChoice(menuItem,windowRef);
  1254.             break;
  1255.  
  1256.         default:
  1257.             if(menuID >= mFirstHierarchical)
  1258.                 doFontMenuChoice(menuID,menuItem,windowRef);
  1259.             break;
  1260.     }
  1261.  
  1262.     HiliteMenu(0);
  1263. }
  1264.  
  1265. // ************************************************************************** doFileMenuChoice
  1266.  
  1267. void  doFileMenuChoice(MenuItemIndex menuItem,WindowRef windowRef)
  1268. {
  1269.     TXNObject    txnObject = NULL;
  1270.     OSStatus    osStatus = noErr;
  1271.  
  1272.     switch(menuItem)
  1273.     {
  1274.         case iNew:
  1275.             if(osStatus = doNewCommand())
  1276.                 doErrorAlert(osStatus);
  1277.             break;
  1278.  
  1279.         case iOpen:
  1280.             if(osStatus = doOpenCommand())
  1281.                 doErrorAlert(osStatus);
  1282.             break;
  1283.  
  1284.         case iClose:
  1285.             if((osStatus = doCloseCommand(kNavSaveChangesClosingDocument)) && 
  1286.                     osStatus != kNavAskSaveChangesCancel)
  1287.                 doErrorAlert(osStatus);
  1288.             break;
  1289.  
  1290.         case iSave:
  1291.             if(osStatus = doSaveCommand())
  1292.                 doErrorAlert(osStatus);
  1293.             break;
  1294.  
  1295.         case iSaveAs:
  1296.             if(osStatus = doSaveAsCommand())
  1297.                 doErrorAlert(osStatus);
  1298.             break;
  1299.  
  1300.         case iRevert:
  1301.             if(osStatus = doRevertCommand())
  1302.                 doErrorAlert(osStatus);
  1303.             break;
  1304.  
  1305.         case iPageSetup:
  1306.             if(isApplicationWindow(windowRef,&txnObject))
  1307.             {
  1308.                 osStatus = TXNPageSetup(txnObject);
  1309.                 if(osStatus != userCanceledErr && osStatus != noErr)
  1310.                     doErrorAlert(osStatus);
  1311.             }
  1312.             break;
  1313.  
  1314.         case iPrint:
  1315.             if(isApplicationWindow(windowRef,&txnObject))
  1316.             {
  1317.                 osStatus = TXNPrint(txnObject);
  1318.                 if(osStatus != userCanceledErr && osStatus != noErr)
  1319.                     doErrorAlert(osStatus);
  1320.             }
  1321.             break;
  1322.  
  1323.         case iQuit:
  1324.             if((osStatus = doQuitCommand(kNavSaveChangesQuittingApplication)) && 
  1325.                     osStatus != kNavAskSaveChangesCancel)
  1326.                 doErrorAlert(osStatus);
  1327.  
  1328.             if(osStatus != kNavAskSaveChangesCancel)
  1329.             {
  1330.                 if(gTXNFontMenuObject != NULL)
  1331.                 {
  1332.                     if(osStatus = TXNDisposeFontMenuObject(gTXNFontMenuObject))
  1333.                         doErrorAlert(osStatus);
  1334.                 }
  1335.  
  1336.                 gTXNFontMenuObject = NULL;
  1337.             
  1338.                 TXNTerminateTextension();
  1339.                 gDone = true;
  1340.             }
  1341.             break;
  1342.     }
  1343. }
  1344.  
  1345. // ************************************************************************** doEditMenuChoice
  1346.  
  1347. void  doEditMenuChoice(MenuItemIndex menuItem,WindowRef windowRef)
  1348. {
  1349.     TXNObject    txnObject = NULL;
  1350.     OSStatus    osStatus = noErr;
  1351.  
  1352.     if(isApplicationWindow(windowRef,&txnObject))
  1353.     {
  1354.         switch(menuItem)
  1355.         {
  1356.             case iUndo:
  1357.                 TXNUndo(txnObject);
  1358.                 break;
  1359.  
  1360.             case iRedo:
  1361.                 TXNRedo(txnObject);
  1362.                 break;
  1363.  
  1364.             case iCut:
  1365.                 if((osStatus = TXNCut(txnObject)) == noErr)
  1366.                     TXNConvertToPublicScrap();
  1367.                 else
  1368.                     doErrorAlert(osStatus);
  1369.                 break;
  1370.  
  1371.             case iCopy:
  1372.                 if((osStatus = TXNCopy(txnObject)) == noErr)
  1373.                     TXNConvertToPublicScrap();
  1374.                 else
  1375.                     doErrorAlert(osStatus);
  1376.                 break;
  1377.  
  1378.             case iPaste:
  1379.                 if(osStatus = TXNPaste(txnObject))
  1380.                     doErrorAlert(osStatus);
  1381.                 break;
  1382.  
  1383.             case iClear:
  1384.                 if(osStatus = TXNClear(txnObject))
  1385.                     doErrorAlert(osStatus);
  1386.                 break;
  1387.  
  1388.             case iSelectAll:
  1389.                 TXNSelectAll(txnObject);
  1390.                 break;
  1391.         }
  1392.     }
  1393. }
  1394.  
  1395. // ************************************************************************** doFontMenuChoice
  1396.  
  1397. void  doFontMenuChoice(MenuID menuID,MenuItemIndex menuItem,WindowRef windowRef)
  1398. {
  1399.     TXNObject    txnObject = NULL;
  1400.     OSStatus    osStatus = noErr;
  1401.  
  1402.     if(isApplicationWindow(windowRef,&txnObject))
  1403.     {
  1404.         if(gTXNFontMenuObject != NULL)
  1405.         {
  1406.             if(osStatus = TXNDoFontMenuSelection(txnObject,gTXNFontMenuObject,menuID,menuItem))
  1407.                 doErrorAlert(osStatus);
  1408.         }
  1409.     }
  1410. }
  1411.  
  1412. // ************************************************************************** doSizeMenuChoice
  1413.  
  1414. void  doSizeMenuChoice(MenuItemIndex menuItem,WindowRef windowRef)
  1415. {
  1416.     TXNObject                 txnObject = NULL;
  1417.     static Fixed            itemSizes[8] = { 0x00090000,0x000A0000,0x000B0000,0x000C0000,
  1418.                                                                          0x000E0000,0x00120000,0x00180000,0x00240000 };
  1419.     Fixed                            sizeToSet;
  1420.     TXNTypeAttributes    txnTypeAttributes;
  1421.     OSStatus                     osStatus = noErr;
  1422.  
  1423.     if(isApplicationWindow(windowRef,&txnObject))
  1424.     {
  1425.         sizeToSet = itemSizes[menuItem - 1];
  1426.  
  1427.         txnTypeAttributes.tag                            = kTXNQDFontSizeAttribute;
  1428.         txnTypeAttributes.size                        = kTXNFontSizeAttributeSize;
  1429.         txnTypeAttributes.data.dataValue    = sizeToSet;
  1430.  
  1431.         if(TXNSetTypeAttributes(txnObject,1,&txnTypeAttributes,kTXNUseCurrentSelection,
  1432.                                                         kTXNUseCurrentSelection))
  1433.             doErrorAlert(osStatus);
  1434.     }
  1435. }
  1436.  
  1437. // ************************************************************************* doStyleMenuChoice
  1438.  
  1439. void  doStyleMenuChoice(MenuItemIndex menuItem,WindowRef windowRef)
  1440. {
  1441.     TXNObject                 txnObject = NULL;
  1442.     static Style            itemStyles[5] = { normal,0,bold,italic,underline };
  1443.     Style                            styleToSet;
  1444.     TXNTypeAttributes    txnTypeAttributes;
  1445.     OSStatus                     osStatus = noErr;
  1446.     
  1447.     if(isApplicationWindow(windowRef,&txnObject))
  1448.     {
  1449.         styleToSet = itemStyles[menuItem - 1];
  1450.  
  1451.         txnTypeAttributes.tag                            = kTXNQDFontStyleAttribute;
  1452.         txnTypeAttributes.size                        = kTXNQDFontStyleAttributeSize;
  1453.         txnTypeAttributes.data.dataValue    = styleToSet;
  1454.                 
  1455.         if(TXNSetTypeAttributes(txnObject,1,&txnTypeAttributes,kTXNUseCurrentSelection,
  1456.                                                         kTXNUseCurrentSelection))
  1457.             doErrorAlert(osStatus);
  1458.     }
  1459. }
  1460.  
  1461. // ************************************************************************ doColourMenuChoice
  1462.  
  1463. void  doColourMenuChoice(MenuItemIndex menuItem,WindowRef windowRef)
  1464. {
  1465.     TXNObject                 txnObject = NULL;
  1466.     Point                            where;
  1467.     Boolean                        colorPickerButton;
  1468.     Str255                        prompt = "\pPick a text colour";
  1469.     RGBColor                    colourToSet;
  1470.     RGBColor                    itemColours[4] = { { 0xFFFF,0x0000,0x0000 },{ 0x0000,0x8888,0x0000 },
  1471.                                                                              { 0x0000,0x0000,0xFFFF },{ 0x0000,0x0000,0x0000 } };
  1472.     TXNTypeAttributes    txnTypeAttributes;
  1473.     OSStatus                      osStatus = noErr;
  1474.  
  1475.     if(isApplicationWindow(windowRef,&txnObject))
  1476.     {
  1477.         if(menuItem == iColourPicker)
  1478.         {
  1479.             where.v = where.h = 0;
  1480.             colorPickerButton = GetColor(where,prompt,&gCurrentColourPickerColour,&colourToSet);
  1481.             if(colorPickerButton)
  1482.                 gCurrentColourPickerColour = colourToSet;
  1483.             else
  1484.                 return;
  1485.         }
  1486.         else
  1487.             colourToSet = itemColours[menuItem - 1];
  1488.  
  1489.         txnTypeAttributes.tag                        = kTXNQDFontColorAttribute;
  1490.         txnTypeAttributes.size                    = kTXNQDFontColorAttributeSize;
  1491.         txnTypeAttributes.data.dataPtr    = &colourToSet;
  1492.  
  1493.         if(TXNSetTypeAttributes(txnObject,1,&txnTypeAttributes,kTXNUseCurrentSelection,
  1494.                                                         kTXNUseCurrentSelection))
  1495.             doErrorAlert(osStatus);
  1496.     }
  1497. }
  1498.  
  1499. // ***************************************************************** doJustificationMenuChoice
  1500.  
  1501. void  doJustificationMenuChoice(MenuItemIndex menuItem,WindowRef windowRef)
  1502. {
  1503.     TXNObject                txnObject = NULL;
  1504.     static UInt32        itemJustifications[6] = { kTXNFlushDefault,kTXNFlushLeft,kTXNFlushRight,
  1505.                                                                                         kTXNCenter,kTXNFullJust,kTXNForceFullJust };
  1506.     OSStatus                osStatus = noErr;
  1507.     UInt32                    justificationToSet;
  1508.     TXNControlTag        txnControlTag[1];
  1509.     TXNControlData    txnControlData[1];
  1510.     
  1511.     if(isApplicationWindow(windowRef,&txnObject)) 
  1512.     {
  1513.         justificationToSet = itemJustifications[menuItem - 1];
  1514.  
  1515.         txnControlTag[0] = kTXNJustificationTag;
  1516.  
  1517.         osStatus = TXNGetTXNObjectControls(txnObject,1,txnControlTag,txnControlData);
  1518.  
  1519.         if(txnControlData[0].uValue != justificationToSet)
  1520.         {
  1521.             txnControlData[0].uValue = justificationToSet;
  1522.             osStatus = TXNSetTXNObjectControls(txnObject,false,1,txnControlTag,txnControlData);
  1523.             if(osStatus != noErr)
  1524.                 doErrorAlert(osStatus);
  1525.         }
  1526.     }
  1527. }
  1528.  
  1529. // *******************************************************************************************
  1530.