GUI button% string label keyboard mnemonics

40 views
Skip to first unread message

Dominik Pantůček

unread,
Jan 6, 2021, 2:56:11 PM1/6/21
to Racket Users
Hello Racketeers,

it's been a while since I was doing some serious GUI work but for one of
my current work projects the time has come and surprise surprise -
troubles on the way :)

When a button is created like:

(new button% (label "&Quit") ...)

everything works as expected. The #\q key becomes a keyboard shortcut
that triggers the callback correctly.

But when the string label is given in the list of icon and label like:

(new button% (label (close-icon) "&Quit" 'left) ...)

The "&" is left intact and no action is triggered by pressing the #\q
character on the keyboard.

The documentation clearly states both alternatives as supported: "If &
occurs in label (when label includes a string), it is specially parsed;
...". Am I doing something wrong or is it a bug?

If I look at the right source for my platform -
gui-lib/mred/private/wx/gtk/button (Linux/X11/Gtk), in both cases
gtk_new_with_mnemonic is used. Seems OK to me.

Any ideas what I might have overlooked?


Cheers,
Dominik

George Neuner

unread,
Jan 8, 2021, 11:12:37 PM1/8/21
to Dominik Pantůček, racket users

On 1/6/2021 2:56 PM, Dominik Pantůček wrote:
> When a button is created like:
>
> (new button% (label "&Quit") ...)
>
> everything works as expected. The #\q key becomes a keyboard shortcut
> that triggers the callback correctly.
>
> But when the string label is given in the list of icon and label like:
>
> (new button% (label (close-icon) "&Quit" 'left) ...)
>
> The "&" is left intact and no action is triggered by pressing the #\q
> character on the keyboard.
>
> The documentation clearly states both alternatives as supported: "If &
> occurs in label (when label includes a string), it is specially parsed;
> ...". Am I doing something wrong or is it a bug?

Maybe a stupid question ...

Is  "close-icon" the name of the bitmap or a function that creates a bitmap?
Have you tried:

(new button% (label close-icon "&Quit" 'left) ...)


George

Dominik Pantůček

unread,
Jan 9, 2021, 3:20:22 AM1/9/21
to George Neuner, racket users

> Maybe a stupid question ...
>
> Is  "close-icon" the name of the bitmap or a function that creates a
> bitmap?

I am using images/icons/misc:

https://docs.racket-lang.org/images/Icons.html?q=close-icon#%28def._%28%28lib._images%2Ficons%2Fmisc..rkt%29._close-icon%29%29

So it generates a bitmap% in the end.

> Have you tried:
>
>    (new button% (label close-icon "&Quit" 'left) ...)
>

Just out of curiosity right now and no, the button% does not accept
procedure to be a part of that list.



Dominik

George Neuner

unread,
Jan 9, 2021, 11:43:07 AM1/9/21
to Dominik Pantůček, racket users
Unfortunately, I'm not well versed in Racket's GUI library - I have done
a lot of GUI work in C and C++.

In the OS graphics [X, GDI, etc], the button and the icon are separate
objects: e.g., deleting the button will not also delete an icon attached
to it, so if the icon doesn't have its own handle, you'll lose it and
have a resource leak.

These things often are handled at higher level by the graphics libraries
- and Racket may do that [I don't know] - but my first thought would be
to name the icon separately, and then pass the name to the button
constructor.  E.g.,

   (define my-icon (close-icon))
(new button% (label my-icon "&Quit" 'left) ...)


I'm thinking that the button isn't functioning correctly because it
isn't being constructed improperly.  Theoretically the reference to the
icon should be the same regardless, but a lot of the library is macro
code and who knows what it really expects.  When in doubt I try to simplify.

YMMV,
George

James Platt

unread,
Jan 13, 2021, 5:28:04 PM1/13/21
to Racket Users
One workaround you could do would be to duplicate the functionality of the button in a pulldown menu and define the keyboard shortcut there. I think it is good general practice anyway to put all functions in the pulldown menus and use other GUI elements only as shortcuts. This gives the user one place they can look to find all the functions of your software. It's part of Apple's Human Interface Guidelines but I think it's a good idea on any platform.
Reply all
Reply to author
Forward
0 new messages