Re: Button accelerators



On Fri, May 26, 2000 at 08:58:17AM -0600, Bruce Ide wrote:
> Oh. It was for the label widget. I'd guess that the button contains a
> label widget. If you can get at it, you can use the
> gtk_label_set_pattern on it to underline the text you want to underline.
> You're on your own with accelerators, though. I haven't needed to do
> anything with them yet.

What I did in Ethereal for radio and check buttons was to create some
convenience functions with calling sequences similar to
"gtk_{radio,check}_button_new_with_label()", which took a "GtkAccelGroup
*" as an argument.  The convenience routines will call the GTK+ routine
in question, and then fix up the label by calling
"gtk_label_parse_uline()" on it and, if it returns an accelerator key,
add that as an accelerator - either with GDK_MOD1_MASK (continuing the
GTK+ tradition of assuming that Alt is MOD1; mail in another thread
suggested that this may not be the right thing to do) or without it -
for the "clicked" signal.

"gtk_label_parse_uline()" does the "gtk_label_set_pattern()" for you.

When creating a dialog box, right after the "gtk_window_new()" call

	dialog_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);

I did:

	accel_group = gtk_accel_group_new();
	gtk_window_add_accel_group(GTK_WINDOW(dialog_w), accel_group);

and then passed the "accel_group" value as an argument to the
convenience routines in question.  The label text passed to the
convenience routines has underscores in it to indicate which letter is
to be used as the mnemonic:

  button = dlg_radio_button_new_with_label_with_mnemonic(NULL, "Plain _Text",
                                accel_group);

I haven't tried this with types of buttons other than check buttons and
radio buttons.

The convenience routines are:

typedef struct {
	GtkWidget *button;
	GtkAccelGroup *accel_group;
} fix_label_args_t;

static void
dlg_fix_label_callback(GtkWidget *label_widget, gpointer data)
{
  fix_label_args_t *args = data;
  gchar *label;
  guint accel_key;

  gtk_label_get(GTK_LABEL(label_widget), &label);
  accel_key = gtk_label_parse_uline(GTK_LABEL(label_widget), label);
  if (accel_key != GDK_VoidSymbol) {
    /* Yes, we have a mnemonic. */
    gtk_widget_add_accelerator(args->button, "clicked", args->accel_group,
				accel_key, 0, GTK_ACCEL_LOCKED);
    gtk_widget_add_accelerator(args->button, "clicked", args->accel_group,
				accel_key, GDK_MOD1_MASK, GTK_ACCEL_LOCKED);
  }
}

static void
dlg_fix_button_label(GtkWidget *button, GtkAccelGroup *accel_group)
{
  fix_label_args_t args;

  args.button = button;
  args.accel_group = accel_group;
  gtk_container_foreach(GTK_CONTAINER(button), dlg_fix_label_callback, &args);
}

GtkWidget *
dlg_radio_button_new_with_label_with_mnemonic(GSList *group,
		const gchar *label, GtkAccelGroup *accel_group)
{
  GtkWidget *radio_button;

  radio_button = gtk_radio_button_new_with_label (group, label);
  dlg_fix_button_label(radio_button, accel_group);
  return radio_button;
}

GtkWidget *
dlg_check_button_new_with_label_with_mnemonic(const gchar *label,
			GtkAccelGroup *accel_group)
{
  GtkWidget *check_button;
	         
  check_button = gtk_check_button_new_with_label (label);
  dlg_fix_button_label(check_button, accel_group);
  return check_button;
}





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]