2002-10-19 09:31:20 +00:00
|
|
|
namespace GConf
|
|
|
|
{
|
|
|
|
using System;
|
2003-08-26 21:26:30 +00:00
|
|
|
using System.Collections;
|
2002-10-19 09:31:20 +00:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
internal enum ValueType
|
|
|
|
{
|
|
|
|
Invalid,
|
|
|
|
String,
|
|
|
|
Int,
|
|
|
|
Float,
|
|
|
|
Bool,
|
|
|
|
Schema,
|
|
|
|
List,
|
|
|
|
Pair
|
|
|
|
};
|
|
|
|
|
|
|
|
internal struct NativeValue
|
|
|
|
{
|
|
|
|
public ValueType type;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class InvalidValueTypeException : Exception
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class Value : IDisposable
|
|
|
|
{
|
|
|
|
IntPtr Raw = IntPtr.Zero;
|
|
|
|
ValueType val_type = ValueType.Invalid;
|
|
|
|
bool managed = true;
|
|
|
|
|
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern void gconf_value_set_string (IntPtr value, string data);
|
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern void gconf_value_set_int (IntPtr value, int data);
|
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern void gconf_value_set_float (IntPtr value, double data);
|
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern void gconf_value_set_bool (IntPtr value, bool data);
|
|
|
|
|
|
|
|
ValueType LookupType (object data)
|
|
|
|
{
|
|
|
|
if (data is string) {
|
|
|
|
return ValueType.String;
|
|
|
|
} else if (data is int) {
|
|
|
|
return ValueType.Int;
|
|
|
|
} else if (data is double) {
|
|
|
|
return ValueType.Float;
|
|
|
|
} else if (data is bool) {
|
|
|
|
return ValueType.Bool;
|
2003-08-26 21:26:30 +00:00
|
|
|
} else if (data is ICollection) {
|
|
|
|
return ValueType.List;
|
2002-10-19 09:31:20 +00:00
|
|
|
} else {
|
|
|
|
return ValueType.Invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Set (object data)
|
|
|
|
{
|
|
|
|
if (data == null)
|
|
|
|
throw new NullReferenceException ();
|
|
|
|
|
|
|
|
ValueType type = LookupType (data);
|
|
|
|
Set (data, type);
|
|
|
|
}
|
|
|
|
|
2003-08-26 21:26:30 +00:00
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern IntPtr gconf_value_set_list_nocopy (IntPtr value, IntPtr list);
|
|
|
|
|
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern IntPtr gconf_value_set_list_type (IntPtr value, ValueType vtype);
|
|
|
|
|
2002-10-19 09:31:20 +00:00
|
|
|
void Set (object data, ValueType type)
|
|
|
|
{
|
|
|
|
if (data == null)
|
|
|
|
throw new NullReferenceException ();
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ValueType.String:
|
|
|
|
gconf_value_set_string (Raw, (string) data);
|
|
|
|
break;
|
|
|
|
case ValueType.Int:
|
|
|
|
gconf_value_set_int (Raw, (int) data);
|
|
|
|
break;
|
|
|
|
case ValueType.Float:
|
|
|
|
gconf_value_set_float (Raw, (double) data);
|
|
|
|
break;
|
|
|
|
case ValueType.Bool:
|
|
|
|
gconf_value_set_bool (Raw, (bool) data);
|
|
|
|
break;
|
2003-08-26 21:26:30 +00:00
|
|
|
case ValueType.List:
|
|
|
|
ValueType listType;
|
|
|
|
GLib.SList list = GetListFromCollection ((ICollection) data, out listType);
|
|
|
|
gconf_value_set_list_type (Raw, listType);
|
|
|
|
gconf_value_set_list_nocopy (Raw, list.Handle);
|
|
|
|
break;
|
2002-10-19 09:31:20 +00:00
|
|
|
default:
|
|
|
|
throw new InvalidValueTypeException ();
|
|
|
|
}
|
|
|
|
}
|
2003-08-26 21:26:30 +00:00
|
|
|
|
|
|
|
GLib.SList GetListFromCollection (ICollection data, out ValueType listType)
|
|
|
|
{
|
|
|
|
object [] arr = (object []) Array.CreateInstance (typeof (object), data.Count);
|
|
|
|
data.CopyTo (arr, 0);
|
|
|
|
|
|
|
|
listType = ValueType.Invalid;
|
|
|
|
GLib.SList list = new GLib.SList (IntPtr.Zero);
|
|
|
|
GC.SuppressFinalize (list);
|
|
|
|
|
|
|
|
foreach (object o in arr) {
|
|
|
|
ValueType type = LookupType (o);
|
|
|
|
if (listType == ValueType.Invalid)
|
|
|
|
listType = type;
|
|
|
|
|
|
|
|
if (listType == ValueType.Invalid || type != listType)
|
|
|
|
throw new InvalidValueTypeException ();
|
|
|
|
|
|
|
|
Value v = new Value (o);
|
|
|
|
GC.SuppressFinalize (v);
|
|
|
|
list.Append (v.Raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2002-10-19 09:31:20 +00:00
|
|
|
|
|
|
|
[DllImport("gconf-2")]
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
static extern IntPtr gconf_value_get_string (IntPtr value);
|
2002-10-19 09:31:20 +00:00
|
|
|
|
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern int gconf_value_get_int (IntPtr value);
|
|
|
|
|
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern double gconf_value_get_float (IntPtr value);
|
|
|
|
|
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern bool gconf_value_get_bool (IntPtr value);
|
|
|
|
|
2003-08-26 21:26:30 +00:00
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern IntPtr gconf_value_get_list (IntPtr value);
|
|
|
|
|
2002-10-19 09:31:20 +00:00
|
|
|
public object Get ()
|
|
|
|
{
|
|
|
|
switch (val_type)
|
|
|
|
{
|
|
|
|
case ValueType.String:
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
return Marshal.PtrToStringAnsi (gconf_value_get_string (Raw));
|
2002-10-19 09:31:20 +00:00
|
|
|
case ValueType.Int:
|
|
|
|
return gconf_value_get_int (Raw);
|
|
|
|
case ValueType.Float:
|
|
|
|
return gconf_value_get_float (Raw);
|
|
|
|
case ValueType.Bool:
|
|
|
|
return gconf_value_get_bool (Raw);
|
2003-08-26 21:26:30 +00:00
|
|
|
case ValueType.List:
|
|
|
|
GLib.SList list = new GLib.SList (gconf_value_get_list (Raw), typeof (Value));
|
|
|
|
Array result = Array.CreateInstance (GetListType (), list.Count);
|
|
|
|
int i = 0;
|
|
|
|
foreach (Value v in list) {
|
|
|
|
((IList) result) [i] = v.Get ();
|
|
|
|
v.managed = false; // This is the trick to prevent a crash
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2002-10-19 09:31:20 +00:00
|
|
|
default:
|
|
|
|
throw new InvalidValueTypeException ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-26 21:26:30 +00:00
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern ValueType gconf_value_get_list_type (IntPtr value);
|
|
|
|
|
|
|
|
Type GetListType ()
|
|
|
|
{
|
|
|
|
ValueType vt = gconf_value_get_list_type (Raw);
|
|
|
|
switch (vt) {
|
|
|
|
case ValueType.String:
|
|
|
|
return typeof (string);
|
|
|
|
case ValueType.Int:
|
|
|
|
return typeof (int);
|
|
|
|
case ValueType.Float:
|
|
|
|
return typeof (float);
|
|
|
|
case ValueType.Bool:
|
|
|
|
return typeof (bool);
|
|
|
|
case ValueType.List:
|
|
|
|
return typeof (GLib.SList);
|
|
|
|
default:
|
|
|
|
throw new InvalidValueTypeException ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-19 09:31:20 +00:00
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern IntPtr gconf_value_new (ValueType type);
|
|
|
|
|
|
|
|
public Value (ValueType type)
|
|
|
|
{
|
|
|
|
Raw = gconf_value_new (type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Initialize (object val, ValueType type)
|
|
|
|
{
|
|
|
|
Raw = gconf_value_new (type);
|
|
|
|
val_type = type;
|
|
|
|
Set (val, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Value (IntPtr raw)
|
|
|
|
{
|
|
|
|
Raw = raw;
|
|
|
|
NativeValue val = (NativeValue) Marshal.PtrToStructure (raw, typeof (NativeValue));
|
|
|
|
val_type = val.type;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
public Value (string val)
|
|
|
|
{
|
|
|
|
Initialize (val, ValueType.String);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Value (int val)
|
|
|
|
{
|
|
|
|
Initialize (val, ValueType.Int);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Value (double val)
|
|
|
|
{
|
|
|
|
Initialize (val, ValueType.Float);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Value (bool val)
|
|
|
|
{
|
|
|
|
Initialize (val, ValueType.Bool);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
public Value (object val)
|
|
|
|
{
|
|
|
|
Initialize (val, LookupType (val));
|
|
|
|
}
|
|
|
|
public bool Managed
|
|
|
|
{
|
|
|
|
get { return managed; }
|
|
|
|
set { managed = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
[DllImport("gconf-2")]
|
|
|
|
static extern void gconf_value_free (IntPtr value);
|
|
|
|
|
|
|
|
~Value ()
|
|
|
|
{
|
2003-08-26 21:26:30 +00:00
|
|
|
Dispose (false);
|
2002-10-19 09:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose ()
|
2003-08-26 21:26:30 +00:00
|
|
|
{
|
|
|
|
Dispose (true);
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose (bool disposing)
|
2002-10-19 09:31:20 +00:00
|
|
|
{
|
|
|
|
if (managed && Raw != IntPtr.Zero)
|
|
|
|
{
|
|
|
|
gconf_value_free (Raw);
|
|
|
|
Raw = IntPtr.Zero;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IntPtr Handle
|
|
|
|
{
|
|
|
|
get {
|
|
|
|
return Raw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|