Make sure the hidden text input does not have focus
[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 #include <math.h>
26
27 #include "config.h"
28
29 #ifndef _GNU_SOURCE
30 #define _GNU_SOURCE                             /* for getopt_long */
31 #endif
32 #include <getopt.h>
33
34 #define min(x,y) ((x) < (y) ? (x) : (y))
35
36 #define AUTOHIDE_TIMEOUT 3
37
38 static gboolean quality = TRUE;
39 static gboolean need_resize = TRUE;
40
41 static int timeout_id=0;
42
43 static GtkWidget* window;
44 static GtkWidget* draw;
45 static GdkCursor *cursor;
46 static GtkWidget* quit;
47 static GtkWidget* tv;
48 static GtkWidget* entry_widget;
49 static GtkSettings* settings;
50 static GtkTextBuffer* tb;
51 static PangoFontDescription *font;
52 static PangoLayout* layout;
53 static char *foreground = NULL;
54 static char *background = NULL;
55 static char *fontdesc = NULL;
56 static rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
57
58 gboolean hide_entry(gpointer *user_data) {
59         gtk_widget_hide(entry_widget);
60         gtk_widget_grab_focus(draw);
61         gtk_widget_queue_draw(draw);
62         gdk_window_set_cursor(GTK_WIDGET(draw)->window, cursor);
63         return FALSE;
64 }
65
66 static void show_entry() {
67         if (timeout_id) {
68                 g_source_remove(timeout_id);
69                 timeout_id = 0;
70         }
71         gtk_widget_show(entry_widget);
72         gtk_widget_grab_focus(tv);
73         gdk_window_set_cursor(GTK_WIDGET(draw)->window, NULL);
74
75         timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
76 }
77
78 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
79         if( gtk_text_buffer_get_char_count(tb) ) {
80                 gtk_text_buffer_set_text(tb,"",-1);
81                 show_entry();
82         } else {
83                 gtk_main_quit();
84         }
85 }
86
87 static char *get_text() {
88         GtkTextIter start, end;
89         gtk_text_buffer_get_start_iter(tb,&start);
90         gtk_text_buffer_get_end_iter(tb,&end);
91         return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
92 }
93
94 static void hq(gboolean q, gboolean force){
95         if (q != quality) 
96                 if (q)
97                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
98                 else
99                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
100         else
101                 if (force)
102                         gtk_widget_queue_draw(draw);
103
104         quality = q;
105 }
106
107 static void redraw() {
108         const char *text = pango_layout_get_text(layout);
109         if (strlen(text) > 0) {
110                 GdkGC *gc = gtk_widget_get_style(draw)->fg_gc[GTK_STATE_NORMAL];
111                 int w1, h1;
112                 pango_layout_get_pixel_size(layout, &w1, &h1);
113                 if (w1>0 && h1>0) {
114                         int w2 = window->allocation.width;
115                         int h2 = window->allocation.height;
116
117                         int rw1, rh1;
118                         if (rotation == 0 || rotation == 2) {
119                                 rw1 = w1;
120                                 rh1 = h1;
121                         } else {
122                                 rw1 = h1;
123                                 rh1 = w1;
124                         }
125
126                         double s = min ((double)w2/rw1, (double)h2/rh1);
127                         
128
129                         PangoMatrix matrix = PANGO_MATRIX_INIT;
130                         // Move matrix to the screen center
131                         pango_matrix_translate(&matrix, w2/2, h2/2);
132                         // Scale as required
133                         pango_matrix_scale(&matrix, s, s);
134                         // Rotate if needed
135                         pango_matrix_rotate (&matrix, rotation * 90);
136                         // Move matrix so that text will be centered
137                         //pango_matrix_translate(&matrix, -w1/2, -h1/2);
138                         
139                         // Apply matrix
140                         PangoContext *context =  pango_layout_get_context(layout);
141                         pango_context_set_matrix (context, &matrix);
142                         pango_layout_context_changed (layout);
143
144                         gdk_draw_layout(draw->window, gc,
145                                 (w2-(s*rw1))/2, (h2-(s*rh1))/2,layout);
146                         
147                         // Reset matrix
148                         PangoMatrix matrix2 = PANGO_MATRIX_INIT;
149                         pango_context_set_matrix (context, &matrix2);
150                         pango_layout_context_changed (layout);
151
152                         hq(TRUE, FALSE);
153                 }
154         }
155 }
156
157 static gboolean text_keypress(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
158         // forward signal to the text view
159         gboolean ret;
160         g_signal_emit_by_name(tv, "key-press-event", event, &ret);
161         gtk_widget_grab_focus(tv);
162         return ret;
163 }
164
165 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
166         show_entry();
167         if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
168                 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
169
170                 gchar *txt = gtk_clipboard_wait_for_text(cb);
171                 if (txt != NULL) {
172                         gtk_text_buffer_set_text(tb,txt,-1);
173                         g_free(txt);
174                 }
175
176         }
177         return FALSE;
178 }
179
180 static void newtext() {
181         show_entry();
182         pango_layout_set_text(layout, get_text(), -1);
183         hq(FALSE, TRUE);
184 }
185
186 static void move_cursor(GtkTextView* tv, GtkMovementStep step, gint count, gboolean extend_selection, gpointer user_data) {
187         show_entry();
188 }
189
190 static struct option const long_options[] =
191 {
192         {"help",       no_argument,       NULL, 'h'},
193         {"version",    no_argument,       NULL, 'V'},
194         {"foreground", required_argument, NULL, 'f'},
195         {"background", required_argument, NULL, 'b'},
196         {"font",       required_argument, NULL, 'n'},
197         {"rotate",     required_argument, NULL, 'r'},
198         {0,0,0,0}
199 };
200
201 static void usage(char *cmd) {
202         printf("Usage: %s [-h|--help] [-V|--version] [-f|--foreground=colordesc] [-b|--background=colordesc] [-n|--font=fontdesc] [-r|--rotate=0,1,2,3]\n", cmd);
203 }
204
205 static void version() {
206         printf("%s\n", PACKAGE_STRING);
207 }
208
209 int main(int argc, char **argv) {
210         GString *input;
211         int c;
212         int input_provided = 0;
213
214         while ((c = getopt_long (argc, argv, "hVf:b:n:r:", long_options, (int *) 0)) != EOF) {
215                 switch (c) {
216                         case 'h':
217                                 usage(argv[0]);
218                                 return 0;
219                                 break;
220
221                         case 'V':
222                                 version();
223                                 return 0;
224                                 break;
225
226                         case 'f':
227                                 foreground = optarg;
228                                 break;
229
230                         case 'b':
231                                 background = optarg;
232                                 break;
233
234                         case 'n':
235                                 fontdesc = optarg;
236                                 break;
237                         case 'r':
238                                 rotation = atoi(optarg);
239                                 break;
240                         default:
241                                 /* unknown switch received - at least
242                                  * give usage but continue and use the
243                                  * data */
244                                 usage(argv[0]);
245                                 break;
246                 }
247         }
248
249         gtk_init(&argc, &argv);
250
251         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
252         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
253         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
254         gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
255
256         GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
257         gtk_widget_set_size_request(window, gdk_screen_get_width(screen),
258                                             gdk_screen_get_height(screen));
259         gtk_window_fullscreen(GTK_WINDOW(window));
260         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
261
262         settings = gtk_settings_get_default();
263         GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
264         GdkColor white, black;
265         gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
266         gdk_colormap_alloc_color(colormap, &black, TRUE, TRUE);
267         if (foreground != NULL) {
268                 gdk_color_parse(foreground, &black);
269         } else {
270                 gdk_color_parse("black", &black);
271         }
272         if (background != NULL) {
273                 gdk_color_parse(background, &white);
274         } else {
275                 gdk_color_parse("white", &white);
276         }
277
278         draw = gtk_drawing_area_new();
279         gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK|GDK_KEY_PRESS_MASK);
280         gtk_widget_set_size_request(draw,400,400);
281         gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
282         gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
283         g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
284         g_signal_connect(G_OBJECT(draw), "key-press-event", G_CALLBACK(text_keypress), NULL);
285         g_object_set(draw,"can-focus",1);
286
287         GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
288         GdkColor color;
289         cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
290
291         tv = gtk_text_view_new();
292         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
293
294         if (argc > optind)
295                 if (!strcmp(argv[optind], "-") ) {
296                         // read from stdin
297                         gchar buf[1024];
298                         int num;
299
300                         input = g_string_new("");
301
302                         while ((num = fread (buf, 1, sizeof(buf), stdin)) > 0) {
303                                 g_string_append_len(input, buf, num);
304                         }
305
306                         // remove trailing newline, if any
307                         if ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
308                                 g_string_truncate(input, input->len - 1);
309                         }
310                         input_provided++;
311                 } else {
312                         int i;
313
314                         input = g_string_new("");
315
316                         for (i = optind; i < argc; i++) {
317                                 g_string_append(input, argv[i]);
318
319                                 if (i < argc - 1) {
320                                         g_string_append(input, " ");
321                                 }
322                         }
323                         input_provided++;
324                 }
325         else
326                 input = g_string_new(":-)");
327
328         gtk_text_buffer_set_text(tb, input->str, input->len);
329         GtkTextIter start, end;
330         gtk_text_buffer_get_bounds(tb, &start, &end);
331         gtk_text_buffer_select_range(tb, &start, &end);
332
333         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
334         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
335
336         GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
337         gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
338
339         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
340         entry_widget = hbox;
341         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
342         gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
343
344         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
345         gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE, TRUE, 0);
346         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
347
348         gtk_container_add(GTK_CONTAINER(window), vbox);
349
350         font = pango_font_description_new();
351         if (fontdesc != NULL) {
352                 pango_font_description_set_family(font, fontdesc);
353         } else {
354                 pango_font_description_set_family(font, "sans-serif");
355         }
356         pango_font_description_set_size(font, 200*PANGO_SCALE);
357
358         layout = gtk_widget_create_pango_layout(window,get_text());
359         pango_layout_set_font_description(layout, font);
360         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
361
362         GtkAccelGroup *accel = gtk_accel_group_new();
363         guint key;
364         GdkModifierType mod;
365         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
366         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
367         gtk_accelerator_parse("Escape", &key, &mod);
368         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
369         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
370         gtk_widget_show_all(window);
371
372         g_signal_connect_after(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
373         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
374         g_signal_connect(G_OBJECT(tv), "move-cursor", G_CALLBACK(move_cursor), NULL);
375
376         if (!input_provided)
377                 show_entry();
378         else
379                 hide_entry(NULL);
380
381         gtk_main();
382
383         return 0;
384 }