]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - bin/notify.py
3caa7d2a70ede6e931d5b535dc3c02f6bce00405
[config/dotfiles.git] / bin / notify.py
1 #!/usr/bin/python
2
3 # notify.py is a simple notification program displaying a message on the
4 # screen.
5
6 # Copyright (C) 2011-2013  Simon Ruderich
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21
22 import sys
23 import locale
24
25 import pygtk
26 pygtk.require('2.0') # there might be a GTK 1 version installed, ignore it
27 import gtk
28 import pango
29 import gobject
30
31
32 # Pressing the mouse inside the window quits notify.
33 def mouse_press_callback(widget, event):
34     gtk.main_quit()
35
36
37 if __name__ == '__main__':
38     if len(sys.argv) != 2:
39         sys.stderr.write("Usage: %s <message>\n" % (sys.argv[0]))
40         sys.exit(1)
41
42     # Display configuration.
43     BOLD     = True
44     FG_COLOR = 'blue'
45     BG_COLOR = 'yellow'
46
47     # We need a popup so the window manager doesn't touch our window.
48     window = gtk.Window(gtk.WINDOW_POPUP)
49     # No window border or close/resize buttons.
50     window.set_decorated(False)
51     # In case set_decorated(False) doesn't work with the used window manager
52     # quit when the window is manually closed.
53     window.connect('delete-event', gtk.main_quit)
54     # Enable mouse click events, by default windows don't receive them.
55     window.add_events(gtk.gdk.BUTTON_PRESS_MASK)
56     window.connect('button-press-event', mouse_press_callback)
57
58     # Background color.
59     window.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(BG_COLOR))
60
61     string = sys.argv[1].decode(locale.getdefaultlocale()[1])
62
63     # Colored message string.
64     label_message = gtk.Label(string)
65     label_message.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(FG_COLOR))
66     window.add(label_message)
67
68     if BOLD:
69         attributes = pango.AttrList()
70         attributes.insert(pango.AttrWeight(pango.WEIGHT_HEAVY, end_index=-1))
71         label_message.set_attributes(attributes)
72
73     # Display at the top at full screen width.
74     screen = window.get_screen()
75     window.resize(screen.get_width(), 30)
76     window.move(0, 15)
77
78     window.show_all()
79     gtk.main()