Commit 0ec68392 authored by Paolo Bacchilega's avatar Paolo Bacchilega

make the accel dialog a gobject type to allow reuse in other places

parent a20f5d22
......@@ -24,6 +24,7 @@
#include <glib.h>
#include <gtk/gtk.h>
#include "gth-accel-button.h"
#include "gth-accel-dialog.h"
#include "gtk-utils.h"
......@@ -182,13 +183,20 @@ accel_dialog_response_cb (GtkDialog *dialog,
gint response_id,
gpointer user_data)
{
GthAccelButton *accel_button = user_data;
GthAccelButton *accel_button = user_data;
guint keycode;
GdkModifierType modifiers;
switch (response_id) {
case GTK_RESPONSE_OK:
if (gth_accel_dialog_get_accel (GTH_ACCEL_DIALOG (dialog), &keycode, &modifiers))
gth_accel_button_set_accelerator (accel_button, keycode, modifiers);
gtk_widget_destroy (GTK_WIDGET (dialog));
break;
case GTK_RESPONSE_CANCEL:
gtk_widget_destroy (GTK_WIDGET (dialog));
break;
case _RESPONSE_RESET:
case GTH_ACCEL_BUTTON_RESPONSE_DELETE:
gth_accel_button_set_accelerator (accel_button, 0, 0);
gtk_widget_destroy (GTK_WIDGET (dialog));
break;
......@@ -196,68 +204,22 @@ accel_dialog_response_cb (GtkDialog *dialog,
}
static gboolean
accel_dialog_keypress_cb (GtkWidget *widget,
GdkEventKey *event,
gpointer user_data)
{
GthAccelButton *accel_button = user_data;
GdkModifierType modifiers;
if (event->keyval == GDK_KEY_Escape)
return FALSE;
modifiers = event->state & gtk_accelerator_get_default_mod_mask ();
if (gth_accel_button_set_accelerator (accel_button, event->keyval, modifiers))
gtk_widget_destroy (widget);
return TRUE;
}
static void
button_clicked_cb (GtkButton *button,
gpointer user_data)
{
GtkWidget *dialog, *box, *label, *secondary_label, *content_area;
dialog = g_object_new (GTK_TYPE_DIALOG,
"use-header-bar", _gtk_settings_get_dialogs_use_header (),
"modal", TRUE,
"transient-for", _gtk_widget_get_toplevel_if_window (GTK_WIDGET (button)),
"resizable", FALSE,
"title", _("Shortcut"),
NULL);
gtk_dialog_add_buttons (GTK_DIALOG (dialog),
_GTK_LABEL_CANCEL, GTK_RESPONSE_CANCEL,
! gth_accel_button_get_valid (GTH_ACCEL_BUTTON (button)) ? NULL : _GTK_LABEL_DELETE,
_RESPONSE_RESET,
NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
label = gtk_label_new (_("Press a combination of keys to use as shortcut."));
secondary_label = gtk_label_new (_("Press Esc to cancel"));
gtk_style_context_add_class (gtk_widget_get_style_context (secondary_label), "dim-label");
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 50);
gtk_widget_set_margin_top (box, 50);
gtk_widget_set_margin_bottom (box, 50);
gtk_widget_set_margin_start (box, 50);
gtk_widget_set_margin_end (box, 50);
gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (box), secondary_label, TRUE, TRUE, 0);
GthAccelButton *accel_button = GTH_ACCEL_BUTTON (button);
GtkWidget *dialog;
dialog = gth_accel_dialog_new (_("Shortcut"),
_gtk_widget_get_toplevel_if_window (GTK_WIDGET (button)),
accel_button->priv->keyval,
accel_button->priv->modifiers);
g_signal_connect (dialog,
"response",
G_CALLBACK (accel_dialog_response_cb),
button);
g_signal_connect (dialog,
"key-press-event",
G_CALLBACK (accel_dialog_keypress_cb),
button);
gtk_container_add (GTK_CONTAINER (content_area), box);
gtk_widget_show_all (dialog);
gtk_widget_show (dialog);
}
......
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* GThumb
*
* Copyright (C) 2019 The Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include "gth-accel-dialog.h"
#include "gtk-utils.h"
#include "gth-shortcut.h"
struct _GthAccelDialogPrivate {
guint keycode;
GdkModifierType modifiers;
gboolean valid;
};
G_DEFINE_TYPE_WITH_CODE (GthAccelDialog,
gth_accel_dialog,
GTK_TYPE_DIALOG,
G_ADD_PRIVATE (GthAccelDialog))
static void
gth_accel_dialog_finalize (GObject *object)
{
/*GthAccelDialog *self;
self = GTH_ACCEL_DIALOG (object);*/
G_OBJECT_CLASS (gth_accel_dialog_parent_class)->finalize (object);
}
static void
gth_accel_dialog_class_init (GthAccelDialogClass *class)
{
GObjectClass *object_class;
object_class = (GObjectClass*) class;
object_class->finalize = gth_accel_dialog_finalize;
}
static void
gth_accel_dialog_init (GthAccelDialog *self)
{
self->priv = gth_accel_dialog_get_instance_private (self);
self->priv->keycode = 0;
self->priv->modifiers = 0;
self->priv->valid = FALSE;
}
static gboolean
accel_dialog_keypress_cb (GtkWidget *widget,
GdkEventKey *event,
gpointer user_data)
{
GthAccelDialog *self = user_data;
GdkModifierType modifiers;
if (event->keyval == GDK_KEY_Escape)
return FALSE;
modifiers = event->state & gtk_accelerator_get_default_mod_mask ();
if (gth_shortcut_valid (event->keyval, modifiers)) {
self->priv->keycode = event->keyval;
self->priv->modifiers = modifiers;
self->priv->valid = TRUE;
gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
}
else
self->priv->valid = FALSE;
return TRUE;
}
static void
gth_accel_dialog_construct (GthAccelDialog *self,
const char *title,
GtkWindow *parent,
guint keycode,
GdkModifierType modifiers)
{
gboolean valid;
GtkWidget *box;
GtkWidget *label;
GtkWidget *secondary_label;
GtkWidget *content_area;
valid = gth_shortcut_valid (keycode, modifiers);
gtk_dialog_add_buttons (GTK_DIALOG (self),
_GTK_LABEL_CANCEL, GTK_RESPONSE_CANCEL,
! valid ? NULL : _GTK_LABEL_DELETE, GTH_ACCEL_BUTTON_RESPONSE_DELETE,
NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (self));
label = gtk_label_new (_("Press a combination of keys to use as shortcut."));
secondary_label = gtk_label_new (_("Press Esc to cancel"));
gtk_style_context_add_class (gtk_widget_get_style_context (secondary_label), "dim-label");
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 50);
gtk_widget_set_margin_top (box, 50);
gtk_widget_set_margin_bottom (box, 50);
gtk_widget_set_margin_start (box, 50);
gtk_widget_set_margin_end (box, 50);
gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (box), secondary_label, TRUE, TRUE, 0);
g_signal_connect (self,
"key-press-event",
G_CALLBACK (accel_dialog_keypress_cb),
self);
gtk_widget_show (box);
gtk_widget_show (label);
gtk_widget_show (secondary_label);
gtk_container_add (GTK_CONTAINER (content_area), box);
}
GtkWidget *
gth_accel_dialog_new (const char *title,
GtkWindow *parent,
guint keycode,
GdkModifierType modifiers)
{
GthAccelDialog *self;
self = g_object_new (GTH_TYPE_ACCEL_DIALOG,
"use-header-bar", _gtk_settings_get_dialogs_use_header (),
"modal", TRUE,
"transient-for", parent,
"resizable", FALSE,
"title", title,
NULL);
gth_accel_dialog_construct (self, title, parent, keycode, modifiers);
return (GtkWidget *) self;
}
gboolean
gth_accel_dialog_get_accel (GthAccelDialog *self,
guint *keycode,
GdkModifierType *modifiers)
{
if (self->priv->valid) {
*keycode = self->priv->keycode;
*modifiers = self->priv->modifiers;
}
else {
*keycode = 0;
*modifiers = 0;
}
return self->priv->valid;
}
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* GThumb
*
* Copyright (C) 2019 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GTH_ACCEL_DIALOG_H
#define GTH_ACCEL_DIALOG_H
#include <glib-object.h>
#include <gtk/gtk.h>
G_BEGIN_DECLS
enum {
GTH_ACCEL_BUTTON_RESPONSE_DELETE
};
#define GTH_TYPE_ACCEL_DIALOG (gth_accel_dialog_get_type ())
#define GTH_ACCEL_DIALOG(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTH_TYPE_ACCEL_DIALOG, GthAccelDialog))
#define GTH_ACCEL_DIALOG_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GTH_TYPE_ACCEL_DIALOG, GthAccelDialogClass))
#define GTH_IS_ACCEL_DIALOG(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTH_TYPE_ACCEL_DIALOG))
#define GTH_IS_ACCEL_DIALOG_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GTH_TYPE_ACCEL_DIALOG))
#define GTH_ACCEL_DIALOG_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS((o), GTH_TYPE_ACCEL_DIALOG, GthAccelDialogClass))
typedef struct _GthAccelDialog GthAccelDialog;
typedef struct _GthAccelDialogPrivate GthAccelDialogPrivate;
typedef struct _GthAccelDialogClass GthAccelDialogClass;
struct _GthAccelDialog {
GtkDialog __parent;
GthAccelDialogPrivate *priv;
};
struct _GthAccelDialogClass {
GtkDialogClass __parent_class;
};
GType gth_accel_dialog_get_type (void) G_GNUC_CONST;
GtkWidget * gth_accel_dialog_new (const char *title,
GtkWindow *parent,
guint keycode,
GdkModifierType modifiers);
gboolean gth_accel_dialog_get_accel (GthAccelDialog *self,
guint *keycode,
GdkModifierType *modifiers);
G_END_DECLS
#endif /* GTH_ACCEL_DIALOG_H */
......@@ -12,6 +12,7 @@ public_header_files = [
'gnome-desktop-thumbnail.h',
'gsignature.h',
'gth-accel-button.h',
'gth-accel-dialog.h',
'gth-auto-paned.h',
'gth-async-task.h',
'gth-buffer-data.h',
......@@ -167,6 +168,7 @@ source_files = files(
'glib-utils.c',
'gsignature.c',
'gth-accel-button.c',
'gth-accel-dialog.c',
'gth-application.c',
'gth-auto-paned.c',
'gth-async-task.c',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment