fpspreadsheet: Fix TsWorksheetGrid to be correctly notified of worksheet changes if not connected to a WorkbookSource (suggested by rvk, http://forum.lazarus.freepascal.org/index.php/topic,29214.msg184168.html#msg184168). Fix related demos.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4272 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2015-08-15 14:24:31 +00:00
parent f4cb9c14a7
commit adbc5c7c21
9 changed files with 132 additions and 30 deletions

View File

@@ -44,7 +44,6 @@
<ComponentName Value="Form1"/> <ComponentName Value="Form1"/>
<HasResources Value="True"/> <HasResources Value="True"/>
<ResourceBaseClass Value="Form"/> <ResourceBaseClass Value="Form"/>
<UnitName Value="main"/>
</Unit1> </Unit1>
</Units> </Units>
</ProjectOptions> </ProjectOptions>

View File

@@ -48,7 +48,6 @@
<ComponentName Value="Form1"/> <ComponentName Value="Form1"/>
<HasResources Value="True"/> <HasResources Value="True"/>
<ResourceBaseClass Value="Form"/> <ResourceBaseClass Value="Form"/>
<UnitName Value="mainform"/>
</Unit1> </Unit1>
</Units> </Units>
</ProjectOptions> </ProjectOptions>

View File

@@ -45,6 +45,31 @@ object Form1: TForm1
OnClick = BtnNewClick OnClick = BtnNewClick
TabOrder = 2 TabOrder = 2
end end
object Label2: TLabel
Left = 296
Height = 15
Top = 13
Width = 132
Caption = 'New text for cell [AB110]:'
ParentColor = False
end
object EdNewCellText: TEdit
Left = 451
Height = 23
Top = 9
Width = 104
TabOrder = 3
Text = 'Test'
end
object BtnEnterText: TButton
Left = 566
Height = 25
Top = 8
Width = 75
Caption = 'Set text'
OnClick = BtnEnterTextClick
TabOrder = 4
end
end end
object WorksheetGrid: TsWorksheetGrid object WorksheetGrid: TsWorksheetGrid
Left = 0 Left = 0
@@ -98,6 +123,10 @@ object Form1: TForm1
Text = 'Sheet 1' Text = 'Sheet 1'
end end
end end
object sWorkbookSource1: TsWorkbookSource
AutoDetectFormat = False
Options = []
end
object OpenDialog: TOpenDialog object OpenDialog: TOpenDialog
DefaultExt = '.xls' DefaultExt = '.xls'
Filter = 'Excel spreadsheet (*.xls)|*.xls|Excel XML spreadsheet (*.xlsx)|*.xlsx|LibreOffice/OpenOffice spreadsheet (*.ods)|*.ods|Wikitable (pipes) (.wikitable_pipes)|.wikitable_pipes|All files (*.*)|*.*' Filter = 'Excel spreadsheet (*.xls)|*.xls|Excel XML spreadsheet (*.xlsx)|*.xlsx|LibreOffice/OpenOffice spreadsheet (*.ods)|*.ods|Wikitable (pipes) (.wikitable_pipes)|.wikitable_pipes|All files (*.*)|*.*'

View File

@@ -6,8 +6,8 @@ interface
uses uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, Menus, ExtCtrls, ActnList, Spin, Buttons, StdCtrls, Menus, ExtCtrls, ActnList, Spin, Buttons, ButtonPanel,
ButtonPanel, fpspreadsheetgrid, fpspreadsheet, {%H-}fpsallformats; fpspreadsheetgrid, fpspreadsheet, fpsallformats, fpspreadsheetctrls;
type type
@@ -17,16 +17,21 @@ type
BtnOpen: TButton; BtnOpen: TButton;
BtnSave: TButton; BtnSave: TButton;
BtnNew: TButton; BtnNew: TButton;
BtnEnterText: TButton;
EdNewCellText: TEdit;
Label2: TLabel;
SheetsCombo: TComboBox; SheetsCombo: TComboBox;
Label1: TLabel; Label1: TLabel;
OpenDialog: TOpenDialog; OpenDialog: TOpenDialog;
Panel1: TPanel; Panel1: TPanel;
Panel2: TPanel; Panel2: TPanel;
SaveDialog: TSaveDialog; SaveDialog: TSaveDialog;
sWorkbookSource1: TsWorkbookSource;
WorksheetGrid: TsWorksheetGrid; WorksheetGrid: TsWorksheetGrid;
procedure BtnNewClick(Sender: TObject); procedure BtnNewClick(Sender: TObject);
procedure BtnOpenClick(Sender: TObject); procedure BtnOpenClick(Sender: TObject);
procedure BtnSaveClick(Sender: TObject); procedure BtnSaveClick(Sender: TObject);
procedure BtnEnterTextClick(Sender: TObject);
procedure SheetsComboSelect(Sender: TObject); procedure SheetsComboSelect(Sender: TObject);
private private
{ private declarations } { private declarations }
@@ -141,6 +146,11 @@ begin
end; end;
end; end;
procedure TForm1.BtnEnterTextClick(Sender: TObject);
begin
WorksheetGrid.Worksheet.WriteText(109, 27, EdNewCellText.Text);
end;
procedure TForm1.SheetsComboSelect(Sender: TObject); procedure TForm1.SheetsComboSelect(Sender: TObject);
begin begin
WorksheetGrid.SelectSheetByIndex(SheetsCombo.ItemIndex); WorksheetGrid.SelectSheetByIndex(SheetsCombo.ItemIndex);

View File

@@ -67,7 +67,6 @@
<ComponentName Value="Form1"/> <ComponentName Value="Form1"/>
<HasResources Value="True"/> <HasResources Value="True"/>
<ResourceBaseClass Value="Form"/> <ResourceBaseClass Value="Form"/>
<UnitName Value="mainfrm"/>
</Unit1> </Unit1>
<Unit2> <Unit2>
<Filename Value="..\..\fpshelpers.pas"/> <Filename Value="..\..\fpshelpers.pas"/>

View File

@@ -45,6 +45,31 @@ object Form1: TForm1
OnClick = BtnSaveClick OnClick = BtnSaveClick
TabOrder = 2 TabOrder = 2
end end
object Label1: TLabel
Left = 288
Height = 15
Top = 11
Width = 144
Caption = 'Enter value for cell [AB110]:'
ParentColor = False
end
object EdCellValue: TEdit
Left = 439
Height = 23
Top = 7
Width = 137
TabOrder = 3
Text = 'Test'
end
object BtnEnterText: TButton
Left = 584
Height = 25
Top = 6
Width = 75
Caption = 'Enter text'
OnClick = BtnEnterTextClick
TabOrder = 4
end
end end
object TabControl: TTabControl object TabControl: TTabControl
Left = 0 Left = 0

View File

@@ -17,10 +17,14 @@ type
BtnNew: TButton; BtnNew: TButton;
BtnLoad: TButton; BtnLoad: TButton;
BtnSave: TButton; BtnSave: TButton;
BtnEnterText: TButton;
ButtonPanel: TPanel; ButtonPanel: TPanel;
EdCellValue: TEdit;
Label1: TLabel;
OpenDialog: TOpenDialog; OpenDialog: TOpenDialog;
SaveDialog: TSaveDialog; SaveDialog: TSaveDialog;
TabControl: TTabControl; TabControl: TTabControl;
procedure BtnEnterTextClick(Sender: TObject);
procedure BtnLoadClick(Sender: TObject); procedure BtnLoadClick(Sender: TObject);
procedure BtnNewClick(Sender: TObject); procedure BtnNewClick(Sender: TObject);
procedure BtnSaveClick(Sender: TObject); procedure BtnSaveClick(Sender: TObject);
@@ -85,6 +89,12 @@ begin
end; end;
end; end;
procedure TForm1.BtnEnterTextClick(Sender: TObject);
begin
Grid.Worksheet.WriteText(109, 27, EdCellValue.Text);
Grid.Worksheet.SelectCell(109, 27);
end;
procedure TForm1.BtnNewClick(Sender: TObject); procedure TForm1.BtnNewClick(Sender: TObject);
begin begin
TabControl.Tabs.Clear; TabControl.Tabs.Clear;

View File

@@ -927,7 +927,7 @@ var
clrName: String; clrName: String;
i: Integer; i: Integer;
begin begin
if WorksheetGrid.Workbook <> nil then begin if (WorksheetGrid.Workbook <> nil) and (FPalette <> nil) then begin
Items.Clear; Items.Clear;
Items.AddObject('no fill', TObject(PtrInt(clNone))); Items.AddObject('no fill', TObject(PtrInt(clNone)));
for i:=0 to FPalette.Count-1 do begin for i:=0 to FPalette.Count-1 do begin

View File

@@ -45,9 +45,10 @@ type
private private
{ Private declarations } { Private declarations }
FWorkbookSource: TsWorkbookSource; FWorkbookSource: TsWorkbookSource;
FOwnedWorkbook: TsWorkbook; FInternalWorkbookSource: TsWorkbookSource;
FOwnsWorkbook: Boolean; // FOwnedWorkbook: TsWorkbook;
FOwnedWorksheet: TsWorksheet; // FOwnsWorkbook: Boolean;
// FOwnedWorksheet: TsWorksheet;
FHeaderCount: Integer; FHeaderCount: Integer;
FInitColCount: Integer; FInitColCount: Integer;
FInitRowCount: Integer; FInitRowCount: Integer;
@@ -103,6 +104,7 @@ type
function GetVertAlignment(ACol, ARow: Integer): TsVertAlignment; function GetVertAlignment(ACol, ARow: Integer): TsVertAlignment;
function GetVertAlignments(ARect: TGridRect): TsVertAlignment; function GetVertAlignments(ARect: TGridRect): TsVertAlignment;
function GetWorkbook: TsWorkbook; function GetWorkbook: TsWorkbook;
function GetWorkbookSource: TsWorkbookSource;
function GetWorksheet: TsWorksheet; function GetWorksheet: TsWorksheet;
function GetWordwrap(ACol, ARow: Integer): Boolean; function GetWordwrap(ACol, ARow: Integer): Boolean;
function GetWordwraps(ARect: TGridRect): Boolean; function GetWordwraps(ARect: TGridRect): Boolean;
@@ -180,7 +182,7 @@ type
} }
procedure KeyDown(var Key : Word; Shift : TShiftState); override; procedure KeyDown(var Key : Word; Shift : TShiftState); override;
procedure Loaded; override; procedure Loaded; override;
procedure LoadFromWorksheet(AWorksheet: TsWorksheet); // procedure LoadFromWorksheet(AWorksheet: TsWorksheet);
procedure MouseDown(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override; procedure MouseDown(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override; procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override; procedure MouseUp(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override;
@@ -846,6 +848,7 @@ end;
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
constructor TsCustomWorksheetGrid.Create(AOwner: TComponent); constructor TsCustomWorksheetGrid.Create(AOwner: TComponent);
begin begin
FInternalWorkbookSource := TsWorkbookSource.Create(self); //AOwner);
inherited Create(AOwner); inherited Create(AOwner);
AutoAdvance := aaDown; AutoAdvance := aaDown;
ExtendedSelect := true; ExtendedSelect := true;
@@ -856,7 +859,7 @@ begin
FHyperlinkTimer := TTimer.Create(self); FHyperlinkTimer := TTimer.Create(self);
FHyperlinkTimer.Interval := HYPERLINK_TIMER_INTERVAL; FHyperlinkTimer.Interval := HYPERLINK_TIMER_INTERVAL;
FHyperlinkTimer.OnTimer := @HyperlinkTimerElapsed; FHyperlinkTimer.OnTimer := @HyperlinkTimerElapsed;
FOwnsWorkbook := true; SetWorkbookSource(FInternalWorkbookSource);
{$IFNDEF FPS_NO_GRID_MULTISELECT} {$IFNDEF FPS_NO_GRID_MULTISELECT}
RangeSelectMode := rsmMulti; RangeSelectMode := rsmMulti;
{$ENDIF} {$ENDIF}
@@ -867,8 +870,11 @@ end;
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
destructor TsCustomWorksheetGrid.Destroy; destructor TsCustomWorksheetGrid.Destroy;
begin begin
if FWorkbookSource <> nil then FWorkbookSource.RemoveListener(self); SetWorkbookSource(nil);
if FOwnsWorkbook then FreeAndNil(FOwnedWorkbook); if FInternalWorkbookSource <> nil then
FInternalWorkbookSource.RemoveListener(self); // will be destroyed automatically
// FreeAndNil(FInternalWorkbookSource);
// end;
FreeAndNil(FCellFont); FreeAndNil(FCellFont);
inherited Destroy; inherited Destroy;
end; end;
@@ -1265,6 +1271,12 @@ end;
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
procedure TsCustomWorksheetGrid.CreateNewWorkbook; procedure TsCustomWorksheetGrid.CreateNewWorkbook;
begin begin
GetWorkbookSource.CreateNewWorkbook;
if FReadFormulas then
Workbook.Options := Workbook.Options + [boReadFormulas] else
Workbook.Options := Workbook.Options - [boReadFormulas];
SetAutoCalc(FAutoCalc);
{
if FOwnsWorkbook then if FOwnsWorkbook then
FreeAndNil(FOwnedWorkbook); FreeAndNil(FOwnedWorkbook);
@@ -1280,6 +1292,7 @@ begin
FOwnedWorkbook.Options := FOwnedWorkbook.Options - [boReadFormulas]; FOwnedWorkbook.Options := FOwnedWorkbook.Options - [boReadFormulas];
SetAutoCalc(FAutoCalc); SetAutoCalc(FAutoCalc);
end; end;
}
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
@@ -3396,7 +3409,7 @@ begin
// CreateNewWorkbook; // CreateNewWorkbook;
NewWorkbook(FInitColCount, FInitRowCount); NewWorkbook(FInitColCount, FInitRowCount);
end; end;
(*
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
Loads the worksheet into the grid and displays its contents. Loads the worksheet into the grid and displays its contents.
@@ -3407,6 +3420,7 @@ begin
if FWorkbookSource <> nil then if FWorkbookSource <> nil then
exit; exit;
GetWorkbookSource.LoadFro
FOwnedWorksheet := AWorksheet; FOwnedWorksheet := AWorksheet;
if FOwnedWorksheet <> nil then begin if FOwnedWorksheet <> nil then begin
inc(FLockSetup); inc(FLockSetup);
@@ -3427,7 +3441,7 @@ begin
end; end;
Setup; Setup;
end; end;
*)
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
Creates a new workbook and loads the given file into it. The file is assumed Creates a new workbook and loads the given file into it. The file is assumed
to have the given file format. Shows the sheet with the given sheet index. to have the given file format. Shows the sheet with the given sheet index.
@@ -3440,6 +3454,8 @@ end;
procedure TsCustomWorksheetGrid.LoadFromSpreadsheetFile(AFileName: string; procedure TsCustomWorksheetGrid.LoadFromSpreadsheetFile(AFileName: string;
AFormat: TsSpreadsheetFormat; AWorksheetIndex: Integer); AFormat: TsSpreadsheetFormat; AWorksheetIndex: Integer);
begin begin
GetWorkbookSource.LoadfromSpreadsheetFile(AFileName, AFormat, AWorksheetIndex);
{
if FOwnsWorkbook then if FOwnsWorkbook then
FreeAndNil(FOwnedWorkbook); FreeAndNil(FOwnedWorkbook);
@@ -3456,6 +3472,7 @@ begin
EndUpdate; EndUpdate;
end; end;
end; end;
}
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
@@ -3469,6 +3486,8 @@ end;
procedure TsCustomWorksheetGrid.LoadFromSpreadsheetFile(AFileName: string; procedure TsCustomWorksheetGrid.LoadFromSpreadsheetFile(AFileName: string;
AWorksheetIndex: Integer); AWorksheetIndex: Integer);
begin begin
GetWorkbookSource.LoadFromSpreadsheetFile(AFileName, AWorksheetIndex);
{
if FOwnsWorkbook then if FOwnsWorkbook then
FreeAndNil(FOwnedWorkbook); FreeAndNil(FOwnedWorkbook);
@@ -3485,6 +3504,7 @@ begin
EndUpdate; EndUpdate;
end; end;
end; end;
}
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
@@ -3711,6 +3731,8 @@ end;
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
procedure TsCustomWorksheetGrid.NewWorkbook(AColCount, ARowCount: Integer); procedure TsCustomWorksheetGrid.NewWorkbook(AColCount, ARowCount: Integer);
begin begin
GetWorkbookSource.CreateNewWorkbook;
{
if FOwnsWorkbook then if FOwnsWorkbook then
FreeAndNil(FOwnedWorkbook); FreeAndNil(FOwnedWorkbook);
@@ -3731,6 +3753,7 @@ begin
EndUpdate; EndUpdate;
end; end;
end; end;
}
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
@@ -3810,8 +3833,11 @@ end;
-------------------------------------------------------------------------------} -------------------------------------------------------------------------------}
procedure TsCustomWorksheetGrid.SelectSheetByIndex(AIndex: Integer); procedure TsCustomWorksheetGrid.SelectSheetByIndex(AIndex: Integer);
begin begin
GetWorkbookSource.SelectWorksheet(Workbook.GetWorksheetByIndex(AIndex));
{
if Workbook <> nil then if Workbook <> nil then
LoadFromWorksheet(Workbook.GetWorksheetByIndex(AIndex)); LoadFromWorksheet(Workbook.GetWorksheetByIndex(AIndex));
}
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
@@ -3882,17 +3908,21 @@ begin
if AValue = FWorkbookSource then if AValue = FWorkbookSource then
exit; exit;
if FOwnsWorkbook then
FreeAndNil(FOwnedWorkbook);
if FWorkbookSource <> nil then if FWorkbookSource <> nil then
FWorkbookSource.RemoveListener(self); FWorkbookSource.RemoveListener(self);
FWorkbookSource := AValue; FInternalWorkbookSource.RemoveListener(self);
if FWorkbookSource <> nil then
FWorkbookSource.AddListener(self);
FOwnsWorkbook := (FWorkbookSource = nil); if (AValue = FInternalWorkbookSource) or (AValue = nil) then
if not (csDestroying in ComponentState) then begin
FWorkbookSource := nil;
FInternalWorkbookSource.AddListener(self);
end else
begin
FWorkbookSource := AValue;
FWorkbookSource.AddListener(self);
end;
if not (csDestroying in ComponentState) and Assigned(Parent) then
ListenerNotification([lniWorksheet, lniSelection]); ListenerNotification([lniWorksheet, lniSelection]);
end; end;
@@ -4347,19 +4377,20 @@ begin
end; end;
function TsCustomWorksheetGrid.GetWorkbook: TsWorkbook; function TsCustomWorksheetGrid.GetWorkbook: TsWorkbook;
begin
Result := GetWorkbookSource.Workbook;
end;
function TsCustomWorksheetGrid.GetWorkbookSource: TsWorkbookSource;
begin begin
if FWorkbookSource <> nil then if FWorkbookSource <> nil then
Result := FWorkbookSource.Workbook Result := FWorkbookSource else
else Result := FInternalWorkbookSource;
Result := FOwnedWorkbook;
end; end;
function TsCustomWorksheetGrid.GetWorksheet: TsWorksheet; function TsCustomWorksheetGrid.GetWorksheet: TsWorksheet;
begin begin
if FWorkbookSource <> nil then Result := GetWorkbookSource.Worksheet;
Result := FWorkbooksource.Worksheet
else
Result := FOwnedWorksheet;
end; end;
function TsCustomWorksheetGrid.GetWordwrap(ACol, ARow: Integer): Boolean; function TsCustomWorksheetGrid.GetWordwrap(ACol, ARow: Integer): Boolean;