1 | #!/usr/bin/env python |
---|
2 | # Copyright (C) 2011, One Laptop Per Child |
---|
3 | # Author, Gonzalo Odiard <gonzalo@laptop.org> |
---|
4 | # Translated from c demo provided by Carlos Garnacho <carlos@lanedo.com> |
---|
5 | |
---|
6 | from gi.repository import Gtk |
---|
7 | from gi.repository import Gdk |
---|
8 | import math |
---|
9 | import logging |
---|
10 | |
---|
11 | class TestTouch(Gtk.DrawingArea): |
---|
12 | |
---|
13 | def __init__(self): |
---|
14 | self.touches = {} |
---|
15 | super(TestTouch, self).__init__() |
---|
16 | self.set_events(Gdk.EventMask.TOUCH_MASK) |
---|
17 | self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK) |
---|
18 | self.add_events(Gdk.EventMask.BUTTON_RELEASE_MASK) |
---|
19 | self.add_events(Gdk.EventMask.BUTTON_MOTION_MASK) |
---|
20 | self.connect('draw', self.__draw_cb) |
---|
21 | self.connect('event', self.__event_cb) |
---|
22 | |
---|
23 | def __event_cb(self, widget, event): |
---|
24 | logging.error('touch event type %s', event.type) |
---|
25 | if event.type in (Gdk.EventType.TOUCH_BEGIN, |
---|
26 | Gdk.EventType.TOUCH_CANCEL, Gdk.EventType.TOUCH_END, |
---|
27 | Gdk.EventType.TOUCH_UPDATE, Gdk.EventType.BUTTON_PRESS, |
---|
28 | Gdk.EventType.BUTTON_RELEASE, Gdk.EventType.MOTION_NOTIFY): |
---|
29 | x = event.get_coords()[1] |
---|
30 | y = event.get_coords()[2] |
---|
31 | seq = str(event.touch.sequence) |
---|
32 | |
---|
33 | if event.type in (Gdk.EventType.TOUCH_BEGIN, |
---|
34 | Gdk.EventType.TOUCH_UPDATE, Gdk.EventType.BUTTON_PRESS, |
---|
35 | Gdk.EventType.MOTION_NOTIFY): |
---|
36 | self.touches[seq] = (x, y) |
---|
37 | elif event.type in (Gdk.EventType.TOUCH_END, |
---|
38 | Gdk.EventType.BUTTON_RELEASE): |
---|
39 | del self.touches[seq] |
---|
40 | self.queue_draw() |
---|
41 | |
---|
42 | def __draw_cb(self, widget, ctx): |
---|
43 | ctx.set_source_rgba(0.3, 0.3, 0.3, 0.7) |
---|
44 | for touch in self.touches.values(): |
---|
45 | x, y = touch |
---|
46 | ctx.save() |
---|
47 | ctx.arc(x, y, 50, 0., 2 * math.pi) |
---|
48 | ctx.fill() |
---|
49 | ctx.restore() |
---|
50 | |
---|
51 | |
---|
52 | def main(): |
---|
53 | window = Gtk.Window() |
---|
54 | test_touch = TestTouch() |
---|
55 | hbox = Gtk.HBox() |
---|
56 | scrolled = Gtk.ScrolledWindow() |
---|
57 | |
---|
58 | # if kinetic scrolling is disabled the problem disappear |
---|
59 | # but the scrolledwindow is not touch friendly |
---|
60 | #scrolled.set_kinetic_scrolling(False) |
---|
61 | |
---|
62 | store = Gtk.ListStore(str) |
---|
63 | for i in range(50): |
---|
64 | store.append(['Test %d' % i]) |
---|
65 | tview = Gtk.TreeView(store) |
---|
66 | renderer = Gtk.CellRendererText() |
---|
67 | column = Gtk.TreeViewColumn("Title", renderer, text=0) |
---|
68 | tview.append_column(column) |
---|
69 | scrolled.add(tview) |
---|
70 | window.add(hbox) |
---|
71 | hbox.add(scrolled) |
---|
72 | hbox.add(test_touch) |
---|
73 | |
---|
74 | window.connect("destroy", Gtk.main_quit) |
---|
75 | window.set_default_size(700, 500) |
---|
76 | window.show_all() |
---|
77 | Gtk.main() |
---|
78 | |
---|
79 | if __name__ == "__main__": |
---|
80 | main() |
---|