/* * SPDX-FileCopyrightText: 2017-2022 Bruce Cowan <bruce@bcowan.me.uk> * * SPDX-License-Identifier: GPL-3.0-or-later */ #include <config.h> #include "rugby-app-window.h" #include "rugby-list-store.h" #include "rugby-possibility-widget.h" #include <glib/gi18n.h> struct _RugbyAppWindow { AdwApplicationWindow parent; GtkWidget *scorespin; GtkWidget *menu_button; }; G_DEFINE_TYPE (RugbyAppWindow, rugby_app_window, ADW_TYPE_APPLICATION_WINDOW) static char * item_tooltip_cb (GtkListItem *item) { RugbyPossibility *possibility = gtk_list_item_get_item (item); if (!possibility) return NULL; int tries, utries, kicks; GString *tooltip = g_string_new (NULL); g_object_get (possibility, "tries", &tries, "utries", &utries, "kicks", &kicks, NULL); if (tries > 0 && utries == 0) { g_string_printf (tooltip, ngettext ("%d converted try", "%d converted tries", tries), tries); } else if (utries > 0 && tries == 0) { g_string_printf (tooltip, ngettext ("%d unconverted try", "%d unconverted tries", utries), utries); } else if (tries + utries > 0) { g_string_printf (tooltip, ngettext ("%d try, %d converted", "%d tries, %d converted", tries + utries), tries + utries, tries); } if (kicks > 0) { if (tries > 0 || utries > 0) g_string_append_printf (tooltip, ", "); g_string_append_printf (tooltip, ngettext ("%d kick", "%d kicks", kicks), kicks); } return g_string_free (tooltip, FALSE); } static void activate_score_changed (G_GNUC_UNUSED GSimpleAction *action, GVariant *parameter, gpointer user_data) { RugbyAppWindow *self = RUGBY_APP_WINDOW (user_data); double current_value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (self->scorespin)); const char *direction = g_variant_get_string (parameter, NULL); if (g_strcmp0 (direction, "up") == 0) gtk_spin_button_set_value (GTK_SPIN_BUTTON (self->scorespin), current_value + 1.0); else if (g_strcmp0 (direction, "down") == 0) gtk_spin_button_set_value (GTK_SPIN_BUTTON (self->scorespin), current_value - 1.0); else g_assert_not_reached (); } static void activate_show_primary_menu (G_GNUC_UNUSED GSimpleAction *action, G_GNUC_UNUSED GVariant *parameter, gpointer user_data) { RugbyAppWindow *self = RUGBY_APP_WINDOW (user_data); gtk_menu_button_popup (GTK_MENU_BUTTON (self->menu_button)); } const GActionEntry win_entries[] = { { .name = "score-changed", .activate = activate_score_changed, .parameter_type = "s" }, { .name = "show-primary-menu", .activate = activate_show_primary_menu }, }; static void rugby_app_window_init (RugbyAppWindow *self) { gtk_widget_init_template (GTK_WIDGET (self)); g_action_map_add_action_entries (G_ACTION_MAP (self), win_entries, G_N_ELEMENTS (win_entries), self); } static void rugby_app_window_class_init (RugbyAppWindowClass *klass) { GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); g_type_ensure (RUGBY_TYPE_POSSIBILITY_WIDGET); g_type_ensure (RUGBY_TYPE_LIST_STORE); gtk_widget_class_set_template_from_resource (widget_class, "/uk/me/bcowan/Rugby/window.ui"); gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, scorespin); gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, menu_button); gtk_widget_class_bind_template_callback (widget_class, item_tooltip_cb); } RugbyAppWindow * rugby_app_window_new (GtkApplication *app) { return g_object_new (RUGBY_TYPE_APP_WINDOW, "application", app, NULL); }