[Spice-devel] [spicy PATCH 2/6 v5] spicy: Changed the dialog into a window

Lukas Venhoda lvenhoda at redhat.com
Tue Jun 16 07:23:30 PDT 2015


Changed connect dialog from GtkDialog to GtkWindow.

Added the necessary signals and buttons, to keep then
behaviour of a dialog (ESC to close, ENTER to submit).

spicy_connect_dialog now returns TRUE and FALSE instead of 0 and -1.
---
Changes since v4
 - Squashed with "Changed response to gboolean" commit
 - Code clean up
    - Better variable names
    - Removed useless function
 - Fixed build on gtk2

Changes since v3
 - Changed block of code in set_connection_info into a for loop

Changes since v2
 - Now only contains changing dialog to window
    - UX changes in later patches
 - Changed response type from GtkResponseType to gboolean
---
 src/spicy-connect.c | 155 +++++++++++++++++++++++++++++++++++++++++-----------
 src/spicy-connect.h |   2 +-
 src/spicy.c         |   6 +-
 3 files changed, 125 insertions(+), 38 deletions(-)

diff --git a/src/spicy-connect.c b/src/spicy-connect.c
index 991aaab..f02976f 100644
--- a/src/spicy-connect.c
+++ b/src/spicy-connect.c
@@ -18,9 +18,22 @@

 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
+#include <gdk/gdkkeysyms.h>
 #include "spice-common.h"
 #include "spicy-connect.h"

+/* Compatability code to allow build on Gtk2 and Gtk3 */
+#ifndef GDK_Escape
+#define GDK_Escape GDK_KEY_Escape
+#endif
+
+typedef struct
+{
+    gboolean connecting;
+    GMainLoop *loop;
+    SpiceSession *session;
+} ConnectionInfo;
+
 static struct {
     const char *text;
     const char *prop;
@@ -31,6 +44,43 @@ static struct {
     { .text = N_("TLS Port"),   .prop = "tls-port"  },
 };

+static void set_connection_info(SpiceSession *session)
+{
+    const gchar *txt;
+    int i;
+
+    for (i = 0; i < SPICE_N_ELEMENTS(connect_entries); i++) {
+        txt = gtk_entry_get_text(GTK_ENTRY(connect_entries[i].entry));
+        g_object_set(session, connect_entries[i].prop, txt, NULL);
+    }
+}
+
+static gboolean close_cb(gpointer data)
+{
+    ConnectionInfo *info = data;
+    info->connecting = FALSE;
+    if (g_main_loop_is_running(info->loop))
+        g_main_loop_quit(info->loop);
+
+    return TRUE;
+}
+
+static gboolean key_pressed_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
+{
+    gboolean tst;
+    if (event->type == GDK_KEY_PRESS) {
+        switch (event->key.keyval) {
+            case GDK_Escape:
+                g_signal_emit_by_name(GTK_WIDGET(data), "delete-event", NULL, &tst);
+                return TRUE;
+            default:
+                return FALSE;
+        }
+    }
+
+    return FALSE;
+}
+
 #ifndef G_OS_WIN32
 static void recent_selection_changed_dialog_cb(GtkRecentChooser *chooser, gpointer data)
 {
@@ -62,32 +112,44 @@ static void recent_selection_changed_dialog_cb(GtkRecentChooser *chooser, gpoint

     gtk_recent_info_unref(info);
 }
+#endif

-static void recent_item_activated_dialog_cb(GtkRecentChooser *chooser, gpointer data)
+static void connect_cb(gpointer data)
 {
-   gtk_dialog_response (GTK_DIALOG (data), GTK_RESPONSE_ACCEPT);
+    ConnectionInfo *info = data;
+    info->connecting = TRUE;
+    set_connection_info(info->session);
+
+    if (g_main_loop_is_running(info->loop))
+        g_main_loop_quit(info->loop);
 }
-#endif

-int spicy_connect_dialog(SpiceSession *session)
+gboolean spicy_connect_dialog(SpiceSession *session)
 {
-    GtkWidget *dialog, *area, *label;
+    GtkWidget *connect_button, *cancel_button, *label;
+    GtkBox *main_box, *recent_box, *button_box;
+    GtkWindow *window;
     GtkTable *table;
-    int i, retval;
+    int i;
+
+    ConnectionInfo info = {
+        FALSE,
+        NULL,
+        session
+    };

     /* Create the widgets */
-    dialog = gtk_dialog_new_with_buttons(_("Connect to SPICE"),
-                                         NULL,
-                                         GTK_DIALOG_DESTROY_WITH_PARENT,
-                                         GTK_STOCK_CANCEL,
-                                         GTK_RESPONSE_REJECT,
-                                         GTK_STOCK_CONNECT,
-                                         GTK_RESPONSE_ACCEPT,
-                                         NULL);
-    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
-    area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
+    window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
+    gtk_window_set_title(window, _("Connect to SPICE"));
+    gtk_window_set_resizable(window, FALSE);
+    gtk_container_set_border_width(GTK_CONTAINER(window), 5);
+
+    main_box = GTK_BOX(gtk_vbox_new(FALSE, 0));
+    gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(main_box));
+
     table = GTK_TABLE(gtk_table_new(3, 2, 0));
-    gtk_box_pack_start(GTK_BOX(area), GTK_WIDGET(table), TRUE, TRUE, 0);
+    gtk_box_pack_start(main_box, GTK_WIDGET(table), FALSE, TRUE, 0);
+    gtk_container_set_border_width(GTK_CONTAINER(table), 5);
     gtk_table_set_row_spacings(table, 5);
     gtk_table_set_col_spacings(table, 5);

@@ -107,16 +169,41 @@ int spicy_connect_dialog(SpiceSession *session)
         }
     }

+    recent_box = GTK_BOX(gtk_vbox_new(FALSE, 0));
+    gtk_box_pack_start(main_box, GTK_WIDGET(recent_box), TRUE, TRUE, 0);
+    gtk_container_set_border_width(GTK_CONTAINER(recent_box), 5);
+
     label = gtk_label_new(_("Recent connections:"));
-    gtk_box_pack_start(GTK_BOX(area), label, TRUE, TRUE, 0);
+    gtk_box_pack_start(recent_box, label, FALSE, TRUE, 0);
     gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
+
+    button_box = GTK_BOX(gtk_hbutton_box_new());
+    gtk_button_box_set_layout(GTK_BUTTON_BOX(button_box), GTK_BUTTONBOX_END);
+    gtk_box_set_spacing(button_box, 5);
+    gtk_container_set_border_width(GTK_CONTAINER(button_box), 5);
+    connect_button = gtk_button_new_with_label(_("Connect"));
+    cancel_button = gtk_button_new_with_label(_("Cancel"));
+    gtk_box_pack_start(button_box, cancel_button, FALSE, TRUE, 0);
+    gtk_box_pack_start(button_box, connect_button, FALSE, TRUE, 1);
+
+    gtk_box_pack_start(main_box, GTK_WIDGET(button_box), FALSE, TRUE, 0);
+
+    g_signal_connect(window, "key-press-event",
+                     G_CALLBACK(key_pressed_cb), window);
+    g_signal_connect_swapped(window, "delete-event",
+                             G_CALLBACK(close_cb), &info);
+    g_signal_connect_swapped(connect_button, "clicked",
+                             G_CALLBACK(connect_cb), &info);
+    g_signal_connect_swapped(cancel_button, "clicked",
+                             G_CALLBACK(close_cb), &info);
+
 #ifndef G_OS_WIN32
     GtkRecentFilter *rfilter;
     GtkWidget *recent;

     recent = GTK_WIDGET(gtk_recent_chooser_widget_new());
     gtk_recent_chooser_set_show_icons(GTK_RECENT_CHOOSER(recent), FALSE);
-    gtk_box_pack_start(GTK_BOX(area), recent, TRUE, TRUE, 0);
+    gtk_box_pack_start(recent_box, recent, TRUE, TRUE, 0);

     rfilter = gtk_recent_filter_new();
     gtk_recent_filter_add_mime_type(rfilter, "application/x-spice");
@@ -124,20 +211,22 @@ int spicy_connect_dialog(SpiceSession *session)
     gtk_recent_chooser_set_local_only(GTK_RECENT_CHOOSER(recent), FALSE);
     g_signal_connect(recent, "selection-changed",
                      G_CALLBACK(recent_selection_changed_dialog_cb), session);
-    g_signal_connect(recent, "item-activated",
-                     G_CALLBACK(recent_item_activated_dialog_cb), dialog);
+    g_signal_connect_swapped(recent, "item-activated",
+                             G_CALLBACK(connect_cb), &info);
 #endif
+
+    for (i = 0; i < SPICE_N_ELEMENTS(connect_entries); i++) {
+        g_signal_connect_swapped(connect_entries[i].entry, "activate",
+                                 G_CALLBACK(connect_cb), &info);
+    }
+
     /* show and wait for response */
-    gtk_widget_show_all(dialog);
-    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
-        for (i = 0; i < SPICE_N_ELEMENTS(connect_entries); i++) {
-            const gchar *txt;
-            txt = gtk_entry_get_text(GTK_ENTRY(connect_entries[i].entry));
-            g_object_set(session, connect_entries[i].prop, txt, NULL);
-        }
-        retval = 0;
-    } else
-        retval = -1;
-    gtk_widget_destroy(dialog);
-    return retval;
+    gtk_widget_show_all(GTK_WIDGET(window));
+
+    info.loop = g_main_loop_new(NULL, FALSE);
+    g_main_loop_run(info.loop);
+
+    gtk_widget_destroy(GTK_WIDGET(window));
+
+    return info.connecting;
 }
diff --git a/src/spicy-connect.h b/src/spicy-connect.h
index e693f2b..56b2d80 100644
--- a/src/spicy-connect.h
+++ b/src/spicy-connect.h
@@ -21,6 +21,6 @@

 #include "spice-widget.h"

-int spicy_connect_dialog(SpiceSession *session);
+gboolean spicy_connect_dialog(SpiceSession *session);

 #endif
diff --git a/src/spicy.c b/src/spicy.c
index f58d33e..3442db0 100644
--- a/src/spicy.c
+++ b/src/spicy.c
@@ -1119,8 +1119,7 @@ static void main_channel_event(SpiceChannel *channel, SpiceChannelEvent event,
             g_message("channel error: %s", error->message);
         }

-        rc = spicy_connect_dialog(conn->session);
-        if (rc == 0) {
+        if (spicy_connect_dialog(conn->session)) {
             connection_connect(conn);
         } else {
             connection_disconnect(conn);
@@ -1699,8 +1698,7 @@ int main(int argc, char *argv[])
     /* If user doesn't provide hostname and port, show the dialog window
        instead of connecting to server automatically */
     if ((host == NULL || (port == NULL && tls_port == NULL)) && unix_path == NULL) {
-        int ret = spicy_connect_dialog(conn->session);
-        if (ret != 0) {
+        if (!spicy_connect_dialog(conn->session)) {
             exit(0);
         }
     }
--
2.4.3



More information about the Spice-devel mailing list