#!/usr/bin/python2 ############################################################################## # Run applications by clicking the icons. Similar to gnome quicklaunch # Author: Chris Rouch # Date: 13/10/2003 # Changelog: # # Copyright (c) Chris Rouch 2003 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # or see http://www.gnu.org/licenses/gpl.txt ############################################################################## import gtk import os import getopt import sys import re import pango ###################################################################### # constants MOD1_MASK = 1 << 3 SHIFT_MASK = 1 << 0 CONTROL_MASK = 1 << 2 ###################################################################### class ButtonBox: def action(self, widget, event,data=None): """Get the OS to run data""" if data: if event.button == 1: # left button if re.match("\$HOME",data): data=re.sub("\$HOME",os.environ["HOME"],data) parts = re.split(' ',data) os.spawnv(os.P_NOWAIT,parts[0],parts) print parts[0] def keypress(self, widget, event,*args): """check for keypresses, exit on ctrl-q""" key = event.keyval if event.state & CONTROL_MASK: if chr(key) == 'q': gtk.main_quit() def addtitle(self,table,title,width): """add a title to the top of the table""" label = gtk.Label(title); font_desc = pango.FontDescription('Serif') label.modify_font(font_desc) label.set_name('mytitle') label.set_use_markup(gtk.TRUE) table.attach(label, 0, width, 0, 1,xpadding=0,ypadding=0) def addbutton(self,table,x,y,action,icon,tip): """add a button to the table at x,y""" button = gtk.Button(); button.set_name('mybutton') image = gtk.Image() if re.match("\$HOME",icon): icon=re.sub("\$HOME",os.environ["HOME"],icon) image.set_from_file(icon) image.set_padding(0,0) button.add(image); button.connect("button_press_event", self.action,action) button.set_relief(gtk.RELIEF_NONE) self.tooltips.set_tip(button, tip,"") table.attach(button, x, x+1, y, y+1,xpadding=0,ypadding=0) def __init__(self,blist,rows=1,cols=1,title=None,name=None): """Create a button box according to the supplied blist""" # Create a new window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_name("buttonbox") self.window.connect("key_press_event", self.keypress) # create tooltips object self.tooltips = gtk.Tooltips() # Create a table table = gtk.Table(rows, cols, gtk.FALSE) table.set_col_spacings(0) table.set_row_spacings(0) # Put the table in the main window self.window.add(table) # Set the window title x=0 y=0 if title: print title self.window.set_title(title) self.addtitle(table,title,cols) y=y+1 elif name: self.window.set_title(name) # add the buttons for tuple in blist: tip=tuple[0].strip() action=tuple[1].strip() icon=tuple[2].strip() self.addbutton(table,x,y,action,icon,tip); x=x+1 if x>= cols: x=0 y=y+1 self.window.show_all() ###################################################################### def ReadArgs(file): """parse a file for a list of buttons format is tooltip,action,icon $HOME is understood, otherwise use full path """ buttons=[] try: f=open(file) except: print file + " not found" return for line in f.xreadlines(): line=line.rstrip() if line: buttons.append(re.split(",",line)) return buttons def main(): gtk.main() return 0 if __name__ == "__main__": file='' rows=-1 cols=-1 title='' name='' buttons=[] opts,args = getopt.getopt(sys.argv[1:],'d:f:t:n:r:c:hv') for opt in opts: if opt[0] == '-f': file=opt[1] elif opt[0] == '-h': rows=1 elif opt[0] == '-v': cols=1 elif opt[0] == '-n': name=opt[1] elif opt[0] == '-t': title=opt[1] elif opt[0] == '-d': dir=opt[1] elif opt[0] == '-r': rows=opt[1] elif opt[0] == '-c': cols=opt[1] if file: buttons=ReadArgs(file) else: for arg in args: buttons.append(re.split(",",arg)) if buttons: count=len(buttons) if rows <= 0: if cols <=0: # default horizontal strip rows=1 cols=count else: rows=int(count)/int(cols) if int(count)%int(cols) > 0: rows+=1 else: cols=int(count)/int(rows) if int(count)%int(rows) > 0: cols+=1 cols=int(cols) rows=int(rows) ButtonBox(buttons,rows,cols,title,name) main() # only get here if we quit