1
0
mirror of https://gitlab.cvh-server.de/skrause/flashcards.git synced 2025-12-14 06:11:38 +01:00
Files
flashcards/src/flashcardsappwin.c

119 lines
3.1 KiB
C

#include <gtk/gtk.h>
#include <adwaita.h>
#include <glib/gi18n.h>
#include "flashcardsapp.h"
#include "flashcardsappwin.h"
#include "create-category.h"
#include "database.h"
struct _FlashcardsAppWindow
{
AdwApplicationWindow parent;
sqlite3 *db;
GArray *categories;
AdwNavigationSplitView *split_view;
AdwNavigationPage *sidebar;
AdwNavigationPage *content;
AdwDialog *create_category_dialog;
GtkListBox *topics;
};
G_DEFINE_TYPE(FlashcardsAppWindow, flashcards_app_window, ADW_TYPE_APPLICATION_WINDOW);
static void
on_category_selected(GtkListBox *box, GtkListBoxRow *row, gpointer user_data)
{
FlashcardsAppWindow *win;
int id;
if (row == NULL)
{
return;
}
printf("Test\n");
win = user_data;
adw_navigation_split_view_set_show_content(ADW_NAVIGATION_SPLIT_VIEW(win->split_view), TRUE);
id = gtk_list_box_row_get_index(gtk_list_box_get_selected_row(win->topics));
printf("%d\n", id);
}
static void
on_add_category(GtkButton *self,
gpointer user_data)
{
FlashcardsCreateCategoryDialog *dialog;
dialog = flashcards_create_category_dialog_new(user_data);
adw_dialog_present(ADW_DIALOG(dialog), GTK_WIDGET(user_data));
}
void flashcards_app_window_test(FlashcardsAppWindow *win, const gchar *test)
{
GtkWidget *child;
database_save_category(win->db, test);
child = adw_action_row_new();
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(child), test);
gtk_list_box_append(GTK_LIST_BOX(win->topics), child);
}
static void
flashcards_app_window_init(FlashcardsAppWindow *win)
{
GArray *categories;
gtk_widget_init_template(GTK_WIDGET(win));
win->db = database_connect(g_get_user_data_dir());
database_create_tables(win->db);
win->categories = database_load_categories(win->db);
categories = win->categories;
for (int i = 0; i < categories->len; i++)
{
GtkWidget *child;
category c;
c = g_array_index(categories, category, i);
printf("%d: %s\n", c.id, c.name);
child = adw_action_row_new();
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(child), c.name);
gtk_list_box_append(GTK_LIST_BOX(win->topics), child);
}
}
static void
flashcards_app_window_class_init(FlashcardsAppWindowClass *class)
{
gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(class), "/li/sopht/flashcards/window.ui");
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, split_view);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, sidebar);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, content);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), FlashcardsAppWindow, topics);
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_category_selected);
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_add_category);
}
FlashcardsAppWindow *
flashcards_app_window_new(FlashcardsApp *app)
{
return g_object_new(FLASHCARDS_APP_WINDOW_TYPE, "application", app, NULL);
}