1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
| /******************************************************************************
* Copyright (C) 2008 by Tanner Jotblad <dreblen@users.sourceforge.net> *
* *
* GtkReflector is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by*
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* GtkReflector is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with GtkReflector. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#include <gtk/gtk.h>
#include "gtkreflector.h"
enum
{
PROP_0 = 0,
PROP_REFLECTION,
PROP_PARENT,
};
typedef struct _GtkReflectorPrivate
{
GtkWidget *reflection;
GtkWidget *parent;
guint idle_id;
} GtkReflectorPrivate;
#define GTK_REFLECTOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), GTK_TYPE_REFLECTOR, GtkReflectorPrivate))
G_DEFINE_TYPE(GtkReflector, gtk_reflector, GTK_TYPE_VBOX)
static gboolean gtk_reflector_reflection_expose_cb(GtkWidget *hbox, GdkEventExpose *ev, GtkWidget *refl);
static gboolean gtk_reflector_reflection_update_idle(gpointer user_data);
static void gtk_reflector_size_request_cb(GtkReflector *refl, GtkRequisition *req, gpointer user_data);
static void gtk_reflector_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
GtkReflectorPrivate *priv = GTK_REFLECTOR_GET_PRIVATE(object);
switch(prop_id)
{
case PROP_REFLECTION:
priv->reflection = g_value_get_object(value);
break;
case PROP_PARENT:
priv->parent = g_value_get_object(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void gtk_reflector_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
GtkReflectorPrivate *priv = GTK_REFLECTOR_GET_PRIVATE(object);
switch(prop_id)
{
case PROP_REFLECTION:
g_value_set_object(value, priv->reflection);
break;
case PROP_PARENT:
g_value_set_object(value, priv->parent);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static GObject *gtk_reflector_constructor(GType type, guint n_props, GObjectConstructParam *construct_props)
{
GObject *object;
GtkReflectorPrivate *priv;
object = G_OBJECT_CLASS(gtk_reflector_parent_class)->constructor(type, n_props, construct_props);
priv = GTK_REFLECTOR_GET_PRIVATE(object);
priv->reflection = gtk_drawing_area_new();
g_signal_connect(priv->reflection, "expose-event", G_CALLBACK(gtk_reflector_reflection_expose_cb), (gpointer)object);
gtk_box_pack_start(GTK_BOX(object), priv->reflection, TRUE, TRUE, 0);
gtk_widget_show(priv->reflection);
return object;
}
static void gtk_reflector_finalize(GObject *object)
{
GtkReflectorPrivate *priv;
priv = GTK_REFLECTOR_GET_PRIVATE(object);
g_source_remove(priv->idle_id);
}
static void gtk_reflector_class_init(GtkReflectorClass *klass)
{
GObjectClass *gobject_class;
gobject_class = G_OBJECT_CLASS(klass);
gobject_class->constructor = gtk_reflector_constructor;
gobject_class->set_property = gtk_reflector_set_property;
gobject_class->get_property = gtk_reflector_get_property;
gobject_class->finalize = gtk_reflector_finalize;
g_type_class_add_private(klass, sizeof(GtkReflectorPrivate));
g_object_class_install_property(gobject_class, PROP_REFLECTION, g_param_spec_object("reflection",
"Reflection",
"The widget holding the reflection",
GTK_TYPE_WIDGET,
G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_PARENT, g_param_spec_object("parent",
"Parent",
"The widget's toplevel parent",
GTK_TYPE_WINDOW,
G_PARAM_READWRITE));
}
static void gtk_reflector_init(GtkReflector *ob)
{
GtkReflectorPrivate *priv;
priv = GTK_REFLECTOR_GET_PRIVATE(ob);
g_signal_connect(ob, "size-request", G_CALLBACK(gtk_reflector_size_request_cb), NULL);
gtk_box_set_homogeneous(GTK_BOX(ob), FALSE);
priv->idle_id = g_idle_add(gtk_reflector_reflection_update_idle, (gpointer)ob);
}
GtkWidget *gtk_reflector_new(GtkWindow *parent)
{
g_return_val_if_fail(GTK_IS_WINDOW(parent), NULL);
return g_object_new(GTK_TYPE_REFLECTOR, "parent", parent, NULL);
}
static gboolean gtk_reflector_reflection_expose_cb(GtkWidget *wi, GdkEventExpose *ev, GtkWidget *refl)
{
GtkReflectorPrivate *priv;
GList *children;
GtkWidget *child;
GdkPixbuf *temp_pixbuf;
GdkPixbuf *child_pixbuf;
GdkPixbuf *blank_pixbuf;
gint len;
gint w, h;
gint x, y;
gint i;
gdouble alpha_step;
priv = GTK_REFLECTOR_GET_PRIVATE(refl);
children = gtk_container_get_children(GTK_CONTAINER(refl));
len = g_list_length(children);
if(len < 2)
return TRUE;
child = GTK_WIDGET(g_list_nth_data(children, len-2));
g_list_free(children);
w = child->allocation.width;
h = child->allocation.height;
x = child->allocation.x;
y = child->allocation.y;
child_pixbuf = gdk_pixbuf_get_from_drawable(NULL, child->window, NULL, x, y, 0, 0, w, h);
temp_pixbuf = child_pixbuf;
child_pixbuf = gdk_pixbuf_flip(child_pixbuf, FALSE);
g_object_unref(temp_pixbuf);
alpha_step = (gdouble)127/h;
blank_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, w, h);
gdk_pixbuf_fill(blank_pixbuf, 0x00000000);
for(i = 0; i<h; i++)
{
temp_pixbuf = blank_pixbuf;
gdk_pixbuf_composite(child_pixbuf, blank_pixbuf, 0, i, w, 1, 0, 0, 1, 1, GDK_INTERP_BILINEAR, (h-i)*alpha_step);
}
gdk_draw_pixbuf(priv->reflection->window, NULL, blank_pixbuf, 0, 0, 0, 0, w, h, GDK_RGB_DITHER_NONE, 0, 0);
g_object_unref(blank_pixbuf);
g_object_unref(child_pixbuf);
return TRUE;
}
static gboolean gtk_reflector_reflection_update_idle(gpointer user_data)
{
GtkReflectorPrivate *priv;
priv = GTK_REFLECTOR_GET_PRIVATE(user_data);
gdk_window_invalidate_rect(priv->reflection->window, NULL, FALSE);
return TRUE;
}
static void gtk_reflector_size_request_cb(GtkReflector *refl, GtkRequisition *req, gpointer user_data)
{
GtkReflectorPrivate *priv;
GList *children;
GtkWidget *size_ref;
gint pos;
gint len;
priv = GTK_REFLECTOR_GET_PRIVATE(refl);
children = gtk_container_get_children(GTK_CONTAINER(refl));
pos = g_list_index(children, (gpointer)priv->reflection);
len = g_list_length(children);
if(len == 1)
return;
if(pos != len-1)
gtk_box_reorder_child(GTK_BOX(refl), priv->reflection, -1);
size_ref = GTK_WIDGET(g_list_nth_data(children, len-2));
gtk_widget_set_size_request(priv->reflection, -1, size_ref->requisition.height);
g_list_free(children);
} |