Beispiel #1
0
 /// <summary>
 /// Disposes of the <see cref="NSAutoreleasePool"/> instance, draining it.
 /// </summary>
 public void Dispose()
 {
     if (_autoreleasePool != IntPtr.Zero)
     {
         Cocoa.SendVoid(_autoreleasePool, Selector.Get("drain"));
     }
 }
Beispiel #2
0
        static NSApplication()
        {
            Cocoa.Initialize();

            // Register a Quit method to be called on cmd-q
            IntPtr nsapp = Class.Get("NSApplication");

            Class.RegisterMethod(nsapp, OnQuitHandler, "quit", "v@:");

            // Fetch the application handle
            Handle = Cocoa.SendIntPtr(nsapp, Selector.Get("sharedApplication"));

            // Setup the application
            Cocoa.SendBool(Handle, Selector.Get("setActivationPolicy:"), (int)NSApplicationActivationPolicy.Regular);
            Cocoa.SendVoid(Handle, Selector.Get("discardEventsMatchingMask:beforeEvent:"), uint.MaxValue, IntPtr.Zero);
            Cocoa.SendVoid(Handle, Selector.Get("activateIgnoringOtherApps:"), true);

            if (Cocoa.SendIntPtr(Handle, Selector.Get("mainMenu")) == IntPtr.Zero)
            {
                // Create the menu bar
                var menubar  = Cocoa.SendIntPtr(Class.Get("NSMenu"), Selector.Alloc);
                var menuItem = Cocoa.SendIntPtr(Class.Get("NSMenuItem"), Selector.Alloc);

                // Add menu item to bar, and bar to application
                Cocoa.SendIntPtr(menubar, Selector.Get("addItem:"), menuItem);
                Cocoa.SendIntPtr(Handle, Selector.Get("setMainMenu:"), menubar);

                // Add a "Quit" menu item and bind the button.
                var appMenu      = Cocoa.SendIntPtr(Class.Get("NSMenu"), Selector.Alloc);
                var quitMenuItem = Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSMenuItem"), Selector.Alloc),
                                                    Selector.Get("initWithTitle:action:keyEquivalent:"), Cocoa.ToNSString("Quit"), selQuit, Cocoa.ToNSString("q"));

                Cocoa.SendIntPtr(appMenu, Selector.Get("addItem:"), quitMenuItem);
                Cocoa.SendIntPtr(menuItem, Selector.Get("setSubmenu:"), appMenu);

                // Tell cocoa we're ready to run the application (usually called by [NSApp run]).
                // Note: if a main menu exists, then this method has already been called and
                // calling it again will result in a crash. For this reason, we only call it
                // when we create our own main menu.
                Cocoa.SendVoid(Handle, Selector.Get("finishLaunching"));
            }

            // Disable momentum scrolling and long-press key pop-ups
            IntPtr settings = Cocoa.SendIntPtr(Class.NSDictionary, Selector.Alloc);
            //IntPtr momentum_scrolling = Cocoa.SendIntPtr(Class.NSNumber, Selector.Get("numberWithBool:"), false);
            IntPtr press_and_hold = Cocoa.SendIntPtr(Class.NSNumber, Selector.Get("numberWithBool:"), false);

            // Initialize and register the settings dictionary
            settings =
                Cocoa.SendIntPtr(settings, Selector.Get("initWithObjectsAndKeys:"),
                                 //momentum_scrolling, Cocoa.ToNSString("AppleMomentumScrollSupported"),
                                 press_and_hold, Cocoa.ToNSString("ApplePressAndHoldEnabled"),
                                 IntPtr.Zero);
            Cocoa.SendVoid(
                Cocoa.SendIntPtr(Class.NSUserDefaults, Selector.Get("standardUserDefaults")),
                Selector.Get("registerDefaults:"),
                settings);
            Cocoa.SendVoid(settings, Selector.Release);
        }
Beispiel #3
0
        private static void OnQuit(IntPtr self, IntPtr cmd)
        {
            var e = new CancelEventArgs();

            Quit(null, e);
            if (!e.Cancel)
            {
                Cocoa.SendVoid(Handle, Selector.Get("terminate:"), Handle);
            }
        }
Beispiel #4
0
        public static IntPtr ToNSString(string str)
        {
            if (str == null)
            {
                return(IntPtr.Zero);
            }

            unsafe
            {
                fixed(char *ptrFirstChar = str)
                {
                    var handle = Cocoa.SendIntPtr(Class.Get("NSString"), Selector.Alloc);

                    handle = Cocoa.SendIntPtr(handle, Selector.Get("initWithCharacters:length:"), (IntPtr)ptrFirstChar, str.Length);
                    return(handle);
                }
            }
        }
Beispiel #5
0
        private void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            if (disposing)
            {
                Cocoa.SendVoid(Handle, Selector.Release);
            }
            else
            {
                Debug.Print("CocoaWindowInfo:{0} leaked, did you forget to call Dispose()?", Handle);
            }

            disposed = true;
        }
Beispiel #6
0
        public static unsafe IntPtr ToNSImage(Image img)
        {
            using (System.IO.MemoryStream s = new System.IO.MemoryStream())
            {
                img.Save(s, ImageFormat.Png);
                byte[] b = s.ToArray();

                fixed(byte *pBytes = b)
                {
                    IntPtr nsData = Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSData"), Selector.Alloc),
                                                     Selector.Get("initWithBytes:length:"), (IntPtr)pBytes, b.Length);

                    IntPtr nsImage = Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSImage"), Selector.Alloc),
                                                      Selector.Get("initWithData:"), nsData);

                    Cocoa.SendVoid(nsData, Selector.Release);
                    return(nsImage);
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Allocates and initializes a new <see cref="NSAutoreleasePool"/>.
        /// </summary>
        public NSAutoreleasePool()
        {
            var uninitializedPool = Cocoa.SendIntPtr(Class.NSAutoreleasePool, Selector.Alloc);

            _autoreleasePool = Cocoa.SendIntPtr(uninitializedPool, Selector.Init);
        }
Beispiel #8
0
        public static bool IsOperatingSystemAtLeastVersion(NSOperatingSystemVersion version)
        {
            var processInfo = Cocoa.SendIntPtr(classProcessInfo, selProcessInfo);

            return(Cocoa.SendBool(processInfo, selIsOperatingSystemAtLeastVersion, version));
        }
Beispiel #9
0
 /// <summary>
 /// Constructs a new instance with the specified parameters.
 /// </summary>
 /// <param name="nsWindowRef">A valid NSWindow reference.</param>
 /// <param name="nsViewRef">A valid NSView reference.</param>
 public CocoaWindowInfo(IntPtr nsWindowRef, IntPtr nsViewRef)
 {
     this.Handle     = nsWindowRef;
     this.ViewHandle = nsViewRef;
     Cocoa.SendVoid(nsWindowRef, Selector.Retain);
 }
Beispiel #10
0
 /// <summary>
 /// Constructs a new instance with the specified parameters.
 /// </summary>
 /// <remarks>This constructor assumes that the NSWindow's contentView is the NSView we want to attach to our context.</remarks>
 /// <param name="nsWindowRef">A valid NSWindow reference.</param>
 public CocoaWindowInfo(IntPtr nsWindowRef) : this(nsWindowRef, Cocoa.SendIntPtr(nsWindowRef, selContentView))
 {
 }