home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / kompon / d345 / PPREV.ZIP / BenPreview / FormSettings.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-10-12  |  8.9 KB  |  295 lines

  1. unit FormSettings;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Registry, StdCtrls;
  8.  
  9. type
  10.    TSaveValOpt = (svEdit, svMemo, svCheckBox, svRadioButton, svListBox,
  11.       svComboBox, svFontDialog);
  12.    TSaveValSet = set of TSaveValOpt;
  13.  
  14.    TFormSettings = class(TComponent)
  15.    protected
  16.       FSavePos    : boolean;
  17.       FSaveVals   : boolean;
  18.       FLoadVals   : boolean;
  19.       FKeyName    : string;
  20.       FSaveOpt    : TSaveValSet;
  21.       FRootCon    : TWinControl;
  22.       DidLastSave : boolean;
  23.       procedure   Loaded; override;
  24.       function    StrToWS(const s: string): TWindowState;
  25.       function    WSToStr(ws: TWindowState): string;
  26.       function    GetKeyName: string;
  27.       procedure   Notification(AComponent: TComponent; Operation: TOperation); override;
  28.       procedure   DoLoadValues(reg: TRegIniFile);
  29.       procedure   DoSaveValues(reg: TRegIniFile);
  30.       procedure   ReadFont(Name: string; f: TFont; reg: TRegIniFile);
  31.       procedure   WriteFont(Name: string; f: TFont; reg: TRegIniFile);
  32.    public
  33.       constructor Create(AOwner: TComponent); override;
  34.       destructor  Destroy; override;
  35.       procedure   LoadSettings;
  36.       procedure   SaveSettings;
  37.    published
  38.       property    SavePosition: boolean read FSavePos write FSavePos;
  39.       property    SaveValues: boolean read FSaveVals write FSaveVals;
  40.       property    LoadValues: boolean read FLoadVals write FLoadVals;
  41.       property    SaveValueOptions: TSaveValSet read FSaveOpt write FSaveOpt;
  42.       property    KeyName: string read GetKeyName write FKeyName;
  43.       property    RootControl: TWinControl read FRootCon write FRootCon;
  44.    end;
  45.  
  46. procedure Register;
  47.  
  48. implementation
  49.  
  50. const
  51.    WindowStr : array[1..3] of string = ('NORMAL', 'MAXIMIZED', 'MINIMIZED');
  52.  
  53.  
  54. constructor TFormSettings.Create(AOwner: TComponent);
  55. begin
  56.    inherited;
  57.    FreeNotification(AOwner);
  58.    FSavePos    := True;
  59.    FSaveVals   := False;
  60.    FLoadVals   := False;
  61.    DidLastSave := False;
  62.    FSaveOpt := [svEdit, svMemo, svCheckBox, svRadioButton, svListBox,
  63.       svComboBox, svFontDialog];
  64. end;
  65.  
  66. destructor TFormSettings.Destroy;
  67. begin
  68.    inherited;
  69. end;
  70.  
  71. procedure TFormSettings.Notification(AComponent: TComponent; Operation: TOperation);
  72. begin
  73.    inherited;
  74.    if (not DidLastSave) and (csDestroying in ComponentState) then begin
  75.       DidLastSave := True;
  76.       // This still doesn't work!  All the window handles are gone! -bpz
  77.       // SaveSettings;
  78.    end;
  79. end;
  80.  
  81. procedure TFormSettings.Loaded;
  82. begin
  83.    inherited;
  84.    LoadSettings;
  85. end;
  86.  
  87. function TFormSettings.StrToWS(const s: string): TWindowState;
  88. var
  89.    t : string;
  90. begin
  91.    t := UpperCase(s);
  92.    Result := wsNormal;
  93.    if t = WindowStr[1] then Result := wsNormal;
  94.    if t = WindowStr[2] then Result := wsMaximized;
  95.    if t = WindowStr[3] then Result := wsMinimized;
  96. end;
  97.  
  98. function TFormSettings.WSToStr(ws: TWindowState): string;
  99. begin
  100.    case ws of
  101.       wsNormal    : Result := WindowStr[1];
  102.       wsMaximized : Result := WindowStr[2];
  103.       wsMinimized : Result := WIndowStr[3];
  104.    else
  105.       Result := WindowStr[1];
  106.    end;
  107. end;
  108.  
  109. function TFormSettings.GetKeyName: string;
  110. begin
  111.    Result := FKeyName;
  112.  
  113.    if (Result='') and (not (csDesigning in ComponentState))
  114.       and (Application<>nil) and (Owner<>nil) then
  115.       Result := 'Software\' + Application.Title + '\' + Owner.Name;
  116. end;
  117.  
  118. procedure TFormSettings.LoadSettings;
  119. var
  120.    f   : TForm;
  121.    reg : TRegIniFile;
  122. begin
  123.    if (Owner = nil) or (not (Owner is TForm)) then exit;
  124.    if csDesigning in ComponentState then exit;
  125.  
  126.    f := Owner as TForm;
  127.    f.Position := poDesigned;
  128.  
  129.    reg := TRegIniFile.Create(KeyName);
  130.  
  131.    if SavePosition then begin
  132.       f.Left        := reg.ReadInteger('Position', 'Left', f.Left);
  133.       f.Top         := reg.ReadInteger('Position', 'Top', f.Top);
  134.       f.Width       := reg.ReadInteger('Position', 'Width', f.Width);
  135.       f.Height      := reg.ReadInteger('Position', 'Height', f.Height);
  136.       f.WindowState := StrToWS(reg.ReadString('Position', 'WindowState', 'Normal'));
  137.    end;
  138.  
  139.    if LoadValues then
  140.       DoLoadValues(reg);
  141.  
  142.    reg.Free;
  143. end;
  144.  
  145. procedure TFormSettings.SaveSettings;
  146. var
  147.    f   : TForm;
  148.    reg : TRegIniFile;
  149. begin
  150.    if (Owner = nil) or (not (Owner is TForm)) then exit;
  151.    if csDesigning in ComponentState then exit;
  152.  
  153.    f := Owner as TForm;
  154.    f.Position := poDesigned;
  155.  
  156.    reg := TRegIniFile.Create(KeyName);
  157.  
  158.    if SavePosition then begin
  159.       if f.WindowState = wsNormal then begin
  160.          reg.WriteInteger('Position', 'Left', f.Left);
  161.          reg.WriteInteger('Position', 'Top', f.Top);
  162.          reg.WriteInteger('Position', 'Width', f.Width);
  163.          reg.WriteInteger('Position', 'Height', f.Height);
  164.       end;
  165.       reg.WriteString('Position', 'WindowState', WSToStr(f.WindowState));
  166.    end;
  167.  
  168.    if SaveValues then
  169.       DoSaveValues(reg);
  170.  
  171.    reg.Free;
  172. end;
  173.  
  174. procedure TFormSettings.DoLoadValues(reg: TRegIniFile);
  175. var
  176.    i     : integer;
  177.    con   : TWinControl;
  178.    c     : TControl;
  179.    cp    : TComponent;
  180. begin
  181.    con := RootControl;
  182.    if con=nil then con := Owner as TForm;
  183.    Assert(con<>nil);
  184.  
  185.    for i := 0 to con.ComponentCount-1 do begin
  186.       cp := con.Components[i];
  187.       if not (cp is TControl) then continue;
  188.       c := cp as TControl;
  189.  
  190.       if c is TEdit then
  191.          TEdit(c).Text := reg.ReadString('Values', c.Name, TEdit(c).Text);
  192.       if c is TMemo then
  193.          TMemo(c).Text := reg.ReadString('Values', c.Name, TMemo(c).Text);
  194.       if c is TCheckBox then
  195.          TCheckBox(c).Checked := reg.ReadBool('Values', c.Name, TCheckBox(c).Checked);
  196.       if c is TRadioButton then
  197.          TRadioButton(c).Checked := reg.ReadBool('Values', c.Name, TRadioButton(c).Checked);
  198.       if c is TListBox then
  199.          TListBox(c).ItemIndex := reg.ReadInteger('Values', c.Name, TListBox(c).ItemIndex);
  200.       if c is TComboBox then
  201.          TComboBox(c).ItemIndex := reg.ReadInteger('Values', c.Name, TComboBox(c).ItemIndex);
  202.    end;
  203.  
  204.    for i := 0 to con.ComponentCount-1 do begin
  205.       cp := con.Components[i];
  206.       if cp is TFontDialog then
  207.          ReadFont(cp.Name, TFontDialog(cp).Font, reg);
  208.    end;
  209. end;
  210.  
  211. procedure TFormSettings.DoSaveValues(reg: TRegIniFile);
  212. var
  213.    i     : integer;
  214.    con   : TWinControl;
  215.    c     : TControl;
  216.    cp    : TComponent;
  217. begin
  218.    con := RootControl;
  219.    if con=nil then con := Owner as TForm;
  220.    Assert(con<>nil);
  221.  
  222.    for i := 0 to con.ComponentCount-1 do begin
  223.       cp := con.Components[i];
  224.       if not (cp is TControl) then continue;
  225.       c := cp as TControl;
  226.  
  227.       if c is TEdit then
  228.          reg.WriteString('Values', c.Name, TEdit(c).Text);
  229.       if c is TMemo then
  230.          reg.WriteString('Values', c.Name, TMemo(c).Text);
  231.       if c is TCheckBox then
  232.          reg.WriteBool('Values', c.Name, TCheckBox(c).Checked);
  233.       if c is TRadioButton then
  234.          reg.WriteBool('Values', c.Name, TRadioButton(c).Checked);
  235.       if c is TListBox then
  236.          reg.WriteInteger('Values', c.Name, TListBox(c).ItemIndex);
  237.       if c is TComboBox then
  238.          reg.WriteInteger('Values', c.Name, TComboBox(c).ItemIndex);
  239.    end;
  240.  
  241.    for i := 0 to con.ComponentCount-1 do begin
  242.       cp := con.Components[i];
  243.       if cp is TFontDialog then
  244.          WriteFont(cp.Name, TFontDialog(cp).Font, reg);
  245.    end;
  246. end;
  247.  
  248. procedure TFormSettings.ReadFont(Name: string; f: TFont; reg: TRegIniFile);
  249. var
  250.    b : boolean;
  251. begin
  252.    f.Name  := reg.ReadString('Values', Name + '_Name', f.Name);
  253.    f.Size  := reg.ReadInteger('Values', Name + '_Size', f.Size);
  254.    f.Color := reg.ReadInteger('Values', Name + '_Color', f.Color);
  255.  
  256.    b := reg.ReadBool('Values', Name + '_Bold', fsBold in f.Style);
  257.    if b then f.Style := f.Style + [fsBold]
  258.       else f.Style := f.Style - [fsBold];
  259.  
  260.    b := reg.ReadBool('Values', Name + '_Italic', fsItalic in f.Style);
  261.    if b then f.Style := f.Style + [fsItalic]
  262.       else f.Style := f.Style - [fsItalic];
  263.  
  264.    b := reg.ReadBool('Values', Name + '_Underline', fsUnderline in f.Style);
  265.    if b then f.Style := f.Style + [fsUnderline]
  266.       else f.Style := f.Style - [fsUnderline];
  267.  
  268.    b := reg.ReadBool('Values', Name + '_StrikeOut', fsStrikeOut in f.Style);
  269.    if b then f.Style := f.Style + [fsStrikeOut]
  270.       else f.Style := f.Style - [fsStrikeOut];
  271. end;
  272.  
  273. procedure TFormSettings.WriteFont(Name: string; f: TFont; reg: TRegIniFile);
  274. begin
  275.    reg.WriteString('Values', Name + '_Name', f.Name);
  276.    reg.WriteInteger('Values', Name + '_Size', f.Size);
  277.    reg.WriteInteger('Values', Name + '_Color', f.Color);
  278.  
  279.    reg.WriteBool('Values', Name + '_Bold', fsBold in f.Style);
  280.    reg.WriteBool('Values', Name + '_Italic', fsItalic in f.Style);
  281.    reg.WriteBool('Values', Name + '_Underline', fsUnderline in f.Style);
  282.    reg.WriteBool('Values', Name + '_StrikeOut', fsStrikeOut in f.Style);
  283. end;
  284.  
  285.  
  286. procedure Register;
  287. begin
  288.   RegisterComponents('Print Preview', [TFormSettings]);
  289. end;
  290.  
  291. end.
  292.  
  293.  
  294.  
  295.