home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / misc / Gfx4PCQ.lha / WindowLib / Examples / WindowDemo / WindowDemo.p < prev    next >
Encoding:
Text File  |  1997-02-09  |  10.8 KB  |  348 lines

  1. Program WindowDemo;
  2. {This is a program that demonstrates some features of THOR's windowlib.
  3.  For more information on how this works read the include file
  4.  utils/windowlib.i
  5.  © 1997 THOR - Software}
  6.  
  7. {Include some useful stuff, and the windowlib itself}
  8. {$I "include:utils/windowlib.i"}
  9. {$I "include:utils/stringlib.i"}
  10.   
  11. {This is the demonstration part...}
  12. PROCEDURE Demo;
  13.     
  14. CONST
  15.     {Setup a menu bar in an simple way. The menu is created out of them.
  16.      The entries in this array are of a structure called MenuCommand,
  17.      each of them describing one menu item, a menu bar or controlling
  18.      the generation. The structure itself consists of:
  19.         1) the type of entry to be created:
  20.             a MC_MENU (menubar), an MC_ITEM (an item of the menu)
  21.             or an MC_SUBITEM (subitem of an item).
  22.             The last item or subitem of a menu must be a
  23.             MC_LASTSUBITEM/MC_LASTITEM or MC_LASTMENU resp.
  24.             to tell windowlib that one element of the menu is
  25.             now complete.
  26.         2)    some flags describing how this should look like.
  27.             They are either MC_NORMALMENU (ordinary menu bar)
  28.             or MC_NORMAL for a select-action menu item, or
  29.             MC_CHECKABLE to get a menu item that can be turned
  30.             on or off. MC_BAR is special in the sense that it
  31.             creates a separation bar.
  32.         3)    One character consisting of a shortcut (Amiga + key)
  33.         4)    A string to be appear in the menu itself.}    
  34.         
  35.     menubar : ARRAY[1..8] OF MenuCommand=(
  36.         (MC_MENU,MC_NORMALMENU,'\0',0,"Menu 1"),
  37.          (MC_ITEM,MC_NORMAL,'\0',0,"Item 1"),
  38.          (MC_ITEM,MC_NORMAL,'\0',0,"Item 2"),
  39.          (MC_ITEM,MC_CHECKABLE,'\0',0,"Onoff"),
  40.          (MC_ITEM,MC_BAR,'\0',0,""),
  41.          (MC_ITEM,MC_NORMAL,'Q',0,"Exit"),
  42.          (MC_LASTITEM,0,'\0',0,""),
  43.         (MC_LASTMENU,0,'\0',0,""));
  44.         
  45. VAR
  46.     x,y    :    SHORT;
  47.     i    :    SHORT;
  48.     s    :    ScreenPtr;
  49.     w    :    WindowPtr;
  50.     g1,g2,g3,g4,g5:    GadgetPtr;
  51.     t    :    INTEGER;
  52.     b    :    BOOLEAN;
  53.     buf    :    ARRAY[0..15] OF CHAR;
  54.     ex    :    BOOLEAN;
  55.     menu,item,subitem : SHORT;
  56.  
  57.     
  58. BEGIN
  59.     InitGraphics;        {setup gfx system}
  60.  
  61.     {open a custom screen. Arguments are:
  62.         left and top edge of the screen, width, height and
  63.         depth of bitplanes (2^depth=# of colors)
  64.         the monitor ID - here plain NTSC in HIRES
  65.         and a title}
  66.     s:=OpenAScreen(0,0,640,300,2,MON_NTSC OR MON_HIRES,"Test screen");
  67.  
  68.     {choose the pen 3 to be fire-brigade red. Arguments are
  69.      the screen, the number of the pen and the red,green and blue
  70.      components of the new color, on a range from 0 (min) to 15 (max).
  71.      The windowlib does not support 16 bit colors...}
  72.     SetColor(s,3,15,0,0);
  73.     {please note that this is bad style since we haven't tested if the
  74.      screen did open. Sigh}
  75.         
  76.     {open a window on this screen. The arguments are
  77.      the screen, the position (left/top coordinates) the
  78.      width and heights and some flags which should be left to
  79.      14 for almost all reasons, and a string giving the title.}
  80.     w:=OpenScreenWindow(s,0,20,640,240,2+4+8,"A Test");
  81.     
  82.     IF w<>NIL THEN BEGIN
  83.         {setup a font to be used in this window. In this case,
  84.          it's topaz.9. Choose something better if you like...}
  85.         b:=SetWindowFont(w,"topaz.font",9);
  86.  
  87.         SetStyle(w,4);    {Set the style for the text to be printed:
  88.             1 is underlined, 2 is bold, 4 is italic.
  89.             Add the values for style combinations}
  90.  
  91.         Color(w,1);    {chose a pen to be used}
  92.  
  93.         {draw a nifty graphics}        
  94.         FOR i:=0 TO 10 DO BEGIN
  95.             Plot(w,0,i*24);        {plot a point}
  96.             DrawTo(w,i*64,240);    {draw a line from it to this position}
  97.         END;
  98.     
  99.         {wait until the user clicks into the window}
  100.         WaitForClick(w);
  101.  
  102.         {clear the window}
  103.         ClearWindow(w);
  104.  
  105.         {draw an ellipse. Arguments are the center and the two
  106.          radii}
  107.         Ellipse(w,320,120,300,150);
  108.  
  109.         {fill it in a different color}
  110.         Color(w,2);
  111.         Fill(w,320,120);
  112.     
  113.  
  114.         {wait until the user clicks into the window}
  115.         WaitForClick(w);
  116.         ClearWindow(w);        {clear it}
  117.     
  118.         Color(w,1);        {choose pen}
  119.         {another nice gfx with ellipses}    
  120.         FOR i:=0 TO 10 DO
  121.             Ellipse(w,320,120,i*20,100-i*10);        
  122.  
  123.         WaitForClick(w);
  124.         ClearWindow(w);        {wait and clear}
  125.  
  126.         {turn on boundary drawing. This affects filled boxes
  127.          and ellipses.}
  128.         Boundary(w,TRUE);
  129.         OlColor(w,1);            {choose the pen for the boundary}
  130.         FOR i:=0 TO 10 DO BEGIN
  131.             Color(w,i);        {choose the pen for the interiour}
  132.             PBox(w,i*32,i*12,640-i*32,240-i*12);    {draw a filled, framed rectangle}
  133.         END;
  134.     
  135.         Boundary(w,FALSE);        {turn off boundary drawing again}
  136.         WaitForClick(w);        {wait until the user clicks into the window}
  137.         ClearWindow(w);            {clear it}
  138.     
  139.         {draw the same, but not filled}
  140.         FOR i:=0 TO 10 DO BEGIN
  141.             Color(w,i);        {chose color}
  142.             Box(w,i*32,i*12,640-i*32,240-i*12);    {draw a frame}
  143.         END;
  144.  
  145.         WaitForClick(w);
  146.         ClearWindow(w);        {wait and clear}
  147.  
  148.         Boundary(w,TRUE);    {again boundary mode}    
  149.         OlColor(w,1);        {color for the boundary}    
  150.     
  151.         FOR i:=0 TO 10 DO BEGIN
  152.             Color(w,i);    {color for the interiour}
  153.             PEllipse(w,320,120,i*20,100-i*10);    {draw a filled, framed ellipse}        
  154.         END;
  155.  
  156.         {some interactive selection functions}
  157.  
  158.         {let the user select one point of the window with a cross}
  159.         SelectPoint(w,x,y);
  160.  
  161.         {let the user select a region of the window by "dragging" it}
  162.         DragBox(w,x,y,x,y);    
  163.  
  164.         {we do actually nothing with them, just for demonstration}
  165.  
  166.  
  167.         {tell the windowlib we want to hear if the user presses a
  168.          mouse button}        
  169.         RequestStart(w,MOUSEBUTTONS_F);
  170.         REPEAT
  171.             Mouse(w,x,y);    {read the mouse position}
  172.             Plot(w,x,y);    {plot a point at this position}
  173.         UNTIL NextRequest(w)=MOUSEBUTTONS_F;    {until the mouse button gets pressed}
  174.         {turn off notification}
  175.         RequestEnd(w,MOUSEBUTTONS_F);
  176.             
  177.         {create some gadgets. A text button is just a button with
  178.          some text in it. It is called a "bool gadget" in intution
  179.          terms. The user can press it to force some action.
  180.          Arguments are: the window, the x and y coordinates of its
  181.          position, the width and height - beeing zero here to tell
  182.          the windowlib to calculate them by the text they should
  183.          contain and the text itself}
  184.         g1:=CreateTextButton(w,4,04,0,0,"Demo gadget");
  185.         g2:=CreateTextButton(w,4,20,0,0,"Another one");
  186.  
  187.         {create a string field: This is a rectangular area where
  188.          the user may insert text. These are called "string gadgets"
  189.          by the intuition part of the OS.
  190.          The arguments are the window, the position x and y and the
  191.          width and height. The height is set to zero to tell window
  192.          lib to fiddle this out by itself - the window's font is
  193.          used here.}
  194.         g3:=CreateStringField(w,4,40,100,0);
  195.  
  196.         {create a slider. They are called "prop gadgets" in intuition
  197.          language and let users select a choise out of a list of
  198.          items. Arguments are the window, the position x,y, the
  199.          width and height, and a flag beeing FALSE for horizontal and
  200.          TRUE for vertical movement. The size of the list = the number
  201.          of choices is setup later.}
  202.         g4:=CreateSlider(w,4,60,100,8,FALSE);
  203.  
  204.         {create a toggle gadget. This is also a "bool gadget" in
  205.          intuition notation. This is a gadget that can be toggled
  206.          "on" or "off" by the user by pressing it.}
  207.         g5:=CreateTextToggle(w,4,80,0,0,"Toggle");
  208.  
  209.         {note that we did some bad style above since we never
  210.          checked if the gadgets could have been created}    
  211.  
  212.         {create a menu and attach it to the window. The definition
  213.          of the menu is done by a structure you see on top of this
  214.          procedure}
  215.         CreateMenu(w,@menubar);
  216.  
  217.         {setup the slider g4 to describe a list of 16 items (=16
  218.          possible choices), one beeing visible at a time, with 0
  219.          beeing the initial choice. The choices returned by window-
  220.          lib are numbered from 0 to 16}
  221.         SetSlider(g4,16,1,0);
  222.  
  223.         {display the cursor in the string field created on top
  224.          and let the user enter something}
  225.         b:=ActivateField(g3);
  226.  
  227.  
  228.         {tell the windowlib we want to get informed if:
  229.             the user wants to close the window
  230.             the user releases a gadget,i.e. the sliders, the buttons
  231.             or the text field
  232.             the user presses a mouse button.
  233.             the user selected a menu.
  234.             the user pressed a key}        
  235.         RequestStart(w,CLOSEWINDOW_f OR GADGETUP_f OR MOUSEBUTTONS_F OR MENUPICK_f OR RAWKEY_f);
  236.  
  237.         Color(w,1);    {choose the pen}
  238.         ex:=FALSE;    {flag if we should exit}
  239.         REPEAT
  240.             {if the mouse button is pressed, read the
  241.              mouse position and draw a point at it.
  242.              read the next event in this case}
  243.             IF MouseButton(w) THEN BEGIN
  244.                 Mouse(w,x,y);
  245.                 Plot(w,x,y);
  246.                 t:=NextRequest(w);
  247.             END ELSE BEGIN
  248.                 {else redraw the gadgets since the
  249.                  user might have drawn over them}
  250.                 RefreshButton(g1);
  251.                 RefreshButton(g2);
  252.                 RefreshButton(g3);
  253.                 RefreshButton(g4);
  254.                 RefreshButton(g5);
  255.                 {and wait for something to be happen}
  256.                 t:=WaitRequest(w);
  257.             END;
  258.                 
  259.             {now look what has happend, if at all}
  260.  
  261.             IF t=GADGETUP_f THEN BEGIN
  262.                 {a gadget has been released}
  263.                 
  264.                 {setup a drawing position}
  265.                 Position(w,50,120);
  266.  
  267.                 {get the number of the gadget that has been
  268.                  pressed. They are numbered from one
  269.                  in increasing order of their creation}
  270.                 CASE LastGadgetID(w) OF
  271.                 1:    {draw some text if one of the buttons has been released}
  272.                     DrawText(w,"Gadget 1");    
  273.                 2:
  274.                     DrawText(w,"Gadget 2");
  275.                 3:    {get the contents of the string field and draw it}
  276.                     DrawText(w,BufferFromField(g3));
  277.                 4:    BEGIN    {read the position of the slider, convert it to text and draw it}
  278.                         t:=IntToStr(@buf,FirstFromSlider(g4));
  279.                         DrawText(w,@buf);
  280.                     END;
  281.                 5:    BEGIN    {the on/off gadget has changed its state. Read it and display it}
  282.                         IF GetToggle(g5) THEN BEGIN
  283.                             DrawText(w,"On ");
  284.                         END ELSE
  285.                             DrawText(w,"Off");
  286.                     END;
  287.                 END;
  288.             END;
  289.             IF t=CLOSEWINDOW_f THEN
  290.                 {if we got the message that the user wants to
  291.                  shutdown, set a flag}
  292.                 ex:=TRUE;
  293.             IF t=RAWKEY_f THEN BEGIN
  294.                 {read the key that has been pressed into a buffer,
  295.                  and print this buffer.}
  296.                 LastKey(w,@buf,i);
  297.                 Position(w,50,90);
  298.                 DrawText(w,@buf);
  299.             END;
  300.             IF t=MENUPICK_f THEN BEGIN
  301.                 {a menu has been selected, read which one.
  302.                  The number returned is the menu number, the item
  303.                  number and the subitem number resp.
  304.                   They are numbered from 0 increasing in the
  305.                  order in the menu bar or menu. The item or
  306.                  subitem is -1 if it has not been selected}
  307.                 LastMenu(w,menu,item,subitem);
  308.                 IF item=4 THEN
  309.                     ex:=TRUE;    {"exit" choosen?}
  310.                 IF item=2 THEN BEGIN
  311.                     {check the state of the checkmark item.
  312.                      The numbers given here are again the
  313.                      number of the menu, of the item and of
  314.                      the subitem as described above. -1 means
  315.                      that this item has no subitem}                
  316.                     IF CheckMarkOfMenu(w,0,2,-1) THEN BEGIN
  317.                         {enable the menu item #1, i.e.
  318.                          make it selectable}
  319.                         OnMenuPoint(w,0,1,-1);
  320.                     END ELSE
  321.                         {disable menu item #1 in the first
  322.                          menu, i.e. forbid selection}
  323.                         OffMenuPoint(w,0,1,-1);
  324.                     {set the state of the toggle gadget by the
  325.                      state of this gadget}
  326.                     SetToggle(g5,CheckMarkOfMenu(w,0,2,-1));
  327.                 END;
  328.             END;
  329.         UNTIL ex;        {repeat until the user had enough}
  330.  
  331.         {tell windowlib we no longer want to receive messages
  332.          from this window}
  333.         RequestEnd(w,CLOSEWINDOW_f OR GADGETUP_f);
  334.     
  335.         {close this window}
  336.         CloseAWindow(w);
  337.     END;
  338.     {close the screen}
  339.     CloseAScreen(s);
  340.     {quit the gfx system}
  341.     ExitGraphics;
  342. END;
  343.  
  344. {a tiny main program}
  345. BEGIN
  346.     Demo;
  347. END.
  348.