Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

gtk2-perl: how to enable stock images in all widgets

by wdef2 (Acolyte)
on Oct 02, 2012 at 09:16 UTC ( #996823=perlquestion: print w/replies, xml ) Need Help??
wdef2 has asked for the wisdom of the Perl Monks concerning the following question:

With astonishing indifference to thousands of existing Perl programs and newbies, some capricious genius at Gtk2 decided to disable by default all stock images displaying in buttons and it seems this is inherited by gtk2-perl since gtk+ 2.6. This generated large numbers of forum posts by puzzled users of course but nobody cares. This means stock images do not show in buttons unless explicitly turned on. I assume this applies to all widgets. Who knows. As usual there's little or no explanation, it's all got to be secret magic absorbed from the ether through experience and endless googling. I found something like the following in a snippet which, if I place this after creating my first button ($sortbutton) turns on the button image - weirdly - in a different button ($quitbutton) created later, $quitbutton is stock but $sortbutton is a stock gtk-refresh icon placed in an iconfactory with a custom label.
my $button = $sortbutton; my %buttonimages; $buttonimages{$button}=$button->get_settings; $buttonimages{$button}->set('gtk-button-images', !$buttonimages{$butto +n}->get('gtk-button-images'));
1. Despite referencing only one button ($sortbutton) this seems to turn on the images for other stock buttons (all of them?). Why? Is it supposed to? 2. How do I propagate this setting to the icon factory images? Or can I just change the label on a stock button image without an icon factory? 3. What is that second argument doing: !$buttonimages{$button}->get('gtk-button-images') Thanks

Replies are listed 'Best First'.
Re: gtk2-perl: how to enable stock images in all widgets
by zentara (Archbishop) on Oct 02, 2012 at 11:32 UTC
    This means stock images do not show in buttons unless explicitly turned on.

    My take on it was it is hard to change the Label of stock image buttons, and most people wanted to be able to label their buttons as they desire. So it is turned off by default. Makes perfect sense to me.

    With the way the Gtk2 themeing engine works, it is probable that it would be set in a ./gtkrc-2.0 theme setting.

    #!/usr/bin/perl use warnings; use strict; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; # create a new window my $window = Gtk2::Window->new('toplevel'); $window ->signal_connect( "destroy" => sub { Gtk2->main_quit; } ); my $vbox = Gtk2::VBox->new( FALSE, 0 ); $window->add($vbox); $vbox->set_border_width(2); my $button = Gtk2::Button->new_from_stock('gtk-close'); my $button1 = Gtk2::Button->new_from_stock('gtk-close'); my $button2 = Gtk2::Button->new_from_stock('gtk-close'); $button->set_label('uh oh lost icon'); #won't work # sub by muppet find_and_set_label_in ($button2->child, "This worked"); $button->signal_connect( "clicked" => \&callback, "cool button" ); $button1->signal_connect( "clicked" => \&callback, "cool button1" ); $button2->signal_connect( "clicked" => \&callback, "cool button2" ); $vbox->pack_start( $button, FALSE, FALSE, 0 ); $vbox->pack_start( $button1, FALSE, FALSE, 0 ); $vbox->pack_start( $button2, FALSE, FALSE, 0 ); $window->show_all(); Gtk2->main; ################################################## # our usual callback function sub callback { my $widget = shift; my $data = shift; printf "Hello again - %s was pressed\n", $data; } ################################################## sub find_and_set_label_in { #muppet magic my ($widget, $text) = @_; print "@_\n"; if ($widget->isa (Gtk2::Container::)) { $widget->foreach (sub { find_and_set_label_in ($_[0], $text); } +); } elsif ($widget->isa (Gtk2::Label::)) { $widget->set_text ($text); } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thank you for this excellent reply. I will study it tomorrow.

        The "muppet magic" recursion and the found code snippet suggested one way to switch on images for all buttons. A block was that 'isa' did not behave as I expected on buttons. It doesn't distinguish between check buttons and normal buttons for example and misidentifies some other things. I may not be calling it correctly (should be blessed? Currently I test for a string match in the widget name and filter out check buttons. I call the following on the main top-level window just before showing all:

        sub turn_button_images_on { my $widget = $_[0]; my %buttonimages; if ("$widget->get_name" =~/Button/){ unless ("$widget->get_name" =~/CheckButton/{ print "Widget $widget is a Gtk2 button\n"; $buttonimages{$widget}=$widget->get_settings: $buttonimages{$widget}=$widget->set('gtk-button-images', TRUE +) } } elsif ($widget->isa(Gtk2::Container::)){ $widget->foreach(sub {turn_button_images_on ($_[0]); }); } }
        (This will be an option in case the user doesn't want button images). My system doesn't have a .gtkrc-2.0 on the filesystem. (EDIT: There are gtkrc files in /usr/share/themes but for the Default theme that contains a comment only. Switching themes in my program does not enable button images). Creating ~/.gtkrc-2.0 to try to turn button images on does nothing there at least for gtk2-perl. I suspect this is system-specific. This issue is very similar to that experienced back in 2005 by the OP on this thread where the 2-line code snippet came from:
Re: gtk2-perl: how to enable stock images in all widgets
by syphilis (Chancellor) on Oct 02, 2012 at 10:10 UTC
    There is a gtk-perl mailing list that might provide good assistance if nothing helpful turns up on this forum.

        It's here: [}
Re: gtk2-perl: how to enable stock images in all widgets
by Anonymous Monk on Oct 02, 2012 at 09:29 UTC

    With astonishing indifference ....

    Yeah, this time I'm not reading, sorry guy

      At the risk of feeding an anonymous sensitive soul who bothers to post that he is not reading and then apologizes for not reading, which is entirely off topic, presumably because (s)he believes all decisions made upstream should be graciously accepted without comment or questioning.

      If it's not indifference then tell me why it isn't indifference. Better yet, forget that and look at the question instead of pouting.



        Whatever you call that style of asking for help (dramatic editorializing?), on a technical forum, for me, its a unincentive

Re: gtk2-perl: how to enable stock images in all widgets
by andal (Hermit) on Oct 05, 2012 at 08:10 UTC

    This stuff is supposed to be controlled from theme. Many desktop environments now offer XSettings server which provides necessary parameters. If there are no such server, then RC files can be used to set them. The application should not do it (though it can). This suggests simpler solution (in C which maps to perl). Though this page suggests that it may not always work.

    Well, I'm not the one to decide if this kind of separation of code and configuration is good. On one hand, it allows flexibility. On the other hand it introduces complexity because the end user has to perform some steps in configuring the application. Lucky are those whose desktop environments already provide all necessary configuration. But I'm also sorry for them, they know so little about the system they use :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996823]
Front-paged by Arunbear
[james28909]: check if it is a hash or an array ect with ref in one sub.
[james28909]: like all the subs calling get_data get_array get_ect, you could just use get_data. once you send the data to get_data, check if it is a hash or an array ect. and do functions for each, in one sub
[james28909]: that looks like it would be alot more work than just renaming the sub though lol
[Lady_Aleena]: james28909, did you even look at the subs?
[james28909]: yes
[Lady_Aleena]: And you see how the first two are vastly different than the third, right?

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2017-05-24 04:56 GMT
Find Nodes?
    Voting Booth?