Provide access to the string representation of accelerators used in

2004-10-05  Matthias Clasen  <mclasen@redhat.com>

	Provide access to the string representation of accelerators used
	in GtkAccelLabel.  (#154068, John Spray)

	* gtk/gtkaccelgroup.h:
	* gtk/gtkaccelgroup.c (gtk_accelerator_get_label): New function
	to return the accelerator label used in GtkAccelLabel.
	(gtk_accelerator_name): Update docs to point to
	gtk_accelerator_get_label().
	* gtk/gtkaccellabel.h:
	* gtk/gtkaccellabel.c (_gtk_accel_label_class_get_accelerator_label):
	New auxiliary function which creates the string representing the
	accelerator.
This commit is contained in:
Matthias Clasen 2004-10-05 05:17:22 +00:00 committed by Matthias Clasen
parent 8e30e2d5c3
commit 0dfe684dde
8 changed files with 188 additions and 68 deletions

View File

@ -1,3 +1,18 @@
2004-10-05 Matthias Clasen <mclasen@redhat.com>
Provide access to the string representation of accelerators used
in GtkAccelLabel. (#154068, John Spray)
* gtk/gtkaccelgroup.h:
* gtk/gtkaccelgroup.c (gtk_accelerator_get_label): New function
to return the accelerator label used in GtkAccelLabel.
(gtk_accelerator_name): Update docs to point to
gtk_accelerator_get_label().
* gtk/gtkaccellabel.h:
* gtk/gtkaccellabel.c (_gtk_accel_label_class_get_accelerator_label):
New auxiliary function which creates the string representing the
accelerator.
2004-10-05 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkcombobox.c (gtk_combo_box_set_model): Resize the

View File

@ -1,3 +1,18 @@
2004-10-05 Matthias Clasen <mclasen@redhat.com>
Provide access to the string representation of accelerators used
in GtkAccelLabel. (#154068, John Spray)
* gtk/gtkaccelgroup.h:
* gtk/gtkaccelgroup.c (gtk_accelerator_get_label): New function
to return the accelerator label used in GtkAccelLabel.
(gtk_accelerator_name): Update docs to point to
gtk_accelerator_get_label().
* gtk/gtkaccellabel.h:
* gtk/gtkaccellabel.c (_gtk_accel_label_class_get_accelerator_label):
New auxiliary function which creates the string representing the
accelerator.
2004-10-05 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkcombobox.c (gtk_combo_box_set_model): Resize the

View File

@ -1,3 +1,18 @@
2004-10-05 Matthias Clasen <mclasen@redhat.com>
Provide access to the string representation of accelerators used
in GtkAccelLabel. (#154068, John Spray)
* gtk/gtkaccelgroup.h:
* gtk/gtkaccelgroup.c (gtk_accelerator_get_label): New function
to return the accelerator label used in GtkAccelLabel.
(gtk_accelerator_name): Update docs to point to
gtk_accelerator_get_label().
* gtk/gtkaccellabel.h:
* gtk/gtkaccellabel.c (_gtk_accel_label_class_get_accelerator_label):
New auxiliary function which creates the string representing the
accelerator.
2004-10-05 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkcombobox.c (gtk_combo_box_set_model): Resize the

View File

@ -1,3 +1,18 @@
2004-10-05 Matthias Clasen <mclasen@redhat.com>
Provide access to the string representation of accelerators used
in GtkAccelLabel. (#154068, John Spray)
* gtk/gtkaccelgroup.h:
* gtk/gtkaccelgroup.c (gtk_accelerator_get_label): New function
to return the accelerator label used in GtkAccelLabel.
(gtk_accelerator_name): Update docs to point to
gtk_accelerator_get_label().
* gtk/gtkaccellabel.h:
* gtk/gtkaccellabel.c (_gtk_accel_label_class_get_accelerator_label):
New auxiliary function which creates the string representing the
accelerator.
2004-10-05 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkcombobox.c (gtk_combo_box_set_model): Resize the

View File

@ -30,7 +30,9 @@
#include "gtkalias.h"
#include "gtkaccelgroup.h"
#include "gtkaccellabel.h" /* For _gtk_accel_label_class_get_accelerator_label */
#include "gtkaccelmap.h"
#include "gtkintl.h"
#include "gtkmain.h" /* For _gtk_boolean_handled_accumulator */
#include "gdk/gdkkeysyms.h"
#include "gtkmarshalers.h"
@ -1056,14 +1058,16 @@ gtk_accelerator_parse (const gchar *accelerator,
* gtk_accelerator_name:
* @accelerator_key: accelerator keyval
* @accelerator_mods: accelerator modifier mask
* @returns: a newly-allocated accelerator name
*
* Converts an accelerator keyval and modifier mask
* into a string parseable by gtk_accelerator_parse().
* For example, if you pass in #GDK_q and #GDK_CONTROL_MASK,
* this function returns "&lt;Control&gt;q".
*
* The caller of this function must free the returned string.
* If you need to display accelerators in the user interface,
* see gtk_accelerator_get_label().
*
* Returns: a newly-allocated accelerator name
*/
gchar*
gtk_accelerator_name (guint accelerator_key,
@ -1155,6 +1159,47 @@ gtk_accelerator_name (guint accelerator_key,
return accelerator;
}
/* Underscores in key names are better displayed as spaces
* E.g., Page_Up should be "Page Up"
*/
static void
substitute_underscores (char *str)
{
char *p;
for (p = str; *p; p++)
if (*p == '_')
*p = ' ';
}
/**
* gtk_accelerator_get_label:
* @accelerator_key: accelerator keyval
* @accelerator_mods: accelerator modifier mask
*
* Converts an accelerator keyval and modifier mask into a string
* which can be used to represent the accelerator to the user.
*
* Returns: a newly-allocated string representing the accelerator.
*
* Since: 2.6
*/
gchar*
gtk_accelerator_get_label (guint accelerator_key,
GdkModifierType accelerator_mods)
{
GtkAccelLabelClass *klass;
gchar *label;
klass = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
label = _gtk_accel_label_class_get_accelerator_label (klass,
accelerator_key,
accelerator_mods);
g_type_class_unref (klass); /* klass is kept alive since gtk uses static types */
return label;
}
/**
* gtk_accelerator_set_default_mod_mask:
* @default_mod_mask: accelerator modifier mask

View File

@ -148,6 +148,8 @@ void gtk_accelerator_parse (const gchar *accelerator,
GdkModifierType *accelerator_mods);
gchar* gtk_accelerator_name (guint accelerator_key,
GdkModifierType accelerator_mods);
gchar* gtk_accelerator_display_name (guint accelerator_key,
GdkModifierType accelerator_mods);
void gtk_accelerator_set_default_mod_mask (GdkModifierType default_mod_mask);
guint gtk_accelerator_get_default_mod_mask (void);

View File

@ -528,6 +528,71 @@ substitute_underscores (char *str)
*p = ' ';
}
gchar *
_gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
guint accelerator_key,
GdkModifierType accelerator_mods)
{
GString *gstring;
gboolean seen_mod = FALSE;
gunichar ch;
gstring = g_string_new ("");
if (accelerator_mods & GDK_SHIFT_MASK)
{
g_string_append (gstring, klass->mod_name_shift);
seen_mod = TRUE;
}
if (accelerator_mods & GDK_CONTROL_MASK)
{
if (seen_mod)
g_string_append (gstring, klass->mod_separator);
g_string_append (gstring, klass->mod_name_control);
seen_mod = TRUE;
}
if (accelerator_mods & GDK_MOD1_MASK)
{
if (seen_mod)
g_string_append (gstring, klass->mod_separator);
g_string_append (gstring, klass->mod_name_alt);
seen_mod = TRUE;
}
if (seen_mod)
g_string_append (gstring, klass->mod_separator);
ch = gdk_keyval_to_unicode (accelerator_key);
if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
(ch < 0x80 || klass->latin1_to_char))
{
switch (ch)
{
case ' ':
g_string_append (gstring, "Space");
break;
case '\\':
g_string_append (gstring, "Backslash");
break;
default:
g_string_append_unichar (gstring, g_unichar_toupper (ch));
break;
}
}
else
{
gchar *tmp;
tmp = gtk_accelerator_name (accelerator_key, 0);
if (tmp[0] != 0 && tmp[1] == 0)
tmp[0] = g_ascii_toupper (tmp[0]);
substitute_underscores (tmp);
g_string_append (gstring, tmp);
g_free (tmp);
}
return g_string_free (gstring, FALSE);
}
gboolean
gtk_accel_label_refetch (GtkAccelLabel *accel_label)
{
@ -549,66 +614,15 @@ gtk_accel_label_refetch (GtkAccelLabel *accel_label)
if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
{
GString *gstring;
gboolean seen_mod = FALSE;
gunichar ch;
gstring = g_string_new (accel_label->accel_string);
g_string_append (gstring, gstring->len ? class->accel_seperator : " ");
if (key->accel_mods & GDK_SHIFT_MASK)
{
g_string_append (gstring, class->mod_name_shift);
seen_mod = TRUE;
}
if (key->accel_mods & GDK_CONTROL_MASK)
{
if (seen_mod)
g_string_append (gstring, class->mod_separator);
g_string_append (gstring, class->mod_name_control);
seen_mod = TRUE;
}
if (key->accel_mods & GDK_MOD1_MASK)
{
if (seen_mod)
g_string_append (gstring, class->mod_separator);
g_string_append (gstring, class->mod_name_alt);
seen_mod = TRUE;
}
if (seen_mod)
g_string_append (gstring, class->mod_separator);
GtkAccelLabelClass *klass;
gchar *tmp;
ch = gdk_keyval_to_unicode (key->accel_key);
if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
(ch < 0x80 || class->latin1_to_char))
{
switch (ch)
{
case ' ':
g_string_append (gstring, "Space");
break;
case '\\':
g_string_append (gstring, "Backslash");
break;
default:
g_string_append_unichar (gstring, g_unichar_toupper (ch));
break;
}
}
else
{
gchar *tmp;
tmp = gtk_accelerator_name (key->accel_key, 0);
if (tmp[0] != 0 && tmp[1] == 0)
tmp[0] = g_ascii_toupper (tmp[0]);
substitute_underscores (tmp);
g_string_append (gstring, tmp);
g_free (tmp);
}
g_free (accel_label->accel_string);
accel_label->accel_string = gstring->str;
g_string_free (gstring, FALSE);
klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
tmp = _gtk_accel_label_class_get_accelerator_label (klass,
key->accel_key,
key->accel_mods);
accel_label->accel_string = g_strconcat (" ", tmp, NULL);
g_free (tmp);
}
if (!accel_label->accel_string)
accel_label->accel_string = g_strdup ("-/-");

View File

@ -34,10 +34,7 @@
#include <gtk/gtklabel.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
G_BEGIN_DECLS
#define GTK_TYPE_ACCEL_LABEL (gtk_accel_label_get_type ())
#define GTK_ACCEL_LABEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ACCEL_LABEL, GtkAccelLabel))
@ -97,9 +94,11 @@ void gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
GClosure *accel_closure);
gboolean gtk_accel_label_refetch (GtkAccelLabel *accel_label);
#ifdef __cplusplus
}
#endif /* __cplusplus */
/* private */
gchar * _gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
guint accelerator_key,
GdkModifierType accelerator_mods);
G_END_DECLS
#endif /* __GTK_ACCEL_LABEL_H__ */