PHP-GTK2 Cookbook Forum
Welcome, Guest
Please Login or Register.    Lost Password?
Re:How to make a popup menu on a GtkToolbar (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: Re:How to make a popup menu on a GtkToolbar
#625
Vadi (User)
Fresh Boarder
Posts: 9
graphgraph
User Offline Click here to see the profile of this user
How to make a popup menu on a GtkToolbar 3 Months, 1 Week ago Karma: 0  
I'd like to create a popup menu when a user right-clicks on a GtkToolbar... is it possible to do that and how?
 
  The administrator has disabled public write access.
#628
kksou (Admin)
Admin
Posts: 391
graph
User Online Now Click here to see the profile of this user
Re:How to make a popup menu on a GtkToolbar 3 Months, 1 Week ago Karma: 8  
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 150);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// define menu definition
$toolbar_definition = array('New', 'Open', 'Save', '<hr>', //n4 note 1
'Cut', 'Copy', 'Paste', '<hr>', //4
'Undo','Redo'); //4
setup_toolbar($vbox, $toolbar_definition);

// display title
$title = new GtkLabel("Set up popup menu with right mouse click in toolbar");
$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) { //n4 note 1
$toolbar = new GtkToolBar(); //n4 note 2
$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(
constant($stock_image_name));
$toolbar->insert($toolbar_item, -1);
$toolbar_item->connect('clicked',
'on_toolbar_button', $item);
$toolbar_item->child->connect(
'button-press-event', 'on_button', $item);
}
}
}
}

// process toolbar
function on_toolbar_button($button, $item) { //n4 note 6
echo "toolbar clicked: $item\n";
}

function on_button($button, $event, $item) {
if ($event->button==1) return false;
if ($event->button==2) return true; // do nothing
if ($event->button==3) { // it's the right mouse button!
$prefix = $item;
// displays the popup menu
$menu_definition = array(
"$item - menu item 1",
"$item - menu item 2",
"$item - menu item 3",
'<hr>',
"$item - menu item 4",
"$item - menu item 5",
"$item - menu item 6"
);
$menu = show_popup_menu($menu_definition);
return true;
}
}

// show popup menu
function show_popup_menu($menu_definition) {
$menu = new GtkMenu();
foreach($menu_definition as $menuitem_definition) {
if ($menuitem_definition=='<hr>') {
$menu->append(new GtkSeparatorMenuItem());
} else {
$menu_item = new GtkMenuItem($menuitem_definition);
$menu->append($menu_item);
$menu_item->connect('activate', 'on_popup_menu_select');
}
}
$menu->show_all();
$menu->popup();
}

// process popup menu item selection
function on_popup_menu_select($menu_item) {
$item = $menu_item->child->get_label();
echo "popup menu selected: $item\n";
}
?>

Regards,
/kksou
 
 
Last Edit: 2008/08/10 20:39 By kksou.
  The administrator has disabled public write access.
#629
Vadi (User)
Fresh Boarder
Posts: 9
graphgraph
User Offline Click here to see the profile of this user
Re:How to make a popup menu on a GtkToolbar 3 Months, 1 Week ago Karma: 0  
Thank you
 
  The administrator has disabled public write access.
#650
kksou (Admin)
Admin
Posts: 391
graph
User Online Now Click here to see the profile of this user
Re:How to make a popup menu on a GtkToolbar 3 Months, 1 Week ago Karma: 8  
 
  The administrator has disabled public write access.
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved