From 18965c7740d877921515336b3aaf6b014977fe21 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sun, 19 Mar 2017 23:09:01 +0000 Subject: [PATCH] fpspreadsheet: Further integration of cell and worksheet protection in visual controls. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5813 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../source/common/fpspreadsheet.pas | 26 ++++++++++++--- .../source/visual/fpsactions.pas | 32 +++++++++++-------- .../source/visual/fpspreadsheetctrls.pas | 2 +- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/components/fpspreadsheet/source/common/fpspreadsheet.pas b/components/fpspreadsheet/source/common/fpspreadsheet.pas index bf42719c9..339ca9519 100644 --- a/components/fpspreadsheet/source/common/fpspreadsheet.pas +++ b/components/fpspreadsheet/source/common/fpspreadsheet.pas @@ -859,15 +859,18 @@ type { Utilities } function ConvertUnits(AValue: Double; AFromUnits, AToUnits: TsSizeUnits): Double; - procedure DisableNotifications; - procedure EnableNotifications; - function NotificationsEnabled: Boolean; procedure UpdateCaches; procedure GetLastRowColIndex(out ALastRow, ALastCol: Cardinal); { Protection } function IsProtected: Boolean; + { Notification } + procedure ChangedWorksheet(AWorksheet: TsWorksheet); + procedure DisableNotifications; + procedure EnableNotifications; + function NotificationsEnabled: Boolean; + { Error messages } procedure AddErrorMsg(const AMsg: String); overload; procedure AddErrorMsg(const AMsg: String; const Args: array of const); overload; @@ -4116,8 +4119,7 @@ begin if AValue = FBiDiMode then exit; FBiDiMode := AValue; - if (FWorkbook.FLockCount = 0) and Assigned(FWorkbook.FOnChangeWorksheet) then - FWorkbook.FOnChangeWorksheet(FWorkbook, self); + FWorkbook.ChangedWorksheet(Self); end; {@@ ---------------------------------------------------------------------------- @@ -4129,6 +4131,7 @@ begin if AEnable then Include(FOptions, soProtected) else Exclude(FOptions, soProtected); + FWorkbook.ChangedWorksheet(self); end; {@@ ---------------------------------------------------------------------------- @@ -7958,6 +7961,19 @@ begin TsWorksheet(data).Free; end; +{@@ ---------------------------------------------------------------------------- + Notification of visual controls that some global data of a worksheet + have changed. +-------------------------------------------------------------------------------} +procedure TsWorkbook.ChangedWorksheet(AWorksheet: TsWorksheet); +begin + if FReadWriteFlag = rwfRead then + exit; + + if NotificationsEnabled and Assigned(FOnChangeWorksheet) + then OnChangeWorksheet(self, AWorksheet); +end; + {@@ ---------------------------------------------------------------------------- Helper method to disable notification of visual controls -------------------------------------------------------------------------------} diff --git a/components/fpspreadsheet/source/visual/fpsactions.pas b/components/fpspreadsheet/source/visual/fpsactions.pas index 1a771e04c..282bab44e 100644 --- a/components/fpspreadsheet/source/visual/fpsactions.pas +++ b/components/fpspreadsheet/source/visual/fpsactions.pas @@ -118,6 +118,7 @@ type property Zoom: Integer read FZoom write SetZoom default 100; end; + { --- Actions related to cell and cell selection formatting--- } TsCopyItem = (ciFormat, ciValue, ciFormula, ciAll); @@ -158,6 +159,8 @@ type end; TsAutoFormatAction = class(TsCellAction) + protected + procedure UpdateTargetFromActiveCell; public procedure ExecuteTarget(Target: TObject); override; procedure UpdateTarget(Target: TObject); override; @@ -928,19 +931,21 @@ begin end; procedure TsAutoFormatAction.UpdateTarget(Target: TObject); +begin + Unused(Target); + Enabled := inherited Enabled and (Worksheet <> nil) and (Length(GetSelection) > 0); + if Enabled then + UpdateTargetFromActiveCell; +end; + +procedure TsAutoFormatAction.UpdateTargetFromActiveCell; var cell: PCell; begin - Unused(Target); - - Enabled := inherited Enabled and (Worksheet <> nil) and (Length(GetSelection) > 0); - if Enabled then - begin - cell := ActiveCell; - if Worksheet.IsMerged(cell) then - cell := Worksheet.FindMergeBase(cell); - ExtractFromCell(cell); - end; + cell := ActiveCell; + if Worksheet.IsMerged(cell) then + cell := Worksheet.FindMergeBase(cell); + ExtractFromCell(cell); end; @@ -1195,7 +1200,7 @@ procedure TsCellProtectionAction.ApplyFormatToCell(ACell: PCell); var p: TsCellProtections; begin - if not (Worksheet.IsProtected and (spCells in Worksheet.Protection)) then + if (Worksheet.IsProtected and (spCells in Worksheet.Protection)) then exit; p := Worksheet.ReadCellProtection(ACell); @@ -1216,12 +1221,13 @@ begin end; p := Worksheet.ReadCellProtection(ACell); Checked := FProtection in p; + Enabled := not (Worksheet.IsProtected and (spCells in Worksheet.Protection)); end; procedure TsCellProtectionAction.UpdateTarget(Target: TObject); begin - inherited; - Enabled := inherited Enabled and Worksheet.IsProtected and (spCells in Worksheet.Protection); + Unused(Target); + UpdateTargetFromActiveCell; end; diff --git a/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas b/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas index 51b6204ab..686e16742 100644 --- a/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas +++ b/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas @@ -29,7 +29,7 @@ interface uses Classes, Graphics, SysUtils, Controls, StdCtrls, ComCtrls, ValEdit, ActnList, LResources, - fpstypes, fpspreadsheet, {%H-}fpsAllFormats; + fpstypes, fpspreadsheet; //, {%H-}fpsAllFormats; type {@@ Event handler procedure for displaying a message if an error or