Sign up

How to use a button with a stock icon and custom text?

I have this code:

optionsButton = new Button(StockID.PREFERENCES);

I am happy with the icon but instead of the text "_Preferences" I want to set "_Options".

However, when I do that the icon disappears. Is it possible to have both the stock icon and my own custom text?

Re: How to use a button with a stock icon and custom text?

On 17-02-2020 08:48, mark wrote:

I have this code:

optionsButton = new Button(StockID.PREFERENCES);

I am happy with the icon but instead of the text "_Preferences" I want to set "_Options".

However, when I do that the icon disappears. Is it possible to have both the stock icon and my own custom text?

You would use setImage() to set the image separately from the text.

import gtk.Image;

button.setImage(new Image(StockID.PREFERENCES));

Re: How to use a button with a stock icon and custom text?

On Tue, 18 Feb 2020 05:28:56 -0400, Mike Wey wrote:

On 17-02-2020 08:48, mark wrote:

I have this code:

optionsButton = new Button(StockID.PREFERENCES);

I am happy with the icon but instead of the text "_Preferences" I want to set "_Options".

However, when I do that the icon disappears. Is it possible to have both the stock icon and my own custom text?

You would use setImage() to set the image separately from the text.

import gtk.Image;

button.setImage(new Image(StockID.PREFERENCES));

I've just tried it like this:

        optionsButton = new Button("_Options");
	optionsButton.setImage(new Image(StockID.PREFERENCES));
        helpButton = new Button(StockID.HELP);

The help button shows an icon & the text "Help"; the options button shows a "missing icon" icon and the text "Options". Even when I use the StockID.HELP for the options button it still shows the "missing icon" icon. I'm testing this on Linux.

Re: How to use a button with a stock icon and custom text?

[snip]

I've just tried it like this:

        optionsButton = new Button("_Options");
	optionsButton.setImage(new Image(StockID.PREFERENCES));
        helpButton = new Button(StockID.HELP);

The help button shows an icon & the text "Help"; the options button shows a "missing icon" icon and the text "Options". Even when I use the StockID.HELP for the options button it still shows the "missing icon" icon. I'm testing this on Linux.

I've now reported this as a bug https://github.com/gtkd-developers/GtkD/issues/300 including a tiny runnable example.

Re: How to use a button with a stock icon and custom text?

On 18-02-2020 10:27, mark wrote:

On Tue, 18 Feb 2020 05:28:56 -0400, Mike Wey wrote:

On 17-02-2020 08:48, mark wrote:

I have this code:

optionsButton = new Button(StockID.PREFERENCES);

I am happy with the icon but instead of the text "_Preferences" I want to set "_Options".

However, when I do that the icon disappears. Is it possible to have both the stock icon and my own custom text?

You would use setImage() to set the image separately from the text.

import gtk.Image;

button.setImage(new Image(StockID.PREFERENCES));

I've just tried it like this:

         optionsButton = new Button("_Options");
	optionsButton.setImage(new Image(StockID.PREFERENCES));
         helpButton = new Button(StockID.HELP);

The help button shows an icon & the text "Help"; the options button shows a "missing icon" icon and the text "Options". Even when I use the StockID.HELP for the options button it still shows the "missing icon" icon. I'm testing this on Linux.

You could try this and use a icon name:
https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html

optionsButton = new Button("_Options");
optionsButton.setImage(new Image("preferences-system", IconSize.BUTTON));

Re: How to use a button with a stock icon and custom text?

On 19-02-2020 05:33, mark wrote:

[snip]

I've just tried it like this:

         optionsButton = new Button("_Options");
	optionsButton.setImage(new Image(StockID.PREFERENCES));
         helpButton = new Button(StockID.HELP);

The help button shows an icon & the text "Help"; the options button shows a "missing icon" icon and the text "Options". Even when I use the StockID.HELP for the options button it still shows the "missing icon" icon. I'm testing this on Linux.

I've now reported this as a bug https://github.com/gtkd-developers/GtkD/issues/300 including a tiny runnable example.

Actually the code is using a other constructor then the one accepting a
StockID, since theh icon size is missing the enum is interpreted as a
string and passed to the constructor that uses a filename.

This should work:

optionsButton = new Button("_Options");
optionsButton.setImage(new Image(StockID.PREFERENCES, IconSize.BUTTON));

Re: How to use a button with a stock icon and custom text?

On Wed, 19 Feb 2020 05:38:16 -0400, Mike Wey wrote:

On 19-02-2020 05:33, mark wrote:

[snip]

Actually the code is using a other constructor then the one accepting a
StockID, since theh icon size is missing the enum is interpreted as a
string and passed to the constructor that uses a filename.

This should work:

optionsButton = new Button("_Options");
optionsButton.setImage(new Image(StockID.PREFERENCES, IconSize.BUTTON));

IconSize.BUTTON doesn't seem to exist for me. However, this worked:

 optionsButton.setImage(new Image(StockID.PREFERENCES,
                                         IconSize.fromName("BUTTON")));