1 module gtk.PopupMenu; 2 3 import gtk.Main; 4 import gtk.EventBox; 5 import gtk.MainWindow; 6 import gtk.Menu; 7 import gtk.Label; 8 import gtk.ImageMenuItem; 9 import gtk.Widget; 10 import gtk.AccelGroup; 11 import gdk.Event; 12 13 class ExampleWindow : MainWindow 14 { 15 Menu menu; 16 17 this() 18 { 19 super("GtkD: Popup Menu"); 20 setDefaultSize(200, 200); 21 22 auto eventBox = new EventBox(); 23 eventBox.add( new Label("Right click") ); 24 eventBox.addOnButtonPress(&onButtonPress); 25 add(eventBox); 26 27 menu = new Menu(); 28 menu.append( new ImageMenuItem(StockID.CUT, cast(AccelGroup)null) ); 29 menu.append( new ImageMenuItem(StockID.COPY, cast(AccelGroup)null) ); 30 menu.append( new ImageMenuItem(StockID.PASTE, cast(AccelGroup)null) ); 31 menu.append( new ImageMenuItem(StockID.DELETE, cast(AccelGroup)null) ); 32 menu.attachToWidget(eventBox, null); 33 34 showAll(); 35 } 36 37 public bool onButtonPress(Event event, Widget widget) 38 { 39 if ( event.type == EventType.BUTTON_PRESS ) 40 { 41 GdkEventButton* buttonEvent = event.button; 42 43 if ( buttonEvent.button == 3) 44 { 45 menu.showAll(); 46 menu.popup(buttonEvent.button, buttonEvent.time); 47 48 return true; 49 } 50 } 51 return false; 52 } 53 } 54 55 void main(string[] arg) 56 { 57 Main.init(arg); 58 59 new ExampleWindow(); 60 61 Main.run(); 62 }