2 * Copyright (c) 2009 Kov Chai <tchaikov@gmail.com>
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
15 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
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.
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.
36 #include <sunpinyin.h>
37 #include "ibus_portable.h"
38 #include "sunpinyin_lookup_table.h"
40 SunPinyinLookupTable::SunPinyinLookupTable()
41 : Pointer<IBusLookupTable>(ibus_lookup_table_new (9, 0, TRUE, TRUE))
44 SunPinyinLookupTable::~SunPinyinLookupTable()
49 SunPinyinLookupTable::update_candidates(const ICandidateList& cl)
51 const int size = cl.size();
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);
73 } else break; // I think this shouldn't happen...
75 for (; cur_total < total; ++cur_total)
76 ibus_lookup_table_append_candidate(*this, ibus_text_new_from_static_string(""));
79 //ibus_lookup_table_set_cursor_pos (m_lookup_table, index);
83 SunPinyinLookupTable::cursor_up()
85 ibus_lookup_table_cursor_down(*this);
90 SunPinyinLookupTable::cursor_down()
92 ibus_lookup_table_cursor_down(*this);
97 SunPinyinLookupTable::get_cursor_pos() const
99 return ibus_lookup_table_get_cursor_pos(*this);
103 SunPinyinLookupTable::decorate_candidate(ibus::Text text, int type)
106 case ICandidateList::BEST_WORD:
107 ibus_text_append_attribute (text, IBUS_ATTR_TYPE_FOREGROUND,
109 ibus_text_append_attribute (text, IBUS_ATTR_TYPE_UNDERLINE,
110 IBUS_ATTR_UNDERLINE_SINGLE, 0, -1);
112 case ICandidateList::USER_SELECTED_WORD:
114 case ICandidateList::NORMAL_WORD:
120 SunPinyinLookupTable::get_current_page_start() const
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;