End program upon destory (Closes: 428893)
[darcs-mirror-screen-message.git] / sm.c
1 /*
2 #     vlshow.c
3 #     Copyright (C) 2006 Joachim Breitner
4 #
5 #     This program is free software; you can redistribute it and/or modify
6 #     it under the terms of the GNU General Public License as published by
7 #     the Free Software Foundation; either version 2 of the License, or
8 #     (at your option) any later version.
9 #
10 #     This program is distributed in the hope that it will be useful,
11 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #     GNU General Public License for more details.
14 #
15 #     You should have received a copy of the GNU General Public License
16 #     along with this program; if not, write to the Free Software
17 #     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18 */
19
20 #include <gtk/gtk.h>
21 #include <gdk/gdk.h>
22 #include <pango/pango.h>
23 #include <string.h>
24 #include <stdio.h>
25
26 static int min(int x, int y) {
27         return x < y ? x : y;
28 }
29
30
31 static gboolean quality = TRUE;
32 static gboolean need_resize = TRUE;
33 static gboolean need_quick = TRUE;
34
35 static GtkWidget* window;
36 static GtkWidget* draw;
37 static GdkCursor *cursor;
38 static GtkWidget* quit;
39 static GtkWidget* tv;
40 static GtkSettings* settings;
41 static GtkTextBuffer* tb;
42 static PangoFontDescription *font;
43 static PangoLayout* layout;
44
45 static void realize(GtkWindow *window, GdkScreen *screen, gpointer data) {
46         gdk_window_set_cursor(draw->window, cursor);
47 }
48
49 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
50         gtk_text_buffer_set_text(tb,"",-1);
51 }
52
53 static char *get_text() {
54         GtkTextIter start, end;
55         gtk_text_buffer_get_start_iter(tb,&start);
56         gtk_text_buffer_get_end_iter(tb,&end);
57         return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
58 }
59
60
61 static void hq(gboolean q, gboolean force){
62         if (q != quality) 
63                 if (q)
64                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
65                 else
66                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
67         else
68                 if (force)
69                         gtk_widget_queue_draw(draw);
70
71         quality = q;
72 }
73
74 static void redraw() {
75         const char *text = pango_layout_get_text(layout);
76         if (strlen(text) > 0) {
77                 GdkGC *gc = gtk_widget_get_style(draw)->fg_gc[GTK_STATE_NORMAL];
78                 int w1, h1;
79                 pango_layout_get_pixel_size(layout, &w1, &h1);
80                 if (w1>0 && h1>0) {
81                         int w2 = draw->allocation.width;
82                         int h2 = draw->allocation.height;
83                         gdk_draw_layout(draw->window, gc, (w2-w1)/2,(h2-h1)/2,layout);
84                         hq(TRUE, FALSE);
85                 }
86         }
87 }
88
89 static void resize() {
90         int w1, h1, w2, h2;
91         pango_layout_get_pixel_size(layout, &w1, &h1);
92         if (w1>0 && h1>0) {
93                 w2 = draw->allocation.width;
94                 h2 = draw->allocation.height;
95                 int s = pango_font_description_get_size(font);
96                 s = min ((s*w2/w1), (s*h2/h1));
97                 pango_font_description_set_size(font,s);
98                 pango_layout_set_font_description(layout, font);
99                 need_resize = FALSE;
100         }
101         else
102                 need_resize = TRUE;
103 }
104
105
106 static void newtext(char *text) {
107         pango_layout_set_text(layout, get_text(), -1);
108         resize();
109         hq(FALSE, TRUE);
110 }
111
112 int main(int argc, char **argv) {
113         gtk_init(&argc, &argv);
114
115         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
116         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
117         gtk_window_fullscreen(GTK_WINDOW(window));
118         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
119
120         settings = gtk_settings_get_default();
121         GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
122         GdkColor white, black;
123         gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
124         gdk_color_parse("white", &white);
125         gdk_color_parse("black", &black);
126         gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &white);
127         gtk_widget_modify_fg(window, GTK_STATE_NORMAL, &black);
128
129         draw = gtk_drawing_area_new();
130         gtk_widget_set_size_request(draw,400,400);
131         gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
132         gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
133         g_signal_connect(G_OBJECT(draw), "realize", G_CALLBACK(realize), NULL);
134
135         GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
136         GdkColor color;
137         cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
138
139         tv = gtk_text_view_new();
140         gtk_widget_modify_bg(tv, GTK_STATE_NORMAL, &white);
141         gtk_widget_modify_fg(tv, GTK_STATE_NORMAL, &black);
142         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
143         
144         if (argc > 1)
145                 //tb.set_text(" ".join(sys.argv[1:]))
146                 gtk_text_buffer_set_text(tb, argv[1], -1);
147         else
148                 gtk_text_buffer_set_text(tb, ":-)", -1);
149
150         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
151         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
152         gtk_widget_modify_bg(quit, GTK_STATE_NORMAL, &white);
153         gtk_widget_modify_fg(quit, GTK_STATE_NORMAL, &black);
154
155         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
156         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
157         gtk_box_pack_start(GTK_BOX(hbox), quit, FALSE, FALSE, 0);
158
159         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
160         gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE,  TRUE,  0);
161         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
162
163         gtk_container_add(GTK_CONTAINER(window), vbox);
164
165         font = pango_font_description_new();
166         pango_font_description_set_family(font, "sans-serif");
167         pango_font_description_set_size(font, 20*PANGO_SCALE);
168
169         layout=  gtk_widget_create_pango_layout(draw,get_text());
170         pango_layout_set_font_description(layout, font);
171         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
172
173         GtkAccelGroup *accel = gtk_accel_group_new();
174         guint key;
175         GdkModifierType mod;
176         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
177         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
178         gtk_accelerator_parse("Escape", &key, &mod);
179         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
180         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
181         gtk_widget_show_all(window);
182
183         g_signal_connect(G_OBJECT(draw), "configure-event", G_CALLBACK(resize), NULL);
184         g_signal_connect(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
185         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
186
187         gtk_main();
188 }