e1c9e9a29028417967c9049fc0a779625fc2e1c5
[platform/framework/web/crosswalk.git] / src / ui / base / ime / win / imm32_manager.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/base/ime/win/imm32_manager.h"
6
7 #include <msctf.h>
8
9 #include "base/basictypes.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/win/scoped_comptr.h"
15 #include "third_party/skia/include/core/SkColor.h"
16 #include "ui/base/ime/composition_text.h"
17
18 // "imm32.lib" is required by IMM32 APIs used in this file.
19 // NOTE(hbono): To comply with a comment from Darin, I have added
20 // this #pragma directive instead of adding "imm32.lib" to a project file.
21 #pragma comment(lib, "imm32.lib")
22
23 // Following code requires wchar_t to be same as char16. It should always be
24 // true on Windows.
25 COMPILE_ASSERT(sizeof(wchar_t) == sizeof(base::char16), wchar_t__char16_diff);
26
27 ///////////////////////////////////////////////////////////////////////////////
28 // IMM32Manager
29
30 namespace {
31
32 // Determines whether or not the given attribute represents a target
33 // (a.k.a. a selection).
34 bool IsTargetAttribute(char attribute) {
35   return (attribute == ATTR_TARGET_CONVERTED ||
36           attribute == ATTR_TARGET_NOTCONVERTED);
37 }
38
39 // Helper function for IMM32Manager::GetCompositionInfo() method, to get the
40 // target range that's selected by the user in the current composition string.
41 void GetCompositionTargetRange(HIMC imm_context, int* target_start,
42                                int* target_end) {
43   int attribute_size = ::ImmGetCompositionString(imm_context, GCS_COMPATTR,
44                                                  NULL, 0);
45   if (attribute_size > 0) {
46     int start = 0;
47     int end = 0;
48     scoped_ptr<char[]> attribute_data(new char[attribute_size]);
49     if (attribute_data.get()) {
50       ::ImmGetCompositionString(imm_context, GCS_COMPATTR,
51                                 attribute_data.get(), attribute_size);
52       for (start = 0; start < attribute_size; ++start) {
53         if (IsTargetAttribute(attribute_data[start]))
54           break;
55       }
56       for (end = start; end < attribute_size; ++end) {
57         if (!IsTargetAttribute(attribute_data[end]))
58           break;
59       }
60     }
61     *target_start = start;
62     *target_end = end;
63   }
64 }
65
66 // Helper function for IMM32Manager::GetCompositionInfo() method, to get
67 // underlines information of the current composition string.
68 void GetCompositionUnderlines(HIMC imm_context,
69                               int target_start,
70                               int target_end,
71                               ui::CompositionUnderlines* underlines) {
72   int clause_size = ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE,
73                                               NULL, 0);
74   int clause_length = clause_size / sizeof(uint32);
75   if (clause_length) {
76     scoped_ptr<uint32[]> clause_data(new uint32[clause_length]);
77     if (clause_data.get()) {
78       ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE,
79                                 clause_data.get(), clause_size);
80       for (int i = 0; i < clause_length - 1; ++i) {
81         ui::CompositionUnderline underline;
82         underline.start_offset = clause_data[i];
83         underline.end_offset = clause_data[i+1];
84         underline.color = SK_ColorBLACK;
85         underline.thick = false;
86
87         // Use thick underline for the target clause.
88         if (underline.start_offset >= static_cast<unsigned>(target_start) &&
89             underline.end_offset <= static_cast<unsigned>(target_end)) {
90           underline.thick = true;
91         }
92         underlines->push_back(underline);
93       }
94     }
95   }
96 }
97
98 // Checks if a given primary language ID is a RTL language.
99 bool IsRTLPrimaryLangID(LANGID lang) {
100   switch (lang) {
101     case LANG_ARABIC:
102     case LANG_HEBREW:
103     case LANG_PERSIAN:
104     case LANG_SYRIAC:
105     case LANG_UIGHUR:
106     case LANG_URDU:
107       return true;
108     default:
109       return false;
110   }
111 }
112
113 }  // namespace
114
115 namespace ui {
116
117 IMM32Manager::IMM32Manager()
118     : ime_status_(false),
119       input_language_id_(LANG_USER_DEFAULT),
120       is_composing_(false),
121       system_caret_(false),
122       caret_rect_(-1, -1, 0, 0),
123       use_composition_window_(false) {
124 }
125
126 IMM32Manager::~IMM32Manager() {
127 }
128
129 bool IMM32Manager::SetInputLanguage() {
130   // Retrieve the current keyboard layout from Windows and determine whether
131   // or not the current input context has IMEs.
132   // Also save its input language for language-specific operations required
133   // while composing a text.
134   HKL keyboard_layout = ::GetKeyboardLayout(0);
135   input_language_id_ = reinterpret_cast<LANGID>(keyboard_layout);
136
137   // Check TSF Input Processor first.
138   // If the active profile is TSF INPUTPROCESSOR, this is IME.
139   base::win::ScopedComPtr<ITfInputProcessorProfileMgr> prof_mgr;
140   TF_INPUTPROCESSORPROFILE prof;
141   if (SUCCEEDED(prof_mgr.CreateInstance(CLSID_TF_InputProcessorProfiles)) &&
142       SUCCEEDED(prof_mgr->GetActiveProfile(GUID_TFCAT_TIP_KEYBOARD, &prof)) &&
143       prof.hkl == NULL &&
144       prof.dwProfileType == TF_PROFILETYPE_INPUTPROCESSOR) {
145       ime_status_ = true;
146   } else {
147     // If the curent language is not using TSF, check IMM32 based IMEs.
148     // As ImmIsIME always returns non-0 value on Vista+, use ImmGetIMEFileName
149     // instead to check if this HKL has any associated IME file.
150     ime_status_ = (ImmGetIMEFileName(keyboard_layout, NULL, 0) != 0);
151   }
152
153   return ime_status_;
154 }
155
156 void IMM32Manager::CreateImeWindow(HWND window_handle) {
157   // When a user disables TSF (Text Service Framework) and CUAS (Cicero
158   // Unaware Application Support), Chinese IMEs somehow ignore function calls
159   // to ::ImmSetCandidateWindow(), i.e. they do not move their candidate
160   // window to the position given as its parameters, and use the position
161   // of the current system caret instead, i.e. it uses ::GetCaretPos() to
162   // retrieve the position of their IME candidate window.
163   // Therefore, we create a temporary system caret for Chinese IMEs and use
164   // it during this input context.
165   // Since some third-party Japanese IME also uses ::GetCaretPos() to determine
166   // their window position, we also create a caret for Japanese IMEs.
167   if (PRIMARYLANGID(input_language_id_) == LANG_CHINESE ||
168       PRIMARYLANGID(input_language_id_) == LANG_JAPANESE) {
169     if (!system_caret_) {
170       if (::CreateCaret(window_handle, NULL, 1, 1)) {
171         system_caret_ = true;
172       }
173     }
174   }
175   // Restore the positions of the IME windows.
176   UpdateImeWindow(window_handle);
177 }
178
179 LRESULT IMM32Manager::SetImeWindowStyle(HWND window_handle, UINT message,
180                                     WPARAM wparam, LPARAM lparam,
181                                     BOOL* handled) {
182   // To prevent the IMM (Input Method Manager) from displaying the IME
183   // composition window, Update the styles of the IME windows and EXPLICITLY
184   // call ::DefWindowProc() here.
185   // NOTE(hbono): We can NEVER let WTL call ::DefWindowProc() when we update
186   // the styles of IME windows because the 'lparam' variable is a local one
187   // and all its updates disappear in returning from this function, i.e. WTL
188   // does not call ::DefWindowProc() with our updated 'lparam' value but call
189   // the function with its original value and over-writes our window styles.
190   *handled = TRUE;
191   lparam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
192   return ::DefWindowProc(window_handle, message, wparam, lparam);
193 }
194
195 void IMM32Manager::DestroyImeWindow(HWND window_handle) {
196   // Destroy the system caret if we have created for this IME input context.
197   if (system_caret_) {
198     ::DestroyCaret();
199     system_caret_ = false;
200   }
201 }
202
203 void IMM32Manager::MoveImeWindow(HWND window_handle, HIMC imm_context) {
204   // Does nothing when the target window has no input focus. This is important
205   // because the renderer may issue SelectionBoundsChanged event even when it
206   // has no input focus. (e.g. the page update caused by incremental search.)
207   // So this event should be ignored when the |window_handle| no longer has the
208   // input focus.
209   if (GetFocus() != window_handle)
210     return;
211
212   int x = caret_rect_.x();
213   int y = caret_rect_.y();
214
215   const int kCaretMargin = 1;
216   if (!use_composition_window_ &&
217       PRIMARYLANGID(input_language_id_) == LANG_CHINESE) {
218     // As written in a comment in IMM32Manager::CreateImeWindow(),
219     // Chinese IMEs ignore function calls to ::ImmSetCandidateWindow()
220     // when a user disables TSF (Text Service Framework) and CUAS (Cicero
221     // Unaware Application Support).
222     // On the other hand, when a user enables TSF and CUAS, Chinese IMEs
223     // ignore the position of the current system caret and uses the
224     // parameters given to ::ImmSetCandidateWindow() with its 'dwStyle'
225     // parameter CFS_CANDIDATEPOS.
226     // Therefore, we do not only call ::ImmSetCandidateWindow() but also
227     // set the positions of the temporary system caret if it exists.
228     CANDIDATEFORM candidate_position = {0, CFS_CANDIDATEPOS, {x, y},
229                                         {0, 0, 0, 0}};
230     ::ImmSetCandidateWindow(imm_context, &candidate_position);
231   }
232   if (system_caret_) {
233     switch (PRIMARYLANGID(input_language_id_)) {
234       case LANG_JAPANESE:
235         ::SetCaretPos(x, y + caret_rect_.height());
236         break;
237       default:
238         ::SetCaretPos(x, y);
239         break;
240     }
241   }
242   if (use_composition_window_) {
243     // Moves the composition text window.
244     COMPOSITIONFORM cf = {CFS_POINT, {x, y}};
245     ::ImmSetCompositionWindow(imm_context, &cf);
246     // Don't need to set the position of candidate window.
247     return;
248   }
249
250   if (PRIMARYLANGID(input_language_id_) == LANG_KOREAN) {
251     // Chinese IMEs and Japanese IMEs require the upper-left corner of
252     // the caret to move the position of their candidate windows.
253     // On the other hand, Korean IMEs require the lower-left corner of the
254     // caret to move their candidate windows.
255     y += kCaretMargin;
256   }
257   // Japanese IMEs and Korean IMEs also use the rectangle given to
258   // ::ImmSetCandidateWindow() with its 'dwStyle' parameter CFS_EXCLUDE
259   // to move their candidate windows when a user disables TSF and CUAS.
260   // Therefore, we also set this parameter here.
261   CANDIDATEFORM exclude_rectangle = {0, CFS_EXCLUDE, {x, y},
262       {x, y, x + caret_rect_.width(), y + caret_rect_.height()}};
263   ::ImmSetCandidateWindow(imm_context, &exclude_rectangle);
264 }
265
266 void IMM32Manager::UpdateImeWindow(HWND window_handle) {
267   // Just move the IME window attached to the given window.
268   if (caret_rect_.x() >= 0 && caret_rect_.y() >= 0) {
269     HIMC imm_context = ::ImmGetContext(window_handle);
270     if (imm_context) {
271       MoveImeWindow(window_handle, imm_context);
272       ::ImmReleaseContext(window_handle, imm_context);
273     }
274   }
275 }
276
277 void IMM32Manager::CleanupComposition(HWND window_handle) {
278   // Notify the IMM attached to the given window to complete the ongoing
279   // composition, (this case happens when the given window is de-activated
280   // while composing a text and re-activated), and reset the omposition status.
281   if (is_composing_) {
282     HIMC imm_context = ::ImmGetContext(window_handle);
283     if (imm_context) {
284       ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
285       ::ImmReleaseContext(window_handle, imm_context);
286     }
287     ResetComposition(window_handle);
288   }
289 }
290
291 void IMM32Manager::ResetComposition(HWND window_handle) {
292   // Currently, just reset the composition status.
293   is_composing_ = false;
294 }
295
296 void IMM32Manager::CompleteComposition(HWND window_handle, HIMC imm_context) {
297   // We have to confirm there is an ongoing composition before completing it.
298   // This is for preventing some IMEs from getting confused while completing an
299   // ongoing composition even if they do not have any ongoing compositions.)
300   if (is_composing_) {
301     ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
302     ResetComposition(window_handle);
303   }
304 }
305
306 void IMM32Manager::GetCompositionInfo(HIMC imm_context, LPARAM lparam,
307                                   CompositionText* composition) {
308   // We only care about GCS_COMPATTR, GCS_COMPCLAUSE and GCS_CURSORPOS, and
309   // convert them into underlines and selection range respectively.
310   composition->underlines.clear();
311
312   int length = static_cast<int>(composition->text.length());
313
314   // Find out the range selected by the user.
315   int target_start = length;
316   int target_end = length;
317   if (lparam & GCS_COMPATTR)
318     GetCompositionTargetRange(imm_context, &target_start, &target_end);
319
320   // Retrieve the selection range information. If CS_NOMOVECARET is specified,
321   // that means the cursor should not be moved, then we just place the caret at
322   // the beginning of the composition string. Otherwise we should honour the
323   // GCS_CURSORPOS value if it's available.
324   // TODO(suzhe): due to a bug of webkit, we currently can't use selection range
325   // with composition string. See: https://bugs.webkit.org/show_bug.cgi?id=40805
326   if (!(lparam & CS_NOMOVECARET) && (lparam & GCS_CURSORPOS)) {
327     // IMM32 does not support non-zero-width selection in a composition. So
328     // always use the caret position as selection range.
329     int cursor = ::ImmGetCompositionString(imm_context, GCS_CURSORPOS, NULL, 0);
330     composition->selection = gfx::Range(cursor);
331   } else {
332     composition->selection = gfx::Range(0);
333   }
334
335   // Retrieve the clause segmentations and convert them to underlines.
336   if (lparam & GCS_COMPCLAUSE) {
337     GetCompositionUnderlines(imm_context, target_start, target_end,
338                              &composition->underlines);
339   }
340
341   // Set default underlines in case there is no clause information.
342   if (!composition->underlines.size()) {
343     CompositionUnderline underline;
344     underline.color = SK_ColorBLACK;
345     if (target_start > 0) {
346       underline.start_offset = 0;
347       underline.end_offset = target_start;
348       underline.thick = false;
349       composition->underlines.push_back(underline);
350     }
351     if (target_end > target_start) {
352       underline.start_offset = target_start;
353       underline.end_offset = target_end;
354       underline.thick = true;
355       composition->underlines.push_back(underline);
356     }
357     if (target_end < length) {
358       underline.start_offset = target_end;
359       underline.end_offset = length;
360       underline.thick = false;
361       composition->underlines.push_back(underline);
362     }
363   }
364 }
365
366 bool IMM32Manager::GetString(HIMC imm_context,
367                          WPARAM lparam,
368                          int type,
369                          base::string16* result) {
370   if (!(lparam & type))
371     return false;
372   LONG string_size = ::ImmGetCompositionString(imm_context, type, NULL, 0);
373   if (string_size <= 0)
374     return false;
375   DCHECK_EQ(0u, string_size % sizeof(wchar_t));
376   ::ImmGetCompositionString(imm_context, type,
377       WriteInto(result, (string_size / sizeof(wchar_t)) + 1), string_size);
378   return true;
379 }
380
381 bool IMM32Manager::GetResult(
382     HWND window_handle, LPARAM lparam, base::string16* result) {
383   bool ret = false;
384   HIMC imm_context = ::ImmGetContext(window_handle);
385   if (imm_context) {
386     ret = GetString(imm_context, lparam, GCS_RESULTSTR, result);
387     ::ImmReleaseContext(window_handle, imm_context);
388   }
389   return ret;
390 }
391
392 bool IMM32Manager::GetComposition(HWND window_handle, LPARAM lparam,
393                               CompositionText* composition) {
394   bool ret = false;
395   HIMC imm_context = ::ImmGetContext(window_handle);
396   if (imm_context) {
397     // Copy the composition string to the CompositionText object.
398     ret = GetString(imm_context, lparam, GCS_COMPSTR, &composition->text);
399
400     if (ret) {
401       // This is a dirty workaround for facebook. Facebook deletes the
402       // placeholder character (U+3000) used by Traditional-Chinese IMEs at the
403       // beginning of composition text. This prevents WebKit from replacing this
404       // placeholder character with a Traditional-Chinese character, i.e. we
405       // cannot input any characters in a comment box of facebook with
406       // Traditional-Chinese IMEs. As a workaround, we replace U+3000 at the
407       // beginning of composition text with U+FF3F, a placeholder character used
408       // by Japanese IMEs.
409       if (input_language_id_ == MAKELANGID(LANG_CHINESE,
410                                            SUBLANG_CHINESE_TRADITIONAL) &&
411           composition->text[0] == 0x3000) {
412         composition->text[0] = 0xFF3F;
413       }
414
415       // Retrieve the composition underlines and selection range information.
416       GetCompositionInfo(imm_context, lparam, composition);
417
418       // Mark that there is an ongoing composition.
419       is_composing_ = true;
420     }
421
422     ::ImmReleaseContext(window_handle, imm_context);
423   }
424   return ret;
425 }
426
427 void IMM32Manager::DisableIME(HWND window_handle) {
428   // A renderer process have moved its input focus to a password input
429   // when there is an ongoing composition, e.g. a user has clicked a
430   // mouse button and selected a password input while composing a text.
431   // For this case, we have to complete the ongoing composition and
432   // clean up the resources attached to this object BEFORE DISABLING THE IME.
433   CleanupComposition(window_handle);
434   ::ImmAssociateContextEx(window_handle, NULL, 0);
435 }
436
437 void IMM32Manager::CancelIME(HWND window_handle) {
438   if (is_composing_) {
439     HIMC imm_context = ::ImmGetContext(window_handle);
440     if (imm_context) {
441       ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
442       ::ImmReleaseContext(window_handle, imm_context);
443     }
444     ResetComposition(window_handle);
445   }
446 }
447
448 void IMM32Manager::EnableIME(HWND window_handle) {
449   // Load the default IME context.
450   // NOTE(hbono)
451   //   IMM ignores this call if the IME context is loaded. Therefore, we do
452   //   not have to check whether or not the IME context is loaded.
453   ::ImmAssociateContextEx(window_handle, NULL, IACE_DEFAULT);
454 }
455
456 void IMM32Manager::UpdateCaretRect(HWND window_handle,
457                                const gfx::Rect& caret_rect) {
458   // Save the caret position, and Update the position of the IME window.
459   // This update is used for moving an IME window when a renderer process
460   // resize/moves the input caret.
461   if (caret_rect_ != caret_rect) {
462     caret_rect_ = caret_rect;
463     // Move the IME windows.
464     HIMC imm_context = ::ImmGetContext(window_handle);
465     if (imm_context) {
466       MoveImeWindow(window_handle, imm_context);
467       ::ImmReleaseContext(window_handle, imm_context);
468     }
469   }
470 }
471
472 void IMM32Manager::SetUseCompositionWindow(bool use_composition_window) {
473   use_composition_window_ = use_composition_window;
474 }
475
476 std::string IMM32Manager::GetInputLanguageName() const {
477   const LCID locale_id = MAKELCID(input_language_id_, SORT_DEFAULT);
478   // max size for LOCALE_SISO639LANGNAME and LOCALE_SISO3166CTRYNAME is 9.
479   wchar_t buffer[9];
480
481   // Get language id.
482   int length = ::GetLocaleInfo(locale_id, LOCALE_SISO639LANGNAME, &buffer[0],
483                                arraysize(buffer));
484   if (length <= 1)
485     return std::string();
486
487   std::string language;
488   base::WideToUTF8(buffer, length - 1, &language);
489   if (SUBLANGID(input_language_id_) == SUBLANG_NEUTRAL)
490     return language;
491
492   // Get region id.
493   length = ::GetLocaleInfo(locale_id, LOCALE_SISO3166CTRYNAME, &buffer[0],
494                            arraysize(buffer));
495   if (length <= 1)
496     return language;
497
498   std::string region;
499   base::WideToUTF8(buffer, length - 1, &region);
500   return language.append(1, '-').append(region);
501 }
502
503 void IMM32Manager::SetTextInputMode(HWND window_handle,
504                                     TextInputMode input_mode) {
505   if (input_mode == ui::TEXT_INPUT_MODE_DEFAULT)
506     return;
507
508   const HIMC imm_context = ::ImmGetContext(window_handle);
509   if (!imm_context)
510     return;
511
512   DWORD conversion_mode = 0;
513   DWORD sentence_mode = 0;
514   if (::ImmGetConversionStatus(imm_context, &conversion_mode, &sentence_mode)
515       == FALSE) {
516     return;
517   }
518
519   BOOL open = FALSE;
520   ConvertInputModeToImmFlags(input_mode, conversion_mode, &open,
521                              &conversion_mode),
522
523   ::ImmSetOpenStatus(imm_context, open);
524   if (open)
525     ::ImmSetConversionStatus(imm_context, conversion_mode, sentence_mode);
526   ::ImmReleaseContext(window_handle, imm_context);
527 }
528
529 // static
530 bool IMM32Manager::IsRTLKeyboardLayoutInstalled() {
531   static enum {
532     RTL_KEYBOARD_LAYOUT_NOT_INITIALIZED,
533     RTL_KEYBOARD_LAYOUT_INSTALLED,
534     RTL_KEYBOARD_LAYOUT_NOT_INSTALLED,
535     RTL_KEYBOARD_LAYOUT_ERROR,
536   } layout = RTL_KEYBOARD_LAYOUT_NOT_INITIALIZED;
537
538   // Cache the result value.
539   if (layout != RTL_KEYBOARD_LAYOUT_NOT_INITIALIZED)
540     return layout == RTL_KEYBOARD_LAYOUT_INSTALLED;
541
542   // Retrieve the number of layouts installed in this system.
543   int size = GetKeyboardLayoutList(0, NULL);
544   if (size <= 0) {
545     layout = RTL_KEYBOARD_LAYOUT_ERROR;
546     return false;
547   }
548
549   // Retrieve the keyboard layouts in an array and check if there is an RTL
550   // layout in it.
551   scoped_ptr<HKL[]> layouts(new HKL[size]);
552   ::GetKeyboardLayoutList(size, layouts.get());
553   for (int i = 0; i < size; ++i) {
554     if (IsRTLPrimaryLangID(PRIMARYLANGID(layouts[i]))) {
555       layout = RTL_KEYBOARD_LAYOUT_INSTALLED;
556       return true;
557     }
558   }
559
560   layout = RTL_KEYBOARD_LAYOUT_NOT_INSTALLED;
561   return false;
562 }
563
564 bool IMM32Manager::IsCtrlShiftPressed(base::i18n::TextDirection* direction) {
565   uint8_t keystate[256];
566   if (!::GetKeyboardState(&keystate[0]))
567     return false;
568
569   // To check if a user is pressing only a control key and a right-shift key
570   // (or a left-shift key), we use the steps below:
571   // 1. Check if a user is pressing a control key and a right-shift key (or
572   //    a left-shift key).
573   // 2. If the condition 1 is true, we should check if there are any other
574   //    keys pressed at the same time.
575   //    To ignore the keys checked in 1, we set their status to 0 before
576   //    checking the key status.
577   const int kKeyDownMask = 0x80;
578   if ((keystate[VK_CONTROL] & kKeyDownMask) == 0)
579     return false;
580
581   if (keystate[VK_RSHIFT] & kKeyDownMask) {
582     keystate[VK_RSHIFT] = 0;
583     *direction = base::i18n::RIGHT_TO_LEFT;
584   } else if (keystate[VK_LSHIFT] & kKeyDownMask) {
585     keystate[VK_LSHIFT] = 0;
586     *direction = base::i18n::LEFT_TO_RIGHT;
587   } else {
588     return false;
589   }
590
591   // Scan the key status to find pressed keys. We should abandon changing the
592   // text direction when there are other pressed keys.
593   // This code is executed only when a user is pressing a control key and a
594   // right-shift key (or a left-shift key), i.e. we should ignore the status of
595   // the keys: VK_SHIFT, VK_CONTROL, VK_RCONTROL, and VK_LCONTROL.
596   // So, we reset their status to 0 and ignore them.
597   keystate[VK_SHIFT] = 0;
598   keystate[VK_CONTROL] = 0;
599   keystate[VK_RCONTROL] = 0;
600   keystate[VK_LCONTROL] = 0;
601   // Oddly, pressing F10 in another application seemingly breaks all subsequent
602   // calls to GetKeyboardState regarding the state of the F22 key. Perhaps this
603   // defect is limited to my keyboard driver, but ignoring F22 should be okay.
604   keystate[VK_F22] = 0;
605   for (int i = 0; i <= VK_PACKET; ++i) {
606     if (keystate[i] & kKeyDownMask)
607       return false;
608   }
609   return true;
610 }
611
612 void IMM32Manager::ConvertInputModeToImmFlags(TextInputMode input_mode,
613                                               DWORD initial_conversion_mode,
614                                               BOOL* open,
615                                               DWORD* new_conversion_mode) {
616   *open = TRUE;
617   *new_conversion_mode = initial_conversion_mode;
618   switch (input_mode) {
619     case ui::TEXT_INPUT_MODE_FULL_WIDTH_LATIN:
620       *new_conversion_mode |= IME_CMODE_FULLSHAPE;
621       *new_conversion_mode &= ~(IME_CMODE_NATIVE
622                               | IME_CMODE_KATAKANA);
623       break;
624     case ui::TEXT_INPUT_MODE_KANA:
625       *new_conversion_mode |= (IME_CMODE_NATIVE
626                              | IME_CMODE_FULLSHAPE);
627       *new_conversion_mode &= ~IME_CMODE_KATAKANA;
628       break;
629     case ui::TEXT_INPUT_MODE_KATAKANA:
630       *new_conversion_mode |= (IME_CMODE_NATIVE
631                              | IME_CMODE_KATAKANA
632                              | IME_CMODE_FULLSHAPE);
633       break;
634     default:
635       *open = FALSE;
636       break;
637   }
638 }
639
640 }  // namespace ui