1# -*- Mode: Python; py-indent-offset: 4 -*- 2# vim: tabstop=4 shiftwidth=4 expandtab 3# 4# Copyright (C) 2011-2012 Johan Dahlin <johan@gnome.org> 5# 6# This library is free software; you can redistribute it and/or 7# modify it under the terms of the GNU Lesser General Public 8# License as published by the Free Software Foundation; either 9# version 2.1 of the License, or (at your option) any later version. 10# 11# This library is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14# Lesser General Public License for more details. 15# 16# You should have received a copy of the GNU Lesser General Public 17# License along with this library; if not, write to the Free Software 18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19# USA 20 21""" 22PyGTK compatibility layer. 23 24This modules goes a little bit longer to maintain PyGTK compatibility than 25the normal overrides system. 26 27It is recommended to not depend on this layer, but only use it as an 28intermediate step when porting your application to PyGI. 29 30Compatibility might never be 100%, but the aim is to make it possible to run 31a well behaved PyGTK application mostly unmodified on top of PyGI. 32 33""" 34 35importsys 36importwarnings 37 38try: 39# Python 3 40fromcollectionsimportUserList 41fromimpimportreload 42UserList# pyflakes 43exceptImportError: 44# Python 2 ships that in a different module 45fromUserListimportUserList 46UserList# pyflakes 47 48importgi 49fromgi.repositoryimportGObject 50 51
99# set the default encoding like PyGTK100reload(sys)101ifsys.version_info<(3,0):102sys.setdefaultencoding('utf-8')103104# atk105gi.require_version('Atk','1.0')106fromgi.repositoryimportAtk107sys.modules['atk']=Atk108_install_enums(Atk)109110# pango111gi.require_version('Pango','1.0')112fromgi.repositoryimportPango113sys.modules['pango']=Pango114_install_enums(Pango)115116# pangocairo117gi.require_version('PangoCairo','1.0')118fromgi.repositoryimportPangoCairo119sys.modules['pangocairo']=PangoCairo120121# gdk122gi.require_version('Gdk',version)123gi.require_version('GdkPixbuf','2.0')124fromgi.repositoryimportGdk125fromgi.repositoryimportGdkPixbuf126sys.modules['gtk.gdk']=Gdk127_install_enums(Gdk)128_install_enums(GdkPixbuf,dest=Gdk)129Gdk._2BUTTON_PRESS=5130Gdk.BUTTON_PRESS=4131132Gdk.screen_get_default=Gdk.Screen.get_default133Gdk.Pixbuf=GdkPixbuf.Pixbuf134Gdk.pixbuf_new_from_file=GdkPixbuf.Pixbuf.new_from_file135Gdk.PixbufLoader=GdkPixbuf.PixbufLoader.new_with_type136137orig_get_formats=GdkPixbuf.Pixbuf.get_formats138139defget_formats():140formats=orig_get_formats()141result=[]142143defmake_dict(format_):144result={}145result['description']=format_.get_description()146result['name']=format_.get_name()147result['mime_types']=format_.get_mime_types()148result['extensions']=format_.get_extensions()149returnresult
150151forformat_informats:152result.append(make_dict(format_))153returnresult154155Gdk.pixbuf_get_formats=get_formats156157orig_get_frame_extents=Gdk.Window.get_frame_extents158159defget_frame_extents(window):160try:161try:162rect=Gdk.Rectangle(0,0,0,0)163exceptTypeError:164rect=Gdk.Rectangle()165orig_get_frame_extents(window,rect)166exceptTypeError:167rect=orig_get_frame_extents(window)168returnrect169Gdk.Window.get_frame_extents=get_frame_extents170171orig_get_origin=Gdk.Window.get_origin172173defget_origin(self):174returnorig_get_origin(self)[1:]175Gdk.Window.get_origin=get_origin176177Gdk.screen_width=Gdk.Screen.width178Gdk.screen_height=Gdk.Screen.height179180# gtk181gi.require_version('Gtk',version)182fromgi.repositoryimportGtk183sys.modules['gtk']=Gtk184Gtk.gdk=Gdk185186Gtk.pygtk_version=(2,99,0)187188Gtk.gtk_version=(Gtk.MAJOR_VERSION,189Gtk.MINOR_VERSION,190Gtk.MICRO_VERSION)191_install_enums(Gtk)192193# Action194195defset_tool_item_type(menuaction,gtype):196warnings.warn('set_tool_item_type() is not supported',197DeprecationWarning,stacklevel=2)198Gtk.Action.set_tool_item_type=classmethod(set_tool_item_type)199200# Alignment201202orig_Alignment=Gtk.Alignment203204classAlignment(orig_Alignment):205def__init__(self,xalign=0.0,yalign=0.0,xscale=0.0,yscale=0.0):206orig_Alignment.__init__(self)207self.props.xalign=xalign208self.props.yalign=yalign209self.props.xscale=xscale210self.props.yscale=yscale211212Gtk.Alignment=Alignment213214# Box215216orig_pack_end=Gtk.Box.pack_end217218defpack_end(self,child,expand=True,fill=True,padding=0):219orig_pack_end(self,child,expand,fill,padding)220Gtk.Box.pack_end=pack_end221222orig_pack_start=Gtk.Box.pack_start223224defpack_start(self,child,expand=True,fill=True,padding=0):225orig_pack_start(self,child,expand,fill,padding)226Gtk.Box.pack_start=pack_start227228# TreeViewColumn229230orig_tree_view_column_pack_end=Gtk.TreeViewColumn.pack_end231232deftree_view_column_pack_end(self,cell,expand=True):233orig_tree_view_column_pack_end(self,cell,expand)234Gtk.TreeViewColumn.pack_end=tree_view_column_pack_end235236orig_tree_view_column_pack_start=Gtk.TreeViewColumn.pack_start237238deftree_view_column_pack_start(self,cell,expand=True):239orig_tree_view_column_pack_start(self,cell,expand)240Gtk.TreeViewColumn.pack_start=tree_view_column_pack_start241242# TreeView243244definsert_column_with_attributes(view,position,title,cell,*args,**kwargs):245pass246Gtk.TreeView.insert_column_with_attributes=insert_column_with_attributes247248# CellLayout249250orig_cell_pack_end=Gtk.CellLayout.pack_end251252defcell_pack_end(self,cell,expand=True):253orig_cell_pack_end(self,cell,expand)254Gtk.CellLayout.pack_end=cell_pack_end255256orig_cell_pack_start=Gtk.CellLayout.pack_start257258defcell_pack_start(self,cell,expand=True):259orig_cell_pack_start(self,cell,expand)260Gtk.CellLayout.pack_start=cell_pack_start261262orig_set_cell_data_func=Gtk.CellLayout.set_cell_data_func263264defset_cell_data_func(self,cell,func,user_data=_unset):265defcallback(*args):266ifargs[-1]==_unset:267args=args[:-1]268returnfunc(*args)269orig_set_cell_data_func(self,cell,callback,user_data)270Gtk.CellLayout.set_cell_data_func=set_cell_data_func271272# CellRenderer273274classGenericCellRenderer(Gtk.CellRenderer):275pass276Gtk.GenericCellRenderer=GenericCellRenderer277278# ComboBox279280orig_combo_row_separator_func=Gtk.ComboBox.set_row_separator_func281282defcombo_row_separator_func(self,func,user_data=_unset):283defcallback(*args):284ifargs[-1]==_unset:285args=args[:-1]286returnfunc(*args)287orig_combo_row_separator_func(self,callback,user_data)288Gtk.ComboBox.set_row_separator_func=combo_row_separator_func289290# ComboBoxEntry291292classComboBoxEntry(Gtk.ComboBox):293def__init__(self,**kwds):294Gtk.ComboBox.__init__(self,has_entry=True,**kwds)295296defset_text_column(self,text_column):297self.set_entry_text_column(text_column)298299defget_text_column(self):300returnself.get_entry_text_column()301Gtk.ComboBoxEntry=ComboBoxEntry302303defcombo_box_entry_new():304returnGtk.ComboBoxEntry()305Gtk.combo_box_entry_new=combo_box_entry_new306307defcombo_box_entry_new_with_model(model):308returnGtk.ComboBoxEntry(model=model)309Gtk.combo_box_entry_new_with_model=combo_box_entry_new_with_model310311# Container312313definstall_child_property(container,flag,pspec):314warnings.warn('install_child_property() is not supported',315DeprecationWarning,stacklevel=2)316Gtk.Container.install_child_property=classmethod(install_child_property)317318defnew_text():319combo=Gtk.ComboBox()320model=Gtk.ListStore(str)321combo.set_model(model)322combo.set_entry_text_column(0)323returncombo324Gtk.combo_box_new_text=new_text325326defappend_text(self,text):327model=self.get_model()328model.append([text])329Gtk.ComboBox.append_text=append_text330Gtk.expander_new_with_mnemonic=Gtk.Expander.new_with_mnemonic331Gtk.icon_theme_get_default=Gtk.IconTheme.get_default332Gtk.image_new_from_pixbuf=Gtk.Image.new_from_pixbuf333Gtk.image_new_from_stock=Gtk.Image.new_from_stock334Gtk.image_new_from_animation=Gtk.Image.new_from_animation335Gtk.image_new_from_icon_set=Gtk.Image.new_from_icon_set336Gtk.image_new_from_file=Gtk.Image.new_from_file337Gtk.settings_get_default=Gtk.Settings.get_default338Gtk.window_set_default_icon=Gtk.Window.set_default_icon339Gtk.clipboard_get=Gtk.Clipboard.get340341#AccelGroup342Gtk.AccelGroup.connect_group=Gtk.AccelGroup.connect343344#StatusIcon345Gtk.status_icon_position_menu=Gtk.StatusIcon.position_menu346Gtk.StatusIcon.set_tooltip=Gtk.StatusIcon.set_tooltip_text347348# Scale349350orig_HScale=Gtk.HScale351orig_VScale=Gtk.VScale352353classHScale(orig_HScale):354def__init__(self,adjustment=None):355orig_HScale.__init__(self,adjustment=adjustment)356Gtk.HScale=HScale357358classVScale(orig_VScale):359def__init__(self,adjustment=None):360orig_VScale.__init__(self,adjustment=adjustment)361Gtk.VScale=VScale362363Gtk.stock_add=lambdaitems:None364365# Widget366367Gtk.widget_get_default_direction=Gtk.Widget.get_default_direction368orig_size_request=Gtk.Widget.size_request369370defsize_request(widget):371classSizeRequest(UserList):372def__init__(self,req):373self.height=req.height374self.width=req.width375UserList.__init__(self,[self.width,self.height])376returnSizeRequest(orig_size_request(widget))377Gtk.Widget.size_request=size_request378Gtk.Widget.hide_all=Gtk.Widget.hide379380defwidget_set_flags(widget,flags):381assertflags==Gtk.CAN_DEFAULT,flags382widget.set_can_default(True)383384Gtk.Widget.set_flags=widget_set_flags385386classBaseGetter(object):387def__init__(self,context):388self.context=context389390def__getitem__(self,state):391color=self.context.get_background_color(state)392returnGdk.Color(red=int(color.red*65535),393green=int(color.green*65535),394blue=int(color.blue*65535))395396classStyles(object):397def__init__(self,widget):398context=widget.get_style_context()399self.base=BaseGetter(context)400self.black=Gdk.Color(red=0,green=0,blue=0)401402classStyleDescriptor(object):403def__get__(self,instance,class_):404returnStyles(instance)405Gtk.Widget.style=StyleDescriptor()406407# gtk.unixprint408409classUnixPrint(object):410pass411unixprint=UnixPrint()412sys.modules['gtkunixprint']=unixprint413414# gtk.keysyms415416classKeysyms(object):417pass418keysyms=Keysyms()419sys.modules['gtk.keysyms']=keysyms420Gtk.keysyms=keysyms421fornameindir(Gdk):422ifname.startswith('KEY_'):423target=name[4:]424iftarget[0]in'0123456789':425target='_'+target426value=getattr(Gdk,name)427setattr(keysyms,target,value)428429Gtk.TreePath.__len__=lambdapath:path.get_depth()430431# TreeStore432433Gtk.TreeStore.get_iter_root=Gtk.TreeStore.get_iter_first434