fpspreadsheet: Fix crash of visual controls when workbook.RemoveAllWorksheets is called. Visual controls can handle the case of active worksheet = nil.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5937 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2017-06-14 16:40:36 +00:00
parent 22f08434bf
commit 7d59e20f9f
3 changed files with 27 additions and 3 deletions

View File

@ -688,7 +688,7 @@ type
{@@ Event procedure containing a specific worksheet } {@@ Event procedure containing a specific worksheet }
TsWorksheetEvent = procedure (Sender: TObject; ASheet: TsWorksheet) of object; TsWorksheetEvent = procedure (Sender: TObject; ASheet: TsWorksheet) of object;
{@@ Event procedure called when a worksheet is removed } {@@ Event procedure called when a worksheet is removed. ASheetIndex = -1 --> all sheets }
TsRemoveWorksheetEvent = procedure (Sender: TObject; ASheetIndex: Integer) of object; TsRemoveWorksheetEvent = procedure (Sender: TObject; ASheetIndex: Integer) of object;
@ -8051,7 +8051,9 @@ end;
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
destructor TsWorkbook.Destroy; destructor TsWorkbook.Destroy;
begin begin
DisableNotifications;
RemoveAllWorksheets; RemoveAllWorksheets;
EnableNotifications;
FWorksheets.Free; FWorksheets.Free;
FCellFormatList.Free; FCellFormatList.Free;
@ -8791,7 +8793,11 @@ end;
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
procedure TsWorkbook.RemoveAllWorksheets; procedure TsWorkbook.RemoveAllWorksheets;
begin begin
FActiveWorksheet := nil;
FWorksheets.ForEachCall(RemoveWorksheetsCallback, nil); FWorksheets.ForEachCall(RemoveWorksheetsCallback, nil);
FWorksheets.Clear;
if (FLockCount = 0) and Assigned(FOnRemoveWorksheet) then
FOnRemoveWorksheet(self, -1);
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------

View File

@ -1610,7 +1610,11 @@ begin
else else
sheet := Workbook.GetWorksheetbyIndex(ASheetIndex); sheet := Workbook.GetWorksheetbyIndex(ASheetIndex);
end else end else
sheet := FWorksheet; if ASheetIndex > -1 then
sheet := FWorksheet
else
sheet := nil; // all sheets were removed
// FWorksheet := sheet; // is needed by listeners! // FWorksheet := sheet; // is needed by listeners!
NotifyListeners([lniWorksheetRemove]); NotifyListeners([lniWorksheetRemove]);
SelectWorksheet(sheet); SelectWorksheet(sheet);
@ -1882,6 +1886,9 @@ end;
procedure TsCellEdit.DoEnter; procedure TsCellEdit.DoEnter;
begin begin
if Worksheet = nil then
exit;
if not CanEditCell(Worksheet.ActiveCellRow, Worksheet.ActiveCellCol) then if not CanEditCell(Worksheet.ActiveCellRow, Worksheet.ActiveCellCol) then
begin begin
MessageDlg('This cell is protected from editing. Unlock worksheet protection '+ MessageDlg('This cell is protected from editing. Unlock worksheet protection '+

View File

@ -2242,6 +2242,8 @@ var
TL: TPoint; TL: TPoint;
begin begin
inherited; inherited;
if Worksheet = nil then
exit;
FTopLeft := CalcTopLeft(false); FTopLeft := CalcTopLeft(false);
@ -4351,6 +4353,9 @@ var
clipArea: TRect; clipArea: TRect;
begin begin
if Worksheet = nil then
exit;
sr := GetWorksheetRow(ARow); sr := GetWorksheetRow(ARow);
scLastused := Worksheet.GetLastColIndex; scLastused := Worksheet.GetLastColIndex;
gc := AFirstCol; gc := AFirstCol;
@ -4966,6 +4971,9 @@ var
cell: PCell; cell: PCell;
r, c: Cardinal; r, c: Cardinal;
begin begin
if Worksheet = nil then
exit;
if FAllowDragAndDrop and if FAllowDragAndDrop and
(not Assigned(DragManager) or not DragManager.IsDragging) and (not Assigned(DragManager) or not DragManager.IsDragging) and
(ssLeft in Shift) and (ssLeft in Shift) and
@ -5016,6 +5024,9 @@ procedure TsCustomWorksheetGrid.MouseMove(Shift: TShiftState; X, Y: Integer);
var var
prevMouseCell: TPoint; prevMouseCell: TPoint;
begin begin
if Worksheet = nil then
exit;
prevMouseCell := GCache.MouseCell; prevMouseCell := GCache.MouseCell;
inherited; inherited;
@ -5264,7 +5275,7 @@ var
cp: TsCellprotections; cp: TsCellprotections;
begin begin
Result := inherited; Result := inherited;
if Result and Worksheet.IsProtected then if Result and Assigned(Worksheet) and Worksheet.IsProtected then
begin begin
cell := Worksheet.FindCell(GetWorksheetRow(ARow), GetWorksheetCol(ACol)); cell := Worksheet.FindCell(GetWorksheetRow(ARow), GetWorksheetCol(ACol));
cp := Worksheet.ReadCellProtection(cell); cp := Worksheet.ReadCellProtection(cell);