Ignore:
Timestamp:
Apr 3, 2025, 10:49:00 PM (13 days ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
File:
1 moved

Legend:

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

    r20 r21  
    1 unit UPersistentForm;
    2 
    3 {$mode delphi}
    4 
    5 // Date: 2015-04-18
     1unit PersistentForm;
    62
    73interface
    84
    95uses
    10   Classes, SysUtils, Forms, URegistry, LCLIntf, Registry, Controls, ComCtrls;
     6  Classes, SysUtils, Forms, RegistryEx, LCLIntf, Registry, Controls, ComCtrls,
     7  ExtCtrls, LCLType;
    118
    129type
     
    1916    FMinVisiblePart: Integer;
    2017    FRegistryContext: TRegistryContext;
     18    FResizeEventOccured: Boolean;
    2119    procedure LoadControl(Control: TControl);
    2220    procedure SaveControl(Control: TControl);
     21    procedure WindowStateChange(Sender: TObject);
    2322  public
    24     FormNormalSize: TRect;
    2523    FormRestoredSize: TRect;
    2624    FormWindowState: TWindowState;
     25    FormFullScreen: Boolean;
    2726    Form: TForm;
    2827    procedure LoadFromRegistry(RegistryContext: TRegistryContext);
     
    3029    function CheckEntireVisible(Rect: TRect): TRect;
    3130    function CheckPartVisible(Rect: TRect; Part: Integer): TRect;
    32     procedure Load(Form: TForm; DefaultMaximized: Boolean = False);
     31    procedure Load(Form: TForm; DefaultMaximized: Boolean = False;
     32      DefaultFullScreen: Boolean = False);
    3333    procedure Save(Form: TForm);
    3434    constructor Create(AOwner: TComponent); override;
     35    procedure SetFullScreen(State: Boolean);
    3536    property RegistryContext: TRegistryContext read FRegistryContext
    3637      write FRegistryContext;
     
    4243procedure Register;
    4344
     45
    4446implementation
    45 
    4647
    4748procedure Register;
     
    5657  I: Integer;
    5758  WinControl: TWinControl;
    58   Count: Integer;
    5959begin
    6060  if Control is TListView then begin
     
    7272  end;
    7373
     74  if (Control is TPanel) then begin
     75    with Form, TRegistryEx.Create do
     76    try
     77      RootKey := RegistryContext.RootKey;
     78      OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True);
     79      if (TPanel(Control).Align = alRight) or (TPanel(Control).Align = alLeft) then begin
     80        if ValueExists('Width') then
     81          TPanel(Control).Width := ReadInteger('Width');
     82      end;
     83      if (TPanel(Control).Align = alTop) or (TPanel(Control).Align = alBottom) then begin
     84        if ValueExists('Height') then
     85          TPanel(Control).Height := ReadInteger('Height');
     86      end;
     87    finally
     88      Free;
     89    end;
     90  end;
     91
    7492  if Control is TWinControl then begin
    7593    WinControl := TWinControl(Control);
     
    96114      for I := 0 to TListView(Control).Columns.Count - 1 do begin
    97115        WriteInteger('ColWidth' + IntToStr(I), TListView(Control).Columns[I].Width);
     116      end;
     117    finally
     118      Free;
     119    end;
     120  end;
     121
     122  if (Control is TPanel) then begin
     123    with Form, TRegistryEx.Create do
     124    try
     125      RootKey := RegistryContext.RootKey;
     126      OpenKey(RegistryContext.Key + '\Forms\' + Form.Name + '\' + Control.Name, True);
     127      if (TPanel(Control).Align = alRight) or (TPanel(Control).Align = alLeft) then begin
     128        WriteInteger('Width', TPanel(Control).Width);
     129      end;
     130      if (TPanel(Control).Align = alTop) or (TPanel(Control).Align = alBottom) then begin
     131        WriteInteger('Height', TPanel(Control).Height);
    98132      end;
    99133    finally
     
    120154    RootKey := RegistryContext.RootKey;
    121155    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;
     156
    129157    // Restored size
    130158    FormRestoredSize.Left := ReadIntegerWithDefault('RestoredLeft', FormRestoredSize.Left);
     
    134162    FormRestoredSize.Bottom := ReadIntegerWithDefault('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top)
    135163      + FormRestoredSize.Top;
     164
    136165    // Other state
    137     FormWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(wsNormal)));
     166    FormWindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(FormWindowState)));
     167    FormFullScreen := ReadBoolWithDefault('FullScreen', FormFullScreen);
    138168  finally
    139169    Free;
     
    147177    RootKey := RegistryContext.RootKey;
    148178    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
     179
     180    // Restored size
    155181    WriteInteger('RestoredWidth', FormRestoredSize.Right - FormRestoredSize.Left);
    156182    WriteInteger('RestoredHeight', FormRestoredSize.Bottom - FormRestoredSize.Top);
    157183    WriteInteger('RestoredTop', FormRestoredSize.Top);
    158184    WriteInteger('RestoredLeft', FormRestoredSize.Left);
     185
    159186    // Other state
    160187    WriteInteger('WindowState', Integer(FormWindowState));
     188    WriteBool('FullScreen', FormFullScreen);
    161189  finally
    162190    Free;
     
    216244end;
    217245
    218 procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False);
    219 var
    220   LoadDefaults: Boolean;
     246procedure TPersistentForm.Load(Form: TForm; DefaultMaximized: Boolean = False;
     247  DefaultFullScreen: Boolean = False);
    221248begin
    222249  Self.Form := Form;
     250
    223251  // Set default
    224   FormNormalSize := Bounds((Screen.Width - Form.Width) div 2,
    225     (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);
    226252  FormRestoredSize := Bounds((Screen.Width - Form.Width) div 2,
    227253    (Screen.Height - Form.Height) div 2, Form.Width, Form.Height);
     254  FormWindowState := Form.WindowState;
     255  FormFullScreen := DefaultFullScreen;
    228256
    229257  LoadFromRegistry(RegistryContext);
    230258
    231   if not EqualRect(FormNormalSize, FormRestoredSize) or
    232     (LoadDefaults and DefaultMaximized) then begin
     259  if (FormWindowState = wsMaximized) or DefaultMaximized then begin
    233260    // Restore to maximized state
    234261    Form.WindowState := wsNormal;
     
    239266    // Restore to normal state
    240267    Form.WindowState := wsNormal;
    241     if FEntireVisible then FormNormalSize := CheckEntireVisible(FormNormalSize)
     268    if FEntireVisible then FormRestoredSize := CheckEntireVisible(FormRestoredSize)
    242269      else if FMinVisiblePart > 0 then
    243     FormNormalSize := CheckPartVisible(FormNormalSize, FMinVisiblePart);
    244     if not EqualRect(FormNormalSize, Form.BoundsRect) then
    245       Form.BoundsRect := FormNormalSize;
    246   end;
     270        FormRestoredSize := CheckPartVisible(FormRestoredSize, FMinVisiblePart);
     271    if not EqualRect(FormRestoredSize, Form.BoundsRect) then
     272      Form.BoundsRect := FormRestoredSize;
     273  end;
     274  if FormFullScreen then SetFullScreen(True);
    247275  LoadControl(Form);
    248276end;
     
    251279begin
    252280  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;
     281  if not FormFullScreen then begin
     282    FormWindowState := Form.WindowState;
     283    if FormWindowState = wsMaximized then begin
     284      FormRestoredSize := Bounds(Form.RestoredLeft, Form.RestoredTop, Form.RestoredWidth,
     285        Form.RestoredHeight);
     286    end else
     287    if FormWindowState = wsNormal then begin
     288      FormRestoredSize := Bounds(Form.Left, Form.Top, Form.Width, Form.Height);
     289    end;
     290  end;
    257291  SaveToRegistry(RegistryContext);
    258292  SaveControl(Form);
     
    268302end;
    269303
     304procedure TPersistentForm.SetFullScreen(State: Boolean);
     305{$IFDEF UNIX}
     306var
     307  OldHandler: TNotifyEvent;
     308var
     309  I: Integer;
     310{$ENDIF}
     311begin
     312  if State then begin
     313    FormFullScreen := True;
     314    if Form.WindowState = wsMaximized then begin
     315      FormRestoredSize := Bounds(Form.RestoredLeft, Form.RestoredTop, Form.RestoredWidth,
     316        Form.RestoredHeight);
     317    end else
     318    if Form.WindowState = wsNormal then begin
     319      FormRestoredSize := Bounds(Form.Left, Form.Top, Form.Width, Form.Height);
     320    end;
     321    FormWindowState := Form.WindowState;
     322    {$IFDEF WINDOWS}
     323    Form.BorderStyle := bsNone;
     324    {$ENDIF}
     325    Form.WindowState := wsFullscreen;
     326    {$IFDEF UNIX}
     327    // Workaround on Linux, WindowState is rewriten by WMSize event to wsNormal.
     328    // We need for that even to occure
     329    OldHandler := Form.OnWindowStateChange;
     330    Form.OnWindowStateChange := WindowStateChange;
     331    FResizeEventOccured := False;
     332    for I := 0 to 10 do begin
     333      if FResizeEventOccured then Break;
     334      Application.ProcessMessages;
     335      Sleep(1);
     336    end;
     337    Form.OnWindowStateChange := OldHandler;
     338    FormFullScreen := True;
     339    {$ENDIF}
     340  end else begin
     341    FormFullScreen := False;
     342    Form.WindowState := wsNormal;
     343    {$IFDEF WINDOWS}
     344    Form.BorderStyle := bsSizeable;
     345    {$ENDIF}
     346    if FormWindowState = wsNormal then begin
     347      Form.WindowState := wsNormal;
     348      Form.BoundsRect := FormRestoredSize;
     349    end else
     350    if FormWindowState = wsMaximized then begin
     351      Form.BoundsRect := FormRestoredSize;
     352      Form.WindowState := wsMaximized;
     353    end;
     354  end;
     355end;
     356
     357procedure TPersistentForm.WindowStateChange(Sender: TObject);
     358begin
     359  Form.WindowState := wsFullscreen;
     360  FResizeEventOccured := True;
     361end;
     362
    270363end.
    271 
Note: See TracChangeset for help on using the changeset viewer.