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

Commit

Permalink
vlastni liststore, treestore pro zjednoduseni pristupu k hodnotam
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Lang committed Mar 7, 2010
1 parent ec8565a commit 6090f0f
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 17 deletions.
4 changes: 3 additions & 1 deletion LPSParser/LPSParser.csproj
Expand Up @@ -185,12 +185,14 @@
<Compile Include="ToolScript\Parser\Statements\TryBlockStatement.cs" />
<Compile Include="ToolScript\Parser\Statements\CatchStatement.cs" />
<Compile Include="ToolScript\Parser\Window\ScrolledExpression.cs" />
<Compile Include="ToolScript\Parser\Window\StoreAndTreeView\TreeViewColumnStatement.cs" />
<Compile Include="ToolScript\Parser\Window\StoreAndTreeView\TreeViewColumnExpression.cs" />
<Compile Include="ToolScript\Parser\Window\StoreAndTreeView\StoreItemStatement.cs" />
<Compile Include="ToolScript\Parser\Window\StoreAndTreeView\StoreExpressionBase.cs" />
<Compile Include="ToolScript\Parser\Window\StoreAndTreeView\ListStoreExpression.cs" />
<Compile Include="ToolScript\Parser\Window\StoreAndTreeView\TreeStoreExpression.cs" />
<Compile Include="ToolScript\Parser\Window\StoreAndTreeView\TreeViewExpression.cs" />
<Compile Include="ToolScript\TreeStore.cs" />
<Compile Include="ToolScript\ListStore.cs" />
</ItemGroup>
<ItemGroup>
<None Include="grammars\c-ansi.grm" />
Expand Down
48 changes: 48 additions & 0 deletions LPSParser/ToolScript/ListStore.cs
@@ -0,0 +1,48 @@
using System;
using Gtk;
using GLib;

namespace LPS.ToolScript
{
public class ListStore : Gtk.ListStore
{
[Obsolete]
protected ListStore(GType gtype)
: base (gtype)
{
}

public ListStore(IntPtr raw)
: base (raw)
{
}

protected ListStore()
: base ()
{
}

public ListStore(params GLib.GType[] types)
: base (types)
{
}

public ListStore(params Type[] types)
: base (types)
{
}

public TreeIter GetIter(TreeView view, TreePath path)
{
TreeIter iter;
if(!GetIter(out iter, path))
throw new InvalidOperationException("Cesta ve stromu není platná");
return iter;
}

public object GetValue(TreeView view, TreePath path, long index)
{
return this.GetValue(GetIter(view, path), (int)index);
}
}
}
Expand Up @@ -89,9 +89,10 @@ public void Invoke(object sender, EventArgs args)
}
}

public EventHandler GetEventHandler(IExecutionContext CustomContext)
public Delegate GetEventHandler(Type EventHandlerType, IExecutionContext CustomContext)
{
return new EventHandler((new EventHandlerHelper(this, CustomContext)).Invoke);
EventHandlerHelper helper = new EventHandlerHelper(this, CustomContext);
return Delegate.CreateDelegate(EventHandlerType, helper, typeof(EventHandlerHelper).GetMethod("Invoke"));
}

public override string ToString ()
Expand Down
Expand Up @@ -66,14 +66,14 @@ public object Execute (NamedArgumentList arguments)
throw new InvalidOperationException(sb.ToString());
}

public EventHandler GetEventHandler (IExecutionContext CustomContext)
public Delegate GetEventHandler (Type EventHandlerType, 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);
return Delegate.CreateDelegate(EventHandlerType, this, typeof(MethodWraper).GetMethod("AsEventHandler"));
}

private void AsEventHandler(object sender, EventArgs args)
public void AsEventHandler(object sender, EventArgs args)
{
Methods[0].Invoke(Instance, new object[] {sender, args});
}
Expand Down
2 changes: 1 addition & 1 deletion LPSParser/ToolScript/Parser/Interfaces/IFunction.cs
Expand Up @@ -5,6 +5,6 @@ namespace LPS.ToolScript.Parser
public interface IFunction
{
object Execute(NamedArgumentList arguments);
EventHandler GetEventHandler(IExecutionContext CustomContext);
Delegate GetEventHandler(Type EventHandlerType, IExecutionContext CustomContext);
}
}
Expand Up @@ -13,7 +13,7 @@ public ListStoreExpression(EvaluatedAttributeList Params, List<StoreItemStatemen

protected override TreeModel CreateStore ()
{
return new ListStore(GetColumnTypes());
return new LPS.ToolScript.ListStore(GetColumnTypes());
}

protected override TreeIter AppendData (TreeModel store, StoreItemStatement item)
Expand Down
Expand Up @@ -13,7 +13,7 @@ public TreeStoreExpression(EvaluatedAttributeList Params, List<StoreItemStatemen

protected override TreeModel CreateStore ()
{
return new TreeStore(GetColumnTypes());
return new LPS.ToolScript.TreeStore(GetColumnTypes());
}

private void AppendChilds(TreeModel store, TreeIter iter, StoreItemStatement item)
Expand Down
@@ -1,5 +1,6 @@
using System;
using Gtk;
using System.Reflection;

namespace LPS.ToolScript.Parser
{
Expand All @@ -22,19 +23,88 @@ public override object Eval (IExecutionContext context)
return this;
}

private void SetColumnAttributes(TreeViewColumn column, WindowContext context)
{
Type t = column.GetType();
t.FindMembers(
MemberTypes.Property | MemberTypes.Event,
BindingFlags.Public | BindingFlags.Instance,
SetGtkPropertyValue, new object[] { column, context });
}

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

public TreeViewColumn CreateColumn(WindowContext context)
{
string title = Params.Get<string>("title", "");
TreeViewColumn column;

switch(this.ColumnType)
{
case "text":
return new TreeViewColumn(title, new CellRendererText(), "text", this.StoreIndex);
column = new TreeViewColumn(title, new CellRendererText(), "text", this.StoreIndex);
break;
case "markup":
return new TreeViewColumn(title, new CellRendererText(), "markup", this.StoreIndex);
column = new TreeViewColumn(title, new CellRendererText(), "markup", this.StoreIndex);
break;
default:
throw new NotSupportedException("Sloupec typu '"+this.ColumnType+"' není podporován");
}
SetColumnAttributes(column, context);
return column;
}

}
Expand Down
6 changes: 4 additions & 2 deletions LPSParser/ToolScript/Parser/Window/WidgetBase.cs
Expand Up @@ -109,12 +109,14 @@ private bool SetGtkPropertyValue(MemberInfo member, object attrs)
foreach(GLib.SignalAttribute signal in ev.GetCustomAttributes(typeof(GLib.SignalAttribute), true))
{
object handler;
if(TryGetAttribute(typeof(object), signal.CName, out handler))
if(TryGetAttribute(typeof(object), signal.CName.Replace('-','_'), out handler))
{
if(handler is Delegate)
ev.AddEventHandler(widget, (Delegate)handler);
else if(handler is IFunction)
ev.AddEventHandler(widget, ((IFunction)handler).GetEventHandler(context));
{
ev.AddEventHandler(widget, ((IFunction)handler).GetEventHandler(ev.EventHandlerType, context));
}
else
throw new NotSupportedException();
return true;
Expand Down
2 changes: 1 addition & 1 deletion LPSParser/ToolScript/ToolScriptFunction.cs
Expand Up @@ -37,7 +37,7 @@ public virtual object Execute (NamedArgumentList arguments)
return args.ReturnValue;
}

public EventHandler GetEventHandler (IExecutionContext CustomContext)
public Delegate GetEventHandler (Type EventHandlerType, IExecutionContext CustomContext)
{
throw new NotImplementedException();
}
Expand Down
48 changes: 48 additions & 0 deletions LPSParser/ToolScript/TreeStore.cs
@@ -0,0 +1,48 @@
using System;
using GLib;
using Gtk;

namespace LPS.ToolScript
{
public class TreeStore : Gtk.TreeStore
{
[Obsolete]
protected TreeStore(GType gtype)
: base (gtype)
{
}

public TreeStore(IntPtr raw)
: base (raw)
{
}

protected TreeStore()
: base ()
{
}

public TreeStore(params GLib.GType[] types)
: base (types)
{
}

public TreeStore(params Type[] types)
: base (types)
{
}

public TreeIter GetIter(TreeView view, TreePath path)
{
TreeIter iter;
if(!GetIter(out iter, path))
throw new InvalidOperationException("Cesta ve stromu není platná");
return iter;
}

public object GetValue(TreeView view, TreePath path, long index)
{
return this.GetValue(GetIter(view, path), (int)index);
}
}
}
4 changes: 2 additions & 2 deletions LPSUtil/Commands/CommandBase.cs
Expand Up @@ -32,9 +32,9 @@ 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)
public Delegate GetEventHandler(Type EventHandlerType, IExecutionContext CustomContext)
{
throw new System.NotImplementedException ();
throw new NotImplementedException();
}

public override string ToString ()
Expand Down
16 changes: 15 additions & 1 deletion LPSUtil/UtilMainWindow.cs
Expand Up @@ -24,7 +24,9 @@ public void AddCommand(ICommand cmd)
public UtilMainWindow() : base(Gtk.WindowType.Toplevel)
{
this.Build();


GLib.ExceptionManager.UnhandledException += UnhandledException;

this.WindowPosition = WindowPosition.Center;
this.WidthRequest = 800;
this.HeightRequest = 500;
Expand Down Expand Up @@ -52,6 +54,18 @@ public UtilMainWindow() : base(Gtk.WindowType.Toplevel)
Instance = this;
}

void UnhandledException(GLib.UnhandledExceptionArgs args)
{
if(args.IsTerminating)
return;
TextBuffer buffer = this.outputView.Buffer;
TextMark mark = buffer.CreateMark(null, buffer.EndIter, true);
WriteBufferWithTag("error", "Vyjímka v obsluze signálu:\n{0}\n",args.ExceptionObject);
outputView.ScrollToMark(mark, 0.0, true, 0.0, 0.0);

args.ExitApplication = false;
}

public void InitCmds()
{
AddCommand(new LoginCommand("login"));
Expand Down

0 comments on commit 6090f0f

Please sign in to comment.