Problem
This is in response to Kovu's post titled "Custom Toolbar Icons (not from stock!)"
Instead of using the stock images, he would like to set his own toolbar icons (say, from a pix buffer) as shown below.
The first three tool buttons are using stock images. The next two are using two .gif images, and the last two .png images.
Solution
To use your own images as toolbar icons
- Load the image with GtkImage::new_from_file(filename).
- Then create the tool button with GtkToolButton($img, $label).
Sample Code
The following image files are required by the sample code below. Please save a copy of the image files and put them in the same directory where you store the sample code.
![]() | button_start32.gif |
![]() | button_stop32.gif |
![]() | ball_blue16.png |
![]() | ball_green16.png |
1 2 3 4 5 6 9 10 11 12 13 15 16 17 18 19 20 22 23 24 25 26 27 28 29 31 33 34 35 36 37 38 39 41 42 43 44 46 47 48 50 51 52 53 54 55 56 57 58 59 60 61 63 65 66 67 | <?php $window = new GtkWindow(); $window->set_title($argv[0]); $window->set_size_request(400, 200); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // define menu definition $toolbar_definition = array('New', 'Open', 'Save', '<hr>', 'button_start32.gif|Start', 'button_stop32.gif|Stop', '<hr>', // note 1 'ball_blue16.png|Img 1', 'ball_green16.png|Img 2'); setup_toolbar($vbox, $toolbar_definition); // display title $title = new GtkLabel("Set up Toolbar with your own custom toolbar icons"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $vbox->pack_start($title); $vbox->pack_start(new GtkLabel('')); $window->show_all(); Gtk::main(); // setup toolbar function setup_toolbar($vbox, $toolbar_definition) { $toolbar = new GtkToolBar(); $vbox->pack_start($toolbar, 0, 0); foreach($toolbar_definition as $item) { if ($item=='<hr>') { $toolbar->insert(new GtkSeparatorToolItem(), -1); } else { $stock_image_name = 'Gtk::STOCK_'.strtoupper($item); if (defined($stock_image_name)) { $toolbar_item = GtkToolButton::new_from_stock( // note 2 constant($stock_image_name)); $label = $item; } elseif (preg_match('/\.(png|gif|jpg)/', $item)) { list($img_name, $label) = explode('|', $item); $img = GtkImage::new_from_file($img_name); // note 3 $toolbar_item = new GtkToolButton($img, $label); // note 4 } else { $label = $item; $toolbar_item = new GtkToolButton(null, $label); } $toolbar->insert($toolbar_item, -1); // note 5 $toolbar_item->connect('clicked', 'on_toolbar_button', $label); } } } // process toolbar function on_toolbar_button($button, $item) { // note 6 echo "toolbar clicked: $item\n"; } ?> |
Output
As shown above.Explanation
We make use of the code from How to set up toolbar?
What's new here:
- This is the toolbar definition. You can intermix stock image with custom images. For custom images, the first part is the image filename, and the second part is the label, delimited by '|'.
- Create a tool button using stock image.
- Load the custom image.
- Create a tool button using custom image.
- Insert into toolbar.
- Process toolbutton click here.
Related Links
- How to create detachable toolbar?
- How to set up menu and radio menu - Part 1?
- How to set up menu and radio menu - Part 2 - add stock images?
- How to set up menu and radio menu - Part 3 - add accelerators?
- How to set up menu and radio menu - Part 4 - allow Alt F Alt N?
- How to set up toolbar with custom text and graphics - Part 1 - labels below graphics?
- How to set up toolbar with custom text and graphics - Part 2 - labels on right of graphics?
- How to set up toolbar with custom text and graphics - Part 3 - exact positioning of labels?
Read more...