From a3d9027f5260dbffbf3f5728b0fe8698fc1b6b20 Mon Sep 17 00:00:00 2001 From: juha Date: Sun, 3 Feb 2013 23:34:11 +0000 Subject: [PATCH] LCL, CoolBar: Use InsertControl to add bands automatically when a control is dropped to CoolBar git-svn-id: trunk@40167 - --- lcl/comctrls.pp | 3 +- lcl/include/coolbar.inc | 90 +++++++++++++++++------------------------ 2 files changed, 40 insertions(+), 53 deletions(-) diff --git a/lcl/comctrls.pp b/lcl/comctrls.pp index ccfacf11c7..76135bb85e 100644 --- a/lcl/comctrls.pp +++ b/lcl/comctrls.pp @@ -2271,10 +2271,11 @@ type procedure SetShowText(aValue: Boolean); procedure SetVertical(aValue: Boolean); procedure ImageListChange(Sender: TObject); - procedure OnIdle(Sender: TObject; var Done: Boolean); protected procedure AlignControls(aControl: TControl; var aRect: TRect); override; procedure Notification(AComponent: TComponent; Operation: TOperation); override; + procedure InsertControl(AControl: TControl; Index: integer); override; + procedure RemoveControl(AControl: TControl); override; procedure Loaded; override; procedure Paint; override; procedure Resize; override; diff --git a/lcl/include/coolbar.inc b/lcl/include/coolbar.inc index 2957fb8fe9..7f5a2e57ba 100644 --- a/lcl/include/coolbar.inc +++ b/lcl/include/coolbar.inc @@ -209,6 +209,7 @@ var Band: TCoolBand; begin if FControl = aValue then Exit; + FCoolBar.BeginUpdate; if Assigned(aValue) then begin Band := TCoolBands(Collection).FindBand(aValue); if Assigned(Band) and (Band <> Self) then begin @@ -221,6 +222,7 @@ begin aValue.Parent := Nil; end; FControl := aValue; + FCoolBar.EndUpdate; Changed(True); end; @@ -344,7 +346,8 @@ begin inherited Update(aItem); if FCoolBar = Nil then Exit; if csDestroying in FCoolBar.ComponentState then Exit; - UpdControls; // Calculate control positions + if FCoolBar.FUpdateCount = 0 then + UpdControls; // Calculate control positions end; procedure TCoolBands.Notify(aItem: TCollectionItem; aAction: TCollectionNotification); @@ -398,9 +401,6 @@ begin Inc(hh, 2); Band.FTop := yy; Band.UpdControl; // Set control's location - //if Assigned(Band.FControl) then - // DebugLn(['TCoolBands.UpdControls, FTop=', Band.FTop, - // ', FControl.Name=', Band.FControl.Name, ', FControl.ClassName=', Band.FControl.ClassName]); Inc(yy, hh+3); end; end; @@ -496,66 +496,52 @@ end; procedure TCustomCoolBar.AlignControls(aControl: TControl; var aRect: TRect); begin //DebugLn('TCoolBar.AlignControls'); - FBands.UpdControls; - inherited AlignControls(aControl, aRect); -end; - -procedure TCustomCoolBar.OnIdle(Sender: TObject; var Done: Boolean); -var - Band: TCoolBand; -begin - Assert(Assigned(FNewControl) and Assigned(FNewControl.Parent), - 'TCoolBar.OnIdle: FNewControl or FNewControl.Parent not assigned'); - DebugLn(['TCoolBar.OnIdle, Control.Name=', FNewControl.Name]); - if FNewControl.Parent = Self then + if FUpdateCount = 0 then begin - Band := FBands.Add; - Band.Control := FNewControl; - FNewControl := Nil; + FBands.UpdControls; + inherited AlignControls(aControl, aRect); end; - Application.RemoveOnIdleHandler(@OnIdle); end; procedure TCustomCoolBar.Notification(AComponent: TComponent; Operation: TOperation); -var - Band: TCoolBand; - s: String; begin inherited Notification(AComponent, Operation); if csDestroying in ComponentState then Exit; - case Operation of - opInsert: begin - if AComponent is TWinControl then - begin - s := ', Comp=' + AComponent.Name + ', class=' + AComponent.ClassName; - if csLoading in ComponentState then - DebugLn('TCoolBar.Notification: Operation = opInsert, Loading' + s) - else begin - if AComponent <> Self then - begin - DebugLn('TCoolBar.Notification: Operation = opInsert, not Loading, adding OnIdle' + s); - // Delay adding band and setting new control's properties by using OnIdle handler. - // Setting them directly in this Notification procedure leads to a crash. - FNewControl := TControl(AComponent); - Application.AddOnIdleHandler(@OnIdle); - end; - end; - end; - end; - opRemove: begin - DebugLn('TCoolBar.Notification: Operation = opRemove'); - if AComponent is TWinControl then - begin - Band := Bands.FindBand(TControl(AComponent)); - if Assigned(Band) then - Band.FControl := nil; - end - else if AComponent = FImages then - Images := nil; + if Operation = opRemove then + begin + DebugLn('TCoolBar.Notification: Operation = opRemove'); + if AComponent = FImages then + Images := nil; + end; +end; + +procedure TCustomCoolBar.InsertControl(AControl: TControl; Index: integer); +var + Band: TCoolBand; +begin + inherited InsertControl(AControl, Index); + if (AControl is TWinControl) and not (csLoading in ComponentState) then + begin + Band := Bands.FindBand(AControl); + if Band = Nil then + begin + DebugLn('TCoolBar.InsertControl: Adding band for Comp=' + AControl.Name + ', class=' + AControl.ClassName); + Band := FBands.Add; + Band.Control := AControl; end; end; end; +procedure TCustomCoolBar.RemoveControl(AControl: TControl); +var + Band: TCoolBand; +begin + Band := Bands.FindBand(AControl); + if Assigned(Band) then + Band.FControl := nil; + inherited RemoveControl(AControl); +end; + procedure TCustomCoolBar.Loaded; begin inherited Loaded;