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