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

  1. /*
  2.  * DumbEdit - Multiple-window TransEdit Demonstration.
  3.  *
  4.  * 28 Oct 86 Paul DuBois
  5.  *
  6.  * 02 Feb 89
  7.  * - Modified to work with TransSkel 2.0 and TransEdit 2.0.
  8.  * - 2-byte and 4-byte integer types are typedef'ed to Integer and Longint
  9.  * to ease porting.
  10.  * 16 Jun 92
  11.  * - Modified for TransSkel 3.00.
  12.  * - Changed SetTextMenus () to compare new state against last and redraw
  13.  * menu bar on a change. This avoids a subtle bug encountered when trying to
  14.  * figure out whether to redraw the menu bar based on whether current and
  15.  * previous front window were edit windows or not.
  16.  * 05 Jun 93 Version 1.03
  17.  * - Conversion for THINK C 6.0.
  18.  * 04 Jan 94
  19.  * - Undid Integer/LongInt type stuff back to short/long.
  20.  * 20 Jan 94 Version 1.04
  21.  * - Use a menu hook procedure to set menus when a mouse click occurs in the
  22.  * menu bar, rather than setting them after every action that could change
  23.  * menu item state.  This simplifies application logic somewhat.
  24.  * - Added Select All item to Edit menu.
  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    maxSize        8        /* no. font sizes made available */
  35. # define    hSize        300        /* horiz, vert size of new windows */
  36. # define    vSize        205
  37. # define    aboutAlrt    1000
  38.  
  39. # define    dumbCreator    'Dumb'    /* file creator type */
  40.  
  41.  
  42. typedef enum        /* File menu item numbers */
  43. {
  44.     new = 1,        /* begin new window */
  45.     open,            /* open existing file */
  46.     close,            /* close file */
  47.     /* --- */
  48.     save = 5,        /* save file */
  49.     saveAs,            /* save under another name */
  50.     saveCopy,        /* save a copy w/o switching file binding */
  51.     revert,            /* revert to version on disk */
  52.     /* --- */
  53.     quit = 10
  54. };
  55.  
  56.  
  57. typedef enum            /* Edit menu item numbers */
  58. {
  59.     undo = 1,
  60.     /* --- */
  61.     cut = 3,
  62.     copy,
  63.     paste,
  64.     clear,
  65.     selectAll
  66. };
  67.  
  68.  
  69.  
  70. typedef enum        /* Format menu item numbers */
  71. {
  72.     wordWrap = 1,
  73.     noWrap,
  74.     /* --- */
  75.     leftJust = 4,
  76.     centerJust,
  77.     rightJust
  78. };
  79.  
  80.  
  81. static MenuHandle    fileMenu;
  82. static MenuHandle    editMenu;
  83. static MenuHandle    fontMenu;
  84. static MenuHandle    sizeMenu;
  85. static MenuHandle    formatMenu;
  86.  
  87. static short    sizes[maxSize] = { 9, 10, 12, 14, 18, 20, 24, 48 };
  88.  
  89.  
  90. /*
  91.  * Set state of all the items in a menu, and unchecks them all.
  92.  */
  93.  
  94. static void
  95. SetMenuItems (MenuHandle m, Boolean enable)
  96. {
  97. short    i, nItems;
  98.  
  99.     nItems = CountMItems (m);
  100.  
  101.     for (i = 1; i <= nItems; ++i)
  102.     {
  103.         if (enable)
  104.             EnableItem (m, i);
  105.         else
  106.             DisableItem (m, i);
  107.         CheckItem (m, i, false);
  108.         SetItemStyle (m, i, 0);
  109.     }
  110. }
  111.  
  112.  
  113. /*
  114.  * Set the Font, Size and Format menus so that the items corresponding
  115.  * to the text characteristics of the window are checked.  If the
  116.  * window isn't an edit window, dim items in all three menus.
  117.  */
  118.  
  119. static void
  120. SetTextMenus (void)
  121. {
  122. WindowPtr    w;
  123. Str255        wFontName;
  124. Str255        mFontName;
  125. short        i, nItems;
  126. TEHandle    hTE;
  127. Boolean        isEditWind;
  128.  
  129.     w = FrontWindow ();
  130.  
  131.     isEditWind = IsEWindow (w);
  132.  
  133.     /* enable/disable menu items, toss checkmarks, make plain style */
  134.  
  135.     SetMenuItems (fontMenu, isEditWind);
  136.     SetMenuItems (sizeMenu, isEditWind);
  137.     SetMenuItems (formatMenu, isEditWind);
  138.  
  139.     if (!isEditWind)
  140.         return;
  141.  
  142.     hTE = GetEWindowTE (w);
  143.  
  144.     /* check appropriate word wrap item */
  145.  
  146.     CheckItem (formatMenu, (**hTE).crOnly < 0 ? noWrap : wordWrap, true);
  147.  
  148.     /* check appropriate justification item */
  149.  
  150.     switch ((**hTE).just)
  151.     {
  152.     case teJustLeft:
  153.         i = leftJust;
  154.         break;
  155.     case teJustRight:
  156.         i = rightJust;
  157.         break;
  158.     case teJustCenter:
  159.         i = centerJust;
  160.         break;
  161.     }
  162.     CheckItem (formatMenu, i, true);
  163.  
  164.     /*
  165.      * Check appropriate font size item, and outline items for sizes
  166.      * present in resource files
  167.      */
  168.  
  169.     for (i = 0; i < maxSize; ++i)
  170.     {
  171.         if ((**hTE).txSize == sizes[i])
  172.             CheckItem (sizeMenu, i + 1, true);
  173.         if (RealFont ((**hTE).txFont, sizes[i]))
  174.             SetItemStyle (sizeMenu, i + 1, outline);
  175.     }
  176.  
  177.     /*
  178.      * Check appropriate font name item
  179.      */
  180.     
  181.     GetFontName ((**hTE).txFont, wFontName);    /* name of window font */
  182.     nItems = CountMItems (fontMenu);            /* # fonts in menu */
  183.     for (i = 1; i <= nItems; ++i)
  184.     {
  185.         GetItem (fontMenu, i, mFontName);    /* get font name */
  186.         if (EqualString (wFontName, mFontName, false, true))
  187.         {
  188.             CheckItem (fontMenu, i, true);
  189.             break;
  190.         }
  191.     }
  192. }
  193.  
  194.  
  195. /*
  196.  * Set File/Edit menu items according to type of front window.
  197.  *
  198.  * The general behavior is:
  199.  *
  200.  * New and Open are always enabled, since a new edit window can always be
  201.  * opened.
  202.  *
  203.  * Close enabled when an edit or DA window in front (i.e., when there's
  204.  * a window at all).
  205.  *
  206.  * Save enabled for edit windows not bound to a file, and edit windows
  207.  * bound to a file when they're dirty (typed into, Edit menu used to
  208.  * do something to them).
  209.  *
  210.  * Save As and Save a Copy As enabled for edit windows.
  211.  *
  212.  * Revert enabled for edit windows bound to a file when they're dirty.
  213.  *
  214.  * Undo disabled when there's an edit window in front.
  215.  */
  216.  
  217. static void
  218. SetNonTextMenus (void)
  219. {
  220. WindowPtr    w;
  221.  
  222.     DisableItem (fileMenu, close);    /* assume no window at all */
  223.     DisableItem (fileMenu, save);
  224.     DisableItem (fileMenu, saveAs);
  225.     DisableItem (fileMenu, saveCopy);
  226.     DisableItem (fileMenu, revert);
  227.     EnableItem (editMenu, undo);
  228.  
  229.     if ((w = FrontWindow ()) != (WindowPtr) nil)
  230.         EnableItem (fileMenu, close);
  231.  
  232.     if (IsEWindow (w))            /* edit window in front */
  233.     {
  234.         EnableItem (fileMenu, saveAs);
  235.         EnableItem (fileMenu, saveCopy);
  236.         if (GetEWindowFile (w, nil) == false)    /* not bound to file */
  237.         {
  238.             EnableItem (fileMenu, save);
  239.         }
  240.         else if (IsEWindowDirty (w))        /* bound - is it dirty? */
  241.         {
  242.             EnableItem (fileMenu, save);
  243.             EnableItem (fileMenu, revert);
  244.         }
  245.         DisableItem (editMenu, undo);
  246.     }
  247. }
  248.  
  249.  
  250. /*
  251.  * Menu hook, called when mouse click occurs in menu bar.
  252.  */
  253.  
  254. static pascal void
  255. AdjustMenus (void)
  256. {
  257.     SetNonTextMenus ();
  258.     SetTextMenus ();
  259. }
  260.  
  261.  
  262. static void
  263. MakeWind (Boolean bindToFile)
  264. {
  265. WindowPtr    w;
  266. Rect        r;
  267. short        offset;
  268. static short    windCount = 0;
  269.  
  270.     if (FrontWindow () == nil)
  271.         windCount = 0;
  272.     SetRect (&r, 0, 0, hSize, vSize);
  273.     offset = 50 + 25 * (windCount++ % 4);
  274.     OffsetRect (&r, offset, offset);
  275.     w = NewEWindow (&r, nil, true, (WindowPtr) -1L, true, 0L, bindToFile);
  276. }
  277.  
  278.  
  279. /*
  280.  * File menu handler
  281.  */
  282.  
  283. static pascal void
  284. DoFileMenu (short item)
  285. {
  286. WindowPtr    theWind;
  287.  
  288.     theWind = FrontWindow ();
  289.     switch (item)
  290.     {
  291.     case new:
  292.         MakeWind (false);
  293.         break;
  294.     case open:
  295.         MakeWind (true);
  296.         break;
  297.     case close:
  298.         SkelClose (theWind);
  299.         break;
  300.     case save:
  301.         (void) EWindowSave (theWind);
  302.         break;
  303.     case saveAs:
  304.         (void) EWindowSaveAs (theWind);
  305.         break;
  306.     case saveCopy:
  307.         (void) EWindowSaveCopy (theWind);
  308.         break;
  309.     case revert:
  310.         (void) EWindowRevert (theWind);
  311.         break;
  312.     case quit:
  313.         if (ClobberEWindows () == true)
  314.             SkelStopEventLoop ();
  315.         break;
  316.     }
  317. }
  318.  
  319.  
  320. static pascal void
  321. DoEditMenu (short item)
  322. {
  323. TEHandle    hTE;
  324.  
  325.     if (item == selectAll)
  326.     {
  327.         hTE = GetEWindowTE (FrontWindow ());
  328.         if (hTE != (TEHandle) nil)
  329.             TESetSelect (0L, 32767L, hTE);
  330.     }
  331.     else
  332.         EWindowEditOp (item);
  333. }
  334.  
  335.  
  336. /*
  337.  * Handle Font menu items
  338.  */
  339.  
  340. static pascal void
  341. DoFontMenu (short item)
  342. {
  343. short        font;
  344. TEHandle    te;
  345. WindowPtr    theWind;
  346. Str255        theFontName;
  347.  
  348.     theWind = FrontWindow ();
  349.     if ((te = GetEWindowTE (theWind)) == nil)
  350.         return;                /* not an edit window */
  351.     GetItem (fontMenu, item, theFontName);
  352.     GetFNum (theFontName, &font);
  353.     SetEWindowStyle (theWind, font, (**te).txSize, (**te).crOnly, (**te).just);
  354. }
  355.  
  356.  
  357. /*
  358.  * Handle Size menu items
  359.  */
  360.  
  361. static pascal void
  362. DoSizeMenu (short item)
  363. {
  364. TEHandle    te;
  365. WindowPtr    theWind;
  366.  
  367.     theWind = FrontWindow ();
  368.     if ((te = GetEWindowTE (theWind)) == nil)
  369.         return;                /* not an edit window */
  370.     SetEWindowStyle (theWind, (**te).txFont, sizes[item-1], (**te).crOnly, (**te).just);
  371. }
  372.  
  373.  
  374. /*
  375.  * Handle Format menu items
  376.  */
  377.  
  378. static pascal void
  379. DoFormatMenu (short item)
  380. {
  381. short        font, size, just, wrap;
  382. TEHandle    te;
  383. WindowPtr    theWind;
  384.  
  385.     theWind = FrontWindow ();
  386.     if ((te = GetEWindowTE (theWind)) == nil)
  387.         return;                /* not an edit window */
  388.     font = (**te).txFont;
  389.     size = (**te).txSize;
  390.     just = (**te).just;
  391.     wrap = (**te).crOnly;
  392.  
  393.     switch (item)
  394.     {
  395.     case wordWrap:
  396.         wrap = 0;
  397.         break;
  398.     case noWrap:
  399.         wrap = -1;
  400.         break;
  401.     case leftJust:
  402.         just = teJustLeft;
  403.         break;
  404.     case centerJust:
  405.         just = teJustCenter;
  406.         break;
  407.     case rightJust:
  408.         just = teJustRight;
  409.         break;
  410.     }
  411.     SetEWindowStyle (theWind, font, size, wrap, just);
  412. }
  413.  
  414.  
  415. /*
  416.  * Handle selection of About... item from Apple menu
  417.  */
  418.  
  419. static pascal void
  420. DoAppleMenu (short item)
  421. {
  422.     (void) SkelAlert (aboutAlrt, SkelDlogFilter (nil, true),
  423.                                         skelPositionOnParentDevice);
  424.     SkelRmveDlogFilter ();
  425. }
  426.  
  427.  
  428. int
  429. main (void)
  430. {
  431.     /*
  432.      * Initialize TransSkel, create menus and install handlers.
  433.      */
  434.  
  435.     SkelInit ((SkelInitParamsPtr) nil);
  436.  
  437.     SkelApple ("\pAbout DumbEdit\311", DoAppleMenu);    /* 311 = ellipsis */
  438.  
  439.     fileMenu = NewMenu (1000, "\pFile");
  440.     AppendMenu (fileMenu, "\pNew/N;Open\311/O;Close/W;(-;Save/S;Save As\311");
  441.     AppendMenu (fileMenu, "\pSave a Copy As\311;Revert/R;(-;Quit/Q");
  442.     (void) SkelMenu (fileMenu, DoFileMenu, nil, false, false);
  443.  
  444.     editMenu = NewMenu (1001, "\pEdit");
  445.     AppendMenu (editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  446.     AppendMenu (editMenu, "\pSelect All/A");
  447.     (void) SkelMenu (editMenu, DoEditMenu, nil, false, false);
  448.  
  449.     fontMenu = NewMenu (1002, "\pFont");
  450.     AddResMenu (fontMenu, 'FONT');
  451.     (void) SkelMenu (fontMenu, DoFontMenu, nil, false, false);
  452.  
  453.     sizeMenu = NewMenu (1003, "\pSize");
  454.     AppendMenu (sizeMenu, "\p9 Point;10 Point;12 Point;14 Point");
  455.     AppendMenu (sizeMenu, "\p18 Point;20 Point;24 Point;48 Point");
  456.     (void) SkelMenu (sizeMenu, DoSizeMenu, nil, false, false);
  457.  
  458.     formatMenu = NewMenu (1004, "\pFormat");
  459.     AppendMenu (formatMenu, "\pWord Wrap;No Word Wrap;(-;Left;Center;Right");
  460.     (void) SkelMenu (formatMenu, DoFormatMenu, nil, false, false);
  461.  
  462.     DrawMenuBar ();
  463.  
  464.     SkelSetMenuHook (AdjustMenus);
  465.  
  466.     /*
  467.      * Do TransEdit-specific setup:  set creator for any files created,
  468.      * set default text style and event notification procedures for
  469.      * new windows.
  470.      */
  471.  
  472.     SetEWindowCreator (dumbCreator);
  473.     SetEWindowStyle (nil, monaco, 9, 0, teJustLeft);
  474.     /*SetEWindowProcs (nil, Key, nil, Close);*/
  475.  
  476.     /*
  477.      * Process events until Quit selected from File menu, then clean up.
  478.      */
  479.  
  480.     SkelEventLoop ();
  481.     SkelCleanup ();
  482. }
  483.