Reposition the memo list next to the applet when the list changes.
[memo.git] / clock.py
blobe029f2441c0e91192ad41fbfd4d263b77f3b0c54
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 import main
21 import gobject
22 import pango
24 menu = Menu.Menu('main', [
25 ("/Show Main Window", "show_main", "", ""),
26 ("/Set Time", "set_time", "<StockItem>", "", g.STOCK_PROPERTIES),
27 ("/Help", "show_help", "<StockItem>", "", g.STOCK_HELP),
28 ("/Options", "options", "<StockItem>", "", g.STOCK_PREFERENCES),
29 ("/Quit", "quit", "<StockItem>", "", g.STOCK_QUIT)
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 line1_font = options.Option("line1_font", None)
39 line1_color = options.Option("line1_color", "#000000")
41 line2_font = options.Option("line2_font", None)
42 line2_color = options.Option("line2_color", "#000000")
44 class Clock:
45 def __init__(self):
46 self.tooltip = g.Tooltips()
47 if tip.value == "": self.tooltip.disable()
48 self.vbox = g.VBox(spacing = 2)
50 self.line1_label = g.Label("")
51 if line1.value != "":
52 self.vbox.add(self.line1_label)
54 self.line2_label = g.Label("")
55 if line2.value != "":
56 self.vbox.add(self.line2_label)
58 self.set_border_width(5)
59 self.add(self.vbox)
61 rox.app_options.add_notify(self.options_changed)
63 self.add_events(g.gdk.BUTTON_PRESS_MASK)
64 self.connect("button-press-event", self.button_press)
66 self.connect("destroy", self.destroyed)
68 self.update_clock()
69 self.timeout = gobject.timeout_add(1000, self.update_clock)
71 self.show_all()
73 def update_clock(self):
74 self.line1_label.set_text(time.strftime(line1.value))
75 self.line2_label.set_text(time.strftime(line2.value))
76 self.update_font()
77 self.update_color()
78 self.tooltip.set_tip(self, time.strftime(tip.value))
79 return True
81 def update_font(self):
82 new_font1 = pango.FontDescription(line1_font.value)
83 new_font2 = pango.FontDescription(line2_font.value)
84 if new_font1:
85 self.line1_label.modify_font(new_font1)
86 if new_font2:
87 self.line2_label.modify_font(new_font2)
89 def update_color(self):
90 new_color1 = g.gdk.color_parse(line1_color.value)
91 new_color2 = g.gdk.color_parse(line2_color.value)
92 if new_color1:
93 self.line1_label.modify_fg(g.STATE_NORMAL, new_color1)
94 if new_color2:
95 self.line2_label.modify_fg(g.STATE_NORMAL, new_color2)
97 def destroyed(self, window):
98 gobject.source_remove(self.timeout)
99 main.main_window.destroy()
101 def button_press(self, window, event):
102 if event.button == 3:
103 menu.popup(window, event)
105 def options_changed(self):
106 if line1.has_changed:
107 if line1.value == '':
108 self.vbox.remove(self.line1_label)
109 else:
110 self.vbox.add(self.line1_label)
111 if line2.value:
112 self.vbox.remove(self.line2_label)
113 self.vbox.add(self.line2_label)
115 if line2.has_changed:
116 if line2.value == '':
117 self.vbox.remove(self.line2_label)
118 else:
119 self.vbox.add(self.line2_label)
121 if tip.has_changed:
122 if tip.value == '':
123 self.tooltip.disable()
124 else:
125 self.tooltip.enable()
127 if line1_font.has_changed or line2_font.has_changed:
128 self.update_font()
129 if line1_color.has_changed or line2_color.has_changed:
130 self.update_color()
132 self.update_clock()
134 def options(self):
135 rox.edit_options()
137 def show_help(self):
138 filer.open_dir(os.path.join(rox.app_dir, 'Help'))
140 def quit(self):
141 self.destroy()
143 def set_time(self):
144 rox.processes.PipeThroughCommand(set_prog.value , None, None).wait()
146 def show_main(self):
147 if main.main_window.get_property('visible'):
148 main.main_window.hide()
149 else:
150 main.main_window.set_decorated(False) #should be done only once?
151 self.position_window(main.main_window)
152 main.main_window.present()
154 class ClockApplet(applet.Applet, Clock):
155 def __init__(self):
156 applet.Applet.__init__(self, sys.argv[1])
157 Clock.__init__(self)
158 main.main_window.memo_list.watchers.append(self.memo_list_changed)
160 def button_press(self, window, event):
161 if event.type != g.gdk.BUTTON_PRESS: return
162 if event.button == 1:
163 self.show_main()
164 elif event.button == 3:
165 menu.popup(window, event, self.position_menu)
167 def get_panel_orientation(self):
168 """Return the panel orientation ('Top', 'Bottom', 'Left', 'Right')
169 and the margin for displaying a popup menu"""
170 pos = self.socket.property_get('_ROX_PANEL_MENU_POS', 'STRING', False)
171 if pos: pos = pos[2]
172 if pos:
173 side, margin = pos.split(',')
174 margin = int(margin)
175 else:
176 side, margin = None, 2
177 return side, margin
179 def memo_list_changed(self):
180 def reposition():
181 if main.main_window.get_property('visible'):
182 self.position_window(main.main_window)
183 # Give the window a chance to resize itself first...
184 gobject.idle_add(reposition)
186 def position_window(self, win):
187 """Set the position of the popup"""
188 side, margin = self.get_panel_orientation()
189 x, y = self.socket.get_origin()
190 w, h = win.size_request()
192 # widget (x, y, w, h, bits)
193 geometry = self.socket.get_geometry()
195 if side == 'Bottom':
196 win.move(x, y-h)
197 elif side == 'Top':
198 win.move(x, y+geometry[3])
199 elif side == 'Left':
200 win.move(x+geometry[2], y)
201 elif side == 'Right':
202 win.move(x-w, y)
203 else:
204 #shouldn't happen?
205 self.thing.move(x,y)