File: helpconnectionunit1.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (99 lines) | stat: -rw-r--r-- 2,101 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
unit HelpConnectionUnit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, LHelpControl,
  Buttons, StdCtrls, LazFileUtils;

const
  IPCFile = 'letstestagain';
type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
  public
    function GetLHelpFilename: string;
  end;

var
  Form1: TForm1; 
  Help: TLHelpConnection;

implementation

{ TForm1 }

function ResponseToString(Ares: TLHelpResponse): String;
begin
  case Ares of
    srNoAnswer: Result := 'NoAnswer';
    srSuccess: Result := 'Success';
    srInvalidFile: Result := 'InvalidFileName';
    srInvalidURL: Result := 'InvalidURL';
    srInvalidContext: Result := 'InvalidContext';
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Res: TLHelpResponse;
begin
  OpenDialog1.InitialDir:=GetCurrentDirUTF8;
  if not OpenDialog1.Execute then exit;
  Screen.Cursor := crHourGlass;
  try
    if Help.ServerRunning = false then
      Help.StartHelpServer(IPCFile, GetLHelpFilename);
    Res := Help.OpenFile(OpenDialog1.FileName);
  finally
    Screen.Cursor := crDefault;
  end;
  Label1.Caption := ResponseToString(Res);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  LHelp: String;
begin
  {$IFDEF Unix}
  DeleteFile('/tmp/'+IPCFile);
  {$ENDIF}
  LHelp := GetLHelpFilename;
  if not FileExistsUTF8(LHelp) then
    MessageDlg('Missing lhelp','Can not find the lhelp application "'+LHelp+'"',
       mtError,[mbOk],0);
  Help := TLHelpConnection.Create;
  Help.ProcessWhileWaiting := @Application.ProcessMessages;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Help.Free;
end;

function TForm1.GetLHelpFilename: string;
begin
  Result := '../lhelp/lhelp';
  {$IFDEF Windows}
  Result := '..\lhelp\lhelp.exe';
  {$ENDIF}
  {$IFDEF darwin} //OS X
  Result:=Result+'.app/Contents/MacOS/'+ExtractFilename(Result);
  {$ENDIF}
end;

{$R *.lfm}

end.