a164029f5f1ca26f2d1a48b8a2e7cd8dfd2943d5
[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_GRADIENT_Y;
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("#000000000000");
582         ss->bgblocks[0].alpha1 = 1.0;
583         ss->bgblocks[0].colour2 = strdup("#ffffffffffff");
584         ss->bgblocks[0].alpha2 = 1.0;
585
586 }
587
588
589 StyleSheet *new_stylesheet()
590 {
591         StyleSheet *ss;
592
593         ss = calloc(1, sizeof(struct _stylesheet));
594         if ( ss == NULL ) return NULL;
595
596         ss->n_styles = 0;
597         ss->styles = NULL;
598
599         return ss;
600 }
601
602
603 int save_stylesheet(StyleSheet *ss, const char *filename)
604 {
605         FILE *fh;
606         struct serializer ser;
607
608         fh = fopen(filename, "w");
609         if ( fh == NULL ) return 1;
610
611         /* Set up the serializer */
612         ser.fh = fh;
613         ser.stack_depth = 0;
614         ser.prefix = NULL;
615
616         fprintf(fh, "# Colloquium style sheet file\n");
617         serialize_f(&ser, "version", 0.1);
618
619         serialize_start(&ser, "stylesheet");
620         write_stylesheet(ss, &ser);
621         serialize_end(&ser);
622
623         return 0;
624 }
625
626
627 StyleSheet *load_stylesheet(const char *filename)
628 {
629         StyleSheet *ss;
630
631         ss = new_stylesheet();
632         if ( ss == NULL ) return NULL;
633
634         return ss;
635 }
636
637
638 StylesheetWindow *open_stylesheet(struct presentation *p)
639 {
640         struct _stylesheetwindow *s;
641         GtkWidget *nb;
642         GtkWidget *text_box;
643         GtkWidget *background_box;
644
645         s = malloc(sizeof(*s));
646         if ( s == NULL ) return NULL;
647
648         s->p = p;
649         s->ss = p->ss;
650         s->cur_style = NULL;
651         s->cur_style = NULL;
652
653         s->window = gtk_dialog_new_with_buttons("Stylesheet",
654                                            GTK_WINDOW(p->window), 0,
655                                            GTK_STOCK_CLOSE, GTK_RESPONSE_ACCEPT,
656                                            NULL);
657         gtk_dialog_set_has_separator(GTK_DIALOG(s->window), FALSE);
658
659         nb = gtk_notebook_new();
660         gtk_notebook_set_tab_pos(GTK_NOTEBOOK(nb), GTK_POS_TOP);
661         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(s->window)->vbox), nb,
662                            TRUE, TRUE, 0);
663
664         text_box = gtk_vbox_new(FALSE, 0);
665         gtk_container_set_border_width(GTK_CONTAINER(text_box), 12);
666         gtk_notebook_append_page(GTK_NOTEBOOK(nb), text_box,
667                                  gtk_label_new("Styles"));
668         do_layout(s, text_box);
669
670         background_box = gtk_vbox_new(FALSE, 0);
671         gtk_container_set_border_width(GTK_CONTAINER(background_box), 12);
672         gtk_notebook_append_page(GTK_NOTEBOOK(nb), background_box,
673                                  gtk_label_new("Background"));
674
675         g_signal_connect(G_OBJECT(s->window), "destroy",
676                          G_CALLBACK(destroy_stylesheet_sig), s);
677         g_signal_connect(G_OBJECT(s->window), "response",
678                          G_CALLBACK(gtk_widget_destroy), NULL);
679
680         gtk_widget_show_all(s->window);
681
682         return s;
683 }
684
685
686 static const char *str_halign(enum justify halign)
687 {
688         switch ( halign ) {
689                 case J_LEFT   : return "left";
690                 case J_RIGHT  : return "right";
691                 case J_CENTER : return "center";
692                 default : return "???";
693         }
694 }
695
696
697 enum justify str_to_halign(char *halign)
698 {
699         if ( strcmp(halign, "left") == 0 ) return J_LEFT;
700         if ( strcmp(halign, "right") == 0 ) return J_RIGHT;
701         if ( strcmp(halign, "center") == 0 ) return J_CENTER;
702
703         return J_LEFT;
704 }
705
706
707 static const char *str_valign(enum vert_pos valign)
708 {
709         switch ( valign ) {
710                 case V_TOP    : return "top";
711                 case V_BOTTOM : return "bottom";
712                 case V_CENTER : return "center";
713                 default : return "???";
714         }
715 }
716
717
718 enum vert_pos str_to_valign(char *valign)
719 {
720         if ( strcmp(valign, "top") == 0 ) return V_TOP;
721         if ( strcmp(valign, "bottom") == 0 ) return V_BOTTOM;
722         if ( strcmp(valign, "center") == 0 ) return V_CENTER;
723
724         return J_LEFT;
725 }
726
727
728 void write_stylesheet(StyleSheet *ss, struct serializer *ser)
729 {
730         int i;
731
732         serialize_start(ser, "styles");
733         for ( i=0; i<ss->n_styles; i++ ) {
734
735                 struct style *s = ss->styles[i];
736                 char id[32];
737
738                 snprintf(id, 31, "%i", i);
739
740                 serialize_start(ser, id);
741                 serialize_s(ser, "name", s->name);
742                 serialize_f(ser, "margin_left", s->margin_left);
743                 serialize_f(ser, "margin_right", s->margin_right);
744                 serialize_f(ser, "margin_top", s->margin_top);
745                 serialize_f(ser, "margin_bottom", s->margin_bottom);
746                 serialize_b(ser, "use_max_width", s->use_max_width);
747                 serialize_f(ser, "max_width", s->max_width);
748                 serialize_f(ser, "offset_x", s->offset_x);
749                 serialize_f(ser, "offset_y", s->offset_y);
750                 serialize_s(ser, "font", s->font);
751                 serialize_s(ser, "colour", s->colour);
752                 serialize_f(ser, "alpha", s->alpha);
753                 serialize_s(ser, "halign", str_halign(s->halign));
754                 serialize_s(ser, "valign", str_valign(s->valign));
755                 serialize_end(ser);
756
757         }
758         serialize_end(ser);
759 }
760
761
762 struct style *find_style(StyleSheet *ss, const char *name)
763 {
764         int i;
765         for ( i=0; i<ss->n_styles; i++ ) {
766                 if ( strcmp(ss->styles[i]->name, name) == 0 ) {
767                         return ss->styles[i];
768                 }
769         }
770
771         return NULL;
772 }