Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / wrapper / ibus / src / sunpinyin_lookup_table.cpp
1 /*
2  * Copyright (c) 2009 Kov Chai <tchaikov@gmail.com>
3  *
4  * The contents of this file are subject to the terms of either the GNU Lesser
5  * General Public License Version 2.1 only ("LGPL") or the Common Development and
6  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
7  * file except in compliance with the License. You can obtain a copy of the CDDL at
8  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
9  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the 
10  * specific language governing permissions and limitations under the License. When
11  * distributing the software, include this License Header Notice in each file and
12  * include the full text of the License in the License file as well as the
13  * following notice:
14  * 
15  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
16  * (CDDL)
17  * For Covered Software in this distribution, this License shall be governed by the
18  * laws of the State of California (excluding conflict-of-law provisions).
19  * Any litigation relating to this License shall be subject to the jurisdiction of
20  * the Federal Courts of the Northern District of California and the state courts
21  * of the State of California, with venue lying in Santa Clara County, California.
22  * 
23  * Contributor(s):
24  * 
25  * If you wish your version of this file to be governed by only the CDDL or only
26  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
27  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
28  * license." If you don't indicate a single choice of license, a recipient has the
29  * option to distribute your version of this file under either the CDDL or the LGPL
30  * Version 2.1, or to extend the choice of license to its licensees as provided
31  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
32  * Version 2 license, then the option applies only if the new code is made subject
33  * to such option by the copyright holder. 
34  */
35
36 #include <sunpinyin.h>
37 #include "ibus_portable.h"
38 #include "sunpinyin_lookup_table.h"
39
40 SunPinyinLookupTable::SunPinyinLookupTable() 
41     : Pointer<IBusLookupTable>(ibus_lookup_table_new (9, 0, TRUE, TRUE))
42 {}
43
44 SunPinyinLookupTable::~SunPinyinLookupTable()
45 {
46 }
47
48 int
49 SunPinyinLookupTable::update_candidates(const ICandidateList& cl)
50 {
51     const int size = cl.size();
52     if (size <= 0)
53         return size;
54     
55     const int total = cl.total();
56     // total is the number of items in all pages, while size is the number of items in the current
57     // page (only these items are included in the candidate list).
58     ibus_lookup_table_set_page_size(*this, size);
59     // We are updating the current page only, thus some ugly hacks are needed...
60     ibus_lookup_table_clear(*this);
61     int page_start = get_current_page_start(), cur_total = 0;
62     // Dummy candidates are inserted when they are not in the current page.
63     // Maybe we can share these candidates (but what if someone else changes the attributes?)
64     for (; cur_total < page_start; ++cur_total)
65         ibus_lookup_table_append_candidate(*this, ibus_text_new_from_static_string(""));
66     for (int i = 0; i < size; ++i) {
67         const TWCHAR* cand = cl.candiString(i);
68         if (cand && cl.candiSize(i)) {
69             ibus::Text text(ibus_text_new_from_ucs4(cand));
70             decorate_candidate(text, cl.candiType(i));
71             ibus_lookup_table_append_candidate(*this, text);
72             ++cur_total;
73         } else break; // I think this shouldn't happen...
74     }
75     for (; cur_total < total; ++cur_total)
76         ibus_lookup_table_append_candidate(*this, ibus_text_new_from_static_string(""));
77
78     return size;
79     //ibus_lookup_table_set_cursor_pos (m_lookup_table, index);
80 }
81
82 bool
83 SunPinyinLookupTable::cursor_up()
84 {
85     ibus_lookup_table_cursor_down(*this);
86     return true;
87 }
88
89 bool
90 SunPinyinLookupTable::cursor_down()
91 {
92     ibus_lookup_table_cursor_down(*this);
93     return true;
94 }
95
96 size_t
97 SunPinyinLookupTable::get_cursor_pos() const
98 {
99     return ibus_lookup_table_get_cursor_pos(*this);
100 }
101
102 void
103 SunPinyinLookupTable::decorate_candidate(ibus::Text text, int type)
104 {
105     switch (type) {
106     case ICandidateList::BEST_WORD:
107         ibus_text_append_attribute (text, IBUS_ATTR_TYPE_FOREGROUND,
108                                     0x003E6B8A, 0, -1);
109         ibus_text_append_attribute (text, IBUS_ATTR_TYPE_UNDERLINE,
110                                     IBUS_ATTR_UNDERLINE_SINGLE, 0, -1);
111         break;
112     case ICandidateList::USER_SELECTED_WORD:
113         break;
114     case ICandidateList::NORMAL_WORD:
115         break;
116     }
117 }
118
119 int
120 SunPinyinLookupTable::get_current_page_start() const
121 {
122     guint cursor = ibus_lookup_table_get_cursor_pos(*this);
123     guint cursor_in_page = ibus_lookup_table_get_cursor_in_page(*this);
124     return cursor - cursor_in_page;
125 }