Skip to content
Snippets Groups Projects
Commit c61766d8 authored by Bruce Cowan's avatar Bruce Cowan :airplane:
Browse files

Make possibility widget a ListBoxRow

parent 2f73d0da
No related branches found
No related tags found
1 merge request!2Gtk4
Pipeline #2940 passed
# SPDX-FileCopyrightText: 2016-2020 Bruce Cowan <bruce@bcowan.me.uk>
# SPDX-FileCopyrightText: 2016-2021 Bruce Cowan <bruce@bcowan.me.uk>
#
# SPDX-License-Identifier: CC0-1.0
sources = [
......@@ -6,7 +6,7 @@ sources = [
'rugby-app-window.c',
'rugby-list-store.c',
'rugby-possibility.c',
'rugby-possibility-widget.c',
'rugby-possibility-row.c',
'rugby-pref-window.c',
]
......
/*
* SPDX-FileCopyrightText: 2017, 2018 Bruce Cowan <bruce@bcowan.me.uk>
* SPDX-FileCopyrightText: 2017-2021 Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
......@@ -8,7 +9,7 @@
#include "rugby-list-store.h"
#include "rugby-possibility.h"
#include "rugby-possibility-widget.h"
#include "rugby-possibility-row.h"
struct _RugbyAppWindow
{
......@@ -28,7 +29,7 @@ listbox_widget_func ( gpointer item,
{
RugbyPossibility *possibility = RUGBY_POSSIBILITY (item);
return rugby_possibility_widget_new (possibility);
return rugby_possibility_row_new (possibility);
}
static void
......
......@@ -5,19 +5,20 @@
*/
#include "config.h"
#include "rugby-possibility-widget.h"
#include "rugby-possibility-row.h"
#include <glib/gi18n.h>
struct _RugbyPossibilityWidget
struct _RugbyPossibilityRow
{
GtkWidget parent_instance;
GtkListBoxRow parent_instance;
GSettings *settings;
RugbyPossibility *possibility;
};
G_DEFINE_TYPE (RugbyPossibilityWidget, rugby_possibility_widget, GTK_TYPE_WIDGET)
G_DEFINE_TYPE (RugbyPossibilityRow, rugby_possibility_row, GTK_TYPE_LIST_BOX_ROW)
enum
{
......@@ -29,23 +30,23 @@ enum
static GParamSpec *properties[N_PROPS];
static void
rugby_possibility_widget_dispose (GObject *object)
rugby_possibility_row_dispose (GObject *object)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object);
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object);
g_clear_object (&self->settings);
g_clear_object (&self->possibility);
G_OBJECT_CLASS (rugby_possibility_widget_parent_class)->dispose (object);
G_OBJECT_CLASS (rugby_possibility_row_parent_class)->dispose (object);
}
static void
rugby_possibility_widget_get_property (GObject *object,
unsigned prop_id,
GValue *value,
GParamSpec *pspec)
rugby_possibility_row_get_property (GObject *object,
unsigned prop_id,
GValue *value,
GParamSpec *pspec)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object);
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object);
switch (prop_id)
{
......@@ -58,12 +59,12 @@ rugby_possibility_widget_get_property (GObject *object,
}
static void
rugby_possibility_widget_set_property (GObject *object,
unsigned prop_id,
const GValue *value,
GParamSpec *pspec)
rugby_possibility_row_set_property (GObject *object,
unsigned prop_id,
const GValue *value,
GParamSpec *pspec)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object);
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object);
switch (prop_id)
{
......@@ -77,10 +78,10 @@ rugby_possibility_widget_set_property (GObject *object,
static void
render_bar (GtkSnapshot *snapshot,
double x,
double y,
double w,
double h,
float x,
float y,
float w,
float h,
const GdkRGBA color)
{
GskRoundedRect rounded;
......@@ -97,15 +98,15 @@ render_bar (GtkSnapshot *snapshot,
GdkRGBA black = { 0.0, 0.0, 0.0, 0.2 };
gtk_snapshot_append_border (snapshot,
&rounded,
(float[]) { 2.f, 2.f, 2.f, 2.f },
(float[]) { 2.0, 2.0, 2.0, 2.0 },
(GdkRGBA[]) { black, black, black, black });
}
static void
rugby_possibility_widget_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot)
rugby_possibility_row_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (widget);
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (widget);
int try_points = g_settings_get_int (self->settings, "try-points");
int utry_points = g_settings_get_int (self->settings, "utry-points");
......@@ -113,7 +114,7 @@ rugby_possibility_widget_snapshot (GtkWidget *widget,
int width = gtk_widget_get_width (widget);
int height = gtk_widget_get_height (widget);
double x = 0.0;
float x = 0.0;
int tries, utries, kicks;
g_object_get (self->possibility,
......@@ -124,37 +125,41 @@ rugby_possibility_widget_snapshot (GtkWidget *widget,
int score = tries * try_points + utries * utry_points + kicks * kick_points;
// Tries
double w = width / (score / (double) try_points);
float w = width / (score / (float) try_points);
for (int i = 0; i < tries; i++)
{
// Green
render_bar (snapshot, x, 0.0, w, height, (GdkRGBA) { 0.20, 0.82, 0.48, 1.0 });
render_bar (snapshot, x, 0.0, w, height,
(GdkRGBA) { 0.20, 0.82, 0.48, 1.0 });
x += w;
}
// Unconverted tries
w = width / (score / (double) utry_points);
w = width / (score / (float) utry_points);
for (int i = 0; i < utries; i++)
{
// Red
render_bar (snapshot, x, 0.0, w, height, (GdkRGBA) { 0.88, 0.11, 0.14, 1.0 });
render_bar (snapshot, x, 0.0, w, height,
(GdkRGBA) { 0.88, 0.11, 0.14, 1.0 });
x += w;
}
// Unconverted kicks
w = width / (score / (double) kick_points);
w = width / (score / (float) kick_points);
for (int i = 0; i < kicks; i++)
{
// Yellow
render_bar (snapshot, x, 0.0, w, height, (GdkRGBA) { 0.97, 0.83, 0.18, 1.0 });
render_bar (snapshot, x, 0.0, w, height,
(GdkRGBA) { 0.96, 0.83, 0.18, 1.0 });
x += w;
}
}
static void
rugby_possibility_widget_constructed (GObject *obj)
rugby_possibility_row_constructed (GObject *obj)
{
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (obj);
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (obj);
int tries, utries, kicks;
g_autoptr (GString) tooltip = g_string_new (NULL);
......@@ -166,11 +171,16 @@ rugby_possibility_widget_constructed (GObject *obj)
if ((tries + utries) > 0)
{
g_string_append_printf (tooltip,
ngettext ("%d try, %d converted",
"%d tries, %d converted",
tries + utries),
tries + utries, tries);
if (tries == 1 && utries == 0)
g_string_printf (tooltip, "1 converted try");
else if (tries == 0 && utries == 1)
g_string_printf (tooltip, "1 unconverted try");
else
g_string_append_printf (tooltip,
ngettext ("%d try, %d converted",
"%d tries, %d converted",
tries + utries),
tries + utries, tries);
if (kicks > 0)
g_string_append_printf (tooltip,
......@@ -189,21 +199,21 @@ rugby_possibility_widget_constructed (GObject *obj)
gtk_widget_set_tooltip_text (GTK_WIDGET (self), tooltip->str);
G_OBJECT_CLASS (rugby_possibility_widget_parent_class)->constructed (obj);
G_OBJECT_CLASS (rugby_possibility_row_parent_class)->constructed (obj);
}
static void
rugby_possibility_widget_class_init (RugbyPossibilityWidgetClass *klass)
rugby_possibility_row_class_init (RugbyPossibilityRowClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->constructed = rugby_possibility_widget_constructed;
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;
object_class->constructed = rugby_possibility_row_constructed;
object_class->dispose = rugby_possibility_row_dispose;
object_class->get_property = rugby_possibility_row_get_property;
object_class->set_property = rugby_possibility_row_set_property;
widget_class->snapshot = rugby_possibility_widget_snapshot;
widget_class->snapshot = rugby_possibility_row_snapshot;
properties[PROP_POSSIBILITY] =
g_param_spec_object ("possibility",
......@@ -218,15 +228,15 @@ rugby_possibility_widget_class_init (RugbyPossibilityWidgetClass *klass)
}
static void
rugby_possibility_widget_init (RugbyPossibilityWidget *self)
rugby_possibility_row_init (RugbyPossibilityRow *self)
{
self->settings = g_settings_new ("uk.me.bcowan.Rugby");
}
GtkWidget *
rugby_possibility_widget_new (RugbyPossibility *possibility)
rugby_possibility_row_new (RugbyPossibility *possibility)
{
return g_object_new (RUGBY_TYPE_POSSIBILITY_WIDGET,
return g_object_new (RUGBY_TYPE_POSSIBILITY_ROW,
"possibility", possibility,
NULL);
}
......@@ -12,10 +12,10 @@
G_BEGIN_DECLS
#define RUGBY_TYPE_POSSIBILITY_WIDGET (rugby_possibility_widget_get_type())
#define RUGBY_TYPE_POSSIBILITY_ROW (rugby_possibility_row_get_type())
G_DECLARE_FINAL_TYPE (RugbyPossibilityWidget, rugby_possibility_widget, RUGBY, POSSIBILITY_WIDGET, GtkWidget)
G_DECLARE_FINAL_TYPE (RugbyPossibilityRow, rugby_possibility_row, RUGBY, POSSIBILITY_ROW, GtkListBoxRow)
GtkWidget * rugby_possibility_widget_new (RugbyPossibility *possibility);
GtkWidget * rugby_possibility_row_new (RugbyPossibility *possibility);
G_END_DECLS
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment