Browse Source

BCImageButton toggle functionality

Now you can use it as 'toggle' button, and get/set pressed state.
lainz 8 years ago
parent
commit
89b1aa422e
1 changed files with 62 additions and 11 deletions
  1. 62 11
      bcimagebutton.pas

+ 62 - 11
bcimagebutton.pas

@@ -237,16 +237,20 @@ type
     FBGRAMultiSliceScaling: TBGRAMultiSliceScaling;
     FBGRAMultiSliceScaling: TBGRAMultiSliceScaling;
     FBGRANormal, FBGRAHover, FBGRAActive, FBGRADisabled: TBGRABitmap;
     FBGRANormal, FBGRAHover, FBGRAActive, FBGRADisabled: TBGRABitmap;
     FDestRect: TRect;
     FDestRect: TRect;
+    FPressed: boolean;
     FTimer: TTimer;
     FTimer: TTimer;
     FFade: TFading;
     FFade: TFading;
     FAnimation: boolean;
     FAnimation: boolean;
     FBitmapFile: string;
     FBitmapFile: string;
     FTextVisible: boolean;
     FTextVisible: boolean;
+    FToggle: boolean;
     procedure SetFAnimation(AValue: boolean);
     procedure SetFAnimation(AValue: boolean);
     procedure SetFBitmapFile(AValue: string);
     procedure SetFBitmapFile(AValue: string);
     procedure SetFBitmapOptions(AValue: TBCImageButtonSliceScalingOptions);
     procedure SetFBitmapOptions(AValue: TBCImageButtonSliceScalingOptions);
     procedure Fade({%H-}Sender: TObject);
     procedure Fade({%H-}Sender: TObject);
+    procedure SetFPressed(AValue: boolean);
     procedure SetFTextVisible(AValue: boolean);
     procedure SetFTextVisible(AValue: boolean);
+    procedure SetFToggle(AValue: boolean);
   protected
   protected
     { Protected declarations }
     { Protected declarations }
     procedure DrawControl; override;
     procedure DrawControl; override;
@@ -260,8 +264,12 @@ type
     procedure DoMouseUp; override;
     procedure DoMouseUp; override;
     procedure DoMouseEnter; override;
     procedure DoMouseEnter; override;
     procedure DoMouseLeave; override;
     procedure DoMouseLeave; override;
+    procedure Click; override;
   public
   public
     { Public declarations }
     { Public declarations }
+    property Toggle: boolean read FToggle write SetFToggle default False;
+    property Pressed: boolean read FPressed write SetFPressed default False;
+    //property State: TBCGraphicButtonState read FState;
     property BitmapOptions: TBCImageButtonSliceScalingOptions
     property BitmapOptions: TBCImageButtonSliceScalingOptions
       read FBitmapOptions write SetFBitmapOptions;
       read FBitmapOptions write SetFBitmapOptions;
     property Animation: boolean read FAnimation write SetFAnimation default True;
     property Animation: boolean read FAnimation write SetFAnimation default True;
@@ -336,13 +344,15 @@ type
     //property SoundClick;
     //property SoundClick;
     //property SoundEnter;
     //property SoundEnter;
     property TextVisible;
     property TextVisible;
-    //property Toggle;
+    property Toggle;
+    property Pressed;
     property Visible;
     property Visible;
   end;
   end;
 
 
 procedure Register;
 procedure Register;
 
 
 implementation
 implementation
+
 uses
 uses
   LCLType;
   LCLType;
 
 
@@ -883,6 +893,15 @@ begin
     Invalidate;
     Invalidate;
 end;
 end;
 
 
+procedure TBCCustomImageButton.SetFPressed(AValue: boolean);
+begin
+  if FPressed = AValue then
+    Exit;
+  FPressed := AValue;
+
+  RenderControl;
+end;
+
 procedure TBCCustomImageButton.SetFTextVisible(AValue: boolean);
 procedure TBCCustomImageButton.SetFTextVisible(AValue: boolean);
 begin
 begin
   if FTextVisible = AValue then
   if FTextVisible = AValue then
@@ -892,6 +911,13 @@ begin
   RenderControl;
   RenderControl;
 end;
 end;
 
 
+procedure TBCCustomImageButton.SetFToggle(AValue: boolean);
+begin
+  if FToggle = AValue then
+    Exit;
+  FToggle := AValue;
+end;
+
 procedure TBCCustomImageButton.SetFBitmapOptions(AValue:
 procedure TBCCustomImageButton.SetFBitmapOptions(AValue:
   TBCImageButtonSliceScalingOptions);
   TBCImageButtonSliceScalingOptions);
 begin
 begin
@@ -926,18 +952,34 @@ begin
 
 
   if Enabled then
   if Enabled then
   begin
   begin
-    case FState of
-      gbsNormal, gbsHover: FBGRANormal.Draw(Canvas, FDestRect.Left,
-          FDestRect.Top, False);
-      gbsActive: FBGRAActive.Draw(Canvas, FDestRect.Left, FDestRect.Top, False);
-    end;
+    if (Toggle) then
+    begin
+      if (Pressed) then
+        FBGRAActive.Draw(Canvas, FDestRect.Left, FDestRect.Top, False)
+      else
+        case FState of
+          gbsHover: FBGRAHover.Draw(Canvas, FDestRect.Left,
+              FDestRect.Top, False);
+          else
+            FBGRANormal.Draw(Canvas, FDestRect.Left,
+              FDestRect.Top, False);
+        end;
+    end
+    else
+    begin
+      case FState of
+        gbsNormal, gbsHover: FBGRANormal.Draw(Canvas, FDestRect.Left,
+            FDestRect.Top, False);
+        gbsActive: FBGRAActive.Draw(Canvas, FDestRect.Left, FDestRect.Top, False);
+      end;
 
 
-    temp := TBGRABitmap.Create(Width, Height);
-    FFade.Execute;
-    FFade.PutImage(temp, 0, 0, FBGRAHover);
+      temp := TBGRABitmap.Create(Width, Height);
+      FFade.Execute;
+      FFade.PutImage(temp, 0, 0, FBGRAHover);
 
 
-    temp.Draw(Canvas, FDestRect.Left, FDestRect.Top, False);
-    temp.Free;
+      temp.Draw(Canvas, FDestRect.Left, FDestRect.Top, False);
+      temp.Free;
+    end;
   end
   end
   else
   else
     FBGRADisabled.Draw(Canvas, FDestRect.Left, FDestRect.Top, False);
     FBGRADisabled.Draw(Canvas, FDestRect.Left, FDestRect.Top, False);
@@ -1157,6 +1199,15 @@ begin
   inherited DoMouseLeave;
   inherited DoMouseLeave;
 end;
 end;
 
 
+procedure TBCCustomImageButton.Click;
+begin
+  inherited Click;
+  if (Toggle) then
+  begin
+    Pressed := not Pressed;
+  end;
+end;
+
 constructor TBCCustomImageButton.Create(AOwner: TComponent);
 constructor TBCCustomImageButton.Create(AOwner: TComponent);
 begin
 begin
   inherited Create(AOwner);
   inherited Create(AOwner);