def on_motion_notify_event(self, view, event): if event.window != self.view.get_window(gtk.TEXT_WINDOW_TEXT): return False if self.drag_start_position != None: # We need to call gtk_drag_begin() ourselves so that we can provide the correct # target list. To do that, we duplicate the drag-and-logic, and just before the # text view would start the drag, we jump in and do it ourselves # start_x, start_y = self.drag_start_position if view.drag_check_threshold(int(start_x), int(start_y), int(event.x), int(event.y)): buf = view.get_buffer() bounds = buf.get_selection_bounds() # Synthesize a release event so GtkTextView doesn't start dragging on it's own release_event = gtk.gdk.Event(gtk.gdk.BUTTON_RELEASE) release_event.x = start_x release_event.y = start_y release_event.button = 1 release_event.window = event.window view.event(release_event) # but then we need to reselect, since the button release deselected if bounds != (): buf.select_range(*bounds) targets = gtk.target_list_add_text_targets() view.drag_begin(targets, gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE, 1, event) return True
def on_motion_notify_event(self, view, event): if event.window != self.view.get_window(gtk.TEXT_WINDOW_TEXT): return False if self.drag_start_position is not None: # We need to call gtk_drag_begin() ourselves so that we can provide the correct # target list. To do that, we duplicate the drag-and-logic, and just before the # text view would start the drag, we jump in and do it ourselves # start_x, start_y = self.drag_start_position if view.drag_check_threshold(int(start_x), int(start_y), int(event.x), int(event.y)): buf = view.get_buffer() bounds = buf.get_selection_bounds() # Synthesize a release event so GtkTextView doesn't start dragging on it's own release_event = gtk.gdk.Event(gtk.gdk.BUTTON_RELEASE) release_event.x = start_x release_event.y = start_y release_event.button = 1 release_event.window = event.window view.event(release_event) # but then we need to reselect, since the button release deselected if bounds != (): buf.select_range(*bounds) targets = gtk.target_list_add_text_targets() view.drag_begin(targets, gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE, 1, event) return True
def clipboard_copy_path(path): targets = gtk.target_list_add_uri_targets() targets = gtk.target_list_add_text_targets(targets) targets.append(("x-special/gnome-copied-files", 0, 0)) clipboard = gtk.clipboard_get() clipboard.set_with_data(targets, __clipboard_copy_path_get, __clipboard_copy_path_clear, path) clipboard.store()
def clipboard_copy_path(path): targets = gtk.target_list_add_uri_targets() targets = gtk.target_list_add_text_targets(targets) targets.append(('x-special/gnome-copied-files', 0, 0)) clipboard = gtk.clipboard_get() clipboard.set_with_data(targets, __clipboard_copy_path_get, __clipboard_copy_path_clear, path) clipboard.store()
def __copy_activate_cb(self, menu_item): clipboard = gtk.Clipboard() targets = gtk.target_list_add_uri_targets() targets = gtk.target_list_add_text_targets(targets) targets.append(('text/x-moz-url', 0, 0)) clipboard.set_with_data(targets, self.__clipboard_get_func_cb, self.__clipboard_clear_func_cb)
def __clipboard_get_func_cb(self, clipboard, selection_data, info, data): uri_targets = \ [target[0] for target in gtk.target_list_add_uri_targets()] text_targets = \ [target[0] for target in gtk.target_list_add_text_targets()] if selection_data.target in uri_targets: selection_data.set_uris([self._url]) elif selection_data.target in text_targets: selection_data.set_text(self._url) elif selection_data.target == 'text/x-moz-url': selection_data.set('text/x-moz-url', 8, self._url)
def copy_to_clipboard(obj, clipboard): """ Copy @obj to @clipboard, a gtk.Clipboard Return True if successful """ ## support copying text to clipboard ## as well as files in both the general uri-list representation ## and in nautilus' file copy clipboard type target_ids = (uri_id, text_id, nautilus_id) = (80, 81, 82) nautilus_target = 'x-special/gnome-copied-files' # udata is the data dict def store(clipboard, sdata, info, udata): if info == uri_id: sdata.set_uris(udata[uri_id]) if info == text_id: sdata.set_text(udata[text_id]) if info == nautilus_id: str_data_format = 8 sdata.set(nautilus_target, str_data_format, udata[nautilus_id]) def clear(clipboard, udata): pass targets = [] data = {} try: urilist = obj.get_urilist_representation() except AttributeError: pass else: if urilist: targets = gtk.target_list_add_uri_targets(targets, uri_id) targets.append((nautilus_target, 0, nautilus_id)) data[uri_id] = urilist data[nautilus_id] = 'copy\n' + '\n'.join(urilist) try: text = obj.get_text_representation() except AttributeError: pass else: targets = gtk.target_list_add_text_targets(targets, text_id) data[text_id] = text if data: clipboard.set_with_data(targets, store, clear, data) # store all targets clipboard.set_can_store(None) clipboard.store() return True return False
def copy_to_clipboard(obj, clipboard): """ Copy @obj to @clipboard, a gtk.Clipboard Return True if successful """ ## support copying text to clipboard ## as well as files in both the general uri-list representation ## and in nautilus' file copy clipboard type target_ids = (uri_id, text_id, nautilus_id) = (80, 81, 82) nautilus_target = 'x-special/gnome-copied-files' # udata is the data dict def store(clipboard, sdata, info, udata): if info == uri_id: sdata.set_uris(udata[uri_id]) if info == text_id: sdata.set_text(udata[text_id]) if info == nautilus_id: str_data_format = 8 sdata.set(nautilus_target, str_data_format, udata[nautilus_id]) def clear(clipboard, udata): pass targets = [] data = {} try: urilist = obj.get_urilist_representation() except AttributeError: pass else: if urilist: targets = gtk.target_list_add_uri_targets(targets, uri_id) targets.append((nautilus_target, 0, nautilus_id)) data[uri_id] = urilist data[nautilus_id] = 'copy\n' + '\n'.join(urilist) try: text = obj.get_text_representation() except AttributeError: pass else: targets = gtk.target_list_add_text_targets(targets, text_id) data[text_id] = text if data: clipboard.set_with_data(targets, store, clear, data) # store all targets clipboard.set_can_store(targets) return True return False
def update_dnd_targets (self): targets = None if self.current_entry: targets = gtk.target_list_add_image_targets (targets) targets = gtk.target_list_add_uri_targets (targets) targets = gtk.target_list_add_text_targets (targets) if targets: self.drag_dest_set (gtk.DEST_DEFAULT_ALL, targets, gtk.gdk.ACTION_COPY) else: self.drag_dest_unset () targets = None if self.current_pixbuf: targets = gtk.target_list_add_image_targets (targets, writable=True) if self.current_uri: targets = gtk.target_list_add_uri_targets (targets) if targets: self.drag_source_set (gtk.gdk.BUTTON1_MASK, targets, gtk.gdk.ACTION_COPY) else: self.drag_source_unset ()
def set_as_current(self, event=None): def path_get(clipboard, selectiondata, info, path): selectiondata.set_text(path) files = path.split("\n") file_paths = [] for copied_file in files: gfile = gio.File(copied_file) file_paths.append(gfile.get_uri()) selectiondata.set_uris(file_paths) selectiondata.set('x-special/gnome-copied-files', 8, 'copy\n' + '\n'.join(file_paths)) def path_clear(self, path): return HistoryMenuItem.set_as_current(self, event) targets = gtk.target_list_add_uri_targets() targets = gtk.target_list_add_text_targets(targets) targets.append(('x-special/gnome-copied-files', 0, 0)) gtk.clipboard_get().set_with_data(targets, path_get, path_clear, self.payload) gtk.clipboard_get().store()
def set_as_current(self, event=None): def path_get(clipboard, selectiondata, info, path): selectiondata.set_text(path) files = path.split("\n") file_paths = [] for copied_file in files: file_path = gnomevfs.escape_path_string(copied_file) file_paths.append("file://" + file_path) selectiondata.set_uris(file_paths) selectiondata.set("x-special/gnome-copied-files", 8, "copy\n" + "\n".join(file_paths)) def path_clear(self, path): return HistoryMenuItem.set_as_current(self, event) targets = gtk.target_list_add_uri_targets() targets = gtk.target_list_add_text_targets(targets) targets.append(("x-special/gnome-copied-files", 0, 0)) gtk.clipboard_get().set_with_data(targets, path_get, path_clear, self.payload) gtk.clipboard_get().store()
def update_dnd_targets(self): targets = None if self.current_entry: targets = gtk.target_list_add_image_targets(targets) targets = gtk.target_list_add_uri_targets(targets) targets = gtk.target_list_add_text_targets(targets) if targets: self.drag_dest_set(gtk.DEST_DEFAULT_ALL, targets, gtk.gdk.ACTION_COPY) else: self.drag_dest_unset() targets = None if self.current_pixbuf: targets = gtk.target_list_add_image_targets(targets, writable=True) if self.current_uri: targets = gtk.target_list_add_uri_targets(targets) if targets: self.drag_source_set(gtk.gdk.BUTTON1_MASK, targets, gtk.gdk.ACTION_COPY) else: self.drag_source_unset()
#~ print IMAGE_TARGETS #~ print IMAGE_TARGET_NAMES URI_TARGET_ID = 7 URI_TARGETS = tuple(gtk.target_list_add_uri_targets(info=URI_TARGET_ID)) # According to docs we should provide list as arg to this function, # but seems docs are not correct URI_TARGET_NAMES = tuple([target[0] for target in URI_TARGETS]) HTML_TARGET_ID = 8 HTML_TARGET_NAMES = ('text/html', 'HTML Format') # "HTML Format" is from MS Word HTML_TARGETS = tuple([(name, 0, HTML_TARGET_ID) for name in HTML_TARGET_NAMES]) TEXT_TARGET_ID = 9 TEXT_TARGETS = tuple(gtk.target_list_add_text_targets(info=TEXT_TARGET_ID)) # According to docs we should provide list as arg to this function, # but seems docs are not correct TEXT_TARGET_NAMES = tuple([target[0] for target in TEXT_TARGETS]) # All targets that we can convert to a parsetree, in order of choice PARSETREE_ACCEPT_TARGETS = ( PARSETREE_TARGET, INTERNAL_PAGELIST_TARGET, PAGELIST_TARGET, ) + IMAGE_TARGETS + URI_TARGETS + TEXT_TARGETS PARSETREE_ACCEPT_TARGET_NAMES = tuple([target[0] for target in PARSETREE_ACCEPT_TARGETS]) #~ print 'ACCEPT', PARSETREE_ACCEPT_TARGET_NAMES # Mimetype text/uri-list is used for drag n drop of URLs # it is plain text encoded list of urls, separated by \r\n
# USA """ Defines the data item FileItem, TextItem; each representing one piece of shelf data. """ FILE_TYPE = "file" TEXT_TYPE = "text" DATA_TYPE = "data" TARGET_TYPE_TEXT = 80 TARGET_TYPE_URI_LIST = 81 from gtk import target_list_add_text_targets, target_list_add_uri_targets text_targets = target_list_add_text_targets([], TARGET_TYPE_TEXT) file_targets = target_list_add_uri_targets([], TARGET_TYPE_URI_LIST) def make_item(obj, type): if type == FILE_TYPE: item = FileItem(obj) else: item = TextItem(obj) return item class Item (object): maxtiplen = 512 def __init__(self, obj, type): self.object = obj self.type = type