commit a299c44d845108efa5f53e7e0f97c9b113823c8f
parent 2847c27689687694f98564b7f6151eb52684a24e
Author: lash <dev@holbrook.no>
Date: Sun, 24 Mar 2024 21:34:28 +0000
Custom list model implementation with list view connection
Diffstat:
5 files changed, 36 insertions(+), 14 deletions(-)
diff --git a/src/gtk/kee-entry-list.c b/src/gtk/kee-entry-list.c
@@ -15,7 +15,19 @@ struct _KeeEntryList {
G_DEFINE_TYPE(KeeEntryList, kee_entry_list, GTK_TYPE_BOX);
-static void kee_entry_handle_setup(KeeEntryList* o, GObject *item, gpointer user_data) {
+static void kee_entry_handle_setup(KeeEntryList* o, GtkListItem *item) {
+ g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "setup");
+}
+
+static void kee_entry_handle_bind(KeeEntryList* o, GtkListItem *item) {
+ const char *s;
+ GObject *go;
+ GtkWidget *label;
+
+ go = G_OBJECT(gtk_list_item_get_item(item));
+ s = g_object_get_data(go, "foo");
+ label = gtk_label_new(s);
+ gtk_list_item_set_child(item, label);
}
static void kee_entry_list_class_init(KeeEntryListClass *kls) {
@@ -24,6 +36,7 @@ static void kee_entry_list_class_init(KeeEntryListClass *kls) {
static void kee_entry_list_init(KeeEntryList *o) {
o->factory = gtk_signal_list_item_factory_new();
g_signal_connect(o->factory, "setup", G_CALLBACK(kee_entry_handle_setup), NULL);
+ g_signal_connect(o->factory, "bind", G_CALLBACK(kee_entry_handle_bind), NULL);
}
GtkWidget* kee_entry_list_new(GListModel *model) {
@@ -31,7 +44,7 @@ GtkWidget* kee_entry_list_new(GListModel *model) {
GtkSingleSelection *sel;
GtkWidget *view;
- o = g_object_new(KEE_TYPE_ENTRY_LIST, "orientation", GTK_ORIENTATION_VERTICAL, "spacing", 10, NULL);
+ o = g_object_new(KEE_TYPE_ENTRY_LIST, "orientation", GTK_ORIENTATION_VERTICAL, NULL);
sel = gtk_single_selection_new(model);
diff --git a/src/gtk/kee-entry-store.c b/src/gtk/kee-entry-store.c
@@ -13,6 +13,7 @@ struct _KeeEntryStore {
struct db_ctx *db;
};
+
static void kee_entry_store_iface_init(GListModelInterface *ifc);
G_DEFINE_TYPE_WITH_CODE(KeeEntryStore, kee_entry_store, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL, kee_entry_store_iface_init));
@@ -26,7 +27,7 @@ static void kee_entry_store_init(KeeEntryStore *o) {
static GType kee_entry_store_get_item_type(GListModel *list) {
- return GTK_TYPE_WIDGET;
+ return G_TYPE_OBJECT;
}
@@ -36,7 +37,12 @@ static guint kee_entry_store_get_n_items(GListModel *list) {
static gpointer kee_entry_store_get_item(GListModel *list, guint index) {
- return gtk_label_new("foo");
+ GObject *o;
+
+ o = g_object_new(G_TYPE_OBJECT, NULL);
+
+ g_object_set_data(o, "foo", "bar");
+ return o;
}
@@ -51,6 +57,5 @@ KeeEntryStore* kee_entry_store_new(struct db_ctx *db) {
o = g_object_new(KEE_TYPE_ENTRY_STORE, NULL);
o->db = db;
-
return o;
}
diff --git a/src/gtk/main.c b/src/gtk/main.c
@@ -7,6 +7,7 @@
//#include "menu.h"
#include "settings.h"
#include "context.h"
+#include "kee-entry-store.h"
//static void state_log(KeeUicontext *uctx, char state_hint, kee_state_t *new_state, kee_state_t *old_state) {
@@ -17,8 +18,9 @@ static void startup(GtkApplication *app, struct kee_context *ctx) {
// kee_uicontext_scaninit(uctx);
}
-static void activate(GtkApplication *app, struct kee_context *ctx) {
- ui_build(app, ctx);
+static void activate(GtkApplication *app, KeeEntryStore *store) { //struct kee_context *ctx) {
+ //ui_build(app, ctx);
+ ui_build(app, store);
}
static void deactivate(GtkApplication *app, gpointer user_data) {
@@ -30,6 +32,7 @@ int main(int argc, char **argv) {
struct kee_settings settings;
GtkApplication *gapp;
struct kee_context ctx;
+ KeeEntryStore *store;
gtk_init();
gst_init(0, NULL);
@@ -44,9 +47,10 @@ int main(int argc, char **argv) {
return r;
}
db_connect(&ctx.db, "./testdata_mdb");
+ store = kee_entry_store_new(&ctx.db);
g_signal_connect (gapp, "startup", G_CALLBACK (startup), &ctx);
- g_signal_connect (gapp, "activate", G_CALLBACK (activate), &ctx);
+ g_signal_connect (gapp, "activate", G_CALLBACK (activate), store);
g_signal_connect (gapp, "shutdown", G_CALLBACK (deactivate), NULL);
//g_signal_connect (uctx, "state", G_CALLBACK(state_log), NULL);
diff --git a/src/gtk/ui.c b/src/gtk/ui.c
@@ -46,11 +46,11 @@ static void ui_handle_unlock(GtkWidget *widget, KeeMenu *menu) {
//}
-void ui_build(GtkApplication *app, struct kee_context *ctx) {
+//void ui_build(GtkApplication *app, struct kee_context *ctx) {
+void ui_build(GtkApplication *app, KeeEntryStore *store) {
GtkWidget *widget;
KeeMenu *win;
KeeImport *import;
- KeeEntryStore *store;
win = kee_menu_new(app);
@@ -58,8 +58,6 @@ void ui_build(GtkApplication *app, struct kee_context *ctx) {
kee_menu_add(win, "unlock", widget);
g_signal_connect (widget, "unlock", G_CALLBACK(ui_handle_unlock), win);
-// widget = ui_build_view(NULL);
- store = kee_entry_store_new(&ctx->db);
widget = kee_entry_list_new(G_LIST_MODEL(store));
kee_menu_add(win, "view", widget);
diff --git a/src/gtk/ui.h b/src/gtk/ui.h
@@ -2,10 +2,12 @@
#define _UI_H
#include <gtk/gtk.h>
-#include "context.h"
+//#include "context.h"
+#include "kee-entry-store.h"
//void ui_build(GtkApplication *app, KeeUicontext *uctx);
-void ui_build(GtkApplication *app, struct kee_context *ctx);
+//void ui_build(GtkApplication *app, struct kee_context *ctx);
+void ui_build(GtkApplication *app, KeeEntryStore *store);
#endif // _UI_H