GtkUtils.def 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. DEFINITION MODULE GtkUtils ;
  2. (*
  3. m2-GTK4 - small pure-Modula-2 helpers over the FOR "C" bindings.
  4. CStrToM2 copies a borrowed NUL-terminated C string (ADDRESS) into
  5. a Modula-2 CHAR array. The destination is always
  6. NUL-terminated and the copy is truncated to fit.
  7. StrAppend appends a NUL-terminated string onto the end of another,
  8. truncating to fit and keeping it NUL-terminated.
  9. IntToStr renders an INTEGER as a decimal string.
  10. Connect wraps GObject.g_signal_connect_data() with the defaults
  11. C's g_signal_connect() macro would use, returning the
  12. handler id so it can be disconnected later.
  13. SetAccel installs one keyboard accelerator for a detailed action
  14. name (e.g. SetAccel(app, "app.quit", "<primary>q")).
  15. C wants a NULL-terminated array of strings, which this
  16. builds on the stack and passes to
  17. gtk_application_set_accels_for_action().
  18. Unref releases a GObject reference (NIL-checked).
  19. Free releases memory from g_malloc/g_strdup/... (NIL-checked).
  20. ErrorMessage copies a GError's message into a CHAR array.
  21. ClearError frees a GError and sets the variable to NIL.
  22. Connect is a real procedure (not a C binding): pass the handler as
  23. ADR(proc), e.g.
  24. Connect(button, "clicked", ADR(OnClicked), NIL);
  25. Ownership
  26. ---------
  27. Modula-2 has no destructors, so follow the C transfer rules:
  28. * GtkUtils.Unref (g_object_unref) for GObject handles;
  29. * GtkUtils.Free (g_free) for strings returned by getters
  30. (g_file_get_basename, g_settings_get_string, ...);
  31. * GtkUtils.ClearError for GError values;
  32. * GVariant values from g_variant_get_*/g_action_get_state need
  33. g_variant_unref, while g_variant_new_* are floating.
  34. *)
  35. FROM SYSTEM IMPORT ADDRESS;
  36. FROM GError IMPORT GError;
  37. EXPORT UNQUALIFIED CStrToM2, StrAppend, IntToStr, Connect, SetAccel,
  38. Unref, Free, ErrorMessage, ClearError;
  39. PROCEDURE CStrToM2 (src: ADDRESS; VAR dst: ARRAY OF CHAR);
  40. PROCEDURE StrAppend (VAR dst: ARRAY OF CHAR; src: ARRAY OF CHAR);
  41. PROCEDURE IntToStr (n: INTEGER; VAR dst: ARRAY OF CHAR);
  42. PROCEDURE Connect (instance: ADDRESS; signal: ARRAY OF CHAR;
  43. handler: ADDRESS; data: ADDRESS) : [ LONGCARD ];
  44. PROCEDURE SetAccel (application: ADDRESS; action: ARRAY OF CHAR;
  45. accel: ARRAY OF CHAR);
  46. PROCEDURE Unref (obj: ADDRESS);
  47. PROCEDURE Free (ptr: ADDRESS);
  48. PROCEDURE ErrorMessage (err: GError; VAR dst: ARRAY OF CHAR);
  49. PROCEDURE ClearError (VAR err: GError);
  50. END GtkUtils.