Problem
You have set up a button that uses pre-defined stock image but with different label. Let's now add a mnemonic to the button (e.g. Alt-N for File New) as shown below:
Solution
- Set up a button that uses stock image as decribed in the previous article.
- When setting the button label, just put a underscore in front of the character that you want to set as the accelerator key e.g. "File _New".
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18 19 20 23 26 27 28 29 35 36 37 38 39 40 41 42 43 51 52 53 54 55 56 59 60 61 64 65 67 68 69 70 74 | <?php $window = new GtkWindow(); $window->set_size_request(400, 200); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Using Stock Images but with different labels\n". "Part 3 - add mnemonic"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $title->set_justify(Gtk::JUSTIFY_CENTER); $alignment = new GtkAlignment(0.5, 0, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0); // setup button $vbox->pack_start($hbox = new GtkHBox(), 0); $hbox->pack_start(create_button(Gtk::STOCK_NEW, '_New File'), 0); // note 1 $hbox->pack_start(create_button(Gtk::STOCK_OPEN, '_Open File'), 0); // note 1 $hbox->pack_start(create_button(Gtk::STOCK_CLOSE, '_Close File'), 0); // note 1 $vbox->pack_start(new GtkLabel(), 0); $vbox->pack_start($hbox = new GtkHBox(), 0); $hbox->pack_start(create_button(Gtk::STOCK_MEDIA_PREVIOUS, 'Pre_v'), 0); // note 1 $hbox->pack_start(create_button(Gtk::STOCK_MEDIA_PLAY), 0); $hbox->pack_start(create_button(Gtk::STOCK_MEDIA_STOP), 0); $hbox->pack_start(create_button(Gtk::STOCK_MEDIA_NEXT), 0); $window->show_all(); Gtk::main(); function create_button($stock_id, $label='') { if ($label=='') { return GtkButton::new_from_stock($stock_id); } $button = new GtkButton($label); $img=GtkImage::new_from_stock($stock_id, Gtk::ICON_SIZE_SMALL_TOOLBAR); return $button; } ?> |
Output
As shown above.
Explanation
- Set the accelerator key by prefixing the key with '_' (underscore).
Read more...