page-control: button state auto-updated, + index prop for TCEPage

+ added a project that can be used to devel TCEPageControl without recompiling CE in its whole
This commit is contained in:
Basile Burg 2015-12-08 12:12:27 +01:00
parent 96f0eed32b
commit 347094cdcf
6 changed files with 217 additions and 8 deletions

1
lazproj/test/pagecontrol/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
pagecontrol

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<General>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="pagecontrol"/>
<ResourceType Value="res"/>
<UseXPManifest Value="True"/>
</General>
<i18n>
<EnableI18N LFM="False"/>
</i18n>
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
</RunParams>
<RequiredPackages Count="1">
<Item1>
<PackageName Value="LCL"/>
</Item1>
</RequiredPackages>
<Units Count="2">
<Unit0>
<Filename Value="pagecontrol.lpr"/>
<IsPartOfProject Value="True"/>
</Unit0>
<Unit1>
<Filename Value="../../pagecontroltester.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Form1"/>
<ResourceBaseClass Value="Form"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="pagecontrol"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="../../../src"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<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 pagecontrol;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, pagecontroltester
{ you can add units after this };
{$R *.res}
begin
RequireDerivedFormResource := True;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

View File

@ -0,0 +1,8 @@
object Form1: TForm1
Left = 486
Height = 317
Top = 193
Width = 819
Caption = 'Form1'
LCLVersion = '1.4.4.0'
end

View File

@ -0,0 +1,72 @@
unit pagecontroltester;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
ce_controls, ce_sharedres;
type
TForm1 = class(TForm)
private
fPageControl: TCEPageControl;
procedure pageControlChanged(sender: TObject);
procedure pagePaint(sender: TObject);
public
constructor create(aOwner: TComponent); override;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
constructor TForm1.create(aOwner: TComponent);
begin
inherited;
fPageControl := TCEPageControl.Create(self);
fPageControl.Parent := self;
fPageControl.Align := alClient;
fPageControl.onChanged:=@pageControlChanged;
AssignPng(fPageControl.closeButton.Glyph, 'document_delete');
AssignPng(fPageControl.addButton.Glyph, 'document_add');
AssignPng(fPageControl.moveLeftButton.Glyph, 'document_back');
AssignPng(fPageControl.moveRightButton.Glyph, 'document_next');
end;
procedure TForm1.pageControlChanged(sender: TObject);
var
page: TCEPage;
begin
page := fPageControl.currentPage;
if assigned(page) then
begin
if page.OnPaint = nil then
begin
page.OnPaint := @pagePaint;
page.Repaint;
end;
if page.Caption = '' then
page.Caption := format('<created index %d>', [page.index]);
end;
end;
procedure TForm1.pagePaint(sender: TObject);
var
page: TCEPage = nil;
begin
page := TCEPage(sender);
if assigned(page) then
begin
page.Canvas.Clear;
page.Canvas.Font.Size := 22;
page.Canvas.TextOut(10, 10, format('current index %d', [page.index]));
end;
end;
end.

View File

@ -18,8 +18,12 @@ type
// Used instead of a TTabSheet since only the caption is interesting
TCEPage = class(TCustomControl)
private
function getIndex: integer;
protected
procedure RealSetText(const Value: TCaption); override;
procedure realSetText(const Value: TCaption); override;
public
property index: integer read getIndex;
end;
(**
@ -61,6 +65,7 @@ type
function getPage(index: integer): TCEPage;
procedure changedNotify;
procedure updateButtonsState;
public
constructor Create(aowner: TComponent); override;
@ -89,13 +94,25 @@ type
implementation
function TCEPage.getIndex: integer;
var
ctrl: TCEPageControl;
i: integer;
begin
ctrl := TCEPageControl(owner);
for i := 0 to ctrl.pageCount-1 do
if ctrl.pages[i] = self then
exit(i);
exit(-1);
end;
procedure TCEPage.RealSetText(const Value: TCaption);
var
i: integer;
ctrl: TCEPageControl;
begin
inherited;
ctrl := TCEPageControl(Owner);
ctrl := TCEPageControl(owner);
i := ctrl.getPageIndex(self);
if i <> -1 then ctrl.fTabs.Tabs.Strings[i] := caption;
end;
@ -164,6 +181,7 @@ begin
fPageIndex := -1;
fButtons:= CEPageControlDefaultButtons;
updateButtonsState;
end;
destructor TCEPageControl.Destroy;
@ -176,6 +194,7 @@ end;
procedure TCEPageControl.changedNotify;
begin
updateButtonsState;
if assigned(fOnChanged) then
fOnChanged(self);
end;
@ -262,7 +281,9 @@ begin
if fPageIndex >= fPages.Count then
fPageIndex -= 1;
if fPages.Count = 0 then exit;
updateButtonsState;
if fPages.Count = 0 then
exit;
setPageIndex(fPageIndex);
end;
@ -315,7 +336,6 @@ begin
setPageIndex(fPageIndex-1);
end;
procedure TCEPageControl.btnCloseClick(sender: TObject);
begin
deletePage(fPageIndex);
@ -338,16 +358,25 @@ end;
procedure TCEPageControl.setButtons(value: TCEPageControlButtons);
begin
fButtons:= value;
fHeader.DisableAlign;
if fButtons = value then
exit;
fButtons := value;
updateButtonsState;
fHeader.ReAlign;
end;
procedure TCEPageControl.updateButtonsState;
begin
fHeader.DisableAlign;
fCloseBtn.Visible:= pbClose in fButtons;
fMoveLeftBtn.Visible:= pbMoveLeft in fButtons;
fCloseBtn.Visible:= pbMoveRight in fButtons;
fAddBtn.Visible:= pbAdd in fButtons;
fHeader.EnableAlign;
fHeader.ReAlign;
fCloseBtn.Enabled := fPageIndex <> -1;
fMoveLeftBtn.Enabled := fPageIndex > 0;
fMoveRightBtn.Enabled := fPageIndex < fPages.Count-1;
end;
end.