Read from stdin if parameter is -
[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 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 gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
90         if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
91                 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
92
93                 gchar *txt = gtk_clipboard_wait_for_text(cb);
94                 if (txt != NULL) {
95                         gtk_text_buffer_set_text(tb,txt,-1);
96                         g_free(txt);
97                 }
98
99         }
100         return FALSE;
101 }
102
103 static void resize() {
104         int w1, h1, w2, h2;
105         pango_layout_get_pixel_size(layout, &w1, &h1);
106         if (w1>0 && h1>0) {
107                 w2 = draw->allocation.width;
108                 h2 = draw->allocation.height;
109                 int s = pango_font_description_get_size(font);
110                 s = min ((s*w2/w1), (s*h2/h1));
111                 pango_font_description_set_size(font,s);
112                 pango_layout_set_font_description(layout, font);
113                 need_resize = FALSE;
114         }
115         else
116                 need_resize = TRUE;
117 }
118
119
120 static void newtext(char *text) {
121         pango_layout_set_text(layout, get_text(), -1);
122         resize();
123         hq(FALSE, TRUE);
124 }
125
126 int main(int argc, char **argv) {
127         gtk_init(&argc, &argv);
128
129         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
130         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
131         gtk_window_fullscreen(GTK_WINDOW(window));
132         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
133
134         settings = gtk_settings_get_default();
135         GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
136         GdkColor white, black;
137         gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
138         gdk_color_parse("white", &white);
139         gdk_color_parse("black", &black);
140         gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &white);
141         gtk_widget_modify_fg(window, GTK_STATE_NORMAL, &black);
142
143         draw = gtk_drawing_area_new();
144         gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK);
145         gtk_widget_set_size_request(draw,400,400);
146         gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
147         gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
148         g_signal_connect(G_OBJECT(draw), "realize", G_CALLBACK(realize), NULL);
149         g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
150
151         GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
152         GdkColor color;
153         cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
154
155         tv = gtk_text_view_new();
156         gtk_widget_modify_bg(tv, GTK_STATE_NORMAL, &white);
157         gtk_widget_modify_fg(tv, GTK_STATE_NORMAL, &black);
158         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
159         
160         if (argc > 1)
161                 if (!strcmp(argv[1], "-") ) {
162                         // read from stdin
163                         gchar text[1024];
164                         int num = fread (text, sizeof(gchar),1024-1, stdin);
165                         text[num] = '\0';
166                         if (num>0 && text[num-1] == '\n') {
167                                 text[num-1] = '\0';
168                         }
169                         gtk_text_buffer_set_text(tb, text, -1);
170                 } else {
171                         gtk_text_buffer_set_text(tb, argv[1], -1);
172                 }
173         else
174                 gtk_text_buffer_set_text(tb, ":-)", -1);
175
176         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
177         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
178         gtk_widget_modify_bg(quit, GTK_STATE_NORMAL, &white);
179         gtk_widget_modify_fg(quit, GTK_STATE_NORMAL, &black);
180
181         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
182         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
183         gtk_box_pack_start(GTK_BOX(hbox), quit, FALSE, FALSE, 0);
184
185         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
186         gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE,  TRUE,  0);
187         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
188
189         gtk_container_add(GTK_CONTAINER(window), vbox);
190
191         font = pango_font_description_new();
192         pango_font_description_set_family(font, "sans-serif");
193         pango_font_description_set_size(font, 20*PANGO_SCALE);
194
195         layout=  gtk_widget_create_pango_layout(draw,get_text());
196         pango_layout_set_font_description(layout, font);
197         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
198
199         GtkAccelGroup *accel = gtk_accel_group_new();
200         guint key;
201         GdkModifierType mod;
202         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
203         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
204         gtk_accelerator_parse("Escape", &key, &mod);
205         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
206         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
207         gtk_widget_show_all(window);
208
209         g_signal_connect(G_OBJECT(draw), "configure-event", G_CALLBACK(resize), NULL);
210         g_signal_connect(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
211         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
212
213         gtk_main();
214 }