File: gtk2compat.py

package info (click to toggle)
mypaint 1.2.0-4.1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 51,284 kB
  • ctags: 5,958
  • sloc: python: 35,898; ansic: 4,067; cpp: 3,416; xml: 2,234; sh: 386; makefile: 25
file content (87 lines) | stat: -rw-r--r-- 2,200 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
"""PyGTK compatibility layer.

To be removed as we migrate to Python-GObject's normal syntax. Do not write new
code in the PyGTK style now, and feel free to simplify "if gtk2compat.USE_GTK3"
clauses in code elsewhere.

"""

from __future__ import absolute_import
import logging
logger = logging.getLogger(__name__)

import gi
from gi.repository import GObject
from gi.repository import Gdk
from gi.repository import Gtk
from gi.repository import GdkPixbuf

USE_GTK3 = True


class GdkPixbufCompat(object):

    @staticmethod
    def new(colorspace, has_alpha, bps, width, height):
        return GdkPixbuf.Pixbuf.new(colorspace, has_alpha, bps, width, height)


class GdkCompat(object):

    @staticmethod
    def display_get_default():
        display_manager = Gdk.DisplayManager.get()
        return display_manager.get_default_display()

    @staticmethod
    def keymap_get_default():
        return Gdk.Keymap.get_default()


class GtkCompat(object):

    def accel_map_load(self, file):
        return Gtk.AccelMap.load(file)

    def accel_map_save(self, file):
        return Gtk.AccelMap.save(file)

    def accel_map_get(self):
        return Gtk.AccelMap.get()

    def accel_map_lookup_entry(self, accel_path):
        # Returns "a 2-tuple containing the keyval and modifier mask
        # corresponding to accel_path or None if not valid", like the GTK2
        # function.
        found, accel_key = Gtk.AccelMap.lookup_entry(accel_path)
        if not found:
            return None
        keyval = accel_key.accel_key
        mods = accel_key.accel_mods
        return keyval, mods


def get_gobject():
    return GObject


def original_gtk():
    logger.debug("Using GTK3")
    try:
        import pygtkcompat
        pygtkcompat.enable()
        pygtkcompat.enable_gtk(version='3.0')
    except ImportError:
        logger.warning('"import pygtkcompat" did not work, trying old '
                       'deprecated way')
        import gi.pygtkcompat
        gi.pygtkcompat.enable()
        gi.pygtkcompat.enable_gtk(version='3.0')
    import gtk
    return gtk

orig_gtk = original_gtk()
gdk = GdkCompat()
gdk.pixbuf = GdkPixbufCompat()
gtk = GtkCompat()
gobject = get_gobject()