Attachment 'drag-incorrect.py'
Download 1 import gtk
2
3 rect_x = 300
4 rect_y = 220
5 rect_size = 80
6 mouse_x = 0
7 mouse_y = 0
8
9 dragging = False
10
11 draw_area = None
12
13 def constrain_to_viewport ():
14 global rect_x, rect_y, draw_area
15
16 if rect_x < 0:
17 rect_x = 0
18 elif rect_x + rect_size >= draw_area.allocation.width:
19 rect_x = draw_area.allocation.width - rect_size
20
21 if rect_y < 0:
22 rect_y = 0
23 elif rect_y + rect_size >= draw_area.allocation.height:
24 rect_y = draw_area.allocation.height - rect_size
25
26 def expose_event_cb (widget, event):
27 cr = widget.window.cairo_create ()
28 cr.set_source_rgb (1, 1, 1)
29
30 cr.rectangle (0, 0, widget.allocation.width, widget.allocation.height)
31 cr.fill ()
32
33 cr.set_source_rgb (1, 0, 0)
34 cr.rectangle (rect_x, rect_y, rect_size, rect_size)
35 cr.fill ()
36
37 return False
38
39 def button_press_event_cb (widget, event):
40 global dragging, draw_area, rect_x, rect_y, mouse_x, mouse_y
41
42 if (dragging or event.button != 1
43 or event.x < rect_x or event.x >= rect_x + rect_size
44 or event.y < rect_y or event.y >= rect_y + rect_size):
45 return True
46
47 dragging = True
48 mouse_x = event.x
49 mouse_y = event.y
50
51 return True
52
53 def button_release_event_cb (widget, event):
54 global dragging, draw_area, rect_x, rect_y, mouse_x, mouse_y
55
56 if not dragging:
57 return True
58
59 if event.button != 1:
60 return True
61
62 dragging = False
63 return True
64
65 def motion_notify_event_cb (widget, event):
66 global dragging, draw_area, rect_x, rect_y, mouse_x, mouse_y
67
68 if not dragging:
69 return True
70
71 rect_x = rect_x + event.x - mouse_x
72 rect_y = rect_y + event.y - mouse_y
73 constrain_to_viewport ()
74
75 mouse_x = event.x
76 mouse_y = event.y
77
78 draw_area.queue_draw ()
79
80 return True
81
82 window = gtk.Window (gtk.WINDOW_TOPLEVEL)
83
84 draw_area = gtk.DrawingArea ()
85 draw_area.set_size_request (640, 480)
86 draw_area.add_events (gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK)
87 draw_area.connect ("expose-event", expose_event_cb)
88 draw_area.connect ("button-press-event", button_press_event_cb)
89 draw_area.connect ("button-release-event", button_release_event_cb)
90 draw_area.connect ("motion-notify-event", motion_notify_event_cb)
91
92 window.add (draw_area)
93
94 window.show_all ()
95
96 gtk.main ()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.