Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • SuborbitalPigeon/rugby
  • webpigeon/rugby
2 results
Show changes
Showing
with 1169 additions and 629 deletions
<?xml version="1.0" encoding="UTF-8"?>
<!--
SPDX-FileCopyrightText: 2012-2023 Bruce Cowan <bruce@bcowan.me.uk>
SPDX-License-Identifier: CC0-1.0
-->
<gresources>
<gresource prefix="/uk/me/bcowan/Rugby">
<file compressed="true" preprocess="xml-stripblanks">gtk/help-overlay.ui</file>
<file compressed="true" preprocess="xml-stripblanks">gtk/prefs.ui</file>
<file compressed="true" preprocess="xml-stripblanks">gtk/window.ui</file>
<file compressed="true">style.css</file>
<file compressed="true" preprocess="xml-stripblanks">uk.me.bcowan.Rugby.metainfo.xml</file>
</gresource>
<gresource prefix="/uk/me/bcowan/Rugby/icons/scalable/actions/">
<file compressed="true" preprocess="xml-stripblanks" alias="football-american-symbolic.svg">icons/football-american-symbolic.svg</file>
<file compressed="true" preprocess="xml-stripblanks" alias="funnel-symbolic.svg">icons/funnel-symbolic.svg</file>
<file compressed="true" preprocess="xml-stripblanks" alias="list-symbolic.svg">icons/list-symbolic.svg</file>
</gresource>
</gresources>
<?xml version="1.0" encoding="UTF-8"?>
<!--
SPDX-FileCopyrightText: 2020-2023 Bruce Cowan <bruce@bcowan.me.uk>
SPDX-License-Identifier: GPL-3.0-or-later
-->
<schemalist>
<schema id="uk.me.bcowan.Rugby" path="/uk/me/bcowan/Rugby/">
<key name="try-points" type="i">
<default>7</default>
<summary>Try points</summary>
<description>Number of points for a try</description>
</key>
<key name="utry-points" type="i">
<default>5</default>
<summary>Unconverted try points</summary>
<description>Number of points for an unconverted try</description>
</key>
<key name="kick-points" type="i">
<default>3</default>
<summary>Kick points</summary>
<description>Number of points for a kick (drop goal or penalty)</description>
</key>
</schema>
<schema id="uk.me.bcowan.Rugby.window" path="/uk/me/bcowan/Rugby/window/">
<key name="height" type="i">
<default>600</default>
<summary>Window height</summary>
<description>The height of the window</description>
</key>
<key name="width" type="i">
<default>400</default>
<summary>Window width</summary>
<description>The width of the window</description>
</key>
</schema>
</schemalist>
<?xml version="1.0" encoding="UTF-8"?>
<!--
SPDX-FileCopyrightText: 2023-2024 Bruce Cowan
SPDX-License-Identifier: CC0-1.0
-->
<component type="desktop-application">
<id>uk.me.bcowan.Rugby</id>
<name>Rugby</name>
<developer_name>Bruce Cowan</developer_name>
<summary>Find possibilities for rugby scores</summary>
<metadata_license>CC0-1.0</metadata_license>
<project_license>GPL-3.0-or-later</project_license>
<supports>
<control>pointing</control>
<control>keyboard</control>
<control>touch</control>
</supports>
<description>
<p>
A program to determine possible combinations of scoring for rugby matches.
</p>
</description>
<launchable type="desktop-id">uk.me.bcowan.Rugby.desktop</launchable>
<icon type="stock">rugby</icon>
<categories>
<category>Utility</category>
<category>GNOME</category>
</categories>
<provides>
<binary>rugby</binary>
</provides>
</component>
project('rugby', 'c', license:'LGPL')
# SPDX-FileCopyrightText: 2016-2024 Bruce Cowan <bruce@bcowan.me.uk>
#
# SPDX-License-Identifier: GPL-3.0-or-later
add_project_arguments('-Wdeprecated-declarations', language: 'c')
add_project_arguments('-DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_32', language: 'c')
add_project_arguments('-DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_32', language: 'c')
add_project_arguments('-DGDK_VERSION_MIN_REQUIRED=GDK_VERSION_3_16', language: 'c')
add_project_arguments('-DGDK_VERSION_MAX_ALLOWED=GDK_VERSION_3_16', language: 'c')
project('rugby', 'c',
version: '0.4.alpha',
license:'GPLv3+',
meson_version: '>= 1.1.0',
default_options: [
'buildtype=debugoptimized',
'c_std=gnu11',
'warning_level=3'
],
)
glib_dep = dependency('glib-2.0', version: '>=2.32')
gtk_dep = dependency('gtk+-3.0', version: '>=3.16')
datadir = get_option('datadir')
desktopdir = datadir / 'applications'
gnome = import('gnome')
gio_dep = dependency('gio-2.0', version: '>= 2.76')
gtk_dep = dependency('gtk4', version: '>= 4.12')
libadwaita_dep = dependency('libadwaita-1', version: '>=1.5.beta')
conf = configuration_data()
profile = get_option('profile')
conf.set_quoted('PROFILE', profile)
if profile == 'development'
conf.set_quoted('VERSION', '@0@-@VCS_TAG@'.format(meson.project_version()))
else
conf.set_quoted('VERSION', meson.project_version())
endif
cflags = ['-DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_70',
'-DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_70',
'-DGDK_VERSION_MIN_REQUIRED=GDK_VERSION_4_12',
'-DGDK_VERSION_MAX_ALLOWED=GDK_VERSION_4_12',
'-Wno-overlength-strings',
]
cc = meson.get_compiler('c')
add_project_arguments(cc.get_supported_arguments(cflags),
language: 'c')
subdir('data')
subdir('src')
gnome.post_install(
glib_compile_schemas: true,
update_desktop_database: true,
)
# SPDX-FileCopyrightText: 2022 Bruce Cowan <bruce@bcowan.me.uk>
#
# SPDX-License-Identifier: CC0-1.0
option(
'profile',
type: 'combo',
choices: ['default', 'development'],
value: 'default',
description: 'What sort of build is this?'
)
/*
* SPDX-FileCopyrightText: 2012-2024 Bruce Cowan <bruce@bcowan.me.uk>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <config.h>
#include "rugby-app-window.h"
#include "rugby-pref-dialog.h"
static void
on_activate ( GApplication *app,
G_GNUC_UNUSED void *user_data)
{
g_assert (GTK_IS_APPLICATION (app));
GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (app));
if (!window)
window = GTK_WINDOW (rugby_app_window_new (GTK_APPLICATION (app)));
if (g_strcmp0 (PROFILE, "development") == 0)
gtk_widget_add_css_class (GTK_WIDGET (window), "devel");
gtk_window_present (GTK_WINDOW (window));
}
static void
about_activated (G_GNUC_UNUSED GSimpleAction *simple,
G_GNUC_UNUSED GVariant *parameter,
void *user_data)
{
GtkApplication *app = GTK_APPLICATION (user_data);
GtkWindow *window = gtk_application_get_active_window (app);
adw_show_about_dialog_from_appdata (GTK_WIDGET (window),
"uk/me/bcowan/Rugby/uk.me.bcowan.Rugby.metainfo.xml",
NULL,
"application-icon", "football-american",
"version", VERSION,
NULL);
}
static void
preferences_activated (G_GNUC_UNUSED GSimpleAction *simple,
G_GNUC_UNUSED GVariant *parameter,
void *user_data)
{
GtkApplication *app = GTK_APPLICATION (user_data);
GtkWindow *window = gtk_application_get_active_window (app);
RugbyPrefDialog *pref_window = rugby_pref_dialog_new ();
adw_dialog_present (ADW_DIALOG (pref_window), GTK_WIDGET (window));
}
static void
on_startup ( GApplication *app,
G_GNUC_UNUSED void *user_data)
{
const GActionEntry app_entries[] =
{
{ .name = "about", .activate = about_activated },
{ .name = "prefs", .activate = preferences_activated },
};
g_action_map_add_action_entries (G_ACTION_MAP (app),
app_entries,
G_N_ELEMENTS (app_entries),
app);
gtk_application_set_accels_for_action (GTK_APPLICATION (app),
"app.prefs",
(const char*[]) { "<Ctrl>comma", NULL });
}
int
main (int argc,
char **argv)
{
g_autoptr (AdwApplication) app = adw_application_new ("uk.me.bcowan.Rugby",
G_APPLICATION_NON_UNIQUE);
g_signal_connect (app, "startup",
G_CALLBACK (on_startup), NULL);
g_signal_connect (app, "activate",
G_CALLBACK (on_activate), NULL);
return g_application_run (G_APPLICATION (app), argc, argv);
}
gnome = import('gnome')
sources = files(
'rugby.c',
'rugby-application.c',
# SPDX-FileCopyrightText: 2016-2024 Bruce Cowan <bruce@bcowan.me.uk>
#
# SPDX-License-Identifier: GPL-3.0-or-later
sources = [
'main.c',
'rugby-app-window.c',
'rugby-cell-renderer-score.c',
'rugby-score-store.c',
'rugby-scoring.c'
)
'rugby-list-store.c',
'rugby-possibility.c',
'rugby-possibility-widget.c',
'rugby-pref-dialog.c',
]
sources += resources
deps = [glib_dep, gtk_dep]
config_h = vcs_tag(
input: configure_file(
output: 'config.h.in',
configuration: conf,
),
output: 'config.h',
command: ['git', 'rev-parse', '--short', 'HEAD'],
fallback: (profile == 'development') ? 'devel' : '',
)
executable('rugby',
sources,
dependencies: deps,
sources, config_h, resources,
dependencies: [gio_dep, gtk_dep, libadwaita_dep],
install_dir: get_option('bindir'),
install: true)
/* rugby-app-window.c
/*
* SPDX-FileCopyrightText: 2017-2024 Bruce Cowan <bruce@bcowan.me.uk>
*
* 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/>.
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <gtk/gtk.h>
#include "rugby-application.h"
#include "config.h"
#include "rugby-app-window.h"
#include "rugby-cell-renderer-score.h"
#include "rugby-score-store.h"
#include "rugby-scoring.h"
#include "rugby-list-store.h"
#include "rugby-possibility.h"
#include "rugby-possibility-widget.h"
#include <glib/gi18n.h>
struct _RugbyAppWindow
{
GtkApplicationWindow parent;
AdwApplicationWindow parent;
RugbyScoreStore *store;
GtkTreeModel *filter;
GtkWidget *scorespin;
GtkWidget *stack;
GtkWidget *tryfilter;
GtkWidget *kickfilter;
GtkWidget *tryscale;
GtkWidget *kickscale;
GtkWidget *treeview;
GSettings *win_settings;
};
G_DEFINE_TYPE(RugbyAppWindow, rugby_app_window, GTK_TYPE_APPLICATION_WINDOW);
G_DEFINE_FINAL_TYPE (RugbyAppWindow, rugby_app_window, ADW_TYPE_APPLICATION_WINDOW)
static void
scorespin_value_changed_cb (GtkSpinButton *spin,
RugbyAppWindow *self)
list_store_items_changed_cb ( GListModel *model,
G_GNUC_UNUSED unsigned position,
G_GNUC_UNUSED unsigned removed,
G_GNUC_UNUSED unsigned added,
RugbyAppWindow *self)
{
gint score = gtk_spin_button_get_value_as_int (spin);
rugby_score_store_set_score (self->store, score);
gint max_tries = MAX (rugby_scoring_get_max_tries (score), 1.0);
gtk_range_set_range (GTK_RANGE (self->tryscale), 0.0, max_tries);
gint max_kicks = MAX (rugby_scoring_get_max_kicks (score), 1.0);
gtk_range_set_range (GTK_RANGE (self->kickscale), 0.0, max_kicks);
unsigned n_items = g_list_model_get_n_items (model);
if (n_items == 0)
gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "empty_page");
else
gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "list_page");
}
static void
tryfilter_active_cb (GObject *gobject,
GParamSpec *pspec,
RugbyAppWindow *self)
static char *
item_tooltip_cb (GtkListItem *item)
{
gboolean active = gtk_switch_get_active (GTK_SWITCH (gobject));
gtk_widget_set_sensitive (self->tryscale, active);
RugbyPossibility *possibility = gtk_list_item_get_item (item);
if (!possibility)
return NULL;
GString *tooltip = g_string_new (NULL);
int tries = rugby_possibility_get_tries (possibility);
int utries = rugby_possibility_get_utries (possibility);
int kicks = rugby_possibility_get_kicks (possibility);
if (tries > 0)
{
g_string_printf (tooltip, ngettext ("%d converted try",
"%d converted tries",
tries),
tries);
}
if (utries > 0)
{
if (tooltip->len > 0)
g_string_append (tooltip, ", ");
g_string_append_printf (tooltip, ngettext ("%d unconverted try",
"%d unconverted tries",
utries),
utries);
}
if (kicks > 0)
{
if (tooltip->len > 0)
g_string_append (tooltip, ", ");
g_string_append_printf (tooltip, ngettext ("%d kick",
"%d kicks",
kicks),
kicks);
}
return g_string_free (tooltip, FALSE);
}
static void
kickfilter_active_cb (GObject *gobject,
GParamSpec *pspec,
RugbyAppWindow *self)
static char *
header_label_cb (GtkListItem *item)
{
gboolean active = gtk_switch_get_active (GTK_SWITCH (gobject));
gtk_widget_set_sensitive (self->kickscale, active);
RugbyPossibility *possibility = gtk_list_header_get_item (GTK_LIST_HEADER (item));
if (!possibility)
return NULL;
int total_tries = rugby_possibility_total_tries (possibility);
return g_strdup_printf (ngettext ("%d try", "%d tries", total_tries), total_tries);
}
static void
scale_value_changed_cb (GtkRange *range,
RugbyAppWindow *self)
activate_score_changed (G_GNUC_UNUSED GSimpleAction *action,
GVariant *parameter,
void *user_data)
{
gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (self->filter));
}
RugbyAppWindow *self = RUGBY_APP_WINDOW (user_data);
static gboolean
filter_visible_func (GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{
RugbyAppWindow *self = RUGBY_APP_WINDOW (data);
gint tries, utries, kicks;
gint ftries = gtk_range_get_value (GTK_RANGE (self->tryscale));
gint fkicks = gtk_range_get_value (GTK_RANGE (self->kickscale));
gtk_tree_model_get (model, iter,
RUGBY_SCORE_STORE_TRIES, &tries,
RUGBY_SCORE_STORE_UTRIES, &utries,
RUGBY_SCORE_STORE_KICKS, &kicks,
-1);
gboolean try_filter = gtk_switch_get_active (GTK_SWITCH (self->tryfilter));
gboolean kick_filter = gtk_switch_get_active (GTK_SWITCH (self->kickfilter));
gboolean try_equal = ((tries + utries) == ftries);
gboolean kick_equal = (kicks == fkicks);
if (try_filter && kick_filter)
return try_equal && kick_equal;
else if (try_filter)
return try_equal;
else if (kick_filter)
return kick_equal;
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
return TRUE;
g_assert_not_reached ();
}
static void
rugby_app_window_init (RugbyAppWindow *self)
rugby_app_window_dispose (GObject *object)
{
gtk_widget_init_template (GTK_WIDGET (self));
gtk_widget_dispose_template (GTK_WIDGET (object), RUGBY_TYPE_APP_WINDOW);
g_clear_object (&RUGBY_APP_WINDOW (object)->win_settings);
GtkCellRenderer *renderer = rugby_cell_renderer_score_new ();
GtkTreeViewColumn *column =
gtk_tree_view_column_new_with_attributes ("Score", renderer,
"tries", RUGBY_SCORE_STORE_TRIES,
"utries", RUGBY_SCORE_STORE_UTRIES,
"kicks", RUGBY_SCORE_STORE_KICKS,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (self->treeview), column);
// TODO: put this in the builder file instead
self->store = rugby_score_store_new ();
self->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (self->store), NULL);
gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (self->filter),
filter_visible_func, self, NULL);
gtk_tree_view_set_model (GTK_TREE_VIEW (self->treeview), self->filter);
G_OBJECT_CLASS (rugby_app_window_parent_class)->dispose (object);
}
static void
rugby_app_window_dispose (GObject *object)
rugby_app_window_init (RugbyAppWindow *self)
{
RugbyAppWindow *self = RUGBY_APP_WINDOW (object);
g_clear_object (&self->store);
g_clear_object (&self->filter);
gtk_widget_init_template (GTK_WIDGET (self));
G_OBJECT_CLASS (rugby_app_window_parent_class)->dispose (object);
const GActionEntry win_entries[] = {
{ .name = "score-changed", .activate = activate_score_changed, .parameter_type = "s" },
};
g_action_map_add_action_entries (G_ACTION_MAP (self),
win_entries,
G_N_ELEMENTS (win_entries),
self);
self->win_settings = g_settings_new ("uk.me.bcowan.Rugby.window");
g_settings_bind (self->win_settings, "height",
self, "default-height",
G_SETTINGS_BIND_DEFAULT);
g_settings_bind (self->win_settings, "width",
self, "default-width",
G_SETTINGS_BIND_DEFAULT);
}
static void
rugby_app_window_class_init (RugbyAppWindowClass *klass)
{
GObjectClass *obj_class = G_OBJECT_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
obj_class->dispose = rugby_app_window_dispose;
g_type_ensure (RUGBY_TYPE_POSSIBILITY_WIDGET);
g_type_ensure (RUGBY_TYPE_LIST_STORE);
object_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, treeview);
gtk_widget_class_bind_template_callback (widget_class, scorespin_value_changed_cb);
gtk_widget_class_bind_template_callback (widget_class, tryfilter_active_cb);
gtk_widget_class_bind_template_callback (widget_class, kickfilter_active_cb);
gtk_widget_class_bind_template_callback (widget_class, scale_value_changed_cb);
"/uk/me/bcowan/Rugby/gtk/window.ui");
gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, scorespin);
gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, stack);
gtk_widget_class_bind_template_callback (widget_class, header_label_cb);
gtk_widget_class_bind_template_callback (widget_class, item_tooltip_cb);
gtk_widget_class_bind_template_callback (widget_class, list_store_items_changed_cb);
}
RugbyAppWindow *
rugby_app_window_new (RugbyApplication *app)
rugby_app_window_new (GtkApplication *app)
{
return g_object_new (RUGBY_TYPE_APP_WINDOW, "application", app, NULL);
return g_object_new (RUGBY_TYPE_APP_WINDOW,
"application", app,
NULL);
}
/* rugby-app-window.h
/*
* SPDX-FileCopyrightText: 2017-2022 Bruce Cowan <bruce@bcowan.me.uk>
*
* Copyright © 2017 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/>.
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#ifndef RUGBY_APP_WINDOW_H
#define RUGBY_APP_WINDOW_H
#pragma once
#include <gtk/gtk.h>
#include <adwaita.h>
G_BEGIN_DECLS
#define RUGBY_TYPE_APP_WINDOW (rugby_app_window_get_type())
G_DECLARE_FINAL_TYPE (RugbyAppWindow, rugby_app_window, RUGBY, APP_WINDOW, GtkApplicationWindow);
G_DECLARE_FINAL_TYPE (RugbyAppWindow, rugby_app_window, RUGBY, APP_WINDOW, AdwApplicationWindow)
RugbyAppWindow *rugby_app_window_new (RugbyApplication *app);
RugbyAppWindow *rugby_app_window_new (GtkApplication *app);
G_END_DECLS
#endif /* RUGBY_APPLICATION_WINDOW_H */
/* rugby-application.c
*
* Copyright © 2012, 2016, 2017 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 <gtk/gtk.h>
#include "rugby-application.h"
#include "rugby-app-window.h"
struct _RugbyApplication
{
GtkApplication parent;
};
G_DEFINE_TYPE (RugbyApplication, rugby_application, GTK_TYPE_APPLICATION);
static void
rugby_application_activate (GApplication *app)
{
RugbyAppWindow *win = rugby_app_window_new (RUGBY_APPLICATION (app));
gtk_window_present (GTK_WINDOW (win));
}
static void
about_activated (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GtkApplication *app = GTK_APPLICATION (user_data);
GtkWindow *window = gtk_application_get_active_window (app);
const gchar *authors[] = { "Bruce Cowan", NULL };
g_autoptr (GDateTime) date = g_date_time_new_now_local ();
gint year = g_date_time_get_year (date);
g_autofree gchar *copyright = g_strdup_printf ("Copyright © 2012–%d Bruce Cowan",
year);
gtk_show_about_dialog (window,
"logo-icon-name", "face-wink",
"program-name", "Rugby",
"copyright", copyright,
"license-type", GTK_LICENSE_LGPL_3_0,
"authors", authors,
"comments", "Rugby scores possiblities program",
NULL);
}
static void
quit_activated (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
g_application_quit (G_APPLICATION (user_data));
}
static const GActionEntry app_entries[] =
{
{ "about", about_activated, NULL, NULL, NULL },
{ "quit", quit_activated, NULL, NULL, NULL }
};
static void
rugby_application_startup (GApplication *app)
{
G_APPLICATION_CLASS (rugby_application_parent_class)->startup (app);
g_action_map_add_action_entries (G_ACTION_MAP (app),
app_entries, G_N_ELEMENTS (app_entries),
app);
g_autoptr (GtkBuilder) builder = gtk_builder_new_from_resource ("/uk/me/bcowan/rugby/menu.ui");
GMenuModel *app_menu = G_MENU_MODEL (gtk_builder_get_object (builder, "appmenu"));
gtk_application_set_app_menu (GTK_APPLICATION (app), app_menu);
// CSS styling
g_autoptr (GtkCssProvider) provider = gtk_css_provider_new ();
gtk_css_provider_load_from_resource (provider, "/uk/me/bcowan/rugby/rugby.css");
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
static void
rugby_application_class_init (RugbyApplicationClass *klass)
{
GApplicationClass *app_class = G_APPLICATION_CLASS (klass);
app_class->activate = rugby_application_activate;
app_class->startup = rugby_application_startup;
}
static void
rugby_application_init (RugbyApplication *self)
{
}
RugbyApplication *
rugby_application_new (void)
{
return g_object_new (RUGBY_TYPE_APPLICATION,
"application-id", "uk.me.bcowan.rugby",
NULL);
}
/* rugby-application.h
*
* Copyright © 2012, 2016 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/>.
*/
#ifndef __RUGBY_APPLICATION_H__
#define __RUGBY_APPLICATION_H__
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define RUGBY_TYPE_APPLICATION rugby_application_get_type ()
G_DECLARE_FINAL_TYPE (RugbyApplication, rugby_application, RUGBY, APPLICATION, GtkApplication)
RugbyApplication * rugby_application_new (void);
G_END_DECLS
#endif /* __RUGBY_APPLICATION_H__ */
/* rugby-cell-renderer-score.c
*
* Copyright © 2012, 2013, 2016, 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 "rugby-cell-renderer-score.h"
#include "rugby-scoring.h"
enum
{
PROP_0,
PROP_TRIES,
PROP_UTRIES,
PROP_KICKS
};
struct _RugbyCellRendererScore
{
GtkCellRenderer parent_instance;
gint tries;
gint utries;
gint kicks;
};
/* Style classes for the different types of score */
#define RUGBY_STYLE_CLASS_TRY "score-try"
#define RUGBY_STYLE_CLASS_UTRY "score-utry"
#define RUGBY_STYLE_CLASS_KICK "score-kick"
G_DEFINE_TYPE (RugbyCellRendererScore, rugby_cell_renderer_score, GTK_TYPE_CELL_RENDERER)
static void
rugby_cell_renderer_score_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
RugbyCellRendererScore *self = RUGBY_CELL_RENDERER_SCORE (object);
switch (prop_id)
{
case PROP_TRIES:
self->tries = g_value_get_int (value);
break;
case PROP_UTRIES:
self->utries = g_value_get_int (value);
break;
case PROP_KICKS:
self->kicks = g_value_get_int (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
rugby_cell_renderer_score_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
RugbyCellRendererScore *self = RUGBY_CELL_RENDERER_SCORE (object);
switch (prop_id)
{
case PROP_TRIES:
g_value_set_int (value, self->tries);
break;
case PROP_UTRIES:
g_value_set_int (value, self->utries);
break;
case PROP_KICKS:
g_value_set_int (value, self->kicks);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
#define FIXED_WIDTH 400
#define FIXED_HEIGHT 20
static void
rugby_cell_renderer_score_get_preferred_width (GtkCellRenderer *cell,
GtkWidget *widget,
gint *minimum_width,
gint *natural_width)
{
gint xpad;
gint width;
gtk_cell_renderer_get_padding (cell, &xpad, NULL);
width = xpad * 2 + FIXED_WIDTH;
if (minimum_width)
*minimum_width = width;
if (natural_width)
*natural_width = width;
}
static void
rugby_cell_renderer_score_get_preferred_height (GtkCellRenderer *cell,
GtkWidget *widget,
gint *minimum_height,
gint *natural_height)
{
gint ypad;
gint height;
gtk_cell_renderer_get_padding (cell, NULL, &ypad);
height = ypad * 2 + FIXED_HEIGHT;
if (minimum_height)
*minimum_height = height;
if (natural_height)
*natural_height = height;
}
static void
render_bar (GtkStyleContext *context,
cairo_t *cr,
gdouble x,
gdouble y,
gdouble width,
gdouble height,
RugbyScoreType type)
{
gtk_style_context_save (context);
switch (type)
{
case RUGBY_SCORE_TYPE_TRY:
gtk_style_context_add_class (context, RUGBY_STYLE_CLASS_TRY);
break;
case RUGBY_SCORE_TYPE_UTRY:
gtk_style_context_add_class (context, RUGBY_STYLE_CLASS_UTRY);
break;
case RUGBY_SCORE_TYPE_KICK:
gtk_style_context_add_class (context, RUGBY_STYLE_CLASS_KICK);
break;
}
gtk_render_background (context, cr, x, y, width, height);
gtk_style_context_restore (context);
}
static void
rugby_cell_renderer_score_render (GtkCellRenderer *cell,
cairo_t *cr,
GtkWidget *widget,
const GdkRectangle *background_area,
const GdkRectangle *cell_area,
GtkCellRendererState flags)
{
RugbyCellRendererScore *self = RUGBY_CELL_RENDERER_SCORE (cell);
GtkStyleContext *context;
gint xpad, ypad;
gdouble x, y, total_w, w, h;
GtkBorder border;
gint tries, utries, kicks;
gint total;
gint i;
context = gtk_widget_get_style_context (widget);
gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
x = cell_area->x + xpad;
y = cell_area->y + ypad;
total_w = cell_area->width - xpad * 2;
h = cell_area->height - ypad * 2;
gtk_style_context_save (context);
gtk_style_context_add_class (context, "level-cell");
gtk_render_background (context, cr, x, y, total_w, h);
gtk_render_frame (context, cr, x, y, total_w, h);
tries = self->tries;
utries = self->utries;
kicks = self->kicks;
total = tries * TRY_POINTS + utries * UTRY_POINTS + kicks * KICK_POINTS;
gtk_style_context_get_border (context, GTK_STATE_FLAG_NORMAL, &border);
x += border.left;
y += border.top;
total_w -= border.left + border.right;
h -= border.top + border.bottom;
gtk_style_context_add_class (context, "fill-block");
w = total_w / (total / 7.0);
for (i = 0; i < tries; i++)
{
render_bar (context, cr, x, y, w, h, RUGBY_SCORE_TYPE_TRY);
x += w;
}
w = total_w / (total / 5.0);
for (i = 0; i < utries; i++)
{
render_bar (context, cr, x, y, w, h, RUGBY_SCORE_TYPE_UTRY);
x += w;
}
w = total_w / (total / 3.0);
for (i = 0; i < kicks; i++)
{
render_bar (context, cr, x, y, w, h, RUGBY_SCORE_TYPE_KICK);
x += w;
}
gtk_style_context_restore (context);
}
static void
rugby_cell_renderer_score_class_init (RugbyCellRendererScoreClass *klass)
{
GObjectClass *obj_class = G_OBJECT_CLASS (klass);
GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
obj_class->get_property = rugby_cell_renderer_score_get_property;
obj_class->set_property = rugby_cell_renderer_score_set_property;
cell_class->get_preferred_width = rugby_cell_renderer_score_get_preferred_width;
cell_class->get_preferred_height = rugby_cell_renderer_score_get_preferred_height;
cell_class->render = rugby_cell_renderer_score_render;
g_object_class_install_property (obj_class, PROP_TRIES,
g_param_spec_int ("tries", "Tries", "Number of tries",
0, 50, 0,
G_PARAM_READWRITE));
g_object_class_install_property (obj_class, PROP_UTRIES,
g_param_spec_int ("utries", "Unconverted Tries", "Number of Unconverted Tries",
0, 50, 0,
G_PARAM_READWRITE));
g_object_class_install_property (obj_class, PROP_KICKS,
g_param_spec_int ("kicks", "Kicks", "Number of kicks",
0, 50, 0,
G_PARAM_READWRITE));
}
static void
rugby_cell_renderer_score_init (RugbyCellRendererScore *self)
{
self->tries = 0;
self->utries = 0;
self->kicks = 0;
gtk_cell_renderer_set_padding (GTK_CELL_RENDERER (self), 2, 2);
}
GtkCellRenderer *
rugby_cell_renderer_score_new (void)
{
return g_object_new (RUGBY_TYPE_CELL_RENDERER_SCORE, NULL);
}
/* rugby-cell-renderer-score.h
*
* Copyright © 2012, 2016 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/>.
*/
#ifndef __RUGBY_CELL_RENDERER_SCORE__
#define __RUGBY_CELL_RENDERER_SCORE__
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define RUGBY_TYPE_CELL_RENDERER_SCORE rugby_cell_renderer_score_get_type ()
G_DECLARE_FINAL_TYPE (RugbyCellRendererScore, rugby_cell_renderer_score, RUGBY, CELL_RENDERER_SCORE, GtkCellRenderer)
GtkCellRenderer * rugby_cell_renderer_score_new (void);
G_END_DECLS
#endif /* __RUGBY_CELL_RENDERER_SCORE__ */
/*
* SPDX-FileCopyrightText: 2018-2024 Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "rugby-list-store.h"
#include "rugby-possibility.h"
#include <gio/gio.h>
#include <gtk/gtk.h>
struct _RugbyListStore
{
GObject parent_instance;
int score;
GListStore *items;
GSettings *settings;
};
static void rugby_list_store_list_model_iface_init (GListModelInterface *iface);
static void rugby_list_store_section_model_iface_init (GtkSectionModelInterface *iface);
G_DEFINE_FINAL_TYPE_WITH_CODE (RugbyListStore, rugby_list_store, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
rugby_list_store_list_model_iface_init)
G_IMPLEMENT_INTERFACE (GTK_TYPE_SECTION_MODEL,
rugby_list_store_section_model_iface_init))
enum
{
PROP_SCORE = 1,
N_PROPS
};
static GParamSpec *properties[N_PROPS];
// Helper functions
static int
sort_func ( gconstpointer a,
gconstpointer b,
G_GNUC_UNUSED void *user_data)
{
int atries = rugby_possibility_get_tries ((void *) a);
int autries = rugby_possibility_get_utries ((void *) a);
int btries = rugby_possibility_get_tries ((void *) b);
int butries = rugby_possibility_get_utries ((void *) b);
int trydiff = (btries + butries) - (atries + autries);
// Sort by total tries first, then converted
if (trydiff != 0)
return trydiff;
else
return btries - atries;
}
static void
process_data (RugbyListStore *self)
{
int try_points = g_settings_get_int (self->settings, "try-points");
int utry_points = g_settings_get_int (self->settings, "utry-points");
int kick_points = g_settings_get_int (self->settings, "kick-points");
unsigned old_length = g_list_model_get_n_items (G_LIST_MODEL (self->items));
g_list_store_remove_all (self->items);
if (self->score == 0)
{
g_list_model_items_changed (G_LIST_MODEL (self), 0, old_length, 0);
return;
}
int max_tries = self->score / try_points;
int max_utries = self->score / utry_points;
for (int tries = 0; tries <= max_tries; tries++)
{
for (int utries = 0; utries <= max_utries; utries++)
{
int left = self->score - (tries * try_points) - (utries * utry_points);
if (left < 0)
break;
if (left % kick_points == 0)
{
int kicks = left / kick_points;
RugbyPossibility *possibility = rugby_possibility_new (tries,
utries,
kicks);
g_list_store_append (self->items, possibility);
g_object_unref (possibility);
}
}
}
g_list_store_sort (self->items, sort_func, NULL);
g_list_model_items_changed (G_LIST_MODEL (self), 0, old_length,
g_list_model_get_n_items (G_LIST_MODEL (self->items)));
}
// GListModel implementation
static GType
rugby_list_store_get_item_type (G_GNUC_UNUSED GListModel *list)
{
return RUGBY_TYPE_POSSIBILITY;
}
static unsigned
rugby_list_store_get_n_items (GListModel *list)
{
RugbyListStore *self = RUGBY_LIST_STORE (list);
return g_list_model_get_n_items (G_LIST_MODEL (self->items));
}
static void *
rugby_list_store_get_item (GListModel *list,
unsigned position)
{
RugbyListStore *self = RUGBY_LIST_STORE (list);
return g_list_model_get_item (G_LIST_MODEL (self->items), position);
}
static void
rugby_list_store_list_model_iface_init (GListModelInterface *iface)
{
iface->get_item_type = rugby_list_store_get_item_type;
iface->get_n_items = rugby_list_store_get_n_items;
iface->get_item = rugby_list_store_get_item;
}
// SectionModel implementation
static void
rugby_list_store_get_section (GtkSectionModel *model,
unsigned position,
unsigned *out_start,
unsigned *out_end)
{
RugbyListStore *self = RUGBY_LIST_STORE (model);
unsigned n_items = g_list_model_get_n_items (G_LIST_MODEL (self->items));
RugbyPossibility *possibility = g_list_model_get_item (G_LIST_MODEL (self->items),
position);
if (!possibility)
{
*out_start = n_items;
*out_end = G_MAXUINT;
return;
}
int target_tries = rugby_possibility_total_tries (possibility);
// Find start
for (unsigned i = 0; i < n_items; i++)
{
possibility = g_list_model_get_item (G_LIST_MODEL (self->items), i);
int total = rugby_possibility_total_tries (possibility);
if (total == target_tries)
{
*out_start = i;
break;
}
}
// Find end
for (unsigned i = *out_start + 1; i < n_items; i++)
{
possibility = g_list_model_get_item (G_LIST_MODEL (self->items), i);
int total = rugby_possibility_total_tries (possibility);
if (total != target_tries)
{
*out_end = i;
return;
}
}
*out_end = n_items;
}
static void
rugby_list_store_section_model_iface_init (GtkSectionModelInterface *iface)
{
iface->get_section = rugby_list_store_get_section;
}
// Class functions
static void
rugby_list_store_dispose (GObject *object)
{
RugbyListStore *self = RUGBY_LIST_STORE (object);
g_clear_object (&self->settings);
g_clear_object (&self->items);
G_OBJECT_CLASS (rugby_list_store_parent_class)->dispose (object);
}
static void
rugby_list_store_get_property (GObject *object,
unsigned prop_id,
GValue *value,
GParamSpec *pspec)
{
RugbyListStore *self = RUGBY_LIST_STORE (object);
switch (prop_id)
{
case PROP_SCORE:
g_value_set_int (value, self->score);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
rugby_list_store_set_property (GObject *object,
unsigned prop_id,
const GValue *value,
GParamSpec *pspec)
{
RugbyListStore *self = RUGBY_LIST_STORE (object);
switch (prop_id)
{
int score;
case PROP_SCORE:
score = g_value_get_int (value);
if (score != self->score)
{
self->score = score;
process_data (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SCORE]);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
rugby_list_store_class_init (RugbyListStoreClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = rugby_list_store_dispose;
object_class->get_property = rugby_list_store_get_property;
object_class->set_property = rugby_list_store_set_property;
properties[PROP_SCORE] = g_param_spec_int ("score", "", "",
0, 200, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class,
N_PROPS,
properties);
}
static void
rugby_list_store_init (RugbyListStore *self)
{
self->settings = g_settings_new ("uk.me.bcowan.Rugby");
g_signal_connect_swapped (self->settings, "changed::try-points",
G_CALLBACK (process_data), self);
g_signal_connect_swapped (self->settings, "changed::utry-points",
G_CALLBACK (process_data), self);
g_signal_connect_swapped (self->settings, "changed::kick-points",
G_CALLBACK (process_data), self);
self->score = 0;
self->items = g_list_store_new (RUGBY_TYPE_POSSIBILITY);
}
/*
* SPDX-FileCopyrightText: 2018-2022 Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <glib-object.h>
G_BEGIN_DECLS
#define RUGBY_TYPE_LIST_STORE (rugby_list_store_get_type())
G_DECLARE_FINAL_TYPE (RugbyListStore, rugby_list_store, RUGBY, LIST_STORE, GObject)
G_END_DECLS
/*
* SPDX-FileCopyrightText: 2018-2024 Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "config.h"
#include "rugby-possibility-widget.h"
#include "rugby-possibility.h"
#include <adwaita.h>
struct _RugbyPossibilityWidget
{
GtkWidget parent_instance;
GSettings *settings;
RugbyPossibility *possibility;
};
G_DEFINE_FINAL_TYPE (RugbyPossibilityWidget, rugby_possibility_widget, GTK_TYPE_WIDGET)
enum
{
PROP_POSSIBILITY = 1,
N_PROPS
};
static GParamSpec *properties[N_PROPS];
static void
rugby_possibility_widget_dispose (GObject *object)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object);
g_clear_object (&self->settings);
g_clear_object (&self->possibility);
G_OBJECT_CLASS (rugby_possibility_widget_parent_class)->dispose (object);
}
static void
rugby_possibility_widget_get_property (GObject *object,
unsigned prop_id,
GValue *value,
GParamSpec *pspec)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object);
switch (prop_id)
{
case PROP_POSSIBILITY:
g_value_set_object (value, self->possibility);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
rugby_possibility_widget_set_property (GObject *object,
unsigned prop_id,
const GValue *value,
GParamSpec *pspec)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object);
switch (prop_id)
{
case PROP_POSSIBILITY:
self->possibility = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
render_bar (GtkSnapshot *snapshot,
float x,
float w,
float h,
const GdkRGBA fill,
const GdkRGBA border)
{
graphene_rect_t area = GRAPHENE_RECT_INIT (x, 0.0, w, h);
GskRoundedRect rounded;
gsk_rounded_rect_init_from_rect (&rounded,
&area,
h / 2.0);
gtk_snapshot_push_rounded_clip (snapshot, &rounded);
gtk_snapshot_append_color (snapshot,
&fill,
&area);
gtk_snapshot_pop (snapshot);
gtk_snapshot_append_border (snapshot,
&rounded,
(float[]) { 2.0, 2.0, 2.0, 2.0 },
(GdkRGBA[]) { border,
border,
border,
border });
}
static void
rugby_possibility_widget_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (widget);
int try_points = g_settings_get_int (self->settings, "try-points");
int utry_points = g_settings_get_int (self->settings, "utry-points");
int kick_points = g_settings_get_int (self->settings, "kick-points");
int width = gtk_widget_get_width (widget);
int height = gtk_widget_get_height (widget);
float x = 0.0;
int tries = rugby_possibility_get_tries (self->possibility);
int utries = rugby_possibility_get_utries (self->possibility);
int kicks = rugby_possibility_get_kicks (self->possibility);
int score = tries * try_points + utries * utry_points + kicks * kick_points;
GdkRGBA fill;
GdkRGBA border;
AdwStyleManager *manager = adw_style_manager_get_default ();
if (adw_style_manager_get_dark (manager))
gdk_rgba_parse (&border, "white");
else
gdk_rgba_parse (&border, "black");
// Tries
float w = width / (score / (float) try_points);
gdk_rgba_parse (&fill, "#33d17a"); // Green 3
for (int i = 0; i < tries; i++)
{
render_bar (snapshot, x, w, height, fill, border);
x += w;
}
// Unconverted tries
w = width / (score / (float) utry_points);
gdk_rgba_parse (&fill, "#f6d32d"); // Yellow 3
for (int i = 0; i < utries; i++)
{
render_bar (snapshot, x, w, height, fill, border);
x += w;
}
// Kicks
w = width / (score / (float) kick_points);
gdk_rgba_parse (&fill, "#e01b24"); // Red 3
for (int i = 0; i < kicks; i++)
{
render_bar (snapshot, x, w, height, fill, border);
x += w;
}
}
static void
rugby_possibility_widget_class_init (RugbyPossibilityWidgetClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->dispose = rugby_possibility_widget_dispose;
object_class->get_property = rugby_possibility_widget_get_property;
object_class->set_property = rugby_possibility_widget_set_property;
widget_class->snapshot = rugby_possibility_widget_snapshot;
properties[PROP_POSSIBILITY] =
g_param_spec_object ("possibility", "", "",
RUGBY_TYPE_POSSIBILITY,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class,
N_PROPS,
properties);
}
static void
rugby_possibility_widget_init (RugbyPossibilityWidget *self)
{
self->settings = g_settings_new ("uk.me.bcowan.Rugby");
}
/*
* SPDX-FileCopyrightText: 2018-2022 Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define RUGBY_TYPE_POSSIBILITY_WIDGET (rugby_possibility_widget_get_type())
G_DECLARE_FINAL_TYPE (RugbyPossibilityWidget, rugby_possibility_widget, RUGBY, POSSIBILITY_WIDGET, GtkWidget)
G_END_DECLS
/*
* SPDX-FileCopyrightText: 2018-2024 Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <config.h>
#include "rugby-possibility.h"
struct _RugbyPossibility
{
GObject parent_instance;
int tries;
int utries;
int kicks;
};
G_DEFINE_FINAL_TYPE (RugbyPossibility, rugby_possibility, G_TYPE_OBJECT)
enum
{
PROP_TRIES = 1,
PROP_UTRIES,
PROP_KICKS,
N_PROPS
};
static GParamSpec *properties[N_PROPS];
static void
rugby_possibility_get_property (GObject *object,
unsigned prop_id,
GValue *value,
GParamSpec *pspec)
{
RugbyPossibility *self = RUGBY_POSSIBILITY (object);
switch (prop_id)
{
case PROP_TRIES:
g_value_set_int (value, rugby_possibility_get_tries (self));
break;
case PROP_UTRIES:
g_value_set_int (value, rugby_possibility_get_utries (self));
break;
case PROP_KICKS:
g_value_set_int (value, rugby_possibility_get_kicks (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
rugby_possibility_set_property (GObject *object,
unsigned prop_id,
const GValue *value,
GParamSpec *pspec)
{
RugbyPossibility *self = RUGBY_POSSIBILITY (object);
switch (prop_id)
{
case PROP_TRIES:
self->tries = g_value_get_int (value);
break;
case PROP_UTRIES:
self->utries = g_value_get_int (value);
break;
case PROP_KICKS:
self->kicks = g_value_get_int (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
rugby_possibility_class_init (RugbyPossibilityClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->get_property = rugby_possibility_get_property;
object_class->set_property = rugby_possibility_set_property;
properties[PROP_TRIES] =
g_param_spec_int ("tries", "", "",
0, 200, 0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
properties[PROP_UTRIES] =
g_param_spec_int ("utries", "", "",
0, 200, 0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
properties[PROP_KICKS] =
g_param_spec_int ("kicks", "", "",
0, 200, 0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class,
N_PROPS,
properties);
}
static void
rugby_possibility_init (G_GNUC_UNUSED RugbyPossibility *self)
{
}
RugbyPossibility *
rugby_possibility_new (int tries,
int utries,
int kicks)
{
return g_object_new (RUGBY_TYPE_POSSIBILITY,
"tries", tries,
"utries", utries,
"kicks", kicks,
NULL);
}
int
rugby_possibility_get_tries (RugbyPossibility *self)
{
g_assert (RUGBY_IS_POSSIBILITY (self));
return self->tries;
}
int
rugby_possibility_get_utries (RugbyPossibility *self)
{
g_assert (RUGBY_IS_POSSIBILITY (self));
return self->utries;
}
int
rugby_possibility_get_kicks (RugbyPossibility *self)
{
g_assert (RUGBY_IS_POSSIBILITY (self));
return self->kicks;
}
int
rugby_possibility_total_tries (RugbyPossibility *self)
{
g_assert (RUGBY_IS_POSSIBILITY (self));
return self->tries + self->utries;
}
/*
* SPDX-FileCopyrightText: 2018-2024 Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <glib-object.h>
G_BEGIN_DECLS
#define RUGBY_TYPE_POSSIBILITY (rugby_possibility_get_type())
G_DECLARE_FINAL_TYPE (RugbyPossibility, rugby_possibility, RUGBY, POSSIBILITY, GObject)
RugbyPossibility *rugby_possibility_new (int tries,
int utries,
int kicks);
int rugby_possibility_get_tries (RugbyPossibility *self);
int rugby_possibility_get_utries (RugbyPossibility *self);
int rugby_possibility_get_kicks (RugbyPossibility *self);
int rugby_possibility_total_tries (RugbyPossibility *self);
G_END_DECLS
/*
* SPDX-FileCopyrightText: 2020-2021 Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "rugby-pref-dialog.h"
struct _RugbyPrefDialog
{
AdwPreferencesDialog parent_instance;
GtkWidget *try_spin;
GtkWidget *utry_spin;
GtkWidget *kick_spin;
GSettings *settings;
};
G_DEFINE_FINAL_TYPE (RugbyPrefDialog, rugby_pref_dialog, ADW_TYPE_PREFERENCES_DIALOG)
static void
rugby_pref_dialog_dispose (GObject *object)
{
RugbyPrefDialog *self = RUGBY_PREF_DIALOG (object);
g_clear_object (&self->settings);
gtk_widget_dispose_template (GTK_WIDGET (object), RUGBY_TYPE_PREF_DIALOG);
G_OBJECT_CLASS (rugby_pref_dialog_parent_class)->dispose (object);
}
static void
rugby_pref_dialog_class_init (RugbyPrefDialogClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->dispose = rugby_pref_dialog_dispose;
gtk_widget_class_set_template_from_resource (widget_class,
"/uk/me/bcowan/Rugby/gtk/prefs.ui");
gtk_widget_class_bind_template_child (widget_class, RugbyPrefDialog, try_spin);
gtk_widget_class_bind_template_child (widget_class, RugbyPrefDialog, utry_spin);
gtk_widget_class_bind_template_child (widget_class, RugbyPrefDialog, kick_spin);
}
static void
rugby_pref_dialog_init (RugbyPrefDialog *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
self->settings = g_settings_new ("uk.me.bcowan.Rugby");
g_settings_bind (self->settings, "try-points",
self->try_spin, "value",
G_SETTINGS_BIND_DEFAULT);
g_settings_bind (self->settings, "utry-points",
self->utry_spin, "value",
G_SETTINGS_BIND_DEFAULT);
g_settings_bind (self->settings, "kick-points",
self->kick_spin, "value",
G_SETTINGS_BIND_DEFAULT);
}
RugbyPrefDialog *
rugby_pref_dialog_new (void)
{
return g_object_new (RUGBY_TYPE_PREF_DIALOG,
NULL);
}