summaryrefslogtreecommitdiff
path: root/shell/cc-shell-category-view.c
blob: edf87ef41fa82a2680e8d5a2ec02fd1b48be09a7 (plain)
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
/*
 * Copyright (c) 2010 Intel, Inc.
 *
 * The Control Center is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * The Control Center 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 General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with the Control Center; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author: Thomas Wood <thos@gnome.org>
 */

#include "cc-shell-category-view.h"
#include "cc-shell-item-view.h"
#include "cc-shell.h"
#include "cc-shell-model.h"

G_DEFINE_TYPE (CcShellCategoryView, cc_shell_category_view, GTK_TYPE_FRAME)

#define SHELL_CATEGORY_VIEW_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_SHELL_CATEGORY_VIEW, CcShellCategoryViewPrivate))

enum
{
  PROP_NAME = 1,
  PROP_MODEL
};

struct _CcShellCategoryViewPrivate
{
  gchar *name;
  GtkTreeModel *model;
  GtkWidget *iconview;
};

static void
cc_shell_category_view_get_property (GObject    *object,
                                     guint       property_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
  CcShellCategoryViewPrivate *priv = CC_SHELL_CATEGORY_VIEW (object)->priv;

  switch (property_id)
    {
    case PROP_NAME:
      g_value_set_string (value, priv->name);
      break;

    case PROP_MODEL:
      g_value_set_object (value, priv->model);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
cc_shell_category_view_set_property (GObject      *object,
                                     guint         property_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  CcShellCategoryViewPrivate *priv = CC_SHELL_CATEGORY_VIEW (object)->priv;

  switch (property_id)
    {
    case PROP_NAME:
      priv->name = g_value_dup_string (value);
      break;

    case PROP_MODEL:
      priv->model = g_value_dup_object (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
cc_shell_category_view_dispose (GObject *object)
{
  CcShellCategoryViewPrivate *priv = CC_SHELL_CATEGORY_VIEW (object)->priv;

  if (priv->model)
    {
      g_object_unref (priv->model);
      priv->model = NULL;
    }

  G_OBJECT_CLASS (cc_shell_category_view_parent_class)->dispose (object);
}

static void
cc_shell_category_view_finalize (GObject *object)
{
  CcShellCategoryViewPrivate *priv = CC_SHELL_CATEGORY_VIEW (object)->priv;

  if (priv->name)
    {
      g_free (priv->name);
      priv->name = NULL;
    }

  G_OBJECT_CLASS (cc_shell_category_view_parent_class)->finalize (object);
}

static void
cc_shell_category_view_constructed (GObject *object)
{
  CcShellCategoryViewPrivate *priv = CC_SHELL_CATEGORY_VIEW (object)->priv;
  GtkWidget *iconview, *vbox;
  GtkCellRenderer *renderer;

  iconview = cc_shell_item_view_new ();
  gtk_icon_view_set_model (GTK_ICON_VIEW (iconview), priv->model);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);

  renderer = gtk_cell_renderer_pixbuf_new ();
  g_object_set (renderer,
                "follow-state", TRUE,
                NULL);
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (iconview),
                              renderer, FALSE);
  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (iconview), renderer,
                                 "pixbuf", COL_PIXBUF);

  gtk_icon_view_set_text_column (GTK_ICON_VIEW (iconview), COL_NAME);
  gtk_icon_view_set_item_width (GTK_ICON_VIEW (iconview), 100);
  cc_shell_item_view_update_cells (CC_SHELL_ITEM_VIEW (iconview));

  /* create the header if required */
  if (priv->name)
    {
      GtkWidget *label;
      PangoAttrList *attrs;

      label = gtk_label_new (priv->name);
      attrs = pango_attr_list_new ();
      pango_attr_list_insert (attrs, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
      gtk_label_set_attributes (GTK_LABEL (label), attrs);
      pango_attr_list_unref (attrs);
      gtk_frame_set_label_widget (GTK_FRAME (object), label);
      gtk_widget_show (label);
    }

  /* add the iconview to the vbox */
  gtk_box_pack_start (GTK_BOX (vbox), iconview, FALSE, TRUE, 0);

  /* add the main vbox to the view */
  gtk_container_add (GTK_CONTAINER (object), vbox);
  gtk_widget_show_all (vbox);

  priv->iconview = iconview;
}

static void
cc_shell_category_view_class_init (CcShellCategoryViewClass *klass)
{
  GParamSpec *pspec;
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (CcShellCategoryViewPrivate));

  object_class->get_property = cc_shell_category_view_get_property;
  object_class->set_property = cc_shell_category_view_set_property;
  object_class->dispose = cc_shell_category_view_dispose;
  object_class->finalize = cc_shell_category_view_finalize;
  object_class->constructed = cc_shell_category_view_constructed;

  pspec = g_param_spec_string ("name",
                               "Name",
                               "Name of the category",
                               NULL,
                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
                               | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_NAME, pspec);

  pspec = g_param_spec_object ("model",
                               "Model",
                               "Model of the category",
                               GTK_TYPE_TREE_MODEL,
                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
                               | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_MODEL, pspec);

}

static void
cc_shell_category_view_init (CcShellCategoryView *self)
{
  self->priv = SHELL_CATEGORY_VIEW_PRIVATE (self);

  gtk_frame_set_shadow_type (GTK_FRAME (self), GTK_SHADOW_NONE);
}

GtkWidget *
cc_shell_category_view_new (const gchar  *name,
                            GtkTreeModel *model)
{
  return g_object_new (CC_TYPE_SHELL_CATEGORY_VIEW,
                       "name", name,
                       "model", model, NULL);
}

CcShellItemView*
cc_shell_category_view_get_item_view (CcShellCategoryView *self)
{
  return (CcShellItemView*) self->priv->iconview;
}