gsignal: add g_signal_handlers_disconnect_by_data
[platform/upstream/glib.git] / gobject / gsignal.h
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000-2001 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
20 #error "Only <glib-object.h> can be included directly."
21 #endif
22
23 #ifndef __G_SIGNAL_H__
24 #define __G_SIGNAL_H__
25
26 #include        <gobject/gclosure.h>
27 #include        <gobject/gvalue.h>
28 #include        <gobject/gparam.h>
29 #include        <gobject/gmarshal.h>
30
31 G_BEGIN_DECLS
32
33 /* --- typedefs --- */
34 typedef struct _GSignalQuery             GSignalQuery;
35 typedef struct _GSignalInvocationHint    GSignalInvocationHint;
36 /**
37  * GSignalCMarshaller:
38  * 
39  * This is the signature of marshaller functions, required to marshall
40  * arrays of parameter values to signal emissions into C language callback
41  * invocations. It is merely an alias to #GClosureMarshal since the #GClosure
42  * mechanism takes over responsibility of actual function invocation for the
43  * signal system.
44  */
45 typedef GClosureMarshal                  GSignalCMarshaller;
46 /**
47  * GSignalEmissionHook:
48  * @ihint: Signal invocation hint, see #GSignalInvocationHint.
49  * @n_param_values: the number of parameters to the function, including
50  *  the instance on which the signal was emitted.
51  * @param_values: (array length=n_param_values): the instance on which
52  *  the signal was emitted, followed by the parameters of the emission.
53  * @data: user data associated with the hook.
54  * 
55  * A simple function pointer to get invoked when the signal is emitted. This 
56  * allows you to tie a hook to the signal type, so that it will trap all 
57  * emissions of that signal, from any object.
58  * 
59  * You may not attach these to signals created with the #G_SIGNAL_NO_HOOKS flag.
60  * 
61  * Returns: whether it wants to stay connected. If it returns %FALSE, the signal 
62  *  hook is disconnected (and destroyed).
63  */
64 typedef gboolean (*GSignalEmissionHook) (GSignalInvocationHint *ihint,
65                                          guint                  n_param_values,
66                                          const GValue          *param_values,
67                                          gpointer               data);
68 /**
69  * GSignalAccumulator:
70  * @ihint: Signal invocation hint, see #GSignalInvocationHint.
71  * @return_accu: Accumulator to collect callback return values in, this
72  *  is the return value of the current signal emission.
73  * @handler_return: A #GValue holding the return value of the signal handler.
74  * @data: Callback data that was specified when creating the signal.
75  * 
76  * The signal accumulator is a special callback function that can be used
77  * to collect return values of the various callbacks that are called
78  * during a signal emission. The signal accumulator is specified at signal
79  * creation time, if it is left %NULL, no accumulation of callback return
80  * values is performed. The return value of signal emissions is then the
81  * value returned by the last callback.
82  * 
83  * Returns: The accumulator function returns whether the signal emission
84  *  should be aborted. Returning %FALSE means to abort the
85  *  current emission and %TRUE is returned for continuation.
86  */
87 typedef gboolean (*GSignalAccumulator)  (GSignalInvocationHint *ihint,
88                                          GValue                *return_accu,
89                                          const GValue          *handler_return,
90                                          gpointer               data);
91
92
93 /* --- run, match and connect types --- */
94 /**
95  * GSignalFlags:
96  * @G_SIGNAL_RUN_FIRST: Invoke the object method handler in the first emission stage.
97  * @G_SIGNAL_RUN_LAST: Invoke the object method handler in the third emission stage.
98  * @G_SIGNAL_RUN_CLEANUP: Invoke the object method handler in the last emission stage.
99  * @G_SIGNAL_NO_RECURSE: Signals being emitted for an object while currently being in
100  *  emission for this very object will not be emitted recursively,
101  *  but instead cause the first emission to be restarted.
102  * @G_SIGNAL_DETAILED: This signal supports "::detail" appendices to the signal name
103  *  upon handler connections and emissions.
104  * @G_SIGNAL_ACTION: Action signals are signals that may freely be emitted on alive
105  *  objects from user code via g_signal_emit() and friends, without
106  *  the need of being embedded into extra code that performs pre or
107  *  post emission adjustments on the object. They can also be thought
108  *  of as object methods which can be called generically by 
109  *  third-party code.
110  * @G_SIGNAL_NO_HOOKS: No emissions hooks are supported for this signal.
111  * @G_SIGNAL_MUST_COLLECT: Varargs signal emission will always collect the
112  *   arguments, even if there are no signal handlers connected.  Since 2.30.
113  * @G_SIGNAL_DEPRECATED: The signal is deprecated and will be removed
114  *   in a future version. A warning will be generated if it is connected while
115  *   running with G_ENABLE_DIAGNOSTIC=1.  Since 2.32.
116  * 
117  * The signal flags are used to specify a signal's behaviour, the overall
118  * signal description outlines how especially the RUN flags control the
119  * stages of a signal emission.
120  */
121 typedef enum
122 {
123   G_SIGNAL_RUN_FIRST    = 1 << 0,
124   G_SIGNAL_RUN_LAST     = 1 << 1,
125   G_SIGNAL_RUN_CLEANUP  = 1 << 2,
126   G_SIGNAL_NO_RECURSE   = 1 << 3,
127   G_SIGNAL_DETAILED     = 1 << 4,
128   G_SIGNAL_ACTION       = 1 << 5,
129   G_SIGNAL_NO_HOOKS     = 1 << 6,
130   G_SIGNAL_MUST_COLLECT = 1 << 7,
131   G_SIGNAL_DEPRECATED   = 1 << 8
132 } GSignalFlags;
133 /**
134  * G_SIGNAL_FLAGS_MASK:
135  * 
136  * A mask for all #GSignalFlags bits.
137  */
138 #define G_SIGNAL_FLAGS_MASK  0x1ff
139 /**
140  * GConnectFlags:
141  * @G_CONNECT_AFTER: whether the handler should be called before or after the 
142  *  default handler of the signal.
143  * @G_CONNECT_SWAPPED: whether the instance and data should be swapped when
144  *  calling the handler.
145  * 
146  * The connection flags are used to specify the behaviour of a signal's 
147  * connection.
148  */
149 typedef enum
150 {
151   G_CONNECT_AFTER       = 1 << 0,
152   G_CONNECT_SWAPPED     = 1 << 1
153 } GConnectFlags;
154 /**
155  * GSignalMatchType:
156  * @G_SIGNAL_MATCH_ID: The signal id must be equal.
157  * @G_SIGNAL_MATCH_DETAIL: The signal detail be equal.
158  * @G_SIGNAL_MATCH_CLOSURE: The closure must be the same.
159  * @G_SIGNAL_MATCH_FUNC: The C closure callback must be the same.
160  * @G_SIGNAL_MATCH_DATA: The closure data must be the same.
161  * @G_SIGNAL_MATCH_UNBLOCKED: Only unblocked signals may matched.
162  * 
163  * The match types specify what g_signal_handlers_block_matched(),
164  * g_signal_handlers_unblock_matched() and g_signal_handlers_disconnect_matched()
165  * match signals by.
166  */
167 typedef enum
168 {
169   G_SIGNAL_MATCH_ID        = 1 << 0,
170   G_SIGNAL_MATCH_DETAIL    = 1 << 1,
171   G_SIGNAL_MATCH_CLOSURE   = 1 << 2,
172   G_SIGNAL_MATCH_FUNC      = 1 << 3,
173   G_SIGNAL_MATCH_DATA      = 1 << 4,
174   G_SIGNAL_MATCH_UNBLOCKED = 1 << 5
175 } GSignalMatchType;
176 /**
177  * G_SIGNAL_MATCH_MASK:
178  * 
179  * A mask for all #GSignalMatchType bits.
180  */
181 #define G_SIGNAL_MATCH_MASK  0x3f
182 /**
183  * G_SIGNAL_TYPE_STATIC_SCOPE:
184  * 
185  * This macro flags signal argument types for which the signal system may 
186  * assume that instances thereof remain persistent across all signal emissions
187  * they are used in. This is only useful for non ref-counted, value-copy types.
188  * 
189  * To flag a signal argument in this way, add 
190  * <literal>| G_SIGNAL_TYPE_STATIC_SCOPE</literal> to the corresponding argument
191  * of g_signal_new().
192  * |[
193  * g_signal_new ("size_request",
194  *   G_TYPE_FROM_CLASS (gobject_class),
195  *       G_SIGNAL_RUN_FIRST,
196  *       G_STRUCT_OFFSET (GtkWidgetClass, size_request),
197  *       NULL, NULL,
198  *       _gtk_marshal_VOID__BOXED,
199  *       G_TYPE_NONE, 1,
200  *       GTK_TYPE_REQUISITION | G_SIGNAL_TYPE_STATIC_SCOPE);
201  * ]|
202  */
203 #define G_SIGNAL_TYPE_STATIC_SCOPE (G_TYPE_FLAG_RESERVED_ID_BIT)
204
205
206 /* --- signal information --- */
207 /**
208  * GSignalInvocationHint:
209  * @signal_id: The signal id of the signal invoking the callback
210  * @detail: The detail passed on for this emission
211  * @run_type: The stage the signal emission is currently in, this
212  *  field will contain one of %G_SIGNAL_RUN_FIRST,
213  *  %G_SIGNAL_RUN_LAST or %G_SIGNAL_RUN_CLEANUP.
214  * 
215  * The #GSignalInvocationHint structure is used to pass on additional information
216  * to callbacks during a signal emission.
217  */
218 struct _GSignalInvocationHint
219 {
220   guint         signal_id;
221   GQuark        detail;
222   GSignalFlags  run_type;
223 };
224 /**
225  * GSignalQuery:
226  * @signal_id: The signal id of the signal being queried, or 0 if the
227  *  signal to be queried was unknown.
228  * @signal_name: The signal name.
229  * @itype: The interface/instance type that this signal can be emitted for.
230  * @signal_flags: The signal flags as passed in to g_signal_new().
231  * @return_type: The return type for user callbacks.
232  * @n_params: The number of parameters that user callbacks take.
233  * @param_types: The individual parameter types for user callbacks, note that the
234  *  effective callback signature is:
235  *  <programlisting>
236  *  @return_type callback (#gpointer     data1,
237  *  [param_types param_names,]
238  *  gpointer     data2);
239  *  </programlisting>
240  * 
241  * A structure holding in-depth information for a specific signal. It is
242  * filled in by the g_signal_query() function.
243  */
244 struct _GSignalQuery
245 {
246   guint         signal_id;
247   const gchar  *signal_name;
248   GType         itype;
249   GSignalFlags  signal_flags;
250   GType         return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
251   guint         n_params;
252   const GType  *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
253 };
254
255
256 /* --- signals --- */
257 guint                 g_signal_newv         (const gchar        *signal_name,
258                                              GType               itype,
259                                              GSignalFlags        signal_flags,
260                                              GClosure           *class_closure,
261                                              GSignalAccumulator  accumulator,
262                                              gpointer            accu_data,
263                                              GSignalCMarshaller  c_marshaller,
264                                              GType               return_type,
265                                              guint               n_params,
266                                              GType              *param_types);
267 guint                 g_signal_new_valist   (const gchar        *signal_name,
268                                              GType               itype,
269                                              GSignalFlags        signal_flags,
270                                              GClosure           *class_closure,
271                                              GSignalAccumulator  accumulator,
272                                              gpointer            accu_data,
273                                              GSignalCMarshaller  c_marshaller,
274                                              GType               return_type,
275                                              guint               n_params,
276                                              va_list             args);
277 guint                 g_signal_new          (const gchar        *signal_name,
278                                              GType               itype,
279                                              GSignalFlags        signal_flags,
280                                              guint               class_offset,
281                                              GSignalAccumulator  accumulator,
282                                              gpointer            accu_data,
283                                              GSignalCMarshaller  c_marshaller,
284                                              GType               return_type,
285                                              guint               n_params,
286                                              ...);
287 guint            g_signal_new_class_handler (const gchar        *signal_name,
288                                              GType               itype,
289                                              GSignalFlags        signal_flags,
290                                              GCallback           class_handler,
291                                              GSignalAccumulator  accumulator,
292                                              gpointer            accu_data,
293                                              GSignalCMarshaller  c_marshaller,
294                                              GType               return_type,
295                                              guint               n_params,
296                                              ...);
297
298 void                  g_signal_emitv        (const GValue       *instance_and_params,
299                                              guint               signal_id,
300                                              GQuark              detail,
301                                              GValue             *return_value);
302 void                  g_signal_emit_valist  (gpointer            instance,
303                                              guint               signal_id,
304                                              GQuark              detail,
305                                              va_list             var_args);
306 void                  g_signal_emit         (gpointer            instance,
307                                              guint               signal_id,
308                                              GQuark              detail,
309                                              ...);
310 void                  g_signal_emit_by_name (gpointer            instance,
311                                              const gchar        *detailed_signal,
312                                              ...);
313 guint                 g_signal_lookup       (const gchar        *name,
314                                              GType               itype);
315 const gchar *         g_signal_name         (guint               signal_id);
316 void                  g_signal_query        (guint               signal_id,
317                                              GSignalQuery       *query);
318 guint*                g_signal_list_ids     (GType               itype,
319                                              guint              *n_ids);
320 gboolean              g_signal_parse_name   (const gchar        *detailed_signal,
321                                              GType               itype,
322                                              guint              *signal_id_p,
323                                              GQuark             *detail_p,
324                                              gboolean            force_detail_quark);
325 GSignalInvocationHint* g_signal_get_invocation_hint (gpointer    instance);
326
327
328 /* --- signal emissions --- */
329 void    g_signal_stop_emission              (gpointer             instance,
330                                              guint                signal_id,
331                                              GQuark               detail);
332 void    g_signal_stop_emission_by_name      (gpointer             instance,
333                                              const gchar         *detailed_signal);
334 gulong  g_signal_add_emission_hook          (guint                signal_id,
335                                              GQuark               detail,
336                                              GSignalEmissionHook  hook_func,
337                                              gpointer             hook_data,
338                                              GDestroyNotify       data_destroy);
339 void    g_signal_remove_emission_hook       (guint                signal_id,
340                                              gulong               hook_id);
341
342
343 /* --- signal handlers --- */
344 gboolean g_signal_has_handler_pending         (gpointer           instance,
345                                                guint              signal_id,
346                                                GQuark             detail,
347                                                gboolean           may_be_blocked);
348 gulong   g_signal_connect_closure_by_id       (gpointer           instance,
349                                                guint              signal_id,
350                                                GQuark             detail,
351                                                GClosure          *closure,
352                                                gboolean           after);
353 gulong   g_signal_connect_closure             (gpointer           instance,
354                                                const gchar       *detailed_signal,
355                                                GClosure          *closure,
356                                                gboolean           after);
357 gulong   g_signal_connect_data                (gpointer           instance,
358                                                const gchar       *detailed_signal,
359                                                GCallback          c_handler,
360                                                gpointer           data,
361                                                GClosureNotify     destroy_data,
362                                                GConnectFlags      connect_flags);
363 void     g_signal_handler_block               (gpointer           instance,
364                                                gulong             handler_id);
365 void     g_signal_handler_unblock             (gpointer           instance,
366                                                gulong             handler_id);
367 void     g_signal_handler_disconnect          (gpointer           instance,
368                                                gulong             handler_id);
369 gboolean g_signal_handler_is_connected        (gpointer           instance,
370                                                gulong             handler_id);
371 gulong   g_signal_handler_find                (gpointer           instance,
372                                                GSignalMatchType   mask,
373                                                guint              signal_id,
374                                                GQuark             detail,
375                                                GClosure          *closure,
376                                                gpointer           func,
377                                                gpointer           data);
378 guint    g_signal_handlers_block_matched      (gpointer           instance,
379                                                GSignalMatchType   mask,
380                                                guint              signal_id,
381                                                GQuark             detail,
382                                                GClosure          *closure,
383                                                gpointer           func,
384                                                gpointer           data);
385 guint    g_signal_handlers_unblock_matched    (gpointer           instance,
386                                                GSignalMatchType   mask,
387                                                guint              signal_id,
388                                                GQuark             detail,
389                                                GClosure          *closure,
390                                                gpointer           func,
391                                                gpointer           data);
392 guint    g_signal_handlers_disconnect_matched (gpointer           instance,
393                                                GSignalMatchType   mask,
394                                                guint              signal_id,
395                                                GQuark             detail,
396                                                GClosure          *closure,
397                                                gpointer           func,
398                                                gpointer           data);
399
400
401 /* --- overriding and chaining --- */
402 void    g_signal_override_class_closure       (guint              signal_id,
403                                                GType              instance_type,
404                                                GClosure          *class_closure);
405 void    g_signal_override_class_handler       (const gchar       *signal_name,
406                                                GType              instance_type,
407                                                GCallback          class_handler);
408 void    g_signal_chain_from_overridden        (const GValue      *instance_and_params,
409                                                GValue            *return_value);
410 void   g_signal_chain_from_overridden_handler (gpointer           instance,
411                                                ...);
412
413
414 /* --- convenience --- */
415 /**
416  * g_signal_connect:
417  * @instance: the instance to connect to.
418  * @detailed_signal: a string of the form "signal-name::detail".
419  * @c_handler: the #GCallback to connect.
420  * @data: data to pass to @c_handler calls.
421  * 
422  * Connects a #GCallback function to a signal for a particular object.
423  * 
424  * The handler will be called before the default handler of the signal.
425  * 
426  * Returns: the handler id
427  */
428 #define g_signal_connect(instance, detailed_signal, c_handler, data) \
429     g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, (GConnectFlags) 0)
430 /**
431  * g_signal_connect_after:
432  * @instance: the instance to connect to.
433  * @detailed_signal: a string of the form "signal-name::detail".
434  * @c_handler: the #GCallback to connect.
435  * @data: data to pass to @c_handler calls.
436  * 
437  * Connects a #GCallback function to a signal for a particular object.
438  * 
439  * The handler will be called after the default handler of the signal.
440  * 
441  * Returns: the handler id
442  */
443 #define g_signal_connect_after(instance, detailed_signal, c_handler, data) \
444     g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, G_CONNECT_AFTER)
445 /**
446  * g_signal_connect_swapped:
447  * @instance: the instance to connect to.
448  * @detailed_signal: a string of the form "signal-name::detail".
449  * @c_handler: the #GCallback to connect.
450  * @data: data to pass to @c_handler calls.
451  * 
452  * Connects a #GCallback function to a signal for a particular object.
453  * 
454  * The instance on which the signal is emitted and @data will be swapped when 
455  * calling the handler.
456  * 
457  * Returns: the handler id
458  */
459 #define g_signal_connect_swapped(instance, detailed_signal, c_handler, data) \
460     g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, G_CONNECT_SWAPPED)
461 /**
462  * g_signal_handlers_disconnect_by_func:
463  * @instance: The instance to remove handlers from.
464  * @func: The C closure callback of the handlers (useless for non-C closures).
465  * @data: The closure data of the handlers' closures.
466  * 
467  * Disconnects all handlers on an instance that match @func and @data.
468  * 
469  * Returns: The number of handlers that matched.
470  */
471 #define g_signal_handlers_disconnect_by_func(instance, func, data)                                              \
472     g_signal_handlers_disconnect_matched ((instance),                                                           \
473                                           (GSignalMatchType) (G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA),       \
474                                           0, 0, NULL, (func), (data))
475
476 /**
477  * g_signal_handlers_disconnect_by_data:
478  * @instance: The instance to remove handlers from
479  * @data: the closure data of the handlers' closures
480  *
481  * Disconnects all handlers on an instance that match @data.
482  *
483  * Returns: The number of handlers that matched.
484  *
485  * Since: 2.32
486  */
487 #define g_signal_handlers_disconnect_by_data(instance, data) \
488   g_signal_handlers_disconnect_matched ((instance), G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, (data))
489
490 /**
491  * g_signal_handlers_block_by_func:
492  * @instance: The instance to block handlers from.
493  * @func: The C closure callback of the handlers (useless for non-C closures).
494  * @data: The closure data of the handlers' closures.
495  * 
496  * Blocks all handlers on an instance that match @func and @data.
497  * 
498  * Returns: The number of handlers that matched.
499  */
500 #define g_signal_handlers_block_by_func(instance, func, data)                                                   \
501     g_signal_handlers_block_matched      ((instance),                                                           \
502                                           (GSignalMatchType) (G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA),       \
503                                           0, 0, NULL, (func), (data))
504 /**
505  * g_signal_handlers_unblock_by_func:
506  * @instance: The instance to unblock handlers from.
507  * @func: The C closure callback of the handlers (useless for non-C closures).
508  * @data: The closure data of the handlers' closures.
509  * 
510  * Unblocks all handlers on an instance that match @func and @data.
511  * 
512  * Returns: The number of handlers that matched.
513  */
514 #define g_signal_handlers_unblock_by_func(instance, func, data)                                                 \
515     g_signal_handlers_unblock_matched    ((instance),                                                           \
516                                           (GSignalMatchType) (G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA),       \
517                                           0, 0, NULL, (func), (data))
518
519
520 gboolean g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
521                                             GValue                *return_accu,
522                                             const GValue          *handler_return,
523                                             gpointer               dummy);
524
525 gboolean g_signal_accumulator_first_wins   (GSignalInvocationHint *ihint,
526                                             GValue                *return_accu,
527                                             const GValue          *handler_return,
528                                             gpointer               dummy);
529
530 /*< private >*/
531 void     g_signal_handlers_destroy            (gpointer           instance);
532 void     _g_signals_destroy                   (GType              itype);
533
534 G_END_DECLS
535
536 #endif /* __G_SIGNAL_H__ */