File: scim_frontend_setup.cpp

package info (click to toggle)
scim 1.4.7-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 20,368 kB
  • ctags: 12,834
  • sloc: cpp: 51,239; sh: 22,370; ansic: 18,716; makefile: 1,258; xml: 641; yacc: 288
file content (581 lines) | stat: -rw-r--r-- 18,559 bytes parent folder | download | duplicates (3)
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/** @file scim_aaa_frontend_setup.cpp
 * implementation of Setup Module for FrontEnd Hotkeys configuration.
 */

/*
 * Smart Common Input Method
 * 
 * Copyright (c) 2005 James Su <suzhe@tsinghua.org.cn>
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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
 *
 * $Id: scim_frontend_setup.cpp,v 1.6 2005/06/29 08:19:17 suzhe Exp $
 *
 */

#define Uses_SCIM_CONFIG_PATH
#define Uses_SCIM_CONFIG_BASE
#define Uses_SCIM_EVENT

#include <iostream>

#include <gtk/gtk.h>

#include "scim_private.h"
#include "scim.h"
#include "scimkeyselection.h"

using namespace scim;

#define scim_module_init aaa_frontend_setup_LTX_scim_module_init
#define scim_module_exit aaa_frontend_setup_LTX_scim_module_exit

#define scim_setup_module_create_ui       aaa_frontend_setup_LTX_scim_setup_module_create_ui
#define scim_setup_module_get_category    aaa_frontend_setup_LTX_scim_setup_module_get_category
#define scim_setup_module_get_name        aaa_frontend_setup_LTX_scim_setup_module_get_name
#define scim_setup_module_get_description aaa_frontend_setup_LTX_scim_setup_module_get_description
#define scim_setup_module_load_config     aaa_frontend_setup_LTX_scim_setup_module_load_config
#define scim_setup_module_save_config     aaa_frontend_setup_LTX_scim_setup_module_save_config
#define scim_setup_module_query_changed   aaa_frontend_setup_LTX_scim_setup_module_query_changed

static GtkWidget * create_setup_window ();
static void        load_config (const ConfigPointer &config);
static void        save_config (const ConfigPointer &config);
static bool        query_changed ();

// Module Interface.
extern "C" {
    void scim_module_init (void)
    {
    }

    void scim_module_exit (void)
    {
    }

    GtkWidget * scim_setup_module_create_ui (void)
    {
        return create_setup_window ();
    }

    String scim_setup_module_get_category (void)
    {
        return String ("FrontEnd");
    }

    String scim_setup_module_get_name (void)
    {
        return String (_("Global Setup"));
    }

    String scim_setup_module_get_description (void)
    {
        return String (_("Setup the global options used by All FrontEnd modules, including X11 FrontEnd, GTK IMModule, QT IMModule etc."));
    }

    void scim_setup_module_load_config (const ConfigPointer &config)
    {
        load_config (config);
    }

    void scim_setup_module_save_config (const ConfigPointer &config)
    {
        save_config (config);
    }

    bool scim_setup_module_query_changed ()
    {
        return query_changed ();
    }
} // extern "C"

// Internal data structure
struct KeyboardConfigData
{
    const char *key;
    const char *label;
    const char *title;
    const char *tooltip;
    GtkWidget  *entry;
    GtkWidget  *button;
    String      data;
};

// Internal data declaration.

static bool           __config_on_the_spot       = true;

static bool           __config_shared_input_method = false;

static KeyboardLayout __config_keyboard_layout   = SCIM_KEYBOARD_Unknown;

static bool           __have_changed             = false;


static GtkWidget     * __widget_on_the_spot      = 0;

static GtkTooltips   * __widget_tooltips         = 0;

static GtkWidget     * __widget_keyboard_layout  = NULL;

static GtkWidget     * __widget_shared_input_method = NULL;

static KeyboardConfigData __config_keyboards [] =
{
    {
        // key
        SCIM_CONFIG_HOTKEYS_FRONTEND_TRIGGER,
        // label
        N_("_Trigger:"),
        // title
        N_("Select the trigger keys"),
        // tooltip
        N_("The key events to turn on/off SCIM input method. "
           "Click on the button on the right to edit it."),
        // entry
        NULL,
        // button
        NULL,
        // data
        "Control+space"
    },
    {
        // key
        SCIM_CONFIG_HOTKEYS_FRONTEND_ON,
        // label
        N_("Turn _On:"),
        // title
        N_("Select the Turn On keys"),
        // tooltip
        N_("The key events to turn on SCIM input method. "
           "Click on the button on the right to edit it."),
        // entry
        NULL,
        // button
        NULL,
        // data
        ""
    },
    {
        // key
        SCIM_CONFIG_HOTKEYS_FRONTEND_OFF,
        // label
        N_("Turn O_ff:"),
        // title
        N_("Select the Turn Off keys"),
        // tooltip
        N_("The key events to turn off SCIM input method. "
           "Click on the button on the right to edit it."),
        // entry
        NULL,
        // button
        NULL,
        // data
        ""
    },
    {
        // key
        SCIM_CONFIG_HOTKEYS_FRONTEND_NEXT_FACTORY,
        // label
        N_("_Next input method:"),
        // title
        N_("Select the next input method keys"),
        // tooltip
        N_("The key events to switch to the next input method. "
           "Click on the button on the right to edit it."),
        // entry
        NULL,
        // button
        NULL,
        // data
        "Control+Alt+Down,"
        "Control+Shift_R,"
        "Control+Shift_L"
    },
    {
        // key
        SCIM_CONFIG_HOTKEYS_FRONTEND_PREVIOUS_FACTORY,
        // label
        N_("_Previous input method:"),
        // title
        N_("Select the previous input method keys"),
        // tooltip
        N_("The key events to switch to the previous input method. "
           "Click on the button on the right to edit it."),
        // entry
        NULL,
        // button
        NULL,
        // data
        "Control+Alt+Up,"
        "Shift+Control_R,"
        "Shift+Control_L"
    },
    {
        // key
        SCIM_CONFIG_HOTKEYS_FRONTEND_SHOW_FACTORY_MENU,
        // label
        N_("Show input method _menu:"),
        // title
        N_("Select the show input method menu keys"),
        // tooltip
        N_("The key events to show the input method menu. "
           "Click on the button on the right to edit it."),
        // entry
        NULL,
        // button
        NULL,
        // data
        "Control+Alt+l,"
        "Control+Alt+m,"
        "Control+Alt+s,"
        "Control+Alt+Right,"
    },
    {
        // key
        NULL,
        // label
        NULL,
        // title
        NULL,
        // tooltip
        NULL,
        // entry
        NULL,
        // button
        NULL,
        // data
        ""
    },
};

// Declaration of internal functions.
static void
on_default_editable_changed          (GtkEditable     *editable,
                                      gpointer         user_data);

static void
on_default_key_selection_clicked     (GtkButton       *button,
                                      gpointer         user_data);

static void
on_keyboard_layout_changed           (GtkComboBox     *combobox,
                                      gpointer         user_data);

static void
on_default_toggle_button_toggled     (GtkToggleButton *togglebutton,
                                      gpointer         user_data);

static void
setup_widget_value ();

// Function implementations.
static GtkWidget *
create_setup_window ()
{
    static GtkWidget *window = 0;

    if (!window) {
        GtkWidget *table;
        GtkWidget *frame;
        GtkWidget *hbox;
        GtkWidget *vbox;
        GtkWidget *label;
        int i;

        __widget_tooltips = gtk_tooltips_new ();

        // Create the toplevel box.
        window = gtk_vbox_new (FALSE, 0);
        gtk_widget_show (window);

        frame = gtk_frame_new (_("Options"));
        gtk_widget_show (frame);
        gtk_container_set_border_width (GTK_CONTAINER (frame), 4);
        gtk_box_pack_start (GTK_BOX (window), frame, FALSE, FALSE, 0);

        vbox = gtk_vbox_new (FALSE, 4);
        gtk_widget_show (vbox);
        gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
        gtk_container_add (GTK_CONTAINER (frame), vbox);

        // Keyboard Layout.
        hbox = gtk_hbox_new (FALSE, 4);
        gtk_widget_show (hbox);
        gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
 
        label = gtk_label_new_with_mnemonic (_("_Keyboard Layout:"));
        gtk_widget_show (label);
        gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

        __widget_keyboard_layout = gtk_combo_box_new_text ();
        gtk_widget_show (__widget_keyboard_layout);

        gtk_label_set_mnemonic_widget (GTK_LABEL (label), __widget_keyboard_layout);

        for (size_t i = 0; i < SCIM_KEYBOARD_NUM_LAYOUTS; ++i) {
            gtk_combo_box_append_text (GTK_COMBO_BOX (__widget_keyboard_layout),
                scim_keyboard_layout_get_display_name (static_cast<KeyboardLayout> (i)).c_str ());
        }

        g_signal_connect (G_OBJECT (__widget_keyboard_layout), "changed",
                          G_CALLBACK (on_keyboard_layout_changed),
                          NULL);

        gtk_box_pack_start (GTK_BOX (hbox), __widget_keyboard_layout, TRUE, TRUE, 0);

        gtk_tooltips_set_tip (__widget_tooltips, __widget_keyboard_layout,
                              _("You should choose your currently used keyboard layout here "
                                "so that input methods, who care about keyboard layout, could work correctly."), NULL);

        // On The Spot.
        __widget_on_the_spot = gtk_check_button_new_with_mnemonic (_("_Embed Preedit String into client window"));
        gtk_widget_show (__widget_on_the_spot);
        gtk_box_pack_start (GTK_BOX (vbox), __widget_on_the_spot, FALSE, FALSE, 0);

        gtk_tooltips_set_tip (__widget_tooltips, __widget_on_the_spot,
                              _("If this option is checked, "
                                "the preedit string will be displayed directly in the client input window, "
                                "rather than in a independent float window."), NULL);

        g_signal_connect ((gpointer) __widget_on_the_spot, "toggled",
                          G_CALLBACK (on_default_toggle_button_toggled),
                          &__config_on_the_spot);

        // Shared input method.
        __widget_shared_input_method = gtk_check_button_new_with_mnemonic (_("_Share the same input method among all applications"));
        gtk_widget_show (__widget_shared_input_method);
        gtk_box_pack_start (GTK_BOX (vbox), __widget_shared_input_method, FALSE, FALSE, 0);

        gtk_tooltips_set_tip (__widget_tooltips, __widget_shared_input_method,
                              _("If this option is checked, "
                                "then only one input method could be used by all applications at the same time."
                                "Otherwise different input method could be used by each application."), NULL);

        g_signal_connect ((gpointer) __widget_shared_input_method, "toggled",
                          G_CALLBACK (on_default_toggle_button_toggled),
                          &__config_shared_input_method);

        frame = gtk_frame_new (_("Hotkeys"));
        gtk_widget_show (frame);
        gtk_container_set_border_width (GTK_CONTAINER (frame), 4);
        gtk_box_pack_start (GTK_BOX (window), frame, TRUE, TRUE, 0);

        table = gtk_table_new (3, 3, FALSE);
        gtk_widget_show (table);
        gtk_container_add (GTK_CONTAINER (frame), table);
        gtk_table_set_row_spacings (GTK_TABLE (table), 0);
        gtk_table_set_col_spacings (GTK_TABLE (table), 8);

        for (i = 0; __config_keyboards [i].key; ++ i) {
            label = gtk_label_new (NULL);
            gtk_label_set_text_with_mnemonic (GTK_LABEL (label), _(__config_keyboards[i].label));
            gtk_widget_show (label);
            gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
            gtk_misc_set_padding (GTK_MISC (label), 4, 0);
            gtk_table_attach (GTK_TABLE (table), label, 0, 1, i, i+1,
                              (GtkAttachOptions) (GTK_FILL),
                              (GtkAttachOptions) (GTK_FILL), 4, 2);

            __config_keyboards [i].entry = gtk_entry_new ();
            gtk_widget_show (__config_keyboards [i].entry);
            gtk_table_attach (GTK_TABLE (table), __config_keyboards [i].entry, 1, 2, i, i+1,
                              (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
                              (GtkAttachOptions) (GTK_FILL), 4, 2);
            gtk_entry_set_editable (GTK_ENTRY (__config_keyboards[i].entry), FALSE);

            __config_keyboards[i].button = gtk_button_new_with_label ("...");
            gtk_widget_show (__config_keyboards[i].button);
            gtk_table_attach (GTK_TABLE (table), __config_keyboards[i].button, 2, 3, i, i+1,
                              (GtkAttachOptions) (GTK_FILL),
                              (GtkAttachOptions) (GTK_FILL), 4, 2);
            gtk_label_set_mnemonic_widget (GTK_LABEL (label), __config_keyboards[i].button);
        }

        for (i = 0; __config_keyboards [i].key; ++ i) {
            g_signal_connect ((gpointer) __config_keyboards [i].button, "clicked",
                              G_CALLBACK (on_default_key_selection_clicked),
                              &(__config_keyboards [i]));
            g_signal_connect ((gpointer) __config_keyboards [i].entry, "changed",
                              G_CALLBACK (on_default_editable_changed),
                              &(__config_keyboards [i].data));
        }

        for (i = 0; __config_keyboards [i].key; ++ i) {
            gtk_tooltips_set_tip (__widget_tooltips, __config_keyboards [i].entry,
                                  _(__config_keyboards [i].tooltip), NULL);
        }

        setup_widget_value ();
    }

    return window;
}

static void
setup_widget_value ()
{
    for (int i = 0; __config_keyboards [i].key; ++ i) {
        if (__config_keyboards [i].entry) {
            gtk_entry_set_text (
                GTK_ENTRY (__config_keyboards [i].entry),
                __config_keyboards [i].data.c_str ());
        }
    }

    if (__widget_on_the_spot) {
        gtk_toggle_button_set_active (
            GTK_TOGGLE_BUTTON (__widget_on_the_spot),
            __config_on_the_spot);
    }

    if (__widget_shared_input_method) {
        gtk_toggle_button_set_active (
            GTK_TOGGLE_BUTTON (__widget_shared_input_method),
            __config_shared_input_method);
    }

    gtk_combo_box_set_active (GTK_COMBO_BOX (__widget_keyboard_layout), (gint) __config_keyboard_layout);
}

static void
load_config (const ConfigPointer &config)
{
    if (!config.null ()) {
        for (int i = 0; __config_keyboards [i].key; ++ i) {
            __config_keyboards [i].data =
                config->read (String (__config_keyboards [i].key),
                              __config_keyboards [i].data);
        }

        __config_on_the_spot = 
            config->read (String (SCIM_CONFIG_FRONTEND_ON_THE_SPOT),
                          __config_on_the_spot);

        __config_shared_input_method = 
            config->read (String (SCIM_CONFIG_FRONTEND_SHARED_INPUT_METHOD),
                          __config_shared_input_method);

        __config_keyboard_layout = scim_get_default_keyboard_layout ();

        setup_widget_value ();

        __have_changed = false;
    }
}

static void
save_config (const ConfigPointer &config)
{
    if (!config.null ()) {
        for (int i = 0; __config_keyboards [i].key; ++ i) {
            config->write (String (__config_keyboards [i].key),
                          __config_keyboards [i].data);
        }

        gint act = gtk_combo_box_get_active (GTK_COMBO_BOX (__widget_keyboard_layout));

        if (act >= 0 && act < SCIM_KEYBOARD_NUM_LAYOUTS)
            __config_keyboard_layout = static_cast<KeyboardLayout> (act);
        else
            __config_keyboard_layout = SCIM_KEYBOARD_Unknown;

        if (__config_keyboard_layout != scim_get_default_keyboard_layout ())
            scim_set_default_keyboard_layout (__config_keyboard_layout);

        config->write (String (SCIM_CONFIG_FRONTEND_ON_THE_SPOT),
                       __config_on_the_spot);

        config->write (String (SCIM_CONFIG_FRONTEND_SHARED_INPUT_METHOD),
                       __config_shared_input_method);

        __have_changed = false;
    }
}

static bool
query_changed ()
{
    return __have_changed;
}

static void
on_default_editable_changed (GtkEditable *editable,
                             gpointer     user_data)
{
    String *str = static_cast <String *> (user_data);

    if (str) {
        *str = String (gtk_entry_get_text (GTK_ENTRY (editable)));
        __have_changed = true;
    }
}

static void
on_default_key_selection_clicked (GtkButton *button,
                                  gpointer   user_data)
{
    KeyboardConfigData *data = static_cast <KeyboardConfigData *> (user_data);

    if (data) {
        GtkWidget *dialog = scim_key_selection_dialog_new (_(data->title));
        gint result;

        scim_key_selection_dialog_set_keys (
            SCIM_KEY_SELECTION_DIALOG (dialog),
            data->data.c_str ());

        result = gtk_dialog_run (GTK_DIALOG (dialog));

        if (result == GTK_RESPONSE_OK) {
            const gchar *keys = scim_key_selection_dialog_get_keys (
                            SCIM_KEY_SELECTION_DIALOG (dialog));

            if (!keys) keys = "";

            if (String (keys) != data->data)
                gtk_entry_set_text (GTK_ENTRY (data->entry), keys);
        }

        gtk_widget_destroy (dialog);
    }
}

static void
on_default_toggle_button_toggled (GtkToggleButton *togglebutton,
                                  gpointer         user_data)
{
    bool *toggle = static_cast<bool*> (user_data);

    if (toggle) {
        *toggle = gtk_toggle_button_get_active (togglebutton);
        __have_changed = true;
    }
}

static void
on_keyboard_layout_changed (GtkComboBox *combobox,
                            gpointer     user_data)
{
    __have_changed = true;
}


/*
vi:ts=4:nowrap:expandtab
*/