From ce34aeb40519c70be4020583f4b44f445947869a Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Mon, 15 Sep 2014 07:33:55 +0000 Subject: [PATCH] fpspreadsheet: Fix new TsWorksheetGrid crashing without workbook. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3567 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/fpsgrid_no_install/mainfrm.lfm | 4 ++-- .../examples/fpsgrid_no_install/mainfrm.pas | 14 +++++++++----- components/fpspreadsheet/fpspreadsheetgrid.pas | 15 ++++++++++----- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/components/fpspreadsheet/examples/fpsgrid_no_install/mainfrm.lfm b/components/fpspreadsheet/examples/fpsgrid_no_install/mainfrm.lfm index 833da40ca..c6d3c1932 100644 --- a/components/fpspreadsheet/examples/fpsgrid_no_install/mainfrm.lfm +++ b/components/fpspreadsheet/examples/fpsgrid_no_install/mainfrm.lfm @@ -32,7 +32,7 @@ object Form1: TForm1 Height = 25 Top = 6 Width = 75 - Caption = 'Load' + Caption = 'Load...' OnClick = BtnLoadClick TabOrder = 1 end @@ -41,7 +41,7 @@ object Form1: TForm1 Height = 25 Top = 6 Width = 75 - Caption = 'Save' + Caption = 'Save...' OnClick = BtnSaveClick TabOrder = 2 end diff --git a/components/fpspreadsheet/examples/fpsgrid_no_install/mainfrm.pas b/components/fpspreadsheet/examples/fpsgrid_no_install/mainfrm.pas index 27547b546..4126e0909 100644 --- a/components/fpspreadsheet/examples/fpsgrid_no_install/mainfrm.pas +++ b/components/fpspreadsheet/examples/fpsgrid_no_install/mainfrm.pas @@ -47,15 +47,19 @@ procedure TForm1.FormCreate(Sender: TObject); begin Grid := TsWorksheetGrid.Create(self); - // Put the grid into the TabControl + // Put the grid into the TabControl and align it to fill the tabcontrol. Grid.Parent := TabControl; Grid.Align := alClient; // Useful options - Grid.Options := Grid.Options + [goColSizing, goRowSizing, goEditing, goThumbTracking]; - Grid.AutoAdvance := aaDown; - Grid.MouseWheelOption := mwGrid; - Grid.TextOverflow := true; + Grid.Options := Grid.Options + [goColSizing, goRowSizing, + goFixedColSizing, // useful if the spreadsheet contains frozen columns + goEditing, // needed for modifying cell content + goThumbTracking // see the grid scroll while you drag the scrollbar + ]; + Grid.AutoAdvance := aaDown; // move active cell down on ENTER + Grid.MouseWheelOption := mwGrid; // mouse wheel scrolls the grid, not the active cell + Grid.TextOverflow := true; // too long text extends into neighbor cells // Create an empty worksheet Grid.NewWorkbook(26, 100); diff --git a/components/fpspreadsheet/fpspreadsheetgrid.pas b/components/fpspreadsheet/fpspreadsheetgrid.pas index 8d332400e..ac89308d6 100644 --- a/components/fpspreadsheet/fpspreadsheetgrid.pas +++ b/components/fpspreadsheet/fpspreadsheetgrid.pas @@ -1469,7 +1469,6 @@ begin end; sr := GetWorksheetRow(ARow); - gcLastUsed := GetGridCol(FWorksheet.GetLastOccupiedColIndex); // Draw columns in this row with GCache.VisibleGrid do @@ -1479,7 +1478,7 @@ begin // Because of possible cell overflow from cells left of the visible range // we have to seek to the left for the first occupied text cell // and start painting from here. - if FTextOverflow and (sr <> Cardinal(-1)) then + if FTextOverflow and (sr <> Cardinal(-1)) and Assigned(FWorksheet) then while (gc > FixedCols) do begin dec(gc); @@ -1503,7 +1502,9 @@ begin // Now find the last column. Again text can overflow into the visible area // from cells to the right. gcLast := Right; - if FTextOverflow and (sr <> Cardinal(-1)) then + if FTextOverflow and (sr <> Cardinal(-1)) and Assigned(FWorksheet) then + begin + gcLastUsed := GetGridCol(FWorksheet.GetLastOccupiedColIndex); while (gcLast < ColCount-1) and (gcLast < gcLastUsed) do begin inc(gcLast); cell := FWorksheet.FindCell(sr, GetWorksheetCol(gcLast)); @@ -1522,6 +1523,7 @@ begin gcLast := Right; Break; end; + end; while (gc <= gcLast) do begin gr := ARow; @@ -1530,7 +1532,7 @@ begin // it to avoid excessive calls to "FindCell". FDrawingCell := nil; gcNext := gc + 1; - if (FWorksheet <> nil) and (gr >= FixedRows) and (gc >= FixedCols) then + if Assigned(FWorksheet) and (gr >= FixedRows) and (gc >= FixedCols) then begin cell := FWorksheet.FindCell(GetWorksheetRow(gr), GetWorksheetCol(gc)); if (cell = nil) or (cell^.MergedNeighbors = []) then begin @@ -1599,7 +1601,10 @@ begin // is this column within the ClipRect? if (rct.Left < rct.Right) and HorizontalIntersect(rct, clipArea) then begin - FDrawingCell := FWorksheet.FindCell(GetWorksheetRow(gr), GetWorksheetCol(gc)); + if Assigned(FWorksheet) then + FDrawingCell := FWorksheet.FindCell(GetWorksheetRow(gr), GetWorksheetCol(gc)) + else + FDrawingCell := nil; DoDrawCell(gc, gr); end; end;