From: Thien Vuong Date: 2003-03-17T04:39:46+09:00 Subject: Re: |FXRUBY] contextual menu in an Icon List Joel VanderWerf wrote: > Pierre Baillet wrote: > >> Dear rubyists, >> >> has any of you ever managed to Build a FXIconList with a contextual menu >> over every item in the list (contextual menu which would change >> depending on which item you right-clicked on) >> >> So far, all my attempts have given something not really usable. >> >> If so, a code snippet would be really welcomed... > > > I have a file browser class based on FXFileList which has a > context-sensitive popup. FXFileList is based on FXIconList, so the same > technique would work. I'll try to extract a snippet, but if you want the > full code it's in my foxtails package on RAA. Look at > lib/foxtails/FTFileBrowser.rb and examples/file-browser.rb. > > When initializing, you connect the icon list with this method call: > > connect(SEL_RIGHTBUTTONRELEASE, method(:onPopupMenu)) > > and define onPopupMenu as in FXFileSelector::onPopupMenu in the fox source: > > def onPopupMenu sender, sel, event > if event.moved then return 1 end > > index = getItemAt(event.win_x, event.win_y) > > filemenu = FXMenuPane.new(self) > > ### Add your menu items here ### > > FXMenuCommand.new(filemenu, ...) > > filemenu.create() > filemenu.popup(nil, event.root_x, event.root_y) > filemenu.grabKeyboard > getApp().runModalWhileShown(filemenu) > return 1 > end > > I haven't noticed any delay whatsoever in dynamically creating panes > like this. YMMV. > > FoxTails also has dynamically generated popup menu buttons (see > examples/simple.rb and lib/foxtails/FTPopupButton.rb). > > That part about "filemenu.grabKeyboard" is really nice. Thanks much for the tip also. If the menu is complex and shows some delay, I'd split the menu pane instantiation part out, and only bind the create/popup/run to the right click event. You may also have to manage the selection a bit to give some feedbackk on which items this is going to run on. if !isItemSelected(index) killSelection setCurrentItem(index) selectItem(index) end