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