# Import Gtk and GObject, put include crazy thunks so that old # code using pygtk and Gtk 2.0 code continues to run. FORCE_GTK2 = True try: if FORCE_GTK2: raise ValueError("forcing gtk2") import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk, GObject, GdkPixbuf from gi.repository import Pango, PangoCairo def Rectangle(x, y, w, h): r = Gdk.Rectangle() r.x, r.y = x, y r.width, r.height = w, h return r GObject.TYPE_NONE = None # compatibility hack class GConf: pass # XXX no more GConf, sigh # No CairoContext wrapper needed for gir/gtk3 Gdk.CairoContext = lambda x: x # GTK2 compatibility except ValueError, ImportError: # fall back to old pygtk, with various evil hacks import pygtk pygtk.require('2.0') import gtk as Gtk import gobject as GObject import gconf import pango as Pango import pangocairo as PangoCairo Gdk = Gtk.gdk Rectangle = Gdk.Rectangle class GConf: class Client: get_default = gconf.client_get_default class GObjectSignalFlags: RUN_FIRST = GObject.SIGNAL_RUN_FIRST GObject.SignalFlags = GObjectSignalFlags Gdk.cairo_set_source_pixbuf = \ lambda cr, img, x, y: cr.set_source_pixbuf(img, x, y) class GdkEventMask: EXPOSURE_MASK = Gdk.EXPOSURE_MASK BUTTON_PRESS_MASK = Gdk.BUTTON_PRESS_MASK BUTTON_RELEASE_MASK = Gdk.BUTTON_RELEASE_MASK POINTER_MOTION_MASK = Gdk.POINTER_MOTION_MASK KEY_PRESS_MASK = Gdk.KEY_PRESS_MASK CONTROL_MASK = Gdk.CONTROL_MASK VISIBILITY_NOTIFY_MASK = Gdk.VISIBILITY_NOTIFY_MASK Gdk.EventMask = GdkEventMask class GdkModifierType: MOD1_MASK = Gdk.MOD1_MASK Gdk.ModifierType = GdkModifierType class GdkPixbuf: class InterpType: NEAREST = Gdk.INTERP_NEAREST class Colorspace: RGB = Gdk.COLORSPACE_RGB class Pixbuf: # XXX not the same as Gdk.Pixbuf, sadly. don't do instanceof! new_from_file_at_size = Gdk.pixbuf_new_from_file_at_size new_from_file = Gdk.pixbuf_new_from_file loader_new_with_mime_type = Gdk.pixbuf_loader_new_with_mime_type class PixbufLoader: @staticmethod def new_with_type(type): return Gdk.PixbufLoader(type) class GdkScreen: width = Gdk.screen_width height = Gdk.screen_height Gdk.Screen = GdkScreen class GdkVisibilityState: FULLY_OBSCURED = Gdk.VISIBILITY_FULLY_OBSCURED Gdk.VisibilityState = GdkVisibilityState class GdkWindowTypeHint: NORMAL = Gdk.WINDOW_TYPE_HINT_NORMAL Gdk.WindowTypeHint = GdkWindowTypeHint class GtkFileChooserAction: OPEN = Gtk.FILE_CHOOSER_ACTION_OPEN SAVE = Gtk.FILE_CHOOSER_ACTION_SAVE Gtk.FileChooserAction = GtkFileChooserAction class GtkResponseType: OK = Gtk.RESPONSE_OK CANCEL = Gtk.RESPONSE_CANCEL ACCEPT = Gtk.RESPONSE_ACCEPT Gtk.ResponseType = GtkResponseType class GtkButtonsType: OK_CANCEL = Gtk.BUTTONS_OK_CANCEL Gtk.ButtonsType = GtkButtonsType class GtkMessageType: INFO = Gtk.MESSAGE_INFO Gtk.MessageType = GtkMessageType class GtkDialogFlags: MODAL = Gtk.DIALOG_MODAL DESTROY_WITH_PARENT = Gtk.DIALOG_DESTROY_WITH_PARENT Gtk.DialogFlags = GtkDialogFlags class GtkIconTheme: get_default = Gtk.icon_theme_get_default Gtk.IconTheme = GtkIconTheme class GtkPolicyType: AUTOMATIC = Gtk.POLICY_AUTOMATIC Gtk.PolicyType = GtkPolicyType class GtkWrapMode: WORD = Gtk.WRAP_WORD Gtk.WrapMode = GtkWrapMode class GtkWindowType: TOPLEVEL = Gtk.WINDOW_TOPLEVEL Gtk.WindowType = GtkWindowType # hack around named parameter for Gtk.Label GtkLabel = Gtk.Label def GtkLabel2(label=''): return GtkLabel(label) Gtk.Label = GtkLabel2 # hack around named parameters GtkVBox1 = Gtk.VBox class GtkVBox2: @staticmethod def new(homogenous, spacing): return GtkVBox1(homogenous, spacing) Gtk.VBox = GtkVBox2 GtkMenuItem1 = Gtk.MenuItem class GtkMenuItem2: @staticmethod def new_with_label(label): return GtkMenuItem1(label) Gtk.MenuItem = GtkMenuItem2 class PangoLayout2(Pango.Layout): @staticmethod def new(cr): # call cr.create_layout(), but wrap the result so we can override # set_text() below return PangoLayout2(cr.create_layout().get_context()) def set_text(self, text, val): if val != -1: text = text[:val] super(PangoLayout2, self).set_text(text) Pango.Layout = PangoLayout2 PangoCairo.create_context = lambda x: PangoCairo.CairoContext(x) PangoCairo.update_layout = lambda cr, pl: cr.update_layout(pl) PangoCairo.show_layout = lambda cr, pl: cr.show_layout(pl)