widget/windows/nsIMM32Handler.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/windows/nsIMM32Handler.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,2142 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sts=2 sw=2 et cin: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifdef MOZ_LOGGING
    1.11 +#define FORCE_PR_LOG /* Allow logging in the release build */
    1.12 +#endif // MOZ_LOGGING
    1.13 +#include "prlog.h"
    1.14 +
    1.15 +#include "nsIMM32Handler.h"
    1.16 +#include "nsWindow.h"
    1.17 +#include "nsWindowDefs.h"
    1.18 +#include "WinUtils.h"
    1.19 +#include "KeyboardLayout.h"
    1.20 +#include <algorithm>
    1.21 +
    1.22 +#include "mozilla/MiscEvents.h"
    1.23 +#include "mozilla/TextEvents.h"
    1.24 +
    1.25 +using namespace mozilla;
    1.26 +using namespace mozilla::widget;
    1.27 +
    1.28 +static nsIMM32Handler* gIMM32Handler = nullptr;
    1.29 +
    1.30 +#ifdef PR_LOGGING
    1.31 +PRLogModuleInfo* gIMM32Log = nullptr;
    1.32 +#endif
    1.33 +
    1.34 +static UINT sWM_MSIME_MOUSE = 0; // mouse message for MSIME 98/2000
    1.35 +
    1.36 +//-------------------------------------------------------------------------
    1.37 +//
    1.38 +// from http://download.microsoft.com/download/6/0/9/60908e9e-d2c1-47db-98f6-216af76a235f/msime.h
    1.39 +// The document for this has been removed from MSDN...
    1.40 +//
    1.41 +//-------------------------------------------------------------------------
    1.42 +
    1.43 +#define RWM_MOUSE           TEXT("MSIMEMouseOperation")
    1.44 +
    1.45 +#define IMEMOUSE_NONE       0x00    // no mouse button was pushed
    1.46 +#define IMEMOUSE_LDOWN      0x01
    1.47 +#define IMEMOUSE_RDOWN      0x02
    1.48 +#define IMEMOUSE_MDOWN      0x04
    1.49 +#define IMEMOUSE_WUP        0x10    // wheel up
    1.50 +#define IMEMOUSE_WDOWN      0x20    // wheel down
    1.51 +
    1.52 +UINT nsIMM32Handler::sCodePage = 0;
    1.53 +DWORD nsIMM32Handler::sIMEProperty = 0;
    1.54 +
    1.55 +/* static */ void
    1.56 +nsIMM32Handler::EnsureHandlerInstance()
    1.57 +{
    1.58 +  if (!gIMM32Handler) {
    1.59 +    gIMM32Handler = new nsIMM32Handler();
    1.60 +  }
    1.61 +}
    1.62 +
    1.63 +/* static */ void
    1.64 +nsIMM32Handler::Initialize()
    1.65 +{
    1.66 +#ifdef PR_LOGGING
    1.67 +  if (!gIMM32Log)
    1.68 +    gIMM32Log = PR_NewLogModule("nsIMM32HandlerWidgets");
    1.69 +#endif
    1.70 +
    1.71 +  if (!sWM_MSIME_MOUSE) {
    1.72 +    sWM_MSIME_MOUSE = ::RegisterWindowMessage(RWM_MOUSE);
    1.73 +  }
    1.74 +  InitKeyboardLayout(::GetKeyboardLayout(0));
    1.75 +}
    1.76 +
    1.77 +/* static */ void
    1.78 +nsIMM32Handler::Terminate()
    1.79 +{
    1.80 +  if (!gIMM32Handler)
    1.81 +    return;
    1.82 +  delete gIMM32Handler;
    1.83 +  gIMM32Handler = nullptr;
    1.84 +}
    1.85 +
    1.86 +/* static */ bool
    1.87 +nsIMM32Handler::IsComposingOnOurEditor()
    1.88 +{
    1.89 +  return gIMM32Handler && gIMM32Handler->mIsComposing;
    1.90 +}
    1.91 +
    1.92 +/* static */ bool
    1.93 +nsIMM32Handler::IsComposingOnPlugin()
    1.94 +{
    1.95 +  return gIMM32Handler && gIMM32Handler->mIsComposingOnPlugin;
    1.96 +}
    1.97 +
    1.98 +/* static */ bool
    1.99 +nsIMM32Handler::IsComposingWindow(nsWindow* aWindow)
   1.100 +{
   1.101 +  return gIMM32Handler && gIMM32Handler->mComposingWindow == aWindow;
   1.102 +}
   1.103 +
   1.104 +/* static */ bool
   1.105 +nsIMM32Handler::IsTopLevelWindowOfComposition(nsWindow* aWindow)
   1.106 +{
   1.107 +  if (!gIMM32Handler || !gIMM32Handler->mComposingWindow) {
   1.108 +    return false;
   1.109 +  }
   1.110 +  HWND wnd = gIMM32Handler->mComposingWindow->GetWindowHandle();
   1.111 +  return WinUtils::GetTopLevelHWND(wnd, true) == aWindow->GetWindowHandle();
   1.112 +}
   1.113 +
   1.114 +/* static */ bool
   1.115 +nsIMM32Handler::ShouldDrawCompositionStringOurselves()
   1.116 +{
   1.117 +  // If current IME has special UI or its composition window should not
   1.118 +  // positioned to caret position, we should now draw composition string
   1.119 +  // ourselves.
   1.120 +  return !(sIMEProperty & IME_PROP_SPECIAL_UI) &&
   1.121 +          (sIMEProperty & IME_PROP_AT_CARET);
   1.122 +}
   1.123 +
   1.124 +/* static */ void
   1.125 +nsIMM32Handler::InitKeyboardLayout(HKL aKeyboardLayout)
   1.126 +{
   1.127 +  WORD langID = LOWORD(aKeyboardLayout);
   1.128 +  ::GetLocaleInfoW(MAKELCID(langID, SORT_DEFAULT),
   1.129 +                   LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
   1.130 +                   (PWSTR)&sCodePage, sizeof(sCodePage) / sizeof(WCHAR));
   1.131 +  sIMEProperty = ::ImmGetProperty(aKeyboardLayout, IGP_PROPERTY);
   1.132 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.133 +    ("IMM32: InitKeyboardLayout, aKeyboardLayout=%08x, sCodePage=%lu, "
   1.134 +     "sIMEProperty=%08x",
   1.135 +     aKeyboardLayout, sCodePage, sIMEProperty));
   1.136 +}
   1.137 +
   1.138 +/* static */ UINT
   1.139 +nsIMM32Handler::GetKeyboardCodePage()
   1.140 +{
   1.141 +  return sCodePage;
   1.142 +}
   1.143 +
   1.144 +/* static */
   1.145 +nsIMEUpdatePreference
   1.146 +nsIMM32Handler::GetIMEUpdatePreference()
   1.147 +{
   1.148 +  return nsIMEUpdatePreference(nsIMEUpdatePreference::NOTIFY_POSITION_CHANGE);
   1.149 +}
   1.150 +
   1.151 +// used for checking the lParam of WM_IME_COMPOSITION
   1.152 +#define IS_COMPOSING_LPARAM(lParam) \
   1.153 +  ((lParam) & (GCS_COMPSTR | GCS_COMPATTR | GCS_COMPCLAUSE | GCS_CURSORPOS))
   1.154 +#define IS_COMMITTING_LPARAM(lParam) ((lParam) & GCS_RESULTSTR)
   1.155 +// Some IMEs (e.g., the standard IME for Korean) don't have caret position,
   1.156 +// then, we should not set caret position to text event.
   1.157 +#define NO_IME_CARET -1
   1.158 +
   1.159 +nsIMM32Handler::nsIMM32Handler() :
   1.160 +  mComposingWindow(nullptr), mCursorPosition(NO_IME_CARET), mCompositionStart(0),
   1.161 +  mIsComposing(false), mIsComposingOnPlugin(false),
   1.162 +  mNativeCaretIsCreated(false)
   1.163 +{
   1.164 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS, ("IMM32: nsIMM32Handler is created\n"));
   1.165 +}
   1.166 +
   1.167 +nsIMM32Handler::~nsIMM32Handler()
   1.168 +{
   1.169 +  if (mIsComposing) {
   1.170 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.171 +      ("IMM32: ~nsIMM32Handler, ERROR, the instance is still composing\n"));
   1.172 +  }
   1.173 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS, ("IMM32: nsIMM32Handler is destroyed\n"));
   1.174 +}
   1.175 +
   1.176 +nsresult
   1.177 +nsIMM32Handler::EnsureClauseArray(int32_t aCount)
   1.178 +{
   1.179 +  NS_ENSURE_ARG_MIN(aCount, 0);
   1.180 +  mClauseArray.SetCapacity(aCount + 32);
   1.181 +  return NS_OK;
   1.182 +}
   1.183 +
   1.184 +nsresult
   1.185 +nsIMM32Handler::EnsureAttributeArray(int32_t aCount)
   1.186 +{
   1.187 +  NS_ENSURE_ARG_MIN(aCount, 0);
   1.188 +  mAttributeArray.SetCapacity(aCount + 64);
   1.189 +  return NS_OK;
   1.190 +}
   1.191 +
   1.192 +/* static */ void
   1.193 +nsIMM32Handler::CommitComposition(nsWindow* aWindow, bool aForce)
   1.194 +{
   1.195 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.196 +    ("IMM32: CommitComposition, aForce=%s, aWindow=%p, hWnd=%08x, mComposingWindow=%p%s\n",
   1.197 +     aForce ? "TRUE" : "FALSE",
   1.198 +     aWindow, aWindow->GetWindowHandle(),
   1.199 +     gIMM32Handler ? gIMM32Handler->mComposingWindow : nullptr,
   1.200 +     gIMM32Handler && gIMM32Handler->mComposingWindow ?
   1.201 +       IsComposingOnOurEditor() ? " (composing on editor)" :
   1.202 +                                  " (composing on plug-in)" : ""));
   1.203 +  if (!aForce && !IsComposingWindow(aWindow)) {
   1.204 +    return;
   1.205 +  }
   1.206 +
   1.207 +  nsIMEContext IMEContext(aWindow->GetWindowHandle());
   1.208 +  bool associated = IMEContext.AssociateDefaultContext();
   1.209 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.210 +    ("IMM32: CommitComposition, associated=%s\n",
   1.211 +     associated ? "YES" : "NO"));
   1.212 +
   1.213 +  if (IMEContext.IsValid()) {
   1.214 +    ::ImmNotifyIME(IMEContext.get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
   1.215 +    ::ImmNotifyIME(IMEContext.get(), NI_COMPOSITIONSTR, CPS_CANCEL, 0);
   1.216 +  }
   1.217 +
   1.218 +  if (associated) {
   1.219 +    IMEContext.Disassociate();
   1.220 +  }
   1.221 +}
   1.222 +
   1.223 +/* static */ void
   1.224 +nsIMM32Handler::CancelComposition(nsWindow* aWindow, bool aForce)
   1.225 +{
   1.226 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.227 +    ("IMM32: CancelComposition, aForce=%s, aWindow=%p, hWnd=%08x, mComposingWindow=%p%s\n",
   1.228 +     aForce ? "TRUE" : "FALSE",
   1.229 +     aWindow, aWindow->GetWindowHandle(),
   1.230 +     gIMM32Handler ? gIMM32Handler->mComposingWindow : nullptr,
   1.231 +     gIMM32Handler && gIMM32Handler->mComposingWindow ?
   1.232 +       IsComposingOnOurEditor() ? " (composing on editor)" :
   1.233 +                                  " (composing on plug-in)" : ""));
   1.234 +  if (!aForce && !IsComposingWindow(aWindow)) {
   1.235 +    return;
   1.236 +  }
   1.237 +
   1.238 +  nsIMEContext IMEContext(aWindow->GetWindowHandle());
   1.239 +  bool associated = IMEContext.AssociateDefaultContext();
   1.240 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.241 +    ("IMM32: CancelComposition, associated=%s\n",
   1.242 +     associated ? "YES" : "NO"));
   1.243 +
   1.244 +  if (IMEContext.IsValid()) {
   1.245 +    ::ImmNotifyIME(IMEContext.get(), NI_COMPOSITIONSTR, CPS_CANCEL, 0);
   1.246 +  }
   1.247 +
   1.248 +  if (associated) {
   1.249 +    IMEContext.Disassociate();
   1.250 +  }
   1.251 +}
   1.252 +
   1.253 +// static
   1.254 +void
   1.255 +nsIMM32Handler::OnUpdateComposition(nsWindow* aWindow)
   1.256 +{
   1.257 +  if (!gIMM32Handler) {
   1.258 +    return;
   1.259 +  }
   1.260 + 
   1.261 +  if (aWindow->PluginHasFocus()) {
   1.262 +    return;
   1.263 +  }
   1.264 +
   1.265 +  nsIMEContext IMEContext(aWindow->GetWindowHandle());
   1.266 +  gIMM32Handler->SetIMERelatedWindowsPos(aWindow, IMEContext);
   1.267 +}
   1.268 +
   1.269 +
   1.270 +/* static */ bool
   1.271 +nsIMM32Handler::ProcessInputLangChangeMessage(nsWindow* aWindow,
   1.272 +                                              WPARAM wParam,
   1.273 +                                              LPARAM lParam,
   1.274 +                                              MSGResult& aResult)
   1.275 +{
   1.276 +  aResult.mResult = 0;
   1.277 +  aResult.mConsumed = false;
   1.278 +  // We don't need to create the instance of the handler here.
   1.279 +  if (gIMM32Handler) {
   1.280 +    gIMM32Handler->OnInputLangChange(aWindow, wParam, lParam, aResult);
   1.281 +  }
   1.282 +  InitKeyboardLayout(reinterpret_cast<HKL>(lParam));
   1.283 +  // We can release the instance here, because the instance may be never
   1.284 +  // used. E.g., the new keyboard layout may not use IME, or it may use TSF.
   1.285 +  Terminate();
   1.286 +  // Don't return as "processed", the messages should be processed on nsWindow
   1.287 +  // too.
   1.288 +  return false;
   1.289 +}
   1.290 +
   1.291 +/* static */ bool
   1.292 +nsIMM32Handler::ProcessMessage(nsWindow* aWindow, UINT msg,
   1.293 +                               WPARAM &wParam, LPARAM &lParam,
   1.294 +                               MSGResult& aResult)
   1.295 +{
   1.296 +  // XXX We store the composing window in mComposingWindow.  If IME messages are
   1.297 +  // sent to different window, we should commit the old transaction.  And also
   1.298 +  // if the new window handle is not focused, probably, we should not start
   1.299 +  // the composition, however, such case should not be, it's just bad scenario.
   1.300 +
   1.301 +  // When a plug-in has focus or compsition, we should dispatch the IME events
   1.302 +  // to the plug-in.
   1.303 +  if (aWindow->PluginHasFocus() || IsComposingOnPlugin()) {
   1.304 +      return ProcessMessageForPlugin(aWindow, msg, wParam, lParam, aResult);
   1.305 +  }
   1.306 +
   1.307 +  aResult.mResult = 0;
   1.308 +  switch (msg) {
   1.309 +    case WM_LBUTTONDOWN:
   1.310 +    case WM_MBUTTONDOWN:
   1.311 +    case WM_RBUTTONDOWN: {
   1.312 +      // We don't need to create the instance of the handler here.
   1.313 +      if (!gIMM32Handler) {
   1.314 +        return false;
   1.315 +      }
   1.316 +      return gIMM32Handler->OnMouseEvent(aWindow, lParam,
   1.317 +                              msg == WM_LBUTTONDOWN ? IMEMOUSE_LDOWN :
   1.318 +                              msg == WM_MBUTTONDOWN ? IMEMOUSE_MDOWN :
   1.319 +                                                      IMEMOUSE_RDOWN, aResult);
   1.320 +    }
   1.321 +    case WM_INPUTLANGCHANGE:
   1.322 +      return ProcessInputLangChangeMessage(aWindow, wParam, lParam, aResult);
   1.323 +    case WM_IME_STARTCOMPOSITION:
   1.324 +      EnsureHandlerInstance();
   1.325 +      return gIMM32Handler->OnIMEStartComposition(aWindow, aResult);
   1.326 +    case WM_IME_COMPOSITION:
   1.327 +      EnsureHandlerInstance();
   1.328 +      return gIMM32Handler->OnIMEComposition(aWindow, wParam, lParam, aResult);
   1.329 +    case WM_IME_ENDCOMPOSITION:
   1.330 +      EnsureHandlerInstance();
   1.331 +      return gIMM32Handler->OnIMEEndComposition(aWindow, aResult);
   1.332 +    case WM_IME_CHAR:
   1.333 +      return OnIMEChar(aWindow, wParam, lParam, aResult);
   1.334 +    case WM_IME_NOTIFY:
   1.335 +      return OnIMENotify(aWindow, wParam, lParam, aResult);
   1.336 +    case WM_IME_REQUEST:
   1.337 +      EnsureHandlerInstance();
   1.338 +      return gIMM32Handler->OnIMERequest(aWindow, wParam, lParam, aResult);
   1.339 +    case WM_IME_SELECT:
   1.340 +      return OnIMESelect(aWindow, wParam, lParam, aResult);
   1.341 +    case WM_IME_SETCONTEXT:
   1.342 +      return OnIMESetContext(aWindow, wParam, lParam, aResult);
   1.343 +    case WM_KEYDOWN:
   1.344 +      return OnKeyDownEvent(aWindow, wParam, lParam, aResult);
   1.345 +    case WM_CHAR:
   1.346 +      if (!gIMM32Handler) {
   1.347 +        return false;
   1.348 +      }
   1.349 +      return gIMM32Handler->OnChar(aWindow, wParam, lParam, aResult);
   1.350 +    default:
   1.351 +      return false;
   1.352 +  };
   1.353 +}
   1.354 +
   1.355 +/* static */ bool
   1.356 +nsIMM32Handler::ProcessMessageForPlugin(nsWindow* aWindow, UINT msg,
   1.357 +                                        WPARAM &wParam, LPARAM &lParam,
   1.358 +                                        MSGResult& aResult)
   1.359 +{
   1.360 +  aResult.mResult = 0;
   1.361 +  aResult.mConsumed = false;
   1.362 +  switch (msg) {
   1.363 +    case WM_INPUTLANGCHANGEREQUEST:
   1.364 +    case WM_INPUTLANGCHANGE:
   1.365 +      aWindow->DispatchPluginEvent(msg, wParam, lParam, false);
   1.366 +      return ProcessInputLangChangeMessage(aWindow, wParam, lParam, aResult);
   1.367 +    case WM_IME_COMPOSITION:
   1.368 +      EnsureHandlerInstance();
   1.369 +      return gIMM32Handler->OnIMECompositionOnPlugin(aWindow, wParam, lParam,
   1.370 +                                                     aResult);
   1.371 +    case WM_IME_STARTCOMPOSITION:
   1.372 +      EnsureHandlerInstance();
   1.373 +      return gIMM32Handler->OnIMEStartCompositionOnPlugin(aWindow, wParam,
   1.374 +                                                          lParam, aResult);
   1.375 +    case WM_IME_ENDCOMPOSITION:
   1.376 +      EnsureHandlerInstance();
   1.377 +      return gIMM32Handler->OnIMEEndCompositionOnPlugin(aWindow, wParam, lParam,
   1.378 +                                                        aResult);
   1.379 +    case WM_IME_CHAR:
   1.380 +      EnsureHandlerInstance();
   1.381 +      return gIMM32Handler->OnIMECharOnPlugin(aWindow, wParam, lParam, aResult);
   1.382 +    case WM_IME_SETCONTEXT:
   1.383 +      return OnIMESetContextOnPlugin(aWindow, wParam, lParam, aResult);
   1.384 +    case WM_CHAR:
   1.385 +      if (!gIMM32Handler) {
   1.386 +        return false;
   1.387 +      }
   1.388 +      return gIMM32Handler->OnCharOnPlugin(aWindow, wParam, lParam, aResult);
   1.389 +    case WM_IME_COMPOSITIONFULL:
   1.390 +    case WM_IME_CONTROL:
   1.391 +    case WM_IME_KEYDOWN:
   1.392 +    case WM_IME_KEYUP:
   1.393 +    case WM_IME_REQUEST:
   1.394 +    case WM_IME_SELECT:
   1.395 +      aResult.mConsumed =
   1.396 +        aWindow->DispatchPluginEvent(msg, wParam, lParam, false);
   1.397 +      return true;
   1.398 +  }
   1.399 +  return false;
   1.400 +}
   1.401 +
   1.402 +/****************************************************************************
   1.403 + * message handlers
   1.404 + ****************************************************************************/
   1.405 +
   1.406 +void
   1.407 +nsIMM32Handler::OnInputLangChange(nsWindow* aWindow,
   1.408 +                                  WPARAM wParam,
   1.409 +                                  LPARAM lParam,
   1.410 +                                  MSGResult& aResult)
   1.411 +{
   1.412 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.413 +    ("IMM32: OnInputLangChange, hWnd=%08x, wParam=%08x, lParam=%08x\n",
   1.414 +     aWindow->GetWindowHandle(), wParam, lParam));
   1.415 +
   1.416 +  aWindow->NotifyIME(REQUEST_TO_COMMIT_COMPOSITION);
   1.417 +  NS_ASSERTION(!mIsComposing, "ResetInputState failed");
   1.418 +
   1.419 +  if (mIsComposing) {
   1.420 +    HandleEndComposition(aWindow);
   1.421 +  }
   1.422 +
   1.423 +  aResult.mConsumed = false;
   1.424 +}
   1.425 +
   1.426 +bool
   1.427 +nsIMM32Handler::OnIMEStartComposition(nsWindow* aWindow,
   1.428 +                                      MSGResult& aResult)
   1.429 +{
   1.430 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.431 +    ("IMM32: OnIMEStartComposition, hWnd=%08x, mIsComposing=%s\n",
   1.432 +     aWindow->GetWindowHandle(), mIsComposing ? "TRUE" : "FALSE"));
   1.433 +  aResult.mConsumed = ShouldDrawCompositionStringOurselves();
   1.434 +  if (mIsComposing) {
   1.435 +    NS_WARNING("Composition has been already started");
   1.436 +    return true;
   1.437 +  }
   1.438 +
   1.439 +  nsIMEContext IMEContext(aWindow->GetWindowHandle());
   1.440 +  HandleStartComposition(aWindow, IMEContext);
   1.441 +  return true;
   1.442 +}
   1.443 +
   1.444 +bool
   1.445 +nsIMM32Handler::OnIMEComposition(nsWindow* aWindow,
   1.446 +                                 WPARAM wParam,
   1.447 +                                 LPARAM lParam,
   1.448 +                                 MSGResult& aResult)
   1.449 +{
   1.450 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.451 +    ("IMM32: OnIMEComposition, hWnd=%08x, lParam=%08x, mIsComposing=%s\n",
   1.452 +     aWindow->GetWindowHandle(), lParam, mIsComposing ? "TRUE" : "FALSE"));
   1.453 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.454 +    ("IMM32: OnIMEComposition, GCS_RESULTSTR=%s, GCS_COMPSTR=%s, GCS_COMPATTR=%s, GCS_COMPCLAUSE=%s, GCS_CURSORPOS=%s\n",
   1.455 +     lParam & GCS_RESULTSTR  ? "YES" : "no",
   1.456 +     lParam & GCS_COMPSTR    ? "YES" : "no",
   1.457 +     lParam & GCS_COMPATTR   ? "YES" : "no",
   1.458 +     lParam & GCS_COMPCLAUSE ? "YES" : "no",
   1.459 +     lParam & GCS_CURSORPOS  ? "YES" : "no"));
   1.460 +
   1.461 +  NS_PRECONDITION(!aWindow->PluginHasFocus(),
   1.462 +    "OnIMEComposition should not be called when a plug-in has focus");
   1.463 +
   1.464 +  nsIMEContext IMEContext(aWindow->GetWindowHandle());
   1.465 +  aResult.mConsumed = HandleComposition(aWindow, IMEContext, lParam);
   1.466 +  return true;
   1.467 +}
   1.468 +
   1.469 +bool
   1.470 +nsIMM32Handler::OnIMEEndComposition(nsWindow* aWindow,
   1.471 +                                    MSGResult& aResult)
   1.472 +{
   1.473 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.474 +    ("IMM32: OnIMEEndComposition, hWnd=%08x, mIsComposing=%s\n",
   1.475 +     aWindow->GetWindowHandle(), mIsComposing ? "TRUE" : "FALSE"));
   1.476 +
   1.477 +  aResult.mConsumed = ShouldDrawCompositionStringOurselves();
   1.478 +  if (!mIsComposing) {
   1.479 +    return true;
   1.480 +  }
   1.481 +
   1.482 +  // Korean IME posts WM_IME_ENDCOMPOSITION first when we hit space during
   1.483 +  // composition. Then, we should ignore the message and commit the composition
   1.484 +  // string at following WM_IME_COMPOSITION.
   1.485 +  MSG compositionMsg;
   1.486 +  if (WinUtils::PeekMessage(&compositionMsg, aWindow->GetWindowHandle(),
   1.487 +                            WM_IME_STARTCOMPOSITION, WM_IME_COMPOSITION,
   1.488 +                            PM_NOREMOVE) &&
   1.489 +      compositionMsg.message == WM_IME_COMPOSITION &&
   1.490 +      IS_COMMITTING_LPARAM(compositionMsg.lParam)) {
   1.491 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.492 +      ("IMM32: OnIMEEndComposition, WM_IME_ENDCOMPOSITION is followed by "
   1.493 +       "WM_IME_COMPOSITION, ignoring the message..."));
   1.494 +    return true;
   1.495 +  }
   1.496 +
   1.497 +  // Otherwise, e.g., ChangJie doesn't post WM_IME_COMPOSITION before
   1.498 +  // WM_IME_ENDCOMPOSITION when composition string becomes empty.
   1.499 +  // Then, we should dispatch a compositionupdate event, a text event and
   1.500 +  // a compositionend event.
   1.501 +  // XXX Shouldn't we dispatch the text event with actual or latest composition
   1.502 +  //     string?
   1.503 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.504 +    ("IMM32: OnIMEEndComposition, mCompositionString=\"%s\"%s",
   1.505 +     NS_ConvertUTF16toUTF8(mCompositionString).get(),
   1.506 +     mCompositionString.IsEmpty() ? "" : ", but canceling it..."));
   1.507 +
   1.508 +  mCompositionString.Truncate();
   1.509 +
   1.510 +  nsIMEContext IMEContext(aWindow->GetWindowHandle());
   1.511 +  DispatchTextEvent(aWindow, IMEContext, false);
   1.512 +
   1.513 +  HandleEndComposition(aWindow);
   1.514 +
   1.515 +  return true;
   1.516 +}
   1.517 +
   1.518 +/* static */ bool
   1.519 +nsIMM32Handler::OnIMEChar(nsWindow* aWindow,
   1.520 +                          WPARAM wParam,
   1.521 +                          LPARAM lParam,
   1.522 +                          MSGResult& aResult)
   1.523 +{
   1.524 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.525 +    ("IMM32: OnIMEChar, hWnd=%08x, char=%08x\n",
   1.526 +     aWindow->GetWindowHandle(), wParam));
   1.527 +
   1.528 +  // We don't need to fire any text events from here. This method will be
   1.529 +  // called when the composition string of the current IME is not drawn by us
   1.530 +  // and some characters are committed. In that case, the committed string was
   1.531 +  // processed in nsWindow::OnIMEComposition already.
   1.532 +
   1.533 +  // We need to consume the message so that Windows don't send two WM_CHAR msgs
   1.534 +  aResult.mConsumed = true;
   1.535 +  return true;
   1.536 +}
   1.537 +
   1.538 +/* static */ bool
   1.539 +nsIMM32Handler::OnIMECompositionFull(nsWindow* aWindow,
   1.540 +                                     MSGResult& aResult)
   1.541 +{
   1.542 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.543 +    ("IMM32: OnIMECompositionFull, hWnd=%08x\n",
   1.544 +     aWindow->GetWindowHandle()));
   1.545 +
   1.546 +  // not implement yet
   1.547 +  aResult.mConsumed = false;
   1.548 +  return true;
   1.549 +}
   1.550 +
   1.551 +/* static */ bool
   1.552 +nsIMM32Handler::OnIMENotify(nsWindow* aWindow,
   1.553 +                            WPARAM wParam,
   1.554 +                            LPARAM lParam,
   1.555 +                            MSGResult& aResult)
   1.556 +{
   1.557 +#ifdef PR_LOGGING
   1.558 +  switch (wParam) {
   1.559 +    case IMN_CHANGECANDIDATE:
   1.560 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.561 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_CHANGECANDIDATE, lParam=%08x\n",
   1.562 +         aWindow->GetWindowHandle(), lParam));
   1.563 +      break;
   1.564 +    case IMN_CLOSECANDIDATE:
   1.565 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.566 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_CLOSECANDIDATE, lParam=%08x\n",
   1.567 +         aWindow->GetWindowHandle(), lParam));
   1.568 +      break;
   1.569 +    case IMN_CLOSESTATUSWINDOW:
   1.570 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.571 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_CLOSESTATUSWINDOW\n",
   1.572 +         aWindow->GetWindowHandle()));
   1.573 +      break;
   1.574 +    case IMN_GUIDELINE:
   1.575 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.576 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_GUIDELINE\n",
   1.577 +         aWindow->GetWindowHandle()));
   1.578 +      break;
   1.579 +    case IMN_OPENCANDIDATE:
   1.580 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.581 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_OPENCANDIDATE, lParam=%08x\n",
   1.582 +         aWindow->GetWindowHandle(), lParam));
   1.583 +      break;
   1.584 +    case IMN_OPENSTATUSWINDOW:
   1.585 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.586 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_OPENSTATUSWINDOW\n",
   1.587 +         aWindow->GetWindowHandle()));
   1.588 +      break;
   1.589 +    case IMN_SETCANDIDATEPOS:
   1.590 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.591 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_SETCANDIDATEPOS, lParam=%08x\n",
   1.592 +         aWindow->GetWindowHandle(), lParam));
   1.593 +      break;
   1.594 +    case IMN_SETCOMPOSITIONFONT:
   1.595 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.596 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_SETCOMPOSITIONFONT\n",
   1.597 +         aWindow->GetWindowHandle()));
   1.598 +      break;
   1.599 +    case IMN_SETCOMPOSITIONWINDOW:
   1.600 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.601 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_SETCOMPOSITIONWINDOW\n",
   1.602 +         aWindow->GetWindowHandle()));
   1.603 +      break;
   1.604 +    case IMN_SETCONVERSIONMODE:
   1.605 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.606 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_SETCONVERSIONMODE\n",
   1.607 +         aWindow->GetWindowHandle()));
   1.608 +      break;
   1.609 +    case IMN_SETOPENSTATUS:
   1.610 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.611 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_SETOPENSTATUS\n",
   1.612 +         aWindow->GetWindowHandle()));
   1.613 +      break;
   1.614 +    case IMN_SETSENTENCEMODE:
   1.615 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.616 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_SETSENTENCEMODE\n",
   1.617 +         aWindow->GetWindowHandle()));
   1.618 +      break;
   1.619 +    case IMN_SETSTATUSWINDOWPOS:
   1.620 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.621 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_SETSTATUSWINDOWPOS\n",
   1.622 +         aWindow->GetWindowHandle()));
   1.623 +      break;
   1.624 +    case IMN_PRIVATE:
   1.625 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.626 +        ("IMM32: OnIMENotify, hWnd=%08x, IMN_PRIVATE\n",
   1.627 +         aWindow->GetWindowHandle()));
   1.628 +      break;
   1.629 +  }
   1.630 +#endif // PR_LOGGING
   1.631 +
   1.632 +  // not implement yet
   1.633 +  aResult.mConsumed = false;
   1.634 +  return true;
   1.635 +}
   1.636 +
   1.637 +bool
   1.638 +nsIMM32Handler::OnIMERequest(nsWindow* aWindow,
   1.639 +                             WPARAM wParam,
   1.640 +                             LPARAM lParam,
   1.641 +                             MSGResult& aResult)
   1.642 +{
   1.643 +  switch (wParam) {
   1.644 +    case IMR_RECONVERTSTRING:
   1.645 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.646 +        ("IMM32: OnIMERequest, hWnd=%08x, IMR_RECONVERTSTRING\n",
   1.647 +         aWindow->GetWindowHandle()));
   1.648 +      aResult.mConsumed = HandleReconvert(aWindow, lParam, &aResult.mResult);
   1.649 +      return true;
   1.650 +    case IMR_QUERYCHARPOSITION:
   1.651 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.652 +        ("IMM32: OnIMERequest, hWnd=%08x, IMR_QUERYCHARPOSITION\n",
   1.653 +         aWindow->GetWindowHandle()));
   1.654 +      aResult.mConsumed =
   1.655 +        HandleQueryCharPosition(aWindow, lParam, &aResult.mResult);
   1.656 +      return true;
   1.657 +    case IMR_DOCUMENTFEED:
   1.658 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.659 +        ("IMM32: OnIMERequest, hWnd=%08x, IMR_DOCUMENTFEED\n",
   1.660 +         aWindow->GetWindowHandle()));
   1.661 +      aResult.mConsumed = HandleDocumentFeed(aWindow, lParam, &aResult.mResult);
   1.662 +      return true;
   1.663 +    default:
   1.664 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.665 +        ("IMM32: OnIMERequest, hWnd=%08x, wParam=%08x\n",
   1.666 +         aWindow->GetWindowHandle(), wParam));
   1.667 +      aResult.mConsumed = false;
   1.668 +      return true;
   1.669 +  }
   1.670 +}
   1.671 +
   1.672 +/* static */ bool
   1.673 +nsIMM32Handler::OnIMESelect(nsWindow* aWindow,
   1.674 +                            WPARAM wParam,
   1.675 +                            LPARAM lParam,
   1.676 +                            MSGResult& aResult)
   1.677 +{
   1.678 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.679 +    ("IMM32: OnIMESelect, hWnd=%08x, wParam=%08x, lParam=%08x\n",
   1.680 +     aWindow->GetWindowHandle(), wParam, lParam));
   1.681 +
   1.682 +  // not implement yet
   1.683 +  aResult.mConsumed = false;
   1.684 +  return true;
   1.685 +}
   1.686 +
   1.687 +/* static */ bool
   1.688 +nsIMM32Handler::OnIMESetContext(nsWindow* aWindow,
   1.689 +                                WPARAM wParam,
   1.690 +                                LPARAM lParam,
   1.691 +                                MSGResult& aResult)
   1.692 +{
   1.693 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.694 +    ("IMM32: OnIMESetContext, hWnd=%08x, %s, lParam=%08x\n",
   1.695 +     aWindow->GetWindowHandle(), wParam ? "Active" : "Deactive", lParam));
   1.696 +
   1.697 +  aResult.mConsumed = false;
   1.698 +
   1.699 +  // NOTE: If the aWindow is top level window of the composing window because
   1.700 +  // when a window on deactive window gets focus, WM_IME_SETCONTEXT (wParam is
   1.701 +  // TRUE) is sent to the top level window first.  After that,
   1.702 +  // WM_IME_SETCONTEXT (wParam is FALSE) is sent to the top level window.
   1.703 +  // Finally, WM_IME_SETCONTEXT (wParam is TRUE) is sent to the focused window.
   1.704 +  // The top level window never becomes composing window, so, we can ignore
   1.705 +  // the WM_IME_SETCONTEXT on the top level window.
   1.706 +  if (IsTopLevelWindowOfComposition(aWindow)) {
   1.707 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.708 +      ("IMM32: OnIMESetContext, hWnd=%08x is top level window\n"));
   1.709 +    return true;
   1.710 +  }
   1.711 +
   1.712 +  // When IME context is activating on another window,
   1.713 +  // we should commit the old composition on the old window.
   1.714 +  bool cancelComposition = false;
   1.715 +  if (wParam && gIMM32Handler) {
   1.716 +    cancelComposition =
   1.717 +      gIMM32Handler->CommitCompositionOnPreviousWindow(aWindow);
   1.718 +  }
   1.719 +
   1.720 +  if (wParam && (lParam & ISC_SHOWUICOMPOSITIONWINDOW) &&
   1.721 +      ShouldDrawCompositionStringOurselves()) {
   1.722 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.723 +      ("IMM32: OnIMESetContext, ISC_SHOWUICOMPOSITIONWINDOW is removed\n"));
   1.724 +    lParam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
   1.725 +  }
   1.726 +
   1.727 +  // We should sent WM_IME_SETCONTEXT to the DefWndProc here because the
   1.728 +  // ancestor windows shouldn't receive this message.  If they receive the
   1.729 +  // message, we cannot know whether which window is the target of the message.
   1.730 +  aResult.mResult = ::DefWindowProc(aWindow->GetWindowHandle(),
   1.731 +                                    WM_IME_SETCONTEXT, wParam, lParam);
   1.732 +
   1.733 +  // Cancel composition on the new window if we committed our composition on
   1.734 +  // another window.
   1.735 +  if (cancelComposition) {
   1.736 +    CancelComposition(aWindow, true);
   1.737 +  }
   1.738 +
   1.739 +  aResult.mConsumed = true;
   1.740 +  return true;
   1.741 +}
   1.742 +
   1.743 +bool
   1.744 +nsIMM32Handler::OnChar(nsWindow* aWindow,
   1.745 +                       WPARAM wParam,
   1.746 +                       LPARAM lParam,
   1.747 +                       MSGResult& aResult)
   1.748 +{
   1.749 +  // The return value must be same as aResult.mConsumed because only when we
   1.750 +  // consume the message, the caller shouldn't do anything anymore but
   1.751 +  // otherwise, the caller should handle the message.
   1.752 +  aResult.mConsumed = false;
   1.753 +  if (IsIMECharRecordsEmpty()) {
   1.754 +    return aResult.mConsumed;
   1.755 +  }
   1.756 +  WPARAM recWParam;
   1.757 +  LPARAM recLParam;
   1.758 +  DequeueIMECharRecords(recWParam, recLParam);
   1.759 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.760 +    ("IMM32: OnChar, aWindow=%p, wParam=%08x, lParam=%08x,\n",
   1.761 +     aWindow->GetWindowHandle(), wParam, lParam));
   1.762 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.763 +    ("               recorded: wParam=%08x, lParam=%08x\n",
   1.764 +     recWParam, recLParam));
   1.765 +  // If an unexpected char message comes, we should reset the records,
   1.766 +  // of course, this shouldn't happen.
   1.767 +  if (recWParam != wParam || recLParam != lParam) {
   1.768 +    ResetIMECharRecords();
   1.769 +    return aResult.mConsumed;
   1.770 +  }
   1.771 +  // Eat the char message which is caused by WM_IME_CHAR because we should
   1.772 +  // have processed the IME messages, so, this message could be come from
   1.773 +  // a windowless plug-in.
   1.774 +  aResult.mConsumed = true;
   1.775 +  return aResult.mConsumed;
   1.776 +}
   1.777 +
   1.778 +/****************************************************************************
   1.779 + * message handlers for plug-in
   1.780 + ****************************************************************************/
   1.781 +
   1.782 +bool
   1.783 +nsIMM32Handler::OnIMEStartCompositionOnPlugin(nsWindow* aWindow,
   1.784 +                                              WPARAM wParam,
   1.785 +                                              LPARAM lParam,
   1.786 +                                              MSGResult& aResult)
   1.787 +{
   1.788 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.789 +    ("IMM32: OnIMEStartCompositionOnPlugin, hWnd=%08x, mIsComposingOnPlugin=%s\n",
   1.790 +     aWindow->GetWindowHandle(), mIsComposingOnPlugin ? "TRUE" : "FALSE"));
   1.791 +  mIsComposingOnPlugin = true;
   1.792 +  mComposingWindow = aWindow;
   1.793 +  nsIMEContext IMEContext(aWindow->GetWindowHandle());
   1.794 +  SetIMERelatedWindowsPosOnPlugin(aWindow, IMEContext);
   1.795 +  aResult.mConsumed =
   1.796 +    aWindow->DispatchPluginEvent(WM_IME_STARTCOMPOSITION, wParam, lParam,
   1.797 +                                 false);
   1.798 +  return true;
   1.799 +}
   1.800 +
   1.801 +bool
   1.802 +nsIMM32Handler::OnIMECompositionOnPlugin(nsWindow* aWindow,
   1.803 +                                         WPARAM wParam,
   1.804 +                                         LPARAM lParam,
   1.805 +                                         MSGResult& aResult)
   1.806 +{
   1.807 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.808 +    ("IMM32: OnIMECompositionOnPlugin, hWnd=%08x, lParam=%08x, mIsComposingOnPlugin=%s\n",
   1.809 +     aWindow->GetWindowHandle(), lParam,
   1.810 +     mIsComposingOnPlugin ? "TRUE" : "FALSE"));
   1.811 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.812 +    ("IMM32: OnIMECompositionOnPlugin, GCS_RESULTSTR=%s, GCS_COMPSTR=%s, GCS_COMPATTR=%s, GCS_COMPCLAUSE=%s, GCS_CURSORPOS=%s\n",
   1.813 +     lParam & GCS_RESULTSTR  ? "YES" : "no",
   1.814 +     lParam & GCS_COMPSTR    ? "YES" : "no",
   1.815 +     lParam & GCS_COMPATTR   ? "YES" : "no",
   1.816 +     lParam & GCS_COMPCLAUSE ? "YES" : "no",
   1.817 +     lParam & GCS_CURSORPOS  ? "YES" : "no"));
   1.818 +  // We should end composition if there is a committed string.
   1.819 +  if (IS_COMMITTING_LPARAM(lParam)) {
   1.820 +    mIsComposingOnPlugin = false;
   1.821 +    mComposingWindow = nullptr;
   1.822 +  }
   1.823 +  // Continue composition if there is still a string being composed.
   1.824 +  if (IS_COMPOSING_LPARAM(lParam)) {
   1.825 +    mIsComposingOnPlugin = true;
   1.826 +    mComposingWindow = aWindow;
   1.827 +    nsIMEContext IMEContext(aWindow->GetWindowHandle());
   1.828 +    SetIMERelatedWindowsPosOnPlugin(aWindow, IMEContext);
   1.829 +  }
   1.830 +  aResult.mConsumed =
   1.831 +    aWindow->DispatchPluginEvent(WM_IME_COMPOSITION, wParam, lParam, true);
   1.832 +  return true;
   1.833 +}
   1.834 +
   1.835 +bool
   1.836 +nsIMM32Handler::OnIMEEndCompositionOnPlugin(nsWindow* aWindow,
   1.837 +                                            WPARAM wParam,
   1.838 +                                            LPARAM lParam,
   1.839 +                                            MSGResult& aResult)
   1.840 +{
   1.841 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.842 +    ("IMM32: OnIMEEndCompositionOnPlugin, hWnd=%08x, mIsComposingOnPlugin=%s\n",
   1.843 +     aWindow->GetWindowHandle(), mIsComposingOnPlugin ? "TRUE" : "FALSE"));
   1.844 +
   1.845 +  mIsComposingOnPlugin = false;
   1.846 +  mComposingWindow = nullptr;
   1.847 +
   1.848 +  if (mNativeCaretIsCreated) {
   1.849 +    ::DestroyCaret();
   1.850 +    mNativeCaretIsCreated = false;
   1.851 +  }
   1.852 +
   1.853 +  aResult.mConsumed =
   1.854 +    aWindow->DispatchPluginEvent(WM_IME_ENDCOMPOSITION, wParam, lParam,
   1.855 +                                 false);
   1.856 +  return true;
   1.857 +}
   1.858 +
   1.859 +bool
   1.860 +nsIMM32Handler::OnIMECharOnPlugin(nsWindow* aWindow,
   1.861 +                                  WPARAM wParam,
   1.862 +                                  LPARAM lParam,
   1.863 +                                  MSGResult& aResult)
   1.864 +{
   1.865 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.866 +    ("IMM32: OnIMECharOnPlugin, hWnd=%08x, char=%08x, scancode=%08x\n",
   1.867 +     aWindow->GetWindowHandle(), wParam, lParam));
   1.868 +
   1.869 +  aResult.mConsumed =
   1.870 +    aWindow->DispatchPluginEvent(WM_IME_CHAR, wParam, lParam, true);
   1.871 +
   1.872 +  if (!aResult.mConsumed) {
   1.873 +    // Record the WM_CHAR messages which are going to be coming.
   1.874 +    EnsureHandlerInstance();
   1.875 +    EnqueueIMECharRecords(wParam, lParam);
   1.876 +  }
   1.877 +  return true;
   1.878 +}
   1.879 +
   1.880 +/* static */ bool
   1.881 +nsIMM32Handler::OnIMESetContextOnPlugin(nsWindow* aWindow,
   1.882 +                                        WPARAM wParam,
   1.883 +                                        LPARAM lParam,
   1.884 +                                        MSGResult& aResult)
   1.885 +{
   1.886 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.887 +    ("IMM32: OnIMESetContextOnPlugin, hWnd=%08x, %s, lParam=%08x\n",
   1.888 +     aWindow->GetWindowHandle(), wParam ? "Active" : "Deactive", lParam));
   1.889 +
   1.890 +  // If the IME context becomes active on a plug-in, we should commit
   1.891 +  // our composition.  And also we should cancel the composition on new
   1.892 +  // window.  Note that if IsTopLevelWindowOfComposition(aWindow) returns
   1.893 +  // true, we should ignore the message here, see the comment in
   1.894 +  // OnIMESetContext() for the detail.
   1.895 +  if (wParam && gIMM32Handler && !IsTopLevelWindowOfComposition(aWindow)) {
   1.896 +    if (gIMM32Handler->CommitCompositionOnPreviousWindow(aWindow)) {
   1.897 +      CancelComposition(aWindow);
   1.898 +    }
   1.899 +  }
   1.900 +
   1.901 +  // Dispatch message to the plug-in.
   1.902 +  // XXX When a windowless plug-in gets focus, we should send
   1.903 +  //     WM_IME_SETCONTEXT
   1.904 +  aWindow->DispatchPluginEvent(WM_IME_SETCONTEXT, wParam, lParam, false);
   1.905 +
   1.906 +  // We should send WM_IME_SETCONTEXT to the DefWndProc here.  It shouldn't
   1.907 +  // be received on ancestor windows, see OnIMESetContext() for the detail.
   1.908 +  aResult.mResult = ::DefWindowProc(aWindow->GetWindowHandle(),
   1.909 +                                    WM_IME_SETCONTEXT, wParam, lParam);
   1.910 +
   1.911 +  // Don't synchronously dispatch the pending events when we receive
   1.912 +  // WM_IME_SETCONTEXT because we get it during plugin destruction.
   1.913 +  // (bug 491848)
   1.914 +  aResult.mConsumed = true;
   1.915 +  return true;
   1.916 +}
   1.917 +
   1.918 +bool
   1.919 +nsIMM32Handler::OnCharOnPlugin(nsWindow* aWindow,
   1.920 +                               WPARAM wParam,
   1.921 +                               LPARAM lParam,
   1.922 +                               MSGResult& aResult)
   1.923 +{
   1.924 +  // We should never consume char message on windowless plugin.
   1.925 +  aResult.mConsumed = false;
   1.926 +  if (IsIMECharRecordsEmpty()) {
   1.927 +    return false;
   1.928 +  }
   1.929 +
   1.930 +  WPARAM recWParam;
   1.931 +  LPARAM recLParam;
   1.932 +  DequeueIMECharRecords(recWParam, recLParam);
   1.933 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.934 +    ("IMM32: OnCharOnPlugin, aWindow=%p, wParam=%08x, lParam=%08x,\n",
   1.935 +     aWindow->GetWindowHandle(), wParam, lParam));
   1.936 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.937 +    ("                       recorded: wParam=%08x, lParam=%08x\n",
   1.938 +     recWParam, recLParam));
   1.939 +  // If an unexpected char message comes, we should reset the records,
   1.940 +  // of course, this shouldn't happen.
   1.941 +  if (recWParam != wParam || recLParam != lParam) {
   1.942 +    ResetIMECharRecords();
   1.943 +  }
   1.944 +  // WM_CHAR on plug-in is always handled by nsWindow.
   1.945 +  return false;
   1.946 +}
   1.947 +
   1.948 +/****************************************************************************
   1.949 + * others
   1.950 + ****************************************************************************/
   1.951 +
   1.952 +void
   1.953 +nsIMM32Handler::HandleStartComposition(nsWindow* aWindow,
   1.954 +                                       const nsIMEContext &aIMEContext)
   1.955 +{
   1.956 +  NS_PRECONDITION(!mIsComposing,
   1.957 +    "HandleStartComposition is called but mIsComposing is TRUE");
   1.958 +  NS_PRECONDITION(!aWindow->PluginHasFocus(),
   1.959 +    "HandleStartComposition should not be called when a plug-in has focus");
   1.960 +
   1.961 +  WidgetQueryContentEvent selection(true, NS_QUERY_SELECTED_TEXT, aWindow);
   1.962 +  nsIntPoint point(0, 0);
   1.963 +  aWindow->InitEvent(selection, &point);
   1.964 +  aWindow->DispatchWindowEvent(&selection);
   1.965 +  if (!selection.mSucceeded) {
   1.966 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.967 +      ("IMM32: HandleStartComposition, FAILED (NS_QUERY_SELECTED_TEXT)\n"));
   1.968 +    return;
   1.969 +  }
   1.970 +
   1.971 +  mCompositionStart = selection.mReply.mOffset;
   1.972 +  mLastDispatchedCompositionString.Truncate();
   1.973 +
   1.974 +  WidgetCompositionEvent event(true, NS_COMPOSITION_START, aWindow);
   1.975 +  aWindow->InitEvent(event, &point);
   1.976 +  aWindow->DispatchWindowEvent(&event);
   1.977 +
   1.978 +  mIsComposing = true;
   1.979 +  mComposingWindow = aWindow;
   1.980 +
   1.981 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
   1.982 +    ("IMM32: HandleStartComposition, START composition, mCompositionStart=%ld\n",
   1.983 +     mCompositionStart));
   1.984 +}
   1.985 +
   1.986 +bool
   1.987 +nsIMM32Handler::HandleComposition(nsWindow* aWindow,
   1.988 +                                  const nsIMEContext &aIMEContext,
   1.989 +                                  LPARAM lParam)
   1.990 +{
   1.991 +  NS_PRECONDITION(!aWindow->PluginHasFocus(),
   1.992 +    "HandleComposition should not be called when a plug-in has focus");
   1.993 +
   1.994 +  // for bug #60050
   1.995 +  // MS-IME 95/97/98/2000 may send WM_IME_COMPOSITION with non-conversion
   1.996 +  // mode before it send WM_IME_STARTCOMPOSITION.
   1.997 +  // However, ATOK sends a WM_IME_COMPOSITION before WM_IME_STARTCOMPOSITION,
   1.998 +  // and if we access ATOK via some APIs, ATOK will sometimes fail to
   1.999 +  // initialize its state.  If WM_IME_STARTCOMPOSITION is already in the
  1.1000 +  // message queue, we should ignore the strange WM_IME_COMPOSITION message and
  1.1001 +  // skip to the next.  So, we should look for next composition message
  1.1002 +  // (WM_IME_STARTCOMPOSITION or WM_IME_ENDCOMPOSITION or WM_IME_COMPOSITION),
  1.1003 +  // and if it's WM_IME_STARTCOMPOSITION, and one more next composition message
  1.1004 +  // is WM_IME_COMPOSITION, current IME is ATOK, probably.  Otherwise, we
  1.1005 +  // should start composition forcibly.
  1.1006 +  if (!mIsComposing) {
  1.1007 +    MSG msg1, msg2;
  1.1008 +    HWND wnd = aWindow->GetWindowHandle();
  1.1009 +    if (WinUtils::PeekMessage(&msg1, wnd, WM_IME_STARTCOMPOSITION,
  1.1010 +                              WM_IME_COMPOSITION, PM_NOREMOVE) &&
  1.1011 +        msg1.message == WM_IME_STARTCOMPOSITION &&
  1.1012 +        WinUtils::PeekMessage(&msg2, wnd, WM_IME_ENDCOMPOSITION,
  1.1013 +                              WM_IME_COMPOSITION, PM_NOREMOVE) &&
  1.1014 +        msg2.message == WM_IME_COMPOSITION) {
  1.1015 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1016 +        ("IMM32: HandleComposition, Ignores due to find a WM_IME_STARTCOMPOSITION\n"));
  1.1017 +      return ShouldDrawCompositionStringOurselves();
  1.1018 +    }
  1.1019 +  }
  1.1020 +
  1.1021 +  bool startCompositionMessageHasBeenSent = mIsComposing;
  1.1022 +
  1.1023 +  //
  1.1024 +  // This catches a fixed result
  1.1025 +  //
  1.1026 +  if (IS_COMMITTING_LPARAM(lParam)) {
  1.1027 +    if (!mIsComposing) {
  1.1028 +      HandleStartComposition(aWindow, aIMEContext);
  1.1029 +    }
  1.1030 +
  1.1031 +    GetCompositionString(aIMEContext, GCS_RESULTSTR);
  1.1032 +
  1.1033 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1034 +      ("IMM32: HandleComposition, GCS_RESULTSTR\n"));
  1.1035 +
  1.1036 +    DispatchTextEvent(aWindow, aIMEContext, false);
  1.1037 +    HandleEndComposition(aWindow);
  1.1038 +
  1.1039 +    if (!IS_COMPOSING_LPARAM(lParam)) {
  1.1040 +      return ShouldDrawCompositionStringOurselves();
  1.1041 +    }
  1.1042 +  }
  1.1043 +
  1.1044 +
  1.1045 +  //
  1.1046 +  // This provides us with a composition string
  1.1047 +  //
  1.1048 +  if (!mIsComposing) {
  1.1049 +    HandleStartComposition(aWindow, aIMEContext);
  1.1050 +  }
  1.1051 +
  1.1052 +  //--------------------------------------------------------
  1.1053 +  // 1. Get GCS_COMPSTR
  1.1054 +  //--------------------------------------------------------
  1.1055 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1056 +    ("IMM32: HandleComposition, GCS_COMPSTR\n"));
  1.1057 +
  1.1058 +  GetCompositionString(aIMEContext, GCS_COMPSTR);
  1.1059 +
  1.1060 +  if (!IS_COMPOSING_LPARAM(lParam)) {
  1.1061 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1062 +      ("IMM32: HandleComposition, lParam doesn't indicate composing, "
  1.1063 +       "mCompositionString=\"%s\", mLastDispatchedCompositionString=\"%s\"",
  1.1064 +       NS_ConvertUTF16toUTF8(mCompositionString).get(),
  1.1065 +       NS_ConvertUTF16toUTF8(mLastDispatchedCompositionString).get()));
  1.1066 +
  1.1067 +    // If composition string isn't changed, we can trust the lParam.
  1.1068 +    // So, we need to do nothing.
  1.1069 +    if (mLastDispatchedCompositionString == mCompositionString) {
  1.1070 +      return ShouldDrawCompositionStringOurselves();
  1.1071 +    }
  1.1072 +
  1.1073 +    // IME may send WM_IME_COMPOSITION without composing lParam values
  1.1074 +    // when composition string becomes empty (e.g., using Backspace key).
  1.1075 +    // If composition string is empty, we should dispatch a text event with
  1.1076 +    // empty string.
  1.1077 +    if (mCompositionString.IsEmpty()) {
  1.1078 +      DispatchTextEvent(aWindow, aIMEContext, false);
  1.1079 +      return ShouldDrawCompositionStringOurselves();
  1.1080 +    }
  1.1081 +
  1.1082 +    // Otherwise, we cannot trust the lParam value.  We might need to
  1.1083 +    // dispatch text event with the latest composition string information.
  1.1084 +  }
  1.1085 +
  1.1086 +  // See https://bugzilla.mozilla.org/show_bug.cgi?id=296339
  1.1087 +  if (mCompositionString.IsEmpty() && !startCompositionMessageHasBeenSent) {
  1.1088 +    // In this case, maybe, the sender is MSPinYin. That sends *only*
  1.1089 +    // WM_IME_COMPOSITION with GCS_COMP* and GCS_RESULT* when
  1.1090 +    // user inputted the Chinese full stop. So, that doesn't send
  1.1091 +    // WM_IME_STARTCOMPOSITION and WM_IME_ENDCOMPOSITION.
  1.1092 +    // If WM_IME_STARTCOMPOSITION was not sent and the composition
  1.1093 +    // string is null (it indicates the composition transaction ended),
  1.1094 +    // WM_IME_ENDCOMPOSITION may not be sent. If so, we cannot run
  1.1095 +    // HandleEndComposition() in other place.
  1.1096 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1097 +      ("IMM32: HandleComposition, Aborting GCS_COMPSTR\n"));
  1.1098 +    HandleEndComposition(aWindow);
  1.1099 +    return IS_COMMITTING_LPARAM(lParam);
  1.1100 +  }
  1.1101 +
  1.1102 +  //--------------------------------------------------------
  1.1103 +  // 2. Get GCS_COMPCLAUSE
  1.1104 +  //--------------------------------------------------------
  1.1105 +  long clauseArrayLength =
  1.1106 +    ::ImmGetCompositionStringW(aIMEContext.get(), GCS_COMPCLAUSE, nullptr, 0);
  1.1107 +  clauseArrayLength /= sizeof(uint32_t);
  1.1108 +
  1.1109 +  if (clauseArrayLength > 0) {
  1.1110 +    nsresult rv = EnsureClauseArray(clauseArrayLength);
  1.1111 +    NS_ENSURE_SUCCESS(rv, false);
  1.1112 +
  1.1113 +    // Intelligent ABC IME (Simplified Chinese IME, the code page is 936)
  1.1114 +    // will crash in ImmGetCompositionStringW for GCS_COMPCLAUSE (bug 424663).
  1.1115 +    // See comment 35 of the bug for the detail. Therefore, we should use A
  1.1116 +    // API for it, however, we should not kill Unicode support on all IMEs.
  1.1117 +    bool useA_API = !(sIMEProperty & IME_PROP_UNICODE);
  1.1118 +
  1.1119 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1120 +      ("IMM32: HandleComposition, GCS_COMPCLAUSE, useA_API=%s\n",
  1.1121 +       useA_API ? "TRUE" : "FALSE"));
  1.1122 +
  1.1123 +    long clauseArrayLength2 = 
  1.1124 +      useA_API ?
  1.1125 +        ::ImmGetCompositionStringA(aIMEContext.get(), GCS_COMPCLAUSE,
  1.1126 +                                   mClauseArray.Elements(),
  1.1127 +                                   mClauseArray.Capacity() * sizeof(uint32_t)) :
  1.1128 +        ::ImmGetCompositionStringW(aIMEContext.get(), GCS_COMPCLAUSE,
  1.1129 +                                   mClauseArray.Elements(),
  1.1130 +                                   mClauseArray.Capacity() * sizeof(uint32_t));
  1.1131 +    clauseArrayLength2 /= sizeof(uint32_t);
  1.1132 +
  1.1133 +    if (clauseArrayLength != clauseArrayLength2) {
  1.1134 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1135 +        ("IMM32: HandleComposition, GCS_COMPCLAUSE, clauseArrayLength=%ld but clauseArrayLength2=%ld\n",
  1.1136 +         clauseArrayLength, clauseArrayLength2));
  1.1137 +      if (clauseArrayLength > clauseArrayLength2)
  1.1138 +        clauseArrayLength = clauseArrayLength2;
  1.1139 +    }
  1.1140 +
  1.1141 +    if (useA_API) {
  1.1142 +      // Convert each values of sIMECompClauseArray. The values mean offset of
  1.1143 +      // the clauses in ANSI string. But we need the values in Unicode string.
  1.1144 +      nsAutoCString compANSIStr;
  1.1145 +      if (ConvertToANSIString(mCompositionString, GetKeyboardCodePage(),
  1.1146 +                              compANSIStr)) {
  1.1147 +        uint32_t maxlen = compANSIStr.Length();
  1.1148 +        mClauseArray[0] = 0; // first value must be 0
  1.1149 +        for (int32_t i = 1; i < clauseArrayLength; i++) {
  1.1150 +          uint32_t len = std::min(mClauseArray[i], maxlen);
  1.1151 +          mClauseArray[i] = ::MultiByteToWideChar(GetKeyboardCodePage(), 
  1.1152 +                                                  MB_PRECOMPOSED,
  1.1153 +                                                  (LPCSTR)compANSIStr.get(),
  1.1154 +                                                  len, nullptr, 0);
  1.1155 +        }
  1.1156 +      }
  1.1157 +    }
  1.1158 +  }
  1.1159 +  // compClauseArrayLength may be negative. I.e., ImmGetCompositionStringW
  1.1160 +  // may return an error code.
  1.1161 +  mClauseArray.SetLength(std::max<long>(0, clauseArrayLength));
  1.1162 +
  1.1163 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1164 +    ("IMM32: HandleComposition, GCS_COMPCLAUSE, mClauseLength=%ld\n",
  1.1165 +     mClauseArray.Length()));
  1.1166 +
  1.1167 +  //--------------------------------------------------------
  1.1168 +  // 3. Get GCS_COMPATTR
  1.1169 +  //--------------------------------------------------------
  1.1170 +  // This provides us with the attribute string necessary 
  1.1171 +  // for doing hiliting
  1.1172 +  long attrArrayLength =
  1.1173 +    ::ImmGetCompositionStringW(aIMEContext.get(), GCS_COMPATTR, nullptr, 0);
  1.1174 +  attrArrayLength /= sizeof(uint8_t);
  1.1175 +
  1.1176 +  if (attrArrayLength > 0) {
  1.1177 +    nsresult rv = EnsureAttributeArray(attrArrayLength);
  1.1178 +    NS_ENSURE_SUCCESS(rv, false);
  1.1179 +    attrArrayLength =
  1.1180 +      ::ImmGetCompositionStringW(aIMEContext.get(), GCS_COMPATTR,
  1.1181 +                                 mAttributeArray.Elements(),
  1.1182 +                                 mAttributeArray.Capacity() * sizeof(uint8_t));
  1.1183 +  }
  1.1184 +
  1.1185 +  // attrStrLen may be negative. I.e., ImmGetCompositionStringW may return an
  1.1186 +  // error code.
  1.1187 +  mAttributeArray.SetLength(std::max<long>(0, attrArrayLength));
  1.1188 +
  1.1189 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1190 +    ("IMM32: HandleComposition, GCS_COMPATTR, mAttributeLength=%ld\n",
  1.1191 +     mAttributeArray.Length()));
  1.1192 +
  1.1193 +  //--------------------------------------------------------
  1.1194 +  // 4. Get GCS_CURSOPOS
  1.1195 +  //--------------------------------------------------------
  1.1196 +  // Some IMEs (e.g., the standard IME for Korean) don't have caret position.
  1.1197 +  if (lParam & GCS_CURSORPOS) {
  1.1198 +    mCursorPosition =
  1.1199 +      ::ImmGetCompositionStringW(aIMEContext.get(), GCS_CURSORPOS, nullptr, 0);
  1.1200 +    if (mCursorPosition < 0) {
  1.1201 +      mCursorPosition = NO_IME_CARET; // The result is error
  1.1202 +    }
  1.1203 +  } else {
  1.1204 +    mCursorPosition = NO_IME_CARET;
  1.1205 +  }
  1.1206 +
  1.1207 +  NS_ASSERTION(mCursorPosition <= (long)mCompositionString.Length(),
  1.1208 +               "illegal pos");
  1.1209 +
  1.1210 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1211 +    ("IMM32: HandleComposition, GCS_CURSORPOS, mCursorPosition=%d\n",
  1.1212 +     mCursorPosition));
  1.1213 +
  1.1214 +  //--------------------------------------------------------
  1.1215 +  // 5. Send the text event
  1.1216 +  //--------------------------------------------------------
  1.1217 +  DispatchTextEvent(aWindow, aIMEContext);
  1.1218 +
  1.1219 +  return ShouldDrawCompositionStringOurselves();
  1.1220 +}
  1.1221 +
  1.1222 +void
  1.1223 +nsIMM32Handler::HandleEndComposition(nsWindow* aWindow)
  1.1224 +{
  1.1225 +  NS_PRECONDITION(mIsComposing,
  1.1226 +    "HandleEndComposition is called but mIsComposing is FALSE");
  1.1227 +  NS_PRECONDITION(!aWindow->PluginHasFocus(),
  1.1228 +    "HandleComposition should not be called when a plug-in has focus");
  1.1229 +
  1.1230 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1231 +    ("IMM32: HandleEndComposition\n"));
  1.1232 +
  1.1233 +  WidgetCompositionEvent event(true, NS_COMPOSITION_END, aWindow);
  1.1234 +  nsIntPoint point(0, 0);
  1.1235 +
  1.1236 +  if (mNativeCaretIsCreated) {
  1.1237 +    ::DestroyCaret();
  1.1238 +    mNativeCaretIsCreated = false;
  1.1239 +  }
  1.1240 +
  1.1241 +  aWindow->InitEvent(event, &point);
  1.1242 +  // The last dispatched composition string must be the committed string.
  1.1243 +  event.data = mLastDispatchedCompositionString;
  1.1244 +  aWindow->DispatchWindowEvent(&event);
  1.1245 +  mIsComposing = false;
  1.1246 +  mComposingWindow = nullptr;
  1.1247 +  mLastDispatchedCompositionString.Truncate();
  1.1248 +}
  1.1249 +
  1.1250 +static void
  1.1251 +DumpReconvertString(RECONVERTSTRING* aReconv)
  1.1252 +{
  1.1253 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1254 +    ("  dwSize=%ld, dwVersion=%ld, dwStrLen=%ld, dwStrOffset=%ld\n",
  1.1255 +     aReconv->dwSize, aReconv->dwVersion,
  1.1256 +     aReconv->dwStrLen, aReconv->dwStrOffset));
  1.1257 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1258 +    ("  dwCompStrLen=%ld, dwCompStrOffset=%ld, dwTargetStrLen=%ld, dwTargetStrOffset=%ld\n",
  1.1259 +     aReconv->dwCompStrLen, aReconv->dwCompStrOffset,
  1.1260 +     aReconv->dwTargetStrLen, aReconv->dwTargetStrOffset));
  1.1261 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1262 +    ("  result str=\"%s\"\n",
  1.1263 +     NS_ConvertUTF16toUTF8(
  1.1264 +       nsAutoString((char16_t*)((char*)(aReconv) + aReconv->dwStrOffset),
  1.1265 +                    aReconv->dwStrLen)).get()));
  1.1266 +}
  1.1267 +
  1.1268 +bool
  1.1269 +nsIMM32Handler::HandleReconvert(nsWindow* aWindow,
  1.1270 +                                LPARAM lParam,
  1.1271 +                                LRESULT *oResult)
  1.1272 +{
  1.1273 +  *oResult = 0;
  1.1274 +  RECONVERTSTRING* pReconv = reinterpret_cast<RECONVERTSTRING*>(lParam);
  1.1275 +
  1.1276 +  WidgetQueryContentEvent selection(true, NS_QUERY_SELECTED_TEXT, aWindow);
  1.1277 +  nsIntPoint point(0, 0);
  1.1278 +  aWindow->InitEvent(selection, &point);
  1.1279 +  aWindow->DispatchWindowEvent(&selection);
  1.1280 +  if (!selection.mSucceeded) {
  1.1281 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1282 +      ("IMM32: HandleReconvert, FAILED (NS_QUERY_SELECTED_TEXT)\n"));
  1.1283 +    return false;
  1.1284 +  }
  1.1285 +
  1.1286 +  uint32_t len = selection.mReply.mString.Length();
  1.1287 +  uint32_t needSize = sizeof(RECONVERTSTRING) + len * sizeof(WCHAR);
  1.1288 +
  1.1289 +  if (!pReconv) {
  1.1290 +    // Return need size to reconvert.
  1.1291 +    if (len == 0) {
  1.1292 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1293 +        ("IMM32: HandleReconvert, There are not selected text\n"));
  1.1294 +      return false;
  1.1295 +    }
  1.1296 +    *oResult = needSize;
  1.1297 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1298 +      ("IMM32: HandleReconvert, SUCCEEDED result=%ld\n",
  1.1299 +       *oResult));
  1.1300 +    return true;
  1.1301 +  }
  1.1302 +
  1.1303 +  if (pReconv->dwSize < needSize) {
  1.1304 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1305 +      ("IMM32: HandleReconvert, FAILED pReconv->dwSize=%ld, needSize=%ld\n",
  1.1306 +       pReconv->dwSize, needSize));
  1.1307 +    return false;
  1.1308 +  }
  1.1309 +
  1.1310 +  *oResult = needSize;
  1.1311 +
  1.1312 +  // Fill reconvert struct
  1.1313 +  pReconv->dwVersion         = 0;
  1.1314 +  pReconv->dwStrLen          = len;
  1.1315 +  pReconv->dwStrOffset       = sizeof(RECONVERTSTRING);
  1.1316 +  pReconv->dwCompStrLen      = len;
  1.1317 +  pReconv->dwCompStrOffset   = 0;
  1.1318 +  pReconv->dwTargetStrLen    = len;
  1.1319 +  pReconv->dwTargetStrOffset = 0;
  1.1320 +
  1.1321 +  ::CopyMemory(reinterpret_cast<LPVOID>(lParam + sizeof(RECONVERTSTRING)),
  1.1322 +               selection.mReply.mString.get(), len * sizeof(WCHAR));
  1.1323 +
  1.1324 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1325 +    ("IMM32: HandleReconvert, SUCCEEDED result=%ld\n",
  1.1326 +     *oResult));
  1.1327 +  DumpReconvertString(pReconv);
  1.1328 +
  1.1329 +  return true;
  1.1330 +}
  1.1331 +
  1.1332 +bool
  1.1333 +nsIMM32Handler::HandleQueryCharPosition(nsWindow* aWindow,
  1.1334 +                                        LPARAM lParam,
  1.1335 +                                        LRESULT *oResult)
  1.1336 +{
  1.1337 +  uint32_t len = mIsComposing ? mCompositionString.Length() : 0;
  1.1338 +  *oResult = false;
  1.1339 +  IMECHARPOSITION* pCharPosition = reinterpret_cast<IMECHARPOSITION*>(lParam);
  1.1340 +  if (!pCharPosition) {
  1.1341 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1342 +      ("IMM32: HandleQueryCharPosition, FAILED (pCharPosition is null)\n"));
  1.1343 +    return false;
  1.1344 +  }
  1.1345 +  if (pCharPosition->dwSize < sizeof(IMECHARPOSITION)) {
  1.1346 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1347 +      ("IMM32: HandleReconvert, FAILED, pCharPosition->dwSize=%ld, sizeof(IMECHARPOSITION)=%ld\n",
  1.1348 +       pCharPosition->dwSize, sizeof(IMECHARPOSITION)));
  1.1349 +    return false;
  1.1350 +  }
  1.1351 +  if (::GetFocus() != aWindow->GetWindowHandle()) {
  1.1352 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1353 +      ("IMM32: HandleReconvert, FAILED, ::GetFocus()=%08x, OurWindowHandle=%08x\n",
  1.1354 +       ::GetFocus(), aWindow->GetWindowHandle()));
  1.1355 +    return false;
  1.1356 +  }
  1.1357 +  if (pCharPosition->dwCharPos > len) {
  1.1358 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1359 +      ("IMM32: HandleQueryCharPosition, FAILED, pCharPosition->dwCharPos=%ld, len=%ld\n",
  1.1360 +      pCharPosition->dwCharPos, len));
  1.1361 +    return false;
  1.1362 +  }
  1.1363 +
  1.1364 +  nsIntRect r;
  1.1365 +  bool ret =
  1.1366 +    GetCharacterRectOfSelectedTextAt(aWindow, pCharPosition->dwCharPos, r);
  1.1367 +  NS_ENSURE_TRUE(ret, false);
  1.1368 +
  1.1369 +  nsIntRect screenRect;
  1.1370 +  // We always need top level window that is owner window of the popup window
  1.1371 +  // even if the content of the popup window has focus.
  1.1372 +  ResolveIMECaretPos(aWindow->GetTopLevelWindow(false),
  1.1373 +                     r, nullptr, screenRect);
  1.1374 +  pCharPosition->pt.x = screenRect.x;
  1.1375 +  pCharPosition->pt.y = screenRect.y;
  1.1376 +
  1.1377 +  pCharPosition->cLineHeight = r.height;
  1.1378 +
  1.1379 +  // XXX we should use NS_QUERY_EDITOR_RECT event here.
  1.1380 +  ::GetWindowRect(aWindow->GetWindowHandle(), &pCharPosition->rcDocument);
  1.1381 +
  1.1382 +  *oResult = TRUE;
  1.1383 +
  1.1384 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1385 +    ("IMM32: HandleQueryCharPosition, SUCCEEDED\n"));
  1.1386 +  return true;
  1.1387 +}
  1.1388 +
  1.1389 +bool
  1.1390 +nsIMM32Handler::HandleDocumentFeed(nsWindow* aWindow,
  1.1391 +                                   LPARAM lParam,
  1.1392 +                                   LRESULT *oResult)
  1.1393 +{
  1.1394 +  *oResult = 0;
  1.1395 +  RECONVERTSTRING* pReconv = reinterpret_cast<RECONVERTSTRING*>(lParam);
  1.1396 +
  1.1397 +  nsIntPoint point(0, 0);
  1.1398 +
  1.1399 +  bool hasCompositionString =
  1.1400 +    mIsComposing && ShouldDrawCompositionStringOurselves();
  1.1401 +
  1.1402 +  int32_t targetOffset, targetLength;
  1.1403 +  if (!hasCompositionString) {
  1.1404 +    WidgetQueryContentEvent selection(true, NS_QUERY_SELECTED_TEXT, aWindow);
  1.1405 +    aWindow->InitEvent(selection, &point);
  1.1406 +    aWindow->DispatchWindowEvent(&selection);
  1.1407 +    if (!selection.mSucceeded) {
  1.1408 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1409 +        ("IMM32: HandleDocumentFeed, FAILED (NS_QUERY_SELECTED_TEXT)\n"));
  1.1410 +      return false;
  1.1411 +    }
  1.1412 +    targetOffset = int32_t(selection.mReply.mOffset);
  1.1413 +    targetLength = int32_t(selection.mReply.mString.Length());
  1.1414 +  } else {
  1.1415 +    targetOffset = int32_t(mCompositionStart);
  1.1416 +    targetLength = int32_t(mCompositionString.Length());
  1.1417 +  }
  1.1418 +
  1.1419 +  // XXX nsString::Find and nsString::RFind take int32_t for offset, so,
  1.1420 +  //     we cannot support this message when the current offset is larger than
  1.1421 +  //     INT32_MAX.
  1.1422 +  if (targetOffset < 0 || targetLength < 0 ||
  1.1423 +      targetOffset + targetLength < 0) {
  1.1424 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1425 +      ("IMM32: HandleDocumentFeed, FAILED (The selection is out of range)\n"));
  1.1426 +    return false;
  1.1427 +  }
  1.1428 +
  1.1429 +  // Get all contents of the focused editor.
  1.1430 +  WidgetQueryContentEvent textContent(true, NS_QUERY_TEXT_CONTENT, aWindow);
  1.1431 +  textContent.InitForQueryTextContent(0, UINT32_MAX);
  1.1432 +  aWindow->InitEvent(textContent, &point);
  1.1433 +  aWindow->DispatchWindowEvent(&textContent);
  1.1434 +  if (!textContent.mSucceeded) {
  1.1435 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1436 +      ("IMM32: HandleDocumentFeed, FAILED (NS_QUERY_TEXT_CONTENT)\n"));
  1.1437 +    return false;
  1.1438 +  }
  1.1439 +
  1.1440 +  nsAutoString str(textContent.mReply.mString);
  1.1441 +  if (targetOffset > int32_t(str.Length())) {
  1.1442 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1443 +      ("IMM32: HandleDocumentFeed, FAILED (The caret offset is invalid)\n"));
  1.1444 +    return false;
  1.1445 +  }
  1.1446 +
  1.1447 +  // Get the focused paragraph, we decide that it starts from the previous CRLF
  1.1448 +  // (or start of the editor) to the next one (or the end of the editor).
  1.1449 +  int32_t paragraphStart = str.RFind("\n", false, targetOffset, -1) + 1;
  1.1450 +  int32_t paragraphEnd =
  1.1451 +    str.Find("\r", false, targetOffset + targetLength, -1);
  1.1452 +  if (paragraphEnd < 0) {
  1.1453 +    paragraphEnd = str.Length();
  1.1454 +  }
  1.1455 +  nsDependentSubstring paragraph(str, paragraphStart,
  1.1456 +                                 paragraphEnd - paragraphStart);
  1.1457 +
  1.1458 +  uint32_t len = paragraph.Length();
  1.1459 +  uint32_t needSize = sizeof(RECONVERTSTRING) + len * sizeof(WCHAR);
  1.1460 +
  1.1461 +  if (!pReconv) {
  1.1462 +    *oResult = needSize;
  1.1463 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1464 +      ("IMM32: HandleDocumentFeed, SUCCEEDED result=%ld\n",
  1.1465 +       *oResult));
  1.1466 +    return true;
  1.1467 +  }
  1.1468 +
  1.1469 +  if (pReconv->dwSize < needSize) {
  1.1470 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1471 +      ("IMM32: HandleDocumentFeed, FAILED pReconv->dwSize=%ld, needSize=%ld\n",
  1.1472 +       pReconv->dwSize, needSize));
  1.1473 +    return false;
  1.1474 +  }
  1.1475 +
  1.1476 +  // Fill reconvert struct
  1.1477 +  pReconv->dwVersion         = 0;
  1.1478 +  pReconv->dwStrLen          = len;
  1.1479 +  pReconv->dwStrOffset       = sizeof(RECONVERTSTRING);
  1.1480 +  if (hasCompositionString) {
  1.1481 +    pReconv->dwCompStrLen      = targetLength;
  1.1482 +    pReconv->dwCompStrOffset   =
  1.1483 +      (targetOffset - paragraphStart) * sizeof(WCHAR);
  1.1484 +    // Set composition target clause information
  1.1485 +    uint32_t offset, length;
  1.1486 +    if (!GetTargetClauseRange(&offset, &length)) {
  1.1487 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1488 +        ("IMM32: HandleDocumentFeed, FAILED, by GetTargetClauseRange\n"));
  1.1489 +      return false;
  1.1490 +    }
  1.1491 +    pReconv->dwTargetStrLen    = length;
  1.1492 +    pReconv->dwTargetStrOffset = (offset - paragraphStart) * sizeof(WCHAR);
  1.1493 +  } else {
  1.1494 +    pReconv->dwTargetStrLen    = targetLength;
  1.1495 +    pReconv->dwTargetStrOffset =
  1.1496 +      (targetOffset - paragraphStart) * sizeof(WCHAR);
  1.1497 +    // There is no composition string, so, the length is zero but we should
  1.1498 +    // set the cursor offset to the composition str offset.
  1.1499 +    pReconv->dwCompStrLen      = 0;
  1.1500 +    pReconv->dwCompStrOffset   = pReconv->dwTargetStrOffset;
  1.1501 +  }
  1.1502 +
  1.1503 +  *oResult = needSize;
  1.1504 +  ::CopyMemory(reinterpret_cast<LPVOID>(lParam + sizeof(RECONVERTSTRING)),
  1.1505 +               paragraph.BeginReading(), len * sizeof(WCHAR));
  1.1506 +
  1.1507 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1508 +    ("IMM32: HandleDocumentFeed, SUCCEEDED result=%ld\n",
  1.1509 +     *oResult));
  1.1510 +  DumpReconvertString(pReconv);
  1.1511 +
  1.1512 +  return true;
  1.1513 +}
  1.1514 +
  1.1515 +bool
  1.1516 +nsIMM32Handler::CommitCompositionOnPreviousWindow(nsWindow* aWindow)
  1.1517 +{
  1.1518 +  if (!mComposingWindow || mComposingWindow == aWindow) {
  1.1519 +    return false;
  1.1520 +  }
  1.1521 +
  1.1522 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1523 +    ("IMM32: CommitCompositionOnPreviousWindow, mIsComposing=%s, mIsComposingOnPlugin=%s\n",
  1.1524 +     mIsComposing ? "TRUE" : "FALSE", mIsComposingOnPlugin ? "TRUE" : "FALSE"));
  1.1525 +
  1.1526 +  // If we have composition, we should dispatch composition events internally.
  1.1527 +  if (mIsComposing) {
  1.1528 +    nsIMEContext IMEContext(mComposingWindow->GetWindowHandle());
  1.1529 +    NS_ASSERTION(IMEContext.IsValid(), "IME context must be valid");
  1.1530 +
  1.1531 +    DispatchTextEvent(mComposingWindow, IMEContext, false);
  1.1532 +    HandleEndComposition(mComposingWindow);
  1.1533 +    return true;
  1.1534 +  }
  1.1535 +
  1.1536 +  // XXX When plug-in has composition, we should commit composition on the
  1.1537 +  // plug-in.  However, we need some more work for that.
  1.1538 +  return mIsComposingOnPlugin;
  1.1539 +}
  1.1540 +
  1.1541 +static uint32_t
  1.1542 +PlatformToNSAttr(uint8_t aAttr)
  1.1543 +{
  1.1544 +  switch (aAttr)
  1.1545 +  {
  1.1546 +    case ATTR_INPUT_ERROR:
  1.1547 +    // case ATTR_FIXEDCONVERTED:
  1.1548 +    case ATTR_INPUT:
  1.1549 +      return NS_TEXTRANGE_RAWINPUT;
  1.1550 +    case ATTR_CONVERTED:
  1.1551 +      return NS_TEXTRANGE_CONVERTEDTEXT;
  1.1552 +    case ATTR_TARGET_NOTCONVERTED:
  1.1553 +      return NS_TEXTRANGE_SELECTEDRAWTEXT;
  1.1554 +    case ATTR_TARGET_CONVERTED:
  1.1555 +      return NS_TEXTRANGE_SELECTEDCONVERTEDTEXT;
  1.1556 +    default:
  1.1557 +      NS_ASSERTION(false, "unknown attribute");
  1.1558 +      return NS_TEXTRANGE_CARETPOSITION;
  1.1559 +  }
  1.1560 +}
  1.1561 +
  1.1562 +#ifdef PR_LOGGING
  1.1563 +static const char*
  1.1564 +GetRangeTypeName(uint32_t aRangeType)
  1.1565 +{
  1.1566 +  switch (aRangeType) {
  1.1567 +    case NS_TEXTRANGE_RAWINPUT:
  1.1568 +      return "NS_TEXTRANGE_RAWINPUT";
  1.1569 +    case NS_TEXTRANGE_CONVERTEDTEXT:
  1.1570 +      return "NS_TEXTRANGE_CONVERTEDTEXT";
  1.1571 +    case NS_TEXTRANGE_SELECTEDRAWTEXT:
  1.1572 +      return "NS_TEXTRANGE_SELECTEDRAWTEXT";
  1.1573 +    case NS_TEXTRANGE_SELECTEDCONVERTEDTEXT:
  1.1574 +      return "NS_TEXTRANGE_SELECTEDCONVERTEDTEXT";
  1.1575 +    case NS_TEXTRANGE_CARETPOSITION:
  1.1576 +      return "NS_TEXTRANGE_CARETPOSITION";
  1.1577 +    default:
  1.1578 +      return "UNKNOWN SELECTION TYPE!!";
  1.1579 +  }
  1.1580 +}
  1.1581 +#endif
  1.1582 +
  1.1583 +void
  1.1584 +nsIMM32Handler::DispatchTextEvent(nsWindow* aWindow,
  1.1585 +                                  const nsIMEContext &aIMEContext,
  1.1586 +                                  bool aCheckAttr)
  1.1587 +{
  1.1588 +  NS_ASSERTION(mIsComposing, "conflict state");
  1.1589 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1590 +    ("IMM32: DispatchTextEvent, aCheckAttr=%s\n",
  1.1591 +     aCheckAttr ? "TRUE": "FALSE"));
  1.1592 +
  1.1593 +  // If we don't need to draw composition string ourselves and this is not
  1.1594 +  // commit event (i.e., under composing), we don't need to fire text event
  1.1595 +  // during composing.
  1.1596 +  if (aCheckAttr && !ShouldDrawCompositionStringOurselves()) {
  1.1597 +    // But we need to adjust composition window pos and native caret pos, here.
  1.1598 +    SetIMERelatedWindowsPos(aWindow, aIMEContext);
  1.1599 +    return;
  1.1600 +  }
  1.1601 +
  1.1602 +  nsRefPtr<nsWindow> kungFuDeathGrip(aWindow);
  1.1603 +
  1.1604 +  nsIntPoint point(0, 0);
  1.1605 +
  1.1606 +  if (mCompositionString != mLastDispatchedCompositionString) {
  1.1607 +    WidgetCompositionEvent compositionUpdate(true, NS_COMPOSITION_UPDATE,
  1.1608 +                                             aWindow);
  1.1609 +    aWindow->InitEvent(compositionUpdate, &point);
  1.1610 +    compositionUpdate.data = mCompositionString;
  1.1611 +    mLastDispatchedCompositionString = mCompositionString;
  1.1612 +
  1.1613 +    aWindow->DispatchWindowEvent(&compositionUpdate);
  1.1614 +
  1.1615 +    if (!mIsComposing || aWindow->Destroyed()) {
  1.1616 +      return;
  1.1617 +    }
  1.1618 +    SetIMERelatedWindowsPos(aWindow, aIMEContext);
  1.1619 +  }
  1.1620 +
  1.1621 +  WidgetTextEvent event(true, NS_TEXT_TEXT, aWindow);
  1.1622 +
  1.1623 +  aWindow->InitEvent(event, &point);
  1.1624 +
  1.1625 +  if (aCheckAttr) {
  1.1626 +    event.mRanges = CreateTextRangeArray();
  1.1627 +  }
  1.1628 +
  1.1629 +  event.theText = mCompositionString.get();
  1.1630 +
  1.1631 +  aWindow->DispatchWindowEvent(&event);
  1.1632 +
  1.1633 +  // Calling SetIMERelatedWindowsPos will be failure on e10s at this point.
  1.1634 +  // text event will notify NOTIFY_IME_OF_COMPOSITION_UPDATE, then
  1.1635 +  // it will call SetIMERelatedWindowsPos.
  1.1636 +}
  1.1637 +
  1.1638 +already_AddRefed<TextRangeArray>
  1.1639 +nsIMM32Handler::CreateTextRangeArray()
  1.1640 +{
  1.1641 +  // Sogou (Simplified Chinese IME) returns contradictory values: The cursor
  1.1642 +  // position is actual cursor position. However, other values (composition
  1.1643 +  // string and attributes) are empty. So, if you want to remove following
  1.1644 +  // assertion, be careful.
  1.1645 +  NS_ASSERTION(ShouldDrawCompositionStringOurselves(),
  1.1646 +    "CreateTextRangeArray is called when we don't need to fire text event");
  1.1647 +
  1.1648 +  nsRefPtr<TextRangeArray> textRangeArray = new TextRangeArray();
  1.1649 +
  1.1650 +  TextRange range;
  1.1651 +  if (mClauseArray.Length() == 0) {
  1.1652 +    // Some IMEs don't return clause array information, then, we assume that
  1.1653 +    // all characters in the composition string are in one clause.
  1.1654 +    range.mStartOffset = 0;
  1.1655 +    range.mEndOffset = mCompositionString.Length();
  1.1656 +    range.mRangeType = NS_TEXTRANGE_RAWINPUT;
  1.1657 +    textRangeArray->AppendElement(range);
  1.1658 +
  1.1659 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1660 +      ("IMM32: CreateTextRangeArray, mClauseLength=0\n"));
  1.1661 +  } else {
  1.1662 +    // iterate over the attributes
  1.1663 +    uint32_t lastOffset = 0;
  1.1664 +    for (uint32_t i = 0; i < mClauseArray.Length() - 1; i++) {
  1.1665 +      uint32_t current = mClauseArray[i + 1];
  1.1666 +      if (current > mCompositionString.Length()) {
  1.1667 +        PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1668 +          ("IMM32: CreateTextRangeArray, mClauseArray[%ld]=%lu. "
  1.1669 +           "This is larger than mCompositionString.Length()=%lu\n",
  1.1670 +           i + 1, current, mCompositionString.Length()));
  1.1671 +        current = int32_t(mCompositionString.Length());
  1.1672 +      }
  1.1673 +
  1.1674 +      range.mRangeType = PlatformToNSAttr(mAttributeArray[lastOffset]);
  1.1675 +      range.mStartOffset = lastOffset;
  1.1676 +      range.mEndOffset = current;
  1.1677 +      textRangeArray->AppendElement(range);
  1.1678 +
  1.1679 +      lastOffset = current;
  1.1680 +
  1.1681 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1682 +        ("IMM32: CreateTextRangeArray, index=%ld, rangeType=%s, range=[%lu-%lu]\n",
  1.1683 +         i, GetRangeTypeName(range.mRangeType), range.mStartOffset,
  1.1684 +         range.mEndOffset));
  1.1685 +    }
  1.1686 +  }
  1.1687 +
  1.1688 +  if (mCursorPosition == NO_IME_CARET) {
  1.1689 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1690 +      ("IMM32: CreateTextRangeArray, no caret\n"));
  1.1691 +    return textRangeArray.forget();
  1.1692 +  }
  1.1693 +
  1.1694 +  int32_t cursor = mCursorPosition;
  1.1695 +  if (uint32_t(cursor) > mCompositionString.Length()) {
  1.1696 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1697 +      ("IMM32: CreateTextRangeArray, mCursorPosition=%ld. "
  1.1698 +       "This is larger than mCompositionString.Length()=%lu\n",
  1.1699 +       mCursorPosition, mCompositionString.Length()));
  1.1700 +    cursor = mCompositionString.Length();
  1.1701 +  }
  1.1702 +
  1.1703 +  range.mStartOffset = range.mEndOffset = cursor;
  1.1704 +  range.mRangeType = NS_TEXTRANGE_CARETPOSITION;
  1.1705 +  textRangeArray->AppendElement(range);
  1.1706 +
  1.1707 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1708 +    ("IMM32: CreateTextRangeArray, caret position=%ld\n",
  1.1709 +     range.mStartOffset));
  1.1710 +
  1.1711 +  return textRangeArray.forget();
  1.1712 +}
  1.1713 +
  1.1714 +void
  1.1715 +nsIMM32Handler::GetCompositionString(const nsIMEContext &aIMEContext,
  1.1716 +                                     DWORD aIndex)
  1.1717 +{
  1.1718 +  // Retrieve the size of the required output buffer.
  1.1719 +  long lRtn = ::ImmGetCompositionStringW(aIMEContext.get(), aIndex, nullptr, 0);
  1.1720 +  if (lRtn < 0 ||
  1.1721 +      !mCompositionString.SetLength((lRtn / sizeof(WCHAR)) + 1, mozilla::fallible_t())) {
  1.1722 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1723 +      ("IMM32: GetCompositionString, FAILED by OOM\n"));
  1.1724 +    return; // Error or out of memory.
  1.1725 +  }
  1.1726 +
  1.1727 +  // Actually retrieve the composition string information.
  1.1728 +  lRtn = ::ImmGetCompositionStringW(aIMEContext.get(), aIndex,
  1.1729 +                                    (LPVOID)mCompositionString.BeginWriting(),
  1.1730 +                                    lRtn + sizeof(WCHAR));
  1.1731 +  mCompositionString.SetLength(lRtn / sizeof(WCHAR));
  1.1732 +
  1.1733 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1734 +    ("IMM32: GetCompositionString, SUCCEEDED mCompositionString=\"%s\"\n",
  1.1735 +     NS_ConvertUTF16toUTF8(mCompositionString).get()));
  1.1736 +}
  1.1737 +
  1.1738 +bool
  1.1739 +nsIMM32Handler::GetTargetClauseRange(uint32_t *aOffset, uint32_t *aLength)
  1.1740 +{
  1.1741 +  NS_ENSURE_TRUE(aOffset, false);
  1.1742 +  NS_ENSURE_TRUE(mIsComposing, false);
  1.1743 +  NS_ENSURE_TRUE(ShouldDrawCompositionStringOurselves(), false);
  1.1744 +
  1.1745 +  bool found = false;
  1.1746 +  *aOffset = mCompositionStart;
  1.1747 +  for (uint32_t i = 0; i < mAttributeArray.Length(); i++) {
  1.1748 +    if (mAttributeArray[i] == ATTR_TARGET_NOTCONVERTED ||
  1.1749 +        mAttributeArray[i] == ATTR_TARGET_CONVERTED) {
  1.1750 +      *aOffset = mCompositionStart + i;
  1.1751 +      found = true;
  1.1752 +      break;
  1.1753 +    }
  1.1754 +  }
  1.1755 +
  1.1756 +  if (!aLength) {
  1.1757 +    return true;
  1.1758 +  }
  1.1759 +
  1.1760 +  if (!found) {
  1.1761 +    // The all composition string is targetted when there is no ATTR_TARGET_*
  1.1762 +    // clause. E.g., there is only ATTR_INPUT
  1.1763 +    *aLength = mCompositionString.Length();
  1.1764 +    return true;
  1.1765 +  }
  1.1766 +
  1.1767 +  uint32_t offsetInComposition = *aOffset - mCompositionStart;
  1.1768 +  *aLength = mCompositionString.Length() - offsetInComposition;
  1.1769 +  for (uint32_t i = offsetInComposition; i < mAttributeArray.Length(); i++) {
  1.1770 +    if (mAttributeArray[i] != ATTR_TARGET_NOTCONVERTED &&
  1.1771 +        mAttributeArray[i] != ATTR_TARGET_CONVERTED) {
  1.1772 +      *aLength = i - offsetInComposition;
  1.1773 +      break;
  1.1774 +    }
  1.1775 +  }
  1.1776 +  return true;
  1.1777 +}
  1.1778 +
  1.1779 +bool
  1.1780 +nsIMM32Handler::ConvertToANSIString(const nsAFlatString& aStr, UINT aCodePage,
  1.1781 +                                   nsACString& aANSIStr)
  1.1782 +{
  1.1783 +  int len = ::WideCharToMultiByte(aCodePage, 0,
  1.1784 +                                  (LPCWSTR)aStr.get(), aStr.Length(),
  1.1785 +                                  nullptr, 0, nullptr, nullptr);
  1.1786 +  NS_ENSURE_TRUE(len >= 0, false);
  1.1787 +
  1.1788 +  if (!aANSIStr.SetLength(len, mozilla::fallible_t())) {
  1.1789 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1790 +      ("IMM32: ConvertToANSIString, FAILED by OOM\n"));
  1.1791 +    return false;
  1.1792 +  }
  1.1793 +  ::WideCharToMultiByte(aCodePage, 0, (LPCWSTR)aStr.get(), aStr.Length(),
  1.1794 +                        (LPSTR)aANSIStr.BeginWriting(), len, nullptr, nullptr);
  1.1795 +  return true;
  1.1796 +}
  1.1797 +
  1.1798 +bool
  1.1799 +nsIMM32Handler::GetCharacterRectOfSelectedTextAt(nsWindow* aWindow,
  1.1800 +                                                 uint32_t aOffset,
  1.1801 +                                                 nsIntRect &aCharRect)
  1.1802 +{
  1.1803 +  nsIntPoint point(0, 0);
  1.1804 +
  1.1805 +  WidgetQueryContentEvent selection(true, NS_QUERY_SELECTED_TEXT, aWindow);
  1.1806 +  aWindow->InitEvent(selection, &point);
  1.1807 +  aWindow->DispatchWindowEvent(&selection);
  1.1808 +  if (!selection.mSucceeded) {
  1.1809 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1810 +      ("IMM32: GetCharacterRectOfSelectedTextAt, aOffset=%lu, FAILED (NS_QUERY_SELECTED_TEXT)\n",
  1.1811 +       aOffset));
  1.1812 +    return false;
  1.1813 +  }
  1.1814 +
  1.1815 +  uint32_t offset = selection.mReply.mOffset + aOffset;
  1.1816 +  bool useCaretRect = selection.mReply.mString.IsEmpty();
  1.1817 +  if (useCaretRect && ShouldDrawCompositionStringOurselves() &&
  1.1818 +      mIsComposing && !mCompositionString.IsEmpty()) {
  1.1819 +    // There is not a normal selection, but we have composition string.
  1.1820 +    // XXX mnakano - Should we implement NS_QUERY_IME_SELECTED_TEXT?
  1.1821 +    useCaretRect = false;
  1.1822 +    if (mCursorPosition != NO_IME_CARET) {
  1.1823 +      uint32_t cursorPosition =
  1.1824 +        std::min<uint32_t>(mCursorPosition, mCompositionString.Length());
  1.1825 +      NS_ASSERTION(offset >= cursorPosition, "offset is less than cursorPosition!");
  1.1826 +      offset -= cursorPosition;
  1.1827 +    }
  1.1828 +  }
  1.1829 +
  1.1830 +  nsIntRect r;
  1.1831 +  if (!useCaretRect) {
  1.1832 +    WidgetQueryContentEvent charRect(true, NS_QUERY_TEXT_RECT, aWindow);
  1.1833 +    charRect.InitForQueryTextRect(offset, 1);
  1.1834 +    aWindow->InitEvent(charRect, &point);
  1.1835 +    aWindow->DispatchWindowEvent(&charRect);
  1.1836 +    if (charRect.mSucceeded) {
  1.1837 +      aCharRect = charRect.mReply.mRect;
  1.1838 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1839 +        ("IMM32: GetCharacterRectOfSelectedTextAt, aOffset=%lu, SUCCEEDED\n",
  1.1840 +         aOffset));
  1.1841 +      PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1842 +        ("IMM32: GetCharacterRectOfSelectedTextAt, aCharRect={ x: %ld, y: %ld, width: %ld, height: %ld }\n",
  1.1843 +         aCharRect.x, aCharRect.y, aCharRect.width, aCharRect.height));
  1.1844 +      return true;
  1.1845 +    }
  1.1846 +  }
  1.1847 +
  1.1848 +  return GetCaretRect(aWindow, aCharRect);
  1.1849 +}
  1.1850 +
  1.1851 +bool
  1.1852 +nsIMM32Handler::GetCaretRect(nsWindow* aWindow, nsIntRect &aCaretRect)
  1.1853 +{
  1.1854 +  nsIntPoint point(0, 0);
  1.1855 +
  1.1856 +  WidgetQueryContentEvent selection(true, NS_QUERY_SELECTED_TEXT, aWindow);
  1.1857 +  aWindow->InitEvent(selection, &point);
  1.1858 +  aWindow->DispatchWindowEvent(&selection);
  1.1859 +  if (!selection.mSucceeded) {
  1.1860 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1861 +      ("IMM32: GetCaretRect,  FAILED (NS_QUERY_SELECTED_TEXT)\n"));
  1.1862 +    return false;
  1.1863 +  }
  1.1864 +
  1.1865 +  uint32_t offset = selection.mReply.mOffset;
  1.1866 +
  1.1867 +  WidgetQueryContentEvent caretRect(true, NS_QUERY_CARET_RECT, aWindow);
  1.1868 +  caretRect.InitForQueryCaretRect(offset);
  1.1869 +  aWindow->InitEvent(caretRect, &point);
  1.1870 +  aWindow->DispatchWindowEvent(&caretRect);
  1.1871 +  if (!caretRect.mSucceeded) {
  1.1872 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1873 +      ("IMM32: GetCaretRect,  FAILED (NS_QUERY_CARET_RECT)\n"));
  1.1874 +    return false;
  1.1875 +  }
  1.1876 +  aCaretRect = caretRect.mReply.mRect;
  1.1877 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1878 +    ("IMM32: GetCaretRect, SUCCEEDED, aCaretRect={ x: %ld, y: %ld, width: %ld, height: %ld }\n",
  1.1879 +     aCaretRect.x, aCaretRect.y, aCaretRect.width, aCaretRect.height));
  1.1880 +  return true;
  1.1881 +}
  1.1882 +
  1.1883 +bool
  1.1884 +nsIMM32Handler::SetIMERelatedWindowsPos(nsWindow* aWindow,
  1.1885 +                                        const nsIMEContext &aIMEContext)
  1.1886 +{
  1.1887 +  nsIntRect r;
  1.1888 +  // Get first character rect of current a normal selected text or a composing
  1.1889 +  // string.
  1.1890 +  bool ret = GetCharacterRectOfSelectedTextAt(aWindow, 0, r);
  1.1891 +  NS_ENSURE_TRUE(ret, false);
  1.1892 +  nsWindow* toplevelWindow = aWindow->GetTopLevelWindow(false);
  1.1893 +  nsIntRect firstSelectedCharRect;
  1.1894 +  ResolveIMECaretPos(toplevelWindow, r, aWindow, firstSelectedCharRect);
  1.1895 +
  1.1896 +  // Set native caret size/position to our caret. Some IMEs honor it. E.g.,
  1.1897 +  // "Intelligent ABC" (Simplified Chinese) and "MS PinYin 3.0" (Simplified
  1.1898 +  // Chinese) on XP.
  1.1899 +  nsIntRect caretRect(firstSelectedCharRect);
  1.1900 +  if (GetCaretRect(aWindow, r)) {
  1.1901 +    ResolveIMECaretPos(toplevelWindow, r, aWindow, caretRect);
  1.1902 +  } else {
  1.1903 +    NS_WARNING("failed to get caret rect");
  1.1904 +    caretRect.width = 1;
  1.1905 +  }
  1.1906 +  if (!mNativeCaretIsCreated) {
  1.1907 +    mNativeCaretIsCreated = ::CreateCaret(aWindow->GetWindowHandle(), nullptr,
  1.1908 +                                          caretRect.width, caretRect.height);
  1.1909 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1910 +      ("IMM32: SetIMERelatedWindowsPos, mNativeCaretIsCreated=%s, width=%ld height=%ld\n",
  1.1911 +       mNativeCaretIsCreated ? "TRUE" : "FALSE",
  1.1912 +       caretRect.width, caretRect.height));
  1.1913 +  }
  1.1914 +  ::SetCaretPos(caretRect.x, caretRect.y);
  1.1915 +
  1.1916 +  if (ShouldDrawCompositionStringOurselves()) {
  1.1917 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1918 +      ("IMM32: SetIMERelatedWindowsPos, Set candidate window\n"));
  1.1919 +
  1.1920 +    // Get a rect of first character in current target in composition string.
  1.1921 +    if (mIsComposing && !mCompositionString.IsEmpty()) {
  1.1922 +      // If there are no targetted selection, we should use it's first character
  1.1923 +      // rect instead.
  1.1924 +      uint32_t offset;
  1.1925 +      if (!GetTargetClauseRange(&offset)) {
  1.1926 +        PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1927 +          ("IMM32: SetIMERelatedWindowsPos, FAILED, by GetTargetClauseRange\n"));
  1.1928 +        return false;
  1.1929 +      }
  1.1930 +      ret = GetCharacterRectOfSelectedTextAt(aWindow,
  1.1931 +                                             offset - mCompositionStart, r);
  1.1932 +      NS_ENSURE_TRUE(ret, false);
  1.1933 +    } else {
  1.1934 +      // If there are no composition string, we should use a first character
  1.1935 +      // rect.
  1.1936 +      ret = GetCharacterRectOfSelectedTextAt(aWindow, 0, r);
  1.1937 +      NS_ENSURE_TRUE(ret, false);
  1.1938 +    }
  1.1939 +    nsIntRect firstTargetCharRect;
  1.1940 +    ResolveIMECaretPos(toplevelWindow, r, aWindow, firstTargetCharRect);
  1.1941 +
  1.1942 +    // Move the candidate window to first character position of the target.
  1.1943 +    CANDIDATEFORM candForm;
  1.1944 +    candForm.dwIndex = 0;
  1.1945 +    candForm.dwStyle = CFS_EXCLUDE;
  1.1946 +    candForm.ptCurrentPos.x = firstTargetCharRect.x;
  1.1947 +    candForm.ptCurrentPos.y = firstTargetCharRect.y;
  1.1948 +    candForm.rcArea.right = candForm.rcArea.left = candForm.ptCurrentPos.x;
  1.1949 +    candForm.rcArea.top = candForm.ptCurrentPos.y;
  1.1950 +    candForm.rcArea.bottom = candForm.ptCurrentPos.y +
  1.1951 +                               firstTargetCharRect.height;
  1.1952 +    ::ImmSetCandidateWindow(aIMEContext.get(), &candForm);
  1.1953 +  } else {
  1.1954 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1955 +      ("IMM32: SetIMERelatedWindowsPos, Set composition window\n"));
  1.1956 +
  1.1957 +    // Move the composition window to caret position (if selected some
  1.1958 +    // characters, we should use first character rect of them).
  1.1959 +    // And in this mode, IME adjusts the candidate window position
  1.1960 +    // automatically. So, we don't need to set it.
  1.1961 +    COMPOSITIONFORM compForm;
  1.1962 +    compForm.dwStyle = CFS_POINT;
  1.1963 +    compForm.ptCurrentPos.x = firstSelectedCharRect.x;
  1.1964 +    compForm.ptCurrentPos.y = firstSelectedCharRect.y;
  1.1965 +    ::ImmSetCompositionWindow(aIMEContext.get(), &compForm);
  1.1966 +  }
  1.1967 +
  1.1968 +  return true;
  1.1969 +}
  1.1970 +
  1.1971 +void
  1.1972 +nsIMM32Handler::SetIMERelatedWindowsPosOnPlugin(nsWindow* aWindow,
  1.1973 +                                                const nsIMEContext& aIMEContext)
  1.1974 +{
  1.1975 +  WidgetQueryContentEvent editorRectEvent(true, NS_QUERY_EDITOR_RECT, aWindow);
  1.1976 +  aWindow->InitEvent(editorRectEvent);
  1.1977 +  aWindow->DispatchWindowEvent(&editorRectEvent);
  1.1978 +  if (!editorRectEvent.mSucceeded) {
  1.1979 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.1980 +      ("IMM32: SetIMERelatedWindowsPosOnPlugin, "
  1.1981 +       "FAILED (NS_QUERY_EDITOR_RECT)"));
  1.1982 +    return;
  1.1983 +  }
  1.1984 +
  1.1985 +  // Clip the plugin rect by the client rect of the window because composition
  1.1986 +  // window needs to be specified the position in the client area.
  1.1987 +  nsWindow* toplevelWindow = aWindow->GetTopLevelWindow(false);
  1.1988 +  nsIntRect pluginRectInScreen =
  1.1989 +    editorRectEvent.mReply.mRect + toplevelWindow->WidgetToScreenOffset();
  1.1990 +  nsIntRect winRectInScreen;
  1.1991 +  aWindow->GetClientBounds(winRectInScreen);
  1.1992 +  // composition window cannot be positioned on the edge of client area.
  1.1993 +  winRectInScreen.width--;
  1.1994 +  winRectInScreen.height--;
  1.1995 +  nsIntRect clippedPluginRect;
  1.1996 +  clippedPluginRect.x =
  1.1997 +    std::min(std::max(pluginRectInScreen.x, winRectInScreen.x),
  1.1998 +             winRectInScreen.XMost());
  1.1999 +  clippedPluginRect.y =
  1.2000 +    std::min(std::max(pluginRectInScreen.y, winRectInScreen.y),
  1.2001 +             winRectInScreen.YMost());
  1.2002 +  int32_t xMost = std::min(pluginRectInScreen.XMost(), winRectInScreen.XMost());
  1.2003 +  int32_t yMost = std::min(pluginRectInScreen.YMost(), winRectInScreen.YMost());
  1.2004 +  clippedPluginRect.width = std::max(0, xMost - clippedPluginRect.x);
  1.2005 +  clippedPluginRect.height = std::max(0, yMost - clippedPluginRect.y);
  1.2006 +  clippedPluginRect -= aWindow->WidgetToScreenOffset();
  1.2007 +
  1.2008 +  // Cover the plugin with native caret.  This prevents IME's window and plugin
  1.2009 +  // overlap.
  1.2010 +  if (mNativeCaretIsCreated) {
  1.2011 +    ::DestroyCaret();
  1.2012 +  }
  1.2013 +  mNativeCaretIsCreated =
  1.2014 +    ::CreateCaret(aWindow->GetWindowHandle(), nullptr,
  1.2015 +                  clippedPluginRect.width, clippedPluginRect.height);
  1.2016 +  ::SetCaretPos(clippedPluginRect.x, clippedPluginRect.y);
  1.2017 +
  1.2018 +  // Set the composition window to bottom-left of the clipped plugin.
  1.2019 +  // As far as we know, there is no IME for RTL language.  Therefore, this code
  1.2020 +  // must not need to take care of RTL environment.
  1.2021 +  COMPOSITIONFORM compForm;
  1.2022 +  compForm.dwStyle = CFS_POINT;
  1.2023 +  compForm.ptCurrentPos.x = clippedPluginRect.BottomLeft().x;
  1.2024 +  compForm.ptCurrentPos.y = clippedPluginRect.BottomLeft().y;
  1.2025 +  if (!::ImmSetCompositionWindow(aIMEContext.get(), &compForm)) {
  1.2026 +    PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.2027 +      ("IMM32: SetIMERelatedWindowsPosOnPlugin, "
  1.2028 +       "FAILED to set composition window"));
  1.2029 +    return;
  1.2030 +  }
  1.2031 +}
  1.2032 +
  1.2033 +void
  1.2034 +nsIMM32Handler::ResolveIMECaretPos(nsIWidget* aReferenceWidget,
  1.2035 +                                   nsIntRect& aCursorRect,
  1.2036 +                                   nsIWidget* aNewOriginWidget,
  1.2037 +                                   nsIntRect& aOutRect)
  1.2038 +{
  1.2039 +  aOutRect = aCursorRect;
  1.2040 +
  1.2041 +  if (aReferenceWidget == aNewOriginWidget)
  1.2042 +    return;
  1.2043 +
  1.2044 +  if (aReferenceWidget)
  1.2045 +    aOutRect.MoveBy(aReferenceWidget->WidgetToScreenOffset());
  1.2046 +
  1.2047 +  if (aNewOriginWidget)
  1.2048 +    aOutRect.MoveBy(-aNewOriginWidget->WidgetToScreenOffset());
  1.2049 +}
  1.2050 +
  1.2051 +bool
  1.2052 +nsIMM32Handler::OnMouseEvent(nsWindow* aWindow, LPARAM lParam, int aAction,
  1.2053 +                             MSGResult& aResult)
  1.2054 +{
  1.2055 +  aResult.mConsumed = false; // always call next wndprc
  1.2056 +
  1.2057 +  if (!sWM_MSIME_MOUSE || !mIsComposing ||
  1.2058 +      !ShouldDrawCompositionStringOurselves()) {
  1.2059 +    return false;
  1.2060 +  }
  1.2061 +
  1.2062 +  nsIntPoint cursor(LOWORD(lParam), HIWORD(lParam));
  1.2063 +  WidgetQueryContentEvent charAtPt(true, NS_QUERY_CHARACTER_AT_POINT, aWindow);
  1.2064 +  aWindow->InitEvent(charAtPt, &cursor);
  1.2065 +  aWindow->DispatchWindowEvent(&charAtPt);
  1.2066 +  if (!charAtPt.mSucceeded ||
  1.2067 +      charAtPt.mReply.mOffset == WidgetQueryContentEvent::NOT_FOUND ||
  1.2068 +      charAtPt.mReply.mOffset < mCompositionStart ||
  1.2069 +      charAtPt.mReply.mOffset >
  1.2070 +        mCompositionStart + mCompositionString.Length()) {
  1.2071 +    return false;
  1.2072 +  }
  1.2073 +
  1.2074 +  // calcurate positioning and offset
  1.2075 +  // char :            JCH1|JCH2|JCH3
  1.2076 +  // offset:           0011 1122 2233
  1.2077 +  // positioning:      2301 2301 2301
  1.2078 +  nsIntRect cursorInTopLevel, cursorRect(cursor, nsIntSize(0, 0));
  1.2079 +  ResolveIMECaretPos(aWindow, cursorRect,
  1.2080 +                     aWindow->GetTopLevelWindow(false), cursorInTopLevel);
  1.2081 +  int32_t cursorXInChar = cursorInTopLevel.x - charAtPt.mReply.mRect.x;
  1.2082 +  // The event might hit to zero-width character, see bug 694913.
  1.2083 +  // The reason might be:
  1.2084 +  // * There are some zero-width characters are actually.
  1.2085 +  // * font-size is specified zero.
  1.2086 +  // But nobody reproduced this bug actually...
  1.2087 +  // We should assume that user clicked on right most of the zero-width
  1.2088 +  // character in such case.
  1.2089 +  int positioning = 1;
  1.2090 +  if (charAtPt.mReply.mRect.width > 0) {
  1.2091 +    positioning = cursorXInChar * 4 / charAtPt.mReply.mRect.width;
  1.2092 +    positioning = (positioning + 2) % 4;
  1.2093 +  }
  1.2094 +
  1.2095 +  int offset = charAtPt.mReply.mOffset - mCompositionStart;
  1.2096 +  if (positioning < 2) {
  1.2097 +    offset++;
  1.2098 +  }
  1.2099 +
  1.2100 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.2101 +    ("IMM32: OnMouseEvent, x,y=%ld,%ld, offset=%ld, positioning=%ld\n",
  1.2102 +     cursor.x, cursor.y, offset, positioning));
  1.2103 +
  1.2104 +  // send MS_MSIME_MOUSE message to default IME window.
  1.2105 +  HWND imeWnd = ::ImmGetDefaultIMEWnd(aWindow->GetWindowHandle());
  1.2106 +  nsIMEContext IMEContext(aWindow->GetWindowHandle());
  1.2107 +  return ::SendMessageW(imeWnd, sWM_MSIME_MOUSE,
  1.2108 +                        MAKELONG(MAKEWORD(aAction, positioning), offset),
  1.2109 +                        (LPARAM) IMEContext.get()) == 1;
  1.2110 +}
  1.2111 +
  1.2112 +/* static */ bool
  1.2113 +nsIMM32Handler::OnKeyDownEvent(nsWindow* aWindow, WPARAM wParam, LPARAM lParam,
  1.2114 +                               MSGResult& aResult)
  1.2115 +{
  1.2116 +  PR_LOG(gIMM32Log, PR_LOG_ALWAYS,
  1.2117 +    ("IMM32: OnKeyDownEvent, hWnd=%08x, wParam=%08x, lParam=%08x\n",
  1.2118 +     aWindow->GetWindowHandle(), wParam, lParam));
  1.2119 +  aResult.mConsumed = false;
  1.2120 +  switch (wParam) {
  1.2121 +    case VK_TAB:
  1.2122 +    case VK_PRIOR:
  1.2123 +    case VK_NEXT:
  1.2124 +    case VK_END:
  1.2125 +    case VK_HOME:
  1.2126 +    case VK_LEFT:
  1.2127 +    case VK_UP:
  1.2128 +    case VK_RIGHT:
  1.2129 +    case VK_DOWN:
  1.2130 +      // If IME didn't process the key message (the virtual key code wasn't
  1.2131 +      // converted to VK_PROCESSKEY), and the virtual key code event causes
  1.2132 +      // to move caret, we should cancel the composition here.  Then, this
  1.2133 +      // event will be dispatched.
  1.2134 +      // XXX I think that we should dispatch all key events during composition,
  1.2135 +      //     and nsEditor should cancel/commit the composition if it *thinks*
  1.2136 +      //     it's needed.
  1.2137 +      if (IsComposingOnOurEditor()) {
  1.2138 +        // NOTE: We don't need to cancel the composition on another window.
  1.2139 +        CancelComposition(aWindow, false);
  1.2140 +      }
  1.2141 +      return false;
  1.2142 +    default:
  1.2143 +      return false;
  1.2144 +  }
  1.2145 +}

mercurial