home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / TransEdit / Demos / C Demos / TinyEdit / TinyEdit.c next >
Encoding:
C/C++ Source or Header  |  1994-02-23  |  3.9 KB  |  185 lines  |  [TEXT/KAHL]

  1. /*
  2.  * TinyEdit - Minimal TransEdit Demonstration.
  3.  *
  4.  * This application is about the smallest that can be written using TransEdit
  5.  * It allows for a single edit window, created either blank or with contents
  6.  * read from a file.  It also provides the standard edit operations from the
  7.  * Edit menu.
  8.  *
  9.  * 08 Nov 86 Paul DuBois
  10.  * 02 Feb 89 Version 1.01
  11.  * - Modified to work with TransSkel 2.0 and TransEdit 2.0. 2-byte and 4-byte
  12.  * integer types are typedef'ed to Integer and Longint to ease porting.
  13.  * 16 Jun 92 Version 1.02
  14.  * - Modified for TransEdit 3.00.
  15.  * 06 Jun 93 Version 1.03
  16.  * - Conversion for THINK C 6.0.
  17.  * 04 Jan 94
  18.  * - Undid Integer/LongInt type stuff back to short/long.
  19.  * 20 Jan 94 Version 1.04
  20.  * - Use a menu hook procedure to set menus when a mouse click occurs in the
  21.  * menu bar, rather than setting them after every action that could change
  22.  * menu item state.  This simplifies application logic somewhat.
  23.  * 21 Jan 94
  24.  * - Use GetNewEWindow() rather than NewEWindow().
  25.  * 21 Feb 94
  26.  * - Updated for TransSkel 3.11, TransEdit 3.05.
  27.  */
  28.  
  29. # include    "TransSkel.h"
  30.  
  31. # include    "TransEdit.h"
  32.  
  33.  
  34. # define    aboutAlrt    1000    /* "About..." alert number */
  35. # define    windRsrc    1000    /* window resource number */
  36.  
  37. typedef enum        /* File menu item numbers */
  38. {
  39.     newWind = 1,        /* begin new window */
  40.     openWind,            /* open existing file */
  41.     closeWind,            /* close window */
  42.     /* --- */
  43.     quitApp = 5
  44. };
  45.  
  46.  
  47. typedef enum            /* Edit menu item numbers */
  48. {
  49.     undo = 1,
  50.     /* --- */
  51.     cut = 3,
  52.     copy,
  53.     paste,
  54.     clear
  55. };
  56.  
  57.  
  58. static WindowPtr    editWind = nil;        /* non-nil if edit window open */
  59. static MenuHandle    fileMenu;
  60. static MenuHandle    editMenu;
  61.  
  62.  
  63. static pascal void
  64. Close (void)
  65. {
  66.     if (EWindowClose (editWind))
  67.         editWind = (WindowPtr) nil;
  68. }
  69.  
  70.  
  71. /*
  72.  * Handle selection of About... item from Apple menu
  73.  */
  74.  
  75. static pascal void
  76. DoAppleMenu (short item)
  77. {
  78.     (void) SkelAlert (aboutAlrt, SkelDlogFilter (nil, true),
  79.                                             skelPositionOnParentDevice);
  80.     SkelRmveDlogFilter ();
  81. }
  82.  
  83.  
  84. /*
  85.  * File menu handler
  86.  */
  87.  
  88. static pascal void
  89. DoFileMenu (short item)
  90. {
  91.     switch (item)
  92.     {
  93.     case newWind:
  94.         editWind = GetNewEWindow (windRsrc, (WindowPtr) -1L, false);
  95.         break;
  96.     case openWind:
  97.         editWind = GetNewEWindow (windRsrc, (WindowPtr) -1L, true);
  98.         break;
  99.     case closeWind:
  100.         SkelClose (FrontWindow ());
  101.         break;
  102.     case quitApp:
  103.         if (ClobberEWindows () == true)
  104.             SkelStopEventLoop ();
  105.         break;
  106.     }
  107. }
  108.  
  109.  
  110. /*
  111.  * Menu hook, called to adjust the menus when a mouse click occurs in
  112.  * the menu bar.
  113.  *
  114.  * Set File/Edit menu items according to type of front window.
  115.  *
  116.  * The general behavior is:
  117.  *
  118.  * New and Open enabled if an edit window is not open, otherwise they
  119.  * are disabled.
  120.  *
  121.  * Close enabled when an edit or DA window is in front (i.e.,
  122.  * when there's a window at all).
  123.  *
  124.  * Undo disabled when the edit window is in front.
  125.  */
  126.  
  127. static pascal void
  128. AdjustMenus (void)
  129. {
  130.     DisableItem (fileMenu, closeWind);    /* assume no window at all */
  131.     EnableItem (editMenu, undo);
  132.  
  133.     if (FrontWindow () != nil)
  134.     {
  135.         EnableItem (fileMenu, closeWind);
  136.         if (IsEWindow (FrontWindow ()))    /* the edit window's in front */
  137.             DisableItem (editMenu, undo);
  138.     }
  139.  
  140.     if (editWind == nil)
  141.     {
  142.         EnableItem (fileMenu, newWind);
  143.         EnableItem (fileMenu, openWind);
  144.     }
  145.     else
  146.     {
  147.         DisableItem (fileMenu, newWind);
  148.         DisableItem (fileMenu, openWind);
  149.     }
  150. }
  151.  
  152.  
  153. int
  154. main (void)
  155. {
  156.     /*
  157.      * Initialize TransSkel, create menus and install handlers.
  158.      */
  159.  
  160.     SkelInit ((SkelInitParamsPtr) nil);
  161.  
  162.     SkelApple ("\pAbout TinyEdit\311", DoAppleMenu);    /* 311 = ellipsis */
  163.  
  164.     fileMenu = NewMenu (1000, "\pFile");
  165.     AppendMenu (fileMenu, "\pNew/N;Open\311/O;(Close/W;(-;Quit/Q");
  166.     (void) SkelMenu (fileMenu, DoFileMenu, nil, false, false);
  167.  
  168.     editMenu = NewMenu (1001, "\pEdit");
  169.     AppendMenu (editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  170.     (void) SkelMenu (editMenu, EWindowEditOp, nil, false, false);
  171.  
  172.     DrawMenuBar ();
  173.     SkelSetMenuHook (AdjustMenus);
  174.  
  175.     SetEWindowProcs (nil, nil, nil, Close);
  176.  
  177.     /*
  178.      * Process events until user quits,
  179.      * then clean up and exit
  180.      */
  181.  
  182.     SkelEventLoop ();
  183.     SkelCleanup ();
  184. }
  185.