clMenu.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2009 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 import gtk
00022 
00023 import os
00024 
00025 import functions
00026 
00027 ## @package clMenu
00028 # Holds the Pyjama MenuBar Class
00029 
00030 ## a gtk.MenuBar with some additional functions
00031 # for creating menus
00032 class Menu(gtk.MenuBar):
00033     ## The Constructor
00034     # @param self Object Pointer
00035     # @param window Reference to pyjama's window class
00036     def __init__(self, window):
00037         ## Pyjama reference
00038         self.pyjama = window.main
00039 
00040         ## Fullscreen indicator
00041         self.bolFullScreen = False
00042 
00043         gtk.MenuBar.__init__(self)
00044 
00045         fake_array_for_translations = [
00046             _("File"), _("View"), _("Browse"), _("Extras"), _("Help"),          # Root menus
00047             _("Save"), _("Quit"),                                  # Sub File
00048             _("About"), _("Help"), _("Developer Information"), _("Report a Bug"),     # Sub Help
00049             _("Plugins"), _("Preferences"),                                  # Sub Extras
00050             _("Full Reload"), _("Fast Reload"), _("Fullscreen")    # Sub View
00051         ]
00052 
00053         menus = ["File", "View", "Browse", "Extras", "Help"]  
00054 
00055         ## Dictionary holding all root menus
00056         self.rootmenus = {}
00057         ## Dictionary holding all Menuitems
00058         self.submenus = {}
00059         
00060         for menu in menus:
00061             self.append_root(_(menu), menu)
00062 
00063         sub1 = ["Quit"]  
00064         self.create_submenu(rootmenu=self.get_rootmenu("File"), submenu=sub1)
00065 
00066         sub2 = ["Plugins", "---"]
00067         self.create_submenu(rootmenu=self.get_rootmenu("Extras"), submenu=sub2)
00068 
00069         sub3 = ["Help", "---", "Developer Information", "Report a Bug", "Translation", "---", "About"]  
00070         hlp = self.get_rootmenu("Help")
00071         if self.pyjama.settings.get_value("appearance", "justify_rightHelp", False):
00072             hlp.set_right_justified(True)
00073         self.create_submenu(rootmenu=hlp, submenu=sub3)
00074 
00075         sub4 = ["Full Reload", "Fast Reload", "---", "Fullscreen"]
00076         view = self.create_submenu(rootmenu=self.get_rootmenu("View"), submenu=sub4)
00077 
00078         sub5 = []  
00079         browse = self.create_submenu(rootmenu=self.get_rootmenu("Browse"), submenu=sub5)
00080 
00081         # 
00082         # Connect
00083         #
00084         about = self.get_submenu("About")
00085         self.set_item_image(about, gtk.STOCK_ABOUT)
00086         about.connect("activate", self.pyjama.window.show_about)
00087 
00088         help = self.get_submenu("Help")
00089         self.set_item_image(help, gtk.STOCK_HELP)
00090         help.connect("activate", self.ev_help)
00091 
00092         report = self.get_submenu("Report a Bug")
00093         self.set_item_image(report, "green_beetle.png")
00094         report.connect("activate", self.cb_report)
00095 
00096         developer = self.get_submenu("Developer Information")
00097         self.set_item_image(developer, gtk.STOCK_HELP)
00098         developer.connect("activate", self.cb_developer)
00099 
00100         trans = self.get_submenu("Translation")
00101         self.set_item_image(trans, "clip_note_1.png")
00102         trans.connect("activate", self.cb_trans)
00103 
00104         quit = self.get_submenu("Quit")
00105         self.set_item_image(quit, gtk.STOCK_QUIT)
00106         quit.connect("activate", self.pyjama.window.really_quit)
00107 
00108         reload = self.get_submenu("Full Reload")
00109         self.set_item_image(reload, gtk.STOCK_REFRESH)
00110         reload.connect("activate", self.pyjama.reload_current_page, True)
00111 
00112         reload_cached = self.get_submenu("Fast Reload")
00113         self.set_item_image(reload_cached, gtk.STOCK_REFRESH)
00114         reload_cached.connect("activate", self.pyjama.reload_current_page, False)
00115 
00116         fullscreen = self.get_submenu("Fullscreen")
00117         self.set_item_image(fullscreen, gtk.STOCK_FULLSCREEN)
00118         fullscreen.connect("activate", self.on_bFullScreen_clicked)
00119 
00120         plugins = self.get_submenu("Plugins")
00121         self.set_item_image(plugins, gtk.STOCK_ABOUT)
00122         plugins.connect("activate", self.pyjama.show_plugins)
00123 
00124 
00125         # Preferences menu is added
00126         # in clWindows.py at the end of
00127         # __init__()
00128 
00129 
00130         #
00131         # Accelerators
00132         #
00133         self.__accel_group = gtk.AccelGroup()
00134         quit.add_accelerator("activate", self.__accel_group, ord("q"), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
00135         help.add_accelerator("activate", self.__accel_group, gtk.keysyms.F1, 0, gtk.ACCEL_VISIBLE)
00136         reload.add_accelerator("activate", self.__accel_group, ord("r"), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
00137         reload_cached.add_accelerator("activate", self.__accel_group, gtk.keysyms.F5, 0, gtk.ACCEL_VISIBLE)
00138         fullscreen.add_accelerator("activate", self.__accel_group, gtk.keysyms.F11, 0, gtk.ACCEL_VISIBLE)
00139         self.pyjama.window.add_accel_group(self.__accel_group)
00140 #        self.pyjama.window.add_accel_group(self.__accel_group)
00141         
00142 
00143         self.show()
00144 
00145     ## Find a Rootmenu
00146     # @param self Object Pointer
00147     # @param name Root menu's name
00148     # @return gtk.MenuItem
00149     def get_rootmenu(self, name):
00150 #        for menu in self.rootmenus:
00151 #            if menu.id == name:
00152 #                return menu
00153 #        return None
00154         return self.rootmenus[name.lower()]
00155 
00156     ## Find a Menuitem
00157     # @param self Object Pointer
00158     # @param name Submenu's name
00159     # @return gtk.ImageMenuItem
00160     def get_submenu(self, name):
00161 #        for menu in self.submenus:
00162 #            if menu.id == name:
00163 #                return menu
00164 #        return None
00165         return self.submenus[name.lower()]
00166 
00167     #
00168     # Root Menu Placing
00169     #
00170     ## Add a menu to the end of the menubar
00171     # @param self Object Pointer
00172     # @param name Name of the menu to create
00173     # @param id Identifier of the menu to create
00174     # @return A gtk.MenuItem
00175     def append_root(self, name, id):
00176         id = id.lower()
00177         mnu = gtk.MenuItem(name)
00178         mnu.id = id
00179         mnu.position = len(self.rootmenus)
00180         self.append(mnu)
00181         mnu.show()
00182 #        self.rootmenus.append(mnu)
00183         self.rootmenus[id] = mnu
00184         return mnu
00185 
00186     ## Add a menu to the start of the menubar
00187     # @param self Object Pointer
00188     # @param name Name of the menu to create
00189     # @param id Identifier of the menu to create
00190     # @return A gtk.MenuItem
00191     def prepend_root(self, name, id):
00192         id = id.lower()
00193         mnu = gtk.MenuItem(name)
00194         mnu.id = id
00195         mnu.position = len(self.rootmenus)
00196         self.prepend(mnu)
00197         mnu.show()
00198 #        self.rootmenus.insert(0, mnu)
00199         self.rootmenus[id] = mnu
00200         return mnu
00201 
00202     ## Inserts a Menu at a particular position
00203     # @param self Object Pointer
00204     # @param pos Position of the menu to create
00205     # @param name Name of the menu to create
00206     # @param id Identifier of the menu to create
00207     # @return A gtk.MenuItem
00208     def insert_root_pos(self, pos, name, id):
00209         id = id.lower()
00210         mnu = gtk.MenuItem(name)
00211         mnu.id = id
00212         mnu.position = pos
00213         self.insert(mnu, pos)
00214         mnu.show()
00215 #        self.rootmenus.insert(pos, mnu)
00216         self.rootmenus[id] = mnu
00217         return mnu
00218 
00219     ## Inserts a Menu after another rootmenu
00220     # @param self Object Pointer
00221     # @param after Name of a rootmenu
00222     # @param name Name of the menu to create
00223     # @param id Identifier of the menu to create
00224     # @return A gtk.MenuItem
00225     def insert_root_after(self, after, name, id):
00226         after = self.get_rootmenu(after)
00227         if after != None:
00228             return self.insert_root_pos(after.position + 1, name, id)
00229         return None
00230 
00231     ## Inserts a Menu before another rootmenu
00232     # @param self Object Pointer
00233     # @param before Name of a rootmenu
00234     # @param name Name of the menu to create
00235     # @param id Identifier of the menu to create
00236     # @return A gtk.MenuItem
00237     def insert_root_before(self, before, name, id):
00238         before = self.get_rootmenu(before)
00239         if before != None:
00240             if before.position != 0:
00241                 return self.insert_root_pos(before.position , name, id)
00242             else:
00243                 return self.prepend_root(name, id)
00244         return None
00245     
00246     #
00247     # Submenu
00248     #
00249     ## Sets an image for a gtk.ImageMenuItem.
00250     # Strings and gtk.STOCK_ICONs allowed for image. 
00251     # @param self Object Pointer
00252     # @param submenu A gtk.MenuItem
00253     # @param image A gtk.STOCK_IMAGE or an URI for a local file
00254     # @param size A gtk.STOCK_SIZE
00255     # @return None
00256     ## \todo Allowing size also being a 2-tuple containing width and height
00257     def set_item_image(self, menuitem, image, size=gtk.ICON_SIZE_MENU):
00258         if image in gtk.stock_list_ids():
00259             # StockIcon
00260             img = gtk.Image()
00261             img.set_from_stock(image, gtk.ICON_SIZE_MENU)
00262             menuitem.set_image(img)
00263         else:
00264             # from file
00265             w, h = gtk.icon_size_lookup(size)
00266             img = gtk.Image()
00267             if not os.path.exists(image):
00268                 image = os.path.join(functions.install_dir(), "images", image)
00269                 if not os.path.exists(image):
00270                     print ("Image not found")
00271                     return -1
00272             pix = gtk.gdk.pixbuf_new_from_file_at_size(image, w, h)
00273             img.set_from_pixbuf(pix)
00274             menuitem.set_image(img)
00275     
00276     ## Creates and populates a submenu from a list of items.
00277     # @param self Object Pointer
00278     # @param rootmenu The menu which gets the submenu
00279     # @param submenu A list with Strings.
00280     # For "---" strings a gtkSeperatorMenuItem will be created
00281     # @return A gtk.Menu
00282     def create_submenu(self, rootmenu, submenu):
00283         mnu = gtk.Menu()
00284         for sub in submenu:
00285             if sub != "---":
00286                 tmp = gtk.ImageMenuItem(_(sub.replace("_", "__")))
00287                 tmp.id = sub.lower()
00288             else:
00289                 tmp = gtk.SeparatorMenuItem()
00290                 tmp.id = None
00291             mnu.append(tmp)
00292             tmp.show()
00293 #            self.submenus.append(tmp)
00294             self.submenus[tmp.id] = tmp
00295 #        tmp.set_property("use-underline", False)
00296         mnu.show()
00297         rootmenu.set_submenu(mnu)
00298         return mnu
00299 
00300     ## Appends an gtk.ImageMenuItem to a Menu.
00301     # @param self Object Pointer
00302     # @param rootmenu The rootmenu which submenu is going to be extended
00303     # @param name The name of the appended item.
00304     # If the name is "---" a gtkSeperatorMenuItem will be created
00305     # @param id Identifier of the appended item
00306     # @return a gtk.ImageMenuItem
00307     def append_entry(self, rootmenu, name, id):
00308         id = id.lower()
00309         if name != "---":
00310             mnu = gtk.ImageMenuItem(name.replace("_", "__"), False)
00311             mnu.id = id
00312         else:
00313             mnu = gtk.SeparatorMenuItem()
00314             mnu.id = id
00315         sub = rootmenu.get_submenu()
00316         sub.append(mnu)
00317         self.submenus[id] = mnu
00318         mnu.show()
00319         return mnu
00320 
00321     ## Inserts an gtk.ImageMenuItem into a Menu.
00322     # @param self Object Pointer
00323     # @param pos Position
00324     # @param rootmenu The rootmenu which submenu is going to be extended
00325     # @param name The name of the inserted item.
00326     # If the name is "---" a gtkSeperatorMenuItem will be created
00327     # @param id Identifier of the appended item
00328     # @return a gtk.ImageMenuItem
00329     def insert_entry(self, pos, rootmenu, name, id):
00330         id = id.lower()
00331         if name != "---":
00332             mnu = gtk.ImageMenuItem(name.replace("_", "__"), False)
00333             mnu.id = id
00334         else:
00335             mnu = gtk.SeparatorMenuItem()
00336             mnu.id = id
00337         sub = rootmenu.get_submenu()
00338         sub.insert(mnu, pos)
00339         self.submenus[id] = mnu
00340         mnu.show()
00341         return mnu
00342 
00343     #
00344     # Callbacks
00345     # 
00346     ## Called when the developer MenuItem was clicked
00347     # @param Object Pointer
00348     # @param widget The MenuItem clicked
00349     def cb_developer(self, widget):
00350         self.pyjama.Events.raise_event("open_url", "http://xn--ngel-5qa.de/pyjama/html", force_default=True)
00351 
00352     ## Called when the translation MenuItem was clicked
00353     # @param Object Pointer
00354     # @param widget The MenuItem clicked
00355     def cb_trans(self, widget):
00356         self.pyjama.Events.raise_event("open_url", "http://translations.launchpad.net/pyjama", force_default=True)
00357 
00358     ## Called when the report-a-bug MenuItem was clicked
00359     # @param Object Pointer
00360     # @param widget The MenuItem clicked
00361     def cb_report(self, widget):
00362         self.pyjama.Events.raise_event("open_url", "https://bugs.launchpad.net/pyjama", force_default=True)
00363 
00364     ## Called when the help MenuItem was clicked
00365     # @param Object Pointer
00366     # @param widget The MenuItem clicked
00367     def ev_help(self, widget):
00368         self.pyjama.Events.raise_event("open_url", "https://answers.launchpad.net/pyjama", force_default=True)
00369 
00370     ## Called when the Fullscreen MenuItem was clicked.
00371     # If you want to switch to Fullscreen, this methode
00372     # might help.
00373     # @param self Object Pointer
00374     # @param widget The MenuItem clicked
00375     def on_bFullScreen_clicked(self, widget=None):
00376         if not self.bolFullScreen:
00377 #            self.pyjama.window.toolbar.bFullScreen.set_stock_id(gtk.STOCK_LEAVE_FULLSCREEN)
00378             self.bolFullScreen = True
00379             self.pyjama.window.fullscreen()
00380         else:
00381 #            self.pyjama.window.toolbar.bFullScreen.set_stock_id(gtk.STOCK_FULLSCREEN)
00382             self.bolFullScreen = False
00383             self.pyjama.window.unfullscreen()

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8