1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
{%MainUnit ../extctrls.pp}
{******************************************************************************
TNotebook
******************************************************************************
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
function CreateUniquePageName(BaseName: string; OwnerComp: TComponent): string;
// Inspired by TCustomFormEditor.CreateUniqueComponentName
var
i, j: integer;
begin
Result:=BaseName;
if (OwnerComp=nil) or (Result='') then exit;
i:=0;
while true do begin
j:=OwnerComp.ComponentCount-1;
while (j>=0) and (CompareText(Result,OwnerComp.Components[j].Name)<>0) do
dec(j);
if j<0 then exit;
inc(i);
if BaseName[length(BaseName)] in ['0'..'9'] then
BaseName:=BaseName+'_';
Result:=BaseName+IntToStr(i);
end;
end;
{******************************************************************************
TUNBPages
******************************************************************************}
constructor TUNBPages.Create(theNotebook: TNotebook);
begin
inherited Create;
FPageList := TObjectList.Create(False);
FNotebook := theNotebook;
end;
destructor TUNBPages.Destroy;
begin
FPageList.Free;
inherited Destroy;
end;
function TUNBPages.GetNotebookOwner: TComponent;
begin
Result := FNotebook.Owner;
if Result = nil then
Result := FNotebook;
end;
function TUNBPages.Get(Index: Integer): String;
begin
if (Index < 0) or (Index >= FPageList.Count) then
RaiseGDBException('TUNBPages.Get Index out of bounds');
Result := TPage(fPageList[Index]).Name;
end;
function TUNBPages.GetCount: Integer;
begin
Result := FPageList.Count;
end;
function TUNBPages.GetObject(Index: Integer): TObject;
begin
if (Index < 0) or (Index >= FPageList.Count) then
RaiseGDBException('TUNBPages.GetObject Index out of bounds');
Result := TPage(FPageList[Index]);
end;
procedure TUNBPages.Put(Index: Integer; const S: String);
begin
if (Index < 0) or (Index >= FPageList.Count) then
RaiseGDBException('TUNBPages.Put Index out of bounds');
TPage(FPageList[Index]).Name := S;
end;
function TUNBPages.Add(const S: string): Integer;
begin
Result := AddObject(S, TPage.Create(GetNotebookOwner));
end;
function TUNBPages.AddObject(const S: string; AObject: TObject): Integer;
var
NewPage: TPage;
begin
Result := FPageList.Add(AObject);
NewPage := TPage(AObject);
if IsValidIdent(S) then
NewPage.Name := CreateUniquePageName(S, FNotebook.Owner);
NewPage.Caption := S;
NewPage.Parent := FNotebook;
NewPage.Align := alClient;
NewPage.Visible := False;
NewPage.ControlStyle := NewPage.ControlStyle + [csNoDesignVisible];
if FNotebook.PageIndex = -1 then
FNotebook.SetPageIndex(Result);
end;
procedure TUNBPages.Clear;
begin
while FPageList.Count > 0 do
Delete(FPageList.Count - 1);
end;
procedure TUNBPages.Delete(Index: Integer);
var
APage: TPage;
NewPageIndex: Integer;
begin
// Make sure Index is in the range of valid pages to delete
if (Index < 0) or (Index >= FPageList.Count) then Exit;
APage := TPage(FPageList[Index]);
FPageList.Delete(Index);
if APage.Parent = FNotebook then begin
Application.ReleaseComponent(APage);
Application.ProcessMessages;
end;
NewPageIndex := FNotebook.FPageIndex;
if ((Index < NewPageIndex) and (NewPageIndex > 0)) or (NewPageIndex = FNotebook.PageCount) then
Dec(NewPageIndex);
FNotebook.FPageIndex := -1;
FNotebook.SetPageIndex(NewPageIndex);
end;
function TUNBPages.IndexOfObject(AObject: TObject): Integer;
var
Index: Integer;
begin
Result := -1;
for Index := 0 to FPageList.Count - 1 do
if TPage(FPageList[Index]) = AObject then Exit(Index);
end;
procedure TUNBPages.Insert(Index: Integer; const S: string);
var
NewPage: TPage;
begin
if (Index < 0) or (Index >= FPageList.Count) then
RaiseGDBException('TUNBPages.Insert Index out of bounds');
NewPage := TPage.Create(GetNotebookOwner);
FPageList.Insert(Index, NewPage);
if IsValidIdent(S) then
NewPage.Name := S;
NewPage.Caption := S;
NewPage.Parent := FNotebook;
NewPage.Align := alClient;
NewPage.Visible := False;
NewPage.ControlStyle := NewPage.ControlStyle + [csNoDesignVisible];
if FNotebook.PageIndex >= Index then
FNotebook.PageIndex := FNotebook.PageIndex + 1;
end;
procedure TUNBPages.Move(CurIndex, NewIndex: Integer);
var
CurPage: TPage;
begin
// Get active page
CurPage := FNotebook.Page[FNotebook.FPageIndex];
FPageList.Move(CurIndex,NewIndex);
// Restore active page
FNotebook.SetPageIndex(IndexOfObject(CurPage));
end;
{******************************************************************************
TNotebook
******************************************************************************}
function TNotebook.GetActivePage: String;
begin
Result := Page[GetPageIndex].Name;
end;
function TNotebook.GetActivePageComponent: TPage;
begin
Result := Page[GetPageIndex];
end;
function TNotebook.GetPage(AIndex: Integer): TPage;
begin
if (AIndex < 0) or (AIndex >= FPages.Count) then
RaiseGDBException(Format('TNotebook.GetCustomPage Index out of bounds. AIndex=%d', [AIndex]));
Result := TPage(FPages.Objects[AIndex]);
end;
function TNotebook.GetPageCount: integer;
begin
Result := FPages.Count;
end;
function TNotebook.GetPageIndex: Integer;
begin
Result := FPageIndex;
end;
function TNotebook.IndexOf(APage: TPage): integer;
begin
Result := FPages.IndexOfObject(APage);
end;
procedure TNotebook.SetPageIndex(AValue: Integer);
var
pg: TPage;
begin
if (AValue < -1) or (AValue >= Pages.Count) then Exit;
if FPageIndex = AValue then Exit;
// Hide the previously shown page
if (FPageIndex >= 0) and (FPageIndex < Pages.Count) then
begin
pg := Page[FPageIndex];
pg.ControlStyle := pg.ControlStyle + [csNoDesignVisible];
pg.Visible := False;
end;
// Update the property
FPageIndex := AValue;
if (FPageIndex = -1) then
Exit;
// And show the new one
pg := Page[FPageIndex];
if Assigned(pg.FOnBeforeShow) then
pg.FOnBeforeShow(Self, pg, FPageIndex); // OnBeforeShow event
pg.Visible := True;
pg.ControlStyle := pg.ControlStyle - [csNoDesignVisible];
pg.Align := alClient;
end;
procedure TNotebook.SetPages(Items: TStrings);
var
Unchanged, Index, DummyIndex, ItemsCount: Integer;
DummyPageList: TObjectList;
APage: TPage;
procedure DummyDelete(aName: String);
var
i, aIndex: Integer;
DelPage: TPage;
begin
aIndex := -1;
for i := 0 to DummyPageList.Count - 1 do
if TPage(DummyPageList[i]).Name = aName then begin
aIndex := i;
Break;
end;
if aIndex = -1 then Exit;
DelPage := TPage(DummyPageList[aIndex]);
if Items.IndexOfObject(DelPage) >= 0 then Exit;
DummyPageList.Delete(aIndex);
DelPage.Parent := nil;
Application.ReleaseComponent(DelPage);
Application.ProcessMessages;
end;
begin
if csDesigning in ComponentState then Exit;
Unchanged := 0;
while (Unchanged < Items.Count) and (Unchanged < FPages.Count)
and (Items.Objects[Unchanged] = FPages.Objects[Unchanged])
and (Items[Unchanged] = TPage(FPages.Objects[Unchanged]).Name) do
Inc(Unchanged);
if (Unchanged = FPages.Count) and (Unchanged = Items.Count) then Exit;
DummyPageList := TObjectList.Create(False);
try
// move all unused/changed pages to dummy
for Index := FPages.Count - 1 downto Unchanged do
begin
APage := TPage(FPages.Objects[Index]);
APage.SetParent(nil);
DummyPageList.Add(APage);
end;
// add new or changed pages to notebook
Index := 0;
while Index < Items.Count do begin
// if Items comes from a other notebook, Items.Count is changed by APage.SetParent
ItemsCount := Items.Count;
if Assigned(Items.Objects[Index]) then begin
DummyDelete(Items[Index]);
APage := TPage(Items.Objects[Index]);
if IsValidIdent(Items[Index]) then
APage.Name := Items[Index];
APage.Caption := Items[Index];
APage.SetParent(Self);
DummyIndex := DummyPageList.IndexOf(APage);
if DummyIndex >= 0 then
DummyPageList.Delete(DummyIndex);
end else begin
DummyDelete(Items[Index]);
FPages.Add(Items[Index]);
end;
Inc(Index, 1 - (ItemsCount - Items.Count));
end;
// delete all unused old pages
for Index := DummyPageList.Count - 1 downto 0 do
begin
APage := TPage(DummyPageList[Index]);
DummyPageList.Delete(Index);
APage.Parent := nil;
Application.ReleaseComponent(APage);
Application.ProcessMessages;
end;
finally
DummyPageList.Free;
end;
end;
constructor TNotebook.Create(TheOwner: TComponent);
var
lSize: TSize;
begin
inherited Create(TheOwner);
FPageIndex := -1;
FPages := TUNBPages.Create(Self);
ControlStyle := []; // do not add csAcceptsControls
// Initial size
lSize := GetControlClassDefaultSize();
SetInitialBounds(0, 0, lSize.CX, lSize.CY);
end;
destructor TNotebook.Destroy;
begin
FreeAndNil(FPages);
inherited Destroy;
end;
procedure TNotebook.ShowControl(AControl: TControl);
var
i: Integer;
begin
if AControl = ActivePageComponent then exit;
i := FPages.IndexOfObject(aControl);
if i >= 0 then
PageIndex := i;
inherited ShowControl(AControl);
end;
|