Work around change in Python 2.5
[memo.git] / clock.py
blobe8247ec488eaf531e19af1d36ad70996320f5002
1 # Based on code in MiniClock v. 2.0.0 - a very very simple clock
2 # Copyright (C) 2005 Edoardo Spadolini <pedonzolo@email.it>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 import rox, time, sys
19 from rox import g, applet, Menu, options, processes
20 import main
21 import gobject
23 menu = Menu.Menu('main', [
24 ("/Show Main Window", "show_main", "", ""),
25 ("/Set Time", "set_time", "<StockItem>", "", g.STOCK_PROPERTIES),
26 ("/Options", "options", "<StockItem>", "", g.STOCK_PREFERENCES),
27 ("/Quit", "quit", "<StockItem>", "", g.STOCK_QUIT)
30 clocktype = options.Option("clocktype", "2lines")
31 showtip = options.Option("showtip", True)
32 set_prog = options.Option('set_program', "gksu time-admin")
34 line1 = options.Option("line1", "%X")
35 line2 = options.Option("line2", "%x")
36 tip = options.Option("tip", "%c")
38 class Clock:
39 def __init__(self):
40 self.tooltip = g.Tooltips()
41 if showtip.value == "False": self.tooltip.disable()
42 self.vbox = g.VBox(spacing = 2)
44 self.line1_label = g.Label("")
45 self.vbox.add(self.line1_label)
47 self.line2_label = g.Label("")
48 if clocktype.value == "2lines":
49 self.vbox.add(self.line2_label)
51 self.set_border_width(5)
52 self.add(self.vbox)
54 rox.app_options.add_notify(self.options_changed)
56 self.add_events(g.gdk.BUTTON_PRESS_MASK)
57 self.connect("button-press-event", self.button_press)
59 self.connect("destroy", self.destroyed)
61 self.update_clock()
62 self.timeout = gobject.timeout_add(1000, self.update_clock)
64 self.show_all()
66 def update_clock(self):
67 self.line1_label.set_text(time.strftime(line1.value))
68 self.line2_label.set_text(time.strftime(line2.value))
69 self.tooltip.set_tip(self, time.strftime(tip.value))
70 return True
72 def destroyed(self, window):
73 gobject.source_remove(self.timeout)
74 main.main_window.destroy()
76 def button_press(self, window, event):
77 if event.button == 3:
78 menu.popup(window, event)
80 def options_changed(self):
81 if clocktype.has_changed:
82 if clocktype.value == "1line":
83 self.vbox.remove(self.line2_label)
84 else:
85 self.vbox.add(self.line2_label)
87 if showtip.has_changed:
88 if showtip.value == "True":
89 self.tooltip.enable()
90 else:
91 self.tooltip.disable()
93 self.update_clock()
95 def options(self):
96 rox.edit_options()
98 def quit(self):
99 self.destroy()
101 def set_time(self):
102 rox.processes.PipeThroughCommand(set_prog.value , None, None).wait()
104 def show_main(self):
105 if main.main_window.get_property('visible'):
106 main.main_window.hide()
107 else:
108 main.main_window.set_decorated(False) #should be done only once?
109 self.position_window(main.main_window)
110 main.main_window.present()
112 class ClockApplet(applet.Applet, Clock):
113 def __init__(self):
114 applet.Applet.__init__(self, sys.argv[1])
115 Clock.__init__(self)
117 def button_press(self, window, event):
118 if event.type != g.gdk.BUTTON_PRESS: return
119 if event.button == 1:
120 self.show_main()
121 elif event.button == 3:
122 menu.popup(window, event, self.position_menu)
124 def get_panel_orientation(self):
125 """Return the panel orientation ('Top', 'Bottom', 'Left', 'Right')
126 and the margin for displaying a popup menu"""
127 pos = self.socket.property_get('_ROX_PANEL_MENU_POS', 'STRING', False)
128 if pos: pos = pos[2]
129 if pos:
130 side, margin = pos.split(',')
131 margin = int(margin)
132 else:
133 side, margin = None, 2
134 return side, margin
136 def position_window(self, win):
137 """Set the position of the popup"""
138 side, margin = self.get_panel_orientation()
139 x, y = self.socket.get_origin()
140 w, h = win.get_size()
142 # widget (x, y, w, h, bits)
143 geometry = self.socket.get_geometry()
145 if side == 'Bottom':
146 win.move(x, y-h)
147 elif side == 'Top':
148 win.move(x, y+geometry[3])
149 elif side == 'Left':
150 win.move(x+geometry[2], y)
151 elif side == 'Right':
152 win.move(x-w, y)
153 else:
154 #shouldn't happen?
155 self.thing.move(x,y)