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/>.
*/
#include "config.h"
#include "rugby-application.h"
#include "rugby-app-window.h"
#include "rugby-possibility-widget.h"
struct _RugbyAppWindow
{
GtkApplicationWindow parent;
GtkWidget *tryfilter;
GtkWidget *kickfilter;
GtkWidget *tryscale;
GtkWidget *kickscale;
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;
}
static void
scorespin_value_changed_cb (GtkSpinButton *spin,
RugbyAppWindow *self)
{
gint score = gtk_spin_button_get_value_as_int (spin);
gint max_tries = MAX (get_max_tries (score), 1.0);
gtk_range_set_range (GTK_RANGE (self->tryscale), 0.0, max_tries);
gint max_kicks = MAX (get_max_kicks (score), 1.0);
gtk_range_set_range (GTK_RANGE (self->kickscale), 0.0, max_kicks);
}
static void
scale_value_changed_cb (GtkRange *range,
RugbyAppWindow *self)
{
static GtkWidget *
listbox_widget_func (gpointer item,
gpointer user_data)
RugbyPossibility *possibility = RUGBY_POSSIBILITY (item);
return rugby_possibility_widget_new (possibility);
}
static void
rugby_app_window_init (RugbyAppWindow *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
gtk_list_box_bind_model (GTK_LIST_BOX (self->listbox),
G_LIST_MODEL (self->store),
listbox_widget_func,
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);
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
}
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);
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);
}