xkb handling of new keyboards
[lxde/lxpanel.git] / src / plugins / xkb / xkb-plugin.c
CommitLineData
438d9fdb
MJ
1/**
2 * Copyright (c) 2010 LxDE Developers, see the file AUTHORS for details.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19/* Originally derived from xfce4-xkb-plugin, Copyright 2004 Alexander Iliev,
20 * which credits Michael Glickman. */
22242ed4 21
4496e880
JL
22/* Modified by Giuseppe Penone <giuspen@gmail.com> starting from 2012-07 and lxpanel 0.5.10 */
23
22242ed4
HJYP
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include <glib/gi18n.h>
29
30#include <stdio.h>
45bd719e 31#include <stdlib.h>
22242ed4
HJYP
32#include <ctype.h>
33
34#include "xkb.h"
35
4496e880
JL
36enum
37{
38 COLUMN_ICON,
39 COLUMN_LAYOUT,
40 COLUMN_VARIANT,
41 NUM_COLUMNS
42};
43
44enum
45{
46 COLUMN_ADD_ICON,
47 COLUMN_ADD_LAYOUT,
48 COLUMN_ADD_DESC,
49 NUM_ADD_COLUMNS
50};
51
52enum
53{
54 COLUMN_MODEL_NAME,
55 COLUMN_MODEL_DESC,
56 NUM_MODEL_COLUMNS
57};
58
59enum
60{
61 COLUMN_CHANGE_NAME,
62 COLUMN_CHANGE_DESC,
63 NUM_CHANGE_COLUMNS
64};
65
18ecfe2a 66void panel_config_save(Panel * panel); /* defined in configurator.c */
67
4496e880
JL
68static int xkb_constructor(Plugin * p_plugin, char ** fp);
69static void xkb_destructor(Plugin * p_plugin);
70static void xkb_configure(Plugin * p, GtkWindow * parent);
71static void xkb_save_configuration(Plugin * p, FILE * fp);
72static void xkb_panel_configuration_changed(Plugin * p);
73static void xkb_settings_fill_layout_tree_model_with_config(XkbPlugin *p_xkb);
4496e880
JL
74static void xkb_update_layouts_n_variants(XkbPlugin *p_xkb);
75static void xkb_add_layout(XkbPlugin *p_xkb, gchar *layout, gchar*variant);
76static int xkb_get_flag_size(XkbPlugin *p_xkb);
77
895da737 78static void on_xkb_fbev_active_window_event(FbEv *ev, gpointer p_data);
4496e880
JL
79static gboolean on_xkb_button_scroll_event(GtkWidget * widget, GdkEventScroll * event, gpointer p_data);
80static gboolean on_xkb_button_press_event(GtkWidget * widget, GdkEventButton * event, gpointer p_data);
81static void on_radiobutton_disp_type_image_toggled(GtkToggleButton *p_radiobutton, gpointer p_data);
82static void on_radiobutton_disp_type_image_cust_toggled(GtkToggleButton *p_radiobutton, gpointer p_data);
83static void on_radiobutton_disp_type_text_toggled(GtkToggleButton *p_radiobutton, gpointer p_data);
895da737 84static void on_xkb_checkbutton_per_app_toggled(GtkToggleButton *tb, gpointer p_data);
4496e880
JL
85static void on_dialog_config_response(GtkDialog *p_dialog, gint response, gpointer p_data);
86
87static unsigned char user_active = FALSE;
88static const gchar flag_filepath_generator[] = "%s/%s.png";
89
90static int xkb_get_flag_size(XkbPlugin *p_xkb)
91{
92 int size = p_xkb->p_plugin->panel->icon_size;
93 switch(p_xkb->flag_size)
94 {
95 case 1: return size*0.5;
96 case 2: return size*0.6;
97 case 3: return size*0.7;
98 case 4: return size*0.8;
895da737 99 case 5: return size*0.9;
4496e880
JL
100 default: break;
101 }
895da737 102 return size;
4496e880 103}
d862b520 104
105/* Redraw the graphics. */
4496e880 106void xkb_redraw(XkbPlugin *p_xkb)
22242ed4 107{
d862b520 108 /* Set the image. */
109 gboolean valid_image = FALSE;
4496e880
JL
110 int size = xkb_get_flag_size(p_xkb);
111 if( (p_xkb->display_type == DISP_TYPE_IMAGE) || (p_xkb->display_type == DISP_TYPE_IMAGE_CUST) )
22242ed4 112 {
4496e880
JL
113 char * group_name = (char *)xkb_get_current_symbol_name_lowercase(p_xkb);
114 if(group_name != NULL)
d862b520 115 {
4496e880
JL
116 gchar *flag_filepath = NULL;
117 gchar *flags_dir = (p_xkb->cust_dir_exists && (p_xkb->display_type == DISP_TYPE_IMAGE_CUST)) ? g_strdup(FLAGSCUSTDIR):g_strdup(FLAGSDIR);
118 if(strchr(group_name, '/') != NULL)
119 {
120 gchar *layout_mod = g_strdup(group_name);
121 layout_mod = g_strdelimit(layout_mod, "/", '-');
122 flag_filepath = g_strdup_printf(flag_filepath_generator, flags_dir, layout_mod);
123 g_free(layout_mod);
124 }
125 else
126 {
127 flag_filepath = g_strdup_printf(flag_filepath_generator, flags_dir, group_name);
128 }
129 GdkPixbuf * unscaled_pixbuf = gdk_pixbuf_new_from_file(flag_filepath, NULL);
130 g_free(flag_filepath);
131 g_free(flags_dir);
438d9fdb
MJ
132 g_free(group_name);
133
4496e880 134 if(unscaled_pixbuf != NULL)
d862b520 135 {
438d9fdb
MJ
136 /* Loaded successfully. */
137 int width = gdk_pixbuf_get_width(unscaled_pixbuf);
138 int height = gdk_pixbuf_get_height(unscaled_pixbuf);
139 GdkPixbuf * pixbuf = gdk_pixbuf_scale_simple(unscaled_pixbuf, size * width / height, size, GDK_INTERP_BILINEAR);
4496e880 140 if(pixbuf != NULL)
438d9fdb 141 {
4496e880 142 gtk_image_set_from_pixbuf(GTK_IMAGE(p_xkb->p_image), pixbuf);
438d9fdb 143 g_object_unref(G_OBJECT(pixbuf));
4496e880
JL
144 gtk_widget_hide(p_xkb->p_label);
145 gtk_widget_show(p_xkb->p_image);
146 gtk_widget_set_tooltip_text(p_xkb->p_plugin->pwid, xkb_get_current_group_name(p_xkb));
438d9fdb
MJ
147 valid_image = TRUE;
148 }
149 g_object_unref(unscaled_pixbuf);
22242ed4
HJYP
150 }
151 }
152 }
153
d862b520 154 /* Set the label. */
4496e880 155 if( (p_xkb->display_type == DISP_TYPE_TEXT) || ( ! valid_image) )
d862b520 156 {
4496e880 157 char *group_name = (char *)xkb_get_current_symbol_name(p_xkb);
438d9fdb
MJ
158 if (group_name != NULL)
159 {
4496e880 160 GString *p_gstring_markup = g_string_new("");
895da737
JL
161 g_string_printf(p_gstring_markup,
162 "<span font='%u' weight='heavy' color=\"#%06x\">%s</span>",
163 size/2, gcolor2rgb24(&p_xkb->p_plugin->panel->gfontcolor), group_name);
4496e880
JL
164 gtk_label_set_markup(GTK_LABEL(p_xkb->p_label), p_gstring_markup->str);
165 g_string_free(p_gstring_markup, TRUE/*free also gstring->str*/);
166 gtk_widget_hide(p_xkb->p_image);
167 gtk_widget_show(p_xkb->p_label);
168 gtk_widget_set_tooltip_text(p_xkb->p_plugin->pwid, xkb_get_current_group_name(p_xkb));
d862b520 169 }
170 }
22242ed4
HJYP
171}
172
895da737
JL
173/* Handler for "active_window" event on root window listener. */
174static void on_xkb_fbev_active_window_event(FbEv * ev, gpointer p_data)
175{
176 XkbPlugin * xkb = (XkbPlugin *)p_data;
177 if (xkb->enable_perwin)
178 {
179 Window * win = fb_ev_active_window(ev);
180 if (*win != None)
181 {
182 xkb_active_window_changed(xkb, *win);
183 xkb_redraw(xkb);
184 }
185 }
186}
187
d862b520 188/* Handler for "scroll-event" on drawing area. */
4496e880 189static gboolean on_xkb_button_scroll_event(GtkWidget * widget, GdkEventScroll * event, gpointer p_data)
22242ed4 190{
4496e880 191 XkbPlugin * xkb = (XkbPlugin *)p_data;
22242ed4 192
d862b520 193 /* Change to next or previous group. */
194 xkb_change_group(xkb,
195 (((event->direction == GDK_SCROLL_UP) || (event->direction == GDK_SCROLL_RIGHT)) ? 1 : -1));
196 return TRUE;
22242ed4
HJYP
197}
198
d862b520 199/* Handler for button-press-event on top level widget. */
4496e880 200static gboolean on_xkb_button_press_event(GtkWidget * widget, GdkEventButton * event, gpointer p_data)
22242ed4 201{
4496e880 202 XkbPlugin * xkb = (XkbPlugin *)p_data;
22242ed4 203
d862b520 204 /* Standard right-click handling. */
4496e880 205 if (plugin_button_press_event(widget, event, xkb->p_plugin))
d862b520 206 return TRUE;
22242ed4 207
d862b520 208 /* Change to next group. */
209 xkb_change_group(xkb, 1);
18ecfe2a 210 return TRUE;
22242ed4
HJYP
211}
212
d862b520 213/* Plugin constructor. */
4496e880 214static int xkb_constructor(Plugin * p_plugin, char ** fp)
22242ed4 215{
d862b520 216 /* Allocate plugin context and set into Plugin private data pointer. */
4496e880
JL
217 XkbPlugin * p_xkb = g_new0(XkbPlugin, 1);
218 p_xkb->p_plugin = p_plugin;
219 p_plugin->priv = p_xkb;
d862b520 220
221 /* Initialize to defaults. */
4496e880 222 p_xkb->display_type = DISP_TYPE_IMAGE;
895da737 223 p_xkb->enable_perwin = FALSE;
4496e880
JL
224 p_xkb->kbd_model = NULL;
225 p_xkb->kbd_layouts = NULL;
226 p_xkb->kbd_variants = NULL;
227 p_xkb->kbd_change_option = NULL;
228 p_xkb->flag_size = 3;
229 p_xkb->cust_dir_exists = g_file_test(FLAGSCUSTDIR, G_FILE_TEST_IS_DIR);
d862b520 230
231 /* Load parameters from the configuration file. */
232 line s;
233 s.len = 256;
234 if (fp != NULL)
235 {
236 while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END)
237 {
238 if (s.type == LINE_NONE)
239 {
240 ERR( "xkb: illegal token %s\n", s.str);
241 return 0;
242 }
243 if (s.type == LINE_VAR)
244 {
4496e880
JL
245 if(g_ascii_strcasecmp(s.t[0], "DisplayType") == 0)
246 {
247 p_xkb->display_type = atoi(s.t[1]);
248 }
895da737
JL
249 else if(g_ascii_strcasecmp(s.t[0], "PerWinLayout") == 0)
250 {
251 p_xkb->enable_perwin = str2num(bool_pair, s.t[1], 0);
252 }
4496e880
JL
253 else if(g_ascii_strcasecmp(s.t[0], "Model") == 0)
254 {
255 p_xkb->kbd_model = g_strdup(s.t[1]);
256 }
257 else if(g_ascii_strcasecmp(s.t[0], "LayoutsList") == 0)
258 {
259 p_xkb->kbd_layouts = g_strdup(s.t[1]);
260 }
261 else if(g_ascii_strcasecmp(s.t[0], "VariantsList") == 0)
262 {
263 p_xkb->kbd_variants = g_strdup(s.t[1]);
264 }
265 else if(g_ascii_strcasecmp(s.t[0], "ToggleOpt") == 0)
266 {
267 p_xkb->kbd_change_option = g_strdup(s.t[1]);
268 }
269 else if(g_ascii_strcasecmp(s.t[0], "FlagSize") == 0)
270 {
271 p_xkb->flag_size = atoi(s.t[1]);
272 }
d862b520 273 else
274 ERR( "xkb: unknown var %s\n", s.t[0]);
275 }
276 else
277 {
278 ERR( "xkb: illegal in this context %s\n", s.str);
279 return 0;
280 }
281 }
282 }
2918994e 283
d862b520 284 /* Allocate top level widget and set into Plugin widget pointer. */
4496e880
JL
285 p_plugin->pwid = gtk_event_box_new();
286 gtk_widget_add_events(p_plugin->pwid, GDK_BUTTON_PRESS_MASK);
d862b520 287
288 /* Create a horizontal box as the child of the button. */
289 GtkWidget * hbox = gtk_hbox_new(FALSE, 0);
4496e880
JL
290 gtk_container_set_border_width(GTK_CONTAINER(hbox), 1);
291 gtk_container_add(GTK_CONTAINER(p_xkb->p_plugin->pwid), hbox);
d862b520 292 gtk_widget_show(hbox);
293
294 /* Create a label and an image as children of the horizontal box.
295 * Only one of these is visible at a time, controlled by user preference
296 * and the successful loading of the image. */
4496e880
JL
297 p_xkb->p_label = gtk_label_new("");
298 gtk_container_add(GTK_CONTAINER(hbox), p_xkb->p_label);
299 p_xkb->p_image = gtk_image_new();
300 gtk_container_add(GTK_CONTAINER(hbox), p_xkb->p_image);
d862b520 301
895da737
JL
302 /* Chech for first run */
303 if( (p_xkb->kbd_model == NULL) || (p_xkb->kbd_layouts == NULL) ||
304 (p_xkb->kbd_variants == NULL) || (p_xkb->kbd_change_option == NULL) )
305 {
306 /* This is a first run, read the current layout */
307 xkb_mechanism_constructor(p_xkb);
308
309 if(p_xkb->kbd_model != NULL) g_free(p_xkb->kbd_model);
310 if(p_xkb->kbd_layouts != NULL) g_free(p_xkb->kbd_layouts);
311 if(p_xkb->kbd_variants != NULL) g_free(p_xkb->kbd_variants);
312 if(p_xkb->kbd_change_option != NULL) g_free(p_xkb->kbd_change_option);
313
314 p_xkb->kbd_model = g_strdup("pc105");
315 gchar *symbol_name_lowercase = (char *)xkb_get_current_symbol_name_lowercase(p_xkb);
316 p_xkb->kbd_layouts = g_strdup(symbol_name_lowercase);
317 g_free(symbol_name_lowercase);
318 p_xkb->kbd_variants = g_strdup(",");
319 p_xkb->kbd_change_option = g_strdup("shift_caps_toggle");
320
321 xkb_mechanism_destructor(p_xkb);
322 }
323
d862b520 324 /* Initialize the XKB interface. */
895da737 325 xkb_setxkbmap(p_xkb);
4496e880 326 xkb_mechanism_constructor(p_xkb);
d862b520 327
d862b520 328 /* Connect signals. */
4496e880
JL
329 g_signal_connect(p_plugin->pwid, "button-press-event", G_CALLBACK(on_xkb_button_press_event), p_xkb);
330 g_signal_connect(p_plugin->pwid, "scroll-event", G_CALLBACK(on_xkb_button_scroll_event), p_xkb);
895da737 331 g_signal_connect(G_OBJECT(fbev), "active_window", G_CALLBACK(on_xkb_fbev_active_window_event), p_xkb);
d862b520 332
333 /* Show the widget and return. */
4496e880
JL
334 xkb_redraw(p_xkb);
335 gtk_widget_show(p_plugin->pwid);
d862b520 336 return 1;
22242ed4
HJYP
337}
338
d862b520 339/* Plugin destructor. */
4496e880 340static void xkb_destructor(Plugin * p_plugin)
22242ed4 341{
4496e880 342 XkbPlugin *p_xkb = (XkbPlugin *)p_plugin->priv;
22242ed4 343
895da737
JL
344 /* Disconnect root window event handler. */
345 g_signal_handlers_disconnect_by_func(G_OBJECT(fbev), on_xkb_fbev_active_window_event, p_xkb);
346
d862b520 347 /* Disconnect from the XKB mechanism. */
4496e880
JL
348 g_source_remove(p_xkb->source_id);
349 xkb_mechanism_destructor(p_xkb);
22242ed4 350
d862b520 351 /* Ensure that the configuration dialog is dismissed. */
4496e880
JL
352 if(p_xkb->p_dialog_config != NULL)
353 gtk_widget_destroy(p_xkb->p_dialog_config);
22242ed4 354
d862b520 355 /* Deallocate all memory. */
4496e880
JL
356 g_free(p_xkb->kbd_model);
357 g_free(p_xkb->kbd_layouts);
358 g_free(p_xkb->kbd_variants);
359 g_free(p_xkb->kbd_change_option);
360 g_free(p_xkb);
361}
362
895da737
JL
363/* Handler for "toggled" event on per-application check box of configuration dialog. */
364static void on_xkb_checkbutton_per_app_toggled(GtkToggleButton *tb, gpointer p_data)
365{
366 if(user_active == TRUE)
367 {
368 /* Fetch the new value and redraw. */
369 XkbPlugin * xkb = (XkbPlugin *)p_data;
370 xkb->enable_perwin = gtk_toggle_button_get_active(tb);
371 if(!xkb->enable_perwin)
372 {
373 /* at deactivation clear the hash table */
374 if(xkb->p_hash_table_group != NULL)
375 g_hash_table_destroy(xkb->p_hash_table_group);
376 xkb->p_hash_table_group = g_hash_table_new(g_direct_hash, NULL);
377 }
378 xkb_redraw(xkb);
379 }
380}
381
4496e880
JL
382static void on_radiobutton_flag_size_1_toggled(GtkToggleButton *p_radiobutton, gpointer p_data)
383{
384 if( (user_active == TRUE) && gtk_toggle_button_get_active(p_radiobutton) )
385 {
386 XkbPlugin * p_xkb = (XkbPlugin *)p_data;
387 p_xkb->flag_size = 1;
388 xkb_redraw(p_xkb);
389 }
390}
391
392static void on_radiobutton_flag_size_2_toggled(GtkToggleButton *p_radiobutton, gpointer p_data)
393{
394 if( (user_active == TRUE) && gtk_toggle_button_get_active(p_radiobutton) )
395 {
396 XkbPlugin * p_xkb = (XkbPlugin *)p_data;
397 p_xkb->flag_size = 2;
398 xkb_redraw(p_xkb);
399 }
22242ed4
HJYP
400}
401
4496e880 402static void on_radiobutton_flag_size_3_toggled(GtkToggleButton *p_radiobutton, gpointer p_data)
22242ed4 403{
4496e880
JL
404 if( (user_active == TRUE) && gtk_toggle_button_get_active(p_radiobutton) )
405 {
406 XkbPlugin * p_xkb = (XkbPlugin *)p_data;
407 p_xkb->flag_size = 3;
408 xkb_redraw(p_xkb);
409 }
22242ed4
HJYP
410}
411
4496e880 412static void on_radiobutton_flag_size_4_toggled(GtkToggleButton *p_radiobutton, gpointer p_data)
22242ed4 413{
4496e880
JL
414 if( (user_active == TRUE) && gtk_toggle_button_get_active(p_radiobutton) )
415 {
416 XkbPlugin * p_xkb = (XkbPlugin *)p_data;
417 p_xkb->flag_size = 4;
418 xkb_redraw(p_xkb);
419 }
22242ed4
HJYP
420}
421
4496e880 422static void on_radiobutton_flag_size_5_toggled(GtkToggleButton *p_radiobutton, gpointer p_data)
22242ed4 423{
4496e880
JL
424 if( (user_active == TRUE) && gtk_toggle_button_get_active(p_radiobutton) )
425 {
426 XkbPlugin * p_xkb = (XkbPlugin *)p_data;
427 p_xkb->flag_size = 5;
428 xkb_redraw(p_xkb);
429 }
430}
431
895da737
JL
432static void on_radiobutton_flag_size_6_toggled(GtkToggleButton *p_radiobutton, gpointer p_data)
433{
434 if( (user_active == TRUE) && gtk_toggle_button_get_active(p_radiobutton) )
435 {
436 XkbPlugin * p_xkb = (XkbPlugin *)p_data;
437 p_xkb->flag_size = 6;
438 xkb_redraw(p_xkb);
439 }
440}
441
4496e880
JL
442/* Handler for "toggled" event on display type image radiobutton of configuration dialog. */
443static void on_radiobutton_disp_type_image_toggled(GtkToggleButton *p_radiobutton, gpointer p_data)
444{
445 if( (user_active == TRUE) && gtk_toggle_button_get_active(p_radiobutton) )
446 {
447 /* Fetch the new value and redraw. */
448 XkbPlugin * p_xkb = (XkbPlugin *)p_data;
449 p_xkb->display_type = DISP_TYPE_IMAGE;
450 xkb_redraw(p_xkb);
451 }
452}
453
454/* Handler for "toggled" event on display type image radiobutton of configuration dialog. */
455static void on_radiobutton_disp_type_image_cust_toggled(GtkToggleButton *p_radiobutton, gpointer p_data)
456{
457 if( (user_active == TRUE) && gtk_toggle_button_get_active(p_radiobutton) )
458 {
459 /* Fetch the new value and redraw. */
460 XkbPlugin * p_xkb = (XkbPlugin *)p_data;
461 p_xkb->display_type = DISP_TYPE_IMAGE_CUST;
462 xkb_redraw(p_xkb);
463 }
464}
465
466/* Handler for "toggled" event on display type image radiobutton of configuration dialog. */
467static void on_radiobutton_disp_type_text_toggled(GtkToggleButton *p_radiobutton, gpointer p_data)
468{
469 if( (user_active == TRUE) && gtk_toggle_button_get_active(p_radiobutton) )
470 {
471 /* Fetch the new value and redraw. */
472 XkbPlugin * p_xkb = (XkbPlugin *)p_data;
473 p_xkb->display_type = DISP_TYPE_TEXT;
474 xkb_redraw(p_xkb);
475 }
22242ed4
HJYP
476}
477
d862b520 478/* Handler for "response" event on configuration dialog. */
4496e880 479static void on_dialog_config_response(GtkDialog *p_dialog, gint response, gpointer data)
22242ed4 480{
4496e880 481 XkbPlugin * p_xkb = (XkbPlugin *)data;
22242ed4 482
d862b520 483 /* Save the new configuration and redraw the plugin. */
4496e880
JL
484 panel_config_save(p_xkb->p_plugin->panel);
485 xkb_redraw(p_xkb);
22242ed4 486
d862b520 487 /* Destroy the dialog. */
4496e880
JL
488 gtk_widget_destroy(p_xkb->p_dialog_config);
489 p_xkb->p_dialog_config = NULL;
490}
491
895da737
JL
492static gboolean on_treeviews_lists_button_press_event(GtkWidget *p_widget,
493 GdkEventButton *p_event,
494 gpointer p_data)
4496e880 495{
895da737
JL
496 if(p_event->button == 1)
497 {
498 if(p_event->type == GDK_2BUTTON_PRESS)
499 {
500 GtkButton *p_button_ok = (GtkButton *)p_data;
501 gtk_button_clicked(p_button_ok);
502 }
503 }
504 return FALSE;
4496e880
JL
505}
506
507static void on_button_kbd_model_clicked(GtkButton *p_button, gpointer *p_data)
508{
509 XkbPlugin *p_xkb = (XkbPlugin *)p_data;
510
511 // dialog
512 GtkWidget *p_dialog = gtk_dialog_new_with_buttons(_("Select Keyboard Model"),
513 GTK_WINDOW(p_xkb->p_dialog_config),
514 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
515 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
516 GTK_STOCK_OK, GTK_RESPONSE_OK,
517 NULL);
518
519 // scrolledwindow
520 GtkWidget * p_scrolledwindow_kbd_model = gtk_scrolled_window_new(NULL, NULL);
521 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(p_scrolledwindow_kbd_model),
522 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
523 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(p_dialog)->vbox), p_scrolledwindow_kbd_model, TRUE, TRUE, 2);
524
525 // liststore
526 GtkListStore *p_liststore_kbd_model = gtk_list_store_new(NUM_MODEL_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
527 GtkWidget *p_treeview_kbd_model = gtk_tree_view_new_with_model(GTK_TREE_MODEL(p_liststore_kbd_model));
528 g_object_unref(G_OBJECT(p_liststore_kbd_model));
529 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(p_treeview_kbd_model), TRUE);
530 gtk_container_add(GTK_CONTAINER(p_scrolledwindow_kbd_model), p_treeview_kbd_model);
531 GtkCellRenderer *p_renderer;
532 GtkTreeViewColumn *p_column;
533 // model
534 p_renderer = gtk_cell_renderer_text_new();
535 p_column = gtk_tree_view_column_new_with_attributes(_("Model"), p_renderer, "text", COLUMN_CHANGE_NAME, NULL);
536 gtk_tree_view_append_column(GTK_TREE_VIEW(p_treeview_kbd_model), p_column);
537 // desc
538 p_renderer = gtk_cell_renderer_text_new();
539 p_column = gtk_tree_view_column_new_with_attributes(_("Description"), p_renderer, "text", COLUMN_CHANGE_DESC, NULL);
540 gtk_tree_view_append_column(GTK_TREE_VIEW(p_treeview_kbd_model), p_column);
541
542 // populate model
543 GKeyFile *p_keyfile = g_key_file_new();
544 gchar *xkbcfg_filepath = g_strdup_printf("%s/models.cfg", XKBCONFDIR);
545 if(g_key_file_load_from_file(p_keyfile, xkbcfg_filepath, 0, NULL))
546 {
547 gchar **keys_models = g_key_file_get_keys(p_keyfile, "MODELS", NULL, NULL);
548 guint model_idx = 0;
549 GtkTreeIter tree_iter;
550 gchar *p_model_desc;
551 while(keys_models[model_idx] != NULL)
552 {
553 p_model_desc = g_key_file_get_string(p_keyfile, "MODELS", keys_models[model_idx], NULL);
554 gtk_list_store_append(p_liststore_kbd_model, &tree_iter);
555 gtk_list_store_set(p_liststore_kbd_model, &tree_iter,
556 COLUMN_MODEL_NAME, keys_models[model_idx],
557 COLUMN_MODEL_DESC, p_model_desc,
558 -1);
559 g_free(p_model_desc);
560 model_idx++;
561 }
562 g_strfreev(keys_models);
563 g_key_file_free(p_keyfile);
564 }
565 g_free(xkbcfg_filepath);
566
567 // callback for double click
4496e880 568 g_signal_connect(p_treeview_kbd_model, "button-press-event",
895da737
JL
569 G_CALLBACK(on_treeviews_lists_button_press_event),
570 gtk_dialog_get_widget_for_response(GTK_DIALOG(p_dialog), GTK_RESPONSE_OK));
4496e880
JL
571 gtk_widget_set_size_request(p_dialog, 600, 500);
572 gtk_widget_show_all(GTK_WIDGET(p_scrolledwindow_kbd_model));
573 gint response = gtk_dialog_run(GTK_DIALOG(p_dialog));
574 if(response == GTK_RESPONSE_OK)
575 {
576 GtkTreeIter tree_iter_sel;
577 GtkTreeSelection *p_treeselection_kbd_model = gtk_tree_view_get_selection(GTK_TREE_VIEW(p_treeview_kbd_model));
578 if(gtk_tree_selection_get_selected(p_treeselection_kbd_model,
579 (GtkTreeModel **)(&p_liststore_kbd_model),
580 &tree_iter_sel))
581 {
582 gchar *kbd_model_new;
583 gtk_tree_model_get(GTK_TREE_MODEL(p_liststore_kbd_model),
584 &tree_iter_sel, COLUMN_MODEL_NAME, &kbd_model_new, -1);
585 g_free(p_xkb->kbd_model);
586 p_xkb->kbd_model = g_strdup(kbd_model_new);
587 gtk_button_set_label(GTK_BUTTON(p_xkb->p_button_kbd_model), p_xkb->kbd_model);
588 g_free(kbd_model_new);
589 xkb_setxkbmap(p_xkb);
590 xkb_redraw(p_xkb);
591 }
592 }
593 gtk_widget_destroy(p_dialog);
594}
595
596static void on_button_kbd_change_layout_clicked(GtkButton *p_button, gpointer *p_data)
597{
598 XkbPlugin *p_xkb = (XkbPlugin *)p_data;
599
600 // dialog
601 GtkWidget *p_dialog = gtk_dialog_new_with_buttons(_("Select Layout Change Type"),
602 GTK_WINDOW(p_xkb->p_dialog_config),
603 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
604 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
605 GTK_STOCK_OK, GTK_RESPONSE_OK,
606 NULL);
607
608 // scrolledwindow
609 GtkWidget * p_scrolledwindow_kbd_change = gtk_scrolled_window_new(NULL, NULL);
610 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(p_scrolledwindow_kbd_change),
611 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
612 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(p_dialog)->vbox), p_scrolledwindow_kbd_change, TRUE, TRUE, 2);
613
614 // liststore
615 GtkListStore *p_liststore_kbd_change = gtk_list_store_new(NUM_CHANGE_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
616 GtkWidget *p_treeview_kbd_change = gtk_tree_view_new_with_model(GTK_TREE_MODEL(p_liststore_kbd_change));
617 g_object_unref(G_OBJECT(p_liststore_kbd_change));
618 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(p_treeview_kbd_change), TRUE);
619 gtk_container_add(GTK_CONTAINER(p_scrolledwindow_kbd_change), p_treeview_kbd_change);
620 GtkCellRenderer *p_renderer;
621 GtkTreeViewColumn *p_column;
622 // model
623 p_renderer = gtk_cell_renderer_text_new();
624 p_column = gtk_tree_view_column_new_with_attributes(_("Change Type"), p_renderer, "text", COLUMN_CHANGE_NAME, NULL);
625 gtk_tree_view_append_column(GTK_TREE_VIEW(p_treeview_kbd_change), p_column);
626 // desc
627 p_renderer = gtk_cell_renderer_text_new();
628 p_column = gtk_tree_view_column_new_with_attributes(_("Description"), p_renderer, "text", COLUMN_CHANGE_DESC, NULL);
629 gtk_tree_view_append_column(GTK_TREE_VIEW(p_treeview_kbd_change), p_column);
630
631 // populate model
632 GKeyFile *p_keyfile = g_key_file_new();
633 gchar *xkbcfg_filepath = g_strdup_printf("%s/toggle.cfg", XKBCONFDIR);
634 if(g_key_file_load_from_file(p_keyfile, xkbcfg_filepath, 0, NULL))
635 {
636 gchar **keys_changes = g_key_file_get_keys(p_keyfile, "TOGGLE", NULL, NULL);
637 guint change_idx = 0;
638 GtkTreeIter tree_iter;
639 gchar *p_change_desc;
640 while(keys_changes[change_idx] != NULL)
641 {
642 p_change_desc = g_key_file_get_string(p_keyfile, "TOGGLE", keys_changes[change_idx], NULL);
643 gtk_list_store_append(p_liststore_kbd_change, &tree_iter);
644 gtk_list_store_set(p_liststore_kbd_change, &tree_iter,
645 COLUMN_CHANGE_NAME, keys_changes[change_idx],
646 COLUMN_CHANGE_DESC, p_change_desc,
647 -1);
648 g_free(p_change_desc);
649 change_idx++;
650 }
651 g_strfreev(keys_changes);
652 g_key_file_free(p_keyfile);
653 }
654 g_free(xkbcfg_filepath);
655
656 // callback for double click
4496e880 657 g_signal_connect(p_treeview_kbd_change, "button-press-event",
895da737
JL
658 G_CALLBACK(on_treeviews_lists_button_press_event),
659 gtk_dialog_get_widget_for_response(GTK_DIALOG(p_dialog), GTK_RESPONSE_OK));
4496e880
JL
660 gtk_widget_set_size_request(p_dialog, 600, 500);
661 gtk_widget_show_all(GTK_WIDGET(p_scrolledwindow_kbd_change));
662 gint response = gtk_dialog_run(GTK_DIALOG(p_dialog));
663 if(response == GTK_RESPONSE_OK)
664 {
665 GtkTreeIter tree_iter_sel;
666 GtkTreeSelection *p_treeselection_kbd_change = gtk_tree_view_get_selection(GTK_TREE_VIEW(p_treeview_kbd_change));
667 if(gtk_tree_selection_get_selected(p_treeselection_kbd_change,
668 (GtkTreeModel **)(&p_liststore_kbd_change),
669 &tree_iter_sel))
670 {
671 gchar *kbd_change_new;
672 gtk_tree_model_get(GTK_TREE_MODEL(p_liststore_kbd_change),
673 &tree_iter_sel, COLUMN_CHANGE_NAME, &kbd_change_new, -1);
674 g_free(p_xkb->kbd_change_option);
675 p_xkb->kbd_change_option = g_strdup(kbd_change_new);
676 gtk_button_set_label(GTK_BUTTON(p_xkb->p_button_change_layout), p_xkb->kbd_change_option);
677 g_free(kbd_change_new);
678 xkb_setxkbmap(p_xkb);
679 xkb_redraw(p_xkb);
680 }
681 }
682 gtk_widget_destroy(p_dialog);
683}
684
685static void on_button_up_layout_clicked(GtkButton *p_button, gpointer *p_data)
686{
687 XkbPlugin *p_xkb = (XkbPlugin *)p_data;
688 GtkTreeIter tree_iter_sel;
689 GtkTreeIter tree_iter_prev;
690 if(gtk_tree_selection_get_selected(p_xkb->p_treeselection_layout,
691 (GtkTreeModel **)(&p_xkb->p_liststore_layout),
692 &tree_iter_sel))
693 {
694 GtkTreePath *p_tree_path = gtk_tree_model_get_path(GTK_TREE_MODEL(p_xkb->p_liststore_layout),
695 &tree_iter_sel);
696 if( (gtk_tree_path_prev(p_tree_path)) &&
697 (gtk_tree_model_get_iter(GTK_TREE_MODEL(p_xkb->p_liststore_layout),
698 &tree_iter_prev,
699 p_tree_path)))
700 {
701 gtk_list_store_swap(p_xkb->p_liststore_layout, &tree_iter_sel, &tree_iter_prev);
702 xkb_update_layouts_n_variants(p_xkb);
703 }
704 gtk_tree_path_free(p_tree_path);
705 }
706}
707
708static void on_button_down_layout_clicked(GtkButton *p_button, gpointer *p_data)
709{
710 XkbPlugin *p_xkb = (XkbPlugin *)p_data;
711 GtkTreeIter tree_iter_sel;
712 GtkTreeIter tree_iter_next;
713 if(gtk_tree_selection_get_selected(p_xkb->p_treeselection_layout,
714 (GtkTreeModel **)(&p_xkb->p_liststore_layout),
715 &tree_iter_sel))
716 {
717 tree_iter_next = tree_iter_sel;
718 if(gtk_tree_model_iter_next(GTK_TREE_MODEL(p_xkb->p_liststore_layout),
719 &tree_iter_next))
720 {
721 gtk_list_store_swap(p_xkb->p_liststore_layout, &tree_iter_sel, &tree_iter_next);
722 xkb_update_layouts_n_variants(p_xkb);
723 }
724 }
725}
726
727static void on_button_rm_layout_clicked(GtkButton *p_button, gpointer *p_data)
728{
729 XkbPlugin *p_xkb = (XkbPlugin *)p_data;
730 GtkTreeIter tree_iter_sel;
731 if(gtk_tree_selection_get_selected(p_xkb->p_treeselection_layout,
732 (GtkTreeModel **)(&p_xkb->p_liststore_layout),
733 &tree_iter_sel))
734 {
735 gtk_list_store_remove(p_xkb->p_liststore_layout, &tree_iter_sel);
736 xkb_update_layouts_n_variants(p_xkb);
737 gtk_widget_set_sensitive(p_xkb->p_button_rm_layout, p_xkb->num_layouts > 1);
738 }
739}
740
741static void on_button_add_layout_clicked(GtkButton *p_button, gpointer *p_data)
742{
743 XkbPlugin *p_xkb = (XkbPlugin *)p_data;
744
745 // dialog
746 GtkWidget *p_dialog = gtk_dialog_new_with_buttons(_("Add Keyboard Layout"),
747 GTK_WINDOW(p_xkb->p_dialog_config),
748 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
749 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
750 GTK_STOCK_OK, GTK_RESPONSE_OK,
751 NULL);
752
753 // scrolledwindow
754 GtkWidget * p_scrolledwindow_add_layout = gtk_scrolled_window_new(NULL, NULL);
755 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(p_scrolledwindow_add_layout),
756 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
757 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(p_dialog)->vbox), p_scrolledwindow_add_layout, TRUE, TRUE, 2);
758
759 // treestore
760 GtkTreeStore *p_treestore_add_layout = gtk_tree_store_new(NUM_ADD_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING);
761 GtkWidget *p_treeview_add_layout = gtk_tree_view_new_with_model(GTK_TREE_MODEL(p_treestore_add_layout));
762 g_object_unref(G_OBJECT(p_treestore_add_layout));
763 gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(p_treeview_add_layout), TRUE);
764 gtk_container_add(GTK_CONTAINER(p_scrolledwindow_add_layout), p_treeview_add_layout);
765 GtkCellRenderer *p_renderer;
895da737 766 GtkTreeViewColumn *p_column, *p_column_desc;
4496e880
JL
767 // icon
768 p_renderer = gtk_cell_renderer_pixbuf_new();
895da737
JL
769 p_column = gtk_tree_view_column_new_with_attributes(_("Flag"), p_renderer, "pixbuf", COLUMN_ADD_ICON, NULL);
770 gtk_tree_view_column_set_sort_column_id(p_column, COLUMN_ADD_DESC);
4496e880
JL
771 gtk_tree_view_append_column(GTK_TREE_VIEW(p_treeview_add_layout), p_column);
772 // layout
773 p_renderer = gtk_cell_renderer_text_new();
895da737
JL
774 p_column = gtk_tree_view_column_new_with_attributes(_("Layout"), p_renderer, "text", COLUMN_ADD_LAYOUT, NULL);
775 gtk_tree_view_column_set_sort_column_id(p_column, COLUMN_ADD_LAYOUT);
4496e880
JL
776 gtk_tree_view_append_column(GTK_TREE_VIEW(p_treeview_add_layout), p_column);
777 // desc
778 p_renderer = gtk_cell_renderer_text_new();
895da737
JL
779 p_column_desc = gtk_tree_view_column_new_with_attributes(_("Description"), p_renderer, "text", COLUMN_ADD_DESC, NULL);
780 gtk_tree_view_column_set_sort_column_id(p_column_desc, COLUMN_ADD_DESC);
781 gtk_tree_view_append_column(GTK_TREE_VIEW(p_treeview_add_layout), p_column_desc);
782 // search column
783 gtk_tree_view_set_search_column(GTK_TREE_VIEW(p_treeview_add_layout), COLUMN_ADD_DESC);
4496e880
JL
784
785 // populate model
786 GKeyFile *p_keyfile = g_key_file_new();
787 gchar *xkbcfg_filepath = g_strdup_printf("%s/layouts.cfg", XKBCONFDIR);
788 if(g_key_file_load_from_file(p_keyfile, xkbcfg_filepath, 0, NULL))
789 {
790 gchar **keys_layouts = g_key_file_get_keys(p_keyfile, "LAYOUTS", NULL, NULL);
791 guint layout_idx = 0;
792 GtkTreeIter tree_top, tree_child;
793 gchar *p_layout_desc;
794 while(keys_layouts[layout_idx] != NULL)
795 {
796 p_layout_desc = g_key_file_get_string(p_keyfile, "LAYOUTS", keys_layouts[layout_idx], NULL);
797 if(strchr(keys_layouts[layout_idx], '(') == NULL)
798 {
799 gchar *flag_filepath = NULL;
800 gchar *flags_dir = (p_xkb->cust_dir_exists && (p_xkb->display_type == DISP_TYPE_IMAGE_CUST)) ? g_strdup(FLAGSCUSTDIR):g_strdup(FLAGSDIR);
801 if(strchr(keys_layouts[layout_idx], '/') != NULL)
802 {
803 gchar *layout_mod = g_strdup(keys_layouts[layout_idx]);
804 layout_mod = g_strdelimit(layout_mod, "/", '-');
805 flag_filepath = g_strdup_printf(flag_filepath_generator, flags_dir, layout_mod);
806 g_free(layout_mod);
807 }
808 else
809 {
810 flag_filepath = g_strdup_printf(flag_filepath_generator, flags_dir, keys_layouts[layout_idx]);
811 }
812 GdkPixbuf *p_pixbuf = gdk_pixbuf_new_from_file_at_size(flag_filepath, -1, 16, NULL);
813 gtk_tree_store_append(p_treestore_add_layout, &tree_top, NULL);
814 if(p_pixbuf != NULL)
815 {
816 gtk_tree_store_set(p_treestore_add_layout, &tree_top,
817 COLUMN_ADD_ICON, p_pixbuf,
818 COLUMN_ADD_LAYOUT, keys_layouts[layout_idx],
819 COLUMN_ADD_DESC, p_layout_desc,
820 -1);
821 g_object_unref(G_OBJECT(p_pixbuf));
822 }
823 else
824 {
825 gtk_tree_store_set(p_treestore_add_layout, &tree_top,
826 COLUMN_ADD_LAYOUT, keys_layouts[layout_idx],
827 COLUMN_ADD_DESC, p_layout_desc,
828 -1);
829 }
830 g_free(flag_filepath);
831 g_free(flags_dir);
832 }
833 else
834 {
835
836 gtk_tree_store_append(p_treestore_add_layout, &tree_child, &tree_top);
837 gtk_tree_store_set(p_treestore_add_layout, &tree_child,
838 COLUMN_ADD_LAYOUT, keys_layouts[layout_idx],
839 COLUMN_ADD_DESC, p_layout_desc,
840 -1);
841 }
842 g_free(p_layout_desc);
843 layout_idx++;
844 }
845 g_strfreev(keys_layouts);
846 g_key_file_free(p_keyfile);
847 }
848 g_free(xkbcfg_filepath);
849
850 // callback for double click
4496e880 851 g_signal_connect(p_treeview_add_layout, "button-press-event",
895da737
JL
852 G_CALLBACK(on_treeviews_lists_button_press_event),
853 gtk_dialog_get_widget_for_response(GTK_DIALOG(p_dialog), GTK_RESPONSE_OK));
854 // sort for description
855 gtk_tree_view_column_clicked(p_column_desc);
856
4496e880
JL
857 gtk_widget_set_size_request(p_dialog, 600, 500);
858 gtk_widget_show_all(GTK_WIDGET(p_scrolledwindow_add_layout));
859 gint response = gtk_dialog_run(GTK_DIALOG(p_dialog));
860 if(response == GTK_RESPONSE_OK)
861 {
862 GtkTreeIter tree_iter_sel;
863 GtkTreeSelection *p_treeselection_add_layout = gtk_tree_view_get_selection(GTK_TREE_VIEW(p_treeview_add_layout));
864 if(gtk_tree_selection_get_selected(p_treeselection_add_layout,
865 (GtkTreeModel **)(&p_treestore_add_layout),
866 &tree_iter_sel))
867 {
868 gchar *layout_add;
869 GString *p_gstring_new_layout = g_string_new("");
870 GString *p_gstring_new_variant = g_string_new("");
871 gtk_tree_model_get(GTK_TREE_MODEL(p_treestore_add_layout),
872 &tree_iter_sel, COLUMN_ADD_LAYOUT, &layout_add, -1);
873
874 if(strchr(layout_add, '(') == NULL)
875 {
876 g_string_append(p_gstring_new_layout, layout_add);
877 }
878 else
879 {
880 gboolean parsing_variant = FALSE;
881 guchar i;
882 for(i=0; layout_add[i]; i++)
883 {
884 if(!parsing_variant)
885 {
886 if(layout_add[i] == '(')
887 parsing_variant = TRUE;
888 else
889 g_string_append_c(p_gstring_new_layout, layout_add[i]);
890 }
891 else
892 {
893 if(layout_add[i] == ')')
894 break;
895 else
896 g_string_append_c(p_gstring_new_variant, layout_add[i]);
897 }
898 }
899 }
900 xkb_add_layout(p_xkb, p_gstring_new_layout->str, p_gstring_new_variant->str);
901 xkb_update_layouts_n_variants(p_xkb);
902
903 gtk_widget_set_sensitive(p_xkb->p_button_rm_layout, p_xkb->num_layouts > 1);
904
905 g_free(layout_add);
906 g_string_free(p_gstring_new_layout, TRUE/*free also gstring->str*/);
907 g_string_free(p_gstring_new_variant, TRUE/*free also gstring->str*/);
908 }
909 }
910 gtk_widget_destroy(p_dialog);
911}
912
047cf801 913void xkb_setxkbmap(XkbPlugin *p_xkb)
4496e880
JL
914{
915 GString *p_gstring_syscmd = g_string_new("");
916 g_string_printf(p_gstring_syscmd,
917 "setxkbmap -model %s -layout %s -variant %s -option grp:%s",
918 p_xkb->kbd_model, p_xkb->kbd_layouts, p_xkb->kbd_variants, p_xkb->kbd_change_option);
895da737
JL
919 if(system("setxkbmap -option")) // reset options
920 {
921 ERR("xkb: system(setxkbmap -option)\n");
922 }
923 if(system(p_gstring_syscmd->str)) // set new map
924 {
925 ERR("xkb: system(%s)\n", p_gstring_syscmd->str);
926 }
4496e880
JL
927 //g_printf("\n%s\n", p_gstring_syscmd->str);
928 g_string_free(p_gstring_syscmd, TRUE/*free also gstring->str*/);
929}
930
931static gboolean layouts_tree_model_foreach(GtkTreeModel *p_model,
932 GtkTreePath *p_path,
933 GtkTreeIter *p_iter,
934 gpointer p_data)
935{
936 XkbPlugin *p_xkb = (XkbPlugin *)p_data;
937 gchar *layout_val;
938 gchar *variant_val;
939
940 gtk_tree_model_get(p_model, p_iter, COLUMN_LAYOUT, &layout_val, -1);
941 gtk_tree_model_get(p_model, p_iter, COLUMN_VARIANT, &variant_val, -1);
942
943 if(strlen(p_xkb->p_gstring_layouts_partial->str))
944 {
945 g_string_append_c(p_xkb->p_gstring_layouts_partial, ',');
946 g_string_append_c(p_xkb->p_gstring_variants_partial, ',');
947 }
948 g_string_append(p_xkb->p_gstring_layouts_partial, layout_val);
949 g_string_append(p_xkb->p_gstring_variants_partial, variant_val);
950
895da737
JL
951 //g_printf("\npartial layouts = '%s'\n", p_xkb->p_gstring_layouts_partial->str);
952 //g_printf("partial variants = '%s'\n", p_xkb->p_gstring_variants_partial->str);
4496e880
JL
953
954 g_free(layout_val);
955 g_free(variant_val);
956 p_xkb->num_layouts++;
957 return FALSE;
958}
959
960static void xkb_update_layouts_n_variants(XkbPlugin *p_xkb)
961{
962 p_xkb->p_gstring_layouts_partial = g_string_new("");
963 p_xkb->p_gstring_variants_partial = g_string_new("");
964 p_xkb->num_layouts = 0;
965 gtk_tree_model_foreach(GTK_TREE_MODEL(p_xkb->p_liststore_layout),
966 layouts_tree_model_foreach,
967 p_xkb);
968 if(!strlen(p_xkb->p_gstring_variants_partial->str))
969 {
970 g_string_append_c(p_xkb->p_gstring_variants_partial, ',');
971 }
972 g_free(p_xkb->kbd_layouts);
973 g_free(p_xkb->kbd_variants);
974 p_xkb->kbd_layouts = g_strdup(p_xkb->p_gstring_layouts_partial->str);
975 p_xkb->kbd_variants = g_strdup(p_xkb->p_gstring_variants_partial->str);
976 g_string_free(p_xkb->p_gstring_layouts_partial, TRUE/*free also gstring->str*/);
977 g_string_free(p_xkb->p_gstring_variants_partial, TRUE/*free also gstring->str*/);
978 xkb_setxkbmap(p_xkb);
979 xkb_mechanism_destructor(p_xkb);
980 xkb_mechanism_constructor(p_xkb);
981 xkb_redraw(p_xkb);
982}
983
984static void xkb_add_layout(XkbPlugin *p_xkb, gchar *layout, gchar*variant)
985{
986 GtkTreeIter tree_iter;
987 gtk_list_store_append(p_xkb->p_liststore_layout, &tree_iter);
988 gchar *flag_filepath = NULL;
989 gchar *flags_dir = (p_xkb->cust_dir_exists && (p_xkb->display_type == DISP_TYPE_IMAGE_CUST)) ? g_strdup(FLAGSCUSTDIR):g_strdup(FLAGSDIR);
990 if(strchr(layout, '/') != NULL)
991 {
992 gchar *layout_mod = g_strdup(layout);
993 layout_mod = g_strdelimit(layout_mod, "/", '-');
994 flag_filepath = g_strdup_printf(flag_filepath_generator, flags_dir, layout_mod);
995 g_free(layout_mod);
996 }
997 else
998 {
999 flag_filepath = g_strdup_printf(flag_filepath_generator, flags_dir, layout);
1000 }
1001 GdkPixbuf *p_pixbuf = gdk_pixbuf_new_from_file_at_size(flag_filepath, -1, 20, NULL);
1002 if(p_pixbuf != NULL)
1003 {
1004 gtk_list_store_set(p_xkb->p_liststore_layout, &tree_iter,
1005 COLUMN_ICON, p_pixbuf,
1006 COLUMN_LAYOUT, layout,
1007 COLUMN_VARIANT, variant,
1008 -1);
1009 g_object_unref(G_OBJECT(p_pixbuf));
1010 }
1011 else
1012 {
1013 gtk_list_store_set(p_xkb->p_liststore_layout, &tree_iter,
1014 COLUMN_LAYOUT, layout,
1015 COLUMN_VARIANT, variant,
1016 -1);
1017 }
1018 g_free(flag_filepath);
1019 g_free(flags_dir);
1020}
1021
1022static void xkb_settings_fill_layout_tree_model_with_config(XkbPlugin *p_xkb)
1023{
1024 p_xkb->num_layouts = 0;
1025 if(strlen(p_xkb->kbd_layouts) && strlen(p_xkb->kbd_variants))
1026 {
1027 char **layouts = g_strsplit_set(p_xkb->kbd_layouts, ",", 0);
1028 char **variants = g_strsplit_set(p_xkb->kbd_variants, ",", 0);
1029
1030 while(layouts[p_xkb->num_layouts] != NULL)
1031 {
1032 xkb_add_layout(p_xkb, layouts[p_xkb->num_layouts], variants[p_xkb->num_layouts]);
1033 p_xkb->num_layouts++;
1034 }
1035 gtk_widget_set_sensitive(p_xkb->p_button_rm_layout, p_xkb->num_layouts > 1);
1036
1037 g_strfreev(layouts);
1038 g_strfreev(variants);
1039 }
22242ed4
HJYP
1040}
1041
d862b520 1042/* Callback when the configuration dialog is to be shown. */
1043static void xkb_configure(Plugin * p, GtkWindow * parent)
22242ed4 1044{
4496e880
JL
1045 XkbPlugin * p_xkb = (XkbPlugin *)p->priv;
1046 gchar markup_str[MAX_MARKUP_LEN];
1047
1048 user_active = FALSE;
d862b520 1049
4496e880 1050 // configuration dialog
d862b520 1051 GtkWidget * dlg = gtk_dialog_new_with_buttons(
4496e880 1052 _("Keyboard Layout Handler"),
d862b520 1053 NULL,
1054 GTK_DIALOG_NO_SEPARATOR,
4496e880 1055 GTK_STOCK_CLOSE,
d862b520 1056 GTK_RESPONSE_OK,
1057 NULL);
4496e880 1058 p_xkb->p_dialog_config = dlg;
d862b520 1059 panel_apply_icon(GTK_WINDOW(dlg));
1060
4496e880
JL
1061 // main vbox of the config dialog
1062 GtkWidget * p_vbox_main = gtk_vbox_new(FALSE, 0);
1063 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dlg)->vbox), p_vbox_main);
1064
1065
1066 // 'KEYBOARD MODEL' frame
1067 GtkWidget * p_frame_kbd_model = gtk_frame_new(NULL);
1068 GtkWidget * p_label_kbd_model = gtk_label_new(NULL);
1069 snprintf(markup_str, MAX_MARKUP_LEN, "<b>%s</b>", _("Keyboard Model"));
1070 gtk_label_set_markup(GTK_LABEL(p_label_kbd_model), markup_str);
1071 gtk_misc_set_padding(GTK_MISC(p_label_kbd_model), 1, 0);
1072 gtk_frame_set_label_widget(GTK_FRAME(p_frame_kbd_model), p_label_kbd_model);
1073 gtk_frame_set_shadow_type(GTK_FRAME(p_frame_kbd_model), GTK_SHADOW_NONE);
1074 gtk_box_pack_start(GTK_BOX(p_vbox_main), p_frame_kbd_model, TRUE, TRUE, 2);
1075 gtk_container_set_border_width(GTK_CONTAINER(p_frame_kbd_model), 3);
1076
1077 // frame alignment
1078 GtkWidget * p_alignment_kbd_model = gtk_alignment_new(0.5, 0.5, 1, 1);
1079 gtk_container_add(GTK_CONTAINER(p_frame_kbd_model), p_alignment_kbd_model);
1080 gtk_alignment_set_padding(GTK_ALIGNMENT(p_alignment_kbd_model), 4, 4, 10, 10);
1081 p_xkb->p_button_kbd_model = gtk_button_new_with_label(p_xkb->kbd_model);
1082 g_signal_connect(p_xkb->p_button_kbd_model, "clicked", G_CALLBACK(on_button_kbd_model_clicked), p_xkb);
1083 gtk_container_add(GTK_CONTAINER(p_alignment_kbd_model), p_xkb->p_button_kbd_model);
1084
1085
1086 // 'KEYBOARD LAYOUTS' frame
1087 GtkWidget * p_frame_kbd_layouts = gtk_frame_new(NULL);
1088 GtkWidget * p_label_kbd_layouts = gtk_label_new(NULL);
1089 snprintf(markup_str, MAX_MARKUP_LEN, "<b>%s</b>", _("Keyboard Layouts"));
1090 gtk_label_set_markup(GTK_LABEL(p_label_kbd_layouts), markup_str);
1091 gtk_misc_set_padding(GTK_MISC(p_label_kbd_layouts), 1, 0);
1092 gtk_frame_set_label_widget(GTK_FRAME(p_frame_kbd_layouts), p_label_kbd_layouts);
1093 gtk_frame_set_shadow_type(GTK_FRAME(p_frame_kbd_layouts), GTK_SHADOW_NONE);
1094 gtk_box_pack_start(GTK_BOX(p_vbox_main), p_frame_kbd_layouts, TRUE, TRUE, 2);
1095 gtk_container_set_border_width(GTK_CONTAINER(p_frame_kbd_layouts), 3);
1096 gtk_widget_set_size_request(GTK_WIDGET(p_frame_kbd_layouts), -1, 180);
1097
1098 // frame alignment
1099 GtkWidget * p_alignment_kbd_layouts = gtk_alignment_new(0.5, 0.5, 1, 1);
1100 gtk_container_add(GTK_CONTAINER(p_frame_kbd_layouts), p_alignment_kbd_layouts);
1101 gtk_alignment_set_padding(GTK_ALIGNMENT(p_alignment_kbd_layouts), 4, 4, 10, 10);
1102 GtkWidget * p_hbox_kbd_layouts = gtk_hbox_new(FALSE, 0);
1103 gtk_container_add(GTK_CONTAINER(p_alignment_kbd_layouts), p_hbox_kbd_layouts);
1104
1105 // scrolledwindow and buttons
1106 GtkWidget * p_scrolledwindow_kbd_layouts = gtk_scrolled_window_new(NULL, NULL);
1107 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(p_scrolledwindow_kbd_layouts),
1108 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1109 gtk_box_pack_start(GTK_BOX(p_hbox_kbd_layouts), p_scrolledwindow_kbd_layouts, TRUE, TRUE, 2);
1110 GtkWidget * p_vbox_kbd_layouts = gtk_vbox_new(FALSE, 2);
1111 gtk_box_pack_start(GTK_BOX(p_hbox_kbd_layouts), p_vbox_kbd_layouts, FALSE, TRUE, 4);
1112 GtkWidget * p_button_add_layout = gtk_button_new_from_stock(GTK_STOCK_ADD);
1113 GtkWidget * p_button_up_layout = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
1114 GtkWidget * p_button_down_layout = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
1115 p_xkb->p_button_rm_layout = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
1116 g_signal_connect(p_button_add_layout, "clicked", G_CALLBACK(on_button_add_layout_clicked), p_xkb);
1117 g_signal_connect(p_xkb->p_button_rm_layout, "clicked", G_CALLBACK(on_button_rm_layout_clicked), p_xkb);
1118 g_signal_connect(p_button_up_layout, "clicked", G_CALLBACK(on_button_up_layout_clicked), p_xkb);
1119 g_signal_connect(p_button_down_layout, "clicked", G_CALLBACK(on_button_down_layout_clicked), p_xkb);
1120 gtk_box_pack_start(GTK_BOX(p_vbox_kbd_layouts), p_button_add_layout, FALSE, TRUE, 0);
1121 gtk_box_pack_start(GTK_BOX(p_vbox_kbd_layouts), p_xkb->p_button_rm_layout, FALSE, TRUE, 0);
1122 gtk_box_pack_start(GTK_BOX(p_vbox_kbd_layouts), p_button_up_layout, FALSE, TRUE, 0);
1123 gtk_box_pack_start(GTK_BOX(p_vbox_kbd_layouts), p_button_down_layout, FALSE, TRUE, 0);
1124
1125 // liststore
1126 p_xkb->p_liststore_layout = gtk_list_store_new(NUM_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING);
1127 p_xkb->p_treeview_layout = gtk_tree_view_new_with_model(GTK_TREE_MODEL(p_xkb->p_liststore_layout));
1128 g_object_unref(G_OBJECT(p_xkb->p_liststore_layout));
1129 p_xkb->p_treeselection_layout = gtk_tree_view_get_selection(GTK_TREE_VIEW(p_xkb->p_treeview_layout));
1130 gtk_container_add(GTK_CONTAINER(p_scrolledwindow_kbd_layouts), p_xkb->p_treeview_layout);
1131 GtkCellRenderer *p_renderer;
1132 GtkTreeViewColumn *p_column;
1133 // icon
1134 p_renderer = gtk_cell_renderer_pixbuf_new();
1135 p_column = gtk_tree_view_column_new_with_attributes(_("Flag"), p_renderer, "pixbuf", COLUMN_ICON, NULL);
1136 gtk_tree_view_append_column(GTK_TREE_VIEW(p_xkb->p_treeview_layout), p_column);
1137 // layout
1138 p_renderer = gtk_cell_renderer_text_new();
1139 p_column = gtk_tree_view_column_new_with_attributes(_("Layout"), p_renderer, "text", COLUMN_LAYOUT, NULL);
1140 gtk_tree_view_append_column(GTK_TREE_VIEW(p_xkb->p_treeview_layout), p_column);
1141 // variant
1142 p_renderer = gtk_cell_renderer_text_new();
1143 p_column = gtk_tree_view_column_new_with_attributes(_("Variant"), p_renderer, "text", COLUMN_VARIANT, NULL);
1144 gtk_tree_view_append_column(GTK_TREE_VIEW(p_xkb->p_treeview_layout), p_column);
1145 xkb_settings_fill_layout_tree_model_with_config(p_xkb);
1146
1147
1148 // 'CHANGE LAYOUT OPTION' frame
1149 GtkWidget * p_frame_change_layout = gtk_frame_new(NULL);
1150 GtkWidget * p_label_change_layout = gtk_label_new(NULL);
1151 snprintf(markup_str, MAX_MARKUP_LEN, "<b>%s</b>", _("Change Layout Option"));
1152 gtk_label_set_markup(GTK_LABEL(p_label_change_layout), markup_str);
1153 gtk_misc_set_padding(GTK_MISC(p_label_change_layout), 1, 0);
1154 gtk_frame_set_label_widget(GTK_FRAME(p_frame_change_layout), p_label_change_layout);
1155 gtk_frame_set_shadow_type(GTK_FRAME(p_frame_change_layout), GTK_SHADOW_NONE);
1156 gtk_box_pack_start(GTK_BOX(p_vbox_main), p_frame_change_layout, TRUE, TRUE, 2);
1157 gtk_container_set_border_width(GTK_CONTAINER(p_frame_change_layout), 3);
1158
1159 // frame alignment
1160 GtkWidget * p_alignment_change_layout = gtk_alignment_new(0.5, 0.5, 1, 1);
1161 gtk_container_add(GTK_CONTAINER(p_frame_change_layout), p_alignment_change_layout);
1162 gtk_alignment_set_padding(GTK_ALIGNMENT(p_alignment_change_layout), 4, 4, 10, 10);
1163 p_xkb->p_button_change_layout = gtk_button_new_with_label(p_xkb->kbd_change_option);
1164 g_signal_connect(p_xkb->p_button_change_layout, "clicked", G_CALLBACK(on_button_kbd_change_layout_clicked), p_xkb);
1165 gtk_container_add(GTK_CONTAINER(p_alignment_change_layout), p_xkb->p_button_change_layout);
1166
1167
895da737
JL
1168 // 'PER WINDOW SETTINGS' frame
1169 GtkWidget * p_frame_perapp_layout = gtk_frame_new(NULL);
1170 GtkWidget * p_label_perapp_layout = gtk_label_new(NULL);
1171 snprintf(markup_str, MAX_MARKUP_LEN, "<b>%s</b>", _("Per Window Settings"));
1172 gtk_label_set_markup(GTK_LABEL(p_label_perapp_layout), markup_str);
1173 gtk_misc_set_padding(GTK_MISC(p_label_perapp_layout), 1, 0);
1174 gtk_frame_set_label_widget(GTK_FRAME(p_frame_perapp_layout), p_label_perapp_layout);
1175 gtk_frame_set_shadow_type(GTK_FRAME(p_frame_perapp_layout), GTK_SHADOW_NONE);
1176 gtk_box_pack_start(GTK_BOX(p_vbox_main), p_frame_perapp_layout, TRUE, TRUE, 2);
1177 gtk_container_set_border_width(GTK_CONTAINER(p_frame_perapp_layout), 3);
1178
1179 // frame alignment
1180 GtkWidget * p_alignment_perapp_layout = gtk_alignment_new(0.5, 0.5, 1, 1);
1181 gtk_container_add(GTK_CONTAINER(p_frame_perapp_layout), p_alignment_perapp_layout);
1182 gtk_alignment_set_padding(GTK_ALIGNMENT(p_alignment_perapp_layout), 4, 4, 10, 10);
1183 GtkWidget *p_checkbutton_per_app = gtk_check_button_new_with_mnemonic(_("_Remember layout for each window"));
1184 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_checkbutton_per_app), p_xkb->enable_perwin);
1185 g_signal_connect(p_checkbutton_per_app, "toggled", G_CALLBACK(on_xkb_checkbutton_per_app_toggled), p_xkb);
1186 gtk_container_add(GTK_CONTAINER(p_alignment_perapp_layout), p_checkbutton_per_app);
1187
1188
4496e880
JL
1189 // 'SHOW LAYOUT AS' frame
1190 GtkWidget * p_frame_display_type = gtk_frame_new(NULL);
1191 GtkWidget * p_label_show_layout_as = gtk_label_new(NULL);
1192 snprintf(markup_str, MAX_MARKUP_LEN, "<b>%s</b>", _("Show Layout as"));
1193 gtk_label_set_markup(GTK_LABEL(p_label_show_layout_as), markup_str);
1194 gtk_misc_set_padding(GTK_MISC(p_label_show_layout_as), 1, 0);
1195 gtk_frame_set_label_widget(GTK_FRAME(p_frame_display_type), p_label_show_layout_as);
1196 gtk_frame_set_shadow_type(GTK_FRAME(p_frame_display_type), GTK_SHADOW_NONE);
1197 gtk_box_pack_start(GTK_BOX(p_vbox_main), p_frame_display_type, TRUE, TRUE, 2);
1198 gtk_container_set_border_width(GTK_CONTAINER(p_frame_display_type), 3);
1199
1200 // frame alignment
1201 GtkWidget * p_alignment_display_type = gtk_alignment_new(0.5, 0.5, 1, 1);
1202 gtk_container_add(GTK_CONTAINER(p_frame_display_type), p_alignment_display_type);
1203 gtk_alignment_set_padding(GTK_ALIGNMENT(p_alignment_display_type), 4, 4, 10, 10);
1204 GtkWidget *p_table = gtk_table_new(3, 2, FALSE);
1205 gtk_table_set_col_spacings(GTK_TABLE(p_table), 10);
1206 gtk_container_add(GTK_CONTAINER(p_alignment_display_type), p_table);
1207
1208 // radiobuttons
1209 GtkWidget * p_image_disp_type_image = gtk_image_new();
1210 GtkWidget * p_image_disp_type_image_cust = NULL;
1211 gchar *symbol_name_lowercase = (char *)xkb_get_current_symbol_name_lowercase(p_xkb);
1212 gchar *flag_filepath = NULL;
1213 gchar *flag_filepath_cust = NULL;
1214 if(strchr(symbol_name_lowercase, '/') != NULL)
1215 {
1216 gchar *layout_mod = g_strdup(symbol_name_lowercase);
1217 layout_mod = g_strdelimit(layout_mod, "/", '-');
1218 flag_filepath = g_strdup_printf(flag_filepath_generator, FLAGSDIR, layout_mod);
1219 if(p_xkb->cust_dir_exists)
1220 flag_filepath_cust = g_strdup_printf(flag_filepath_generator, FLAGSCUSTDIR, layout_mod);
1221 g_free(layout_mod);
1222 }
1223 else
1224 {
1225 flag_filepath = g_strdup_printf(flag_filepath_generator, FLAGSDIR, symbol_name_lowercase);
1226 if(p_xkb->cust_dir_exists)
1227 flag_filepath_cust = g_strdup_printf(flag_filepath_generator, FLAGSCUSTDIR, symbol_name_lowercase);
1228 }
1229 GdkPixbuf *p_pixbuf = gdk_pixbuf_new_from_file_at_size(flag_filepath, -1, 16, NULL);
1230 if(p_pixbuf != NULL)
1231 {
1232 gtk_image_set_from_pixbuf(GTK_IMAGE(p_image_disp_type_image), p_pixbuf);
1233 g_object_unref(G_OBJECT(p_pixbuf));
1234 }
1235 if(flag_filepath_cust != NULL)
1236 {
1237 p_image_disp_type_image_cust = gtk_image_new();
1238 GdkPixbuf *p_pixbuf_cust = gdk_pixbuf_new_from_file_at_size(flag_filepath_cust, -1, 16, NULL);
1239 if(p_pixbuf_cust != NULL)
1240 {
1241 gtk_image_set_from_pixbuf(GTK_IMAGE(p_image_disp_type_image_cust), p_pixbuf_cust);
1242 g_object_unref(G_OBJECT(p_pixbuf_cust));
1243 }
1244 }
1245 g_free(flag_filepath);
1246 if(flag_filepath_cust != NULL)
1247 g_free(flag_filepath_cust);
1248 g_free(symbol_name_lowercase);
1249 GtkWidget * p_label_disp_type_text = gtk_label_new(NULL);
1250 snprintf(markup_str, MAX_MARKUP_LEN, "<span font='%d' font_weight='heavy'>%s</span>", 16, xkb_get_current_symbol_name(p_xkb));
1251 gtk_label_set_markup(GTK_LABEL(p_label_disp_type_text), markup_str);
895da737 1252 GtkWidget * p_radiobutton_disp_type_image = gtk_radio_button_new_with_label(NULL, (const gchar *)_("Image"));
4496e880
JL
1253 GtkWidget * p_radiobutton_disp_type_image_cust = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(p_radiobutton_disp_type_image), (const gchar *)_("Custom Image"));
1254 GtkWidget * p_radiobutton_disp_type_text = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(p_radiobutton_disp_type_image), (const gchar *)_("Text"));
1255
1256 gtk_table_attach(GTK_TABLE(p_table), p_image_disp_type_image, 0, 1, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
1257 gtk_table_attach(GTK_TABLE(p_table), p_radiobutton_disp_type_image, 1, 2, 0, 1, GTK_FILL, GTK_FILL, 0, 0);
1258 if(p_xkb->cust_dir_exists)
1259 {
1260 gtk_table_attach(GTK_TABLE(p_table), p_image_disp_type_image_cust, 0, 1, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
1261 gtk_table_attach(GTK_TABLE(p_table), p_radiobutton_disp_type_image_cust, 1, 2, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
1262 }
1263 gtk_table_attach(GTK_TABLE(p_table), p_label_disp_type_text, 0, 1, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
1264 gtk_table_attach(GTK_TABLE(p_table), p_radiobutton_disp_type_text, 1, 2, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
1265
1266 g_signal_connect(p_radiobutton_disp_type_image, "toggled", G_CALLBACK(on_radiobutton_disp_type_image_toggled), p_xkb);
1267 if(p_xkb->cust_dir_exists)
1268 g_signal_connect(p_radiobutton_disp_type_image_cust, "toggled", G_CALLBACK(on_radiobutton_disp_type_image_cust_toggled), p_xkb);
1269 g_signal_connect(p_radiobutton_disp_type_text, "toggled", G_CALLBACK(on_radiobutton_disp_type_text_toggled), p_xkb);
1270 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_disp_type_image), (p_xkb->display_type == DISP_TYPE_IMAGE) ||
1271 ( (p_xkb->display_type == DISP_TYPE_IMAGE_CUST) && (!p_xkb->cust_dir_exists) ) );
1272 if(p_xkb->cust_dir_exists)
1273 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_disp_type_image_cust), p_xkb->display_type == DISP_TYPE_IMAGE_CUST);
1274 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_disp_type_text), p_xkb->display_type == DISP_TYPE_TEXT);
1275
1276
1277 // 'PANEL ICON SIZE' frame
1278 GtkWidget * p_frame_flag_size = gtk_frame_new(NULL);
1279 GtkWidget * p_label_flag_size = gtk_label_new(NULL);
1280 snprintf(markup_str, MAX_MARKUP_LEN, "<b>%s</b>", _("Panel Icon Size"));
1281 gtk_label_set_markup(GTK_LABEL(p_label_flag_size), markup_str);
1282 gtk_misc_set_padding(GTK_MISC(p_label_flag_size), 1, 0);
1283 gtk_frame_set_label_widget(GTK_FRAME(p_frame_flag_size), p_label_flag_size);
1284 gtk_frame_set_shadow_type(GTK_FRAME(p_frame_flag_size), GTK_SHADOW_NONE);
1285 gtk_box_pack_start(GTK_BOX(p_vbox_main), p_frame_flag_size, TRUE, TRUE, 2);
1286 gtk_container_set_border_width(GTK_CONTAINER(p_frame_flag_size), 3);
1287
1288 // frame alignment
1289 GtkWidget * p_alignment_flag_size = gtk_alignment_new(0.5, 0.5, 1, 1);
1290 gtk_container_add(GTK_CONTAINER(p_frame_flag_size), p_alignment_flag_size);
1291 gtk_alignment_set_padding(GTK_ALIGNMENT(p_alignment_flag_size), 4, 4, 10, 10);
1292 GtkWidget *p_hbox_flag_size = gtk_hbox_new(FALSE, 2);
1293 gtk_container_add(GTK_CONTAINER(p_alignment_flag_size), p_hbox_flag_size);
1294
1295 GtkWidget *p_radiobutton_flag_size_1 = gtk_radio_button_new_with_label(NULL, "1");
1296 GtkWidget *p_radiobutton_flag_size_2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(p_radiobutton_flag_size_1), "2");
1297 GtkWidget *p_radiobutton_flag_size_3 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(p_radiobutton_flag_size_1), "3");
1298 GtkWidget *p_radiobutton_flag_size_4 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(p_radiobutton_flag_size_1), "4");
1299 GtkWidget *p_radiobutton_flag_size_5 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(p_radiobutton_flag_size_1), "5");
895da737 1300 GtkWidget *p_radiobutton_flag_size_6 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(p_radiobutton_flag_size_1), "6");
4496e880
JL
1301 g_signal_connect(p_radiobutton_flag_size_1, "toggled", G_CALLBACK(on_radiobutton_flag_size_1_toggled), p_xkb);
1302 g_signal_connect(p_radiobutton_flag_size_2, "toggled", G_CALLBACK(on_radiobutton_flag_size_2_toggled), p_xkb);
1303 g_signal_connect(p_radiobutton_flag_size_3, "toggled", G_CALLBACK(on_radiobutton_flag_size_3_toggled), p_xkb);
1304 g_signal_connect(p_radiobutton_flag_size_4, "toggled", G_CALLBACK(on_radiobutton_flag_size_4_toggled), p_xkb);
1305 g_signal_connect(p_radiobutton_flag_size_5, "toggled", G_CALLBACK(on_radiobutton_flag_size_5_toggled), p_xkb);
895da737 1306 g_signal_connect(p_radiobutton_flag_size_6, "toggled", G_CALLBACK(on_radiobutton_flag_size_6_toggled), p_xkb);
4496e880
JL
1307 gtk_box_pack_start(GTK_BOX(p_hbox_flag_size), p_radiobutton_flag_size_1, TRUE, TRUE, 2);
1308 gtk_box_pack_start(GTK_BOX(p_hbox_flag_size), p_radiobutton_flag_size_2, TRUE, TRUE, 2);
1309 gtk_box_pack_start(GTK_BOX(p_hbox_flag_size), p_radiobutton_flag_size_3, TRUE, TRUE, 2);
1310 gtk_box_pack_start(GTK_BOX(p_hbox_flag_size), p_radiobutton_flag_size_4, TRUE, TRUE, 2);
1311 gtk_box_pack_start(GTK_BOX(p_hbox_flag_size), p_radiobutton_flag_size_5, TRUE, TRUE, 2);
895da737 1312 gtk_box_pack_start(GTK_BOX(p_hbox_flag_size), p_radiobutton_flag_size_6, TRUE, TRUE, 2);
4496e880
JL
1313 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_flag_size_1), p_xkb->flag_size == 1);
1314 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_flag_size_2), p_xkb->flag_size == 2);
1315 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_flag_size_3), p_xkb->flag_size == 3);
1316 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_flag_size_4), p_xkb->flag_size == 4);
1317 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_flag_size_5), p_xkb->flag_size == 5);
895da737 1318 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(p_radiobutton_flag_size_6), p_xkb->flag_size == 6);
22242ed4 1319
d862b520 1320 /* Connect signals. */
4496e880 1321 g_signal_connect(p_xkb->p_dialog_config, "response", G_CALLBACK(on_dialog_config_response), p_xkb);
22242ed4 1322
d862b520 1323 /* Display the dialog. */
4496e880
JL
1324 gtk_widget_set_size_request(p_xkb->p_dialog_config, 350, -1);
1325 gtk_widget_show_all(p_xkb->p_dialog_config);
1326 gtk_window_present(GTK_WINDOW(p_xkb->p_dialog_config));
1327
1328 user_active = TRUE;
22242ed4
HJYP
1329}
1330
8f9e6256 1331/* Callback when the configuration is to be saved. */
1332static void xkb_save_configuration(Plugin * p, FILE * fp)
1333{
4496e880
JL
1334 XkbPlugin * p_xkb = (XkbPlugin *)p->priv;
1335 lxpanel_put_int(fp, "DisplayType", p_xkb->display_type);
895da737 1336 lxpanel_put_int(fp, "PerWinLayout", p_xkb->enable_perwin);
4496e880
JL
1337 lxpanel_put_str(fp, "Model", p_xkb->kbd_model);
1338 lxpanel_put_str(fp, "LayoutsList", p_xkb->kbd_layouts);
1339 lxpanel_put_str(fp, "VariantsList", p_xkb->kbd_variants);
1340 lxpanel_put_str(fp, "ToggleOpt", p_xkb->kbd_change_option);
1341 lxpanel_put_int(fp, "FlagSize", p_xkb->flag_size);
8f9e6256 1342}
1343
1344/* Callback when panel configuration changes. */
1345static void xkb_panel_configuration_changed(Plugin * p)
1346{
1347 /* Do a full redraw. */
4496e880
JL
1348 XkbPlugin * p_xkb = (XkbPlugin *)p->priv;
1349 xkb_redraw(p_xkb);
8f9e6256 1350}
22242ed4 1351
d862b520 1352/* Plugin descriptor. */
22242ed4 1353PluginClass xkb_plugin_class = {
2918994e 1354
1355 PLUGINCLASS_VERSIONING,
22242ed4
HJYP
1356
1357 type : "xkb",
4496e880
JL
1358 name : N_("Keyboard Layout Handler"),
1359 version: "2.0",
1360 description : N_("Handle keyboard layouts"),
22242ed4
HJYP
1361
1362 constructor : xkb_constructor,
1363 destructor : xkb_destructor,
8f9e6256 1364 config : xkb_configure,
1365 save : xkb_save_configuration,
1366 panel_configuration_changed : xkb_panel_configuration_changed
d862b520 1367
22242ed4 1368};