Skip to content
Snippets Groups Projects
rugby-app-window.c 4.28 KiB
Newer Older
/* rugby-app-window.c
 *
 * Copyright © 2017-2018 Bruce Cowan <bruce@bcowan.eu>
 *
 * This file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

Bruce Cowan's avatar
Bruce Cowan committed
#include <gtk/gtk.h>

Bruce Cowan's avatar
Bruce Cowan committed
#include "rugby-application.h"
#include "rugby-app-window.h"
Bruce Cowan's avatar
Bruce Cowan committed
#include "rugby-list-store.h"
#include "rugby-possibility.h"
#include "rugby-possibility-widget.h"
Bruce Cowan's avatar
Bruce Cowan committed

struct _RugbyAppWindow
{
    GtkApplicationWindow parent;

Bruce Cowan's avatar
Bruce Cowan committed
    RugbyListStore *store;
Bruce Cowan's avatar
Bruce Cowan committed

    GtkWidget *tryfilter;
    GtkWidget *kickfilter;
    GtkWidget *tryscale;
    GtkWidget *kickscale;
    GtkWidget *listbox;
Bruce Cowan's avatar
Bruce Cowan committed
G_DEFINE_TYPE (RugbyAppWindow, rugby_app_window, GTK_TYPE_APPLICATION_WINDOW)
static inline guint
get_max_tries (guint score)
{
    return (score % 5 == 1) ? score / UTRY_POINTS - 1 : score / UTRY_POINTS;
}

static inline guint
get_max_kicks (guint score)
{
    return (score % 3 == 1) ? score / KICK_POINTS : score / KICK_POINTS - 1;
}

Bruce Cowan's avatar
Bruce Cowan committed
static void
scorespin_value_changed_cb (GtkSpinButton  *spin,
                            RugbyAppWindow *self)
{
    gint score = gtk_spin_button_get_value_as_int (spin);
Bruce Cowan's avatar
Bruce Cowan committed
    rugby_list_store_set_score (self->store, score);
    gint max_tries = MAX (get_max_tries (score), 1.0);
Bruce Cowan's avatar
Bruce Cowan committed
    gtk_range_set_range (GTK_RANGE (self->tryscale), 0.0, max_tries);

    gint max_kicks = MAX (get_max_kicks (score), 1.0);
Bruce Cowan's avatar
Bruce Cowan committed
    gtk_range_set_range (GTK_RANGE (self->kickscale), 0.0, max_kicks);
}

static void
scale_value_changed_cb (GtkRange       *range,
                        RugbyAppWindow *self)
{
    // pass
static GtkWidget *
listbox_widget_func (gpointer item,
                     gpointer user_data)
    RugbyPossibility *possibility = RUGBY_POSSIBILITY (item);
    return rugby_possibility_widget_new (possibility);
Bruce Cowan's avatar
Bruce Cowan committed
}

static void
rugby_app_window_init (RugbyAppWindow *self)
{
    gtk_widget_init_template (GTK_WIDGET (self));

Bruce Cowan's avatar
Bruce Cowan committed
    self->store = rugby_list_store_new ();
    gtk_list_box_bind_model (GTK_LIST_BOX (self->listbox),
                             G_LIST_MODEL (self->store),
                             listbox_widget_func,
Bruce Cowan's avatar
Bruce Cowan committed
                             NULL,
                             NULL);
Bruce Cowan's avatar
Bruce Cowan committed

    g_object_bind_property (self->tryfilter, "active",
                            self->tryscale, "sensitive",
                            G_BINDING_DEFAULT);
    g_object_bind_property (self->kickfilter, "active",
                            self->kickscale, "sensitive",
                            G_BINDING_DEFAULT);
Bruce Cowan's avatar
Bruce Cowan committed
}

static void
rugby_app_window_dispose (GObject *object)
{
    RugbyAppWindow *self = RUGBY_APP_WINDOW (object);

    g_clear_object (&self->store);

    G_OBJECT_CLASS (rugby_app_window_parent_class)->dispose (object);
}

static void
rugby_app_window_class_init (RugbyAppWindowClass *klass)
{
    GObjectClass *obj_class = G_OBJECT_CLASS (klass);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

    obj_class->dispose = rugby_app_window_dispose;

    gtk_widget_class_set_template_from_resource (widget_class,
                                                 "/uk/me/bcowan/rugby/interface.ui");
    gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, tryfilter);
    gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, kickfilter);
    gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, tryscale);
    gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, kickscale);
    gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, listbox);
Bruce Cowan's avatar
Bruce Cowan committed

    gtk_widget_class_bind_template_callback (widget_class, scorespin_value_changed_cb);
    gtk_widget_class_bind_template_callback (widget_class, scale_value_changed_cb);
}

RugbyAppWindow *
rugby_app_window_new (RugbyApplication *app)
{
    return g_object_new (RUGBY_TYPE_APP_WINDOW, "application", app, NULL);
}