Changeset 212
- Timestamp:
- 04/30/08 08:40:09 (8 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
packages/defconAppKit/trunk/Lib/defconAppKit/views/glyphCellView.py
r209 r212 1 1 import weakref 2 2 import time 3 import objc 3 4 from Foundation import * 4 5 from AppKit import * … … 11 12 selectionColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(.82, .82, .9, 1.0) 12 13 13 14 DefconAppKitSelectedGlyphIndexesPboardType = "DefconAppKitSelectedGlyphIndexesPboardType"15 14 16 15 … … 119 118 self._allowDrag = value 120 119 121 def setAllowsDrop_(self, value):122 if value:123 self.registerForDraggedTypes_([DefconAppKitSelectedGlyphIndexesPboardType])124 else:125 self.unregisterDraggedTypes()126 127 120 def setGlyphs_(self, glyphs): 128 121 currentSelection = [self._glyphs[index] for index in self._selection] … … 362 355 self._mouseSelection(event, mouseDown=True) 363 356 if event.clickCount() > 1: 364 self.vanillaWrapper()._doubleClick() 357 vanillaWrapper = self.vanillaWrapper() 358 if vanillaWrapper._doubleClickCallback is not None: 359 vanillaWrapper._doubleClickCallback(vanillaWrapper) 365 360 self.autoscroll_(event) 366 361 … … 372 367 self._mouseSelection(event, mouseUp=True) 373 368 if self._selection != self._oldSelection: 374 self.vanillaWrapper()._selection() 369 vanillaWrapper = self.vanillaWrapper() 370 if vanillaWrapper._selectionCallback is not None: 371 vanillaWrapper._selectionCallback(vanillaWrapper) 375 372 del self._oldSelection 376 373 if self._glyphDetailMenu is not None: … … 502 499 # delete key. call the delete callback. 503 500 if characters in deleteCharacters: 504 self.vanillaWrapper()._ delete()501 self.vanillaWrapper()._removeSelection() 505 502 # non key. reset the typing entry if necessary. 506 503 elif characters in nonCharacters: … … 647 644 location = (location[0] - 10, location[1] + 10) 648 645 646 dragAndDropType = self.vanillaWrapper()._dragAndDropType 649 647 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) 652 650 653 651 self.dragImage_at_offset_event_pasteboard_source_slideBack_( … … 660 658 if source != self: 661 659 return None 660 dragAndDropType = self.vanillaWrapper()._dragAndDropType 662 661 pboard = draggingInfo.draggingPasteboard() 663 indexes = pboard.propertyListForType_( "DefconAppKitSelectedGlyphIndexesPboardType")662 indexes = pboard.propertyListForType_(dragAndDropType) 664 663 glyphs = self.getGlyphsAtIndexes_(indexes) 665 664 return glyphs … … 667 666 # drop 668 667 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 669 729 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) 674 731 675 732 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) 680 734 681 735 def draggingExited_(self, sender): … … 683 737 684 738 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) 690 740 691 741 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) 697 743 698 744 699 745 class GlyphCellView(vanilla.ScrollView): 700 746 701 def __init__(self, posSize, allowDrag=False,747 def __init__(self, posSize, 702 748 selectionCallback=None, doubleClickCallback=None, deleteCallback=None, dropCallback=None, 703 749 cellRepresentationName="defconAppKitGlyphCell", detailRepresentationName="defconAppKitGlyphCellDetail", 704 autohidesScrollers=True): 750 autohidesScrollers=True, selfWindowDropSettings=None, selfDocumentDropSettings=None, 751 selfApplicationDropSettings=None, otherApplicationDropSettings=None, allowDrag=False, 752 dragAndDropType="DefconAppKitSelectedGlyphIndexesPboardType"): 705 753 self._glyphCellView = DefconAppKitGlyphCellNSView.alloc().initWithFrame_cellRepresentationName_detailRepresentationName_( 706 754 ((0, 0), (400, 400)), cellRepresentationName, detailRepresentationName) … … 708 756 super(GlyphCellView, self).__init__(posSize, self._glyphCellView, hasHorizontalScroller=False, autohidesScrollers=autohidesScrollers, backgroundColor=backgroundColor) 709 757 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 710 777 self._glyphCellView.setAllowsDrag_(allowDrag) 711 self._glyphCellView.setAllowsDrop_(dropCallback is not None) 778 self._dragAndDropType = dragAndDropType 779 # callbacks 780 self._dropCallback = dropCallback 712 781 self._selectionCallback = selectionCallback 713 782 self._doubleClickCallback = doubleClickCallback 714 783 self._deleteCallback = deleteCallback 715 self._dropCallback = dropCallback784 # storage 716 785 self._glyphs = [] 717 786 … … 726 795 super(GlyphCellView, self)._breakCycles() 727 796 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): 737 798 if self._deleteCallback is not None: 738 799 self._deleteCallback(self) 739 800 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"]) 744 809 745 810 def getGlyphCellView(self):
