properly use multiple commandline arguments
[darcs-mirror-screen-message.debian.git] / sm.c
diff --git a/sm.c b/sm.c
index 0c4bd87..b1c07c8 100644 (file)
--- a/sm.c
+++ b/sm.c
@@ -86,6 +86,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 +124,74 @@ 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_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);
 
        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 +204,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());