File: mx-gtk-light-switch.c

package info (click to toggle)
mx 1.4.6-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,628 kB
  • sloc: ansic: 46,358; sh: 11,012; makefile: 645; xml: 13
file content (438 lines) | stat: -rw-r--r-- 13,137 bytes parent folder | download | duplicates (3)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
 * Copyright 2009 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
 * Boston, MA 02111-1307, USA.
 *
 */

/**
 * SECTION:mx-gtk-light-switch
 * @short_description: a toggle switch between two states
 *
 * A visual representation of a toggle switch that can move between two states.
 */

#include <config.h>
#include "mx-gtk-light-switch.h"

#include <glib/gi18n-lib.h>

/* We use the special gcc constructor attribute so we can avoid
 * requiring an init function to get translations to work! This
 * function is also in mx-utils but we also need it here
 * because that is a separate library */
static void __attribute__ ((constructor))
_start (void)
{
  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
}

G_DEFINE_TYPE (MxGtkLightSwitch, mx_gtk_light_switch, GTK_TYPE_DRAWING_AREA)

#define MX_GTK_LIGHT_SWITCH_GET_PRIVATE(o)                                 \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), MX_GTK_TYPE_LIGHT_SWITCH, MxGtkLightSwitchPrivate))

static gboolean mx_gtk_light_switch_configure (GtkWidget         *lightswitch,
                                               GdkEventConfigure *event);
static gboolean mx_gtk_light_switch_expose (GtkWidget      *lightswitch,
                                            GdkEventExpose *event);
static gboolean mx_gtk_light_switch_button_release (GtkWidget      *lightswitch,
                                                    GdkEventButton *event);
static gboolean mx_gtk_light_switch_button_press (GtkWidget      *lightswitch,
                                                  GdkEventButton *event);
static gboolean mx_gtk_light_switch_motion_notify (GtkWidget      *lightswitch,
                                                   GdkEventMotion *event);
static void mx_gtk_light_switch_size_request (GtkWidget      *lightswitch,
                                              GtkRequisition *req);

static void mx_gtk_light_switch_style_set (GtkWidget *lightswitch,
                                           GtkStyle  *previous_style);

enum {
  SWITCH_FLIPPED,
  LAST_SIGNAL
};

static guint mx_gtk_light_switch_signals[LAST_SIGNAL] = { 0 };

enum {
  PROP_0,
  PROP_ACTIVE,
};

typedef struct _MxGtkLightSwitchPrivate MxGtkLightSwitchPrivate;

struct _MxGtkLightSwitchPrivate {
  gboolean active; /* boolean state of switch */
  gboolean dragging; /* true if dragging switch */
  gint     x; /* the x position of the switch */
  gint     drag_start; /* position dragging started at */
  gint     drag_threshold;
  gint     switch_width;
  gint     switch_height;
  gint     trough_width;
  gint     offset; /* offset of the mouse to slider when dragging */
};

static void
mx_gtk_light_switch_set_property (GObject      *object,
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  MxGtkLightSwitch *ls;

  ls = MX_GTK_LIGHT_SWITCH (object);

  switch (prop_id)
    {
    case PROP_ACTIVE:
      mx_gtk_light_switch_set_active (ls, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
mx_gtk_light_switch_get_property (GObject      *object,
                                  guint         prop_id,
                                  GValue       *value,
                                  GParamSpec   *pspec)
{
  MxGtkLightSwitchPrivate *priv;

  priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (object);

  switch (prop_id)
    {
    case PROP_ACTIVE:
      g_value_set_boolean (value, priv->active);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
mx_gtk_light_switch_class_init (MxGtkLightSwitchClass *klass)
{
  GObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GParamSpec *spec;

  object_class = G_OBJECT_CLASS (klass);
  widget_class = GTK_WIDGET_CLASS (klass);

  object_class->set_property = mx_gtk_light_switch_set_property;
  object_class->get_property = mx_gtk_light_switch_get_property;

  widget_class->configure_event = mx_gtk_light_switch_configure;
  widget_class->expose_event = mx_gtk_light_switch_expose;
  widget_class->button_release_event = mx_gtk_light_switch_button_release;
  widget_class->button_press_event = mx_gtk_light_switch_button_press;
  widget_class->motion_notify_event = mx_gtk_light_switch_motion_notify;
  widget_class->size_request = mx_gtk_light_switch_size_request;
  widget_class->style_set = mx_gtk_light_switch_style_set;

  spec = g_param_spec_boolean ("active",
                               "Active",
                               "Is the light switch on or not",
                               FALSE,
                               G_PARAM_READWRITE);
  g_object_class_install_property (object_class, PROP_ACTIVE, spec);

  /* MxGtkLightSwitch signals */
  mx_gtk_light_switch_signals[SWITCH_FLIPPED] =
    g_signal_new ("switch-flipped",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (MxGtkLightSwitchClass, switch_flipped),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__BOOLEAN,
                  G_TYPE_NONE, 1,
                  G_TYPE_BOOLEAN);

  g_type_class_add_private (klass,
                            sizeof (MxGtkLightSwitchPrivate));
}

static void
mx_gtk_light_switch_init (MxGtkLightSwitch *self)
{
  MxGtkLightSwitchPrivate *priv;

  priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (self);
  priv->active = FALSE;
  priv->x = 0;

  /* add events, do initial draw/update, etc */
  gtk_widget_add_events (GTK_WIDGET (self),
                         GDK_BUTTON_PRESS_MASK
                         | GDK_BUTTON_RELEASE_MASK
                         | GDK_POINTER_MOTION_MASK);
}

static void
mx_gtk_light_switch_size_request (GtkWidget      *lightswitch,
                                  GtkRequisition *req)
{
  MxGtkLightSwitchPrivate *priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (lightswitch);

  req->height = priv->switch_height;
  req->width = priv->trough_width;
}

static void
mx_gtk_light_switch_style_set (GtkWidget *lightswitch,
                               GtkStyle  *previous_style)
{
  MxGtkLightSwitchPrivate *priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (lightswitch);

  /* TODO: use style properties for these values */

  /* MxToggle is 98x24, so make sure light-switch is at least this size */
  priv->trough_width = 98;
  priv->switch_height = 24;
  priv->switch_width = 50;
}

static gboolean
mx_gtk_light_switch_configure (GtkWidget         *lightswitch,
                               GdkEventConfigure *event)
{
  MxGtkLightSwitchPrivate *priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (lightswitch);

  if (priv->active)
    priv->x = priv->trough_width - priv->switch_width;
  else
    priv->x = 0;

  return FALSE;
}

static gboolean
mx_gtk_light_switch_expose (GtkWidget      *lightswitch,
                            GdkEventExpose *event)
{
  MxGtkLightSwitchPrivate *priv;
  GtkStyle *style;
  GtkStateType state_type;
  cairo_t *cr;

  cr = gdk_cairo_create (gtk_widget_get_window (lightswitch));

  cairo_rectangle (cr,
                   event->area.x,
                   event->area.y,
                   event->area.width,
                   event->area.height);

  cairo_clip (cr);

  priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (lightswitch);
  style = gtk_widget_get_style (lightswitch);
  state_type = gtk_widget_get_state (lightswitch);

  /* draw the trough */
  gtk_paint_box (style,
                 gtk_widget_get_window (lightswitch),
                 (state_type != GTK_STATE_INSENSITIVE && priv->active)
                 ? GTK_STATE_SELECTED : state_type,
                 GTK_SHADOW_IN,
                 NULL,
                 NULL,
                 "light-switch-trough",
                 0,
                 0,
                 (priv->trough_width),
                 priv->switch_height);


  /* draw the switch itself */
  if (state_type != GTK_STATE_INSENSITIVE)
    {
      gtk_paint_box (style,
                     gtk_widget_get_window (lightswitch),
                     gtk_widget_get_state (lightswitch),
                     GTK_SHADOW_OUT,
                     NULL,
                     NULL,
                     "light-switch-handle",
                     priv->x + style->xthickness,
                     style->ythickness,
                     priv->switch_width - style->xthickness * 2,
                     priv->switch_height - style->ythickness * 2);
    }

  cairo_destroy (cr);

  return FALSE;
}

static gboolean
mx_gtk_light_switch_motion_notify (GtkWidget      *lightswitch,
                                   GdkEventMotion *event)
{
  MxGtkLightSwitchPrivate *priv;

  priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (lightswitch);

  if (ABS (event->x - priv->drag_start) < priv->drag_threshold)
    return TRUE;

  if (event->state & GDK_BUTTON1_MASK)
    {
      gint position = event->x - priv->offset;

      if (position > (priv->trough_width - priv->switch_width))
        priv->x = (priv->trough_width - priv->switch_width);
      else if (position < 0)
        priv->x = 0;
      else
        priv->x = position;

      priv->dragging = TRUE;
      gtk_widget_queue_draw ((GtkWidget *) lightswitch);
    }

  return TRUE;
}

/**
 * mx_gtk_light_switch_get_active:
 * @lightswitch: A #MxGtkLightSwitch
 *
 * Get the value of the "active" property
 *
 * Returns: #TRUE if the switch is "on"
 */
gboolean
mx_gtk_light_switch_get_active (MxGtkLightSwitch *lightswitch)
{
  MxGtkLightSwitchPrivate *priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (lightswitch);

  return priv->active;
}

/**
 * mx_gtk_light_switch_set_active:
 * @lightswitch: A #MxGtkLightSwitch
 * @active: #TRUE to set the switch to its ON state
 *
 * Set the value of the "active" property
 *
 */
void
mx_gtk_light_switch_set_active (MxGtkLightSwitch *lightswitch,
                                gboolean          active)
{
  MxGtkLightSwitchPrivate *priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (lightswitch);

  if (priv->active == active)
    {
      return;
    }
  else
    {
      priv->active = active;
      if (active == TRUE)
        {
          priv->x = priv->trough_width - priv->switch_width;
        }
      else
        {
          priv->x = 0;
        }

      gtk_widget_queue_draw ((GtkWidget *) lightswitch);

      g_object_notify (G_OBJECT (lightswitch), "active");
      g_signal_emit (lightswitch,
                     mx_gtk_light_switch_signals[SWITCH_FLIPPED],
                     0,
                     priv->active);
    }
}

static gboolean
mx_gtk_light_switch_button_press (GtkWidget      *lightswitch,
                                  GdkEventButton *event)
{
  MxGtkLightSwitchPrivate *priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (lightswitch);

  if (priv->active)
    priv->offset = event->x - (priv->trough_width - priv->switch_width);
  else
    priv->offset = event->x;

  priv->drag_start = event->x;

  g_object_get (gtk_widget_get_settings (lightswitch),
                "gtk-dnd-drag-threshold", &priv->drag_threshold,
                NULL);

  return FALSE;
}

static gboolean
mx_gtk_light_switch_button_release (GtkWidget      *lightswitch,
                                    GdkEventButton *event)
{
  MxGtkLightSwitchPrivate *priv;

  priv = MX_GTK_LIGHT_SWITCH_GET_PRIVATE (lightswitch);

  /* detect whereabouts we are and "drop" into a state */
  if (priv->dragging)
    {
      priv->dragging = FALSE;
      if (priv->x + (priv->switch_width / 2) > priv->trough_width / 2)
        {
          mx_gtk_light_switch_set_active ((MxGtkLightSwitch *) lightswitch, TRUE);
          priv->x = priv->trough_width - priv->switch_width;
        }
      else
        {
          mx_gtk_light_switch_set_active ((MxGtkLightSwitch *) lightswitch, FALSE);
          priv->x = 0;
        }
      /* we always need to queue a redraw after dragging to put the slider back
       * in the correct place */
      gtk_widget_queue_draw ((GtkWidget *) lightswitch);
    }
  else
    {
      mx_gtk_light_switch_set_active ((MxGtkLightSwitch *) lightswitch, !priv->active);
    }

  return FALSE;
}

/**
 * mx_gtk_light_switch_new:
 *
 * Create a #MxGtkLightSwitch
 *
 * Returns: a newly allocated #MxGtkLightSwitch
 */
GtkWidget*
mx_gtk_light_switch_new (void)
{
  return g_object_new (MX_GTK_TYPE_LIGHT_SWITCH, NULL);
}