fpspreadsheet: Add example for zooming.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5229 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2016-09-28 09:08:15 +00:00
parent c9fcde924c
commit f87e5706fb
5 changed files with 318 additions and 0 deletions

View File

@ -0,0 +1,10 @@
This example demonstrates two ways of zooming in the WorksheetGrid.
In the first one (checkbox "Override file zoom factor" off), the zoom factor
read from the spreadsheet file is used for displaying the worksheet in the
grid.
In the second one (checkbox on), the file zoom factor is ignored.
The zoom factor can be changed by entering a zoom percentage in the edit box,
or by using the mouse wheel with CTRL and SHIFT keys pressed.

View File

@ -0,0 +1,67 @@
object MainForm: TMainForm
Left = 280
Height = 482
Top = 130
Width = 680
Caption = 'Zoom demo'
ClientHeight = 482
ClientWidth = 680
OnCreate = FormCreate
LCLVersion = '1.7'
object Grid: TsWorksheetGrid
Left = 5
Height = 429
Top = 8
Width = 667
FrozenCols = 0
FrozenRows = 0
ReadFormulas = False
TextOverflow = True
WorkbookSource = Grid.internal
Anchors = [akTop, akLeft, akRight, akBottom]
AutoAdvance = aaDown
ColCount = 27
DefaultColWidth = 64
DefaultRowHeight = 22
MouseWheelOption = mwGrid
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goRowSizing, goColSizing, goEditing, goThumbTracking, goSmoothScroll]
RowCount = 101
TabOrder = 0
OnMouseWheel = GridMouseWheel
end
object BtnOpen: TButton
Left = 5
Height = 25
Top = 447
Width = 75
Anchors = [akLeft, akBottom]
Caption = 'Open...'
OnClick = BtnOpenClick
TabOrder = 1
end
object edZoom: TSpinEdit
Left = 97
Height = 23
Top = 448
Width = 66
Alignment = taRightJustify
Anchors = [akLeft, akBottom]
MaxValue = 900
MinValue = 10
OnEditingDone = edZoomEditingDone
TabOrder = 2
Value = 100
end
object CbOverrideZoomFactor: TCheckBox
Left = 184
Height = 19
Top = 450
Width = 151
Caption = 'Override file zoom factor'
TabOrder = 3
end
object OpenDialog: TOpenDialog
left = 280
top = 128
end
end

View File

@ -0,0 +1,138 @@
unit zdMain;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
Spin, fpstypes, fpspreadsheet, fpspreadsheetgrid, Types;
type
{ TMainForm }
TMainForm = class(TForm)
BtnOpen: TButton;
CbOverrideZoomFactor: TCheckBox;
edZoom: TSpinEdit;
Grid: TsWorksheetGrid;
OpenDialog: TOpenDialog;
procedure BtnOpenClick(Sender: TObject);
procedure edZoomEditingDone(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure GridMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
private
FWorkbook: TsWorkbook;
procedure LoadFile(const AFileName: String);
procedure UpdateCaption;
public
end;
var
MainForm: TMainForm;
implementation
{$R *.lfm}
uses
fpsRegFileFormats;
{ TMainForm }
procedure TMainForm.BtnOpenClick(Sender: TObject);
begin
if FWorkbook <> nil then
OpenDialog.InitialDir := ExtractFileDir(FWorkbook.FileName);
if OpenDialog.Execute then
LoadFile(OpenDialog.FileName);
end;
{ Set the zoom factor to the value in the edit control. Is called after
pressing ENTER. }
procedure TMainForm.edZoomEditingDone(Sender: TObject);
begin
Grid.ZoomFactor := edZoom.Value / 100;
end;
procedure TMainForm.FormCreate(Sender: TObject);
var
priorityFormats: Array[0..7] of TsSpreadFormatID;
begin
priorityFormats[0] := ord(sfOOXML);
priorityFormats[1] := ord(sfExcel8);
priorityFormats[2] := ord(sfExcel5);
priorityFormats[3] := ord(sfExcel2);
priorityFormats[4] := ord(sfExcelXML);
priorityFormats[5] := ord(sfOpenDocument);
priorityFormats[6] := ord(sfCSV);
priorityFormats[7] := ord(sfHTML);
OpenDialog.Filter := GetFileFormatFilter('|', ';', faRead, priorityFormats, true, true);
end;
{ Mouse wheel event handler for setting the zoom factor using the mouse wheel
(together with pressing the SHFT + CTRL keys) }
procedure TMainForm.GridMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
if ([ssCtrl, ssShift] * Shift = [ssCtrl, ssShift]) then begin
if WheelDelta > 0 then
Grid.ZoomFactor := Grid.ZoomFactor * 1.05
else
Grid.ZoomFactor := Grid.ZoomFactor / 1.05;
edZoom.Value := round(Grid.ZoomFactor * 100);
Handled := true;
end;
end;
procedure TMainForm.LoadFile(const AFileName: String);
var
crs: TCursor;
book: TsWorkbook;
begin
crs := Screen.Cursor;
try
if CbOverrideZoomFactor.Checked then begin
book := TsWorkbook.Create;
try
Screen.Cursor := crHourglass;
// Read the file
book.ReadFromFile(AFilename);
// If you want to override the zoom factor of the file set it before
// assigning the worksheet to the grid.
book.GetFirstWorksheet.ZoomFactor := edZoom.Value / 100;
// Load the worksheet into the grid.
Grid.LoadFromWorkbook(book, 0);
except
on E:Exception do begin
MessageDlg(Format('File "%s" cannot be loaded.'#13 + E.Message, [AFilename]),
mtError, [mbOK], 0);
book.Free;
end;
end;
end else begin
Grid.LoadSheetFromSpreadsheetFile(AFilename, 0);
edZoom.Value := Grid.ZoomFactor * 100;
end;
UpdateCaption;
finally
Screen.Cursor := crs;
end;
end;
procedure TMainForm.UpdateCaption;
begin
if FWorkbook = nil then
Caption := 'Zoom demo'
else
Caption := 'Zoom demo - "' + FWorkbook.Filename + '"';
end;
end.

View File

@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<PathDelim Value="\"/>
<General>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="zoomdemo"/>
<ResourceType Value="res"/>
<UseXPManifest Value="True"/>
<Icon Value="0"/>
</General>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
</RunParams>
<RequiredPackages Count="2">
<Item1>
<PackageName Value="laz_fpspreadsheet_visual"/>
</Item1>
<Item2>
<PackageName Value="LCL"/>
</Item2>
</RequiredPackages>
<Units Count="2">
<Unit0>
<Filename Value="zoomdemo.lpr"/>
<IsPartOfProject Value="True"/>
</Unit0>
<Unit1>
<Filename Value="zdmain.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="MainForm"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="zdMain"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="zoomdemo"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Debugging>
<UseExternalDbgSyms Value="True"/>
</Debugging>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -0,0 +1,21 @@
program zoomdemo;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, zdMain
{ you can add units after this };
{$R *.res}
begin
RequireDerivedFormResource := True;
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.