Re: gtk-perl makes ASCII world domination one step closer




On Apr 13, 2008, at 2:54 AM, nadim khemir wrote:
Hi all,

gpad is now know as 'AsciiO'. It can be downloaded from CPAN at:

http://search.cpan.org/~nkh/App-Asciio-0.95_01/

It would be great if someone could answer the button questions found at the
bottom of my previous mail.

[snip]

a few questions:

How do I add icons to buttons as code below looks boring (a link will do
fine):

$dialog->add_button ('Continue editing' => 'no');
$dialog->add_button ('Quit and loose changes' => 'yes');



gtk_dialog_add_button() is shorthand for gtk_button_new_from_stock() and gtk_dialog_add_action_widget(). If you register new stock items for your actions, this will work automatically.

But, for things that will be used in only one place, making a new stock id can be overkill. You can roll your own fairly easily.


Is it possible to change the keyboard shortcut for a stock button? I'd like
to use ctl(or alt)-enter instead for alt-c in the dialog below:


Changes to stock items like that are global. Not sure if that's what you want.

You can add accelerators to your dialog, and have those accelerators trigger a particular dialog response. That might be easier.


#!/usr/bin/perl -w

use Gtk2 -init;
use Glib 'FALSE';
use strict;

my $dialog = Gtk2::Dialog->new ('Button Demo', undef, []);

my $label = Gtk2::Label->new ("Hit Ctrl+Return to do it.");
$dialog->vbox->add ($label);
$label->show;

my $textview = Gtk2::TextView->new ();
$dialog->vbox->add ($textview);
$textview->show;

add_button_with_icon ($dialog, "Don't do it", 'gtk-cancel' => 'cancel');
add_button_with_icon ($dialog, "Yeah, do it", 'gtk-ok' => 'ok');

#
# Set up the dialog such that Ctrl+Return will activate the "ok" response.
#
my $accel = Gtk2::AccelGroup->new;
$accel->connect (Gtk2::Gdk->keyval_from_name ('Return'), ['control- mask'], [],
                 sub { $dialog->response ('ok'); });
$dialog->add_accel_group ($accel);

my $response = $dialog->run;
print "Response: $response\n";
$dialog->destroy;

exit;


sub add_button_with_icon {
        my ($dialog, $text, $stock_id, $response_id) = @_;

        my $button = create_button ($text, $stock_id);
        $button->show;

        $dialog->add_action_widget ($button, $response_id);
}

#
# Create a button with a stock icon but with non-stock text.
#
sub create_button {
        my ($text, $stock_id) = @_;

        my $button = Gtk2::Button->new ();
        
        #
        # This setup is cribbed from gtk_button_construct_child()
        # in gtkbutton.c.  It does not handle all the details like
        # left-to-right ordering and alignment and such, as in the
        # real button code.
        #
        my $image = Gtk2::Image->new_from_stock ($stock_id, 'button');
        my $label = Gtk2::Label->new ($text); # accepts mnemonics
        $label->set_mnemonic_widget ($button);

        my $hbox = Gtk2::HBox->new ();
        $hbox->pack_start ($image, FALSE, FALSE, 0);
        $hbox->pack_start ($label, FALSE, FALSE, 0);

        $hbox->show_all ();

        $button->add ($hbox);

        return $button;
}


--
I hate to break it to you, but magic data pixies don't exist.
  -- Simon Cozens





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