Stop moving binary around
[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         gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
137
138         GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
139         gtk_widget_set_size_request(window, gdk_screen_get_width(screen),
140                                             gdk_screen_get_height(screen));
141         gtk_window_fullscreen(GTK_WINDOW(window));
142         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
143
144         settings = gtk_settings_get_default();
145         GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
146         GdkColor white, black;
147         gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
148         gdk_color_parse("white", &white);
149         gdk_color_parse("black", &black);
150         gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &white);
151         gtk_widget_modify_fg(window, GTK_STATE_NORMAL, &black);
152
153         draw = gtk_drawing_area_new();
154         gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK);
155         gtk_widget_set_size_request(draw,400,400);
156         gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
157         gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
158         g_signal_connect(G_OBJECT(draw), "realize", G_CALLBACK(realize), NULL);
159         g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
160
161         GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
162         GdkColor color;
163         cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
164
165         tv = gtk_text_view_new();
166         gtk_widget_modify_bg(tv, GTK_STATE_NORMAL, &white);
167         gtk_widget_modify_fg(tv, GTK_STATE_NORMAL, &black);
168         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
169
170         if (argc > 1)
171                 if (!strcmp(argv[1], "-") ) {
172                         // read from stdin
173                         gchar buf[1024];
174                         int num;
175
176                         input = g_string_new("");
177
178                         while ((num = fread (buf, 1, sizeof(buf), stdin)) > 0) {
179                                 g_string_append_len(input, buf, num);
180                         }
181                 } else {
182                         int i;
183
184                         input = g_string_new("");
185
186                         for (i = 1; i < argc; i++) {
187                                 g_string_append(input, argv[i]);
188
189                                 if (i < argc - 1) {
190                                         g_string_append(input, " ");
191                                 }
192                         }
193                 }
194         else
195                 input = g_string_new(":-)");
196
197         gtk_text_buffer_set_text(tb, input->str, input->len);
198         GtkTextIter start, end;
199         gtk_text_buffer_get_bounds(tb, &start, &end);
200         gtk_text_buffer_select_range(tb, &start, &end);
201
202         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
203         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
204         gtk_widget_modify_bg(quit, GTK_STATE_NORMAL, &white);
205         gtk_widget_modify_fg(quit, GTK_STATE_NORMAL, &black);
206
207         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
208         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
209         gtk_box_pack_start(GTK_BOX(hbox), quit, FALSE, FALSE, 0);
210
211         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
212         gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE,  TRUE,  0);
213         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
214
215         gtk_container_add(GTK_CONTAINER(window), vbox);
216
217         font = pango_font_description_new();
218         pango_font_description_set_family(font, "sans-serif");
219         pango_font_description_set_size(font, 20*PANGO_SCALE);
220
221         layout=  gtk_widget_create_pango_layout(draw,get_text());
222         pango_layout_set_font_description(layout, font);
223         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
224
225         GtkAccelGroup *accel = gtk_accel_group_new();
226         guint key;
227         GdkModifierType mod;
228         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
229         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
230         gtk_accelerator_parse("Escape", &key, &mod);
231         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
232         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
233         gtk_widget_show_all(window);
234
235         g_signal_connect(G_OBJECT(draw), "configure-event", G_CALLBACK(resize), NULL);
236         g_signal_connect(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
237         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
238
239         resize();
240
241         gtk_main();
242
243         return 0;
244 }