File: _wx.py

package info (click to toggle)
python-vispy 0.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,112 kB
  • sloc: python: 61,648; javascript: 6,800; ansic: 2,104; makefile: 141; sh: 6
file content (492 lines) | stat: -rw-r--r-- 15,540 bytes parent folder | download
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# -*- coding: utf-8 -*-
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.

"""vispy backend for wxPython."""

from __future__ import division

from time import sleep
import gc
import warnings

from ..base import (BaseApplicationBackend, BaseCanvasBackend,
                    BaseTimerBackend)
from ...util import keys, logger
from ...util.ptime import time
from ... import config

USE_EGL = config['gl_backend'].lower().startswith('es')


# -------------------------------------------------------------------- init ---

try:
    # avoid silly locale warning on OSX
    with warnings.catch_warnings(record=True):
        import wx
        from wx import glcanvas
        from wx.glcanvas import GLCanvas

    # Map native keys to vispy keys
    KEYMAP = {
        wx.WXK_SHIFT: keys.SHIFT,
        wx.WXK_CONTROL: keys.CONTROL,
        wx.WXK_ALT: keys.ALT,
        wx.WXK_WINDOWS_MENU: keys.META,

        wx.WXK_LEFT: keys.LEFT,
        wx.WXK_UP: keys.UP,
        wx.WXK_RIGHT: keys.RIGHT,
        wx.WXK_DOWN: keys.DOWN,
        wx.WXK_PAGEUP: keys.PAGEUP,
        wx.WXK_PAGEDOWN: keys.PAGEDOWN,

        wx.WXK_INSERT: keys.INSERT,
        wx.WXK_DELETE: keys.DELETE,
        wx.WXK_HOME: keys.HOME,
        wx.WXK_END: keys.END,

        wx.WXK_ESCAPE: keys.ESCAPE,
        wx.WXK_BACK: keys.BACKSPACE,

        wx.WXK_F1: keys.F1,
        wx.WXK_F2: keys.F2,
        wx.WXK_F3: keys.F3,
        wx.WXK_F4: keys.F4,
        wx.WXK_F5: keys.F5,
        wx.WXK_F6: keys.F6,
        wx.WXK_F7: keys.F7,
        wx.WXK_F8: keys.F8,
        wx.WXK_F9: keys.F9,
        wx.WXK_F10: keys.F10,
        wx.WXK_F11: keys.F11,
        wx.WXK_F12: keys.F12,

        wx.WXK_SPACE: keys.SPACE,
        wx.WXK_RETURN: keys.ENTER,  # == pyglet.window.key.RETURN
        wx.WXK_NUMPAD_ENTER: keys.ENTER,
        wx.WXK_TAB: keys.TAB,
    }
except Exception as exp:
    available, testable, why_not, which = False, False, str(exp), None

    class GLCanvas(object):
        pass
else:
    if USE_EGL:
        available, testable, why_not = False, False, 'EGL not supported'
    else:
        available, testable, why_not = True, True, None
    which = 'wxPython ' + str(wx.__version__)


# -------------------------------------------------------------- capability ---

capability = dict(  # things that can be set by the backend
    title=True,
    size=True,
    position=True,
    show=True,
    vsync=True,
    resizable=True,
    decorate=True,
    fullscreen=True,
    context=True,
    multi_window=True,
    scroll=True,
    parent=True,
    always_on_top=True,
)


# ------------------------------------------------------- set_configuration ---

def _set_config(c):
    """Set gl configuration"""
    gl_attribs = [glcanvas.WX_GL_RGBA,
                  glcanvas.WX_GL_DEPTH_SIZE, c['depth_size'],
                  glcanvas.WX_GL_STENCIL_SIZE, c['stencil_size'],
                  glcanvas.WX_GL_MIN_RED, c['red_size'],
                  glcanvas.WX_GL_MIN_GREEN, c['green_size'],
                  glcanvas.WX_GL_MIN_BLUE, c['blue_size'],
                  glcanvas.WX_GL_MIN_ALPHA, c['alpha_size']]
    gl_attribs += [glcanvas.WX_GL_DOUBLEBUFFER] if c['double_buffer'] else []
    gl_attribs += [glcanvas.WX_GL_STEREO] if c['stereo'] else []
    return gl_attribs


# ------------------------------------------------------------- application ---

_wx_app = None
_timers = []


class ApplicationBackend(BaseApplicationBackend):

    def __init__(self):
        BaseApplicationBackend.__init__(self)
        self._event_loop = wx.GUIEventLoop()
        wx.EventLoop.SetActive(self._event_loop)

    def _vispy_get_backend_name(self):
        return 'wx'

    def _vispy_process_events(self):
        # inpsired by https://github.com/wxWidgets/wxPython/blob/master/
        #             samples/mainloop/mainloop.py
        for _ in range(3):  # trial-and-error found this to work (!)
            while self._event_loop.Pending():
                self._event_loop.Dispatch()
            if hasattr(_wx_app, 'ProcessIdle'):
                _wx_app.ProcessIdle()
            else:
                self._event_loop.ProcessIdle()
            sleep(0.01)

    def _vispy_run(self):
        return _wx_app.MainLoop()

    def _vispy_quit(self):
        global _wx_app
        _wx_app.ExitMainLoop()

    def _vispy_get_native_app(self):
        # Get native app in save way. Taken from guisupport.py
        global _wx_app
        _wx_app = wx.GetApp()  # in case the user already has one
        if _wx_app is None:
            if hasattr(wx, 'App'):
                _wx_app = wx.App()
            else:
                # legacy wx
                _wx_app = wx.PySimpleApp()
        _wx_app.SetExitOnFrameDelete(True)
        return _wx_app


# ------------------------------------------------------------------ canvas ---

def _get_mods(evt):
    """Helper to extract list of mods from event"""
    mods = []
    mods += [keys.CONTROL] if evt.ControlDown() else []
    mods += [keys.ALT] if evt.AltDown() else []
    mods += [keys.SHIFT] if evt.ShiftDown() else []
    mods += [keys.META] if evt.MetaDown() else []
    return mods


def _process_key(evt):
    """Helper to convert from wx keycode to vispy keycode"""
    key = evt.GetKeyCode()
    if key in KEYMAP:
        return KEYMAP[key], ''
    if 97 <= key <= 122:
        key -= 32
    if key >= 32 and key <= 127:
        return keys.Key(chr(key)), chr(key)
    else:
        return None, None


class DummySize(object):
    def __init__(self, size):
        self.size = size

    def GetSize(self):
        return self.size

    def Skip(self):
        pass


class CanvasBackend(GLCanvas, BaseCanvasBackend):
    """wxPython backend for Canvas abstract class."""

    def __init__(self, vispy_canvas, **kwargs):
        BaseCanvasBackend.__init__(self, vispy_canvas)
        p = self._process_backend_kwargs(kwargs)

        # WX supports OS double-click events, so we set this here to
        # avoid double events
        self._double_click_supported = True

        # Set config
        self._gl_attribs = _set_config(p.context.config)
        # Deal with context
        p.context.shared.add_ref('wx', self)
        if p.context.shared.ref is self:
            self._gl_context = None  # set for real once we init the GLCanvas
        else:
            self._gl_context = p.context.shared.ref._gl_context

        if p.position is None:
            pos = wx.DefaultPosition
        else:
            pos = p.position

        if p.parent is None:
            style = (wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX | wx.CLOSE_BOX |
                     wx.SYSTEM_MENU | wx.CAPTION | wx.CLIP_CHILDREN)
            style |= wx.NO_BORDER if not p.decorate else wx.RESIZE_BORDER
            style |= wx.STAY_ON_TOP if p.always_on_top else 0
            self._frame = wx.Frame(None, wx.ID_ANY, p.title, pos, p.size,
                                   style)

            if not p.resizable:
                self._frame.SetSizeHints(p.size[0], p.size[1],
                                         p.size[0], p.size[1])
            if p.fullscreen is not False:
                if p.fullscreen is not True:
                    logger.warning('Cannot specify monitor number for wx '
                                   'fullscreen, using default')
                self._fullscreen = True
            else:
                self._fullscreen = False
            _wx_app.SetTopWindow(self._frame)
            parent = self._frame
            self._frame.Show()
            self._frame.Raise()
            self._frame.Bind(wx.EVT_CLOSE, self.on_close)
        else:
            parent = p.parent
            self._frame = None
            self._fullscreen = False
        self._init = False
        GLCanvas.__init__(self, parent, wx.ID_ANY, pos=pos,
                          size=p.size, style=0, name='GLCanvas',
                          attribList=self._gl_attribs)

        if self._gl_context is None:
            self._gl_context = glcanvas.GLContext(self)

        self.SetFocus()
        self._vispy_set_title(p.title)
        self._size = None
        self.Bind(wx.EVT_SIZE, self.on_resize)
        self.Bind(wx.EVT_PAINT, self.on_draw)
        self.Bind(wx.EVT_KEY_DOWN, self.on_key_down)
        self.Bind(wx.EVT_KEY_UP, self.on_key_up)
        self.Bind(wx.EVT_MOUSE_EVENTS, self.on_mouse_event)
        self._size_init = p.size
        self._vispy_set_visible(p.show)

    def on_resize(self, event):
        if self._vispy_canvas is None or not self._init:
            event.Skip()
            return
        size = event.GetSize()
        self._vispy_canvas.events.resize(size=size)
        self.Refresh()
        event.Skip()

    def on_draw(self, event):
        if self._vispy_canvas is None:
            return
        dc = wx.PaintDC(self)  # needed for wx
        if not self._init:
            self._initialize()
        self._vispy_canvas.set_current()
        self._vispy_canvas.events.draw(region=None)
        del dc
        event.Skip()

    def _initialize(self):
        if self._vispy_canvas is None:
            return
        self._init = True
        self._vispy_canvas.set_current()
        self._vispy_canvas.events.initialize()
        self.on_resize(DummySize(self._size_init))

    def _vispy_set_current(self):
        if self.IsShown():
            self.SetCurrent(self._gl_context)

    def _vispy_warmup(self):
        etime = time() + 0.3
        while time() < etime:
            sleep(0.01)
            self._vispy_canvas.set_current()
            self._vispy_canvas.app.process_events()

    def _vispy_swap_buffers(self):
        # Swap front and back buffer
        self._vispy_canvas.set_current()
        self.SwapBuffers()

    def _vispy_set_title(self, title):
        # Set the window title. Has no effect for widgets
        if self._frame is not None:
            self._frame.SetLabel(title)

    def _vispy_set_size(self, w, h):
        # Set size of the widget or window
        if not self._init:
            self._size_init = (w, h)
        if hasattr(self, 'SetSize'):
            # phoenix
            self.SetSize(w, h)
        else:
            # legacy
            self.SetSizeWH(w, h)

    def _vispy_set_position(self, x, y):
        # Set positionof the widget or window. May have no effect for widgets
        if self._frame is not None:
            self._frame.SetPosition((x, y))

    def _vispy_get_fullscreen(self):
        return self._fullscreen

    def _vispy_set_fullscreen(self, fullscreen):
        if self._frame is not None:
            self._fullscreen = bool(fullscreen)
            self._vispy_set_visible(True)

    def _vispy_set_visible(self, visible):
        # Show or hide the window or widget
        self.Show(visible)
        if visible:
            if self._frame is not None:
                self._frame.ShowFullScreen(self._fullscreen)

    def _vispy_update(self):
        # Invoke a redraw
        self.Refresh()

    def _vispy_close(self):
        if self._vispy_canvas is None:
            return
        # Force the window or widget to shut down
        canvas = self
        frame = self._frame
        self._gl_context = None  # let RC destroy this in case it's shared
        canvas.Close()
        canvas.Destroy()
        if frame:
            frame.Close()
            frame.Destroy()
        gc.collect()  # ensure context gets destroyed if it should be

    def _vispy_get_size(self):
        if self._vispy_canvas is None:
            return
        w, h = self.GetClientSize()
        return w, h

    def _vispy_get_physical_size(self):
        w, h = self.GetClientSize()
        ratio = self.GetContentScaleFactor()
        return int(w * ratio), int(h * ratio)

    def _vispy_get_position(self):
        if self._vispy_canvas is None:
            return
        x, y = self.GetPosition()
        return x, y

    def on_close(self, evt):
        if not self:  # wx control evaluates to false if C++ part deleted
            return
        if self._vispy_canvas is None:
            return
        self._vispy_canvas.close()

    @staticmethod
    def _pressed_mouse_buttons(evt):
        buttons = []
        if evt.LeftIsDown():
            buttons.append(1)
        if evt.RightIsDown():
            buttons.append(2)
        if evt.MiddleIsDown():
            buttons.append(3)

        return buttons

    def on_mouse_event(self, evt):
        if self._vispy_canvas is None:
            return
        pos = (evt.GetX(), evt.GetY())
        buttons = self._pressed_mouse_buttons(evt)
        mods = _get_mods(evt)
        if evt.GetWheelRotation() != 0:
            delta = (0., float(evt.GetWheelRotation())/120.0)
            self._vispy_canvas.events.mouse_wheel(delta=delta, pos=pos,
                                                  buttons=buttons, modifiers=mods)
        elif evt.Moving() or evt.Dragging():  # mouse move event
            self._vispy_mouse_move(pos=pos, buttons=buttons, modifiers=mods)
        elif evt.ButtonDown():
            if evt.LeftDown():
                button = 1
            elif evt.MiddleDown():
                button = 3
            elif evt.RightDown():
                button = 2
            else:
                evt.Skip()
                return
            self._vispy_mouse_press(pos=pos, button=button, buttons=buttons, modifiers=mods)
        elif evt.ButtonUp():
            if evt.LeftUp():
                button = 1
            elif evt.MiddleUp():
                button = 3
            elif evt.RightUp():
                button = 2
            else:
                evt.Skip()
                return
            self._vispy_mouse_release(pos=pos, button=button, buttons=buttons, modifiers=mods)
        elif evt.ButtonDClick():
            if evt.LeftDClick():
                button = 1
            elif evt.MiddleDClick():
                button = 3
            elif evt.RightDClick():
                button = 2
            else:
                evt.Skip()
                return
            self._vispy_mouse_press(pos=pos, button=button, buttons=buttons, modifiers=mods)
            self._vispy_mouse_double_click(pos=pos, button=button,
                                           buttons=buttons, modifiers=mods)
        evt.Skip()

    def on_key_down(self, evt):
        if self._vispy_canvas is None:
            return
        key, text = _process_key(evt)
        self._vispy_canvas.events.key_press(key=key, text=text,
                                            modifiers=_get_mods(evt))

    def on_key_up(self, evt):
        if self._vispy_canvas is None:
            return
        key, text = _process_key(evt)
        self._vispy_canvas.events.key_release(key=key, text=text,
                                              modifiers=_get_mods(evt))


# ------------------------------------------------------------------- timer ---

class TimerBackend(BaseTimerBackend):

    def __init__(self, vispy_timer):
        BaseTimerBackend.__init__(self, vispy_timer)
        assert _wx_app is not None
        parent = _wx_app.GetTopWindow()  # assume it's the parent window
        self._timer = wx.Timer(parent, -1)
        parent.Bind(wx.EVT_TIMER, self._vispy_timeout, self._timer)

    def _vispy_start(self, interval):
        self._timer.Start(int(interval * 1000.), False)

    def _vispy_stop(self):
        self._timer.Stop()

    def _vispy_timeout(self, evt):
        self._vispy_timer._timeout()
        evt.Skip()