def initWithFrame_cocoaWindow_(self, frame, window): # The tracking area is used to get mouseEntered, mouseExited, and cursorUpdate # events so that we can custom set the mouse cursor within the view. self._tracking_area = None self = cocoapy.ObjCInstance( cocoapy.send_super(self, 'initWithFrame:', frame, argtypes=[cocoapy.NSRect])) if not self: return None # CocoaWindow object. self._window = window self.updateTrackingAreas() # Create an instance of PygletTextView to handle text events. # We must do this because NSOpenGLView doesn't conform to the # NSTextInputClient protocol by default, and the insertText: method will # not do the right thing with respect to translating key sequences like # "Option-e", "e" if the protocol isn't implemented. So the easiest # thing to do is to subclass NSTextView which *does* implement the # protocol and let it handle text input. PygletTextView = cocoapy.ObjCClass('PygletTextView') self._textview = PygletTextView.alloc().initWithCocoaWindow_(window) # Add text view to the responder chain. self.addSubview_(self._textview) return self
def performKeyEquivalent_(self, nsevent): # Let arrow keys and certain function keys pass through the responder # chain so that the textview can handle on_text_motion events. modifierFlags = nsevent.modifierFlags() if modifierFlags & cocoapy.NSNumericPadKeyMask: return False if modifierFlags & cocoapy.NSFunctionKeyMask: ch = cocoapy.cfstring_to_string(nsevent.charactersIgnoringModifiers()) if ch in (cocoapy.NSHomeFunctionKey, cocoapy.NSEndFunctionKey, cocoapy.NSPageUpFunctionKey, cocoapy.NSPageDownFunctionKey): return False # Send the key equivalent to the main menu to perform menu items. NSApp = cocoapy.ObjCClass('NSApplication').sharedApplication() NSApp.mainMenu().performKeyEquivalent_(nsevent) # Indicate that we've handled the event so system won't beep. return True
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # ---------------------------------------------------------------------------- from pyglet.window import key, mouse from pyglet.libs.darwin.quartzkey import keymap, charmap from pyglet.libs.darwin import cocoapy NSTrackingArea = cocoapy.ObjCClass('NSTrackingArea') # Event data helper functions. def getMouseDelta(nsevent): dx = nsevent.deltaX() dy = -nsevent.deltaY() return int(round(dx)), int(round(dy)) def getMousePosition(self, nsevent): in_window = nsevent.locationInWindow() in_window = self.convertPoint_fromView_(in_window, None) x = int(in_window.x) y = int(in_window.y)
# POSSIBILITY OF SUCH DAMAGE. # ---------------------------------------------------------------------------- import platform from ctypes import c_uint32, c_int, byref from pyglet.gl.base import Config, CanvasConfig, Context from pyglet.gl import ContextException from pyglet.gl import gl from pyglet.gl import agl from pyglet.canvas.cocoa import CocoaCanvas from pyglet.libs.darwin import cocoapy NSOpenGLPixelFormat = cocoapy.ObjCClass('NSOpenGLPixelFormat') NSOpenGLContext = cocoapy.ObjCClass('NSOpenGLContext') # Version info, needed as OpenGL different Lion and onward """Version is based on Darwin kernel, not OS-X version. OS-X / Darwin version history http://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history pre-release: 0.1, 0.2, 1.0, 1.1, kodiak: 1.2.1, cheetah: 1.3.1, puma: 1.4.1, 5.1 -> 5.5 jaguar: 6.0.1 -> 6.8 panther: 7.0 -> 7.9 tiger: 8.0 -> 8.11 leopard: 9.0 -> 9.8 snow_leopard: 10.0 -> 10.8
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # ---------------------------------------------------------------------------- from pyglet.app.base import PlatformEventLoop from pyglet.libs.darwin import cocoapy NSApplication = cocoapy.ObjCClass('NSApplication') NSMenu = cocoapy.ObjCClass('NSMenu') NSMenuItem = cocoapy.ObjCClass('NSMenuItem') NSAutoreleasePool = cocoapy.ObjCClass('NSAutoreleasePool') NSDate = cocoapy.ObjCClass('NSDate') NSEvent = cocoapy.ObjCClass('NSEvent') NSUserDefaults = cocoapy.ObjCClass('NSUserDefaults') class AutoReleasePool: def __enter__(self): self.pool = NSAutoreleasePool.alloc().init() return self.pool def __exit__(self, exc_type, exc_value, traceback): self.pool.drain()
import pyglet from pyglet.window import BaseWindow from pyglet.window import MouseCursor, DefaultMouseCursor from pyglet.event import EventDispatcher from pyglet.canvas.cocoa import CocoaCanvas from pyglet.libs.darwin import cocoapy, CGPoint from .systemcursor import SystemCursor from .pyglet_delegate import PygletDelegate from .pyglet_window import PygletWindow, PygletToolWindow from .pyglet_view import PygletView NSApplication = cocoapy.ObjCClass('NSApplication') NSCursor = cocoapy.ObjCClass('NSCursor') NSAutoreleasePool = cocoapy.ObjCClass('NSAutoreleasePool') NSColor = cocoapy.ObjCClass('NSColor') NSEvent = cocoapy.ObjCClass('NSEvent') NSImage = cocoapy.ObjCClass('NSImage') quartz = cocoapy.quartz cf = cocoapy.cf class CocoaMouseCursor(MouseCursor): gl_drawable = False def __init__(self, cursorName): # cursorName is a string identifying one of the named default NSCursors