[virt-tools-list] [remote-viewer PATCH 4/7 v2] remote-viewer-connect: Changed dialog into a window

Lukas Venhoda lvenhoda at redhat.com
Thu Jun 11 14:28:39 UTC 2015


Changed connect dialog from GtkDialog to a GtkWindow.
Added the necessary signals and buttons, to keep the
behaviour of a dialog. (ESC to close, ENTER to submit)
---
Changes since v1
 - Now only contains changing dialog to window
    - Change to XML in later patch
---
 src/remote-viewer-connect.c | 135 ++++++++++++++++++++++++++++++++++++--------
 src/remote-viewer-connect.h |   2 +-
 src/remote-viewer.c         |   4 +-
 3 files changed, 114 insertions(+), 27 deletions(-)

diff --git a/src/remote-viewer-connect.c b/src/remote-viewer-connect.c
index 6cfb7b1..61f4691 100644
--- a/src/remote-viewer-connect.c
+++ b/src/remote-viewer-connect.c
@@ -20,6 +20,61 @@

 #include "remote-viewer-connect.h"
 #include <glib/gi18n.h>
+#include <gdk/gdkkeysyms.h>
+
+typedef struct
+{
+    GtkResponseType response;
+    GMainLoop *loop;
+} ConnectionInfo;
+
+static void
+shutdown_loop(GMainLoop *loop)
+{
+    if (g_main_loop_is_running(loop))
+        g_main_loop_quit(loop);
+}
+
+static gboolean
+window_deleted_cb(ConnectionInfo *ci)
+{
+    ci->response = GTK_RESPONSE_CANCEL;
+    shutdown_loop(ci->loop);
+    return TRUE;
+}
+
+static gboolean
+key_pressed_cb(GtkWidget *widget G_GNUC_UNUSED, GdkEvent *event, gpointer data)
+{
+    GtkWidget *window = data;
+    gboolean retval;
+    if (event->type == GDK_KEY_PRESS) {
+        switch (event->key.keyval) {
+            case GDK_KEY_Escape:
+                g_signal_emit_by_name(window, "delete-event", NULL, &retval);
+                return TRUE;
+            default:
+                return FALSE;
+        }
+    }
+
+    return FALSE;
+}
+
+static void
+connect_button_clicked_cb(GtkButton *button G_GNUC_UNUSED, gpointer data)
+{
+    ConnectionInfo *ci = data;
+    ci->response = GTK_RESPONSE_OK;
+    shutdown_loop(ci->loop);
+}
+
+static void
+connect_dialog_run(ConnectionInfo *ci)
+{
+    ci->loop = g_main_loop_new(NULL, FALSE);
+    g_main_loop_run(ci->loop);
+}

 static void
 entry_icon_release_cb(GtkEntry* entry, gpointer data G_GNUC_UNUSED)
@@ -42,6 +97,14 @@ entry_changed_cb(GtkEditable* entry, gpointer data G_GNUC_UNUSED)
 }

 static void
+entry_activated_cb(GtkEntry *entry G_GNUC_UNUSED, gpointer data)
+{
+    ConnectionInfo *ci = data;
+    ci->response = GTK_RESPONSE_OK;
+    shutdown_loop(ci->loop);
+}
+
+static void
 recent_selection_changed_dialog_cb(GtkRecentChooser *chooser, gpointer data)
 {
     GtkRecentInfo *info;
@@ -63,7 +126,9 @@ recent_selection_changed_dialog_cb(GtkRecentChooser *chooser, gpointer data)
 static void
 recent_item_activated_dialog_cb(GtkRecentChooser *chooser G_GNUC_UNUSED, gpointer data)
 {
-   gtk_dialog_response(GTK_DIALOG (data), GTK_RESPONSE_ACCEPT);
+    ConnectionInfo *ci = data;
+    ci->response = GTK_RESPONSE_OK;
+    shutdown_loop(ci->loop);
 }

 static void
@@ -102,31 +167,27 @@ make_label_bold(GtkLabel* label)
 * @return GTK_RESPONSE_OK if Connect or ENTER is pressed
 * @return GTK_RESPONSE_CANCEL if Cancel is pressed or dialog is closed
 */
+
 GtkResponseType
-remote_viewer_connect_dialog(GtkWindow *main_window, gchar **uri)
+remote_viewer_connect_dialog(gchar **uri)
 {
-    GtkWidget *dialog, *area, *box, *label, *entry, *recent;
+    GtkWidget *window,*box, *label, *entry, *recent, *connect_button, *cancel_button, *button_box;
 #if !GTK_CHECK_VERSION(3, 0, 0)
     GtkWidget *alignment;
 #endif
     GtkRecentFilter *rfilter;
-    GtkResponseType retval;
+
+    ConnectionInfo ci = {
+        GTK_RESPONSE_NONE,
+        NULL
+    };

     /* Create the widgets */
-    dialog = gtk_dialog_new_with_buttons(_("Connection details"),
-                                         main_window,
-                                         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);
-    gtk_container_set_border_width(GTK_CONTAINER(dialog), 5);
-    area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
+    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+    gtk_container_set_border_width(GTK_CONTAINER(window), 5);
     box = gtk_vbox_new(FALSE, 6);
     gtk_container_set_border_width(GTK_CONTAINER(box), 5);
-    gtk_box_pack_start(GTK_BOX(area), box, TRUE, TRUE, 0);
+    gtk_container_add(GTK_CONTAINER(window), box);

     label = gtk_label_new_with_mnemonic(_("_Connection Address"));
     gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
@@ -170,27 +231,55 @@ remote_viewer_connect_dialog(GtkWindow *main_window, gchar **uri)
     gtk_recent_filter_add_mime_type(rfilter, "application/x-virt-viewer");
     gtk_recent_chooser_set_filter(GTK_RECENT_CHOOSER(recent), rfilter);
     gtk_recent_chooser_set_local_only(GTK_RECENT_CHOOSER(recent), FALSE);
+
+    button_box = gtk_hbutton_box_new();
+    gtk_button_box_set_layout(GTK_BUTTON_BOX(button_box), GTK_BUTTONBOX_END);
+    connect_button = gtk_button_new_with_label("Connect");
+    cancel_button = gtk_button_new_with_label("Cancel");
+    gtk_box_pack_start(GTK_BOX(button_box), cancel_button, FALSE, TRUE, 0);
+    gtk_box_pack_start(GTK_BOX(button_box), connect_button, FALSE, TRUE, 1);
+
+    gtk_box_pack_start(GTK_BOX(box), button_box, FALSE, TRUE, 0);
+
+    g_signal_connect(window, "key-press-event",
+                     G_CALLBACK(key_pressed_cb), window);
+    g_signal_connect(connect_button, "clicked",
+                     G_CALLBACK(connect_button_clicked_cb), &ci);
+
+    /* make sure that user_data is passed as first parameter */
+    g_signal_connect_swapped(cancel_button, "clicked",
+                             G_CALLBACK(window_deleted_cb), &ci);
+    g_signal_connect_swapped(window, "delete-event",
+                             G_CALLBACK(window_deleted_cb), &ci);
+
+    g_signal_connect(entry, "activate",
+                     G_CALLBACK(entry_activated_cb), &ci);
+    g_signal_connect(entry, "changed",
+                     G_CALLBACK(entry_changed_cb), connect_button);
+    g_signal_connect(entry, "icon-release",
+                     G_CALLBACK(entry_icon_release_cb), entry);
+
     g_signal_connect(recent, "selection-changed",
                      G_CALLBACK(recent_selection_changed_dialog_cb), entry);
     g_signal_connect(recent, "item-activated",
-                     G_CALLBACK(recent_item_activated_dialog_cb), dialog);
+                     G_CALLBACK(recent_item_activated_dialog_cb), &ci);

     /* show and wait for response */
-    gtk_widget_show_all(dialog);
+    gtk_widget_show_all(window);

     g_free(*uri);

-    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
+    connect_dialog_run(&ci);
+    if (ci.response == GTK_RESPONSE_OK) {
         *uri = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
         g_strstrip(*uri);
-        retval = GTK_RESPONSE_OK;
     } else {
         *uri = NULL;
-        retval = GTK_RESPONSE_CANCEL;
     }
-    gtk_widget_destroy(dialog);

-    return retval;
+    gtk_widget_destroy(window);
+
+    return ci.response;
 }

 /*
diff --git a/src/remote-viewer-connect.h b/src/remote-viewer-connect.h
index f095f57..1a196c0 100644
--- a/src/remote-viewer-connect.h
+++ b/src/remote-viewer-connect.h
@@ -23,7 +23,7 @@

 #include <gtk/gtk.h>

-GtkResponseType remote_viewer_connect_dialog(GtkWindow *main_window, gchar **uri);
+GtkResponseType remote_viewer_connect_dialog(gchar **uri);

 #endif /* REMOTE_VIEWER_CONNECT_H */

diff --git a/src/remote-viewer.c b/src/remote-viewer.c
index ca8a3aa..1d32de2 100644
--- a/src/remote-viewer.c
+++ b/src/remote-viewer.c
@@ -1065,7 +1065,6 @@ remote_viewer_start(VirtViewerApp *app, GError **err)

     RemoteViewer *self = REMOTE_VIEWER(app);
     RemoteViewerPrivate *priv = self->priv;
-    VirtViewerWindow *main_window;
     GFile *file = NULL;
     VirtViewerFile *vvfile = NULL;
     gboolean ret = FALSE;
@@ -1096,9 +1095,8 @@ remote_viewer_start(VirtViewerApp *app, GError **err)
     } else {
 #endif
 retry_dialog:
-        main_window = virt_viewer_app_get_main_window(app);
         if (priv->open_recent_dialog) {
-            if (remote_viewer_connect_dialog(virt_viewer_window_get_window(main_window), &guri) != GTK_RESPONSE_OK) {
+            if (remote_viewer_connect_dialog(&guri) != GTK_RESPONSE_OK) {
                 g_set_error_literal(&error,
                             VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_CANCELLED,
                             _("No connection was chosen"));
--
2.4.2




More information about the virt-tools-list mailing list