Browse Source

refactor belonging

Johann ELSASS 5 years ago
parent
commit
9600332334
2 changed files with 112 additions and 71 deletions
  1. 53 59
      bgrasvgtheme.pas
  2. 59 12
      bgratheme.pas

+ 53 - 59
bgrasvgtheme.pas

@@ -60,7 +60,6 @@ type
     procedure SetRadioButtonChecked(AValue: TStringList);
     procedure SetRadioButtonUnchecked(AValue: TStringList);
   protected
-    procedure Refresh;
     procedure LoadTheme(const XMLConf: TXMLConfig);
     procedure SaveTheme(const XMLConf: TXMLConfig);
     procedure CheckEmptyResourceException(const aResource: string);
@@ -301,7 +300,7 @@ begin
   if (AValue <> FCheckBoxUnchecked) then
   begin
     FCheckBoxUnchecked.Assign(AValue);
-    Refresh;
+    InvalidateThemedControls;
   end;
 end;
 
@@ -310,14 +309,14 @@ begin
   if FColorizeActive = AValue then
     Exit;
   FColorizeActive := AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetColorizeActiveOp(AValue: TBlendOperation);
 begin
   if FColorizeActiveOp=AValue then Exit;
   FColorizeActiveOp:=AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetColorizeDisabled(AValue: string);
@@ -325,14 +324,14 @@ begin
   if FColorizeDisabled = AValue then
     Exit;
   FColorizeDisabled := AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetColorizeDisabledOp(AValue: TBlendOperation);
 begin
   if FColorizeDisabledOp=AValue then Exit;
   FColorizeDisabledOp:=AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetColorizeHover(AValue: string);
@@ -340,14 +339,14 @@ begin
   if FColorizeHover = AValue then
     Exit;
   FColorizeHover := AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetColorizeHoverOp(AValue: TBlendOperation);
 begin
   if FColorizeHoverOp=AValue then Exit;
   FColorizeHoverOp:=AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetColorizeNormal(AValue: string);
@@ -355,14 +354,14 @@ begin
   if FColorizeNormal = AValue then
     Exit;
   FColorizeNormal := AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetColorizeNormalOp(AValue: TBlendOperation);
 begin
   if FColorizeNormalOp=AValue then Exit;
   FColorizeNormalOp:=AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetRadioButtonChecked(AValue: TStringList);
@@ -371,7 +370,7 @@ begin
   if (AValue <> FRadioButtonChecked) then
   begin
     FRadioButtonChecked.Assign(AValue);
-    Refresh;
+    InvalidateThemedControls;
   end;
 end;
 
@@ -381,16 +380,10 @@ begin
   if (AValue <> FRadioButtonUnchecked) then
   begin
     FRadioButtonUnchecked.Assign(AValue);
-    Refresh;
+    InvalidateThemedControls;
   end;
 end;
 
-procedure TBGRASVGTheme.Refresh;
-begin
-  if Assigned(FOwner) and (FOwner is TControl) then
-    TControl(FOwner).Invalidate;
-end;
-
 procedure TBGRASVGTheme.LoadDefaultTheme;
 begin
   FCheckBoxUnchecked.Text := RES_CHECKBOXUNCHECKED;
@@ -417,35 +410,39 @@ end;
 
 procedure TBGRASVGTheme.LoadTheme(const XMLConf: TXMLConfig);
 begin
-  XMLConf.RootName := 'BGRASVGTheme';
-  // Button
-  FButtonActive.Text := XMLConf.GetValue('Button/Active/SVG', RES_BUTTON){%H-};
-  FButtonHover.Text := XMLConf.GetValue('Button/Hover/SVG', ''){%H-};
-  FButtonNormal.Text := XMLConf.GetValue('Button/Normal/SVG', ''){%H-};
-  FButtonSliceScalingBottom := XMLConf.GetValue('Button/SliceScaling/Bottom', 10);
-  FButtonSliceScalingLeft := XMLConf.GetValue('Button/SliceScaling/Left', 10);
-  FButtonSliceScalingRight := XMLConf.GetValue('Button/SliceScaling/Right', 10);
-  FButtonSliceScalingTop := XMLConf.GetValue('Button/SliceScaling/Top', 10);
-  // CheckBox
-  FCheckBoxChecked.Text := XMLConf.GetValue('CheckBox/Checked/SVG',
-    RES_CHECKBOXCHECKED){%H-};
-  FCheckBoxUnchecked.Text := XMLConf.GetValue('CheckBox/Unchecked/SVG',
-    RES_CHECKBOXUNCHECKED){%H-};
-  FCheckBoxTextSpacing := XMLConf.GetValue('CheckBox/TextSpacing', DEFAULT_CHECKBOX_TEXT_SPACING);
-  // Colorize
-  FColorizeActive := XMLConf{%H-}.GetValue('Colorize/Active', RES_COLORIZEACTIVE);
-  FColorizeDisabled := XMLConf{%H-}.GetValue('Colorize/Disabled', RES_COLORIZEDISABLED);
-  FColorizeHover := XMLConf{%H-}.GetValue('Colorize/Hover', RES_COLORIZEHOVER);
-  FColorizeNormal := XMLConf{%H-}.GetValue('Colorize/Normal', RES_COLORIZENORMAL);
-  FColorizeActiveOp := StrToBlendOperation(XMLConf{%H-}.GetValue('Colorize/ActiveOp', BlendOperationStr[boTransparent]));
-  FColorizeDisabledOp := StrToBlendOperation(XMLConf{%H-}.GetValue('Colorize/DisabledOp', BlendOperationStr[boTransparent]));
-  FColorizeHoverOp := StrToBlendOperation(XMLConf{%H-}.GetValue('Colorize/HoverOp', BlendOperationStr[boTransparent]));
-  FColorizeNormalOp := StrToBlendOperation(XMLConf{%H-}.GetValue('Colorize/NormalOp', BlendOperationStr[boTransparent]));
-  // RadioButton
-  FRadioButtonChecked.Text :=
-    XMLConf.GetValue('RadioButton/Checked/SVG', RES_RADIOBUTTONCHECKED{%H-}){%H-};
-  FRadioButtonUnchecked.Text :=
-    XMLConf.GetValue('RadioButton/Unchecked/SVG', RES_RADIOBUTTONUNCHECKED{%H-}){%H-};
+  try
+    XMLConf.RootName := 'BGRASVGTheme';
+    // Button
+    FButtonActive.Text := XMLConf.GetValue('Button/Active/SVG', RES_BUTTON){%H-};
+    FButtonHover.Text := XMLConf.GetValue('Button/Hover/SVG', ''){%H-};
+    FButtonNormal.Text := XMLConf.GetValue('Button/Normal/SVG', ''){%H-};
+    FButtonSliceScalingBottom := XMLConf.GetValue('Button/SliceScaling/Bottom', 10);
+    FButtonSliceScalingLeft := XMLConf.GetValue('Button/SliceScaling/Left', 10);
+    FButtonSliceScalingRight := XMLConf.GetValue('Button/SliceScaling/Right', 10);
+    FButtonSliceScalingTop := XMLConf.GetValue('Button/SliceScaling/Top', 10);
+    // CheckBox
+    FCheckBoxChecked.Text := XMLConf.GetValue('CheckBox/Checked/SVG',
+      RES_CHECKBOXCHECKED){%H-};
+    FCheckBoxUnchecked.Text := XMLConf.GetValue('CheckBox/Unchecked/SVG',
+      RES_CHECKBOXUNCHECKED){%H-};
+    FCheckBoxTextSpacing := XMLConf.GetValue('CheckBox/TextSpacing', DEFAULT_CHECKBOX_TEXT_SPACING);
+    // Colorize
+    FColorizeActive := XMLConf{%H-}.GetValue('Colorize/Active', RES_COLORIZEACTIVE);
+    FColorizeDisabled := XMLConf{%H-}.GetValue('Colorize/Disabled', RES_COLORIZEDISABLED);
+    FColorizeHover := XMLConf{%H-}.GetValue('Colorize/Hover', RES_COLORIZEHOVER);
+    FColorizeNormal := XMLConf{%H-}.GetValue('Colorize/Normal', RES_COLORIZENORMAL);
+    FColorizeActiveOp := StrToBlendOperation(XMLConf{%H-}.GetValue('Colorize/ActiveOp', BlendOperationStr[boTransparent]));
+    FColorizeDisabledOp := StrToBlendOperation(XMLConf{%H-}.GetValue('Colorize/DisabledOp', BlendOperationStr[boTransparent]));
+    FColorizeHoverOp := StrToBlendOperation(XMLConf{%H-}.GetValue('Colorize/HoverOp', BlendOperationStr[boTransparent]));
+    FColorizeNormalOp := StrToBlendOperation(XMLConf{%H-}.GetValue('Colorize/NormalOp', BlendOperationStr[boTransparent]));
+    // RadioButton
+    FRadioButtonChecked.Text :=
+      XMLConf.GetValue('RadioButton/Checked/SVG', RES_RADIOBUTTONCHECKED{%H-}){%H-};
+    FRadioButtonUnchecked.Text :=
+      XMLConf.GetValue('RadioButton/Unchecked/SVG', RES_RADIOBUTTONUNCHECKED{%H-}){%H-};
+  finally
+    InvalidateThemedControls;
+  end;
 end;
 
 procedure TBGRASVGTheme.SaveTheme(const XMLConf: TXMLConfig);
@@ -699,7 +696,7 @@ begin
   if (AValue <> FCheckBoxChecked) then
   begin
     FCheckBoxChecked.Assign(AValue);
-    Refresh;
+    InvalidateThemedControls;
   end;
 end;
 
@@ -707,7 +704,7 @@ procedure TBGRASVGTheme.SetCheckboxTextSpacing(AValue: integer);
 begin
   if FCheckboxTextSpacing=AValue then Exit;
   FCheckboxTextSpacing:=AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetButtonActive(AValue: TStringList);
@@ -715,7 +712,7 @@ begin
   if (AValue <> FButtonActive) then
   begin
     FButtonActive.Assign(AValue);
-    Refresh;
+    InvalidateThemedControls;
   end;
 end;
 
@@ -724,7 +721,7 @@ begin
   if (AValue <> FButtonHover) then
   begin
     FButtonHover.Assign(AValue);
-    Refresh;
+    InvalidateThemedControls;
   end;
 end;
 
@@ -734,7 +731,7 @@ begin
   if (AValue <> FButtonNormal) then
   begin
     FButtonNormal.Assign(AValue);
-    Refresh;
+    InvalidateThemedControls;
   end;
 end;
 
@@ -743,7 +740,7 @@ begin
   if FButtonSliceScalingBottom = AValue then
     Exit;
   FButtonSliceScalingBottom := AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetButtonSliceScalingLeft(AValue: integer);
@@ -751,7 +748,7 @@ begin
   if FButtonSliceScalingLeft = AValue then
     Exit;
   FButtonSliceScalingLeft := AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetButtonSliceScalingRight(AValue: integer);
@@ -759,7 +756,7 @@ begin
   if FButtonSliceScalingRight = AValue then
     Exit;
   FButtonSliceScalingRight := AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.SetButtonSliceScalingTop(AValue: integer);
@@ -767,7 +764,7 @@ begin
   if FButtonSliceScalingTop = AValue then
     Exit;
   FButtonSliceScalingTop := AValue;
-  Refresh;
+  InvalidateThemedControls;
 end;
 
 procedure TBGRASVGTheme.DrawCheckBox(Caption: string; State: TBGRAThemeButtonState;
@@ -828,7 +825,6 @@ begin
     LoadTheme(FXMLConf);
   finally
     FXMLConf.Free;
-    Refresh;
   end;
 end;
 
@@ -858,7 +854,6 @@ begin
     LoadTheme(FXMLConf);
   finally
     FXMLConf.Free;
-    Refresh;
   end;
 end;
 
@@ -869,7 +864,6 @@ begin
   AStream := BGRAResource.GetResourceStream(AResource);
   LoadFromStream(AStream);
   AStream.Free;
-  Refresh;
 end;
 
 end.

+ 59 - 12
bgratheme.pas

@@ -45,11 +45,8 @@ type
 
   TBGRAThemeControl = class(TCustomControl)
   private
-    procedure SetTheme(AValue: TBGRATheme);
-  protected
     FTheme: TBGRATheme;
-    procedure Notification(AComponent: TComponent;
-                            Operation: TOperation); override;
+    procedure SetTheme(AValue: TBGRATheme);
   published
     property Theme: TBGRATheme read FTheme write SetTheme;
   end;
@@ -58,16 +55,28 @@ type
 
   TBGRATheme = class(TComponent)
   private
+    FThemedControls: TList;
+    function GetThemedControl(AIndex: integer): TBGRAThemeControl;
+    function GetThemedControlCount: integer;
+    procedure AddThemedControl(AControl: TBGRAThemeControl);
+    procedure RemoveThemedControl(AControl: TBGRAThemeControl);
 
   protected
 
   public
+    constructor Create(AOwner: TComponent); override;
+    destructor Destroy; override;
+    procedure InvalidateThemedControls;
+
     procedure DrawButton(Caption: string; State: TBGRAThemeButtonState;
       Focused: boolean; ARect: TRect; ASurface: TBGRAThemeSurface); virtual;
     procedure DrawRadioButton(Caption: string; State: TBGRAThemeButtonState;
     {%H-}Focused: boolean; Checked: boolean; ARect: TRect; ASurface: TBGRAThemeSurface); virtual;
     procedure DrawCheckBox(Caption: string; State: TBGRAThemeButtonState;
     {%H-}Focused: boolean; Checked: boolean; ARect: TRect; ASurface: TBGRAThemeSurface); virtual;
+
+    property ThemedControlCount: integer read GetThemedControlCount;
+    property ThemedControl[AIndex: integer]: TBGRAThemeControl read GetThemedControl;
   published
 
   end;
@@ -91,18 +100,12 @@ end;
 procedure TBGRAThemeControl.SetTheme(AValue: TBGRATheme);
 begin
   if FTheme=AValue then Exit;
+  if Assigned(AValue) then AValue.RemoveThemedControl(self);
   FTheme:=AValue;
+  if Assigned(AValue) then AValue.AddThemedControl(self);
   Invalidate;
 end;
 
-procedure TBGRAThemeControl.Notification(AComponent: TComponent;
-  Operation: TOperation);
-begin
-  inherited Notification(AComponent, Operation);
-  if (Operation = opRemove) and (AComponent = FTheme)
-    then FTheme := nil;
-end;
-
 { TBGRAThemeSurface }
 
 function TBGRAThemeSurface.GetBitmap: TBGRABitmap;
@@ -178,6 +181,50 @@ end;
 
 { TBGRATheme }
 
+function TBGRATheme.GetThemedControl(AIndex: integer): TBGRAThemeControl;
+begin
+  result := TBGRAThemeControl(FThemedControls[AIndex]);
+end;
+
+function TBGRATheme.GetThemedControlCount: integer;
+begin
+  result := FThemedControls.Count;
+end;
+
+procedure TBGRATheme.AddThemedControl(AControl: TBGRAThemeControl);
+begin
+  if FThemedControls.IndexOf(AControl) = -1 then
+    FThemedControls.Add(AControl);
+end;
+
+procedure TBGRATheme.RemoveThemedControl(AControl: TBGRAThemeControl);
+begin
+  FThemedControls.Remove(AControl);
+end;
+
+constructor TBGRATheme.Create(AOwner: TComponent);
+begin
+  inherited Create(AOwner);
+  FThemedControls := TList.Create;
+end;
+
+destructor TBGRATheme.Destroy;
+var i: integer;
+begin
+  for i := ThemedControlCount-1 downto 0 do
+    ThemedControl[i].Theme := nil;
+  FThemedControls.Free;
+  inherited Destroy;
+end;
+
+procedure TBGRATheme.InvalidateThemedControls;
+var
+  i: Integer;
+begin
+  for i := 0 to ThemedControlCount-1 do
+    ThemedControl[i].Invalidate;
+end;
+
 procedure TBGRATheme.DrawButton(Caption: string; State: TBGRAThemeButtonState;
   Focused: boolean; ARect: TRect; ASurface: TBGRAThemeSurface);
 var