]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/geninclude.pl.in
Accept dashes in demo titles.
[~andy/gtk] / demos / gtk-demo / geninclude.pl.in
1 #!@PERL@ -w
2
3 print <<EOT;
4 typedef GtkWidget *(*GDoDemoFunc) (GtkWidget *do_widget);
5
6 typedef struct _Demo Demo;
7
8 struct _Demo 
9 {
10   gchar *title;
11   gchar *filename;
12   GDoDemoFunc func;
13   Demo *children;
14 };
15
16 EOT
17
18 for $file (@ARGV) {
19     my %demo;
20     
21     ($basename = $file) =~ s/\.c$//;
22
23     open INFO_FILE, $file or die "Cannot open '$file'\n";
24     $title = <INFO_FILE>;
25     $title =~ s@^\s*/\*\s*@@;
26     $title =~ s@\s*$@@;
27
28     close INFO_FILE;
29
30     print "GtkWidget *do_$basename (GtkWidget *do_widget);\n";
31
32     push @demos, {"title" => $title, "file" => $file,
33                   "func"  => "do_$basename"};
34 }
35
36 # generate a list of 'parent names'
37 foreach $href (@demos) {
38     if ($href->{"title"} =~ m|^([-\w\s]+)/[-\w\s]+$|) {
39         my $parent_name = $1;
40         my $do_next = 0;
41
42         # parent detected
43         if (defined @parents) {
44             foreach $foo (@parents) {
45                 if ($foo eq $parent_name) {
46                     $do_next = 1;
47                 }
48             }
49             
50             if ($do_next) {
51                 next;
52             }
53         }
54
55         push @parents, $parent_name;
56
57         $tmp = (defined @child_arrays)?($#child_arrays + 1):0;
58         push @child_arrays, "child$tmp";
59
60         push @demos, {"title" => $parent_name, "file" => "NULL",
61                       "func" => "NULL"};
62     }
63 }
64
65 if (defined @parents) {
66     $i = 0;
67     for ($i = 0; $i <= $#parents; $i++) {
68         $first = 1;
69         
70         print "\nDemo ", $child_arrays[$i], "[] = {\n";
71         
72         $j = 0;
73         for ($j = 0; $j <= $#demos; $j++) {
74             $href = $demos[$j];
75             
76             if (!defined $demos[$j]) {
77                 next;
78             }
79             
80             if ($demos[$j]{"title"} =~ m|^$parents[$i]/([-\w\s]+)$|) {
81                 if ($first) {
82                     $first = 0;
83                 } else {
84                     print ",\n";
85                 }
86                 
87                 print qq (  { "$1", "$demos[$j]{file}", $demos[$j]{func}, NULL });
88
89                 # hack ... ugly
90                 $demos[$j]{"title"} = "foo";
91             }
92         }
93
94         print ",\n";
95         print qq (  { NULL } );
96         print "\n};\n";
97     }   
98 }
99
100 # sort @demos
101 @demos_old = @demos;
102
103 @demos = sort {
104     $a->{"title"} cmp $b->{"title"};
105 } @demos_old;
106
107 # sort the child arrays
108 if (defined @child_arrays) {
109     for ($i = 0; $i <= $#child_arrays; $i++) {
110         @foo_old = @{$child_arrays[$i]};
111
112         @{$child_arrays[$i]} = sort {
113             $a->{"title"} cmp $b->{"title"};
114         } @foo_old;
115     }
116 }
117
118 # toplevel
119 print "\nDemo testgtk_demos[] = {\n";
120
121 $first = 1;
122 foreach $href (@demos) {
123     $handled = 0;
124
125     # ugly evil hack
126     if ($href->{title} eq "foo") {
127         next;
128     }
129
130     if ($first) {
131         $first = 0;
132     } else {
133         print ", \n";
134     }
135
136     if (defined @parents) {
137         for ($i = 0; $i <= $#parents; $i++) {
138             if ($parents[$i] eq $href->{title}) {
139
140                 if ($href->{file} eq 'NULL') {
141                     print qq (  { "$href->{title}", NULL, $href->{func}, $child_arrays[$i] });
142                 } else {
143                     print qq (  { "$href->{title}", "$href->{file}", $href->{func}, $child_arrays[$i] });
144                 }
145                 
146                 $handled = 1;
147                 last;
148             }
149         }
150     }
151     
152     if ($handled) {
153         next;
154     }
155     
156     print qq (  { "$href->{title}", "$href->{file}", $href->{func}, NULL });
157 }
158
159 print ",\n";
160 print qq (  { NULL } );
161 print "\n};\n";
162
163 exit 0;