TAG 0.24
[darcs-mirror-screen-message.debian.git] / sm.c
diff --git a/sm.c b/sm.c
index f1ff023..0adc28f 100644 (file)
--- a/sm.c
+++ b/sm.c
@@ -1,11 +1,6 @@
 /*
 #     sm.c
-#     Copyright (C) 2006 - 2012 Joachim Breitner
-#
-#     The Code for making a window fullscreen was taken from src/fullscreen.c in
-#     the geeqie package:
-#     Copyright (C) 2004 John Ellis
-#                   2008 - 2010 The Geeqie Team
+#     Copyright (C) 2006 - 2014 Joachim Breitner
 #
 #     This program is free software; you can redistribute it and/or modify
 #     it under the terms of the GNU General Public License as published by
@@ -25,6 +20,7 @@
 #include <gtk/gtk.h>
 #include <gdk/gdk.h>
 #include <pango/pango.h>
+#include <cairo.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -41,8 +37,6 @@
 
 #define AUTOHIDE_TIMEOUT 3
 
-static gboolean quality = TRUE;
-
 static int timeout_id=0;
 
 static GtkWidget* window;
@@ -51,12 +45,11 @@ static GdkCursor *cursor;
 static GtkWidget* quit;
 static GtkWidget* tv;
 static GtkWidget* entry_widget;
-static GtkSettings* settings;
 static GtkTextBuffer* tb;
 static PangoFontDescription *font;
-static PangoLayout* layout;
 static char *foreground = NULL;
 static char *background = NULL;
+static GdkRGBA  white, black;
 static char *fontdesc = NULL;
 static int rotation = 0; // 0 = normal, 1 = left, 2 = inverted, 3 = right
 static int alignment = 0; // 0 = centered, 1 = left-aligned, 2 = right-aligned
@@ -64,10 +57,11 @@ static GString *partial_input;
 static gulong text_change_handler;
 
 gboolean hide_entry(gpointer *user_data) {
+       timeout_id = 0;
        gtk_widget_hide(entry_widget);
        gtk_widget_grab_focus(draw);
        gtk_widget_queue_draw(draw);
-       gdk_window_set_cursor(GTK_WIDGET(draw)->window, cursor);
+       gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(draw)), cursor);
        return FALSE;
 }
 
@@ -78,7 +72,7 @@ static void show_entry() {
        }
        gtk_widget_show(entry_widget);
        gtk_widget_grab_focus(tv);
-       gdk_window_set_cursor(GTK_WIDGET(draw)->window, NULL);
+       gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(draw)), NULL);
 
        timeout_id = g_timeout_add_seconds (AUTOHIDE_TIMEOUT, (GSourceFunc)hide_entry, NULL);
 }
@@ -99,28 +93,42 @@ static char *get_text() {
        return gtk_text_buffer_get_text(tb, &start, &end, FALSE);
 }
 
-static void hq(gboolean q, gboolean force){
-       if (q != quality) 
-               if (q)
-                       gtk_settings_set_long_property(settings,"gtk-xft-antialias",1,"Hier halt");
-               else
-                       gtk_settings_set_long_property(settings,"gtk-xft-antialias",0,"Hier halt");
-       else
-               if (force)
-                       gtk_widget_queue_draw(draw);
 
-       quality = q;
-}
+static void redraw(GtkWidget *draw, cairo_t *cr, gpointer data) {
+       int q;
 
-static void redraw() {
-       const char *text = pango_layout_get_text(layout);
+       gdk_cairo_set_source_rgba(cr, &white);
+       cairo_paint(cr);
+
+       const char *text = get_text();
        if (strlen(text) > 0) {
-               GdkGC *gc = gtk_widget_get_style(draw)->fg_gc[GTK_STATE_NORMAL];
                int w1, h1;
+               static PangoLayout* layout;
+
+               layout = gtk_widget_create_pango_layout(draw, get_text());
+               pango_layout_set_font_description(layout, font);
+
+               switch(alignment){
+                       case 0: // center
+                               pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
+                               break;
+                       case 1: // left
+                               pango_layout_set_alignment(layout,PANGO_ALIGN_LEFT);
+                               break;
+                       case 2: // left
+                               pango_layout_set_alignment(layout,PANGO_ALIGN_RIGHT);
+                               break;
+                       default:
+                               // we propably don't want to annoy the user, so default to
+                               // the old default-behaviour:
+                               pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
+               }
+
+
                pango_layout_get_pixel_size(layout, &w1, &h1);
                if (w1>0 && h1>0) {
-                       int w2 = window->allocation.width;
-                       int h2 = window->allocation.height;
+                       int w2 = gtk_widget_get_allocated_width(draw);
+                       int h2 = gtk_widget_get_allocated_height(draw);
 
                        int rw1, rh1;
                        if (rotation == 0 || rotation == 2) {
@@ -132,33 +140,21 @@ static void redraw() {
                        }
 
                        double s = min ((double)w2/rw1, (double)h2/rh1);
-                       
-
-                       PangoMatrix matrix = PANGO_MATRIX_INIT;
-                       // Move matrix to the screen center
-                       pango_matrix_translate(&matrix, w2/2, h2/2);
-                       // Scale as required
-                       pango_matrix_scale(&matrix, s, s);
-                       // Rotate if needed
-                       pango_matrix_rotate (&matrix, rotation * 90);
-                       // Move matrix so that text will be centered
-                       //pango_matrix_translate(&matrix, -w1/2, -h1/2);
-                       
-                       // Apply matrix
-                       PangoContext *context =  pango_layout_get_context(layout);
-                       pango_context_set_matrix (context, &matrix);
-                       pango_layout_context_changed (layout);
-
-                       gdk_draw_layout(draw->window, gc,
-                               (w2-(s*rw1))/2, (h2-(s*rh1))/2,layout);
-                       
-                       // Reset matrix
-                       PangoMatrix matrix2 = PANGO_MATRIX_INIT;
-                       pango_context_set_matrix (context, &matrix2);
-                       pango_layout_context_changed (layout);
-
-                       hq(TRUE, FALSE);
+
+                       if (alignment == 1) { // left align
+                               cairo_translate(cr, (s * rw1)/2, h2/2);
+                       } else if (alignment == 2) { // right align
+                               cairo_translate(cr, w2 - (s * rw1)/2, h2/2);
+                       } else {
+                               cairo_translate(cr, w2/2, h2/2);
+                       }
+                       cairo_rotate(cr, rotation * M_PI_2);
+                       cairo_scale(cr, s, s);
+                       cairo_translate(cr, -w1/2, -h1/2);
+                       gdk_cairo_set_source_rgba(cr, &black);
+                       pango_cairo_show_layout (cr, layout);
                }
+               g_object_unref(layout);
        }
 }
 
@@ -247,8 +243,7 @@ static gboolean read_chan(GIOChannel *chan, GIOCondition condition, gpointer dat
 }
 
 static void newtext() {
-       pango_layout_set_text(layout, get_text(), -1);
-       hq(FALSE, TRUE);
+       gtk_widget_queue_draw(draw);
 }
 
 static void newtext_show_input() {
@@ -279,6 +274,13 @@ static void version() {
        printf("%s\n", PACKAGE_STRING);
 }
 
+gboolean my_gdk_rgba_parse (GdkRGBA *rgba, const gchar *spec) {
+       gboolean ret = gdk_rgba_parse(rgba, spec);
+       if (!ret)  {
+           fprintf (stderr, "Failed to parse color specification %s\n", spec);
+       }
+}
+
 int main(int argc, char **argv) {
        GString *input;
        int c;
@@ -325,62 +327,30 @@ int main(int argc, char **argv) {
        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));
-       int w = gdk_screen_get_width(screen);
-       int h = gdk_screen_get_height(screen);
-
-       GdkGeometry geometry;
-       geometry.min_width = w;
-       geometry.min_height = h;
-       geometry.max_width = w;
-       geometry.max_height = h;
-       geometry.base_width = w;
-       geometry.base_height = h;
-       geometry.win_gravity = GDK_GRAVITY_STATIC;
-       gtk_window_set_geometry_hints(GTK_WINDOW(window), window, &geometry,
-                                     GDK_HINT_MIN_SIZE |
-                                     GDK_HINT_MAX_SIZE |
-                                     GDK_HINT_BASE_SIZE |
-                                     GDK_HINT_WIN_GRAVITY |
-                                     GDK_HINT_USER_POS);
-       gtk_window_set_default_size(GTK_WINDOW(window), w, h);
-       gtk_window_move(GTK_WINDOW(window), 0, 0);
-
+       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, black;
-       gdk_colormap_alloc_color(colormap, &white, TRUE, TRUE);
-       gdk_colormap_alloc_color(colormap, &black, TRUE, TRUE);
        if (foreground != NULL) {
-               gdk_color_parse(foreground, &black);
+               my_gdk_rgba_parse(&black, foreground);
        } else {
-               gdk_color_parse("black", &black);
+               my_gdk_rgba_parse(&black, "black");
        }
        if (background != NULL) {
-               gdk_color_parse(background, &white);
+               my_gdk_rgba_parse(&white, background);
        } else {
-               gdk_color_parse("white", &white);
+               my_gdk_rgba_parse(&white, "white");
        }
 
        draw = gtk_drawing_area_new();
        gtk_widget_set_events(draw, GDK_BUTTON_PRESS_MASK|GDK_KEY_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), "button-press-event", G_CALLBACK(text_clicked), NULL);
        g_signal_connect(G_OBJECT(draw), "key-press-event", G_CALLBACK(text_keypress), NULL);
        gtk_widget_set_can_focus(draw, TRUE);
 
-       GdkPixmap *pixmap = gdk_pixmap_new(NULL, 1, 1, 1);
-       GdkColor color;
-       cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 0, 0);
+       cursor = gdk_cursor_new_for_display(gtk_widget_get_display(GTK_WIDGET(draw)), GDK_BLANK_CURSOR);
 
        tv = gtk_text_view_new();
        tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv));
@@ -419,18 +389,18 @@ int main(int argc, char **argv) {
        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);
+       quit = gtk_button_new_from_icon_name("application-exit", GTK_ICON_SIZE_BUTTON);
        g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(gtk_main_quit), NULL);
 
-       GtkWidget *vbox_button = gtk_vbox_new(FALSE, 0);
+       GtkWidget *vbox_button = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
        gtk_box_pack_end(GTK_BOX(vbox_button), quit, FALSE, FALSE, 0);
 
-       GtkWidget *hbox = gtk_hbox_new(FALSE,0);
+       GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,0);
        entry_widget = hbox;
        gtk_box_pack_start(GTK_BOX(hbox), tv,   TRUE,  TRUE,  0);
        gtk_box_pack_start(GTK_BOX(hbox), vbox_button, FALSE, FALSE, 0);
 
-       GtkWidget *vbox = gtk_vbox_new(FALSE,0);
+       GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,0);
        gtk_box_pack_start(GTK_BOX(vbox), draw, TRUE, TRUE, 0);
        gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
 
@@ -444,25 +414,6 @@ int main(int argc, char **argv) {
        }
        pango_font_description_set_size(font, 200*PANGO_SCALE);
 
-       layout = gtk_widget_create_pango_layout(window,get_text());
-       pango_layout_set_font_description(layout, font);
-
-       switch(alignment){
-               case 0: // center
-                       pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
-                       break;
-               case 1: // left
-                       pango_layout_set_alignment(layout,PANGO_ALIGN_LEFT);
-                       break;
-               case 2: // left
-                       pango_layout_set_alignment(layout,PANGO_ALIGN_RIGHT);
-                       break;
-               default:
-                       // we propably don't want to annoy the user, so default to
-                       // the old default-behaviour:
-                       pango_layout_set_alignment(layout,PANGO_ALIGN_CENTER);
-       }
-
        GtkAccelGroup *accel = gtk_accel_group_new();
        guint key;
        GdkModifierType mod;
@@ -473,15 +424,15 @@ int main(int argc, char **argv) {
        gtk_window_add_accel_group(GTK_WINDOW(window), accel);
        gtk_widget_show_all(window);
 
-       g_signal_connect_after(G_OBJECT(draw), "expose-event", G_CALLBACK(redraw), NULL);
+       g_signal_connect_after(G_OBJECT(draw), "draw", G_CALLBACK(redraw), NULL);
        text_change_handler = g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext_show_input), NULL);
        g_signal_connect(G_OBJECT(tb), "changed", G_CALLBACK(newtext), NULL);
        g_signal_connect(G_OBJECT(tv), "move-cursor", G_CALLBACK(move_cursor), NULL);
 
-       if (!input_provided)
-               show_entry();
-       else
+       if (input_provided)
                hide_entry(NULL);
+       else
+               show_entry();
 
        gtk_main();