-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path890764e83f63f1412ea88c28540d8730bff8d1a2.diff
1478 lines (1467 loc) · 57.7 KB
/
890764e83f63f1412ea88c28540d8730bff8d1a2.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/ui/base/BUILD.gn b/ui/base/BUILD.gn
index 3452178f0db87..3ab9b27c88aed 100644
--- a/ui/base/BUILD.gn
+++ b/ui/base/BUILD.gn
@@ -1162,6 +1162,7 @@ test("ui_base_unittests") {
]
if (is_win) {
sources += [
+ "ime/win/imm32_manager_unittest.cc",
"ime/win/on_screen_keyboard_display_manager_unittest.cc",
"ime/win/tsf_input_scope_unittest.cc",
"ime/win/tsf_text_store_unittest.cc",
diff --git a/ui/base/ime/init/input_method_factory.cc b/ui/base/ime/init/input_method_factory.cc
index c99fd18826153..5d4a41a4d64c2 100644
--- a/ui/base/ime/init/input_method_factory.cc
+++ b/ui/base/ime/init/input_method_factory.cc
@@ -13,6 +13,7 @@
#include "ui/gfx/switches.h"
#if BUILDFLAG(IS_WIN)
+#include "ui/base/ime/win/input_method_win_imm32.h"
#include "ui/base/ime/win/input_method_win_tsf.h"
#elif BUILDFLAG(IS_APPLE)
#include "ui/base/ime/mac/input_method_mac.h"
@@ -53,7 +54,12 @@ std::unique_ptr<InputMethod> CreateInputMethod(
return base::WrapUnique(new MockInputMethod(ime_key_event_dispatcher));
#if BUILDFLAG(IS_WIN)
- return std::make_unique<InputMethodWinTSF>(ime_key_event_dispatcher, widget);
+ if (base::FeatureList::IsEnabled(features::kTSFImeSupport)) {
+ return std::make_unique<InputMethodWinTSF>(ime_key_event_dispatcher,
+ widget);
+ }
+ return std::make_unique<InputMethodWinImm32>(ime_key_event_dispatcher,
+ widget);
#elif BUILDFLAG(IS_APPLE)
return std::make_unique<InputMethodMac>(ime_key_event_dispatcher);
#elif BUILDFLAG(IS_OZONE)
diff --git a/ui/base/ime/win/BUILD.gn b/ui/base/ime/win/BUILD.gn
index 61d1e9b662430..1a2a40db4dae5 100644
--- a/ui/base/ime/win/BUILD.gn
+++ b/ui/base/ime/win/BUILD.gn
@@ -7,8 +7,12 @@ assert(is_win)
component("win") {
output_name = "ui_base_ime_win"
sources = [
+ "imm32_manager.cc",
+ "imm32_manager.h",
"input_method_win_base.cc",
"input_method_win_base.h",
+ "input_method_win_imm32.cc",
+ "input_method_win_imm32.h",
"input_method_win_tsf.cc",
"input_method_win_tsf.h",
"mock_tsf_bridge.cc",
@@ -37,4 +41,8 @@ component("win") {
"//ui/base",
"//ui/display",
]
+
+ libs = [ "imm32.lib" ]
+
+ ldflags = [ "/DELAYLOAD:imm32.dll" ]
}
diff --git a/ui/base/ime/win/imm32_manager.cc b/ui/base/ime/win/imm32_manager.cc
new file mode 100644
index 0000000000000..af0e4318d606e
--- /dev/null
+++ b/ui/base/ime/win/imm32_manager.cc
@@ -0,0 +1,487 @@
+// Copyright 2013 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/base/ime/win/imm32_manager.h"
+
+#include <stdint.h>
+
+#include <memory>
+#include <string>
+
+#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "third_party/skia/include/core/SkColor.h"
+#include "ui/base/ime/composition_text.h"
+
+///////////////////////////////////////////////////////////////////////////////
+// IMM32Manager
+
+namespace {
+
+// Determines whether or not the given attribute represents a target
+// (a.k.a. a selection).
+bool IsTargetAttribute(char attribute) {
+ return (attribute == ATTR_TARGET_CONVERTED ||
+ attribute == ATTR_TARGET_NOTCONVERTED);
+}
+
+// Helper function for IMM32Manager::GetCompositionInfo() method, to get the
+// target range that's selected by the user in the current composition string.
+void GetCompositionTargetRange(HIMC imm_context, int* target_start,
+ int* target_end) {
+ int attribute_size = ::ImmGetCompositionString(imm_context, GCS_COMPATTR,
+ NULL, 0);
+ if (attribute_size > 0) {
+ int start = 0;
+ int end = 0;
+ std::unique_ptr<char[]> attribute_data(new char[attribute_size]);
+ if (attribute_data.get()) {
+ ::ImmGetCompositionString(imm_context, GCS_COMPATTR,
+ attribute_data.get(), attribute_size);
+ for (start = 0; start < attribute_size; ++start) {
+ if (IsTargetAttribute(attribute_data[start]))
+ break;
+ }
+ for (end = start; end < attribute_size; ++end) {
+ if (!IsTargetAttribute(attribute_data[end]))
+ break;
+ }
+ }
+ *target_start = start;
+ *target_end = end;
+ }
+}
+
+// Helper function for IMM32Manager::GetCompositionInfo() method, to get
+// underlines information of the current composition string.
+void GetImeTextSpans(HIMC imm_context,
+ int target_start,
+ int target_end,
+ ui::ImeTextSpans* ime_text_spans) {
+ int clause_size = ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE,
+ NULL, 0);
+ int clause_length = clause_size / sizeof(uint32_t);
+ if (clause_length) {
+ std::unique_ptr<uint32_t[]> clause_data(new uint32_t[clause_length]);
+ if (clause_data.get()) {
+ ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE,
+ clause_data.get(), clause_size);
+ for (int i = 0; i < clause_length - 1; ++i) {
+ ui::ImeTextSpan ime_text_span;
+ ime_text_span.start_offset = clause_data[i];
+ ime_text_span.end_offset = clause_data[i + 1];
+ ime_text_span.thickness = ui::ImeTextSpan::Thickness::kThin;
+ ime_text_span.background_color = SK_ColorTRANSPARENT;
+
+ // Use thick underline for the target clause.
+ if (ime_text_span.start_offset >= static_cast<uint32_t>(target_start) &&
+ ime_text_span.end_offset <= static_cast<uint32_t>(target_end)) {
+ ime_text_span.thickness = ui::ImeTextSpan::Thickness::kThick;
+ }
+ ime_text_spans->push_back(ime_text_span);
+ }
+ }
+ }
+}
+
+} // namespace
+
+namespace ui {
+
+IMM32Manager::IMM32Manager()
+ : is_composing_(false),
+ input_language_id_(LANG_USER_DEFAULT),
+ system_caret_(false),
+ caret_rect_(-1, -1, 0, 0),
+ use_composition_window_(false) {
+}
+
+IMM32Manager::~IMM32Manager() {
+}
+
+void IMM32Manager::SetInputLanguage() {
+ // Retrieve the current input language from the system's keyboard layout.
+ // Using GetKeyboardLayoutName instead of GetKeyboardLayout, because
+ // the language from GetKeyboardLayout is the language under where the
+ // keyboard layout is installed. And the language from GetKeyboardLayoutName
+ // indicates the language of the keyboard layout itself.
+ // See crbug.com/344834.
+ WCHAR keyboard_layout[KL_NAMELENGTH];
+ if (::GetKeyboardLayoutNameW(keyboard_layout)) {
+ input_language_id_ =
+ static_cast<LANGID>(
+ wcstol(&keyboard_layout[KL_NAMELENGTH >> 1], nullptr, 16));
+ } else {
+ input_language_id_ = 0x0409; // Fallback to en-US.
+ }
+}
+
+void IMM32Manager::CreateImeWindow(HWND window_handle) {
+ // When a user disables TSF (Text Service Framework) and CUAS (Cicero
+ // Unaware Application Support), Chinese IMEs somehow ignore function calls
+ // to ::ImmSetCandidateWindow(), i.e. they do not move their candidate
+ // window to the position given as its parameters, and use the position
+ // of the current system caret instead, i.e. it uses ::GetCaretPos() to
+ // retrieve the position of their IME candidate window.
+ // Therefore, we create a temporary system caret for Chinese IMEs and use
+ // it during this input context.
+ // Since some third-party Japanese IME also uses ::GetCaretPos() to determine
+ // their window position, we also create a caret for Japanese IMEs.
+ if (PRIMARYLANGID(input_language_id_) == LANG_CHINESE ||
+ PRIMARYLANGID(input_language_id_) == LANG_JAPANESE) {
+ if (!system_caret_) {
+ if (::CreateCaret(window_handle, NULL, 1, 1)) {
+ system_caret_ = true;
+ }
+ }
+ }
+ // Restore the positions of the IME windows.
+ UpdateImeWindow(window_handle);
+}
+
+LRESULT IMM32Manager::SetImeWindowStyle(HWND window_handle, UINT message,
+ WPARAM wparam, LPARAM lparam,
+ BOOL* handled) {
+ // To prevent the IMM (Input Method Manager) from displaying the IME
+ // composition window, Update the styles of the IME windows and EXPLICITLY
+ // call ::DefWindowProc() here.
+ // NOTE(hbono): We can NEVER let WTL call ::DefWindowProc() when we update
+ // the styles of IME windows because the 'lparam' variable is a local one
+ // and all its updates disappear in returning from this function, i.e. WTL
+ // does not call ::DefWindowProc() with our updated 'lparam' value but call
+ // the function with its original value and over-writes our window styles.
+ *handled = TRUE;
+ lparam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
+ return ::DefWindowProc(window_handle, message, wparam, lparam);
+}
+
+void IMM32Manager::DestroyImeWindow(HWND window_handle) {
+ // Destroy the system caret if we have created for this IME input context.
+ if (system_caret_) {
+ ::DestroyCaret();
+ system_caret_ = false;
+ }
+}
+
+void IMM32Manager::MoveImeWindow(HWND window_handle, HIMC imm_context) {
+ // Does nothing when the target window has no input focus. This is important
+ // because the renderer may issue SelectionBoundsChanged event even when it
+ // has no input focus. (e.g. the page update caused by incremental search.)
+ // So this event should be ignored when the |window_handle| no longer has the
+ // input focus.
+ if (GetFocus() != window_handle)
+ return;
+
+ int x = caret_rect_.x();
+ int y = caret_rect_.y();
+
+ const int kCaretMargin = 1;
+ if (!use_composition_window_ &&
+ PRIMARYLANGID(input_language_id_) == LANG_CHINESE) {
+ // As written in a comment in IMM32Manager::CreateImeWindow(),
+ // Chinese IMEs ignore function calls to ::ImmSetCandidateWindow()
+ // when a user disables TSF (Text Service Framework) and CUAS (Cicero
+ // Unaware Application Support).
+ // On the other hand, when a user enables TSF and CUAS, Chinese IMEs
+ // ignore the position of the current system caret and uses the
+ // parameters given to ::ImmSetCandidateWindow() with its 'dwStyle'
+ // parameter CFS_CANDIDATEPOS.
+ // Therefore, we do not only call ::ImmSetCandidateWindow() but also
+ // set the positions of the temporary system caret if it exists.
+ CANDIDATEFORM candidate_position = {0, CFS_CANDIDATEPOS, {x, y},
+ {0, 0, 0, 0}};
+ ::ImmSetCandidateWindow(imm_context, &candidate_position);
+ }
+ if (system_caret_) {
+ switch (PRIMARYLANGID(input_language_id_)) {
+ case LANG_JAPANESE:
+ ::SetCaretPos(x, y + caret_rect_.height());
+ break;
+ default:
+ ::SetCaretPos(x, y);
+ break;
+ }
+ }
+ if (use_composition_window_) {
+ // Moves the composition text window.
+ COMPOSITIONFORM cf = {CFS_POINT, {x, y}};
+ ::ImmSetCompositionWindow(imm_context, &cf);
+ // Don't need to set the position of candidate window.
+ return;
+ }
+
+ if (PRIMARYLANGID(input_language_id_) == LANG_KOREAN) {
+ // Chinese IMEs and Japanese IMEs require the upper-left corner of
+ // the caret to move the position of their candidate windows.
+ // On the other hand, Korean IMEs require the lower-left corner of the
+ // caret to move their candidate windows.
+ y += kCaretMargin;
+ }
+ // Japanese IMEs and Korean IMEs also use the rectangle given to
+ // ::ImmSetCandidateWindow() with its 'dwStyle' parameter CFS_EXCLUDE
+ // to move their candidate windows when a user disables TSF and CUAS.
+ // Therefore, we also set this parameter here.
+ CANDIDATEFORM exclude_rectangle = {0, CFS_EXCLUDE, {x, y},
+ {x, y, x + caret_rect_.width(), y + caret_rect_.height()}};
+ ::ImmSetCandidateWindow(imm_context, &exclude_rectangle);
+}
+
+void IMM32Manager::UpdateImeWindow(HWND window_handle) {
+ // Just move the IME window attached to the given window.
+ if (caret_rect_.x() >= 0 && caret_rect_.y() >= 0) {
+ HIMC imm_context = ::ImmGetContext(window_handle);
+ if (imm_context) {
+ MoveImeWindow(window_handle, imm_context);
+ ::ImmReleaseContext(window_handle, imm_context);
+ }
+ }
+}
+
+void IMM32Manager::CleanupComposition(HWND window_handle) {
+ // Notify the IMM attached to the given window to complete the ongoing
+ // composition, (this case happens when the given window is de-activated
+ // while composing a text and re-activated), and reset the omposition status.
+ if (is_composing_) {
+ HIMC imm_context = ::ImmGetContext(window_handle);
+ if (imm_context) {
+ ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
+ ::ImmReleaseContext(window_handle, imm_context);
+ }
+ ResetComposition(window_handle);
+ }
+}
+
+void IMM32Manager::ResetComposition(HWND window_handle) {
+ // Currently, just reset the composition status.
+ is_composing_ = false;
+}
+
+void IMM32Manager::CompleteComposition(HWND window_handle, HIMC imm_context) {
+ // We have to confirm there is an ongoing composition before completing it.
+ // This is for preventing some IMEs from getting confused while completing an
+ // ongoing composition even if they do not have any ongoing compositions.)
+ if (is_composing_) {
+ ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
+ ResetComposition(window_handle);
+ }
+}
+
+void IMM32Manager::GetCompositionInfo(HIMC imm_context,
+ LPARAM lparam,
+ CompositionText* composition) {
+ // We only care about GCS_COMPATTR, GCS_COMPCLAUSE and GCS_CURSORPOS, and
+ // convert them into composition underlines and selection range respectively.
+ composition->ime_text_spans.clear();
+
+ int length = static_cast<int>(composition->text.length());
+
+ // Find out the range selected by the user.
+ int target_start = length;
+ int target_end = length;
+ if (lparam & GCS_COMPATTR)
+ GetCompositionTargetRange(imm_context, &target_start, &target_end);
+
+ // Retrieve the selection range information. If CS_NOMOVECARET is specified,
+ // that means the cursor should not be moved, then we just place the caret at
+ // the beginning of the composition string. Otherwise we should honour the
+ // GCS_CURSORPOS value if it's available.
+ // TODO(suzhe): due to a bug of webkit, we currently can't use selection range
+ // with composition string. See: https://bugs.webkit.org/show_bug.cgi?id=40805
+ if (!(lparam & CS_NOMOVECARET) && (lparam & GCS_CURSORPOS)) {
+ // IMM32 does not support non-zero-width selection in a composition. So
+ // always use the caret position as selection range.
+ int cursor = ::ImmGetCompositionString(imm_context, GCS_CURSORPOS, NULL, 0);
+ composition->selection = gfx::Range(cursor);
+ } else {
+ composition->selection = gfx::Range(0);
+ }
+
+ // Retrieve the clause segmentations and convert them to ime_text_spans.
+ if (lparam & GCS_COMPCLAUSE) {
+ GetImeTextSpans(imm_context, target_start, target_end,
+ &composition->ime_text_spans);
+ }
+
+ // Set default composition underlines in case there is no clause information.
+ if (!composition->ime_text_spans.empty())
+ return;
+
+ ImeTextSpan ime_text_span;
+ ime_text_span.underline_color = SK_ColorTRANSPARENT;
+ ime_text_span.background_color = SK_ColorTRANSPARENT;
+ if (target_start > 0) {
+ ime_text_span.start_offset = 0U;
+ ime_text_span.end_offset = static_cast<uint32_t>(target_start);
+ ime_text_span.thickness = ui::ImeTextSpan::Thickness::kThin;
+ composition->ime_text_spans.push_back(ime_text_span);
+ }
+ if (target_end > target_start) {
+ ime_text_span.start_offset = static_cast<uint32_t>(target_start);
+ ime_text_span.end_offset = static_cast<uint32_t>(target_end);
+ ime_text_span.thickness = ui::ImeTextSpan::Thickness::kThick;
+ composition->ime_text_spans.push_back(ime_text_span);
+ }
+ if (target_end < length) {
+ ime_text_span.start_offset = static_cast<uint32_t>(target_end);
+ ime_text_span.end_offset = static_cast<uint32_t>(length);
+ ime_text_span.thickness = ui::ImeTextSpan::Thickness::kThin;
+ composition->ime_text_spans.push_back(ime_text_span);
+ }
+}
+
+bool IMM32Manager::GetString(HIMC imm_context,
+ WPARAM lparam,
+ int type,
+ std::u16string* result) {
+ if (!(lparam & type))
+ return false;
+ LONG string_size = ::ImmGetCompositionString(imm_context, type, NULL, 0);
+ if (string_size <= 0)
+ return false;
+ DCHECK_EQ(0u, string_size % sizeof(wchar_t));
+ ::ImmGetCompositionString(imm_context, type,
+ base::as_writable_wcstr(base::WriteInto(
+ result, (string_size / sizeof(wchar_t)) + 1)),
+ string_size);
+ return true;
+}
+
+bool IMM32Manager::GetResult(HWND window_handle,
+ LPARAM lparam,
+ std::u16string* result) {
+ bool ret = false;
+ HIMC imm_context = ::ImmGetContext(window_handle);
+ if (imm_context) {
+ ret = GetString(imm_context, lparam, GCS_RESULTSTR, result);
+ ::ImmReleaseContext(window_handle, imm_context);
+ }
+ return ret;
+}
+
+bool IMM32Manager::GetComposition(HWND window_handle, LPARAM lparam,
+ CompositionText* composition) {
+ bool ret = false;
+ HIMC imm_context = ::ImmGetContext(window_handle);
+ if (imm_context) {
+ // Copy the composition string to the CompositionText object.
+ ret = GetString(imm_context, lparam, GCS_COMPSTR, &composition->text);
+
+ if (ret) {
+ // This is a dirty workaround for facebook. Facebook deletes the
+ // placeholder character (U+3000) used by Traditional-Chinese IMEs at the
+ // beginning of composition text. This prevents WebKit from replacing this
+ // placeholder character with a Traditional-Chinese character, i.e. we
+ // cannot input any characters in a comment box of facebook with
+ // Traditional-Chinese IMEs. As a workaround, we replace U+3000 at the
+ // beginning of composition text with U+FF3F, a placeholder character used
+ // by Japanese IMEs.
+ if (input_language_id_ == MAKELANGID(LANG_CHINESE,
+ SUBLANG_CHINESE_TRADITIONAL) &&
+ composition->text[0] == 0x3000) {
+ composition->text[0] = 0xFF3F;
+ }
+
+ // Retrieve the IME text spans and selection range information.
+ GetCompositionInfo(imm_context, lparam, composition);
+
+ // Mark that there is an ongoing composition.
+ is_composing_ = true;
+ }
+
+ ::ImmReleaseContext(window_handle, imm_context);
+ }
+ return ret;
+}
+
+void IMM32Manager::DisableIME(HWND window_handle) {
+ // A renderer process have moved its input focus to a password input
+ // when there is an ongoing composition, e.g. a user has clicked a
+ // mouse button and selected a password input while composing a text.
+ // For this case, we have to complete the ongoing composition and
+ // clean up the resources attached to this object BEFORE DISABLING THE IME.
+ CleanupComposition(window_handle);
+ ::ImmAssociateContextEx(window_handle, NULL, 0);
+}
+
+void IMM32Manager::CancelIME(HWND window_handle) {
+ if (is_composing_) {
+ HIMC imm_context = ::ImmGetContext(window_handle);
+ if (imm_context) {
+ ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
+ ::ImmReleaseContext(window_handle, imm_context);
+ }
+ ResetComposition(window_handle);
+ }
+}
+
+void IMM32Manager::EnableIME(HWND window_handle) {
+ // Load the default IME context.
+ // NOTE(hbono)
+ // IMM ignores this call if the IME context is loaded. Therefore, we do
+ // not have to check whether or not the IME context is loaded.
+ ::ImmAssociateContextEx(window_handle, NULL, IACE_DEFAULT);
+}
+
+void IMM32Manager::UpdateCaretRect(HWND window_handle,
+ const gfx::Rect& caret_rect) {
+ // Save the caret position, and Update the position of the IME window.
+ // This update is used for moving an IME window when a renderer process
+ // resize/moves the input caret.
+ if (caret_rect_ != caret_rect) {
+ caret_rect_ = caret_rect;
+ // Move the IME windows.
+ HIMC imm_context = ::ImmGetContext(window_handle);
+ if (imm_context) {
+ MoveImeWindow(window_handle, imm_context);
+ ::ImmReleaseContext(window_handle, imm_context);
+ }
+ }
+}
+
+void IMM32Manager::SetUseCompositionWindow(bool use_composition_window) {
+ use_composition_window_ = use_composition_window;
+}
+
+bool IMM32Manager::IsInputLanguageCJK() const {
+ LANGID lang = PRIMARYLANGID(input_language_id_);
+ return lang == LANG_CHINESE || lang == LANG_JAPANESE ||
+ lang == LANG_KOREAN;
+}
+
+void IMM32Manager::SetTextInputMode(HWND window_handle,
+ TextInputMode input_mode) {
+ if (input_mode == ui::TEXT_INPUT_MODE_DEFAULT)
+ return;
+
+ const HIMC imm_context = ::ImmGetContext(window_handle);
+ if (!imm_context)
+ return;
+
+ DWORD conversion_mode = 0;
+ DWORD sentence_mode = 0;
+ if (::ImmGetConversionStatus(imm_context, &conversion_mode, &sentence_mode)
+ == FALSE) {
+ return;
+ }
+
+ BOOL open = FALSE;
+ ConvertInputModeToImmFlags(input_mode, conversion_mode, &open,
+ &conversion_mode),
+
+ ::ImmSetOpenStatus(imm_context, open);
+ if (open)
+ ::ImmSetConversionStatus(imm_context, conversion_mode, sentence_mode);
+ ::ImmReleaseContext(window_handle, imm_context);
+}
+
+// static
+void IMM32Manager::ConvertInputModeToImmFlags(TextInputMode input_mode,
+ DWORD initial_conversion_mode,
+ BOOL* open,
+ DWORD* new_conversion_mode) {
+ *open = FALSE;
+ *new_conversion_mode = initial_conversion_mode;
+}
+
+} // namespace ui
diff --git a/ui/base/ime/win/imm32_manager.h b/ui/base/ime/win/imm32_manager.h
new file mode 100644
index 0000000000000..e8271a992d4b1
--- /dev/null
+++ b/ui/base/ime/win/imm32_manager.h
@@ -0,0 +1,308 @@
+// Copyright 2013 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef UI_BASE_IME_WIN_IMM32_MANAGER_H_
+#define UI_BASE_IME_WIN_IMM32_MANAGER_H_
+
+#include <windows.h>
+
+#include <string>
+#include <vector>
+
+#include "base/component_export.h"
+#include "base/i18n/rtl.h"
+#include "ui/base/ime/text_input_mode.h"
+#include "ui/gfx/geometry/rect.h"
+
+namespace ui {
+
+struct CompositionText;
+
+// This header file defines a struct and a class used for encapsulating IMM32
+// APIs, controls IMEs attached to a window, and enables the 'on-the-spot'
+// input without deep knowledge about the APIs, i.e. knowledge about the
+// language-specific and IME-specific behaviors.
+// The following items enumerates the simplest steps for an (window)
+// application to control its IMEs with the struct and the class defined
+// this file.
+// 1. Add an instance of the IMM32Manager class to its window class.
+// (The IMM32Manager class needs a window handle.)
+// 2. Add messages handlers listed in the following subsections, follow the
+// instructions written in each subsection, and use the IMM32Manager class.
+// 2.1. WM_IME_SETCONTEXT (0x0281)
+// Call the functions listed below:
+// - IMM32Manager::CreateImeWindow();
+// - IMM32Manager::CleanupComposition(), and;
+// - IMM32Manager::SetImeWindowStyle().
+// An application MUST prevent from calling ::DefWindowProc().
+// 2.2. WM_IME_STARTCOMPOSITION (0x010D)
+// Call the functions listed below:
+// - IMM32Manager::CreateImeWindow(), and;
+// - IMM32Manager::ResetComposition().
+// An application MUST prevent from calling ::DefWindowProc().
+// 2.3. WM_IME_COMPOSITION (0x010F)
+// Call the functions listed below:
+// - IMM32Manager::UpdateImeWindow();
+// - IMM32Manager::GetResult();
+// - IMM32Manager::GetComposition(), and;
+// - IMM32Manager::ResetComposition() (optional).
+// An application MUST prevent from calling ::DefWindowProc().
+// 2.4. WM_IME_ENDCOMPOSITION (0x010E)
+// Call the functions listed below:
+// - IMM32Manager::ResetComposition(), and;
+// - IMM32Manager::DestroyImeWindow().
+// An application CAN call ::DefWindowProc().
+// 2.5. WM_INPUTLANGCHANGE (0x0051)
+// Call the functions listed below:
+// - IMM32Manager::SetInputLanguage().
+// An application CAN call ::DefWindowProc().
+
+// This class controls the IMM (Input Method Manager) through IMM32 APIs and
+// enables it to retrieve the string being controled by the IMM. (I wrote
+// a note to describe the reason why I do not use 'IME' but 'IMM' below.)
+// NOTE(hbono):
+// Fortunately or unfortunately, TSF (Text Service Framework) and
+// CUAS (Cicero Unaware Application Support) allows IMM32 APIs for
+// retrieving not only the inputs from IMEs (Input Method Editors), used
+// only for inputting East-Asian language texts, but also the ones from
+// tablets (on Windows XP Tablet PC Edition and Windows Vista), voice
+// recognizers (e.g. ViaVoice and Microsoft Office), etc.
+// We can disable TSF and CUAS in Windows XP Tablet PC Edition. On the other
+// hand, we can NEVER disable either TSF or CUAS in Windows Vista, i.e.
+// THIS CLASS IS NOT ONLY USED ON THE INPUT CONTEXTS OF EAST-ASIAN
+// LANGUAGES BUT ALSO USED ON THE INPUT CONTEXTS OF ALL LANGUAGES.
+class COMPONENT_EXPORT(UI_BASE_IME_WIN) IMM32Manager {
+ public:
+ IMM32Manager();
+
+ IMM32Manager(const IMM32Manager&) = delete;
+ IMM32Manager& operator=(const IMM32Manager&) = delete;
+
+ virtual ~IMM32Manager();
+
+ // Retrieves whether or not there is an ongoing composition.
+ bool is_composing() const { return is_composing_; }
+
+ // Retrieves the input language from Windows and update it.
+ void SetInputLanguage();
+
+ // Creates the IME windows, and allocate required resources for them.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ void CreateImeWindow(HWND window_handle);
+
+ // Updates the style of the IME windows.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ // * message [in] (UINT)
+ // * wparam [in] (WPARAM)
+ // * lparam [in] (LPARAM)
+ // Represent the windows message of the caller.
+ // These parameters are used for verifying if this function is called
+ // in a handler function for WM_IME_SETCONTEXT messages because this
+ // function uses ::DefWindowProc() to update the style.
+ // A caller just has to pass the input parameters for the handler
+ // function without modifications.
+ // * handled [out] (BOOL*)
+ // Returns ::DefWindowProc() is really called in this function.
+ // PLEASE DO NOT CALL ::DefWindowProc() IF THIS VALUE IS TRUE!
+ // All the window styles set in this function are over-written when
+ // calling ::DefWindowProc() after returning this function.
+ // Returns the value returned by DefWindowProc.
+ LRESULT SetImeWindowStyle(HWND window_handle, UINT message,
+ WPARAM wparam, LPARAM lparam, BOOL* handled);
+
+ // Destroys the IME windows and all the resources attached to them.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ void DestroyImeWindow(HWND window_handle);
+
+ // Updates the position of the IME windows.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ void UpdateImeWindow(HWND window_handle);
+
+ // Cleans up the all resources attached to the given IMM32Manager object, and
+ // reset its composition status.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ void CleanupComposition(HWND window_handle);
+
+ // Resets the composition status.
+ // Cancel the ongoing composition if it exists.
+ // NOTE(hbono): This method does not release the allocated resources.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ void ResetComposition(HWND window_handle);
+
+ // Retrieves a composition result of the ongoing composition if it exists.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ // * lparam [in] (LPARAM)
+ // Specifies the updated members of the ongoing composition, and must be
+ // the same parameter of a WM_IME_COMPOSITION message handler.
+ // This parameter is used for checking if the ongoing composition has
+ // its result string,
+ // * result [out] (std::u16string)
+ // Represents the object contains the composition result.
+ // Return values
+ // * true
+ // The ongoing composition has a composition result.
+ // * false
+ // The ongoing composition does not have composition results.
+ // Remarks
+ // This function is designed for being called from WM_IME_COMPOSITION
+ // message handlers.
+ bool GetResult(HWND window_handle, LPARAM lparam, std::u16string* result);
+
+ // Retrieves the current composition status of the ongoing composition.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ // * lparam [in] (LPARAM)
+ // Specifies the updated members of the ongoing composition, and must be
+ // the same parameter of a WM_IME_COMPOSITION message handler.
+ // This parameter is used for checking if the ongoing composition has
+ // its result string,
+ // * composition [out] (Composition)
+ // Represents the struct contains the composition status.
+ // Return values
+ // * true
+ // The status of the ongoing composition is updated.
+ // * false
+ // The status of the ongoing composition is not updated.
+ // Remarks
+ // This function is designed for being called from WM_IME_COMPOSITION
+ // message handlers.
+ bool GetComposition(HWND window_handle, LPARAM lparam,
+ CompositionText* composition);
+
+ // Enables the IME attached to the given window, i.e. allows user-input
+ // events to be dispatched to the IME.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ // * complete [in] (bool)
+ // Represents whether or not to complete the ongoing composition.
+ // + true
+ // After finishing the ongoing composition and close its IME windows,
+ // start another composition and display its IME windows to the given
+ // position.
+ // + false
+ // Just move the IME windows of the ongoing composition to the given
+ // position without finishing it.
+ void EnableIME(HWND window_handle);
+
+ // Disables the IME attached to the given window, i.e. prohibits any
+ // user-input events from being dispatched to the IME.
+ // In Chrome, this function is used when:
+ // * a renreder process sets its input focus to a password input.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ void DisableIME(HWND window_handle);
+
+ // Cancels an ongoing composition of the IME attached to the given window.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ void CancelIME(HWND window_handle);
+
+ // Updates the caret position of the given window.
+ // Parameters
+ // * window_handle [in] (HWND)
+ // Represents the window handle of the caller.
+ // * caret_rect [in] (const gfx::Rect&)
+ // Represent the rectangle of the input caret.
+ // This rectangle is used for controlling the positions of IME windows.
+ void UpdateCaretRect(HWND window_handle, const gfx::Rect& caret_rect);
+
+ // Updates the setting whether we want IME to render composition text.
+ void SetUseCompositionWindow(bool use_composition_window);
+
+ // Returns the current input language id.
+ LANGID input_language_id() const { return input_language_id_; }
+
+ // Returns whether the system's input language is CJK.
+ bool IsInputLanguageCJK() const;
+
+ // Sets conversion status corresponding to |input_mode|.
+ virtual void SetTextInputMode(HWND window_handle, TextInputMode input_mode);
+
+ // Helper functions ----------------------------------------------------------
+
+ // Gets parameters for ::ImmSetOpenStatus and ::ImmSetConversionStatus from
+ // |input_mode|.
+ static void ConvertInputModeToImmFlags(TextInputMode input_mode,
+ DWORD initial_conversion_mode,
+ BOOL* open,
+ DWORD* new_conversion_mode);
+
+ protected:
+ // Retrieves the composition information.
+ void GetCompositionInfo(HIMC imm_context, LPARAM lparam,
+ CompositionText* composition);
+
+ // Updates the position of the IME windows.
+ void MoveImeWindow(HWND window_handle, HIMC imm_context);
+
+ // Completes the ongoing composition if it exists.
+ void CompleteComposition(HWND window_handle, HIMC imm_context);
+
+ // Retrieves a string from the IMM.
+ bool GetString(HIMC imm_context,
+ WPARAM lparam,
+ int type,
+ std::u16string* result);
+
+ private:
+ // Represents whether or not there is an ongoing composition in a browser
+ // process, i.e. whether or not a browser process is composing a text.
+ bool is_composing_;
+
+ // The current input Language ID retrieved from Windows, which consists of:
+ // * Primary Language ID (bit 0 to bit 9), which shows a natunal language
+ // (English, Korean, Chinese, Japanese, etc.) and;
+ // * Sub-Language ID (bit 10 to bit 15), which shows a geometrical region
+ // the language is spoken (For English, United States, United Kingdom,
+ // Australia, Canada, etc.)
+ // The following list enumerates some examples for the Language ID:
+ // * "en-US" (0x0409)
+ // MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
+ // * "ko-KR" (0x0412)
+ // MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN);
+ // * "zh-TW" (0x0404)
+ // MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL);
+ // * "zh-CN" (0x0804)
+ // MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);
+ // * "ja-JP" (0x0411)
+ // MAKELANGID(LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN), etc.
+ // (See <winnt.h> for other available values.)
+ // This Language ID is used for processing language-specific operations in
+ // IME functions.
+ LANGID input_language_id_;
+
+ // Represents whether or not the current input context has created a system
+ // caret to set the position of its IME candidate window.
+ // * true: it creates a system caret.
+ // * false: it does not create a system caret.
+ bool system_caret_;
+
+ // The rectangle of the input caret retrieved from a renderer process.
+ gfx::Rect caret_rect_;
+
+ // Indicates whether or not we want IME to render composition text.
+ bool use_composition_window_;
+};
+
+} // namespace ui
+
+#endif // UI_BASE_IME_WIN_IMM32_MANAGER_H_
diff --git a/ui/base/ime/win/imm32_manager_unittest.cc b/ui/base/ime/win/imm32_manager_unittest.cc
new file mode 100644
index 0000000000000..4d40973c626ec
--- /dev/null
+++ b/ui/base/ime/win/imm32_manager_unittest.cc
@@ -0,0 +1,69 @@
+// Copyright 2013 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/base/ime/win/imm32_manager.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace ui {
+namespace {
+
+struct InputModeTestCase {
+ TextInputMode input_mode;
+ DWORD conversion_mode;
+ BOOL expected_open;
+ DWORD expected_conversion_mode;
+};
+
+// Google Test pretty-printer.
+void PrintTo(const InputModeTestCase& data, std::ostream* os) {
+ *os << " input_mode: " << testing::PrintToString(data.input_mode)
+ << "; conversion_mode: " << testing::PrintToString(data.conversion_mode);
+}
+
+class IMM32ManagerTest
+ : public ::testing::TestWithParam<InputModeTestCase> {
+};
+
+const InputModeTestCase kInputModeTestCases[] = {
+ {TEXT_INPUT_MODE_DEFAULT, 0, FALSE, 0},
+ {TEXT_INPUT_MODE_DEFAULT, IME_CMODE_NATIVE, FALSE, IME_CMODE_NATIVE},
+ {TEXT_INPUT_MODE_TEXT, 0, FALSE, 0},
+ {TEXT_INPUT_MODE_TEXT, IME_CMODE_NATIVE, FALSE, IME_CMODE_NATIVE},
+ {TEXT_INPUT_MODE_NUMERIC, 0, FALSE, 0},
+ {TEXT_INPUT_MODE_NUMERIC, IME_CMODE_FULLSHAPE, FALSE, IME_CMODE_FULLSHAPE},
+ {TEXT_INPUT_MODE_DECIMAL, 0, FALSE, 0},
+ {TEXT_INPUT_MODE_DECIMAL, IME_CMODE_FULLSHAPE, FALSE, IME_CMODE_FULLSHAPE},
+ {TEXT_INPUT_MODE_TEL, 0, FALSE, 0},
+ {TEXT_INPUT_MODE_TEL, IME_CMODE_ROMAN, FALSE, IME_CMODE_ROMAN},
+ {TEXT_INPUT_MODE_EMAIL, 0, FALSE, 0},
+ {TEXT_INPUT_MODE_EMAIL, IME_CMODE_CHARCODE, FALSE, IME_CMODE_CHARCODE},
+ {TEXT_INPUT_MODE_URL, 0, FALSE, 0},
+ {TEXT_INPUT_MODE_URL, IME_CMODE_HANJACONVERT, FALSE,
+ IME_CMODE_HANJACONVERT},
+ {TEXT_INPUT_MODE_SEARCH, 0, FALSE, 0},
+ {TEXT_INPUT_MODE_SEARCH, IME_CMODE_CHARCODE, FALSE, IME_CMODE_CHARCODE},
+};
+
+TEST_P(IMM32ManagerTest, ConvertInputModeToImmFlags) {
+ const InputModeTestCase& test_case = GetParam();
+
+ BOOL open;
+ DWORD conversion_mode;
+ // Call testee method.
+ IMM32Manager::ConvertInputModeToImmFlags(test_case.input_mode,
+ test_case.conversion_mode,
+ &open,
+ &conversion_mode);
+
+ EXPECT_EQ(test_case.expected_open, open);
+ EXPECT_EQ(test_case.expected_conversion_mode, conversion_mode);
+}
+
+INSTANTIATE_TEST_SUITE_P(All,
+ IMM32ManagerTest,
+ ::testing::ValuesIn(kInputModeTestCases));
+
+} // namespace
+} // namespace ui
diff --git a/ui/base/ime/win/input_method_win_base.h b/ui/base/ime/win/input_method_win_base.h
index aeec7cbe8940d..d6ad5d11a5279 100644
--- a/ui/base/ime/win/input_method_win_base.h
+++ b/ui/base/ime/win/input_method_win_base.h
@@ -8,8 +8,8 @@
#include <windows.h>
#include "base/component_export.h"
-#include "base/i18n/rtl.h"
#include "ui/base/ime/input_method_base.h"
+#include "ui/base/ime/win/imm32_manager.h"
namespace ui {
diff --git a/ui/base/ime/win/input_method_win_imm32.cc b/ui/base/ime/win/input_method_win_imm32.cc
new file mode 100644
index 0000000000000..fd539da5912b3
--- /dev/null
+++ b/ui/base/ime/win/input_method_win_imm32.cc
@@ -0,0 +1,350 @@
+// Copyright 2012 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/base/ime/win/input_method_win_imm32.h"
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "base/auto_reset.h"
+#include "base/command_line.h"
+#include "base/functional/bind.h"
+#include "ui/base/ime/text_input_client.h"
+#include "ui/base/ime/win/tsf_input_scope.h"
+#include "ui/display/win/screen_win.h"
+#include "ui/events/event.h"
+#include "ui/events/event_constants.h"
+#include "ui/events/event_utils.h"
+#include "ui/events/keycodes/keyboard_codes.h"
+#include "ui/gfx/win/hwnd_util.h"
+
+namespace ui {
+
+InputMethodWinImm32::InputMethodWinImm32(
+ ImeKeyEventDispatcher* ime_key_event_dispatcher,
+ HWND attached_window_handle)
+ : InputMethodWinBase(ime_key_event_dispatcher, attached_window_handle),
+
+ enabled_(false),
+ is_candidate_popup_open_(false),
+ composing_window_handle_(NULL) {
+ imm32_manager_.SetInputLanguage();