From 0d5dd661173af1794164a7dae5369e96c8a027a9 Mon Sep 17 00:00:00 2001 From: Alexander Corrado Date: Fri, 22 Jul 2011 03:04:03 -0400 Subject: [PATCH] qt: add handwritten QString because generated version had problems --- src/qt/Makefile.am | 19 ++++++------- src/qt/QString.cs | 66 ++++++++++++++++++++++++++++++++++++++++++++++ src/qt/hello.cs | 22 ++-------------- 3 files changed, 78 insertions(+), 29 deletions(-) create mode 100644 src/qt/QString.cs diff --git a/src/qt/Makefile.am b/src/qt/Makefile.am index 89698455..ef615946 100644 --- a/src/qt/Makefile.am +++ b/src/qt/Makefile.am @@ -1,28 +1,29 @@ top_srcdir = ../.. INTEROP_DLL = \ - $(top_srcdir)/src/Mono.VisualC.Interop/bin/Debug/Mono.VisualC.Interop.dll + $(top_srcdir)/bin/Debug/Mono.Cxxi.dll all: hello.exe qt-gui.xml: qt-gui.h - gccxml -fxml=$@ --gccxml-cxxflags "`pkg-config --cflags QtCore QtGui`" qt-gui.h + $(GCCXML) -fxml=$@ --gccxml-cxxflags "`pkg-config --cflags QtCore QtGui`" qt-gui.h generated: qt-gui.xml $(RM) -r generated - mono --debug $(top_srcdir)/src/generator/bin/Debug/generator.exe -o=generated -ns=Qt.Gui -lib=QtGui --filters=qt-gui-filters.txt qt-gui.xml + $(MONO) --debug $(top_srcdir)/bin/Debug/generator.exe -o=generated -ns=Qt.Gui -lib=QtGui --filters=qt-gui-filters.txt qt-gui.xml -libQtGui-inline.so: qt-gui.cpp - g++ `pkg-config --cflags QtCore QtGui` --shared -fPIC -o $@ -fkeep-inline-functions qt-gui.cpp `pkg-config --libs QtCore QtGui` +#libQtGui-inline.so: qt-gui.cpp +# $(CXX) `pkg-config --cflags QtCore QtGui` --shared -fPIC -o $@ -fkeep-inline-functions qt-gui.cpp `pkg-config --libs QtCore QtGui` -Qt.Gui-binding.dll: generated - $(GMCS) -out:$@ -target:library -unsafe -r:$(INTEROP_DLL) generated/*.cs +Qt.Gui-binding.dll: generated QString.cs + rm generated/QString.cs + $(GMCS) -out:$@ -target:library -unsafe -r:$(INTEROP_DLL) generated/*.cs QString.cs -hello.exe: Qt.Gui-binding.dll hello.cs libQtGui-inline.so +hello.exe: Qt.Gui-binding.dll hello.cs #libQtGui-inline.so $(GMCS) -out:$@ -target:exe -r:$(INTEROP_DLL) -r:Qt.Gui-binding.dll hello.cs clean: $(RM) -rf generated hello.exe qt-gui.xml Qt.Gui-binding.dll libQtGui-inline.so run: hello.exe - MONO_PATH=$(top_srcdir)/src/Mono.VisualC.Interop/bin/Debug mono hello.exe + MONO_PATH=$(top_srcdir)/bin/Debug $(MONO) hello.exe diff --git a/src/qt/QString.cs b/src/qt/QString.cs new file mode 100644 index 00000000..260e0ac3 --- /dev/null +++ b/src/qt/QString.cs @@ -0,0 +1,66 @@ +using System; +using System.Text; +using System.Runtime.InteropServices; + +using Mono.Cxxi; + +namespace Qt.Gui { + //TODO: Will this leak? + [StructLayout (LayoutKind.Sequential)] + public unsafe struct QString { + #region Sync with qstring.h + public interface IQString : ICppClass { + [Constructor] void QString(ref QString @this, [MangleAs ("const QChar*")] IntPtr unicode, int size); + } + + [StructLayout (LayoutKind.Sequential)] + public struct Data { + public int @ref; + public int alloc, size; + public IntPtr data; + public ushort clean; + public ushort simpletext; + public ushort righttoleft; + public ushort asciiCache; + public ushort capacity; + public ushort reserved; + public IntPtr array; + } + + + public Data* d; + #endregion + + private static IQString impl = Libs.QtGui.GetClass ("QString"); + + public QString (string str) : this () + { + var strPtr = Marshal.StringToHGlobalUni (str); + impl.QString (ref this, strPtr, str.Length); + Marshal.FreeHGlobal (strPtr); + + // TODO: I deref this on construction to let Qt free it when it's done with it. + // My assumption is that this struct will only be used to interop with Qt and + // no managed class is going to hold on to it. + this.DeRef (); + } + + public static implicit operator QString (string str) + { + return new QString (str); + } + + public QString AddRef () + { + d->@ref++; + return this; + } + + public QString DeRef () + { + d->@ref--; + return this; + } + } +} + diff --git a/src/qt/hello.cs b/src/qt/hello.cs index 655ea573..00a63e2d 100644 --- a/src/qt/hello.cs +++ b/src/qt/hello.cs @@ -1,7 +1,7 @@ using System; using Qt.Gui; -using Mono.VisualC.Interop; +using Mono.Cxxi; namespace QtTest { class MainClass { @@ -9,29 +9,11 @@ namespace QtTest { { int argc = args.Length; using (QApplication app = new QApplication (ref argc, args, 0x040602)) { - QPushButton hello = new QPushButton (QString.FromUtf8 ("Hello", 5), null); + QPushButton hello = new QPushButton ("Hello", null); hello.Resize (100, 30); hello.Show (); - CppLibrary.SaveInteropAssembly (); QApplication.Exec (); } - /* - using (QPushButton hello = new QPushButton ("Hello world!"), - hello2 = new QPushButton ("Another button")) { - - hello.Resize (100, 30); - hello2.Resize (200, 30); - - //CppLibrary.SaveInteropAssembly (); - hello.Visible = true; - hello2.Visible = true; - - app.Exec (); - - - } - } - */ } } }