r2228: Made 'Automatic' an icon size, rather than a separate option.
[rox-filer.git] / ROX-Filer / src / cell_icon.c
blob56ac0279f354cc3eed777e2b7c842fa8469c3932
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* cell_icon.c - a GtkCellRenderer used for the icons in details mode
24 * Based on gtkcellrendererpixbuf.c.
27 #include "config.h"
29 #include <gtk/gtk.h>
31 #include "global.h"
33 #include "cell_icon.h"
34 #include "display.h"
35 #include "diritem.h"
36 #include "pixmaps.h"
38 typedef struct _CellIcon CellIcon;
39 typedef struct _CellIconClass CellIconClass;
41 struct _CellIcon {
42 GtkCellRenderer parent;
44 DirItem *item;
45 GdkColor background;
46 gboolean selected;
49 struct _CellIconClass {
50 GtkCellRendererClass parent_class;
54 /* Static prototypes */
55 static void cell_icon_set_property(GObject *object, guint param_id,
56 const GValue *value, GParamSpec *pspec);
57 static void cell_icon_init (CellIcon *cell);
58 static void cell_icon_class_init (CellIconClass *class);
59 static void cell_icon_get_size (GtkCellRenderer *cell,
60 GtkWidget *widget,
61 GdkRectangle *rectangle,
62 gint *x_offset,
63 gint *y_offset,
64 gint *width,
65 gint *height);
66 static void cell_icon_render (GtkCellRenderer *cell,
67 GdkWindow *window,
68 GtkWidget *widget,
69 GdkRectangle *background_area,
70 GdkRectangle *cell_area,
71 GdkRectangle *expose_area,
72 guint flags);
73 static GtkType cell_icon_get_type(void);
75 enum {
76 PROP_ZERO,
77 PROP_ITEM,
78 PROP_BACKGROUND_GDK,
81 /****************************************************************
82 * EXTERNAL INTERFACE *
83 ****************************************************************/
85 GtkCellRenderer *cell_icon_new(void)
87 return GTK_CELL_RENDERER(g_object_new(cell_icon_get_type(), NULL));
90 /****************************************************************
91 * INTERNAL FUNCTIONS *
92 ****************************************************************/
95 static GtkType cell_icon_get_type(void)
97 static GtkType cell_icon_type = 0;
99 if (!cell_icon_type)
101 static const GTypeInfo cell_icon_info =
103 sizeof (CellIconClass),
104 NULL, /* base_init */
105 NULL, /* base_finalize */
106 (GClassInitFunc) cell_icon_class_init,
107 NULL, /* class_finalize */
108 NULL, /* class_data */
109 sizeof (CellIcon),
110 0, /* n_preallocs */
111 (GInstanceInitFunc) cell_icon_init,
114 cell_icon_type = g_type_register_static(GTK_TYPE_CELL_RENDERER,
115 "CellIcon",
116 &cell_icon_info, 0);
119 return cell_icon_type;
122 static void cell_icon_init(CellIcon *icon)
126 static void cell_icon_class_init(CellIconClass *class)
128 GObjectClass *object_class = G_OBJECT_CLASS(class);
129 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(class);
131 object_class->set_property = cell_icon_set_property;
133 cell_class->get_size = cell_icon_get_size;
134 cell_class->render = cell_icon_render;
136 g_object_class_install_property(object_class,
137 PROP_ITEM,
138 g_param_spec_pointer("item",
139 _("DirItem"),
140 _("The item to render."),
141 G_PARAM_WRITABLE));
143 g_object_class_install_property(object_class,
144 PROP_BACKGROUND_GDK,
145 g_param_spec_boxed("background_gdk",
146 _("Background color"),
147 _("Background color as a GdkColor"),
148 GDK_TYPE_COLOR,
149 G_PARAM_WRITABLE));
152 static void cell_icon_set_property(GObject *object, guint param_id,
153 const GValue *value, GParamSpec *pspec)
155 CellIcon *icon = (CellIcon *) object;
157 switch (param_id)
159 case PROP_ITEM:
160 icon->item = (DirItem *) g_value_get_pointer(value);
161 break;
162 case PROP_BACKGROUND_GDK:
164 GdkColor *bg = g_value_get_boxed(value);
165 icon->selected = bg != NULL;
166 if (bg)
168 icon->background.red = bg->red;
169 icon->background.green = bg->green;
170 icon->background.blue = bg->blue;
172 break;
174 default:
175 G_OBJECT_WARN_INVALID_PROPERTY_ID(object,
176 param_id, pspec);
177 break;
181 static void cell_icon_get_size(GtkCellRenderer *cell,
182 GtkWidget *widget,
183 GdkRectangle *cell_area,
184 gint *x_offset,
185 gint *y_offset,
186 gint *width,
187 gint *height)
189 if (x_offset)
190 *x_offset = 0;
191 if (y_offset)
192 *y_offset = 0;
193 if (width)
194 *width = SMALL_WIDTH;
195 if (height)
196 *height = SMALL_HEIGHT;
199 static void cell_icon_render(GtkCellRenderer *cell,
200 GdkWindow *window,
201 GtkWidget *widget,
202 GdkRectangle *background_area,
203 GdkRectangle *cell_area,
204 GdkRectangle *expose_area,
205 guint flags)
207 CellIcon *icon = (CellIcon *) cell;
209 g_return_if_fail(icon->item != NULL);
211 /* Drag the background */
213 if (icon->selected)
215 GdkColor color;
216 GdkGC *gc;
218 color.red = icon->background.red;
219 color.green = icon->background.green;
220 color.blue = icon->background.blue;
222 gc = gdk_gc_new(window);
224 gdk_gc_set_rgb_fg_color(gc, &color);
226 gdk_draw_rectangle(window, gc, TRUE,
227 background_area->x,
228 background_area->y,
229 background_area->width,
230 background_area->height);
232 g_object_unref(G_OBJECT(gc));
235 /* Draw the icon */
237 if (!icon->item->image)
238 return;
240 draw_small_icon(window, cell_area, icon->item, icon->item->image,
241 FALSE); /* XXX: selected */