home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / borbutn / borbtns.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-12-22  |  13.6 KB  |  530 lines

  1. {-------------------------------------------------------------------}
  2. { BORBTNS - BWCC Style CheckBoxes & Radio Buttons for Delphi        }
  3. { v. 1.00 April, 8 1995                                             }
  4. {-------------------------------------------------------------------}
  5. { Copyright Enrico Lodolo                                           }
  6. { via F.Bolognese 27/3 - 440129 Bologna - Italy                     }
  7. { CIS 100275,1255 - Internet ldlc18k1@bo.nettuno.it                 }
  8. {-------------------------------------------------------------------}
  9.  
  10. unit BorBtns;
  11.  
  12. interface
  13.  
  14. uses
  15.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  16.   Forms, Dialogs, StdCtrls, Menus;
  17.  
  18. type
  19.   TBorCheck = class(TCustomControl)
  20.   private
  21.     FDown:Boolean;
  22.     FState:TCheckBoxState;
  23.     FFocused:Boolean;
  24.     FCheckColor:TColor;
  25.   protected
  26.     constructor Create(AOwner: TComponent); override;
  27.     procedure Paint; override;
  28.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  29.       X, Y: Integer); override;
  30.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  31.       X, Y: Integer); override;
  32.     procedure MouseMove(Shift: TShiftState;X, Y: Integer);
  33.       override;
  34.     procedure KeyDown(var Key:Word;Shift:TShiftSTate); override;
  35.     procedure KeyUp(var Key:Word;Shift:TShiftSTate); override;
  36.     procedure SetDown(Value:Boolean);
  37.     procedure SetState(Value:TCheckBoxState);
  38.     procedure SetChecked(Value:Boolean);
  39.     function  GetChecked:Boolean;
  40.     procedure SetCheckColor(Value:TColor);
  41.     procedure DoEnter; override;
  42.     procedure DoExit; override;
  43.   public
  44.   published
  45.     property Caption;
  46.     property CheckColor:TColor read FCheckColor write SetCheckColor
  47.              default clBlack;
  48.     property Checked:Boolean read GetChecked write SetChecked
  49.              default False;
  50.     property Down:Boolean read FDown write SetDown default False;
  51.     property DragCursor;
  52.     property DragMode;
  53.     property Font;
  54.     property ParentFont;
  55.     property PopupMenu;
  56.     property ShowHint;
  57.     property State:TCheckBoxState read FState write SetState
  58.              default cbUnchecked;
  59.     property TabOrder;
  60.     property TabStop;
  61.     property OnClick;
  62.     property OnDragDrop;
  63.     property OnDragOver;
  64.     property OnEndDrag;
  65.     property OnKeyDown;
  66.     property OnKeyPress;
  67.     property OnKeyUp;
  68.     property OnMouseDown;
  69.     property OnMouseMove;
  70.     property OnMouseUp;
  71.   end;
  72.  
  73. type
  74.   TBorRadio = class(TCustomControl)
  75.   private
  76.     FDown:Boolean;
  77.     FChecked:Boolean;
  78.     FFocused:Boolean;
  79.     FCheckColor:TColor;
  80.     FGroupIndex:Byte;
  81.     procedure TurnSiblingsOff;
  82.   protected
  83.     constructor Create(AOwner: TComponent); override;
  84.     procedure Paint; override;
  85.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  86.       X, Y: Integer); override;
  87.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  88.       X, Y: Integer); override;
  89.     procedure MouseMove(Shift: TShiftState;X, Y: Integer);
  90.       override;
  91.     procedure KeyDown(var Key:Word;Shift:TShiftSTate); override;
  92.     procedure KeyUp(var Key:Word;Shift:TShiftSTate); override;
  93.     procedure SetDown(Value:Boolean);
  94.     procedure SetChecked(Value:Boolean);
  95.     procedure SetCheckColor(Value:TColor);
  96.     procedure DoEnter; override;
  97.     procedure DoExit; override;
  98.   public
  99.   published
  100.     property Caption;
  101.     property CheckColor:TColor read FCheckColor write SetCheckColor
  102.              default clBlack;
  103.     property Checked:Boolean read FChecked write SetChecked
  104.              default False;
  105.     property Down:Boolean read FDown write SetDown default False;
  106.     property DragCursor;
  107.     property DragMode;
  108.     property Font;
  109.     property GroupIndex:Byte read FGroupIndex write FGroupIndex
  110.       default 0;
  111.     property ParentFont;
  112.     property PopupMenu;
  113.     property ShowHint;
  114.     property TabOrder;
  115.     property TabStop;
  116.     property OnClick;
  117.     property OnDragDrop;
  118.     property OnDragOver;
  119.     property OnEndDrag;
  120.     property OnKeyDown;
  121.     property OnKeyPress;
  122.     property OnKeyUp;
  123.     property OnMouseDown;
  124.     property OnMouseMove;
  125.     property OnMouseUp;
  126.   end;
  127.  
  128. procedure Register;
  129.  
  130. implementation
  131.  
  132. {-------------------------------------------------------------------}
  133. {                          Borland Style CheckBox                   }
  134. {-------------------------------------------------------------------}
  135.  
  136. constructor TBorCheck.Create(AOwner: TComponent);
  137.  
  138. begin
  139.   inherited Create(AOwner);
  140.   Width := 98;
  141.   Height := 20;
  142.   ParentColor:=False;
  143.   Color:=clBtnFace;
  144. end;
  145.  
  146. const BW=12;
  147.  
  148. procedure TBorCheck.Paint;
  149.  
  150. var BL,BT,BR,BB:Integer;
  151.     TX,TY,TW,TH:Integer;
  152.     Rect:TRect;
  153.  
  154. begin
  155.      Canvas.Font:=Font;
  156.      with Canvas do
  157.        begin
  158.          BT:=(Height div 2)-(BW div 2);
  159.          BB:=BT+BW;
  160.          BL:=1;
  161.          BR:=BW+1;
  162.          Brush.Color:=clBtnFace;
  163.          if not FDown then
  164.            begin
  165.              Pen.Color:=clBtnFace;
  166.              Rectangle(BL,BT,BR,BB);
  167.              Pen.Color:=clBtnHighLight;
  168.              MoveTo(BL,BB);
  169.              LineTo(BL,BT);
  170.              LineTo(BR,BT);
  171.              Pen.Color:=clBtnShadow;
  172.              LineTo(BR,BB);
  173.              LineTo(BL,BB);
  174.            end
  175.          else
  176.            begin
  177.              Pen.Color:=clBlack;
  178.              Pen.Width:=2;
  179.              Rectangle(BL+1,BT+1,BR+1,BB+1);
  180.              Pen.Width:=1;
  181.            end;
  182.          TX:=BR+5;
  183.          TY:=(Height div 2)+(Font.Height div 2)-1;
  184.          TW:=TextWidth(Caption);
  185.          TH:=TextHeight(Caption);
  186.          TextOut(TX,TY,Caption);
  187.          case State of
  188.            cbChecked:begin
  189.                        Pen.Color:=FCheckColor;
  190.                        Pen.Width:=1;
  191.                        Dec(BT);Dec(BB);
  192.                        MoveTo(BL+2,BT+BW div 2+1);
  193.                        LineTo(BL+2,BB-1);
  194.                        MoveTo(BL+3,BT+BW div 2);
  195.                        LineTo(BL+3,BB-2);
  196.                        MoveTo(BL+2,BB-1);
  197.                        LineTo(BR-2,BT+3);
  198.                        MoveTo(BL+3,BB-1);
  199.                        LineTo(BR-1,BT+3);
  200.                      end;
  201.             cbGrayed:begin
  202.                        if Down then
  203.                          begin
  204.                            Pen.Color:=clBtnFace;
  205.                            Brush.Color:=clBtnFace;
  206.                            Rectangle(BL+2,BT+2,BR-1,BB-1);
  207.                          end;
  208.                        Brush.Color:=clBtnShadow;
  209.                        Rectangle(BL+2,BT+2,BR-1,BB-1);
  210.                      end;
  211.          end;
  212.          Brush.Color:=clBtnFace;
  213.          Rect:=Bounds(TX-1,TY,TW+3,TH+1);
  214.          FrameRect(Rect);
  215.          if FFocused then
  216.            DrawFocusRect(Rect);
  217.        end;
  218. end;
  219.  
  220. procedure TBorCheck.SetDown(Value:Boolean);
  221.  
  222. begin
  223.      if FDown<>Value then
  224.        begin
  225.          FDown:=Value;
  226.          Paint;
  227.        end;
  228. end;
  229.  
  230. procedure TBorCheck.SetState(Value:TCheckBoxState);
  231.  
  232. begin
  233.      if FState<>Value then
  234.        begin
  235.          FState:=Value;
  236.          Paint;
  237.          Click;
  238.        end;
  239. end;
  240.  
  241. function TBorCheck.GetChecked: Boolean;
  242.  
  243. begin
  244.      Result:=State=cbChecked;
  245. end;
  246.  
  247. procedure TBorCheck.SetChecked(Value:Boolean);
  248.  
  249. begin
  250.      if Value then State := cbChecked
  251.               else State := cbUnchecked;
  252. end;
  253.  
  254. procedure TBorCheck.SetCheckColor(Value:TColor);
  255.  
  256. begin
  257.      FCheckColor:=Value;
  258.      Paint;
  259. end;
  260.  
  261. procedure TBorCheck.DoEnter;
  262.  
  263. begin
  264.      inherited DoEnter;
  265.      FFocused:=True;
  266.      Paint;
  267. end;
  268.  
  269. procedure TBorCheck.DoExit;
  270.  
  271. begin
  272.      inherited DoExit;
  273.      FFocused:=False;
  274.      Paint;
  275. end;
  276.  
  277. procedure TBorCheck.MouseDown(Button: TMouseButton; Shift: TShiftState;
  278.                                   X, Y: Integer);
  279.  
  280. begin
  281.      SetFocus;
  282.      FFocused:=True;
  283.      inherited MouseDown(Button, Shift, X, Y);
  284.      MouseCapture:=True;
  285.      Down:=True;
  286. end;
  287.  
  288. procedure TBorCheck.MouseUp(Button: TMouseButton; Shift: TShiftState;
  289.                                   X, Y: Integer);
  290.  
  291. begin
  292.      MouseCapture:=False;
  293.      Down:=False;
  294.      if (X>=0) and (X<=Width) and (Y>=0) and (Y<=Height) then
  295.        Checked:=not Checked;
  296.      inherited MouseUp(Button, Shift, X, Y);
  297. end;
  298.  
  299. procedure TBorCheck.MouseMove(Shift: TShiftState;X, Y: Integer);
  300.  
  301. begin
  302.      if MouseCapture then
  303.        Down:=(X>=0) and (X<=Width) and (Y>=0) and (Y<=Height);
  304.      inherited MouseMove(Shift,X,Y);
  305. end;
  306.  
  307. procedure TBorCheck.KeyDown(var Key:Word;Shift:TShiftSTate);
  308.  
  309. begin
  310.      if Key=vk_Space then Down:=True;
  311.      inherited KeyDown(Key,Shift);
  312. end;
  313.  
  314. procedure TBorCheck.KeyUp(var Key:Word;Shift:TShiftSTate);
  315.  
  316. begin
  317.      if Key=vk_Space then
  318.        begin
  319.          Down:=False;
  320.          Checked:=not Checked;
  321.        end;
  322. end;
  323.  
  324. {-------------------------------------------------------------------}
  325. {                           Borland Radio Button                    }
  326. {-------------------------------------------------------------------}
  327.  
  328. constructor TBorRadio.Create(AOwner: TComponent);
  329.  
  330. begin
  331.   inherited Create(AOwner);
  332.   Width := 98;
  333.   Height := 20;
  334.   ParentColor:=False;
  335.   Color:=clBtnFace;
  336. end;
  337.  
  338. procedure TBorRadio.Paint;
  339.  
  340. var BL,BT,BR,BB,BM:Integer;
  341.     TX,TY,TW,TH:Integer;
  342.     CX,CY:Integer;
  343.     Rect:TRect;
  344.  
  345. begin
  346.      Canvas.Font:=Font;
  347.      with Canvas do
  348.        begin
  349.          BM:=BW div 2;
  350.          BT:=(Height div 2)-BM;
  351.          BB:=BT+BW;
  352.          BL:=1;
  353.          BR:=BW+1;
  354.          Brush.Color:=clBtnFace;
  355.          if Down then
  356.            begin
  357.              Pen.Color:=clBlack;
  358.              MoveTo(BL+BM,BT);
  359.              LineTo(BL,BT+BM);
  360.              LineTo(BL+BM,BB);
  361.              LineTo(BR,BT+BM);
  362.              LineTo(BL+BM,BT);
  363.              MoveTo(BL+BM,BT+1);
  364.              LineTo(BL+1,BT+BM);
  365.              LineTo(BL+BM,BB-1);
  366.              LineTo(BR-1,BT+BM);
  367.              LineTo(BL+BM,BT+1);
  368.            end
  369.          else
  370.            begin
  371.              Pen.Color:=clBtnFace;
  372.              Rectangle(BL,BT,BR,BB);
  373.              if Checked then Pen.Color:=clBtnShadow
  374.                         else Pen.Color:=clBtnHighLight;
  375.              MoveTo(BL+BM,BT);
  376.              LineTo(BL,BT+BM);
  377.              LineTo(BL+BM,BB);
  378.              if Checked then Pen.Color:=clBtnHighLight
  379.                         else Pen.Color:=clBtnShadow;
  380.              LineTo(BR,BT+BM);
  381.              LineTo(BL+BM,BT);
  382.            end;
  383.          if Checked then
  384.             begin
  385.               Pen.Color:=CheckColor;
  386.               CX:=BL+BM;CY:=BT+BM;
  387.               MoveTo(CX-1,CY-1);
  388.               LineTo(CX+2,CY-1);
  389.               MoveTo(CX-2,CY);
  390.               LineTo(CX+3,CY);
  391.               MoveTo(CX-1,CY+1);
  392.               LineTo(CX+2,CY+1);
  393.               MoveTo(CX,CY-2);
  394.               LineTo(CX,CY+3);
  395.             end;
  396.          TX:=BR+5;
  397.          TY:=(Height div 2)+(Font.Height div 2)-1;
  398.          TW:=TextWidth(Caption);
  399.          TH:=TextHeight(Caption);
  400.          TextOut(TX,TY,Caption);
  401.          Brush.Color:=clBtnFace;
  402.          Rect:=Bounds(TX-1,TY,TW+3,TH+1);
  403.          FrameRect(Rect);
  404.          if FFocused then
  405.            DrawFocusRect(Rect);
  406.        end;
  407. end;
  408.  
  409. procedure TBorRadio.SetDown(Value:Boolean);
  410.  
  411. begin
  412.      if FDown<>Value then
  413.        begin
  414.          FDown:=Value;
  415.          Paint;
  416.        end;
  417. end;
  418.  
  419. procedure TBorRadio.TurnSiblingsOff;
  420.  
  421. var i:Integer;
  422.     Sibling: TBorRadio;
  423.  
  424. begin
  425.      if Parent <> nil then
  426.        for i:=0 to Parent.ControlCount-1 do
  427.          if Parent.Controls[i] is TBorRadio then
  428.            begin
  429.              Sibling:=TBorRadio(Parent.Controls[i]);
  430.              if (Sibling<>Self) and
  431.                 (Sibling.GroupIndex=GroupIndex) then
  432.                   Sibling.SetChecked(False);
  433.            end;
  434. end;
  435.  
  436. procedure TBorRadio.SetChecked(Value: Boolean);
  437.  
  438. begin
  439.      if FChecked <> Value then
  440.        begin
  441.          TabStop:=Value;
  442.          FChecked:=Value;
  443.          if Value then
  444.            begin
  445.              TurnSiblingsOff;
  446.              Click;
  447.            end;
  448.          Paint;
  449.        end;
  450. end;
  451.  
  452. procedure TBorRadio.SetCheckColor(Value:TColor);
  453.  
  454. begin
  455.      FCheckColor:=Value;
  456.      Paint;
  457. end;
  458.  
  459. procedure TBorRadio.DoEnter;
  460.  
  461. begin
  462.      inherited DoEnter;
  463.      FFocused:=True;
  464.      Checked:=True;
  465.      Paint;
  466. end;
  467.  
  468. procedure TBorRadio.DoExit;
  469.  
  470. begin
  471.      inherited DoExit;
  472.      FFocused:=False;
  473.      Paint;
  474. end;
  475.  
  476. procedure TBorRadio.MouseDown(Button: TMouseButton; Shift: TShiftState;
  477.                                   X, Y: Integer);
  478.  
  479. begin
  480.      SetFocus;
  481.      FFocused:=True;
  482.      inherited MouseDown(Button, Shift, X, Y);
  483.      MouseCapture:=True;
  484.      Down:=True;
  485. end;
  486.  
  487. procedure TBorRadio.MouseUp(Button: TMouseButton; Shift: TShiftState;
  488.                                   X, Y: Integer);
  489.  
  490. begin
  491.      MouseCapture:=False;
  492.      Down:=False;
  493.      if (X>=0) and (X<=Width) and (Y>=0) and (Y<=Height)
  494.        and not Checked then Checked:=True;
  495.      inherited MouseUp(Button, Shift, X, Y);
  496. end;
  497.  
  498. procedure TBorRadio.MouseMove(Shift: TShiftState;X, Y: Integer);
  499.  
  500. begin
  501.      if MouseCapture then
  502.        Down:=(X>=0) and (X<=Width) and (Y>=0) and (Y<=Height);
  503.      inherited MouseMove(Shift,X,Y);
  504. end;
  505.  
  506. procedure TBorRadio.KeyDown(var Key:Word;Shift:TShiftSTate);
  507.  
  508. begin
  509.      if Key=vk_Space then Down:=True;
  510.      inherited KeyDown(Key,Shift);
  511. end;
  512.  
  513. procedure TBorRadio.KeyUp(var Key:Word;Shift:TShiftSTate);
  514.  
  515. begin
  516.      if Key=vk_Space then
  517.        begin
  518.          Down:=False;
  519.          if not Checked then Checked:=True;
  520.        end;
  521. end;
  522.  
  523. procedure Register;
  524.  
  525. begin
  526.      RegisterComponents('Samples',[TBorCheck,TBorRadio]);
  527. end;
  528.  
  529. end.
  530.