guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

G-Golf - Help with ListView needed


From: Michele Lindroos
Subject: G-Golf - Help with ListView needed
Date: Sat, 4 Mar 2023 11:09:14 +0200

Hello Guilers!

I'm trying to learn g-golf. It has been a blast, great technology!
However, the last few days I've tried to get ListView working and now
I'm stuck.

There are plenty of code samples online written in C. I've added at
the bottom of the e-mail a code sample I tried and it works on my
machine as expected. I tried to rewrite the code sample in guile
scheme using g-golf. However, I don't get any elements drawn. Also, I
get no error messages. I don't know where to look for solutions. Have
I failed in translating the C code to scheme? Or is there some other
problem?

Any help is appreciated!

BR,
Michele

$ cat list1.c
#include <gtk/gtk.h>

static void
setup_cb (GtkSignalListItemFactory *self, GtkListItem *listitem,
gpointer user_data) {
  GtkWidget *lb = gtk_label_new (NULL);
  gtk_list_item_set_child (listitem, lb);
  /* Because gtk_list_item_set_child sunk the floating reference of
lb, releasing (unref) isn't necessary for lb. */
}

static void
bind_cb (GtkSignalListItemFactory *self, GtkListItem *listitem,
gpointer user_data) {
  GtkWidget *lb = gtk_list_item_get_child (listitem);
  /* Strobj is owned by the instance. Caller mustn't change or destroy it. */
  GtkStringObject *strobj = gtk_list_item_get_item (listitem);
  /* The string returned by gtk_string_object_get_string is owned by
the instance. */
  gtk_label_set_text (GTK_LABEL (lb), gtk_string_object_get_string (strobj));
}

static void
unbind_cb (GtkSignalListItemFactory *self, GtkListItem *listitem,
gpointer user_data) {
  /* There's nothing to do here. */
}

static void
teardown_cb (GtkSignalListItemFactory *self, GtkListItem *listitem,
gpointer user_data) {
  /* There's nothing to do here. */
  /* GtkListItem instance will be destroyed soon. You don't need to
set the child to NULL. */
}

static void
app_activate (GApplication *application) {
  GtkApplication *app = GTK_APPLICATION (application);
  GtkWidget *win = gtk_application_window_new (app);
  gtk_window_set_default_size (GTK_WINDOW (win), 600, 400);
  GtkWidget *scr = gtk_scrolled_window_new ();
  gtk_window_set_child (GTK_WINDOW (win), scr);

  char *array[] = {
    "one", "two", "three", "four", NULL
  };
  /* sl is owned by ns */
  /* ns and factory are owned by lv. */
  /* Therefore, you don't need to care about their destruction. */
  GtkStringList *sl =  gtk_string_list_new ((const char * const *) array);
  GtkNoSelection *ns =  gtk_no_selection_new (G_LIST_MODEL (sl));

  GtkListItemFactory *factory = gtk_signal_list_item_factory_new ();
  g_signal_connect (factory, "setup", G_CALLBACK (setup_cb), NULL);
  g_signal_connect (factory, "bind", G_CALLBACK (bind_cb), NULL);
  /* The following two lines can be left out. The handlers do nothing. */
  g_signal_connect (factory, "unbind", G_CALLBACK (unbind_cb), NULL);
  g_signal_connect (factory, "teardown", G_CALLBACK (teardown_cb), NULL);

  GtkWidget *lv = gtk_list_view_new (GTK_SELECTION_MODEL (ns), factory);
  gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), lv);
  gtk_window_present (GTK_WINDOW (win));
}

/* ----- main ----- */
#define APPLICATION_ID "com.github.ToshioCP.list1"

int
main (int argc, char **argv) {
  GtkApplication *app;
  int stat;

  app = gtk_application_new (APPLICATION_ID, G_APPLICATION_FLAGS_NONE);

  g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL);

  stat =g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);
  return stat;
}

$ cat gtk-signal-list-item-factory.scm
#! /bin/sh
# -*- mode: scheme; coding: utf-8 -*-
exec guile -e main -s "$0" "$@"
!#

(eval-when (expand load eval)
    (use-modules (oop goops))

    (default-duplicate-binding-handler
        '(merge-generics replace warn-override-core warn last))

    (use-modules (g-golf))

    (g-irepository-require "Gtk" #:version "4.0")

    (for-each (lambda (name)
              (gi-import-by-name "Gdk" name))
        '("Display"))

    (for-each (lambda (name)
              (gi-import-by-name "Gtk" name))
        '("Application"
          "ApplicationWindow"
          "Label"
          "ListView"
          "NoSelection"
          "ScrolledWindow"
          "SignalListItemFactory"
          "StringList"
          "StringObject"))

    (for-each (lambda (name)
              (gi-import-by-name "Gio" name))
        '("ListStore")))

(define (activate app)
  (let* ((display (gdk-display-get-default))
         (window (make <gtk-application-window>
                   #:default-width 600
                   #:default-height 400
                   #:application app))
         (scr (make <gtk-scrolled-window>))

         (array '("one" "two" "three" "four"))

         (sl (make <gtk-string-list>
                #:strings array))
         (ns (make <gtk-no-selection>
                #:model sl))

         (signal-list-item-factory (make <gtk-signal-list-item-factory>))

         (lv (make <gtk-list-view>
                #:model ns
                #:factory signal-list-item-factory)))

    (connect signal-list-item-factory
        'setup
        (lambda (factory item)
            (let* ((lb (make <gtk-label>)))
                (set-child item lb))))
    (connect signal-list-item-factory
        'bind
        (lambda (factory item)
            (let* ((lb (get-child (item)))
                   (obj (get-item (item))))
                (set-label lb (get-string obj)))))

    (set-child window scr)
    (set-child scr lv)
    (show window)))

(define (main args)
  (let ((app (make <gtk-application>
               #:application-id "keele.g-golf.gtk-signal-list-item-factory")))
    (connect app 'activate activate)
    (let ((status (g-application-run app args)))
      (exit status))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]