home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / GDebi / SimpleGladeApp.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  12.6 KB  |  368 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. '''
  5.  SimpleGladeApp.py
  6.  Module that provides an object oriented abstraction to pygtk and libglade.
  7.  Copyright (C) 2004 Sandino Flores Moreno
  8. '''
  9. import os
  10. import sys
  11. import re
  12. import tokenize
  13. import gtk
  14. import gtk.glade as gtk
  15. import weakref
  16. import inspect
  17. __version__ = '1.0'
  18. __author__ = 'Sandino "tigrux" Flores-Moreno'
  19.  
  20. def bindtextdomain(app_name, locale_dir = None):
  21.     '''    
  22.     Bind the domain represented by app_name to the locale directory locale_dir.
  23.     It has the effect of loading translations, enabling applications for different
  24.     languages.
  25.  
  26.     app_name:
  27.         a domain to look for translations, tipically the name of an application.
  28.  
  29.     locale_dir:
  30.         a directory with locales like locale_dir/lang_isocode/LC_MESSAGES/app_name.mo
  31.         If omitted or None, then the current binding for app_name is used.
  32.     '''
  33.     
  34.     try:
  35.         import locale
  36.         import gettext
  37.         locale.setlocale(locale.LC_ALL, '')
  38.         gtk.glade.bindtextdomain(app_name, locale_dir)
  39.         gettext.install(app_name, locale_dir, unicode = 1)
  40.     except (IOError, locale.Error):
  41.         e = None
  42.         print 'Warning', app_name, e
  43.         
  44.         __builtins__.__dict__['_'] = lambda x: x
  45.  
  46.  
  47.  
  48. class SimpleGladeApp:
  49.     
  50.     def __init__(self, path, root = None, domain = None, **kwargs):
  51.         '''
  52.         Load a glade file specified by glade_filename, using root as
  53.         root widget and domain as the domain for translations.
  54.  
  55.         If it receives extra named arguments (argname=value), then they are used
  56.         as attributes of the instance.
  57.  
  58.         path:
  59.             path to a glade filename.
  60.             If glade_filename cannot be found, then it will be searched in the
  61.             same directory of the program (sys.argv[0])
  62.  
  63.         root:
  64.             the name of the widget that is the root of the user interface,
  65.             usually a window or dialog (a top level widget).
  66.             If None or ommited, the full user interface is loaded.
  67.  
  68.         domain:
  69.             A domain to use for loading translations.
  70.             If None or ommited, no translation is loaded.
  71.  
  72.         **kwargs:
  73.             a dictionary representing the named extra arguments.
  74.             It is useful to set attributes of new instances, for example:
  75.                 glade_app = SimpleGladeApp("ui.glade", foo="some value", bar="another value")
  76.             sets two attributes (foo and bar) to glade_app.
  77.         '''
  78.         if os.path.isfile(path):
  79.             self.glade_path = path
  80.         else:
  81.             glade_dir = os.path.dirname(sys.argv[0])
  82.             self.glade_path = os.path.join(glade_dir, path)
  83.         for key, value in kwargs.items():
  84.             
  85.             try:
  86.                 setattr(self, key, weakref.proxy(value))
  87.             continue
  88.             except TypeError:
  89.                 setattr(self, key, value)
  90.                 continue
  91.             
  92.  
  93.         
  94.         self.glade = None
  95.         self.install_custom_handler(self.custom_handler)
  96.         self.glade = self.create_glade(self.glade_path, root, domain)
  97.         if root:
  98.             self.main_widget = self.get_widget(root)
  99.         else:
  100.             self.main_widget = None
  101.         self.normalize_names()
  102.         self.add_callbacks(self)
  103.         self.new()
  104.  
  105.     
  106.     def __repr__(self):
  107.         class_name = self.__class__.__name__
  108.         if self.main_widget:
  109.             root = gtk.Widget.get_name(self.main_widget)
  110.             repr = '%s(path="%s", root="%s")' % (class_name, self.glade_path, root)
  111.         else:
  112.             repr = '%s(path="%s")' % (class_name, self.glade_path)
  113.         return repr
  114.  
  115.     
  116.     def new(self):
  117.         '''
  118.         Method called when the user interface is loaded and ready to be used.
  119.         At this moment, the widgets are loaded and can be refered as self.widget_name
  120.         '''
  121.         pass
  122.  
  123.     
  124.     def add_callbacks(self, callbacks_proxy):
  125.         '''
  126.         It uses the methods of callbacks_proxy as callbacks.
  127.         The callbacks are specified by using:
  128.             Properties window -> Signals tab
  129.             in glade-2 (or any other gui designer like gazpacho).
  130.  
  131.         Methods of classes inheriting from SimpleGladeApp are used as
  132.         callbacks automatically.
  133.  
  134.         callbacks_proxy:
  135.             an instance with methods as code of callbacks.
  136.             It means it has methods like on_button1_clicked, on_entry1_activate, etc.
  137.         '''
  138.         self.glade.signal_autoconnect(callbacks_proxy)
  139.  
  140.     
  141.     def normalize_names(self):
  142.         '''
  143.         It is internally used to normalize the name of the widgets.
  144.         It means a widget named foo:vbox-dialog in glade
  145.         is refered self.vbox_dialog in the code.
  146.  
  147.         It also sets a data "prefixes" with the list of
  148.         prefixes a widget has for each widget.
  149.         '''
  150.         for widget in self.get_widgets():
  151.             widget_name = gtk.Widget.get_name(widget)
  152.             prefixes_name_l = widget_name.split(':')
  153.             prefixes = prefixes_name_l[:-1]
  154.             widget_api_name = prefixes_name_l[-1]
  155.             widget_api_name = '_'.join(re.findall(tokenize.Name, widget_api_name))
  156.             gtk.Widget.set_name(widget, widget_api_name)
  157.             if hasattr(self, widget_api_name):
  158.                 raise AttributeError('instance %s already has an attribute %s' % (self, widget_api_name))
  159.                 continue
  160.             setattr(self, widget_api_name, widget)
  161.             if prefixes:
  162.                 gtk.Widget.set_data(widget, 'prefixes', prefixes)
  163.                 continue
  164.         
  165.  
  166.     
  167.     def add_prefix_actions(self, prefix_actions_proxy):
  168.         '''
  169.         By using a gui designer (glade-2, gazpacho, etc)
  170.         widgets can have a prefix in theirs names
  171.         like foo:entry1 or foo:label3
  172.         It means entry1 and label3 has a prefix action named foo.
  173.  
  174.         Then, prefix_actions_proxy must have a method named prefix_foo which
  175.         is called everytime a widget with prefix foo is found, using the found widget
  176.         as argument.
  177.  
  178.         prefix_actions_proxy:
  179.             An instance with methods as prefix actions.
  180.             It means it has methods like prefix_foo, prefix_bar, etc.
  181.         '''
  182.         prefix_s = 'prefix_'
  183.         prefix_pos = len(prefix_s)
  184.         
  185.         is_method = lambda t: callable(t[1])
  186.         
  187.         is_prefix_action = lambda t: t[0].startswith(prefix_s)
  188.         
  189.         drop_prefix = lambda .0: (k, w) = .0(k[prefix_pos:], w)
  190.         members_t = inspect.getmembers(prefix_actions_proxy)
  191.         methods_t = filter(is_method, members_t)
  192.         prefix_actions_t = filter(is_prefix_action, methods_t)
  193.         prefix_actions_d = dict(map(drop_prefix, prefix_actions_t))
  194.         for widget in self.get_widgets():
  195.             prefixes = gtk.Widget.get_data(widget, 'prefixes')
  196.             if prefixes:
  197.                 for prefix in prefixes:
  198.                     if prefix in prefix_actions_d:
  199.                         prefix_action = prefix_actions_d[prefix]
  200.                         prefix_action(widget)
  201.                         continue
  202.                 
  203.         
  204.  
  205.     
  206.     def custom_handler(self, glade, function_name, widget_name, str1, str2, int1, int2):
  207.         '''
  208.         Generic handler for creating custom widgets, internally used to
  209.         enable custom widgets (custom widgets of glade).
  210.  
  211.         The custom widgets have a creation function specified in design time.
  212.         Those creation functions are always called with str1,str2,int1,int2 as
  213.         arguments, that are values specified in design time.
  214.  
  215.         Methods of classes inheriting from SimpleGladeApp are used as
  216.         creation functions automatically.
  217.  
  218.         If a custom widget has create_foo as creation function, then the
  219.         method named create_foo is called with str1,str2,int1,int2 as arguments.
  220.         '''
  221.         
  222.         try:
  223.             handler = getattr(self, function_name)
  224.             return handler(str1, str2, int1, int2)
  225.         except AttributeError:
  226.             return None
  227.  
  228.  
  229.     
  230.     def gtk_widget_show(self, widget, *args):
  231.         '''
  232.         Predefined callback.
  233.         The widget is showed.
  234.         Equivalent to widget.show()
  235.         '''
  236.         widget.show()
  237.  
  238.     
  239.     def gtk_widget_hide(self, widget, *args):
  240.         '''
  241.         Predefined callback.
  242.         The widget is hidden.
  243.         Equivalent to widget.hide()
  244.         '''
  245.         widget.hide()
  246.  
  247.     
  248.     def gtk_widget_grab_focus(self, widget, *args):
  249.         '''
  250.         Predefined callback.
  251.         The widget grabs the focus.
  252.         Equivalent to widget.grab_focus()
  253.         '''
  254.         widget.grab_focus()
  255.  
  256.     
  257.     def gtk_widget_destroy(self, widget, *args):
  258.         '''
  259.         Predefined callback.
  260.         The widget is destroyed.
  261.         Equivalent to widget.destroy()
  262.         '''
  263.         widget.destroy()
  264.  
  265.     
  266.     def gtk_window_activate_default(self, window, *args):
  267.         '''
  268.         Predefined callback.
  269.         The default widget of the window is activated.
  270.         Equivalent to window.activate_default()
  271.         '''
  272.         widget.activate_default()
  273.  
  274.     
  275.     def gtk_true(self, *args):
  276.         '''
  277.         Predefined callback.
  278.         Equivalent to return True in a callback.
  279.         Useful for stopping propagation of signals.
  280.         '''
  281.         return True
  282.  
  283.     
  284.     def gtk_false(self, *args):
  285.         '''
  286.         Predefined callback.
  287.         Equivalent to return False in a callback.
  288.         '''
  289.         return False
  290.  
  291.     
  292.     def gtk_main_quit(self, *args):
  293.         '''
  294.         Predefined callback.
  295.         Equivalent to self.quit()
  296.         '''
  297.         self.quit()
  298.  
  299.     
  300.     def main(self):
  301.         '''
  302.         Starts the main loop of processing events.
  303.         The default implementation calls gtk.main()
  304.  
  305.         Useful for applications that needs a non gtk main loop.
  306.         For example, applications based on gstreamer needs to override
  307.         this method with gst.main()
  308.  
  309.         Do not directly call this method in your programs.
  310.         Use the method run() instead.
  311.         '''
  312.         gtk.main()
  313.  
  314.     
  315.     def quit(self):
  316.         '''
  317.         Quit processing events.
  318.         The default implementation calls gtk.main_quit()
  319.         
  320.         Useful for applications that needs a non gtk main loop.
  321.         For example, applications based on gstreamer needs to override
  322.         this method with gst.main_quit()
  323.         '''
  324.         gtk.main_quit()
  325.  
  326.     
  327.     def run(self):
  328.         '''
  329.         Starts the main loop of processing events checking for Control-C.
  330.  
  331.         The default implementation checks wheter a Control-C is pressed,
  332.         then calls on_keyboard_interrupt().
  333.  
  334.         Use this method for starting programs.
  335.         '''
  336.         
  337.         try:
  338.             self.main()
  339.         except KeyboardInterrupt:
  340.             self.on_keyboard_interrupt()
  341.  
  342.  
  343.     
  344.     def on_keyboard_interrupt(self):
  345.         '''
  346.         This method is called by the default implementation of run()
  347.         after a program is finished by pressing Control-C.
  348.         '''
  349.         pass
  350.  
  351.     
  352.     def install_custom_handler(self, custom_handler):
  353.         gtk.glade.set_custom_handler(custom_handler)
  354.  
  355.     
  356.     def create_glade(self, glade_path, root, domain):
  357.         return gtk.glade.XML(self.glade_path, root, domain)
  358.  
  359.     
  360.     def get_widget(self, widget_name):
  361.         return self.glade.get_widget(widget_name)
  362.  
  363.     
  364.     def get_widgets(self):
  365.         return self.glade.get_widget_prefix('')
  366.  
  367.  
  368.