a597abd7c91bf23e607f9069fca7f8d87364b877
[colloquium.git] / src / stylesheet.c
1 /*
2  * stylesheet.c
3  *
4  * Colloquium - A tiny presentation program
5  *
6  * Copyright (c) 2011 Thomas White <taw@bitwiz.org.uk>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <gtk/gtk.h>
31 #include <assert.h>
32
33 #include "presentation.h"
34 #include "stylesheet.h"
35 #include "objects.h"
36 #include "loadsave.h"
37
38
39 struct _stylesheetwindow
40 {
41         struct presentation *p;  /* Presentation to update when user alters
42                                   * something in this window */
43         GtkWidget           *window;
44         StyleSheet          *ss; /* Style sheet this window corresponds to */
45
46         GtkWidget           *text_font;
47         GtkWidget           *text_colour;
48
49         GtkWidget           *margin_left;
50         GtkWidget           *margin_right;
51         GtkWidget           *margin_top;
52         GtkWidget           *margin_bottom;
53         GtkWidget           *offset_x;
54         GtkWidget           *offset_y;
55         GtkWidget           *halign;
56         GtkWidget           *valign;
57         GtkWidget           *max_width;
58         GtkWidget           *use_max;
59
60         struct style        *cur_style;
61 };
62
63
64 static void text_font_set_sig(GtkFontButton *widget,
65                               struct _stylesheetwindow *s)
66 {
67         const gchar *font;
68
69         font = gtk_font_button_get_font_name(widget);
70         free(s->cur_style->font);
71         s->cur_style->font = strdup(font);
72
73         notify_style_update(s->p, s->cur_style);
74 }
75
76
77 static void text_colour_set_sig(GtkColorButton *widget,
78                               struct _stylesheetwindow *s)
79 {
80         GdkColor col;
81         guint16 al;
82
83         gtk_color_button_get_color(widget, &col);
84         free(s->cur_style->colour);
85         s->cur_style->colour = gdk_color_to_string(&col);
86         al = gtk_color_button_get_alpha(widget);
87         s->cur_style->alpha = (double)al / 65535.0;
88
89         notify_style_update(s->p, s->cur_style);
90 }
91
92
93 static void margin_left_changed_sig(GtkSpinButton *spin,
94                                     struct _stylesheetwindow *s)
95 {
96         s->cur_style->margin_left = gtk_spin_button_get_value(spin);
97         notify_style_update(s->p, s->cur_style);
98 }
99
100
101 static void margin_right_changed_sig(GtkSpinButton *spin,
102                                      struct _stylesheetwindow *s)
103 {
104         s->cur_style->margin_right = gtk_spin_button_get_value(spin);
105         notify_style_update(s->p, s->cur_style);
106 }
107
108
109 static void margin_top_changed_sig(GtkSpinButton *spin,
110                                    struct _stylesheetwindow *s)
111 {
112         s->cur_style->margin_top = gtk_spin_button_get_value(spin);
113         notify_style_update(s->p, s->cur_style);
114 }
115
116
117 static void margin_bottom_changed_sig(GtkSpinButton *spin,
118                                       struct _stylesheetwindow *s)
119 {
120         s->cur_style->margin_bottom = gtk_spin_button_get_value(spin);
121         notify_style_update(s->p, s->cur_style);
122 }
123
124
125 static void offset_x_changed_sig(GtkSpinButton *spin,
126                                  struct _stylesheetwindow *s)
127 {
128         s->cur_style->offset_x = gtk_spin_button_get_value(spin);
129         notify_style_update(s->p, s->cur_style);
130 }
131
132
133 static void offset_y_changed_sig(GtkSpinButton *spin,
134                                  struct _stylesheetwindow *s)
135 {
136         s->cur_style->offset_y = gtk_spin_button_get_value(spin);
137         notify_style_update(s->p, s->cur_style);
138 }
139
140
141 static void halign_changed_sig(GtkComboBox *combo,
142                                  struct _stylesheetwindow *s)
143 {
144         s->cur_style->halign = gtk_combo_box_get_active(combo);
145         notify_style_update(s->p, s->cur_style);
146 }
147
148
149 static void valign_changed_sig(GtkComboBox *combo,
150                                  struct _stylesheetwindow *s)
151 {
152         s->cur_style->valign = gtk_combo_box_get_active(combo);
153         notify_style_update(s->p, s->cur_style);
154 }
155
156
157 static void max_changed_sig(GtkSpinButton *spin,
158                             struct _stylesheetwindow *s)
159 {
160         s->cur_style->max_width = gtk_spin_button_get_value(spin);
161         notify_style_update(s->p, s->cur_style);
162 }
163
164
165 static void use_max_toggled_sig(GtkToggleButton *combo,
166                                  struct _stylesheetwindow *s)
167 {
168         int v;
169
170         v = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(s->use_max));
171         s->cur_style->use_max_width = v;
172         gtk_widget_set_sensitive(s->max_width,
173                                  s->cur_style->use_max_width);
174         notify_style_update(s->p, s->cur_style);
175 }
176
177
178 static void style_changed_sig(GtkComboBox *combo,
179                                struct _stylesheetwindow *s)
180 {
181         int n;
182         GdkColor col;
183
184         n = gtk_combo_box_get_active(GTK_COMBO_BOX(combo));
185         s->cur_style = s->ss->styles[n];
186
187         if ( n == 0 ) {
188                 gtk_widget_set_sensitive(s->offset_x, FALSE);
189                 gtk_widget_set_sensitive(s->offset_y, FALSE);
190                 gtk_widget_set_sensitive(s->use_max, FALSE);
191                 gtk_widget_set_sensitive(s->max_width, FALSE);
192                 gtk_widget_set_sensitive(s->halign, FALSE);
193                 gtk_widget_set_sensitive(s->valign, FALSE);
194         } else {
195                 gtk_widget_set_sensitive(s->offset_x, TRUE);
196                 gtk_widget_set_sensitive(s->offset_y, TRUE);
197                 gtk_widget_set_sensitive(s->use_max, TRUE);
198                 gtk_widget_set_sensitive(s->max_width, TRUE);
199                 gtk_widget_set_sensitive(s->halign, TRUE);
200                 gtk_widget_set_sensitive(s->valign, TRUE);
201         }
202
203         gtk_spin_button_set_value(GTK_SPIN_BUTTON(s->margin_left),
204                                   s->cur_style->margin_left);
205         gtk_spin_button_set_value(GTK_SPIN_BUTTON(s->margin_right),
206                                   s->cur_style->margin_right);
207         gtk_spin_button_set_value(GTK_SPIN_BUTTON(s->margin_bottom),
208                                   s->cur_style->margin_bottom);
209         gtk_spin_button_set_value(GTK_SPIN_BUTTON(s->margin_top),
210                                   s->cur_style->margin_top);
211
212         gtk_spin_button_set_value(GTK_SPIN_BUTTON(s->offset_x),
213                                   s->cur_style->offset_x);
214         gtk_spin_button_set_value(GTK_SPIN_BUTTON(s->offset_y),
215                                   s->cur_style->offset_y);
216
217         gtk_combo_box_set_active(GTK_COMBO_BOX(s->halign),
218                                  s->cur_style->halign);
219         gtk_combo_box_set_active(GTK_COMBO_BOX(s->valign),
220                                  s->cur_style->valign);
221
222         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(s->use_max),
223                                      s->cur_style->use_max_width);
224         gtk_widget_set_sensitive(s->max_width, s->cur_style->use_max_width);
225
226         gtk_spin_button_set_value(GTK_SPIN_BUTTON(s->max_width),
227                                   s->cur_style->max_width);
228
229         n = gtk_combo_box_get_active(GTK_COMBO_BOX(combo));
230         s->cur_style = s->ss->styles[n];
231
232         gtk_font_button_set_font_name(GTK_FONT_BUTTON(s->text_font),
233                                       s->cur_style->font);
234
235         gdk_color_parse(s->cur_style->colour, &col);
236         gtk_color_button_set_color(GTK_COLOR_BUTTON(s->text_colour), &col);
237 }
238
239
240 static void do_layout(struct _stylesheetwindow *s, GtkWidget *b)
241 {
242         GtkWidget *table;
243         GtkWidget *line;
244         GtkWidget *label;
245         GtkWidget *combo;
246         GtkWidget *box;
247         GtkWidget *vbox;
248         int i;
249
250         box = gtk_hbox_new(FALSE, 5);
251         gtk_box_pack_start(GTK_BOX(b), box, FALSE, FALSE, 5);
252         label = gtk_label_new("Style:");
253         gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
254         gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
255         combo = gtk_combo_box_new_text();
256         for ( i=0; i<s->ss->n_styles; i++ ) {
257                 gtk_combo_box_append_text(GTK_COMBO_BOX(combo),
258                                           s->ss->styles[i]->name);
259         }
260         gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
261         g_signal_connect(G_OBJECT(combo), "changed",
262                          G_CALLBACK(style_changed_sig), s);
263         gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
264
265         line = gtk_hseparator_new();
266         gtk_box_pack_start(GTK_BOX(b), line, FALSE, FALSE, 5);
267
268         box = gtk_hbox_new(TRUE, 30);
269         gtk_box_pack_start(GTK_BOX(b), box, FALSE, FALSE, 5);
270
271         vbox = gtk_vbox_new(FALSE, 0);
272         gtk_box_pack_start(GTK_BOX(box), vbox, FALSE, FALSE, 0);
273         label = gtk_label_new("Margins:");
274         gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
275         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
276         table = gtk_table_new(3, 3, TRUE);
277         gtk_table_set_row_spacings(GTK_TABLE(table), 5.0);
278         gtk_table_set_col_spacings(GTK_TABLE(table), 5.0);
279         gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
280
281         /* Left */
282         s->margin_left = gtk_spin_button_new_with_range(0.0, 1024.0, 1.0);
283         gtk_table_attach_defaults(GTK_TABLE(table), s->margin_left,
284                                   0, 1, 1, 2);
285         g_signal_connect(G_OBJECT(s->margin_left), "value-changed",
286                          G_CALLBACK(margin_left_changed_sig), s);
287
288         /* Up */
289         s->margin_top = gtk_spin_button_new_with_range(0.0, 1024.0, 1.0);
290         gtk_table_attach_defaults(GTK_TABLE(table), s->margin_top,
291                                   1, 2, 0, 1);
292         g_signal_connect(G_OBJECT(s->margin_top), "value-changed",
293                          G_CALLBACK(margin_top_changed_sig), s);
294
295         /* Right */
296         s->margin_right = gtk_spin_button_new_with_range(0.0, 1024.0, 1.0);
297         gtk_table_attach_defaults(GTK_TABLE(table), s->margin_right,
298                                   2, 3, 1, 2);
299         g_signal_connect(G_OBJECT(s->margin_right), "value-changed",
300                          G_CALLBACK(margin_right_changed_sig), s);
301
302         /* Down */
303         s->margin_bottom = gtk_spin_button_new_with_range(0.0, 1024.0, 1.0);
304         gtk_table_attach_defaults(GTK_TABLE(table), s->margin_bottom,
305                                   1, 2, 2, 3);
306         g_signal_connect(G_OBJECT(s->margin_bottom), "value-changed",
307                          G_CALLBACK(margin_bottom_changed_sig), s);
308
309         vbox = gtk_vbox_new(FALSE, 0);
310         gtk_box_pack_start(GTK_BOX(box), vbox, FALSE, FALSE, 0);
311         label = gtk_label_new("Offset from centre:");
312         gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
313         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
314         table = gtk_table_new(2, 2, TRUE);
315         gtk_table_set_row_spacings(GTK_TABLE(table), 5.0);
316         gtk_table_set_col_spacings(GTK_TABLE(table), 5.0);
317         gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
318
319         /* Up */
320         label = gtk_label_new("Upwards:");
321         gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
322         s->offset_y = gtk_spin_button_new_with_range(-s->p->slide_height,
323                                                      +s->p->slide_height, 1.0);
324         gtk_table_attach_defaults(GTK_TABLE(table), s->offset_y, 1, 2, 0, 1);
325         g_signal_connect(G_OBJECT(s->offset_y), "value-changed",
326                          G_CALLBACK(offset_y_changed_sig), s);
327
328         /* Right */
329         label = gtk_label_new("Across:");
330         gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
331         s->offset_x = gtk_spin_button_new_with_range(-s->p->slide_width,
332                                                      +s->p->slide_width, 1.0);
333         gtk_table_attach_defaults(GTK_TABLE(table), s->offset_x, 1, 2, 1, 2);
334         g_signal_connect(G_OBJECT(s->offset_x), "value-changed",
335                          G_CALLBACK(offset_x_changed_sig), s);
336
337         table = gtk_table_new(3, 2, TRUE);
338         gtk_table_set_row_spacings(GTK_TABLE(table), 5.0);
339         gtk_table_set_col_spacings(GTK_TABLE(table), 5.0);
340         gtk_box_pack_start(GTK_BOX(b), table, FALSE, FALSE, 5);
341         label = gtk_label_new("Horizontal alignment:");
342         gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
343         gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
344         s->halign = gtk_combo_box_new_text();
345         gtk_table_attach_defaults(GTK_TABLE(table), s->halign, 1, 2, 0, 1);
346         gtk_combo_box_append_text(GTK_COMBO_BOX(s->halign), "Left");
347         gtk_combo_box_append_text(GTK_COMBO_BOX(s->halign), "Centre");
348         gtk_combo_box_append_text(GTK_COMBO_BOX(s->halign), "Right");
349         g_signal_connect(G_OBJECT(s->halign), "changed",
350                          G_CALLBACK(halign_changed_sig), s);
351
352         label = gtk_label_new("Vertical alignment:");
353         gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
354         gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
355         s->valign = gtk_combo_box_new_text();
356         gtk_table_attach_defaults(GTK_TABLE(table), s->valign, 1, 2, 1, 2);
357         gtk_combo_box_append_text(GTK_COMBO_BOX(s->valign), "Top");
358         gtk_combo_box_append_text(GTK_COMBO_BOX(s->valign), "Centre");
359         gtk_combo_box_append_text(GTK_COMBO_BOX(s->valign), "Bottom");
360         g_signal_connect(G_OBJECT(s->valign), "changed",
361                          G_CALLBACK(valign_changed_sig), s);
362
363         label = gtk_label_new("Maximum width:");
364         gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
365         gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3);
366         box = gtk_hbox_new(FALSE, 5.0);
367         gtk_table_attach_defaults(GTK_TABLE(table), box, 1, 2, 2, 3);
368         s->use_max = gtk_check_button_new();
369         gtk_box_pack_start(GTK_BOX(box), s->use_max, FALSE, FALSE, 0);
370         s->max_width = gtk_spin_button_new_with_range(0.0, 1024.0, 1.0);
371         gtk_box_pack_start(GTK_BOX(box), s->max_width, FALSE, FALSE, 0);
372         g_signal_connect(G_OBJECT(s->use_max), "toggled",
373                          G_CALLBACK(use_max_toggled_sig), s);
374         g_signal_connect(G_OBJECT(s->max_width), "value-changed",
375                          G_CALLBACK(max_changed_sig), s);
376
377         /* Font/colour stuff */
378         label = gtk_label_new("Font:");
379         gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
380         gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4);
381         s->text_font = gtk_font_button_new_with_font("Sans 12");
382         box = gtk_hbox_new(FALSE, 0);
383         gtk_table_attach_defaults(GTK_TABLE(table), box, 1, 2, 3, 4);
384         gtk_box_pack_start(GTK_BOX(box), s->text_font, FALSE, FALSE, 0);
385         g_signal_connect(G_OBJECT(s->text_font), "font-set",
386                          G_CALLBACK(text_font_set_sig), s);
387
388         label = gtk_label_new("Colour:");
389         gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
390         gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 4, 5);
391         s->text_colour = gtk_color_button_new();
392         box = gtk_hbox_new(FALSE, 0);
393         gtk_table_attach_defaults(GTK_TABLE(table), box, 1, 2, 4, 5);
394         gtk_box_pack_start(GTK_BOX(box), s->text_colour, FALSE, FALSE, 0);
395         g_signal_connect(G_OBJECT(s->text_colour), "color-set",
396                          G_CALLBACK(text_colour_set_sig), s);
397
398         /* Force first update */
399         style_changed_sig(GTK_COMBO_BOX(combo), s);
400 }
401
402
403
404 static gint destroy_stylesheet_sig(GtkWidget *w, struct _stylesheetwindow *s)
405 {
406         s->p->stylesheetwindow = NULL;
407         free(s);
408         return FALSE;
409 }
410
411
412 struct style *new_style(StyleSheet *ss, const char *name)
413 {
414         struct style *sty;
415         int n;
416         struct style **styles_new;
417
418         sty = calloc(1, sizeof(*sty));
419         if ( sty == NULL ) return NULL;
420
421         sty->name = strdup(name);
422
423         n = ss->n_styles;
424         styles_new = realloc(ss->styles, (n+1)*sizeof(sty));
425         if ( styles_new == NULL ) {
426                 free(sty->name);
427                 free(sty);
428                 return NULL;
429         }
430         ss->styles = styles_new;
431         ss->styles[n] = sty;
432         ss->n_styles = n+1;
433
434         return sty;
435 }
436
437
438 void free_stylesheet(StyleSheet *ss)
439 {
440         int i;
441
442         for ( i=0; i<ss->n_styles; i++ ) {
443                 free(ss->styles[i]->name);
444                 free(ss->styles[i]);
445         }
446
447         free(ss->styles);
448         free(ss);
449 }
450
451
452 void default_stylesheet(StyleSheet *ss)
453 {
454         struct style *sty;
455
456         /* Default style must be first */
457         sty = new_style(ss, "Default");
458         sty->font = strdup("Sans 18");
459         sty->colour = strdup("#000000000000");  /* Black */
460         sty->alpha = 1.0;
461         sty->margin_left = 20.0;
462         sty->margin_right = 20.0;
463         sty->margin_top = 20.0;
464         sty->margin_bottom = 20.0;
465         sty->halign = J_CENTER;  /* Ignored */
466         sty->valign = V_CENTER;  /* Ignored */
467         sty->offset_x = 0.0;  /* Ignored */
468         sty->offset_y = 0.0;  /* Ignored */
469
470         sty = new_style(ss, "Slide title");
471         sty->font = strdup("Sans 40");
472         sty->colour = strdup("#000000000000");  /* Black */
473         sty->alpha = 1.0;
474         sty->margin_left = 20.0;
475         sty->margin_right = 20.0;
476         sty->margin_top = 20.0;
477         sty->margin_bottom = 20.0;
478         sty->halign = J_CENTER;
479         sty->valign = V_TOP;
480         sty->offset_x = 0.0;
481         sty->offset_y = 0.0;  /* irrelevant */
482
483         sty = new_style(ss, "Slide credit");
484         sty->font = strdup("Sans 14");
485         sty->colour = strdup("#000000000000");  /* Black */
486         sty->alpha = 1.0;
487         sty->margin_left = 20.0;
488         sty->margin_right = 20.0;
489         sty->margin_top = 20.0;
490         sty->margin_bottom = 35.0;
491         sty->halign = J_RIGHT;
492         sty->valign = V_BOTTOM;
493         sty->offset_x = 0.0;
494         sty->offset_y = 0.0;
495
496         sty = new_style(ss, "Slide date");
497         sty->font = strdup("Sans 12");
498         sty->colour = strdup("#999999999999");  /* Grey */
499         sty->alpha = 1.0;
500         sty->margin_left = 600.0;
501         sty->margin_right = 100.0;
502         sty->margin_top = 745.0;
503         sty->margin_bottom = 5.0;
504         sty->halign = J_RIGHT;
505         sty->valign = V_BOTTOM;
506         sty->offset_x = 0.0;
507         sty->offset_y = 0.0;
508
509         sty = new_style(ss, "Slide number");
510         sty->font = strdup("Sans 12");
511         sty->colour = strdup("#999999999999");  /* Grey */
512         sty->alpha = 1.0;
513         sty->margin_left = 600.0;
514         sty->margin_right = 5.0;
515         sty->margin_top = 745.0;
516         sty->margin_bottom = 5.0;
517         sty->halign = J_RIGHT;
518         sty->valign = V_BOTTOM;
519         sty->offset_x = 0.0;
520         sty->offset_y = 0.0;
521
522         sty = new_style(ss, "Presentation title on slide");
523         sty->font = strdup("Sans 12");
524         sty->colour = strdup("#999999999999");  /* Grey */
525         sty->alpha = 1.0;
526         sty->margin_left = 5.0;
527         sty->margin_right = 600.0;
528         sty->margin_top = 745.0;
529         sty->margin_bottom = 5.0;
530         sty->halign = J_LEFT;
531         sty->valign = V_BOTTOM;
532         sty->offset_x = 0.0;
533         sty->offset_y = 0.0;
534
535         sty = new_style(ss, "Presentation title");
536         sty->font = strdup("Sans 50");
537         sty->colour = strdup("#000000000000");  /* Black */
538         sty->alpha = 1.0;
539         sty->margin_left = 20.0;
540         sty->margin_right = 20.0;
541         sty->margin_top = 20.0;
542         sty->margin_bottom = 20.0;
543         sty->halign = J_CENTER;
544         sty->valign = V_CENTER;
545         sty->offset_x = -200.0;
546         sty->offset_y = +300.0;
547
548         sty = new_style(ss, "Presentation author");
549         sty->font = strdup("Sans 30");
550         sty->colour = strdup("#000000000000");  /* Black */
551         sty->alpha = 1.0;
552         sty->margin_left = 20.0;
553         sty->margin_right = 20.0;
554         sty->margin_top = 20.0;
555         sty->margin_bottom = 20.0;
556         sty->halign = J_CENTER;
557         sty->valign = V_CENTER;
558         sty->offset_x = +200.0;
559         sty->offset_y = -300.0;
560
561         sty = new_style(ss, "Presentation date");
562         sty->font = strdup("Sans 30");
563         sty->colour = strdup("#000000000000");  /* Black */
564         sty->alpha = 1.0;
565         sty->margin_left = 20.0;
566         sty->margin_right = 20.0;
567         sty->margin_top = 20.0;
568         sty->margin_bottom = 20.0;
569         sty->halign = J_CENTER;
570         sty->valign = V_CENTER;
571         sty->offset_x = +200.0;
572         sty->offset_y = -300.0;
573
574         ss->bgblocks = malloc(sizeof(struct bgblock));
575         ss->n_bgblocks = 1;
576         ss->bgblocks[0].type = BGBLOCK_SOLID;
577         ss->bgblocks[0].min_x = 0.0;
578         ss->bgblocks[0].max_x = 1024.0;
579         ss->bgblocks[0].min_y = 0.0;
580         ss->bgblocks[0].max_y = 768.0;
581         ss->bgblocks[0].colour1 = strdup("#ffffffffffff");
582         ss->bgblocks[0].alpha1 = 1.0;
583
584 }
585
586
587 static const char *str_halign(enum justify halign)
588 {
589         switch ( halign ) {
590                 case J_LEFT   : return "left";
591                 case J_RIGHT  : return "right";
592                 case J_CENTER : return "center";
593                 default : return "???";
594         }
595 }
596
597
598 enum justify str_to_halign(char *halign)
599 {
600         if ( strcmp(halign, "left") == 0 ) return J_LEFT;
601         if ( strcmp(halign, "right") == 0 ) return J_RIGHT;
602         if ( strcmp(halign, "center") == 0 ) return J_CENTER;
603
604         return J_LEFT;
605 }
606
607
608 static const char *str_valign(enum vert_pos valign)
609 {
610         switch ( valign ) {
611                 case V_TOP    : return "top";
612                 case V_BOTTOM : return "bottom";
613                 case V_CENTER : return "center";
614                 default : return "???";
615         }
616 }
617
618
619 enum vert_pos str_to_valign(char *valign)
620 {
621         if ( strcmp(valign, "top") == 0 ) return V_TOP;
622         if ( strcmp(valign, "bottom") == 0 ) return V_BOTTOM;
623         if ( strcmp(valign, "center") == 0 ) return V_CENTER;
624
625         return J_LEFT;
626 }
627
628
629 static const char *str_bgtype(enum bgblocktype t)
630 {
631         switch ( t ) {
632                 case BGBLOCK_SOLID             : return "solid";
633                 case BGBLOCK_GRADIENT_X        : return "gradient_x";
634                 case BGBLOCK_GRADIENT_Y        : return "gradient_y";
635                 case BGBLOCK_GRADIENT_CIRCULAR : return "gradient_circular";
636                 case BGBLOCK_IMAGE             : return "image";
637                 default : return "???";
638         }
639 }
640
641
642 static enum bgblocktype str_to_bgtype(char *t)
643 {
644         if ( strcmp(t, "solid") == 0 ) return BGBLOCK_SOLID;
645         if ( strcmp(t, "gradient_x") == 0 ) return BGBLOCK_GRADIENT_X;
646         if ( strcmp(t, "gradient_y") == 0 ) return BGBLOCK_GRADIENT_Y;
647         if ( strcmp(t, "gradient_ciruclar") == 0 ) {
648                 return BGBLOCK_GRADIENT_CIRCULAR;
649         }
650         if ( strcmp(t, "image") == 0 ) return BGBLOCK_IMAGE;
651
652         return BGBLOCK_SOLID;
653 }
654
655
656 static int read_style(struct style *sty, struct ds_node *root)
657 {
658         char *align;
659
660         get_field_f(root, "margin_left",   &sty->margin_left);
661         get_field_f(root, "margin_right",  &sty->margin_right);
662         get_field_f(root, "margin_top",    &sty->margin_top);
663         get_field_f(root, "margin_bottom", &sty->margin_bottom);
664
665         get_field_i(root, "use_max_width", &sty->use_max_width);
666         get_field_f(root, "max_width",     &sty->max_width);
667
668         get_field_f(root, "offset_x",      &sty->offset_x);
669         get_field_f(root, "offset_y",      &sty->offset_y);
670
671         get_field_s(root, "font",          &sty->font);
672         get_field_s(root, "colour",        &sty->colour);
673         get_field_f(root, "alpha",         &sty->alpha);
674
675         get_field_s(root, "halign",        &align);
676         sty->halign = str_to_halign(align);
677         free(align);
678         get_field_s(root, "valign",        &align);
679         sty->valign = str_to_valign(align);
680         free(align);
681
682         return 0;
683 }
684
685
686 static int read_bgblock(struct bgblock *b, struct ds_node *root)
687 {
688         char *type;
689
690         get_field_s(root, "type",  &type);
691         b->type = str_to_bgtype(type);
692
693         get_field_f(root, "min_x",  &b->min_x);
694         get_field_f(root, "max_x",  &b->max_x);
695         get_field_f(root, "min_y",  &b->min_y);
696         get_field_f(root, "max_y",  &b->max_y);
697
698         switch ( b->type ) {
699
700                 case BGBLOCK_SOLID :
701                 get_field_s(root, "colour1",  &b->colour1);
702                 get_field_f(root, "alpha1",  &b->alpha1);
703
704                 case BGBLOCK_GRADIENT_X :
705                 case BGBLOCK_GRADIENT_Y :
706                 case BGBLOCK_GRADIENT_CIRCULAR :
707                 get_field_s(root, "colour1",  &b->colour1);
708                 get_field_f(root, "alpha1",  &b->alpha1);
709                 get_field_s(root, "colour2",  &b->colour2);
710                 get_field_f(root, "alpha2",  &b->alpha2);
711
712                 default:
713                 break;
714
715         }
716
717         return 0;
718 }
719
720
721 StyleSheet *tree_to_stylesheet(struct ds_node *root)
722 {
723         StyleSheet *ss;
724         struct ds_node *node;
725         int i;
726
727         ss = new_stylesheet();
728         if ( ss == NULL ) return NULL;
729
730         node = find_node(root, "styles");
731         if ( node == NULL ) {
732                 fprintf(stderr, "Couldn't find styles\n");
733                 free_stylesheet(ss);
734                 return NULL;
735         }
736
737         for ( i=0; i<node->n_children; i++ ) {
738
739                 struct style *ns;
740                 char *v;
741
742                 get_field_s(node->children[i], "name", &v);
743                 if ( v == NULL ) {
744                         fprintf(stderr, "No name for style '%s'\n",
745                                 node->children[i]->key);
746                         continue;
747                 }
748
749                 ns = new_style(ss, v);
750                 if ( ns == NULL ) {
751                         fprintf(stderr, "Couldn't create style for '%s'\n",
752                                 node->children[i]->key);
753                         continue;
754                 }
755
756                 if ( read_style(ns, node->children[i]) ) {
757                         fprintf(stderr, "Couldn't read style '%s'\n", v);
758                         continue;
759                 }
760
761         }
762
763         node = find_node(root, "bgblocks");
764         if ( node == NULL ) {
765                 fprintf(stderr, "Couldn't find bgblocks\n");
766                 free_stylesheet(ss);
767                 return NULL;
768         }
769
770         ss->bgblocks = malloc(node->n_children * sizeof(struct bgblock));
771         if ( ss->bgblocks == NULL ) {
772                 fprintf(stderr, "Couldn't allocate bgblocks\n");
773                 free_stylesheet(ss);
774                 return NULL;
775         }
776         ss->n_bgblocks = node->n_children;
777
778         for ( i=0; i<node->n_children; i++ ) {
779
780                 struct bgblock *b;
781
782                 b = &ss->bgblocks[i];
783
784                 if ( read_bgblock(b, node->children[i]) ) {
785                         fprintf(stderr, "Couldn't read bgblock %i\n", i);
786                         continue;
787                 }
788
789         }
790
791         return ss;
792 }
793
794
795 StyleSheet *new_stylesheet()
796 {
797         StyleSheet *ss;
798
799         ss = calloc(1, sizeof(struct _stylesheet));
800         if ( ss == NULL ) return NULL;
801
802         ss->n_styles = 0;
803         ss->styles = NULL;
804
805         return ss;
806 }
807
808
809 int save_stylesheet(StyleSheet *ss, const char *filename)
810 {
811         FILE *fh;
812         struct serializer ser;
813
814         fh = fopen(filename, "w");
815         if ( fh == NULL ) return 1;
816
817         /* Set up the serializer */
818         ser.fh = fh;
819         ser.stack_depth = 0;
820         ser.prefix = NULL;
821
822         fprintf(fh, "# Colloquium style sheet file\n");
823         serialize_f(&ser, "version", 0.1);
824
825         serialize_start(&ser, "stylesheet");
826         write_stylesheet(ss, &ser);
827         serialize_end(&ser);
828
829         return 0;
830 }
831
832
833 StyleSheet *load_stylesheet(const char *filename)
834 {
835         StyleSheet *ss;
836
837         ss = new_stylesheet();
838         if ( ss == NULL ) return NULL;
839
840         return ss;
841 }
842
843
844 StylesheetWindow *open_stylesheet(struct presentation *p)
845 {
846         struct _stylesheetwindow *s;
847         GtkWidget *nb;
848         GtkWidget *text_box;
849         GtkWidget *background_box;
850
851         s = malloc(sizeof(*s));
852         if ( s == NULL ) return NULL;
853
854         s->p = p;
855         s->ss = p->ss;
856         s->cur_style = NULL;
857         s->cur_style = NULL;
858
859         s->window = gtk_dialog_new_with_buttons("Stylesheet",
860                                            GTK_WINDOW(p->window), 0,
861                                            GTK_STOCK_CLOSE, GTK_RESPONSE_ACCEPT,
862                                            NULL);
863         gtk_dialog_set_has_separator(GTK_DIALOG(s->window), FALSE);
864
865         nb = gtk_notebook_new();
866         gtk_notebook_set_tab_pos(GTK_NOTEBOOK(nb), GTK_POS_TOP);
867         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(s->window)->vbox), nb,
868                            TRUE, TRUE, 0);
869
870         text_box = gtk_vbox_new(FALSE, 0);
871         gtk_container_set_border_width(GTK_CONTAINER(text_box), 12);
872         gtk_notebook_append_page(GTK_NOTEBOOK(nb), text_box,
873                                  gtk_label_new("Styles"));
874         do_layout(s, text_box);
875
876         background_box = gtk_vbox_new(FALSE, 0);
877         gtk_container_set_border_width(GTK_CONTAINER(background_box), 12);
878         gtk_notebook_append_page(GTK_NOTEBOOK(nb), background_box,
879                                  gtk_label_new("Background"));
880
881         g_signal_connect(G_OBJECT(s->window), "destroy",
882                          G_CALLBACK(destroy_stylesheet_sig), s);
883         g_signal_connect(G_OBJECT(s->window), "response",
884                          G_CALLBACK(gtk_widget_destroy), NULL);
885
886         gtk_widget_show_all(s->window);
887
888         return s;
889 }
890
891
892 void write_stylesheet(StyleSheet *ss, struct serializer *ser)
893 {
894         int i;
895
896         serialize_start(ser, "bgblocks");
897         for ( i=0; i<ss->n_bgblocks; i++ ) {
898
899                 struct bgblock *b = &ss->bgblocks[i];
900                 char id[32];
901
902                 snprintf(id, 31, "%i", i);
903
904                 serialize_start(ser, id);
905                 serialize_s(ser, "type", str_bgtype(b->type));
906                 serialize_f(ser, "min_x", b->min_x);
907                 serialize_f(ser, "min_y", b->min_y);
908                 serialize_f(ser, "max_x", b->max_x);
909                 serialize_f(ser, "max_y", b->max_y);
910
911                 switch ( b->type ) {
912
913                         case BGBLOCK_SOLID :
914                         serialize_s(ser, "colour1", b->colour1);
915                         serialize_f(ser, "alpha1", b->alpha1);
916                         break;
917
918                         case BGBLOCK_GRADIENT_X :
919                         case BGBLOCK_GRADIENT_Y :
920                         case BGBLOCK_GRADIENT_CIRCULAR :
921                         serialize_s(ser, "colour1", b->colour1);
922                         serialize_f(ser, "alpha1", b->alpha1);
923                         serialize_s(ser, "colour2", b->colour2);
924                         serialize_f(ser, "alpha2", b->alpha2);
925                         break;
926
927                         default:
928                         break;
929
930                 }
931
932                 serialize_end(ser);
933
934         }
935         serialize_end(ser);
936
937         serialize_start(ser, "styles");
938         for ( i=0; i<ss->n_styles; i++ ) {
939
940                 struct style *s = ss->styles[i];
941                 char id[32];
942
943                 snprintf(id, 31, "%i", i);
944
945                 serialize_start(ser, id);
946                 serialize_s(ser, "name", s->name);
947                 serialize_f(ser, "margin_left", s->margin_left);
948                 serialize_f(ser, "margin_right", s->margin_right);
949                 serialize_f(ser, "margin_top", s->margin_top);
950                 serialize_f(ser, "margin_bottom", s->margin_bottom);
951                 serialize_b(ser, "use_max_width", s->use_max_width);
952                 serialize_f(ser, "max_width", s->max_width);
953                 serialize_f(ser, "offset_x", s->offset_x);
954                 serialize_f(ser, "offset_y", s->offset_y);
955                 serialize_s(ser, "font", s->font);
956                 serialize_s(ser, "colour", s->colour);
957                 serialize_f(ser, "alpha", s->alpha);
958                 serialize_s(ser, "halign", str_halign(s->halign));
959                 serialize_s(ser, "valign", str_valign(s->valign));
960                 serialize_end(ser);
961
962         }
963         serialize_end(ser);
964 }
965
966
967 struct style *find_style(StyleSheet *ss, const char *name)
968 {
969         int i;
970         for ( i=0; i<ss->n_styles; i++ ) {
971                 if ( strcmp(ss->styles[i]->name, name) == 0 ) {
972                         return ss->styles[i];
973                 }
974         }
975
976         return NULL;
977 }