home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / lib / pygtk / 2.0 / demos / ui_manager.py < prev    next >
Text File  |  2010-05-11  |  7KB  |  192 lines

  1. #!/usr/bin/env python
  2. '''UI Manager
  3.  
  4. The GtkUIManager object allows the easy creation of menus
  5. from an array of actions and a description of the menu hierarchy.
  6. '''
  7. # pygtk version: Maik Hertha <maik.hertha@berlin.de>
  8.  
  9. import gobject
  10. import gtk
  11.  
  12. def activate_action(action):
  13.     print 'Action "%s" activated' % action.get_name()
  14.  
  15. def activate_radio_action(action, current):
  16.     print 'Radio action "%s" selected'% current.get_name()
  17.  
  18. entries = (
  19.   ( "FileMenu", None, "_File" ),               # name, stock id, label
  20.   ( "PreferencesMenu", None, "_Preferences" ), # name, stock id, label
  21.   ( "ColorMenu", None, "_Color"  ),            # name, stock id, label
  22.   ( "ShapeMenu", None, "_Shape" ),             # name, stock id, label
  23.   ( "HelpMenu", None, "_Help" ),               # name, stock id, label
  24.   ( "New", gtk.STOCK_NEW,                      # name, stock id
  25.     "_New", "<control>N",                      # label, accelerator
  26.     "Create a new file",                       # tooltip
  27.     activate_action ),
  28.   ( "Open", gtk.STOCK_OPEN,                    # name, stock id
  29.     "_Open","<control>O",                      # label, accelerator
  30.     "Open a file",                             # tooltip
  31.     activate_action ),
  32.   ( "Save", gtk.STOCK_SAVE,                    # name, stock id
  33.     "_Save","<control>S",                      # label, accelerator
  34.     "Save current file",                       # tooltip
  35.     activate_action ),
  36.   ( "SaveAs", gtk.STOCK_SAVE,                  # name, stock id
  37.     "Save _As...", None,                       # label, accelerator
  38.     "Save to a file",                          # tooltip
  39.     activate_action ),
  40.   ( "Quit", gtk.STOCK_QUIT,                    # name, stock id
  41.     "_Quit", "<control>Q",                     # label, accelerator
  42.     "Quit",                                    # tooltip
  43.     activate_action ),
  44.   ( "About", None,                             # name, stock id
  45.     "_About", "<control>A",                    # label, accelerator
  46.     "About",                                   # tooltip
  47.     activate_action ),
  48.   ( "Logo", "demo-gtk-logo",                   # name, stock id
  49.      None, None,                               # label, accelerator
  50.     "GTK+",                                    # tooltip
  51.     activate_action ),
  52. )
  53.  
  54. toggle_entries = (
  55.   ( "Bold", gtk.STOCK_BOLD,                    # name, stock id
  56.      "_Bold", "<control>B",                    # label, accelerator
  57.     "Bold",                                    # tooltip
  58.     activate_action,
  59.     True ),                                    # is_active
  60. )
  61.  
  62. (
  63.   COLOR_RED,
  64.   COLOR_GREEN,
  65.   COLOR_BLUE
  66. ) = range(3)
  67.  
  68. color_entries = (
  69.   ( "Red", None,                               # name, stock id
  70.     "_Red", "<control>R",                      # label, accelerator
  71.     "Blood", COLOR_RED ),                      # tooltip, value
  72.   ( "Green", None,                             # name, stock id
  73.     "_Green", "<control>G",                    # label, accelerator
  74.     "Grass", COLOR_GREEN ),                    # tooltip, value
  75.   ( "Blue", None,                              # name, stock id
  76.     "_Blue", "<control>B",                     # label, accelerator
  77.     "Sky", COLOR_BLUE ),                       # tooltip, value
  78. )
  79.  
  80. (
  81.   SHAPE_SQUARE,
  82.   SHAPE_RECTANGLE,
  83.   SHAPE_OVAL,
  84. ) = range(3)
  85.  
  86. # GtkRadioActionEntry
  87. shape_entries = (
  88.   ( "Square", None,                            # name, stock id
  89.     "_Square", "<control>S",                   # label, accelerator
  90.     "Square",  SHAPE_SQUARE ),                 # tooltip, value
  91.   ( "Rectangle", None,                         # name, stock id
  92.     "_Rectangle", "<control>R",                # label, accelerator
  93.     "Rectangle", SHAPE_RECTANGLE ),            # tooltip, value
  94.   ( "Oval", None,                              # name, stock id
  95.     "_Oval", "<control>O",                     # label, accelerator
  96.     "Egg", SHAPE_OVAL ),                       # tooltip, value
  97. )
  98.  
  99. ui_info = \
  100. '''<ui>
  101.   <menubar name='MenuBar'>
  102.     <menu action='FileMenu'>
  103.       <menuitem action='New'/>
  104.       <menuitem action='Open'/>
  105.       <menuitem action='Save'/>
  106.       <menuitem action='SaveAs'/>
  107.       <separator/>
  108.       <menuitem action='Quit'/>
  109.     </menu>
  110.     <menu action='PreferencesMenu'>
  111.       <menu action='ColorMenu'>
  112.         <menuitem action='Red'/>
  113.         <menuitem action='Green'/>
  114.         <menuitem action='Blue'/>
  115.       </menu>
  116.       <menu action='ShapeMenu'>
  117.         <menuitem action='Square'/>
  118.         <menuitem action='Rectangle'/>
  119.         <menuitem action='Oval'/>
  120.       </menu>
  121.       <menuitem action='Bold'/>
  122.     </menu>
  123.     <menu action='HelpMenu'>
  124.       <menuitem action='About'/>
  125.     </menu>
  126.   </menubar>
  127.   <toolbar  name='ToolBar'>
  128.     <toolitem action='Open'/>
  129.     <toolitem action='Quit'/>
  130.     <separator action='Sep1'/>
  131.     <toolitem action='Logo'/>
  132.   </toolbar>
  133. </ui>'''
  134.  
  135. class UIManagerDemo(gtk.Window):
  136.  
  137.     def __init__(self, parent=None):
  138.         gtk.Window.__init__(self)
  139.         try:
  140.             self.set_screen(parent.get_screen())
  141.         except AttributeError:
  142.             self.connect('destroy', lambda *w: gtk.main_quit())
  143.         self.set_title(self.__class__.__name__)
  144.         self.set_border_width(0)
  145.  
  146.         actions = gtk.ActionGroup("Actions")
  147.         actions.add_actions(entries)
  148.         actions.add_toggle_actions(toggle_entries)
  149.         actions.add_radio_actions(color_entries, COLOR_RED, activate_radio_action)
  150.         actions.add_radio_actions(shape_entries, SHAPE_OVAL, activate_radio_action)
  151.  
  152.         ui = gtk.UIManager()
  153.         ui.insert_action_group(actions, 0)
  154.         self.add_accel_group(ui.get_accel_group())
  155.  
  156.         try:
  157.             mergeid = ui.add_ui_from_string(ui_info)
  158.         except gobject.GError, msg:
  159.             print "building menus failed: %s" % msg
  160.  
  161.         box1 = gtk.VBox(False, 0)
  162.         self.add(box1)
  163.  
  164.         box1.pack_start(ui.get_widget("/MenuBar"), False, False, 0)
  165.  
  166.         label = gtk.Label("Type\n<alt>\nto start")
  167.         label.set_size_request(200, 200)
  168.         label.set_alignment(0.5, 0.5)
  169.         box1.pack_start(label, True, True, 0)
  170.  
  171.         separator = gtk.HSeparator()
  172.         box1.pack_start(separator, False, True, 0)
  173.  
  174.         box2 = gtk.VBox(False, 10)
  175.         box2.set_border_width(10)
  176.         box1.pack_start(box2, False, True, 0)
  177.  
  178.         button = gtk.Button("close")
  179.         button.connect("clicked", lambda b, w=self: w.destroy())
  180.         box2.pack_start(button, True, True, 0)
  181.         button.set_flags(gtk.CAN_DEFAULT)
  182.         button.grab_default()
  183.  
  184.         self.show_all()
  185.  
  186. def main():
  187.     UIManagerDemo()
  188.     gtk.main()
  189.  
  190. if __name__ == '__main__':
  191.     main()
  192.