Float entry above background
[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 GdkCursor *cursor;
42 static GtkWidget* quit;
43 static GtkWidget* tv;
44 static GtkSettings* settings;
45 static GtkTextBuffer* tb;
46 static PangoFontDescription *font;
47 static PangoLayout* layout;
48 static char *foreground = NULL;
49 static char *background = NULL;
50 static char *fontdesc = NULL;
51 static rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
52
53 static void realize(GtkWindow *window, GdkScreen *screen, gpointer data) {
54         gdk_window_set_cursor(GTK_WIDGET(window)->window, cursor);
55 }
56
57 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
58         if( gtk_text_buffer_get_char_count(tb) )
59                 gtk_text_buffer_set_text(tb,"",-1);
60         else
61                 gtk_main_quit();
62 }
63
64 static char *get_text() {
65         GtkTextIter start, end;
66         gtk_text_buffer_get_start_iter(tb,&start);
67         gtk_text_buffer_get_end_iter(tb,&end);
68         return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
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(window);
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(window)->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 = window->allocation.width;
92                         int h2 = window->allocation.height;
93
94                         int rw1, rh1;
95                         if (rotation == 0 || rotation == 2) {
96                                 rw1 = w1;
97                                 rh1 = h1;
98                         } else {
99                                 rw1 = h1;
100                                 rh1 = w1;
101                         }
102
103                         double s = min ((double)w2/rw1, (double)h2/rh1);
104                         
105
106                         PangoMatrix matrix = PANGO_MATRIX_INIT;
107                         // Move matrix to the screen center
108                         pango_matrix_translate(&matrix, w2/2, h2/2);
109                         // Scale as required
110                         pango_matrix_scale(&matrix, s, s);
111                         // Rotate if needed
112                         pango_matrix_rotate (&matrix, rotation * 90);
113                         // Move matrix so that text will be centered
114                         //pango_matrix_translate(&matrix, -w1/2, -h1/2);
115                         
116                         // Apply matrix
117                         PangoContext *context =  pango_layout_get_context(layout);
118                         pango_context_set_matrix (context, &matrix);
119                         pango_layout_context_changed (layout);
120
121                         gdk_draw_layout(window->window, gc,
122                                 (w2-(s*rw1))/2, (h2-(s*rh1))/2,layout);
123                         hq(TRUE, FALSE);
124                 }
125         }
126 }
127
128 static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
129         printf("Hi\n");
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         gtk_widget_set_events(window, GDK_BUTTON_PRESS_MASK);
238         g_signal_connect(G_OBJECT(window), "realize", G_CALLBACK(realize), NULL);
239         g_signal_connect(G_OBJECT(window), "button-press-event", G_CALLBACK(text_clicked), NULL);
240
241         GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
242         GdkColor color;
243         cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
244
245         tv = gtk_text_view_new();
246         gtk_widget_modify_bg(tv, GTK_STATE_NORMAL, &white);
247         gtk_widget_modify_fg(tv, GTK_STATE_NORMAL, &black);
248         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
249
250         if (argc > optind)
251                 if (!strcmp(argv[optind], "-") ) {
252                         // read from stdin
253                         gchar buf[1024];
254                         int num;
255
256                         input = g_string_new("");
257
258                         while ((num = fread (buf, 1, sizeof(buf), stdin)) > 0) {
259                                 g_string_append_len(input, buf, num);
260                         }
261                 } else {
262                         int i;
263
264                         input = g_string_new("");
265
266                         for (i = optind; i < argc; i++) {
267                                 g_string_append(input, argv[i]);
268
269                                 if (i < argc - 1) {
270                                         g_string_append(input, " ");
271                                 }
272                         }
273                 }
274         else
275                 input = g_string_new(":-)");
276
277         gtk_text_buffer_set_text(tb, input->str, input->len);
278         GtkTextIter start, end;
279         gtk_text_buffer_get_bounds(tb, &start, &end);
280         gtk_text_buffer_select_range(tb, &start, &end);
281
282         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
283         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
284         gtk_widget_modify_bg(quit, GTK_STATE_NORMAL, &white);
285         gtk_widget_modify_fg(quit, GTK_STATE_NORMAL, &black);
286
287         GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
288         gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
289
290         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
291         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
292         gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
293
294         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
295         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
296
297         gtk_container_add(GTK_CONTAINER(window), vbox);
298
299         font = pango_font_description_new();
300         if (fontdesc != NULL) {
301                 pango_font_description_set_family(font, fontdesc);
302         } else {
303                 pango_font_description_set_family(font, "sans-serif");
304         }
305         pango_font_description_set_size(font, 200*PANGO_SCALE);
306
307         layout = gtk_widget_create_pango_layout(window,get_text());
308         pango_layout_set_font_description(layout, font);
309         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
310
311         GtkAccelGroup *accel = gtk_accel_group_new();
312         guint key;
313         GdkModifierType mod;
314         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
315         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
316         gtk_accelerator_parse("Escape", &key, &mod);
317         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
318         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
319         gtk_widget_show_all(window);
320
321         g_signal_connect_after(G_OBJECT(window), "expose-event", G_CALLBACK(redraw), NULL);
322         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
323
324         gtk_main();
325
326         return 0;
327 }