File: mg-entry-boolean.c

package info (click to toggle)
mergeant 0.52-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 12,872 kB
  • ctags: 6,584
  • sloc: ansic: 63,372; xml: 23,218; sh: 10,895; makefile: 612; sql: 237
file content (259 lines) | stat: -rw-r--r-- 7,240 bytes parent folder | download | duplicates (2)
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
/* mg-entry-boolean.c
 *
 * Copyright (C) 2003 Vivien Malerba
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include "mg-entry-boolean.h"
#include <libmergeant/mg-data-handler.h>

/* 
 * Main static functions 
 */
static void mg_entry_boolean_class_init (MgEntryBooleanClass * class);
static void mg_entry_boolean_init (MgEntryBoolean * srv);
static void mg_entry_boolean_dispose (GObject   * object);
static void mg_entry_boolean_finalize (GObject   * object);

/* virtual functions */
static GtkWidget *create_entry (MgEntryWrapper *mgwrap);
static void       real_set_value (MgEntryWrapper *mgwrap, const GdaValue *value);
static GdaValue  *real_get_value (MgEntryWrapper *mgwrap);
static void       connect_signals(MgEntryWrapper *mgwrap, GCallback callback);
static gboolean   expand_in_layout (MgEntryWrapper *mgwrap);


/* get a pointer to the parents to be able to call their destructor */
static GObjectClass  *parent_class = NULL;

/* private structure */
struct _MgEntryBooleanPrivate
{
	GtkWidget *hbox;
	GtkWidget *check;
};


guint
mg_entry_boolean_get_type (void)
{
	static GType type = 0;

	if (!type) {
		static const GTypeInfo info = {
			sizeof (MgEntryBooleanClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) mg_entry_boolean_class_init,
			NULL,
			NULL,
			sizeof (MgEntryBoolean),
			0,
			(GInstanceInitFunc) mg_entry_boolean_init
		};
		
		type = g_type_register_static (MG_ENTRY_WRAPPER_TYPE, "MgEntryBoolean", &info, 0);
	}
	return type;
}

static void
mg_entry_boolean_class_init (MgEntryBooleanClass * class)
{
	GObjectClass   *object_class = G_OBJECT_CLASS (class);

	parent_class = g_type_class_peek_parent (class);

	object_class->dispose = mg_entry_boolean_dispose;
	object_class->finalize = mg_entry_boolean_finalize;

	MG_ENTRY_WRAPPER_CLASS (class)->create_entry = create_entry;
	MG_ENTRY_WRAPPER_CLASS (class)->real_set_value = real_set_value;
	MG_ENTRY_WRAPPER_CLASS (class)->real_get_value = real_get_value;
	MG_ENTRY_WRAPPER_CLASS (class)->connect_signals = connect_signals;
	MG_ENTRY_WRAPPER_CLASS (class)->expand_in_layout = expand_in_layout;
}

static void
mg_entry_boolean_init (MgEntryBoolean * mg_entry_boolean)
{
	mg_entry_boolean->priv = g_new0 (MgEntryBooleanPrivate, 1);
	mg_entry_boolean->priv->hbox = NULL;
	mg_entry_boolean->priv->check = NULL;
}

/**
 * mg_entry_boolean_new
 * @dh: the data handler to be used by the new widget
 * @type: the requested data type (compatible with @dh)
 *
 * Creates a new widget which is mainly a GtkEntry
 *
 * Returns: the new widget
 */
GtkWidget *
mg_entry_boolean_new (MgDataHandler *dh, GdaValueType type)
{
	GObject *obj;
	MgEntryBoolean *mgbool;

	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);
	g_return_val_if_fail (type != GDA_VALUE_TYPE_UNKNOWN, NULL);
	g_return_val_if_fail (mg_data_handler_accepts_gda_type (dh, type), NULL);

	obj = g_object_new (MG_ENTRY_BOOLEAN_TYPE, "handler", dh, NULL);
	mgbool = MG_ENTRY_BOOLEAN (obj);
	mg_data_entry_set_value_type (MG_DATA_ENTRY (mgbool), type);

	return GTK_WIDGET (obj);
}


static void
mg_entry_boolean_dispose (GObject   * object)
{
	MgEntryBoolean *mg_entry_boolean;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_MG_ENTRY_BOOLEAN (object));

	mg_entry_boolean = MG_ENTRY_BOOLEAN (object);
	if (mg_entry_boolean->priv) {

	}

	/* parent class */
	parent_class->dispose (object);
}

static void
mg_entry_boolean_finalize (GObject   * object)
{
	MgEntryBoolean *mg_entry_boolean;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_MG_ENTRY_BOOLEAN (object));

	mg_entry_boolean = MG_ENTRY_BOOLEAN (object);
	if (mg_entry_boolean->priv) {

		g_free (mg_entry_boolean->priv);
		mg_entry_boolean->priv = NULL;
	}

	/* parent class */
	parent_class->finalize (object);
}

static GtkWidget *
create_entry (MgEntryWrapper *mgwrap)
{
	GtkWidget *hbox, *cb, *label;
	MgEntryBoolean *mgbool;

	g_return_val_if_fail (mgwrap && IS_MG_ENTRY_BOOLEAN (mgwrap), NULL);
	mgbool = MG_ENTRY_BOOLEAN (mgwrap);
	g_return_val_if_fail (mgbool->priv, NULL);

	hbox = gtk_hbox_new (FALSE, 5);
	mgbool->priv->hbox = hbox;

	cb = gtk_check_button_new ();
	mgbool->priv->check = cb;
	gtk_box_pack_start (GTK_BOX (hbox), cb, FALSE, FALSE, 0);
	gtk_widget_show (cb);

	return hbox;
}

static void
real_set_value (MgEntryWrapper *mgwrap, const GdaValue *value)
{
	MgEntryBoolean *mgbool;

	g_return_if_fail (mgwrap && IS_MG_ENTRY_BOOLEAN (mgwrap));
	mgbool = MG_ENTRY_BOOLEAN (mgwrap);
	g_return_if_fail (mgbool->priv);

	if (value) {
		if (gda_value_is_null (value)) {
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mgbool->priv->check), FALSE);
			gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON (mgbool->priv->check), TRUE);
		}
		else {
			gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON (mgbool->priv->check), FALSE);
			if (gda_value_get_boolean (value)) 
				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mgbool->priv->check), TRUE);
			else 
				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mgbool->priv->check), FALSE);
		}
	}
	else {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mgbool->priv->check), FALSE);
		gtk_toggle_button_set_inconsistent (GTK_TOGGLE_BUTTON (mgbool->priv->check), TRUE);
	}
}

static GdaValue *
real_get_value (MgEntryWrapper *mgwrap)
{
	GdaValue *value;
	MgEntryBoolean *mgbool;
	MgDataHandler *dh;
	const gchar *str;

	g_return_val_if_fail (mgwrap && IS_MG_ENTRY_BOOLEAN (mgwrap), NULL);
	mgbool = MG_ENTRY_BOOLEAN (mgwrap);
	g_return_val_if_fail (mgbool->priv, NULL);

	dh = mg_data_entry_get_handler (MG_DATA_ENTRY (mgwrap));
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (mgbool->priv->check)))
		str = "TRUE";
	else
		str = "FALSE";
	value = mg_data_handler_get_value_from_sql (dh, str, mg_data_entry_get_value_type (MG_DATA_ENTRY (mgwrap)));

	return value;
}

static void check_toggled_cb (GtkToggleButton *toggle, MgEntryBoolean *mgbool);
static void
connect_signals(MgEntryWrapper *mgwrap, GCallback callback)
{
	MgEntryBoolean *mgbool;

	g_return_if_fail (mgwrap && IS_MG_ENTRY_BOOLEAN (mgwrap));
	mgbool = MG_ENTRY_BOOLEAN (mgwrap);
	g_return_if_fail (mgbool->priv);

	g_signal_connect (G_OBJECT (mgbool->priv->check), "toggled",
			  callback, mgwrap);
	g_signal_connect (G_OBJECT (mgbool->priv->check), "toggled",
                          G_CALLBACK (check_toggled_cb), mgwrap);
}

static void
check_toggled_cb (GtkToggleButton *toggle, MgEntryBoolean *mgbool)
{
	gtk_toggle_button_set_inconsistent (toggle, FALSE);
}

static gboolean
expand_in_layout (MgEntryWrapper *mgwrap)
{
	return FALSE;
}