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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * lapiz-encodings-combo-box.c
 * This file is part of lapiz
 *
 * Copyright (C) 2003-2005 - Paolo Maggi
 *
 * 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.
 */

/*
 * Modified by the lapiz Team, 2003-2005. See the AUTHORS file for a
 * list of people on the lapiz Team.
 * See the ChangeLog files for a list of changes.
 *
 * $Id: lapiz-encodings-combo-box.c 6112 2008-01-23 08:26:24Z sfre $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n.h>
#include <ctk/ctk.h>

#include <lapiz/lapiz-encodings-combo-box.h>
#include <lapiz/lapiz-prefs-manager.h>
#include <lapiz/dialogs/lapiz-encodings-dialog.h>

#define ENCODING_KEY "Enconding"

struct _LapizEncodingsComboBoxPrivate
{
	CtkListStore *store;
	glong changed_id;

	guint activated_item;

	guint save_mode : 1;
};

enum
{
	NAME_COLUMN,
	ENCODING_COLUMN,
	ADD_COLUMN,
	N_COLUMNS
};

/* Properties */
enum
{
	PROP_0,
	PROP_SAVE_MODE
};


G_DEFINE_TYPE_WITH_PRIVATE (LapizEncodingsComboBox, lapiz_encodings_combo_box, CTK_TYPE_COMBO_BOX)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_PRIVATE is a macro then please configure it.

static void	  update_menu 		(LapizEncodingsComboBox       *combo_box);

static void
lapiz_encodings_combo_box_set_property (GObject    *object,
					guint       prop_id,
					const       GValue *value,
					GParamSpec *pspec)
{
	LapizEncodingsComboBox *combo;

	combo = LAPIZ_ENCODINGS_COMBO_BOX (object);

	switch (prop_id)
	{
		case PROP_SAVE_MODE:
			combo->priv->save_mode = g_value_get_boolean (value);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
lapiz_encodings_combo_box_get_property (GObject    *object,
					guint       prop_id,
					GValue 	   *value,
					GParamSpec *pspec)
{
	LapizEncodingsComboBox *combo;

	combo = LAPIZ_ENCODINGS_COMBO_BOX (object);

	switch (prop_id)
	{
		case PROP_SAVE_MODE:
			g_value_set_boolean (value, combo->priv->save_mode);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
lapiz_encodings_combo_box_dispose (GObject *object)
{
	LapizEncodingsComboBox *combo = LAPIZ_ENCODINGS_COMBO_BOX (object);

	if (combo->priv->store != NULL)
	{
		g_object_unref (combo->priv->store);
		combo->priv->store = NULL;
	}

	G_OBJECT_CLASS (lapiz_encodings_combo_box_parent_class)->dispose (object);
}

static void
lapiz_encodings_combo_box_class_init (LapizEncodingsComboBoxClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->set_property = lapiz_encodings_combo_box_set_property;
	object_class->get_property = lapiz_encodings_combo_box_get_property;
	object_class->dispose = lapiz_encodings_combo_box_dispose;

	g_object_class_install_property (object_class,
					 PROP_SAVE_MODE,
					 g_param_spec_boolean ("save-mode",
							       "Save Mode",
							       "Save Mode",
							       FALSE,
							       G_PARAM_READWRITE |
							       G_PARAM_CONSTRUCT |
							       G_PARAM_STATIC_STRINGS));
}

static void
dialog_response_cb (CtkDialog              *dialog,
                    gint                    response_id,
                    LapizEncodingsComboBox *menu)
{
	if (response_id == CTK_RESPONSE_OK)
	{
		update_menu (menu);
	}

	ctk_widget_destroy (CTK_WIDGET (dialog));
}

static void
add_or_remove (LapizEncodingsComboBox *menu,
	       CtkTreeModel           *model)
{
	CtkTreeIter iter;
	gboolean add_item = FALSE;

	if (ctk_combo_box_get_active_iter (CTK_COMBO_BOX (menu), &iter))
	{
		ctk_tree_model_get (model, &iter,
				    ADD_COLUMN, &add_item,
				    -1);
	}

	if (!add_item)
	{
		menu->priv->activated_item = ctk_combo_box_get_active (CTK_COMBO_BOX (menu));
	}
	else
	{
		CtkWidget *dialog;

		CtkWidget *toplevel = ctk_widget_get_toplevel (CTK_WIDGET (menu));

		if (!ctk_widget_is_toplevel (toplevel))
			toplevel = NULL;

		g_signal_handler_block (menu, menu->priv->changed_id);
		ctk_combo_box_set_active (CTK_COMBO_BOX (menu),
					  menu->priv->activated_item);
		g_signal_handler_unblock (menu, menu->priv->changed_id);

		dialog = lapiz_encodings_dialog_new();

		if (toplevel != NULL)
		{
			CtkWindowGroup *wg;

			ctk_window_set_transient_for (CTK_WINDOW (dialog),
						      CTK_WINDOW (toplevel));

			wg = ctk_window_get_group (CTK_WINDOW (toplevel));
			if (wg == NULL)
			{
				wg = ctk_window_group_new ();
				ctk_window_group_add_window (wg,
							     CTK_WINDOW (toplevel));
			}

			ctk_window_group_add_window (wg,
						     CTK_WINDOW (dialog));
		}

		ctk_window_set_modal (CTK_WINDOW (dialog), TRUE);

		g_signal_connect (dialog,
				  "response",
				  G_CALLBACK (dialog_response_cb),
				  menu);

		ctk_widget_show (dialog);
	}
}

static gboolean
separator_func (CtkTreeModel *model,
		CtkTreeIter  *iter,
		gpointer      data G_GNUC_UNUSED)
{
	gchar *str;
	gboolean ret;

	ctk_tree_model_get (model, iter, NAME_COLUMN, &str, -1);
	ret = (str == NULL || *str == '\0');
	g_free (str);

	return ret;
}

static void
update_menu (LapizEncodingsComboBox *menu)
{
	CtkListStore *store;
	CtkTreeIter iter;
	GSList *encodings, *l;
	gchar *str;
	const LapizEncoding *utf8_encoding;
	const LapizEncoding *current_encoding;

	store = menu->priv->store;

	/* Unset the previous model */
	g_signal_handler_block (menu, menu->priv->changed_id);
	ctk_list_store_clear (store);
	ctk_combo_box_set_model (CTK_COMBO_BOX (menu),
				 NULL);

	utf8_encoding = lapiz_encoding_get_utf8 ();
	current_encoding = lapiz_encoding_get_current ();

	if (!menu->priv->save_mode)
	{
		ctk_list_store_append (store, &iter);
		ctk_list_store_set (store, &iter,
				    NAME_COLUMN, _("Automatically Detected"),
				    ENCODING_COLUMN, NULL,
				    ADD_COLUMN, FALSE,
				    -1);

		ctk_list_store_append (store, &iter);
		ctk_list_store_set (store, &iter,
				    NAME_COLUMN, "",
				    ENCODING_COLUMN, NULL,
				    ADD_COLUMN, FALSE,
				    -1);
	}

	if (current_encoding != utf8_encoding)
		str = lapiz_encoding_to_string (utf8_encoding);
	else
		str = g_strdup_printf (_("Current Locale (%s)"),
				       lapiz_encoding_get_charset (utf8_encoding));

	ctk_list_store_append (store, &iter);
	ctk_list_store_set (store, &iter,
			    NAME_COLUMN, str,
			    ENCODING_COLUMN, utf8_encoding,
			    ADD_COLUMN, FALSE,
			    -1);

	g_free (str);

	if ((utf8_encoding != current_encoding) &&
	    (current_encoding != NULL))
	{
		str = g_strdup_printf (_("Current Locale (%s)"),
				       lapiz_encoding_get_charset (current_encoding));

		ctk_list_store_append (store, &iter);
		ctk_list_store_set (store, &iter,
				    NAME_COLUMN, str,
				    ENCODING_COLUMN, current_encoding,
				    ADD_COLUMN, FALSE,
				    -1);

		g_free (str);
	}

	encodings = lapiz_prefs_manager_get_shown_in_menu_encodings ();

	for (l = encodings; l != NULL; l = g_slist_next (l))
	{
		const LapizEncoding *enc = (const LapizEncoding *)l->data;

		if ((enc != current_encoding) &&
		    (enc != utf8_encoding) &&
		    (enc != NULL))
		{
			str = lapiz_encoding_to_string (enc);

			ctk_list_store_append (store, &iter);
			ctk_list_store_set (store, &iter,
					    NAME_COLUMN, str,
					    ENCODING_COLUMN, enc,
					    ADD_COLUMN, FALSE,
					    -1);

			g_free (str);
		}
	}

	g_slist_free (encodings);

	if (lapiz_prefs_manager_shown_in_menu_encodings_can_set ())
	{
		ctk_list_store_append (store, &iter);
		/* separator */
		ctk_list_store_set (store, &iter,
				    NAME_COLUMN, "",
				    ENCODING_COLUMN, NULL,
				    ADD_COLUMN, FALSE,
				    -1);

		ctk_list_store_append (store, &iter);
		ctk_list_store_set (store, &iter,
				    NAME_COLUMN, _("Add or Remove..."),
				    ENCODING_COLUMN, NULL,
				    ADD_COLUMN, TRUE,
				    -1);
	}

	/* set the model back */
	ctk_combo_box_set_model (CTK_COMBO_BOX (menu),
				 CTK_TREE_MODEL (menu->priv->store));
	ctk_combo_box_set_active (CTK_COMBO_BOX (menu), 0);

	g_signal_handler_unblock (menu, menu->priv->changed_id);
}

static void
lapiz_encodings_combo_box_init (LapizEncodingsComboBox *menu)
{
	CtkCellRenderer *text_renderer;

	menu->priv = lapiz_encodings_combo_box_get_instance_private (menu);

	menu->priv->store = ctk_list_store_new (N_COLUMNS,
						G_TYPE_STRING,
						G_TYPE_POINTER,
						G_TYPE_BOOLEAN);

	/* Setup up the cells */
	text_renderer = ctk_cell_renderer_text_new ();
	ctk_cell_layout_pack_end (CTK_CELL_LAYOUT (menu),
				  text_renderer, TRUE);

	ctk_cell_layout_set_attributes (CTK_CELL_LAYOUT (menu),
					text_renderer,
					"text",
					NAME_COLUMN,
					NULL);

	ctk_combo_box_set_row_separator_func (CTK_COMBO_BOX (menu),
					      separator_func, NULL,
					      NULL);

	menu->priv->changed_id = g_signal_connect (menu, "changed",
						   G_CALLBACK (add_or_remove),
						   menu->priv->store);

	update_menu (menu);
}

CtkWidget *
lapiz_encodings_combo_box_new (gboolean save_mode)
{
	return g_object_new (LAPIZ_TYPE_ENCODINGS_COMBO_BOX,
			     "save_mode", save_mode,
			     NULL);
}

const LapizEncoding *
lapiz_encodings_combo_box_get_selected_encoding (LapizEncodingsComboBox *menu)
{
	CtkTreeIter iter;

	g_return_val_if_fail (LAPIZ_IS_ENCODINGS_COMBO_BOX (menu), NULL);

	if (ctk_combo_box_get_active_iter (CTK_COMBO_BOX (menu), &iter))
	{
		const LapizEncoding *ret;
		CtkTreeModel *model;

		model = ctk_combo_box_get_model (CTK_COMBO_BOX (menu));

		ctk_tree_model_get (model, &iter,
				    ENCODING_COLUMN, &ret,
				    -1);

		return ret;
	}

	return NULL;
}

/**
 * lapiz_encodings_combo_box_set_selected_encoding:
 * @menu:
 * @encoding: (allow-none):
 **/
void
lapiz_encodings_combo_box_set_selected_encoding (LapizEncodingsComboBox *menu,
						 const LapizEncoding    *encoding)
{
	CtkTreeIter iter;
	CtkTreeModel *model;
	gboolean b;
	g_return_if_fail (LAPIZ_IS_ENCODINGS_COMBO_BOX (menu));
	g_return_if_fail (CTK_IS_COMBO_BOX (menu));

	model = ctk_combo_box_get_model (CTK_COMBO_BOX (menu));
	b = ctk_tree_model_get_iter_first (model, &iter);

	while (b)
	{
		const LapizEncoding *enc;

		ctk_tree_model_get (model, &iter,
				    ENCODING_COLUMN, &enc,
				    -1);

		if (enc == encoding)
		{
			ctk_combo_box_set_active_iter (CTK_COMBO_BOX (menu),
						       &iter);

			return;
		}

		b = ctk_tree_model_iter_next (model, &iter);
	}
}