home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 10 / Fresh_Fish_10_2352.bin / new / dev / e / amigae / rkrmsrc / intuition / menus / menulayout.e next >
Text File  |  1995-03-26  |  18KB  |  461 lines

  1. -> menulayout.e - Example showing how to do menu layout in general.  This
  2. -> example also illustrates handling menu events, including IDCMP_MENUHELP
  3. -> events.
  4. ->
  5. -> Note that handling arbitrary fonts is fairly complex.  Applications that
  6. -> require V37 should use the simpler menu layout routines found in the
  7. -> GadTools library.
  8.  
  9. OPT PREPROCESS
  10.  
  11. MODULE 'dos/dos',
  12.        'graphics/rastport',
  13.        'graphics/text',
  14.        'intuition/intuition',
  15.        'intuition/screens'
  16.  
  17. ENUM ERR_NONE, ERR_DRAW, ERR_FONT, ERR_KICK, ERR_MENU, ERR_PUB, ERR_WIN
  18.  
  19. RAISE ERR_DRAW IF GetScreenDrawInfo()=NIL,
  20.       ERR_FONT IF OpenFont()=NIL,
  21.       ERR_MENU IF SetMenuStrip()=FALSE,
  22.       ERR_PUB  IF LockPubScreen()=NIL,
  23.       ERR_WIN  IF OpenWindowTagList()=NIL
  24.  
  25. DEF firstMenu
  26.  
  27. -> Open all of the required libraries.  Note that we require V37, as the
  28. -> routine uses OpenWindowTags().
  29. PROC main() HANDLE
  30.   IF KickVersion(37)=FALSE THEN Raise(ERR_KICK)
  31.  
  32.   doWindow()
  33.  
  34. EXCEPT DO
  35.   -> E-Note: we can print a minimal error message
  36.   SELECT exception
  37.   CASE ERR_DRAW; WriteF('Error: Failed to get screen DrawInfo\n')
  38.   CASE ERR_KICK; WriteF('Error: Needs Kickstart V37+\n')
  39.   CASE ERR_MENU; WriteF('Error: Failed to attach menu\n')
  40.   CASE ERR_PUB;  WriteF('Error: Failed to lock public screen\n')
  41.   CASE ERR_WIN;  WriteF('Error: Failed to open window\n')
  42.   ENDSELECT
  43.   RETURN IF exception=ERR_NONE THEN RETURN_FAIL ELSE RETURN_OK
  44. ENDPROC
  45.  
  46. -> Open a window with some properly positioned text.  Layout and set the menus,
  47. -> then process any events received.  Cleanup when done.
  48. PROC doWindow() HANDLE
  49.   -> E-Note: some of these are global arrays in the C version
  50.   DEF settItem, editItem, prtItem, projItem, menus,
  51.       winText0:PTR TO intuitext, winText1:PTR TO intuitext,
  52.       window=NIL:PTR TO window, screen=NIL:PTR TO screen,
  53.       drawinfo=NIL:PTR TO drawinfo,
  54.       win_width, alt_width, win_height
  55.  
  56.   screen:=LockPubScreen(NIL)
  57.   drawinfo:=GetScreenDrawInfo(screen)
  58.  
  59.   -> Window Text for Explanation of Program
  60.   -> Get the colors for the window text
  61.   -> Use the screen's font for the text
  62.   -> E-Note: link directly without an array
  63.   winText0:=[drawinfo.pens[TEXTPEN], drawinfo.pens[BACKGROUNDPEN], RP_JAM2,
  64.             0, 0, screen.font, 'How to do a Menu', NIL]:intuitext
  65.   winText1:=[drawinfo.pens[TEXTPEN], drawinfo.pens[BACKGROUNDPEN], RP_JAM2,
  66.             0, 0, screen.font, '(with Style)', winText0]:intuitext
  67.  
  68.   -> Calculate window size
  69.   win_width:=100+IntuiTextLength(winText0)
  70.   alt_width:=100+IntuiTextLength(winText1)
  71.   IF win_width<alt_width THEN win_width:=alt_width
  72.   win_height:=1+screen.wbortop+screen.wborbottom+(screen.font.ysize*5)
  73.  
  74.   -> Calculate the correct positions for the text in the window
  75.   winText0.leftedge:=Shr(win_width-IntuiTextLength(winText0), 1)
  76.   winText0.topedge:=1+screen.wbortop+(2*screen.font.ysize)
  77.   winText1.leftedge:=Shr(win_width-IntuiTextLength(winText1), 1)
  78.   winText1.topedge:=winText0.topedge+screen.font.ysize
  79.  
  80.   -> Open the window
  81.   window:=OpenWindowTagList(NIL,
  82.             [WA_PUBSCREEN, screen,
  83.              WA_IDCMP, IDCMP_MENUPICK OR IDCMP_CLOSEWINDOW OR IDCMP_MENUHELP,
  84.              WA_FLAGS, WFLG_DRAGBAR OR WFLG_DEPTHGADGET OR WFLG_CLOSEGADGET OR
  85.                          WFLG_ACTIVATE OR WFLG_NOCAREREFRESH,
  86.              WA_LEFT,  10,             WA_TOP,      screen.barheight+1,
  87.              WA_WIDTH, win_width,      WA_HEIGHT,   win_height,
  88.              WA_TITLE, 'Menu Example', WA_MENUHELP, TRUE,
  89.              NIL])
  90.  
  91.   -> Give a brief explanation of the program
  92.   PrintIText(window.rport, winText1, 0, 0)
  93.  
  94.   -> E-Note: define the menus using (local) typed lists rather than arrays
  95.   -> E-Note: link menu items in reverse order to layout
  96.  
  97.   -> Settings Items
  98.   -> 'Eat It Too' (excludes 'Have Your Cake')
  99.   settItem:=[NIL, 0, 0, 0, 0,
  100.              ITEMTEXT OR ITEMENABLED OR HIGHCOMP OR CHECKIT, 4,
  101.               [0, 1, RP_JAM2, CHECKWIDTH, 1, NIL,
  102.                ' Eat It Too', NIL]:intuitext,
  103.              NIL, NIL, NIL, MENUNULL]:menuitem
  104.   -> 'Have Your Cake' (initially selected, excludes 'Eat It Too')
  105.   settItem:=[settItem, 0, 0, 0, 0,
  106.              ITEMTEXT OR ITEMENABLED OR HIGHCOMP OR CHECKIT OR CHECKED, 8,
  107.               [0, 1, RP_JAM2, CHECKWIDTH, 1, NIL,
  108.                ' Have Your Cake', NIL]:intuitext,
  109.              NIL, NIL, NIL, MENUNULL]:menuitem
  110.   -> 'Auto Save' (toggle-select, initially selected)
  111.   settItem:=[settItem, 0, 0, 0, 0,
  112.              ITEMTEXT OR ITEMENABLED OR HIGHCOMP OR CHECKIT OR
  113.                MENUTOGGLE OR CHECKED, 0,
  114.               [0, 1, RP_JAM2, CHECKWIDTH, 1, NIL,
  115.                ' Auto Save', NIL]:intuitext,
  116.              NIL, NIL, NIL, MENUNULL]:menuitem
  117.   -> 'Sound...'
  118.   settItem:=[settItem, 0, 0, 0, 0,
  119.              ITEMTEXT OR ITEMENABLED OR HIGHCOMP, 0,
  120.               [0, 1, RP_JAM2, 2, 1, NIL,
  121.                'Sound...', NIL]:intuitext,
  122.              NIL, NIL, NIL, MENUNULL]:menuitem
  123.  
  124.   -> Edit Menu Items
  125.   -> 'Undo' (key-equivalent: "Z")
  126.   editItem:=[NIL, 0, 0, 0, 0,
  127.              ITEMTEXT OR COMMSEQ OR ITEMENABLED OR HIGHCOMP, 0,
  128.               [0, 1, RP_JAM2, 2, 1, NIL, 'Undo', NIL]:intuitext,
  129.              NIL, "Z", NIL, MENUNULL]:menuitem
  130.   -> 'Erase' (disabled)
  131.   editItem:=[editItem, 0, 0, 0, 0,
  132.              ITEMTEXT OR HIGHCOMP, 0,
  133.               [0, 1, RP_JAM2, 2, 1, NIL, 'Erase', NIL]:intuitext,
  134.              NIL, NIL, NIL, MENUNULL]:menuitem
  135.   -> 'Paste' (key-equivalent: "V")
  136.   editItem:=[editItem, 0, 0, 0, 0,
  137.              ITEMTEXT OR COMMSEQ OR ITEMENABLED OR HIGHCOMP, 0,
  138.               [0, 1, RP_JAM2, 2, 1, NIL, 'Paste', NIL]:intuitext,
  139.              NIL, "V", NIL, MENUNULL]:menuitem
  140.   -> 'Copy' (key-equivalent: "C")
  141.   editItem:=[editItem, 0, 0, 0, 0,
  142.              ITEMTEXT OR COMMSEQ OR ITEMENABLED OR HIGHCOMP, 0,
  143.               [0, 1, RP_JAM2, 2, 1, NIL, 'Copy', NIL]:intuitext,
  144.              NIL, "C", NIL, MENUNULL]:menuitem
  145.   -> 'Cut' (key-equivalent: "X")
  146.   editItem:=[editItem, 0, 0, 0, 0,
  147.              ITEMTEXT OR COMMSEQ OR ITEMENABLED OR HIGHCOMP, 0,
  148.               [0, 1, RP_JAM2, 2, 1, NIL, 'Cut', NIL]:intuitext,
  149.              NIL, "X", NIL, MENUNULL]:menuitem
  150.  
  151.   -> Print Sub-Items
  152.   -> 'Draft'
  153.   prtItem:=[NIL, 0, 0, 0, 0,
  154.             ITEMTEXT OR ITEMENABLED OR HIGHCOMP, 0,
  155.              [0, 1, RP_JAM2, 2, 1, NIL, 'Draft', NIL]:intuitext,
  156.             NIL, NIL, NIL, MENUNULL]:menuitem
  157.   -> 'NLQ'
  158.   prtItem:=[prtItem, 0, 0, 0, 0,
  159.             ITEMTEXT OR ITEMENABLED OR HIGHCOMP, 0,
  160.              [0, 1, RP_JAM2, 2, 1, NIL, 'NLQ', NIL]:intuitext,
  161.             NIL, NIL, NIL, MENUNULL]:menuitem
  162.  
  163.   -> Uses the >> character to indicate a sub-menu item.
  164.   -> This is \273 Octal, 0xBB Hex or Alt-0 from the Keyboard.
  165.   ->
  166.   -> NOTE that standard menus place this character at the right margin of the
  167.   -> menu box.  This may be done by using a second IntuiText structure for the
  168.   -> single character, linking this IntuiText to the first one, and positioning
  169.   -> the IntuiText so that the character appears at the right margin.
  170.   -> GadTools library will provide the correct behavior.
  171.  
  172.   -> Project Menu Items
  173.   -> 'Quit' (key-equivalent: "Q")
  174.   projItem:=[NIL, 0, 0, 0, 0,
  175.              ITEMTEXT OR COMMSEQ OR ITEMENABLED OR HIGHCOMP, 0,
  176.               [0, 1, RP_JAM2, 2, 1, NIL, 'Quit', NIL]:intuitext,
  177.              NIL, "Q", NIL, MENUNULL]:menuitem
  178.   -> 'About...'
  179.   projItem:=[projItem, 0, 0, 0, 0,
  180.              ITEMTEXT OR ITEMENABLED OR HIGHCOMP, 0,
  181.               [0, 1, RP_JAM2, 2, 1, NIL, 'About...', NIL]:intuitext,
  182.              NIL, NIL, NIL, MENUNULL]:menuitem
  183.   -> 'Print' (has sub-menu)
  184.   projItem:=[projItem, 0, 0, 0, 0,
  185.              ITEMTEXT OR ITEMENABLED OR HIGHCOMP, 0,
  186.               [0, 1, RP_JAM2, 2, 1, NIL, 'Print     »', NIL]:intuitext,
  187.              NIL, NIL, prtItem, MENUNULL]:menuitem
  188.   -> 'Save As...' (key-equivalent: "A")
  189.   projItem:=[projItem, 0, 0, 0, 0,
  190.              ITEMTEXT OR COMMSEQ OR ITEMENABLED OR HIGHCOMP, 0,
  191.               [0, 1, RP_JAM2, 2, 1, NIL, 'Save As...', NIL]:intuitext,
  192.              NIL, "A", NIL, MENUNULL]:menuitem
  193.   -> 'Save' (key-equivalent: "S")
  194.   projItem:=[projItem, 0, 0, 0, 0,
  195.              ITEMTEXT OR COMMSEQ OR ITEMENABLED OR HIGHCOMP, 0,
  196.               [0, 1, RP_JAM2, 2, 1, NIL, 'Save', NIL]:intu