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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* -*- mode: c; style: linux -*- */

/* cafe-keyboard-properties-xkbot.c
 * Copyright (C) 2003-2007 Sergey V. Udaltsov
 *
 * Written by: Sergey V. Udaltsov <svu@gnome.org>
 *             John Spray <spray_john@users.sourceforge.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, 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.
 */

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

#include <glib/gi18n.h>
#include <string.h>
#include <gio/gio.h>

#include "capplet-util.h"

#include "cafe-keyboard-properties-xkb.h"

static CtkBuilder *chooser_dialog = NULL;
static const char *current1st_level_id = NULL;
static GSList *option_checks_list = NULL;
static CtkWidget *current_none_radio = NULL;
static CtkWidget *current_expander = NULL;
static gboolean current_multi_select = FALSE;
static GSList *current_radio_group = NULL;

#define OPTION_ID_PROP "optionID"
#define SELCOUNTER_PROP "selectionCounter"
#define EXPANDERS_PROP "expandersList"

GSList *
xkb_options_get_selected_list (void)
{
	gchar **array;
	GSList *retval = NULL;
	gint i;
	array = g_settings_get_strv (xkb_kbd_settings, "options");
	if (array != NULL) {
		for (i = 0; array[i]; i++) {
			retval = g_slist_append (retval, g_strdup (array[i]));
		}
	}
	g_strfreev (array);

	if (retval == NULL) {
		if (initial_config.options != NULL) {
			for (i = 0; initial_config.options[i] != NULL; i++)
				retval =
				    g_slist_prepend (retval, g_strdup (initial_config.options[i]));
		}
		retval = g_slist_reverse (retval);
	}

	return retval;
}

/* Returns the selection counter of the expander (static current_expander) */
static int
xkb_options_expander_selcounter_get (void)
{
	return
	    GPOINTER_TO_INT (g_object_get_data
			     (G_OBJECT (current_expander),
			      SELCOUNTER_PROP));
}

/* Increments the selection counter in the expander (static current_expander)
   using the value (can be 0)*/
static void
xkb_options_expander_selcounter_add (int value)
{
	g_object_set_data (G_OBJECT (current_expander), SELCOUNTER_PROP,
			   GINT_TO_POINTER
			   (xkb_options_expander_selcounter_get ()
			    + value));
}

/* Resets the seletion counter in the expander (static current_expander) */
static void
xkb_options_expander_selcounter_reset (void)
{
	g_object_set_data (G_OBJECT (current_expander), SELCOUNTER_PROP,
			   GINT_TO_POINTER (0));
}

/* Formats the expander (static current_expander), based on the selection counter */
static void
xkb_options_expander_highlight (void)
{
	char *utf_group_name =
	    g_object_get_data (G_OBJECT (current_expander),
			       "utfGroupName");
	int counter = xkb_options_expander_selcounter_get ();
	if (utf_group_name != NULL) {
		gchar *titlemarkup =
		    g_strconcat (counter >
				 0 ? "<span weight=\"bold\">" : "<span>",
				 utf_group_name, "</span>", NULL);
		ctk_expander_set_label (CTK_EXPANDER (current_expander),
					titlemarkup);
		g_free (titlemarkup);
	}
}

/* Add optionname from the backend's selection list if it's not
   already in there. */
static void
xkb_options_select (gchar * optionname)
{
	gboolean already_selected = FALSE;
	GSList *options_list = xkb_options_get_selected_list ();
	GSList *option;
	for (option = options_list; option != NULL; option = option->next)
		if (!strcmp ((gchar *) option->data, optionname))
			already_selected = TRUE;

	if (!already_selected)
		options_list =
		    g_slist_append (options_list, g_strdup (optionname));
	xkb_options_set_selected_list (options_list);

	clear_xkb_elements_list (options_list);
}

/* Remove all occurences of optionname from the backend's selection list */
static void
xkb_options_deselect (gchar * optionname)<--- Parameter 'optionname' can be declared as pointer to const
{
	GSList *options_list = xkb_options_get_selected_list ();
	GSList *nodetmp;
	GSList *option = options_list;
	while (option != NULL) {
		gchar *id = (char *) option->data;
		if (!strcmp (id, optionname)) {
			nodetmp = option->next;
			g_free (id);
			options_list =
			    g_slist_remove_link (options_list, option);
			g_slist_free_1 (option);
			option = nodetmp;
		} else
			option = option->next;
	}
	xkb_options_set_selected_list (options_list);
	clear_xkb_elements_list (options_list);
}

/* Return true if optionname describes a string already in the backend's
   list of selected options */
static gboolean
xkb_options_is_selected (gchar * optionname)<--- Parameter 'optionname' can be declared as pointer to const
{
	gboolean retval = FALSE;
	GSList *options_list = xkb_options_get_selected_list ();
	GSList *option;
	for (option = options_list; option != NULL; option = option->next) {
		if (!strcmp ((gchar *) option->data, optionname))
			retval = TRUE;
	}
	clear_xkb_elements_list (options_list);
	return retval;
}

/* Make sure selected options stay visible when navigating with the keyboard */
static gboolean
option_focused_cb (CtkWidget * widget, CdkEventFocus * event,
		   gpointer data)
{
	CtkScrolledWindow *win = CTK_SCROLLED_WINDOW (data);
	CtkAllocation alloc;
	CtkAdjustment *adj;

	ctk_widget_get_allocation (widget, &alloc);
	adj = ctk_scrolled_window_get_vadjustment (win);
	ctk_adjustment_clamp_page (adj, alloc.y,
				   alloc.y + alloc.height);

	return FALSE;
}

/* Update xkb backend to reflect the new UI state */
static void
option_toggled_cb (CtkWidget * checkbutton, gpointer data)
{
	gpointer optionID =
	    g_object_get_data (G_OBJECT (checkbutton), OPTION_ID_PROP);
	if (ctk_toggle_button_get_active (CTK_TOGGLE_BUTTON (checkbutton)))
		xkb_options_select (optionID);
	else
		xkb_options_deselect (optionID);
}

/* Add a check_button or radio_button to control a particular option
   This function makes particular use of the current... variables at
   the top of this file. */
static void
xkb_options_add_option (XklConfigRegistry * config_registry,
			XklConfigItem * config_item, CtkBuilder * dialog)
{
	CtkWidget *option_check;
	gchar *utf_option_name = xci_desc_to_utf8 (config_item);
	/* Copy this out because we'll load it into the widget with set_data */
	gchar *full_option_name =
	    g_strdup (cafekbd_keyboard_config_merge_items
		      (current1st_level_id, config_item->name));
	gboolean initial_state;

	if (current_multi_select)
		option_check =
		    ctk_check_button_new_with_label (utf_option_name);
	else {
		if (current_radio_group == NULL) {
			/* The first radio in a group is to be "Default", meaning none of
			   the below options are to be included in the selected list.
			   This is a HIG-compliant alternative to allowing no
			   selection in the group. */
			option_check =
			    ctk_radio_button_new_with_label
			    (current_radio_group, _("Default"));
			ctk_toggle_button_set_active (CTK_TOGGLE_BUTTON
						      (option_check),
						      TRUE);
			/* Make option name underscore -
			   to enforce its first position in the list */
			g_object_set_data_full (G_OBJECT (option_check),
						"utfOptionName",
						g_strdup (" "), g_free);
			option_checks_list =
			    g_slist_append (option_checks_list,
					    option_check);
			current_radio_group =
			    ctk_radio_button_get_group (CTK_RADIO_BUTTON
							(option_check));
			current_none_radio = option_check;

			g_signal_connect (option_check, "focus-in-event",
					  G_CALLBACK (option_focused_cb),
					  WID ("options_scroll"));
		}
		option_check =
		    ctk_radio_button_new_with_label (current_radio_group,
						     utf_option_name);
		current_radio_group =
		    ctk_radio_button_get_group (CTK_RADIO_BUTTON
						(option_check));
		g_object_set_data (G_OBJECT (option_check), "NoneRadio",
				   current_none_radio);
	}

	initial_state = xkb_options_is_selected (full_option_name);

	ctk_toggle_button_set_active (CTK_TOGGLE_BUTTON (option_check),
				      initial_state);

	g_object_set_data_full (G_OBJECT (option_check), OPTION_ID_PROP,
				full_option_name, g_free);
	g_object_set_data_full (G_OBJECT (option_check), "utfOptionName",
				utf_option_name, g_free);

	g_signal_connect (option_check, "toggled",
			  G_CALLBACK (option_toggled_cb), NULL);

	option_checks_list =
	    g_slist_append (option_checks_list, option_check);

	g_signal_connect (option_check, "focus-in-event",
			  G_CALLBACK (option_focused_cb),
			  WID ("options_scroll"));

	xkb_options_expander_selcounter_add (initial_state);
}

static gint
xkb_option_checks_compare (CtkWidget * chk1, CtkWidget * chk2)
{
	const gchar *t1 =
	    g_object_get_data (G_OBJECT (chk1), "utfOptionName");
	const gchar *t2 =
	    g_object_get_data (G_OBJECT (chk2), "utfOptionName");
	return g_utf8_collate (t1, t2);
}

/* Add a group of options: create title and layout widgets and then
   add widgets for all the options in the group. */
static void
xkb_options_add_group (XklConfigRegistry * config_registry,
		       XklConfigItem * config_item, CtkBuilder * dialog)
{
	CtkWidget *vbox, *option_check;
	gboolean allow_multiple_selection =
	    GPOINTER_TO_INT (g_object_get_data (G_OBJECT (config_item),
						XCI_PROP_ALLOW_MULTIPLE_SELECTION));

	GSList *expanders_list =
	    g_object_get_data (G_OBJECT (dialog), EXPANDERS_PROP);

	gchar *utf_group_name = xci_desc_to_utf8 (config_item);
	gchar *titlemarkup =
	    g_strconcat ("<span>", utf_group_name, "</span>", NULL);

	current_expander = ctk_expander_new (titlemarkup);
	ctk_expander_set_use_markup (CTK_EXPANDER (current_expander),
				     TRUE);
	g_object_set_data_full (G_OBJECT (current_expander),
				"utfGroupName", utf_group_name, g_free);
	g_object_set_data_full (G_OBJECT (current_expander), "groupId",
				g_strdup (config_item->name), g_free);

	g_free (titlemarkup);
	vbox = ctk_box_new (CTK_ORIENTATION_VERTICAL, 6);
	ctk_widget_set_halign (vbox, CTK_ALIGN_FILL);
	ctk_widget_set_valign (vbox, CTK_ALIGN_START);
	ctk_widget_set_hexpand (vbox, TRUE);
	ctk_widget_set_vexpand (vbox, TRUE);
	ctk_widget_set_margin_top (vbox, 6);
	ctk_widget_set_margin_bottom (vbox, 12);
	ctk_widget_set_margin_start (vbox, 12);
	ctk_container_add (CTK_CONTAINER (current_expander), vbox);

	current_multi_select = (gboolean) allow_multiple_selection;
	current_radio_group = NULL;
	current1st_level_id = config_item->name;

	option_checks_list = NULL;

	xkl_config_registry_foreach_option (config_registry,
					    config_item->name,
					    (ConfigItemProcessFunc)
					    xkb_options_add_option,
					    dialog);
	/* sort it */
	option_checks_list =
	    g_slist_sort (option_checks_list,
			  (GCompareFunc) xkb_option_checks_compare);
	while (option_checks_list) {
		option_check = CTK_WIDGET (option_checks_list->data);
		ctk_box_pack_start (CTK_BOX (vbox), option_check, TRUE, TRUE, 0);
		option_checks_list = option_checks_list->next;
	}
	/* free it */
	g_slist_free (option_checks_list);
	option_checks_list = NULL;

	xkb_options_expander_highlight ();

	expanders_list = g_slist_append (expanders_list, current_expander);
	g_object_set_data (G_OBJECT (dialog), EXPANDERS_PROP,
			   expanders_list);

	g_signal_connect (current_expander, "focus-in-event",
			  G_CALLBACK (option_focused_cb),
			  WID ("options_scroll"));
}

static gint
xkb_options_expanders_compare (CtkWidget * expander1,
			       CtkWidget * expander2)
{
	const gchar *t1 =
	    g_object_get_data (G_OBJECT (expander1), "utfGroupName");
	const gchar *t2 =
	    g_object_get_data (G_OBJECT (expander2), "utfGroupName");
	return g_utf8_collate (t1, t2);
}

/* Create widgets to represent the options made available by the backend */
void
xkb_options_load_options (CtkBuilder * dialog)
{
	CtkWidget *opts_vbox = WID ("options_vbox");
	GSList *expanders_list;
	CtkWidget *expander;

	current1st_level_id = NULL;
	current_none_radio = NULL;
	current_multi_select = FALSE;
	current_radio_group = NULL;

	/* fill the list */
	xkl_config_registry_foreach_option_group (config_registry,
						  (ConfigItemProcessFunc)
						  xkb_options_add_group,
						  dialog);
	/* sort it */
	expanders_list =
	    g_object_get_data (G_OBJECT (dialog), EXPANDERS_PROP);
	expanders_list =
	    g_slist_sort (expanders_list,
			  (GCompareFunc) xkb_options_expanders_compare);
	g_object_set_data (G_OBJECT (dialog), EXPANDERS_PROP,
			   expanders_list);
	while (expanders_list) {
		expander = CTK_WIDGET (expanders_list->data);
		ctk_box_pack_start (CTK_BOX (opts_vbox), expander, FALSE,
				    FALSE, 0);
		expanders_list = expanders_list->next;
	}

	ctk_widget_show_all (opts_vbox);
}

static void
chooser_response_cb (CtkDialog * dialog, gint response, gpointer data)
{
	switch (response) {
	case CTK_RESPONSE_HELP:
		capplet_help (CTK_WINDOW (dialog),
			      "prefs-keyboard#prefs-keyboard-layoutoptions");
		break;
	case CTK_RESPONSE_CLOSE:{
			/* just cleanup */
			GSList *expanders_list =
			    g_object_get_data (G_OBJECT (dialog),
					       EXPANDERS_PROP);
			g_object_set_data (G_OBJECT (dialog),
					   EXPANDERS_PROP, NULL);
			g_slist_free (expanders_list);

			ctk_widget_destroy (CTK_WIDGET (dialog));
			chooser_dialog = NULL;
		}
		break;
	}
}

/* Create popup dialog */
void
xkb_options_popup_dialog (CtkBuilder * dialog)
{
	CtkWidget *chooser;

	chooser_dialog = ctk_builder_new ();
	ctk_builder_add_from_resource (chooser_dialog,
	                               "/org/cafe/ccc/keyboard/cafe-keyboard-properties-options-dialog.ui",
	                               NULL);

	chooser = CWID ("xkb_options_dialog");
	ctk_window_set_transient_for (CTK_WINDOW (chooser),
				      CTK_WINDOW (WID
						  ("keyboard_dialog")));
	xkb_options_load_options (chooser_dialog);

	g_signal_connect (chooser, "response",
			  G_CALLBACK (chooser_response_cb), dialog);

	ctk_dialog_run (CTK_DIALOG (chooser));
}

/* Update selected option counters for a group-bound expander */
static void
xkb_options_update_option_counters (XklConfigRegistry * config_registry,
				    XklConfigItem * config_item)
{
	gchar *full_option_name =
	    g_strdup (cafekbd_keyboard_config_merge_items
		      (current1st_level_id, config_item->name));
	gboolean current_state =
	    xkb_options_is_selected (full_option_name);
	xkb_options_expander_selcounter_add (current_state);
}

/* Respond to a change in the xkb gsettings settings */
static void
xkb_options_update (GSettings * settings, gchar * key, CtkBuilder * dialog)
{
	/* Updating options is handled by gsettings notifies for each widget
	   This is here to avoid calling it N_OPTIONS times for each gsettings
	   change. */
	enable_disable_restoring (dialog);

	if (chooser_dialog != NULL) {
		GSList *expanders_list =
		    g_object_get_data (G_OBJECT (chooser_dialog),
				       EXPANDERS_PROP);
		while (expanders_list) {
			current_expander =
			    CTK_WIDGET (expanders_list->data);
			gchar *group_id =
			    g_object_get_data (G_OBJECT (current_expander),
					       "groupId");
			current1st_level_id = group_id;
			xkb_options_expander_selcounter_reset ();
			xkl_config_registry_foreach_option
			    (config_registry, group_id,
			     (ConfigItemProcessFunc)
			     xkb_options_update_option_counters,
			     current_expander);
			xkb_options_expander_highlight ();
			expanders_list = expanders_list->next;
		}
	}
}

void
xkb_options_register_gsettings_listener (CtkBuilder * dialog)
{
	g_signal_connect (xkb_kbd_settings,
			  "changed::options",
			  G_CALLBACK (xkb_options_update),
			  dialog);
}