| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* Copyright (C) 2004-2005 Takuro Ashie |
| 4 |
* Copyright (C) 2012 CSSlayer |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2, or (at your option) |
| 9 |
* any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifdef HAVE_CONFIG_H |
| 22 |
#include <config.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include "factory.h" |
| 28 |
#include "imengine.h" |
| 29 |
#include "preedit.h" |
| 30 |
#include "utils.h" |
| 31 |
#include <fcitx/candidate.h> |
| 32 |
|
| 33 |
static ConvRule *get_period_rule (TypingMethod method, |
| 34 |
PeriodStyle period); |
| 35 |
static ConvRule *get_comma_rule (TypingMethod method, |
| 36 |
CommaStyle period); |
| 37 |
|
| 38 |
Preedit::Preedit (AnthyInstance &anthy) |
| 39 |
: m_anthy (anthy), |
| 40 |
//m_key2kana_tables (tables), |
| 41 |
m_reading (anthy), |
| 42 |
m_conversion (m_anthy, m_reading), |
| 43 |
m_input_mode (FCITX_ANTHY_MODE_HIRAGANA) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
Preedit::~Preedit () |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
/* |
| 53 |
* getting status |
| 54 |
*/ |
| 55 |
unsigned int |
| 56 |
Preedit::get_length (void) |
| 57 |
{ |
| 58 |
if (is_converting ()) |
| 59 |
return m_conversion.get_length (); |
| 60 |
else |
| 61 |
return m_reading.get_length (); |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
/* |
| 68 |
* getting status |
| 69 |
*/ |
| 70 |
unsigned int |
| 71 |
Preedit::get_length_by_char (void) |
| 72 |
{ |
| 73 |
if (is_converting ()) |
| 74 |
return m_conversion.get_length_by_char (); |
| 75 |
else |
| 76 |
return m_reading.get_length_by_char (); |
| 77 |
|
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
std::string |
| 82 |
Preedit::get_string (void) |
| 83 |
{ |
| 84 |
if (is_converting ()) { |
| 85 |
return m_conversion.get (); |
| 86 |
} else if (!m_source.empty ()) { |
| 87 |
return m_source; |
| 88 |
} else { |
| 89 |
std::string widestr; |
| 90 |
switch (m_input_mode) { |
| 91 |
case FCITX_ANTHY_MODE_KATAKANA: |
| 92 |
util_convert_to_katakana (widestr, m_reading.get_by_char ()); |
| 93 |
return widestr; |
| 94 |
|
| 95 |
case FCITX_ANTHY_MODE_HALF_KATAKANA: |
| 96 |
util_convert_to_katakana (widestr, m_reading.get_by_char (), true); |
| 97 |
return widestr; |
| 98 |
|
| 99 |
case FCITX_ANTHY_MODE_LATIN: |
| 100 |
return m_reading.get_raw_by_char (); |
| 101 |
|
| 102 |
case FCITX_ANTHY_MODE_WIDE_LATIN: |
| 103 |
util_convert_to_wide (widestr, m_reading.get_raw_by_char ()); |
| 104 |
return widestr; |
| 105 |
|
| 106 |
case FCITX_ANTHY_MODE_HIRAGANA: |
| 107 |
default: |
| 108 |
return m_reading.get_by_char (); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return std::string (); |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
Preedit::update_preedit (void) |
| 117 |
{ |
| 118 |
if (is_converting ()) |
| 119 |
{ |
| 120 |
m_conversion.update_preedit (); |
| 121 |
} else { |
| 122 |
FcitxMessages* preedit; |
| 123 |
if (m_anthy.support_client_preedit()) |
| 124 |
preedit = m_anthy.get_client_preedit(); |
| 125 |
else |
| 126 |
preedit = m_anthy.get_preedit(); |
| 127 |
|
| 128 |
std::string s = get_string(); |
| 129 |
if (s.length() > 0) |
| 130 |
FcitxMessagesAddMessageAtLast(preedit, MSG_INPUT, "%s", s.c_str()); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
Reading & |
| 135 |
Preedit::get_reading (void) |
| 136 |
{ |
| 137 |
return m_reading; |
| 138 |
} |
| 139 |
|
| 140 |
bool |
| 141 |
Preedit::is_preediting (void) |
| 142 |
{ |
| 143 |
if (m_reading.get_length () > 0 || |
| 144 |
m_conversion.is_converting () || |
| 145 |
!m_source.empty ()) |
| 146 |
{ |
| 147 |
return true; |
| 148 |
} else { |
| 149 |
return false; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
bool |
| 154 |
Preedit::is_converting (void) |
| 155 |
{ |
| 156 |
return m_conversion.is_converting (); |
| 157 |
} |
| 158 |
|
| 159 |
bool |
| 160 |
Preedit::is_predicting (void) |
| 161 |
{ |
| 162 |
return m_conversion.is_predicting (); |
| 163 |
} |
| 164 |
|
| 165 |
bool |
| 166 |
Preedit::is_reconverting (void) |
| 167 |
{ |
| 168 |
return !m_source.empty (); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
/* |
| 173 |
* manipulating the preedit string |
| 174 |
*/ |
| 175 |
bool |
| 176 |
Preedit::can_process_key_event (const KeyEvent & key) |
| 177 |
{ |
| 178 |
return m_reading.can_process_key_event (key); |
| 179 |
} |
| 180 |
|
| 181 |
bool |
| 182 |
Preedit::process_key_event (const KeyEvent & key) |
| 183 |
{ |
| 184 |
if (!m_reading.can_process_key_event (key)) |
| 185 |
return false; |
| 186 |
|
| 187 |
bool retval = m_reading.process_key_event (key); |
| 188 |
|
| 189 |
if (m_input_mode == FCITX_ANTHY_MODE_LATIN || |
| 190 |
m_input_mode == FCITX_ANTHY_MODE_WIDE_LATIN) |
| 191 |
{ |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
return retval; |
| 196 |
} |
| 197 |
|
| 198 |
bool |
| 199 |
Preedit::append (const KeyEvent & key, |
| 200 |
const std::string & string) |
| 201 |
{ |
| 202 |
return m_reading.append (key, string); |
| 203 |
} |
| 204 |
|
| 205 |
void |
| 206 |
Preedit::erase (bool backward) |
| 207 |
{ |
| 208 |
if (m_reading.get_length_by_char () <= 0) |
| 209 |
return; |
| 210 |
|
| 211 |
// cancel conversion |
| 212 |
revert (); |
| 213 |
|
| 214 |
// erase |
| 215 |
TypingMethod method = get_typing_method (); |
| 216 |
bool allow_split |
| 217 |
= method == FCITX_ANTHY_TYPING_METHOD_ROMAJI && |
| 218 |
m_anthy.get_config()->m_romaji_allow_split; |
| 219 |
if (backward && m_reading.get_caret_pos_by_char () == 0) |
| 220 |
return; |
| 221 |
if (!backward && m_reading.get_caret_pos_by_char () >= m_reading.get_length_by_char ()) |
| 222 |
return; |
| 223 |
if (backward) |
| 224 |
m_reading.move_caret (-1, allow_split); |
| 225 |
m_reading.erase (m_reading.get_caret_pos_by_char (), 1, allow_split); |
| 226 |
} |
| 227 |
|
| 228 |
void |
| 229 |
Preedit::finish (void) |
| 230 |
{ |
| 231 |
m_reading.finish (); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
/* |
| 236 |
* manipulating conversion string |
| 237 |
*/ |
| 238 |
void |
| 239 |
Preedit::convert (CandidateType type, bool single_segment) |
| 240 |
{ |
| 241 |
if (m_source.empty ()) |
| 242 |
m_conversion.convert (type, single_segment); |
| 243 |
else |
| 244 |
m_conversion.convert (m_source, single_segment); |
| 245 |
} |
| 246 |
|
| 247 |
void |
| 248 |
Preedit::convert (const std::string &source, bool single_segment) |
| 249 |
{ |
| 250 |
m_conversion.convert (source, single_segment); |
| 251 |
m_source = source; |
| 252 |
} |
| 253 |
|
| 254 |
void |
| 255 |
Preedit::revert (void) |
| 256 |
{ |
| 257 |
m_conversion.clear (); |
| 258 |
} |
| 259 |
|
| 260 |
void |
| 261 |
Preedit::commit (int segment_id, bool learn) |
| 262 |
{ |
| 263 |
if (m_conversion.is_converting ()) |
| 264 |
m_conversion.commit (segment_id, learn); |
| 265 |
if (!m_conversion.is_converting ()) |
| 266 |
clear (); |
| 267 |
} |
| 268 |
|
| 269 |
int |
| 270 |
Preedit::get_nr_segments (void) |
| 271 |
{ |
| 272 |
return m_conversion.get_nr_segments (); |
| 273 |
} |
| 274 |
|
| 275 |
std::string |
| 276 |
Preedit::get_segment_string (int segment_id) |
| 277 |
{ |
| 278 |
return m_conversion.get_segment_string (segment_id); |
| 279 |
} |
| 280 |
|
| 281 |
int |
| 282 |
Preedit::get_selected_segment (void) |
| 283 |
{ |
| 284 |
return m_conversion.get_selected_segment (); |
| 285 |
} |
| 286 |
|
| 287 |
void |
| 288 |
Preedit::select_segment (int segment_id) |
| 289 |
{ |
| 290 |
m_conversion.select_segment (segment_id); |
| 291 |
} |
| 292 |
|
| 293 |
int |
| 294 |
Preedit::get_segment_size (int segment_id) |
| 295 |
{ |
| 296 |
return m_conversion.get_segment_size (segment_id); |
| 297 |
} |
| 298 |
|
| 299 |
void |
| 300 |
Preedit::resize_segment (int relative_size, int segment_id) |
| 301 |
{ |
| 302 |
m_conversion.resize_segment (relative_size, segment_id); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
/* |
| 307 |
* candidates for a segment |
| 308 |
*/ |
| 309 |
void |
| 310 |
Preedit::get_candidates (FcitxCandidateWordList *table, int segment_id) |
| 311 |
{ |
| 312 |
m_conversion.get_candidates (table, segment_id); |
| 313 |
} |
| 314 |
|
| 315 |
int |
| 316 |
Preedit::get_selected_candidate (int segment_id) |
| 317 |
{ |
| 318 |
return m_conversion.get_selected_candidate (segment_id); |
| 319 |
} |
| 320 |
|
| 321 |
void |
| 322 |
Preedit::select_candidate (int candidate_id, int segment_id) |
| 323 |
{ |
| 324 |
m_conversion.select_candidate (candidate_id, segment_id); |
| 325 |
} |
| 326 |
|
| 327 |
/* |
| 328 |
* manipulating the caret |
| 329 |
*/ |
| 330 |
unsigned int |
| 331 |
Preedit::get_caret_pos (void) |
| 332 |
{ |
| 333 |
if (is_converting ()) { |
| 334 |
return m_conversion.get_segment_position (); |
| 335 |
} else { |
| 336 |
if (get_input_mode () == FCITX_ANTHY_MODE_HALF_KATAKANA) { |
| 337 |
// FIXME! It's ad-hoc |
| 338 |
std::string substr; |
| 339 |
substr = m_reading.get_by_char (0, m_reading.get_caret_pos_by_char (), |
| 340 |
FCITX_ANTHY_STRING_HALF_KATAKANA); |
| 341 |
return substr.length (); |
| 342 |
} else { |
| 343 |
return m_reading.get_caret_pos (); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
void |
| 349 |
Preedit::set_caret_pos_by_char (unsigned int pos) |
| 350 |
{ |
| 351 |
if (is_converting ()) |
| 352 |
return; |
| 353 |
|
| 354 |
m_reading.set_caret_pos_by_char (pos); |
| 355 |
} |
| 356 |
|
| 357 |
void |
| 358 |
Preedit::move_caret (int step) |
| 359 |
{ |
| 360 |
if (is_converting ()) |
| 361 |
return; |
| 362 |
|
| 363 |
TypingMethod method = get_typing_method (); |
| 364 |
bool allow_split |
| 365 |
= method == FCITX_ANTHY_TYPING_METHOD_ROMAJI && |
| 366 |
m_anthy.get_config()->m_romaji_allow_split; |
| 367 |
|
| 368 |
m_reading.move_caret (step, allow_split); |
| 369 |
} |
| 370 |
|
| 371 |
void |
| 372 |
Preedit::predict (void) |
| 373 |
{ |
| 374 |
m_conversion.predict (); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
/* |
| 379 |
* clear all string |
| 380 |
*/ |
| 381 |
void |
| 382 |
Preedit::clear (int segment_id) |
| 383 |
{ |
| 384 |
// FIXME! We should add implementation not only for conversion string but |
| 385 |
// also for reading string. |
| 386 |
|
| 387 |
if (!is_converting ()) { |
| 388 |
m_reading.clear (); |
| 389 |
m_conversion.clear (); |
| 390 |
m_source = std::string (); |
| 391 |
return; |
| 392 |
} |
| 393 |
|
| 394 |
m_conversion.clear (segment_id); |
| 395 |
if (m_conversion.get_nr_segments () <= 0) { |
| 396 |
m_reading.clear (); |
| 397 |
m_source = std::string (); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
/* |
| 403 |
* preference |
| 404 |
*/ |
| 405 |
void |
| 406 |
Preedit::set_input_mode (InputMode mode) |
| 407 |
{ |
| 408 |
m_input_mode = mode; |
| 409 |
} |
| 410 |
|
| 411 |
InputMode |
| 412 |
Preedit::get_input_mode (void) |
| 413 |
{ |
| 414 |
return m_input_mode; |
| 415 |
} |
| 416 |
|
| 417 |
void |
| 418 |
Preedit::set_typing_method (TypingMethod method) |
| 419 |
{ |
| 420 |
m_reading.set_typing_method (method); |
| 421 |
} |
| 422 |
|
| 423 |
TypingMethod |
| 424 |
Preedit::get_typing_method (void) |
| 425 |
{ |
| 426 |
return m_reading.get_typing_method (); |
| 427 |
} |
| 428 |
|
| 429 |
void |
| 430 |
Preedit::set_period_style (PeriodStyle style) |
| 431 |
{ |
| 432 |
m_reading.set_period_style (style); |
| 433 |
} |
| 434 |
|
| 435 |
PeriodStyle |
| 436 |
Preedit::get_period_style (void) |
| 437 |
{ |
| 438 |
return m_reading.get_period_style (); |
| 439 |
} |
| 440 |
|
| 441 |
void |
| 442 |
Preedit::set_comma_style (CommaStyle style) |
| 443 |
{ |
| 444 |
m_reading.set_comma_style (style); |
| 445 |
} |
| 446 |
|
| 447 |
CommaStyle |
| 448 |
Preedit::get_comma_style (void) |
| 449 |
{ |
| 450 |
return m_reading.get_comma_style (); |
| 451 |
} |
| 452 |
|
| 453 |
void |
| 454 |
Preedit::set_bracket_style (BracketStyle style) |
| 455 |
{ |
| 456 |
m_reading.set_bracket_style (style); |
| 457 |
} |
| 458 |
|
| 459 |
BracketStyle |
| 460 |
Preedit::get_bracket_style (void) |
| 461 |
{ |
| 462 |
return m_reading.get_bracket_style (); |
| 463 |
} |
| 464 |
|
| 465 |
void |
| 466 |
Preedit::set_slash_style (SlashStyle style) |
| 467 |
{ |
| 468 |
m_reading.set_slash_style (style); |
| 469 |
} |
| 470 |
|
| 471 |
SlashStyle |
| 472 |
Preedit::get_slash_style (void) |
| 473 |
{ |
| 474 |
return m_reading.get_slash_style (); |
| 475 |
} |
| 476 |
|
| 477 |
void |
| 478 |
Preedit::set_symbol_width (bool half) |
| 479 |
{ |
| 480 |
m_reading.set_symbol_width (half); |
| 481 |
} |
| 482 |
|
| 483 |
bool |
| 484 |
Preedit::get_symbol_width (void) |
| 485 |
{ |
| 486 |
return m_reading.get_symbol_width (); |
| 487 |
} |
| 488 |
|
| 489 |
void |
| 490 |
Preedit::set_number_width (bool half) |
| 491 |
{ |
| 492 |
m_reading.set_number_width (half); |
| 493 |
} |
| 494 |
|
| 495 |
bool |
| 496 |
Preedit::get_number_width (void) |
| 497 |
{ |
| 498 |
return m_reading.get_number_width (); |
| 499 |
} |
| 500 |
|
| 501 |
void |
| 502 |
Preedit::set_pseudo_ascii_mode (int mode) |
| 503 |
{ |
| 504 |
m_reading.set_pseudo_ascii_mode (mode); |
| 505 |
} |
| 506 |
|
| 507 |
bool |
| 508 |
Preedit::is_pseudo_ascii_mode (void) |
| 509 |
{ |
| 510 |
return m_reading.is_pseudo_ascii_mode (); |
| 511 |
} |
| 512 |
|
| 513 |
void |
| 514 |
Preedit::reset_pseudo_ascii_mode (void) |
| 515 |
{ |
| 516 |
m_reading.reset_pseudo_ascii_mode (); |
| 517 |
} |
| 518 |
|
| 519 |
bool |
| 520 |
Preedit::is_comma_or_period (const std::string & str) |
| 521 |
{ |
| 522 |
TypingMethod typing = get_typing_method (); |
| 523 |
PeriodStyle period = get_period_style (); |
| 524 |
CommaStyle comma = get_comma_style (); |
| 525 |
|
| 526 |
ConvRule *period_rule = get_period_rule (typing, period); |
| 527 |
ConvRule *comma_rule = get_comma_rule (typing, comma); |
| 528 |
|
| 529 |
for (unsigned int i = 0; period_rule && period_rule[i].string; i++) { |
| 530 |
if (period_rule[i].string && |
| 531 |
!strcmp (period_rule[i].string, str.c_str ())) |
| 532 |
{ |
| 533 |
return true; |
| 534 |
} |
| 535 |
} |
| 536 |
for (unsigned int i = 0; comma_rule && comma_rule[i].string; i++) { |
| 537 |
if (comma_rule[i].string && |
| 538 |
!strcmp (comma_rule[i].string, str.c_str ())) |
| 539 |
{ |
| 540 |
return true; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
return false; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
/* |
| 549 |
* utilities |
| 550 |
*/ |
| 551 |
static ConvRule * |
| 552 |
get_period_rule (TypingMethod method, PeriodStyle period) |
| 553 |
{ |
| 554 |
switch (method) { |
| 555 |
case FCITX_ANTHY_TYPING_METHOD_KANA: |
| 556 |
switch (period) { |
| 557 |
case FCITX_ANTHY_PERIOD_WIDE: |
| 558 |
return fcitx_anthy_kana_wide_period_rule; |
| 559 |
case FCITX_ANTHY_PERIOD_HALF: |
| 560 |
return fcitx_anthy_kana_half_period_rule; |
| 561 |
case FCITX_ANTHY_PERIOD_JAPANESE: |
| 562 |
default: |
| 563 |
return fcitx_anthy_kana_ja_period_rule; |
| 564 |
}; |
| 565 |
break; |
| 566 |
|
| 567 |
case FCITX_ANTHY_TYPING_METHOD_ROMAJI: |
| 568 |
default: |
| 569 |
switch (period) { |
| 570 |
case FCITX_ANTHY_PERIOD_WIDE: |
| 571 |
return fcitx_anthy_romaji_wide_period_rule; |
| 572 |
case FCITX_ANTHY_PERIOD_HALF: |
| 573 |
return fcitx_anthy_romaji_half_period_rule; |
| 574 |
case FCITX_ANTHY_PERIOD_JAPANESE: |
| 575 |
default: |
| 576 |
return fcitx_anthy_romaji_ja_period_rule; |
| 577 |
}; |
| 578 |
break; |
| 579 |
}; |
| 580 |
|
| 581 |
return NULL; |
| 582 |
} |
| 583 |
|
| 584 |
static ConvRule * |
| 585 |
get_comma_rule (TypingMethod method, CommaStyle period) |
| 586 |
{ |
| 587 |
switch (method) { |
| 588 |
case FCITX_ANTHY_TYPING_METHOD_KANA: |
| 589 |
switch (period) { |
| 590 |
case FCITX_ANTHY_PERIOD_WIDE: |
| 591 |
return fcitx_anthy_kana_wide_comma_rule; |
| 592 |
case FCITX_ANTHY_PERIOD_HALF: |
| 593 |
return fcitx_anthy_kana_half_comma_rule; |
| 594 |
case FCITX_ANTHY_PERIOD_JAPANESE: |
| 595 |
default: |
| 596 |
return fcitx_anthy_kana_ja_comma_rule; |
| 597 |
}; |
| 598 |
break; |
| 599 |
|
| 600 |
case FCITX_ANTHY_TYPING_METHOD_ROMAJI: |
| 601 |
default: |
| 602 |
switch (period) { |
| 603 |
case FCITX_ANTHY_PERIOD_WIDE: |
| 604 |
return fcitx_anthy_romaji_wide_comma_rule; |
| 605 |
case FCITX_ANTHY_PERIOD_HALF: |
| 606 |
return fcitx_anthy_romaji_half_comma_rule; |
| 607 |
case FCITX_ANTHY_PERIOD_JAPANESE: |
| 608 |
default: |
| 609 |
return fcitx_anthy_romaji_ja_comma_rule; |
| 610 |
}; |
| 611 |
break; |
| 612 |
}; |
| 613 |
|
| 614 |
return NULL; |
| 615 |
} |
| 616 |
/* |
| 617 |
vi:ts=4:nowrap:ai:expandtab |
| 618 |
*/ |