summaryrefslogtreecommitdiff
path: root/endless/eosflexygridcell.c
blob: 4091f069cfb2e6380615058cd968c5eb7e5f13fc (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* Copyright 2013 Endless Mobile, Inc. */

#include "config.h"

#include "eosflexygrid-private.h"

#include <glib-object.h>
#include <gtk/gtk.h>

EOS_DEFINE_ENUM_TYPE (EosFlexyShape, eos_flexy_shape,
                      EOS_ENUM_VALUE (EOS_FLEXY_SHAPE_SMALL, small)
                      EOS_ENUM_VALUE (EOS_FLEXY_SHAPE_MEDIUM_HORIZONTAL, medium-horizontal)
                      EOS_ENUM_VALUE (EOS_FLEXY_SHAPE_MEDIUM_VERTICAL, medium-vertical)
                      EOS_ENUM_VALUE (EOS_FLEXY_SHAPE_LARGE, large))

typedef struct {
  EosFlexyShape shape;

  GSequenceIter *iter;

  guint selected : 1;
} EosFlexyGridCellPrivate;

#if GLIB_CHECK_VERSION (2, 37, 5)

# define EOS_FLEXY_GRID_CELL_GET_PRIV(obj) \
  ((EosFlexyGridCellPrivate *) eos_flexy_grid_cell_get_instance_private ((EosFlexyGridCell *) (obj)))

G_DEFINE_TYPE_WITH_PRIVATE (EosFlexyGridCell, eos_flexy_grid_cell, GTK_TYPE_BIN)

#else

# define EOS_FLEXY_GRID_CELL_GET_PRIV(obj) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EOS_TYPE_FLEXY_GRID_CELL, EosFlexyGridCellPrivate))

G_DEFINE_TYPE (EosFlexyGridCell, eos_flexy_grid_cell, GTK_TYPE_BIN)

#endif /* GLIB_CHECK_VERSION (2, 37, 5) */

enum
{
  PROP_0,

  PROP_SHAPE,

  PROP_LAST
};

static GParamSpec *obj_props[PROP_LAST] = { NULL, };

static void
eos_flexy_grid_cell_set_property (GObject      *gobject,
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  EosFlexyGridCell *self = EOS_FLEXY_GRID_CELL (gobject);

  switch (prop_id)
    {
    case PROP_SHAPE:
      eos_flexy_grid_cell_set_shape (self, g_value_get_enum (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
    }
}

static void
eos_flexy_grid_cell_get_property (GObject    *gobject,
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
  EosFlexyGridCell *self = EOS_FLEXY_GRID_CELL (gobject);
  EosFlexyGridCellPrivate *priv = self->priv;

  switch (prop_id)
    {
    case PROP_SHAPE:
      g_value_set_enum (value, priv->shape);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
    }
}

static void
eos_flexy_grid_cell_class_init (EosFlexyGridCellClass *klass)
{
#if !GLIB_CHECK_VERSION (2, 37, 6)
  g_type_class_add_private (klass, sizeof (EosFlexyGridCellPrivate));
#endif

  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->set_property = eos_flexy_grid_cell_set_property;
  gobject_class->get_property = eos_flexy_grid_cell_get_property;

  /**
   * EosFlexyGridCell:shape:
   *
   * The shape of the cell.
   */
  obj_props[PROP_SHAPE] =
    g_param_spec_enum ("shape",
                       "Shape",
                       "The shape of the cell",
                       EOS_TYPE_FLEXY_SHAPE,
                       EOS_FLEXY_SHAPE_SMALL,
                       G_PARAM_STATIC_STRINGS |
                       G_PARAM_READWRITE);

  g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
}

static void
eos_flexy_grid_cell_init (EosFlexyGridCell *self)
{
  EosFlexyGridCellPrivate *priv = EOS_FLEXY_GRID_CELL_GET_PRIV (self);

  priv->shape = EOS_FLEXY_SHAPE_SMALL;

  self->priv = priv;

  GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (self));
  gtk_style_context_add_class (context, EOS_STYLE_CLASS_FLEXY_GRID_CELL);
}

/**
 * eos_flexy_grid_cell_new:
 *
 * Creates a new #EosFlexyGridCell widget.
 *
 * Return value: (transfer full): the newly created #EosFlexyGridCell widget
 */
GtkWidget *
eos_flexy_grid_cell_new (void)
{
  return g_object_new (EOS_TYPE_FLEXY_GRID_CELL, NULL);
}

/**
 * eos_flexy_grid_cell_set_shape:
 * @cell: a #EosFlexyGridCell
 * @shape: the shape of the cell
 *
 * Sets the shape of the @cell. The @shape determines the amount of
 * space inside a #EosFlexyGrid that will be assigned to the @cell.
 */
void
eos_flexy_grid_cell_set_shape (EosFlexyGridCell *cell,
                               EosFlexyShape     shape)
{
  EosFlexyGridCellPrivate *priv;

  g_return_if_fail (EOS_IS_FLEXY_GRID_CELL (cell));

  priv = cell->priv;
  if (priv->shape != shape)
    {
      priv->shape = shape;

      g_object_notify_by_pspec (G_OBJECT (cell), obj_props[PROP_SHAPE]);

      gtk_widget_queue_resize (GTK_WIDGET (cell));
    }
}

/**
 * eos_flexy_grid_cell_get_shape:
 * @cell: a #EosFlexyGridCell
 *
 * Retrieves the shape of @cell.
 *
 * Return value: the shape of the #EosFlexyGridCell
 */
EosFlexyShape
eos_flexy_grid_cell_get_shape (EosFlexyGridCell *cell)
{
  EosFlexyGridCellPrivate *priv;

  g_return_val_if_fail (EOS_IS_FLEXY_GRID_CELL (cell), EOS_FLEXY_SHAPE_SMALL);

  priv = cell->priv;

  return priv->shape;
}

/*< private >
 * eos_flexy_grid_cell_set_iter:
 * @cell: a #EosFlexyGridCell
 * @iter: a #GSequenceIter
 *
 * The #GSequenceIter associated to @cell.
 */
void
eos_flexy_grid_cell_set_iter (EosFlexyGridCell *cell,
                              GSequenceIter    *iter)
{
  EosFlexyGridCellPrivate *priv = cell->priv;

  priv->iter = iter;
}

/*< private >
 * eos_flexy_grid_cell_get_iter:
 * @cell: a #EosFlexyGridCell
 *
 * Retrieves the #GSequenceIter associated to @cell.
 *
 * Return value: (transfer none): a #GSequenceIter
 */
GSequenceIter *
eos_flexy_grid_cell_get_iter (EosFlexyGridCell *cell)
{
  EosFlexyGridCellPrivate *priv = cell->priv;

  return priv->iter;
}

/*< private >
 * eos_flexy_grid_cell_set_selected:
 * @cell: a #EosFlexyGridCell
 * @selected: %TRUE to select the @cell, and %FALSE otherwise
 *
 * Sets whether the @cell should be selected or not.
 */
void
eos_flexy_grid_cell_set_selected (EosFlexyGridCell *cell,
                                  gboolean          selected)
{
  g_return_if_fail (EOS_IS_FLEXY_GRID_CELL (cell));

  EosFlexyGridCellPrivate *priv = cell->priv;

  selected = !!selected;
  if (priv->selected != selected)
    {
      priv->selected = selected;
    }
}

/**
 * eos_flexy_grid_cell_get_selected:
 * @cell: a #EosFlexyGridCell
 *
 * Checks whether @cell is selected.
 *
 * Return value: %TRUE if the #EosFlexyGridCell is selected,
 *   and %FALSE otherwise
 */
gboolean
eos_flexy_grid_cell_get_selected (EosFlexyGridCell *cell)
{
  g_return_val_if_fail (EOS_IS_FLEXY_GRID_CELL (cell), FALSE);

  EosFlexyGridCellPrivate *priv = cell->priv;

  return priv->selected;
}