forked from zhujian198/unispim
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy patheditor.c
2797 lines (2336 loc) · 155 KB
/
editor.c
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 用户输入:wo3'men2da'jia
* 解析:
* 音节 wo3 men2 da jia
* 有无' y n y n
* 有无调 y y n n
* 汉字 - - - -
* 字符串 wo3 men2 da' jia
* 字符串长度 3 4 3 3
* Real光标:13;显示光标:13。
*
* 当用户选择了“我们”这个词的时候
* 音节 wo3 men2 da jia
* 有无' y n y n
* 有无调 y y n n
* 汉字 我 们 - -
* 字符串 我 们 da' jia
* 字符串长度 2 2 3 3
* 当前光标:13;显示光标:11。
*
*
*/
#include <kernel.h>
#include <config.h>
#include <spw.h>
#include <zi.h>
//#include <url.h>
#include <ci.h>
#include <editor.h>
#include <symbol.h>
#include <context.h>
#include <utility.h>
#include <windows.h>
#include <win32/pim_ime.h>
#include <assert.h>
#include <tchar.h>
#include <english.h>
#include <win32/main_window.h>
/** 获得候选的音节
*/
int GetCandidateSyllable(CANDIDATE *candidate, SYLLABLE *syllables, int length)
{
assert(length >= 1);
switch(candidate->type)
{
case CAND_TYPE_ZI:
if (length < 1)
return 0;
//判断是否为类似xian的词
if (!candidate->hz.is_word)
{
syllables[0] = candidate->hz.item->syllable;
return 1;
}
if (length < (int)candidate->hz.word_item->syllable_length)
return 0;
memcpy(syllables, candidate->hz.word_item->syllable, sizeof(SYLLABLE) * candidate->hz.word_item->syllable_length);
return candidate->hz.word_item->syllable_length;
case CAND_TYPE_ICW:
if (length < candidate->icw.length)
return 0;
memcpy(syllables, candidate->icw.syllable, sizeof(SYLLABLE) * candidate->icw.length);
return candidate->icw.length;
case CAND_TYPE_CI:
if (length < (int)candidate->word.item->syllable_length)
return 0;
memcpy(syllables, candidate->word.syllable, sizeof(SYLLABLE) * candidate->word.item->syllable_length);
return candidate->word.item->syllable_length;
case CAND_TYPE_SPW: //短语输入的串不作为拼音处理
//case CAND_TYPE_URL:
return 0;
case CAND_TYPE_ZFW:
if (length < 1)
return 0;
syllables[0] = candidate->zfw.syllable;
return 1;
}
return 0;
}
/** 获得候选的音节长度
*/
int GetCandidateSyllableCount(CANDIDATE *candidate)
{
SYLLABLE syllable[0x40];
return GetCandidateSyllable(candidate, syllable, sizeof(syllable)/sizeof(syllable[0]));
}
/** 获得已经选择项的音节数组
*/
int GetSelectedItemSyllable(SELECT_ITEM *item, SYLLABLE *syllables, int length)
{
if (length < 1)
return 0;
if (item->left_or_right == ZFW_NONE)
return GetCandidateSyllable(&item->candidate, syllables, length);
if (item->candidate.type != CAND_TYPE_CI)
return 0;
if (item->left_or_right == ZFW_LEFT)
{
if (item->candidate.type == CAND_TYPE_CI)
{
syllables[0] = item->candidate.word.syllable[0];
return 1;
}
if (item->candidate.type == CAND_TYPE_ICW)
{
syllables[0] = item->candidate.icw.syllable[0];
return 1;
}
return 0;
}
if (item->left_or_right == ZFW_RIGHT)
{
if (item->candidate.type == CAND_TYPE_CI)
{
syllables[0] = item->candidate.word.syllable[item->candidate.word.item->syllable_length - 1];
return 1;
}
if (item->candidate.type == CAND_TYPE_ICW)
{
syllables[0] = item->candidate.icw.syllable[item->candidate.icw.length - 1];
return 1;
}
return 0;
}
return 0;
}
/** 获得候选的汉字
* 返回:
* 候选字符串的长度(以字节为单位)
*/
int GetCandidateString(PIMCONTEXT *context, CANDIDATE *candidate, TCHAR *buffer, int length)
{
assert(length >= 1);
wmemset(buffer, 0, length);
switch(candidate->type)
{
case CAND_TYPE_ZI:
if (candidate->hz.is_word)
{
if (length < (int)candidate->hz.word_item->ci_length)
return 0;
memcpy(buffer, GetItemHZPtr(candidate->hz.word_item), candidate->hz.word_item->ci_length * sizeof(HZ));
return candidate->hz.word_item->ci_length;
}
if (length < 1)
return 0;
if (candidate->hz.item->hz > 0xFFFF)
{
UCS32ToUCS16(candidate->hz.item->hz, buffer);
return 2;
}
else
{
*(HZ*)buffer = candidate->hz.item->hz;
return 1;
}
case CAND_TYPE_ICW:
if (length < (int)(candidate->icw.length))
return 0;
memcpy(buffer, candidate->icw.hz, sizeof(HZ) * candidate->icw.length);
buffer[candidate->icw.length] = 0;
if (pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL)
WordJ2F(buffer);
return candidate->icw.length;
case CAND_TYPE_CI:
if (length < (int)(candidate->word.item->ci_length))
return 0;
memcpy(buffer, candidate->word.hz, sizeof(HZ) * candidate->word.item->ci_length);
buffer[candidate->word.item->ci_length] = 0;
if (pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL)
WordJ2F(buffer);
return candidate->word.item->ci_length;
case CAND_TYPE_SPW:
if (length < candidate->spw.length)
return 0;
if (candidate->spw.type == SPW_STIRNG_ENGLISH)
{
AnsiToUtf16(candidate->spw.string, buffer, candidate->spw.length);
if (context->input_length && length)
{
if (context->input_string[0] != buffer[0] && tolower(context->input_string[0]) == tolower(buffer[0]))
buffer[0] = context->input_string[0];
}
}
else if (candidate->spw.type == SPW_STRING_BH)
UCS32ToUCS16(candidate->spw.hz, buffer);
else
memcpy(buffer, candidate->spw.string, candidate->spw.length * sizeof(TCHAR));
buffer[candidate->spw.length] = 0;
return candidate->spw.length;
//case CAND_TYPE_URL:
// if(length<candidate->url.length)
// return 0;
// memcpy(buffer, candidate->url.string, sizeof(TCHAR) * candidate->url.length);
// buffer[candidate->url.length] = 0;
// return candidate->url.length;
case CAND_TYPE_ZFW:
if (length < 1)
return 0;
*(TCHAR*)buffer = candidate->zfw.hz;
return 1;
}
return 0;
}
/** 获得候选的汉字的显示字符串(过长的要缩短)
* 返回:
* 候选字符串的长度(以字节为单位)
*/
int GetCandidateDisplayString(PIMCONTEXT *context, CANDIDATE *candidate, TCHAR *buffer, int length, int first_candidate)
{
int str_length;
TCHAR str_buffer[MAX_SPW_LENGTH + 2];
//执行程序串
if (candidate->type == CAND_TYPE_SPW)
{
if (!pim_config->use_u_hint && candidate->spw.type == SPW_STRING_EXEC)
{
GetUDisplayString(candidate, buffer, length);
return (int)_tcslen(buffer);
}
if (candidate->spw.type == SPW_STRING_BH)
{
GetBHDisplayString(candidate, buffer, length);
return (int)_tcslen(buffer);
}
}
str_length = GetCandidateString(context, candidate, str_buffer, MAX_SPW_LENGTH + 2);
if (first_candidate && !context->selected_item_count &&
context->compose_cursor_index && context->compose_cursor_index < context->compose_length)
{
TCHAR str[MAX_SPW_LENGTH + 2];
int syllable_index = GetSyllableIndexByComposeCursor(context, context->compose_cursor_index);
_tcsncpy_s(str, _SizeOf(str), context->default_hz, syllable_index);
_tcscat_s(str, _SizeOf(str), str_buffer);
_tcscpy_s(str_buffer, _SizeOf(str_buffer), str);
}
return PackStringToBuffer(str_buffer, str_length, buffer, length);
}
/** 获得当前已经选择的项的字符串
*/
int GetSelectedItemString(PIMCONTEXT *context, SELECT_ITEM *item, TCHAR *buffer, int length)
{
TCHAR ft_ci[0x100];
if (length <= 1)
return 0;
//特殊处理以词定字
if (item->candidate.type == CAND_TYPE_CI)
{
if (item->left_or_right == ZFW_LEFT)
{
if (!(pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL))
{
*(HZ*)buffer = item->candidate.word.hz[0];
*(buffer + 1) = 0;
}
else
{
memcpy(ft_ci, item->candidate.word.hz, sizeof(HZ) * item->candidate.word.item->ci_length);
ft_ci[item->candidate.word.item->ci_length] = 0;
WordJ2F(ft_ci);
*(HZ*)buffer = *(HZ*)ft_ci;
*(buffer + 1) = 0;
}
return 1;
}
if (item->left_or_right == ZFW_RIGHT)
{
if (!(pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL))
{
*(HZ*)buffer = item->candidate.word.hz[item->candidate.word.item->ci_length - 1];
*(buffer + 1) = 0;
}
else
{
memcpy(ft_ci, item->candidate.word.hz, sizeof(HZ) * item->candidate.word.item->ci_length);
ft_ci[item->candidate.word.item->ci_length] = 0;
WordJ2F(ft_ci);
*(HZ*)buffer = *((HZ*)ft_ci + item->candidate.word.item->ci_length - 1);
*(buffer + 1) = 0;
}
return 1;
}
}
//特殊处理以词定字
if (item->candidate.type == CAND_TYPE_ICW)
{
if (item->left_or_right == ZFW_LEFT)
{
if (!(pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL))
{
*(HZ*)buffer = item->candidate.icw.hz[0];
*(buffer + 1) = 0;
}
else
{
memcpy(ft_ci, item->candidate.icw.hz, sizeof(HZ) * item->candidate.icw.length);
ft_ci[item->candidate.icw.length] = 0;
WordJ2F(ft_ci);
*(HZ*)buffer = *((HZ*)ft_ci);
*(buffer + 1) = 0;
}
return 1;
}
if (item->left_or_right == ZFW_RIGHT)
{
if (!(pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL))
{
*(HZ*)buffer = item->candidate.icw.hz[item->candidate.icw.length - 1];
*(buffer + 1) = 0;
}
else
{
memcpy(ft_ci, item->candidate.icw.hz, sizeof(HZ) * item->candidate.icw.length);
ft_ci[item->candidate.icw.length] = 0;
WordJ2F(ft_ci);
*(HZ*)buffer = *((HZ*)ft_ci + item->candidate.icw.length - 1);
*(buffer + 1) = 0;
}
return 1;
}
}
return GetCandidateString(context, &item->candidate, buffer, length);
}
/** 用户选择后续处理
*/
void PostResult(PIMCONTEXT *context)
{
if (!context || !context->result_syllable_count)
return;
//是不是一次选择
if (context->selected_item_count == 1) //只有一个
{
//ICW
if (context->selected_items[0].candidate.type == CAND_TYPE_ICW)
{
if (pim_config->save_icw && !(pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL))
{ //加入到词库以及Cache中
AddCi(context->result_syllables, context->result_syllable_count, (HZ*)context->result_string, context->result_syllable_count);
//准备删除词汇
PrepareDeleteNewCi((HZ*)context->result_string, context->result_syllable_count, context->result_syllables, context->result_syllable_count);
//加入到cache
InsertCiToCache((HZ*)context->result_string, context->selected_items[0].candidate.icw.length, context->result_syllable_count, 0);
}
return;
}
//单字
if (context->selected_items[0].candidate.type == CAND_TYPE_ZI)
{
//输入类似xian的词汇
if (context->selected_items[0].candidate.hz.is_word)
InsertCiToCache((HZ*)context->result_string,
context->selected_items[0].candidate.hz.word_item->ci_length,
context->selected_items[0].candidate.hz.word_item->syllable_length,
0);
else
ProcessZiSelected(context->selected_items[0].candidate.hz.item);
}
//词
if (context->selected_items[0].candidate.type == CAND_TYPE_CI)
{
//非以词定字
if (context->selected_items[0].left_or_right == ZFW_NONE)
{
ProcessCiSelected(context->result_syllables,
context->result_syllable_count,
(HZ*)context->result_string,
context->selected_items[0].candidate.hz.word_item->ci_length);
CheckQuoteInput(*(HZ*)context->result_string);
return;
}
//以词定字
ProcessZiSelectedByWord(*(HZ*)context->result_string, context->result_syllables[0]);
}
return;
}
context->result_length = (int)_tcslen(context->result_string);
//检查是否为用户词库中的新词
//CheckNewUserWord(context->result_syllables, context->result_syllable_count, (HZ*)context->result_string, context->result_length);
//多次选择,组成新词
AddCi(context->result_syllables, context->result_syllable_count, (HZ*)context->result_string, context->result_length);
//加入到Cache中
InsertCiToCache((HZ*)context->result_string, context->result_length, context->result_syllable_count, 0);
//准备删除词汇
PrepareDeleteNewCi((HZ*)context->result_string, context->result_length, context->result_syllables, context->result_syllable_count);
}
/** 生成结果串以及结果音节数组
*/
void MakeResult(PIMCONTEXT *context)
{
int item_count = context->selected_item_count;
TCHAR *p_result = context->result_string;
SYLLABLE *p_syllable = context->result_syllables;
int syllable_count;
TCHAR cand_string[MAX_RESULT_LENGTH + 1];
SYLLABLE syllables[MAX_SYLLABLE_PER_INPUT];
int i, length;
const TCHAR *symbol_string;
//非法输入串,以用户输入为结果串
if (context->syllable_count == 0 && context->selected_item_count == 0)
{
_tcsncpy(p_result, context->input_string, MAX_RESULT_LENGTH);
context->result_syllable_count = 0;
context->modify_flag |= MODIFY_RESULT;
return;
}
//遍历用户已经选择的词/字/
for (i = 0; i < context->selected_item_count; i++)
{
length = GetSelectedItemString(context, &context->selected_items[i], cand_string, _SizeOf(cand_string));
if (length + (p_result - context->result_string) >= MAX_RESULT_LENGTH)
continue; //跳过越界的候选
syllable_count = GetSelectedItemSyllable(&context->selected_items[i], syllables, MAX_SYLLABLE_PER_INPUT);
if (syllable_count + (p_syllable - context->result_syllables) > MAX_SYLLABLE_PER_INPUT)
continue;
if (pim_config->space_after_english_word &&
context->selected_items[i].candidate.type == CAND_TYPE_SPW &&
context->selected_items[i].candidate.spw.type == SPW_STIRNG_ENGLISH)
{
_tcscat_s(cand_string, MAX_RESULT_LENGTH, TEXT(" "));
length++;
}
memcpy(p_result, cand_string, length * sizeof(TCHAR));
p_result += length;
memcpy(p_syllable, syllables, syllable_count * sizeof(SYLLABLE));
p_syllable += syllable_count;
}
*p_result = 0;
if (context->last_symbol && (symbol_string = GetSymbol(context, context->last_symbol)) != 0)
{
_tcscat_s(p_result, MAX_RESULT_LENGTH - _tcslen(context->result_string), symbol_string);
CheckQuoteInput(*(HZ*)symbol_string);
}
context->result_syllable_count = (int)(p_syllable - context->result_syllables);
context->modify_flag |= MODIFY_RESULT;
context->state = STATE_RESULT;
//处理最后的结果,汉字增加频度、词Cache等
PostResult(context);
}
/** 处理候选字符串,当前候选数目等事务
*/
void ProcessCandidateStuff(PIMCONTEXT *context)
{
int i;
SetCandidatesViewMode(context);
if (VIEW_MODE_EXPAND == context->candidates_view_mode)
context->candidate_page_count = pim_config->candidates_per_line * GetExpandCandidateLine();
else
context->candidate_page_count = pim_config->candidates_per_line;
//如果超过边界
if (context->candidate_index + context->candidate_page_count > context->candidate_count)
context->candidate_page_count = context->candidate_count - context->candidate_index;
//获得候选字符串
for (i = 0; i < context->candidate_page_count; i++)
{
GetCandidateDisplayString(context,
&context->candidate_array[context->candidate_index + i],
context->candidate_string[i],
MAX_CANDIDATE_STRING_LENGTH,
i == 0 && context->candidate_index);
context->candidate_trans_string[i][0] = 0;
if (context->english_state != ENGLISH_STATE_NONE && pim_config->use_english_input && pim_config->use_english_translate &&
context->candidate_array[context->candidate_index + i].spw.type == SPW_STIRNG_ENGLISH)
{
TCHAR* trans_str = GetEnglishTranslation(context->candidate_string[i]);
if (trans_str)
_tcscpy_s(context->candidate_trans_string[i], MAX_TRANSLATE_STRING_LENGTH, trans_str);
}
}
context->modify_flag |= MODIFY_COMPOSE;
}
/** 处理候选下翻页
*/
void NextCandidatePage(PIMCONTEXT *context)
{
int old_index = context->candidate_index;
if (VIEW_MODE_EXPAND == context->candidates_view_mode)
context->candidate_index += pim_config->candidates_per_line * GetExpandCandidateLine();
else
context->candidate_index += pim_config->candidates_per_line;
if (context->candidate_index >= context->candidate_count)
context->candidate_index = old_index;
if (old_index != context->candidate_index)
{
context->selected_digital = 0;
context->candidate_selected_index = 0;
ProcessCandidateStuff(context);
}
}
/** 处理候选上翻页
*/
void PrevCandidatePage(PIMCONTEXT *context)
{
int old_index = context->candidate_index;
if (VIEW_MODE_EXPAND == context->candidates_view_mode)
context->candidate_index -= pim_config->candidates_per_line * GetExpandCandidateLine();
else
context->candidate_index -= pim_config->candidates_per_line;
if (context->candidate_index < 0)
context->candidate_index = 0;
if (old_index != context->candidate_index)
{
context->selected_digital = 0;
context->candidate_selected_index = 0;
ProcessCandidateStuff(context);
}
}
/** 向下一行候选
*/
void NextCandidateLine(PIMCONTEXT *context)
{
int index = context->candidate_selected_index + pim_config->candidates_per_line;
//在本页中
if (index < context->candidate_page_count)
{
context->selected_digital = 0;
context->candidate_selected_index = index;
context->modify_flag |= MODIFY_COMPOSE;
return;
}
//如果为最后一页,则不处理
if (context->candidate_index + context->candidate_page_count == context->candidate_count)
return;
//翻到下一页,并且设置为0
NextCandidatePage(context);
index = index % pim_config->candidates_per_line;
if (index < context->candidate_page_count)
context->candidate_selected_index = index;
else
context->candidate_selected_index = 0;
}
/** 向上滚动一行
*/
void PrevCandidateLine(PIMCONTEXT *context)
{
int index = context->candidate_selected_index;
//在本页中
if (index >= pim_config->candidates_per_line)
{
context->selected_digital = 0;
context->candidate_selected_index = index - pim_config->candidates_per_line;
context->modify_flag |= MODIFY_COMPOSE;
return;
}
//如果为第一页,则不处理
if (!context->candidate_index)
return;
//翻到上一页,并且设置为最后
PrevCandidatePage(context);
context->candidate_selected_index = index;
}
/** 处理候选下移一个
*/
void NextCandidateItem(PIMCONTEXT *context)
{
//最后一个需要进行翻页
if (context->candidate_selected_index >= context->candidate_page_count - 1)
{
//不是最后一页的话进行翻页
if (context->candidate_index + context->candidate_page_count >= context->candidate_count)
return;
NextCandidatePage(context);
context->candidate_selected_index = 0;
return;
}
context->selected_digital = 0;
context->candidate_selected_index++;
context->modify_flag |= MODIFY_COMPOSE;
}
/** 处理候选最后一个
*/
void EndCandidateItem(PIMCONTEXT *context)
{
int page_index;
if (!context->candidate_count)
return;
if (VIEW_MODE_EXPAND == context->candidates_view_mode)
page_index = context->candidate_count - pim_config->candidates_per_line * GetExpandCandidateLine();
else
page_index = context->candidate_count - pim_config->candidates_per_line;
if (page_index < 0)
page_index = 0;
context->candidate_page_count = context->candidate_count - page_index;
context->candidate_selected_index = context->candidate_page_count - 1;
context->candidate_index = page_index;
context->selected_digital = 0;
ProcessCandidateStuff(context);
context->modify_flag |= MODIFY_COMPOSE;
}
/** 处理候选上移一个
*/
void PrevCandidateItem(PIMCONTEXT *context)
{
int cand_pos = context->candidate_index + context->candidate_selected_index;
if (!context->candidate_selected_index)
{
if (!context->candidate_index) //第一页不进行翻页
return;
PrevCandidatePage(context);
context->candidate_selected_index = context->candidate_page_count - 1;
return;
}
context->selected_digital = 0;
context->candidate_selected_index--;
context->modify_flag |= MODIFY_COMPOSE;
}
/** 处理候选到第一个
*/
void HomeCandidateItem(PIMCONTEXT *context)
{
int cand_pos = context->candidate_index + context->candidate_selected_index;
context->candidate_index = 0;
context->candidate_selected_index = 0;
context->selected_digital = 0;
ProcessCandidateStuff(context);
context->modify_flag |= MODIFY_COMPOSE;
}
/** 获得当前上下文的候选信息
*/
void MakeCandidate(PIMCONTEXT *context)
{
int i, compose_cursor_index, cursor_pos, candidate_count = 0;
if (context->state == STATE_IEDIT)
{
int syllable_count = context->iedit_syllable_index == context->syllable_count ?
context->syllable_count : context->syllable_count - context->iedit_syllable_index;
SYLLABLE *syllables = context->iedit_syllable_index == context->syllable_count ?
context->syllables : context->syllables + context->iedit_syllable_index;
context->candidate_count =
GetCandidates(context, 0, syllables, syllable_count, context->candidate_array, MAX_CANDIDATES, 0);
ProcessCandidateStuff(context);
return;
}
//***当光标位于拼音串中间(而非首位)时,第一个候选项应该是所有尚未转化为汉字的音节得出的候选项,从第二个候选项
//开始才是光标之后的音节得出的候选项,第一个候选项的汉字通常被存储在context->syllable_hz中,来处理光标位于拼音
//串中间时的选择问题
if (context->syllable_mode && context->compose_cursor_index && context->compose_cursor_index < context->compose_length)
{
//希望能给所有尚未转化为汉字的拼音找一个汉字串作为第一个候选
//(默认的候选结果),但context->compose_cursor_index和context->cursor_pos
//会影响这一逻辑(见GetCandidates代码,例如光标(context->compose_cursor_index
//)位于拼音串中间时,候选的结果通常应该是光标之后的音节对应的
//词(而不是所有未转化为汉字的音节对应的词)),所以这里先将二者
//置0,获得候选后再恢复
//例:不将context->cursor_pos置0
//输入zi'guang'h'w'h'q,本来默认的候选为"紫光海外华侨",按left
//键4次后变为"海外华侨"
//保存之前的context->compose_cursor_index
compose_cursor_index = context->compose_cursor_index;
context->compose_cursor_index = 0;
//保存之前的context->cursor_pos
cursor_pos = context->cursor_pos;
context->cursor_pos = 0;
context->candidate_count =
GetCandidates(context,
context->input_string + context->input_pos,
context->syllables + context->syllable_pos,
context->syllable_count - context->syllable_pos,
context->candidate_array,
MAX_CANDIDATES,
!context->syllable_pos);
if (context->candidate_count)
candidate_count = context->candidate_count = 1;
//恢复之前的context->compose_cursor_index
context->compose_cursor_index = compose_cursor_index;
//恢复之前的context->cursor_pos
context->cursor_pos = cursor_pos;
}
//获得候选
context->candidate_count = candidate_count +
GetCandidates(context,
//如果已经有输入,则不应该读取首字母输入
context->input_string + context->input_pos,
context->syllables + context->syllable_pos,
context->syllable_count - context->syllable_pos,
context->candidate_array + candidate_count,
MAX_CANDIDATES - candidate_count,
!context->syllable_pos);
//更新默认汉字串
//if (context->candidate_count &&
//(0 == context->compose_cursor_index || context->compose_length == context->compose_cursor_index ||
//(context->syllable_count != (int)_tcslen(context->syllable_hz))))
// GetCandidateString(context, &context->candidate_array[0], context->syllable_hz, _SizeOf(context->syllable_hz));
//上面代码废弃的原因:1.默认汉字串的意义应该是为所有尚未转化为汉字的音节指定一个默认的候选结果,由于移动光标
//并不会造成任何汉字转化行为,因此context->syllable_hz应该与光标无关;2.context->syllable_hz应该尽量与尚未转
//化为汉字的音节数保持一致,且是最新的
if (context->candidate_count)
{
TCHAR candidate_hz[MAX_SYLLABLE_PER_INPUT + 0x10] = {0};
GetCandidateString(context, &context->candidate_array[0], candidate_hz, _SizeOf(candidate_hz));
if ((int)_tcslen(candidate_hz) >= context->syllable_count - context->syllable_pos)
{
_tcscpy_s((TCHAR*)context->default_hz, _SizeOf(context->default_hz), candidate_hz);
GetCandidateSyllable(&context->candidate_array[0], context->default_hz_syllables, MAX_SYLLABLE_PER_INPUT + 0x10);
}
}
//处理第一条候选和第二条候选相等的情况,应该是为了避免和上面的***处存在重复
if (candidate_count && context->candidate_count > 1)
{
TCHAR cand_str1[MAX_SPW_LENGTH + 2], cand_str2[MAX_SPW_LENGTH + 2];
int is_same;
GetCandidateString(context, &context->candidate_array[0], cand_str1, MAX_SPW_LENGTH);
GetCandidateString(context, &context->candidate_array[1], cand_str2, MAX_SPW_LENGTH);
is_same = !_tcscmp(cand_str1, cand_str2);
if (!is_same)
{
int len1 = (int)_tcslen(cand_str1);
int len2 = (int)_tcslen(cand_str2);
if (len1 > len2)
is_same = !_tcscmp(cand_str1 + len1 - len2, cand_str2);
}
if (is_same)
{
for (i = 1; i < context->candidate_count - 1; i++)
context->candidate_array[i] = context->candidate_array[i + 1];
context->candidate_count--;
}
}
//获取SPW的提示信息
context->spw_hint_string[0] = 0;
if (!context->syllable_pos && context->english_state == ENGLISH_STATE_NONE)
{
const TCHAR *spw_hint;
if (spw_hint = GetSPWHintString(context->input_string))
_tcscpy_s(context->spw_hint_string, _SizeOf(context->spw_hint_string), spw_hint);
}
if (context->candidate_index >= context->candidate_count)
context->candidate_index = 0;
context->candidate_selected_index = 0;
//处理候选准备事务
ProcessCandidateStuff(context);
}
/** 设置非法模式下的写作串
*/
void SetIllegalComposeString(PIMCONTEXT *context)
{
TCHAR *pc = context->compose_string;
TCHAR *pi = context->input_string;
TCHAR at_count = 0;
while(*pi)
{
if (*pi == '@')
{
at_count++;
if (at_count == 2)
{
pi++;
continue;
}
}
*pc++ = *pi++;
}
*pc = 0;
context->compose_length = (int)_tcslen(context->compose_string);
context->compose_cursor_index = context->cursor_pos - (at_count >= 2 ? 1 : 0);
}
/** 当输入非法的时候
*/
void PostIllegalInput(PIMCONTEXT *context)
{
context->compose_cursor_index = context->cursor_pos;
context->candidate_count = context->candidate_index = context->candidate_page_count = 0;
context->input_pos = context->syllable_count = context->syllable_pos = 0;
context->selected_item_count = 0;
SetIllegalComposeString(context);
context->modify_flag |= MODIFY_COMPOSE;
context->state = STATE_ILLEGAL;
}
/** 检查用户输入的音节是V还是U,需要与用户输入保持一致
*/
void CheckSyllableStringVAndU(PIMCONTEXT *context, int index, TCHAR *pinyin)
{
int pos = context->syllable_start_pos[index];
int i;
for (i = 0; pinyin[i]; i++)
if (context->input_string[i + pos] == 'v' && pinyin[i] == 'u')
pinyin[i] = 'v';
}
//get url hint message,put it the cache
/*void CalcCurrentURLStr(PIMCONTEXT *context)
{
//config->useurl
if(pim_config->use_url_hint)
CalcURLString(context);
else
ClearURLString();
}*/
/* 处理上下文。
* 上下文处理的基础:
* 0. state
* 1. input_string
* 2. input_pos
* 3. syllables
* 4. syllable_pos
* 5. selected_items
* 6. selected_item_count
*/
void ProcessContext(PIMCONTEXT *context)
{
SYLLABLE new_syllables[MAX_SYLLABLE_PER_INPUT];
//int new_syllable_flags[MAX_SYLLABLE_PER_INPUT] = {0};
//int new_separator_flags[MAX_SYLLABLE_PER_INPUT] = {0};
TCHAR cand_string[MAX_RESULT_LENGTH + 1];
int new_syllable_count;
int selected_length;
int cursor_pos, input_pos, new_cursor_pos;
int legal_length;
int i, j;
TCHAR *p;
//智能编辑状态
if (context->state == STATE_IEDIT && context->english_state == ENGLISH_STATE_NONE)
{
MakeCandidate(context);
//CalcCurrentURLStr(context);
return;
}
//V输入状态
if (context->state == STATE_VINPUT && context->english_state == ENGLISH_STATE_NONE)
{
if (pim_config->v_mode_enabled)
{
_tcscpy(context->compose_string, TEXT(">"));
_tcscat(context->compose_string, context->input_string + 1);
}
else
{
_tcscpy(context->compose_string, context->input_string);
}
context->compose_length = (int)_tcslen(context->compose_string);
context->compose_cursor_index = context->cursor_pos;
MakeCandidate(context); //寻找SPW
//CalcCurrentURLStr(context);
return;