Added missing import
[memo.git] / clock.py
blob96235ae5c17bd32bd54ab703a03706dbf32bf64a
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, os
19 from rox import g, applet, Menu, options, processes, filer
20 from MenuWindow import MenuWindow
21 import main
22 import gobject
23 import pango
25 menuAdditions={
26 'topActions': [
27 ("/Main Window", "toggle_main", "", ""),
29 'bottomActions': [
30 ('/', "", "<Separator>" ),
31 (_('/Set Time'), "set_time", "<StockItem>", "", g.STOCK_PROPERTIES),
32 ] }
34 set_prog = options.Option('set_program', "gksu time-admin")
36 line1 = options.Option("line1", "%X")
37 line2 = options.Option("line2", "%x")
38 tip = options.Option("tip", "%c")
40 line1_font = options.Option("line1_font", None)
41 line1_color = options.Option("line1_color", "#000000")
43 line2_font = options.Option("line2_font", None)
44 line2_color = options.Option("line2_color", "#000000")
46 class Clock:
47 def __init__(self):
48 self.tooltip = g.Tooltips()
49 if tip.value == "": self.tooltip.disable()
50 self.vbox = g.VBox(spacing = 2)
52 self.line1_label = g.Label("")
53 if line1.value != "":
54 self.vbox.add(self.line1_label)
56 self.line2_label = g.Label("")
57 if line2.value != "":
58 self.vbox.add(self.line2_label)
60 self.set_border_width(5)
61 self.add(self.vbox)
63 rox.app_options.add_notify(self.options_changed)
65 self.add_events(g.gdk.BUTTON_PRESS_MASK)
66 self.connect("button-press-event", self.button_press)
68 self.connect("destroy", self.destroyed)
70 self.update_clock()
71 self.timeout = gobject.timeout_add(1000, self.update_clock)
73 self.show_all()
75 def update_clock(self):
76 self.line1_label.set_text(time.strftime(line1.value))
77 self.line2_label.set_text(time.strftime(line2.value))
78 self.update_font()
79 self.update_color()
80 self.tooltip.set_tip(self, time.strftime(tip.value))
81 return True
83 def update_font(self):
84 new_font1 = pango.FontDescription(line1_font.value)
85 new_font2 = pango.FontDescription(line2_font.value)
86 if new_font1:
87 self.line1_label.modify_font(new_font1)
88 if new_font2:
89 self.line2_label.modify_font(new_font2)
91 def update_color(self):
92 new_color1 = g.gdk.color_parse(line1_color.value)
93 new_color2 = g.gdk.color_parse(line2_color.value)
94 if new_color1:
95 self.line1_label.modify_fg(g.STATE_NORMAL, new_color1)
96 if new_color2:
97 self.line2_label.modify_fg(g.STATE_NORMAL, new_color2)
99 def destroyed(self, window):
100 gobject.source_remove(self.timeout)
101 main.main_window.destroy()
103 def button_press(self, window, event):
104 if event.button == 3:
105 self.popup_menu(event)
107 def options_changed(self):
108 if line1.has_changed:
109 if line1.value == '':
110 self.vbox.remove(self.line1_label)
111 else:
112 self.vbox.add(self.line1_label)
113 if line2.value:
114 self.vbox.remove(self.line2_label)
115 self.vbox.add(self.line2_label)
117 if line2.has_changed:
118 if line2.value == '':
119 self.vbox.remove(self.line2_label)
120 else:
121 self.vbox.add(self.line2_label)
123 if tip.has_changed:
124 if tip.value == '':
125 self.tooltip.disable()
126 else:
127 self.tooltip.enable()
129 if line1_font.has_changed or line2_font.has_changed:
130 self.update_font()
131 if line1_color.has_changed or line2_color.has_changed:
132 self.update_color()
134 self.update_clock()
136 def set_time(self):
137 rox.processes.PipeThroughCommand(set_prog.value , None, None).wait()
139 def toggle_main(self):
140 if main.main_window.get_property('visible'):
141 main.main_window.hide()
142 else:
143 main.main_window.set_decorated(False) #should be done only once?
144 self.position_window(main.main_window)
145 main.main_window.present()
147 class ClockApplet(applet.Applet, Clock, MenuWindow):
148 def __init__(self):
149 applet.Applet.__init__(self, sys.argv[1])
150 Clock.__init__(self)
151 MenuWindow.__init__(self, attach=False, additions=menuAdditions )
152 main.main_window.memo_list.connect("MemoListChanged", self.memo_list_changed)
154 def button_press(self, window, event):
155 if event.type != g.gdk.BUTTON_PRESS: return
156 if event.button == 1:
157 self.toggle_main()
158 elif event.button == 3:
159 self.popup_menu(event, self.position_menu)
161 def get_panel_orientation(self):
162 """Return the panel orientation ('Top', 'Bottom', 'Left', 'Right')
163 and the margin for displaying a popup menu"""
164 pos = self.socket.property_get('_ROX_PANEL_MENU_POS', 'STRING', False)
165 if pos: pos = pos[2]
166 if pos:
167 side, margin = pos.split(',')
168 margin = int(margin)
169 else:
170 side, margin = None, 2
171 return side, margin
173 def memo_list_changed(self, memo_list = None):
174 def reposition():
175 if main.main_window.get_property('visible'):
176 self.position_window(main.main_window)
177 # Give the window a chance to resize itself first...
178 gobject.idle_add(reposition)
180 def position_window(self, win):
181 """Set the position of the popup"""
182 side, margin = self.get_panel_orientation()
183 x, y = self.socket.get_origin()
184 w, h = win.size_request()
186 # widget (x, y, w, h, bits)
187 geometry = self.socket.get_geometry()
189 if side == 'Bottom':
190 win.move(x, y-h)
191 elif side == 'Top':
192 win.move(x, y+geometry[3])
193 elif side == 'Left':
194 win.move(x+geometry[2], y)
195 elif side == 'Right':
196 win.move(x-w, y)
197 else:
198 #shouldn't happen?
199 self.thing.move(x,y)