#!/usr/bin/python2 ############################################################################## # Display a simple dialog box # Author: Chris Rouch # Date: 28/01/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 socket import os import crypt import stat import getopt import sys import random import string import signal import re import pango import time ###################################################################### # constants MOD1_MASK = 1 << 3 SHIFT_MASK = 1 << 0 CONTROL_MASK = 1 << 2 TITLESIZE = 24000 LABELSIZE = 18000 INFOIMG='/usr/share/pixmaps/gnome-info.png' QIMG='/usr/share/pixmaps/gnome-question.png' # set this as appropriate PLAYER='/usr/bin/ogg123' ###################################################################### class Dialog: def create_bbox(self,horizontal=gtk.TRUE, spacing=0,layout=gtk.BUTTONBOX_SPREAD): """Create a button box""" if horizontal: bbox = gtk.HButtonBox() else: bbox = gtk.VButtonBox() bbox.set_border_width(0) bbox.set_layout(layout) bbox.set_spacing(spacing) return bbox def reshow(self): """Re-display the dialog after a sleep""" if bell: gtk.gdk.beep() if sound: os.system("sinplay %s" % (sound)); self.window.show() return 0; def action(self, widget, event,data=None): """Button was clicked. Exit, optionally printing the value of the button""" if event.button == 1: # left button if quiet==0: print data gtk.main_quit() def sleep(self, widget, event,data=None): """Hide window for a while""" msecs=int(sleep) * 60 *1000; self.window.hide() timeout_handler_id = gtk.timeout_add(msecs, self.reshow) def addtitle(self,table,title,width,ifile=None): """Add a title""" if ifile: image = gtk.Image() image.set_from_file(ifile) table.attach(image, 0, 1, 0, 1,xpadding=1,ypadding=1) left=1 else: left=0 label = gtk.Label(title); event_box = gtk.EventBox() event_box.set_name('mytitle') font_desc = pango.FontDescription('Serif') label.modify_font(font_desc) label.set_name('mytitle') label.set_use_markup(gtk.TRUE) tstr='' + title + '' label.set_markup(tstr) table.attach(label, left, width, 0, 1,xpadding=1,ypadding=1) def addbutton(self,bbox,text,x): if re.search('^yes$',text,re.IGNORECASE): button = gtk.Button(None,gtk.STOCK_YES); elif re.search('^no$',text,re.IGNORECASE): button = gtk.Button(None,gtk.STOCK_NO); elif re.search('^ok$',text,re.IGNORECASE): button = gtk.Button(None,gtk.STOCK_OK); elif re.search('^cancel$',text,re.IGNORECASE): button = gtk.Button(None,gtk.STOCK_CANCEL); else: button = gtk.Button(); button.set_name('mybutton') label = gtk.Label(text); label.set_name('mytitle') label.set_use_markup(gtk.TRUE) tstr='' + text + '' label.set_markup(text) button.add(label); font_desc = pango.FontDescription('Serif') button.modify_font(font_desc) button.connect("button_press_event", self.action,text) bbox.add(button) def addsleep(self,bbox,sleep,x): """Add a button""" sleep=int(sleep); if sleep == 1: text="Sleep for 1 minute" else: text="Sleep for %s minutes" % (sleep) button = gtk.Button(); font_desc = pango.FontDescription('Serif') button.modify_font(font_desc) label = gtk.Label(text); button.add(label); button.connect("button_press_event", self.sleep,sleep) bbox.add(button) def __init__(self,args,sleep): """Create the dialog""" size=len(args) if size == 0: args=['OK','Cancel'] size=2 ifile=QIMG elif size==1: ifile=INFOIMG size=2 else: ifile=QIMG if sleep: size+=1 # Create a new window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_name("pyinfo") # Set the window title self.window.set_title(title) # Sets the border width of the window. self.window.set_border_width(10) # Create a table table = gtk.Table(2, size, gtk.FALSE) table.set_col_spacings(10) # Put the table in the main window self.window.add(table) self.addtitle(table,title,2,ifile) # use a hbox for the buttons buttonbox=self.create_bbox(gtk.TRUE, 0, gtk.BUTTONBOX_SPREAD) x=0 for arg in args: self.addbutton(buttonbox,arg,x) x+=1 if sleep: self.addsleep(buttonbox,sleep,x) table.attach(buttonbox, 0, size, 1, 2,xpadding=1,ypadding=1) self.window.show_all() if bell: gtk.gdk.beep() if sound: os.spawnv(os.P_NOWAIT,PLAYER,[PLAYER,sound]) ###################################################################### def main(): gtk.main() return 0 if __name__ == "__main__": bell=0 quiet=0 title="No Title" sleep=0 sound="" opts,args = getopt.getopt(sys.argv[1:],'qt:bs:S:') for opt in opts: if opt[0] == '-q': quiet=1 elif opt[0] == '-t': title=opt[1] elif opt[0] == '-b': bell=1 elif opt[0] == '-s': sleep=opt[1] elif opt[0] == '-S': sound=opt[1] Dialog(args,sleep) main() # only get here if we quit ######################################################################