Changeset 212

Show
Ignore:
Timestamp:
04/30/08 08:40:09 (8 months ago)
Author:
tal
Message:
Massive change. The GlyphCellView is now deprecated. In its I've added a new GlyphCollectionView. This view is a hybrid of the GlyphCellView and a standard vanilla.List. This required some big changes to the NSView that is used for the cell view and it required the creation of some new objects.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • packages/defconAppKit/trunk/Lib/defconAppKit/views/glyphCellView.py

    r209 r212  
    11import weakref 
    22import time 
     3import objc 
    34from Foundation import * 
    45from AppKit import * 
     
    1112selectionColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(.82, .82, .9, 1.0) 
    1213 
    13  
    14 DefconAppKitSelectedGlyphIndexesPboardType = "DefconAppKitSelectedGlyphIndexesPboardType" 
    1514 
    1615 
     
    119118        self._allowDrag = value 
    120119 
    121     def setAllowsDrop_(self, value): 
    122         if value: 
    123             self.registerForDraggedTypes_([DefconAppKitSelectedGlyphIndexesPboardType]) 
    124         else: 
    125             self.unregisterDraggedTypes() 
    126  
    127120    def setGlyphs_(self, glyphs): 
    128121        currentSelection = [self._glyphs[index] for index in self._selection] 
     
    362355        self._mouseSelection(event, mouseDown=True) 
    363356        if event.clickCount() > 1: 
    364             self.vanillaWrapper()._doubleClick() 
     357            vanillaWrapper = self.vanillaWrapper() 
     358            if vanillaWrapper._doubleClickCallback is not None: 
     359                vanillaWrapper._doubleClickCallback(vanillaWrapper) 
    365360        self.autoscroll_(event) 
    366361 
     
    372367        self._mouseSelection(event, mouseUp=True) 
    373368        if self._selection != self._oldSelection: 
    374             self.vanillaWrapper()._selection() 
     369            vanillaWrapper = self.vanillaWrapper() 
     370            if vanillaWrapper._selectionCallback is not None: 
     371                vanillaWrapper._selectionCallback(vanillaWrapper) 
    375372        del self._oldSelection 
    376373        if self._glyphDetailMenu is not None: 
     
    502499        # delete key. call the delete callback. 
    503500        if characters in deleteCharacters: 
    504             self.vanillaWrapper()._delete() 
     501            self.vanillaWrapper()._removeSelection() 
    505502        # non key. reset the typing entry if necessary. 
    506503        elif characters in nonCharacters: 
     
    647644        location = (location[0] - 10, location[1] + 10) 
    648645 
     646        dragAndDropType = self.vanillaWrapper()._dragAndDropType 
    649647        pboard = NSPasteboard.pasteboardWithName_(NSDragPboard) 
    650         pboard.declareTypes_owner_([DefconAppKitSelectedGlyphIndexesPboardType], self) 
    651         pboard.setPropertyList_forType_(indexes, DefconAppKitSelectedGlyphIndexesPboardType) 
     648        pboard.declareTypes_owner_([dragAndDropType], self) 
     649        pboard.setPropertyList_forType_(indexes, dragAndDropType) 
    652650 
    653651        self.dragImage_at_offset_event_pasteboard_source_slideBack_( 
     
    660658        if source != self: 
    661659            return None 
     660        dragAndDropType = self.vanillaWrapper()._dragAndDropType 
    662661        pboard = draggingInfo.draggingPasteboard() 
    663         indexes = pboard.propertyListForType_("DefconAppKitSelectedGlyphIndexesPboardType"
     662        indexes = pboard.propertyListForType_(dragAndDropType
    664663        glyphs = self.getGlyphsAtIndexes_(indexes) 
    665664        return glyphs 
     
    667666    # drop 
    668667 
     668    def _handleDrop(self, draggingInfo, isProposal=False, callCallback=False): 
     669        vanillaWrapper = self.vanillaWrapper() 
     670        draggingSource = draggingInfo.draggingSource() 
     671        sourceForCallback = draggingSource 
     672        if hasattr(draggingSource, "vanillaWrapper") and getattr(draggingSource, "vanillaWrapper") is not None: 
     673            sourceForCallback = getattr(draggingSource, "vanillaWrapper")() 
     674        # make the info dict 
     675        dropOnRow = False # XXX support in future 
     676        rowIndex = len(self._glyphs) 
     677        dropInformation = dict(isProposal=isProposal, dropOnRow=dropOnRow, rowIndex=rowIndex, data=None, source=sourceForCallback) 
     678        # drag from self 
     679        if draggingSource == self: 
     680            # XXX not supported yet 
     681            return NSDragOperationNone 
     682        # drag from same window 
     683        window = self.window() 
     684        if window is not None and draggingSource is not None and window == draggingSource.window() and vanillaWrapper._selfWindowDropSettings is not None: 
     685            if vanillaWrapper._selfWindowDropSettings is None: 
     686                return NSDragOperationNone 
     687            settings = vanillaWrapper._selfWindowDropSettings 
     688            return self._handleDropBasedOnSettings(settings, vanillaWrapper, dropOnRow, draggingInfo, dropInformation, callCallback) 
     689        # drag from same document 
     690        document = self.window().document() 
     691        if document is not None and document == draggingSource.window().document(): 
     692            if vanillaWrapper._selfDocumentDropSettings is None: 
     693                return NSDragOperationNone 
     694            settings = vanillaWrapper._selfDocumentDropSettings 
     695            return self._handleDropBasedOnSettings(settings, vanillaWrapper, dropOnRow, draggingInfo, dropInformation, callCallback) 
     696        # drag from same application 
     697        applicationWindows = NSApp().windows() 
     698        if draggingSource is not None and draggingSource.window() in applicationWindows: 
     699            if vanillaWrapper._selfApplicationDropSettings is None: 
     700                return NSDragOperationNone 
     701            settings = vanillaWrapper._selfApplicationDropSettings 
     702            return self._handleDropBasedOnSettings(settings, vanillaWrapper, dropOnRow, draggingInfo, dropInformation, callCallback) 
     703        # fall back to drag from other application 
     704        if vanillaWrapper._otherApplicationDropSettings is None: 
     705            return NSDragOperationNone 
     706        settings = vanillaWrapper._otherApplicationDropSettings 
     707        return self._handleDropBasedOnSettings(settings, vanillaWrapper, dropOnRow, draggingInfo, dropInformation, callCallback) 
     708 
     709    def _handleDropBasedOnSettings(self, settings, vanillaWrapper, dropOnRow, draggingInfo, dropInformation, callCallback): 
     710        # XXX validate drop position in future 
     711        # sometimes the callback will need to be called 
     712        if callCallback: 
     713            dropInformation["data"] = self._unpackPboard(settings, draggingInfo) 
     714            result = settings["callback"](vanillaWrapper, dropInformation) 
     715            if result: 
     716                return settings.get("operation", NSDragOperationCopy) 
     717        # other times it won't 
     718        else: 
     719            return settings.get("operation", NSDragOperationCopy) 
     720        return NSDragOperationNone 
     721 
     722    def _unpackPboard(self, settings, draggingInfo): 
     723        pboard = draggingInfo.draggingPasteboard() 
     724        data = pboard.propertyListForType_(settings["type"]) 
     725        if isinstance(data, (NSString, objc.pyobjc_unicode)): 
     726            data = data.propertyList() 
     727        return data 
     728 
    669729    def draggingEntered_(self, sender): 
    670         source = sender.draggingSource() 
    671         if source == self: 
    672             return NSDragOperationNone 
    673         return NSDragOperationCopy 
     730        return self._handleDrop(sender, isProposal=True, callCallback=False) 
    674731 
    675732    def draggingUpdated_(self, sender): 
    676         source = sender.draggingSource() 
    677         if source == self: 
    678             return NSDragOperationNone 
    679         return NSDragOperationCopy 
     733        return self._handleDrop(sender, isProposal=True, callCallback=False) 
    680734 
    681735    def draggingExited_(self, sender): 
     
    683737 
    684738    def prepareForDragOperation_(self, sender): 
    685         source = sender.draggingSource() 
    686         if source == self: 
    687             return NSDragOperationNone 
    688         glyphs = source.getGlyphsFromDraggingInfo_(sender) 
    689         return self.vanillaWrapper()._proposeDrop(glyphs, testing=True) 
     739        return self._handleDrop(sender, isProposal=True, callCallback=True) 
    690740 
    691741    def performDragOperation_(self, sender): 
    692         source = sender.draggingSource() 
    693         if source == self: 
    694             return NSDragOperationNone 
    695         glyphs = source.getGlyphsFromDraggingInfo_(sender) 
    696         return self.vanillaWrapper()._proposeDrop(glyphs, testing=False) 
     742        return self._handleDrop(sender, isProposal=False, callCallback=True) 
    697743 
    698744 
    699745class GlyphCellView(vanilla.ScrollView): 
    700746 
    701     def __init__(self, posSize, allowDrag=False, 
     747    def __init__(self, posSize, 
    702748        selectionCallback=None, doubleClickCallback=None, deleteCallback=None, dropCallback=None, 
    703749        cellRepresentationName="defconAppKitGlyphCell", detailRepresentationName="defconAppKitGlyphCellDetail", 
    704         autohidesScrollers=True): 
     750        autohidesScrollers=True, selfWindowDropSettings=None, selfDocumentDropSettings=None, 
     751        selfApplicationDropSettings=None, otherApplicationDropSettings=None, allowDrag=False, 
     752        dragAndDropType="DefconAppKitSelectedGlyphIndexesPboardType"): 
    705753        self._glyphCellView = DefconAppKitGlyphCellNSView.alloc().initWithFrame_cellRepresentationName_detailRepresentationName_( 
    706754            ((0, 0), (400, 400)), cellRepresentationName, detailRepresentationName) 
     
    708756        super(GlyphCellView, self).__init__(posSize, self._glyphCellView, hasHorizontalScroller=False, autohidesScrollers=autohidesScrollers, backgroundColor=backgroundColor) 
    709757        self._glyphCellView.subscribeToScrollViewFrameChange_(self._nsObject) 
     758 
     759        if dropCallback is not None: 
     760            from warnings import warn 
     761            warn(DeprecationWarning("dropCallback is deprecated. Use the new drop attributes.")) 
     762            selfWindowDropSettings = dict(operation=NSDragOperationCopy, callback=self._deprecatedDropCallback) 
     763            selfDocumentDropSettings = dict(operation=NSDragOperationCopy, callback=self._deprecatedDropCallback) 
     764            selfApplicationDropSettings = dict(operation=NSDragOperationCopy, callback=self._deprecatedDropCallback) 
     765            otherApplicationDropSettings = dict(operation=NSDragOperationCopy, callback=self._deprecatedDropCallback) 
     766        for i in (selfWindowDropSettings, selfDocumentDropSettings, selfApplicationDropSettings, otherApplicationDropSettings): 
     767            if i is not None: 
     768                i["type"] = dragAndDropType 
     769        for i in (selfWindowDropSettings, selfDocumentDropSettings, selfApplicationDropSettings, otherApplicationDropSettings): 
     770            if i is not None: 
     771                self._glyphCellView.registerForDraggedTypes_([dragAndDropType]) 
     772                break 
     773        self._selfWindowDropSettings = selfWindowDropSettings 
     774        self._selfDocumentDropSettings = selfDocumentDropSettings 
     775        self._otherApplicationDropSettings = selfApplicationDropSettings 
     776        self._otherApplicationDropSettings = otherApplicationDropSettings 
    710777        self._glyphCellView.setAllowsDrag_(allowDrag) 
    711         self._glyphCellView.setAllowsDrop_(dropCallback is not None) 
     778        self._dragAndDropType = dragAndDropType 
     779        # callbacks 
     780        self._dropCallback = dropCallback 
    712781        self._selectionCallback = selectionCallback 
    713782        self._doubleClickCallback = doubleClickCallback 
    714783        self._deleteCallback = deleteCallback 
    715         self._dropCallback = dropCallback 
     784        # storage 
    716785        self._glyphs = [] 
    717786 
     
    726795        super(GlyphCellView, self)._breakCycles() 
    727796 
    728     def _selection(self): 
    729         if self._selectionCallback is not None: 
    730             self._selectionCallback(self) 
    731  
    732     def _doubleClick(self): 
    733         if self._doubleClickCallback is not None: 
    734             self._doubleClickCallback(self) 
    735  
    736     def _delete(self): 
     797    def _removeSelection(self): 
    737798        if self._deleteCallback is not None: 
    738799            self._deleteCallback(self) 
    739800 
    740     def _proposeDrop(self, glyphs, testing): 
    741         if self._dropCallback is not None: 
    742             return self._dropCallback(self, glyphs, testing) 
    743         return False 
     801    def _deprecatedDropCallback(self, sender, dropInfo): 
     802        source = dropInfo["source"] 
     803        indexes = [int(i) for i in dropInfo["data"]] 
     804        if isinstance(source, vanilla.VanillaBaseObject): 
     805            glyphs = [source[i] for i in indexes] 
     806        else: 
     807            glyphs = source.getGlyphsAtIndexes_(indexes) 
     808        return self._dropCallback(self, glyphs, not dropInfo["isProposal"]) 
    744809 
    745810    def getGlyphCellView(self):