Ejemplo n.º 1
0
        /// <summary>Helper used to create an NSApplication subclass.</summary>
        /// <param name = "appClass">The name of the subclass, e.g. "MyApplication".</param>
        /// <param name = "nibName">The name of a nib file, e.g. "MainMenu.nib".</param>
        /// <param name = "extendDebugMenu">Used to add custom items to the debug menu. May be null.</param>
        public static NSApplication Create(string appClass, string nibName, Action<NSMenu> extendDebugMenu)
        {
            NSApplication app = new Class(appClass).Call("sharedApplication").To<NSApplication>();

            ms_mainThread = Thread.CurrentThread;

            // Load our nib. This will instantiate all of the native objects and wire them together.
            // The C# objects will be created the first time a managed method is called.
            NSMutableDictionary dict = NSMutableDictionary.Create();
            bool loaded = NSBundle.mainBundle().loadNibFile_externalNameTable_withZone(
                NSString.Create(nibName), dict, IntPtr.Zero);

            if (!loaded)
                throw new InvalidOperationException("Couldn't load " + nibName + ".");

            // We need an NSAutoreleasePool to do Native.Call, but we don't want to have one
            // hanging around while we're in the main event loop because that may hide bugs.
            // So, we'll instantiate a Native instance here and call Invoke later which can
            // be done without an NSAutoreleasePool.
            ms_run = new Native(app, new Selector("run"));

            ms_helper = AppHelper.Create();
            #if DEBUG
            app.BeginInvoke(() => DoInitDebugMenu(extendDebugMenu));
            #endif

            ms_startupPool.release();
            ms_startupPool = null;

            return app;
        }
Ejemplo n.º 2
0
        private void DoGetClassMethodsString(StringBuilder builder, bool includePrivate)
        {
            Class klass = new Class(object_getClass(m_class));

            bool needNewLine = false;
            while (klass != null && (IntPtr) klass != IntPtr.Zero)
            {
                List<List<string>> table = new List<List<string>>();
                DoAddMethods(klass, table, includePrivate);

                if (table.Count > 0)
                {
                    if (needNewLine)
                    {
                        builder.AppendLine();
                        needNewLine = false;
                    }

                    table.Sort((lhs, rhs) => lhs[0].CompareTo(rhs[0]));
                    DoAlign(table);

                    builder.Append(klass.Name);
                    builder.AppendLine(" class methods:");
                    for (int i = 0; i < table.Count; ++i)
                    {
                        foreach (string entry in table[i])
                        {
                            builder.Append(entry);
                            builder.Append(" ");
                        }

                        builder.AppendLine();
                    }

                    needNewLine = true;
                }

                if (klass.Name == "NSObject")
                    break;

                klass = klass.BaseClass;
            }
        }
Ejemplo n.º 3
0
 internal Native(IntPtr target, Selector selector, Class klass)
     : this(target, selector, DoGetImp(target, selector, (IntPtr) klass), null)
 {
 }
Ejemplo n.º 4
0
        private void DoAddMethods(Class klass, List<List<string>> table, bool includePrivate)
        {
            int count = 0;
            IntPtr buffer = class_copyMethodList((IntPtr) klass, ref count);

            if (count > 0)
            {
                PtrArray methods = new PtrArray(buffer, count);

                for (int i = 0; i < count; ++i)
                {
                    Selector sel = new Selector(method_getName(methods[i]));
                    if (includePrivate || (sel.Name.Length > 0 && sel.Name[0] != '_'))
                    {
                        List<string> row = new List<string>();
                        row.Add(sel.Name);

                        IntPtr imp = method_getImplementation(methods[i]);
                        row.Add(imp.ToInt32().ToString("X"));

                        table.Add(row);
                    }
                }
            }

            Marshal.FreeHGlobal(buffer);
        }
Ejemplo n.º 5
0
        private static object DoDrainBuffer(IntPtr buffer, string encoding)
        {
            object result;

            switch (encoding)
            {
                case "i":
                    result = Marshal.ReadInt32(buffer);
                    break;

                case "l":
                    result = Marshal.ReadInt32(buffer);
                    break;

                case "q":
                    result = Marshal.ReadInt64(buffer);
                    break;

                case "I":
                    result = unchecked((UInt32) Marshal.ReadInt32(buffer));
                    break;

                case "L":
                    result = unchecked((UInt32) Marshal.ReadInt32(buffer));
                    break;

                case "Q":
                    result = unchecked((UInt64) Marshal.ReadInt64(buffer));
                    break;

                case "f":
                    result = Marshal.PtrToStructure(buffer, typeof(float));
                    break;

                case "d":
                    result = Marshal.PtrToStructure(buffer, typeof(double));
                    break;

                case "v":
                case "Vv":
                    result = null;
                    break;

                case "@":
                    IntPtr ip = Marshal.ReadIntPtr(buffer);
                    result = NSObject.Lookup(ip);
                    break;

                case "#":
                    result = new Class(Marshal.ReadIntPtr(buffer));
                    break;

                case ":":
                    result = new Selector(Marshal.ReadIntPtr(buffer));
                    break;

                default:
                    result = DoDrainPtrBuffer(buffer, encoding);

                    if (result == null)
                        result = DoDrainStructBuffer(buffer, encoding);

                    if (result == null)
                        throw new ArgumentException("Don't know how to read buffer of: " + encoding);
                    break;
            }

            return result;
        }