Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > How to properly bind context menus to canvas in Ruby/Tk?
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 12th November 21:10
josef wolf
External User
 
Posts: 1
Default How to properly bind context menus to canvas in Ruby/Tk?



Hello!

I am trying to bind context-menus to a Tk canvas. The canvas itself should
have a context menu, giving the ability to create new objects. In addition,
every object should have its own context menu so that the object can be
modified/deleted. This is what I'm doing:

#! /usr/bin/ruby
require 'tk'
$-w = 1

root = TkRoot.new
canvas = TkCanvas.new.pack

# This is the context menu for the canvas to create new objects
#
menu=TkMenu.new
menu.add('command', 'label'=>'New Foo', 'command'=>proc{p "new foo"})
menu.add('command', 'label'=>'New Bar', 'command'=>proc{p "new bar"})

# canvas binding
canvas.bind("Button-3") { |e| evt=e; menu.popup(e.x_root, e.y_root) }

# Now create a new object and bind to it a context menu to modify/delete
# the object
#
rect = TkcRectangle.new(canvas, 0, 0, 50, 50, "fill"=>"white")
menu=TkMenu.new
menu.add('command', 'label'=>'Edit', 'command'=>proc{p "edit"})
menu.add('command', 'label'=>'Delete', 'command'=>proc{p "del"})

# object binding
canvas.itembind(rect, "Button-3") { |e| menu.popup(e.x_root, e.y_root) }

Tk.mainloop

Unfortunately, this don't work as desired. As soon as the binding to the
object is done, the canvas binding seems to be overridden. Even when
right-klicking on empty space in the canvas, the object's menu is invoked.

Any ideas what I am doing wrong here?
  Reply With Quote


 


2 12th November 21:10
paul lutus
External User
 
Posts: 1
Default How to properly bind context menus to canvas in Ruby/Tk?



First, rename the second menu something other than "menu" You have already
used that name, and this multiple use is causing you a lot of confusion.

Second, after a bit of experimentation, I offer this educated guess. The
rectangle object cannot process mouse events, so it cannot invoke a context
menu different from that bound to the canvas.

It is apparent that the canvas is receiving all mouse events, not the
rectangle, and in order for the rectangle to launch its own context menu,
it would have to process mouse events independently. AFAICS it can't.

I want to emphasize this is just a guess, and I don't use the 'Tk' library
because it is too poorly documented.

Have you considered using something other than 'Tk'?

--
Paul Lutus
http://www.arachnoid.com
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666