From: Andrea Zagli Date: Sun, 3 Apr 2011 09:43:43 +0000 (+0200) Subject: Aggiunte le funzioni SolipaUtils::save_window_position e X-Git-Tag: 0.0.4~8 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=1d46bd98ca7a3069e1ef60eccd0d2816ce19a7d5;p=solipa%2Flibsolipa Aggiunte le funzioni SolipaUtils::save_window_position e SolipaUtils::load_window_position. --- diff --git a/src/utils.c b/src/utils.c index 043af3b..e678f75 100644 --- a/src/utils.c +++ b/src/utils.c @@ -912,6 +912,157 @@ solipa_chk_partita_iva (const gchar *partita_iva, gboolean empty_good) return TRUE; } +void +solipa_save_window_position (const gchar *app_name, GtkWidget *widget, const gchar *widget_name) +{ + const gchar *config_dir; + gchar *base_dir; + gchar *filename; + GKeyFile *kfile; + + GError *error; + + gboolean exists; + + GtkWidget *wtop; + + config_dir = g_get_user_config_dir (); + base_dir = g_build_filename (config_dir, app_name, NULL); + g_mkdir_with_parents (base_dir, S_IRWXU); + filename = g_build_filename (base_dir, app_name, NULL); + + kfile = g_key_file_new (); + + error = NULL; + exists = g_key_file_load_from_file (kfile, filename, + G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, + &error); + + if (exists) + { + g_key_file_remove_group (kfile, widget_name, NULL); + } + + wtop = gtk_widget_get_toplevel (widget); + if (gtk_widget_is_toplevel (wtop) && GTK_IS_WINDOW (wtop)) + { + gint x; + gint y; + gint width; + gint height; + GdkWindowState state; + + gchar *data; + GFile *fout; + GFileOutputStream *ostream; + + gtk_window_get_position (GTK_WINDOW (wtop), &x, &y); + + g_key_file_set_integer (kfile, widget_name, "x", x); + g_key_file_set_integer (kfile, widget_name, "y", y); + + gtk_window_get_size (GTK_WINDOW (wtop), &width, &height); + + g_key_file_set_integer (kfile, widget_name, "width", width); + g_key_file_set_integer (kfile, widget_name, "height", height); + + state = gdk_window_get_state (gtk_widget_get_window (wtop)); + g_key_file_set_boolean (kfile, widget_name, "maximized", state & GDK_WINDOW_STATE_MAXIMIZED); + + data = g_key_file_to_data (kfile, NULL, NULL); + fout = g_file_new_for_path (filename); + + error = NULL; + ostream = g_file_replace (fout, NULL, FALSE, + G_FILE_CREATE_NONE, + NULL, &error); + if (ostream == NULL || error != NULL) + { + g_warning ("Impossibile scrivere il file di configurazione: %s.", + error->message != NULL ? error->message : "nessun dettaglio"); + } + else + { + error = NULL; + if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream), + data, strlen (data), + NULL, NULL, &error)) + { + g_warning ("Errore nella scrittura del file di configurazione: %s", + error != NULL && error->message != NULL ? error->message : "nessun dettaglio"); + } + + error = NULL; + if (!g_output_stream_close (G_OUTPUT_STREAM (ostream), NULL, &error)) + { + g_warning ("Errore nella chiusura del file di configurazione: %s", + error != NULL && error->message != NULL ? error->message : "nessun dettaglio"); + } + g_object_unref (ostream); + } + } + else + { + g_warning ("Impossibile trovare la finestra principale del widget."); + } + + g_key_file_free (kfile); +} + +void +solipa_load_window_position (const gchar *app_name, GtkWidget *widget, const gchar *widget_name) +{ + const gchar *config_dir; + gchar *base_dir; + gchar *filename; + GKeyFile *kfile; + + GError *error; + + GtkWidget *wtop; + + config_dir = g_get_user_config_dir (); + base_dir = g_build_filename (config_dir, app_name, NULL); + g_mkdir_with_parents (base_dir, S_IRWXU); + filename = g_build_filename (base_dir, app_name, NULL); + + kfile = g_key_file_new (); + + error = NULL; + if (g_key_file_load_from_file (kfile, filename, + G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, + &error)) + { + wtop = gtk_widget_get_toplevel (widget); + if (gtk_widget_is_toplevel (wtop) && GTK_IS_WINDOW (wtop)) + { + gint x; + gint y; + gint width; + gint height; + + if (g_key_file_get_boolean (kfile, widget_name, "maximized", NULL)) + { + gtk_window_maximize (GTK_WINDOW (wtop)); + } + else + { + x = g_key_file_get_integer (kfile, widget_name, "x", NULL); + y = g_key_file_get_integer (kfile, widget_name, "y", NULL); + + gtk_window_move (GTK_WINDOW (wtop), x, y); + + width = g_key_file_get_integer (kfile, widget_name, "width", NULL); + height = g_key_file_get_integer (kfile, widget_name, "height", NULL); + + gtk_window_resize (GTK_WINDOW (wtop), width, height); + } + } + } + + g_key_file_free (kfile); +} + /** * This function is copied from * http://bugzilla.gnome.org/show_bug.cgi?id=524831. diff --git a/src/utils.h b/src/utils.h index 24092c4..6f16736 100644 --- a/src/utils.h +++ b/src/utils.h @@ -67,6 +67,9 @@ const gchar *solipa_tm_to_sql (struct tm *tm_data, const gchar *format); gboolean solipa_chk_codice_fiscale (const gchar *codice_fiscale, gboolean empty_good); gboolean solipa_chk_partita_iva (const gchar *partita_iva, gboolean empty_good); +void solipa_save_window_position (const gchar *app_name, GtkWidget *widget, const gchar *widget_name); +void solipa_load_window_position (const gchar *app_name, GtkWidget *widget, const gchar *widget_name); + gchar *g_mkdtemp (gchar *tmpl); diff --git a/tests/allegato.c b/tests/allegato.c index 269f97b..2666a2a 100644 --- a/tests/allegato.c +++ b/tests/allegato.c @@ -19,6 +19,18 @@ #include #include +GtkWidget *w; + +gboolean +on_w_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer user_data) +{ + solipa_save_window_position ("solipa_allegato", w, "w_main"); + + return FALSE; +} + int main (int argc, char *argv[]) { @@ -28,8 +40,6 @@ main (int argc, char *argv[]) Solipa *solipa; SolipaAllegato *allegato; - GtkWidget *w; - gtk_init (&argc, &argv); solipa = solipa_new (); @@ -62,11 +72,14 @@ main (int argc, char *argv[]) gtk_builder_connect_signals (builder, NULL); w = GTK_WIDGET (gtk_builder_get_object (builder, "w_allegato")); + g_signal_connect (w, "delete-event", G_CALLBACK (on_w_delete_event), (gpointer)solipa); gtk_box_pack_start (GTK_BOX (gtk_builder_get_object (builder, "vbox1")), solipa_allegato_get_widget (allegato), TRUE, TRUE, 5); + solipa_load_window_position ("solipa_allegato", w, "w_main"); + gtk_widget_show (w); gtk_main (); diff --git a/tests/tests.ui b/tests/tests.ui index 68fe913..8d97153 100644 --- a/tests/tests.ui +++ b/tests/tests.ui @@ -5,7 +5,6 @@ Test SolipaAllegato - True