* LHelp: fix saving/restoring position/size/maximized state. Fixes issue #26804

git-svn-id: trunk@46575 -
This commit is contained in:
reiniero 2014-10-17 08:50:27 +00:00
parent a820e5a564
commit 4ebb1c0ec1
2 changed files with 60 additions and 34 deletions

View File

@ -109,6 +109,11 @@ type
fConfig: TXMLConfig; fConfig: TXMLConfig;
fHasShowed: Boolean; fHasShowed: Boolean;
fHide: boolean; //If yes, start with content hidden. Otherwise start normally fHide: boolean; //If yes, start with content hidden. Otherwise start normally
// Keep track of whether size/position preferences were loaded and applied to form
fLayoutApplied: boolean;
// Applies layout (size/position/fullscreen) preferences once in lhelp lifetime
// Needs LoadPreference to be run first to get fConfig object.
procedure ApplyLayoutPreferencesOnce;
// Load preferences. Preferences are unique for server-lhelp pairs and plain lhelp // Load preferences. Preferences are unique for server-lhelp pairs and plain lhelp
procedure LoadPreferences(AIPCName: String); procedure LoadPreferences(AIPCName: String);
// Saves preferences. Uses existing config loaded by LoadPreferences // Saves preferences. Uses existing config loaded by LoadPreferences
@ -163,6 +168,9 @@ implementation
uses uses
LHelpControl; LHelpControl;
const
DigitsInPID=5; // Number of digits in the formatted PID according to the Help Protocol
type type
TRecentMenuItem = class(TMenuItem) TRecentMenuItem = class(TMenuItem)
public public
@ -362,6 +370,29 @@ begin
RefreshState; RefreshState;
end; end;
procedure THelpForm.ApplyLayoutPreferencesOnce;
begin
if not(assigned(fConfig)) then exit;
if (not(fHide)) and
(not(fLayoutApplied)) then
begin
if (fConfig.GetValue('Position/Maximized', false)=true) then
begin
Windowstate:=wsMaximized
end
else
begin
Left := fConfig.GetValue('Position/Left/Value', Left);
Top := fConfig.GetValue('Position/Top/Value', Top);
Width := fConfig.GetValue('Position/Width/Value', Width);
Height := fConfig.GetValue('Position/Height/Value', Height);
end;
// Keep track so we do not reapply initial settings as user may have
// changed size etc in the meantime.
fLayoutApplied := true;
end;
end;
procedure THelpForm.ViewMenuContentsClick(Sender: TObject); procedure THelpForm.ViewMenuContentsClick(Sender: TObject);
begin begin
// TabsControl property in TChmContentProvider // TabsControl property in TChmContentProvider
@ -388,29 +419,16 @@ begin
ForceDirectoriesUTF8(PrefFile); ForceDirectoriesUTF8(PrefFile);
// --ipcname passes a server ID that consists of a // --ipcname passes a server ID that consists of a
// server-dependent constant together with a process ID. // server-dependent constant together with a process ID.
// Strip out the process ID to get fixed config file names for one server // Strip out the formatted process ID to get fixed config file names for
ServerPart := Copy(AIPCName, 1, length(AIPCName)-5); //strip out PID // one server
ServerPart := Copy(AIPCName, 1, length(AIPCName)-DigitsInPID);
PrefFile := Format('%slhelp-%s.conf',[IncludeTrailingPathDelimiter(PrefFile), ServerPart]); PrefFile := Format('%slhelp-%s.conf',[IncludeTrailingPathDelimiter(PrefFile), ServerPart]);
fConfig := TXMLConfig.Create(Self); fConfig := TXMLConfig.Create(Self);
fConfig.Filename := PrefFile; fConfig.Filename := PrefFile;
// Restore window but only if currently not being asked to hide // Restore window but only if currently not being asked to hide
if not(fHide) then ApplyLayoutPreferencesOnce;
begin
if (fConfig.GetValue('Position/Maximized',false)=true) then
begin
Windowstate:=wsMaximized
end
else
begin
Left := fConfig.GetValue('Position/Left/Value', Left);
Top := fConfig.GetValue('Position/Top/Value', Top);
Width := fConfig.GetValue('Position/Width/Value', Width);
Height := fConfig.GetValue('Position/Height/Value', Height);
end;
end;
OpenDialog1.FileName := fConfig.GetValue('LastFileOpen/Value', OpenDialog1.FileName); OpenDialog1.FileName := fConfig.GetValue('LastFileOpen/Value', OpenDialog1.FileName);
RecentCount:= fConfig.GetValue('Recent/ItemCount/Value', 0); RecentCount:= fConfig.GetValue('Recent/ItemCount/Value', 0);
@ -427,6 +445,7 @@ begin
exit; //silently abort exit; //silently abort
if not (WindowState = wsMaximized) then if not (WindowState = wsMaximized) then
begin begin
fConfig.SetValue('Position/Maximized', false);
fConfig.SetValue('Position/Left/Value', Left); fConfig.SetValue('Position/Left/Value', Left);
fConfig.SetValue('Position/Top/Value', Top); fConfig.SetValue('Position/Top/Value', Top);
fConfig.SetValue('Position/Width/Value', Width); fConfig.SetValue('Position/Width/Value', Width);
@ -616,6 +635,9 @@ begin
Self.SendToBack; Self.SendToBack;
Self.BringToFront; Self.BringToFront;
Self.ShowOnTop; Self.ShowOnTop;
// If lhelp was run with hidden parameter, we need to apply
// layout preferences once:
ApplyLayoutPreferencesOnce;
end; end;
end; end;
end; end;

View File

@ -63,6 +63,7 @@ type
procedure SetHelpEXE(AValue: String); procedure SetHelpEXE(AValue: String);
protected protected
function GetFileNameAndURL(RawUrl: String; out FileName: String; out URL: String): Boolean; function GetFileNameAndURL(RawUrl: String; out FileName: String; out URL: String): Boolean;
// Sets label/ID used for simpleipc communications
procedure SetHelpLabel(AValue: String); procedure SetHelpLabel(AValue: String);
// Check for lhelp executable, if not present, build if possible // Check for lhelp executable, if not present, build if possible
function CheckBuildLHelp: Integer; // modal result function CheckBuildLHelp: Integer; // modal result
@ -112,12 +113,15 @@ const
// Part of help name. Stored/retrieved in Lazarus options CHMHelp/Name. // Part of help name. Stored/retrieved in Lazarus options CHMHelp/Name.
// Do not localize. // Do not localize.
CHMHelpName='lazhelp'; CHMHelpName='lazhelp';
DigitsInPID=5; // Number of digits in the formatted PID according to the Help Protocol
// Formats (part of) process ID according to help protocol for // Formats (part of) process ID according to help protocol for
// use in ipcname parameter // use in ipcname parameter
function HelpProtocolFormattedPID: string; function HelpProtocolFormattedPID: string;
begin begin
result := copy(inttostr(GetProcessID)+'00000',1,5) // Make sure at least DigitsInPID digits present even if 0
result := copy(inttostr(GetProcessID)+
StringOfChar('0',DigitsInPID),1,DigitsInPID)
end; end;
procedure Register; procedure Register;
@ -149,8 +153,7 @@ function TChmHelpViewer.GetHelpLabel: String;
begin begin
// fHelpLabel is used for SimpleIPC server id; // fHelpLabel is used for SimpleIPC server id;
// lhelp protocol specifies server-dependent constant string // lhelp protocol specifies server-dependent constant string
// followed by string representation of last 5 digits of the processID // followed by formatted string representation of (part of) processid/PID
// padded with 00000 at the right
// Autocalculate if needed: // Autocalculate if needed:
if Length(fHelpLabel) = 0 then if Length(fHelpLabel) = 0 then
fHelpLabel := CHMHelpName + HelpProtocolFormattedPID; fHelpLabel := CHMHelpName + HelpProtocolFormattedPID;
@ -280,21 +283,22 @@ begin
if not (fHelpLabel[i] in ['a'..'z', '0'..'9', 'A'..'Z']) then if not (fHelpLabel[i] in ['a'..'z', '0'..'9', 'A'..'Z']) then
fHelpLabel[i] := '_'; fHelpLabel[i] := '_';
end; end;
// todo: for some reason we get assigned strings like // Validity check for labels like
// lazhelp501204548 // lazhelp501204548
// i.e. with too many numbers in the PID part // i.e. with too many numbers in the PID part.
// Hacky way to fix this: if too many numbers, replace with // These may be left over in option files from earlier Lazarus versions.
// default // If too many numbers at end, replace with default
if fHelpLabel[length(FHelpLabel)] in ['0'..'9'] then if fHelpLabel[length(FHelpLabel)] in ['0'..'9'] then
begin begin
for i := Length(fHelpLabel) downto 1 do for i := Length(fHelpLabel) downto 1 do
begin begin
if not(fHelpLabel[i] in ['0'..'9']) then if not(fHelpLabel[i] in ['0'..'9']) then
begin begin
if (Length(fHelpLabel)-i>5) then if (Length(fHelpLabel)-i>DigitsInPID) then
begin begin
Debugln('TChmHelpViewer.SetHelpLabel: got '+fHelpLabel+'. Help protocol does not allow this many digits. Resetting to empty string.'); Debugln('TChmHelpViewer.SetHelpLabel: got '+fHelpLabel+'. Help protocol does not allow this many digits. Resetting to empty string.');
fHelpLabel := ''; // Revert to default
fHelpLabel := CHMHelpName + HelpProtocolFormattedPID;
end; end;
break; break;
end; end;
@ -557,14 +561,14 @@ begin
if Trim(fHelpExeParams) = '' then if Trim(fHelpExeParams) = '' then
begin begin
Result := shrViewerError; Result := shrViewerError;
ErrMsg := 'If you do not use "lhelp" as viewer you have to set up ' ErrMsg := 'If you do not use "lhelp" as viewer you have to set up ' +
+ 'HelpExeParams correctly in' + sLineBreak 'HelpExeParams correctly in' + LineEnding +
+ 'Tools -> Options -> Help -> Help Options -> ' 'Tools -> Options -> Help -> Help Options -> ' +
+ 'under HelpViewers - CHM Help Viewer' + sLineBreak 'under HelpViewers - CHM Help Viewer' + LineEnding +
+ 'e.g. for HH.EXE (HTML Help in Windows) it must be' + sLineBreak 'e.g. for HH.EXE (HTML Help in Windows) it must be' + LineEnding +
+ ' "%s::%s"' + sLineBreak ' "%s::%s"' + LineEnding +
+ 'where first %s will be replaced by CHM file name' + sLineBreak 'where first %s will be replaced by CHM file name' + LineEnding +
+ 'and the second one will be replaced by URL'; 'and the second one will be replaced by URL';
Exit; Exit;
end; end;