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