This file is indexed.

/usr/lib/lazarus/0.9.30.4/ide/idewindowhelp.pas is in lazarus-src-0.9.30.4 0.9.30.4-6.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 *                                                                         *
 ***************************************************************************

  Author: Mattias Gaertner

  Abstract:
    Help for IDE windows (controls).
}
unit IDEWindowHelp;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLProc, Controls, FileUtil, Dialogs, HelpIntfs,
  LazConfigStorage, EnvironmentOpts, IDEOptionDefs;
  
type

  { TIWHelpNode }
  
  TIWHelpNode = class
  private
    FIsRoot: boolean;
    FItems: TFPList;// list of TIWHelpNode
    FHasHelp: Boolean;
    FName: string;
    FParent: TIWHelpNode;
    FPath: string;
    function GetChilds(Index: integer): TIWHelpNode;
    function GetCount: integer;
    procedure SetHasHelp(const AValue: Boolean);
    procedure SetIsRoot(const AValue: boolean);
    procedure SetName(const AValue: string);
    procedure SetPath(const AValue: string);
    procedure DoRemove(AChild: TIWHelpNode);
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear;
    procedure Assign(Source: TIWHelpNode);
    function AddChild(const ChildName: string = '';
                      const ChildPath: string = ''): TIWHelpNode;
    procedure Load(Config: TConfigStorage; const CfgPath: string);
    procedure Save(Config: TConfigStorage; const CfgPath: string);
    function FindByName(const ChildName: string): TIWHelpNode;
    procedure DeleteLeavesWithoutHelp;
    function GetFullPath: string;
  public
    property HasHelp: Boolean read FHasHelp write SetHasHelp;
    property IsRoot: boolean read FIsRoot write SetIsRoot;// skip parent paths, except path of the top node
    property Name: string read FName write SetName;
    property Path: string read FPath write SetPath;
    property Parent: TIWHelpNode read FParent;
    property Count: integer read GetCount;
    property Children[Index: integer]: TIWHelpNode read GetChilds; default;
  end;
  
  { TIWHelpTree }

  TIWHelpTree = class
  private
    FRoot: TIWHelpNode;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear;
    procedure Assign(Source: TIWHelpTree);
    procedure Load(Config: TConfigStorage; const Path: string);
    procedure Save(Config: TConfigStorage; const Path: string);
    function ControlHasValidNamePath(AControl: TControl): Boolean;
    function FindNodeForControl(AControl: TControl;
                               CreateIfNotExists: Boolean = false): TIWHelpNode;
    procedure WriteDebugReport;
    procedure DeleteLeavesWithoutHelp;
    procedure InvokeHelp(AControl: TControl);
    function CreateURL(AControl: TControl): string;
  public
    property Root: TIWHelpNode read FRoot;
  end;
  
const
  IDEWindowHelpTreeFile = 'docs/IDEWindowHelpTree.xml';

var
  IDEWindowHelpNodes: TIWHelpTree = nil;
  
function GetIDEWindowHelpFilename: string;
procedure LoadIDEWindowHelp;
procedure SaveIDEWindowHelp;

implementation

function GetIDEWindowHelpFilename: string;
begin
  Result:=AppendPathDelim(EnvironmentOptions.LazarusDirectory)
           +SetDirSeparators(IDEWindowHelpTreeFile);
end;

procedure LoadIDEWindowHelp;
var
  Filename: String;
  Config: TXMLOptionsStorage;
begin
  if IDEWindowHelpNodes=nil then
    IDEWindowHelpNodes:=TIWHelpTree.Create;
  Filename:=GetIDEWindowHelpFilename;
  try
    Config:=TXMLOptionsStorage.Create(Filename,true);
    if Config=nil then exit;
    try
      IDEWindowHelpNodes.Load(Config,'');
    finally
      Config.Free;
    end;
  except
    on E: Exception do begin
      MessageDlg('Read error','Error reading file '+Filename+#13+E.Message,
        mtError,[mbOk],0);
    end;
  end;
end;

procedure SaveIDEWindowHelp;
var
  Filename: String;
  Config: TConfigStorage;
begin
  if IDEWindowHelpNodes=nil then exit;
  Filename:=GetIDEWindowHelpFilename;
  try
    Config:=TXMLOptionsStorage.Create(Filename,false);
    if Config=nil then exit;
    try
      IDEWindowHelpNodes.Save(Config,'');
      Config.WriteToDisk;
    finally
      Config.Free;
    end;
  except
    on E: Exception do begin
      MessageDlg('Write error','Error writing file '+Filename+#13+E.Message,
        mtError,[mbOk],0);
    end;
  end;
end;

{ TIWHelpNode }

procedure TIWHelpNode.SetHasHelp(const AValue: Boolean);
begin
  if FHasHelp=AValue then exit;
  FHasHelp:=AValue;
end;

procedure TIWHelpNode.SetIsRoot(const AValue: boolean);
begin
  if FIsRoot=AValue then exit;
  FIsRoot:=AValue;
end;

procedure TIWHelpNode.SetName(const AValue: string);
begin
  if FName=AValue then exit;
  FName:=AValue;
end;

function TIWHelpNode.GetChilds(Index: integer): TIWHelpNode;
begin
  Result:=TIWHelpNode(FItems[Index]);
end;

function TIWHelpNode.GetCount: integer;
begin
  if FItems<>nil then
    Result:=FItems.Count
  else
    Result:=0;
end;

procedure TIWHelpNode.SetPath(const AValue: string);
begin
  if FPath=AValue then exit;
  FPath:=AValue;
end;

procedure TIWHelpNode.DoRemove(AChild: TIWHelpNode);
begin
  FItems.Remove(AChild);
  AChild.FParent:=nil;
end;

constructor TIWHelpNode.Create;
begin

end;

destructor TIWHelpNode.Destroy;
begin
  Clear;
  if FParent<>nil then
    FParent.DoRemove(Self);
  FreeAndNil(FItems);
  inherited Destroy;
end;

procedure TIWHelpNode.Clear;
var
  i: Integer;
  CurChild: TIWHelpNode;
begin
  if FItems<>nil then begin
    for i:=FItems.Count-1 downto 0 do begin
      CurChild:=Children[i];
      CurChild.FParent:=nil;
      CurChild.Free;
    end;
    FreeAndNil(FItems);
  end;
end;

procedure TIWHelpNode.Assign(Source: TIWHelpNode);
var
  i: Integer;
  SrcNode: TIWHelpNode;
  NewNode: TIWHelpNode;
begin
  Clear;
  Name:=Source.Name;
  Path:=Source.Path;
  HasHelp:=Source.HasHelp;
  IsRoot:=Source.IsRoot;
  for i:=0 to Source.Count-1 do begin
    SrcNode:=Source[i];
    NewNode:=AddChild;
    NewNode.Assign(SrcNode);
  end;
end;

function TIWHelpNode.AddChild(const ChildName: string;
  const ChildPath: string): TIWHelpNode;
begin
  Result:=TIWHelpNode.Create;
  Result.FParent:=Self;
  Result.Name:=ChildName;
  Result.Path:=ChildPath;
  if FItems=nil then
    FItems:=TFPList.Create;
  FItems.Add(Result);
end;

procedure TIWHelpNode.Load(Config: TConfigStorage; const CfgPath: string);
var
  NewChildCount: LongInt;
  i: Integer;
  NewChild: TIWHelpNode;
  NewName: String;
begin
  Clear;
  NewName:=Config.GetValue(CfgPath+'Name','');
  if NewName='' then exit;
  Name:=NewName;
  Path:=Config.GetValue(CfgPath+'Path','');
  HasHelp:=Config.GetValue(CfgPath+'HasHelp',false);
  IsRoot:=Config.GetValue(CfgPath+'IsRoot',false);
  NewChildCount:=Config.GetValue(CfgPath+'ChildCount',0);
  for i:=0 to NewChildCount-1 do begin
    NewChild:=AddChild('');
    NewChild.Load(Config,CfgPath+'Node'+IntToStr(i+1)+'/');
  end;
end;

procedure TIWHelpNode.Save(Config: TConfigStorage; const CfgPath: string);
var
  i: Integer;
begin
  Config.SetDeleteValue(CfgPath+'Name',Name,'');
  Config.SetDeleteValue(CfgPath+'Path',Path,'');
  Config.SetDeleteValue(CfgPath+'HasHelp',HasHelp,false);
  Config.SetDeleteValue(CfgPath+'IsRoot',IsRoot,false);
  Config.SetDeleteValue(CfgPath+'ChildCount',Count,0);
  for i:=0 to Count-1 do
    Children[i].Save(Config,CfgPath+'Node'+IntToStr(i+1)+'/');
end;

function TIWHelpNode.FindByName(const ChildName: string): TIWHelpNode;
var
  i: Integer;
begin
  for i := 0 to Count-1 do begin
    Result:=Children[i];
    if CompareText(Result.Name,ChildName)=0 then exit;
  end;
  Result:=nil;
end;

procedure TIWHelpNode.DeleteLeavesWithoutHelp;
var
  CurChild: TIWHelpNode;
  i: Integer;
begin
  for i:=Count-1 downto 0 do begin
    CurChild:=Children[i];
    CurChild.DeleteLeavesWithoutHelp;
    if (CurChild.Count=0) and (not CurChild.HasHelp) then
      CurChild.Free;
  end;
end;

function TIWHelpNode.GetFullPath: string;
var
  Node: TIWHelpNode;
  SkipTillRoot: Boolean;
begin
  Result:='';
  Node:=Self;
  SkipTillRoot:=false;
  while Node<>nil do begin
    if (Node.Parent=nil) or (not SkipTillRoot) then
      Result:=Node.Path+Result;
    if Node.IsRoot then
      SkipTillRoot:=true;
    Node:=Node.Parent;
  end;
end;

{ TIWHelpTree }

constructor TIWHelpTree.Create;
begin
  Clear;
end;

destructor TIWHelpTree.Destroy;
begin
  Clear;
  FreeAndNil(FRoot);
  inherited Destroy;
end;

procedure TIWHelpTree.Clear;
begin
  FreeAndNil(FRoot);
  FRoot:=TIWHelpNode.Create;
  Root.Name:='IDE windows and dialogs';
  Root.Path:='IDE_Window:_';
end;

procedure TIWHelpTree.Assign(Source: TIWHelpTree);
begin
  Clear;
  Root.Assign(Source.Root);
end;

procedure TIWHelpTree.Load(Config: TConfigStorage; const Path: string);
begin
  Clear;
  FRoot.Load(Config,Path);
end;

procedure TIWHelpTree.Save(Config: TConfigStorage; const Path: string);
begin
  FRoot.Save(Config,Path);
end;

function TIWHelpTree.ControlHasValidNamePath(AControl: TControl): Boolean;
begin
  if (AControl=nil) then exit(false);
  if AControl.Name='' then exit(false);
  if AControl.Parent=nil then begin
    Result:=true;
  end else begin
    Result:=ControlHasValidNamePath(AControl.Parent);
  end;
end;

function TIWHelpTree.FindNodeForControl(AControl: TControl;
  CreateIfNotExists: Boolean): TIWHelpNode;

  function Find(TheControl: TControl): TIWHelpNode;
  var
    NextParent: TWinControl;
    ParentHelpNode: TIWHelpNode;
    CurName: String;
  begin
    Result:=nil;
    //DebugLn('TIWHelpTree.FindNodeForControl.Find ',dbgsName(TheControl));
    NextParent:=TheControl.Parent;
    if NextParent=nil then begin
      CurName:=TheControl.ClassName;
      ParentHelpNode:=Root;
    end else begin
      CurName:=TheControl.Name;
      if CurName='' then exit;
      ParentHelpNode:=Find(NextParent);
      if ParentHelpNode=nil then exit;
    end;
    Result:=ParentHelpNode.FindByName(CurName);
    if (Result=nil) and CreateIfNotExists then begin
      Result:=ParentHelpNode.AddChild(CurName,CurName);
      //DebugLn('Find Create: ParentHelpNode=',ParentHelpNode.Name,' Result=',Result.Name);
    end;
  end;

begin
  Result:=Find(AControl);
end;

procedure TIWHelpTree.WriteDebugReport;

  procedure WriteNode(const Prefix: string; Node: TIWHelpNode);
  var
    i: Integer;
  begin
    if Node=nil then exit;
    DebugLn(Prefix,'Name="',Node.Name,'" Path="',Node.Path,'" HashHelp=',dbgs(Node.HasHelp));
    for i:=0 to Node.Count-1 do
      WriteNode(Prefix+'  ',Node[i]);
  end;

begin
  DebugLn('TIWHelpTree.WriteDebugReport =====================================');
  WriteNode('',Root);
end;

procedure TIWHelpTree.DeleteLeavesWithoutHelp;
begin
  Root.DeleteLeavesWithoutHelp;
end;

procedure TIWHelpTree.InvokeHelp(AControl: TControl);
var
  URL: String;
begin
  URL:=CreateURL(AControl);
  if URL='' then exit;
  ShowHelpOrError(URL,'Help for '+dbgsName(AControl),'text/html');
end;

function TIWHelpTree.CreateURL(AControl: TControl): string;
var
  HelpNode: TIWHelpNode;

  function Find(TheControl: TControl): TIWHelpNode;
  var
    NextParent: TWinControl;
    ParentHelpNode: TIWHelpNode;
    CurName: String;
  begin
    Result:=nil;
    NextParent:=TheControl.Parent;
    if NextParent=nil then begin
      CurName:=TheControl.ClassName;
      ParentHelpNode:=Root;
    end else begin
      CurName:=TheControl.Name;
      ParentHelpNode:=Find(NextParent);
      if ParentHelpNode=nil then exit;
    end;
    Result:=ParentHelpNode.FindByName(CurName);
    if (Result<>nil) and Result.HasHelp then
      HelpNode:=Result;
  end;

begin
  HelpNode:=nil;
  // search a help for this control
  Find(AControl);
  if HelpNode=nil then begin
    Result:='';
  end else begin
    Result:='http://wiki.lazarus.freepascal.org/'+HelpNode.GetFullPath;
  end;
end;

finalization
  FreeAndNil(IDEWindowHelpNodes);

end.