glade-cell-renderer-icon.c 7.41 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2008 Tristan Van Berkom.
 *
4
 * This library is free software; you can redistribute it and/or modify it
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Tristan Van Berkom <tvb@gnome.org>
 */

#include "config.h"
#include "glade-cell-renderer-icon.h"
#include "glade-marshallers.h"

26 27 28 29 30 31 32 33 34 35 36 37 38 39
static void glade_cell_renderer_icon_get_property (GObject      *object,
                                                   guint         param_id,
                                                   GValue       *value,
                                                   GParamSpec   *pspec);
static void glade_cell_renderer_icon_set_property (GObject      *object,
                                                   guint         param_id,
                                                   const GValue *value,
                                                   GParamSpec   *pspec);
static gboolean glade_cell_renderer_icon_activate (GtkCellRenderer     *cell,
                                                   GdkEvent            *event,
                                                   GtkWidget           *widget,
                                                   const gchar         *path,
                                                   const GdkRectangle  *background_area,
                                                   const GdkRectangle  *cell_area,
40 41
                                                   GtkCellRendererState flags);

42 43 44 45 46 47
struct _GladeCellRendererIconPrivate 
{
  guint active : 1;
  guint activatable : 1;
};

48 49
enum
{
50 51 52 53
  ACTIVATE,
  LAST_SIGNAL
};

54 55
enum
{
56 57 58
  PROP_0,
  PROP_ACTIVATABLE,
  PROP_ACTIVE,
59
  N_PROPERTIES
60 61
};

62
static GParamSpec *properties[N_PROPERTIES];
63 64 65
static guint icon_cell_signals[LAST_SIGNAL] = { 0 };


66 67 68
G_DEFINE_TYPE_WITH_PRIVATE (GladeCellRendererIcon,
                            glade_cell_renderer_icon,
                            GTK_TYPE_CELL_RENDERER_PIXBUF)
69 70

static void glade_cell_renderer_icon_init (GladeCellRendererIcon *cellicon)
71
{
72
  cellicon->priv = glade_cell_renderer_icon_get_instance_private (cellicon);
73 74 75

  cellicon->priv->activatable = TRUE;
  cellicon->priv->active = FALSE;
76

77 78
  g_object_set (G_OBJECT (cellicon), "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
                NULL);
79 80 81
}

static void
82
glade_cell_renderer_icon_class_init (GladeCellRendererIconClass *class)
83 84 85 86 87 88 89 90
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);

  object_class->get_property = glade_cell_renderer_icon_get_property;
  object_class->set_property = glade_cell_renderer_icon_set_property;

  cell_class->activate = glade_cell_renderer_icon_activate;
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  properties[PROP_ACTIVE] =
    g_param_spec_boolean ("active", "Icon state",
                          "The icon state of the button",
                          FALSE,
                          G_PARAM_READABLE | G_PARAM_WRITABLE);

  properties[PROP_ACTIVATABLE] =
    g_param_spec_boolean ("activatable",
                          "Activatable",
                          "The icon button can be activated",
                          TRUE,
                          G_PARAM_READABLE | G_PARAM_WRITABLE);

  /* Install all properties */
  g_object_class_install_properties (object_class, N_PROPERTIES, properties);
107

108
  icon_cell_signals[ACTIVATE] =
109 110 111 112 113
      g_signal_new ("activate",
                    G_OBJECT_CLASS_TYPE (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeCellRendererIconClass, activate),
                    NULL, NULL,
114
                    _glade_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
115 116 117
}

static void
118 119 120 121
glade_cell_renderer_icon_get_property (GObject    *object,
                                       guint       param_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
122 123
{
  GladeCellRendererIcon *cellicon = GLADE_CELL_RENDERER_ICON (object);
124

125 126
  switch (param_id)
    {
127
      case PROP_ACTIVE:
128
        g_value_set_boolean (value, cellicon->priv->active);
129 130
        break;
      case PROP_ACTIVATABLE:
131
        g_value_set_boolean (value, cellicon->priv->activatable);
132 133 134 135
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
136 137 138 139 140
    }
}


static void
141 142 143 144
glade_cell_renderer_icon_set_property (GObject      *object,
                                       guint         param_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
145 146 147 148 149
{
  GladeCellRendererIcon *cellicon = GLADE_CELL_RENDERER_ICON (object);

  switch (param_id)
    {
150
      case PROP_ACTIVE:
151
        cellicon->priv->active = g_value_get_boolean (value);
152 153
        break;
      case PROP_ACTIVATABLE:
154
        cellicon->priv->activatable = g_value_get_boolean (value);
155 156 157 158
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
159 160 161 162 163 164 165 166 167 168
    }
}

GtkCellRenderer *
glade_cell_renderer_icon_new (void)
{
  return g_object_new (GLADE_TYPE_CELL_RENDERER_ICON, NULL);
}

static gint
169 170 171 172 173 174
glade_cell_renderer_icon_activate (GtkCellRenderer     *cell,
                                   GdkEvent            *event,
                                   GtkWidget           *widget,
                                   const gchar         *path,
                                   const GdkRectangle  *background_area,
                                   const GdkRectangle  *cell_area,
175
                                   GtkCellRendererState flags)
176 177
{
  GladeCellRendererIcon *cellicon;
178

179
  cellicon = GLADE_CELL_RENDERER_ICON (cell);
180
  if (cellicon->priv->activatable)
181 182 183 184 185 186 187 188 189
    {
      g_signal_emit (cell, icon_cell_signals[ACTIVATE], 0, path);
      return TRUE;
    }

  return FALSE;
}

gboolean
190
glade_cell_renderer_icon_get_active (GladeCellRendererIcon *icon)
191 192 193
{
  g_return_val_if_fail (GLADE_IS_CELL_RENDERER_ICON (icon), FALSE);

194
  return icon->priv->active;
195 196 197
}

void
198 199
glade_cell_renderer_icon_set_active (GladeCellRendererIcon *icon,
                                     gboolean               setting)
200 201 202
{
  g_return_if_fail (GLADE_IS_CELL_RENDERER_ICON (icon));

203 204 205
  if (icon->priv->active != setting)
    {
      icon->priv->active = setting ? TRUE : FALSE;
206
      g_object_notify_by_pspec (G_OBJECT (icon), properties[PROP_ACTIVE]);
207
    }
208 209 210
}

gboolean
211
glade_cell_renderer_icon_get_activatable (GladeCellRendererIcon *icon)
212 213 214
{
  g_return_val_if_fail (GLADE_IS_CELL_RENDERER_ICON (icon), FALSE);

215
  return icon->priv->activatable;
216 217 218
}

void
219 220
glade_cell_renderer_icon_set_activatable (GladeCellRendererIcon *icon,
                                          gboolean               setting)
221 222 223
{
  g_return_if_fail (GLADE_IS_CELL_RENDERER_ICON (icon));

224
  if (icon->priv->activatable != setting)
225
    {
226
      icon->priv->activatable = setting ? TRUE : FALSE;
227
      g_object_notify_by_pspec (G_OBJECT (icon), properties[PROP_ACTIVATABLE]);
228 229
    }
}