File: gtk-sat-list-col-sel.c

package info (click to toggle)
gpredict 1.1-7
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,280 kB
  • ctags: 2,873
  • sloc: ansic: 34,903; sh: 10,151; makefile: 417
file content (330 lines) | stat: -rw-r--r-- 8,508 bytes parent folder | download
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
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
213
214
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
    Gpredict: Real-time satellite tracking and orbit prediction program

    Copyright (C)  2001-2009  Alexandru Csete, OZ9AEC.

    Authors: Alexandru Csete <oz9aec@gmail.com>

    Comments, questions and bugreports should be submitted via
    http://sourceforge.net/projects/gpredict/
    More details can be found at the project home page:

            http://gpredict.oz9aec.net/
 
    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, visit http://www.fsf.org/
*/
/** \brief SatList column selector.
 *
 * More info...
 */
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "gtk-sat-list.h"
#include "gtk-sat-list-col-sel.h"



/* defined in gtk-sat-list.c; we use them for labels */
extern const gchar *SAT_LIST_COL_HINT[];



static void gtk_sat_list_col_sel_class_init (GtkSatListColSelClass *class);
static void gtk_sat_list_col_sel_init       (GtkSatListColSel      *sel);
static void gtk_sat_list_col_sel_destroy    (GtkObject             *object);

static GtkTreeModel *create_and_fill_model (guint32 flags);
static void          column_toggled        (GtkCellRendererToggle *cell,
					    gchar                 *path_str,
					    gpointer               data);

static gboolean set_col (GtkTreeModel *model,
			 GtkTreePath *path,
			 GtkTreeIter *iter,
			 gpointer data);


static GtkVBoxClass *parent_class = NULL;


GType
gtk_sat_list_col_sel_get_type ()
{
	static GType gtk_sat_list_col_sel_type = 0;

	if (!gtk_sat_list_col_sel_type)
	{
		static const GTypeInfo gtk_sat_list_col_sel_info =
			{
				sizeof (GtkSatListColSelClass),
				NULL,  /* base_init */
				NULL,  /* base_finalize */
				(GClassInitFunc) gtk_sat_list_col_sel_class_init,
				NULL,  /* class_finalize */
				NULL,  /* class_data */
				sizeof (GtkSatListColSel),
				5,     /* n_preallocs */
				(GInstanceInitFunc) gtk_sat_list_col_sel_init,
			};

		gtk_sat_list_col_sel_type = g_type_register_static (GTK_TYPE_VBOX,
								    "GtkSatListColSel",
								    &gtk_sat_list_col_sel_info,
								    0);
	}

	return gtk_sat_list_col_sel_type;
}


static void
gtk_sat_list_col_sel_class_init (GtkSatListColSelClass *class)
{
	GObjectClass      *gobject_class;
	GtkObjectClass    *object_class;
	GtkWidgetClass    *widget_class;
	GtkContainerClass *container_class;

	gobject_class   = G_OBJECT_CLASS (class);
	object_class    = (GtkObjectClass*) class;
	widget_class    = (GtkWidgetClass*) class;
	container_class = (GtkContainerClass*) class;

	parent_class = g_type_class_peek_parent (class);

	object_class->destroy = gtk_sat_list_col_sel_destroy;
 
}



static void
gtk_sat_list_col_sel_init (GtkSatListColSel *list)
{

}

static void
gtk_sat_list_col_sel_destroy (GtkObject *object)
{
	//GtkSatListColSel *sel = GTK_SAT_LIST_COL_SEL (object);

	(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}



GtkWidget *
gtk_sat_list_col_sel_new (guint32 flags)
{
	GtkWidget         *widget;
	GtkSatListColSel  *sel;
	GtkTreeModel      *model;
	GtkCellRenderer   *renderer;
	GtkTreeViewColumn *column;


	widget = g_object_new (GTK_TYPE_SAT_LIST_COL_SEL, NULL);
	sel = GTK_SAT_LIST_COL_SEL (widget);

	sel->flags = flags;

	/* create list and model */
	sel->list = gtk_tree_view_new ();
	model = create_and_fill_model (flags);
	gtk_tree_view_set_model (GTK_TREE_VIEW (sel->list), model);
	g_object_unref (model);

	/* create tree view columns */
	/* label column */
	renderer = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes (_("Column Name"), renderer,
							   "text", 0, NULL);
	gtk_tree_view_insert_column (GTK_TREE_VIEW (sel->list), column, -1);

	/* checkbox column */
	renderer = gtk_cell_renderer_toggle_new ();
	sel->handler_id = g_signal_connect (renderer, "toggled",
					    G_CALLBACK (column_toggled), widget);

	column = gtk_tree_view_column_new_with_attributes (_("Visible"), renderer,
							   "active", 1, NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (sel->list), column);
	gtk_tree_view_column_set_alignment (column, 0.5);

	/* invisible row number */
	renderer = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes (_("Row"), renderer,
							   "text", 2, NULL);

	gtk_tree_view_append_column (GTK_TREE_VIEW (sel->list), column);
	gtk_tree_view_column_set_visible (column, FALSE);
	
	/* this is discouraged but looks cool in this case */
	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (sel->list), TRUE);

	/* scrolled window */
	GTK_SAT_LIST_COL_SEL (widget)->swin = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (GTK_SAT_LIST_COL_SEL (widget)->swin),
					GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);

	gtk_container_add (GTK_CONTAINER (GTK_SAT_LIST_COL_SEL (widget)->swin),
			   GTK_SAT_LIST_COL_SEL (widget)->list);

	gtk_container_add (GTK_CONTAINER (widget), GTK_SAT_LIST_COL_SEL (widget)->swin);
	gtk_widget_show_all (widget);


	return widget;
}

static GtkTreeModel *
create_and_fill_model (guint32 flags)
{
	GtkListStore *liststore;    /* the list store data structure */
	GtkTreeIter  item;       /* new item added to the list store */
	guint i;
	gboolean checked;

	/* create a new list store */
	liststore = gtk_list_store_new (3,
					G_TYPE_STRING,    // column label
					G_TYPE_BOOLEAN,   // visible checkbox
					G_TYPE_INT        // row number
					);


	for (i = 0; i < SAT_LIST_COL_NUMBER; i++) {

		checked = (flags & (1 << i)) ? TRUE : FALSE;

		gtk_list_store_append (liststore, &item);
		gtk_list_store_set (liststore, &item,
				    0, SAT_LIST_COL_HINT[i],
				    1, checked,
				    2, i,
				    -1);

	}


	return GTK_TREE_MODEL (liststore);
}



guint32
gtk_sat_list_col_sel_get_flags (GtkSatListColSel *sel)
{

	g_return_val_if_fail ((sel != NULL) || !IS_GTK_SAT_LIST_COL_SEL (sel), 0);

	return sel->flags;
}


void
gtk_sat_list_col_sel_set_flags (GtkSatListColSel *sel, guint32 flags)
{
	GtkTreeModel *liststore;    /* the list store data structure */


	g_return_if_fail ((sel != NULL) || !IS_GTK_SAT_LIST_COL_SEL (sel));

	sel->flags = flags;


	liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (sel->list));
	gtk_tree_model_foreach (liststore, set_col, sel);
}


static gboolean
set_col (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
{
	GtkSatListColSel *sel = GTK_SAT_LIST_COL_SEL (data);
	guint i;
	gboolean checked;

	/* get row number */
	gtk_tree_model_get (model, iter, 2, &i, -1);

	/* get flag */
	checked = (sel->flags & (1 << i)) ? TRUE : FALSE;

	/* store flag */
	gtk_list_store_set (GTK_LIST_STORE (model), iter, 1, checked, -1);

	return FALSE;
}





/** \brief Manage toggle signals.
 *  \param cell cell.
 *  \param path_str Path string.
 *  \param data Pointer to the GtkSatListColSel widget.
 *
 * This function is called when the user toggles the visibility for a column.
 */
static void
column_toggled        (GtkCellRendererToggle *cell,
		       gchar                 *path_str,
		       gpointer               data)
{
	GtkSatListColSel *sel = GTK_SAT_LIST_COL_SEL (data);
	GtkTreeModel *model;
	GtkTreeIter   iter;
	GtkTreePath  *path = gtk_tree_path_new_from_string (path_str);
	gboolean      checked;
	gint          row;


	/* block toggle signals while we mess with the check boxes */
	g_signal_handler_block (cell, sel->handler_id);

	/* get toggled iter */
	model = gtk_tree_view_get_model (GTK_TREE_VIEW (sel->list));
	gtk_tree_model_get_iter (model, &iter, path);
	gtk_tree_model_get (model, &iter,
			    1, &checked,
			    2, &row,
			    -1);

	/* reverse status */
	checked = !checked;

	gtk_list_store_set (GTK_LIST_STORE (model), &iter,
			    1, checked,
			    -1);

	if (checked) {
		/* turn bit ON */
		sel->flags |= (1 << row);
	}
	else {
		/* turn bit OFF */
		sel->flags &= ~(1 << row);
	}

	/* clean up */
	gtk_tree_path_free (path);

	/* unblock toggle signals */
	g_signal_handler_unblock (cell, sel->handler_id);

}