~ubuntu-branches/ubuntu/precise/hime/precise

« back to all changes in this revision

Viewing changes to src/modules/intcode.c

  • Committer: Package Import Robot
  • Author(s): Yao Wei (魏銘廷)
  • Date: 2012-01-14 00:24:08 UTC
  • Revision ID: package-import@ubuntu.com-20120114002408-e79gagbeg1rt8npv
Tags: upstream-0.9.9
Import upstream version 0.9.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 1994-2011 Edward Der-Hua Liu, Hsin-Chu, Taiwan
 
2
 *
 
3
 * This library is free software; you can redistribute it and/or
 
4
 * modify it under the terms of the GNU Lesser General Public
 
5
 * License as published by the Free Software Foundation; either
 
6
 * version 2.1 of the License, or (at your option) any later version.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public
 
14
 * License along with this library; if not, write to the Free Software
 
15
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 */
 
17
 
 
18
#define InAreaX (0)
 
19
 
 
20
#include "hime.h"
 
21
#include "intcode.h"
 
22
#include "pho.h"
 
23
#include "gst.h"
 
24
#include "im-client/hime-im-client-attr.h"
 
25
#include "hime-module.h"
 
26
#include "hime-module-cb.h"
 
27
 
 
28
extern GtkWidget *gwin_int;
 
29
HIME_module_main_functions gmf;
 
30
int current_intcode = INTCODE_UTF32;
 
31
 
 
32
static char inch[MAX_INTCODE];
 
33
int intcode_cin;
 
34
 
 
35
void create_win_intcode();
 
36
void module_show_win();
 
37
 
 
38
int module_init_win(HIME_module_main_functions *funcs)
 
39
{
 
40
  intcode_cin=0;
 
41
  gmf = *funcs;
 
42
  create_win_intcode();
 
43
  return TRUE;
 
44
}
 
45
 
 
46
static int h2i(int x)
 
47
{
 
48
  return (x<='9'?x-'0':x-'A'+10);
 
49
}
 
50
 
 
51
static void utf32to8(char *t, char *s)
 
52
{
 
53
  gsize rn,wn=0;
 
54
  GError *err = NULL;
 
55
  char *utf8 = g_convert(s, 4, "UTF-8", "UTF-32", &rn, &wn, &err);
 
56
 
 
57
  if (utf8) {
 
58
    memcpy(t, utf8, wn);
 
59
    g_free(utf8);
 
60
  }
 
61
 
 
62
  t[wn]=0;
 
63
}
 
64
 
 
65
 
 
66
void big5_utf8_n(char *s, int len, char out[])
 
67
{
 
68
  out[0]=0;
 
69
 
 
70
  GError *err = NULL;
 
71
  gsize rn, wn;
 
72
  char *utf8 = g_convert(s, len, "UTF-8", "Big5", &rn, &wn, &err);
 
73
 
 
74
  if (err) {
 
75
    dbg("big5_utf8  convert error\n");
 
76
    out[0]=0;
 
77
//    abort();
 
78
    return;
 
79
  }
 
80
 
 
81
  strcpy(out, utf8);
 
82
  g_free(utf8);
 
83
}
 
84
 
 
85
 
 
86
void big5_utf8(char *s, char out[])
 
87
{
 
88
  big5_utf8_n(s, strlen(s), out);
 
89
}
 
90
 
 
91
 
 
92
static unich_t *dstr[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
 
93
 
 
94
void disp_int(int index, char *intcode);
 
95
void clear_int_code_all();
 
96
 
 
97
gboolean module_feedkey(int key, int kvstate)
 
98
{
 
99
  int i;
 
100
#if 0
 
101
  if (key <= XK_KP_9 && key >= XK_KP_0)
 
102
    key -= XK_KP_0 - '0';
 
103
#endif
 
104
  key=toupper(key);
 
105
  if (key==XK_BackSpace||key==XK_Delete) {
 
106
    if (intcode_cin)
 
107
      intcode_cin--;
 
108
    else
 
109
      return 0;
 
110
 
 
111
    goto dispIn;
 
112
  }
 
113
  else
 
114
  if ((key<'0'||key>'F'||(key>'9' && key<'A')) && (key!=' ')){
 
115
    return 0;
 
116
  }
 
117
 
 
118
  if (current_intcode==INTCODE_BIG5) {
 
119
    if (intcode_cin==0 && key<'8')
 
120
      return 1;
 
121
    if (intcode_cin==1 && inch[0]=='F' && key=='F')
 
122
      return 1;
 
123
    if (intcode_cin==2 && (key<'4' || (key>'7' && key<'A')))
 
124
      return 1;
 
125
    if (intcode_cin==3 && (inch[2]=='7'||inch[2]=='F') && key=='F')
 
126
      return 1;
 
127
  }
 
128
 
 
129
  if (!intcode_cin && key==' ')
 
130
    return 0;
 
131
  if ((intcode_cin<MAX_INTCODE-1 || (current_intcode!=INTCODE_BIG5 && intcode_cin < MAX_INTCODE)) && key!=' ')
 
132
    inch[intcode_cin++]=key;
 
133
 
 
134
dispIn:
 
135
  clear_int_code_all();
 
136
 
 
137
#if 1
 
138
  if (intcode_cin)
 
139
    module_show_win();
 
140
#endif
 
141
 
 
142
  for(i=0;i<intcode_cin;i++) {
 
143
    disp_int(i, _(dstr[h2i(inch[i])]));
 
144
  }
 
145
 
 
146
  if (((current_intcode==INTCODE_BIG5 && intcode_cin==4) ||
 
147
       (current_intcode==INTCODE_UTF32 && intcode_cin==6)) || key==' ') {
 
148
    u_char utf8[CH_SZ+1];
 
149
 
 
150
    if (current_intcode==INTCODE_BIG5) {
 
151
      u_char ttt[4];
 
152
      ttt[2]=ttt[3]=0;
 
153
      ttt[0]=(h2i(inch[0])<<4)+h2i(inch[1]);
 
154
      ttt[1]=(h2i(inch[2])<<4)+h2i(inch[3]);
 
155
      big5_utf8((char *)ttt, (char *)utf8);
 
156
    } else {
 
157
      int i;
 
158
      u_int v = 0;
 
159
 
 
160
      for(i=0; i < intcode_cin; i++) {
 
161
        v <<= 4;
 
162
        v |= h2i(inch[i]);
 
163
      }
 
164
 
 
165
      utf32to8((char *)utf8, (char *)&v);
 
166
    }
 
167
 
 
168
    gmf.mf_send_utf8_ch((char *)utf8);
 
169
    intcode_cin=0;
 
170
 
 
171
    clear_int_code_all();
 
172
  }
 
173
 
 
174
  return 1;
 
175
}
 
176
 
 
177
extern GtkWidget *gwin_int;
 
178
 
 
179
int module_get_preedit(char *str, HIME_PREEDIT_ATTR attr[], int *cursor, int *comp_flag)
 
180
{
 
181
  *comp_flag = intcode_cin>0;
 
182
#if 1
 
183
  if (gwin_int && GTK_WIDGET_VISIBLE(gwin_int))
 
184
    *comp_flag|=2;
 
185
#endif
 
186
//  dbg("comp_len %x\n", *sub_comp_len);
 
187
  str[0]=0;
 
188
  *cursor=0;
 
189
  return 0;
 
190
}
 
191
 
 
192
int module_feedkey_release(KeySym xkey, int kbstate)
 
193
{
 
194
        return 0;
 
195
}
 
196
 
 
197
int module_flush_input()
 
198
{
 
199
  return FALSE;
 
200
}
 
201
 
 
202
int module_reset()
 
203
{
 
204
  return TRUE;
 
205
}