#!/usr/bin/python # garclock - xarclock clone in PyGTK2 # Copyright (C) 2003 Nicolas Centa. # 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, 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, USA. from time import * import pygtk pygtk.require ('2.0') import gtk import pango from math import * import sys origin = 2*pi-pi/2 if len (sys.argv) > 1: try: origin = 2*pi-(int (sys.argv [1])*10*pi/60) except ValueError: print "Usage: garclock [origin]" def win_delete_event (win, event): gtk.main_quit () def drw_expose_event (drw, event): drw_update (drw) def drw_update (drw): pix = gtk.gdk.Pixmap (drw.window, drw.allocation.width, drw.allocation.height, drw.window.get_depth ()) white = gtk.gdk.GC (pix) white.set_rgb_fg_color (gtk.gdk.color_parse ("white")) black = gtk.gdk.GC (pix) black.set_rgb_fg_color (gtk.gdk.color_parse ("black")) red = gtk.gdk.GC (pix) red.set_rgb_fg_color (gtk.gdk.color_parse ("red")) bg = gtk.gdk.GC (drw.window) bg.set_rgb_fg_color (gtk.gdk.color_parse ("white")) pix.draw_rectangle (white, True, 0, 0, drw.allocation.width, drw.allocation.height) sz = -1 if drw.allocation.width > drw.allocation.height: sz = drw.allocation.height else: sz = drw.allocation.width sz = sz - sz/10 x = drw.allocation.width / 2 y = drw.allocation.height / 2 angle = -pi/2 i = 0 while angle < 2*pi-pi/2: if i % 5 == 0: pix.draw_line (black, x + int (cos (angle)*(sz/2-sz/15)), y + int (sin (angle)*(sz/2-sz/15)), x + int (cos (angle)*(sz/2)), y + int (sin (angle)*(sz/2))) layout = pango.Layout (drw.get_pango_context ()) else: pix.draw_line (black, x + int (cos (angle)*(sz/2)), y + int (sin (angle)*(sz/2)), x + int (cos (angle)*(sz/2-sz/35)), y + int (sin (angle)*(sz/2-sz/35))) i += 1 angle += (2*pi)/60 i = 0 angle = origin while angle > origin - 2*pi: if i > 0: layout.set_text (str (i/5)) else: layout.set_text (str (12)) xy = layout.get_pixel_size () pix.draw_layout (black, x - xy [0]/2 + int (cos (angle)*(sz/2-sz/6)), y - xy[1]/2 + int (sin (angle)*(sz/2-sz/6)), layout) i += 5 angle -= 5*(2*pi)/60 t = localtime () angle = origin - 2*pi - (t [3] + t [4]/60.0)*(2*pi)/12 nx = x + int (cos (angle+pi)*(sz/2-sz/2.2)) ny = y + int (sin (angle+pi)*(sz/2-sz/2.2)) pix.draw_polygon (black, True, [(nx - int (cos (angle-pi/2)*(sz/2-sz/2.1)), ny - int (sin (angle-pi/2)*(sz/2-sz/2.1))), (nx + int (cos (angle-pi/2)*(sz/2-sz/2.1)), ny + int (sin (angle-pi/2)*(sz/2-sz/2.1))), (x + int (cos (angle)*(sz/2-sz/3.5)), y + int (sin (angle)*(sz/2-sz/3.5)))]) angle = origin - 2*pi - t [4]*(2*pi)/60 nx = x + int (cos (angle+pi)*(sz/2-sz/2.2)) ny = y + int (sin (angle+pi)*(sz/2-sz/2.2)) pix.draw_polygon (black, True, [(nx - int (cos (angle-pi/2)*(sz/2-sz/2.1)), ny - int (sin (angle-pi/2)*(sz/2-sz/2.1))), (nx + int (cos (angle-pi/2)*(sz/2-sz/2.1)), ny + int (sin (angle-pi/2)*(sz/2-sz/2.1))), (x + int (cos (angle)*(sz/2-sz/5.5)), y + int (sin (angle)*(sz/2-sz/5.5)))]) angle = origin - 2*pi - t [5]*(2*pi)/60 nx = x + int (cos (angle+pi)*(sz/2-sz/2.2)) ny = y + int (sin (angle+pi)*(sz/2-sz/2.2)) pix.draw_polygon (red, True, [(nx - int (cos (angle-pi/2)*(sz/2-sz/2.05)), ny - int (sin (angle-pi/2)*(sz/2-sz/2.05))), (nx + int (cos (angle-pi/2)*(sz/2-sz/2.05)), ny + int (sin (angle-pi/2)*(sz/2-sz/2.05))), (x + int (cos (angle)*(sz/2-sz/7)), y + int (sin (angle)*(sz/2-sz/7)))]) drw.window.draw_drawable (drw.style.bg_gc [drw.state], pix, 0, 0, 0, 0, drw.allocation.width, drw.allocation.height) def drw_refresh (drw): try: drw_update (drw) return True except KeyboardInterrupt: gtk.main_quit () win = gtk.Window () win.set_title ("garclock") win.connect ("delete-event", win_delete_event) drw = gtk.DrawingArea () drw.connect ("expose-event", drw_expose_event) win.add (drw) win.show_all () gtk.timeout_add (1000, drw_refresh, drw) try: gtk.main () except KeyboardInterrupt: pass