diff options
Diffstat (limited to 'util/gtkcompat.py')
-rw-r--r-- | util/gtkcompat.py | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/util/gtkcompat.py b/util/gtkcompat.py new file mode 100644 index 0000000..c60ea73 --- /dev/null +++ b/util/gtkcompat.py @@ -0,0 +1,132 @@ +# 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 + Rectangle = lambda x, y, w, h: (x,y,w,h) + GObject.TYPE_NONE = None # compatibility hack + class GConf: + pass # XXX no more GConf, sigh +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 + 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 + + 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 + PixbufLoader = Gdk.PixbufLoader + + 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 |