File: go-distribution-prefs.c

package info (click to toggle)
goffice 0.10.57-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 28,136 kB
  • sloc: ansic: 114,820; sh: 4,993; makefile: 1,241; perl: 235; xml: 232
file content (155 lines) | stat: -rw-r--r-- 5,171 bytes parent folder | download | duplicates (6)
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
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
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
/*
 * go-distribution-prefs.c
 *
 * Copyright (C) 2008 Jean Brefort (jean.brefort@normalesup.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include "goffice-config.h"
#include "go-distribution-prefs.h"
#include <goffice/graph/gog-data-set.h>
#include <goffice/math/go-distribution.h>
#include <goffice/utils/go-persist.h>
#include <glib/gi18n-lib.h>

typedef struct
{
	GObject *client;
	GParamSpec *props[2];
	GtkWidget *labels[2];
	GtkWidget *data[2];
	GtkGrid *grid;
	GogDataAllocator *dalloc;
} DistPrefs;

static void
destroy_cb (DistPrefs *prefs)
{
	g_free (prefs);
}

static void
distribution_changed_cb (GtkComboBox *box, DistPrefs *prefs)
{
	GODistribution *dist = NULL;
	int i, j, n;
	GtkTreeIter iter;
	GODistributionType dist_type;
	GParamSpec **props;
	GtkTreeModel *model = gtk_combo_box_get_model (box);
	gtk_combo_box_get_active_iter (box, &iter);
	gtk_tree_model_get (model, &iter, 1, &dist_type, -1);
	dist = go_distribution_new (dist_type);
	g_object_set (prefs->client, "distribution", dist, NULL);
	props = g_object_class_list_properties (G_OBJECT_GET_CLASS (dist), &n);
	for (j = i = 0; j < n; j++)
		if (props[j]->flags & GO_PARAM_PERSISTENT) {
			char *lbl = g_strconcat (_(g_param_spec_get_nick (props[j])), _(":"), NULL);
			if (!prefs->labels[i]) {
				GtkWidget *w = gtk_label_new (lbl);
				g_free (lbl);
				g_object_set (w, "xalign", 0., NULL);
				gtk_grid_attach (prefs->grid, w, 0, i + 1, 1, 1);
				prefs->labels[i] = w;
			} else
				gtk_label_set_text (GTK_LABEL (prefs->labels[i]), lbl);
			if (!prefs->data[i]) {
				GtkWidget *w = GTK_WIDGET (gog_data_allocator_editor (prefs->dalloc, GOG_DATASET (prefs->client), i, GOG_DATA_SCALAR));
				gtk_grid_attach (prefs->grid, w, 1, i + 1, 1, 1);
				prefs->data[i] = w;
			}
			gtk_widget_show (prefs->labels[i]);
			gtk_widget_show (prefs->data[i]);
			prefs->props[i++] = props[j];
		}
	while (i < 2) {
		if (prefs->labels[i])
			gtk_widget_hide (prefs->labels[i]);
		if (prefs->data[i])
			gtk_widget_hide (prefs->data[i]);
		prefs->props[i++] = NULL;
	}
	g_free (props);
	g_object_unref (dist);
}

GtkWidget *
go_distribution_pref_new (GObject *obj, GogDataAllocator *dalloc, G_GNUC_UNUSED GOCmdContext *cc)
{
	GtkListStore *model;
	GtkCellRenderer *renderer;
	GtkTreeIter iter;
	GParamSpec **props;
	int n, i, j;
	DistPrefs *prefs = g_new0 (DistPrefs, 1);
	GtkWidget *res = gtk_grid_new ();
	GtkWidget *w = gtk_label_new (_("Distribution:"));
	GODistribution *dist = NULL;
	GODistributionType dist_type;

	prefs->dalloc = dalloc;
	prefs->grid = GTK_GRID (res);
	g_object_get (obj, "distribution", &dist, NULL);
	g_return_val_if_fail (GO_IS_DISTRIBUTION (dist), NULL);

	dist_type = go_distribution_get_distribution_type (dist);
	g_object_set (res, "border-width", 12, "row-spacing", 12, "column-spacing", 24, NULL);
	g_object_set (w, "xalign", 0., NULL);
	gtk_grid_attach (prefs->grid, w, 0, 0, 1, 1);
	g_signal_connect_swapped (res, "destroy", G_CALLBACK (destroy_cb), prefs);
	prefs->client = obj;

	/* add the list of known distributions in a combobox */
	model = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
	w = gtk_combo_box_new_with_model (GTK_TREE_MODEL (model));
	g_object_unref (model);
	renderer = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (w), renderer, FALSE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (w), renderer,
					"text", 0,
					NULL);
	for (i = 0; i < GO_DISTRIBUTION_MAX; i++) {
		gtk_list_store_append (model, &iter);
		gtk_list_store_set (model, &iter, 0, _(go_distribution_type_to_string (i)), 1, i, -1);
		if (i == dist_type)
			gtk_combo_box_set_active_iter (GTK_COMBO_BOX (w), &iter);
	}
	g_signal_connect (w, "changed", G_CALLBACK (distribution_changed_cb), prefs);
	gtk_grid_attach (prefs->grid, w, 1, 0, 1, 1);

	/* other persistent properties */
	i = 1;
	props = g_object_class_list_properties (G_OBJECT_GET_CLASS (dist), &n);
	for (j = 0; j < n; j++)
		if (props[j]->flags & GO_PARAM_PERSISTENT) {
			char *lbl = g_strconcat (_(g_param_spec_get_nick (props[j])), _(":"), NULL);
			w = gtk_label_new (lbl);
			g_free (lbl);
			g_object_set (w, "xalign", 0., NULL);
			gtk_grid_attach (prefs->grid, w, 0, i, 1, 1);
			prefs->labels[i-1] = w;
			prefs->props[i-1] = props[n];
			w = GTK_WIDGET (gog_data_allocator_editor (dalloc, GOG_DATASET (obj), i - 1, GOG_DATA_SCALAR));
			gtk_grid_attach (prefs->grid, w, 1, i, 1, 1);
			prefs->data[i] = w;
			i++;
		}
	g_free (props);

	gtk_widget_show_all (res);
	return res;
}