* 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;
fHasShowed: Boolean;
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
procedure LoadPreferences(AIPCName: String);
// Saves preferences. Uses existing config loaded by LoadPreferences
@ -163,6 +168,9 @@ implementation
uses
LHelpControl;
const
DigitsInPID=5; // Number of digits in the formatted PID according to the Help Protocol
type
TRecentMenuItem = class(TMenuItem)
public
@ -362,6 +370,29 @@ begin
RefreshState;
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);
begin
// TabsControl property in TChmContentProvider
@ -388,29 +419,16 @@ begin
ForceDirectoriesUTF8(PrefFile);
// --ipcname passes a server ID that consists of a
// server-dependent constant together with a process ID.
// Strip out the process ID to get fixed config file names for one server
ServerPart := Copy(AIPCName, 1, length(AIPCName)-5); //strip out PID
// Strip out the formatted process ID to get fixed config file names for
// one server
ServerPart := Copy(AIPCName, 1, length(AIPCName)-DigitsInPID);
PrefFile := Format('%slhelp-%s.conf',[IncludeTrailingPathDelimiter(PrefFile), ServerPart]);
fConfig := TXMLConfig.Create(Self);
fConfig.Filename := PrefFile;
// Restore window but only if currently not being asked to hide
if not(fHide) 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;
end;
ApplyLayoutPreferencesOnce;
OpenDialog1.FileName := fConfig.GetValue('LastFileOpen/Value', OpenDialog1.FileName);
RecentCount:= fConfig.GetValue('Recent/ItemCount/Value', 0);
@ -427,6 +445,7 @@ begin
exit; //silently abort
if not (WindowState = wsMaximized) then
begin
fConfig.SetValue('Position/Maximized', false);
fConfig.SetValue('Position/Left/Value', Left);
fConfig.SetValue('Position/Top/Value', Top);
fConfig.SetValue('Position/Width/Value', Width);
@ -616,6 +635,9 @@ begin
Self.SendToBack;
Self.BringToFront;
Self.ShowOnTop;
// If lhelp was run with hidden parameter, we need to apply
// layout preferences once:
ApplyLayoutPreferencesOnce;
end;
end;
end;

View File

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