home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / lib / pygtk / 2.0 / demos / dnd.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2010-05-11  |  8KB  |  215 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. '''Drag and Drop
  5.  
  6. This is a test of the drag and drop capabilities of gtk.  It is a
  7. fairly straight forward port of the example distributed with gtk.
  8. '''
  9. import gtk
  10. import gobject
  11. from dndpixmap import drag_icon_xpm, trashcan_open_xpm, trashcan_closed_xpm
  12. TARGET_STRING = 0
  13. TARGET_ROOTWIN = 1
  14. target = [
  15.     ('STRING', 0, TARGET_STRING),
  16.     ('text/plain', 0, TARGET_STRING),
  17.     ('application/x-rootwin-drop', 0, TARGET_ROOTWIN)]
  18.  
  19. def create_pixmap(widget, xpm_data):
  20.     return gtk.gdk.pixmap_colormap_create_from_xpm_d(None, widget.get_colormap(), None, xpm_data)
  21.  
  22.  
  23. class DragAndDropDemo(gtk.Window):
  24.     trashcan_open = None
  25.     trashcan_open_mask = None
  26.     trashcan_closed = None
  27.     trashcan_closed_mask = None
  28.     drag_icon = None
  29.     drag_mask = None
  30.     have_drag = False
  31.     popped_up = False
  32.     in_popup = False
  33.     popup_timer = 0
  34.     popdown_timer = 0
  35.     popup_win = None
  36.     
  37.     def __init__(self, parent = None):
  38.         gtk.Window.__init__(self)
  39.         
  40.         try:
  41.             self.set_screen(parent.get_screen())
  42.         except AttributeError:
  43.             self.connect('destroy', (lambda : gtk.main_quit()))
  44.  
  45.         self.set_title(self.__class__.__name__)
  46.         table = gtk.Table(2, 2)
  47.         self.add(table)
  48.         (self.drag_icon, self.drag_mask) = create_pixmap(self, drag_icon_xpm)
  49.         (self.trashcan_open, self.trashcan_open_mask) = create_pixmap(self, trashcan_open_xpm)
  50.         (self.trashcan_closed, self.trashcan_closed_mask) = create_pixmap(self, trashcan_closed_xpm)
  51.         label = gtk.Label('Drop to Trashcan!\n')
  52.         label.drag_dest_set(gtk.DEST_DEFAULT_ALL, target[:-1], gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE)
  53.         label.connect('drag_data_received', self.label_drag_data_received)
  54.         table.attach(label, 0, 1, 0, 1)
  55.         label = gtk.Label('Popup\n')
  56.         label.drag_dest_set(gtk.DEST_DEFAULT_ALL, target[:-1], gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE)
  57.         table.attach(label, 1, 2, 1, 2)
  58.         label.connect('drag_motion', self.popsite_motion)
  59.         label.connect('drag_leave', self.popsite_leave)
  60.         image = gtk.Image()
  61.         image.set_from_pixmap(self.trashcan_closed, self.trashcan_closed_mask)
  62.         image.drag_dest_set(0, [], 0)
  63.         table.attach(image, 1, 2, 0, 1)
  64.         image.connect('drag_leave', self.target_drag_leave)
  65.         image.connect('drag_motion', self.target_drag_motion)
  66.         image.connect('drag_drop', self.target_drag_drop)
  67.         image.connect('drag_data_received', self.target_drag_data_received)
  68.         b = gtk.Button('Drag from Here\n')
  69.         b.drag_source_set(gtk.gdk.BUTTON1_MASK | gtk.gdk.BUTTON3_MASK, target, gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE)
  70.         b.drag_source_set_icon(self.get_colormap(), self.drag_icon, self.drag_mask)
  71.         table.attach(b, 0, 1, 1, 2)
  72.         b.connect('drag_data_get', self.source_drag_data_get)
  73.         b.connect('drag_data_delete', self.source_drag_data_delete)
  74.         self.show_all()
  75.  
  76.     
  77.     def label_drag_data_received(self, w, context, x, y, data, info, time):
  78.         if data and data.format == 8:
  79.             print 'Received "%s" in label' % data.data
  80.             context.finish(True, False, time)
  81.         else:
  82.             context.finish(False, False, time)
  83.  
  84.     
  85.     def popsite_motion(self, w, context, x, y, time):
  86.         if not self.popup_timer:
  87.             self.popup_timer = gobject.timeout_add(500, self.popup_cb)
  88.         
  89.         return True
  90.  
  91.     
  92.     def popsite_leave(self, w, context, time):
  93.         if self.popup_timer:
  94.             gobject.source_remove(self.popup_timer)
  95.             self.popup_timer = 0
  96.         
  97.  
  98.     
  99.     def popup_motion(self, w, context, x, y, time):
  100.         print 'popup_motion'
  101.         if not self.in_popup:
  102.             self.in_popup = True
  103.             if self.popdown_timer:
  104.                 print 'removed popdown'
  105.                 gobject.source_remove(self.popdown_timer)
  106.                 self.popdown_timer = 0
  107.             
  108.         
  109.         return True
  110.  
  111.     
  112.     def popup_leave(self, w, context, time):
  113.         print 'popup_leave'
  114.         if self.in_popup:
  115.             self.in_popup = False
  116.             if not self.popdown_timer:
  117.                 print 'added popdown'
  118.                 self.popdown_timer = gobject.timeout_add(500, self.popdown_cb)
  119.             
  120.         
  121.  
  122.     
  123.     def popup_cb(self):
  124.         if not self.popped_up:
  125.             if self.popup_win is None:
  126.                 self.popup_win = gtk.Window(gtk.WINDOW_POPUP)
  127.                 self.popup_win.set_position(gtk.WIN_POS_MOUSE)
  128.                 table = gtk.Table(3, 3)
  129.                 for k in range(9):
  130.                     (i, j) = divmod(k, 3)
  131.                     b = gtk.Button('%d,%d' % (i, j))
  132.                     b.drag_dest_set(gtk.DEST_DEFAULT_ALL, target[:-1], gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE)
  133.                     b.connect('drag_motion', self.popup_motion)
  134.                     b.connect('drag_leave', self.popup_leave)
  135.                     table.attach(b, i, i + 1, j, j + 1)
  136.                 
  137.                 table.show_all()
  138.                 self.popup_win.add(table)
  139.             
  140.             self.popup_win.present()
  141.             self.popped_up = True
  142.         
  143.         self.popdown_timer = gobject.timeout_add(500, self.popdown_cb)
  144.         print 'added popdown'
  145.         self.popup_timer = 0
  146.         return False
  147.  
  148.     
  149.     def popdown_cb(self):
  150.         print 'popdown'
  151.         self.popdown_timer = 0
  152.         self.popup_win.hide()
  153.         self.popped_up = False
  154.         return False
  155.  
  156.     
  157.     def target_drag_leave(self, img, context, time):
  158.         print 'leave'
  159.         self.have_drag = False
  160.         img.set_from_pixmap(self.trashcan_closed, self.trashcan_closed_mask)
  161.  
  162.     
  163.     def target_drag_motion(self, img, context, x, y, time):
  164.         if self.have_drag is False:
  165.             self.have_drag = True
  166.             img.set_from_pixmap(self.trashcan_open, self.trashcan_open_mask)
  167.         
  168.         source_widget = context.get_source_widget()
  169.         print 'motion, source ',
  170.         if source_widget:
  171.             print source_widget.__class__.__name__
  172.         else:
  173.             print 'unknown'
  174.         context.drag_status(context.suggested_action, time)
  175.         return True
  176.  
  177.     
  178.     def target_drag_drop(self, img, context, x, y, time):
  179.         print 'drop'
  180.         self.have_drag = False
  181.         img.set_from_pixmap(self.trashcan_closed, self.trashcan_closed_mask)
  182.         if context.targets:
  183.             img.drag_get_data(context, context.targets[0], time)
  184.             return True
  185.         return False
  186.  
  187.     
  188.     def target_drag_data_received(self, img, context, x, y, data, info, time):
  189.         if data.format == 8:
  190.             print 'Received "%s" in trashcan' % data.data
  191.             context.finish(True, False, time)
  192.         else:
  193.             context.finish(False, False, time)
  194.  
  195.     
  196.     def source_drag_data_get(self, btn, context, selection_data, info, time):
  197.         if info == TARGET_ROOTWIN:
  198.             print 'I was dropped on the rootwin'
  199.         else:
  200.             selection_data.set(selection_data.target, 8, "I'm Data!")
  201.  
  202.     
  203.     def source_drag_data_delete(self, btn, context, data):
  204.         print 'Delete the data!'
  205.  
  206.  
  207.  
  208. def main():
  209.     DragAndDropDemo()
  210.     gtk.main()
  211.  
  212. if __name__ == '__main__':
  213.     main()
  214.  
  215.