Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "rugby-application.h"
#include "rugby-cell-renderer-score.h"
#include "rugby-score-store.h"
#include "rugby-scoring.h"
struct _RugbyApplicationPrivate
{
GtkWidget *enablefilter;
GtkWidget *filterscale;
RugbyScoreStore *store;
GtkTreeModel *fmodel;
};
G_DEFINE_TYPE (RugbyApplication, rugby_application, GTK_TYPE_APPLICATION)
static void
scorespin_changed_cb (GtkSpinButton *spin,
RugbyApplication *app)
{
gint score;
gint max;
score = gtk_spin_button_get_value_as_int (spin);
rugby_score_store_set_score (app->priv->store, score);
/* I'd rather not have to do this */
max = MAX (rugby_scoring_get_max_tries (score), 1.0);
gtk_range_set_range (GTK_RANGE (app->priv->filterscale), 0.0, max);
}
static void
enablefilter_toggled_cb (GtkToggleButton *toggle,
RugbyApplication *app)
{
if (gtk_toggle_button_get_active (toggle))
gtk_widget_set_sensitive (app->priv->filterscale, TRUE);
else
gtk_widget_set_sensitive (app->priv->filterscale, FALSE);
gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (app->priv->fmodel));
}
static void
filterscale_changed_cb (GtkRange *range,
RugbyApplication *app)
{
gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (app->priv->fmodel));
}
static void
store_finished_cb (RugbyScoreStore *store,
gint possibilities,
GtkStatusbar *bar)
{
gchar *status;
guint context_id;
status = g_strdup_printf ("%d possibilities", possibilities);
context_id = gtk_statusbar_get_context_id (bar, "possibilities count");
gtk_statusbar_pop (bar, context_id);
gtk_statusbar_push (bar, context_id, status);
g_free (status);
}
static gboolean
filter_func (GtkTreeModel *model,
GtkTreeIter *iter,
RugbyApplication *app)
{
gint tries, utries;
gint current;
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (app->priv->enablefilter)))
{
current = (gint) gtk_range_get_value (GTK_RANGE (app->priv->filterscale));
gtk_tree_model_get (model, iter,
RUGBY_SCORE_STORE_TRIES, &tries,
RUGBY_SCORE_STORE_UTRIES, &utries, -1);
if (tries + utries == current)
return TRUE;
else
return FALSE;
}
else
return TRUE;
}
static void
app_about (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GtkApplication *application = GTK_APPLICATION (user_data);
GList *windows;
GtkWindow *window;
windows = gtk_application_get_windows (application);
window = windows->data;
/* TODO show proper about dialogue */
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
gtk_show_about_dialog (window,
"program-name", "Rugby",
"license-type", GTK_LICENSE_MIT_X11,
NULL);
}
static void
app_quit (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GApplication *application = G_APPLICATION (user_data);
g_application_quit (application);
}
static GActionEntry app_entries[] =
{
{ "about", app_about, NULL, NULL, NULL },
{ "quit", app_quit, NULL, NULL, NULL }
};
static void
rugby_application_startup (GApplication *application)
{
GtkBuilder *builder;
GError *err = NULL;
G_APPLICATION_CLASS (rugby_application_parent_class)->startup (application);
g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application);
builder = gtk_builder_new ();
if (!gtk_builder_add_from_file (builder, "interface.ui", &err))
{
g_error ("Error: %s", err->message);
}
gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
g_object_unref (builder);
}
static void
rugby_application_activate (GApplication *app)
{
RugbyApplicationPrivate *priv = RUGBY_APPLICATION (app)->priv;
GtkBuilder *builder;
GError *err = NULL;
GtkWidget *score;
GtkWidget *tree;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkWidget *statusbar;
GtkWidget *window;
GtkCssProvider *provider;
builder = gtk_builder_new ();
if (!gtk_builder_add_from_file (builder, "interface.ui", &err))
{
g_error ("Error: %s", err->message);
}
score = GTK_WIDGET (gtk_builder_get_object (builder, "scorespin"));
g_signal_connect (score, "value-changed",
G_CALLBACK (scorespin_changed_cb), app);
priv->enablefilter = GTK_WIDGET (gtk_builder_get_object (builder, "enablefilter"));
g_signal_connect (priv->enablefilter, "toggled",
G_CALLBACK (enablefilter_toggled_cb), app);
priv->filterscale = GTK_WIDGET (gtk_builder_get_object (builder, "filterscale"));
g_signal_connect (priv->filterscale, "value-changed",
G_CALLBACK (filterscale_changed_cb), app);
tree = GTK_WIDGET (gtk_builder_get_object (builder, "treeview"));
/* Would be nice to use GtkBuilder */
renderer = rugby_cell_renderer_score_new ();
column = gtk_tree_view_column_new_with_attributes ("Score", renderer,
"tries", RUGBY_SCORE_STORE_TRIES,
"utries", RUGBY_SCORE_STORE_UTRIES,
"pens", RUGBY_SCORE_STORE_PENS,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
/* Create a TreeModelFilter */
priv->store = rugby_score_store_new ();
priv->fmodel = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->fmodel),
(GtkTreeModelFilterVisibleFunc) filter_func, app, NULL);
gtk_tree_view_set_model (GTK_TREE_VIEW (tree), priv->fmodel);
statusbar = GTK_WIDGET (gtk_builder_get_object (builder, "status"));
g_signal_connect (priv->store, "finished",
G_CALLBACK (store_finished_cb), statusbar);
/* init CSS */
provider = gtk_css_provider_new ();
gtk_css_provider_load_from_path (provider, "rugby.css", NULL);
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
gtk_window_set_application (GTK_WINDOW (window), GTK_APPLICATION (app));
gtk_widget_show_all (window);
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
}
static void
rugby_application_class_init (RugbyApplicationClass *klass)
{
GApplicationClass *app_class = G_APPLICATION_CLASS (klass);
app_class->startup = rugby_application_startup;
app_class->activate = rugby_application_activate;
g_type_class_add_private (klass, sizeof (RugbyApplicationPrivate));
}
static void
rugby_application_init (RugbyApplication *app)
{
app->priv = G_TYPE_INSTANCE_GET_PRIVATE (app, RUGBY_TYPE_APPLICATION, RugbyApplicationPrivate);
}
/**
* rugby_application_new:
*
* Creates a #RugbyApplication. This is the main UI of the program
*
* Returns: a new #RugbyApplication
*/
RugbyApplication *
rugby_application_new (void)
{
g_type_init ();
g_set_application_name ("Rugby");
return g_object_new (RUGBY_TYPE_APPLICATION,
"application-id", "uk.me.bcowan.Rugby",
NULL);
}