home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PSGUI130.ZIP / PGUIBUT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-01-01  |  13.6 KB  |  477 lines

  1. {
  2.  
  3.                                                       ╔══════════════════╗
  4.                                                       ║  PGUI Graphic    ║
  5.                                                       ║  Button Include  ║
  6.                                                       ║    Rev.  1.00    ║
  7.                                                       ╚══════════════════╝
  8.  
  9. }
  10.  
  11. Procedure DrwButtonUp(X,Y,Len,Depth,Thickness,Background:Word;
  12.                       Picture:Pointer;Name:String);
  13.  
  14. { ╔════════════════════════════════════════════════════════════════════════╗ }
  15. { ║  Draw Button of Variable Size.                                         ║ }
  16. { ║                                                                        ║ }
  17. { ║  Draw at X,Y with the specified Length, Depth, and Thickness.          ║ }
  18. { ║  The current Background colour must be specified.                      ║ }
  19. { ╚════════════════════════════════════════════════════════════════════════╝ }
  20.  
  21. Var
  22.    Shape:Array[1..8] of Word;
  23.  
  24. Begin
  25.   Mouse.Hide;
  26.   SetFillStyle(SolidFill,LightGray);
  27.   Bar(X,Y,X+Len,Y+Depth-1);
  28.   SetColor(Background);
  29.   Line(X,Y+Depth,X+Len,Y+Depth);
  30.   SetColor(Black);
  31.   SetFillStyle(Solidfill,White);
  32.   Shape[1] := X;
  33.   Shape[2] := Y;
  34.   Shape[3] := X+Len;
  35.   Shape[4] := Y;
  36.   Shape[5] := X+Len-Thickness;
  37.   Shape[6] := Y+Thickness;
  38.   Shape[7] := X+Thickness;
  39.   Shape[8] := Y+Thickness;
  40.   FillPoly(4,Shape);
  41.   Shape[1] := X;
  42.   Shape[2] := Y;
  43.   Shape[3] := X;
  44.   Shape[4] := Y+Depth;
  45.   Shape[5] := X+Thickness;
  46.   Shape[6] := Y+Depth-Thickness;
  47.   Shape[7] := X+Thickness;
  48.   Shape[8] := Y+Thickness;
  49.   FillPoly(4,Shape);
  50.   SetFillStyle(solidfill,DarkGray);
  51.   Shape[1] := X;
  52.   Shape[2] := Y+Depth;
  53.   Shape[3] := X+Len;
  54.   Shape[4] := Y+Depth;
  55.   Shape[5] := X+Len-Thickness;
  56.   Shape[6] := Y+Depth-Thickness;
  57.   Shape[7] := X+Thickness;
  58.   Shape[8] := Y+Depth-Thickness;
  59.   FillPoly(4,Shape);
  60.   Shape[1] := X+Len;
  61.   Shape[2] := Y+Depth;
  62.   Shape[3] := X+Len;
  63.   Shape[4] := Y;
  64.   Shape[5] := X+Len-Thickness;
  65.   Shape[6] := Y+Thickness;
  66.   Shape[7] := X+Len-Thickness;
  67.   Shape[8] := Y+Depth-Thickness;
  68.   FillPoly(4,Shape);
  69.   OutTextXY(X+(Len-8*Length(Name)) DIV 2,(Y+(Depth-8) DIV 2)+Thickness-2,Name);
  70.   Mouse.Show;
  71. End;
  72.  
  73. Procedure DrwButtonDown(X,Y,Len,Depth,Thickness:Word;Picture:Pointer;Name:String);
  74.  
  75. { ╔════════════════════════════════════════════════════════════════════════╗ }
  76. { ║  Draw Button of Variable Size Being Pressed.                           ║ }
  77. { ╚════════════════════════════════════════════════════════════════════════╝ }
  78.  
  79. Begin
  80.   Mouse.Hide;
  81.   SetFillStyle(SolidFill,LightGray);
  82.   Bar(X,Y,X+Len,Y+Depth);
  83.   SetFillStyle(Solidfill,DarkGray);
  84.   Bar(X,Y,X+Len,Y+Thickness);
  85.   Bar(X,Y,X+Thickness,Y+Depth);
  86.   SetFillStyle(Solidfill,White);
  87.   Bar(X+Thickness,Y+Thickness,X+Len,Y+Thickness);
  88.   Bar(X+Thickness,Y+Thickness,X+Thickness,Y+Depth);
  89.   SetColor(Black);
  90.   Line(X,Y+Depth,X+Len,Y+Depth);
  91.   Line(X+Len,Y,X+Len,Y+Depth);
  92.   OutTextXY(X+(Len-8*Length(Name))DIV 2+Thickness,Y+(Depth-8) DIV 2+Thickness,Name);
  93.   Mouse.Show;
  94. End;
  95.  
  96. Procedure DrawButtonUp(X1,Y1,X2,Y2,Thickness,Background:Word;Picture:Pointer;Name:String);
  97.  
  98. { ╔════════════════════════════════════════════════════════════════════════╗ }
  99. { ║  Draw Button of Variable Size.                                         ║ }
  100. { ╚════════════════════════════════════════════════════════════════════════╝ }
  101.  
  102. Begin
  103.   DrwButtonUp(X1,Y1,X2-X1+1,Y2-Y1+1,Thickness,Background,Picture,Name);
  104. End;
  105.  
  106. Procedure DrawButtonDown(X1,Y1,X2,Y2,Thickness:Word;Picture:Pointer;Name:String);
  107.  
  108. { ╔════════════════════════════════════════════════════════════════════════╗ }
  109. { ║  Draw Button of Variable Size Being Pressed.                           ║ }
  110. { ╚════════════════════════════════════════════════════════════════════════╝ }
  111.  
  112. Begin
  113.   DrwButtonDown(X1,Y1,X2-X1+1,Y2-Y1+1,Thickness,Picture,Name);
  114. End;
  115.  
  116. Procedure ButtonChain.Init;
  117. Begin
  118.   Root     :=NIL;
  119.   Total    :=0;
  120.   Buttons  :=NIL;
  121. End;
  122.  
  123. Function ButtonChain.Position:Word;
  124.  
  125. Var
  126.   B     :Pointer;
  127.   X     :Word;
  128.  
  129. Begin
  130.   X:=1;
  131.   B:=Buttons;
  132.   Buttons:=Root;
  133.  
  134.   While (Buttons<>NIL) And (Buttons^.Next<>NIL) And (B<>Buttons) do
  135.   Begin
  136.     Inc(X);
  137.     Buttons:=Buttons^.Next;
  138.   End;
  139.  
  140.   If (B<>Buttons) Or (B=NIL) Then Position:=0;
  141.   Buttons:=B;
  142. End;
  143.  
  144. Function ButtonChain.Number:Word;
  145. Begin
  146.   If Buttons=NIL Then
  147.     Number:=0
  148.   Else
  149.     Number:=Buttons^.Number;
  150. End;
  151.  
  152. Procedure ButtonChain.GotoPosition(Here:Word);
  153.  
  154. Var
  155.   X     :Word;
  156.  
  157. Begin
  158.   If Here=0 Then
  159.   Begin
  160.     Buttons:=NIL;
  161.     Exit;
  162.   End;
  163.  
  164.   X:=1;
  165.   Buttons:=Root;
  166.   While (Buttons<>NIL) And (Buttons^.Next<>NIL) And (X<Here) do
  167.   Begin
  168.     Buttons:=Buttons^.Next;
  169.     Inc(X);
  170.   End;
  171. End;
  172.  
  173. Procedure ButtonChain.GotoNumber(ButtonNumber:Word);    {NIL if not found}
  174. Begin
  175.   Buttons:=Root;
  176.   While (Buttons<>NIL) And (ButtonNumber<>Buttons^.Number) do
  177.     Buttons:=Buttons^.Next;
  178. End;
  179.  
  180. Function ButtonChain.NewButtonNumber:Word;
  181.  
  182. Var
  183.   Highest:Word;
  184.  
  185. Begin
  186.   Buttons:=Root;
  187.   Highest:=0;
  188.   While Buttons<>NIL do
  189.   Begin
  190.     Highest:=Buttons^.Number;
  191.     Buttons:=Buttons^.Next;
  192.   End;
  193.   If Highest>65500 Then
  194.   Begin
  195.     Repeat
  196.       Highest:=Random(65499)+1;
  197.       Buttons:=Root;
  198.       While (Buttons<>NIL) And (Highest<>0) do
  199.       Begin
  200.         If Highest=Buttons^.Number Then Highest:=0;
  201.         Buttons:=Buttons^.Next;
  202.       End;
  203.     Until Highest<>0;
  204.     NewButtonNumber:=Highest;
  205.   End
  206.   Else
  207.     NewButtonNumber:=Highest+1;
  208. End;
  209.  
  210. Procedure ButtonChain.Add(X1, Y1, X2, Y2:Word; Thickness,
  211.                           Background:Word;
  212.                           Picture:Pointer; Name:String;
  213.                           Special:Boolean; Key:Char);
  214.  
  215. { ╔════════════════════════════════════════════════════════════════════════╗ }
  216. { ║                                                                        ║ }
  217. { ║   The button is added to the list.  Button coordinates are at X1,Y1    ║ }
  218. { ║   to X2,Y2.  The equivalent key press is in Key.  Special is True      ║ }
  219. { ║   if the key returned if preceeded by a character 0.                   ║ }
  220. { ║                                                                        ║ }
  221. { ║   Background is the background colour, Picture is a possible           ║ }
  222. { ║   Button picture and it returns a the button's number.                 ║ }
  223. { ║                                                                        ║ }
  224. { ╚════════════════════════════════════════════════════════════════════════╝ }
  225.  
  226. Var
  227.   P   :ButtonListPtr;   {New Button}
  228.  
  229. Begin
  230.   New(P);
  231.   P^.X1         :=X1;
  232.   P^.Y1         :=Y1;
  233.   P^.X2         :=X2;
  234.   P^.Y2         :=Y2;
  235.   P^.Thickness  :=Thickness;
  236.   P^.Background :=Background;
  237.   P^.Picture    :=Picture;
  238.   P^.Name       :=Name;
  239.   P^.Number     :=NewButtonNumber;
  240.   P^.Special    :=Special;
  241.   P^.Key        :=Key;
  242.   P^.Next       :=NIL;
  243.  
  244.   GotoPosition(65535);
  245.  
  246.   If Root=NIL Then
  247.   Begin
  248.     Root:=P;
  249.     Buttons:=P;
  250.   End
  251.   Else
  252.   Begin
  253.     Buttons^.Next:=P;
  254.     Buttons:=P;
  255.   End;
  256.   Inc(Total);
  257. End;
  258.  
  259. Procedure ButtonChain.Create(X1, Y1, X2, Y2:Word; Thickness,
  260.                              Background:Word;
  261.                              Picture:Pointer; Name:String;
  262.                              Special:Boolean; Key:Char);
  263.  
  264. { ╔════════════════════════════════════════════════════════════════════════╗ }
  265. { ║   Displays a Button, and adds it to a Linked List of Buttons.          ║ }
  266. { ╚════════════════════════════════════════════════════════════════════════╝ }
  267.  
  268. Begin
  269.   Add(X1,Y1,X2,Y2,Thickness,Background,Picture,Name,Special,Key);
  270.   DrawButtonUp(X1,Y1,X2,Y2,Thickness,Background,Picture,Name);
  271. End;
  272.  
  273. Procedure ButtonChain.WaitForClick(Var X, Y:Word;Var MouseButtons:Byte;
  274.                                    Var Held,Doubled,Special:Boolean; Var Key:Char);
  275.  
  276. { ╔════════════════════════════════════════════════════════════════════════╗ }
  277. { ║                                                                        ║ }
  278. { ║  This procedure will return which of the currently active buttons      ║ }
  279. { ║  has been selected.  If a user presses the key equivalent of a         ║ }
  280. { ║  button or moves the mouse onto a button and clicks, the procedure     ║ }
  281. { ║  returns that button's key press.  It also checks for a double click.  ║ }
  282. { ║                                                                        ║ }
  283. { ║  If Key=#255 then no valid key was received.                           ║ }
  284. { ║                                                                        ║ }
  285. { ║  It will return the co-ordinates if no button was clicked on.          ║ }
  286. { ║                                                                        ║ }
  287. { ║  If a double click was selected, Double is set to True, else False.    ║ }
  288. { ║                                                                        ║ }
  289. { ║  Special is set to True if and only if a "Special" key was pressed.    ║ }
  290. { ║  A "Special" key is a key pressed that is preceeded by a NUL or        ║ }
  291. { ║  character 0.  Example, the cursor keys.                               ║ }
  292. { ║                                                                        ║ }
  293. { ║  Which mouse buttons are being pressed is also returned.               ║ }
  294. { ║                                                                        ║ }
  295. { ╚════════════════════════════════════════════════════════════════════════╝ }
  296.  
  297. Var
  298.   Z,W            :Word;
  299.   FoundAgain,
  300.   Found          :Boolean;      {Has the user pressed a button}
  301.   CheckDouble    :Pointer;
  302.   C              :Char;         {Key Pressed}
  303.  
  304. Begin
  305.   C      :=Chr(255);
  306.   Found  :=False;
  307.   Buttons:=Root;
  308.  
  309.   Begin
  310.     Special:=False;
  311.  
  312.     If Mouse.Active Then
  313.     Begin
  314.       Mouse.GetClick(X,Y,Z,W,MouseButtons,Held,Doubled);
  315.       While (Not Found) And (Buttons<>NIL) And (Not KeyPressed) do
  316.       Begin
  317.         If (X>=Buttons^.X1) And (X<=Buttons^.X2) And
  318.            (Y>=Buttons^.Y1) And (Y<=Buttons^.Y2) Then Found:=True;
  319.         If Not Found Then Buttons:=Buttons^.Next;
  320.       End;
  321.       If KeyPressed Then C:=UpCase(ReadKey);
  322.  
  323.       FoundAgain:=False;
  324.       CheckDouble:=Buttons;
  325.       If Doubled Then
  326.       Begin
  327.         Buttons:=Root;
  328.         While Not FoundAgain And (Buttons<>NIL) do
  329.         Begin
  330.           If (Z>=Buttons^.X1) And (Z<=Buttons^.X2) And
  331.              (W>=Buttons^.Y1) And (W<=Buttons^.Y2) Then FoundAgain:=True;
  332.           If Not FoundAgain Then Buttons:=Buttons^.Next;
  333.         End;
  334.         If CheckDouble<>Buttons Then
  335.         Begin
  336.           Doubled:=False;
  337.           If Buttons=NIL Then Buttons:=CheckDouble;
  338.         End;
  339.       End;
  340.     End;     {Now, for Mouse and No Mouse}
  341.  
  342.     If Not Found Then Buttons:=Root;
  343.     If Not Mouse.Active Then C:=UpCase(ReadKey);
  344.     If C=Chr(0) Then If KeyPressed Then
  345.     Begin
  346.       C:=ReadKey;
  347.       Special:=True;
  348.     End;
  349.     If C=Chr(0) Then C:=Chr(255);
  350.  
  351.     While Not Found And (Buttons<>NIL) do
  352.     Begin
  353.       If (C=Buttons^.Key) And (Special=Buttons^.Special) Then Found:=True;
  354.       If Not Found Then Buttons:=Buttons^.Next;
  355.     End;
  356.   End;
  357.  
  358.   If Found Then
  359.   Begin
  360.     Special:=Buttons^.Special;
  361.     Key    :=Buttons^.Key;
  362.   End
  363.   Else
  364.   Begin
  365.     Key    :=#255;
  366.     Buttons:=NIL;
  367.   End;
  368. End;
  369.  
  370. Procedure ButtonChain.DrawUp(ButtonNumber:Word);
  371. Begin
  372.   GotoNumber(ButtonNumber);
  373.   If Buttons=NIL Then Exit;
  374.   With Buttons^ do
  375.     DrawButtonUp(X1,Y1,X2,Y2,Thickness,Background,Picture,Name);
  376. End;
  377.  
  378. Procedure ButtonChain.DrawDown(ButtonNumber:Word);
  379. Begin
  380.   GotoNumber(ButtonNumber);
  381.   If Buttons=NIL Then Exit;
  382.   With Buttons^ do
  383.     DrawButtonDown(X1,Y1,X2,Y2,Thickness,Picture,Name);
  384. End;
  385.  
  386. Procedure ButtonChain.Move(X,Y:Integer;ButtonNumber:Word);
  387. Begin
  388.   GotoNumber(ButtonNumber);
  389.   If Buttons=NIL Then Exit;
  390.   Inc(Buttons^.X1,X);
  391.   Inc(Buttons^.X2,X);
  392.   Inc(Buttons^.Y1,Y);
  393.   Inc(Buttons^.Y2,Y);
  394. End;
  395.  
  396. Procedure ButtonChain.MoveAll(X,Y:Integer);
  397.  
  398. Var
  399.   OldBut  :Pointer;
  400.  
  401. Begin
  402.   OldBut :=Buttons;
  403.   Buttons:=Root;
  404.   While Buttons<>NIL do
  405.   Begin
  406.     Inc(Buttons^.X1,X);
  407.     Inc(Buttons^.X2,X);
  408.     Inc(Buttons^.Y1,Y);
  409.     Inc(Buttons^.Y2,Y);
  410.     Buttons:=Buttons^.Next;
  411.   End;
  412.   Buttons:=OldBut;
  413. End;
  414.  
  415. Procedure ButtonChain.KillFrom;
  416.  
  417. { ╔════════════════════════════════════════════════════════════════════════╗ }
  418. { ║  Removes *some* buttons from memory.                                   ║ }
  419. { ╚════════════════════════════════════════════════════════════════════════╝ }
  420.  
  421. Var
  422.   Q     :Pointer;
  423.  
  424. Begin
  425.   While Buttons<>NIL do
  426.   Begin
  427.     Q:=Buttons^.Next;
  428.     Dispose(Buttons);
  429.     Buttons:=Q;
  430.     Dec(Total);
  431.   End;
  432.   If Total=0 Then Root:=NIL;
  433. End;
  434.  
  435. Procedure ButtonChain.KillAll;
  436.  
  437. { ╔════════════════════════════════════════════════════════════════════════╗ }
  438. { ║  Removes all buttons from memory.                                      ║ }
  439. { ╚════════════════════════════════════════════════════════════════════════╝ }
  440.  
  441. Begin
  442.   Buttons:=Root;
  443.   KillFrom;
  444. End;
  445.  
  446. Procedure ButtonChain.KillOne;
  447.  
  448. { ╔════════════════════════════════════════════════════════════════════════╗ }
  449. { ║  Removes *one* button from memory, if it exists.                       ║ }
  450. { ╚════════════════════════════════════════════════════════════════════════╝ }
  451.  
  452. Var
  453.   Q     :ButtonListPtr;
  454.   Here  :Word;
  455.  
  456. Begin
  457.   Here:=Position;
  458.   If Here>Total Then Exit;
  459.   If Here=1 Then
  460.   Begin
  461.     Root:=Buttons^.Next;
  462.     Dispose(Buttons);
  463.     Buttons:=Root;
  464.     Dec(Total);
  465.   End
  466.   Else
  467.   Begin
  468.     GotoPosition(Here-1);
  469.     Q:=Buttons^.Next;
  470.     Buttons^.Next:=Buttons^.Next^.Next;
  471.     Dispose(Q);
  472.     Dec(Total);
  473.   End;
  474. End;
  475.  
  476. { Copyright 1993, Michael Gallias }
  477.