[gthumb: 17/23] contact_sheet: added the preview in the theme editor



commit 19108a06473ea4e2ab50aa91c7a1015c4be7a264
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Sun Dec 26 19:28:18 2010 +0100

    contact_sheet: added the preview in the theme editor

 .../data/ui/contact-sheet-theme-properties.ui      |   12 +-
 extensions/contact_sheet/dlg-contact-sheet.c       |    2 +-
 .../contact_sheet/gth-contact-sheet-creator.c      |    2 +-
 .../contact_sheet/gth-contact-sheet-theme-dialog.c |  316 ++++++++++++++-----
 extensions/contact_sheet/gth-contact-sheet-theme.c |  347 ++++++++++++++++----
 extensions/contact_sheet/gth-contact-sheet-theme.h |    9 +-
 6 files changed, 544 insertions(+), 144 deletions(-)
---
diff --git a/extensions/contact_sheet/data/ui/contact-sheet-theme-properties.ui b/extensions/contact_sheet/data/ui/contact-sheet-theme-properties.ui
index 3557e1b..d06a15f 100644
--- a/extensions/contact_sheet/data/ui/contact-sheet-theme-properties.ui
+++ b/extensions/contact_sheet/data/ui/contact-sheet-theme-properties.ui
@@ -247,7 +247,7 @@
                             <property name="can_focus">True</property>
                             <property name="receives_default">True</property>
                             <property name="title" translatable="yes">Select a color</property>
-                            <property name="color">#ffffffffffff</property>
+                            <property name="color">#000000000000</property>
                           </object>
                           <packing>
                             <property name="left_attach">2</property>
@@ -262,7 +262,7 @@
                             <property name="can_focus">True</property>
                             <property name="receives_default">True</property>
                             <property name="title" translatable="yes">Select a color</property>
-                            <property name="color">#ffffffffffff</property>
+                            <property name="color">#000000000000</property>
                           </object>
                           <packing>
                             <property name="left_attach">2</property>
@@ -279,7 +279,7 @@
                             <property name="can_focus">True</property>
                             <property name="receives_default">True</property>
                             <property name="title" translatable="yes">Select a color</property>
-                            <property name="color">#ffff00000000</property>
+                            <property name="color">#000000000000</property>
                           </object>
                           <packing>
                             <property name="left_attach">4</property>
@@ -296,7 +296,7 @@
                             <property name="can_focus">True</property>
                             <property name="receives_default">True</property>
                             <property name="title" translatable="yes">Select a color</property>
-                            <property name="color">#00000000ffff</property>
+                            <property name="color">#000000000000</property>
                           </object>
                           <packing>
                             <property name="left_attach">2</property>
@@ -422,7 +422,6 @@
                           <object class="GtkComboBox" id="frame_style_combobox">
                             <property name="visible">True</property>
                             <property name="model">frame_style_liststore</property>
-                            <property name="active">2</property>
                             <child>
                               <object class="GtkCellRendererText" id="cellrenderertext1"/>
                               <attributes>
@@ -665,7 +664,7 @@
                 <property name="resize_mode">queue</property>
                 <property name="shadow_type">none</property>
                 <child>
-                  <object class="GtkDrawingArea" id="prop_thumb_preview">
+                  <object class="GtkDrawingArea" id="preview_area">
                     <property name="visible">True</property>
                   </object>
                 </child>
@@ -678,7 +677,6 @@
         </child>
       </object>
       <packing>
-        <property name="expand">False</property>
         <property name="position">1</property>
       </packing>
     </child>
diff --git a/extensions/contact_sheet/dlg-contact-sheet.c b/extensions/contact_sheet/dlg-contact-sheet.c
index e7de1fb..467fcd0 100644
--- a/extensions/contact_sheet/dlg-contact-sheet.c
+++ b/extensions/contact_sheet/dlg-contact-sheet.c
@@ -350,7 +350,7 @@ add_themes_from_dir (DialogData *data,
 				    THEME_COLUMN_PREVIEW, preview,
 				    -1);
 
-		g_object_unref (preview);
+		_g_object_unref (preview);
 		g_key_file_free (key_file);
 		g_free (buffer);
 		g_object_unref (file);
diff --git a/extensions/contact_sheet/gth-contact-sheet-creator.c b/extensions/contact_sheet/gth-contact-sheet-creator.c
index cc57765..07d3010 100644
--- a/extensions/contact_sheet/gth-contact-sheet-creator.c
+++ b/extensions/contact_sheet/gth-contact-sheet-creator.c
@@ -259,7 +259,7 @@ begin_page (GthContactSheetCreator *self,
 	self->priv->cr = cairo_create (surface);
 	cairo_surface_destroy (surface);
 
-	gth_contact_sheet_theme_paint_background (self->priv->theme, self->priv->cr);
+	gth_contact_sheet_theme_paint_background (self->priv->theme, self->priv->cr, width, height);
 
 	/* image map file. */
 
diff --git a/extensions/contact_sheet/gth-contact-sheet-theme-dialog.c b/extensions/contact_sheet/gth-contact-sheet-theme-dialog.c
index e2e2135..a88be88 100644
--- a/extensions/contact_sheet/gth-contact-sheet-theme-dialog.c
+++ b/extensions/contact_sheet/gth-contact-sheet-theme-dialog.c
@@ -30,8 +30,8 @@ static gpointer parent_class = NULL;
 
 
 struct _GthContactSheetThemeDialogPrivate {
-	GtkBuilder *builder;
-	GFile      *file;
+	GtkBuilder           *builder;
+	GthContactSheetTheme *theme;
 };
 
 
@@ -43,7 +43,7 @@ gth_contact_sheet_theme_dialog_finalize (GObject *object)
 	self = GTH_CONTACT_SHEET_THEME_DIALOG (object);
 
 	_g_object_unref (self->priv->builder);
-	_g_object_unref (self->priv->file);
+	gth_contact_sheet_theme_unref (self->priv->theme);
 
 	G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -62,6 +62,124 @@ gth_contact_sheet_theme_dialog_class_init (GthContactSheetThemeDialogClass *klas
 }
 
 
+static gboolean
+preview_area_expose_event_cb (GtkWidget      *widget,
+			      GdkEventExpose *event,
+			      gpointer        user_data)
+{
+	GthContactSheetThemeDialog *self = user_data;
+	cairo_t                    *cr;
+	int                         width;
+	int                         height;
+
+	cr = gdk_cairo_create (gtk_widget_get_window (widget));
+	gdk_drawable_get_size (gtk_widget_get_window (widget), &width, &height);
+	gth_contact_sheet_theme_paint_preview (self->priv->theme, cr, width, height);
+	cairo_destroy (cr);
+
+	return TRUE;
+}
+
+
+static void
+update_theme_from_controls (GthContactSheetThemeDialog *self)
+{
+	self->priv->theme->display_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("name_entry"))));
+
+	/* background */
+
+	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (GET_WIDGET ("solid_color_radiobutton")))) {
+		self->priv->theme->background_type = GTH_CONTACT_SHEET_BACKGROUND_TYPE_SOLID;
+		gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("solid_color_colorpicker")), &self->priv->theme->background_color1);
+	}
+	else {
+		gboolean h_gradient_active = FALSE;
+		gboolean v_gradient_active = FALSE;
+
+		h_gradient_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (GET_WIDGET ("h_gradient_checkbutton")));
+		v_gradient_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (GET_WIDGET ("v_gradient_checkbutton")));
+
+		if (h_gradient_active && v_gradient_active) {
+			self->priv->theme->background_type = GTH_CONTACT_SHEET_BACKGROUND_TYPE_FULL;
+			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_1_colorpicker")), &self->priv->theme->background_color1);
+			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_2_colorpicker")), &self->priv->theme->background_color2);
+			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_1_colorpicker")), &self->priv->theme->background_color3);
+			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_2_colorpicker")), &self->priv->theme->background_color4);
+		}
+		else if (h_gradient_active) {
+			self->priv->theme->background_type = GTH_CONTACT_SHEET_BACKGROUND_TYPE_HORIZONTAL;
+			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_1_colorpicker")), &self->priv->theme->background_color1);
+			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_2_colorpicker")), &self->priv->theme->background_color2);
+		}
+		else if (v_gradient_active) {
+			self->priv->theme->background_type = GTH_CONTACT_SHEET_BACKGROUND_TYPE_VERTICAL;
+			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_1_colorpicker")), &self->priv->theme->background_color1);
+			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_2_colorpicker")), &self->priv->theme->background_color2);
+		}
+	}
+
+	/* frame */
+
+	self->priv->theme->frame_style = gtk_combo_box_get_active (GTK_COMBO_BOX (GET_WIDGET ("frame_style_combobox")));
+	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("frame_colorpicker")), &self->priv->theme->frame_color);
+
+	/* header */
+
+	self->priv->theme->header_font_name = g_strdup (gtk_font_button_get_font_name (GTK_FONT_BUTTON (GET_WIDGET ("header_fontpicker"))));
+	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("header_colorpicker")), &self->priv->theme->header_color);
+
+	/* footer */
+
+	self->priv->theme->footer_font_name = g_strdup (gtk_font_button_get_font_name (GTK_FONT_BUTTON (GET_WIDGET ("footer_fontpicker"))));
+	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("footer_colorpicker")), &self->priv->theme->footer_color);
+
+	/* caption */
+
+	self->priv->theme->caption_font_name = g_strdup (gtk_font_button_get_font_name (GTK_FONT_BUTTON (GET_WIDGET ("caption_fontpicker"))));
+	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("caption_colorpicker")), &self->priv->theme->caption_color);
+}
+
+
+static void
+update_preview (GthContactSheetThemeDialog *self)
+{
+	update_theme_from_controls (self);
+	gtk_widget_queue_draw (GET_WIDGET ("preview_area"));
+}
+
+
+static void
+h_gradient_swap_button_clicked_cb (GtkButton *button,
+				   gpointer   user_data)
+{
+	GthContactSheetThemeDialog *self = user_data;
+	GdkColor                    color1;
+	GdkColor                    color2;
+
+	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_1_colorpicker")), &color1);
+	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_2_colorpicker")), &color2);
+	gtk_color_button_set_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_2_colorpicker")), &color1);
+	gtk_color_button_set_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_1_colorpicker")), &color2);
+	update_preview (self);
+}
+
+
+static void
+v_gradient_swap_button_clicked_cb (GtkButton *button,
+				   gpointer   user_data)
+{
+	GthContactSheetThemeDialog *self = user_data;
+	GdkColor                    color1;
+	GdkColor                    color2;
+
+	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_1_colorpicker")), &color1);
+	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_2_colorpicker")), &color2);
+	gtk_color_button_set_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_2_colorpicker")), &color1);
+	gtk_color_button_set_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_1_colorpicker")), &color2);
+	update_preview (self);
+}
+
+
 static void
 gth_contact_sheet_theme_dialog_init (GthContactSheetThemeDialog *self)
 {
@@ -69,10 +187,10 @@ gth_contact_sheet_theme_dialog_init (GthContactSheetThemeDialog *self)
 
 	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTH_TYPE_CONTACT_SHEET_THEME_DIALOG, GthContactSheetThemeDialogPrivate);
 	self->priv->builder = _gtk_builder_new_from_file ("contact-sheet-theme-properties.ui", "contact_sheet");
-	self->priv->file = NULL;
+	self->priv->theme = NULL;
 
 	gtk_window_set_title (GTK_WINDOW (self), _("Theme Properties"));
-	gtk_window_set_resizable (GTK_WINDOW (self), FALSE);
+	gtk_window_set_resizable (GTK_WINDOW (self), TRUE);
 	gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
 	gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), 5);
 	gtk_container_set_border_width (GTK_CONTAINER (self), 5);
@@ -91,6 +209,87 @@ gth_contact_sheet_theme_dialog_init (GthContactSheetThemeDialog *self)
 			       GTK_STOCK_OK,
 			       GTK_RESPONSE_OK);
 	gtk_dialog_set_default_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
+
+	g_signal_connect (GET_WIDGET ("preview_area"),
+			  "expose_event",
+			  G_CALLBACK (preview_area_expose_event_cb),
+			  self);
+	g_signal_connect_swapped (GET_WIDGET ("solid_color_radiobutton"),
+			  	  "toggled",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("gradient_radiobutton"),
+			  	  "toggled",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("h_gradient_checkbutton"),
+			  	  "toggled",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("v_gradient_checkbutton"),
+			  	  "toggled",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("solid_color_colorpicker"),
+			  	  "color-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("h_gradient_1_colorpicker"),
+			  	  "color-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("h_gradient_2_colorpicker"),
+			  	  "color-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("v_gradient_1_colorpicker"),
+			  	  "color-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("v_gradient_2_colorpicker"),
+			  	  "color-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("frame_colorpicker"),
+			  	  "color-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("header_colorpicker"),
+			  	  "color-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("footer_colorpicker"),
+			  	  "color-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("caption_colorpicker"),
+			  	  "color-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("frame_style_combobox"),
+			  	  "changed",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("header_fontpicker"),
+			  	  "font-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("footer_fontpicker"),
+			  	  "font-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect_swapped (GET_WIDGET ("caption_fontpicker"),
+			  	  "font-set",
+			  	  G_CALLBACK (update_preview),
+			  	  self);
+	g_signal_connect (GET_WIDGET ("h_gradient_swap_button"),
+			  "clicked",
+			  G_CALLBACK (h_gradient_swap_button_clicked_cb),
+			  self);
+	g_signal_connect (GET_WIDGET ("v_gradient_swap_button"),
+			  "clicked",
+			  G_CALLBACK (v_gradient_swap_button_clicked_cb),
+			  self);
 }
 
 
@@ -122,12 +321,44 @@ gth_contact_sheet_theme_dialog_get_type (void)
 }
 
 
+GthContactSheetTheme *
+_gth_contact_sheet_theme_new_default (void)
+{
+	GthContactSheetTheme *theme;
+
+	theme = gth_contact_sheet_theme_new ();
+	theme->display_name = g_strdup (_("New theme"));
+
+	theme->background_type = GTH_CONTACT_SHEET_BACKGROUND_TYPE_SOLID;
+	gdk_color_parse ("#fff", &theme->background_color1);
+	gdk_color_parse ("#fff", &theme->background_color2);
+	gdk_color_parse ("#fff", &theme->background_color3);
+	gdk_color_parse ("#fff", &theme->background_color4);
+
+	theme->background_type = GTH_CONTACT_SHEET_FRAME_STYLE_SIMPLE_WITH_SHADOW;
+	gdk_color_parse ("#000", &theme->frame_color);
+
+	theme->header_font_name = g_strdup ("Sans 22");
+	gdk_color_parse ("#000", &theme->frame_color);
+
+	theme->footer_font_name = g_strdup ("Sans Bold 12");
+	gdk_color_parse ("#000", &theme->footer_color);
+
+	theme->caption_font_name = g_strdup ("Sans 8");
+	gdk_color_parse ("#000", &theme->caption_color);
+
+	return theme;
+}
+
+
 static void
 update_controls_from_theme (GthContactSheetThemeDialog *self,
 			    GthContactSheetTheme       *theme)
 {
-	_g_object_unref (self->priv->file);
-	self->priv->file = _g_object_ref (theme->file);
+	if (theme != NULL)
+		self->priv->theme = gth_contact_sheet_theme_dup (theme);
+	else
+		self->priv->theme = _gth_contact_sheet_theme_new_default ();
 
 	gtk_entry_set_text (GTK_ENTRY (GET_WIDGET ("name_entry")), theme->display_name);
 
@@ -170,13 +401,7 @@ update_controls_from_theme (GthContactSheetThemeDialog *self,
 	gtk_font_button_set_font_name (GTK_FONT_BUTTON (GET_WIDGET ("caption_fontpicker")), theme->caption_font_name);
 	gtk_color_button_set_color (GTK_COLOR_BUTTON (GET_WIDGET ("caption_colorpicker")), &theme->caption_color);
 
-}
-
-
-static void
-update_theme_preview (GthContactSheetThemeDialog *self)
-{
-
+	update_preview (self);
 }
 
 
@@ -186,7 +411,7 @@ gth_contact_sheet_theme_dialog_construct (GthContactSheetThemeDialog *self,
 {
 	if (theme != NULL)
 		update_controls_from_theme (self, theme);
-	update_theme_preview (self);
+	gtk_widget_queue_draw (GET_WIDGET ("preview_area"));
 }
 
 
@@ -205,63 +430,6 @@ gth_contact_sheet_theme_dialog_new (GthContactSheetTheme *theme)
 GthContactSheetTheme *
 gth_contact_sheet_theme_dialog_get_theme (GthContactSheetThemeDialog *self)
 {
-	GthContactSheetTheme *theme;
-
-	theme = gth_contact_sheet_theme_new ();
-	theme->file = _g_object_ref (self->priv->file);
-	theme->display_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("name_entry"))));
-
-	/* background */
-
-	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (GET_WIDGET ("solid_color_radiobutton")))) {
-		theme->background_type = GTH_CONTACT_SHEET_BACKGROUND_TYPE_SOLID;
-		gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("solid_color_colorpicker")), &theme->background_color1);
-	}
-	else {
-		gboolean h_gradient_active = FALSE;
-		gboolean v_gradient_active = FALSE;
-
-		h_gradient_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (GET_WIDGET ("h_gradient_checkbutton")));
-		v_gradient_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (GET_WIDGET ("v_gradient_checkbutton")));
-
-		if (h_gradient_active && v_gradient_active) {
-			theme->background_type = GTH_CONTACT_SHEET_BACKGROUND_TYPE_FULL;
-			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_1_colorpicker")), &theme->background_color1);
-			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_2_colorpicker")), &theme->background_color2);
-			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_1_colorpicker")), &theme->background_color3);
-			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_2_colorpicker")), &theme->background_color4);
-		}
-		else if (h_gradient_active) {
-			theme->background_type = GTH_CONTACT_SHEET_BACKGROUND_TYPE_HORIZONTAL;
-			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_1_colorpicker")), &theme->background_color1);
-			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("h_gradient_2_colorpicker")), &theme->background_color2);
-		}
-		else if (v_gradient_active) {
-			theme->background_type = GTH_CONTACT_SHEET_BACKGROUND_TYPE_VERTICAL;
-			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_1_colorpicker")), &theme->background_color1);
-			gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("v_gradient_2_colorpicker")), &theme->background_color2);
-		}
-	}
-
-	/* frame */
-
-	theme->frame_style = gtk_combo_box_get_active (GTK_COMBO_BOX (GET_WIDGET ("frame_style_combobox")));
-	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("frame_colorpicker")), &theme->frame_color);
-
-	/* header */
-
-	theme->header_font_name = g_strdup (gtk_font_button_get_font_name (GTK_FONT_BUTTON (GET_WIDGET ("header_fontpicker"))));
-	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("header_colorpicker")), &theme->header_color);
-
-	/* footer */
-
-	theme->footer_font_name = g_strdup (gtk_font_button_get_font_name (GTK_FONT_BUTTON (GET_WIDGET ("footer_fontpicker"))));
-	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("footer_colorpicker")), &theme->footer_color);
-
-	/* caption */
-
-	theme->caption_font_name = g_strdup (gtk_font_button_get_font_name (GTK_FONT_BUTTON (GET_WIDGET ("caption_fontpicker"))));
-	gtk_color_button_get_color (GTK_COLOR_BUTTON (GET_WIDGET ("caption_colorpicker")), &theme->caption_color);
-
-	return theme;
+	update_theme_from_controls (self);
+	return gth_contact_sheet_theme_dup (self->priv->theme);
 }
diff --git a/extensions/contact_sheet/gth-contact-sheet-theme.c b/extensions/contact_sheet/gth-contact-sheet-theme.c
index 405b368..e14d966 100644
--- a/extensions/contact_sheet/gth-contact-sheet-theme.c
+++ b/extensions/contact_sheet/gth-contact-sheet-theme.c
@@ -27,6 +27,10 @@
 #include "gth-contact-sheet-theme.h"
 
 
+#define DEFAULT_FONT "Sans 12"
+#define FRAME_BORDER 5
+
+
 static void
 _g_key_file_get_color (GKeyFile  *key_file,
 		       char      *group_name,
@@ -118,6 +122,30 @@ gth_contact_sheet_theme_new_from_key_file (GKeyFile *key_file)
 }
 
 
+GthContactSheetTheme *
+gth_contact_sheet_theme_dup (GthContactSheetTheme  *theme)
+{
+	GthContactSheetTheme *new_theme = NULL;
+	void                 *data;
+	gsize                 length;
+	GKeyFile             *key_file;
+
+	gth_contact_sheet_theme_to_data (theme, &data, &length, NULL);
+	key_file = g_key_file_new ();
+	if (g_key_file_load_from_data (key_file, data, length, G_KEY_FILE_NONE, NULL))
+		new_theme = gth_contact_sheet_theme_new_from_key_file (key_file);
+
+	g_key_file_free (key_file);
+
+	if (new_theme != NULL) {
+		_g_object_unref (new_theme->file);
+		new_theme->file = _g_object_ref (theme->file);
+	}
+
+	return new_theme;
+}
+
+
 gboolean
 gth_contact_sheet_theme_to_data (GthContactSheetTheme  *theme,
 				 void                 **buffer,
@@ -155,7 +183,7 @@ gth_contact_sheet_theme_to_data (GthContactSheetTheme  *theme,
 
 	g_key_file_free (key_file);
 
-	return *error == NULL;
+	return *buffer != NULL;
 }
 
 
@@ -170,6 +198,9 @@ gth_contact_sheet_theme_ref (GthContactSheetTheme *theme)
 void
 gth_contact_sheet_theme_unref (GthContactSheetTheme *theme)
 {
+	if (theme == NULL)
+		return;
+
 	theme->ref--;
 	if (theme->ref > 0)
 		return;
@@ -185,21 +216,14 @@ gth_contact_sheet_theme_unref (GthContactSheetTheme *theme)
 
 void
 gth_contact_sheet_theme_paint_background (GthContactSheetTheme *theme,
-					  cairo_t              *cr)
+					  cairo_t              *cr,
+					  int                   width,
+					  int                   height)
 {
 	cairo_surface_t *surface;
-	int              width;
-	int              height;
 	cairo_pattern_t *pattern;
 	cairo_color_t    color;
 
-	surface = cairo_get_target (cr);
-	if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
-		return;
-
-	width = cairo_image_surface_get_width (surface);
-	height = cairo_image_surface_get_height (surface);
-
 	switch (theme->background_type) {
 	case GTH_CONTACT_SHEET_BACKGROUND_TYPE_SOLID:
 		gdk_cairo_set_source_color (cr, &theme->background_color1);
@@ -313,62 +337,67 @@ gth_contact_sheet_theme_paint_frame (GthContactSheetTheme *theme,
 }
 
 
-GdkPixbuf *
-gth_contact_sheet_theme_create_preview (GthContactSheetTheme *theme,
-					int                   preview_size)
+static void
+get_text_bounds (GthContactSheetTheme  *theme,
+		 const char            *font_name,
+		 int                    width,
+		 double                 font_scale,
+		 const char            *text,
+		 PangoRectangle        *bounds)
 {
-	const int        preview_frame_border = 5;
-	cairo_surface_t *surface;
-	cairo_t         *cr;
-	GdkRectangle     frame_rect;
-	GdkRectangle     image_rect;
-	GdkRectangle     text_rect;
-	/*
-	PangoContext    *pango_context;
-	PangoLayout     *pango_layout;
-	*/
-	GdkPixbuf       *pixbuf;
+	PangoContext         *pango_context;
+	PangoLayout          *pango_layout;
+	PangoFontDescription *font_desc;
 
-	surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, preview_size, preview_size);
-	cr = cairo_create (surface);
-	cairo_surface_destroy (surface);
+	pango_context = gdk_pango_context_get ();
+	pango_context_set_language (pango_context, gtk_get_default_language ());
+	pango_layout = pango_layout_new (pango_context);
+	pango_layout_set_alignment (pango_layout, PANGO_ALIGN_CENTER);
 
-	gth_contact_sheet_theme_paint_background (theme, cr);
-	image_rect.width = preview_size / 2;
-	image_rect.height = preview_size / 2;
-	image_rect.x = (preview_size - image_rect.width) / 2;
-	image_rect.y = (preview_size - image_rect.height) / 2;
-	frame_rect.x = image_rect.x - preview_frame_border;
-	frame_rect.y = image_rect.y - preview_frame_border;
-	frame_rect.width = image_rect.width + (preview_frame_border * 2);
-	frame_rect.height = image_rect.height + (preview_frame_border * 2);
-	gth_contact_sheet_theme_paint_frame (theme, cr, &frame_rect, &image_rect);
+	if (font_name != NULL)
+		font_desc = pango_font_description_from_string (font_name);
+	else
+		font_desc = pango_font_description_from_string (DEFAULT_FONT);
 
-	cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
-	cairo_rectangle (cr, image_rect.x, image_rect.y, image_rect.width, image_rect.height);
-	cairo_fill (cr);
+	if (font_scale != 1.0) {
+		double                size_in_points;
+		cairo_font_options_t *options;
 
-	/* header */
+		size_in_points = (double) pango_font_description_get_size (font_desc) * font_scale;
+		pango_font_description_set_absolute_size (font_desc, size_in_points);
+		pango_layout_set_font_description (pango_layout, font_desc);
 
-	gdk_cairo_set_source_color (cr, &theme->header_color);
-	text_rect.width = preview_size / 3;
-	text_rect.height = 6;
-	text_rect.x = (preview_size - text_rect.width) / 2;
-	text_rect.y = 4;
-	cairo_rectangle (cr, text_rect.x, text_rect.y, text_rect.width, text_rect.height);
-	cairo_fill (cr);
+		options = cairo_font_options_create ();
+		cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
+		pango_cairo_context_set_font_options (pango_context, options);
+		cairo_font_options_destroy (options);
+	}
 
-	/* footer */
+	pango_layout_set_font_description (pango_layout, font_desc);
+	pango_layout_set_width (pango_layout, width * PANGO_SCALE);
+	pango_layout_set_wrap (pango_layout, PANGO_WRAP_WORD_CHAR);
+	pango_layout_set_text (pango_layout, text, -1);
+	pango_layout_get_pixel_extents (pango_layout, NULL, bounds);
+}
 
-	gdk_cairo_set_source_color (cr, &theme->footer_color);
-	text_rect.width = preview_size / 5;
-	text_rect.height = 4;
-	text_rect.x = (preview_size - text_rect.width) / 2;
-	text_rect.y = preview_size - text_rect.height - 4;
-	cairo_rectangle (cr, text_rect.x, text_rect.y, text_rect.width, text_rect.height);
-	cairo_fill (cr);
 
-	/*
+static void
+paint_text (GthContactSheetTheme  *theme,
+	    cairo_t               *cr,
+	    const char            *font_name,
+	    GdkColor              *color,
+	    int                    x,
+	    int                    y,
+	    int                    width,
+	    gboolean               footer,
+	    double                 font_scale,
+	    const char            *text)
+{
+	PangoContext         *pango_context;
+	PangoLayout          *pango_layout;
+	PangoFontDescription *font_desc;
+	PangoRectangle        bounds;
+
 	pango_context = gdk_pango_context_get ();
 	pango_context_set_language (pango_context, gtk_get_default_language ());
 	pango_layout = pango_layout_new (pango_context);
@@ -378,12 +407,210 @@ gth_contact_sheet_theme_create_preview (GthContactSheetTheme *theme,
 		font_desc = pango_font_description_from_string (font_name);
 	else
 		font_desc = pango_font_description_from_string (DEFAULT_FONT);
+
+	if (font_scale != 1.0) {
+		double                size_in_points;
+		cairo_font_options_t *options;
+
+		size_in_points = (double) pango_font_description_get_size (font_desc) * font_scale;
+		pango_font_description_set_absolute_size (font_desc, size_in_points);
+		pango_layout_set_font_description (pango_layout, font_desc);
+
+		options = cairo_font_options_create ();
+		cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
+		pango_cairo_context_set_font_options (pango_context, options);
+		cairo_font_options_destroy (options);
+	}
+
 	pango_layout_set_font_description (pango_layout, font_desc);
-	pango_layout_set_text (pango_layout, text, -1);
 	pango_layout_set_width (pango_layout, width * PANGO_SCALE);
+	pango_layout_set_wrap (pango_layout, PANGO_WRAP_WORD_CHAR);
+	pango_layout_set_text (pango_layout, text, -1);
 	pango_layout_get_pixel_extents (pango_layout, NULL, &bounds);
-	*/
 
+	cairo_save (cr);
+	gdk_cairo_set_source_color (cr, color);
+	pango_cairo_update_layout (cr, pango_layout);
+	if (footer)
+		cairo_move_to (cr, x, y - bounds.height - 2);
+	else
+		cairo_move_to (cr, x, y);
+	pango_cairo_show_layout (cr, pango_layout);
+	cairo_restore (cr);
+
+#if 0
+	cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
+	cairo_rectangle (cr, x, y, width, bounds.height);
+	cairo_stroke (cr);
+#endif
+
+	pango_font_description_free (font_desc);
+	g_object_unref (pango_layout);
+	g_object_unref (pango_context);
+}
+
+
+static void
+paint_thumbnail (GthContactSheetTheme  *theme,
+		 cairo_t               *cr,
+		 GdkRectangle          *image_rect,
+		 double                 font_scale)
+{
+	GdkRectangle frame_rect;
+
+	/* frame */
+
+	frame_rect.x = image_rect->x - FRAME_BORDER;
+	frame_rect.y = image_rect->y - FRAME_BORDER;
+	frame_rect.width = image_rect->width + (FRAME_BORDER * 2);
+	frame_rect.height = image_rect->height + (FRAME_BORDER * 2);
+	gth_contact_sheet_theme_paint_frame (theme, cr, &frame_rect, image_rect);
+
+	/* pseduo-image */
+
+	cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
+	cairo_rectangle (cr, image_rect->x, image_rect->y, image_rect->width, image_rect->height);
+	cairo_fill (cr);
+
+	cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
+	cairo_set_line_width (cr, 1.0);
+	cairo_rectangle (cr, image_rect->x + 0.5, image_rect->y + 0.5, image_rect->width, image_rect->height);
+	cairo_move_to (cr, image_rect->x, image_rect->y);
+	cairo_line_to (cr, image_rect->x + image_rect->width, image_rect->y + image_rect->height);
+	cairo_move_to (cr, image_rect->x + image_rect->width, image_rect->y);
+	cairo_line_to (cr, image_rect->x, image_rect->y + image_rect->height);
+	cairo_stroke (cr);
+
+	/* caption */
+
+	paint_text (theme,
+		    cr,
+		    theme->caption_font_name,
+		    &theme->caption_color,
+		    frame_rect.x,
+		    frame_rect.y + frame_rect.height + 2,
+		    frame_rect.width,
+		    FALSE,
+		    font_scale,
+		    _("Caption"));
+}
+
+
+void
+gth_contact_sheet_theme_paint_preview (GthContactSheetTheme  *theme,
+				       cairo_t               *cr,
+				       int                    width,
+				       int                    height)
+{
+	double       font_scale;
+	GdkRectangle image_rect;
+
+	if (height < 200)
+		font_scale = height / 200.0;
+	else
+		font_scale = 1.0;
+
+	gth_contact_sheet_theme_paint_background (theme, cr, width, height);
+
+	/* thumbnail */
+
+	if (height < 200) {
+		image_rect.width = width / 2;
+		image_rect.height = image_rect.width;
+		image_rect.x = (width - image_rect.width) / 2;
+		image_rect.y = (height - image_rect.height) / 2 - 3;
+		paint_thumbnail (theme, cr, &image_rect, font_scale);
+	}
+	else {
+		PangoRectangle header_rect;
+		PangoRectangle footer_rect;
+		PangoRectangle caption_rect;
+		int            n_columns;
+		int            n_rows;
+		int            size;
+		int            x_offset;
+		int            y_offset;
+		int            c, r;
+
+		size = 80;
+
+		get_text_bounds (theme,
+				 theme->header_font_name,
+				 width,
+				 font_scale,
+				 _("Header"),
+				 &header_rect);
+		get_text_bounds (theme,
+				 theme->footer_font_name,
+				 width,
+				 font_scale,
+				 _("Footer"),
+				 &footer_rect);
+		get_text_bounds (theme,
+				 theme->caption_font_name,
+				 size,
+				 font_scale,
+				 _("Caption"),
+				 &caption_rect);
+
+		n_columns = ((width- (theme->col_spacing * 2)) / (size + (FRAME_BORDER * 2) + theme->col_spacing));
+		n_rows = ((height - header_rect.height - (theme->row_spacing * 2) - footer_rect.height) / (size + theme->col_spacing + caption_rect.height));
+		x_offset = (width - (n_columns * (size + theme->col_spacing))) / 2;
+		y_offset = header_rect.height + theme->row_spacing;
+		for (r = 0; r < n_rows; r++) {
+			int y;
+
+			y = y_offset + r * (size + caption_rect.height + theme->row_spacing);
+			for (c = 0; c < n_columns; c++) {
+				image_rect.width = size;
+				image_rect.height = size;
+				image_rect.x = x_offset + c * (size + theme->col_spacing);
+				image_rect.y = y;
+				paint_thumbnail (theme, cr, &image_rect, font_scale);
+			}
+		}
+	}
+
+	/* header */
+
+	paint_text (theme,
+		    cr,
+		    theme->header_font_name,
+		    &theme->header_color,
+		    0,
+		    0,
+		    width,
+		    FALSE,
+		    font_scale,
+		    _("Header"));
+
+	/* footer */
+
+	paint_text (theme,
+		    cr,
+		    theme->footer_font_name,
+		    &theme->footer_color,
+		    0,
+		    height,
+		    width,
+		    TRUE,
+		    font_scale,
+		    _("Footer"));
+}
+
+
+GdkPixbuf *
+gth_contact_sheet_theme_create_preview (GthContactSheetTheme *theme,
+					int                   preview_size)
+{
+	cairo_surface_t *surface;
+	cairo_t         *cr;
+	GdkPixbuf       *pixbuf;
+
+	surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, preview_size, preview_size);
+	cr = cairo_create (surface);
+	cairo_surface_destroy (surface);
+	gth_contact_sheet_theme_paint_preview (theme, cr, preview_size, preview_size);
 	pixbuf = _gdk_pixbuf_new_from_cairo_surface (cr);
 
 	cairo_destroy (cr);
diff --git a/extensions/contact_sheet/gth-contact-sheet-theme.h b/extensions/contact_sheet/gth-contact-sheet-theme.h
index 182b61e..d2f4f9a 100644
--- a/extensions/contact_sheet/gth-contact-sheet-theme.h
+++ b/extensions/contact_sheet/gth-contact-sheet-theme.h
@@ -79,6 +79,7 @@ typedef struct {
 
 GthContactSheetTheme * gth_contact_sheet_theme_new               (void);
 GthContactSheetTheme * gth_contact_sheet_theme_new_from_key_file (GKeyFile              *key_file);
+GthContactSheetTheme * gth_contact_sheet_theme_dup               (GthContactSheetTheme  *theme);
 gboolean               gth_contact_sheet_theme_to_data           (GthContactSheetTheme  *theme,
 								  void                 **buffer,
 								  gsize                 *count,
@@ -86,11 +87,17 @@ gboolean               gth_contact_sheet_theme_to_data           (GthContactShee
 GthContactSheetTheme * gth_contact_sheet_theme_ref               (GthContactSheetTheme  *theme);
 void                   gth_contact_sheet_theme_unref             (GthContactSheetTheme  *theme);
 void                   gth_contact_sheet_theme_paint_background  (GthContactSheetTheme  *theme,
-								  cairo_t               *cr);
+								  cairo_t               *cr,
+								  int                    width,
+								  int                    height);
 void                   gth_contact_sheet_theme_paint_frame       (GthContactSheetTheme  *theme,
 								  cairo_t               *cr,
 								  GdkRectangle          *frame_rect,
 								  GdkRectangle          *image_rect);
+void                   gth_contact_sheet_theme_paint_preview     (GthContactSheetTheme  *theme,
+								  cairo_t               *cr,
+								  int                    width,
+								  int                    height);
 GdkPixbuf *            gth_contact_sheet_theme_create_preview    (GthContactSheetTheme  *theme,
 								  int                    preview_size);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]