home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 January / Pcwk0198.iso / Dcomplib / TOOLPANL.LZH / TOOLBAR.PAS
Pascal/Delphi Source File  |  1996-10-21  |  3KB  |  157 lines

  1. { This is my first try on a floating toolbar         }
  2. { Please note that the buttons must be the same size }
  3. { Comments are welcome to gda@hol.gr                 }
  4. { Version 0.0.1                                      }
  5.  
  6. unit toolbar;
  7. interface
  8. uses
  9.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  10.   Forms, Dialogs, ExtCtrls;
  11.  
  12. type
  13.   TToolPanel = class( TPanel )
  14.   private
  15.   protected
  16.      procedure Horizontal;
  17.      procedure Vertical;
  18.      procedure AsForm;
  19.      procedure AlignLeft;
  20.      procedure AlignRight;
  21.      procedure AlignTop;
  22.      procedure AlignBottom;
  23.      procedure WMMouseDown(Var Msg: TMessage); message wm_LButtonDown;
  24.   public
  25.      constructor Create( AOwner: TComponent ); override;
  26.   published
  27.   end;
  28.  
  29. procedure Register;
  30.  
  31. implementation
  32.  
  33. constructor TToolPanel.Create( AOwner: TComponent );
  34. begin
  35.   inherited Create( AOwner );
  36.   Caption := '';
  37. end;
  38.  
  39. procedure TToolPanel.Horizontal;
  40. Var
  41.   ctl: TWinControl;
  42.   i,x: Integer;
  43.   w,h: Integer;
  44. begin
  45.   If ControlCount = 0 then Exit;
  46.   Width := TWinControl(Controls[0]).Width + 4;
  47.   Height := TWinControl(Controls[0]).Height + 4;
  48.   x := 2;
  49.   for i := 0 to ControlCount - 1 do
  50.   begin
  51.     ctl := TWinControl( Controls[i] );
  52.     ctl.Left := x;
  53.     ctl.Top := 2;
  54.     inc(x, ctl.Width);
  55.   end;
  56. end;
  57.  
  58. procedure TToolPanel.Vertical;
  59. Var
  60.   ctl: TWinControl;
  61.   i,y: Integer;
  62.   w,h: Integer;
  63. begin
  64.   If ControlCount = 0 then Exit;
  65.   Width := TWinControl(Controls[0]).Width + 4;
  66.   Height := TWinControl(Controls[0]).Height + 4;
  67.   y := 2;
  68.   for i := 0 to ControlCount - 1 do
  69.   begin
  70.     ctl := TWinControl( Controls[i] );
  71.     ctl.Left := 2;
  72.     ctl.Top := y;
  73.     inc(y, ctl.Height);
  74.   end;
  75. end;
  76.  
  77. procedure TToolPanel.AsForm;
  78. Var
  79.   ctl: TWinControl;
  80.   i,j,x,y: Integer;
  81.   w,h: Integer;
  82. begin
  83.   If ControlCount = 0 then Exit;
  84.   Align := AlNone;
  85.   w := 3;
  86.   h := ControlCount div w;
  87.   Width := TWinControl(Controls[0]).Width * w + 10;
  88.   Height := TWinControl(Controls[0]).Height * h + 20;
  89.   x := 2; y := 16; j := 1;
  90.   for i := 0 to ControlCount - 1 do
  91.   begin
  92.     ctl := TWinControl( Controls[i] );
  93.     ctl.Left := x;
  94.     ctl.Top := y;
  95.     inc(x, ctl.Width);
  96.     if j mod w = 0 then
  97.     begin
  98.       inc(y, ctl.Height);
  99.       x := 2;
  100.     end;
  101.     inc(j);
  102.   end;
  103.   Align := AlNone;
  104. end;
  105.  
  106. procedure TToolPanel.AlignLeft;
  107. begin
  108.   Vertical;
  109.   Align := alLeft;
  110.   Width := TWinControl(Controls[0]).Width + 6;
  111. end;
  112.  
  113. procedure TToolPanel.AlignRight;
  114. begin
  115.   Vertical;
  116.   Align := alRight;
  117.   Width := TWinControl(Controls[0]).Width + 6;
  118. end;
  119.  
  120. procedure TToolPanel.AlignTop;
  121. begin
  122.   Horizontal;
  123.   Align := alTop;
  124.   Height := TWinControl(Controls[0]).Height + 6;
  125. end;
  126.  
  127. procedure TToolPanel.AlignBottom;
  128. begin
  129.   Horizontal;
  130.   Align := alBottom;
  131.   Height := TWinControl(Controls[0]).Height + 6;
  132. end;
  133.  
  134. procedure TToolPanel.WMMouseDown(Var Msg: TMessage);
  135. Const SC_DragMove = $F012;
  136. begin
  137.   ReleaseCapture;
  138.   perform(WM_SysCommand, SC_DragMove, 0);
  139.   if (Top < 0) then AlignTop
  140.   else
  141.   if (Top+Height > (Owner as TWinControl).Height) Then AlignBottom
  142.   else
  143.   if (Left < 0) then AlignLeft
  144.   else
  145.   if (Left+Width > (Owner as TWinControl).Width) then AlignRight
  146.   else
  147.     AsForm;
  148. end;
  149.  
  150. procedure Register;
  151. begin
  152.   RegisterComponents('Andreas', [TToolPanel]);
  153. end;
  154.  
  155. end.
  156.  
  157.