[virt-tools-list] [remote-viewer PATCH 4/7 v3] remote-viewer-connect: Changed dialog into a window
Pavel Grunt
pgrunt at redhat.com
Tue Jun 16 12:16:02 UTC 2015
On Mon, 2015-06-15 at 12:47 +0200, Lukas Venhoda wrote:
> 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 v2
> - Fixed missing space
> - Changed response type from GtkResponseType to gboolean
>
> Changes since v1
> - Now only contains changing dialog to window
> - Change to XML in later patch
> ---
> src/remote-viewer-connect.c | 134 ++++++++++++++++++++++++++++++++++++-------
> -
> src/remote-viewer-connect.h | 2 +-
> src/remote-viewer.c | 4 +-
> 3 files changed, 113 insertions(+), 27 deletions(-)
>
> diff --git a/src/remote-viewer-connect.c b/src/remote-viewer-connect.c
> index a4bce27..601c314 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
> +{
> + gboolean 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 = FALSE;
> + 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 = TRUE;
> + 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 = TRUE;
> + 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 = TRUE;
> + shutdown_loop(ci->loop);
> }
>
> static void
> @@ -103,32 +168,27 @@ make_label_bold(GtkLabel* label)
> * @return FALSE if Cancel is pressed or dialog is closed
> */
> gboolean
> -remote_viewer_connect_dialog(GtkWindow *main_window, gchar **uri)
> +remote_viewer_connect_dialog(gchar **uri)
main_window should be also removed from the comment, it can be done before
pushing
-* @param main_window Parent window of the dialog
> {
> - 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;
> - gboolean retval;
> +
> + ConnectionInfo ci = {
> + FALSE,
> + NULL
> + };
>
> g_return_val_if_fail(uri && *uri == NULL, FALSE);
>
> /* 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);
> @@ -172,25 +232,53 @@ 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);
>
> - if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
> + connect_dialog_run(&ci);
> + if (ci.response == TRUE) {
> *uri = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
> g_strstrip(*uri);
> - retval = TRUE;
> } else {
> *uri = NULL;
> - retval = FALSE;
> }
> - 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 99779ef..f00581e 100644
> --- a/src/remote-viewer-connect.h
> +++ b/src/remote-viewer-connect.h
> @@ -23,7 +23,7 @@
>
> #include <gtk/gtk.h>
>
> -gboolean remote_viewer_connect_dialog(GtkWindow *main_window, gchar **uri);
> +gboolean remote_viewer_connect_dialog(gchar **uri);
>
> #endif /* REMOTE_VIEWER_CONNECT_H */
>
> diff --git a/src/remote-viewer.c b/src/remote-viewer.c
> index 04d7bf0..8e4754f 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)) {
> + if (!remote_viewer_connect_dialog(&guri)) {
> g_set_error_literal(&error,
> VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_CANCELLED,
> _("No connection was chosen"));
> --
> 2.4.2
>
> _______________________________________________
> virt-tools-list mailing list
> virt-tools-list at redhat.com
> https://www.redhat.com/mailman/listinfo/virt-tools-list
More information about the virt-tools-list
mailing list