Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate
1
# Import Gtk and GObject, put include crazy thunks so that old
2
# code using pygtk and Gtk 2.0 code continues to run.
3
FORCE_GTK2 = False
4
5
try:
6
  if FORCE_GTK2: raise ValueError("forcing gtk2")
7
  import gi
8
  gi.require_version('Gtk', '3.0')
9
  from gi.repository import Gtk, Gdk, GObject, GdkPixbuf
10
  from gi.repository import Pango, PangoCairo
11
  def Rectangle(x, y, w, h):
12
    r = Gdk.Rectangle()
13
    r.x, r.y = x, y
14
    r.width, r.height = w, h
15
    return r
16
  GObject.TYPE_NONE = None # compatibility hack
17
  class GConf:
18
    pass # XXX no more GConf, sigh
19
20
  # No CairoContext wrapper needed for gir/gtk3
21
  Gdk.CairoContext = lambda x: x # GTK2 compatibility
22
23
except ValueError, ImportError:
24
  # fall back to old pygtk, with various evil hacks
25
  import pygtk
26
  pygtk.require('2.0')
27
  import gtk as Gtk
28
  import gobject as GObject
29
  import gconf
30
  import pango as Pango
31
  import pangocairo as PangoCairo
32
  import cairo
33
  Gdk = Gtk.gdk
34
  Rectangle = Gdk.Rectangle
35
36
  class GConf:
37
    class Client:
38
      get_default = gconf.client_get_default
39
40
  class GObjectSignalFlags:
41
    RUN_FIRST = GObject.SIGNAL_RUN_FIRST
42
  GObject.SignalFlags = GObjectSignalFlags
43
44
  Gdk.cairo_set_source_pixbuf = \
45
    lambda cr, img, x, y: cr.set_source_pixbuf(img, x, y)
46
  def gdk_pixbuf_get_from_surface(surface, x, y, w, h):
47
    pixmap = Gdk.Pixmap(None, w, h, 24)
48
    cr = pixmap.cairo_create()
49
    cr.set_source_surface(surface, -x, -y)
50
    cr.rectangle(0,0,w,h);
51
    cr.set_operator(cairo.OPERATOR_SOURCE)
52
    cr.fill()
53
    cmap = Gdk.colormap_get_system()
54
    return Gdk.pixbuf_get_from_drawable(None, pixmap, cmap, 0, 0, 0, 0, w, h)
55
  Gdk.pixbuf_get_from_surface = gdk_pixbuf_get_from_surface
56
57
  class GdkEventMask:
58
    EXPOSURE_MASK = Gdk.EXPOSURE_MASK
59
    BUTTON_PRESS_MASK = Gdk.BUTTON_PRESS_MASK
60
    BUTTON_RELEASE_MASK = Gdk.BUTTON_RELEASE_MASK
61
    POINTER_MOTION_MASK = Gdk.POINTER_MOTION_MASK
62
    KEY_PRESS_MASK = Gdk.KEY_PRESS_MASK
63
    VISIBILITY_NOTIFY_MASK = Gdk.VISIBILITY_NOTIFY_MASK
64
  Gdk.EventMask = GdkEventMask
65
66
  class GdkModifierType:
67
    MOD1_MASK = Gdk.MOD1_MASK
68
    CONTROL_MASK = Gdk.CONTROL_MASK
69
  Gdk.ModifierType = GdkModifierType
70
71
  class GdkPixbuf:
72
    class InterpType:
73
      NEAREST = Gdk.INTERP_NEAREST
74
    class Colorspace:
75
      RGB = Gdk.COLORSPACE_RGB
76
    class Pixbuf:
77
      # XXX not the same as Gdk.Pixbuf, sadly.  don't do instanceof!
78
      new_from_file_at_size = Gdk.pixbuf_new_from_file_at_size
79
      new_from_file = Gdk.pixbuf_new_from_file
80
      loader_new_with_mime_type = Gdk.pixbuf_loader_new_with_mime_type
81
    class PixbufLoader:
82
      @staticmethod
83
      def new_with_type(type):
84
        return Gdk.PixbufLoader(type)
85
86
  class GdkScreen:
87
    width = Gdk.screen_width
88
    height = Gdk.screen_height
89
  Gdk.Screen = GdkScreen
90
91
  class GdkVisibilityState:
92
    FULLY_OBSCURED = Gdk.VISIBILITY_FULLY_OBSCURED
93
  Gdk.VisibilityState = GdkVisibilityState
94
95
  class GdkWindowTypeHint:
96
    NORMAL = Gdk.WINDOW_TYPE_HINT_NORMAL
97
  Gdk.WindowTypeHint = GdkWindowTypeHint
98
99
  class GtkFileChooserAction:
100
    OPEN = Gtk.FILE_CHOOSER_ACTION_OPEN
101
    SAVE = Gtk.FILE_CHOOSER_ACTION_SAVE
102
  Gtk.FileChooserAction = GtkFileChooserAction
103
104
  class GtkResponseType:
105
    OK = Gtk.RESPONSE_OK
106
    CANCEL = Gtk.RESPONSE_CANCEL
107
    ACCEPT = Gtk.RESPONSE_ACCEPT
108
  Gtk.ResponseType = GtkResponseType
109
110
  class GtkButtonsType:
111
    OK_CANCEL = Gtk.BUTTONS_OK_CANCEL
112
  Gtk.ButtonsType = GtkButtonsType
113
114
  class GtkMessageType:
115
    INFO = Gtk.MESSAGE_INFO
116
  Gtk.MessageType = GtkMessageType
117
118
  class GtkDialogFlags:
119
    MODAL = Gtk.DIALOG_MODAL
120
    DESTROY_WITH_PARENT = Gtk.DIALOG_DESTROY_WITH_PARENT
121
  Gtk.DialogFlags = GtkDialogFlags
122
123
  class GtkIconTheme:
124
    get_default = Gtk.icon_theme_get_default
125
  Gtk.IconTheme = GtkIconTheme
126
127
  class GtkPolicyType:
128
    AUTOMATIC = Gtk.POLICY_AUTOMATIC
129
  Gtk.PolicyType = GtkPolicyType
130
131
  class GtkWrapMode:
132
    WORD = Gtk.WRAP_WORD
133
  Gtk.WrapMode = GtkWrapMode
134
135
  class GtkWindowType:
136
    TOPLEVEL = Gtk.WINDOW_TOPLEVEL
137
  Gtk.WindowType = GtkWindowType
138
139
  # hack around named parameter for Gtk.Label
140
  GtkLabel = Gtk.Label
141
  def GtkLabel2(label=''):
142
    return GtkLabel(label)
143
  Gtk.Label = GtkLabel2
144
145
  # hack around named parameters
146
  GtkVBox1 = Gtk.VBox
147
  class GtkVBox2:
148
    @staticmethod
149
    def new(homogenous, spacing):
150
      return GtkVBox1(homogenous, spacing)
151
  Gtk.VBox = GtkVBox2
152
153
  GtkMenuItem1 = Gtk.MenuItem
154
  class GtkMenuItem2:
155
    @staticmethod
156
    def new_with_label(label):
157
      return GtkMenuItem1(label)
158
  Gtk.MenuItem = GtkMenuItem2
159
160
  class PangoLayout2(Pango.Layout):
161
    @staticmethod
162
    def new(cr):
163
      # call cr.create_layout(), but wrap the result so we can override
164
      # set_text() below
165
      return PangoLayout2(cr.create_layout().get_context())
166
    def set_text(self, text, val):
167
      if val != -1: text = text[:val]
168
      super(PangoLayout2, self).set_text(text)
169
170
  Pango.Layout = PangoLayout2
171
172
  PangoCairo.create_context = lambda x: PangoCairo.CairoContext(x)
173
  PangoCairo.update_layout = lambda cr, pl: cr.update_layout(pl)
174
  PangoCairo.show_layout = lambda cr, pl: cr.show_layout(pl)