/usr/share/doc/python-gtk2-tutorial/html/sec-DNDMethods.html is in python-gtk2-tutorial 2.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>22.3. DND Methods</title><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="PyGTK 2.0 Tutorial"><link rel="up" href="ch-DragAndDrop.html" title="Chapter 22. Drag-and-drop (DND)"><link rel="previous" href="sec-DNDProperties.html" title="22.2. DND Properties"><link rel="next" href="ch-GtkRcFiles.html" title="Chapter 23. GTK's rc Files"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">22.3. DND Methods</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="sec-DNDProperties.html">Prev</a> </td><th width="60%" align="center">Chapter 22. Drag-and-drop (DND)</th><td width="20%" align="right"> <a accesskey="n" href="ch-GtkRcFiles.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec-DNDMethods"></a>22.3. DND Methods</h2></div></div><div></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id3154462"></a>22.3.1. Setting Up the Source Widget</h3></div></div><div></div></div><p>The method <tt class="methodname">drag_source_set</tt>() specifies
a set of target types for a drag operation on a widget.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
widget.drag_source_set(<b class="parameter"><tt>start_button_mask</tt></b>, <b class="parameter"><tt>targets</tt></b>, <b class="parameter"><tt>actions</tt></b>)
</pre></td></tr></table><p>The parameters signify the following:</p><div class="itemizedlist"><ul type="disc"><li><p><i class="parameter"><tt>widget</tt></i> specifies the drag source
widget</p></li><li><p><i class="parameter"><tt>start_button_mask</tt></i> specifies a bitmask
of buttons that can start the drag (e.g.
<tt class="literal">BUTTON1_MASK</tt>)</p></li><li><p><i class="parameter"><tt>targets</tt></i> specifies a list of target data
types the drag will support</p></li><li><p><i class="parameter"><tt>actions</tt></i> specifies a bitmask of possible
actions for a drag from this window</p></li></ul></div><p>The <i class="parameter"><tt>targets</tt></i> parameter is a list of tuples
each similar to:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
(target, flags, info)
</pre></td></tr></table><p><i class="parameter"><tt>target</tt></i> specifies a string representing the
drag type.</p><p><i class="parameter"><tt>flags</tt></i> restrict the drag scope.
<i class="parameter"><tt>flags</tt></i> can be set to 0 (no limitation of scope) or the
following flags:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
gtk.TARGET_SAME_APP # Target will only be selected for drags within a single application.
gtk.TARGET_SAME_WIDGET # Target will only be selected for drags within a single widget.
</pre></td></tr></table><p><i class="parameter"><tt>info</tt></i> is an application assigned integer
identifier.</p><p>If a widget is no longer required to act as a source for
drag-and-drop operations, the method
<tt class="methodname">drag_source_unset</tt>() can be used to remove a set of
drag-and-drop target types.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
widget.drag_source_unset()
</pre></td></tr></table></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-SignalsOnSourceWidget"></a>22.3.2. Signals On the Source Widget</h3></div></div><div></div></div><p>The source widget is sent the following signals during a
drag-and-drop operation.</p><div class="table"><a name="id3084387"></a><p class="title"><b>Table 22.1. Source Widget Signals</b></p><table summary="Source Widget Signals" width="100%" border="1"><colgroup><col><col></colgroup><tbody><tr><td>drag_begin</td><td>def drag_begin_cb(widget, drag_context, data):</td></tr><tr><td>drag_data_get</td><td>def drag_data_get_cb(widget, drag_context, selection_data, info, time, data):</td></tr><tr><td>drag_data_delete</td><td>def drag_data_delete_cb(widget, drag_context, data):</td></tr><tr><td>drag_end</td><td>def drag_end_cb(widget, drag_context, data):</td></tr></tbody></table></div><p>The "drag-begin" signal handler can be used to set up some
inital conditions such as a drag icon using one of the
<tt class="classname">Widget</tt> methods:
<tt class="methodname">drag_source_set_icon</tt>(),
<tt class="methodname">drag_source_set_icon_pixbuf</tt>(),
<tt class="methodname">drag_source_set_icon_stock</tt>(). The "drag-end' signal
handler can be used to undo the actions of the "drag-begin" signal
ahndler.</p><p>The "drag-data-get" signal handler should return the drag data
matching the target specified by <i class="parameter"><tt>info</tt></i>. It fills in
the <tt class="classname">gtk.gdk.SelectionData</tt> with the drag data.</p><p>The "drag-delete" signal handler is used to delete the drag data
for a <tt class="literal">gtk.gdk.ACTION_MOVE</tt> action after the data has been
copied.</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-SettingUpDestinationWidget"></a>22.3.3. Setting Up a Destination Widget</h3></div></div><div></div></div><p>The <tt class="methodname">drag_dest_set</tt>() method specifies
that this widget can receive drops and specifies what types of drops it can
receive.</p><p><tt class="methodname">drag_dest_unset</tt>() specifies that the
widget can no longer receive drops.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
widget.drag_dest_set(<b class="parameter"><tt>flags</tt></b>, <b class="parameter"><tt>targets</tt></b>, <b class="parameter"><tt>actions</tt></b>)
widget.drag_dest_unset()
</pre></td></tr></table><p><i class="parameter"><tt>flags</tt></i> specifies what actions GTK+ should
take on behalf of widget for drops on it. The possible values of
<i class="parameter"><tt>flags</tt></i> are:</p><table border="0" width="100%" bgcolor="#FFECCE"><col align="left" valign="top" width="0*"><tbody><tr><td><span class="term"><tt class="literal">gtk.DEST_DEFAULT_MOTION</tt></span></td><td><p>If set for a widget, GTK+, during a drag over this widget
will check if the drag matches this widget's list of possible targets and
actions. GTK+ will then call <tt class="methodname">drag_status</tt>() as
appropriate.</p></td></tr><tr><td><span class="term"><tt class="literal">gtk.DEST_DEFAULT_HIGHLIGHT</tt></span></td><td><p>If set for a widget, GTK+ will draw a highlight on this
widget as long as a drag is over this widget and the widget drag format and
action is acceptable.</p></td></tr><tr><td><span class="term"><tt class="literal">gtk.DEST_DEFAULT_DROP</tt></span></td><td><p>If set for a widget, when a drop occurs, GTK+ will check
if the drag matches this widget's list of possible targets and actions. If
so, GTK+ will call <tt class="methodname">drag_get_data</tt>() on behalf of the
widget. Whether or not the drop is succesful, GTK+ will call
<tt class="methodname">drag_finish</tt>(). If the action was a move and the
drag was succesful, then <tt class="literal">TRUE</tt> will be passed for the
<i class="parameter"><tt>delete</tt></i> parameter to
<tt class="methodname">drag_finish</tt>().</p></td></tr><tr><td><span class="term"><tt class="literal">gtk.DEST_DEFAULT_ALL</tt></span></td><td><p>If set, specifies that all default actions should be
taken.</p></td></tr></tbody></table><p><i class="parameter"><tt>targets</tt></i> is a list of target information
tuples as described above.</p><p><i class="parameter"><tt>actions</tt></i> is a bitmask of possible actions
for a drag onto this widget. The possible values that can be or'd for
actions are:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
gtk.gdk.ACTION_DEFAULT
gtk.gdk.ACTION_COPY
gtk.gdk.ACTION_MOVE
gtk.gdk.ACTION_LINK
gtk.gdk.ACTION_PRIVATE
gtk.gdk.ACTION_ASK
</pre></td></tr></table><p><i class="parameter"><tt>targets</tt></i> and
<i class="parameter"><tt>actions</tt></i> are ignored if <i class="parameter"><tt>flags</tt></i>
does not contain <tt class="literal">gtk.DEST_DEFAULT_MOTION</tt> or
<tt class="literal">gtk.DEST_DEFAULT_DROP</tt>. In that case the application must
handle the "drag-motion" and "drag-drop" signals.</p><p>The "drag-motion" handler must determine if the drag data is
appropriate by matching the destination targets with the
<tt class="classname">gtk.gdk.DragContext</tt>
targets and optionally by examining the drag data by calling the
<tt class="methodname">drag_get_data</tt>() method. The
<tt class="classname">gtk.gdk.DragContext</tt>.
<tt class="methodname">drag_status</tt>() method must be called to update the
<i class="parameter"><tt>drag_context</tt></i> status.</p><p>The "drag-drop" handler must determine the matching target using
the Widget <tt class="methodname">drag_dest_find_target</tt>() method and then
ask for the drag data using the Widget
<tt class="methodname">drag_get_data</tt>() method. The data will be available
in the "drag-data-received" handler.</p><p>The <a href="examples/dragtargets.py" target="_top"><tt class="filename">dragtargets.py</tt></a>
program prints out the targets of a drag operation in a label:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
1 #!/usr/local/env python
2
3 import pygtk
4 pygtk.require('2.0')
5 import gtk
6
7 def motion_cb(wid, context, x, y, time):
8 context.drag_status(gtk.gdk.ACTION_COPY, time)
9 return True
10
11 def drop_cb(wid, context, x, y, time):
12 l.set_text('\n'.join([str(t) for t in context.targets]))
13 return True
14
15 w = gtk.Window()
16 w.set_size_request(200, 150)
17 w.drag_dest_set(0, [], 0)
18 w.connect('drag_motion', motion_cb)
19 w.connect('drag_drop', drop_cb)
20 w.connect('destroy', lambda w: gtk.main_quit())
21 l = gtk.Label()
22 w.add(l)
23 w.show_all()
24
25 gtk.main()
</pre></td></tr></table><p>The program creates a window and then sets it as a drag
destination for no targets and actions by setting the flags to zero. The
<tt class="function">motion_cb</tt>() and <tt class="function">drop_cb</tt>() handlers
are connected to the "drag-motion" and "drag-drop" signals respectively. The
<tt class="function">motion_cb</tt>() handler just sets the drag status for the
drag context so that a drop will be enabled. The
<tt class="function">drop_cb</tt>() sets the label text to a string containing
the drag targets and ignores the data so the drop never completes.</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-SignalsOnDestinationWidget"></a>22.3.4. Signals On the Destination Widget</h3></div></div><div></div></div><p>The destination widget is sent the following signals during a
drag-and-drop operation.</p><div class="table"><a name="id3088434"></a><p class="title"><b>Table 22.2. Destination Widget Signals</b></p><table summary="Destination Widget Signals" width="100%" border="1"><colgroup><col><col></colgroup><tbody><tr><td>drag_motion</td><td>def drag_motion_cb(widget, drag_context, x, y, time, data):</td></tr><tr><td>drag_drop</td><td>def drag_drop_cb(widget, drag_context, x, y, time, data):</td></tr><tr><td>drag_data_received</td><td>def drag_data_received_cb(widget, drag_context,
x, y, selection_data, info, time, data):</td></tr></tbody></table></div><p>The <a href="examples/dragndrop.py" target="_top"><span><b class="command">dragndrop.py</b></span></a> example
program demonstrates the use of drag and drop in one application. A button
with a xpm pixmap (in <a href="examples/gtkxpm.py" target="_top"><tt class="filename">gtkxpm.py</tt></a>) is the
source for the drag; it provides both text and xpm data. A layout widget is
the destination for the xpm drop while a button is the destination for the
text drop. <a href="sec-DNDMethods.html#dragndropfig" title="Figure 22.1. Drag and Drop Example">Figure 22.1, “Drag and Drop Example”</a> illustrates the program display
after an xpm drop has been made on the layout and a text drop has been made
on the button:</p><div class="figure"><a name="dragndropfig"></a><p class="title"><b>Figure 22.1. Drag and Drop Example</b></p><div class="mediaobject" align="center"><img src="figures/dragndrop.png" align="middle" alt="Drag and Drop Example"></div></div><p>The <a href="examples/dragndrop.py" target="_top"><span><b class="command">dragndrop.py</b></span></a> source
code is:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="programlisting">
1 #!/usr/bin/env python
2
3 # example dragndrop.py
4
5 import pygtk
6 pygtk.require('2.0')
7 import gtk
8 import string, time
9
10 import gtkxpm
11
12 class DragNDropExample:
13 HEIGHT = 600
14 WIDTH = 600
15 TARGET_TYPE_TEXT = 80
16 TARGET_TYPE_PIXMAP = 81
17 fromImage = [ ( "text/plain", 0, TARGET_TYPE_TEXT ),
18 ( "image/x-xpixmap", 0, TARGET_TYPE_PIXMAP ) ]
19 toButton = [ ( "text/plain", 0, TARGET_TYPE_TEXT ) ]
20 toCanvas = [ ( "image/x-xpixmap", 0, TARGET_TYPE_PIXMAP ) ]
21
22 def layout_resize(self, widget, event):
23 x, y, width, height = widget.get_allocation()
24 if width > self.lwidth or height > self.lheight:
25 self.lwidth = max(width, self.lwidth)
26 self.lheight = max(height, self.lheight)
27 widget.set_size(self.lwidth, self.lheight)
28
29 def makeLayout(self):
30 self.lwidth = self.WIDTH
31 self.lheight = self.HEIGHT
32 box = gtk.VBox(False,0)
33 box.show()
34 table = gtk.Table(2, 2, False)
35 table.show()
36 box.pack_start(table, True, True, 0)
37 layout = gtk.Layout()
38 self.layout = layout
39 layout.set_size(self.lwidth, self.lheight)
40 layout.connect("size-allocate", self.layout_resize)
41 layout.show()
42 table.attach(layout, 0, 1, 0, 1, gtk.FILL|gtk.EXPAND,
43 gtk.FILL|gtk.EXPAND, 0, 0)
44 # create the scrollbars and pack into the table
45 vScrollbar = gtk.VScrollbar(None)
46 vScrollbar.show()
47 table.attach(vScrollbar, 1, 2, 0, 1, gtk.FILL|gtk.SHRINK,
48 gtk.FILL|gtk.SHRINK, 0, 0)
49 hScrollbar = gtk.HScrollbar(None)
50 hScrollbar.show()
51 table.attach(hScrollbar, 0, 1, 1, 2, gtk.FILL|gtk.SHRINK,
52 gtk.FILL|gtk.SHRINK,
53 0, 0)
54 # tell the scrollbars to use the layout widget's adjustments
55 vAdjust = layout.get_vadjustment()
56 vScrollbar.set_adjustment(vAdjust)
57 hAdjust = layout.get_hadjustment()
58 hScrollbar.set_adjustment(hAdjust)
59 layout.connect("drag_data_received", self.receiveCallback)
60 layout.drag_dest_set(gtk.DEST_DEFAULT_MOTION |
61 gtk.DEST_DEFAULT_HIGHLIGHT |
62 gtk.DEST_DEFAULT_DROP,
63 self.toCanvas, gtk.gdk.ACTION_COPY)
64 self.addImage(gtkxpm.gtk_xpm, 0, 0)
65 button = gtk.Button("Text Target")
66 button.show()
67 button.connect("drag_data_received", self.receiveCallback)
68 button.drag_dest_set(gtk.DEST_DEFAULT_MOTION |
69 gtk.DEST_DEFAULT_HIGHLIGHT |
70 gtk.DEST_DEFAULT_DROP,
71 self.toButton, gtk.gdk.ACTION_COPY)
72 box.pack_start(button, False, False, 0)
73 return box
74
75 def addImage(self, xpm, xd, yd):
76 hadj = self.layout.get_hadjustment()
77 vadj = self.layout.get_vadjustment()
78 style = self.window.get_style()
79 pixmap, mask = gtk.gdk.pixmap_create_from_xpm_d(
80 self.window.window, style.bg[gtk.STATE_NORMAL], xpm)
81 image = gtk.Image()
82 image.set_from_pixmap(pixmap, mask)
83 button = gtk.Button()
84 button.add(image)
85 button.connect("drag_data_get", self.sendCallback)
86 button.drag_source_set(gtk.gdk.BUTTON1_MASK, self.fromImage,
87 gtk.gdk.ACTION_COPY)
88 button.show_all()
89 # have to adjust for the scrolling of the layout - event location
90 # is relative to the viewable not the layout size
91 self.layout.put(button, int(xd+hadj.value), int(yd+vadj.value))
92 return
93
94 def sendCallback(self, widget, context, selection, targetType, eventTime):
95 if targetType == self.TARGET_TYPE_TEXT:
96 now = time.time()
97 str = time.ctime(now)
98 selection.set(selection.target, 8, str)
99 elif targetType == self.TARGET_TYPE_PIXMAP:
100 selection.set(selection.target, 8,
101 string.join(gtkxpm.gtk_xpm, '\n'))
102
103 def receiveCallback(self, widget, context, x, y, selection, targetType,
104 time):
105 if targetType == self.TARGET_TYPE_TEXT:
106 label = widget.get_children()[0]
107 label.set_text(selection.data)
108 elif targetType == self.TARGET_TYPE_PIXMAP:
109 self.addImage(string.split(selection.data, '\n'), x, y)
110
111 def __init__(self):
112 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
113 self.window.set_default_size(300, 300)
114 self.window.connect("destroy", lambda w: gtk.main_quit())
115 self.window.show()
116 layout = self.makeLayout()
117 self.window.add(layout)
118
119 def main():
120 gtk.main()
121
122 if __name__ == "__main__":
123 DragNDropExample()
124 main()
</pre></td></tr></table></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sec-DNDProperties.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch-DragAndDrop.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch-GtkRcFiles.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">22.2. DND Properties </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 23. GTK's rc Files</td></tr></table></div></body></html>
|