Ignore:
Timestamp:
Mar 22, 2018, 8:31:19 PM (6 years ago)
Author:
chronos
Message:
  • Modified: Update Common and CollTranslator packages to fix build under Lazarus 1.8.
  • Fixed: Some memory leaks.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/UPersistentForm.pas

    r10 r15  
    33{$mode delphi}
    44
    5 // Date: 2010-06-01
     5// Date: 2015-04-18
    66
    77interface
    88
    99uses
    10   Classes, SysUtils, Forms, URegistry, LCLIntf, Registry;
     10  Classes, SysUtils, Forms, URegistry, LCLIntf, Registry, Controls, ComCtrls;
    1111
    1212type
     
    1919    FMinVisiblePart: Integer;
    2020    FRegistryContext: TRegistryContext;
     21    procedure LoadControl(Control: TControl);
     22    procedure SaveControl(Control: TControl);
    2123  public
     24    FormNormalSize: TRect;
     25    FormRestoredSize: TRect;
     26    FormWindowState: TWindowState;
     27    Form: TForm;
     28    procedure LoadFromRegistry(RegistryContext: TRegistryContext);
     29    procedure SaveToRegistry(RegistryContext: TRegistryContext);
    2230    function CheckEntireVisible(Rect: TRect): TRect;
    2331    function CheckPartVisible(Rect: TRect; Part: Integer): TRect;
     
    4452{ TPersistentForm }
    4553
     54procedure TPersistentForm.LoadControl(Control: TControl);
     55var
     56  I: Integer;
     57  WinControl: TWinControl;
     58  Count: Integer;
     59begin
     60  if Control is TListView then begin
     61    with Form, TRegistryEx.Create do
     62    try
     63      RootKey := RegistryContext.RootKey;
     64      OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True);
     65      for I := 0 to TListView(Control).Columns.Count - 1 do begin
     66        if ValueExists('ColWidth' + IntToStr(I)) then
     67          TListView(Control).Columns[I].Width := ReadInteger('ColWidth' + IntToStr(I));
     68      end;
     69    finally
     70      Free;
     71    end;
     72  end;
     73
     74  if Control is TWinControl then begin
     75    WinControl := TWinControl(Control);
     76    if WinControl.ControlCount > 0 then begin
     77      for I := 0 to WinControl.ControlCount - 1 do begin
     78        if WinControl.Controls[I] is TControl then begin
     79          LoadControl(WinControl.Controls[I]);
     80        end;
     81      end;
     82    end;
     83  end;
     84end;
     85
     86procedure TPersistentForm.SaveControl(Control: TControl);
     87var
     88  I: Integer;
     89  WinControl: TWinControl;
     90begin
     91  if Control is TListView then begin
     92    with Form, TRegistryEx.Create do
     93    try
     94      RootKey := RegistryContext.RootKey;
     95      OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True);
     96      for I := 0 to TListView(Control).Columns.Count - 1 do begin
     97        WriteInteger('ColWidth' + IntToStr(I), TListView(Control).Columns[I].Width);
     98      end;
     99    finally
     100      Free;
     101    end;
     102  end;
     103
     104  if Control is TWinControl then begin
     105      WinControl := TWinControl(Control);
     106      if WinControl.ControlCount > 0 then begin
     107        for I := 0 to WinControl.ControlCount - 1 do begin
     108          if WinControl.Controls[I] is TControl then begin
     109            SaveControl(WinControl.Controls[I]);
     110          end;
     111        end;
     112      end;
     113    end;
     114end;
     115
     116procedure TPersistentForm.LoadFromRegistry(RegistryContext: TRegistryContext);
     117begin
     118  with TRegistryEx.Create do
     119  try
     120    RootKey := RegistryContext.RootKey;
     121    OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
     122    // Normal size
     123    FormNormalSize.Left := ReadIntegerWithDefault('NormalLeft', FormNormalSize.Left);
     124    FormNormalSize.Top := ReadIntegerWithDefault('NormalTop', FormNormalSize.Top);
     125    FormNormalSize.Right := ReadIntegerWithDefault('NormalWidth', FormNormalSize.Right - FormNormalSize.Left)
     126      + FormNormalSize.Left;
     127    FormNormalSize.Bottom := ReadIntegerWithDefault('NormalHeight', FormNormalSize.Bottom - FormNormalSize.Top)
     128      + FormNormalSize.Top;
     129    // Restored size
     130    FormRestoredSize.Left := ReadIntegerWithDefault('RestoredLeft', FormRestoredSize.Left);
     131    FormRestoredSize.Top := ReadIntegerWithDefault('RestoredTop', FormRestoredSize.Top);
     132    FormRestoredSize.Right := ReadIntegerWithDefault('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left)
     133      + FormRestoredSize.Left;
     134    FormRestoredSize.Bottom := ReadIntegerWithDefault('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top)
     135      + FormRestoredSize.Top;
     136    // Other state
     137    FormWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(wsNormal)));
     138  finally
     139    Free;
     140  end;
     141end;
     142
     143procedure TPersistentForm.SaveToRegistry(RegistryContext: TRegistryContext);
     144begin
     145  with Form, TRegistryEx.Create do
     146  try
     147    RootKey := RegistryContext.RootKey;
     148    OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
     149    // Normal state
     150    WriteInteger('NormalWidth', FormNormalSize.Right - FormNormalSize.Left);
     151    WriteInteger('NormalHeight', FormNormalSize.Bottom - FormNormalSize.Top);
     152    WriteInteger('NormalTop', FormNormalSize.Top);
     153    WriteInteger('NormalLeft', FormNormalSize.Left);
     154    // Restored state
     155    WriteInteger('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left);
     156    WriteInteger('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top);
     157    WriteInteger('RestoredTop', FormRestoredSize.Top);
     158    WriteInteger('RestoredLeft', FormRestoredSize.Left);
     159    // Other state
     160    WriteInteger('WindowState', Integer(FormWindowState));
     161  finally
     162    Free;
     163  end;
     164end;
     165
    46166function TPersistentForm.CheckEntireVisible(Rect: TRect): TRect;
    47167var
     
    98218procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False);
    99219var
    100   Normal: TRect;
    101   Restored: TRect;
    102220  LoadDefaults: Boolean;
    103221begin
    104   with TRegistryEx.Create do
    105     try
    106       RootKey := RegistryContext.RootKey;
    107       OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
    108 
    109       //RestoredWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(Form.WindowState)));
    110       //if RestoredWindowState = wsMinimized then
    111       //  RestoredWindowState := wsNormal;
    112       //Form.WindowState := RestoredWindowState;
    113       LoadDefaults := not ValueExists('NormalLeft');
    114       Normal := Bounds(ReadIntegerWithDefault('NormalLeft', (Screen.Width - Form.Width) div 2),
    115         ReadIntegerWithDefault('NormalTop', (Screen.Height - Form.Height) div 2),
    116         ReadIntegerWithDefault('NormalWidth', Form.Width),
    117         ReadIntegerWithDefault('NormalHeight', Form.Height));
    118       Restored := Bounds(ReadIntegerWithDefault('RestoredLeft', (Screen.Width - Form.Width) div 2),
    119         ReadIntegerWithDefault('RestoredTop', (Screen.Height - Form.Height) div 2),
    120         ReadIntegerWithDefault('RestoredWidth', Form.Width),
    121         ReadIntegerWithDefault('RestoredHeight', Form.Height));
    122 
    123       if not EqualRect(Normal, Restored) or
    124         (LoadDefaults and DefaultMaximized) then begin
    125         // Restore to maximized state
    126         Form.WindowState := wsNormal;
    127         if not EqualRect(Restored, Form.BoundsRect) then
    128           Form.BoundsRect := Restored;
    129         Form.WindowState := wsMaximized;
    130       end else begin
    131         // Restore to normal state
    132         Form.WindowState := wsNormal;
    133         if FEntireVisible then Normal := CheckEntireVisible(Normal)
    134           else if FMinVisiblePart > 0 then
    135         Normal := CheckPartVisible(Normal, FMinVisiblePart);
    136         if not EqualRect(Normal, Form.BoundsRect) then
    137           Form.BoundsRect := Normal;
    138       end;
    139 
    140       //if ReadBoolWithDefault('Visible', False) then Form.Show;
    141     finally
    142       Free;
    143     end;
     222  Self.Form := Form;
     223  // Set default
     224  FormNormalSize := Bounds((Screen.Width - Form.Width) div 2,
     225    (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);
     226  FormRestoredSize := Bounds((Screen.Width - Form.Width) div 2,
     227    (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);
     228
     229  LoadFromRegistry(RegistryContext);
     230
     231  if not EqualRect(FormNormalSize, FormRestoredSize) or
     232    (LoadDefaults and DefaultMaximized) then begin
     233    // Restore to maximized state
     234    Form.WindowState := wsNormal;
     235    if not EqualRect(FormRestoredSize, Form.BoundsRect) then
     236      Form.BoundsRect := FormRestoredSize;
     237    Form.WindowState := wsMaximized;
     238  end else begin
     239    // Restore to normal state
     240    Form.WindowState := wsNormal;
     241    if FEntireVisible then FormNormalSize := CheckEntireVisible(FormNormalSize)
     242      else if FMinVisiblePart > 0 then
     243    FormNormalSize := CheckPartVisible(FormNormalSize, FMinVisiblePart);
     244    if not EqualRect(FormNormalSize, Form.BoundsRect) then
     245      Form.BoundsRect := FormNormalSize;
     246  end;
     247  LoadControl(Form);
    144248end;
    145249
    146250procedure TPersistentForm.Save(Form: TForm);
    147251begin
    148   with Form, TRegistryEx.Create do
    149     try
    150       RootKey := RegistryContext.RootKey;
    151       OpenKey(RegistryContext.Key + '\Forms\' + Form.Name, True);
    152       WriteInteger('NormalWidth', Form.Width);
    153       WriteInteger('NormalHeight', Form.Height);
    154       WriteInteger('NormalTop', Form.Top);
    155       WriteInteger('NormalLeft', Form.Left);
    156       WriteInteger('RestoredWidth', Form.RestoredWidth);
    157       WriteInteger('RestoredHeight', Form.RestoredHeight);
    158       WriteInteger('RestoredTop', Form.RestoredTop);
    159       WriteInteger('RestoredLeft', Form.RestoredLeft);
    160       //WriteInteger('WindowState', Integer(Form.WindowState));
    161       //WriteBool('Visible', Form.Visible);
    162     finally
    163       Free;
    164     end;
     252  Self.Form := Form;
     253  FormNormalSize := Bounds(Form.Left, Form.Top, Form.Width, Form.Height);
     254  FormRestoredSize := Bounds(Form.RestoredLeft, Form.RestoredTop, Form.RestoredWidth,
     255    Form.RestoredHeight);
     256  FormWindowState := Form.WindowState;
     257  SaveToRegistry(RegistryContext);
     258  SaveControl(Form);
    165259end;
    166260
     
    168262begin
    169263  inherited;
     264  if AOwner is TForm then Form := TForm(AOwner)
     265    else Form := nil;
    170266  FMinVisiblePart := 50;
    171267  FRegistryContext.RootKey := HKEY_CURRENT_USER;
Note: See TracChangeset for help on using the changeset viewer.