#!/usr/bin/perl -w

use strict;

print "(*  (C)2000 by Tjabo Kloppenburg <tjabo\@unix-ag.org> *)\n";
print "(*  -+- This is the glade_c to FreePascal Converter -+-*)\n";

if ($#ARGV != 0) {
  print "Usage: gc2pas /glade/project/src/callbacks.c >testapp.pas\n";
  print "       gi2pas /glade/project/src/interface.c >>testapp.pas\n";
  print "       ppc386 testapp.pas\n";
  exit;
}


my $infile = $ARGV[0];
my $firstfuncname = "";

# --------------------------------------------------------------------------

print "(* Pass 1... *)\n";
my $firstwidget = "";
my $emptylines = 0;

open(INFILE, "<$infile") || die "Infile $infile nicht lesbar!";
my ($zeile,$dummy);
$zeile = <INFILE>;
$zeile = <INFILE>;
$zeile = <INFILE>;
my $is_var = 0;

while($zeile = <INFILE>) {
  chop($zeile);
  if ($zeile =~ /^GtkWidget\*$/) {
    my $funcname = <INFILE>;
    chop($funcname);
    $funcname =~ /^([^\ ]+)\ .*$/;
    if (defined($1)) {
      $funcname = $1;
      $firstfuncname = $firstfuncname ? $firstfuncname : $funcname; # for dummy code
      print "function $funcname : pGtkWidget;\n";
      print "var\n";
    }
    else {
      print "Fehler beim function-Kopf.:-(\n";
    }
  }

  elsif ($zeile =~ /^\{$/) {

  }
  elsif ($zeile =~ /^\}$/) {
  }

  elsif ($zeile =~ /^\ *return ([^;]+);$/) {
    print "  result := $1;\n";
    print "end;\n";
  }

  # Variablen:
  elsif ( ($zeile =~ /^\ *GtkWidget\ \*(.*);$/) or ($zeile =~ /^\ *GtkAccelGroup\ \*(.*);$/)) {
    my $varname = $1;
    if ($zeile =~ /accel_group/) {
      print "  " . LString($varname,20) . " : pGtkAccelGroup;\n";
    }
    else {
      print "  " . LString($varname,20) . " : pGtkWidget;\n";
    }
    $is_var = 1;
  }

  elsif ($zeile =~ /^#(.*)$/) {
   #    print "# $1\n";
  }

  else {

    if ($is_var == 1) {
      $is_var = 0;
      print "begin\n";
    }

    if ($zeile =~ /^\ *$/) {
      $emptylines += 1;
      if ($emptylines < 2) {
        print "\n";
      }
    }
    else {
      $emptylines = 0;
      $zeile =~ s/->/\./g;
      $zeile =~ s/\"/\'/g;
      $zeile =~ s/\ =\ /\ :=\ /g;
      $zeile =~ s/NULL/NIL/g;

      if ($zeile =~ /gtk_object_set_data_full/) {
        $zeile = <INFILE>; # 2 zeilen nicht ausgeben
      }
      elsif ($zeile =~ /gtk_widget_ref/) {
      }
      else {

        if ($zeile =~ /gtk_progress_set_show_text/) {
          $zeile =~ s/true/1/gi;
          $zeile =~ s/false/0/gi;
        }
        if ($zeile =~ /(\ *GTK_SIGNAL_FUNC\ *\()([^\)]+)(.*)$/) {
          $zeile = "$1\@$2$3";
        }

        if ($zeile =~ /gtk_menu_ensure_uline_accel_group/) {
          $zeile = "\n  { Doesn''t work: $zeile }\n\n";
        }

        if ($zeile =~ /gtk_widget_set_usize\ \(label/) {
          $zeile = "\n  { GLADE-BUG: $zeile }\n\n";
        }

        print "$zeile\n";
      }
    }

  }
}
close(INFILE);

print "\n";
print "(* Now some code to test the function: *)\n\n";
print "var window : pGtkWidget;\n";
print "begin\n";
print "  gtk_init (\@argc, \@argv);\n";
print "\n";
print "  window := $firstfuncname;\n";
print "  gtk_widget_show(window);\n";
print "\n";
print "  gtk_main();\n";
print "\n";
print "end.\n";

exit;
# -----------------------------------------------------------------------------

sub LString {
  my ($s1, $len) = @_;
  $s1 = sprintf("%" . $len . "s", $s1);
  return $s1;
}

