[plt-scheme] a popup-menu ?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Wed Dec 17 09:46:19 EST 2003

At Mon, 15 Dec 2003 13:14:59 EST, TheKoRnKilleR at aol.com wrote:
> I'm developping a little interpreter Prolog with DrScheme and I've a problem 
> with a mouse event. I've defined a frame in which I've also defined 2 panels. 
> Each one contains an editor-canvas% and its text% associated.
> I'd like to open a little popup menu when the mouse right button is clicked 
> (with items like "edit","copy","paste" ...). I tried in several ways but no 
> one 
> works ...

Below is one way to do it --- the right way, I think when the popup
menu is associated with a text%.

It's also possible to override `on-event' in editor-canvas% to
intercept the mouse event directly, and then use `popup-menu' in
editor-canvas%. (That technique works for plain canvas%, too.)



(define f (new frame%
	       [label "Example"]
	       [width 200]
	       [height 200]))
(define e (new text%))
(new editor-canvas%
     [parent f]
     [editor e])

;; In the editor keymap, map right-button click to
;;  popup a menu
(let ([km (send e get-keymap)])
  ;; Add a popup-menu function to the keymap:
  (send km add-function "do-popup"
	(lambda (e evt)
          ;; Create the menu....
	  (let ([menu (new popup-menu%)])
	    (new menu-item% 
		 [parent menu]
		 [label "Example"]
		 [callback (lambda (i evt)
			     (printf "Got it~n"))])
            ;; Convert the event coordinates to editr coordinates
	    (let-values ([(x y) (send e dc-location-to-editor-location
				      (send evt get-x) (send evt get-y))])
              ;; Ask the editor's display to show the pop-up menu
	      (send (send e get-admin) popup-menu menu x y)))))
  ;; Map rightbutton to the popup-menu function
  (send km map-function "rightbutton" "do-popup"))

(send f show #t)

Posted on the users mailing list.