Assorted fixes of warnings
[darcs-mirror-screen-message.debian.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
34 static GtkWidget* window;
35 static GtkWidget* draw;
36 static GdkCursor *cursor;
37 static GtkWidget* quit;
38 static GtkWidget* tv;
39 static GtkSettings* settings;
40 static GtkTextBuffer* tb;
41 static PangoFontDescription *font;
42 static PangoLayout* layout;
43
44 static void realize(GtkWindow *window, GdkScreen *screen, gpointer data) {
45         gdk_window_set_cursor(draw->window, cursor);
46 }
47
48 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
49         if( gtk_text_buffer_get_char_count(tb) )
50                 gtk_text_buffer_set_text(tb,"",-1);
51         else
52                 gtk_main_quit();
53 }
54
55 static char *get_text() {
56         GtkTextIter start, end;
57         gtk_text_buffer_get_start_iter(tb,&start);
58         gtk_text_buffer_get_end_iter(tb,&end);
59         return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
60 }
61
62
63 static void hq(gboolean q, gboolean force){
64         if (q != quality) 
65                 if (q)
66                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
67                 else
68                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
69         else
70                 if (force)
71                         gtk_widget_queue_draw(draw);
72
73         quality = q;
74 }
75
76 static void redraw() {
77         const char *text = pango_layout_get_text(layout);
78         if (strlen(text) > 0) {
79                 GdkGC *gc = gtk_widget_get_style(draw)->fg_gc[GTK_STATE_NORMAL];
80                 int w1, h1;
81                 pango_layout_get_pixel_size(layout, &w1, &h1);
82                 if (w1>0 && h1>0) {
83                         int w2 = draw->allocation.width;
84                         int h2 = draw->allocation.height;
85                         gdk_draw_layout(draw->window, gc, (w2-w1)/2,(h2-h1)/2,layout);
86                         hq(TRUE, FALSE);
87                 }
88         }
89 }
90
91 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
92         if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
93                 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
94
95                 gchar *txt = gtk_clipboard_wait_for_text(cb);
96                 if (txt != NULL) {
97                         gtk_text_buffer_set_text(tb,txt,-1);
98                         g_free(txt);
99                 }
100
101         }
102         return FALSE;
103 }
104
105 static void resize() {
106         int w1, h1, w2, h2;
107         pango_layout_get_pixel_size(layout, &w1, &h1);
108         if (w1>0 && h1>0) {
109                 w2 = draw->allocation.width;
110                 h2 = draw->allocation.height;
111                 int s = pango_font_description_get_size(font);
112                 s = min ((s*w2/w1), (s*h2/h1));
113                 pango_font_description_set_size(font,s);
114                 pango_layout_set_font_description(layout, font);
115                 need_resize = FALSE;
116         }
117         else
118                 need_resize = TRUE;
119 }
120
121
122 static void newtext(char *text) {
123         pango_layout_set_text(layout, get_text(), -1);
124         resize();
125         hq(FALSE, TRUE);
126 }
127
128 int main(int argc, char **argv) {
129         GString *input;
130
131         gtk_init(&argc, &argv);
132
133         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
134         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
135         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
136         GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
137         gtk_widget_set_size_request(window, gdk_screen_get_width(screen),
138                                             gdk_screen_get_height(screen));
139         gtk_window_fullscreen(GTK_WINDOW(window));
140         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
141
142         settings = gtk_settings_get_default();
143         GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
144         GdkColor white, black;
145         gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
146         gdk_color_parse("white", &white);
147         gdk_color_parse("black", &black);
148         gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &white);
149         gtk_widget_modify_fg(window, GTK_STATE_NORMAL, &black);
150
151         draw = gtk_drawing_area_new();
152         gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK);
153         gtk_widget_set_size_request(draw,400,400);
154         gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
155         gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
156         g_signal_connect(G_OBJECT(draw), "realize", G_CALLBACK(realize), NULL);
157         g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
158
159         GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
160         GdkColor color;
161         cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
162
163         tv = gtk_text_view_new();
164         gtk_widget_modify_bg(tv, GTK_STATE_NORMAL, &white);
165         gtk_widget_modify_fg(tv, GTK_STATE_NORMAL, &black);
166         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
167
168         if (argc > 1)
169                 if (!strcmp(argv[1], "-") ) {
170                         // read from stdin
171                         gchar buf[1024];
172                         int num;
173
174                         input = g_string_new("");
175
176                         while ((num = fread (buf, 1, sizeof(buf), stdin)) > 0) {
177                                 g_string_append_len(input, buf, num);
178                         }
179                 } else {
180                         int i;
181
182                         input = g_string_new("");
183
184                         for (i = 1; i < argc; i++) {
185                                 g_string_append(input, argv[i]);
186
187                                 if (i < argc - 1) {
188                                         g_string_append(input, " ");
189                                 }
190                         }
191                 }
192         else
193                 input = g_string_new(":-)");
194
195         gtk_text_buffer_set_text(tb, input->str, input->len);
196
197         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
198         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
199         gtk_widget_modify_bg(quit, GTK_STATE_NORMAL, &white);
200         gtk_widget_modify_fg(quit, GTK_STATE_NORMAL, &black);
201
202         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
203         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
204         gtk_box_pack_start(GTK_BOX(hbox), quit, FALSE, FALSE, 0);
205
206         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
207         gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE,  TRUE,  0);
208         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
209
210         gtk_container_add(GTK_CONTAINER(window), vbox);
211
212         font = pango_font_description_new();
213         pango_font_description_set_family(font, "sans-serif");
214         pango_font_description_set_size(font, 20*PANGO_SCALE);
215
216         layout=  gtk_widget_create_pango_layout(draw,get_text());
217         pango_layout_set_font_description(layout, font);
218         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
219
220         GtkAccelGroup *accel = gtk_accel_group_new();
221         guint key;
222         GdkModifierType mod;
223         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
224         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
225         gtk_accelerator_parse("Escape", &key, &mod);
226         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
227         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
228         gtk_widget_show_all(window);
229
230         g_signal_connect(G_OBJECT(draw), "configure-event", G_CALLBACK(resize), NULL);
231         g_signal_connect(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
232         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
233
234         resize();
235
236         gtk_main();
237
238         return 0;
239 }