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