File: apiwizard.pp

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 (938 lines) | stat: -rw-r--r-- 25,458 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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
{  $Id: apiwizard.pp 54253 2017-02-22 22:16:40Z marcus $  }
{
 /***************************************************************************
                                APIWizard.pp
                             -------------------
                   APIWiZZ is an API wizard to generate WINAPI
                   Templates for the LCL widgetsets.

                   Initial Revision  : 05-02-2000


 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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.                                   *
 *                                                                         *
 ***************************************************************************/
}
{
@author(M. Weustink <marc@dommelstein.net>)                       
@created(02-May-2000)
}
unit APIWizard;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls, ExtCtrls, LCLproc, ComCtrls,
  LazFileUtils, LazUTF8, LazUTF8Classes;

type

  { TApiWizForm }

  TApiWizForm = class(TForm)
    cmdCopy: TButton;
    cmdScan: TButton;
    cbgLCLPlatform: TCHECKGROUP;
    cmdGenerate: TButton;
    Groupbox1: TGROUPBOX;
    lvExisting: TListView;
    txtDeclarations: TMemo;
    rbIndependent: TRADIOBUTTON;
    rbDependent: TRADIOBUTTON;
    rdgApiType: TRadioGroup;
    lblDeclare: TLabel;
    txtLazarus: TEdit;
    lblLazarus: TLabel;
    procedure cmdCopyClick(Sender: TObject);
    procedure cmdGenerateClick(Sender: TObject);
    procedure ApiWizFormCreate(Sender: TObject);
    procedure cmdScanClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure rbPlatformDependencyClick(Sender: TObject);
    procedure rdgApiTypeClick (Sender: TObject );
  private
    FLineInfo: TStringList;
    procedure Clear;
  public
  end;

  TProcType = (ptFunction, ptProcedure);

  TApiWidgetset = (
    awCarbon,
    awCocoa,
    awFpgui,
    awGtk,
    awGtk2,
    awGtk3,
    awMUI,
    awNoGui,
    awQt,
    awWin32,
    awWinCE);
  TApiWidgetsets = set of TApiWidgetset;

  TApiLine = class
    Declaration: String;
    Independent: Boolean;
    WinApi: Boolean;
    Widgetsets: TApiWidgetsets;
    Base: Boolean;
  end;

var
  ApiWizForm: TApiWizForm;

implementation

{$R apiwizard.lfm}

const
  DECL_OFFSET: array[TProctype] of Integer = (9, 10);

  WS_NAME: array[TApiWidgetset] of string = (
    'carbon',
    'cocoa',
    'fpgui',
    'gtk',
    'gtk2',
    'gtk3',
    'mui',
    'nogui',
    'qt',
    'win32',
    'wince'
  );

function GetName(const ADeclaration: String): String;
var
  n, NameEnd: Integer;
begin
  Result := LowerCase(ADeclaration);
  n := Pos('function ', Result);
  if n <> 0
  then Result := Trim(Copy(ADeclaration, n + DECL_OFFSET[ptFunction], Length(ADeclaration)))
  else begin
    n := Pos('procedure ', Result);
    if n <> 0
    then Result := Trim(Copy(ADeclaration, n + DECL_OFFSET[ptProcedure], Length(ADeclaration)))
    else Result := '';
  end;

  if Result = '' then Exit;

  NameEnd := Pos('(', Result);
  if NameEnd = 0
  then NameEnd := Pos(':', Result);
  if NameEnd = 0
  then NameEnd := Pos(';', Result);
  if NameEnd = 0
  then NameEnd := Pos(' ', Result);

  n := Pos('.', Result);

  if (n > 0) and (((NameEnd > 0) and (n < NameEnd)) or (NameEnd = 0))
  then begin
    Result := Trim(Copy(Result, n + 1, Length(Result)));
    if NameEnd > 0 then Dec(NameEnd, n);
  end;
  if NameEnd > 0
  then Result := Trim(Copy(Result, 1, NameEnd - 1));

end;

function SplitDeclaration(const ADeclaration: String;
  out ProcType: TProcType; out ProcName: String; const ProcParams: TStringList): Boolean;

  function ProcessParams(Params: String): Boolean;
  var
    n: Integer;
    S, part: String;
  begin
    Result := False;
    while Params <> '' do
    begin
      n := Pos(';', Params);
      if n = 0
      then begin
        Part := Params;
        Params :=  '';
      end
      else begin
        Part := Copy(Params, 1, n - 1);
        Params := Trim(Copy(Params, n + 1, Length(Params)));
        if Params = '' then Exit;
      end;
      n := Pos(':', Part);
      if n <> 0 then Part := Copy(Part, 1, n - 1);
      Part := Trim(Part);

      if (LowerCase(Copy(Part, 1, 4)) = 'var ')
      then Part := Trim(Copy(Part, 5, Length(Part)))
      else if (LowerCase(Copy(Part, 1, 4)) = 'out ')
      then Part := Trim(Copy(Part, 5, Length(Part)))
      else if (LowerCase(Copy(Part, 1, 6)) = 'const ')
      then Part := Trim(Copy(Part, 7, Length(Part)));

      repeat
        n := Pos(',', Part);
        if n = 0
        then begin
          S := Part;
          Part := '';
        end
        else begin
          S := Trim(Copy(Part, 1, n - 1));
          Part := Trim(Copy(Part, n + 1, Length(Part)));
        end;
        if S = ''
        then Exit
        else ProcParams.Add(S);

      until Part = '';
    end;
    Result := True;
  end;
var
  ParamStart, ParamEnd, ParamLen: Integer;
  S: String;
begin
  ProcParams.Clear;

  Result := False;

  ProcName := GetName(ADeclaration);
  if ProcName = '' then Exit;

  S := LowerCase(Trim(Copy(ADeclaration, 1, Pos(ProcName, ADeclaration) - 2)));
  if S = 'function'
  then ProcType := ptFunction
  else
    if S = 'procedure'
    then ProcType := ptProcedure
    else Exit;


  ParamStart := Pos('(', ADeclaration);
  ParamEnd := Pos(')', ADeclaration);
  if (ParamStart <> 0)
  and (ParamEnd <> 0)
  then ParamLen := ParamEnd - ParamStart - 1
  else ParamLen := 0;

  if ((ParamStart <> 0) <> (ParamEnd <> 0))
  or (ParamLen < 0)
  then Exit;

  if (ParamLen > 0)
  then Result := ProcessParams(Trim(Copy(ADeclaration, ParamStart + 1, ParamLen)))
  else Result := True;
end;

function FindPrevious(const Lines: TStringList; const AStart: Integer; const ATag, AFind: String): Integer;
var
  StartTag: String;
  InsertFound: Boolean;
begin
  InsertFound := False;
  StartTag := Format('##apiwiz##s%s##', [ATag]);
  for Result := AStart downto 0 do
  begin
    InsertFound := (Pos(AFind, Lines[Result]) <> 0) or (Pos(StartTag, Lines[Result]) <> 0);
    if InsertFound then Break;
  end;
  if InsertFound
  then Inc(Result)
  else Result := -1
end;

function FindPreviousNonBlank(const Lines: TStringList; const AStart: Integer; const ATag: String): Integer;
var
  StartTag: String;
  InsertFound: Boolean;
begin
  InsertFound := False;
  StartTag := Format('##apiwiz##s%s##', [ATag]);
  for Result := AStart downto 0 do
  begin
    InsertFound := (Lines[Result] <> '') or (Pos(StartTag, Lines[Result]) <> 0);
    if InsertFound then Break;
  end;
  if InsertFound
  then Inc(Result)
  else Result := -1
end;

function FindInsertPoint(const Lines: TStringList; const Tag, ProcName, FileName: String; const IsHeader: Boolean): Integer;
var
  StartFound, InsertFound: Boolean;
  StartTag, EndTag: String;
  Line, SearchName, LineName: String;
  n: Integer;
begin

  StartFound := False;
  InsertFound := False;
  StartTag := Format('##apiwiz##s%s##', [Tag]);
  EndTag   := Format('##apiwiz##e%s##', [Tag]);
  SearchName := LowerCase(ProcName);

  for Result := 0 to Lines.Count - 1 do
  begin
    Line := Lines[Result];
    if StartFound
    then begin
      if Pos(EndTag, Line) <> 0
      then begin
        LineName := '';
        InsertFound := True;
        Break;
      end;

      if (Line = '') or (Line[1] = ' ') then Continue;
      LineName := GetName(Line);
      if LineName = '' then Continue;

      n := CompareText(SearchName, LineName);
      if n = 0
      then begin
        ShowMessageFmt('Function exists in [%s]', [FileName]);
        Break;
      end
      else if n < 0
      then begin
        InsertFound := True;
        Break;
      end;
    end
    else begin
      StartFound := Pos(StartTag, Line) <> 0;
      Continue;
    end;
  end;

  if InsertFound
  then begin
    if IsHeader
    then begin
      if (LineName <> '')
      and (LowerCase(LineName[1]) <> LowerCase(Procname[1]))
      then Result := FindPreviousNonBlank(Lines, Result - 1, Tag);
    end
    else begin
      Result := FindPrevious(Lines, Result - 1, Tag, 'end;');
    end;
  end
  else Result := -1
end;

procedure InsertLines(const Idx: Integer; const TargetLines, SourceLines: TStringList);
var
  n: Integer;
begin
  for n := SourceLines.Count - 1 downto 0 do
  begin
    TargetLines.Insert(Idx, SourceLines[n]);
  end;
end;

procedure Scan(const AFileName, ATag: String; AResult: TStringList);
var
  StartFound: Boolean;
  StartTag, EndTag: String;
  Line, LineName: String;
  Lines: TStringListUTF8;
  n: Integer;
begin
  AResult.Clear;
  if not FileExistsUTF8(AFileName) then Exit;

  Lines := TStringListUTF8.Create;
  Lines.LoadFromFile(AFileName);

  StartFound := False;
  StartTag := Format('##apiwiz##s%s##', [ATag]);
  EndTag   := Format('##apiwiz##e%s##', [ATag]);

  for n := 0 to Lines.Count - 1 do
  begin
    Line := Lines[n];
    if Length(Line) < 8 then Continue; // min lenght of function,procedure,tag

    if not StartFound
    then begin
      StartFound := Pos(StartTag, Line) <> 0;
      Continue;
    end;

    if Pos(EndTag, Line) <> 0
    then begin
      Break;
    end;

    if Line[1] = ' ' then Continue;
    if (Line[1] = '/') and (Line[2] = '/') then Continue;

    LineName := GetName(Line);
    if LineName = '' then Continue;
    Line := GetPart([], ['{$'], Line);

    AResult.Add(line);
  end;

  Lines.Free;
end;

{ TApiWizForm }

procedure TApiWizForm.ApiWizFormCreate(Sender: TObject);
var
  n: Integer;
  S: String;
  WS: TApiWidgetset;
  C: TListColumn;
begin
  cbgLCLPlatform.Items.Clear;
  for WS := Low(WS) to High(Ws) do
    cbgLCLPlatform.Items.Add(WS_NAME[WS]);

  lvExisting.Columns.BeginUpdate;
  for WS := Low(WS) to High(Ws) do
  begin
    C := lvExisting.Columns.Add;
    C.Caption := WS_NAME[WS];
    C.Alignment := taCenter;
    C.AutoSize := True;
  end;
  // add dummy
  C := lvExisting.Columns.Add;
  C.Caption := '';
  C.AutoSize := True;
  lvExisting.Columns.EndUpdate;

  FLineInfo := TStringList.Create;
  FLineInfo.Sorted := True;
  FLineInfo.Duplicates := dupIgnore;
  FLineInfo.CaseSensitive := False;

  S := ParamStrUTF8(1);
  // find the lazarus dir
  if S = ''
  then begin
    S := ExtractFilePath(ParamStrUTF8(0));
    n := Pos('apiwizz', S);
    if n <> 0
    then S := Copy(S, 1, n - 7)
    else S := '';
  end;

  if S = ''
  then begin
    S := GetCurrentDirUTF8;
    n := Pos('apiwizz', S);
    if n <> 0 then S := Copy(S, 1, n - 7);
  end;
  txtLazarus.Text := S;
end;

procedure TApiWizForm.Clear;
var
  n: Integer;
begin
  lvExisting.Clear;
  for n := 0 to FLineInfo.Count - 1 do
    FLineInfo.Objects[n].Free;

  FLineInfo.Clear;
end;

procedure TApiWizForm.cmdCopyClick(Sender: TObject);
var
  Line: TApiLine;
  WS: TApiWidgetset;
begin
  if lvExisting.Selected = nil then Exit;

  Line := TApiLine(lvExisting.Selected.Data);
  if Line = nil then Exit; //???

  if txtDeclarations.Lines.Count = 0
  then begin
    txtDeclarations.Lines.Add(Line.Declaration);

    if Line.WinApi
    then rdgApiType.ItemIndex := 0
    else rdgApiType.ItemIndex := 1;

    rbIndependent.Checked := Line.Independent;
    rbDependent.Checked := not Line.Independent;

    for WS := Low(WS) to High(Ws) do
    begin
      cbgLCLPlatform.Checked[Ord(WS)] := not(WS in Line.Widgetsets);
    end;
    Exit;
  end;

  // Check match
  if Line.WinApi <> (rdgApiType.ItemIndex = 0)
  then begin
    ShowMessage('API type mismatch');
    Exit;
  end;

  if rbIndependent.Checked <> Line.Independent
  then begin
    ShowMessage('Dependency mismatch');
    Exit;
  end;

  txtDeclarations.Lines.Add(Line.Declaration);
  for WS := Low(WS) to High(Ws) do
  begin
    if (WS in Line.Widgetsets) then Continue;
    cbgLCLPlatform.Checked[Ord(WS)] := True;
  end;
end;

procedure TApiWizForm.cmdGenerateClick(Sender: TObject);
const
  RETURN_PROC: array[TProcType] of String = ('',  ' Nothing');
  PROC_DESC: array[TProcType] of String = ('Function',  'Procedure');
  PROC_RESULT: array[TProcType] of String = ('Result := ',  '');
var
  ApiText, ProcLines, ProcParams: TStringListUTF8;
  S, DeclarationText: String;
  ProcName, FileName, IntfBase: String;
  PlatformPrefix, PlatformDir, PlatformObject: String;
  Line, LineCount, n, Idx: Integer;
  ProcType: TProcType;
  WS: TApiWidgetset;

  procedure CreateLeadingCR;
  var
    LineName: String;
  begin
    // create leading CR if needed
    LineName := GetName(ApiText[Idx - 1]);
    if ((LineName <> '') and (LowerCase(ProcName)[1] <> LowerCase(LineName)[1]))
    or (Pos('##apiwiz##', ApiText[Idx]) <> 0)
    then ProcLines.Add('');
  end;
begin
  if txtDeclarations.Lines.Count = 0
  then begin
    ShowMessage('No declaration specified');
    Exit;
  end;

  case rdgApiType.ItemIndex of
    0: begin
      FileName := 'winapi';
      IntfBase := 'intfbasewinapi.inc';
    end;
    1: begin
      FileName := 'lclintf';
      IntfBase := 'intfbaselcl.inc';
    end;
  else
    ShowMessage('No API type selected');
    Exit;
  end;

  LineCount := 0;
  ProcParams := TStringListUTF8.Create;
  ApiText := TStringListUTF8.Create;
  ProcLines := TStringListUTF8.Create;
  try
    for Line := 0 to txtDeclarations.Lines.Count - 1 do
    begin
      DeclarationText := Trim(txtDeclarations.Lines[Line]);

      while (Length(DeclarationText) > 0) and (DeclarationText[Length(DeclarationText)] in [';', ' ']) do
      begin
        Delete(DeclarationText, Length(DeclarationText), 1);
      end;

      if Length(DeclarationText) = 0 then Continue;

      if not SplitDeclaration(DeclarationText, ProcType, ProcName, ProcParams)
      then begin
        if MessageDlg(Format('Line %d, bad formatted declaration: %s', [Line + 1, DeclarationText]),
                   mtError, [mbIgnore, mbAbort], 0) = mrIgnore then Continue;
        Exit;
      end;
      Inc(LineCount);

      //--------------------------------
      // open winapih.inc / lclintfh.inc
      //--------------------------------
      ApiText.LoadFromFile(txtLazarus.text + '/lcl/include/' + FileName + 'h.inc');
      Idx := FindInsertPoint(ApiText, 'ps', ProcName, FileName + 'h.inc', True);
      if Idx <> -1
      then begin
        ProcLines.Clear;
        CreateLeadingCR;
        if rbIndependent.Checked
        then ProcLines.Add(Format('//%s %s --> independent', [LowerCase(PROC_DESC[proctype]), procname]))
        else ProcLines.Add(DeclarationText + '; {$IFDEF IF_BASE_MEMBER}virtual;{$ENDIF}');

        InsertLines(Idx, ApiText, ProcLines);

        if rbIndependent.Checked
        then begin
          Idx := FindInsertPoint(ApiText, 'pi', ProcName, FileName + 'h.inc', True);
          if Idx <> -1
          then begin
            ProcLines.Clear;
            CreateLeadingCR;
            ProcLines.Add(DeclarationText + ';');
            InsertLines(Idx, ApiText, ProcLines);
          end;
        end;
        ApiText.SaveToFile(txtLazarus.text + '/lcl/include/' + FileName + 'h.inc');
      end;
      //------------------------------
      // open winapi.inc / lclintf.inc
      //------------------------------
      ApiText.LoadFromFile(txtLazarus.text + '/lcl/include/' + FileName + '.inc');
      if rbIndependent.Checked
      then begin
        Idx := FindInsertPoint(ApiText, 'pi', ProcName, FileName + '.inc', False);
        if Idx <> -1
        then begin
          with ProcLines do
          begin
            Clear;
            Add('');
            Add('{------------------------------------------------------------------------------');
            Add('  ' + PROC_DESC[ProcType] + ': ' + ProcName);
            if ProcParams.Count = 0
            then Add('  Params: none' )
            else begin
              Add('  Params: ' + ProcParams[0] + ':');
              for n := 1 to ProcParams.Count  - 1  do
                Add('          ' + ProcParams[n] + ':');
            end;
            Add('  Returns:' + RETURN_PROC[ProcType]);
            Add('');
            Add(' ------------------------------------------------------------------------------}');
            Add(DeclarationText + ';');
            Add('begin');
            Add('//TODO:'+ProcName);
            Add('  // your code here');
            Add('end;');
          end;
        end;
      end
      else begin
        Idx := FindInsertPoint(ApiText, 'ps', ProcName, FileName + '.inc', False);
        if Idx <> -1
        then begin

          if ProcParams.Count = 0
          then S := ''
          else begin
            S := '(' + ProcParams[0];
            for n := 1 to ProcParams.Count - 1 do
              S := S + ', ' + ProcParams[n];
            S := S + ')'
          end;

          with ProcLines do
          begin
            Clear;
            Add('');
            Add(DeclarationText + ';');
            Add('begin');
            Add('  ' + PROC_RESULT[ProcType] + 'WidgetSet.' + ProcName + S + ';');
            Add('end;');
          end;
        end;
      end;
      if Idx <> -1
      then begin
        InsertLines(Idx, ApiText, ProcLines);
        ApiText.SaveToFile(txtLazarus.text + '/lcl/include/' + FileName + '.inc');
      end;

      // ++++++++++++++++++++++++++++++ //
      // from here only dependent stuff //
      // ++++++++++++++++++++++++++++++ //
      if rbDependent.Checked
      then begin
        //------------------------------------------
        // open intfbasewinapi.inc / intfbaselcl.inc
        //------------------------------------------
        ApiText.LoadFromFile(txtLazarus.text + '/lcl/include/' + IntfBase);
        Idx := FindInsertPoint(ApiText, 'ps', ProcName, IntfBase, False);
        if Idx <> -1
        then begin
          S := DeclarationText;
          // Remove spaces
          while S[DECL_OFFSET[ProcType]] = ' ' do Delete(S, DECL_OFFSET[ProcType], 1);

          System.Insert(' TWidgetSet.', S, DECL_OFFSET[ProcType]);
          with ProcLines do
          begin
            Clear;
            Add('');
            Add(S + ';');
            Add('begin');
            if ProcType = ptFunction
            then begin
              Add('  // Your default here');
              Add('  // Result := ');
            end;
            Add('end;');
          end;
          InsertLines(Idx, ApiText, ProcLines);
          ApiText.SaveToFile(txtLazarus.text + '/lcl/include/' + IntfBase);
        end;

        for WS := Low(WS) to High(Ws) do
        begin
          if not cbgLCLPlatform.Checked[Ord(WS)] then Continue;

          PlatformPrefix := WS_NAME[WS];
          PlatformDir := WS_NAME[WS];
          PlatformObject := 'T' + WS_NAME[WS] + 'WidgetSet';
          PlatformObject[2] := UpCase(PlatformObject[2]);

          //------------------
          // open *winapih.inc
          //------------------
          ApiText.LoadFromFile(txtLazarus.text + '/lcl/interfaces/' + PlatformDir + '/' + PlatformPrefix + FileName + 'h.inc');
          Idx := FindInsertPoint(ApiText, 'ps', ProcName, PlatformPrefix + FileName + 'h.inc', True);
          if IDX <> -1
          then begin
          	ProcLines.Clear;
            CreateLeadingCR;
            ProcLines.Add(DeclarationText + '; override;');
            InsertLines(Idx, ApiText, ProcLines);
            ApiText.SaveToFile(txtLazarus.text + '/lcl/interfaces/' + PlatformDir + '/' + PlatformPrefix + FileName + 'h.inc');
          end;

          //-----------------
          // open *winapi.inc
          //-----------------
          ApiText.LoadFromFile(txtLazarus.text + '/lcl/interfaces/' + PlatformDir + '/' + PlatformPrefix + FileName + '.inc');
          Idx := FindInsertPoint(ApiText, 'ps', ProcName, PlatformPrefix + FileName + '.inc', False);
          if Idx <> -1
          then begin
            S := DeclarationText;
            // Remove spaces
            while S[DECL_OFFSET[ProcType]] = ' ' do Delete(S, DECL_OFFSET[ProcType], 1);

            System.Insert(' ' + PlatformObject + '.', S, DECL_OFFSET[ProcType]);
            with ProcLines do
            begin
              Clear;
              Add('');
              Add('{------------------------------------------------------------------------------' );
              Add('  ' + PROC_DESC[ProcType] + ': ' + ProcName );
              if ProcParams.Count = 0
              then Add('  Params: none' )
              else begin
                Add('  Params: ' + ProcParams[0] + ':');
                for n := 1 to ProcParams.Count  - 1  do
                  Add('          ' + ProcParams[n] + ':');
              end;
              Add('  Returns:' + RETURN_PROC[ProcType] );
              Add('' );
              Add(' ------------------------------------------------------------------------------}' );
              Add(S + ';');
              Add('begin' );
              Add('  // Your code here' );
              Add('end;');
            end;
            InsertLines(Idx, ApiText, ProcLines);
            ApiText.SaveToFile(txtLazarus.text + '/lcl/interfaces/' + PlatformDir + '/' + PlatformPrefix + FileName + '.inc');
          end;
        end;
      end;
    end;
  finally
    ApiText.Free;
    ProcLines.Free;
    ProcParams.Free;

    ShowMessage(Format('%d lines inserted', [LineCount]));
  end;
end;

procedure TApiWizForm.cmdScanClick(Sender: TObject);
var
  Lines: TStringlist;
  S: String;
  n, idx: Integer;
  Line: TApiLine;
  Item: TListItem;
  WS: TApiWidgetset;
begin
  Screen.Cursor := crHourGlass;
  lvExisting.BeginUpdate;
  Clear;

  Lines := TStringList.Create;
  Scan(txtLazarus.text + '/lcl/include/winapih.inc', 'ps', Lines);
  for n := 0 to Lines.Count - 1 do
  begin
    Line := TApiLine.Create;
    Line.Declaration := Lines[n];
    Line.WinApi := True;
    S := GetName(Lines[n]);
    FLineInfo.AddObject(S, Line);
  end;

  Scan(txtLazarus.text + '/lcl/include/winapih.inc', 'pi', Lines);
  for n := 0 to Lines.Count - 1 do
  begin
    S := GetName(Lines[n]);
    if FLineInfo.IndexOf(s) >= 0 then Continue;  //overloaded

    Line := TApiLine.Create;
    Line.Independent := True;
    Line.WinApi := True;
    Line.Declaration := Lines[n];
    FLineInfo.AddObject(S, Line);
  end;

  Scan(txtLazarus.text + '/lcl/include/lclintfh.inc', 'ps', Lines);
  for n := 0 to Lines.Count - 1 do
  begin
    Line := TApiLine.Create;
    Line.Declaration := Lines[n];
    S := GetName(Lines[n]);
    FLineInfo.AddObject(S, Line);
  end;

  Scan(txtLazarus.text + '/lcl/include/lclintfh.inc', 'pi', Lines);
  for n := 0 to Lines.Count - 1 do
  begin
    S := GetName(Lines[n]);
    if FLineInfo.IndexOf(s) >= 0 then Continue;  //overloaded

    Line := TApiLine.Create;
    Line.Independent := True;
    Line.Declaration := Lines[n];
    FLineInfo.AddObject(S, Line);
  end;

  // implementations

  Scan(txtLazarus.text + '/lcl/include/intfbasewinapi.inc', 'ps', Lines);
  for n := 0 to Lines.Count - 1 do
  begin
    S := GetName(Lines[n]);
    idx := FLineInfo.IndexOf(S);
    if idx = -1 then Continue;

    Line := TApiLine(FLineInfo.Objects[idx]);
    Line.Base := True;
  end;

  Scan(txtLazarus.text + '/lcl/include/intfbaselcl.inc', 'ps', Lines);
  for n := 0 to Lines.Count - 1 do
  begin
    S := GetName(Lines[n]);
    idx := FLineInfo.IndexOf(S);
    if idx = -1 then Continue;

    Line := TApiLine(FLineInfo.Objects[idx]);
    Line.Base := True;
  end;

  for WS := Low(WS) to High(Ws) do
  begin
    Scan(txtLazarus.text + '/lcl/interfaces/' + WS_NAME[WS] + '/' +  WS_NAME[WS] + 'winapih.inc', 'ps', Lines);
    for n := 0 to Lines.Count - 1 do
    begin
      S := GetName(Lines[n]);
      idx := FLineInfo.IndexOf(S);
      if idx = -1 then Continue;

      Line := TApiLine(FLineInfo.Objects[idx]);
      Include(Line.Widgetsets, WS);
    end;

    Scan(txtLazarus.text + '/lcl/interfaces/' + WS_NAME[WS] + '/' +  WS_NAME[WS] + 'lclintfh.inc', 'ps', Lines);
    for n := 0 to Lines.Count - 1 do
    begin
      S := GetName(Lines[n]);
      idx := FLineInfo.IndexOf(S);
      if idx = -1 then Continue;

      Line := TApiLine(FLineInfo.Objects[idx]);
      Include(Line.Widgetsets, WS);
    end;
  end;


  for n := 0 to FLineInfo.Count - 1 do
  begin
    Item := lvExisting.Items.Add;
    Item.Caption := FLineInfo[n];
    Line := TApiLine(FLineInfo.Objects[n]);
    Item.Data := Line;

    if Line.WinApi
    then Item.SubItems.Add('Win')
    else Item.SubItems.Add('LCL');

    if Line.Independent
    then Item.SubItems.Add('X')
    else Item.SubItems.Add('');

    if Line.Base
    then Item.SubItems.Add('X')
    else begin
      if Line.Independent
      then Item.SubItems.Add('-')
      else Item.SubItems.Add('');
    end;

    for WS := Low(WS) to High(Ws) do
    begin
      if WS in Line.Widgetsets
      then Item.SubItems.Add('X')
      else begin
        if Line.Independent
        then Item.SubItems.Add('-')
        else Item.SubItems.Add('');
      end;
    end;
  end;

  lvExisting.EndUpdate;

  Lines.Free;

  Screen.Cursor := crDefault;
end;

procedure TApiWizForm.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FLineInfo);
end;

procedure TApiWizForm.rbPlatformDependencyClick (Sender: TObject );
begin
  cbgLCLPlatform.Enabled := rbDependent.Checked;
end;

procedure TApiWizForm.rdgApiTypeClick (Sender: TObject );
begin

end;

end.