FOX Community

Recent site activity

Cookbook‎ > ‎

Right mouse context menu

Problem

You want to display a context-sensitive popup menu when the user clicks the right mouse button (e.g. while positioned over a list item).

Solution

Here's the canonical solution to this problem, presented in Ruby code:

theListWidget.connect(SEL_RIGHTBUTTONRELEASE) { |sender, sel, event|
unless event.moved?
FXMenuPane.new(self) do |theMenuPane|
FXMenuCommand.new(theMenuPane, "Undo")
FXMenuCommand.new(theMenuPane, "Redo")
FXMenuSeparator.new(theMenuPane)
FXMenuCommand.new(theMenuPane, "Cut")
FXMenuCommand.new(theMenuPane, "Copy")
FXMenuCommand.new(theMenuPane, "Paste")
FXMenuCommand.new(theMenuPane, "Select All")
theMenuPane.create
theMenuPane.popup(nil, event.root_x, event.root_y)
getApp().runModalWhileShown(theMenuPane)
end
end
}

Note that for this example, the event is tied to the SEL_RIGHTBUTTONRELEASE message and not the SEL_RIGHTBUTTONPRESS message.

Note that the 'nil' (or NULL for C++ programmers) in the call to popup() is required if you want runModalWhileShown to work as expected. If you specify some other FXWindow for that first argument, you will confuse the messaging loops and lose modality. I only mention this because when I wrote similar code myself I passed in the "owner" window and had all sorts of bugs. I didn't notice the 'nil' in the above example until someone kindly pointed it out to me.

Contributor(s)

Lyle Johnson