~kamstrup/gtkhotkey/trunk

« back to all changes in this revision

Viewing changes to src/gtk-hotkey.c

  • Committer: "Mikkel Kamstrup Erlandsen"
  • Date: 2009-10-01 08:59:31 UTC
  • Revision ID: mikkel.kamstrup@gmail.com-20091001085931-dma9e0loluq3snum
More work towards getting the 0.3 API ready. Among which implement gtk_hotkey_set_signature() with rebinding if necessary (also add this functionality for when setting the signature via the properties)

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include "gtk-hotkey-listener.h"
22
22
#include "gtk-hotkey-registry.h"
23
23
 
 
24
G_DEFINE_TYPE (GtkHotkey, gtk_hotkey, G_TYPE_OBJECT)
 
25
 
24
26
struct _GtkHotkeyPrivate {
25
27
        gchar           *app_id;
26
28
        gchar           *key_id;
52
54
 
53
55
guint                           info_signals[LAST_SIGNAL] = { 0 };
54
56
 
55
 
static gpointer         gtk_hotkey_parent_class = NULL;
56
 
 
57
57
static void                     gtk_hotkey_finalize (GObject * obj);
58
58
 
59
59
/**
158
158
        return result;
159
159
}
160
160
 
 
161
/* Rebinds a hotkey if it is bound, otherwise a no-op */
 
162
static gboolean
 
163
_gtk_hotkey_check_rebind (GtkHotkey *self, GError **error)
 
164
{
 
165
        g_return_val_if_fail (GTK_HOTKEY_IS_HOTKEY (self), FALSE);
 
166
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
167
        
 
168
        if (!gtk_hotkey_is_bound (self))
 
169
                return TRUE;
 
170
        
 
171
        if (!gtk_hotkey_unbind (self, error)) {         
 
172
            return FALSE;
 
173
        }
 
174
        
 
175
        if (!gtk_hotkey_bind (self, error)) {           
 
176
            return FALSE;
 
177
        }
 
178
        
 
179
        return TRUE;
 
180
}
 
181
 
161
182
/**
162
183
 * gtk_hotkey_is_bound
163
184
 * @self: The hotkey to inspect
233
254
}
234
255
 
235
256
/**
 
257
 * gtk_hotkey_set_signature
 
258
 * @self: The %GtkHotkey to set the signature on
 
259
 * @signature: The keyboard signature to listen for. The signature should be
 
260
 *             in a format parseable by gtk_accelerator_parse()
 
261
 * @error: Return location for GError pointer. If %NULL errors will not be
 
262
 *         reported
 
263
 *
 
264
 * Set the keyboard signature of the hotkey. This could for example be
 
265
 * '<Alt>F3' or '<Control><Shift>G'.
 
266
 *
 
267
 * If @self is already bound it will be rebound to listen for @signature. If it
 
268
 * is not bound it will remain unbound.
 
269
 *
 
270
 * Return value: %TRUE if the assignment was succesful %FALSE otherwise,
 
271
 *               in which case @error will be set if @error was not %NULL
 
272
 */
 
273
gboolean
 
274
gtk_hotkey_set_signature (GtkHotkey   *self,
 
275
                          const gchar *signature,
 
276
                          GError      **error)
 
277
{
 
278
        GtkHotkeyPrivate *priv;
 
279
        
 
280
        g_return_val_if_fail (GTK_HOTKEY_IS_HOTKEY (self), FALSE);
 
281
        g_return_val_if_fail (signature != NULL, FALSE);
 
282
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
283
        
 
284
        priv = self->priv;
 
285
        g_free (priv->signature);
 
286
        priv->signature = g_strdup (signature);
 
287
        g_object_notify (G_OBJECT (self), "signature");
 
288
        
 
289
        /* Rebind the key if it is bound */
 
290
        error = NULL;
 
291
        if (!_gtk_hotkey_check_rebind (self, error))
 
292
                return FALSE;
 
293
        
 
294
        return TRUE;
 
295
}
 
296
 
 
297
/**
236
298
 * gtk_hotkey_get_description
237
299
 * @self:
238
300
 * @returns: The description of the hotkey or %NULL if none is set
397
459
{
398
460
        GtkHotkey                       *self;
399
461
        GtkHotkeyPrivate        *priv;
 
462
        GError                          *error;
400
463
        
401
464
        self = GTK_HOTKEY (object);
402
465
        priv = self->priv;
421
484
                        priv->app_info = g_value_dup_object (value);
422
485
                        break;
423
486
                case GTK_HOTKEY_SIGNATURE:
424
 
                        if (priv->signature)
425
 
                                g_critical ("Overwriting construct only property 'signature'");
426
487
                        priv->signature = g_value_dup_string (value);
 
488
                        
 
489
                        error = NULL;
 
490
                        if (!_gtk_hotkey_check_rebind (self, &error)) {
 
491
                                g_critical ("Failed to rebind hotkey '%s': %s",
 
492
                                            gtk_hotkey_get_key_id (self),
 
493
                                            error->message);
 
494
                                g_error_free (error);
 
495
                        }                       
427
496
                        break;
428
497
                case GTK_HOTKEY_DESCRIPTION:
429
498
                        if (priv->description)
536
605
                                                                                                                  "Signature",
537
606
                                                                                                                  "String defining the keyboard shortcut",
538
607
                                                                                                                  NULL, 
539
 
                                                                                                                  G_PARAM_READABLE | G_PARAM_WRITABLE |
540
 
                                                                                                                  G_PARAM_CONSTRUCT_ONLY));
 
608
                                                                                                                  G_PARAM_READWRITE));
541
609
        
542
610
        /**
543
611
         * GtkHotkey:description
549
617
                                                                         g_param_spec_string ("description",
550
618
                                                                                                                  "Description",
551
619
                                                                                                                  "Short description of what happens upon activation",
552
 
                                                                                                                  "", 
553
 
                                                                                                                  G_PARAM_READABLE | G_PARAM_WRITABLE));
 
620
                                                                                                                  "No description", 
 
621
                                                                                                                  G_PARAM_READWRITE));
554
622
        /**
555
623
         * GtkHotkey:listener
556
624
         *
603
671
static void
604
672
gtk_hotkey_init (GtkHotkey * self)
605
673
{
 
674
        GtkHotkeyPrivate *priv;
 
675
        
606
676
        self->priv = GTK_HOTKEY_GET_PRIVATE (self);
 
677
        priv = self->priv;
607
678
        
608
 
        self->priv->app_id = NULL;
609
 
        self->priv->key_id = NULL;
610
 
        self->priv->app_info = NULL;
 
679
        priv->app_id = NULL;
 
680
        priv->key_id = NULL;
 
681
        priv->app_info = NULL;
 
682
        priv->signature = NULL;
 
683
        priv->listener = NULL;
 
684
        priv->registry = NULL;
611
685
}
612
686
 
613
687
 
630
704
                g_free (priv->signature);
631
705
        if (priv->description)
632
706
                g_free (priv->description);
633
 
        if (GTK_HOTKEY_IS_LISTENER (priv->listener))
 
707
        if (priv->listener != NULL)
634
708
                g_object_unref (priv->listener);
 
709
        if (priv->registry != NULL)
 
710
                g_object_unref (priv->registry);
635
711
        
636
712
        G_OBJECT_CLASS (gtk_hotkey_parent_class)->finalize (obj);
637
713
}
638
714
 
639
715
 
640
 
GType
 
716
/*GType
641
717
gtk_hotkey_get_type (void)
642
718
{
643
719
        static GType gtk_hotkey_type_id = 0;
662
738
        }
663
739
        
664
740
        return gtk_hotkey_type_id;
665
 
}
 
741
}*/
666
742
 
667
743
 
668
744