gtk2/gtk/gtkcellrendereraccel.c

611 lines
18 KiB
C
Raw Normal View History

/* gtkcellrendereraccel.h
2005-09-11 05:43:19 +00:00
* Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "gtkintl.h"
#include "gtkaccelgroup.h"
#include "gtkmarshalers.h"
#include "gtkcellrendereraccel.h"
2005-09-11 05:43:19 +00:00
#include "gtklabel.h"
#include "gtkeventbox.h"
#include "gtkprivate.h"
#include "gdk/gdkkeysyms.h"
#include "gtkalias.h"
static void gtk_cell_renderer_accel_finalize (GObject *object);
static GtkCellEditable *gtk_cell_renderer_accel_start_editing (GtkCellRenderer *cell,
GdkEvent *event,
GtkWidget *widget,
const gchar *path,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GtkCellRendererState flags);
static void gtk_cell_renderer_accel_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
static void gtk_cell_renderer_accel_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_cell_renderer_accel_get_size (GtkCellRenderer *cell,
GtkWidget *widget,
GdkRectangle *cell_area,
gint *x_offset,
gint *y_offset,
gint *width,
gint *height);
2005-09-11 05:43:19 +00:00
enum {
ACCEL_EDITED,
ACCEL_CLEARED,
LAST_SIGNAL
};
enum {
PROP_0,
PROP_ACCEL_KEY,
PROP_ACCEL_MODS,
PROP_KEYCODE,
PROP_ACCEL_MODE
};
static guint signals[LAST_SIGNAL] = { 0 };
2006-05-14 04:25:34 +00:00
G_DEFINE_TYPE (GtkCellRendererAccel, gtk_cell_renderer_accel, GTK_TYPE_CELL_RENDERER_TEXT)
2005-09-11 05:43:19 +00:00
static void
gtk_cell_renderer_accel_init (GtkCellRendererAccel *cell_accel)
2005-09-11 05:43:19 +00:00
{
}
static void
gtk_cell_renderer_accel_class_init (GtkCellRendererAccelClass *cell_accel_class)
2005-09-11 05:43:19 +00:00
{
GObjectClass *object_class;
GtkCellRendererClass *cell_renderer_class;
object_class = G_OBJECT_CLASS (cell_accel_class);
cell_renderer_class = GTK_CELL_RENDERER_CLASS (cell_accel_class);
2005-09-11 05:43:19 +00:00
GTK_CELL_RENDERER_CLASS (cell_accel_class)->start_editing = gtk_cell_renderer_accel_start_editing;
2005-09-11 05:43:19 +00:00
object_class->set_property = gtk_cell_renderer_accel_set_property;
object_class->get_property = gtk_cell_renderer_accel_get_property;
cell_renderer_class->get_size = gtk_cell_renderer_accel_get_size;
2005-09-11 05:43:19 +00:00
object_class->finalize = gtk_cell_renderer_accel_finalize;
2005-09-11 05:43:19 +00:00
/**
* GtkCellRendererAccel:accel-key:
2005-09-11 05:43:19 +00:00
*
* The keyval of the accelerator.
*
* Since: 2.10
*/
g_object_class_install_property (object_class,
PROP_ACCEL_KEY,
g_param_spec_uint ("accel-key",
P_("Accelerator key"),
P_("The keyval of the accelerator"),
0,
G_MAXINT,
0,
GTK_PARAM_READWRITE));
/**
* GtkCellRendererAccel:accel-mods:
2005-09-11 05:43:19 +00:00
*
* The modifier mask of the accelerator.
*
* Since: 2.10
*/
g_object_class_install_property (object_class,
PROP_ACCEL_MODS,
g_param_spec_flags ("accel-mods",
P_("Accelerator modifiers"),
P_("The modifier mask of the accelerator"),
GDK_TYPE_MODIFIER_TYPE,
0,
GTK_PARAM_READWRITE));
/**
* GtkCellRendererAccel:keycode:
2005-09-11 05:43:19 +00:00
*
* The hardware keycode of the accelerator. Note that the hardware keycode is
* only relevant if the key does not have a keyval. Normally, the keyboard
* configuration should assign keyvals to all keys.
*
* Since: 2.10
*/
g_object_class_install_property (object_class,
PROP_KEYCODE,
g_param_spec_uint ("keycode",
P_("Accelerator keycode"),
P_("The hardware keycode of the accelerator"),
0,
G_MAXINT,
0,
GTK_PARAM_READWRITE));
/**
* GtkCellRendererAccel:accel-mode:
2005-09-11 05:43:19 +00:00
*
* Determines if the edited accelerators are GTK+ accelerators. If
* they are, consumed modifiers are suppressed, only accelerators
* accepted by GTK+ are allowed, and the accelerators are rendered
* in the same way as they are in menus.
*
* Since: 2.10
*/
g_object_class_install_property (object_class,
PROP_ACCEL_MODE,
g_param_spec_enum ("accel-mode",
P_("Accelerator Mode"),
P_("The type of accelerators"),
GTK_TYPE_CELL_RENDERER_ACCEL_MODE,
GTK_CELL_RENDERER_ACCEL_MODE_GTK,
2005-09-11 05:43:19 +00:00
GTK_PARAM_READWRITE));
/**
* GtkCellRendererAccel::accel-edited:
* @accel: the object reveiving the signal
2005-09-11 05:43:19 +00:00
* @path_string: the path identifying the row of the edited cell
* @accel_key: the new accelerator keyval
* @accel_mods: the new acclerator modifier mask
* @hardware_keycode: the keycode of the new accelerator
*
* Gets emitted when the user has selected a new accelerator.
*
* Since: 2.10
*/
signals[ACCEL_EDITED] = g_signal_new (I_("accel-edited"),
GTK_TYPE_CELL_RENDERER_ACCEL,
2005-09-11 05:43:19 +00:00
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkCellRendererAccelClass, accel_edited),
2005-09-11 05:43:19 +00:00
NULL, NULL,
_gtk_marshal_VOID__STRING_UINT_FLAGS_UINT,
G_TYPE_NONE, 4,
G_TYPE_STRING,
G_TYPE_UINT,
GDK_TYPE_MODIFIER_TYPE,
G_TYPE_UINT);
/**
* GtkCellRendererAccel::accel-cleared:
* @accel: the object reveiving the signal
2005-09-11 05:43:19 +00:00
* @path_string: the path identifying the row of the edited cell
*
* Gets emitted when the user has removed the accelerator.
*
* Since: 2.10
*/
signals[ACCEL_CLEARED] = g_signal_new (I_("accel-cleared"),
GTK_TYPE_CELL_RENDERER_ACCEL,
2005-09-11 05:43:19 +00:00
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkCellRendererAccelClass, accel_cleared),
2005-09-11 05:43:19 +00:00
NULL, NULL,
g_cclosure_marshal_VOID__STRING,
G_TYPE_NONE, 1,
G_TYPE_STRING);
}
/**
* gtk_cell_renderer_accel_new:
2005-09-11 05:43:19 +00:00
*
* Creates a new #GtkCellRendererAccel.
2005-09-11 05:43:19 +00:00
*
* Returns: the new cell renderer
*
* Since: 2.10
*/
GtkCellRenderer *
gtk_cell_renderer_accel_new (void)
2005-09-11 05:43:19 +00:00
{
return GTK_CELL_RENDERER (g_object_new (GTK_TYPE_CELL_RENDERER_ACCEL, NULL));
2005-09-11 05:43:19 +00:00
}
static void
gtk_cell_renderer_accel_finalize (GObject *object)
2005-09-11 05:43:19 +00:00
{
(* G_OBJECT_CLASS (gtk_cell_renderer_accel_parent_class)->finalize) (object);
2005-09-11 05:43:19 +00:00
}
static gchar *
convert_keysym_state_to_string (GtkCellRendererAccel *accel,
guint keysym,
GdkModifierType mask,
guint keycode)
2005-09-11 05:43:19 +00:00
{
if (keysym == 0 && keycode == 0)
/* This label is displayed in a treeview cell displaying
* a disabled accelerator key combination. Only include
* the text after the | in the translation.
*/
return g_strdup (Q_("Accelerator|Disabled"));
else
{
if (accel->accel_mode == GTK_CELL_RENDERER_ACCEL_MODE_GTK)
2005-09-11 05:43:19 +00:00
return gtk_accelerator_get_label (keysym, mask);
else
{
gchar *name;
name = gtk_accelerator_name (keysym, mask);
if (keysym == 0)
{
gchar *tmp;
tmp = name;
name = g_strdup_printf ("%s0x%02x", tmp, keycode);
g_free (tmp);
}
return name;
}
}
}
static void
gtk_cell_renderer_accel_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
2005-09-11 05:43:19 +00:00
{
GtkCellRendererAccel *accel;
2005-09-11 05:43:19 +00:00
g_return_if_fail (GTK_IS_CELL_RENDERER_ACCEL (object));
2005-09-11 05:43:19 +00:00
accel = GTK_CELL_RENDERER_ACCEL (object);
2005-09-11 05:43:19 +00:00
switch (param_id)
{
case PROP_ACCEL_KEY:
g_value_set_uint (value, accel->accel_key);
2005-09-11 05:43:19 +00:00
break;
case PROP_ACCEL_MODS:
g_value_set_flags (value, accel->accel_mods);
2005-09-11 05:43:19 +00:00
break;
case PROP_ACCEL_MODE:
g_value_set_enum (value, accel->accel_mode);
2005-09-11 05:43:19 +00:00
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
}
}
static void
gtk_cell_renderer_accel_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
2005-09-11 05:43:19 +00:00
{
GtkCellRendererAccel *accel;
2005-09-11 05:43:19 +00:00
gboolean changed = FALSE;
g_return_if_fail (GTK_IS_CELL_RENDERER_ACCEL (object));
2005-09-11 05:43:19 +00:00
accel = GTK_CELL_RENDERER_ACCEL (object);
2005-09-11 05:43:19 +00:00
switch (param_id)
{
case PROP_ACCEL_KEY:
{
guint accel_key = g_value_get_uint (value);
if (accel->accel_key != accel_key)
2005-09-11 05:43:19 +00:00
{
accel->accel_key = accel_key;
2005-09-11 05:43:19 +00:00
changed = TRUE;
}
}
break;
case PROP_ACCEL_MODS:
{
guint accel_mods = g_value_get_flags (value);
if (accel->accel_mods != accel_mods)
2005-09-11 05:43:19 +00:00
{
accel->accel_mods = accel_mods;
2005-09-11 05:43:19 +00:00
changed = TRUE;
}
}
break;
case PROP_KEYCODE:
{
guint keycode = g_value_get_uint (value);
if (accel->keycode != keycode)
2005-09-11 05:43:19 +00:00
{
accel->keycode = keycode;
2005-09-11 05:43:19 +00:00
changed = TRUE;
}
}
break;
case PROP_ACCEL_MODE:
accel->accel_mode = g_value_get_enum (value);
2005-09-11 05:43:19 +00:00
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
}
if (changed)
{
gchar *text;
text = convert_keysym_state_to_string (accel, accel->accel_key, accel->accel_mods, accel->keycode);
g_object_set (accel, "text", text, NULL);
2005-09-11 05:43:19 +00:00
g_free (text);
}
}
static void
gtk_cell_renderer_accel_get_size (GtkCellRenderer *cell,
GtkWidget *widget,
GdkRectangle *cell_area,
gint *x_offset,
gint *y_offset,
gint *width,
gint *height)
2005-09-11 05:43:19 +00:00
{
GtkCellRendererAccel *accel = (GtkCellRendererAccel *) cell;
2005-09-11 05:43:19 +00:00
GtkRequisition requisition;
if (accel->sizing_label == NULL)
accel->sizing_label = gtk_label_new (_("New accelerator..."));
2005-09-11 05:43:19 +00:00
gtk_widget_size_request (accel->sizing_label, &requisition);
(* GTK_CELL_RENDERER_CLASS (gtk_cell_renderer_accel_parent_class)->get_size) (cell, widget, cell_area,
x_offset, y_offset, width, height);
2005-09-11 05:43:19 +00:00
/* FIXME: need to take the cell_area et al. into account */
if (width)
*width = MAX (*width, requisition.width);
if (height)
*height = MAX (*height, requisition.height);
}
static gboolean
grab_key_callback (GtkWidget *widget,
GdkEventKey *event,
void *data)
{
GdkModifierType accel_mods = 0;
guint accel_key;
GtkCellRendererAccel *accel;
2005-09-11 05:43:19 +00:00
char *path;
gboolean edited;
gboolean cleared;
GdkModifierType consumed_modifiers;
GdkDisplay *display;
accel = GTK_CELL_RENDERER_ACCEL (data);
2005-09-11 05:43:19 +00:00
display = gtk_widget_get_display (widget);
if (event->is_modifier)
return TRUE;
edited = FALSE;
cleared = FALSE;
gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
event->hardware_keycode,
event->state,
event->group,
NULL, NULL, NULL, &consumed_modifiers);
accel_key = gdk_keyval_to_lower (event->keyval);
if (accel_key == GDK_ISO_Left_Tab)
accel_key = GDK_Tab;
accel_mods = event->state & gtk_accelerator_get_default_mod_mask ();
/* Filter consumed modifiers
*/
if (accel->accel_mode == GTK_CELL_RENDERER_ACCEL_MODE_GTK)
2005-09-11 05:43:19 +00:00
accel_mods &= ~consumed_modifiers;
/* Put shift back if it changed the case of the key, not otherwise.
*/
if (accel_key != event->keyval)
accel_mods |= GDK_SHIFT_MASK;
if (accel_mods == 0)
{
switch (event->keyval)
{
case GDK_Escape:
goto out; /* cancel */
case GDK_BackSpace:
/* clear the accelerator on Backspace */
cleared = TRUE;
goto out;
default:
break;
}
}
if (accel->accel_mode == GTK_CELL_RENDERER_ACCEL_MODE_GTK)
2005-09-11 05:43:19 +00:00
{
if (!gtk_accelerator_valid (accel_key, accel_mods))
{
Add new infrastructure for notifications of failed keyboard navigation and 2006-11-16 Michael Natterer <mitch@imendio.com> Add new infrastructure for notifications of failed keyboard navigation and navigation with restricted set of keys. The patch handles configurable beeping, navigating the GUI with cursor keys only (as in phone environments), and configurable wrap-around. Fixes bugs #322640, #70986, #318827, #334726, #334742 and #309291. * gtk/gtksettings.c: added properties gtk-keynav-cursor-only, gtk-keynav-wrap-around and gtk-error-bell. * gtk/gtkwidget.[ch]: added new signal "keynav-failed" and public API to emit it. Added New function gtk_widget_error_bell() which looks at the gtk-error-bell setting and calls gdk_window_beep() accordingly. * gtk/gtk.symbols: add the new widget symbols. * gtk/gtkcellrendereraccel.c * gtk/gtkimcontextsimple.c * gtk/gtkmenu.c * gtk/gtknotebook.c: use gtk_widget_error_bell() or look at the gtk-error-bell setting instead of calling gdk_display_beep() unconditionally. * gtk/gtkcombobox.c * gtk/gtkentry.c * gtk/gtkiconview.c * gtk/gtklabel.c * gtk/gtkmenushell.c * gtk/gtkspinbutton.c * gtk/gtktextview.c * gtk/gtktreeview.c: call gtk_widget_error_bell() on failed keynav. * gtk/gtkentry.c * gtk/gtklabel.c * gtk/gtkrange.c * gtk/gtktextview.c: consult gtk_widget_keynav_failed() on failed cursor navigation and leave the widget if it returns FALSE. * gtk/gtkmenushell.c * gtk/gtknotebook.c: only wrap around if gtk-keynav-wrap-around is TRUE. * gtk/gtkradiobutton.c: ask gtk_widget_keynav_failed() to decide whether to to wrap-around, and don't select active items on cursor navigation if gtk-keynav-cursor-only is TRUE. Should look at gtk-keynav-wrap-around too, will look into that.
2006-11-16 12:56:30 +00:00
gtk_widget_error_bell (widget);
2005-09-11 05:43:19 +00:00
return TRUE;
}
}
edited = TRUE;
out:
gdk_keyboard_ungrab (event->time);
gdk_pointer_ungrab (event->time);
path = g_strdup (g_object_get_data (G_OBJECT (accel->edit_widget), "gtk-cell-renderer-text"));
2005-09-11 05:43:19 +00:00
gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (accel->edit_widget));
gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (accel->edit_widget));
accel->edit_widget = NULL;
accel->grab_widget = NULL;
2005-09-11 05:43:19 +00:00
if (edited)
g_signal_emit (accel, signals[ACCEL_EDITED], 0, path,
2005-09-11 05:43:19 +00:00
accel_key, accel_mods, event->hardware_keycode);
else if (cleared)
g_signal_emit (accel, signals[ACCEL_CLEARED], 0, path);
2005-09-11 05:43:19 +00:00
g_free (path);
return TRUE;
}
static void
ungrab_stuff (GtkWidget *widget,
gpointer data)
2005-09-11 05:43:19 +00:00
{
GtkCellRendererAccel *accel = GTK_CELL_RENDERER_ACCEL (data);
2005-09-11 05:43:19 +00:00
gdk_keyboard_ungrab (GDK_CURRENT_TIME);
gdk_pointer_ungrab (GDK_CURRENT_TIME);
g_signal_handlers_disconnect_by_func (G_OBJECT (accel->grab_widget),
2005-09-11 05:43:19 +00:00
G_CALLBACK (grab_key_callback), data);
}
static void
_gtk_cell_editable_event_box_start_editing (GtkCellEditable *cell_editable,
GdkEvent *event)
{
/* do nothing, because we are pointless */
}
static void
_gtk_cell_editable_event_box_cell_editable_init (GtkCellEditableIface *iface)
{
iface->start_editing = _gtk_cell_editable_event_box_start_editing;
}
typedef GtkEventBox GtkCellEditableEventBox;
typedef GtkEventBoxClass GtkCellEditableEventBoxClass;
G_DEFINE_TYPE_WITH_CODE (GtkCellEditableEventBox, _gtk_cell_editable_event_box, GTK_TYPE_EVENT_BOX, { \
G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE, _gtk_cell_editable_event_box_cell_editable_init) \
})
2005-09-11 05:43:19 +00:00
static void
_gtk_cell_editable_event_box_class_init (GtkCellEditableEventBoxClass *class)
{
}
static void
_gtk_cell_editable_event_box_init (GtkCellEditableEventBox *box)
{
}
static GtkCellEditable *
gtk_cell_renderer_accel_start_editing (GtkCellRenderer *cell,
GdkEvent *event,
GtkWidget *widget,
const gchar *path,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GtkCellRendererState flags)
2005-09-11 05:43:19 +00:00
{
GtkCellRendererText *celltext;
GtkCellRendererAccel *accel;
2005-09-11 05:43:19 +00:00
GtkWidget *label;
GtkWidget *eventbox;
celltext = GTK_CELL_RENDERER_TEXT (cell);
accel = GTK_CELL_RENDERER_ACCEL (cell);
2005-09-11 05:43:19 +00:00
/* If the cell isn't editable we return NULL. */
if (celltext->editable == FALSE)
return NULL;
g_return_val_if_fail (widget->window != NULL, NULL);
if (gdk_keyboard_grab (widget->window, FALSE,
gdk_event_get_time (event)) != GDK_GRAB_SUCCESS)
return NULL;
if (gdk_pointer_grab (widget->window, FALSE,
GDK_BUTTON_PRESS_MASK,
NULL, NULL,
gdk_event_get_time (event)) != GDK_GRAB_SUCCESS)
{
gdk_keyboard_ungrab (gdk_event_get_time (event));
return NULL;
}
accel->grab_widget = widget;
2005-09-11 05:43:19 +00:00
g_signal_connect (G_OBJECT (widget), "key_press_event",
G_CALLBACK (grab_key_callback),
accel);
2005-09-11 05:43:19 +00:00
eventbox = g_object_new (_gtk_cell_editable_event_box_get_type (), NULL);
accel->edit_widget = eventbox;
g_object_add_weak_pointer (G_OBJECT (accel->edit_widget),
(void**) &accel->edit_widget);
2005-09-11 05:43:19 +00:00
label = gtk_label_new (NULL);
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
gtk_widget_modify_bg (eventbox, GTK_STATE_NORMAL,
&widget->style->bg[GTK_STATE_SELECTED]);
gtk_widget_modify_fg (label, GTK_STATE_NORMAL,
&widget->style->fg[GTK_STATE_SELECTED]);
/* This label is displayed in a treeview cell displaying
* an accelerator when the cell is clicked to change the
* acelerator.
*/
gtk_label_set_text (GTK_LABEL (label), _("New accelerator..."));
gtk_container_add (GTK_CONTAINER (eventbox), label);
g_object_set_data_full (G_OBJECT (accel->edit_widget), "gtk-cell-renderer-text",
2005-09-11 05:43:19 +00:00
g_strdup (path), g_free);
gtk_widget_show_all (accel->edit_widget);
2005-09-11 05:43:19 +00:00
g_signal_connect (G_OBJECT (accel->edit_widget), "unrealize",
G_CALLBACK (ungrab_stuff), accel);
2005-09-11 05:43:19 +00:00
return GTK_CELL_EDITABLE (accel->edit_widget);
2005-09-11 05:43:19 +00:00
}
#define __GTK_CELL_RENDERER_ACCEL_C__
2005-09-11 05:43:19 +00:00
#include "gtkaliasdef.c"