From 895555fd994394fd6df70fb43101cbe48fb9f1e5 Mon Sep 17 00:00:00 2001 From: Philip Allison Date: Wed, 3 Jun 2009 20:25:29 +0100 Subject: [PATCH] Set window shape in addition to input mask If we can't get a true RGBA visual, set the window's shape using the same bitmap as used for the input mask, so we can at least remove the horrid black border (even if we can't get proper blended edges). Also, set the window's icon from the displayed image. Signed-off-by: Philip Allison --- src/cteddy.cxx | 62 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/cteddy.cxx b/src/cteddy.cxx index ad02800..cac2cb5 100644 --- a/src/cteddy.cxx +++ b/src/cteddy.cxx @@ -53,14 +53,18 @@ // // Main window -GtkWidget* window; +GtkWidget* window = NULL; // GTK/GDK returned pointer GError* gerr = NULL; // Image to display and its dimensions -GdkPixbuf* pixbuf; -gint width, height; +GdkPixbuf* pixbuf = NULL; +gint width = 0, height = 0; + +// Bitmap generated by thresholding image's alpha channel +// Used for window input (and sometimes shape) mask +GdkBitmap* bitmap = NULL; // Whether or not the key binding for quit is disabled bool noquit = false; @@ -124,10 +128,18 @@ void handle_screen(GtkWidget* window, GdkScreen* old, gpointer data) GdkScreen* s = gtk_widget_get_screen(window); GdkColormap* c = gdk_screen_get_rgba_colormap(s); + // Clear window's shape mask + gtk_widget_shape_combine_mask(window, NULL, 0, 0); + if (!c) - // Use standard RGB colourmap if we must. Window will have - // an ugly black border if this is the case. + { + // Use standard RGB colourmap if we must. c = gdk_screen_get_rgb_colormap(s); + // Also, set window's shape mask so that even without + // a true RGBA window, we don't get an ugly black border + if (bitmap) + gtk_widget_shape_combine_mask(window, bitmap, 0, 0); + } gtk_widget_set_colormap(window, c); } @@ -556,33 +568,33 @@ int main (int argc, char* argv[]) g_signal_connect(G_OBJECT(window), "screen-changed", G_CALLBACK(handle_screen), NULL); g_signal_connect(G_OBJECT(window), "key-press-event", G_CALLBACK(handle_key_press), NULL); + // Grab pixmap and alpha-thresholded bitmap from original pixmap + GdkPixmap* pixmap; + gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128); + + // Set window size to image size + gdk_drawable_get_size(pixmap, &width, &height); + gtk_widget_set_size_request(window, width, height); + + // If we have an alpha channel on the image, set the window's input shape, + // clearing it completely first + if (bitmap) { - // Grap pixmap and alpha-thresholded bitmap from original pixmap - GdkPixmap* pixmap; - GdkBitmap* bitmap; - gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128); - - // Set window size to image size - gdk_drawable_get_size(pixmap, &width, &height); - gtk_widget_set_size_request(window, width, height); - - // If we have an alpha channel on the image, set the window's input shape, - // clearing it completely first - if (bitmap) - { - gtk_widget_input_shape_combine_mask(window, NULL, 0, 0); - gtk_widget_input_shape_combine_mask(window, bitmap, 0, 0); - } - - g_object_unref(G_OBJECT(pixmap)); - g_object_unref(G_OBJECT(bitmap)); + gtk_widget_input_shape_combine_mask(window, NULL, 0, 0); + gtk_widget_input_shape_combine_mask(window, bitmap, 0, 0); } + + // Deref the pixmap, but keep the bitmap around for now + // May be useful later for setting window's shape mask if we cannot get + // a true RGBA visual (i.e. compositing manager not running) + g_object_unref(G_OBJECT(pixmap)); // Set up various other properties we're interested in gtk_window_set_title(GTK_WINDOW(window), "cteddy"); gtk_window_set_resizable(GTK_WINDOW(window), false); gtk_window_set_decorated(GTK_WINDOW(window), false); gtk_widget_set_app_paintable(window, true); + gtk_window_set_icon(GTK_WINDOW(window), pixbuf); // Trigger initial attempt to switch to an RGBA colourmap handle_screen(window, NULL, NULL); @@ -660,7 +672,7 @@ int main (int argc, char* argv[]) else return 0; } - + gtk_main(); return 0; -- 2.11.4.GIT