diff options
author | Thomas White <taw@bitwiz.org.uk> | 2011-07-24 21:26:31 +0200 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2011-07-24 21:26:31 +0200 |
commit | a7c93728218e9b14f4ce82516cfc6b42d98abd17 (patch) | |
tree | 7bb9e9c892da9c42559ea80a4460eb4cc61f6cb2 | |
parent | 64445c72214d55ba7864342b84f4da4019253c2a (diff) |
Style sheet plumbing
-rw-r--r-- | src/stylesheet.c | 265 | ||||
-rw-r--r-- | src/stylesheet.h | 43 |
2 files changed, 195 insertions, 113 deletions
diff --git a/src/stylesheet.c b/src/stylesheet.c index 5339067..c16424a 100644 --- a/src/stylesheet.c +++ b/src/stylesheet.c @@ -42,31 +42,19 @@ struct _stylesheetwindow GtkWidget *window; StyleSheet *ss; /* Style sheet this window corresponds to */ - GtkWidget *fixed_font; + GtkWidget *text_font; + GtkWidget *text_colour; - struct fixed_text_style *cur_fixed_style; -}; - - -struct fixed_text_style -{ - char *name; - char *font; -}; - - -struct text_style -{ - char *name; - char *font; + struct text_style *cur_text_style; + struct layout_element *cur_layout_element; }; struct _stylesheet { - /* Fixed text styles */ - struct fixed_text_style **fixed_text_styles; - int n_fixed_text_styles; + /* Slide layout */ + struct layout_element **layout_elements; + int n_layout_elements; /* Normal text styles */ struct text_style **text_styles; @@ -77,6 +65,45 @@ struct _stylesheet /* Image styles */ }; + +static void text_changed_sig(GtkComboBox *combo, + struct _stylesheetwindow *s) +{ + int n; + + n = gtk_combo_box_get_active(GTK_COMBO_BOX(combo)); + s->cur_text_style = s->ss->text_styles[n]; + + gtk_font_button_set_font_name(GTK_FONT_BUTTON(s->text_font), + s->cur_text_style->font); +} + + +static void text_font_set_sig(GtkFontButton *widget, + struct _stylesheetwindow *s) +{ + const gchar *font; + + font = gtk_font_button_get_font_name(widget); + free(s->cur_text_style->font); + s->cur_text_style->font = strdup(font); +} + + +static void text_colour_set_sig(GtkColorButton *widget, + struct _stylesheetwindow *s) +{ + GdkColor col; + guint16 al; + + gtk_color_button_get_color(widget, &col); + free(s->cur_text_style->colour); + s->cur_text_style->colour = gdk_color_to_string(&col); + al = gtk_color_button_get_alpha(widget); + s->cur_text_style->alpha = (double)al / 65535.0; +} + + static void do_text(struct _stylesheetwindow *s, GtkWidget *b) { GtkWidget *table; @@ -84,8 +111,6 @@ static void do_text(struct _stylesheetwindow *s, GtkWidget *b) GtkWidget *line; GtkWidget *label; GtkWidget *combo; - GtkWidget *font; - GtkWidget *colour; int i; table = gtk_table_new(4, 2, FALSE); @@ -100,13 +125,13 @@ static void do_text(struct _stylesheetwindow *s, GtkWidget *b) gtk_table_attach_defaults(GTK_TABLE(table), combo, 1, 2, 0, 1); for ( i=0; i<s->ss->n_text_styles; i++ ) { - gtk_combo_box_append_text(GTK_COMBO_BOX(combo), s->ss->text_styles[i]->name); - } gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0); gtk_widget_set_size_request(GTK_WIDGET(combo), 300, -1); + g_signal_connect(G_OBJECT(combo), "changed", + G_CALLBACK(text_changed_sig), s); line = gtk_hseparator_new(); gtk_table_attach_defaults(GTK_TABLE(table), line, 0, 4, 1, 2); @@ -116,53 +141,45 @@ static void do_text(struct _stylesheetwindow *s, GtkWidget *b) label = gtk_label_new("Font:"); gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5); gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3); - font = gtk_font_button_new_with_font("Sans 12"); + s->text_font = gtk_font_button_new_with_font("Sans 12"); box = gtk_hbox_new(FALSE, 0); gtk_table_attach_defaults(GTK_TABLE(table), box, 1, 2, 2, 3); - gtk_box_pack_start(GTK_BOX(box), font, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(box), s->text_font, FALSE, FALSE, 0); + g_signal_connect(G_OBJECT(s->text_font), "font-set", + G_CALLBACK(text_font_set_sig), s); label = gtk_label_new("Colour:"); gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5); gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4); - colour = gtk_color_button_new(); + s->text_colour = gtk_color_button_new(); box = gtk_hbox_new(FALSE, 0); gtk_table_attach_defaults(GTK_TABLE(table), box, 1, 2, 3, 4); - gtk_box_pack_start(GTK_BOX(box), colour, FALSE, FALSE, 0); -} + gtk_box_pack_start(GTK_BOX(box), s->text_colour, FALSE, FALSE, 0); + g_signal_connect(G_OBJECT(s->text_colour), "color-set", + G_CALLBACK(text_colour_set_sig), s); - -static void fixed_text_changed_sig(GtkComboBox *combo, - struct _stylesheetwindow *s) -{ - int n; - - n = gtk_combo_box_get_active(GTK_COMBO_BOX(combo)); - s->cur_fixed_style = s->ss->fixed_text_styles[n]; - - gtk_font_button_set_font_name(GTK_FONT_BUTTON(s->fixed_font), - s->cur_fixed_style->font); + /* Force first update */ + text_changed_sig(GTK_COMBO_BOX(combo), s); } -static void fixed_font_set_sig(GtkFontButton *widget, +static void layout_changed_sig(GtkComboBox *combo, struct _stylesheetwindow *s) { - const gchar *font; + int n; - font = gtk_font_button_get_font_name(widget); - free(s->cur_fixed_style->font); - s->cur_fixed_style->font = strdup(font); + n = gtk_combo_box_get_active(GTK_COMBO_BOX(combo)); + s->cur_layout_element = s->ss->layout_elements[n]; } -static void do_fixed_text(struct _stylesheetwindow *s, GtkWidget *b) +static void do_layout(struct _stylesheetwindow *s, GtkWidget *b) { GtkWidget *table; GtkWidget *box; GtkWidget *line; GtkWidget *label; GtkWidget *combo; - GtkWidget *colour; int i; table = gtk_table_new(4, 2, FALSE); @@ -170,46 +187,25 @@ static void do_fixed_text(struct _stylesheetwindow *s, GtkWidget *b) gtk_table_set_row_spacings(GTK_TABLE(table), 5.0); gtk_table_set_col_spacings(GTK_TABLE(table), 5.0); - label = gtk_label_new("Style:"); + label = gtk_label_new("Element:"); gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5); gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1); combo = gtk_combo_box_new_text(); gtk_table_attach_defaults(GTK_TABLE(table), combo, 1, 2, 0, 1); - for ( i=0; i<s->ss->n_fixed_text_styles; i++ ) { + for ( i=0; i<s->ss->n_layout_elements; i++ ) { gtk_combo_box_append_text(GTK_COMBO_BOX(combo), - s->ss->fixed_text_styles[i]->name); + s->ss->layout_elements[i]->name); } gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0); gtk_widget_set_size_request(GTK_WIDGET(combo), 300, -1); g_signal_connect(G_OBJECT(combo), "changed", - G_CALLBACK(fixed_text_changed_sig), s); + G_CALLBACK(layout_changed_sig), s); line = gtk_hseparator_new(); gtk_table_attach_defaults(GTK_TABLE(table), line, 0, 4, 1, 2); gtk_table_set_row_spacing(GTK_TABLE(table), 0, 10); gtk_table_set_row_spacing(GTK_TABLE(table), 1, 10); - - label = gtk_label_new("Font:"); - gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5); - gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3); - s->fixed_font = gtk_font_button_new_with_font("Sans 12"); - box = gtk_hbox_new(FALSE, 0); - gtk_table_attach_defaults(GTK_TABLE(table), box, 1, 2, 2, 3); - gtk_box_pack_start(GTK_BOX(box), s->fixed_font, FALSE, FALSE, 0); - g_signal_connect(G_OBJECT(s->fixed_font), "font-set", - G_CALLBACK(fixed_font_set_sig), s); - - label = gtk_label_new("Colour:"); - gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5); - gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4); - colour = gtk_color_button_new(); - box = gtk_hbox_new(FALSE, 0); - gtk_table_attach_defaults(GTK_TABLE(table), box, 1, 2, 3, 4); - gtk_box_pack_start(GTK_BOX(box), colour, FALSE, FALSE, 0); - - /* Force first update */ - fixed_text_changed_sig(GTK_COMBO_BOX(combo), s); } @@ -221,75 +217,117 @@ static gint destroy_stylesheet_sig(GtkWidget *w, struct _stylesheetwindow *s) } -static void add_fixed_text_style(StyleSheet *ss, struct fixed_text_style *st) +static void add_text_style(StyleSheet *ss, struct text_style *st) { - int n = ss->n_fixed_text_styles; - ss->fixed_text_styles = realloc(ss->fixed_text_styles, + int n = ss->n_text_styles; + ss->text_styles = realloc(ss->text_styles, (n+1)*sizeof(st)); - ss->fixed_text_styles[n] = st; + ss->text_styles[n] = st; - ss->n_fixed_text_styles = n+1; + ss->n_text_styles = n+1; } -static struct fixed_text_style *new_fixed_text_style(const char *name) +static struct text_style *new_text_style(StyleSheet *ss, const char *name) { - struct fixed_text_style *st; + struct text_style *st; st = malloc(sizeof(*st)); if ( st == NULL ) return NULL; st->name = strdup(name); + st->colour = strdup("#000000000000"); /* Black */ + st->alpha = 1.0; + + add_text_style(ss, st); return st; } -static void add_text_style(StyleSheet *ss, struct text_style *st) +static struct layout_element *new_layout_element(StyleSheet *ss, + const char *name) { - int n = ss->n_text_styles; - ss->text_styles = realloc(ss->text_styles, (n+1)*sizeof(st)); - - ss->text_styles[n] = st; - - ss->n_text_styles = n+1; -} + struct layout_element *ly; + int n; + ly = malloc(sizeof(*ly)); + if ( ly == NULL ) return NULL; -static struct text_style *new_text_style(const char *name) -{ - struct text_style *st; + ly->name = strdup(name); - st = malloc(sizeof(*st)); - if ( st == NULL ) return NULL; + n = ss->n_layout_elements; + ss->layout_elements = realloc(ss->layout_elements, (n+1)*sizeof(ly)); + /* Yes, the size of the pointer */ - st->name = strdup(name); + ss->layout_elements[n] = ly; + ss->n_layout_elements = n+1; - return st; + return ly; } static void default_stylesheet(StyleSheet *ss) { - struct fixed_text_style *st; - struct text_style *nst; + struct text_style *st; + struct layout_element *ly; - st = new_fixed_text_style("Slide title"); + st = new_text_style(ss, "Slide title"); st->font = strdup("Sans 40"); - add_fixed_text_style(ss, st); - - st = new_fixed_text_style("Presentation title"); + ly = new_layout_element(ss, st->name); + ly->text_style = st; + ly->margin_left = 20.0; + ly->margin_right = 20.0; + ly->margin_top = 20.0; + ly->margin_bottom = 20.0; + ly->just = J_CENTER; + ly->vert = V_TOP; + ly->offset_x = 0.0; + ly->offset_y = 0.0; /* irrelevant */ + + st = new_text_style(ss, "Presentation title"); st->font = strdup("Sans 50"); - add_fixed_text_style(ss, st); - - st = new_fixed_text_style("Presentation author"); + ly = new_layout_element(ss, st->name); + ly->text_style = st; + ly->margin_left = 20.0; + ly->margin_right = 20.0; + ly->margin_top = 20.0; + ly->margin_bottom = 20.0; + ly->just = J_CENTER; + ly->vert = V_CENTER; + ly->offset_x = -200.0; + ly->offset_y = +300.0; + + st = new_text_style(ss, "Presentation author"); st->font = strdup("Sans 30"); - add_fixed_text_style(ss, st); + ly = new_layout_element(ss, st->name); + ly->text_style = st; + ly->text_style = st; + ly->margin_left = 20.0; + ly->margin_right = 20.0; + ly->margin_top = 20.0; + ly->margin_bottom = 20.0; + ly->just = J_CENTER; + ly->vert = V_CENTER; + ly->offset_x = +200.0; + ly->offset_y = -300.0; + + st = new_text_style(ss, "Running text"); + st->font = strdup("Sans 14"); + + ly = new_layout_element(ss, "Slide content"); + ly->text_style = st; + ly->text_style = st; + ly->margin_left = 20.0; + ly->margin_right = 20.0; + ly->margin_top = 20.0; + ly->margin_bottom = 20.0; + ly->just = J_CENTER; + ly->vert = V_CENTER; + ly->offset_x = +200.0; + ly->offset_y = -300.0; - nst = new_text_style("Running text"); - nst->font = strdup("Sans 14"); - add_text_style(ss, nst); } @@ -302,8 +340,8 @@ StyleSheet *new_stylesheet() ss->n_text_styles = 0; ss->text_styles = NULL; - ss->n_fixed_text_styles = 0; - ss->fixed_text_styles = NULL; + ss->n_layout_elements = 0; + ss->layout_elements = NULL; default_stylesheet(ss); return ss; @@ -361,7 +399,8 @@ StylesheetWindow *open_stylesheet(struct presentation *p) s->p = p; s->ss = p->ss; - s->cur_fixed_style = NULL; + s->cur_text_style = NULL; + s->cur_layout_element = NULL; s->window = gtk_dialog_new_with_buttons("Stylesheet", GTK_WINDOW(p->window), 0, @@ -377,14 +416,14 @@ StylesheetWindow *open_stylesheet(struct presentation *p) text_box = gtk_vbox_new(FALSE, 0); gtk_container_set_border_width(GTK_CONTAINER(text_box), 12); gtk_notebook_append_page(GTK_NOTEBOOK(nb), text_box, - gtk_label_new("Fixed text")); - do_fixed_text(s, text_box); + gtk_label_new("Text styles")); + do_text(s, text_box); text_box = gtk_vbox_new(FALSE, 0); gtk_container_set_border_width(GTK_CONTAINER(text_box), 12); gtk_notebook_append_page(GTK_NOTEBOOK(nb), text_box, - gtk_label_new("Text")); - do_text(s, text_box); + gtk_label_new("Slide layout")); + do_layout(s, text_box); background_box = gtk_vbox_new(FALSE, 0); gtk_container_set_border_width(GTK_CONTAINER(background_box), 12); diff --git a/src/stylesheet.h b/src/stylesheet.h index 5c46d83..3e1c8be 100644 --- a/src/stylesheet.h +++ b/src/stylesheet.h @@ -28,6 +28,49 @@ #endif +struct text_style +{ + char *name; + char *font; + char *colour; + double alpha; +}; + + +enum justify +{ + J_LEFT, + J_CENTER, + J_RIGHT, +}; + + +enum vert_pos +{ + V_TOP, + V_CENTER, + V_BOTTOM, +}; + + +struct layout_element +{ + char *name; + struct text_style *text_style; + + double margin_left; + double margin_right; + double margin_top; + double margin_bottom; + + enum justify just; + enum vert_pos vert; + + double offset_x; + double offset_y; +}; + + typedef struct _stylesheetwindow StylesheetWindow; typedef struct _stylesheet StyleSheet; struct presentation; |