Move to gtk 3.0
[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                 pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
132
133                 pango_layout_get_pixel_size(layout, &w1, &h1);
134                 if (w1>0 && h1>0) {
135                         int w2 = gtk_widget_get_allocated_width(draw);
136                         int h2 = gtk_widget_get_allocated_height(draw);
137
138                         int rw1, rh1;
139                         if (rotation == 0 || rotation == 2) {
140                                 rw1 = w1;
141                                 rh1 = h1;
142                         } else {
143                                 rw1 = h1;
144                                 rh1 = w1;
145                         }
146
147                         double s = min ((double)w2/rw1, (double)h2/rh1);
148
149                         cairo_save(cr);
150
151                         GdkRGBA color;
152                         gtk_style_context_get_color (gtk_widget_get_style_context(draw),
153                                 GTK_STATE_NORMAL, &color);
154                         gdk_cairo_set_source_rgba(cr, &color);
155
156
157                         cairo_translate(cr, w2/2, h2/2);
158                         cairo_rotate(cr, rotation * M_PI_2);
159                         cairo_scale(cr, s, s);
160                         cairo_translate(cr, -w1/2, -h1/2);
161                         pango_cairo_show_layout (cr, layout);
162
163                         cairo_restore(cr);
164
165                         if (quality_high_handler) 
166                                 g_source_remove(quality_high_handler);
167                         quality_high_handler = g_timeout_add(0, quality_high, NULL);
168                 }
169                 g_object_unref(layout);
170         }
171 }
172
173 static gboolean text_keypress(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
174         // forward signal to the text view
175         gboolean ret;
176         g_signal_emit_by_name(tv, "key-press-event", event, &ret);
177         gtk_widget_grab_focus(tv);
178         return ret;
179 }
180
181 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
182         show_entry();
183         if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
184                 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
185
186                 gchar *txt = gtk_clipboard_wait_for_text(cb);
187                 if (txt != NULL) {
188                         gtk_text_buffer_set_text(tb,txt,-1);
189                         g_free(txt);
190                 }
191
192         }
193         return FALSE;
194 }
195
196 static gboolean read_chan(GIOChannel *chan, GIOCondition condition, gpointer data){
197         gchar buf[1024];
198         GString *input;
199         GIOStatus stat = G_IO_STATUS_NORMAL;
200         gsize read;
201         GError *err = NULL;
202
203         while ((stat = g_io_channel_read_chars(chan, buf, sizeof(buf), &read, &err)) == G_IO_STATUS_NORMAL && err == NULL) {
204                 g_string_append_len(partial_input, buf, read);
205         }
206
207         if (err != NULL)
208         {
209             fprintf (stderr, "Unable to read stdin: %s\n", err->message);
210             g_error_free (err);
211             return TRUE;
212         }
213
214
215         if (stat == G_IO_STATUS_EOF) {
216                 // There is an end of file, so use the whole input
217                 input = g_string_new_len(partial_input->str, partial_input->len);
218                 g_string_truncate(partial_input, 0);
219         } else {
220                 // There is no end of file. Check for form feed characters.
221                 // Use from the second-to-last to the last.
222                 char *last_ff = strrchr(partial_input->str, '\f');
223                 if (last_ff) {
224                         *last_ff = '\0';
225                         char *snd_last_ff = strrchr(partial_input->str, '\f');
226                         if (snd_last_ff == NULL) snd_last_ff = partial_input->str;
227                         input = g_string_new_len(snd_last_ff, last_ff - snd_last_ff);
228                         g_string_erase(partial_input, 0, last_ff - partial_input->str + 1);
229                 } else {
230                         return TRUE;
231                 }
232         }
233
234         // remove beginning and trailing newlines, if any
235         gssize cnt = 0;
236         while ((input->len > cnt) && (input->str[cnt] == '\n')) {
237                 cnt++;
238         }
239         g_string_erase(input, 0, cnt);
240
241         while ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
242                 g_string_truncate(input, input->len - 1);
243         }
244
245         g_signal_handler_block (tb, text_change_handler);
246         gtk_text_buffer_set_text (tb, input->str, input->len);
247         g_signal_handler_unblock (tb, text_change_handler);
248
249         g_string_free(input, TRUE);
250
251         if (stat == G_IO_STATUS_AGAIN)
252                 return TRUE;
253         else
254                 return FALSE;
255 }
256
257 static void newtext() {
258         if (quality_high_handler) {
259                 g_source_remove(quality_high_handler);
260                 quality_high_handler = 0;
261         }
262         hq(FALSE, TRUE);
263 }
264
265 static void newtext_show_input() {
266         show_entry();
267 }
268
269 static void move_cursor(GtkTextView* tv, GtkMovementStep step, gint count, gboolean extend_selection, gpointer user_data) {
270         show_entry();
271 }
272
273 static struct option const long_options[] =
274 {
275         {"help",       no_argument,       NULL, 'h'},
276         {"version",    no_argument,       NULL, 'V'},
277         {"foreground", required_argument, NULL, 'f'},
278         {"background", required_argument, NULL, 'b'},
279         {"font",       required_argument, NULL, 'n'},
280         {"rotate",     required_argument, NULL, 'r'},
281         {"align",      required_argument, NULL, 'a'},
282         {0,0,0,0}
283 };
284
285 static void usage(char *cmd) {
286         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);
287 }
288
289 static void version() {
290         printf("%s\n", PACKAGE_STRING);
291 }
292
293 int main(int argc, char **argv) {
294         GString *input;
295         int c;
296         int input_provided = 0;
297
298         while ((c = getopt_long (argc, argv, "hVf:b:n:r:a:", long_options, (int *) 0)) != EOF) {
299                 switch (c) {
300                         case 'h':
301                                 usage(argv[0]);
302                                 return 0;
303                                 break;
304
305                         case 'V':
306                                 version();
307                                 return 0;
308                                 break;
309
310                         case 'f':
311                                 foreground = optarg;
312                                 break;
313
314                         case 'b':
315                                 background = optarg;
316                                 break;
317
318                         case 'n':
319                                 fontdesc = optarg;
320                                 break;
321                         case 'r':
322                                 rotation = atoi(optarg);
323                                 break;
324                         case 'a':
325                                 alignment = atoi(optarg);
326                                 break;
327                         default:
328                                 /* unknown switch received - at least
329                                  * give usage but continue and use the
330                                  * data */
331                                 usage(argv[0]);
332                                 break;
333                 }
334         }
335
336         gtk_init(&argc, &argv);
337
338         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
339         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
340         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
341         gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
342
343         GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
344         int w = gdk_screen_get_width(screen);
345         int h = gdk_screen_get_height(screen);
346
347         GdkGeometry geometry;
348         geometry.min_width = w;
349         geometry.min_height = h;
350         geometry.max_width = w;
351         geometry.max_height = h;
352         geometry.base_width = w;
353         geometry.base_height = h;
354         geometry.win_gravity = GDK_GRAVITY_STATIC;
355         gtk_window_set_geometry_hints(GTK_WINDOW(window), window, &geometry,
356                                       GDK_HINT_MIN_SIZE |
357                                       GDK_HINT_MAX_SIZE |
358                                       GDK_HINT_BASE_SIZE |
359                                       GDK_HINT_WIN_GRAVITY |
360                                       GDK_HINT_USER_POS);
361         gtk_window_set_default_size(GTK_WINDOW(window), w, h);
362         gtk_window_move(GTK_WINDOW(window), 0, 0);
363
364
365         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
366
367         settings = gtk_settings_get_default();
368         GdkRGBA  white, black;
369         if (foreground != NULL) {
370                 gdk_rgba_parse(&black, foreground);
371         } else {
372                 gdk_rgba_parse(&black, "black");
373         }
374         if (background != NULL) {
375                 gdk_rgba_parse(&white, background);
376         } else {
377                 gdk_rgba_parse(&white, "white");
378         }
379
380         draw = gtk_drawing_area_new();
381         gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK|GDK_KEY_PRESS_MASK);
382         gtk_widget_set_size_request(draw,400,400);
383         gtk_widget_override_background_color(draw, GTK_STATE_NORMAL, &white);
384         gtk_widget_override_color(draw, GTK_STATE_NORMAL, &black);
385         g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
386         g_signal_connect(G_OBJECT(draw), "key-press-event", G_CALLBACK(text_keypress), NULL);
387         gtk_widget_set_can_focus(draw, TRUE);
388
389         cursor = gdk_cursor_new_for_display(gtk_widget_get_display(GTK_WIDGET(draw)), GDK_BLANK_CURSOR);
390
391         tv = gtk_text_view_new();
392         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
393
394         partial_input = g_string_new("");
395
396         if (argc > optind)
397                 if (!strcmp(argv[optind], "-") ) {
398                         // read from stdin
399                         GIOChannel *chan = g_io_channel_unix_new(0);
400                         g_io_channel_set_flags(chan, G_IO_FLAG_NONBLOCK, NULL);
401                         g_io_add_watch (chan, G_IO_IN | G_IO_HUP, &read_chan, NULL);
402
403                         input = g_string_new("");
404                         input_provided++;
405                 } else {
406                         int i;
407
408                         input = g_string_new("");
409
410                         for (i = optind; i < argc; i++) {
411                                 g_string_append(input, argv[i]);
412
413                                 if (i < argc - 1) {
414                                         g_string_append(input, " ");
415                                 }
416                         }
417                         input_provided++;
418                 }
419         else
420                 input = g_string_new(":-)");
421
422         gtk_text_buffer_set_text(tb, input->str, input->len);
423         g_string_free(input, TRUE);
424         GtkTextIter start, end;
425         gtk_text_buffer_get_bounds(tb, &start, &end);
426         gtk_text_buffer_select_range(tb, &start, &end);
427
428         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
429         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
430
431         GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
432         gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
433
434         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
435         entry_widget = hbox;
436         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
437         gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
438
439         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
440         gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE, TRUE, 0);
441         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
442
443         gtk_container_add(GTK_CONTAINER(window), vbox);
444
445         font = pango_font_description_new();
446         if (fontdesc != NULL) {
447                 pango_font_description_set_family(font, fontdesc);
448         } else {
449                 pango_font_description_set_family(font, "sans-serif");
450         }
451         pango_font_description_set_size(font, 200*PANGO_SCALE);
452
453         layout = gtk_widget_create_pango_layout(window,get_text());
454         pango_layout_set_font_description(layout, font);
455         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
456
457         GtkAccelGroup *accel = gtk_accel_group_new();
458         guint key;
459         GdkModifierType mod;
460         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
461         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
462         gtk_accelerator_parse("Escape", &key, &mod);
463         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
464         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
465         gtk_widget_show_all(window);
466
467         g_signal_connect_after(G_OBJECT(draw), "draw", G_CALLBACK(redraw), NULL);
468         text_change_handler = g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext_show_input), NULL);
469         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
470         g_signal_connect(G_OBJECT(tv), "move-cursor", G_CALLBACK(move_cursor), NULL);
471
472         if (!input_provided)
473                 show_entry();
474         else
475                 hide_entry(NULL);
476
477         gtk_main();
478
479         return 0;
480 }