GtkClosures.def 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. DEFINITION MODULE GtkClosures ;
  2. (*
  3. m2-GTK4 - per-connection signal closures.
  4. GObject signals carry a single gpointer of user data. This module
  5. allocates a small context for each connection, stores the handler and
  6. the user payload in it, and installs a trampoline as the C callback.
  7. When the connection is removed - explicitly with
  8. g_signal_handler_disconnect(), or implicitly when the GObject is
  9. finalized - the optional destroy procedure runs and the context is
  10. freed. Every connection therefore owns its own state and cleans up
  11. after itself.
  12. Handlers receive exactly the payload passed to Connect*; recover the
  13. typed state with SYSTEM.CAST:
  14. PROCEDURE OnClick (button, user: ADDRESS);
  15. VAR state: State;
  16. BEGIN state := CAST(State, user); ... END OnClick;
  17. ...
  18. Connect2(button, "clicked", OnClick, CAST(ADDRESS, state));
  19. ISO dialect required
  20. --------------------
  21. This module (and therefore the whole build) uses -fiso: CAST comes
  22. from ISO's SYSTEM, and NEW/DISPOSE need ALLOCATE/DEALLOCATE from the
  23. ISO Storage module.
  24. *)
  25. FROM SYSTEM IMPORT ADDRESS;
  26. EXPORT UNQUALIFIED
  27. Handler2, Handler3, DestroyProc,
  28. Connect2, Connect3, Connect2Owned, Connect3Owned;
  29. TYPE
  30. (* void handler(instance, user_data)
  31. e.g. GtkButton "clicked". *)
  32. Handler2 = PROCEDURE (ADDRESS, ADDRESS);
  33. (* void handler(instance, parameter, user_data)
  34. e.g. GAction "activate"/"change-state", GObject "notify". *)
  35. Handler3 = PROCEDURE (ADDRESS, ADDRESS, ADDRESS);
  36. (* void destroy(user_data) - release the payload when the connection
  37. goes away; pass NIL when the payload is not owned. *)
  38. DestroyProc = PROCEDURE (ADDRESS);
  39. (* Connect a 2-argument handler; the payload is not owned. *)
  40. PROCEDURE Connect2 (instance: ADDRESS; signal: ARRAY OF CHAR;
  41. handler: Handler2; user: ADDRESS) : [ LONGCARD ];
  42. (* Connect a 3-argument handler; the payload is not owned. *)
  43. PROCEDURE Connect3 (instance: ADDRESS; signal: ARRAY OF CHAR;
  44. handler: Handler3; user: ADDRESS) : [ LONGCARD ];
  45. (* As Connect2, but destroy(user) runs when the connection is removed. *)
  46. PROCEDURE Connect2Owned (instance: ADDRESS; signal: ARRAY OF CHAR;
  47. handler: Handler2; user: ADDRESS;
  48. destroy: DestroyProc) : [ LONGCARD ];
  49. (* As Connect3, but destroy(user) runs when the connection is removed. *)
  50. PROCEDURE Connect3Owned (instance: ADDRESS; signal: ARRAY OF CHAR;
  51. handler: Handler3; user: ADDRESS;
  52. destroy: DestroyProc) : [ LONGCARD ];
  53. END GtkClosures.