Merge pull request #346 from spouliot/gh345

[glib] Fix `g_filename_[from|to]_utf8` signatures for 64 bits platforms
This commit is contained in:
lytico 2022-05-02 14:50:12 +02:00 committed by GitHub
commit c2e4f61033
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 6 deletions

View file

@ -48,15 +48,14 @@ namespace GLib {
g_free (ptrs [i]); g_free (ptrs [i]);
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate IntPtr d_g_filename_to_utf8(IntPtr mem, int len, IntPtr read, out IntPtr written, out IntPtr error); delegate IntPtr d_g_filename_to_utf8(IntPtr mem, IntPtr len, IntPtr read, out IntPtr written, out IntPtr error);
static d_g_filename_to_utf8 g_filename_to_utf8 = FuncLoader.LoadFunction<d_g_filename_to_utf8>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_filename_to_utf8")); static d_g_filename_to_utf8 g_filename_to_utf8 = FuncLoader.LoadFunction<d_g_filename_to_utf8>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_filename_to_utf8"));
public static string FilenamePtrToString (IntPtr ptr) public static string FilenamePtrToString (IntPtr ptr)
{ {
if (ptr == IntPtr.Zero) return null; if (ptr == IntPtr.Zero) return null;
IntPtr dummy, error; IntPtr utf8 = g_filename_to_utf8 (ptr, (IntPtr)(-1), IntPtr.Zero, out _, out var error);
IntPtr utf8 = g_filename_to_utf8 (ptr, -1, IntPtr.Zero, out dummy, out error);
if (error != IntPtr.Zero) if (error != IntPtr.Zero)
throw new GLib.GException (error); throw new GLib.GException (error);
@ -117,7 +116,7 @@ namespace GLib {
return ret; return ret;
} }
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate IntPtr d_g_filename_from_utf8(IntPtr mem, int len, IntPtr read, out IntPtr written, out IntPtr error); delegate IntPtr d_g_filename_from_utf8(IntPtr mem, IntPtr len, IntPtr read, out IntPtr written, out IntPtr error);
static d_g_filename_from_utf8 g_filename_from_utf8 = FuncLoader.LoadFunction<d_g_filename_from_utf8>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_filename_from_utf8")); static d_g_filename_from_utf8 g_filename_from_utf8 = FuncLoader.LoadFunction<d_g_filename_from_utf8>(FuncLoader.GetProcAddress(GLibrary.Load(Library.GLib), "g_filename_from_utf8"));
public static IntPtr StringToFilenamePtr (string str) public static IntPtr StringToFilenamePtr (string str)
@ -125,9 +124,8 @@ namespace GLib {
if (str == null) if (str == null)
return IntPtr.Zero; return IntPtr.Zero;
IntPtr dummy, error;
IntPtr utf8 = StringToPtrGStrdup (str); IntPtr utf8 = StringToPtrGStrdup (str);
IntPtr result = g_filename_from_utf8 (utf8, -1, IntPtr.Zero, out dummy, out error); IntPtr result = g_filename_from_utf8 (utf8, (IntPtr)(-1), IntPtr.Zero, out _, out var error);
g_free (utf8); g_free (utf8);
if (error != IntPtr.Zero) if (error != IntPtr.Zero)

View file

@ -0,0 +1,32 @@
using Gtk;
namespace Samples
{
[Section(ContentType = typeof(FileChooserDialog), Category = Category.Dialogs)]
class FileChooserDialogSection : ListSection
{
public FileChooserDialogSection ()
{
AddItem ($"Press button to open {nameof(FileChooserDialog)} :", new FileChooserDialogDemo ("Press me"));
}
}
class FileChooserDialogDemo : Button
{
public FileChooserDialogDemo (string text) : base (text) { }
protected override void OnPressed ()
{
var fcd = new FileChooserDialog ("Open File", null, FileChooserAction.Open);
fcd.AddButton (Stock.Cancel, ResponseType.Cancel);
fcd.AddButton (Stock.Open, ResponseType.Ok);
fcd.DefaultResponse = ResponseType.Ok;
fcd.SelectMultiple = false;
ResponseType response = (ResponseType) fcd.Run ();
if (response == ResponseType.Ok)
ApplicationOutput.WriteLine (fcd.Filename);
fcd.Destroy ();
}
}
}