Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
2009-11-30 Mike Kestner <[email protected]>
Browse files Browse the repository at this point in the history
	* generator/Method.cs: support win32_utf8_variant attribute on methods.
	* glib/*.cs: support win32 utf8 variant methods.
	* gtk/*.custom: support win32 utf8 variant methods.
	* gtk/Gtk.metadata: mark some win32_utf8_variant methods.
	  [Fixes #550961] Adapted from a patch by Tor Lillqvist.

svn path=/trunk/gtk-sharp/; revision=147113
  • Loading branch information
mkestner committed Nov 30, 2009
1 parent f1bf740 commit 536c3ac
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 21 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2009-11-30 Mike Kestner <[email protected]>

* generator/Method.cs: support win32_utf8_variant attribute on methods.
* glib/*.cs: support win32 utf8 variant methods.
* gtk/*.custom: support win32 utf8 variant methods.
* gtk/Gtk.metadata: mark some win32_utf8_variant methods.
[Fixes #550961] Adapted from a patch by Tor Lillqvist.

2009-11-28 Mike Kestner <[email protected]>

* glib/GException.cs: add Code and Domain props to expose the GError
Expand Down
50 changes: 44 additions & 6 deletions generator/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Method : MethodBase {
private string call;
private bool is_get, is_set;
private bool deprecated = false;
private bool win32_utf8_variant = false;

public Method (XmlElement elem, ClassBase container_type) : base (elem, container_type)
{
Expand All @@ -44,10 +45,19 @@ public Method (XmlElement elem, ClassBase container_type) : base (elem, containe
deprecated = attr == "1" || attr == "true";
}

if (elem.HasAttribute ("win32_utf8_variant")) {
string attr = elem.GetAttribute ("win32_utf8_variant");
win32_utf8_variant = attr == "1" || attr.ToLower () == "true";
}

if (Name == "GetType")
Name = "GetGType";
}

public bool HasWin32Utf8Variant {
get { return win32_utf8_variant; }
}

public bool IsDeprecated {
get {
return deprecated;
Expand Down Expand Up @@ -191,6 +201,15 @@ public void GenerateImport (StreamWriter sw)
else
sw.WriteLine("\t\tstatic extern " + Safety + retval.MarshalType + " " + CName + "(" + import_sig + ");");
sw.WriteLine();

if (HasWin32Utf8Variant) {
sw.WriteLine("\t\t[DllImport(\"" + LibraryName + "\")]");
if (retval.MarshalType.StartsWith ("[return:"))
sw.WriteLine("\t\t" + retval.MarshalType + " static extern " + Safety + retval.CSType + " " + CName + "_utf8(" + import_sig + ");");
else
sw.WriteLine("\t\tstatic extern " + Safety + retval.MarshalType + " " + CName + "_utf8(" + import_sig + ");");
sw.WriteLine();
}
}

public void Generate (GenerationInfo gen_info, ClassBase implementor)
Expand Down Expand Up @@ -271,12 +290,31 @@ public void GenerateBody (GenerationInfo gen_info, ClassBase implementor, string
Body.InitAccessor (sw, Signature, indent);
Body.Initialize(gen_info, is_get, is_set, indent);

sw.Write(indent + "\t\t\t");
if (retval.IsVoid)
sw.WriteLine(CName + call + ";");
else {
sw.WriteLine(retval.MarshalType + " raw_ret = " + CName + call + ";");
sw.WriteLine(indent + "\t\t\t" + retval.CSType + " ret = " + retval.FromNative ("raw_ret") + ";");
if (HasWin32Utf8Variant) {
if (!retval.IsVoid)
sw.WriteLine(indent + "\t\t\t" + retval.MarshalType + " raw_ret;");
sw.WriteLine(indent + "\t\t\t" + "if (Environment.OSVersion.Platform == PlatformID.Win32NT ||");
sw.WriteLine(indent + "\t\t\t" + " Environment.OSVersion.Platform == PlatformID.Win32S ||");
sw.WriteLine(indent + "\t\t\t" + " Environment.OSVersion.Platform == PlatformID.Win32Windows ||");
sw.WriteLine(indent + "\t\t\t" + " Environment.OSVersion.Platform == PlatformID.WinCE)");
if (retval.IsVoid) {
sw.WriteLine(indent + "\t\t\t\t" + CName + "_utf8" + call + ";");
sw.WriteLine(indent + "\t\t\t" + "else");
sw.WriteLine(indent + "\t\t\t\t" + CName + call + ";");
} else {
sw.WriteLine(indent + "\t\t\t\traw_ret = " + CName + "_utf8" + call + ";");
sw.WriteLine(indent + "\t\t\t" + "else");
sw.WriteLine(indent + "\t\t\t\traw_ret = " + CName + call + ";");
sw.WriteLine(indent + "\t\t\t" + retval.CSType + " ret = " + retval.FromNative ("raw_ret") + ";");
}
} else {
sw.Write(indent + "\t\t\t");
if (retval.IsVoid)
sw.WriteLine(CName + call + ";");
else {
sw.WriteLine(retval.MarshalType + " raw_ret = " + CName + call + ";");
sw.WriteLine(indent + "\t\t\t" + retval.CSType + " ret = " + retval.FromNative ("raw_ret") + ";");
}
}

if (!IsStatic && implementor != null)
Expand Down
12 changes: 10 additions & 2 deletions glib/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ public class FileUtils
[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
extern static bool g_file_get_contents (IntPtr filename, out IntPtr contents, out int length, out IntPtr error);

[DllImport("libglib-2.0-0.dll")]
extern static bool g_file_get_contents_utf8 (IntPtr filename, out IntPtr contents, out int length, out IntPtr error);

public static string GetFileContents (string filename)
{
int length;
IntPtr contents, error;
IntPtr native_filename = Marshaller.StringToPtrGStrdup (filename);

if (!g_file_get_contents (native_filename, out contents, out length, out error))
throw new GException (error);
if (Global.IsWindowsPlatform) {
if (!g_file_get_contents_utf8 (native_filename, out contents, out length, out error))
throw new GException (error);
} else {
if (!g_file_get_contents (native_filename, out contents, out length, out error))
throw new GException (error);
}

Marshaller.Free (native_filename);
return Marshaller.Utf8PtrToString (contents);
Expand Down
14 changes: 14 additions & 0 deletions glib/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ public class Global
//this is a static class
private Global () {}

internal static bool IsWindowsPlatform {
get {
switch (Environment.OSVersion.Platform) {
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.WinCE:
return true;
default:
return false;
}
}
}

public static string ProgramName {
get {
return GLib.Marshaller.PtrToStringGFree(g_get_prgname());
Expand Down
9 changes: 8 additions & 1 deletion glib/IOChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ public IOChannel (string filename, string mode)
IntPtr native_filename = Marshaller.StringToPtrGStrdup (filename);
IntPtr native_mode = Marshaller.StringToPtrGStrdup (mode);
IntPtr error;
handle = g_io_channel_new_file(native_filename, native_mode, out error);

if (Global.IsWindowsPlatform)
handle = g_io_channel_new_file_utf8(native_filename, native_mode, out error);
else
handle = g_io_channel_new_file(native_filename, native_mode, out error);
Marshaller.Free (native_filename);
Marshaller.Free (native_mode);
if (error != IntPtr.Zero) throw new GException (error);
Expand Down Expand Up @@ -325,6 +329,9 @@ public static IOChannelError ErrorFromErrno (int en)
[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr g_io_channel_new_file (IntPtr filename, IntPtr mode, out IntPtr error);

[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr g_io_channel_new_file_utf8 (IntPtr filename, IntPtr mode, out IntPtr error);

[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int g_io_channel_error_quark ();

Expand Down
23 changes: 21 additions & 2 deletions glib/Marshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,21 @@ public static void Free (IntPtr[] ptrs)
[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr g_filename_to_utf8 (IntPtr mem, int len, IntPtr read, out IntPtr written, out IntPtr error);

[DllImport("libglib-2.0-0.dll")]
static extern IntPtr g_filename_to_utf8_utf8 (IntPtr mem, int len, IntPtr read, out IntPtr written, out IntPtr error);

public static string FilenamePtrToString (IntPtr ptr)
{
if (ptr == IntPtr.Zero) return null;

IntPtr dummy, error;
IntPtr utf8 = g_filename_to_utf8 (ptr, -1, IntPtr.Zero, out dummy, out error);
IntPtr utf8;

if (Global.IsWindowsPlatform)
utf8 = g_filename_to_utf8_utf8 (ptr, -1, IntPtr.Zero, out dummy, out error);
else
utf8 = g_filename_to_utf8 (ptr, -1, IntPtr.Zero, out dummy, out error);

if (error != IntPtr.Zero)
throw new GLib.GException (error);
return Utf8PtrToString (utf8);
Expand Down Expand Up @@ -117,14 +126,24 @@ public static string[] PtrToStringGFree (IntPtr[] ptrs) {
[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr g_filename_from_utf8 (IntPtr mem, int len, IntPtr read, out IntPtr written, out IntPtr error);

[DllImport("libglib-2.0-0.dll")]
static extern IntPtr g_filename_from_utf8_utf8 (IntPtr mem, int len, IntPtr read, out IntPtr written, out IntPtr error);

public static IntPtr StringToFilenamePtr (string str)
{
if (str == null)
return IntPtr.Zero;

IntPtr dummy, error;
IntPtr utf8 = StringToPtrGStrdup (str);
IntPtr result = g_filename_from_utf8 (utf8, -1, IntPtr.Zero, out dummy, out error);

IntPtr result;

if (Global.IsWindowsPlatform)
result = g_filename_from_utf8_utf8 (utf8, -1, IntPtr.Zero, out dummy, out error);
else
result = g_filename_from_utf8 (utf8, -1, IntPtr.Zero, out dummy, out error);

g_free (utf8);
if (error != IntPtr.Zero)
throw new GException (error);
Expand Down
55 changes: 50 additions & 5 deletions glib/Spawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public void Close ()
[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool g_spawn_async (IntPtr dir, IntPtr[] argv, IntPtr[] envp, int flags, SpawnChildSetupFuncNative func, IntPtr data, out int pid, out IntPtr error);

[DllImport ("libglib-2.0-0.dll")]
static extern bool g_spawn_async_utf8 (IntPtr dir, IntPtr[] argv, IntPtr[] envp, int flags, SpawnChildSetupFuncNative func, IntPtr data, out int pid, out IntPtr error);

public static bool SpawnAsync (string working_directory, string[] argv, string[] envp, SpawnFlags flags, SpawnChildSetupFunc child_setup, out Process child_process)
{
int pid;
Expand All @@ -121,7 +124,13 @@ public static bool SpawnAsync (string working_directory, string[] argv, string[]
IntPtr[] native_argv = Marshaller.StringArrayToNullTermPointer (argv);
IntPtr[] native_envp = Marshaller.StringArrayToNullTermPointer (envp);
SpawnChildSetupWrapper wrapper = new SpawnChildSetupWrapper (child_setup);
bool result = g_spawn_async (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out pid, out error);
bool result;

if (Global.IsWindowsPlatform)
result = g_spawn_async_utf8 (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out pid, out error);
else
result = g_spawn_async (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out pid, out error);

child_process = new Process (pid);
Marshaller.Free (native_dir);
Marshaller.Free (native_argv);
Expand All @@ -133,6 +142,9 @@ public static bool SpawnAsync (string working_directory, string[] argv, string[]
[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool g_spawn_async_with_pipes (IntPtr dir, IntPtr[] argv, IntPtr[] envp, int flags, SpawnChildSetupFuncNative func, IntPtr data, out int pid, IntPtr stdin, IntPtr stdout, IntPtr stderr, out IntPtr error);

[DllImport ("libglib-2.0-0.dll")]
static extern bool g_spawn_async_with_pipes_utf8 (IntPtr dir, IntPtr[] argv, IntPtr[] envp, int flags, SpawnChildSetupFuncNative func, IntPtr data, out int pid, IntPtr stdin, IntPtr stdout, IntPtr stderr, out IntPtr error);

public static bool SpawnAsyncWithPipes (string working_directory, string[] argv, string[] envp, SpawnFlags flags, SpawnChildSetupFunc child_setup, out Process child_process, ref int stdin, ref int stdout, ref int stderr)
{
int pid;
Expand All @@ -144,7 +156,13 @@ public static bool SpawnAsyncWithPipes (string working_directory, string[] argv,
IntPtr in_ptr = stdin == IgnorePipe ? IntPtr.Zero : Marshal.AllocHGlobal (4);
IntPtr out_ptr = stdout == IgnorePipe ? IntPtr.Zero : Marshal.AllocHGlobal (4);
IntPtr err_ptr = stderr == IgnorePipe ? IntPtr.Zero : Marshal.AllocHGlobal (4);
bool result = g_spawn_async_with_pipes (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out pid, in_ptr, out_ptr, err_ptr, out error);
bool result;

if (Global.IsWindowsPlatform)
result = g_spawn_async_with_pipes_utf8 (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out pid, in_ptr, out_ptr, err_ptr, out error);
else
result = g_spawn_async_with_pipes (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out pid, in_ptr, out_ptr, err_ptr, out error);

child_process = new Process (pid);
if (in_ptr != IntPtr.Zero) {
stdin = Marshal.ReadInt32 (in_ptr);
Expand All @@ -168,14 +186,23 @@ public static bool SpawnAsyncWithPipes (string working_directory, string[] argv,
[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool g_spawn_sync (IntPtr dir, IntPtr[] argv, IntPtr[] envp, int flags, SpawnChildSetupFuncNative func, IntPtr data, out IntPtr stdout, out IntPtr stderr, out int exit_status, out IntPtr error);

[DllImport ("libglib-2.0-0.dll")]
static extern bool g_spawn_sync_utf8 (IntPtr dir, IntPtr[] argv, IntPtr[] envp, int flags, SpawnChildSetupFuncNative func, IntPtr data, out IntPtr stdout, out IntPtr stderr, out int exit_status, out IntPtr error);

public static bool SpawnSync (string working_directory, string[] argv, string[] envp, SpawnFlags flags, SpawnChildSetupFunc child_setup, out string stdout, out string stderr, out int exit_status)
{
IntPtr native_stdout, native_stderr, error;
IntPtr native_dir = Marshaller.StringToPtrGStrdup (working_directory);
IntPtr[] native_argv = Marshaller.StringArrayToNullTermPointer (argv);
IntPtr[] native_envp = Marshaller.StringArrayToNullTermPointer (envp);
SpawnChildSetupWrapper wrapper = new SpawnChildSetupWrapper (child_setup);
bool result = g_spawn_sync (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out native_stdout, out native_stderr, out exit_status, out error);
bool result;

if (Global.IsWindowsPlatform)
result = g_spawn_sync (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out native_stdout, out native_stderr, out exit_status, out error);
else
result = g_spawn_sync (native_dir, native_argv, native_envp, (int) flags, wrapper.NativeCallback, wrapper.Data, out native_stdout, out native_stderr, out exit_status, out error);

Marshaller.Free (native_dir);
Marshaller.Free (native_argv);
Marshaller.Free (native_envp);
Expand All @@ -188,11 +215,20 @@ public static bool SpawnSync (string working_directory, string[] argv, string[]
[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool g_spawn_command_line_async (IntPtr cmdline, out IntPtr error);

[DllImport ("libglib-2.0-0.dll")]
static extern bool g_spawn_command_line_async_utf8 (IntPtr cmdline, out IntPtr error);

public static bool SpawnCommandLineAsync (string command_line)
{
IntPtr error;
IntPtr native_cmd = Marshaller.StringToPtrGStrdup (command_line);
bool result = g_spawn_command_line_async (native_cmd, out error);
bool result;

if (Global.IsWindowsPlatform)
result = g_spawn_command_line_async_utf8 (native_cmd, out error);
else
result = g_spawn_command_line_async (native_cmd, out error);

Marshaller.Free (native_cmd);
if (error != IntPtr.Zero) throw new GLib.GException (error);
return result;
Expand All @@ -201,11 +237,20 @@ public static bool SpawnCommandLineAsync (string command_line)
[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool g_spawn_command_line_sync (IntPtr cmdline, out IntPtr stdout, out IntPtr stderr, out int exit_status, out IntPtr error);

[DllImport ("libglib-2.0-0.dll")]
static extern bool g_spawn_command_line_sync_utf8 (IntPtr cmdline, out IntPtr stdout, out IntPtr stderr, out int exit_status, out IntPtr error);

public static bool SpawnCommandLineSync (string command_line, out string stdout, out string stderr, out int exit_status)
{
IntPtr error, native_stdout, native_stderr;
IntPtr native_cmd = Marshaller.StringToPtrGStrdup (command_line);
bool result = g_spawn_command_line_sync (native_cmd, out native_stdout, out native_stderr, out exit_status, out error);
bool result;

if (Global.IsWindowsPlatform)
result = g_spawn_command_line_sync_utf8 (native_cmd, out native_stdout, out native_stderr, out exit_status, out error);
else
result = g_spawn_command_line_sync (native_cmd, out native_stdout, out native_stderr, out exit_status, out error);

Marshaller.Free (native_cmd);
stdout = Marshaller.PtrToStringGFree (native_stdout);
stderr = Marshaller.PtrToStringGFree (native_stderr);
Expand Down
21 changes: 18 additions & 3 deletions gtk/FileSelection.custom
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,30 @@ public class FSButton : Gtk.Button {
}
}

[DllImport ("libgtk-win32-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
[DllImport("libgtk-win32-2.0-0.dll")]
static extern IntPtr gtk_file_selection_get_selections (IntPtr handle);

[DllImport ("libglib-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
[DllImport("libgtk-win32-2.0-0.dll")]
static extern IntPtr gtk_file_selection_get_selections_utf8 (IntPtr handle);

[DllImport("libglib-2.0-0.dll")]
static extern void g_strfreev (IntPtr handle);

public string[] Selections {
get {
IntPtr strv = gtk_file_selection_get_selections (Handle);
IntPtr strv;

switch (Environment.OSVersion.Platform) {
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.WinCE:
strv = gtk_file_selection_get_selections_utf8 (Handle);
break;
default:
strv = gtk_file_selection_get_selections (Handle);
break;
}

System.Collections.ArrayList result = new System.Collections.ArrayList ();

Expand Down
Loading

0 comments on commit 536c3ac

Please sign in to comment.