~bennabiy/+junk/python-xlib

« back to all changes in this revision

Viewing changes to .pc/fix-rhomboid-examples.patch/examples/draw.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 -- high-level xlib 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, display, Xutil
 
29
 
 
30
# Application window (only one)
 
31
class Window:
 
32
    def __init__(self, display):
 
33
        self.d = display
 
34
        self.objects = []
 
35
 
 
36
        # Find which screen to open the window on
 
37
        self.screen = self.d.screen()
 
38
 
 
39
        self.window = self.screen.root.create_window(
 
40
            50, 50, 300, 200, 2,
 
41
            self.screen.root_depth,
 
42
            X.InputOutput,
 
43
            X.CopyFromParent,
 
44
 
 
45
            # special attribute values
 
46
            background_pixel = self.screen.white_pixel,
 
47
            event_mask = (X.ExposureMask |
 
48
                          X.StructureNotifyMask |
 
49
                          X.ButtonPressMask |
 
50
                          X.ButtonReleaseMask |
 
51
                          X.Button1MotionMask),
 
52
            colormap = X.CopyFromParent,
 
53
            )
 
54
 
 
55
        self.gc = self.window.create_gc(
 
56
            foreground = self.screen.black_pixel,
 
57
            background = self.screen.white_pixel,
 
58
            )
 
59
 
 
60
        # Set some WM info
 
61
 
 
62
        self.WM_DELETE_WINDOW = self.d.intern_atom('WM_DELETE_WINDOW')
 
63
        self.WM_PROTOCOLS = self.d.intern_atom('WM_PROTOCOLS')
 
64
 
 
65
        self.window.set_wm_name('Xlib example: draw.py')
 
66
        self.window.set_wm_icon_name('draw.py')
 
67
        self.window.set_wm_class('draw', 'XlibExample')
 
68
 
 
69
        self.window.set_wm_protocols([self.WM_DELETE_WINDOW])
 
70
        self.window.set_wm_hints(flags = Xutil.StateHint,
 
71
                                 initial_state = Xutil.NormalState)
 
72
 
 
73
        self.window.set_wm_normal_hints(flags = (Xutil.PPosition | Xutil.PSize
 
74
                                                 | Xutil.PMinSize),
 
75
                                        min_width = 20,
 
76
                                        min_height = 20)
 
77
 
 
78
        # Map the window, making it visible
 
79
        self.window.map()
 
80
 
 
81
    # Main loop, handling events
 
82
    def loop(self):
 
83
        current = None
 
84
        while 1:
 
85
            e = self.d.next_event()
 
86
 
 
87
            # Window has been destroyed, quit
 
88
            if e.type == X.DestroyNotify:
 
89
                sys.exit(0)
 
90
 
 
91
            # Some part of the window has been exposed,
 
92
            # redraw all the objects.
 
93
            if e.type == X.Expose:
 
94
                for o in self.objects:
 
95
                    o.expose(e)
 
96
 
 
97
            # Left button pressed, start to draw
 
98
            if e.type == X.ButtonPress and e.detail == 1:
 
99
                current = Movement(self, e)
 
100
                self.objects.append(current)
 
101
 
 
102
            # Left button released, finish drawing
 
103
            if e.type == X.ButtonRelease and e.detail == 1 and current:
 
104
                current.finish(e)
 
105
                current = None
 
106
 
 
107
            # Mouse movement with button pressed, draw
 
108
            if e.type == X.MotionNotify and current:
 
109
                current.motion(e)
 
110
 
 
111
            if e.type == X.ClientMessage:
 
112
                if e.client_type == self.WM_PROTOCOLS:
 
113
                    fmt, data = e.data
 
114
                    if fmt == 32 and data[0] == self.WM_DELETE_WINDOW:
 
115
                        sys.exit(0)
 
116
 
 
117
# A drawed objects, consisting of either a single
 
118
# romboid, or two romboids connected by a winding line
 
119
 
 
120
class Movement:
 
121
    def __init__(self, win, ev):
 
122
        self.win = win
 
123
 
 
124
        self.left = ev.event_x - 5
 
125
        self.right = ev.event_x + 5
 
126
        self.top = ev.event_y - 5
 
127
        self.bottom = ev.event_y + 5
 
128
 
 
129
        self.time = ev.time
 
130
        self.lines = [(ev.event_x, ev.event_y)]
 
131
 
 
132
        self.first = Romboid(self.win, ev)
 
133
        self.last = None
 
134
 
 
135
    def motion(self, ev):
 
136
        # Find all the mouse coordinates since the
 
137
        # last event received
 
138
 
 
139
        events = self.win.window.get_motion_events(self.time, ev.time)
 
140
        self.time = ev.time
 
141
 
 
142
        # Record the previous last coordinate, and append
 
143
        # the new coordinates
 
144
        firstline = len(self.lines) - 1
 
145
 
 
146
        if events:
 
147
            # Discard the first coordinate if that is identical to
 
148
            # the last recorded coordinate
 
149
 
 
150
            pos = events[0]
 
151
            if (pos.x, pos.y) == self.lines[-1]:
 
152
                events = events[1:]
 
153
 
 
154
            # Append all coordinates
 
155
            for pos in events:
 
156
                x = pos.x
 
157
                y = pos.y
 
158
 
 
159
                if x < self.left:
 
160
                    self.left = x
 
161
                if x > self.right:
 
162
                    self.right = x
 
163
 
 
164
                if y < self.top:
 
165
                    self.top = y
 
166
                if y > self.bottom:
 
167
                    self.bottom = y
 
168
 
 
169
                self.lines.append((x, y))
 
170
 
 
171
        # Append the event coordinate, if that is different from the
 
172
        # last movement coordinate
 
173
        if (ev.event_x, ev.event_y) != self.lines[-1]:
 
174
            self.lines.append((ev.event_x, ev.event_y))
 
175
 
 
176
        # Draw a line between the new coordinates
 
177
        self.win.window.poly_line(self.win.gc,
 
178
                                  X.CoordModeOrigin,
 
179
                                  self.lines[firstline:])
 
180
 
 
181
 
 
182
    def finish(self, ev):
 
183
        self.motion(ev)
 
184
        if len(self.lines) > 1:
 
185
            self.last = Romboid(self.win, ev)
 
186
 
 
187
            self.left = min(ev.event_x - 5, self.left)
 
188
            self.right = max(ev.event_x + 5, self.right)
 
189
            self.top = min(ev.event_y - 5, self.top)
 
190
            self.bottom = max(ev.event_y + 5, self.bottom)
 
191
 
 
192
    def expose(self, ev):
 
193
        # We should check if this object is in the exposed
 
194
        # area, but I can't be bothered right now, so just
 
195
        # redraw on the last Expose in every batch
 
196
 
 
197
        if ev.count == 0:
 
198
            self.first.draw()
 
199
            if self.last:
 
200
                # Redraw all the lines
 
201
                self.win.window.poly_line(self.win.gc,
 
202
                                          X.CoordModeOrigin,
 
203
                                          self.lines)
 
204
                self.last.draw()
 
205
 
 
206
 
 
207
# A romboid, drawed around the Movement endpoints
 
208
class Romboid:
 
209
    def __init__(self, win, ev):
 
210
        self.win = win
 
211
        self.x = ev.event_x
 
212
        self.y = ev.event_y
 
213
        self.draw()
 
214
 
 
215
    def draw(self):
 
216
        # Draw the segments of the romboid
 
217
        self.win.window.poly_line(self.win.gc, X.CoordModePrevious,
 
218
                                  [(self.x, self.y - 5),
 
219
                                   (5, 5),
 
220
                                   (-5, 5),
 
221
                                   (-5, -5),
 
222
                                   (5, -5)])
 
223
 
 
224
 
 
225
if __name__ == '__main__':
 
226
    Window(display.Display()).loop()