home *** CD-ROM | disk | FTP | other *** search
/ Windoware / WINDOWARE_1_6.iso / demos / tgtp60 / tutor.com / BUTTONS.TXT next >
Text File  |  1991-08-16  |  11KB  |  385 lines

  1. BUTTONS
  2. -------------------------------------------------------------
  3.  
  4. Buttons are things that you attach to frames. Pressing a button
  5. (by pointing with the mouse cursor and pressing the left
  6. mouse button) is an event, so a button must have an associated
  7. event-handler.
  8.  
  9.  
  10. Procedure DefineSquareButtonText(ifs : ImageStkPtr;
  11.                                  x1, y1, x2, y2, rx, ry: Word;
  12.                                  Msg: String; Event: CallProc);
  13.  
  14. This will draw a square button on the frame ifs. x1, y1, x2, y2 are
  15. relative coordinates on the frame where the button will be drawn.
  16. rx and ry are offsets to draw the text msg at. Event is the
  17. event-handler to call when the button is pressed.
  18.  
  19. Function VisualSquareButtonPress(ifs: ImageStkPtr; ms: MsClickPtr): Boolean;
  20.  
  21. This function presses a squarebutton down and then returns TRUE if
  22. the mouse button was released while the mouse pointer was over the button.
  23. This function should be called from within an event-handler and would
  24. be passed the same parameters that were passed to the event-handler.
  25.  
  26. The event-handler would use the return value to determine if an action
  27. is required (i.e. if TRUE). If so then the first thing that should be
  28. done is a call to ReleaseSquareButton which will pop the button back
  29. up.
  30.  
  31. If VisualSquareButtonPress returns FALSE then ReleaseSquareButton is
  32. called automatically and the event-handler should not call it.
  33.  
  34.  
  35. Procedure ReleaseSquareButton(ifs: ImageStkPtr; ms: MsClickPtr);
  36.  
  37. This will release a button that was previously pressed by
  38. VisualSquareButtonPress.
  39.  
  40.  
  41. This example shows the complete use of a button attached to a frame.
  42. When the button is pressed and released then the frame is disposed
  43. of.
  44.  
  45. BEGINFILE> button1.pas
  46. {-- button1.pas}
  47. {$F+}
  48.  
  49. USES teglunit,teglmain;
  50.  
  51. VAR ifs : ImageStkPtr;
  52.  
  53. function cancelevent(frame: ImageStkPtr; mouse: MsClickPtr): Word;
  54.   BEGIN
  55.     {-- if true then the button has been released }
  56.     if VisualSquareButtonPress(frame,mouse) then
  57.       begin
  58.         {-- this will pop the button back up }
  59.         ReleaseSquareButton(frame,mouse);
  60.         {-- and dispose of the frame. }
  61.         dropstackimage(ifs);
  62.       end;
  63.   END;
  64.  
  65. BEGIN
  66.   easytegl;
  67.   easyout;
  68.   PushImage(50,50,200,150);
  69.   ShadowBox(50,50,200,150);
  70.   ifs := StackPtr;
  71.   DefineSquareButtonText(ifs,30,35,120,65,5,5,'PRESS ME',cancelevent);
  72.   TeglSupervisor;
  73. END.
  74.  
  75.  
  76. ENDFILE>
  77.  
  78.  
  79. Buttons can have more than text displayed on them. In true GUI fashion
  80. buttons might have appropriate icons displayed on them to indicate
  81. their purpose.
  82.  
  83. Procedure DefineSquareButtonClick(ifs: ImageStkPtr;
  84.                                   x1, y1, x2, y2, rx, ry: Word;
  85.                                   button: pointer; event: CallProc);
  86.  
  87. DefineSquareButtonClick is the same as DefineSquareButtonText above
  88. except that an icon is displayed on the button instead of text.
  89.  
  90. This example creates a frame with squarebutton that shows a
  91. lightbulb icon.
  92.  
  93.  
  94. BEGINFILE> button2.pas
  95. {-- button2.pas}
  96. {$F+}
  97.  
  98. USES moreicon,teglunit,teglmain;
  99.  
  100. VAR ifs : ImageStkPtr;
  101.  
  102. function cancelevent(frame: ImageStkPtr; mouse: MsClickPtr): Word;
  103.   BEGIN
  104.     {-- if true then the button has been released }
  105.     if VisualSquareButtonPress(frame,mouse) then
  106.       begin
  107.         {-- this will pop the button back up }
  108.         ReleaseSquareButton(frame,mouse);
  109.         {-- and dispose of the frame. }
  110.         dropstackimage(ifs);
  111.       end;
  112.   END;
  113.  
  114. BEGIN
  115.   easytegl;
  116.   easyout;
  117.   PushImage(50,50,200,150);
  118.   ShadowBox(50,50,200,150);
  119.   ifs := StackPtr;
  120.   DefineSquareButtonClick(ifs,30,35,120,65,35,5,@ImageBulb,cancelevent);
  121.   TeglSupervisor;
  122. END.
  123.  
  124.  
  125. ENDFILE>
  126.  
  127.  
  128. It may be useful at some time to have a button floating around by itself.
  129. The easyout button that is displayed at the bottom right corner of the screen
  130. is just such a button. In fact it is a frame with a button that is the full
  131. size of the frame. We can use these for other purposes.
  132.  
  133. This example places two floating buttons on the screen, one with
  134. a 5.25" floppy disk icon and the other with a 3.5" floppy disk icon.
  135.  
  136. BEGINFILE> button3.pas
  137. {-- button3.pas}
  138. {$F+}
  139.  
  140. USES moreicon,teglunit,teglmain;
  141.  
  142.  
  143. function Disk35event(frame: ImageStkPtr; mouse: MsClickPtr): Word;
  144.   BEGIN
  145.     {-- if true then the button has been released }
  146.     if VisualSquareButtonPress(frame,mouse) then
  147.       begin
  148.         {-- this will pop the button back up }
  149.         ReleaseSquareButton(frame,mouse);
  150.       end;
  151.   END;
  152.  
  153. function Diskevent(frame: ImageStkPtr; mouse: MsClickPtr): Word;
  154.   BEGIN
  155.     {-- if true then the button has been released }
  156.     if VisualSquareButtonPress(frame,mouse) then
  157.       begin
  158.         {-- this will pop the button back up }
  159.         { ReleaseSquareButton(frame,mouse); }
  160.       end;
  161.   END;
  162.  
  163.  
  164.  
  165. BEGIN
  166.   easytegl;
  167.   easyout;
  168.   PushImage(0,09,39,49);
  169.   DefineSquareButtonClick(stackptr,0,0,39,39,5,5,@ImageDisk,diskevent);
  170.   PushImage(0,49,39,89);
  171.   DefineSquareButtonClick(stackptr,0,0,39,39,5,5,@ImageDisk35,disk35event);
  172.  
  173.   TeglSupervisor;
  174. END.
  175.  
  176.  
  177. ENDFILE>
  178.  
  179.  
  180. One of the things that can go wrong is forgetting to release a
  181. square button once it is pressed. Try commenting out one of the
  182. ReleaseSquareButton calls from the previous program and see what
  183. happens when the button is pressed repeatedly.
  184.  
  185.  
  186.  
  187. The next example gives a complete illustration of a floating button that
  188. opens up a window to do something. Important items to note here are:
  189.  
  190.   * the use of a control variable to keep track of a frame that you
  191.     don't want more than one instance running.
  192.   * setting the viewport to cover a frame, you don't have to calculate
  193.     the relative displacement for text or graphics then.
  194.   * using more than one button on a frame. Each with its own event.
  195.  
  196.  
  197. BEGINFILE> button4.pas
  198. {-- button4.pas}
  199. {$F+}
  200.  
  201. USES teglfont,moreicon,fastgrph,tgraph,teglunit,teglmain;
  202.  
  203.  
  204. {-- a control variable to make sure that only one instance of the }
  205. {-- format frame is displayed }
  206.  
  207. CONST
  208.   FormatInstance : Boolean = FALSE;
  209.  
  210. function FormatEvent(frame: ImageStkPtr; Mouse: MsClickPtr): Word;
  211.   BEGIN
  212.     if VisualSquareButtonPress(frame,mouse) then
  213.       BEGIN
  214.         ReleaseSquareButton(frame,mouse);
  215.         SetViewPort(0,0,Getmaxx,getmaxy,clipoff);
  216.         ErrMess(GetMaxx DIV 4,GetMaxy DIV 4,'Function not compete!');
  217.       END;
  218.   END;
  219.  
  220. function CancelEvent(frame: ImageStkPtr; Mouse: MsClickPtr): Word;
  221.   BEGIN
  222.     if VisualSquareButtonPress(frame,mouse) then
  223.       BEGIN
  224.         ReleaseSquareButton(frame,mouse);
  225.         DropStackImage(frame);
  226.         FormatInstance := FALSE;
  227.       END;
  228.   END;
  229.  
  230.  
  231. function Diskevent(frame: ImageStkPtr; mouse: MsClickPtr): Word;
  232.   VAR ifs : ImageStkPtr;
  233.  
  234.   BEGIN
  235.     {-- if true then the button has been released }
  236.     if VisualSquareButtonPress(frame,mouse) then
  237.       begin
  238.         {-- this will pop the button back up }
  239.         ReleaseSquareButton(frame,mouse);
  240.         if FormatInstance THEN
  241.           BEGIN
  242.             SetViewPort(0,0,GetMaxx,GetMaxy,clipoff);
  243.             ErrMess(200,100,'The format window is open');
  244.             exit;
  245.           END;
  246.         FormatInstance := TRUE;
  247.         SetViewPort(0,0,getmaxx,getmaxy,clipoff);  {-- fullscreen }
  248.         QuickFrame(ifs, 100, 100,   {-- upper corner }
  249.                         200, 100);  {-- width & height }
  250.         SetTeglFont(@Font14);
  251.         {-- setting the viewport to the frame makes it easy }
  252.         {-- for outputting text and graphics. }
  253.         PrepareForUpdate(ifs);
  254.         SetViewPort(ifs^.x,ifs^.y,ifs^.x1, ifs^.y1,clipon);
  255.         SetTextJustify(centertext,toptext);
  256.         OutTextXY(100,20,'Format a disk?');
  257.         SetTextJustify(lefttext,toptext);
  258.         CommitUpdate;
  259.  
  260.         DefineSquareButtonText(ifs,20,70,60,90,10,5,'OK',FormatEvent);
  261.         DefineSquareButtonText(ifs,110,70,180,90,10,5,'CANCEL',CancelEvent);
  262.  
  263.       end;
  264.   END;
  265.  
  266.  
  267.  
  268. BEGIN
  269.   easytegl;
  270.   easyout;
  271.   setautorotate(TRUE);
  272.   PushImage(0,09,39,49);
  273.   DefineSquareButtonClick(stackptr,0,0,39,39,6,4,@ImageDisk,diskevent);
  274.   SetTeglFont(@f5x6norm);
  275.   SetColor(Black);
  276.   OutTextXy(4,33,'Format');
  277.   OutTextXy(9,40,'Disk');
  278.   TeglSupervisor;
  279. END.
  280.  
  281.  
  282. ENDFILE>
  283.  
  284. This example illustrates how mouse click numbers are associated
  285. with a button. It draws frame with fifty number buttons on it that
  286. display their button numbers when clicked on.
  287.  
  288. This shows how easy it is to set up a matrix of buttons. The important
  289. part of this program is how the event-handler (ShowNumber) can determine
  290. which button was pressed. Consider how straight forward it would be
  291. to implement a calculator.
  292.  
  293.  
  294. BEGINFILE> button5.pas
  295. {-- button5.pas}
  296. {$F+}
  297.  
  298. USES teglfont,moreicon,fastgrph,tgraph,teglunit,teglmain;
  299.  
  300.  
  301. function itos(i : Integer): String;
  302.   VAR s : String;
  303.   BEGIN
  304.     str(i,s);
  305.     itos := s;
  306.   END;
  307.  
  308. function ShowNumber(frame: ImageStkPtr; mouse: MsClickPtr): Word;
  309.   BEGIN
  310.     {-- if true then the button has been released }
  311.     if VisualSquareButtonPress(frame,mouse) then
  312.       begin
  313.         {-- this will pop the button back up }
  314.         ReleaseSquareButton(frame,mouse);
  315.         PrepareForUpdate(frame);
  316.         SetViewPort(frame^.x,frame^.y,frame^.x1,frame^.y1,clipon);
  317.         setfillstyle(solidfill,white);
  318.         bar(5,110,210,130);
  319.         setteglfont(@f8x12bol);
  320.         {-- the mouse pointer contains what clicknumber it was }
  321.         {-- assigned to. }
  322.         OutTextXY(5,112,'Button '+itos(mouse^.clickNumber));
  323.         CommitUpDate;
  324.       end;
  325.   END;
  326.  
  327. Procedure ButtonBunch;
  328. VAR i,xd,yd : integer;
  329.     ifs : ImageStkPtr;
  330.   BEGIN
  331.     SetViewPort(0,0,Getmaxx,getmaxy,clipoff);
  332.     QuickFrame(ifs,100,100,222,140);
  333.     xd := 0; yd := 0;
  334.     for i := 1 to 50 do
  335.       BEGIN
  336.         {-- as we define each button we put its clicknumber on it}
  337.         {-- note that MsClickCount would only be the correct on return}
  338.         {-- so we add one to it. }
  339.         SetTeglFont(@f7x7bold);
  340.         DefineSquareButtonText(ifs,xd,yd,xd+20,yd+20,3,8,
  341.             itos(ifs^.MsclickCount+1),ShowNumber);
  342.         xd := xd + 22;
  343.         if xd > 200 then
  344.           begin
  345.             xd := 0;
  346.             yd := yd + 22;
  347.           end;
  348.       END;
  349.   END;
  350.  
  351.  
  352. BEGIN
  353.   easytegl;
  354.   easyout;
  355.   buttonbunch;
  356.   TeglSupervisor;
  357. END.
  358.  
  359.  
  360. ENDFILE>
  361.  
  362.  
  363. Things to remember!
  364.  
  365.     * Square button arguments are RELATIVE to the frame they are being
  366.       attached to.
  367.  
  368.     * A square button is just another mouse click on a frame. The
  369.       ImageStkPtr that is passed to DefineSquareButtonClick or
  370.       DefineSquareButtonText will contain the clicknumber of the
  371.       on return in the MsClickCount field.
  372.  
  373.     * You can make floating buttons by making the button and the frame
  374.       the same size.
  375.  
  376.     * Beware of viewport settings. Many routines assume that the
  377.       viewport and text settings are the startup defaults. If in doubt
  378.       do a SetViewPort(0,0,Getmaxx,Getmaxy,Clipoff) and
  379.       SetTextJustify(LeftText,TopText).
  380.  
  381. ----------------------------------------------------------
  382. END buttons.txt
  383.  
  384.  
  385.