public ToolMenuButton(Gtk.Toolbar toolbar, Gtk.Image image, string label, Gtk.Menu menu) : base() { this.IconWidget = image; Gtk.Label l = new Gtk.Label(label); l.UseUnderline = true; this.LabelWidget = l; this.CanFocus = true; // this.FocusOnClick = false; // TODO: Not supported anymore? this.menu = menu; menu.AttachToWidget(this, GuiUtils.DetachMenu); menu.Deactivated += ReleaseButton; this.ShowAll(); }
protected override void ShowPopupMenu(Gdk.Event ev) { Gtk.Menu menu = new Gtk.Menu(); for (int i = 0; i < ObjectGroupEditor.ObjectNames.Length; i++) { if (i >= 2 && i <= 4) // Skip "Pointer" objects { continue; } Gtk.MenuItem item = new Gtk.MenuItem("Add " + ObjectGroupEditor.ObjectNames[i]); menu.Append(item); int index = i; item.Activated += (sender, args) => { SetSelectedIndex(ObjectGroup.AddObject((ObjectType)index)); }; } if (HoveringIndex != -1) { menu.Append(new Gtk.SeparatorMenuItem()); Gtk.MenuItem deleteItem = new Gtk.MenuItem("Delete"); deleteItem.Activated += (sender, args) => { if (SelectedIndex != -1) { ObjectGroup.RemoveObject(SelectedIndex); } }; menu.Append(deleteItem); } menu.AttachToWidget(this, null); menu.ShowAll(); menu.PopupAtPointer(ev); }
private bool DoPopupMenu(Gdk.EventButton evnt) { Gtk.Menu menu = new Gtk.Menu(); PopulatePopup(this, menu); if (menu.Children.Length == 0) { return(false); } menu.AttachToWidget(this, null); if (evnt != null) { menu.Popup(null, null, null, evnt.Button, evnt.Time); } else { menu.Popup(); } return(true); }
/// <summary>Shows a context menu.</summary> /// <param name='menu'>The menu.</param> /// <param name='parent'>The parent widget.</param> /// <param name='evt'>The mouse event. May be null if triggered by keyboard.</param> /// <param name='caret'>The caret/selection position within the parent, if the EventButton is null.</param> public static void ShowContextMenu(Gtk.Menu menu, Gtk.Widget parent, Gdk.EventButton evt, Gdk.Rectangle caret) { Gtk.MenuPositionFunc posFunc = null; if (parent != null) { menu.AttachToWidget(parent, null); posFunc = delegate(Gtk.Menu m, out int x, out int y, out bool pushIn) { Gdk.Window window = evt != null? evt.Window : parent.GdkWindow; window.GetOrigin(out x, out y); var alloc = parent.Allocation; if (evt != null) { x += (int)evt.X; y += (int)evt.Y; } else if (caret.X >= alloc.X && caret.Y >= alloc.Y) { x += caret.X; y += caret.Y + caret.Height; } else { x += alloc.X; y += alloc.Y; } Gtk.Requisition request = m.SizeRequest(); var screen = parent.Screen; Gdk.Rectangle geometry = GetUsableMonitorGeometry(screen, screen.GetMonitorAtPoint(x, y)); //whether to push or flip menus that would extend offscreen //FIXME: this is the correct behaviour for mac, check other platforms bool flip_left = true; bool flip_up = false; if (x + request.Width > geometry.Right) { if (flip_left) { x -= request.Width; } else { x = geometry.Right - request.Width; } if (x < geometry.Left) { x = geometry.Left; } } if (y + request.Height > geometry.Bottom) { if (flip_up) { y -= request.Height; } else { y = geometry.Bottom - request.Height; } if (y < geometry.Top) { y = geometry.Top; } } pushIn = false; }; } uint time; uint button; if (evt == null) { time = Gtk.Global.CurrentEventTime; button = 0; } else { time = evt.Time; button = evt.Button; } //HACK: work around GTK menu issues on mac when passing button to menu.Popup //some menus appear and immediately hide, and submenus don't activate if (Platform.IsMac) { button = 0; } menu.Popup(null, null, posFunc, button, time); }