1 /** @file scim_compose_key.cpp
2 * @brief Implementation of class ComposeKeyFactory and ComposeKeyInstance.
5 /* ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable. */
8 * Smart Common Input Method
10 * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn>
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this program; if not, write to the
25 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
26 * Boston, MA 02111-1307 USA
28 * $Id: scim_compose_key.cpp,v 1.7 2005/08/16 07:26:54 suzhe Exp $
32 #define Uses_SCIM_COMPOSE_KEY
35 #include "scim_private.h"
40 #define SCIM_MAX_COMPOSE_LEN 5
42 struct ComposeSequence
44 uint32 keys [SCIM_MAX_COMPOSE_LEN];
48 class ComposeSequenceLessByKeys
51 bool operator () (const ComposeSequence &lhs, const ComposeSequence &rhs) const {
52 for (size_t i = 0; i < SCIM_MAX_COMPOSE_LEN; ++i) {
53 if (lhs.keys [i] < rhs.keys [i]) return true;
54 else if (lhs.keys [i] > rhs.keys [i]) return false;
59 bool operator () (const ComposeSequence &lhs, const uint32 *rhs) const {
60 for (size_t i = 0; i < SCIM_MAX_COMPOSE_LEN; ++i) {
61 if (lhs.keys [i] < rhs [i]) return true;
62 else if (lhs.keys [i] > rhs [i]) return false;
67 bool operator () (const uint32 *lhs, const ComposeSequence &rhs) const {
68 for (size_t i = 0; i < SCIM_MAX_COMPOSE_LEN; ++i) {
69 if (lhs [i] < rhs.keys [i]) return true;
70 else if (lhs [i] > rhs.keys [i]) return false;
75 bool operator () (const uint32 *lhs, const uint32 *rhs) const {
76 for (size_t i = 0; i < SCIM_MAX_COMPOSE_LEN; ++i) {
77 if (lhs [i] < rhs [i]) return true;
78 else if (lhs [i] > rhs [i]) return false;
85 // Generated from /usr/X11R6/lib/X11/locale/en_US.UTF-8/Compose
86 // Get rid off all keys with unicode value.
87 // Merged with the table in gtk+2.x
88 static const ComposeSequence __scim_compose_seqs[] = {
89 #include "scim_compose_key_data.h"
92 #define SCIM_NUM_COMPOSE_SEQS (sizeof (__scim_compose_seqs) / sizeof (__scim_compose_seqs [0]))
94 static uint16 __scim_compose_ignores [] = {
112 #define SCIM_NUM_COMPOSE_IGNORES (sizeof (__scim_compose_ignores) / sizeof (__scim_compose_ignores [0]))
114 ComposeKeyFactory::ComposeKeyFactory ()
119 ComposeKeyFactory::~ComposeKeyFactory ()
124 ComposeKeyFactory::get_name () const
126 return utf8_mbstowcs (_("English/Keyboard"));
130 ComposeKeyFactory::get_authors () const
132 return utf8_mbstowcs ("James Su <suzhe@tsinghua.org.cn>");
136 ComposeKeyFactory::get_credits () const
138 return WideString ();
142 ComposeKeyFactory::get_help () const
144 //return WideString ();
145 return utf8_mbstowcs (_("English input service"));
149 ComposeKeyFactory::get_uuid () const
151 return String (SCIM_COMPOSE_KEY_FACTORY_UUID);
155 ComposeKeyFactory::get_icon_file () const
157 return String (SCIM_KEYBOARD_ICON_FILE);
161 ComposeKeyFactory::validate_encoding (const String& encoding) const
167 ComposeKeyFactory::validate_locale (const String& locale) const
172 IMEngineInstancePointer
173 ComposeKeyFactory::create_instance (const String& encoding, int id)
175 return new ComposeKeyInstance (this, encoding, id);
178 ComposeKeyInstance::ComposeKeyInstance (ComposeKeyFactory *factory,
179 const String& encoding,
181 : IMEngineInstanceBase (factory, encoding, id)
183 m_compose_buffer [0] = m_compose_buffer [1] = m_compose_buffer [2] = m_compose_buffer [3] = 0;
184 m_compose_buffer [4] = m_compose_buffer [5] = m_compose_buffer [6] = m_compose_buffer [7] = 0;
187 ComposeKeyInstance::~ComposeKeyInstance ()
192 ComposeKeyInstance::process_key_event (const KeyEvent& key)
194 if (key.is_key_release ()) return false;
196 // Ignore modifier key presses.
197 if (std::binary_search (__scim_compose_ignores, __scim_compose_ignores + SCIM_NUM_COMPOSE_IGNORES, (uint16) key.code))
200 // Ignore the key if ctrl or alt is down.
201 if (key.is_control_down () || key.is_alt_down ())
206 while (m_compose_buffer [n_compose] != 0 && n_compose < SCIM_MAX_COMPOSE_LEN)
209 // The buffer is full, then reset the buffer first.
210 if (n_compose == SCIM_MAX_COMPOSE_LEN) {
215 m_compose_buffer [n_compose] = (uint32) key.code;
217 const ComposeSequence *it = std::lower_bound (__scim_compose_seqs,
218 __scim_compose_seqs + SCIM_NUM_COMPOSE_SEQS,
220 ComposeSequenceLessByKeys ());
222 // Not result found, reset the buffer and return false.
223 if (it == __scim_compose_seqs + SCIM_NUM_COMPOSE_SEQS) {
228 // Check if the compose sequence is match.
229 for (n_compose = 0; n_compose < SCIM_MAX_COMPOSE_LEN; ++ n_compose) {
230 if (m_compose_buffer [n_compose] == 0)
233 // Not match, reset the buffer and return.
234 // If it's the first key press, then return false to forward it.
235 // Otherwise return true to ignore it.
236 if (m_compose_buffer [n_compose] != it->keys [n_compose]) {
238 return n_compose != 0;
242 // Match exactly, commit the result.
243 if (n_compose == SCIM_MAX_COMPOSE_LEN || it->keys [n_compose] == 0) {
245 wstr.push_back (it->unicode);
246 commit_string (wstr);
254 ComposeKeyInstance::move_preedit_caret (unsigned int /*pos*/)
259 ComposeKeyInstance::select_candidate (unsigned int /*item*/)
264 ComposeKeyInstance::update_lookup_table_page_size (unsigned int /*page_size*/)
269 ComposeKeyInstance::lookup_table_page_up ()
274 ComposeKeyInstance::lookup_table_page_down ()
279 ComposeKeyInstance::reset ()
281 m_compose_buffer [0] = m_compose_buffer [1] = m_compose_buffer [2] = m_compose_buffer [3] = 0;
282 m_compose_buffer [4] = m_compose_buffer [5] = m_compose_buffer [6] = m_compose_buffer [7] = 0;
286 ComposeKeyInstance::focus_in ()
288 register_properties (PropertyList ());
293 ComposeKeyInstance::focus_out ()
298 ComposeKeyInstance::trigger_property (const String & /*property*/)
305 vi:ts=4:nowrap:ai:expandtab