Ejemplo n.º 1
0
 def __init__(self, style='standard', zoomable=None, **kwds):
     # We ignore zoomable, since it's the same as resizable.
     self._style = style
     options = dict(_default_options_for_style[style])
     for option in ['movable', 'closable', 'hidable', 'resizable']:
         if option in kwds:
             options[option] = kwds.pop(option)
     self._ns_style_mask = self._ns_window_style_mask(**options)
     if style == 'fullscreen':
         ns_rect = NSScreen.mainScreen().frame()
     else:
         ns_rect = NSRect(NSPoint(0, 0),
                          NSSize(self._default_width, self._default_height))
     ns_window = PyGUI_NSWindow.alloc()
     ns_window.initWithContentRect_styleMask_backing_defer_(
         ns_rect, self._ns_style_mask, AppKit.NSBackingStoreBuffered, True)
     ns_content = PyGUI_NS_ContentView.alloc()
     ns_content.initWithFrame_(NSRect(NSPoint(0, 0), NSSize(0, 0)))
     ns_content.pygui_component = self
     ns_window.setContentView_(ns_content)
     ns_window.setAcceptsMouseMovedEvents_(True)
     ns_window.setDelegate_(ns_window)
     ns_window.pygui_component = self
     self._ns_window = ns_window
     GWindow.__init__(self,
                      style=style,
                      closable=options['closable'],
                      _ns_view=ns_window.contentView(),
                      _ns_responder=ns_window,
                      _ns_set_autoresizing_mask=False,
                      **kwds)
Ejemplo n.º 2
0
 def _create_ns_textfield(self, editable, text, font,
         multiline = False, password = False, border = False,
         padding = (0, 0)):
     self._ns_is_password = password
     if password:
         ns_class = PyGUI_NSSecureTextField
     else:
         ns_class = PyGUI_NSTextField
     ns_frame = NSRect(NSPoint(0, 0), NSSize(20, 10))
     ns_textfield = ns_class.alloc().initWithFrame_(ns_frame)
     ns_textfield.pygui_component = self
     if multiline and not password:
         ns_textfield.pygui_multiline = True
     # Be careful here -- calling setBordered_ seems to affect isBezeled as well
     if editable:
         ns_textfield.setBezeled_(border)
     else:
         ns_textfield.setBordered_(border)
     if not editable:
         ns_textfield.setDrawsBackground_(False)
     ns_textfield.setEditable_(editable)
     ns_textfield.setSelectable_(editable)
     ns_textfield.setFont_(font._ns_font)
     ns_textfield.setStringValue_(text)
     ns_size_to_fit(ns_textfield, padding = padding)
     return ns_textfield
Ejemplo n.º 3
0
def buildNotdef(thisFont, override=False):
    questionGlyph = thisFont.glyphs["question"]
    if not questionGlyph:
        print(
            "⚠️ Error building .notdef: No question mark is available in your font. Cannot create .notdef."
        )
    else:
        name = ".notdef"
        notdefGlyph = createGlyph(thisFont, name, None, override=override)
        if notdefGlyph:
            sourceLayer = questionGlyph.layers[0]
            area = areaOfLayer(sourceLayer)
            for qLayer in questionGlyph.layers:
                if qLayer.isMasterLayer or qLayer.isSpecialLayer:
                    qArea = areaOfLayer(qLayer)
                    if qArea > area:
                        sourceLayer = qLayer
                        area = qArea

            if sourceLayer:
                # Build .notdef from question mark and circle:
                questionmarkLayer = sourceLayer.copyDecomposedLayer()
                scaleLayerByFactor(questionmarkLayer, 0.8)
                qOrigin = questionmarkLayer.bounds.origin
                qWidth = questionmarkLayer.bounds.size.width
                qHeight = questionmarkLayer.bounds.size.height
                qCenter = NSPoint(qOrigin.x + 0.5 * qWidth,
                                  qOrigin.y + 0.5 * qHeight)
                side = max((qWidth, qHeight)) * 1.5
                circleRect = NSRect(
                    NSPoint(qCenter.x - 0.5 * side, qCenter.y - 0.5 * side),
                    NSSize(side, side))
                circle = circleInsideRect(circleRect)
                try:
                    # GLYPHS 3
                    questionmarkLayer.shapes.append(circle)
                except:
                    # GLYPHS 2
                    questionmarkLayer.paths.append(circle)
                questionmarkLayer.correctPathDirection()

                # Create glyph:
                notdefGlyph.leftMetricsKey = "=40"
                notdefGlyph.rightMetricsKey = "=|"
                for masterID in [m.id for m in thisFont.masters]:
                    notdefLayer = notdefGlyph.layers[masterID]
                    for thisPath in questionmarkLayer.paths:
                        try:
                            # GLYPHS 3:
                            notdefLayer.shapes.append(thisPath.copy())
                        except:
                            # GLYPHS 2:
                            notdefLayer.paths.append(thisPath.copy())
                    notdefLayer.syncMetrics()
            else:
                print(
                    "⚠️ Error building .notdef: Could not determine source layer of glyph 'question'."
                )
Ejemplo n.º 4
0
 def __init__(self, width, height, config=None, share_group=None, **kwds):
     pf = GLConfig._from_args(config, kwds)
     ns_pf = pf._ns_get_pixel_format()
     ns_size = NSSize(width, height)
     ns_cache = NSCachedImageRep.alloc().initWithSize_depth_separate_alpha_(
         ns_size, 0, True, True)
     ns_image = NSImage.alloc().initWithSize_(ns_size)
     GLContext.__init__(self,
                        share_group=share_group,
                        _ns_pixel_format=ns_pf)
     self._ns_context.setView_(ns_cache.window().contentView())
     self._init_with_ns_image(ns_image, flipped=False)
     self._ns_cache = ns_cache
     self.with_context(self._init_context)
Ejemplo n.º 5
0
 def windowWillReturnFieldEditor_toObject_(self, ns_window, ns_obj):
     #  Return special field editor for newline handling in text fields.
     #print "Window: Field editor requested for", object.__repr__(ns_obj) ###
     #editor = self.pygui_field_editor
     #if not editor:
     try:
         editor = self.pygui_field_editor
     except AttributeError:
         #print "...creating new field editor" ###
         editor = PyGUI_FieldEditor.alloc().initWithFrame_(
             NSRect(NSPoint(0, 0), NSSize(0, 0)))
         editor.setFieldEditor_(True)
         editor.setDrawsBackground_(False)
         self.pygui_field_editor = editor
     return editor
Ejemplo n.º 6
0
 def _ns_flush(self):
     glFlush()
     width, height = self.size
     pixels = glReadPixels(0, 0, int(width), int(height), GL_RGBA,
                           GL_UNSIGNED_BYTE)
     bytes_per_row = int(width) * 4
     ns_new_bitmap = NSBitmapImageRep.alloc().\
         initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
         (pixels, "", "", "", ""), int(width), int(height), 8, 4, True, False, AppKit.NSDeviceRGBColorSpace, bytes_per_row, 0)
     ns_image = NSImage.alloc().initWithSize_(NSSize(width, height))
     ns_image.addRepresentation_(ns_new_bitmap)
     ns_image.lockFocus()
     ns_image.unlockFocus()
     self._ns_image = ns_image
     self._ns_bitmap_image_rep = ns_new_bitmap
Ejemplo n.º 7
0
 def __init__(self,
              title,
              value=50,
              minValue=0,
              maxValue=100,
              callback=None,
              name=None,
              dimensions=(180, 15)):
     if isinstance(
             title,
             SliderMenuItem):  # don't initialize already existing instances
         return
     self._slider = NSSlider.alloc().init()
     self._slider.setMinValue_(minValue)
     self._slider.setMaxValue_(maxValue)
     self._slider.setValue_(value)
     self._slider.setFrameSize_(NSSize(*dimensions))
     self._slider.setTarget_(NSApp)
     self.set_slider_callback(callback)
     super(SliderMenuItem, self).__init__(title, name=name)
     self._menuitem.setView_(self._slider)
Ejemplo n.º 8
0
    if thisFont.glyphs["question"]:
        sourceMasterID = thisFont.masters[len(thisFont.masters) - 1].id
        sourceLayer = thisFont.glyphs["question"].layers[sourceMasterID]
        if sourceLayer:
            # Build .notdef from question mark and circle:
            questionmarkLayer = sourceLayer.copyDecomposedLayer()
            scaleLayerByFactor(questionmarkLayer, 0.8)
            qOrigin = questionmarkLayer.bounds.origin
            qWidth = questionmarkLayer.bounds.size.width
            qHeight = questionmarkLayer.bounds.size.height
            qCenter = NSPoint(qOrigin.x + 0.5 * qWidth,
                              qOrigin.y + 0.5 * qHeight)
            side = max((qWidth, qHeight)) * 1.5
            circleRect = NSRect(
                NSPoint(qCenter.x - 0.5 * side, qCenter.y - 0.5 * side),
                NSSize(side, side))
            circle = circleInsideRect(circleRect)
            questionmarkLayer.paths.append(circle)
            questionmarkLayer.correctPathDirection()

            # Create glyph:
            notdefName = ".notdef"
            notdefGlyph = GSGlyph()
            notdefGlyph.name = notdefName
            thisFont.glyphs.append(notdefGlyph)
            if thisFont.glyphs[notdefName]:
                print("%s added successfully" % notdefName)
                print(thisFont.glyphs[notdefName])
                print(len(thisFont.glyphs[notdefName].layers))
            for masterID in [m.id for m in thisFont.masters]:
                notdefLayer = thisFont.glyphs[notdefName].layers[masterID]
Ejemplo n.º 9
0
 def layout(self, constraints):
     self.perform_layout(constraints)
     self.view.setFrameSize_(NSSize(self.bounds.size.x, self.bounds.size.y))
     self.layout_children()
Ejemplo n.º 10
0
class Window(GWindow):
    #  _ns_window        PyGUI_NSWindow
    #  _ns_style_mask    int

    def __init__(self, style='standard', zoomable=None, **kwds):
        # We ignore zoomable, since it's the same as resizable.
        self._style = style
        options = dict(_default_options_for_style[style])
        for option in ['movable', 'closable', 'hidable', 'resizable']:
            if option in kwds:
                options[option] = kwds.pop(option)
        self._ns_style_mask = self._ns_window_style_mask(**options)
        if style == 'fullscreen':
            ns_rect = NSScreen.mainScreen().frame()
        else:
            ns_rect = NSRect(NSPoint(0, 0),
                             NSSize(self._default_width, self._default_height))
        ns_window = PyGUI_NSWindow.alloc()
        ns_window.initWithContentRect_styleMask_backing_defer_(
            ns_rect, self._ns_style_mask, AppKit.NSBackingStoreBuffered, True)
        ns_content = PyGUI_NS_ContentView.alloc()
        ns_content.initWithFrame_(NSRect(NSPoint(0, 0), NSSize(0, 0)))
        ns_content.pygui_component = self
        ns_window.setContentView_(ns_content)
        ns_window.setAcceptsMouseMovedEvents_(True)
        ns_window.setDelegate_(ns_window)
        ns_window.pygui_component = self
        self._ns_window = ns_window
        GWindow.__init__(self,
                         style=style,
                         closable=options['closable'],
                         _ns_view=ns_window.contentView(),
                         _ns_responder=ns_window,
                         _ns_set_autoresizing_mask=False,
                         **kwds)

    def _ns_window_style_mask(self, movable, closable, hidable, resizable):
        if movable or closable or hidable or resizable:
            mask = AppKit.NSTitledWindowMask
            if closable:
                mask |= AppKit.NSClosableWindowMask
            if hidable:
                mask |= AppKit.NSMiniaturizableWindowMask
            if resizable:
                mask |= AppKit.NSResizableWindowMask
        else:
            mask = AppKit.NSBorderlessWindowMask
        return mask

    def destroy(self):
        #print "Window.destroy:", self ###
        self.hide()
        app = application()
        if app._ns_key_window is self:
            app._ns_key_window = None
        GWindow.destroy(self)
        #  We can't drop all references to the NSWindow yet, because this method
        #  can be called from its windowShouldClose: method, and allowing an
        #  NSWindow to be released while executing one of its own methods seems
        #  to be a very bad idea (Cocoa hangs). So we hide the NSWindow and store
        #  a reference to it in a global. It will be released the next time a
        #  window is closed and the global is re-used.
        global _ns_zombie_window
        _ns_zombie_window = self._ns_window
        self._ns_window.pygui_component = None
        #self._ns_window = None

    def get_bounds(self):
        ns_window = self._ns_window
        ns_frame = ns_window.frame()
        (l, y), (w, h) = ns_window.contentRectForFrameRect_styleMask_(
            ns_frame, self._ns_style_mask)
        b = Globals.ns_screen_height - y
        result = (l, b - h, l + w, b)
        return result

    def set_bounds(self, (l, t, r, b)):
        y = Globals.ns_screen_height - b
        ns_rect = NSRect(NSPoint(l, y), NSSize(r - l, b - t))
        ns_window = self._ns_window
        ns_frame = ns_window.frameRectForContentRect_styleMask_(
            ns_rect, self._ns_style_mask)
        ns_window.setFrame_display_(ns_frame, False)
Ejemplo n.º 11
0
def vectorFromNodes(n1, n2):
    return NSSize(n2.x - n1.x, n2.y - n1.y)