Ignore:
Timestamp:
Mar 8, 2011, 12:58:28 PM (14 years ago)
Author:
george
Message:
  • Added: Store and restore basic form properties with use of layouts.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Docking/CoolDocking/UCoolDockLayout.pas

    r178 r179  
    1919    Visible: Boolean;
    2020    Rect: TRectangle;
    21     FormState: TFormState;
     21    RestoredRect: TRectangle;
     22    WindowState: TWindowState;
    2223    UndockSize: TPoint;
    2324    DockStyle: TDockStyle;
    2425    procedure SaveToNode(Node: TDOMNode);
    2526    procedure LoadFromNode(Node: TDOMNode);
     27    constructor Create;
     28    destructor Destroy; override;
    2629  end;
    2730
     
    3538    constructor Create;
    3639    destructor Destroy; override;
     40    procedure Store;
     41    procedure Restore;
    3742  end;
    3843
     
    4651    procedure LoadFromFile(FileName: string);
    4752    procedure SaveToFile(FileName: string);
     53    procedure PopulateStringList(List: TStrings);
    4854    constructor Create(AOwner: TComponent); override;
    4955    destructor Destroy; override;
     
    7884  Doc: TXMLDocument;
    7985  Child: TDOMNode;
    80   NewItem: TCoolDockLayoutItem;
     86  NewItem: TCoolDockLayout;
    8187  NewNode: TDOMNode;
    8288begin
     
    8894      if Assigned(NewNode) then
    8995      with NewNode do begin
    90         Child := Doc.DocumentElement.FirstChild;
     96        Child := FirstChild;
    9197        while Assigned(Child) do begin
    92           NewItem := TCoolDockLayoutItem.Create;
     98          NewItem := TCoolDockLayout.Create;
    9399          NewItem.LoadFromNode(Child);
    94100          Items.Add(NewItem);
     
    117123      with RootNode do begin
    118124        NewNode := OwnerDocument.CreateElement('Items');
     125        with NewNode do
    119126        for I := 0 to Items.Count - 1 do begin
    120127          NewNode2 := OwnerDocument.CreateElement('Layout');
     
    150157    if FileExistsUTF8(FileName) then Stream := TFileStream.Create(FileName, fmOpenReadWrite)
    151158      else Stream := TFileStream.Create(FileName, fmCreate);
     159    Stream.Size := 0;
    152160    SaveToStream(Stream);
    153161  finally
     
    156164end;
    157165
     166procedure TCoolDockLayoutList.PopulateStringList(List: TStrings);
     167var
     168  I: Integer;
     169begin
     170  List.Clear;
     171  for I := 0 to Items.Count - 1 do
     172    List.AddObject(TCoolDockLayout(Items[I]).Name, TCoolDockLayout(Items[I]));
     173end;
     174
    158175{ TCoolDockLayoutItem }
    159176
     
    172189    NewNode.TextContent := UTF8Decode(Caption);
    173190    AppendChild(NewNode);
    174     NewNode := OwnerDocument.CreateElement('FormState');
    175     NewNode.TextContent := IntToStr(Integer(FormState));
     191    NewNode := OwnerDocument.CreateElement('WindowState');
     192    NewNode.TextContent := IntToStr(Integer(WindowState));
    176193    AppendChild(NewNode);
    177194    NewNode := OwnerDocument.CreateElement('UndockWidth');
     
    199216    NewNode.TextContent := IntToStr(Integer(DockStyle));
    200217    AppendChild(NewNode);
     218    NewNode := OwnerDocument.CreateElement('RestoredWidth');
     219    NewNode.TextContent := IntToStr(RestoredRect.Width);
     220    AppendChild(NewNode);
     221    NewNode := OwnerDocument.CreateElement('RestoredHeight');
     222    NewNode.TextContent := IntToStr(RestoredRect.Height);
     223    AppendChild(NewNode);
     224    NewNode := OwnerDocument.CreateElement('RestoredTop');
     225    NewNode.TextContent := IntToStr(RestoredRect.Top);
     226    AppendChild(NewNode);
     227    NewNode := OwnerDocument.CreateElement('RestoredLeft');
     228    NewNode.TextContent := IntToStr(RestoredRect.Left);
     229    AppendChild(NewNode);
    201230  end;
    202231end;
     
    216245    if Assigned(NewNode) then
    217246      Caption := UTF8Encode(NewNode.TextContent);
    218     NewNode := FindNode('FormState');
    219     if Assigned(NewNode) then
    220       FormState := TFormState(StrToInt(NewNode.TextContent));
     247    NewNode := FindNode('WindowState');
     248    if Assigned(NewNode) then
     249      WindowState := TWindowState(StrToInt(NewNode.TextContent));
    221250    NewNode := FindNode('UndockWidth');
    222251    if Assigned(NewNode) then
     
    243272    if Assigned(NewNode) then
    244273      DockStyle := TDockStyle(StrToInt(NewNode.TextContent));
    245   end;
     274    NewNode := FindNode('RestoredTop');
     275    if Assigned(NewNode) then
     276      RestoredRect.Top := StrToInt(NewNode.TextContent);
     277    NewNode := FindNode('RestoredLeft');
     278    if Assigned(NewNode) then
     279      RestoredRect.Left := StrToInt(NewNode.TextContent);
     280    NewNode := FindNode('RestoredWidth');
     281    if Assigned(NewNode) then
     282      RestoredRect.Width := StrToInt(NewNode.TextContent);
     283    NewNode := FindNode('RestoredHeight');
     284    if Assigned(NewNode) then
     285      RestoredRect.Height := StrToInt(NewNode.TextContent);
     286  end;
     287end;
     288
     289constructor TCoolDockLayoutItem.Create;
     290begin
     291  Rect := TRectangle.Create;
     292  RestoredRect := TRectangle.Create;
     293end;
     294
     295destructor TCoolDockLayoutItem.Destroy;
     296begin
     297  Rect.Free;
     298  RestoredRect.Free;
     299  inherited Destroy;
    246300end;
    247301
     
    259313    AppendChild(NewNode);
    260314    NewNode := OwnerDocument.CreateElement('Items');
     315    with NewNode do
    261316    for I := 0 to Items.Count - 1 do begin
    262317      NewNode2 := OwnerDocument.CreateElement('Form');
     
    303358end;
    304359
     360procedure TCoolDockLayout.Store;
     361var
     362  I: Integer;
     363  Form: TForm;
     364  NewItem: TCoolDockLayoutItem;
     365begin
     366  Items.Clear;
     367  for I := 0 to Application.ComponentCount - 1 do
     368  if (Application.Components[I] is TForm) then begin
     369    Form := (Application.Components[I] as TForm);
     370    NewItem := TCoolDockLayoutItem.Create;
     371    NewItem.Name := Form.Name;
     372    NewItem.Caption := Form.Caption;
     373    NewItem.UndockSize.X := Form.UndockWidth;
     374    NewItem.UndockSize.Y := Form.UndockHeight;
     375    NewItem.Visible := Form.Visible;
     376    NewItem.Rect.Left := Form.Left;
     377    NewItem.Rect.Top := Form.Top;
     378    NewItem.Rect.Width := Form.Width;
     379    NewItem.Rect.Height := Form.Height;
     380    NewItem.RestoredRect.Left := Form.RestoredLeft;
     381    NewItem.RestoredRect.Top := Form.RestoredTop;
     382    NewItem.RestoredRect.Width := Form.RestoredWidth;
     383    NewItem.RestoredRect.Height := Form.RestoredHeight;
     384    NewItem.WindowState := Form.WindowState;
     385    Items.Add(NewItem);
     386  end;
     387end;
     388
     389procedure TCoolDockLayout.Restore;
     390var
     391  Form: TForm;
     392  I: Integer;
     393begin
     394  for I := 0 to Items.Count - 1 do
     395  with TCoolDockLayoutItem(Items[I]) do begin
     396    Form := TForm(Application.FindComponent(Name));
     397    if WindowState = wsMaximized then begin
     398      Form.SetRestoredBounds(RestoredRect.Left, RestoredRect.Top,
     399        RestoredRect.Width, RestoredRect.Height);
     400      Form.WindowState := WindowState;
     401    end else begin
     402      Form.WindowState := WindowState;
     403      Form.SetRestoredBounds(RestoredRect.Left, RestoredRect.Top,
     404        RestoredRect.Width, RestoredRect.Height);
     405    end;
     406    Form.Caption := Caption;
     407    Form.SetBounds(Rect.Left, Rect.Top, Rect.Width, Rect.Height);
     408    Form.UndockWidth := UndockSize.X;
     409    Form.UndockHeight := UndockSize.Y;
     410    Form.Visible := Visible;
     411  end;
     412end;
     413
    305414end.
    306415
Note: See TracChangeset for help on using the changeset viewer.