Commit 6fbbfad40efa9391d189fd9c0660f0681c765a42

Thomas de Grivel 2024-12-18T07:29:31

refactor g_signal_connect

diff --git a/gtk4/kc3_glib.c b/gtk4/kc3_glib.c
index f4bbb75..3865f39 100644
--- a/gtk4/kc3_glib.c
+++ b/gtk4/kc3_glib.c
@@ -15,8 +15,10 @@
 
 bool g_kc3_g_main_stop = false;
 
+/*
 static void kc3_g_signal_callback (GObject *object,
                                    s_callable *callback);
+*/
 
 bool * kc3_g_main_context_iteration (bool *dest)
 {
@@ -30,6 +32,7 @@ void kc3_g_main (void)
     g_main_context_iteration(NULL, TRUE);
 }
 
+/*
 static void kc3_g_signal_callback (GObject *object,
                                    s_callable *callback)
 {
@@ -52,3 +55,4 @@ void kc3_g_signal_connect (GObject **instance, const s_str *signal,
 		   G_CALLBACK(kc3_g_signal_callback),
 		   *callback);
 }
+*/
diff --git a/gtk4/signal.c b/gtk4/signal.c
new file mode 100644
index 0000000..eb9a7ba
--- /dev/null
+++ b/gtk4/signal.c
@@ -0,0 +1,85 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#include <libkc3/assert.h>
+#include <libkc3/callable.h>
+#include <libkc3/eval.h>
+#include <libkc3/list.h>
+#include <libkc3/tag.h>
+#include <libkc3/tuple.h>
+#include "signal.h"
+
+void kc3_gtk4_signal_callback (GObject *object, s_tuple *tuple)
+{
+  s_list *args = NULL;
+  s_callable *callable;
+  s_tag tag;
+  s_list *tmp;
+  if (tuple->count != 2 ||
+      tuple->tag[0].type != TAG_CALLABLE) {
+    err_puts("kc3_gtk4_signal_callback: invalid tuple");
+    assert(! "kc3_gtk4_signal_callback: invalid tuple");
+    abort();
+  }
+  callable = tuple->tag->data.callable;
+  if (! callable ||
+      callable->type == CALLABLE_VOID) {
+    err_puts("kc3_gtk4_signal_callback: invalid callable");
+    assert(! "kc3_gtk4_signal_callback: invalid callable");
+    abort();
+  }
+  if (! (tmp = list_new_tag_copy(tuple->tag + 1, NULL))) {
+    err_puts("kc3_gtk4_signal_callback: list_new_tag_copy");
+    assert(! "kc3_gtk4_signal_callback: list_new_tag_copy");
+    abort();
+  }
+  args = tmp;
+  if (! (tmp = list_new_ptr(object, args))) {
+    err_puts("kc3_gtk4_signal_callback: list_new_tag_copy");
+    assert(! "kc3_gtk4_signal_callback: list_new_tag_copy");
+    abort();
+  }
+  args = tmp;  
+  if (! eval_callable_call(callable, args, &tag)) {
+    err_puts("kc3_gtk4_signal_callback: eval_callable_call");
+    assert(! "kc3_gtk4_signal_callback: eval_callable_call");
+    abort();
+  }
+  tag_clean(&tag);
+  list_delete_all(args);
+}
+
+void kc3_gtk4_signal_connect (GObject **object,
+                              const s_str *detailed_signal,
+                              s_callable **callback,
+                              s_tag *data)
+{
+  s_tuple *tuple;
+  if (! (tuple = tuple_new(2))) {
+    err_puts("kc3_gtk4_signal_connect: tuple_new");
+    assert(! "kc3_gtk4_signal_connect: tuple_new");
+    abort();
+  }
+  tuple->tag->type = TAG_CALLABLE;
+  if (! p_callable_init_copy(&tuple->tag->data.callable, callback)) {
+    err_puts("kc3_gtk4_signal_connect: p_callable_init_copy");
+    assert(! "kc3_gtk4_signal_connect: p_callable_init_copy");
+    abort();
+  }
+  if (! tag_init_copy(tuple->tag + 1, data)) {
+    err_puts("kc3_gtk4_signal_connect: tag_init_copy");
+    assert(! "kc3_gtk4_signal_connect: tag_init_copy");
+    abort();
+  }
+  g_signal_connect(*object, detailed_signal->ptr.pchar,
+                   G_CALLBACK(kc3_gtk4_signal_callback), tuple);
+}
diff --git a/gtk4/signal.h b/gtk4/signal.h
new file mode 100644
index 0000000..4cda4de
--- /dev/null
+++ b/gtk4/signal.h
@@ -0,0 +1,23 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#ifndef KC3_GTK4_SIGNAL_H
+#define KC3_GTK4_SIGNAL_H
+
+#include "types.h"
+
+void kc3_gtk4_signal_connect (GObject **object,
+                              const s_str *detailed_signal,
+                              s_callable **callback,
+                              s_tag *data);
+
+#endif /* KC3_GTK4_SIGNAL_H */
diff --git a/gtk4/simple_action.c b/gtk4/simple_action.c
new file mode 100644
index 0000000..7a919b0
--- /dev/null
+++ b/gtk4/simple_action.c
@@ -0,0 +1,23 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#include "simple_action.h"
+
+GSimpleAction ** kc3_gtk4_simple_action_new (GSimpleAction **dest,
+                                             const s_str *name)
+{
+  GSimpleAction *tmp;
+  if (! (tmp = g_simple_action_new(name->ptr.pchar, NULL)))
+    return NULL;
+  *dest = tmp;
+  return dest;
+}
diff --git a/gtk4/simple_action.h b/gtk4/simple_action.h
new file mode 100644
index 0000000..23700ea
--- /dev/null
+++ b/gtk4/simple_action.h
@@ -0,0 +1,21 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#ifndef KC3_GTK4_SIMPLE_ACTION_H
+#define KC3_GTK4_SIMPLE_ACTION_H
+
+#include "types.h"
+
+GSimpleAction ** kc3_gtk4_simple_action_new (GSimpleAction **dest,
+                                             const s_str *name);
+
+#endif /* KC3_GTK4_SIMPLE_ACTION_H */
diff --git a/lib/kc3/0.1/gtk4.kc3 b/lib/kc3/0.1/gtk4.kc3
index c9999a0..872b547 100644
--- a/lib/kc3/0.1/gtk4.kc3
+++ b/lib/kc3/0.1/gtk4.kc3
@@ -11,7 +11,7 @@ defmodule Gtk4 do
   def main_context_iteration =
     cfn Bool "kc3_g_main_context_iteration" (Result)
 
-  def signal_connect = cfn Void "kc3_g_signal_connect" (Ptr, Str,
-    Callable)
+  def signal_connect = cfn Void "kc3_gtk4_signal_connect" (Ptr, Str,
+    Callable, Tag)
 
 end