Do not export unnecessary symbols.
[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
26 static int min(int x, int y) {
27         return x < y ? x : y;
28 }
29
30
31 static gboolean quality = TRUE;
32 static gboolean need_resize = TRUE;
33 static gboolean need_quick = TRUE;
34
35 static GtkWidget* window;
36 static GtkWidget* draw;
37 static GdkCursor *cursor;
38 static GtkWidget* quit;
39 static GtkWidget* tv;
40 static GtkSettings* settings;
41 static GtkTextBuffer* tb;
42 static PangoFontDescription *font;
43 static PangoLayout* layout;
44
45 static void realize(GtkWindow *window, GdkScreen *screen, gpointer data) {
46         gdk_window_set_cursor(draw->window, cursor);
47 }
48
49 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
50         gtk_text_buffer_set_text(tb,"",-1);
51 }
52
53 static char *get_text() {
54         GtkTextIter start, end;
55         gtk_text_buffer_get_start_iter(tb,&start);
56         gtk_text_buffer_get_end_iter(tb,&end);
57         return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
58 }
59
60
61 static void hq(gboolean q, gboolean force){
62         if (q != quality) 
63                 if (q)
64                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
65                 else
66                         gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
67         else
68                 if (force)
69                         gtk_widget_queue_draw(draw);
70
71         quality = q;
72 }
73
74 static void redraw() {
75         const char *text = pango_layout_get_text(layout);
76         if (strlen(text) > 0) {
77                 GdkGC *gc = gtk_widget_get_style(draw)->fg_gc[GTK_STATE_NORMAL];
78                 int w1, h1;
79                 pango_layout_get_pixel_size(layout, &w1, &h1);
80                 if (w1>0 && h1>0) {
81                         int w2 = draw->allocation.width;
82                         int h2 = draw->allocation.height;
83                         gdk_draw_layout(draw->window, gc, (w2-w1)/2,(h2-h1)/2,layout);
84                         hq(TRUE, FALSE);
85                 }
86         }
87 }
88
89 static void resize() {
90         int w1, h1, w2, h2;
91         pango_layout_get_pixel_size(layout, &w1, &h1);
92         if (w1>0 && h1>0) {
93                 w2 = draw->allocation.width;
94                 h2 = draw->allocation.height;
95                 int s = pango_font_description_get_size(font);
96                 s = min ((s*w2/w1), (s*h2/h1));
97                 pango_font_description_set_size(font,s);
98                 pango_layout_set_font_description(layout, font);
99                 need_resize = FALSE;
100         }
101         else
102                 need_resize = TRUE;
103 }
104
105
106 static void newtext(char *text) {
107         pango_layout_set_text(layout, get_text(), -1);
108         resize();
109         hq(FALSE, TRUE);
110 }
111
112 int main(int argc, char **argv) {
113         gtk_init(&argc, &argv);
114
115         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
116         gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
117         gtk_window_fullscreen(GTK_WINDOW(window));
118
119         settings = gtk_settings_get_default();
120         GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
121         GdkColor white;
122         gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
123         gdk_color_parse("white", &white);
124         gtk_widget_modify_bg(GTK_WIDGET(window), GTK_STATE_NORMAL, &white);
125
126         draw = gtk_drawing_area_new();
127         gtk_widget_set_size_request(draw,400,400);
128         gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
129         g_signal_connect(G_OBJECT(draw), "realize", G_CALLBACK(realize), NULL);
130
131         GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
132         GdkColor color;
133         cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
134
135         tv = gtk_text_view_new();
136         tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
137         
138         if (argc > 1)
139                 //tb.set_text(" ".join(sys.argv[1:]))
140                 gtk_text_buffer_set_text(tb, argv[1], -1);
141         else
142                 gtk_text_buffer_set_text(tb, ":-)", -1);
143
144         quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
145         g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
146         gtk_widget_modify_bg(quit, GTK_STATE_NORMAL, &white);
147
148         GtkWidget *hbox = gtk_hbox_new(FALSE,0);
149         gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
150         gtk_box_pack_start(GTK_BOX(hbox), quit, FALSE, FALSE, 0);
151
152         GtkWidget *vbox = gtk_vbox_new(FALSE,0);
153         gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE,  TRUE,  0);
154         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
155
156         gtk_container_add(GTK_CONTAINER(window), vbox);
157
158         font = pango_font_description_new();
159         pango_font_description_set_size(font, 20*PANGO_SCALE);
160
161         layout=  gtk_widget_create_pango_layout(draw,get_text());
162         pango_layout_set_font_description(layout, font);
163         pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
164
165         GtkAccelGroup *accel = gtk_accel_group_new();
166         guint key;
167         GdkModifierType mod;
168         gtk_accelerator_parse("<Ctrl>Q", &key, &mod);
169         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(gtk_main_quit), NULL, NULL));
170         gtk_accelerator_parse("Escape", &key, &mod);
171         gtk_accel_group_connect(accel, key, mod, 0, g_cclosure_new(G_CALLBACK(clear_text), NULL, NULL));
172         gtk_window_add_accel_group(GTK_WINDOW(window), accel);
173         gtk_widget_show_all(window);
174
175         g_signal_connect(G_OBJECT(draw), "configure-event", G_CALLBACK(resize), NULL);
176         g_signal_connect(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
177         g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
178
179         gtk_main();
180 }