Use icon
[darcs-mirror-screen-message.debian.git] / sm.c
diff --git a/sm.c b/sm.c
index afcd19c..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,10 +126,18 @@ 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);
 
@@ -127,10 +151,12 @@ int main(int argc, char **argv) {
        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;
@@ -140,12 +166,38 @@ int main(int argc, char **argv) {
        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);
@@ -184,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;
 }