summaryrefslogtreecommitdiff
path: root/capplets/keyboard/gnome-keyboard-properties-xkbot.c
blob: f06ef6dce9345a9ff165e5c9ed873206546636bd (plain)
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
/* -*- mode: c; style: linux -*- */

/* gnome-keyboard-properties-xkbot.c
 * Copyright (C) 2003 Sergey V. Oudaltsov
 *
 * Written by: Sergey V. Oudaltsov <svu@users.sourceforge.net>
 *             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., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

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

#include <gnome.h>
#include <gconf/gconf-client.h>
#include <glade/glade.h>

#include "libgswitchit/gswitchit_config.h"

#include "capplet-util.h"
#include "gconf-property-editor.h"
#include "activate-settings-daemon.h"
#include "capplet-stock-icons.h"
#include <../accessibility/keyboard/accessibility-keyboard.h>

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

static const char *current1stLevelId = NULL;
static GtkWidget *currentVbox = NULL;
static GtkWidget *currentNoneRadio = NULL;
static gboolean currentMultiSelect = FALSE;
static GSList *currentRadioGroup = NULL;

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

static GtkWidget *
xkb_options_get_expander (GtkWidget * option_button)
{
  return gtk_widget_get_parent (
           gtk_widget_get_parent (
           gtk_widget_get_parent (option_button)));
}

static int
xkb_options_expander_selcounter_get (GtkWidget * expander)
{
  return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (expander), SELCOUNTER_PROP));
}

static void
xkb_options_expander_selcounter_add (GtkWidget * expander, int value)
{
  g_object_set_data (G_OBJECT (expander), SELCOUNTER_PROP,
    GINT_TO_POINTER (xkb_options_expander_selcounter_get (expander) + value));
}

static void
xkb_options_expander_highlight (GtkWidget * expander)
{
  char * utfGroupName = g_object_get_data (G_OBJECT (expander), "utfGroupName");
  int counter = xkb_options_expander_selcounter_get (expander);
  gchar *titlemarkup = g_strconcat (counter > 0 ? "<span weight=\"bold\">" : "<span>", 
                                    utfGroupName, "</span>", NULL);
  gtk_expander_set_label (GTK_EXPANDER (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 *optionsList = xkb_options_get_selected_list ();
  GSList *option;
  for (option = optionsList ; option != NULL ; option = option->next)
    if (!strcmp ((gchar*)option->data, optionname))
      already_selected = TRUE;

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

  clear_xkb_elements_list (optionsList);
}

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

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

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

/* Update UI state from xkb backend */
static void
option_update_cb (GConfClient * client,
                         guint cnxn_id, GConfEntry * entry, gpointer data)
{
  GtkToggleButton *toggle = GTK_TOGGLE_BUTTON (data);
  GtkWidget *expander = xkb_options_get_expander (GTK_WIDGET (toggle));
  gboolean old_state = gtk_toggle_button_get_active (toggle);
  gboolean new_state = xkb_options_is_selected (
                         g_object_get_data (G_OBJECT (toggle), OPTION_ID_PROP));
  int old_gstate = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toggle), GCONFSTATE_PROP));
  int state_diff = new_state - old_gstate;

  if (GTK_WIDGET_TYPE (toggle) == GTK_TYPE_RADIO_BUTTON &&
      old_state == TRUE && new_state == FALSE)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (g_object_get_data (G_OBJECT (toggle), "NoneRadio")), TRUE);
  else
    gtk_toggle_button_set_active (toggle, new_state);

  g_object_set_data (G_OBJECT (toggle), GCONFSTATE_PROP, GINT_TO_POINTER (new_state));
  xkb_options_expander_selcounter_add (expander, state_diff);
  xkb_options_expander_highlight (expander);
}

/* 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 (const XklConfigItemPtr
                                          configItem, GladeXML * dialog)
{
  GtkWidget *option_check;
  gchar *utfOptionName = xci_desc_to_utf8 (configItem);
  /* Copy this out because we'll load it into the widget with set_data */
  gchar *fullOptionName = g_strdup(
    GSwitchItKbdConfigMergeItems (current1stLevelId, configItem->name));
  gboolean initial_state;

  if (currentMultiSelect)
    option_check = gtk_check_button_new_with_label (utfOptionName);
  else
    {
      if (currentRadioGroup == 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 = gtk_radio_button_new_with_label (currentRadioGroup, _("Default"));
          gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (option_check), TRUE);
          gtk_box_pack_start_defaults (GTK_BOX (currentVbox), option_check);
          currentRadioGroup = gtk_radio_button_get_group (GTK_RADIO_BUTTON (option_check));
          currentNoneRadio = option_check;
        }
      option_check = gtk_radio_button_new_with_label (currentRadioGroup, utfOptionName);
      currentRadioGroup = gtk_radio_button_get_group (GTK_RADIO_BUTTON (option_check));
      g_object_set_data (G_OBJECT (option_check), "NoneRadio", currentNoneRadio);
    }
  g_free (utfOptionName);

  initial_state = xkb_options_is_selected (fullOptionName);

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (option_check), initial_state);

  g_object_set_data_full (G_OBJECT (option_check), OPTION_ID_PROP, fullOptionName, g_free);

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

  gconf_client_notify_add (xkbGConfClient,
			   GSWITCHIT_KBD_CONFIG_KEY_OPTIONS,
			   (GConfClientNotifyFunc)
			   option_update_cb, option_check, NULL, NULL);

  gtk_box_pack_start_defaults (GTK_BOX (currentVbox), option_check);

  xkb_options_expander_selcounter_add (xkb_options_get_expander (option_check), initial_state);
  g_object_set_data (G_OBJECT (option_check), GCONFSTATE_PROP, GINT_TO_POINTER (initial_state));
}

/* 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 (const XklConfigItemPtr
                                         configItem,
                                         Bool allowMultipleSelection,
                                         GladeXML * dialog)
{
  GtkWidget *expander, *align, *vbox;

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

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

  expander = gtk_expander_new (titlemarkup);
  g_object_set_data_full (G_OBJECT (expander), "utfGroupName", utfGroupName, g_free);

  g_free (titlemarkup);
  gtk_expander_set_use_markup (GTK_EXPANDER (expander), TRUE);
  align = gtk_alignment_new (0, 0, 1, 1);
  gtk_alignment_set_padding (GTK_ALIGNMENT (align), 6, 12, 12, 0);
  vbox = gtk_vbox_new (TRUE, 6);
  gtk_container_add (GTK_CONTAINER (align), vbox);
  gtk_container_add (GTK_CONTAINER (expander), align);
  currentVbox = vbox;

  currentMultiSelect = (gboolean) allowMultipleSelection;
  currentRadioGroup = NULL;

  current1stLevelId = configItem->name;
  XklConfigEnumOptions (configItem->name, (ConfigItemProcessFunc)
                        xkb_options_add_option, dialog);

  xkb_options_expander_highlight (expander);

  expanders_list = g_slist_append (expanders_list, expander);
  g_object_set_data (G_OBJECT (dialog), EXPANDERS_PROP, expanders_list);
}

static gint
xkb_options_expanders_compare (GtkWidget * expander1, GtkWidget * expander2)
{
  const gchar *t1 = gtk_expander_get_label (GTK_EXPANDER (expander1));
  const gchar *t2 = gtk_expander_get_label (GTK_EXPANDER (expander2));
  return g_utf8_collate (t1, t2);
}

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

  /* fill the list */
  XklConfigEnumOptionGroups ((GroupProcessFunc)
			     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);
  while (expanders_list)
  {
    expander = GTK_WIDGET (expanders_list->data);
    gtk_box_pack_start (GTK_BOX (opts_vbox), expander, FALSE, FALSE, 0);
    expanders_list = expanders_list->next;
  }

  /* just cleanup */
  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);

  gtk_widget_show_all (opts_vbox);
}

/* Respond to a change in the xkb gconf settings */
static void
xkb_options_update (GConfClient * client,
                    guint cnxn_id, GConfEntry * entry, GladeXML * dialog)
{
  /* Updating options is handled by gconf notifies for each widget
     This is here to avoid calling it N_OPTIONS times for each gconf
     change.*/
  enable_disable_restoring (dialog);
}

void
xkb_options_register_gconf_listener (GladeXML * dialog)
{
  gconf_client_notify_add (xkbGConfClient,
			   GSWITCHIT_KBD_CONFIG_KEY_OPTIONS,
			   (GConfClientNotifyFunc)
			   xkb_options_update, dialog, NULL, NULL);
}