#!/usr/bin/env python # example setselection.py import gtk import GDK import time class SetSelectionExample: SELECTION_PRIMARY = 1 CURRENT_TIME = 0 # Callback when the user toggles the selection def selection_toggled(self, widget): if widget.active: self.have_selection = widget.selection_owner_set( self.SELECTION_PRIMARY, self.CURRENT_TIME) # if claiming the selection failed, we return the button to # the out state if not self.have_selection: widget.set_active(gtk.FALSE) else: if self.have_selection: # Not possible to release the selection in PyGTK # just mark that we don't have it self.have_selection = gtk.FALSE return # Called when another application claims the selection def selection_clear(self, widget, event): self.have_selection = gtk.FALSE widget.set_active(gtk.FALSE) return gtk.TRUE # Supplies the current time as the selection. def selection_handle(self, widget, selection_data, info, time_stamp): current_time = time.time() timestr = time.asctime(time.localtime(current_time)) # When we return a single string, it should not be null terminated. # That will be done for us selection_data.set(GDK.SELECTION_TYPE_STRING,8, timestr) return def __init__(self): self.have_selection = gtk.FALSE # Create the toplevel window window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL) window.set_title("Set Selection") window.set_border_width(10) window.connect("destroy", gtk.mainquit) # Create a toggle button to act as the selection selection_button = gtk.GtkToggleButton("Claim Selection") window.add(selection_button) selection_button.show() selection_button.connect("toggled", self.selection_toggled) selection_button.connect("selection_clear_event", self.selection_clear) selection_button.selection_add_target( self.SELECTION_PRIMARY, GDK.SELECTION_TYPE_STRING, 1) selection_button.connect("selection_get", self.selection_handle) selection_button.show() window.show() def main(): gtk.mainloop() return 0 if __name__ == "__main__": SetSelectionExample() main()