Typo.
[doldaconnect.git] / clients / gtk2 / makegdesc
1 #!/usr/bin/perl
2
3 $tempvar = 0;
4
5 sub printwidgets
6 {
7     my($widget, $sl, $p, $sig, $cb, $data, $pf, $cpf, $mod, $key, @delayedlines);
8     $sl = $_[1];
9     $p = "    " . (" " x $sl);
10     $cpf = $_[2];
11     @delayedlines = ();
12     foreach $widget (@{$_[0]})
13     {
14         if($widget->{"type"} eq "wnd")
15         {
16             print "${p}stack[$sl] = gtk_window_new(GTK_WINDOW_TOPLEVEL);\n";
17             if($options{"hasaccels"}) {
18                 print "${p}gtk_window_add_accel_group(GTK_WINDOW(stack[$sl]), accel_group);\n";
19             }
20             if($widget->{"title"}) {
21                 print "${p}gtk_window_set_title(GTK_WINDOW(stack[$sl]), \"" . $widget->{"title"} . "\");\n";
22             }
23             $pf = sub
24             {
25                 my($widget, $p, $sl) = @_;
26                 print "${p}gtk_container_add(GTK_CONTAINER(stack[" . ($sl - 1) . "]), stack[$sl]);\n";
27             }
28         } elsif($widget->{"type"} =~ /[hv]box/) {
29             print "${p}stack[$sl] = gtk_" . $widget->{"type"} . "_new(";
30             print $widget->{"homo"}?"TRUE, ":"FALSE, ";
31             print $widget->{"spacing"} || "0";
32             print ");\n";
33             $pf = sub
34             {
35                 my($widget, $p, $sl) = @_;
36                 print "${p}gtk_box_pack_start(GTK_BOX(stack[" . ($sl - 1) . "]), stack[$sl], ";
37                 print (($widget->{"expand"} || $widget->{"parent"}->{"dexpand"})?"TRUE, ":"FALSE, ");
38                 print (($widget->{"fill"} || $widget->{"parent"}->{"dfill"})?"TRUE, ":"FALSE, ");
39                 print $widget->{"pad"} || "0";
40                 print ");\n";
41             }
42         } elsif($widget->{"type"} eq "table") {
43             print "${p}stack[$sl] = gtk_table_new(" . $widget->{"rows"} . ", " . $widget->{"cols"};
44             print ", " . (($widget->{"homo"} eq "TRUE")?"TRUE":"FALSE");
45             print ");\n";
46             $pf = sub
47             {
48                 my($widget, $p, $sl) = @_;
49                 print "${p}gtk_table_attach(GTK_TABLE(stack[" . ($sl - 1) . "]), stack[$sl]";
50                 print ", " . $widget->{"tx"};
51                 print ", " . ($widget->{"tx"} + (defined($widget->{"tw"})?$widget->{"tw"}:1));
52                 print ", " . $widget->{"ty"};
53                 print ", " . ($widget->{"ty"} + (defined($widget->{"th"})?$widget->{"th"}:1));
54                 if($widget->{"fill"} eq "y") {
55                     $widget->{"fillx"} = "y";
56                     $widget->{"filly"} = "y";
57                 }
58                 if($widget->{"shrink"} eq "y") {
59                     $widget->{"shrinkx"} = "y";
60                     $widget->{"shrinky"} = "y";
61                 }
62                 if($widget->{"expand"} eq "y") {
63                     $widget->{"expandx"} = "y";
64                     $widget->{"expandy"} = "y";
65                 }
66                 print ", 0";
67                 print " | GTK_FILL" if $widget->{"fillx"} eq "y";
68                 print " | GTK_SHRINK" if $widget->{"shrinkx"} eq "y";
69                 print " | GTK_EXPAND" if $widget->{"expandx"} eq "y";
70                 print ", 0";
71                 print " | GTK_FILL" if $widget->{"filly"} eq "y";
72                 print " | GTK_SHRINK" if $widget->{"shrinky"} eq "y";
73                 print " | GTK_EXPAND" if $widget->{"expandy"} eq "y";
74                 print ", " . (defined($widget->{"padx"})?$widget->{"padx"}:"0");
75                 print ", " . (defined($widget->{"pady"})?$widget->{"pady"}:"0");
76                 print ");\n";
77             }
78         } elsif($widget->{"type"} eq "btn") {
79             $widget->{"label"} || die("Can't have button without label\n");
80             print "${p}stack[$sl] = gtk_button_new_with_mnemonic(_(\"" . $widget->{"label"} . "\"));\n";
81         } elsif($widget->{"type"} eq "chk") {
82             $widget->{"label"} || die("Can't have check button without label\n");
83             print "${p}stack[$sl] = gtk_check_button_new_with_mnemonic(_(\"" . $widget->{"label"} . "\"));\n";
84         } elsif($widget->{"type"} eq "sbtn") {
85             $widget->{"stock"} || die("Can't have button without stock\n");
86             print "${p}stack[$sl] = gtk_button_new_from_stock(GTK_STOCK_" . $widget->{"stock"} . ");\n";
87         } elsif($widget->{"type"} eq "simg") {
88             $widget->{"stock"} || die("Can't have image without stock\n");
89             $widget->{"size"} || die("Can't have image without size\n");
90             print "${p}stack[$sl] = gtk_image_new_from_stock(GTK_STOCK_" . $widget->{"stock"} . ", GTK_ICON_SIZE_" . $widget->{"size"} . ");\n";
91         } elsif($widget->{"type"} eq "lbl") {
92             $widget->{"label"} || die("Can't have label without label\n");
93             print "${p}stack[$sl] = gtk_label_new(_(\"" . $widget->{"label"} . "\"));\n";
94         } elsif($widget->{"type"} eq "mlbl") {
95             $widget->{"label"} || die("Can't have label without label\n");
96             print "${p}stack[$sl] = gtk_label_new_with_mnemonic(_(\"" . $widget->{"label"} . "\"));\n";
97             if(defined($widget->{"mwidget"}))
98             {
99                 if($widget->{"var"} ne "y") {
100                     $widget->{"var"} = "l";
101                 }
102                 if(!defined($widget->{"name"})) {
103                     $widget->{"name"} = "temp" . $tempvar++;
104                 }
105                 $str = "gtk_label_set_mnemonic_widget(GTK_LABEL(";
106                 if($widget->{"var"} eq "y") {
107                     $str .= $options{"prefix"};
108                 }
109                 $str .= $widget->{"name"};
110                 $str .= "), " . $options{"prefix"} . $widget->{"mwidget"} . ");";
111                 push @delayedlines, ($str);
112             }
113         } elsif($widget->{"type"} eq "text") {
114             print "${p}stack[$sl] = gtk_entry_new();\n";
115             if($widget->{"default"}) {
116                 print "${p}gtk_entry_set_text(GTK_ENTRY(stack[$sl]), \"" . $widget->{"default"} . "\");\n";
117             }
118         } elsif($widget->{"type"} eq "menubar") {
119             print "${p}stack[$sl] = gtk_menu_bar_new();\n";
120             $pf = sub
121             {
122                 my($widget, $p, $sl) = @_;
123                 print "${p}gtk_menu_shell_append(GTK_MENU_SHELL(stack[" . ($sl - 1) . "]), stack[$sl]);\n";
124             }
125         } elsif($widget->{"type"} eq "menuitem") {
126             print "${p}stack[$sl] = gtk_menu_item_new_with_mnemonic(_(\"" . $widget->{"label"} . "\"));\n";
127             $pf = sub
128             {
129                 my($widget, $p, $sl) = @_;
130                 print "${p}gtk_menu_item_set_submenu(GTK_MENU_ITEM(stack[" . ($sl - 1) . "]), stack[$sl]);\n";
131             }
132         } elsif($widget->{"type"} eq "smenuitem") {
133             print "${p}stack[$sl] = gtk_image_menu_item_new_from_stock(GTK_STOCK_" . $widget->{"stock"} . ", accel_group);\n";
134             $pf = sub
135             {
136                 my($widget, $p, $sl) = @_;
137                 print "${p}gtk_menu_item_set_submenu(GTK_MENU_ITEM(stack[" . ($sl - 1) . "]), stack[$sl]);\n";
138             }
139         } elsif($widget->{"type"} eq "menusep") {
140             print "${p}stack[$sl] = gtk_separator_menu_item_new();\n";
141         } elsif($widget->{"type"} eq "menu") {
142             print "${p}stack[$sl] = gtk_menu_new();\n";
143             if($options{"hasaccels"}) {
144                 print "${p}gtk_menu_set_accel_group(GTK_MENU(stack[$sl]), accel_group);\n";
145             }
146             $pf = sub
147             {
148                 my($widget, $p, $sl) = @_;
149                 print "${p}gtk_menu_shell_append(GTK_MENU_SHELL(stack[" . ($sl - 1) . "]), stack[$sl]);\n";
150             };
151             $widget->{"noshow"} = 1;
152         } elsif($widget->{"type"} =~ /^[hv]paned$/) {
153             print "${p}stack[$sl] = gtk_" . $widget->{"type"} . "_new();\n";
154             $widget->{"cur"} = 1;
155             $pf = sub
156             {
157                 my($widget, $p, $sl) = @_;
158                 print "${p}gtk_paned_pack" . ($widget->{"parent"}->{"cur"}) . "(GTK_PANED(stack[" . ($sl - 1) . "]), stack[$sl]";
159                 print ", " . ((index($widget->{"parent"}->{"resize"}, $widget->{"parent"}->{"cur"}) < 0)?"FALSE":"TRUE");
160                 print ", " . ((index($widget->{"parent"}->{"shrink"}, $widget->{"parent"}->{"cur"}) < 0)?"FALSE":"TRUE");
161                 print ");\n";
162                 $widget->{"parent"}->{"cur"}++;
163             }
164         } elsif($widget->{"type"} eq "notebook") {
165             print "${p}stack[$sl] = gtk_notebook_new();\n";
166             $pf = sub
167             {
168                 my($widget, $p, $sl) = @_;
169                 print "${p}gtk_notebook_append_page(GTK_NOTEBOOK(stack[" . ($sl - 1) . "]), stack[$sl]";
170                 print ", gtk_label_new_with_mnemonic(_(\"" . $widget->{"nblabel"} . "\"))";
171                 print ");\n";
172             }
173         } elsif($widget->{"type"} eq "sw") {
174             print "${p}stack[$sl] = gtk_scrolled_window_new(NULL, NULL);\n";
175             $pf = sub
176             {
177                 my($widget, $p, $sl) = @_;
178                 print "${p}gtk_container_add(GTK_CONTAINER(stack[" . ($sl - 1) . "]), stack[$sl]);\n";
179             }
180         } elsif($widget->{"type"} eq "frame") {
181             print "${p}stack[$sl] = gtk_frame_new(_(\"" . $widget->{"label"} . "\"));\n";
182             $pf = sub
183             {
184                 my($widget, $p, $sl) = @_;
185                 print "${p}gtk_container_add(GTK_CONTAINER(stack[" . ($sl - 1) . "]), stack[$sl]);\n";
186             }
187         } elsif($widget->{"type"} eq "exp") {
188             print "${p}stack[$sl] = gtk_expander_new_with_mnemonic(_(\"" . $widget->{"label"} . "\"));\n";
189             $pf = sub
190             {
191                 my($widget, $p, $sl) = @_;
192                 print "${p}gtk_container_add(GTK_CONTAINER(stack[" . ($sl - 1) . "]), stack[$sl]);\n";
193             }
194         } elsif($widget->{"type"} eq "treeview") {
195             print "${p}stack[$sl] = gtk_tree_view_new();\n";
196             if(defined($widget->{"hvis"})) {
197                 print "${p}gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(stack[$sl]), " . $widget->{"hvis"} . ");\n";
198             }
199             if(defined($widget->{"rules"})) {
200                 print "${p}gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(stack[$sl]), " . $widget->{"rules"} . ");\n";
201             }
202             if(defined($widget->{"searchcol"})) {
203                 print "${p}gtk_tree_view_set_search_column(GTK_TREE_VIEW(stack[$sl]), " . $widget->{"searchcol"} . ");\n";
204                 print "${p}gtk_tree_view_set_enable_search(GTK_TREE_VIEW(stack[$sl]), TRUE);\n";
205             }
206             $pf = sub
207             {
208                 my($widget, $p, $sl) = @_;
209                 print "${p}gtk_tree_view_append_column(GTK_TREE_VIEW(stack[" . ($sl - 1) . "]), column);\n";
210                 if($widget->{"expander"} eq "y") {
211                     print "${p}gtk_tree_view_set_expander_column(GTK_TREE_VIEW(stack[" . ($sl - 1) . "]), column);\n";
212                 }
213             }
214         } elsif($widget->{"type"} eq "tvcol") {
215             if(!defined($widget->{"subwidgets"}))
216             {
217                 print "${p}column = gtk_tree_view_column_new_with_attributes(";
218                 print "_(\"" . $widget->{"title"} . "\")";
219                 print ", gtk_cell_renderer_text_new()";
220                 if(defined($widget->{"text"})) {
221                     print ", \"text\", " . $widget->{"text"};
222                 }
223                 print ", NULL);\n";
224             } else {
225                 print "${p}column = gtk_tree_view_column_new();\n";
226                 print "${p}gtk_tree_view_column_set_title(column, _(\"" . $widget->{"title"} . "\"));\n";
227             }
228             if(defined($widget->{"sortcol"})) {
229                 print "${p}gtk_tree_view_column_set_sort_column_id(column, " . $widget->{"sortcol"} . ");\n";
230             }
231             if(defined($widget->{"resizable"})) {
232                 print "${p}gtk_tree_view_column_set_resizable(column, " . $widget->{"resizable"} . ");\n";
233             }
234             $widget->{"noshow"} = 1;
235             $pf = sub
236             {
237             }
238         } elsif($widget->{"type"} eq "textrend") {
239             print "${p}renderer = gtk_cell_renderer_text_new();\n";
240             print "${p}gtk_tree_view_column_pack_start(column, renderer, " . (defined($widget->{"expand"})?$widget->{"expand"}:"TRUE") . ");\n";
241             if(defined($widget->{"text"})) {
242                 print "${p}gtk_tree_view_column_add_attribute(column, renderer, \"text\", " . $widget->{"text"} . ");\n";
243             }
244             if(defined($widget->{"func"})) {
245                 print "${p}gtk_tree_view_column_set_cell_data_func(column, renderer, " . $widget->{"func"} . ", " . ($widget->{"funcdata"} || "NULL") . ", NULL);\n";
246             }
247             $widget->{"noshow"} = 1;
248         } elsif($widget->{"type"} eq "custrend") {
249             print "${p}renderer = GTK_CELL_RENDERER(" . $widget->{"newfunc"} . "());\n";
250             print "${p}gtk_tree_view_column_pack_start(column, renderer, " . (defined($widget->{"expand"})?$widget->{"expand"}:"FALSE") . ");\n";
251             foreach $attr (keys %{$widget})
252             {
253                 if($attr =~ /attr\((\S+)\)/)
254                 {
255                     print "${p}gtk_tree_view_column_add_attribute(column, renderer, \"" . $1 . "\", " . $widget->{$attr} . ");\n";
256                 }
257             }
258             $widget->{"noshow"} = 1;
259         } elsif($widget->{"type"} eq "pixbufrend") {
260             print "${p}renderer = gtk_cell_renderer_pixbuf_new();\n";
261             print "${p}gtk_tree_view_column_pack_start(column, renderer, FALSE);\n";
262             if(defined($widget->{"stock_id"})) {
263                 print "${p}gtk_tree_view_column_add_attribute(column, renderer, \"stock_id\", " . $widget->{"stock_id"} . ");\n";
264             }
265             $widget->{"noshow"} = 1;
266         } elsif($widget->{"type"} eq "textview") {
267             print "${p}stack[$sl] = gtk_text_view_new();\n";
268             if(defined($widget->{"editable"})) {
269                 print "${p}gtk_text_view_set_editable(GTK_TEXT_VIEW(stack[$sl]), " . $widget->{"editable"} . ");\n";
270             }
271         } elsif($widget->{"type"} eq "hr") {
272             print "${p}stack[$sl] = gtk_hseparator_new();\n";
273         } elsif($widget->{"type"} eq "sbar") {
274             print "${p}stack[$sl] = gtk_statusbar_new();\n";
275             if($widget->{"grip"} eq "n") {
276                 print "${p}gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(stack[$sl]), FALSE);\n";
277             }
278         } else {
279             print STDERR "Unknown widget: " . $widget->{"type"} ."\n";
280         }
281         if($widget->{"sensitive"}) {
282             print "${p}gtk_widget_set_sensitive(stack[$sl], " . $widget->{"sensitive"} . ");\n";
283         }
284         if($widget->{"var"} eq "y") {
285             print $p . $options{"prefix"} . $widget->{"name"} . " = stack[$sl];\n";
286         }
287         if($widget->{"var"} eq "l") {
288             print $p . "GtkWidget *" . $widget->{"name"} . " = stack[$sl];\n";
289         }
290         if($widget->{"sig"})
291         {
292             while($widget->{"sig"} =~ /\G([\w_]+),?/g) {
293                 print "${p}g_signal_connect(G_OBJECT(stack[$sl]), \"$1\", G_CALLBACK(cb_" . $options{"prefix"} . $widget->{"name"} . "_" . $1 . "), (gpointer)NULL);\n";
294             }
295         }
296         if($widget->{"accel"})
297         {
298             $mod = "";
299             while($widget->{"accel"} =~ /\G(\w+)\+/gc)
300             {
301                 $mod .= " | " if($mod);
302                 $mod = $mod . "GDK_" . $1 . "_MASK";
303             }
304             $mod || ($mod = "0");
305             $widget->{"accel"} =~ /\G(\w+)/g;
306             $key = $1;
307             print "${p}gtk_widget_add_accelerator(stack[$sl], \"activate\", accel_group, GDK_$key, $mod, GTK_ACCEL_VISIBLE);\n";
308         }
309         foreach $attr (keys %{$widget})
310         {
311             if($attr =~ /^sig\((\S+)\)/)
312             {
313                 $sig = $1;
314                 if($widget->{$attr} =~ /([^,]*),(.*)/)
315                 {
316                     $cb = $1;
317                     $data = $2;
318                 } else {
319                     $cb = $widget->{$attr};
320                     $data = "NULL";
321                 }
322                 print "${p}g_signal_connect(G_OBJECT(stack[$sl]), \"$1\", G_CALLBACK($cb), (gpointer)$data);\n";
323             }
324         }
325         if($widget->{"subwidgets"})
326         {
327             print "$p\n";
328             printwidgets($widget->{"subwidgets"}, $sl + 1, $pf);
329         }
330         if($sl > 0)
331         {
332             &$cpf($widget, $p, $sl);
333             if(!$widget->{"noshow"}) {
334                 print "${p}gtk_widget_show(stack[$sl]);\n";
335             }
336         }
337         print "$p\n";
338     }
339     foreach $line (@delayedlines)
340     {
341         print $p . $line . "\n";
342     }
343 }
344
345 sub printvars
346 {
347     my($widget);
348     foreach $widget (@{$_[0]})
349     {
350         if($widget->{"var"})
351         {
352             print "GtkWidget *" . $options{"prefix"} . $widget->{"name"} .";\n";
353         }
354         printvars($widget->{"subwidgets"}) if($widget->{"subwidgets"});
355     }
356 }
357
358 sub dequote
359 {
360     my($text);
361     ($text) = @_;
362     $text =~ s/([^\\]|^)\"/$1/g;
363     $text =~ s/\\(.)/$1/g;
364     return $text;
365 }
366
367 $rootwidgets = [];
368 @estack = ($rootwidgets);
369 @wstack = ();
370 $curwidget = 0;
371 $maxstack = 1;
372
373 while(<>)
374 {
375     chomp;
376     s/(^|\s+)\#.*$//;
377     s/^\s*//;
378     if(/^;\s*(\w+)\s*:\s*(\w.*)/)
379     {
380         $options{$1} = $2;
381     } elsif(/^([:\$])\s*(\w+)/g) {
382         $curwidget = {"type" => $2};
383         push @{$estack[0]}, $curwidget;
384         if((scalar @wstack) > 0) {
385             $curwidget->{"parent"} = $wstack[0];
386         }
387         if($1 eq ":")
388         {
389             unshift @estack, ($curwidget->{"subwidgets"} = []);
390             unshift @wstack, $curwidget;
391         }
392         $maxstack = (scalar @estack) if((scalar @estack) > $maxstack);
393         while(/\G\s*(\S+)\s*:\s*((\w+|\"([^\\\"]+|\\.)*([^\\]|)\"|\\.)+)/g)
394         {
395             $curwidget->{$1} = dequote($2);
396         }
397     } elsif(/^%\s*(\S+)\s*:\s*((\w+|\"([^\\\"]+|\\.)*([^\\]|)\"|\\.)+)/) {
398         $curwidget || die("No current widget\n");
399         $curwidget->{$1} = dequote($2);
400     } elsif(/^end/) {
401         shift @estack;
402         shift @wstack;
403         $curwidget = $wstack[0] if((scalar @wstack) > 0);
404     } elsif(!$_) {
405     } else {
406         print STDERR "Invalid construct: $_\n";
407     }
408 }
409
410 printvars $rootwidgets;
411 print "\n";
412 print "GtkWidget *create_" . $options{"prefix"} . "wnd(void)\n";
413 print "{\n";
414 print "    GtkWidget *stack[$maxstack];\n";
415 print "    GtkAccelGroup *accel_group;\n" if $options{"hasaccels"};
416 print "    GtkTreeViewColumn *column;\n" if $options{"hascolumns"};
417 print "    GtkCellRenderer *renderer;\n" if $options{"hasrenderers"};
418 print "    \n";
419 print "    accel_group = gtk_accel_group_new();\n" if $options{"hasaccels"};
420 printwidgets $rootwidgets, 0;
421 print "    return(stack[0]);\n";
422 print "}\n";