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