Fix issue detected by static analysis tool
[platform/core/uifw/ise-engine-sunpinyin.git] / src / ime-core / imi_view_classic.cpp
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * The contents of this file are subject to the terms of either the GNU Lesser
7  * General Public License Version 2.1 only ("LGPL") or the Common Development and
8  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
9  * file except in compliance with the License. You can obtain a copy of the CDDL at
10  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
11  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
12  * specific language governing permissions and limitations under the License. When
13  * distributing the software, include this License Header Notice in each file and
14  * include the full text of the License in the License file as well as the
15  * following notice:
16  *
17  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
18  * (CDDL)
19  * For Covered Software in this distribution, this License shall be governed by the
20  * laws of the State of California (excluding conflict-of-law provisions).
21  * Any litigation relating to this License shall be subject to the jurisdiction of
22  * the Federal Courts of the Northern District of California and the state courts
23  * of the State of California, with venue lying in Santa Clara County, California.
24  *
25  * Contributor(s):
26  *
27  * If you wish your version of this file to be governed by only the CDDL or only
28  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
29  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
30  * license." If you don't indicate a single choice of license, a recipient has the
31  * option to distribute your version of this file under either the CDDL or the LGPL
32  * Version 2.1, or to extend the choice of license to its licensees as provided
33  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34  * Version 2 license, then the option applies only if the new code is made subject
35  * to such option by the copyright holder.
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include "imi_view_classic.h"
43 #include "imi_uiobjects.h"
44
45 #include "imi_keys.h"
46
47 CIMIClassicView::CIMIClassicView()
48     : CIMIView(), m_cursorFrIdx(0), m_candiFrIdx(0),
49       m_candiPageFirst(0)
50 {
51 }
52
53 CIMIClassicView::~CIMIClassicView()
54 {
55 }
56
57 size_t CIMIClassicView::top_candidate_threshold = 5;
58
59 void
60 CIMIClassicView::attachIC(CIMIContext* pIC)
61 {
62     CIMIView::attachIC(pIC);
63     clearIC();
64 }
65
66 unsigned
67 CIMIClassicView::clearIC(void)
68 {
69     if (!m_pIC->isEmpty()) {
70         m_cursorFrIdx = m_candiFrIdx = m_candiPageFirst = 0;
71
72         m_pIC->clear();
73         m_pPySegmentor->clear();
74         m_candiList.clear();
75         //m_tailSentence.clear ();
76         m_sentences.clear();
77         m_tails.clear();
78         return PREEDIT_MASK | CANDIDATE_MASK;
79     }
80     return 0;
81 }
82
83 void
84 CIMIClassicView::updateWindows(unsigned mask)
85 {
86     if (!m_pWinHandler)
87         return;
88
89     if (mask & PREEDIT_MASK) {
90         m_uiPreeditString.clear();
91         getPreeditString(m_uiPreeditString);
92         // m_pWinHandler->updatePreedit(&ps);
93     }
94
95     if ((mask & PREEDIT_MASK) || (mask & CANDIDATE_MASK)) {
96         // calculate all possible best sentences
97         m_sentences.clear();
98         int best_rank = -1;
99         for (size_t i = 0; i < m_pIC->getNBest(); i++) {
100             wstring sentence;
101             unsigned word_num = m_pIC->getBestSentence(sentence, i,
102                                                        m_candiFrIdx);
103             if (word_num == 0) goto pass;  // when sentence is not worthy of
104
105 #ifdef DEBUG
106             printf("%d ", i);
107             print_wide(sentence.c_str());
108             printf("\n");
109 #endif
110
111             for (size_t j = 0; j < m_sentences.size(); j++) {
112                 if (sentence == m_sentences[j].second) goto pass;
113             }
114
115             if (best_rank < 0 && word_num > 1) {
116                 best_rank = i;
117             }
118             m_sentences.push_back(std::make_pair(i, sentence));
119         pass:
120             continue;
121         }
122         // build all possible tails with best_rank
123         std::vector<CCandidates> tails =
124             m_pIC->getBestSentenceTails(best_rank, m_candiFrIdx);
125         m_tails.clear();
126         for (size_t i = 0; i < tails.size(); i++) {
127             CCandidates& tail = tails[i];
128             wstring tail_text;
129             // translate this tail into text
130             for (size_t j = 0; j < tail.size(); j++) {
131                 tail_text += tail[j].m_cwstr;
132             }
133             m_tails.push_back(std::make_pair(tail_text, tail));
134         }
135     }
136
137     if (mask & CANDIDATE_MASK) {
138         m_uiCandidateList.clear();
139         getCandidateList(m_uiCandidateList, m_candiPageFirst,
140                          m_candiWindowSize);
141         // m_pWinHandler->updateCandidates(&cl);
142         handlerUpdateCandidates(&m_uiPreeditString, &m_uiCandidateList);
143     }
144
145     if (mask & PREEDIT_MASK) {
146         handlerUpdatePreedit(&m_uiPreeditString);
147     }
148 }
149
150 bool
151 CIMIClassicView::onKeyEvent(const CKeyEvent& key)
152 {
153     unsigned changeMasks = 0;
154
155     unsigned keycode = key.code;
156     unsigned keyvalue = key.value;
157     unsigned modifiers = key.modifiers;
158
159 #ifdef DEBUG
160     printf("Classic View got a key (0x%x-0x%x-0x%x)...",
161            keycode, keyvalue, modifiers);
162     if (((modifiers & IM_CTRL_MASK) != 0) &&
163         (keyvalue == 'P' || keyvalue == 'p'))
164         m_pIC->printLattice();
165 #endif
166
167     if (m_pHotkeyProfile && m_pHotkeyProfile->isModeSwitchKey(key)) {
168         setStatusAttrValue(CIMIWinHandler::STATUS_ID_CN, (!m_bCN) ? 1 : 0);
169         if (!m_pIC->isEmpty()) {
170             changeMasks |= CANDIDATE_MASK | PREEDIT_MASK;
171             clearIC();
172         }
173     } else if (m_pHotkeyProfile && m_pHotkeyProfile->isPunctSwitchKey(key)) {
174         // On CTRL+. switch Full/Half punc
175         changeMasks |= KEYEVENT_USED;
176         setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLPUNC,
177                            (!m_bFullPunct) ? 1 : 0);
178     } else if (m_pHotkeyProfile && m_pHotkeyProfile->isSymbolSwitchKey(key)) {
179         // On SHIFT+SPACE switch Full/Half symbol
180         changeMasks |= KEYEVENT_USED;
181         setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLSYMBOL,
182                            (!m_bFullSymbol) ? 1 : 0);
183     } else if (modifiers == IM_CTRL_MASK && keycode == IM_VK_LEFT) {
184         // move left
185         if (!m_pIC->isEmpty()) {
186             changeMasks |= KEYEVENT_USED;
187             _moveLeft(changeMasks);
188         }
189     } else if (modifiers == IM_CTRL_MASK && keycode == IM_VK_RIGHT) {
190         // move right
191         if (!m_pIC->isEmpty()) {
192             changeMasks |= KEYEVENT_USED;
193             _moveRight(changeMasks);
194         }
195     } else if (((modifiers == 0 && keycode == IM_VK_PAGE_UP)
196                 || (m_pHotkeyProfile && m_pHotkeyProfile->isPageUpKey(key)))
197                && !m_pIC->isEmpty()) {
198         changeMasks |= KEYEVENT_USED;
199         if (m_candiPageFirst > 0) {
200             if (m_candiPageFirst > m_candiWindowSize)
201                 m_candiPageFirst -= m_candiWindowSize;
202             else
203                 m_candiPageFirst = 0;
204
205             changeMasks |= CANDIDATE_MASK;
206         }
207     } else if (((modifiers == 0 && keycode == IM_VK_PAGE_DOWN)
208                 || (m_pHotkeyProfile && m_pHotkeyProfile->isPageDownKey(key)))
209                && !m_pIC->isEmpty()) {
210         changeMasks |= KEYEVENT_USED;
211         if (m_candiPageFirst + m_candiWindowSize < candidateListSize()) {
212             m_candiPageFirst += m_candiWindowSize;
213             changeMasks |= CANDIDATE_MASK;
214         }
215     } else if (m_pHotkeyProfile
216               && m_pHotkeyProfile->isCandiDeleteKey(key, m_candiWindowSize)
217               && !m_pIC->isEmpty()) {
218         changeMasks |= KEYEVENT_USED;
219         unsigned sel = (keyvalue == '0' ? 9 : keyvalue - '1');
220         deleteCandidate(sel, changeMasks);
221         goto PROCESSED;
222     } else if ((modifiers &
223                 (IM_CTRL_MASK | IM_ALT_MASK | IM_SUPER_MASK |
224                  IM_RELEASE_MASK)) == 0) {
225         if ((keyvalue >= '0' && keyvalue <= '9')
226             && (m_candiWindowSize >= 10
227                 || keyvalue < ('1' + m_candiWindowSize))) {
228             // try to make selection
229             if (!m_pIC->isEmpty()) {
230                 changeMasks |= KEYEVENT_USED;
231                 unsigned sel = (keyvalue == '0' ? 9 : keyvalue - '1');
232                 makeSelection(sel, changeMasks);
233             } else if (m_smartPunct) {
234                 m_pIC->omitNextPunct();
235             }
236             goto PROCESSED;
237         }
238
239         if (keyvalue > 0x60 && keyvalue < 0x7b) {
240             /* islower(keyvalue) */
241             changeMasks |= KEYEVENT_USED;
242             _insert(keyvalue, changeMasks);
243         } else if (keyvalue > 0x20 && keyvalue < 0x7f) {
244             /* isprint(keyvalue) && !isspace(keyvalue) */
245             changeMasks |= KEYEVENT_USED;
246             if (m_pIC->isEmpty()) {
247                 _insert(keyvalue, changeMasks);
248                 _doCommit();
249                 clearIC();
250             } else {
251                 _insert(keyvalue, changeMasks);
252             }
253         } else if (keycode == IM_VK_BACK_SPACE || keycode == IM_VK_DELETE) {
254             if (!m_pIC->isEmpty()) {
255                 changeMasks |= KEYEVENT_USED;
256                 _erase(keycode == IM_VK_BACK_SPACE, changeMasks);
257             }
258         } else if (keycode == IM_VK_SPACE) {
259             if (!m_pIC->isEmpty()) {
260                 changeMasks |= KEYEVENT_USED;
261                 makeSelection(0, changeMasks);
262             } else {
263                 wstring wstr = (m_pIC->fullPuncOp())(keyvalue);
264                 if (wstr.size()) {
265                     _commitString(wstr);
266                     changeMasks |= KEYEVENT_USED;
267                 }
268             }
269         } else if (keycode == IM_VK_ENTER) {
270             if (!m_pIC->isEmpty()) {
271                 changeMasks |= KEYEVENT_USED | CANDIDATE_MASK | PREEDIT_MASK;
272                 _doCommit(false);
273                 clearIC();
274             }
275         } else if (keycode == IM_VK_ESCAPE) {
276             if (!m_pIC->isEmpty()) {
277                 changeMasks |= KEYEVENT_USED | CANDIDATE_MASK | PREEDIT_MASK;
278                 clearIC();
279             }
280         } else if (keycode == IM_VK_LEFT) { // move left syllable
281             if (!m_pIC->isEmpty()) {
282                 changeMasks |= KEYEVENT_USED;
283                 _moveLeftSyllable(changeMasks);
284             }
285         } else if (keycode == IM_VK_RIGHT) { // move right syllable
286             if (!m_pIC->isEmpty()) {
287                 changeMasks |= KEYEVENT_USED;
288                 _moveRightSyllable(changeMasks);
289             }
290         } else if (keycode == IM_VK_HOME) { // move home
291             if (!m_pIC->isEmpty()) {
292                 changeMasks |= KEYEVENT_USED;
293                 _moveHome(changeMasks);
294             }
295         } else if (keycode == IM_VK_END) { // move end
296             if (!m_pIC->isEmpty()) {
297                 changeMasks |= KEYEVENT_USED;
298                 _moveEnd(changeMasks);
299             }
300         }
301     } else {
302         goto RETURN;
303     }
304
305 PROCESSED:;
306     m_pHotkeyProfile->rememberLastKey(key);
307
308 RETURN:;
309
310 #ifdef DEBUG
311     printf("   |-->(Mask=0x%x)\n", changeMasks);
312 #endif
313
314     updateWindows(changeMasks);
315     return changeMasks & KEYEVENT_USED;
316 }
317
318 int
319 CIMIClassicView::onCandidatePageRequest(int pgno, bool relative)
320 {
321     unsigned changeMasks = 0;
322     int ncandi, lastpgidx;
323
324     if (!m_pIC->isEmpty()) {
325         changeMasks |= KEYEVENT_USED;
326         size_t sz = candidateListSize();
327         lastpgidx = (sz - 1) / m_candiWindowSize * m_candiWindowSize;
328         if (relative == true) {
329             ncandi = m_candiPageFirst + pgno * m_candiWindowSize;
330             if (ncandi >= (int) sz)
331                 ncandi = lastpgidx;
332             if (ncandi < 0)
333                 ncandi = 0;
334             if (ncandi != (int) m_candiPageFirst) {
335                 m_candiPageFirst = ncandi;
336                 changeMasks |= CANDIDATE_MASK;
337             }
338         } else {
339             if (pgno == -1) { //last page
340                 ncandi = lastpgidx;
341             } else {
342                 ncandi = pgno * m_candiWindowSize;
343                 if (ncandi > lastpgidx)
344                     ncandi = lastpgidx;
345             }
346             if (ncandi != (int) m_candiPageFirst) {
347                 m_candiPageFirst = ncandi;
348                 changeMasks |= CANDIDATE_MASK;
349             }
350         }
351     }
352
353     updateWindows(changeMasks);
354     return 0;
355 }
356
357 int
358 CIMIClassicView::onCandidateSelectRequest(int index)
359 {
360     unsigned changeMasks = 0;
361
362     if (!m_pIC->isEmpty())
363         makeSelection(index, changeMasks);
364
365     updateWindows(changeMasks);
366     return 0;
367 }
368
369 void
370 CIMIClassicView::getPreeditString(IPreeditString& ps)
371 {
372     ps.clear();
373
374     wstring &wstr = ps.getString();
375     IPreeditString::CCharTypeVec& charTypes = ps.getCharTypeVec();
376
377     m_pIC->getSelectedSentence(wstr, 0, m_candiFrIdx);
378
379     int caret = wstr.size();
380     charTypes.reserve(caret);
381     for (int i = 0; i < caret; ++i)
382         charTypes.push_back(
383             IPreeditString::HANZI_CHAR | IPreeditString::USER_CHOICE);
384
385     const wstring& pystr = m_pPySegmentor->getInputBuffer();
386     std::vector<unsigned>& seg_path = m_pIC->getBestSegPath();
387
388     if (pystr.empty())
389         return;
390
391     CLattice& lattice = m_pIC->getLattice();
392     unsigned last = 0, len = 0;
393     for (std::vector<unsigned>::iterator it = seg_path.begin();
394          it != seg_path.end(); ++it) {
395         len = *it - last;
396         if (*it <= m_candiFrIdx) {
397             last = *it;
398             continue;
399         }
400         if (last < m_cursorFrIdx && m_cursorFrIdx <= last + len)
401             caret = wstr.size() + (m_cursorFrIdx - last);
402
403         CLatticeFrame &fr = lattice[last + len];
404         int ct = IPreeditString::PINYIN_CHAR;
405         if (fr.isSyllableSepFrame()) {
406             ct = IPreeditString::BOUNDARY | IPreeditString::USER_CHOICE;
407         } else if (fr.m_type == CLatticeFrame::ASCII) {
408             ct = IPreeditString::ASCII_CHAR;
409         } else if (fr.m_type == CLatticeFrame::SYMBOL) {
410             ct = IPreeditString::SYMBOL_CHAR;
411         }
412
413         wstr.insert(wstr.end(), pystr.begin() + last,
414                     pystr.begin() + last + len);
415         for (size_t c = 0; c < len; ++c)
416             charTypes.push_back(ct);
417
418         if (fr.isSyllableFrame() && !fr.isSyllableSepFrame()) {
419             if (it != seg_path.end() - 1
420                 && !lattice[last + len + 1].isSyllableSepFrame()) {
421                 wstr.push_back(' ');
422                 charTypes.push_back(IPreeditString::BOUNDARY);
423             }
424         }
425         last = *it;
426     }
427     // segment path might be missing or incomplete, therefore append the input
428     // buffer left to wstr
429     wstr.insert(wstr.end(), pystr.begin() + last, pystr.end());
430
431     ps.setCaret(caret);
432 }
433
434 void
435 CIMIClassicView::getCandidateList(ICandidateList& cl, int start, int size)
436 {
437     cl.clear();
438     cl.setSize(size);
439
440     cl.setFirst(start);
441     cl.setTotal(candidateListSize());
442
443     // sentences
444     for (size_t i = 0; i < m_sentences.size(); i++) {
445         cl.pushBackCandidate(m_sentences[i].second,
446                              ICandidateList::BEST_TAIL, i);
447     }
448     // possible tails
449     for (size_t i = 0; i < m_tails.size(); i++) {
450         cl.pushBackCandidate(m_tails[i].first,
451                              ICandidateList::OTHER_BEST_TAIL, i);
452     }
453     // word candidates
454     for (size_t i = 0; i < m_candiList.size(); i++) {
455         if (i == 0) {
456             cl.pushBackCandidate(m_candiList[i].m_cwstr,
457                                  ICandidateList::BEST_WORD, i);
458         } else {
459             cl.pushBackCandidate(m_candiList[i].m_cwstr,
460                                  ICandidateList::NORMAL_WORD, i);
461         }
462     }
463 }
464
465 void
466 CIMIClassicView::_insert(unsigned keyvalue, unsigned &changeMasks)
467 {
468     changeMasks |= KEYEVENT_USED;
469
470     if (m_pPySegmentor->getInputBuffer().size() >= MAX_LATTICE_LENGTH - 2)
471         return;
472
473     if (m_cursorFrIdx == m_pIC->getLastFrIdx())
474         m_pPySegmentor->push(keyvalue);
475     else
476         m_pPySegmentor->insertAt(m_cursorFrIdx, keyvalue);
477
478     m_cursorFrIdx++;
479
480     if (m_pIC->buildLattice(m_pPySegmentor))
481         _getCandidates();
482
483     changeMasks |= PREEDIT_MASK | CANDIDATE_MASK;
484 }
485
486 void
487 CIMIClassicView::_erase(bool backward, unsigned &changeMasks)
488 {
489     if (backward) {
490         // if possible to cancel the last selection
491         if (m_backspaceCancel) {
492             if (m_candiFrIdx > 0) {
493                 m_candiFrIdx = m_pIC->cancelSelection(m_candiFrIdx, true);
494                 goto done;
495             }
496         }
497         if (m_cursorFrIdx == m_pIC->getLastFrIdx()) {
498             m_pPySegmentor->pop();
499         } else if (m_cursorFrIdx > 0) {
500             m_pPySegmentor->deleteAt(m_cursorFrIdx - 1, backward);
501         } else {
502             return;
503         }
504         _moveLeft(changeMasks, true);
505     } else {
506         if (m_cursorFrIdx == m_pIC->getLastFrIdx() - 1) {
507             m_pPySegmentor->pop();
508         } else if (m_cursorFrIdx < m_pIC->getLastFrIdx() - 1) {
509             m_pPySegmentor->deleteAt(m_cursorFrIdx - 1, backward);
510         } else {
511             return;
512         }
513     }
514 done:
515     if (m_pIC->buildLattice(m_pPySegmentor))
516         _getCandidates();
517
518     changeMasks |= PREEDIT_MASK | CANDIDATE_MASK | KEYEVENT_USED;
519 }
520
521 void
522 CIMIClassicView::_getCandidates()
523 {
524     m_candiPageFirst = 0;
525     m_pIC->getCandidates(m_candiFrIdx, m_candiList);
526 }
527
528 void
529 CIMIClassicView::_commitChar(TWCHAR ch)
530 {
531     TWCHAR wa[2] = { ch, 0 };
532     m_pWinHandler->commit(wa);
533 }
534
535 void
536 CIMIClassicView::_commitString(const wstring& wstr)
537 {
538     m_pWinHandler->commit(wstr.c_str());
539 }
540
541 void
542 CIMIClassicView::_doCommit(bool bConvert)
543 {
544     wstring bs;
545
546     if (bConvert) {
547         m_pIC->memorize();
548         m_pIC->getSelectedSentence(bs);
549         handlerCommit(bs.c_str());
550     } else {
551         bs += m_pPySegmentor->getInputBuffer();
552         handlerCommit(bs.c_str());
553     }
554 }
555
556 unsigned
557 CIMIClassicView::_moveLeft(unsigned& mask, bool searchAgain)
558 {
559     if (m_cursorFrIdx == 0)
560         return _moveEnd(mask);
561
562     mask |= PREEDIT_MASK;
563     if (m_cursorFrIdx == m_candiFrIdx) {
564         mask |= CANDIDATE_MASK;
565         m_candiFrIdx = m_pIC->cancelSelection(m_candiFrIdx, searchAgain);
566         _getCandidates();
567     }
568
569     return --m_cursorFrIdx;
570 }
571
572 unsigned
573 CIMIClassicView::_moveLeftSyllable(unsigned& mask, bool searchAgain)
574 {
575     if (m_cursorFrIdx == 0)
576         return _moveEnd(mask);
577
578     mask |= PREEDIT_MASK;
579
580     if (m_cursorFrIdx == m_candiFrIdx) {
581         mask |= CANDIDATE_MASK;
582         m_candiFrIdx = m_pIC->cancelSelection(m_candiFrIdx, searchAgain);
583         _getCandidates();
584     }
585
586     std::vector<unsigned>& seg_path = m_pIC->getBestSegPath();
587     std::vector<unsigned>::iterator it =
588         std::upper_bound(seg_path.begin(), seg_path.end(), m_cursorFrIdx - 1);
589     return m_cursorFrIdx = *(--it);
590 }
591
592 unsigned
593 CIMIClassicView::_moveHome(unsigned& mask, bool searchAgain)
594 {
595     if (m_cursorFrIdx == 0)
596         return 0;
597
598     mask |= PREEDIT_MASK;
599
600     if (m_candiFrIdx != 0) {
601         std::vector<unsigned>& best_path = m_pIC->getBestPath();
602         std::vector<unsigned>::iterator it = best_path.begin();
603         std::vector<unsigned>::iterator ite = best_path.end();
604         CLattice& lattice = m_pIC->getLattice();
605
606         for (; it != ite; ++it) {
607             if (lattice[*it].m_bwType & CLatticeFrame::USER_SELECTED)
608                 m_pIC->cancelSelection(*it, false);
609         }
610
611         mask |= CANDIDATE_MASK;
612         m_candiFrIdx = 0;
613         _getCandidates();
614
615         if (searchAgain) m_pIC->searchFrom();
616     }
617
618     m_cursorFrIdx = 0;
619     return 0;
620 }
621
622 unsigned
623 CIMIClassicView::_moveRight(unsigned& mask)
624 {
625     if (m_cursorFrIdx < m_pIC->getLastFrIdx()) {
626         mask |= PREEDIT_MASK;
627         ++m_cursorFrIdx;
628         return m_cursorFrIdx;
629     }
630
631     return _moveHome(mask);
632 }
633
634 unsigned
635 CIMIClassicView::_moveRightSyllable(unsigned& mask)
636 {
637     if (m_cursorFrIdx < m_pIC->getLastFrIdx()) {
638         mask |= PREEDIT_MASK;
639
640         std::vector<unsigned>& seg_path = m_pIC->getBestSegPath();
641         std::vector<unsigned>::iterator it = std::upper_bound(
642             seg_path.begin(), seg_path.end(), m_cursorFrIdx);
643         m_cursorFrIdx = *it;
644         return m_cursorFrIdx;
645     }
646
647     return _moveHome(mask);
648 }
649
650 unsigned
651 CIMIClassicView::_moveEnd(unsigned& mask)
652 {
653     if (m_cursorFrIdx < m_pIC->getLastFrIdx()) {
654         mask |= PREEDIT_MASK;
655         m_cursorFrIdx = m_pIC->getLastFrIdx();
656     }
657
658     return m_cursorFrIdx;
659 }
660
661 void
662 CIMIClassicView::makeSelection(int candiIdx, unsigned& mask)
663 {
664     if (m_candiList.size() == 0 || m_sentences.size() == 0) {
665         // user might delete all the left over pinyin characters, this will
666         // make m_candiList empty
667         // 0 or space choices should commit previous selected candidates
668         mask |= PREEDIT_MASK | CANDIDATE_MASK;
669         _doCommit();
670         clearIC();
671         return;
672     }
673
674     // candiIdx += m_candiPageFirst;
675     if (candiIdx >= m_uiCandidateList.size()) {
676         return;
677     }
678     int idx = m_uiCandidateList.getUserIndex(candiIdx);
679     int type = m_uiCandidateList.getCandiTypeVec()[candiIdx];
680     bool selected = false;
681
682     mask |= PREEDIT_MASK | CANDIDATE_MASK;
683     if (type == ICandidateList::BEST_TAIL) {
684         // get the rank of that sentence and select it
685         m_pIC->selectSentence(m_sentences[idx].first);
686         _doCommit();
687         clearIC();
688     } else if (type == ICandidateList::OTHER_BEST_TAIL) {
689         CCandidates& tail = m_tails[idx].second;
690         for (size_t i = 0; i < tail.size(); i++) {
691             m_pIC->makeSelection(tail[i]);
692         }
693         m_candiFrIdx = tail.back().m_end;
694         selected = true;
695     } else if (type == ICandidateList::BEST_WORD
696                || type == ICandidateList::NORMAL_WORD) {
697         CCandidate& candi = m_candiList[idx];
698         m_pIC->makeSelection(candi);
699         m_candiFrIdx = candi.m_end;
700         selected = true;
701     } else if (type == ICandidateList::PLUGIN_TAIL) {
702         handlerCommit(m_uiCandidateList.getCandiStrings()[candiIdx]);
703         clearIC();
704     }
705
706     if (selected) {
707         if (m_cursorFrIdx < m_candiFrIdx)
708             m_cursorFrIdx = m_candiFrIdx;
709
710         CLattice& lattice = m_pIC->getLattice();
711         while (m_candiFrIdx < m_pIC->getLastFrIdx() &&
712                !lattice[m_candiFrIdx + 1].isUnusedFrame() &&
713                !lattice[m_candiFrIdx + 1].isSyllableFrame()) {
714             ++m_candiFrIdx;
715             lattice[m_candiFrIdx].m_bwType |= CLatticeFrame::IGNORED;
716         }
717
718         if (m_candiFrIdx == m_pIC->getLastFrIdx()) {
719             _doCommit();
720             clearIC();
721         } else {
722             m_candiPageFirst = 0;
723             _getCandidates();
724         }
725     }
726 }
727
728 void
729 CIMIClassicView::deleteCandidate(int candiIdx, unsigned& mask)
730 {
731     // candiIdx += m_candiPageFirst;
732     int idx = m_uiCandidateList.getUserIndex(candiIdx);
733     int type = m_uiCandidateList.getCandiTypeVec()[candiIdx];
734
735     if (type == ICandidateList::BEST_TAIL) {
736         // try to remove candidate 0 which is a calculated sentence
737         std::vector<unsigned> wids;
738         m_pIC->getSelectedSentence(wids, m_candiFrIdx);
739         m_pIC->removeFromHistoryCache(wids);
740
741         /* if the sentence wid length is 1, also delete this word */
742         if (wids.size() == 1) {
743             unsigned wid = wids[0];
744             m_pIC->deleteCandidateByWID(wid);
745         }
746     } else if (type == ICandidateList::BEST_WORD
747                || type == ICandidateList::NORMAL_WORD) {
748         // remove an ordinary candidate
749         CCandidate& candi = m_candiList[idx];
750         m_pIC->deleteCandidate(candi);
751     }
752
753     _getCandidates();
754     mask |= PREEDIT_MASK | CANDIDATE_MASK;
755 }