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