Package screenlets :: Module menu

[hide private]
[frames] | no frames]

# Source Code for Module screenlets.menu

      1  # This application is released under the GNU General Public License
      2  # v3 (or, at your option, any later version). You can find the full
      3  # text of the license under http://www.gnu.org/licenses/gpl.txt.
      4  # By using, editing and/or distributing this software you agree to
      5  # the terms and conditions of this license.
      6  # Thank you for using free software!
      7
      8  # a very hackish, XML-based menu-system (c) RYX (Rico Pfaus) 2007
      9  #
     10  # NOTE: This thing is to be considered a quick hack and it lacks on all ends.
     11  #       It should be either improved (and become a OOP-system ) or removed
     12  #       once there is a suitable alternative ...
     13  #
     14
     15  import glob, gtk
     16  import xml.dom.minidom
     17  from xml.dom.minidom import Node
     18  import os
     19  import gettext
     20
     21  gettext.textdomain('screenlets')
     22  gettext.bindtextdomain('screenlets', '/usr/share/locale')
     23
     24 -def _(s):
     25          return gettext.gettext(s)
     26
     27 -def add_menuitem (menu, label, callback=None, cb_data=None):
     28          """Convenience function to create a menuitem, connect
     29          a callback, and add the menuitem to menu."""
     30          if label == "-":
     31                  item = gtk.SeparatorMenuItem()
     32          else:
     33                  item = gtk.MenuItem(label)
     34          return add_menuitem_with_item(menu, item, callback, cb_data)
     35
     36 -def add_image_menuitem (menu, stock, label=None, callback=None, cb_data=None):
     37          """Convenience function to create an ImageMenuItem, connect
     38          a callback, and add the menuitem to menu."""
     39          item = ImageMenuItem(stock, label)
     40          return add_menuitem_with_item(menu, item, callback, cb_data)
     41
     42 -def add_menuitem_with_item (menu, item, callback=None, cb_data=None):
     43          """Convenience function to add a menuitem to a menu
     44          and connect a callback."""
     45          if callback:
     46                  if cb_data:
     47                          item.connect("activate", callback, cb_data)
     48                  else:
     49                          item.connect("activate", callback)
     50          menu.append(item)
     51          item.show()
     52          return item
     53
     63
    138
    168
    169 -def imageitem_from_name (filename, label, icon_size=32):
    170          """Creates a new gtk.ImageMenuItem from a given icon/filename.
    171          If an absolute path is not given, the function checks for the name
    172          of the icon within the current gtk-theme."""
    173          item = gtk.ImageMenuItem(label)
    174          image = gtk.Image()
    175          if filename and filename[0]=='/':
    176                  # load from file
    177                  try:
    178                          image.set_from_file(filename)
    179                          pb = image.get_pixbuf()
    180                          # rescale, if too big
    181                          if pb.get_width() > icon_size :
    182                                  pb2 = pb.scale_simple(
    183                                          icon_size, icon_size,
    184                                          gtk.gdk.INTERP_HYPER)
    185                                  image.set_from_pixbuf(pb2)
    186                          else:
    187                                  image.set_from_pixbuf(pb)
    188                  except:
    189                          print _("Error while creating image from file: %s") % filename
    190                          return None
    191          else:
    192                  image.set_from_icon_name(filename, 3)   # TODO: use better size
    193          if image:
    194                  item.set_image(image)
    195          return item
    196
    197 -def read_desktop_file (filename):
    198          """Read ".desktop"-file into a dict
    199          NOTE: Should use utils.IniReader ..."""
    200          list = {}
    201          f=None
    202          try:
    203                  f = open (filename, "r")
    204          except:
    205                  print _("Error: file %s not found.") % filename
    206          if f:
    207                  lines = f.readlines()
    208                  for line in lines:
    209                          if line[0] != "#" and line !="\n" and line[0] != "[":
    210                                  ll = line.split('=', 1)
    211                                  if len(ll) > 1:
    212                                          list[ll[0]] = ll[1].replace("\n", "")
    213          return list
    214
    215  #-----------------------------------------------
    216  # Classes
    217  #-----------------------------------------------
    218
    289
    309
    310
    351