Align the whole text area as well
[darcs-mirror-screen-message.debian.git] / sm.c
1 /*
2 #     sm.c
3 #     Copyright (C) 2006 - 2012 Joachim Breitner
4 #
5 #     The Code for making a window fullscreen was taken from src/fullscreen.c in
6 #     the geeqie package:
7 #     Copyright (C) 2004 John Ellis
8 #                   2008 - 2010 The Geeqie Team
9 #
10 #     This program is free software; you can redistribute it and/or modify
11 #     it under the terms of the GNU General Public License as published by
12 #     the Free Software Foundation; either version 2 of the License, or
13 #     (at your option) any later version.
14 #
15 #     This program is distributed in the hope that it will be useful,
16 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #     GNU General Public License for more details.
19 #
20 #     You should have received a copy of the GNU General Public License
21 #     along with this program; if not, write to the Free Software
22 #     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
23 */
24
25 #include <gtk/gtk.h>
26 #include <gdk/gdk.h>
27 #include <pango/pango.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <math.h>
32
33 #include "config.h"
34
35 #ifndef _GNU_SOURCE
36 #define _GNU_SOURCE                             /* for getopt_long */
37 #endif
38 #include <getopt.h>
39
40 #define min(x,y) ((x) < (y) ? (x) : (y))
41
42 #define AUTOHIDE_TIMEOUT 3
43
44 static gboolean quality = TRUE;
45
46 static int timeout_id=0;
47
48 static GtkWidget* window;
49 static GtkWidget* draw;
50 static GdkCursor *cursor;
51 static GtkWidget* quit;
52 static GtkWidget* tv;
53 static GtkWidget* entry_widget;
54 static GtkSettings* settings;
55 static GtkTextBuffer* tb;
56 static PangoFontDescription *font;
57 static char *foreground = NULL;
58 static char *background = NULL;
59 static char *fontdesc = NULL;
60 static int rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
61 static int alignment = 0; // 0 = centered, 1 = left-aligned, 2 = right-aligned
62 static GString *partial_input;
63 static gulong quality_high_handler = 0;
64 static gulong text_change_handler;
65
66 gboolean hide_entry(gpointer *user_data) {
67         gtk_widget_hide(entry_widget);
68         gtk_widget_grab_focus(draw);
69         gtk_widget_queue_draw(draw);
70         gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(draw)), cursor);
71         return FALSE;
72 }
73
74 static void show_entry() {
75         if (timeout_id) {
76                 g_source_remove(timeout_id);
77                 timeout_id = 0;
78         }
79         gtk_widget_show(entry_widget);
80         gtk_widget_grab_focus(tv);
81         gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(draw)), NULL);
82
83         timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
84 }
85
86 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
87         if( gtk_text_buffer_get_char_count(tb) ) {
88                 gtk_text_buffer_set_text(tb,"",-1);
89                 show_entry();
90         } else {
91                 gtk_main_quit();
92         }
93 }
94
95 static char *get_text() {
96         GtkTextIter start, end;
97         gtk_text_buffer_get_start_iter(tb,&start);
98         gtk_text_buffer_get_end_iter(tb,&end);
99         return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
100 }
101
102 static void hq(gboolean q, gboolean force){
103         if (q != quality) {
104                 if (q)
105                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
106                 else
107                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
108         }
109         else
110                 if (force)
111                         gtk_widget_queue_draw(draw);
112
113         quality = q;
114 }
115
116 static gboolean quality_high (gpointer data) {
117         hq(TRUE, FALSE);
118         return FALSE;
119 }
120
121 static void redraw(GtkWidget *draw, cairo_t *cr, gpointer data) {
122         int q;
123         const char *text = get_text();
124
125         if (strlen(text) > 0) {
126                 int w1, h1;
127                 static PangoLayout* layout;
128
129                 layout = gtk_widget_create_pango_layout(draw, get_text());
130                 pango_layout_set_font_description(layout, font);
131
132                 switch(alignment){
133                         case 0: // center
134                                 pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
135                                 break;
136                         case 1: // left
137                                 pango_layout_set_alignment(layout,PANGO_ALIGN_LEFT);
138                                 break;
139                         case 2: // left
140                                 pango_layout_set_alignment(layout,PANGO_ALIGN_RIGHT);
141                                 break;
142                         default:
143                                 // we propably don't want to annoy the user, so default to
144                                 // the old default-behaviour:
145                                 pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
146                 }
147
148                 pango_layout_get_pixel_size(layout, &w1, &h1);
149                 if (w1>0 && h1>0) {
150                         int w2 = gtk_widget_get_allocated_width(draw);
151                         int h2 = gtk_widget_get_allocated_height(draw);
152
153                         int rw1, rh1;
154                         if (rotation == 0 || rotation == 2) {
155                                 rw1 = w1;
156                                 rh1 = h1;
157                         } else {
158                                 rw1 = h1;
159                                 rh1 = w1;
160                         }
161
162                         double s = min ((double)w2/rw1, (double)h2/rh1);
163
164                         cairo_save(cr);
165
166                         GdkRGBA color;
167                         gtk_style_context_get_color (gtk_widget_get_style_context(draw),
168                                 GTK_STATE_NORMAL, &color);
169                         gdk_cairo_set_source_rgba(cr, &color);
170
171
172                         if (alignment == 1) { // left align
173                                 cairo_translate(cr, (s * rw1)/2, h2/2);
174                         } else if (alignment == 2) { // right align
175                                 cairo_translate(cr, w2 - (s * rw1)/2, h2/2);
176                         } else {
177                                 cairo_translate(cr, w2/2, h2/2);
178                         }
179                         cairo_rotate(cr, rotation * M_PI_2);
180                         cairo_scale(cr, s, s);
181                         cairo_translate(cr, -w1/2, -h1/2);
182                         pango_cairo_show_layout (cr, layout);
183
184                         cairo_restore(cr);
185
186                         if (quality_high_handler) 
187                                 g_source_remove(quality_high_handler);
188                         quality_high_handler = g_timeout_add(0, quality_high, NULL);
189                 }
190                 g_object_unref(layout);
191         }
192 }
193
194 static gboolean text_keypress(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
195         // forward signal to the text view
196         gboolean ret;
197         g_signal_emit_by_name(tv, "key-press-event", event, &ret);
198         gtk_widget_grab_focus(tv);
199         return ret;
200 }
201
202 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
203         show_entry();
204         if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
205                 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
206
207                 gchar *txt = gtk_clipboard_wait_for_text(cb);
208                 if (txt != NULL) {
209                         gtk_text_buffer_set_text(tb,txt,-1);
210                         g_free(txt);
211                 }
212
213         }
214         return FALSE;
215 }
216
217 static gboolean read_chan(GIOChannel *chan, GIOCondition condition, gpointer data){
218         gchar buf[1024];
219         GString *input;
220         GIOStatus stat = G_IO_STATUS_NORMAL;
221         gsize read;
222         GError *err = NULL;
223
224         while ((stat = g_io_channel_read_chars(chan, buf, sizeof(buf), &read, &err)) == G_IO_STATUS_NORMAL && err == NULL) {
225                 g_string_append_len(partial_input, buf, read);
226         }
227
228         if (err != NULL)
229         {
230             fprintf (stderr, "Unable to read stdin: %s\n", err->message);
231             g_error_free (err);
232             return TRUE;
233         }
234
235
236         if (stat == G_IO_STATUS_EOF) {
237                 // There is an end of file, so use the whole input
238                 input = g_string_new_len(partial_input->str, partial_input->len);
239                 g_string_truncate(partial_input, 0);
240         } else {
241                 // There is no end of file. Check for form feed characters.
242                 // Use from the second-to-last to the last.
243                 char *last_ff = strrchr(partial_input->str, '\f');
244                 if (last_ff) {
245                         *last_ff = '\0';
246                         char *snd_last_ff = strrchr(partial_input->str, '\f');
247                         if (snd_last_ff == NULL) snd_last_ff = partial_input->str;
248                         input = g_string_new_len(snd_last_ff, last_ff - snd_last_ff);
249                         g_string_erase(partial_input, 0, last_ff - partial_input->str + 1);
250                 } else {
251                         return TRUE;
252                 }
253         }
254
255         // remove beginning and trailing newlines, if any
256         gssize cnt = 0;
257         while ((input->len > cnt) && (input->str[cnt] == '\n')) {
258                 cnt++;
259         }
260         g_string_erase(input, 0, cnt);
261
262         while ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
263                 g_string_truncate(input, input->len - 1);
264         }
265
266         g_signal_handler_block (tb, text_change_handler);
267         gtk_text_buffer_set_text (tb, input->str, input->len);
268         g_signal_handler_unblock (tb, text_change_handler);
269
270         g_string_free(input, TRUE);
271
272         if (stat == G_IO_STATUS_AGAIN)
273                 return TRUE;
274         else
275                 return FALSE;
276 }
277
278 static void newtext() {
279         if (quality_high_handler) {
280                 g_source_remove(quality_high_handler);
281                 quality_high_handler = 0;
282         }
283         hq(FALSE, TRUE);
284 }
285
286 static void newtext_show_input() {
287         show_entry();
288 }
289
290 static void move_cursor(GtkTextView* tv, GtkMovementStep step, gint count, gboolean extend_selection, gpointer user_data) {
291         show_entry();
292 }
293
294 static struct option const long_options[] =
295 {
296         {"help",       no_argument,       NULL, 'h'},
297         {"version",    no_argument,       NULL, 'V'},
298         {"foreground", required_argument, NULL, 'f'},
299         {"background", required_argument, NULL, 'b'},
300         {"font",       required_argument, NULL, 'n'},
301         {"rotate",     required_argument, NULL, 'r'},
302         {"align",      required_argument, NULL, 'a'},
303         {0,0,0,0}
304 };
305
306 static void usage(char *cmd) {
307         printf("Usage: %s [-h|--help] [-V|--version] [-f|--foreground=colordesc] [-b|--background=colordesc] [-n|--font=fontdesc] [-r|--rotate=0,1,2,3] [-a|--align=0,1,2]\n", cmd);
308 }
309
310 static void version() {
311         printf("%s\n", PACKAGE_STRING);
312 }
313
314 int main(int argc, char **argv) {
315         GString *input;
316         int c;
317         int input_provided = 0;
318
319         while ((c = getopt_long (argc, argv, "hVf:b:n:r:a:", long_options, (int *) 0)) != EOF) {
320                 switch (c) {
321                         case 'h':
322                                 usage(argv[0]);
323                                 return 0;
324                                 break;
325
326                         case 'V':
327                                 version();
328                                 return 0;
329                                 break;
330
331                         case 'f':
332                                 foreground = optarg;
333                                 break;
334
335                         case 'b':
336                                 background = optarg;
337                                 break;
338
339                         case 'n':
340                                 fontdesc = optarg;
341                                 break;
342                         case 'r':
343                                 rotation = atoi(optarg);
344                                 break;
345                         case 'a':
346                                 alignment = atoi(optarg);
347                                 break;
348                         default:
349                                 /* unknown switch received - at least
350                                  * give usage but continue and use the
351                                  * data */
352                                 usage(argv[0]);
353                                 break;
354                 }
355         }
356
357         gtk_init(&argc, &argv);
358
359         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
360         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
361         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
362         gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
363
364         GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
365         int w = gdk_screen_get_width(screen);
366         int h = gdk_screen_get_height(screen);
367
368         GdkGeometry geometry;
369         geometry.min_width = w;
370         geometry.min_height = h;
371         geometry.max_width = w;
372         geometry.max_height = h;
373         geometry.base_width = w;
374         geometry.base_height = h;
375         geometry.win_gravity = GDK_GRAVITY_STATIC;
376         gtk_window_set_geometry_hints(GTK_WINDOW(window), window, &geometry,
377                                       GDK_HINT_MIN_SIZE |
378                                       GDK_HINT_MAX_SIZE |
379                                       GDK_HINT_BASE_SIZE |
380                                       GDK_HINT_WIN_GRAVITY |
381                                       GDK_HINT_USER_POS);
382         gtk_window_set_default_size(GTK_WINDOW(window), w, h);
383         gtk_window_move(GTK_WINDOW(window), 0, 0);
384
385
386         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
387
388         settings = gtk_settings_get_default();
389         GdkRGBA  white, black;
390         if (foreground != NULL) {
391                 gdk_rgba_parse(&black, foreground);
392         } else {
393                 gdk_rgba_parse(&black, "black");
394         }
395         if (background != NULL) {
396                 gdk_rgba_parse(&white, background);
397         } else {
398                 gdk_rgba_parse(&white, "white");
399         }
400
401         draw = gtk_drawing_area_new();
402         gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK|GDK_KEY_PRESS_MASK);
403         gtk_widget_set_size_request(draw,400,400);
404         gtk_widget_override_background_color(draw, GTK_STATE_NORMAL, &white);
405         gtk_widget_override_color(draw, GTK_STATE_NORMAL, &black);
406         g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
407         g_signal_connect(G_OBJECT(draw), "key-press-event", G_CALLBACK(text_keypress), NULL);
408         gtk_widget_set_can_focus(draw, TRUE);
409
410         cursor = gdk_cursor_new_for_display(gtk_widget_get_display(GTK_WIDGET(draw)), GDK_BLANK_CURSOR);
411
412         tv = gtk_text_view_new();
413         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
414
415         partial_input = g_string_new("");
416
417         if (argc > optind)
418                 if (!strcmp(argv[optind], "-") ) {
419                         // read from stdin
420                         GIOChannel *chan = g_io_channel_unix_new(0);
421                         g_io_channel_set_flags(chan, G_IO_FLAG_NONBLOCK, NULL);
422                         g_io_add_watch (chan, G_IO_IN | G_IO_HUP, &read_chan, NULL);
423
424                         input = g_string_new("");
425                         input_provided++;
426                 } else {
427                         int i;
428
429                         input = g_string_new("");
430
431                         for (i = optind; i < argc; i++) {
432                                 g_string_append(input, argv[i]);
433
434                                 if (i < argc - 1) {
435                                         g_string_append(input, " ");
436                                 }
437                         }
438                         input_provided++;
439                 }
440         else
441                 input = g_string_new(":-)");
442
443         gtk_text_buffer_set_text(tb, input->str, input->len);
444         g_string_free(input, TRUE);
445         GtkTextIter start, end;
446         gtk_text_buffer_get_bounds(tb, &start, &end);
447         gtk_text_buffer_select_range(tb, &start, &end);
448
449         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
450         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
451
452         GtkWidget *vbox_button = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
453         gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
454
455         GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,0);
456         entry_widget = hbox;
457         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
458         gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
459
460         GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,0);
461         gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE, TRUE, 0);
462         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
463
464         gtk_container_add(GTK_CONTAINER(window), vbox);
465
466         font = pango_font_description_new();
467         if (fontdesc != NULL) {
468                 pango_font_description_set_family(font, fontdesc);
469         } else {
470                 pango_font_description_set_family(font, "sans-serif");
471         }
472         pango_font_description_set_size(font, 200*PANGO_SCALE);
473
474         GtkAccelGroup *accel = gtk_accel_group_new();
475         guint key;
476         GdkModifierType mod;
477         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
478         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
479         gtk_accelerator_parse("Escape", &key, &mod);
480         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
481         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
482         gtk_widget_show_all(window);
483
484         g_signal_connect_after(G_OBJECT(draw), "draw", G_CALLBACK(redraw), NULL);
485         text_change_handler = g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext_show_input), NULL);
486         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
487         g_signal_connect(G_OBJECT(tv), "move-cursor", G_CALLBACK(move_cursor), NULL);
488
489         if (!input_provided)
490                 show_entry();
491         else
492                 hide_entry(NULL);
493
494         gtk_main();
495
496         return 0;
497 }