1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 Object methods and callbacks.
7 <!-- ##### SECTION Long_Description ##### -->
9 <title>What are signals?</title>
11 Signals are a way to get notification when something happens
12 and to customize object behavior according to the
14 Every <WordAsWord>signal</WordAsWord> is uniquely identified by a name,
15 "class_name::signal_name", where signal_name might be something like
16 "clicked" and class_name might be "GtkButton". Note that some other class
17 may also define a "clicked" callback, so long as it doesn't derive from
21 When they are created, they are also assigned a unique positive integer,
22 the signal id (1 is the first signal id- 0 is used to flag an error).
23 Each is also tied to an array of types that describes
24 the prototype of the function pointer(s) (handlers) you may
25 connect to the signal. Finally, every signal has
26 a default handler that is given by a function pointer
27 in its class structure: it is run by default whenever the
28 signal is emitted. (It is possible that a signal will
29 be emitted and a user-defined handler will prevent the default handler
33 Signals are used by everyone, but they are only
34 created on a per class basis-- so you should call
35 call gtk_signal_new() unless you are writing
36 a new #GtkObject type. However, if you want to make a new signal
37 for an existing type, you may use gtk_object_class_user_signal_new()
38 to create a signal that doesn't correspond to a class's builtin
43 <title>How are signals used?</title>
45 There are two basic actions in the signal handling game.
46 If you want notification of an event, you must <Emphasis>connect</Emphasis>
47 a function pointer and a data pointer to that signal; the data pointer
48 will be passed as the last argument to the function (so long as you
49 are using the default marshalling functions).
50 You will receive a connection id, a unique positive integer
51 corresponding to that attachment.
54 Functions that want to notify the user of certain actions,
55 <Emphasis>emit</Emphasis> signals.
59 <title>Basic Terminology</title>
64 <listitem><para>A class method, e.g. GtkButton::clicked.
65 More precisely it is a unique class-branch/signal-name pair.
66 This means you may not define a signal handler for a class which
67 derives from GtkButton that is called clicked,
68 but it is okay to share signals names if they are separate in
74 <term>default handler</term>
75 <listitem><para>The object's internal method which is invoked
76 when the signal is emitted.</para>
81 <term>user-defined handler</term>
82 <listitem><para>A function pointer and data connected
83 to a signal (for a particular object).</para>
84 <para>There are really two types: those which are connected
85 normally, and those which are connected by one
86 of the connect_after functions. The connect_after handlers
87 are always run after the default handler.</para>
88 <para>Many toolkits refer to these as <wordasword>callbacks</wordasword>.</para>
94 <listitem><para>the whole process of emitting a signal,
95 including the invocation of all
96 the different handler types mentioned above.</para>
101 <term>signal id</term>
102 <listitem><para>The unique positive (nonzero) integer
103 used to identify a signal. It can be used instead of
104 a name to many functions for a slight performance
110 <term>connection id</term>
111 <listitem><para>The unique positive (nonzero) integer
112 used to identify the connection of a user-defined handler
113 to a signal. Notice that it is allowed to connect the
114 same function-pointer/user-data pair twice, so
115 there is no guarantee that a function-pointer/user-data
116 maps to a unique connection id.
124 <refsect2><title>A brief note on how they work.</title>
126 The functions responsible for translating an array of #GtkArgs
127 to your C compiler's normal semantics are called Marshallers.
128 They are identified by
129 gtk_marshal_return_value__parameter_list()
130 for example a C function returning a gboolean and taking a gint
131 can be invoked by using gtk_marshal_BOOL__INT().
132 Not all possibly combinations of return/params are available,
133 of course, so if you are writing a #GtkObject with parameters
134 you might have to write a marshaller.
138 <!-- ##### SECTION See_Also ##### -->
143 <term>#GtkObject</term>
144 <listitem><para>The base class for things which emit signals.</para></listitem>
150 <!-- ##### MACRO GTK_SIGNAL_OFFSET ##### -->
155 <!-- # Unused Parameters # -->
160 <!-- ##### ENUM GtkSignalRunType ##### -->
162 These configure the signal's emission. They control
163 whether the signal can be emitted recursively on an object
165 whether to run the default method before or after the user-defined handlers.
171 <term>GTK_RUN_FIRST</term>
172 <listitem><para>Run the default handler before the connected user-defined
178 <term>GTK_RUN_LAST</term>
179 <listitem><para>Run the default handler after the connected
180 user-defined handlers.
181 (Handlers registered as "after" always run after the default handler though)
186 <term>GTK_RUN_BOTH</term>
187 <listitem><para>Run the default handler twice,
188 once before the user-defined handlers,
195 <term>GTK_RUN_NO_RECURSE</term>
196 <listitem><para>Whether to prevent a handler or hook
197 from reemitting the signal from within itself.
199 emit the signal while it is running will result in the signal
200 emission being restarted once it is done with the current processing.
203 careful to avoid having two handlers endlessly reemitting signals,
204 gtk_signal_n_emissions() can be helpful.
209 <term>GTK_RUN_ACTION</term>
210 <listitem><para>The signal is an action you can
211 invoke without any particular setup or cleanup.
212 The signal is treated no differently, but some
213 other code can determine if the signal is appropriate to
214 delegate to user control. For example, key binding sets
215 only allow bindings of ACTION signals to keystrokes.
220 <term>GTK_RUN_NO_HOOKS</term>
221 <listitem><para>This prevents the connection of emission hooks
235 <!-- ##### FUNCTION gtk_signal_new ##### -->
237 Create a new signal type. (This is usually done in the
241 @name: the event name for the signal, e.g. "clicked".
242 @signal_flags: a combination of GTK_RUN flags
243 specifying detail of when the default handler is to be invoked.
244 You should at least specify #GTK_RUN_FIRST
246 @object_type: the type of object this signal pertains to.
247 It will also pertain to derivers of this type automatically.
248 @function_offset: How many bytes the function pointer is in
249 the class structure for this type. Used to invoke a class
251 @marshaller: the function to translate between an array
252 of GtkArgs and the native calling convention. Usually they
253 are identified just by the type of arguments they take:
254 for example, gtk_marshal_BOOL__STRING() describes a marshaller
255 which takes a string and returns a boolean value.
256 @return_val: the type of return value, or GTK_TYPE_NONE for a signal
257 without a return value.
259 @Varargs: a list of GTK_TYPE_*, one for each parameter.
260 @Returns: the signal id.
261 <!-- # Unused Parameters # -->
262 @nparams: the number of parameter the handlers may take.
265 <!-- ##### FUNCTION gtk_signal_newv ##### -->
267 Create a new signal type. (This is usually done in a
271 This function take the types as an array, instead of a list
272 following the arguments. Otherwise the same as gtk_signal_new().
275 @name: the name of the signal to create.
276 @signal_flags: see gtk_signal_new().
277 @object_type: the type of GtkObject to associate the signal with.
278 @function_offset: how many bytes the function pointer is in
279 the class structure for this type.
281 @return_val: the type of the return value, or GTK_TYPE_NONE if
282 you don't want a return value.
285 @Returns: the signal id.
286 <!-- # Unused Parameters # -->
287 @nparams: the number of parameters to the user-defined handlers.
288 @params: an array of GtkTypes, describing the prototype to
292 <!-- ##### FUNCTION gtk_signal_lookup ##### -->
294 Given the name of the signal and the type of object it connects
295 to, get the signal's identifying integer. Emitting the signal
296 by number is somewhat faster than using the name each time.
299 It also tries the ancestors of the given type.
302 @name: the signal's name, e.g. clicked.
303 @object_type: the type that the signal operates on, e.g. #GTK_TYPE_BUTTON.
304 @Returns: the signal's identifying number, or 0 if no signal was found.
307 <!-- ##### FUNCTION gtk_signal_name ##### -->
309 Given the signal's identifier, find its name.
312 Two different signals may have the same name, if they have differing types.
315 @signal_id: the signal's identifying number.
316 @Returns: the signal name, or NULL if the signal number was invalid.
319 <!-- ##### FUNCTION gtk_signal_emit ##### -->
321 Emit a signal. This causes the default handler and user-defined
325 Here is what gtk_signal_emit() does:
328 1. Calls the default handler and the user-connected handlers.
329 The default handler will be called first if
330 GTK_RUN_FIRST is set, and last if GTK_RUN_LAST is set.
333 2. Calls all handlers connected with the "after" flag set.
336 @object: the object that emits the signal.
337 @signal_id: the signal identifier.
338 @Varargs: the parameters to the function, followed
339 by a pointer to the return type, if any.
342 <!-- ##### FUNCTION gtk_signal_emit_by_name ##### -->
344 Emit a signal. This causes the default handler and user-connected
348 @object: the object that emits the signal.
349 @name: the name of the signal.
350 @Varargs: the parameters to the function, followed
351 by a pointer to the return type, if any.
354 <!-- ##### FUNCTION gtk_signal_emitv ##### -->
356 Emit a signal. This causes the default handler and user-connected
357 handlers to be run. This differs from gtk_signal_emit() by taking
358 an array of GtkArgs instead of using C's varargs mechanism.
361 @object: the object to emit the signal to.
362 @signal_id: the signal identifier.
364 <!-- # Unused Parameters # -->
365 @params: an array of GtkArgs, one for each parameter,
366 followed by one which is a pointer to the return type.
369 <!-- ##### FUNCTION gtk_signal_emitv_by_name ##### -->
371 Emit a signal by name. This causes the default handler and user-connected
372 handlers to be run. This differs from gtk_signal_emit() by taking
373 an array of GtkArgs instead of using C's varargs mechanism.
376 @object: the object to emit the signal to.
377 @name: the name of the signal.
379 <!-- # Unused Parameters # -->
380 @params: an array of GtkArgs, one for each parameter,
381 followed by one which is a pointer to the return type.
384 <!-- ##### FUNCTION gtk_signal_emit_stop ##### -->
386 This function aborts a signal's current emission.
389 It will prevent the default method from running,
390 if the signal was #GTK_RUN_LAST and you connected
391 normally (i.e. without the "after" flag).
394 It will print a warning if used on a signal which
398 @object: the object whose signal handlers you wish to stop.
399 @signal_id: the signal identifier, as returned by gtk_signal_lookup().
400 <!-- # Unused Parameters # -->
405 <!-- ##### FUNCTION gtk_signal_emit_stop_by_name ##### -->
407 This function aborts a signal's current emission.
409 <para>It is just like
410 gtk_signal_emit_stop()
411 except it will lookup the signal id for you.
414 @object: the object whose signal handlers you wish to stop.
415 @name: the name of the signal you wish to stop.
418 <!-- ##### FUNCTION gtk_signal_connect ##### -->
420 Attach a function pointer and user data to a signal for
424 The GtkSignalFunction takes a <StructName>GtkObject</StructName> as its first parameter.
425 It will be the same object as the one you're connecting
426 the hook to. The func_data will be passed as the last parameter
430 All else being equal, signal handlers are invoked in the order
431 connected (see gtk_signal_emit() for the other details of
432 which order things are called in).
435 Here is how one passes an integer as user data,
436 for when you just want to specify a constant int
437 as parameter to your function:
441 static void button_clicked_int(GtkButton* button, gpointer func_data)
443 g_print("button pressed: %d\n", GPOINTER_TO_INT(func_data));
446 /* By calling this function, you will make the g_print above
447 * execute, printing the number passed as `to_print'. */
448 static void attach_print_signal(GtkButton* button, gint to_print)
450 gtk_signal_connect(GTK_OBJECT(button), "clicked",
451 GTK_SIGNAL_FUNC(button_clicked_int),
452 GINT_TO_POINTER(to_print));
457 @object: the object associated with the signal, e.g. if a button
458 is getting pressed, this is that button.
459 @name: name of the signal.
460 @func: function pointer to attach to the signal.
461 @func_data: value to pass as to your function (through the marshaller).
462 @Returns: the connection id.
463 <!-- # Unused Parameters # -->
470 <!-- ##### FUNCTION gtk_signal_connect_after ##### -->
472 Attach a function pointer and user data to a signal
473 so that this handler will be called after the other handlers.
476 @object: the object associated with the signal.
477 @name: name of the signal.
478 @func: function pointer to attach to the signal.
479 @func_data: value to pass as to your function (through the marshaller).
480 @Returns: the unique identifier for this attachment: the connection id.
481 <!-- # Unused Parameters # -->
488 <!-- ##### FUNCTION gtk_signal_connect_object ##### -->
490 This function is for registering a callback that will
491 call another object's callback. That is,
492 instead of passing the object which is responsible
493 for the event as the first parameter of the callback,
494 it is switched with the user data (so the object which emits
495 the signal will be the last parameter, which is where the
496 user data usually is).
499 This is useful for passing a standard function in as a callback.
500 For example, if you wanted a button's press to gtk_widget_show()
501 some widget, you could write:
505 gtk_signal_connect_object(button, "clicked", gtk_widget_show, window);
509 @object: the object which emits the signal.
510 @name: the name of the signal.
511 @func: the function to callback.
512 @slot_object: the object to pass as the first parameter to func.
513 (Though it pretends to take an object, you can
514 really pass any gpointer as the #slot_object .)
515 @Returns: the connection id.
516 <!-- # Unused Parameters # -->
523 <!-- ##### FUNCTION gtk_signal_connect_object_after ##### -->
525 Attach a signal hook to a signal, passing in an alternate
526 object as the first parameter, and guaranteeing
527 that the default handler and all normal
528 handlers are called first.
531 @object: the object associated with the signal.
532 @name: name of the signal.
533 @func: function pointer to attach to the signal.
534 @slot_object: the object to pass as the first parameter to #func.
535 @Returns: the connection id.
536 <!-- # Unused Parameters # -->
543 <!-- ##### FUNCTION gtk_signal_connect_full ##### -->
545 Attach a function pointer and user data to a signal with
549 @object: the object which emits the signal. For example, a button
550 in the button press signal.
551 @name: the name of the signal.
552 @func: function pointer to attach to the signal.
554 @data: the user data associated with the function.
555 @destroy_func: function to call when this particular hook is
557 @object_signal: whether this is an object signal-- basically an "object
558 signal" is one that wants its user_data and object fields switched,
559 which is useful for calling functions which operate on another
561 @after: whether to invoke the user-defined handler after the signal, or to let
562 the signal's default behavior preside (i.e. depending on #GTK_RUN_FIRST
564 @Returns: the connection id.
565 <!-- # Unused Parameters # -->
566 @marshal: the function marshal, see the gtkmarshall documentation for
567 more details, but briefly: the marshaller is a function which takes
568 the #GtkObject which emits the signal, the user data, the number of the
569 arguments, and the array of arguments. It is responsible for
570 calling the function in the appropriate calling convention.
571 gtk_signal_default_marshaller is usually fine.
572 (This shows up, for example, when worrying about matching
573 c++ or other languages' calling conventions.)
576 <!-- ##### FUNCTION gtk_signal_connect_while_alive ##### -->
578 Attach a function pointer and another GtkObject to a signal.
581 This function takes an object whose "destroy" signal
583 That way, you don't have to clean up the
584 signal handler when you destroy the object.
585 It is a little less efficient though.
588 (Instead you may call gtk_signal_disconnect_by_data(), if you want
589 to explicitly delete all attachments to this object. This
590 is perhaps not recommended since it could be confused
591 with an integer masquerading as a pointer (through GINT_AS_POINTER).)
594 @object: the object that emits the signal.
596 @func: function pointer to attach to the signal.
597 @func_data: pointer to pass to func.
598 @alive_object: object whose death should cause the handler connection
600 <!-- # Unused Parameters # -->
601 @name: name of the signal.
604 <!-- ##### FUNCTION gtk_signal_connect_object_while_alive ##### -->
606 These signal connectors are for signals which refer to objects,
607 so they must not be called after the object is deleted.
610 Unlike gtk_signal_connect_while_alive(),
611 this swaps the object and user data, making it suitable for
612 use with functions which primarily operate on the user data.
615 This function acts just like gtk_signal_connect_object() except
616 it traps the "destroy" signal to prevent you from having to
617 clean up the handler.
620 @object: the object associated with the signal.
622 @func: function pointer to attach to the signal.
623 @alive_object: the user data, which must be an object, whose destruction
624 should signal the removal of this signal.
625 <!-- # Unused Parameters # -->
626 @name: name of the signal.
629 <!-- ##### FUNCTION gtk_signal_disconnect ##### -->
631 Destroy a user-defined handler connection.
634 @object: the object which the handler pertains to.
635 @handler_id: the connection id.
638 <!-- ##### FUNCTION gtk_signal_disconnect_by_func ##### -->
640 Destroy all connections for a particular object, with
641 the given function-pointer and user-data.
644 @object: the object which emits the signal.
645 @func: the function pointer to search for.
646 @data: the user data to search for.
647 <!-- # Unused Parameters # -->
653 <!-- ##### FUNCTION gtk_signal_disconnect_by_data ##### -->
655 Destroy all connections for a particular object, with
659 @object: the object which emits the signal.
660 @data: the user data to search for.
661 <!-- # Unused Parameters # -->
666 <!-- ##### FUNCTION gtk_signal_handler_block ##### -->
668 Prevent an user-defined handler from being invoked. All other
669 signal processing will go on as normal, but this particular
670 handler will ignore it.
673 @object: the object which emits the signal to block.
674 @handler_id: the connection id.
677 <!-- ##### FUNCTION gtk_signal_handler_block_by_func ##### -->
679 Prevent a user-defined handler from being invoked, by reference to
680 the user-defined handler's function pointer and user data. (It may result in
681 multiple hooks being blocked, if you've called connect multiple times.)
684 @object: the object which emits the signal to block.
685 @func: the function pointer of the handler to block.
686 @data: the user data of the handler to block.
687 <!-- # Unused Parameters # -->
693 <!-- ##### FUNCTION gtk_signal_handler_block_by_data ##### -->
695 Prevent all user-defined handlers with a certain user data from being invoked.
698 @object: the object which emits the signal we want to block.
699 @data: the user data of the handlers to block.
700 <!-- # Unused Parameters # -->
705 <!-- ##### FUNCTION gtk_signal_handler_unblock ##### -->
707 Undo a block, by connection id. Note that undoing a block doesn't
708 necessarily make the hook callable, because if you block a
709 hook twice, you must unblock it twice.
712 @object: the object which emits the signal we want to unblock.
713 @handler_id: the emission handler identifier, as returned by
714 gtk_signal_connect(), etc.
717 <!-- ##### FUNCTION gtk_signal_handler_unblock_by_func ##### -->
719 Undo a block, by function pointer and data.
720 Note that undoing a block doesn't
721 necessarily make the hook callable, because if you block a
722 hook twice, you must unblock it twice.
725 @object: the object which emits the signal we want to unblock.
726 @func: the function pointer to search for.
727 @data: the user data to search for.
728 <!-- # Unused Parameters # -->
734 <!-- ##### FUNCTION gtk_signal_handler_unblock_by_data ##### -->
736 Undo block(s), to all signals for a particular object
737 with a particular user-data pointer
740 @object: the object which emits the signal we want to unblock.
741 @data: the user data to search for.
742 <!-- # Unused Parameters # -->
747 <!-- ##### FUNCTION gtk_signal_handler_pending ##### -->
749 Returns a connection id corresponding to a given signal id and object.
752 One example of when you might use this is when the arguments
753 to the signal are difficult to compute. A class implementor
754 may opt to not emit the signal if no one is attached anyway,
755 thus saving the cost of building the arguments.
758 @object: the object to search for the desired user-defined handler.
759 @signal_id: the number of the signal to search for.
760 @may_be_blocked: whether it is acceptable to return a blocked
762 @Returns: the connection id, if a connection was found. 0 otherwise.
763 <!-- # Unused Parameters # -->
769 <!-- ##### FUNCTION gtk_signal_handler_pending_by_func ##### -->
771 Returns a connection id corresponding to a given signal id, object, function
772 pointer and user data.
775 @object: the object to search for the desired handler.
776 @signal_id: the number of the signal to search for.
777 @may_be_blocked: whether it is acceptable to return a blocked
779 @func: the function pointer to search for.
780 @data: the user data to search for.
781 @Returns: the connection id, if a handler was found. 0 otherwise.
782 <!-- # Unused Parameters # -->
790 <!-- ##### MACRO gtk_signal_default_marshaller ##### -->
792 A marshaller that returns void and takes no extra parameters.