VrNavigator.pas
上传用户:hbszzs
上传日期:2008-08-20
资源大小:628k
文件大小:24k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrNavigator;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   VrTypes, VrClasses, VrControls, VrSysUtils;
  15. type
  16.   TVrButtonType = (btPower, btPlay, btPause, btStop, btPrev, btBack, btStep,
  17.     btNext, btRecord, btEject);
  18.   TVrButtonSet = set of TVrButtonType;
  19.   TVrNavButton = record
  20.     Visible: Boolean;
  21.     Enabled: Boolean;
  22.   end;
  23.   TVrMediaButton = class(TVrCustomImageControl)
  24.   private
  25.     FButtonType: TVrButtonType;
  26.     FFocusColor: TColor;
  27.     FBorderColor: TColor;
  28.     FEnGlyphs: TBitmap;
  29.     FDiGlyphs: TBitmap;
  30.     Down: Boolean;
  31.     Pressed: Boolean;
  32.     MaskColor: TColor;
  33.     HasFocus: Boolean;
  34.     procedure SetButtonType(Value: TVrButtonType);
  35.     procedure SetFocusColor(Value: TColor);
  36.     procedure SetBorderColor(Value: TColor);
  37.     procedure DoMouseDown(XPos, YPos: Integer);
  38.     procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
  39.     procedure WMMouseMove(var Message: TWMMouseMove); message WM_MOUSEMOVE;
  40.     procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
  41.     procedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LBUTTONDBLCLK;
  42.     procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
  43.     procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
  44.     procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
  45.   protected
  46.     procedure LoadBitmaps;
  47.     procedure Paint; override;
  48.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  49.     procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  50.   public
  51.     constructor Create(AOwner: TComponent); override;
  52.     destructor Destroy; override;
  53.   published
  54.     property ButtonType: TVrButtonType read FButtonType write SetButtonType default btPause;
  55.     property FocusColor: TColor read FFocusColor write SetFocusColor default clBlue;
  56.     property BorderColor: TColor read FBorderColor write SetBorderColor default clBlack;
  57. {$IFDEF VER110}
  58.     property Anchors;
  59.     property Constraints;
  60. {$ENDIF}
  61.     property DragCursor;
  62. {$IFDEF VER110}
  63.     property DragKind;
  64. {$ENDIF}
  65.     property DragMode;
  66.     property Enabled;
  67.     property ParentShowHint;
  68.     property PopupMenu;
  69.     property ShowHint;
  70.     property TabOrder;
  71.     property TabStop default false;
  72.     property Visible;
  73.     property OnClick;
  74. {$IFDEF VER130}
  75.     property OnContextPopup;
  76. {$ENDIF}
  77.     property OnKeyDown;
  78.     property OnKeyPress;
  79.     property OnKeyUp;
  80.     property OnDragDrop;
  81.     property OnDragOver;
  82. {$IFDEF VER110}
  83.     property OnEndDock;
  84. {$ENDIF}
  85.     property OnEndDrag;
  86.     property OnEnter;
  87.     property OnExit;
  88.     property OnMouseDown;
  89.     property OnMouseMove;
  90.     property OnMouseUp;
  91. {$IFDEF VER110}
  92.     property OnStartDock;
  93. {$ENDIF}
  94.     property OnStartDrag;
  95.   end;
  96. const
  97.   DefEnabledButtons = [btPower, btPlay, btPause, btStop, btPrev, btBack, btStep,
  98.     btNext, btRecord, btEject];
  99.   DefVisibleButtons = [btPower, btPlay, btPause, btStop, btPrev, btBack, btStep,
  100.     btNext, btRecord, btEject];
  101. type
  102.   TVrClickEvent = procedure (Sender: TObject; Button: TVrButtonType) of object;
  103.   TVrNavigator = class(TVrCustomImageControl)
  104.   private
  105.     FVisibleButtons: TVrButtonSet;
  106.     FEnabledButtons: TVrButtonSet;
  107.     FBevel: TVrBevel;
  108.     FFocusColor: TColor;
  109.     FBorderColor: TColor;
  110.     FSpacing: Integer;
  111.     FEnGlyphs: TBitmap;
  112.     FDiGlyphs: TBitmap;
  113.     FNumeric: Boolean;
  114.     FOrientation: TVrOrientation;
  115.     FOnButtonClick: TVrClickEvent;
  116.     Bitmap: TBitmap;
  117.     Pressed: Boolean;
  118.     Down: Boolean;
  119.     CurrentButton: TVrButtonType;
  120.     ViewPort: TRect;
  121.     ButtonWidth: Integer;
  122.     ButtonHeight: Integer;
  123.     FocusedButton: TVrButtonType;
  124.     Buttons: array[TVrButtonType] of TVrNavButton;
  125.     function VisibleButtonCount: Integer;
  126.     procedure SetVisibleButtons(Value: TVrButtonSet);
  127.     procedure SetEnabledButtons(Value: TVrButtonSet);
  128.     procedure SetSpacing(Value: Integer);
  129.     procedure SetFocusColor(Value: TColor);
  130.     procedure SetBorderColor(Value: TColor);
  131.     procedure SetOrientation(Value: TVrOrientation);
  132.     procedure SetNumeric(Value: Boolean);
  133.     procedure SetBevel(Value: TVrBevel);
  134.     procedure DoMouseDown(XPos, YPos: Integer);
  135.     procedure BevelChanged(Sender: TObject);
  136.     procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LButtonDown;
  137.     procedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LButtonDblClk;
  138.     procedure WMMouseMove(var Message: TWMMouseMove); message WM_MouseMove;
  139.     procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LButtonUp;
  140.     procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
  141.     procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
  142.     procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
  143.   protected
  144.     procedure DoClick(Button: TVrButtonType);
  145.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  146.     procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  147.     procedure LoadBitmaps;
  148.     procedure CalcPaintParams;
  149.     procedure GetButtonRect(Btn: TVrButtonType; var R: TRect);
  150.     procedure DrawButton(Btn: TVrButtonType);
  151.     procedure SetFocusedButton(Btn: TVrButtonType);
  152.     procedure Paint; override;
  153.     procedure Loaded; override;
  154.   public
  155.     constructor Create(AOwner: TComponent); override;
  156.     destructor Destroy; override;
  157.     procedure ButtonClick(Button: TVrButtonType); dynamic;
  158.     function ButtonIndex(Button: TVrButtonType): Integer;
  159.   published
  160.     property Bevel: TVrBevel read FBevel write SetBevel;
  161.     property Spacing: Integer read FSpacing write SetSpacing default 1;
  162.     property FocusColor: TColor read FFocusColor write SetFocusColor default clBlue;
  163.     property BorderColor: TColor read FBorderColor write SetBorderColor default clBlack;
  164.     property VisibleButtons: TVrButtonSet read FVisibleButtons write SetVisibleButtons default DefVisibleButtons;
  165.     property EnabledButtons: TVrButtonSet read FEnabledButtons write SetEnabledButtons default DefEnabledButtons;
  166.     property Orientation: TVrOrientation read FOrientation write SetOrientation default voHorizontal;
  167.     property Numeric: Boolean read FNumeric write SetNumeric default false;
  168.     property OnButtonClick: TVrClickEvent read FOnButtonClick write FOnButtonClick;
  169.     property Color;
  170. {$IFDEF VER110}
  171.     property Anchors;
  172.     property Constraints;
  173. {$ENDIF}
  174.     property DragCursor;
  175. {$IFDEF VER110}
  176.     property DragKind;
  177. {$ENDIF}
  178.     property DragMode;
  179.     property ParentShowHint;
  180.     property PopupMenu;
  181.     property ShowHint;
  182.     property TabOrder;
  183.     property TabStop default True;
  184.     property Visible;
  185. {$IFDEF VER130}
  186.     property OnContextPopup;
  187. {$ENDIF}
  188.     property OnKeyDown;
  189.     property OnKeyPress;
  190.     property OnKeyUp;
  191.     property OnDragDrop;
  192.     property OnDragOver;
  193. {$IFDEF VER110}
  194.     property OnEndDock;
  195. {$ENDIF}
  196.     property OnEndDrag;
  197.     property OnEnter;
  198.     property OnExit;
  199.     property OnMouseDown;
  200.     property OnMouseMove;
  201.     property OnMouseUp;
  202. {$IFDEF VER110}
  203.     property OnStartDock;
  204. {$ENDIF}
  205.     property OnStartDrag;
  206.   end;
  207. implementation
  208. {$R VRNAVIGATOR.D32}
  209. { TVrNavigator }
  210. constructor TVrNavigator.Create(AOwner: TComponent);
  211. var
  212.   I: TVrButtonType;
  213. begin
  214.   inherited Create(AOwner);
  215.   ControlStyle := ControlStyle + [csOpaque] - [csSetCaption];
  216.   Width := 340;
  217.   Height := 25;
  218.   TabStop := True;
  219.   for I := Low(Buttons) to High(Buttons) do
  220.   begin
  221.     Buttons[I].Visible := True;
  222.     Buttons[I].Enabled := True;
  223.   end;
  224.   FBevel := TVrBevel.Create;
  225.   with FBevel do
  226.   begin
  227.     InnerSpace := 0;
  228.     OnChange := BevelChanged;
  229.   end;
  230.   FFocusColor := clBlue;
  231.   FBorderColor := clBlack;
  232.   FSpacing := 1;
  233.   FVisibleButtons := DefVisibleButtons;
  234.   FEnabledButtons := DefEnabledButtons;
  235.   FOrientation := voHorizontal;
  236.   FNumeric := false;
  237.   Bitmap := TBitmap.Create;
  238.   FEnGlyphs := TBitmap.Create;
  239.   FDiGlyphs := TBitmap.Create;
  240.   LoadBitmaps;
  241. end;
  242. destructor TVrNavigator.Destroy;
  243. begin
  244.   Bitmap.Free;
  245.   FEnGlyphs.Free;
  246.   FDiGlyphs.Free;
  247.   inherited Destroy;
  248. end;
  249. procedure TVrNavigator.Loaded;
  250. var
  251.   I: TVrButtonType;
  252. begin
  253.   inherited Loaded;
  254.   for I := Low(Buttons) to High(Buttons) do
  255.     if Buttons[I].Visible then
  256.     begin
  257.       FocusedButton := I;
  258.       CurrentButton := I;
  259.       Exit;
  260.     end;
  261. end;
  262. procedure TVrNavigator.BevelChanged(Sender: TObject);
  263. var
  264.   R: TRect;
  265. begin
  266.   if not Loading then
  267.   begin
  268.     R := ClientRect;
  269.     FBevel.GetVisibleArea(R);
  270.     InflateRect(ViewPort, R.Left, R.Top);
  271.     BoundsRect := Bounds(Left, Top, WidthOf(ViewPort),
  272.       HeightOf(ViewPort));
  273.   end;
  274.   UpdateControlCanvas;
  275. end;
  276. procedure TVrNavigator.LoadBitmaps;
  277. begin
  278.   if FNumeric then
  279.   begin
  280.     FEnGlyphs.Handle := LoadBitmap(hInstance, 'EN_NUMERIC');
  281.     FDiGlyphs.Handle := LoadBitmap(hInstance, 'DI_NUMERIC');
  282.   end else
  283.   begin
  284.     FEnGlyphs.Handle := LoadBitmap(hInstance, 'EN_IMAGES');
  285.     FDiGlyphs.Handle := LoadBitmap(hInstance, 'DI_IMAGES');
  286.   end;
  287. end;
  288. procedure TVrNavigator.SetEnabledButtons(Value: TVrButtonSet);
  289. var
  290.   I: TVrButtonType;
  291. begin
  292.   FEnabledButtons := Value;
  293.   for I := Low(Buttons) to High(Buttons) do
  294.     Buttons[I].Enabled := I in FEnabledButtons;
  295.   UpdateControlCanvas;
  296. end;
  297. procedure TVrNavigator.SetVisibleButtons(Value: TVrButtonSet);
  298. var
  299.   I: TVrButtonType;
  300. begin
  301.   FVisibleButtons := Value;
  302.   for I := Low(Buttons) to High(Buttons) do
  303.     Buttons[I].Visible := I in FVisibleButtons;
  304.   UpdateControlCanvas;
  305. end;
  306. function TVrNavigator.VisibleButtonCount: Integer;
  307. var
  308.   I: TVrButtonType;
  309. begin
  310.   Result := 0;
  311.   for I := Low(Buttons) to High(Buttons) do
  312.     if Buttons[I].Visible then Inc(Result);
  313.   if Result = 0 then Inc(Result);
  314. end;
  315. procedure TVrNavigator.SetSpacing(Value: Integer);
  316. begin
  317.   if FSpacing <> Value then
  318.   begin
  319.     FSpacing := Value;
  320.     UpdateControlCanvas;
  321.   end;
  322. end;
  323. procedure TVrNavigator.SetFocusColor(Value: TColor);
  324. begin
  325.   if FFocusColor <> Value then
  326.   begin
  327.     FFocusColor := Value;
  328.     UpdateControlCanvas;
  329.   end;
  330. end;
  331. procedure TVrNavigator.SetBorderColor(Value: TColor);
  332. begin
  333.   if FBorderColor <> Value then
  334.   begin
  335.     FBorderColor := Value;
  336.     UpdateControlCanvas;
  337.   end;
  338. end;
  339. procedure TVrNavigator.SetOrientation(Value: TVrOrientation);
  340. var
  341.   R: TRect;
  342.   NewWidth: Integer;
  343.   NewHeight: Integer;
  344. begin
  345.   if FOrientation <> Value then
  346.   begin
  347.     FOrientation := Value;
  348.     if not Loading then
  349.     begin
  350.       NewWidth := 0;
  351.       NewHeight := 0;
  352.       case Value of
  353.         voHorizontal:
  354.           begin
  355.             NewWidth := (VisibleButtonCount * (ButtonWidth + FSpacing));
  356.             NewHeight := ButtonHeight;
  357.           end;
  358.         voVertical:
  359.           begin
  360.             NewWidth := ButtonWidth;
  361.             NewHeight := (VisibleButtonCount * (ButtonHeight + FSpacing));
  362.           end;
  363.       end;
  364.       R := Bounds(0, 0, NewWidth, NewHeight);
  365.       InflateRect(R, ViewPort.Left, ViewPort.Top);
  366.       BoundsRect := Bounds(Left, Top, WidthOf(R), HeightOf(R));
  367.     end;
  368.     UpdateControlCanvas;
  369.   end;
  370. end;
  371. procedure TVrNavigator.SetBevel(Value: TVrBevel);
  372. begin
  373.   FBevel.Assign(Value);
  374. end;
  375. procedure TVrNavigator.SetNumeric(Value: Boolean);
  376. begin
  377.   if FNumeric <> Value then
  378.   begin
  379.     FNumeric := Value;
  380.     LoadBitmaps;
  381.     UpdateControlCanvas;
  382.   end;
  383. end;
  384. function TVrNavigator.ButtonIndex(Button: TVrButtonType): Integer;
  385. begin
  386.   Result := Ord(Button) + 1;
  387. end;
  388. procedure TVrNavigator.DrawButton(Btn: TVrButtonType);
  389. var
  390.   IsDown: Boolean;
  391.   GW, GH, BX, BY, hRes: Integer;
  392.   Glyph: TBitmap;
  393.   R: TRect;
  394.   BtnRect: TRect;
  395.   Colors: array[0..1] of TColor;
  396. begin
  397.   GetButtonRect(Btn, BtnRect);
  398.   IsDown := Down and (Btn = CurrentButton);
  399.   Bitmap.Width := ButtonWidth;
  400.   Bitmap.Height := ButtonHeight;
  401.   with Bitmap.Canvas do
  402.   begin
  403.     if Down then
  404.     begin
  405.       Colors[0] := clBtnFace;
  406.       Colors[1] := clBtnHighlight;
  407.     end
  408.     else
  409.     begin
  410.       Colors[0] := clBtnHighlight;
  411.       Colors[1] := clBtnShadow;
  412.     end;
  413.     hRes := ButtonHeight div 10;
  414.     if hRes < 2 then HRes := 2;
  415.     R := Bounds(0, 0, ButtonWidth, ButtonHeight);
  416.     InflateRect(R, -2, -2);
  417.     DrawGradient(Bitmap.Canvas, R, Colors[0], Colors[1], voVertical, hRes);
  418.     if Buttons[Btn].Enabled then
  419.       Glyph := FEnGlyphs else Glyph := FDiGlyphs;
  420.     GH := Glyph.Height;
  421.     GW := Glyph.Width div 10;
  422.     BX := (ButtonWidth div 2) - (GW div 2);
  423.     BY := (ButtonHeight div 2) - (GH div 2);
  424.     if IsDown then
  425.     begin
  426.       Inc(BX);
  427.       Inc(BY);
  428.     end;
  429.     Brush.Style := bsClear;
  430.     BrushCopy(Bounds(BX, BY, GW, GH),
  431.       Glyph, Bounds(ord(Btn) * GW, 0, GW, GH), clOlive);
  432.     R := Bounds(0, 0, ButtonWidth, ButtonHeight);
  433.     if (Focused) and (Btn = FocusedButton) then
  434.       DrawFrame3D(Bitmap.Canvas, R, FFocusColor, FFocusColor, 1)
  435.     else DrawFrame3D(Bitmap.Canvas, R, FBorderColor, FBorderColor, 1);
  436.     if Down then
  437.       DrawFrame3D(Bitmap.Canvas, R, clBtnShadow, clBtnFace, 1)
  438.     else DrawOutline3D(Bitmap.Canvas, R, Colors[0], Colors[1], 1);
  439.   end;
  440.   DestCanvas.Draw(BtnRect.Left, BtnRect.Top, Bitmap);
  441. end;
  442. procedure TVrNavigator.Paint;
  443. var
  444.   I: TVrButtonType;
  445.   R: TRect;
  446. begin
  447.   CalcPaintParams;
  448.   ClearBitmapCanvas;
  449.   DestCanvas := BitmapCanvas;
  450.   try
  451.     with DestCanvas do
  452.     begin
  453.       R := ClientRect;
  454.       FBevel.Paint(DestCanvas, R);
  455.       for I := Low(Buttons) to High(Buttons) do
  456.         if Buttons[I].Visible then DrawButton(I);
  457.     end;
  458.   finally
  459.     DestCanvas := Self.Canvas;
  460.   end;
  461.   inherited Paint;
  462. end;
  463. procedure TVrNavigator.CalcPaintParams;
  464. var
  465.   Gap: Integer;
  466.   Count: Integer;
  467. begin
  468.   ViewPort := ClientRect;
  469.   FBevel.GetVisibleArea(ViewPort);
  470.   Count := VisibleButtonCount;
  471.   if Count > 0 then
  472.   begin
  473.     Gap := (Count - 1) * FSpacing;
  474.     case FOrientation of
  475.       voHorizontal:
  476.         begin
  477.           ButtonWidth := (WidthOf(ViewPort) - Gap) div Count;
  478.           ButtonHeight := HeightOf(ViewPort);
  479.           if Count > 1 then
  480.             Width := (ViewPort.Left * 2) + (Count * ButtonWidth) + Gap;
  481.         end;
  482.       voVertical:
  483.         begin
  484.           ButtonWidth := WidthOf(ViewPort);
  485.           ButtonHeight := (HeightOf(ViewPort) - Gap) div Count;
  486.           if Count > 1 then
  487.             Height := (ViewPort.Top * 2) + (Count * ButtonHeight) + Gap;
  488.         end;
  489.     end;
  490.   end;
  491. end;
  492. procedure TVrNavigator.GetButtonRect(Btn: TVrButtonType; var R: TRect);
  493. var
  494.   X, Y: Integer;
  495.   I: TVrButtonType;
  496. begin
  497.   X := ViewPort.Left;
  498.   Y := ViewPort.Top;
  499.   for I := Low(Buttons) to High(Buttons) do
  500.   begin
  501.     if Buttons[I].Visible then
  502.     begin
  503.       if I = Btn then Break;
  504.       case FOrientation of
  505.         voHorizontal: Inc(X, ButtonWidth + FSpacing);
  506.         voVertical: Inc(Y, ButtonHeight + FSpacing);
  507.       end;
  508.     end;
  509.   end;
  510.   R := Bounds(X, Y, ButtonWidth, ButtonHeight);
  511. end;
  512. procedure TVrNavigator.SetFocusedButton(Btn: TVrButtonType);
  513. var
  514.   OrgBtn: TVrButtonType;
  515. begin
  516.   if FocusedButton <> Btn then
  517.   begin
  518.     OrgBtn := FocusedButton;
  519.     FocusedButton := Btn;
  520.     DrawButton(OrgBtn);
  521.     DrawButton(FocusedButton);
  522.   end;
  523. end;
  524. procedure TVrNavigator.DoMouseDown(XPos, YPos: Integer);
  525. var
  526.   I: TVrButtonType;
  527.   BtnRect: TRect;
  528.   Clicked: Boolean;
  529. begin
  530.   Clicked := false;
  531.   for I := Low(Buttons) to High(Buttons) do
  532.     if Buttons[I].Visible then
  533.     begin
  534.       GetButtonRect(I, BtnRect);
  535.       if PtInRect(BtnRect, Point(XPos, YPos)) then
  536.       begin
  537.         if Buttons[I].Enabled then
  538.         begin
  539.           Clicked := True;
  540.           Break;
  541.         end else Exit;
  542.       end;
  543.     end;
  544.   if not Clicked then Exit;
  545.   CurrentButton := I;
  546.   if TabStop then SetFocus;
  547.   if CurrentButton <> FocusedButton then
  548.     SetFocusedButton(CurrentButton);
  549.   Pressed := True;
  550.   Down := True;
  551.   DrawButton(I);
  552.   MouseCapture := True;
  553. end;
  554. procedure TVrNavigator.WMLButtonDown(var Message: TWMLButtonDown);
  555. begin
  556.   DoMouseDown(Message.XPos, Message.YPos);
  557. end;
  558. procedure TVrNavigator.WMLButtonDblClk(var Message: TWMLButtonDblClk);
  559. begin
  560.   DoMouseDown(Message.XPos, Message.YPos);
  561. end;
  562. procedure TVrNavigator.WMMouseMove(var Message: TWMMouseMove);
  563. var
  564.   P: TPoint;
  565.   R: TRect;
  566. begin
  567.   if Pressed then
  568.   begin
  569.     P := Point(Message.XPos, Message.YPos);
  570.     GetButtonRect(CurrentButton, R);
  571.     if PtInRect(R, P) <> Down then
  572.     begin
  573.       Down := not Down;
  574.       DrawButton(CurrentButton);
  575.     end;
  576.   end;
  577. end;
  578. procedure TVrNavigator.DoClick(Button: TVrButtonType);
  579. begin
  580.   ButtonClick(CurrentButton);
  581. end;
  582. procedure TVrNavigator.WMLButtonUp(var Message: TWMLButtonUp);
  583. begin
  584.   MouseCapture := False;
  585.   if Pressed then
  586.   begin
  587.     Pressed := False;
  588.     if Down then
  589.     begin
  590.       Down := False;
  591.       DrawButton(CurrentButton);  {raise button before calling code}
  592.       DoClick(CurrentButton);
  593.     end;
  594.   end;
  595. end;
  596. procedure TVrNavigator.WMSetFocus(var Message: TWMSetFocus);
  597. begin
  598.   inherited;
  599.   DrawButton(FocusedButton);
  600. end;
  601. procedure TVrNavigator.WMKillFocus(var Message: TWMKillFocus);
  602. begin
  603.   DrawButton(FocusedButton);
  604.   inherited;
  605. end;
  606. procedure TVrNavigator.WMGetDlgCode(var Message: TWMGetDlgCode);
  607. begin
  608.   Message.Result := DLGC_WANTARROWS;
  609. end;
  610. procedure TVrNavigator.KeyUp(var Key: Word; Shift: TShiftState);
  611. begin
  612.   case Key of
  613.     VK_SPACE:
  614.       if Down then
  615.         if Buttons[FocusedButton].Enabled then
  616.         begin
  617.           Down := false;
  618.           DrawButton(CurrentButton);
  619.           DoClick(CurrentButton);
  620.         end;
  621.   end;
  622. end;
  623. procedure TVrNavigator.KeyDown(var Key: Word; Shift: TShiftState);
  624. var
  625.   NewFocus: TVrButtonType;
  626. begin
  627.   case Key of
  628.     VK_RIGHT:
  629.       if not Down then
  630.       begin
  631.         NewFocus := FocusedButton;
  632.         repeat
  633.           if NewFocus < High(Buttons) then
  634.             NewFocus := Succ(NewFocus);
  635.         until (NewFocus = High(Buttons)) or (Buttons[NewFocus].Visible);
  636.         if Buttons[NewFocus].Visible then
  637.           SetFocusedButton(NewFocus);
  638.       end;
  639.     VK_LEFT:
  640.       if not Down then
  641.       begin
  642.         NewFocus := FocusedButton;
  643.         repeat
  644.           if NewFocus > Low(Buttons) then
  645.             NewFocus := Pred(NewFocus);
  646.         until (NewFocus = Low(Buttons)) or (Buttons[NewFocus].Visible);
  647.         if Buttons[NewFocus].Visible then
  648.           SetFocusedButton(NewFocus);
  649.       end;
  650.     VK_SPACE:
  651.       begin
  652.         if Buttons[FocusedButton].Enabled then
  653.         begin
  654.           CurrentButton := FocusedButton;
  655.           Down := True;
  656.           DrawButton(CurrentButton);
  657.         end;
  658.       end;
  659.   end;
  660. end;
  661. procedure TVrNavigator.ButtonClick(Button: TVrButtonType);
  662. begin
  663.   if Assigned(FOnButtonClick) then FOnButtonClick(Self, Button);
  664. end;
  665. { TVrMediaButton }
  666. constructor TVrMediaButton.Create(AOwner: TComponent);
  667. begin
  668.   inherited Create(AOwner);
  669.   ControlStyle := ControlStyle + [csOpaque] - [csSetCaption];
  670.   Height := 15;
  671.   Width := 40;
  672.   TabStop := false;
  673.   FFocusColor := clBlue;
  674.   FBorderColor := clBlack;
  675.   FButtonType := btPause;
  676.   FEnGlyphs := TBitmap.Create;
  677.   FDiGlyphs := TBitmap.Create;
  678.   LoadBitmaps;
  679. end;
  680. destructor TVrMediaButton.Destroy;
  681. begin
  682.   FEnGlyphs.Free;
  683.   FDiGlyphs.Free;
  684.   inherited Destroy;
  685. end;
  686. procedure TVrMediaButton.LoadBitmaps;
  687. begin
  688.   FEnGlyphs.Handle := LoadBitmap(hInstance, 'EN_IMAGES');
  689.   FDiGlyphs.Handle := LoadBitmap(hInstance, 'DI_IMAGES');
  690.   MaskColor := clOlive;
  691. end;
  692. procedure TVrMediaButton.SetButtonType(Value: TVrButtonType);
  693. begin
  694.   if FButtonType <> Value then
  695.   begin
  696.     FButtonType := Value;
  697.     UpdateControlCanvas;
  698.   end;
  699. end;
  700. procedure TVrMediaButton.SetFocusColor(Value: TColor);
  701. begin
  702.   if FFocusColor <> Value then
  703.   begin
  704.     FFocusColor := Value;
  705.     UpdateControlCanvas;
  706.   end;
  707. end;
  708. procedure TVrMediaButton.SetBorderColor(Value: TColor);
  709. begin
  710.   if FBorderColor <> Value then
  711.   begin
  712.     FBorderColor := Value;
  713.     UpdateControlCanvas;
  714.   end;
  715. end;
  716. procedure TVrMediaButton.Paint;
  717. var
  718.   R: TRect;
  719.   GW, GH, HRes: Integer;
  720.   Colors: array[0..1] of TColor;
  721.   BX, BY: Integer;
  722.   Glyph: TBitmap;
  723. begin
  724.   if Down then
  725.   begin
  726.     Colors[0] := clBtnFace;
  727.     Colors[1] := clBtnHighlight;
  728.   end
  729.   else
  730.   begin
  731.     Colors[0] := clBtnHighlight;
  732.     Colors[1] := clBtnShadow;
  733.   end;
  734.   HRes := ClientHeight div 10;
  735.   if HRes < 2 then HRes := 2;
  736.   R := ClientRect;
  737.   InflateRect(R, -2, -2);
  738.   DrawGradient(BitmapCanvas, R, Colors[0], Colors[1], voVertical, HRes);
  739.   if Enabled then
  740.     Glyph := FEnGlyphs else Glyph := FDiGlyphs;
  741.   GH := Glyph.Height;
  742.   GW := Glyph.Width div 10;
  743.   BX := (Width div 2) - (GW div 2);
  744.   BY := (Height div 2) - (GH div 2);
  745.   if Down then Inc(BY);
  746.   BitmapCanvas.Brush.Style := bsClear;
  747.   BitmapCanvas.BrushCopy(Bounds(BX, BY, GW, GH),
  748.     Glyph, Bounds(ord(ButtonType) * GW, 0, GW, GH), MaskColor);
  749.   R := ClientRect;
  750.   if HasFocus then
  751.     DrawFrame3D(BitmapCanvas, R, FFocusColor, FFocusColor, 1)
  752.   else DrawFrame3D(BitmapCanvas, R, FBorderColor, FBorderColor, 1);
  753.   if Down then
  754.     DrawFrame3D(BitmapCanvas, R, clBtnShadow, clBtnFace, 1)
  755.   else DrawOutline3D(BitmapCanvas, R, Colors[0], Colors[1], 1);
  756.   inherited Paint;
  757. end;
  758. procedure TVrMediaButton.DoMouseDown(XPos, YPos: Integer);
  759. var
  760.   P: TPoint;
  761. begin
  762.   P := Point(XPos, YPos);
  763.   if PtInRect(ClientRect, P) then
  764.   begin
  765.     Pressed := True;
  766.     Down := True;
  767.     MouseCapture := true;
  768.     UpdateControlCanvas;
  769.   end;
  770. end;
  771. procedure TVrMediaButton.WMLButtonDown(var Message: TWMLButtonDown);
  772. begin
  773.   inherited;
  774.   DoMouseDown(Message.XPos, Message.YPos);
  775.   if TabStop then SetFocus;
  776. end;
  777. procedure TVrMediaButton.WMLButtonDblClk(var Message: TWMLButtonDblClk);
  778. begin
  779.   inherited;
  780.   DoMouseDown(Message.XPos, Message.YPos);
  781. end;
  782. procedure TVrMediaButton.WMMouseMove(var Message: TWMMouseMove);
  783. var
  784.   P: TPoint;
  785. begin
  786.   inherited;
  787.   if Pressed then
  788.   begin
  789.     P := Point(Message.XPos, Message.YPos);
  790.     if PtInRect(ClientRect, P) <> Down then
  791.     begin
  792.       Down := not Down;
  793.       UpdateControlCanvas;
  794.     end;
  795.   end;
  796. end;
  797. procedure TVrMediaButton.WMLButtonUp(var Message: TWMLButtonUp);
  798. var
  799.   DoClick: Boolean;
  800. begin
  801.   MouseCapture := false;
  802.   DoClick := Pressed and Down;
  803.   Down := False;
  804.   Pressed := false;
  805.   if DoClick then UpdateControlCanvas;
  806.   inherited;
  807. end;
  808. procedure TVrMediaButton.CMEnabledChanged(var Message: TMessage);
  809. begin
  810.   inherited;
  811.   UpdateControlCanvas;
  812. end;
  813. procedure TVrMediaButton.WMSetFocus(var Message: TWMSetFocus);
  814. begin
  815.   HasFocus := true;
  816.   UpdateControlCanvas;
  817.   inherited;
  818. end;
  819. procedure TVrMediaButton.WMKillFocus(var Message: TWMKillFocus);
  820. begin
  821.   HasFocus := false;
  822.   UpdateControlCanvas;
  823.   inherited;
  824. end;
  825. procedure TVrMediaButton.KeyDown(var Key: Word; Shift: TShiftState);
  826. begin
  827.   inherited KeyDown(Key, Shift);
  828.   if (not Down) and (Key = VK_SPACE) then DoMouseDown(0, 0);
  829. end;
  830. procedure TVrMediaButton.KeyUp(var Key: Word; Shift: TShiftState);
  831. begin
  832.   inherited KeyUp(Key, Shift);
  833.   if Key = VK_SPACE then
  834.   begin
  835.     MouseCapture := false;
  836.     if Pressed and Down then
  837.     begin
  838.       Down := False;
  839.       UpdateControlCanvas;
  840.       inherited Click;
  841.     end;
  842.     Pressed := False;
  843.   end;
  844. end;
  845. end.