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

Change to GtkListView

parent 681cc5cb
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,11 @@
<property name="upper">200</property>
<property name="step_increment">1</property>
</object>
<object class="RugbyListStore" id="list_store">
<binding name="score">
<lookup name="value">scorespin</lookup>
</binding>
</object>
<menu id="menu">
<section>
<item>
......@@ -30,23 +35,20 @@
<object class="GtkScrolledWindow">
<property name="height_request">600</property>
<child>
<object class="GtkViewport">
<child>
<object class="GtkListBox" id="listbox">
<property name="selection_mode">none</property>
<style>
<class name="rich-list"/>
</style>
<child type="placeholder">
<object class="GtkLabel">
<property name="label">No possibilities for this score</property>
<style>
<class name="dim-label"/>
</style>
</object>
</child>
<object class="GtkListView" id="listview">
<property name="model">
<object class="GtkNoSelection">
<property name="model">list_store</property>
</object>
</property>
<property name="factory">
<object class="GtkBuilderListItemFactory">
<property name="resource">/uk/me/bcowan/rugby/score-item.ui</property>
</object>
</child>
</property>
<style>
<class name="rich-list"/>
</style>
</object>
</child>
</object>
......
......@@ -8,5 +8,6 @@
<gresource prefix="/uk/me/bcowan/rugby">
<file preprocess="xml-stripblanks" compressed="true">interface.ui</file>
<file preprocess="xml-stripblanks" compressed="true">prefs.ui</file>
<file preprocess="xml-stripblanks" compressed="true">score-item.ui</file>
</gresource>
</gresources>
<?xml version="1.0" encoding="UTF-8"?>
<!--
SPDX-FileCopyrightText: 2021 Bruce Cowan <bruce@bcowan.me.uk>
SPDX-License-Identifier: GPL-3.0-or-later
-->
<interface>
<requires lib="gtk" version="4.0"/>
<template class="GtkListItem">
<property name="child">
<object class="RugbyPossibilityWidget">
<binding name="possibility">
<lookup name="item">GtkListItem</lookup>
</binding>
<binding name="tooltip-text">
<closure type="gchararray" function="item_tooltip_cb">
<lookup name="item">GtkListItem</lookup>
</closure>
</binding>
</object>
</property>
</template>
</interface>
......@@ -6,7 +6,7 @@ sources = [
'rugby-app-window.c',
'rugby-list-store.c',
'rugby-possibility.c',
'rugby-possibility-row.c',
'rugby-possibility-widget.c',
'rugby-pref-window.c',
]
......
......@@ -5,73 +5,86 @@
*/
#include "config.h"
#include "rugby-app-window.h"
#include "rugby-app-window.h"
#include "rugby-list-store.h"
#include "rugby-possibility.h"
#include "rugby-possibility-row.h"
#include "rugby-possibility-widget.h"
#include <glib/gi18n.h>
struct _RugbyAppWindow
{
GtkApplicationWindow parent;
RugbyListStore *store;
GtkWidget *scorespin;
GtkWidget *listbox;
};
G_DEFINE_TYPE (RugbyAppWindow, rugby_app_window, GTK_TYPE_APPLICATION_WINDOW)
static GtkWidget *
listbox_widget_func ( gpointer item,
G_GNUC_UNUSED gpointer user_data)
static char *
item_tooltip_cb (GtkListItem *item)
{
RugbyPossibility *possibility = RUGBY_POSSIBILITY (item);
return rugby_possibility_row_new (possibility);
RugbyPossibility *possibility = gtk_list_item_get_item (item);
int tries, utries, kicks;
GString *tooltip = g_string_new (NULL);
if (!possibility)
return NULL;
g_object_get (possibility,
"tries", &tries,
"utries", &utries,
"kicks", &kicks,
NULL);
if ((tries + utries) > 0)
{
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,
ngettext (", %d kick",
", %d kicks",
kicks),
kicks);
}
else if (kicks > 0)
{
g_string_append_printf (tooltip,
ngettext ("%d kick",
"%d kicks", kicks),
kicks);
}
return g_string_free (tooltip, FALSE);
}
static void
rugby_app_window_init (RugbyAppWindow *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
self->store = rugby_list_store_new ();
gtk_list_box_bind_model (GTK_LIST_BOX (self->listbox),
G_LIST_MODEL (self->store),
listbox_widget_func,
NULL,
NULL);
g_object_bind_property (self->scorespin, "value",
self->store, "score",
G_BINDING_DEFAULT);
}
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;
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/interface.ui");
gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, scorespin);
gtk_widget_class_bind_template_child (widget_class, RugbyAppWindow, listbox);
gtk_widget_class_bind_template_callback (widget_class, item_tooltip_cb);
}
RugbyAppWindow *
......
......@@ -6,19 +6,17 @@
#include "config.h"
#include "rugby-possibility-row.h"
#include "rugby-possibility-widget.h"
#include <glib/gi18n.h>
struct _RugbyPossibilityRow
struct _RugbyPossibilityWidget
{
GtkListBoxRow parent_instance;
GtkWidget parent_instance;
GSettings *settings;
RugbyPossibility *possibility;
};
G_DEFINE_TYPE (RugbyPossibilityRow, rugby_possibility_row, GTK_TYPE_LIST_BOX_ROW)
G_DEFINE_TYPE (RugbyPossibilityWidget, rugby_possibility_widget, GTK_TYPE_WIDGET)
enum
{
......@@ -29,23 +27,23 @@ enum
static GParamSpec *properties[N_PROPS];
static void
rugby_possibility_row_dispose (GObject *object)
rugby_possibility_widget_dispose (GObject *object)
{
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object);
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object);
g_clear_object (&self->settings);
g_clear_object (&self->possibility);
G_OBJECT_CLASS (rugby_possibility_row_parent_class)->dispose (object);
G_OBJECT_CLASS (rugby_possibility_widget_parent_class)->dispose (object);
}
static void
rugby_possibility_row_get_property (GObject *object,
unsigned prop_id,
GValue *value,
GParamSpec *pspec)
rugby_possibility_widget_get_property (GObject *object,
unsigned prop_id,
GValue *value,
GParamSpec *pspec)
{
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object);
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object);
switch (prop_id)
{
......@@ -58,12 +56,12 @@ rugby_possibility_row_get_property (GObject *object,
}
static void
rugby_possibility_row_set_property (GObject *object,
unsigned prop_id,
const GValue *value,
GParamSpec *pspec)
rugby_possibility_widget_set_property (GObject *object,
unsigned prop_id,
const GValue *value,
GParamSpec *pspec)
{
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (object);
RugbyPossibilityWidget *self = RUGBY_POSSIBILITY_WIDGET (object);
switch (prop_id)
{
......@@ -78,20 +76,21 @@ rugby_possibility_row_set_property (GObject *object,
static void
render_bar (GtkSnapshot *snapshot,
float x,
float y,
float w,
float h,
const GdkRGBA color)
{
graphene_rect_t area = GRAPHENE_RECT_INIT (x, 0.0, w, h);
GskRoundedRect rounded;
gsk_rounded_rect_init_from_rect (&rounded,
&GRAPHENE_RECT_INIT (x, y, w, h),
&area,
h / 2.0);
gtk_snapshot_push_rounded_clip (snapshot, &rounded);
gtk_snapshot_append_color (snapshot,
&color,
&GRAPHENE_RECT_INIT (x, y, w, h));
&area);
gtk_snapshot_pop (snapshot);
GdkRGBA black = { 0.0, 0.0, 0.0, 0.2 };
......@@ -102,10 +101,10 @@ render_bar (GtkSnapshot *snapshot,
}
static void
rugby_possibility_row_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot)
rugby_possibility_widget_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot)
{
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (widget);
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");
......@@ -125,11 +124,10 @@ rugby_possibility_row_snapshot (GtkWidget *widget,
// Tries
float w = width / (score / (float) try_points);
for (int i = 0; i < tries; i++)
{
// Green
render_bar (snapshot, x, 0.0, w, height,
render_bar (snapshot, x, w, height,
(GdkRGBA) { 0.20, 0.82, 0.48, 1.0 });
x += w;
}
......@@ -139,7 +137,7 @@ rugby_possibility_row_snapshot (GtkWidget *widget,
for (int i = 0; i < utries; i++)
{
// Red
render_bar (snapshot, x, 0.0, w, height,
render_bar (snapshot, x, w, height,
(GdkRGBA) { 0.88, 0.11, 0.14, 1.0 });
x += w;
}
......@@ -149,75 +147,28 @@ rugby_possibility_row_snapshot (GtkWidget *widget,
for (int i = 0; i < kicks; i++)
{
// Yellow
render_bar (snapshot, x, 0.0, w, height,
render_bar (snapshot, x, w, height,
(GdkRGBA) { 0.96, 0.83, 0.18, 1.0 });
x += w;
}
}
static void
rugby_possibility_row_constructed (GObject *obj)
{
RugbyPossibilityRow *self = RUGBY_POSSIBILITY_ROW (obj);
int tries, utries, kicks;
g_autoptr (GString) tooltip = g_string_new (NULL);
g_object_get (self->possibility,
"tries", &tries,
"utries", &utries,
"kicks", &kicks,
NULL);
if ((tries + utries) > 0)
{
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,
ngettext (", %d kick",
", %d kicks",
kicks),
kicks);
}
else if (kicks > 0)
{
g_string_append_printf (tooltip,
ngettext ("%d kick",
"%d kicks", kicks),
kicks);
}
gtk_widget_set_tooltip_text (GTK_WIDGET (self), tooltip->str);
G_OBJECT_CLASS (rugby_possibility_row_parent_class)->constructed (obj);
}
static void
rugby_possibility_row_class_init (RugbyPossibilityRowClass *klass)
rugby_possibility_widget_class_init (RugbyPossibilityWidgetClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
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;
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_row_snapshot;
widget_class->snapshot = rugby_possibility_widget_snapshot;
properties[PROP_POSSIBILITY] =
g_param_spec_object ("possibility", "", "",
RUGBY_TYPE_POSSIBILITY,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
G_PARAM_READWRITE);
g_object_class_install_properties (object_class,
N_PROPS,
......@@ -225,15 +176,15 @@ rugby_possibility_row_class_init (RugbyPossibilityRowClass *klass)
}
static void
rugby_possibility_row_init (RugbyPossibilityRow *self)
rugby_possibility_widget_init (RugbyPossibilityWidget *self)
{
self->settings = g_settings_new ("uk.me.bcowan.Rugby");
}
GtkWidget *
rugby_possibility_row_new (RugbyPossibility *possibility)
rugby_possibility_widget_new (RugbyPossibility *possibility)
{
return g_object_new (RUGBY_TYPE_POSSIBILITY_ROW,
return g_object_new (RUGBY_TYPE_POSSIBILITY_WIDGET,
"possibility", possibility,
NULL);
}
......@@ -12,10 +12,10 @@
G_BEGIN_DECLS
#define RUGBY_TYPE_POSSIBILITY_ROW (rugby_possibility_row_get_type())
#define RUGBY_TYPE_POSSIBILITY_WIDGET (rugby_possibility_widget_get_type())
G_DECLARE_FINAL_TYPE (RugbyPossibilityRow, rugby_possibility_row, RUGBY, POSSIBILITY_ROW, GtkListBoxRow)
G_DECLARE_FINAL_TYPE (RugbyPossibilityWidget, rugby_possibility_widget, RUGBY, POSSIBILITY_WIDGET, GtkWidget)
GtkWidget * rugby_possibility_row_new (RugbyPossibility *possibility);
GtkWidget * rugby_possibility_widget_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