
sub plugin {
  my $global=shift;
  print "Generating cross referenced index\n";
  cross($global);
  print "Done with cross index index\n";
}

sub cross {
  my $space=shift;
  my %hash;
  my $line;
  my @defs;
  my $columns=3;

  foreach (all $space) { 
    # We don't want Friends, Typedefs, dtor, isA
    next if ($_->is_friend());
    next if ($_->is_typedef());
    next if ($_->{name} =~ /^~/);
    next if ($_->{name} =~ /^isA/);
    next if ($_->{name} =~ /^get_type/);
    next if ($_->{name} =~ /^gtkobj/);

    my $name=$_->{name};

    $hash{$name}=[] if (!$hash{$name});
    my $r=$hash{$name};
    push(@$r,$_);
  }

  foreach (sort keys %hash) {
    $line= "<dt><b>$_</b>";
    my $ref=$hash{$_};
    foreach (@$ref) {
      $line.="\n<dd>";
      $line.=$_->type_name();
      $line.=" ";
      $line.=$_->href($_->{fullname});
    }
    $line.= "\n";
    push(@defs,$line);
  }

  # split into columns
  my $length=0;
  my $num;
  my $i=1;
  my $j;
  my @array;
  my @def_length;
  foreach (@defs) {
    @array=split("\n",$_);
    push(@def_length,$#array+1);
    $length+=$#array+1;
  }
  push(@def_length,1000);
  $num=$length/$columns+2;

  my @lindex;
  my @index;
  my @start;
  my $cindex=0;
  my $end;
  my $sindex=0;
  #calculate column starts
  while ($i<$length) { 
    $start[$cindex]=$sindex;
    $end=$i+$num; 
    while($i<$end) {
      $i+=$def_length[$sindex];
      $sindex++;
    }
    $cindex++;
  }

  for ($i=0;$i<$columns;$i++) {
    $index[$i]=$start[$i];
    $lindex[$i]=0;
  }

  #print results
  open(FILE,'>html/cross_index.html');

  print FILE '
<html>
<head>
<title>ClanLib Reference: Cross Reference Index</title>
</head>
<body text=black link=blue vlink=#800080 bgcolor=white topmargin=0 leftmargin=0>
<table border=0 cellspacing=0 cellpadding=0 width=100%><tr><td><img border=0 src="../images/eyeheader-main-yellow-a.gif" alt="ClanSoft logo" width=525 height=96></td></tr>
<tr bgcolor=#dadada><td align=left valign=top><img border=0 src="../images/eyeheader-main-yellow-b.gif" alt="ClanSoft logo" width=190 height=14></td><td align=right valign=top><img border=0 src="../images/clanlib_light.gif" width=42 height=14></td></tr></table>
<table align=center border=0 cellspacing=10> <tr>
<td><a href="entire_class_index.html">Entire Class Index</a></td>
<td><a href="class_index.html">Main Class Index</a></td>
<td>Cross Index</td>
<td><a href="global_index.html">Global Index</a></td>
</table>
<h1 align=center>Cross Reference</h1>
<table border=0>
';

  for ($i=0;$i<$num+1;$i++)  {
     print FILE "<tr>";
     for ($j=0;$j<$columns;$j++) {
       next if ($i<$lindex[$j]);
       next if ($index[$j]==$start[$j+1]);
       my $dl=$def_length[$index[$j]];
       my $df=$defs[$index[$j]];
#       print FILE 
#"<td valign=\"top\" rowspan=\"${dl}\"><font size=-2>${df}</font></td>";
       print FILE 
"<td rowspan=\"${dl}\"><font size=-2>${df}</font></td>";
       $lindex[$j]+=$def_length[$index[$j]];
       $index[$j]++;
     }
     print FILE "       <td><font size=-2>&nbsp;</font></td>\n";
  }

  print FILE '
</table>
</body>
</html>
';
}
