1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
// WARNING: This file is in UTF8 format due to the use of ã
// XML.custom
//
// Author: Ricardo Fernández Pascual <ric@users.sourceforge.net>
//
// Copyright (c) 2002 Ricardo Fernández Pascual
//
// Field binding code by Rachel Hestilow <hestilow@ximian.com>
// Copyright (c) 2003 Rachel Hestilow
//
// This code is inserted after the automatically generated code.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General
// Public License as published by the Free Software Foundation.
//
// 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// keep this around so it doesn't get GC'd
static GladeSharp.XMLCustomWidgetHandlerWrapper callback_wrapper = null;
[DllImport("libglade-2.0-0.dll")]
static extern void glade_set_custom_handler (GladeSharp.XMLCustomWidgetHandlerNative handler, IntPtr user_data);
static public void SetCustomHandler (Glade.XMLCustomWidgetHandler handler)
{
callback_wrapper = new GladeSharp.XMLCustomWidgetHandlerWrapper (handler, null);
glade_set_custom_handler (callback_wrapper.NativeDelegate, IntPtr.Zero);
}
[DllImport("gladesharpglue")]
static extern IntPtr gtksharp_glade_xml_get_filename (IntPtr raw);
public string Filename {
get {
string ret;
IntPtr ptr = gtksharp_glade_xml_get_filename (Handle);
if (ptr == IntPtr.Zero)
{
// from resource
ret = System.Reflection.Assembly.GetCallingAssembly ().Location;
}
else
{
ret = Marshal.PtrToStringAnsi (ptr);
}
return ret;
}
}
public Gtk.Widget this [string name] {
get {
return GetWidget (name);
}
}
[DllImport("libglade-2.0-0.dll")]
static extern IntPtr glade_get_widget_name (IntPtr widget);
static public string GetWidgetName (Gtk.Widget w) {
string ret;
IntPtr ptr = glade_get_widget_name (w.Handle);
if (ptr == IntPtr.Zero)
ret = "";
else
ret = Marshal.PtrToStringAnsi (ptr);
return ret;
}
[DllImport("libglade-2.0-0.dll")]
static extern IntPtr glade_get_widget_tree (IntPtr widget);
static public Glade.XML GetWidgetTree (Gtk.Widget w) {
IntPtr ret_raw = glade_get_widget_tree (w.Handle);
Glade.XML ret = GLib.Object.GetObject (ret_raw, false) as Glade.XML;
return ret;
}
/* a constructor that reads the XML from a Stream */
[DllImport("libglade-2.0-0.dll")]
static extern IntPtr glade_xml_new_from_buffer(byte[] buffer, int size, string root, string domain);
public XML (System.IO.Stream s, string root, string domain) : base (IntPtr.Zero)
{
if (GetType() != typeof (XML))
throw new InvalidOperationException ("Can't chain to this constructor from subclasses.");
if (s == null)
throw new ArgumentNullException ("s");
int size = (int) s.Length;
byte[] buffer = new byte[size];
s.Read (buffer, 0, size);
Raw = glade_xml_new_from_buffer(buffer, size, root, domain);
}
public XML (string resource_name, string root) : this (System.Reflection.Assembly.GetEntryAssembly (), resource_name, root, null)
{
}
public XML (System.Reflection.Assembly assembly, string resource_name, string root, string domain) : base (IntPtr.Zero)
{
if (GetType() != typeof (XML))
throw new InvalidOperationException ("Cannot chain to this constructor from subclasses.");
if (assembly == null)
assembly = System.Reflection.Assembly.GetCallingAssembly ();
System.IO.Stream s = assembly.GetManifestResourceStream (resource_name);
if (s == null)
throw new ArgumentException ("Cannot get resource file '" + resource_name + "'",
"resource_name");
int size = (int) s.Length;
byte[] buffer = new byte[size];
s.Read (buffer, 0, size);
s.Close ();
Raw = glade_xml_new_from_buffer(buffer, size, root, domain);
}
/* signal autoconnection using reflection */
public void Autoconnect (object handler)
{
BindFields (handler);
SignalConnector sc = new SignalConnector (this, handler);
sc.Autoconnect ();
}
public void Autoconnect (Type handler_class)
{
BindFields (handler_class);
SignalConnector sc = new SignalConnector (this, handler_class);
sc.Autoconnect ();
}
class SignalConnector
{
/* the Glade.XML object whose signals we want to connect */
XML gxml;
/* the object to look for handlers */
object handler_object;
/* the type to look for handlers if no object has been specified */
Type handler_type;
public SignalConnector (XML gxml, object handler)
{
this.gxml = gxml;
this.handler_object = handler;
this.handler_type = handler.GetType ();
}
public SignalConnector (XML gxml, Type type)
{
this.gxml = gxml;
this.handler_object = null;
this.handler_type = type;
}
delegate void RawXMLConnectFunc (string handler_name, IntPtr objekt,
string signal_name, string signal_data,
IntPtr connect_object, int after, IntPtr user_data);
[DllImport("libglade-2.0-0.dll")]
static extern void glade_xml_signal_autoconnect_full (IntPtr raw, RawXMLConnectFunc func,
IntPtr user_data);
public void Autoconnect () {
RawXMLConnectFunc cf = new RawXMLConnectFunc (ConnectFunc);
glade_xml_signal_autoconnect_full (gxml.Handle, cf, IntPtr.Zero);
}
void ConnectFunc (string handler_name, IntPtr objekt_ptr,
string signal_name, string signal_data,
IntPtr connect_object_ptr, int after, IntPtr user_data) {
GLib.Object objekt = GLib.Object.GetObject (objekt_ptr, false);
/* if an connect_object_ptr is provided, use that as handler */
object connect_object =
connect_object_ptr == IntPtr.Zero
? handler_object
: GLib.Object.GetObject (connect_object_ptr, false);
/* search for the event to connect */
System.Reflection.MemberInfo[] evnts = objekt.GetType ().
FindMembers (System.Reflection.MemberTypes.Event,
System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.Static
| System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.NonPublic,
signalFilter, signal_name);
foreach (System.Reflection.EventInfo ei in evnts)
{
bool connected = false;
System.Reflection.MethodInfo add = ei.GetAddMethod ();
System.Reflection.ParameterInfo[] addpi = add.GetParameters ();
if (addpi.Length == 1)
{ /* this should be always true, unless there's something broken */
Type delegate_type = addpi[0].ParameterType;
/* look for an instance method */
if (connect_object != null) try
{
Delegate d = Delegate.CreateDelegate
(delegate_type, connect_object, handler_name);
add.Invoke (objekt, new object[] { d } );
connected = true;
}
catch (ArgumentException)
{
/* ignore if there is not such instance method */
}
/* look for a static method if no instance method has been found */
if (!connected && handler_type != null) try
{
Delegate d = Delegate.CreateDelegate
(delegate_type, handler_type, handler_name);
add.Invoke (objekt, new object[] { d } );
connected = true;
}
catch (ArgumentException)
{
/* ignore if there is not such static method */
}
if (!connected)
{
throw new HandlerNotFoundException (handler_name, signal_name, ei, delegate_type);
}
}
}
}
System.Reflection.MemberFilter signalFilter = new System.Reflection.MemberFilter (SignalFilter);
/* matches events to GLib signal names */
static bool SignalFilter (System.Reflection.MemberInfo m, object filterCriteria)
{
string signame = (filterCriteria as string);
object[] attrs = m.GetCustomAttributes (typeof (GLib.SignalAttribute), true);
if (attrs.Length > 0)
{
foreach (GLib.SignalAttribute a in attrs)
{
if (signame == a.CName)
{
return true;
}
}
return false;
}
else
{
/* this tries to match the names when no attibutes are present.
It is only a fallback. */
signame = signame.ToLower ().Replace ("_", "");
string evname = m.Name.ToLower ();
return signame == evname;
}
}
}
private void BindFields (object target, Type type)
{
System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;
if (target != null)
flags |= System.Reflection.BindingFlags.Instance;
else
flags |= System.Reflection.BindingFlags.Static;
System.Reflection.FieldInfo[] fields = type.GetFields (flags);
if (fields == null)
return;
foreach (System.Reflection.FieldInfo field in fields)
{
object[] attrs = field.GetCustomAttributes (typeof (WidgetAttribute), true);
if (attrs == null || attrs.Length == 0)
continue;
// The widget to field binding must be 1:1, so only check
// the first attribute.
WidgetAttribute attr = (WidgetAttribute) attrs[0];
Gtk.Widget widget;
if (attr.Specified)
widget = GetWidget (attr.Name);
else
widget = GetWidget (field.Name);
if (widget != null)
field.SetValue (target, widget, flags, null, null);
}
}
public void BindFields (object target)
{
BindFields (target, target.GetType ());
}
public void BindFields (Type type)
{
BindFields (null, type);
}
public static Glade.XML FromStream (System.IO.Stream stream, string root, string domain)
{
return new Glade.XML (stream, root, domain);
}
public static Glade.XML FromAssembly (
System.Reflection.Assembly assembly, string resource_name, string root, string domain)
{
return new Glade.XML (assembly, resource_name, root, domain);
}
public static Glade.XML FromAssembly (string resource_name, string root, string domain)
{
return new Glade.XML (
System.Reflection.Assembly.GetCallingAssembly (), resource_name, root, domain);
}
[DllImport("libglade-2.0-0.dll")]
static extern IntPtr glade_xml_get_widget_prefix(IntPtr raw, string name);
public Gtk.Widget[] GetWidgetPrefix(string name)
{
IntPtr raw_ret = glade_xml_get_widget_prefix(Handle, name);
if (raw_ret == IntPtr.Zero)
return new Gtk.Widget [0];
GLib.List list = new GLib.List (raw_ret);
Gtk.Widget[] result = new Gtk.Widget [list.Count];
int i = 0;
foreach (Gtk.Widget w in list)
result [i++] = w;
return result;
}
|