source: trunk/gtk/gtkcellrenderertorrent.c @ 17

Last change on this file since 17 was 11, checked in by root, 16 years ago

Update 2005-12-04

File size: 9.6 KB
Line 
1/* gtkcellrenderertorrent.c
2 * Copyright (C) 2002 Naba Kumar <kh_naba@users.sourceforge.net>
3 * heavily modified by Jörgen Scheibengruber <mfcn@gmx.de>
4 * heavily modified by Marco Pesenti Gritti <marco@gnome.org>
5 * heavily modified by Josh Elsasser <josh@elsasser.org> for transmission
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22/*
23 * Modified by the GTK+ Team and others 1997-2004.  See the AUTHORS
24 * file for a list of people on the GTK+ Team.  See the ChangeLog
25 * files for a list of changes.  These files are distributed with
26 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 */
28
29#include <gtk/gtk.h>
30
31#include "gtkcellrenderertorrent.h"
32#include "util.h"
33
34enum { PROP_0, PROP_VALUE, PROP_TEXT, PROP_LABEL }; 
35
36struct _GtkCellRendererTorrentPrivate {
37  gfloat value;
38  gchar *text;
39  PangoAttrList *text_attrs;
40  gchar *label;
41  PangoAttrList *label_attrs;
42  GtkStyle *style;
43};
44
45static void
46finalize(GObject *object);
47static void
48get_property(GObject *obj, guint id, GValue *value, GParamSpec *spec);
49static void
50set_property(GObject *obj, guint id, const GValue *value, GParamSpec *spec);
51static void
52get_size(GtkCellRenderer *cell, GtkWidget *widget, GdkRectangle *area,
53         gint *xoff, gint *yoff, gint *width, gint *height);
54static void
55render(GtkCellRenderer *cell, GdkWindow *window, GtkWidget *widget,
56       GdkRectangle *bg, GdkRectangle *area, GdkRectangle *exp, guint flags);
57
58     
59G_DEFINE_TYPE(GtkCellRendererTorrent, gtk_cell_renderer_torrent, GTK_TYPE_CELL_RENDERER);
60
61static void
62gtk_cell_renderer_torrent_class_init (GtkCellRendererTorrentClass *klass) {
63  GObjectClass *object_class = G_OBJECT_CLASS (klass);
64  GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
65 
66  object_class->finalize = finalize;
67  object_class->get_property = get_property;
68  object_class->set_property = set_property;
69 
70  cell_class->get_size = get_size;
71  cell_class->render = render;
72 
73  g_object_class_install_property(
74    object_class, PROP_VALUE,
75    g_param_spec_float("value", "Value", "Value of the torrent bar",
76                       0.0, 1.0, 0.0, G_PARAM_READWRITE));
77
78  g_object_class_install_property(
79    object_class, PROP_TEXT,
80    g_param_spec_string ("text", "Text", "Text under the torrent bar",
81  /* XXX should I have NULL or "" here, and is initial strdup needed? */
82                         NULL, G_PARAM_READWRITE));
83
84  g_object_class_install_property(
85    object_class, PROP_LABEL,
86    g_param_spec_string ("label", "Label", "Text on the torrent bar",
87                         NULL, G_PARAM_READWRITE));
88
89  g_type_class_add_private (object_class, 
90                            sizeof (GtkCellRendererTorrentPrivate));
91}
92
93static void
94gtk_cell_renderer_torrent_init(GtkCellRendererTorrent *tcell) {
95  tcell->priv = G_TYPE_INSTANCE_GET_PRIVATE(
96    tcell, GTK_TYPE_CELL_RENDERER_TORRENT, GtkCellRendererTorrentPrivate);
97  tcell->priv->value = 0.0;
98  tcell->priv->text = g_strdup("");
99  tcell->priv->text_attrs = NULL;
100  tcell->priv->label = g_strdup("");
101  tcell->priv->text_attrs = NULL;
102  tcell->priv->style = NULL;
103}
104
105GtkCellRenderer*
106gtk_cell_renderer_torrent_new(void) {
107  return g_object_new (GTK_TYPE_CELL_RENDERER_TORRENT, NULL);
108}
109
110static void
111finalize(GObject *object) {
112  GtkCellRendererTorrent *tcell = GTK_CELL_RENDERER_TORRENT(object);
113
114  g_free(tcell->priv->text);
115  g_free(tcell->priv->label);
116
117  if(NULL != tcell->priv->text_attrs)
118    pango_attr_list_unref(tcell->priv->text_attrs);
119  if(NULL != tcell->priv->label_attrs)
120    pango_attr_list_unref(tcell->priv->label_attrs);
121  if(NULL != tcell->priv->style) {
122    gtk_style_detach(tcell->priv->style);
123    gtk_style_unref(tcell->priv->style);
124  }
125
126  G_OBJECT_CLASS (gtk_cell_renderer_torrent_parent_class)->finalize(object);
127}
128
129static void
130get_property(GObject *object, guint id, GValue *value, GParamSpec *pspec) {
131  GtkCellRendererTorrent *tcell = GTK_CELL_RENDERER_TORRENT (object);
132 
133  switch (id) {
134    case PROP_VALUE:
135      g_value_set_float (value, tcell->priv->value);
136      break;
137    case PROP_TEXT:
138      g_value_set_string (value, tcell->priv->text);
139      break;
140    case PROP_LABEL:
141      g_value_set_string (value, tcell->priv->label);
142      break;
143    default:
144      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, id, pspec);
145  }
146}
147
148static void
149set_property(GObject *obj, guint id, const GValue *value, GParamSpec *spec) {
150  GtkCellRendererTorrent *tcell = GTK_CELL_RENDERER_TORRENT(obj);
151  gchar **prop = NULL; 
152  PangoAttrList **attrs = NULL;
153  /*GError *err = NULL;*/
154  const gchar *markup;
155 
156  switch(id) {
157    case PROP_VALUE:
158      tcell->priv->value = g_value_get_float(value);
159      break;
160
161    case PROP_TEXT:
162      prop = &tcell->priv->text;
163      attrs = &tcell->priv->text_attrs;
164      /* fallthrough */
165
166    case PROP_LABEL:
167      if(PROP_LABEL == id) {
168        prop = &tcell->priv->label;
169        attrs = &tcell->priv->label_attrs;
170      }
171
172      if(NULL == (markup = g_value_get_string(value)))
173        markup = "";
174
175      g_free(*prop);
176      if(NULL != *attrs)
177        pango_attr_list_unref(*attrs);
178
179      *prop = g_strdup(markup);
180
181      /*
182      if(pango_parse_markup(markup, -1, 0, attrs, prop, NULL, &err))
183        break;
184       
185      g_warning ("Failed to parse markup: %s", err->message);
186      g_error_free(err);
187      */
188      break;
189
190    default:
191      G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, id, spec);
192  }
193}
194
195static void
196get_size(GtkCellRenderer *cell, GtkWidget *widget, GdkRectangle *area,
197         gint *xoff, gint *yoff, gint *width, gint *height) {
198  GtkCellRendererTorrent *tcell = GTK_CELL_RENDERER_TORRENT(cell);
199  /* XXX do I have to unref the context? */
200  PangoLayout *layout = pango_layout_new(gtk_widget_get_pango_context(widget));
201  PangoRectangle rect;
202  gint h = cell->ypad * 2;
203  gint w1, w2;
204
205  pango_layout_set_markup(layout, tcell->priv->label, -1);
206  pango_layout_get_pixel_extents(layout, NULL, &rect);
207  w1 = rect.width;
208  h += rect.height;
209
210  pango_layout_set_markup(layout, tcell->priv->text, -1);
211  pango_layout_get_pixel_extents(layout, NULL, &rect);
212  w2 = rect.width;
213  h += rect.height;
214
215  if(NULL != xoff)
216    *xoff = 0;
217  if(NULL != yoff)
218    *yoff = (area->height - h) / 2;
219  if(NULL != width)
220    *width = MAX(w1, w2) + cell->xpad * 2;
221  if(NULL != height)
222    *height = h;
223
224  g_object_unref(layout);
225}
226
227#define RECTARGS(rect)          (rect).x, (rect).y, (rect).width, (rect).height
228
229static void
230render(GtkCellRenderer *cell, GdkWindow *window, GtkWidget *widget,
231       GdkRectangle *bg SHUTUP, GdkRectangle *area, GdkRectangle *exp SHUTUP,
232       guint flags) {
233  GtkCellRendererTorrent *tcell = GTK_CELL_RENDERER_TORRENT(cell);
234  PangoContext *ctx = gtk_widget_get_pango_context(widget);
235  PangoLayout *llayout, *tlayout;
236  PangoRectangle lrect, trect;
237  GdkRectangle bar, complete, text;
238  gboolean rtl;
239  GtkStyle *style;
240
241  /* try to use the style for GtkProgressBar */
242  if(NULL == tcell->priv->style)
243    if(NULL != (tcell->priv->style = gtk_rc_get_style_by_paths(
244                  gtk_widget_get_settings(widget), NULL, NULL,
245                  gtk_progress_bar_get_type())))
246      tcell->priv->style = gtk_style_attach(gtk_style_ref(tcell->priv->style),
247                                            window);
248  style = (NULL == tcell->priv->style ? widget->style : tcell->priv->style);
249
250  rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
251
252  /* get the text layouts */
253  llayout = pango_layout_new(ctx);
254  /* XXX cache parsed markup? */
255  pango_layout_set_markup(llayout, tcell->priv->label, -1);
256  pango_layout_get_pixel_extents(llayout, NULL, &lrect);
257  tlayout = pango_layout_new(ctx);
258  pango_layout_set_markup(tlayout, tcell->priv->text, -1);
259  pango_layout_get_pixel_extents (tlayout, NULL, &trect);
260
261  /* set up the dimensions for the bar */
262  bar.x = area->x + cell->xpad;
263  bar.y = area->y + cell->ypad +
264    (area->height - lrect.height - trect.height) / 2;
265  bar.width = area->width - cell->xpad * 2;
266  bar.height = lrect.height;
267
268  /* set up the dimensions for the complete portion of the bar */
269  complete.x = bar.x + style->xthickness;
270  complete.y = bar.y + style->ythickness;
271  complete.width = (bar.width - style->xthickness * 2) * tcell->priv->value;
272  complete.height = bar.height - style->ythickness * 2;
273  if(rtl)
274    complete.x += bar.width - complete.width;
275
276  /* set up the dimensions for the text under the bar */
277  text.x = bar.x;
278  text.y = bar.y + bar.height;
279  text.width = bar.width;
280  text.height = area->height - bar.height;
281
282  /* draw the background of the bar */
283  if(complete.width < bar.width)
284    gtk_paint_box(style, window, GTK_STATE_NORMAL, GTK_SHADOW_IN, 
285                  NULL, widget, "trough", RECTARGS(bar));
286
287  /* draw the complete portion of the bar */
288  if(0 < complete.width)
289    gtk_paint_box(style, window, GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
290                  NULL, widget, "bar", RECTARGS(complete));
291
292  /* draw the text under the bar */
293  gtk_paint_layout(style, window, (GTK_CELL_RENDERER_SELECTED & flags ?
294                   GTK_STATE_SELECTED : GTK_STATE_NORMAL), FALSE, &text,
295                   widget, "cellrenderertext", text.x, text.y, tlayout);
296
297  /* free memory */
298  g_object_unref(llayout);
299  g_object_unref(tlayout);
300}
Note: See TracBrowser for help on using the repository browser.