1 from string
import rfind
14 def icon_for_type(window
, media
, subtype
):
15 '''Search <Choices> for a suitable icon. Returns (pixmap, mask) '''
16 path
= choices
.load('MIME-icons', media
+ '_' + subtype
+ '.xpm')
18 path
= choices
.load('MIME-icons', media
+ '.xpm')
20 p
, m
= load_pixmap(window
, path
)
24 p
, m
= load_pixmap(window
, __main__
.app_dir
+ '/icons/File.xpm')
27 def write_xds_property(context
, value
):
28 XdndDirectSave
= _gtk
.gdk_atom_intern('XdndDirectSave0', FALSE
)
29 text_plain
= _gtk
.gdk_atom_intern('text/plain', FALSE
)
30 win
= context
.source_window
32 win
.property_change(XdndDirectSave
, text_plain
, 8,
36 win
.property_delete(XdndDirectSave
)
38 def read_xds_property(context
, delete
):
39 XdndDirectSave
= _gtk
.gdk_atom_intern('XdndDirectSave0', FALSE
)
40 text_plain
= _gtk
.gdk_atom_intern('text/plain', FALSE
)
41 win
= context
.source_window
42 retval
= win
.property_get(XdndDirectSave
, text_plain
, delete
)
47 # 'window' should have the following methods/attribs:
49 # uri - the initial pathname to use
50 # save_as(path) - write data to file, TRUE on success
51 # set_uri(uri) - data is safely saved to this location
52 # send_raw(selection_data) - write data to selection
53 # (if missing, data can only be saved to the filesystem)
54 # discard() - discard button clicked
55 # (only needed if discard = TRUE)
57 class SaveBox(GtkWindow
):
58 def __init__(self
, window
, media
, subtype
, discard
= FALSE
):
59 GtkWindow
.__init
__(self
, WINDOW_DIALOG
)
60 self
.discard
= discard
61 self
.set_title('Save As:')
62 self
.set_position(WIN_POS_MOUSE
)
63 self
.set_border_width(4)
66 vbox
= GtkVBox(FALSE
, 0)
69 align
= GtkAlignment()
70 align
.set(.5, .5, 0, 0)
71 vbox
.pack_start(align
, TRUE
, TRUE
, 0)
73 drag_box
= GtkEventBox()
74 drag_box
.set_border_width(4)
75 drag_box
.add_events(BUTTON_PRESS_MASK
)
78 pixmap
, mask
= icon_for_type(self
, media
, subtype
)
79 self
.icon
= GtkPixmap(pixmap
, mask
)
81 if (hasattr(window
, 'send_raw')):
82 target
= [('XdndDirectSave0', 0, TARGET_XDS
),
83 ('%s/%s' % (media
, subtype
), 0, TARGET_RAW
),
84 ('application/octet-stream', 0, TARGET_RAW
)
87 target
= [('XdndDirectSave0', 0, TARGET_XDS
)]
89 drag_box
.drag_source_set(BUTTON1_MASK | BUTTON3_MASK
,
91 ACTION_COPY | ACTION_MOVE
)
92 drag_box
.connect('drag_begin', self
.drag_begin
)
93 drag_box
.connect('drag_data_get', self
.drag_data_get
)
95 drag_box
.add(self
.icon
)
99 vbox
.pack_start(entry
, FALSE
, TRUE
, 4)
101 hbox
= GtkHBox(TRUE
, 0)
102 vbox
.pack_start(hbox
, FALSE
, TRUE
, 0)
104 ok
= GtkButton("Save")
105 ok
.set_flags(CAN_DEFAULT
)
106 hbox
.pack_start(ok
, FALSE
, TRUE
, 0)
108 cancel
= GtkButton("Cancel")
109 cancel
.set_flags(CAN_DEFAULT
)
110 hbox
.pack_start(cancel
, FALSE
, TRUE
, 0)
111 cancel
.connect('clicked', self
.cancel
)
114 vbox
.pack_start(GtkHSeparator(), FALSE
, TRUE
, 4)
115 button
= GtkButton('Discard')
116 vbox
.pack_start(button
, FALSE
, TRUE
, 0)
117 button
.connect('clicked', self
.discard_clicked
)
121 ok
.connect('clicked', self
.ok
, entry
)
124 entry
.connect('activate', self
.ok
, entry
)
126 entry
.set_text(window
.uri
)
127 i
= rfind(window
.uri
, '/')
131 entry
.set_position(-1)
132 entry
.select_region(i
, -1)
134 def cancel(self
, widget
):
137 def ok(self
, widget
, entry
):
138 uri
= entry
.get_text()
139 path
= get_local_path(uri
)
142 if self
.window
.save_as(path
):
143 self
.window
.set_uri(path
)
148 report_error("Drag the icon to a directory viewer\n" +
149 "(or enter a full pathname)",
152 def drag_begin(self
, drag_box
, context
):
155 p
, m
= self
.icon
.get()
156 drag_box
.drag_source_set_icon(self
.icon
.get_colormap(), p
, m
)
158 uri
= self
.entry
.get_text()
167 write_xds_property(context
, leaf
)
169 def drag_data_get(self
, widget
, context
, selection_data
, info
, time
):
170 if info
== TARGET_RAW
:
171 self
.window
.send_raw(selection_data
)
173 write_xds_property(context
, None)
178 elif info
!= TARGET_XDS
:
179 write_xds_property(context
, None)
180 report_error("Bad target requested!")
185 # Get the path that the destination app wants us to save to.
186 # If it's local, save and return Success
187 # (or Error if save fails)
188 # If it's remote, return Failure (remote may try another method)
189 # If no URI is given, return Error
191 uri
= read_xds_property(context
, FALSE
)
193 path
= get_local_path(uri
)
195 self
.data_sent
= self
.window
.save_as(path
)
200 to_send
= 'F' # Non-local transfer
202 report_error("Remote application wants to use " +
203 "Direct Save, but I can't read the " +
204 "XdndDirectSave0 (type text/plain) " +
207 selection_data
.set(selection_data
.target
, 8, to_send
)
210 write_xds_property(context
, None)
211 path
= get_local_path(uri
)
213 self
.window
.set_uri(path
)
215 self
.window
.set_uri(uri
)
221 def discard_clicked(self
, event
):
222 self
.window
.discard()