~ubuntu-branches/ubuntu/saucy/fcitx/saucy-proposed

« back to all changes in this revision

Viewing changes to src/module/punc/punc.c

  • Committer: Package Import Robot
  • Author(s): Aron Xu
  • Date: 2013-02-13 13:50:37 UTC
  • mfrom: (1.3.19) (10.1.30 sid)
  • Revision ID: package-import@ubuntu.com-20130213135037-n5rh0ke1rm0j5tie
Tags: 1:4.2.7-1ubuntu1
Add upstream indicator patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
 
51
51
struct _FcitxPuncState;
52
52
typedef struct _WidePunc {
53
 
    char            ASCII;
54
 
    char            strWidePunc[MAX_PUNC_NO][MAX_PUNC_LENGTH * UTF8_MAX_LENGTH + 1];
55
 
    unsigned        iCount: 2;
 
53
    char ASCII;
 
54
    char strWidePunc[MAX_PUNC_NO][MAX_PUNC_LENGTH * UTF8_MAX_LENGTH + 1];
 
55
    unsigned iCount: 2;
56
56
} WidePunc;
57
57
 
58
58
typedef struct _PuncWhich {
74
74
static void* PuncCreate(FcitxInstance* instance);
75
75
static boolean PuncPreFilter(void* arg, FcitxKeySym sym, unsigned int state, INPUT_RETURN_VALUE* retVal);
76
76
static boolean ProcessPunc(void* arg, FcitxKeySym sym, unsigned int state, INPUT_RETURN_VALUE* retVal);
77
 
static void* PuncGetPunc(void* a, FcitxModuleFunctionArg arg);
78
 
static void* PuncGetPunc2(void* a, FcitxModuleFunctionArg arg);
79
77
static void TogglePuncState(void *arg);
80
78
static boolean GetPuncState(void *arg);
81
79
static void ReloadPunc(void *arg);
89
87
static void* PuncWhichCopy(void* arg, void* data, void* src);
90
88
static void PuncWhichFree(void* arg, void* data);
91
89
 
 
90
DECLARE_ADDFUNCTIONS(Punc)
92
91
 
93
92
typedef struct _FcitxPuncState {
94
93
    char cLastIsAutoConvert;
110
109
void* PuncCreate(FcitxInstance* instance)
111
110
{
112
111
    FcitxPuncState* puncState = fcitx_utils_malloc0(sizeof(FcitxPuncState));
113
 
    FcitxAddon* puncaddon = FcitxAddonsGetAddonByName(FcitxInstanceGetAddons(instance), FCITX_PUNC_NAME);
114
112
    puncState->owner = instance;
115
113
    LoadPuncDict(puncState);
116
114
    FcitxKeyFilterHook hk;
141
139
 
142
140
    FcitxInstanceRegisterInputUnFocusHook(instance, hook);
143
141
 
144
 
    FcitxInstanceWatchContext(instance, CONTEXT_IM_LANGUAGE, PuncLanguageChanged, puncState);
 
142
    FcitxInstanceWatchContext(instance, CONTEXT_IM_LANGUAGE,
 
143
                              PuncLanguageChanged, puncState);
145
144
 
146
145
    FcitxProfile* profile = FcitxInstanceGetProfile(instance);
147
146
    FcitxUIRegisterStatus(instance, puncState, "punc",
148
 
                          profile->bUseWidePunc ? _("Full width punct") :  _("Latin punct"),
149
 
                          _("Toggle Full Width Punctuation"), TogglePuncState, GetPuncState);
150
 
 
151
 
    puncState->slot = FcitxInstanceAllocDataForIC(instance, PuncWhichAlloc, PuncWhichCopy, PuncWhichFree, puncState);
152
 
 
153
 
    AddFunction(puncaddon, PuncGetPunc);
154
 
    AddFunction(puncaddon, PuncGetPunc2);
 
147
                          profile->bUseWidePunc ? _("Full width punct") :
 
148
                          _("Latin punct"),
 
149
                          _("Toggle Full Width Punctuation"), TogglePuncState,
 
150
                          GetPuncState);
 
151
 
 
152
    puncState->slot = FcitxInstanceAllocDataForIC(instance, PuncWhichAlloc,
 
153
                                                  PuncWhichCopy, PuncWhichFree,
 
154
                                                  puncState);
 
155
 
 
156
    FcitxPuncAddFunctions(instance);
155
157
    return puncState;
156
158
}
157
159
 
158
 
void* PuncWhichAlloc(void* arg)
 
160
void* PuncWhichAlloc(void *arg)
159
161
{
160
 
    FcitxPunc* puncState = arg;
161
 
    PuncWhich* which = fcitx_utils_new(PuncWhich);
 
162
    FcitxPunc *puncState = arg;
 
163
    PuncWhich *which = fcitx_utils_new(PuncWhich);
162
164
    which->lastPunc = puncState->curPunc;
163
165
    which->bitset = fcitx_bitset_new(256);
164
166
    return which;
166
168
 
167
169
void* PuncWhichCopy(void* arg, void* data, void* src)
168
170
{
169
 
    PuncWhich* which = data;
170
 
    PuncWhich* whichsrc = src;
 
171
    FCITX_UNUSED(arg);
 
172
    PuncWhich *which = data;
 
173
    PuncWhich *whichsrc = src;
171
174
    which->lastPunc = whichsrc->lastPunc;
172
175
    memcpy(which->bitset, whichsrc->bitset, fcitx_bitset_size(256));
173
176
    return data;
175
178
 
176
179
void PuncWhichFree(void* arg, void* data)
177
180
{
178
 
    PuncWhich* which = data;
 
181
    FCITX_UNUSED(arg);
 
182
    PuncWhich *which = data;
179
183
    free(which->bitset);
180
184
    free(data);
181
185
}
197
201
    FcitxUISetStatusVisable (puncState->owner, "punc",  puncState->curPunc != NULL) ;
198
202
}
199
203
 
200
 
void* PuncGetPunc(void* a, FcitxModuleFunctionArg arg)
201
 
{
202
 
    FcitxPuncState* puncState = (FcitxPuncState*) a;
203
 
    int *key = arg.args[0];
204
 
    return GetPunc(puncState, *key);
205
 
}
206
 
 
207
 
 
208
 
void* PuncGetPunc2(void* a, FcitxModuleFunctionArg arg)
209
 
{
210
 
    FcitxPuncState* puncState = (FcitxPuncState*) a;
211
 
    int *key = arg.args[0];
212
 
    char** p1 = arg.args[1];
213
 
    char** p2 = arg.args[2];
214
 
    int             iIndex = 0;
215
 
    WidePunc       *curPunc = puncState->curPunc;
 
204
static void
 
205
PuncGetPunc2(FcitxPuncState *puncState, int key, char **p1, char **p2)
 
206
{
 
207
    int iIndex = 0;
 
208
    WidePunc *curPunc = puncState->curPunc;
216
209
 
217
210
    if (!curPunc)
218
 
        return NULL;
 
211
        return;
219
212
 
220
213
    while (curPunc[iIndex].ASCII) {
221
 
        if (curPunc[iIndex].ASCII == *key) {
 
214
        if (curPunc[iIndex].ASCII == key) {
222
215
            if (p1)
223
216
                *p1 = curPunc[iIndex].strWidePunc[0];
224
217
            if (curPunc[iIndex].iCount > 1 && p2)
227
220
        }
228
221
        iIndex++;
229
222
    }
230
 
 
231
 
    return NULL;
232
223
}
233
224
 
234
225
void ResetPunc(void* arg)
253
244
    fcitx_bitset_clear(puncWhich->bitset);
254
245
}
255
246
 
256
 
boolean PuncPreFilter(void* arg, FcitxKeySym sym, unsigned int state, INPUT_RETURN_VALUE* retVal)
 
247
boolean PuncPreFilter(void* arg, FcitxKeySym sym, unsigned int state,
 
248
                      INPUT_RETURN_VALUE* retVal)
257
249
{
258
 
    if (!FcitxHotkeyIsHotKeySimple(sym, state))
259
 
        return false;
260
 
 
261
 
    FcitxPuncState* puncState = (FcitxPuncState*) arg;
262
 
    if (!FcitxHotkeyIsHotKeyDigit(sym, state) && !IsHotKeyPunc(sym, state))
 
250
    FCITX_UNUSED(retVal);
 
251
    FcitxPuncState *puncState = (FcitxPuncState*)arg;
 
252
    if (FcitxHotkeyIsHotKeySimple(sym, state) &&
 
253
        !FcitxHotkeyIsHotKeyDigit(sym, state) && !IsHotKeyPunc(sym, state))
263
254
        puncState->bLastIsNumber = false;
264
 
 
265
255
    return false;
266
256
}
267
257
 
278
268
    if (*retVal != IRV_TO_PROCESS)
279
269
        return false;
280
270
 
 
271
    FcitxCandidateWordList *candList = FcitxInputStateGetCandidateList(input);
 
272
    if (FcitxCandidateWordGetListSize(candList) != 0) {
 
273
        if (FcitxCandidateWordGetHasGoneToNextPage(candList) &&
 
274
            FcitxHotkeyIsHotKey(sym, state,
 
275
                                FcitxConfigPrevPageKey(instance, config))) {
 
276
            return false;
 
277
        }
 
278
        /*
 
279
         * comparing with upper case, if paging is occupied,
 
280
         * punc will not let next page pass
 
281
         */
 
282
        if (FcitxHotkeyIsHotKey(sym, state,
 
283
                                FcitxConfigNextPageKey(instance, config))) {
 
284
            return false;
 
285
        }
 
286
    }
 
287
 
281
288
    FcitxKeySym origsym = sym;
282
289
    sym = FcitxHotkeyPadToMain(sym);
283
290
    if (profile->bUseWidePunc) {
284
 
 
285
291
        if (puncState->bLastIsNumber && config->bEngPuncAfterNumber
286
292
                && (FcitxHotkeyIsHotKey(origsym, state, FCITX_PERIOD)
287
293
                    || FcitxHotkeyIsHotKey(origsym, state, FCITX_SEMICOLON)
302
308
        FcitxInputStateGetOutputString(input)[0] = '\0';
303
309
        INPUT_RETURN_VALUE ret = IRV_TO_PROCESS;
304
310
        if (!FcitxInputStateGetIsInRemind(input))
305
 
            ret = FcitxCandidateWordChooseByIndex(FcitxInputStateGetCandidateList(input), 0);
 
311
            ret = FcitxCandidateWordChooseByTotalIndex(FcitxInputStateGetCandidateList(input), 0);
306
312
 
307
313
        /* if there is nothing to commit */
308
314
        if (ret == IRV_TO_PROCESS) {
342
348
            puncState->cLastIsAutoConvert = 0;
343
349
            *retVal = IRV_DO_NOTHING;
344
350
            return true;
345
 
        } else if (FcitxHotkeyIsHotKeySimple(sym, state)) {
346
 
            if (FcitxHotkeyIsHotKeyDigit(sym, state))
347
 
                puncState->bLastIsNumber = true;
348
 
            else {
349
 
                puncState->bLastIsNumber = false;
350
 
            }
 
351
        } else if (FcitxHotkeyIsHotKeyDigit(sym, state)) {
 
352
            puncState->bLastIsNumber = true;
 
353
        } else {
 
354
            puncState->bLastIsNumber = false;
351
355
        }
352
356
    }
353
357
    puncState->cLastIsAutoConvert = 0;
516
520
 * 根据字符得到相应的标点符号
517
521
 * 如果该字符不在标点符号集中,则返回NULL
518
522
 */
519
 
char           *GetPunc(FcitxPuncState* puncState, int iKey)
 
523
static char*
 
524
GetPunc(FcitxPuncState *puncState, int iKey)
520
525
{
521
 
    int             iIndex = 0;
522
 
    char           *pPunc;
523
 
    WidePunc       *curPunc = puncState->curPunc;
 
526
    int iIndex = 0;
 
527
    char *pPunc;
 
528
    WidePunc *curPunc = puncState->curPunc;
524
529
 
525
530
    if (!curPunc)
526
 
        return (char *) NULL;
 
531
        return NULL;
527
532
 
528
533
    while (curPunc[iIndex].ASCII) {
529
534
        if (curPunc[iIndex].ASCII == iKey) {
530
 
            pPunc = curPunc[iIndex].strWidePunc[GetPuncWhich(puncState, &curPunc[iIndex])];
 
535
            pPunc = curPunc[iIndex].strWidePunc[GetPuncWhich(puncState,
 
536
                                                             &curPunc[iIndex])];
531
537
            SetPuncWhich(puncState, &curPunc[iIndex]);
532
538
            return pPunc;
533
539
        }
534
540
        iIndex++;
535
541
    }
536
542
 
537
 
    return (char *) NULL;
 
543
    return NULL;
538
544
}
539
545
 
540
546
void TogglePuncState(void* arg)
545
551
    profile->bUseWidePunc = !profile->bUseWidePunc;
546
552
 
547
553
    FcitxUISetStatusString(puncState->owner, "punc",
548
 
                           profile->bUseWidePunc ? _("Full width punct") :  _("Latin punct"),
549
 
                          _("Toggle Full Width Punctuation"));
 
554
                           profile->bUseWidePunc ? _("Full width punct") :
 
555
                           _("Latin punct"),
 
556
                           _("Toggle Full Width Punctuation"));
550
557
    FcitxProfileSave(profile);
551
558
}
552
559
 
591
598
 
592
599
    return false;
593
600
}
594
 
 
595
 
 
596
 
// kate: indent-mode cstyle; space-indent on; indent-width 0;
 
601
#include "fcitx-punc-addfunctions.h"