loupiottes/DMX-2.0/HelperFunctions.cs
tzim 9c41df3665 * Conduite.cs: Optimisation : acces direct : pas de conversion
'AsReadOnly'

* SequenceurLineaire.cs: Optimisation : Retrait d'un controle inutile.

* SeqLinUI.cs: Optimisation : Pas de lecture du nom de chaque tirette
  a chaque MAJ : on stocke localement et on change juste quand
  nécessaire

* HelperFunctions.cs: Classe de mesure de temps. Innutilisée,
  normalement
2013-11-22 11:06:37 +00:00

118 lines
No EOL
2.6 KiB
C#

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<ContextMenuEventArgs> ContextMenu;
public ContextMenuHelper ()
{
}
public ContextMenuHelper (Widget widget)
{
AttachToWidget (widget);
}
public ContextMenuHelper (Widget widget, EventHandler<ContextMenuEventArgs> 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);
}
//}
}
}
}