[gthumb] use a ui file to define the message dialog



commit c2adef0b1f3ff85c73d21305eb7010b4b51dba5f
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Sun Jan 20 18:29:11 2013 +0100

    use a ui file to define the message dialog

 gthumb/gthumb.gresource.xml        |    1 +
 gthumb/gtk-utils.c                 |  122 +++++++++++++++++++----------------
 gthumb/gtk-utils.h                 |    1 +
 gthumb/resources/Makefile.am       |    5 +-
 gthumb/resources/message-dialog.ui |   90 ++++++++++++++++++++++++++
 5 files changed, 161 insertions(+), 58 deletions(-)
---
diff --git a/gthumb/gthumb.gresource.xml b/gthumb/gthumb.gresource.xml
index 6bb8194..756a044 100644
--- a/gthumb/gthumb.gresource.xml
+++ b/gthumb/gthumb.gresource.xml
@@ -2,5 +2,6 @@
 <gresources>
   <gresource prefix="/org/gnome/gThumb">
     <file compressed="true">resources/gthumb.css</file>
+    <file compressed="true">resources/message-dialog.ui</file>
   </gresource>
 </gresources>
diff --git a/gthumb/gtk-utils.c b/gthumb/gtk-utils.c
index be0e460..a5f8924 100644
--- a/gthumb/gtk-utils.c
+++ b/gthumb/gtk-utils.c
@@ -26,6 +26,7 @@
 
 
 #define REQUEST_ENTRY_WIDTH_IN_CHARS 40
+#define GTHUMB_RESOURCE_BASE_PATH "/org/gnome/gThumb/resources/"
 
 
 void
@@ -122,72 +123,64 @@ _gtk_message_dialog_new (GtkWindow        *parent,
 			 const char       *first_button_text,
 			 ...)
 {
-	GtkWidget    *d;
-	GtkWidget    *label;
-	GtkWidget    *image;
-	GtkWidget    *hbox;
-	va_list       args;
-	const gchar  *text;
-	int           response_id;
-	char         *escaped_message, *markup_text;
-
-	g_return_val_if_fail (message != NULL, NULL);
-
-	if (stock_id == NULL)
-		stock_id = GTK_STOCK_DIALOG_INFO;
+	GtkBuilder  *builder;
+	GtkWidget   *dialog;
+	GtkWidget   *label;
+	va_list      args;
+	const gchar *text;
+	int          response_id;
+	char        *markup_text;
+
+	builder = _gtk_builder_new_from_resource ("message-dialog.ui");
+	dialog = _gtk_builder_get_widget (builder, "message_dialog");
+	gtk_window_set_title (GTK_WINDOW (dialog), "");
+	gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
+	gtk_window_set_modal (GTK_WINDOW (dialog), (flags & GTK_DIALOG_MODAL));
+	gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), (flags & GTK_DIALOG_DESTROY_WITH_PARENT));
+	g_object_weak_ref (G_OBJECT (dialog), (GWeakNotify) g_object_unref, builder);
 
-	d = gtk_dialog_new_with_buttons ("", parent, flags, NULL, NULL);
-	gtk_window_set_resizable (GTK_WINDOW (d), FALSE);
 	if (flags & GTK_DIALOG_MODAL)
-		_gtk_dialog_add_to_window_group (GTK_DIALOG (d));
-
-	gtk_container_set_border_width (GTK_CONTAINER (d), 6);
-	gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (d))), 6);
-	gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (d))), 8);
+		_gtk_dialog_add_to_window_group (GTK_DIALOG (dialog));
 
-	/* Add label and image */
+	/* set the icon */
 
-	image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_DIALOG);
-	gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
+	gtk_image_set_from_stock (GTK_IMAGE (_gtk_builder_get_widget (builder, "icon_image")),
+				  stock_id,
+				  GTK_ICON_SIZE_DIALOG);
 
-	label = gtk_label_new ("");
+	/* set the message */
 
-	escaped_message = g_markup_escape_text (message, -1);
-	if (secondary_message != NULL) {
-		char *escaped_secondary_message = g_markup_escape_text (secondary_message, -1);
-		markup_text = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s",
-					       escaped_message,
-					       escaped_secondary_message);
-		g_free (escaped_secondary_message);
-	}
-	else
-		markup_text = g_strdup (escaped_message);
-	gtk_label_set_markup (GTK_LABEL (label), markup_text);
-	g_free (markup_text);
-	g_free (escaped_message);
+	label = _gtk_builder_get_widget (builder, "message_label");
 
-	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
-	gtk_label_set_selectable (GTK_LABEL (label), TRUE);
+	if (message != NULL) {
+		char *escaped_message;
 
-	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 18);
-	gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
+		escaped_message = g_markup_escape_text (message, -1);
+		if (secondary_message != NULL) {
+			char *escaped_secondary_message;
 
-	gtk_box_pack_start (GTK_BOX (hbox), image,
-			    FALSE, FALSE, 0);
+			escaped_secondary_message = g_markup_escape_text (secondary_message, -1);
+			markup_text = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s",
+						       escaped_message,
+						       escaped_secondary_message);
 
-	gtk_box_pack_start (GTK_BOX (hbox), label,
-			    TRUE, TRUE, 0);
+			g_free (escaped_secondary_message);
+		}
+		else
+			markup_text = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>", escaped_message);
 
-	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (d))),
-			    hbox,
-			    FALSE, FALSE, 0);
+		g_free (escaped_message);
+	}
+	else
+		markup_text = g_markup_escape_text (secondary_message, -1);
 
-	gtk_widget_show_all (hbox);
+	gtk_label_set_markup (GTK_LABEL (label), markup_text);
+	g_free (markup_text);
 
-	/* Add buttons */
+	/* add the buttons */
 
 	if (first_button_text == NULL)
-		return d;
+		return dialog;
 
 	va_start (args, first_button_text);
 
@@ -195,9 +188,9 @@ _gtk_message_dialog_new (GtkWindow        *parent,
 	response_id = va_arg (args, gint);
 
 	while (text != NULL) {
-		gtk_dialog_add_button (GTK_DIALOG (d), text, response_id);
+		gtk_dialog_add_button (GTK_DIALOG (dialog), text, response_id);
 
-		text = va_arg (args, gchar*);
+		text = va_arg (args, char*);
 		if (text == NULL)
 			break;
 		response_id = va_arg (args, int);
@@ -205,9 +198,7 @@ _gtk_message_dialog_new (GtkWindow        *parent,
 
 	va_end (args);
 
-	gtk_dialog_set_default_response (GTK_DIALOG (d), GTK_RESPONSE_YES);
-
-	return d;
+	return dialog;
 }
 
 
@@ -606,6 +597,25 @@ _gtk_builder_new_from_file (const char *ui_file,
 }
 
 
+GtkBuilder *
+_gtk_builder_new_from_resource (const char *resource_path)
+{
+	GtkBuilder *builder;
+	char       *full_path;
+	GError     *error = NULL;
+
+	builder = gtk_builder_new ();
+	full_path = g_strconcat (GTHUMB_RESOURCE_BASE_PATH, resource_path, NULL);
+        if (! gtk_builder_add_from_resource (builder, full_path, &error)) {
+                g_warning ("%s\n", error->message);
+                g_clear_error (&error);
+        }
+	g_free (full_path);
+
+        return builder;
+}
+
+
 GtkWidget *
 _gtk_builder_get_widget (GtkBuilder *builder,
 			 const char *name)
diff --git a/gthumb/gtk-utils.h b/gthumb/gtk-utils.h
index f7573fd..27664a3 100644
--- a/gthumb/gtk-utils.h
+++ b/gthumb/gtk-utils.h
@@ -96,6 +96,7 @@ int             _gtk_container_get_pos                     (GtkContainer     *co
 guint           _gtk_container_get_n_children              (GtkContainer     *container);
 GtkBuilder *    _gtk_builder_new_from_file                 (const char       *filename,
 					    	    	    const char       *extension);
+GtkBuilder *    _gtk_builder_new_from_resource             (const char       *resource_path);
 GtkWidget *     _gtk_builder_get_widget                    (GtkBuilder       *builder,
 	 		 		    	    	    const char       *name);
 GtkWidget *     _gtk_combo_box_new_with_texts              (const char       *first_text,
diff --git a/gthumb/resources/Makefile.am b/gthumb/resources/Makefile.am
index 9265f7c..bb720a1 100644
--- a/gthumb/resources/Makefile.am
+++ b/gthumb/resources/Makefile.am
@@ -1,4 +1,5 @@
-EXTRA_DIST =		\
-	gthumb.css
+EXTRA_DIST =			\
+	gthumb.css		\
+	message-dialog.ui
 	
 -include $(top_srcdir)/git.mk
\ No newline at end of file
diff --git a/gthumb/resources/message-dialog.ui b/gthumb/resources/message-dialog.ui
new file mode 100644
index 0000000..5b771c2
--- /dev/null
+++ b/gthumb/resources/message-dialog.ui
@@ -0,0 +1,90 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.0 -->
+  <object class="GtkDialog" id="message_dialog">
+    <property name="can_focus">False</property>
+    <property name="border_width">5</property>
+    <property name="type_hint">dialog</property>
+    <child internal-child="vbox">
+      <object class="GtkBox" id="dialog-vbox4">
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">12</property>
+        <child internal-child="action_area">
+          <object class="GtkButtonBox" id="dialog-action_area4">
+            <property name="can_focus">False</property>
+            <property name="layout_style">end</property>
+            <child>
+              <placeholder/>
+            </child>
+            <child>
+              <placeholder/>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="pack_type">end</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkHBox" id="hbox28">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="border_width">5</property>
+            <property name="spacing">12</property>
+            <child>
+              <object class="GtkImage" id="icon_image">
+                <property name="width_request">64</property>
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="yalign">0</property>
+                <property name="stock">gtk-info</property>
+                <property name="pixel_size">48</property>
+                <property name="icon-size">6</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkBox" id="box1">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="orientation">vertical</property>
+                <property name="spacing">12</property>
+                <child>
+                  <object class="GtkLabel" id="message_label">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="xalign">0</property>
+                    <property name="use_underline">True</property>
+                    <property name="selectable">True</property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">True</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+              </object>
+              <packing>
+                <property name="expand">True</property>
+                <property name="fill">True</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>



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