Re: Right-Click Menus

From: Date: Sat, 30 Oct 2004 01:50:19 +0000
Subject: Re: Right-Click Menus
References: 1  Groups: php.gtk.general 
Request: Send a blank email to php-gtk-general+get-5188@lists.php.net to get a copy of this message
Hi, Saturday, October 30, 2004, 6:17:15 AM, you wrote: krc> How can you make a right-click menu appear whilst having text krc> selected in an 'entry' at the same time so the user can cut/copy/paste in krc> and out of the field? This should do what you want <?php if( !extension_loaded('gtk')) { dl( 'php_gtk.' . PHP_SHLIB_SUFFIX) || die("Can't load php_gtk module!\n"); } function entry_clicked($entry,$event){ if($event->button == 3){ $info['entry'] =& $entry; $info['start'] = $entry->selection_start_pos; $info['end'] = $entry->selection_end_pos; $menu =& new GtkMenu(); if($entry->has_selection){ $copy =& new GtkMenuItem('Copy'); $copy->connect_object('activate','popupCmd','Copy',$info); $menu->append($copy); } $paste =& new GtkMenuItem('Paste'); $paste->connect_object('activate','popupCmd','Paste',$info); $menu->append($paste); if($entry->has_selection){ $cut =& new GtkMenuItem('Cut'); $cut->connect_object('activate','popupCmd','Cut',$info); $menu->append($cut); } $menu->show_all(); $menu->popup(null,null,null,1,$event->time); } } function popupCmd($cmd,$info){ $start = $info['start']; $end = $info['end']; $info['entry']->select_region($start,$end); //put back selection switch($cmd){ case 'Copy': $info['entry']->copy_clipboard(); break; case 'Paste': $info['entry']->paste_clipboard(); break; case 'Cut': $info['entry']->cut_clipboard(); break; } } $window = &new GtkWindow(); $window->set_position(GTK_WIN_POS_CENTER); $window->set_default_size(400,100); $window->connect_object("destroy", array("gtk", "main_quit")); $window->realize(); $box = &new GtkVBox(false, 5); $window->add($box); $entry = &new GtkEntry(); $entry->connect('button_press_event', 'entry_clicked'); $box->pack_start($entry, false, false, 0); $entry2 = &new GtkEntry(); $entry2->connect('button_press_event', 'entry_clicked'); $box->pack_end($entry2, true, true, 0); $window->show_all(); gtk::main(); I haven't found a way to stop the selection disappearing with the right click. This was tested on windows. -- regards, Tom

« previous php.gtk.general (#5188) next »