[gi] Add signal annotations
[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  * 
114  * The signal flags are used to specify a signal's behaviour, the overall
115  * signal description outlines how especially the RUN flags control the
116  * stages of a signal emission.
117  */
118 typedef enum
119 {
120   G_SIGNAL_RUN_FIRST    = 1 << 0,
121   G_SIGNAL_RUN_LAST     = 1 << 1,
122   G_SIGNAL_RUN_CLEANUP  = 1 << 2,
123   G_SIGNAL_NO_RECURSE   = 1 << 3,
124   G_SIGNAL_DETAILED     = 1 << 4,
125   G_SIGNAL_ACTION       = 1 << 5,
126   G_SIGNAL_NO_HOOKS     = 1 << 6,
127   G_SIGNAL_MUST_COLLECT = 1 << 7
128 } GSignalFlags;
129 /**
130  * G_SIGNAL_FLAGS_MASK:
131  * 
132  * A mask for all #GSignalFlags bits.
133  */
134 #define G_SIGNAL_FLAGS_MASK  0xff
135 /**
136  * GConnectFlags:
137  * @G_CONNECT_AFTER: whether the handler should be called before or after the 
138  *  default handler of the signal.
139  * @G_CONNECT_SWAPPED: whether the instance and data should be swapped when
140  *  calling the handler.
141  * 
142  * The connection flags are used to specify the behaviour of a signal's 
143  * connection.
144  */
145 typedef enum
146 {
147   G_CONNECT_AFTER       = 1 << 0,
148   G_CONNECT_SWAPPED     = 1 << 1
149 } GConnectFlags;
150 /**
151  * GSignalMatchType:
152  * @G_SIGNAL_MATCH_ID: The signal id must be equal.
153  * @G_SIGNAL_MATCH_DETAIL: The signal detail be equal.
154  * @G_SIGNAL_MATCH_CLOSURE: The closure must be the same.
155  * @G_SIGNAL_MATCH_FUNC: The C closure callback must be the same.
156  * @G_SIGNAL_MATCH_DATA: The closure data must be the same.
157  * @G_SIGNAL_MATCH_UNBLOCKED: Only unblocked signals may matched.
158  * 
159  * The match types specify what g_signal_handlers_block_matched(),
160  * g_signal_handlers_unblock_matched() and g_signal_handlers_disconnect_matched()
161  * match signals by.
162  */
163 typedef enum
164 {
165   G_SIGNAL_MATCH_ID        = 1 << 0,
166   G_SIGNAL_MATCH_DETAIL    = 1 << 1,
167   G_SIGNAL_MATCH_CLOSURE   = 1 << 2,
168   G_SIGNAL_MATCH_FUNC      = 1 << 3,
169   G_SIGNAL_MATCH_DATA      = 1 << 4,
170   G_SIGNAL_MATCH_UNBLOCKED = 1 << 5
171 } GSignalMatchType;
172 /**
173  * G_SIGNAL_MATCH_MASK:
174  * 
175  * A mask for all #GSignalMatchType bits.
176  */
177 #define G_SIGNAL_MATCH_MASK  0x3f
178 /**
179  * G_SIGNAL_TYPE_STATIC_SCOPE:
180  * 
181  * This macro flags signal argument types for which the signal system may 
182  * assume that instances thereof remain persistent across all signal emissions
183  * they are used in. This is only useful for non ref-counted, value-copy types.
184  * 
185  * To flag a signal argument in this way, add 
186  * <literal>| G_SIGNAL_TYPE_STATIC_SCOPE</literal> to the corresponding argument
187  * of g_signal_new().
188  * |[
189  * g_signal_new ("size_request",
190  *   G_TYPE_FROM_CLASS (gobject_class),
191  *       G_SIGNAL_RUN_FIRST,
192  *       G_STRUCT_OFFSET (GtkWidgetClass, size_request),
193  *       NULL, NULL,
194  *       _gtk_marshal_VOID__BOXED,
195  *       G_TYPE_NONE, 1,
196  *       GTK_TYPE_REQUISITION | G_SIGNAL_TYPE_STATIC_SCOPE);
197  * ]|
198  */
199 #define G_SIGNAL_TYPE_STATIC_SCOPE (G_TYPE_FLAG_RESERVED_ID_BIT)
200
201
202 /* --- signal information --- */
203 /**
204  * GSignalInvocationHint:
205  * @signal_id: The signal id of the signal invoking the callback
206  * @detail: The detail passed on for this emission
207  * @run_type: The stage the signal emission is currently in, this
208  *  field will contain one of %G_SIGNAL_RUN_FIRST,
209  *  %G_SIGNAL_RUN_LAST or %G_SIGNAL_RUN_CLEANUP.
210  * 
211  * The #GSignalInvocationHint structure is used to pass on additional information
212  * to callbacks during a signal emission.
213  */
214 struct _GSignalInvocationHint
215 {
216   guint         signal_id;
217   GQuark        detail;
218   GSignalFlags  run_type;
219 };
220 /**
221  * GSignalQuery:
222  * @signal_id: The signal id of the signal being queried, or 0 if the
223  *  signal to be queried was unknown.
224  * @signal_name: The signal name.
225  * @itype: The interface/instance type that this signal can be emitted for.
226  * @signal_flags: The signal flags as passed in to g_signal_new().
227  * @return_type: The return type for user callbacks.
228  * @n_params: The number of parameters that user callbacks take.
229  * @param_types: The individual parameter types for user callbacks, note that the
230  *  effective callback signature is:
231  *  <programlisting>
232  *  @return_type callback (#gpointer     data1,
233  *  [#param_types param_names,]
234  *  #gpointer     data2);
235  *  </programlisting>
236  * 
237  * A structure holding in-depth information for a specific signal. It is
238  * filled in by the g_signal_query() function.
239  */
240 struct _GSignalQuery
241 {
242   guint         signal_id;
243   const gchar  *signal_name;
244   GType         itype;
245   GSignalFlags  signal_flags;
246   GType         return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
247   guint         n_params;
248   const GType  *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
249 };
250
251
252 /* --- signals --- */
253 guint                 g_signal_newv         (const gchar        *signal_name,
254                                              GType               itype,
255                                              GSignalFlags        signal_flags,
256                                              GClosure           *class_closure,
257                                              GSignalAccumulator  accumulator,
258                                              gpointer            accu_data,
259                                              GSignalCMarshaller  c_marshaller,
260                                              GType               return_type,
261                                              guint               n_params,
262                                              GType              *param_types);
263 guint                 g_signal_new_valist   (const gchar        *signal_name,
264                                              GType               itype,
265                                              GSignalFlags        signal_flags,
266                                              GClosure           *class_closure,
267                                              GSignalAccumulator  accumulator,
268                                              gpointer            accu_data,
269                                              GSignalCMarshaller  c_marshaller,
270                                              GType               return_type,
271                                              guint               n_params,
272                                              va_list             args);
273 guint                 g_signal_new          (const gchar        *signal_name,
274                                              GType               itype,
275                                              GSignalFlags        signal_flags,
276                                              guint               class_offset,
277                                              GSignalAccumulator  accumulator,
278                                              gpointer            accu_data,
279                                              GSignalCMarshaller  c_marshaller,
280                                              GType               return_type,
281                                              guint               n_params,
282                                              ...);
283 guint            g_signal_new_class_handler (const gchar        *signal_name,
284                                              GType               itype,
285                                              GSignalFlags        signal_flags,
286                                              GCallback           class_handler,
287                                              GSignalAccumulator  accumulator,
288                                              gpointer            accu_data,
289                                              GSignalCMarshaller  c_marshaller,
290                                              GType               return_type,
291                                              guint               n_params,
292                                              ...);
293
294 void                  g_signal_emitv        (const GValue       *instance_and_params,
295                                              guint               signal_id,
296                                              GQuark              detail,
297                                              GValue             *return_value);
298 void                  g_signal_emit_valist  (gpointer            instance,
299                                              guint               signal_id,
300                                              GQuark              detail,
301                                              va_list             var_args);
302 void                  g_signal_emit         (gpointer            instance,
303                                              guint               signal_id,
304                                              GQuark              detail,
305                                              ...);
306 void                  g_signal_emit_by_name (gpointer            instance,
307                                              const gchar        *detailed_signal,
308                                              ...);
309 guint                 g_signal_lookup       (const gchar        *name,
310                                              GType               itype);
311 const gchar *         g_signal_name         (guint               signal_id);
312 void                  g_signal_query        (guint               signal_id,
313                                              GSignalQuery       *query);
314 guint*                g_signal_list_ids     (GType               itype,
315                                              guint              *n_ids);
316 gboolean              g_signal_parse_name   (const gchar        *detailed_signal,
317                                              GType               itype,
318                                              guint              *signal_id_p,
319                                              GQuark             *detail_p,
320                                              gboolean            force_detail_quark);
321 GSignalInvocationHint* g_signal_get_invocation_hint (gpointer    instance);
322
323
324 /* --- signal emissions --- */
325 void    g_signal_stop_emission              (gpointer             instance,
326                                              guint                signal_id,
327                                              GQuark               detail);
328 void    g_signal_stop_emission_by_name      (gpointer             instance,
329                                              const gchar         *detailed_signal);
330 gulong  g_signal_add_emission_hook          (guint                signal_id,
331                                              GQuark               detail,
332                                              GSignalEmissionHook  hook_func,
333                                              gpointer             hook_data,
334                                              GDestroyNotify       data_destroy);
335 void    g_signal_remove_emission_hook       (guint                signal_id,
336                                              gulong               hook_id);
337
338
339 /* --- signal handlers --- */
340 gboolean g_signal_has_handler_pending         (gpointer           instance,
341                                                guint              signal_id,
342                                                GQuark             detail,
343                                                gboolean           may_be_blocked);
344 gulong   g_signal_connect_closure_by_id       (gpointer           instance,
345                                                guint              signal_id,
346                                                GQuark             detail,
347                                                GClosure          *closure,
348                                                gboolean           after);
349 gulong   g_signal_connect_closure             (gpointer           instance,
350                                                const gchar       *detailed_signal,
351                                                GClosure          *closure,
352                                                gboolean           after);
353 gulong   g_signal_connect_data                (gpointer           instance,
354                                                const gchar       *detailed_signal,
355                                                GCallback          c_handler,
356                                                gpointer           data,
357                                                GClosureNotify     destroy_data,
358                                                GConnectFlags      connect_flags);
359 void     g_signal_handler_block               (gpointer           instance,
360                                                gulong             handler_id);
361 void     g_signal_handler_unblock             (gpointer           instance,
362                                                gulong             handler_id);
363 void     g_signal_handler_disconnect          (gpointer           instance,
364                                                gulong             handler_id);
365 gboolean g_signal_handler_is_connected        (gpointer           instance,
366                                                gulong             handler_id);
367 gulong   g_signal_handler_find                (gpointer           instance,
368                                                GSignalMatchType   mask,
369                                                guint              signal_id,
370                                                GQuark             detail,
371                                                GClosure          *closure,
372                                                gpointer           func,
373                                                gpointer           data);
374 guint    g_signal_handlers_block_matched      (gpointer           instance,
375                                                GSignalMatchType   mask,
376                                                guint              signal_id,
377                                                GQuark             detail,
378                                                GClosure          *closure,
379                                                gpointer           func,
380                                                gpointer           data);
381 guint    g_signal_handlers_unblock_matched    (gpointer           instance,
382                                                GSignalMatchType   mask,
383                                                guint              signal_id,
384                                                GQuark             detail,
385                                                GClosure          *closure,
386                                                gpointer           func,
387                                                gpointer           data);
388 guint    g_signal_handlers_disconnect_matched (gpointer           instance,
389                                                GSignalMatchType   mask,
390                                                guint              signal_id,
391                                                GQuark             detail,
392                                                GClosure          *closure,
393                                                gpointer           func,
394                                                gpointer           data);
395
396
397 /* --- overriding and chaining --- */
398 void    g_signal_override_class_closure       (guint              signal_id,
399                                                GType              instance_type,
400                                                GClosure          *class_closure);
401 void    g_signal_override_class_handler       (const gchar       *signal_name,
402                                                GType              instance_type,
403                                                GCallback          class_handler);
404 void    g_signal_chain_from_overridden        (const GValue      *instance_and_params,
405                                                GValue            *return_value);
406 void   g_signal_chain_from_overridden_handler (gpointer           instance,
407                                                ...);
408
409
410 /* --- convenience --- */
411 /**
412  * g_signal_connect:
413  * @instance: the instance to connect to.
414  * @detailed_signal: a string of the form "signal-name::detail".
415  * @c_handler: the #GCallback to connect.
416  * @data: data to pass to @c_handler calls.
417  * 
418  * Connects a #GCallback function to a signal for a particular object.
419  * 
420  * The handler will be called before the default handler of the signal.
421  * 
422  * Returns: the handler id
423  */
424 #define g_signal_connect(instance, detailed_signal, c_handler, data) \
425     g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, (GConnectFlags) 0)
426 /**
427  * g_signal_connect_after:
428  * @instance: the instance to connect to.
429  * @detailed_signal: a string of the form "signal-name::detail".
430  * @c_handler: the #GCallback to connect.
431  * @data: data to pass to @c_handler calls.
432  * 
433  * Connects a #GCallback function to a signal for a particular object.
434  * 
435  * The handler will be called after the default handler of the signal.
436  * 
437  * Returns: the handler id
438  */
439 #define g_signal_connect_after(instance, detailed_signal, c_handler, data) \
440     g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, G_CONNECT_AFTER)
441 /**
442  * g_signal_connect_swapped:
443  * @instance: the instance to connect to.
444  * @detailed_signal: a string of the form "signal-name::detail".
445  * @c_handler: the #GCallback to connect.
446  * @data: data to pass to @c_handler calls.
447  * 
448  * Connects a #GCallback function to a signal for a particular object.
449  * 
450  * The instance on which the signal is emitted and @data will be swapped when 
451  * calling the handler.
452  * 
453  * Returns: the handler id
454  */
455 #define g_signal_connect_swapped(instance, detailed_signal, c_handler, data) \
456     g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, G_CONNECT_SWAPPED)
457 /**
458  * g_signal_handlers_disconnect_by_func:
459  * @instance: The instance to remove handlers from.
460  * @func: The C closure callback of the handlers (useless for non-C closures).
461  * @data: The closure data of the handlers' closures.
462  * 
463  * Disconnects all handlers on an instance that match @func and @data.
464  * 
465  * Returns: The number of handlers that matched.
466  */
467 #define g_signal_handlers_disconnect_by_func(instance, func, data)                                              \
468     g_signal_handlers_disconnect_matched ((instance),                                                           \
469                                           (GSignalMatchType) (G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA),       \
470                                           0, 0, NULL, (func), (data))
471 /**
472  * g_signal_handlers_block_by_func:
473  * @instance: The instance to block handlers from.
474  * @func: The C closure callback of the handlers (useless for non-C closures).
475  * @data: The closure data of the handlers' closures.
476  * 
477  * Blocks all handlers on an instance that match @func and @data.
478  * 
479  * Returns: The number of handlers that matched.
480  */
481 #define g_signal_handlers_block_by_func(instance, func, data)                                                   \
482     g_signal_handlers_block_matched      ((instance),                                                           \
483                                           (GSignalMatchType) (G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA),       \
484                                           0, 0, NULL, (func), (data))
485 /**
486  * g_signal_handlers_unblock_by_func:
487  * @instance: The instance to unblock handlers from.
488  * @func: The C closure callback of the handlers (useless for non-C closures).
489  * @data: The closure data of the handlers' closures.
490  * 
491  * Unblocks all handlers on an instance that match @func and @data.
492  * 
493  * Returns: The number of handlers that matched.
494  */
495 #define g_signal_handlers_unblock_by_func(instance, func, data)                                                 \
496     g_signal_handlers_unblock_matched    ((instance),                                                           \
497                                           (GSignalMatchType) (G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA),       \
498                                           0, 0, NULL, (func), (data))
499
500
501 gboolean g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
502                                             GValue                *return_accu,
503                                             const GValue          *handler_return,
504                                             gpointer               dummy);
505
506 gboolean g_signal_accumulator_first_wins   (GSignalInvocationHint *ihint,
507                                             GValue                *return_accu,
508                                             const GValue          *handler_return,
509                                             gpointer               dummy);
510
511 /*< private >*/
512 void     g_signal_handlers_destroy            (gpointer           instance);
513 void     _g_signals_destroy                   (GType              itype);
514
515 G_END_DECLS
516
517 #endif /* __G_SIGNAL_H__ */