Use icon
[darcs-mirror-screen-message.debian.git] / sm.c
diff --git a/sm.c b/sm.c
index 0c4bd87..7ab0402 100644 (file)
--- a/sm.c
+++ b/sm.c
@@ -30,7 +30,6 @@ static int min(int x, int y) {
 
 static gboolean quality = TRUE;
 static gboolean need_resize = TRUE;
-static gboolean need_quick = TRUE;
 
 static GtkWidget* window;
 static GtkWidget* draw;
@@ -47,7 +46,10 @@ static void realize(GtkWindow *window, GdkScreen *screen, gpointer data) {
 }
 
 static void clear_text(GtkAccelGroup *accel, GObject *window, guint keyval,  GdkModifierType modifier) {
-       gtk_text_buffer_set_text(tb,"",-1);
+       if( gtk_text_buffer_get_char_count(tb) )
+               gtk_text_buffer_set_text(tb,"",-1);
+       else
+               gtk_main_quit();
 }
 
 static char *get_text() {
@@ -86,6 +88,20 @@ static void redraw() {
        }
 }
 
+static gboolean text_clicked(GtkWidget *widget, GdkEventButton *event, gpointer *user_data) {
+       if (event->type == GDK_BUTTON_PRESS && event->button == 2) {
+               GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
+
+               gchar *txt = gtk_clipboard_wait_for_text(cb);
+               if (txt != NULL) {
+                       gtk_text_buffer_set_text(tb,txt,-1);
+                       g_free(txt);
+               }
+
+       }
+       return FALSE;
+}
+
 static void resize() {
        int w1, h1, w2, h2;
        pango_layout_get_pixel_size(layout, &w1, &h1);
@@ -110,40 +126,83 @@ static void newtext(char *text) {
 }
 
 int main(int argc, char **argv) {
+       GString *input;
+
        gtk_init(&argc, &argv);
 
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
+       gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
+       gtk_window_set_icon_name (GTK_WINDOW (window), "sm");
+
+       GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
+       gtk_widget_set_size_request(window, gdk_screen_get_width(screen),
+                                           gdk_screen_get_height(screen));
        gtk_window_fullscreen(GTK_WINDOW(window));
+       g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
 
        settings = gtk_settings_get_default();
        GdkColormap *colormap = gtk_widget_get_colormap(GTK_WIDGET(window));
-       GdkColor white;
+       GdkColor white, black;
        gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
        gdk_color_parse("white", &white);
-       gtk_widget_modify_bg(GTK_WIDGET(window), GTK_STATE_NORMAL, &white);
+       gdk_color_parse("black", &black);
+       gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &white);
+       gtk_widget_modify_fg(window, GTK_STATE_NORMAL, &black);
 
        draw = gtk_drawing_area_new();
+       gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK);
        gtk_widget_set_size_request(draw,400,400);
        gtk_widget_modify_bg(draw, GTK_STATE_NORMAL, &white);
+       gtk_widget_modify_fg(draw, GTK_STATE_NORMAL, &black);
        g_signal_connect(G_OBJECT(draw), "realize", G_CALLBACK(realize), NULL);
+       g_signal_connect(G_OBJECT(draw), "button-press-event", G_CALLBACK(text_clicked), NULL);
 
        GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
        GdkColor color;
        cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
 
        tv = gtk_text_view_new();
+       gtk_widget_modify_bg(tv, GTK_STATE_NORMAL, &white);
+       gtk_widget_modify_fg(tv, GTK_STATE_NORMAL, &black);
        tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
-       
+
        if (argc > 1)
-               //tb.set_text(" ".join(sys.argv[1:]))
-               gtk_text_buffer_set_text(tb, argv[1], -1);
+               if (!strcmp(argv[1], "-") ) {
+                       // read from stdin
+                       gchar buf[1024];
+                       int num;
+
+                       input = g_string_new("");
+
+                       while ((num = fread (buf, 1, sizeof(buf), stdin)) > 0) {
+                               g_string_append_len(input, buf, num);
+                       }
+               } else {
+                       int i;
+
+                       input = g_string_new("");
+
+                       for (i = 1; i < argc; i++) {
+                               g_string_append(input, argv[i]);
+
+                               if (i < argc - 1) {
+                                       g_string_append(input, " ");
+                               }
+                       }
+               }
        else
-               gtk_text_buffer_set_text(tb, ":-)", -1);
+               input = g_string_new(":-)");
+
+       gtk_text_buffer_set_text(tb, input->str, input->len);
+       GtkTextIter start, end;
+       gtk_text_buffer_get_bounds(tb, &start, &end);
+       gtk_text_buffer_select_range(tb, &start, &end);
 
        quit = gtk_button_new_from_stock(GTK_STOCK_QUIT);
        g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
        gtk_widget_modify_bg(quit, GTK_STATE_NORMAL, &white);
+       gtk_widget_modify_fg(quit, GTK_STATE_NORMAL, &black);
 
        GtkWidget *hbox = gtk_hbox_new(FALSE,0);
        gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
@@ -156,6 +215,7 @@ int main(int argc, char **argv) {
        gtk_container_add(GTK_CONTAINER(window), vbox);
 
        font = pango_font_description_new();
+        pango_font_description_set_family(font, "sans-serif");
        pango_font_description_set_size(font, 20*PANGO_SCALE);
 
        layout=  gtk_widget_create_pango_layout(draw,get_text());
@@ -176,5 +236,9 @@ int main(int argc, char **argv) {
        g_signal_connect(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
        g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
 
+       resize();
+
        gtk_main();
+
+       return 0;
 }