Only show entry from start on when nothing is provided
[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 5
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 GdkCursor *cursor;
45 static GtkWidget* quit;
46 static GtkWidget* tv;
47 static GtkWidget* entry_widget;
48 static GtkSettings* settings;
49 static GtkTextBuffer* tb;
50 static PangoFontDescription *font;
51 static PangoLayout* layout;
52 static char *foreground = NULL;
53 static char *background = NULL;
54 static char *fontdesc = NULL;
55 static rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
56
57 gboolean hide_entry(gpointer *user_data) {
58         gtk_widget_hide(entry_widget);
59         gtk_widget_grab_focus(tv);
60         return FALSE;
61 }
62
63 static void show_entry() {
64         if (timeout_id) {
65                 g_source_remove(timeout_id);
66                 timeout_id = 0;
67         }
68         gtk_widget_show(entry_widget);
69
70         timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
71 }
72
73 static void realize(GtkWindow *window, GdkScreen *screen, gpointer data) {
74         gdk_window_set_cursor(GTK_WIDGET(window)->window, cursor);
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(window);
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(window)->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(window->window, gc,
144                                 (w2-(s*rw1))/2, (h2-(s*rh1))/2,layout);
145                         hq(TRUE, FALSE);
146                 }
147         }
148 }
149
150 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
151         show_entry();
152         if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
153                 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
154
155                 gchar *txt = gtk_clipboard_wait_for_text(cb);
156                 if (txt != NULL) {
157                         gtk_text_buffer_set_text(tb,txt,-1);
158                         g_free(txt);
159                 }
160
161         }
162         return FALSE;
163 }
164
165 static void newtext(char *text) {
166         show_entry();
167         pango_layout_set_text(layout, get_text(), -1);
168         hq(FALSE, TRUE);
169 }
170
171 static struct option const long_options[] =
172 {
173         {"help",       no_argument,       NULL, 'h'},
174         {"version",    no_argument,       NULL, 'V'},
175         {"foreground", required_argument, NULL, 'f'},
176         {"background", required_argument, NULL, 'b'},
177         {"font",       required_argument, NULL, 'n'},
178         {"rotate",     required_argument, NULL, 'r'},
179         {0,0,0,0}
180 };
181
182 static void usage(char *cmd) {
183         printf("Usage: %s [-h|--help] [-V|--version] [-f|--foreground=colordesc] [-b|--background=colordesc] [-n|--font=fontdesc] [-r|--rotate=0,1,2,3]\n", cmd);
184 }
185
186 static void version() {
187         printf("%s\n", PACKAGE_STRING);
188 }
189
190 int main(int argc, char **argv) {
191         GString *input;
192         int c;
193         int input_provided = 0;
194
195         while ((c = getopt_long (argc, argv, "hVf:b:n:r:", long_options, (int *) 0)) != EOF) {
196                 switch (c) {
197                         case 'h':
198                                 usage(argv[0]);
199                                 return 0;
200                                 break;
201
202                         case 'V':
203                                 version();
204                                 return 0;
205                                 break;
206
207                         case 'f':
208                                 foreground = optarg;
209                                 break;
210
211                         case 'b':
212                                 background = optarg;
213                                 break;
214
215                         case 'n':
216                                 fontdesc = optarg;
217                                 break;
218                         case 'r':
219                                 rotation = atoi(optarg);
220                                 break;
221                         default:
222                                 /* unknown switch received - at least
223                                  * give usage but continue and use the
224                                  * data */
225                                 usage(argv[0]);
226                                 break;
227                 }
228         }
229
230         gtk_init(&argc, &argv);
231
232         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
233         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
234         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
235         gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
236
237         GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
238         gtk_widget_set_size_request(window, gdk_screen_get_width(screen),
239                                             gdk_screen_get_height(screen));
240         gtk_window_fullscreen(GTK_WINDOW(window));
241         g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
242
243         settings = gtk_settings_get_default();
244         GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
245         GdkColor white, black;
246         gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
247         gdk_colormap_alloc_color(colormap, &black, TRUE, TRUE);
248         if (foreground != NULL) {
249                 gdk_color_parse(foreground, &black);
250         } else {
251                 gdk_color_parse("black", &black);
252         }
253         if (background != NULL) {
254                 gdk_color_parse(background, &white);
255         } else {
256                 gdk_color_parse("white", &white);
257         }
258         gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &white);
259         gtk_widget_modify_fg(window, GTK_STATE_NORMAL, &black);
260
261         gtk_widget_set_events(window, GDK_BUTTON_PRESS_MASK);
262         g_signal_connect(G_OBJECT(window), "realize", G_CALLBACK(realize), NULL);
263         g_signal_connect(G_OBJECT(window), "button-press-event", G_CALLBACK(text_clicked), NULL);
264
265         GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
266         GdkColor color;
267         cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
268
269         tv = gtk_text_view_new();
270         gtk_widget_modify_bg(tv, GTK_STATE_NORMAL, &white);
271         gtk_widget_modify_fg(tv, GTK_STATE_NORMAL, &black);
272         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
273
274         if (argc > optind)
275                 if (!strcmp(argv[optind], "-") ) {
276                         // read from stdin
277                         gchar buf[1024];
278                         int num;
279
280                         input = g_string_new("");
281
282                         while ((num = fread (buf, 1, sizeof(buf), stdin)) > 0) {
283                                 g_string_append_len(input, buf, num);
284                         }
285
286                         // remove trailing newline, if any
287                         if ((input->len > 0) && (input->str[input->len - 1] == '\n')) {
288                                 g_string_truncate(input, input->len - 1);
289                         }
290                         input_provided++;
291                 } else {
292                         int i;
293
294                         input = g_string_new("");
295
296                         for (i = optind; i < argc; i++) {
297                                 g_string_append(input, argv[i]);
298
299                                 if (i < argc - 1) {
300                                         g_string_append(input, " ");
301                                 }
302                         }
303                         input_provided++;
304                 }
305         else
306                 input = g_string_new(":-)");
307
308         gtk_text_buffer_set_text(tb, input->str, input->len);
309         GtkTextIter start, end;
310         gtk_text_buffer_get_bounds(tb, &start, &end);
311         gtk_text_buffer_select_range(tb, &start, &end);
312
313         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
314         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
315         gtk_widget_modify_bg(quit, GTK_STATE_NORMAL, &white);
316         gtk_widget_modify_fg(quit, GTK_STATE_NORMAL, &black);
317
318         GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
319         gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
320
321         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
322         entry_widget = hbox;
323         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
324         gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
325
326         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
327         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
328
329         gtk_container_add(GTK_CONTAINER(window), vbox);
330
331         font = pango_font_description_new();
332         if (fontdesc != NULL) {
333                 pango_font_description_set_family(font, fontdesc);
334         } else {
335                 pango_font_description_set_family(font, "sans-serif");
336         }
337         pango_font_description_set_size(font, 200*PANGO_SCALE);
338
339         layout = gtk_widget_create_pango_layout(window,get_text());
340         pango_layout_set_font_description(layout, font);
341         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
342
343         GtkAccelGroup *accel = gtk_accel_group_new();
344         guint key;
345         GdkModifierType mod;
346         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
347         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
348         gtk_accelerator_parse("Escape", &key, &mod);
349         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
350         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
351         gtk_widget_show_all(window);
352
353         g_signal_connect_after(G_OBJECT(window), "expose-event", G_CALLBACK(redraw), NULL);
354         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
355
356         if (!input_provided)
357                 show_entry();
358         else
359                 hide_entry(NULL);
360
361         gtk_main();
362
363         return 0;
364 }