/* Copyright (C) Arnaud Houdelette 2012-2014 Copyright (C) Emmanuel Langlois 2012-2014 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ using System; using Gdk; using GLib; using Gtk; using System.Xml; namespace DMX2 { class TimerHelper:IDisposable{ string text; DateTime st; public TimerHelper (string _text) { text=_text; st= DateTime.Now; } #region IDisposable implementation public void Dispose () { Console.WriteLine("{0} -> {1}",text,DateTime.Now - st); } #endregion } public static class XmlHelpers { public static string TryGetAttribute (this XmlElement element, string name, string defaultval) { if(!element.HasAttribute(name)) return defaultval; return element.GetAttribute(name); } } public class ContextMenuEventArgs : EventArgs { private Widget widget; public Widget Widget { get { return widget; } } private bool rightClick; public bool RightClick { get { return rightClick; } } public ContextMenuEventArgs (Widget widget, bool rightClick) { this.widget = widget; this.rightClick = rightClick; } } public class ContextMenuHelper { public event EventHandler ContextMenu; public ContextMenuHelper () { } public ContextMenuHelper (Widget widget) { AttachToWidget (widget); } public ContextMenuHelper (Widget widget, EventHandler handler) { AttachToWidget (widget); ContextMenu += handler; } public void AttachToWidget (Widget widget) { widget.PopupMenu += Widget_PopupMenu; widget.ButtonPressEvent += Widget_ButtonPressEvent; } public void DetachFromWidget (Widget widget) { widget.PopupMenu -= Widget_PopupMenu; widget.ButtonPressEvent -= Widget_ButtonPressEvent; } [GLib.ConnectBefore] private void Widget_PopupMenu (object o, PopupMenuArgs args) { RaiseContextMenuEvent (args, (Widget)o, false); } [GLib.ConnectBefore] private void Widget_ButtonPressEvent (object o, ButtonPressEventArgs args) { if (args.Event.Button == 3 && args.Event.Type == EventType.ButtonPress) { RaiseContextMenuEvent (args, (Widget)o, true); } } //private bool propagating = false; //Prevent reentry private void RaiseContextMenuEvent (SignalArgs signalArgs, Widget widget, bool rightClick) { //if (!propagating) { //Propagate the event // Event evnt = Gtk.Global.CurrentEvent; // propagating = true; // Gtk.Global.PropagateEvent (widget, evnt); // propagating = false; signalArgs.RetVal = true; //The widget already processed the event in the propagation //Raise the context menu event ContextMenuEventArgs args = new ContextMenuEventArgs (widget, rightClick); if (ContextMenu != null) { ContextMenu.Invoke (this, args); } //} } } }