Skip to content
This repository has been archived by the owner on May 19, 2019. It is now read-only.

Commit

Permalink
nabindeni gtk signalu na skript
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Lang committed Mar 5, 2010
1 parent c24ff2d commit 1ccc141
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 50 deletions.
23 changes: 9 additions & 14 deletions LPSParser/ToolScript/ExecutionContext.cs
Expand Up @@ -5,7 +5,7 @@

namespace LPS.ToolScript
{
public class ExecutionContext : IExecutionContext, ICloneable
public class ExecutionContext : IExecutionContext
{
public IExecutionContext GlobalContext { get; private set; }
public IExecutionContext ParentContext { get; private set; }
Expand Down Expand Up @@ -97,7 +97,7 @@ public object Eval(string code)
return SpecialValue.Void;
}

public void Dispose()
public virtual void Dispose()
{
this.LocalVariables.Clear();
}
Expand Down Expand Up @@ -139,7 +139,13 @@ public void UnsetVariable(string name)
this.LocalVariables.Remove(name);
}

public virtual ExecutionContext Clone()
public object this[string name]
{
get { return GetVariable(name); }
set { SetVariable(name, value); }
}

public virtual object Clone()
{
ExecutionContext clone = (ExecutionContext)this.MemberwiseClone();
clone.LocalVariables = new Dictionary<string, object>();
Expand All @@ -152,16 +158,5 @@ public virtual ExecutionContext Clone()
}
return clone;
}

public object this[string name]
{
get { return GetVariable(name); }
set { SetVariable(name, value); }
}

object ICloneable.Clone()
{
return this.Clone();
}
}
}
Expand Up @@ -53,6 +53,47 @@ public object Execute(NamedArgumentList arguments)
}
}

private class EventHandlerHelper
{
private FunctionExpression Func;
private IExecutionContext CustomContext;

public EventHandlerHelper(FunctionExpression Func, IExecutionContext CustomContext)
{
this.Func = Func;
this.CustomContext = CustomContext;
}

public void Invoke(object sender, EventArgs args)
{
IExecutionContext context;
if(CustomContext != null)
context = CustomContext.CreateChildContext();
else
context = Func.Context.CreateChildContext();
try
{
context.InitVariable("sender", sender);
context.InitVariable("args", args);
Func.Body.Run(context);
}
catch(IterationTermination info)
{
if(info.Reason != TerminationReason.Return)
throw new InvalidOperationException("volání funkce bylo přerušeno jiným důvodem než return: " + info.Reason.ToString());
}
finally
{
context.Dispose();
}
}
}

public EventHandler GetEventHandler(IExecutionContext CustomContext)
{
return new EventHandler((new EventHandlerHelper(this, CustomContext)).Invoke);
}

public override string ToString ()
{
return string.Format("function({0})", Parameters.ToString());
Expand Down
12 changes: 12 additions & 0 deletions LPSParser/ToolScript/Parser/Expressions/Callable/MethodWraper.cs
Expand Up @@ -65,5 +65,17 @@ public object Execute (NamedArgumentList arguments)
sb.AppendLine(method.ToString());
throw new InvalidOperationException(sb.ToString());
}

public EventHandler GetEventHandler (IExecutionContext CustomContext)
{
if(Methods.Length != 1 || Methods[0].GetParameters().Length != 2)
throw new Exception("Metoda nelze použít jako event handler");
return new EventHandler(AsEventHandler);
}

private void AsEventHandler(object sender, EventArgs args)
{
Methods[0].Invoke(Instance, new object[] {sender, args});
}
}
}
Expand Up @@ -16,11 +16,16 @@ public NamedArgument(string Name, IExpression DefaultValue)
this.Value = SpecialValue.VariableNotSet;
}

public NamedArgument(string Name, IExpression DefaultValue, object Value)
{
this.Name = Name;
this.DefaultValue = DefaultValue;
this.Value = Value;
}

public void Run(IExecutionContext context)
{
if(this.DefaultValue == null)
this.Value = SpecialValue.VariableNotSet;
else
if(this.DefaultValue != null)
this.Value = this.DefaultValue.Eval(context);
}
}
Expand Down
Expand Up @@ -78,7 +78,7 @@ public bool TryGet(Type type, string name, out object value)
if(result.Value != null)
{
Type t = result.Value.GetType();
if(t != type || t.IsSubclassOf(type))
if(t != type && !t.IsSubclassOf(type))
{
value = Convert.ChangeType(result.Value, type);
return true;
Expand Down
Expand Up @@ -3,7 +3,7 @@

namespace LPS.ToolScript
{
public interface IExecutionContext : IDisposable
public interface IExecutionContext : IDisposable, ICloneable
{
IExecutionContext GlobalContext { get; }
IExecutionContext ParentContext { get; }
Expand Down
1 change: 1 addition & 0 deletions LPSParser/ToolScript/Parser/Interfaces/IFunction.cs
Expand Up @@ -5,5 +5,6 @@ namespace LPS.ToolScript.Parser
public interface IFunction
{
object Execute(NamedArgumentList arguments);
EventHandler GetEventHandler(IExecutionContext CustomContext);
}
}
75 changes: 52 additions & 23 deletions LPSParser/ToolScript/Parser/Window/WidgetBase.cs
Expand Up @@ -18,7 +18,9 @@ public override object Eval (IExecutionContext context)
{
Params.Eval(context);
if(this.Name != null)
context.InitVariable(this.Name, this);
{
context.ParentContext.InitVariable(this.Name, this);
}
return this;
}

Expand Down Expand Up @@ -50,51 +52,78 @@ public virtual bool TryGetAttribute(Type type, string name, out object value)
public virtual Gtk.Widget Build(WindowContext context)
{
Gtk.Widget widget = CreateWidget(context);
SetWidgetAttributes(widget);
SetWidgetAttributes(widget, context);
if(HasAttribute("name"))
context.InitVariable(GetAttribute<string>("name"), widget, false);
return widget;
}

protected abstract Gtk.Widget CreateWidget(WindowContext context);

public virtual void SetWidgetAttributes(Gtk.Widget widget)
public virtual void SetWidgetAttributes(Gtk.Widget widget, WindowContext context)
{
Type t = widget.GetType();
t.FindMembers(
MemberTypes.Property,
MemberTypes.Property | MemberTypes.Event,
BindingFlags.Public | BindingFlags.Instance,
SetGtkPropertyValue, widget);
widget.Visible = GetAttribute<bool>("visible", !HasAttribute("hidden"));
SetGtkPropertyValue, new object[] { widget, context });
if(!(widget is Gtk.Window))
widget.Visible = GetAttribute<bool>("visible", !HasAttribute("hidden"));
}

private bool SetGtkPropertyValue(MemberInfo member, object widget)
private bool SetGtkPropertyValue(MemberInfo member, object attrs)
{
PropertyInfo prop = (PropertyInfo)member;
if(!prop.CanWrite)
return false;
bool gtkprop = false;
object val;
foreach(GLib.PropertyAttribute propname in member.GetCustomAttributes(typeof(GLib.PropertyAttribute), true))
object widget = ((object[])attrs)[0];
WindowContext context = (WindowContext)(((object[])attrs)[1]);
if(member is PropertyInfo)
{
gtkprop = true;
if(TryGetAttribute(prop.PropertyType, propname.Name.Replace('-','_'), out val))
PropertyInfo prop = (PropertyInfo)member;
if(!prop.CanWrite)
return false;
bool gtkprop = false;
object val;
foreach(GLib.PropertyAttribute propname in member.GetCustomAttributes(typeof(GLib.PropertyAttribute), true))
{
prop.SetValue(widget, val, null);
return true;
gtkprop = true;
if(TryGetAttribute(prop.PropertyType, propname.Name.Replace('-','_'), out val))
{
prop.SetValue(widget, val, null);
return true;
}
else if(!String.IsNullOrEmpty(propname.Nickname) && TryGetAttribute(prop.PropertyType, propname.Nickname.Replace('-','_'), out val))
{
prop.SetValue(widget, val, null);
return true;
}
}
else if(!String.IsNullOrEmpty(propname.Nickname) && TryGetAttribute(prop.PropertyType, propname.Nickname.Replace('-','_'), out val))
if(TryGetAttribute(prop.PropertyType, prop.Name, out val))
{
prop.SetValue(widget, val, null);
return true;
return true; //??
}
return gtkprop;
}
if(TryGetAttribute(prop.PropertyType, prop.Name, out val))
else if(member is EventInfo)
{
prop.SetValue(widget, val, null);
return true; //??
EventInfo ev = (EventInfo)member;
foreach(GLib.SignalAttribute signal in ev.GetCustomAttributes(typeof(GLib.SignalAttribute), true))
{
object handler;
if(TryGetAttribute(typeof(object), signal.CName, out handler))
{
if(handler is Delegate)
ev.AddEventHandler(widget, (Delegate)handler);
else if(handler is IFunction)
ev.AddEventHandler(widget, ((IFunction)handler).GetEventHandler(context));
else
throw new NotSupportedException();
return true;
}
}
return false;
}
return gtkprop;
else
throw new NotSupportedException();
}

public override bool EvalAsBool (IExecutionContext context)
Expand Down
2 changes: 1 addition & 1 deletion LPSParser/ToolScript/Parser/Window/WindowExpression.cs
Expand Up @@ -24,7 +24,7 @@ public override object Eval(IExecutionContext context)

public WindowContext Create()
{
WindowContext result = wincontext.CloneWindowContext();
WindowContext result = (WindowContext)wincontext.Clone();
result.Window = (Gtk.Window)Build(result);
return result;
}
Expand Down
6 changes: 6 additions & 0 deletions LPSParser/ToolScript/ToolScriptFunction.cs
Expand Up @@ -36,5 +36,11 @@ public virtual object Execute (NamedArgumentList arguments)
Invoked(this, args);
return args.ReturnValue;
}

public EventHandler GetEventHandler (IExecutionContext CustomContext)
{
throw new NotImplementedException();
}

}
}
27 changes: 20 additions & 7 deletions LPSParser/ToolScript/WindowContext.cs
Expand Up @@ -19,15 +19,10 @@ public static WindowContext CreateAsChild(IExecutionContext parent)
return new WindowContext(parent, parent.GlobalContext, parent.Parser);
}

public WindowContext CloneWindowContext()
{
WindowContext clone = (WindowContext)base.Clone();
clone.AccelGroup = new Gtk.AccelGroup();
return clone;
}

public WindowContext Show()
{
if(this.Window is Gtk.Dialog)
throw new InvalidOperationException("Okno je dialog, použij ShowDialog()");
this.Window.ShowAll();
return this;
}
Expand All @@ -46,5 +41,23 @@ public int ShowDialog()
}
}

public override object Clone ()
{
WindowContext clone = (WindowContext)base.Clone();
clone.Window = null;
clone.AccelGroup = new Gtk.AccelGroup();
return clone;
}

public override void Dispose ()
{
base.Dispose();
if(this.Window != null)
{
this.Window.Destroy();
this.Window = null;
}
}

}
}
5 changes: 5 additions & 0 deletions LPSUtil/Commands/CommandBase.cs
Expand Up @@ -32,6 +32,11 @@ protected T Get<T>(object[] parameters, int index)

public abstract object Execute(IExecutionContext context, TextWriter Out, TextWriter Info, TextWriter Err, object[] Params);

public EventHandler GetEventHandler (IExecutionContext CustomContext)
{
throw new System.NotImplementedException ();
}

public override string ToString ()
{
return String.Format("function {0}(...);", Name);
Expand Down

0 comments on commit 1ccc141

Please sign in to comment.