~bennabiy/+junk/python-xlib

« back to all changes in this revision

Viewing changes to .pc/fix-rhomboid-examples.patch/examples/draw-proto.py

  • Committer: Package Import Robot
  • Author(s): Andrew Shadura, Ramkumar Ramachandra, Andrew Shadura
  • Date: 2015-08-13 08:14:19 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20150813081419-hdefinnghp2iydkx
Tags: 0.14+20091101-3
[ Ramkumar Ramachandra ]
* Remove useless debugging output (Closes: #565996)

[ Andrew Shadura ]
* Switch to 3.0 (quilt) format.
* Rename patches.
* Use debhelper 9 in its short form.
* Use pybuild.
* Bump Standards-Version.
* Don't build or install PostScript documentation and info files.
* Use system-provided texi2html instead of a shipped version
  (Closes: #795057).
* Update debian/copyright (Closes: #795057).
* Don't install Makefile or texi2html with the documentation.
* Set executable bit for examples.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
# examples/draw.py -- protocol test application.
 
4
#
 
5
#    Copyright (C) 2000 Peter Liljenberg <petli@ctrl-c.liu.se>
 
6
#
 
7
#    This program is free software; you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License as published by
 
9
#    the Free Software Foundation; either version 2 of the License, or
 
10
#    (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU General Public License
 
18
#    along with this program; if not, write to the Free Software
 
19
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 
 
21
 
 
22
import sys
 
23
import os
 
24
 
 
25
# Change path so we find Xlib
 
26
sys.path.insert(1, os.path.join(sys.path[0], '..'))
 
27
 
 
28
from Xlib import X
 
29
from Xlib.protocol import display
 
30
from Xlib.protocol.request import *
 
31
 
 
32
# Application window (only one)
 
33
class Window:
 
34
    def __init__(self, display):
 
35
        self.d = display
 
36
        self.objects = []
 
37
 
 
38
        # Find which screen to open the window on
 
39
        self.screen = self.d.info.roots[self.d.default_screen]
 
40
 
 
41
        # Allocate ids to the window and gc
 
42
        self.window = self.d.allocate_resource_id()
 
43
        self.gc = self.d.allocate_resource_id()
 
44
 
 
45
        # Create a window
 
46
        CreateWindow(self.d, None,
 
47
                     self.screen.root_depth,
 
48
                     self.window,
 
49
                     self.screen.root,
 
50
                     50, 50, 300, 200, 2,
 
51
                     X.InputOutput,
 
52
                     X.CopyFromParent,
 
53
 
 
54
                     # special attribute values
 
55
                     background_pixel = self.screen.white_pixel,
 
56
                     event_mask = (X.ExposureMask |
 
57
                                   X.StructureNotifyMask |
 
58
                                   X.ButtonPressMask |
 
59
                                   X.ButtonReleaseMask |
 
60
                                   X.Button1MotionMask),
 
61
                     colormap = X.CopyFromParent)
 
62
 
 
63
        # Create a gc for drawing
 
64
        CreateGC(self.d, None,
 
65
                 self.gc,
 
66
                 self.window,
 
67
 
 
68
                 # special attribute values
 
69
                 foreground = self.screen.black_pixel,
 
70
                 background = self.screen.white_pixel)
 
71
 
 
72
        # Map the window, making it visible
 
73
        MapWindow(self.d, None, self.window)
 
74
 
 
75
    # Main loop, handling events
 
76
 
 
77
    def loop(self):
 
78
        current = None
 
79
        while 1:
 
80
            e = self.d.next_event()
 
81
 
 
82
            # Window has been destroyed, quit
 
83
            if e.type == X.DestroyNotify:
 
84
                sys.exit(0)
 
85
 
 
86
            # Some part of the window has been exposed,
 
87
            # redraw all the objects.
 
88
            if e.type == X.Expose:
 
89
                for o in self.objects:
 
90
                    o.expose(e)
 
91
 
 
92
            # Left button pressed, start to draw
 
93
            if e.type == X.ButtonPress and e.detail == 1:
 
94
                current = Movement(self, e)
 
95
                self.objects.append(current)
 
96
 
 
97
            # Left button released, finish drawing
 
98
            if e.type == X.ButtonRelease and e.detail == 1 and current:
 
99
                current.finish(e)
 
100
                current = None
 
101
 
 
102
            # Mouse movement with button pressed, draw
 
103
            if e.type == X.MotionNotify and current:
 
104
                current.motion(e)
 
105
 
 
106
# A drawed objects, consisting of either a single
 
107
# romboid, or two romboids connected by a winding line
 
108
 
 
109
class Movement:
 
110
    def __init__(self, win, ev):
 
111
        self.win = win
 
112
 
 
113
        self.left = ev.event_x - 5
 
114
        self.right = ev.event_x + 5
 
115
        self.top = ev.event_y - 5
 
116
        self.bottom = ev.event_y + 5
 
117
 
 
118
        self.time = ev.time
 
119
        self.lines = [(ev.event_x, ev.event_y)]
 
120
 
 
121
        self.first = Romboid(self.win, ev)
 
122
        self.last = None
 
123
 
 
124
    def motion(self, ev):
 
125
        # Find all the mouse coordinates since the
 
126
        # last event received
 
127
 
 
128
        r = GetMotionEvents(self.win.d,
 
129
                            window = self.win.window,
 
130
                            start = self.time,
 
131
                            stop = ev.time)
 
132
 
 
133
        # Record the previous last coordinate, and append
 
134
        # the new coordinates
 
135
 
 
136
        firstline = len(self.lines) - 1
 
137
 
 
138
        if r.events:
 
139
            # Discard the first coordinate if that is identical to
 
140
            # the last recorded coordinate
 
141
 
 
142
            pos = r.events[0]
 
143
            if (pos.x, pos.y) == self.lines[-1]:
 
144
                events = r.events[1:]
 
145
            else:
 
146
                events = r.events
 
147
 
 
148
            # Append all coordinates
 
149
            for pos in events:
 
150
                x = pos.x
 
151
                y = pos.y
 
152
 
 
153
                if x < self.left:
 
154
                    self.left = x
 
155
                if x > self.right:
 
156
                    self.right = x
 
157
 
 
158
                if y < self.top:
 
159
                    self.top = y
 
160
                if y > self.bottom:
 
161
                    self.bottom = y
 
162
 
 
163
                self.lines.append((x, y))
 
164
 
 
165
        # Append the event coordinate, if that is different from the
 
166
        # last movement coordinate
 
167
        if (ev.event_x, ev.event_y) != self.lines[-1]:
 
168
            self.lines.append((ev.event_x, ev.event_y))
 
169
 
 
170
        # Draw a line between the new coordinates
 
171
        PolyLine(self.win.d, None,
 
172
                 X.CoordModeOrigin,
 
173
                 self.win.window,
 
174
                 self.win.gc,
 
175
                 self.lines[firstline:])
 
176
 
 
177
        self.time = ev.time
 
178
 
 
179
    def finish(self, ev):
 
180
        self.motion(ev)
 
181
        if len(self.lines) > 1:
 
182
            self.last = Romboid(self.win, ev)
 
183
 
 
184
            self.left = min(ev.event_x - 5, self.left)
 
185
            self.right = max(ev.event_x + 5, self.right)
 
186
            self.top = min(ev.event_y - 5, self.top)
 
187
            self.bottom = max(ev.event_y + 5, self.bottom)
 
188
 
 
189
    def expose(self, ev):
 
190
        # We should check if this object is in the exposed
 
191
        # area, but I can't be bothered right now, so just
 
192
        # redraw on the last Expose in every batch
 
193
 
 
194
        if ev.count == 0:
 
195
            self.first.draw()
 
196
            if self.last:
 
197
                # Redraw all the lines
 
198
                PolyLine(self.win.d, None,
 
199
                         X.CoordModeOrigin,
 
200
                         self.win.window,
 
201
                         self.win.gc,
 
202
                         self.lines)
 
203
                self.last.draw()
 
204
 
 
205
 
 
206
# A romboid, drawed around the Movement endpoints
 
207
class Romboid:
 
208
    def __init__(self, win, ev):
 
209
        self.win = win
 
210
        self.x = ev.event_x
 
211
        self.y = ev.event_y
 
212
        self.draw()
 
213
 
 
214
    def draw(self):
 
215
        # Draw the segments of the romboid
 
216
        PolyLine(self.win.d, None,
 
217
                 X.CoordModePrevious,
 
218
                 self.win.window,
 
219
                 self.win.gc,
 
220
                 [(self.x, self.y - 5),
 
221
                  (5, 5),
 
222
                  (-5, 5),
 
223
                  (-5, -5),
 
224
                  (5, -5)])
 
225
 
 
226
 
 
227
if __name__ == '__main__':
 
228
    Window(display.Display()).loop()