Upload to unstable
[darcs-mirror-screen-message.debian.git] / sm.py
1 #!/usr/bin/python
2 # encoding:utf8
3
4 #     vlshow.py
5 #     Copyright (C) 2006 Joachim Breitner
6 #
7 #     This program is free software; you can redistribute it and/or modify
8 #     it under the terms of the GNU General Public License as published by
9 #     the Free Software Foundation; either version 2 of the License, or
10 #     (at your option) any later version.
11 #
12 #     This program is distributed in the hope that it will be useful,
13 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #     GNU General Public License for more details.
16 #
17 #     You should have received a copy of the GNU General Public License
18 #     along with this program; if not, write to the Free Software
19 #     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20
21
22 import pygtk
23 pygtk.require('2.0')
24 import gtk
25 import pango
26 import sys
27 import gobject
28
29 window = gtk.Window(gtk.WINDOW_TOPLEVEL)
30 window.set_decorated(False)
31 window.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
32 window.fullscreen()
33
34 settings = gtk.settings_get_default()
35
36 draw = gtk.DrawingArea()
37 draw.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
38 draw.set_size_request(400,300)
39
40 pixmap = gtk.gdk.Pixmap(None, 1, 1, 1)
41 color = gtk.gdk.Color()
42 cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0)
43
44
45 tv = gtk.TextView()
46 tb = tv.get_buffer()
47
48 def get_text():
49         return tb.get_text(tb.get_start_iter(), tb.get_end_iter())
50
51 if len(sys.argv) > 1:
52         tb.set_text(" ".join(sys.argv[1:]))
53 else:
54         tb.set_text(";-)")
55
56 quit = gtk.Button(stock=gtk.STOCK_QUIT)
57 quit.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
58 quit.connect("clicked",gtk.main_quit)
59
60 hbox = gtk.HBox()
61 hbox.pack_start(tv, expand=True, fill=True)
62 hbox.pack_start(quit,  expand=False,fill=False)
63
64 vbox = gtk.VBox()
65 vbox.pack_start(draw, expand=True, fill=True)
66 vbox.pack_start(hbox, expand=False, fill=False)
67 window.add(vbox)
68
69 font = pango.FontDescription()
70 font.set_family("sans-serif")
71 font.set_size(60*pango.SCALE)
72 layout = draw.create_pango_layout(get_text())
73 layout.set_font_description(font)
74 layout.set_alignment(pango.ALIGN_CENTER)
75
76
77 accel = gtk.AccelGroup()
78 key, mod = gtk.accelerator_parse("<Ctrl>Q")
79 accel.connect_group(key, mod, 0, gtk.main_quit)
80 key, mod = gtk.accelerator_parse("Escape")
81 #accel.connect_group(key, mod, 0, gtk.main_quit)
82 #key, mod = gtk.accelerator_parse("<Ctrl>C")
83 accel.connect_group(key, mod, 0, (lambda x,y,z,v: tb.set_text("")))
84 window.add_accel_group(accel)
85
86 window.show_all()
87
88 need_resize=True
89 need_quick=True
90
91 def resize(w=None,rect=None):
92         global need_resize
93         draw.window.set_cursor(cursor)
94         (w1,h1) = layout.get_pixel_size()
95         if h1>0 and w1>0:
96                 (x,y,w2,h2) = draw.get_allocation()
97                 s = font.get_size()
98                 s = min ( int (s*w2/w1), int (s*h2/h1) )
99                 font.set_size(s)
100                 layout.set_font_description(font)
101                 need_resize=False
102         else:
103                 need_resize=True
104
105 def redraw(w=None,e=None):
106         global need_resize, need_quick
107         if layout.get_text(): # Fails for empty lines :-(
108                 gc = draw.get_style().fg_gc[gtk.STATE_NORMAL]
109                 (w1,h1) = layout.get_pixel_size()
110                 if h1>0 and w1>0:
111                         (x,y,w2,h2) = draw.get_allocation()
112                         draw.window.draw_layout(gc,(w2-w1)/2,(h2-h1)/2,layout)
113                         hq(True)
114
115 quality = False
116 def hq(q,force=False):
117         global quality
118         if q != quality:
119                 if q:
120                         settings.set_long_property("gtk-xft-antialias",1,"Hier halt")
121                 else:
122                         settings.set_long_property("gtk-xft-antialias",0,"Hier halt")
123         else:
124                 if force:
125                         draw.queue_draw()
126
127         quality = q
128         return False
129
130 def newtext(w):
131         global timeout_id
132         layout.set_text(get_text())
133         resize()
134         hq(False, True)
135
136 draw.connect("configure-event", resize)
137 draw.connect("expose-event", redraw)
138 tb.connect("changed", newtext)
139
140 gtk.main()