{
  Coco/R-file specific support (*.atg)
}

unit CocoR;

{ Global variables }
var
  CocoRItem: TMenuItem;
  CocoRItemAdd: TMenuItem;
  CocoRShowProductionsItem: TMenuItem;

const
  CocoRGotoProductionLineOffset = 3;

{ function NewItem(const ACaption: string; AShortCut: TShortCut; AChecked, AEnabled: Boolean; AOnClick: TNotifyEvent; hCtx: Word; const AName: string): TMenuItem; }

procedure InitCocoR;
var
  Empty: Integer;
  Ctrl: Integer;
begin
  CocoRItem := nil;
  CocoRItem := NewItem('&Coco/R', 0, False, True, 0, nil {ignored - always must be nil}, 'miCocoR');
  MainWindow.Menu.Items.Insert(MainWindow.Menu.Items.Count - 1, CocoRItem);
  CocoRItem.OnClick := CocoRClick;
  CocoRItem.Visible := False;

  Empty := [];
  Ctrl := [ssCtrl];

  CocoRShowProductionsItem := NewItem('Go To Production', ShortCut(ord('L'), Ctrl), False, True, 0, 0, 'miCocoRShowProductionsItem');
  CocoRShowProductionsItem.OnClick := CocoRShowProductionsItemClick;
  CocoRItem.Add(CocoRShowProductionsItem);
end;

procedure OpenCocoR(FileName: string);
begin
  CocoRItem.Visible := Cmp(Editor.HighlighterName, 'CocoR');
  CocoRItem.Enabled := CocoRItem.Visible;
end;

procedure CloseCocoR(FileName: string);
begin
  CocoRItem.Visible := False;
  CocoRItem.Enabled := CocoRItem.Visible;
end;

procedure CocoRClick(Sender: TObject);
begin
//  IniItemFind.Enabled := P > 0;
end;

procedure CocoRShowProductionsItemClick(Sender: TObject);
var
  Form: TForm;
  ListBox: TListBox;
  i: Integer;
  S: string;
  Y: Integer;
  YY: Integer;
  Save: Boolean;
begin
  // All forms was constructed in Delphi 5....
  Form := JvInterpreterMakeForm(WorkingDir + 'fIniSections.pas');
  try
    ListBox := Form.FindComponent('ListBox1');
    Y := Editor.CaretY;
    YY := -1;
    Save := False;
    for i := 0 to Editor.Lines.Count - 1 do
    begin
      S := Editor.Lines.Strings[i];
      if Cmp(S, 'PRODUCTIONS') then
        Save := True;
      if Save and (Length(S) > 2) then
        if S[1] <> ' ' then
        begin
          ListBox.Items.AddObject(S, i);
          if (YY = -1) and (Y < i) then
            YY := ListBox.Items.Count - 2;
        end;
    end;
    if YY = -1 then
      YY := ListBox.Items.Count - 1;
    ListBox.ItemIndex := YY;
    if Form.ShowModal = mrOk then
      if ListBox.ItemIndex > -1 then
      begin
        i := Integer(ListBox.Items.Objects[ListBox.ItemIndex]);
        Editor.SetLeftTop(0, i - IniGotoSectionLineOffset);
        Editor.CaretY := i + 1;
        Editor.CaretX := 0;
      end;
  finally
    Form.Free;
  end;
end;


end.
