Commit 9b3919db4a036913063cf9a9f6d496167fddb2a9

Thomas de Grivel 2024-12-19T11:42:46

gtk4: fix simple_action

diff --git a/gtk4/label.c b/gtk4/label.c
index e364a92..9d79d4f 100644
--- a/gtk4/label.c
+++ b/gtk4/label.c
@@ -10,15 +10,23 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
+#include <libkc3/assert.h>
 #include "label.h"
 
 GtkWidget ** kc3_gtk4_label_new (GtkWidget **dest, s_str *str)
 {
   GtkWidget *tmp;
+  assert(dest);
+  assert(str);
   if (! (tmp = gtk_label_new(str->ptr.pchar)))
     return NULL;
   *dest = tmp;
   return dest;
 }
 
-
+void kc3_gtk4_label_set_use_markup (GtkLabel **label, bool use_markup)
+{
+  assert(label);
+  assert(*label);
+  gtk_label_set_use_markup(GTK_LABEL(*label), use_markup);
+}
diff --git a/gtk4/label.h b/gtk4/label.h
index 1e11d69..d8ae4a4 100644
--- a/gtk4/label.h
+++ b/gtk4/label.h
@@ -19,5 +19,6 @@
 GtkWidget ** kc3_gtk4_label_new (GtkWidget **dest, s_str *str);
 
 /* Operators. */
+void kc3_gtk4_label_set_use_markup (GtkLabel **label, bool use_markup);
 
 #endif /* KC3_GTK4_LABEL_H */
diff --git a/gtk4/simple_action.c b/gtk4/simple_action.c
index 7a919b0..db18cf6 100644
--- a/gtk4/simple_action.c
+++ b/gtk4/simple_action.c
@@ -10,14 +10,84 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
+#include <libkc3/kc3.h>
 #include "simple_action.h"
 
+static s_action_callback *g_action_callbacks = NULL;
+
+void kc3_gtk4_simple_action_callback (GSimpleAction *action)
+{
+  s_list *args;
+  s_callable *callable;
+  s_action_callback *cb;
+  s_tag tag;
+  s_list *tmp;
+  cb = g_action_callbacks;
+  while (cb && cb->action != action)
+    cb = cb->next;
+  if (! cb) {
+    err_puts("kc3_gtk4_simple_action_callback:"
+             " action not found in callbacks");
+    assert(!("kc3_gtk4_simple_action_callback:"
+             " action not found in callbacks"));
+    abort();
+  }
+  callable = cb->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(&cb->tag, 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(action, 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);
+
+}
+
 GSimpleAction ** kc3_gtk4_simple_action_new (GSimpleAction **dest,
-                                             const s_str *name)
+                                             const s_str *name,
+                                             p_callable *callable,
+                                             s_tag *data)
 {
+  s_action_callback *cb;
   GSimpleAction *tmp;
+  if (! (cb = alloc(sizeof(s_action_callback))))
+    return NULL;
   if (! (tmp = g_simple_action_new(name->ptr.pchar, NULL)))
     return NULL;
+  cb->action = tmp;
+  cb->next = g_action_callbacks;
+  g_action_callbacks = cb;
+  if (! (p_callable_init_copy(&cb->callable, callable))) {
+    err_puts("kc3_gtk4_simple_action_new: p_callable_init_copy");
+    assert(! "kc3_gtk4_simple_action_new: p_callable_init_copy");
+    return NULL;
+  }
+  if (! (tag_init_copy(&cb->tag, data))) {
+    err_puts("kc3_gtk4_simple_action_new: tag_init_copy");
+    assert(! "kc3_gtk4_simple_action_new: tag_init_copy");
+    return NULL;
+  }
+  g_signal_connect(tmp, "activate",
+                   G_CALLBACK(kc3_gtk4_simple_action_callback),
+                   NULL);
   *dest = tmp;
   return dest;
 }
diff --git a/gtk4/simple_action.h b/gtk4/simple_action.h
index 23700ea..adc23ab 100644
--- a/gtk4/simple_action.h
+++ b/gtk4/simple_action.h
@@ -16,6 +16,8 @@
 #include "types.h"
 
 GSimpleAction ** kc3_gtk4_simple_action_new (GSimpleAction **dest,
-                                             const s_str *name);
-
+                                             const s_str *name,
+                                             p_callable *callable,
+                                             s_tag *data);
+  
 #endif /* KC3_GTK4_SIMPLE_ACTION_H */
diff --git a/gtk4/types.h b/gtk4/types.h
index 628537e..ce2d58f 100644
--- a/gtk4/types.h
+++ b/gtk4/types.h
@@ -16,4 +16,15 @@
 #include <gtk/gtk.h>
 #include <libkc3/types.h>
 
+typedef struct action_callback s_action_callback;
+
+/* 1 */
+
+struct action_callback {
+  GSimpleAction *action;
+  s_callable *callable;
+  s_tag tag;
+  s_action_callback *next;
+};
+
 #endif /* KC3_GTK4_TYPES_H */
diff --git a/kmsg/kmsg.kc3 b/kmsg/kmsg.kc3
index 173971d..633583b 100755
--- a/kmsg/kmsg.kc3
+++ b/kmsg/kmsg.kc3
@@ -1,5 +1,4 @@
 #!/usr/bin/env ikc3
-
 defmodule Kmsg do
 
   require Gtk4
@@ -48,7 +47,8 @@ defmodule Kmsg do
     list_box2 = Gtk4.ListBox.new()
     Gtk4.Widget.set_vexpand(list_box2, true)
     labels2 = List.map(List.count(200), fn (x) {
-      label = Gtk4.Label.new("thodg: message")
+      label = Gtk4.Label.new("<b>thodg</b>: Message <i>with <b>markup</b></i> !")
+      Gtk4.Label.set_use_markup(label, true)
       Gtk4.Widget.set_halign(label, :start)
       Gtk4.ListBox.append(list_box2, label)
       label
@@ -67,10 +67,9 @@ defmodule Kmsg do
     button = Gtk4.Button.new_with_label("Send")
     Gtk4.Box.append(box2, button)
     Gtk4.Paned.set_position(paned, 240);
-    connect_action = Gtk4.SimpleAction.new("connect")
-    Gtk4.signal_connect(connect_action, "activate", Kmsg.connect, void)
+    connect_action = Gtk4.SimpleAction.new("connect", Kmsg.connect, void)
     Gtk4.ActionMap.add_action(app, connect_action)
-    Gtk4.Application.set_accel_for_action(app, "connect", "<Control>N");
+    Gtk4.Application.set_accel_for_action(app, "app.connect", "<Control>N");
     menu = Gtk4.Menu.new()
     file_menu = Gtk4.Menu.new()
     item = Gtk4.MenuItem.new("Connect", "app.connect")
@@ -92,5 +91,4 @@ defmodule Kmsg do
   }
 
 end
-
 Kmsg.main()
diff --git a/lib/kc3/0.1/gtk4/label.kc3 b/lib/kc3/0.1/gtk4/label.kc3
index e205733..967549f 100644
--- a/lib/kc3/0.1/gtk4/label.kc3
+++ b/lib/kc3/0.1/gtk4/label.kc3
@@ -3,4 +3,7 @@ defmodule Gtk4.Label do
   # new(orientation, spacing) => widget
   def new = cfn Ptr "kc3_gtk4_label_new" (Result, Str)
 
+  def set_use_markup = cfn Void "kc3_gtk4_label_set_use_markup" (Ptr,
+    Bool)
+
 end
diff --git a/lib/kc3/0.1/gtk4/simple_action.kc3 b/lib/kc3/0.1/gtk4/simple_action.kc3
index cd09079..5477944 100644
--- a/lib/kc3/0.1/gtk4/simple_action.kc3
+++ b/lib/kc3/0.1/gtk4/simple_action.kc3
@@ -1,6 +1,7 @@
 defmodule Gtk4.SimpleAction do
 
-  # new(name, id) -> app
-  def new = cfn Ptr "kc3_gtk4_simple_action_new" (Result, Str)
+  # new(name, cb, data) -> app
+  def new = cfn Ptr "kc3_gtk4_simple_action_new" (Result, Str, Callable,
+    Tag)
 
 end