1 /*
   2  * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifndef AWT_COMPONENT_H
  27 #define AWT_COMPONENT_H
  28 
  29 #include "awtmsg.h"
  30 #include "awt_Object.h"
  31 #include "awt_Font.h"
  32 #include "awt_Brush.h"
  33 #include "awt_Pen.h"
  34 #include "awt_Win32GraphicsDevice.h"
  35 #include "GDIWindowSurfaceData.h"
  36 
  37 #include "java_awt_Component.h"
  38 #include "sun_awt_windows_WComponentPeer.h"
  39 #include "java_awt_event_KeyEvent.h"
  40 #include "java_awt_event_MouseEvent.h"
  41 #include "java_awt_event_WindowEvent.h"
  42 #include "java_awt_Dimension.h"
  43 
  44 extern LPCTSTR szAwtComponentClassName;
  45 
  46 static LPCTSTR DrawingStateProp = TEXT("SunAwtDrawingStateProp");
  47 
  48 const UINT IGNORE_KEY = (UINT)-1;
  49 const UINT MAX_ACP_STR_LEN = 7; // ANSI CP identifiers are no longer than this
  50 
  51 #define LEFT_BUTTON 1
  52 #define MIDDLE_BUTTON 2
  53 #define RIGHT_BUTTON 4
  54 #define DBL_CLICK 8
  55 #define X1_BUTTON 16
  56 #define X2_BUTTON 32
  57 
  58 #ifndef MK_XBUTTON1
  59 #define MK_XBUTTON1         0x0020
  60 #endif
  61 
  62 #ifndef MK_XBUTTON2
  63 #define MK_XBUTTON2         0x0040
  64 #endif
  65 
  66 // combination of standard mouse button flags
  67 const int ALL_MK_BUTTONS = MK_LBUTTON|MK_MBUTTON|MK_RBUTTON;
  68 const int X_BUTTONS = MK_XBUTTON1|MK_XBUTTON2;
  69 
  70 // The allowable difference between coordinates of the WM_TOUCH event and the
  71 // corresponding WM_LBUTTONDOWN/WM_LBUTTONUP event letting to associate these
  72 // events, when their coordinates are slightly different.
  73 const int TOUCH_MOUSE_COORDS_DELTA = 10;
  74 
  75 // Whether to check for embedded frame and adjust location
  76 #define CHECK_EMBEDDED 0
  77 #define DONT_CHECK_EMBEDDED 1
  78 
  79 class AwtPopupMenu;
  80 
  81 class AwtDropTarget;
  82 
  83 /*
  84  * Message routing codes
  85  */
  86 enum MsgRouting {
  87     mrPassAlong,    /* pass along to next in chain */
  88     mrDoDefault,    /* skip right to underlying default behavior */
  89     mrConsume,      /* consume msg & terminate routing immediatly,
  90                      * don't pass anywhere
  91                      */
  92 };
  93 
  94 /************************************************************************
  95  * AwtComponent class
  96  */
  97 
  98 class AwtComponent : public AwtObject {
  99 public:
 100     /* java.awt.Component fields and method IDs */
 101     static jfieldID peerID;
 102     static jfieldID xID;
 103     static jfieldID yID;
 104     static jfieldID widthID;
 105     static jfieldID heightID;
 106     static jfieldID visibleID;
 107     static jfieldID backgroundID;
 108     static jfieldID foregroundID;
 109     static jfieldID enabledID;
 110     static jfieldID parentID;
 111     static jfieldID cursorID;
 112     static jfieldID graphicsConfigID;
 113     static jfieldID peerGCID;
 114     static jfieldID focusableID;
 115     static jfieldID appContextID;
 116     static jfieldID hwndID;
 117 
 118     static jmethodID getFontMID;
 119     static jmethodID getToolkitMID;
 120     static jmethodID isEnabledMID;
 121     static jmethodID getLocationOnScreenMID;
 122     static jmethodID replaceSurfaceDataMID;
 123     static jmethodID replaceSurfaceDataLaterMID;
 124     static jmethodID disposeLaterMID;
 125 
 126     static const UINT WmAwtIsComponent;
 127     static jint * masks; //InputEvent mask array
 128     AwtComponent();
 129     virtual ~AwtComponent();
 130 
 131     /*
 132      * Dynamic class registration & creation
 133      */
 134     virtual LPCTSTR GetClassName() = 0;
 135     /*
 136      * Fix for 4964237: Win XP: Changing theme changes java dialogs title icon
 137      * WNDCLASS structure has been superseded by the WNDCLASSEX in Win32
 138      */
 139     virtual void FillClassInfo(WNDCLASSEX *lpwc);
 140     virtual void RegisterClass();
 141     virtual void UnregisterClass();
 142 
 143     virtual void CreateHWnd(JNIEnv *env, LPCWSTR title,
 144                     DWORD windowStyle, DWORD windowExStyle,
 145                     int x, int y, int w, int h,
 146                     HWND hWndParent, HMENU hMenu,
 147                     COLORREF colorForeground, COLORREF colorBackground,
 148                     jobject peer);
 149     virtual void DestroyHWnd();
 150     void InitPeerGraphicsConfig(JNIEnv *env, jobject peer);
 151 
 152     virtual void Dispose();
 153 
 154     void UpdateBackground(JNIEnv *env, jobject target);
 155 
 156     virtual void SubclassHWND();
 157     virtual void UnsubclassHWND();
 158 
 159     static LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
 160         WPARAM wParam, LPARAM lParam);
 161 
 162     /*
 163      * Access to the various objects of this aggregate component
 164      */
 165     INLINE HWND GetHWnd() { return m_hwnd; }
 166     INLINE void SetHWnd(HWND hwnd) { m_hwnd = hwnd; }
 167 
 168     static AwtComponent* GetComponent(HWND hWnd);
 169 
 170     /*
 171      * Access to the properties of the component
 172      */
 173     INLINE COLORREF GetColor() { return m_colorForeground; }
 174     virtual void SetColor(COLORREF c);
 175     HPEN GetForegroundPen();
 176 
 177     COLORREF GetBackgroundColor();
 178     virtual void SetBackgroundColor(COLORREF c);
 179     HBRUSH GetBackgroundBrush();
 180     INLINE BOOL IsBackgroundColorSet() { return m_backgroundColorSet; }
 181 
 182     virtual void SetFont(AwtFont *pFont);
 183 
 184     INLINE void SetText(LPCTSTR text) { ::SetWindowText(GetHWnd(), text); }
 185     INLINE int GetText(LPTSTR buffer, int size) {
 186         return ::GetWindowText(GetHWnd(), buffer, size);
 187     }
 188     INLINE int GetTextLength() { return ::GetWindowTextLength(GetHWnd()); }
 189 
 190     virtual void GetInsets(RECT* rect) {
 191         VERIFY(::SetRectEmpty(rect));
 192     }
 193 
 194     BOOL IsVisible() { return m_visible;};
 195 
 196     HDC GetDCFromComponent();
 197 
 198     /*
 199      * Enable/disable component
 200      */
 201     virtual void Enable(BOOL bEnable);
 202 
 203     /*
 204      * Validate and call handleExpose on rects of UpdateRgn
 205      */
 206     void PaintUpdateRgn(const RECT *insets);
 207 
 208     static HWND GetTopLevelParentForWindow(HWND hwndDescendant);
 209 
 210     static jobject FindHeavyweightUnderCursor(BOOL useCache);
 211 
 212     /*
 213      * Returns the parent component.  If no parent window, or the
 214      * parent window isn't an AwtComponent, returns NULL.
 215      */
 216     AwtComponent* GetParent();
 217 
 218     /* Get the component's immediate container. Note: may return NULL while
 219        the component is being reparented in full-screen mode by Direct3D */
 220     class AwtWindow* GetContainer();
 221 
 222     /* Is a component a container? Used by above method */
 223     virtual BOOL IsContainer() { return FALSE;} // Plain components can't
 224 
 225     /**
 226      * Returns TRUE if this message will trigger native focus change, FALSE otherwise.
 227      */
 228     virtual BOOL IsFocusingKeyMessage(MSG *pMsg);
 229     virtual BOOL IsFocusingMouseMessage(MSG *pMsg);
 230 
 231     BOOL IsFocusable();
 232 
 233     /*
 234      * Returns an increasing unsigned value used for child control IDs.
 235      * There is no attempt to reclaim command ID's.
 236      */
 237     INLINE UINT CreateControlID() { return m_nextControlID++; }
 238 
 239     // returns the current keyboard layout
 240     INLINE static HKL GetKeyboardLayout() {
 241         return m_hkl;
 242     }
 243 
 244     // returns the current code page that should be used in
 245     // all MultiByteToWideChar and WideCharToMultiByte calls.
 246     // This code page should also be use in IsDBCSLeadByteEx.
 247     INLINE static UINT GetCodePage()
 248     {
 249         return m_CodePage;
 250     }
 251 
 252 // Added by waleed for BIDI Support
 253     // returns the right to left status
 254     INLINE static BOOL GetRTLReadingOrder() {
 255         return sm_rtlReadingOrder;
 256     }
 257     // returns the right to left status
 258     INLINE static BOOL GetRTL() {
 259         return sm_rtl;
 260     }
 261     // returns the current sub language
 262     INLINE static LANGID GetSubLanguage() {
 263         return SUBLANGID(m_idLang);
 264     }
 265 // end waleed
 266 
 267     // returns the current input language
 268     INLINE static LANGID GetInputLanguage()
 269     {
 270         return m_idLang;
 271     }
 272     // Convert Language ID to CodePage
 273     static UINT LangToCodePage(LANGID idLang);
 274 
 275     /*
 276      * methods on this component
 277      */
 278     virtual void Show();
 279     virtual void Hide();
 280     virtual void Reshape(int x, int y, int w, int h);
 281 
 282     /*
 283      * Fix for 4046446.
 284      * Component size/position helper, for the values above the short int limit.
 285      */
 286     static BOOL SetWindowPos(HWND wnd, HWND after,
 287                              int x, int y, int w, int h, UINT flags);
 288 
 289     /*
 290      * Sets the scrollbar values.  'bar' can be either SB_VERT or
 291      * SB_HORZ.  'min', 'value', and 'max' can have the value INT_MAX
 292      * which means that the value should not be changed.
 293      */
 294     void SetScrollValues(UINT bar, int min, int value, int max);
 295 
 296     INLINE LRESULT SendMessage(UINT msg, WPARAM wParam=0, LPARAM lParam=0) {
 297         DASSERT(GetHWnd());
 298         return ::SendMessage(GetHWnd(), msg, wParam, lParam);
 299     }
 300 
 301     void PostUngrabEvent();
 302 
 303     INLINE virtual LONG GetStyle() {
 304         DASSERT(GetHWnd());
 305         return ::GetWindowLong(GetHWnd(), GWL_STYLE);
 306     }
 307     INLINE virtual void SetStyle(LONG style) {
 308         DASSERT(GetHWnd());
 309         // SetWindowLong() error handling as recommended by Win32 API doc.
 310         ::SetLastError(0);
 311         DWORD ret = ::SetWindowLong(GetHWnd(), GWL_STYLE, style);
 312         DASSERT(ret != 0 || ::GetLastError() == 0);
 313     }
 314     INLINE virtual LONG GetStyleEx() {
 315         DASSERT(GetHWnd());
 316         return ::GetWindowLong(GetHWnd(), GWL_EXSTYLE);
 317     }
 318     INLINE virtual void SetStyleEx(LONG style) {
 319         DASSERT(GetHWnd());
 320         // SetWindowLong() error handling as recommended by Win32 API doc.
 321         ::SetLastError(0);
 322         DWORD ret = ::SetWindowLong(GetHWnd(), GWL_EXSTYLE, style);
 323         DASSERT(ret != 0 || ::GetLastError() == 0);
 324     }
 325 
 326     virtual BOOL NeedDblClick() { return FALSE; }
 327 
 328     /* for multifont component */
 329     static void DrawWindowText(HDC hDC, jobject font, jstring text,
 330                                int x, int y);
 331     static void DrawGrayText(HDC hDC, jobject font, jstring text,
 332                              int x, int y);
 333 
 334     void DrawListItem(JNIEnv *env, DRAWITEMSTRUCT &drawInfo);
 335 
 336     void MeasureListItem(JNIEnv *env, MEASUREITEMSTRUCT &measureInfo);
 337 
 338     jstring GetItemString(JNIEnv *env, jobject target, jint index);
 339 
 340     jint GetFontHeight(JNIEnv *env);
 341 
 342     virtual jobject PreferredItemSize(JNIEnv *env) {DASSERT(FALSE); return NULL; }
 343 
 344     INLINE BOOL isEnabled() {
 345         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 346         if (env->EnsureLocalCapacity(2) < 0) {
 347             return NULL;
 348         }
 349         jobject self = GetPeer(env);
 350         jobject target = env->GetObjectField(self, AwtObject::targetID);
 351         BOOL e = env->CallBooleanMethod(target, AwtComponent::isEnabledMID);
 352         DASSERT(!safe_ExceptionOccurred(env));
 353 
 354         env->DeleteLocalRef(target);
 355 
 356         return e;
 357     }
 358 
 359     INLINE BOOL isRecursivelyEnabled() {
 360         AwtComponent* p = this;
 361         do {
 362             if (!p->isEnabled()) {
 363                 return FALSE;
 364             }
 365         } while (!p->IsTopLevel() &&
 366             (p = p->GetParent()) != NULL);
 367         return TRUE;
 368     }
 369 
 370     void SendKeyEventToFocusOwner(jint id, jlong when, jint raw, jint cooked,
 371                                   jint modifiers, jint keyLocation, jlong nativeCode,
 372                                   MSG *msg = NULL);
 373     /*
 374      * Allocate and initialize a new java.awt.event.KeyEvent, and
 375      * post it to the peer's target object.  No response is expected
 376      * from the target.
 377      */
 378     void SendKeyEvent(jint id, jlong when, jint raw, jint cooked,
 379                       jint modifiers, jint keyLocation, jlong nativeCode,
 380                       MSG *msg = NULL);
 381 
 382     /*
 383      * Allocate and initialize a new java.awt.event.MouseEvent, and
 384      * post it to the peer's target object.  No response is expected
 385      * from the target.
 386      */
 387     void SendMouseEvent(jint id, jlong when, jint x, jint y,
 388                         jint modifiers, jint clickCount,
 389                         jboolean popupTrigger, jint button = 0,
 390                         MSG *msg = NULL, BOOL causedByTouchEvent = FALSE);
 391 
 392     /*
 393      * Allocate and initialize a new java.awt.event.MouseWheelEvent, and
 394      * post it to the peer's target object.  No response is expected
 395      * from the target.
 396      */
 397     void SendMouseWheelEvent(jint id, jlong when, jint x, jint y,
 398                              jint modifiers, jint clickCount,
 399                              jboolean popupTrigger, jint scrollType,
 400                              jint scrollAmount, jint wheelRotation,
 401                              jdouble preciseWheelRotation, MSG *msg = NULL);
 402 
 403     /*
 404      * Allocate and initialize a new java.awt.event.FocusEvent, and
 405      * post it to the peer's target object.  No response is expected
 406      * from the target.
 407      */
 408     void SendFocusEvent(jint id, HWND opposite);
 409 
 410     /* Forward a filtered event directly to the subclassed window.
 411        synthetic should be TRUE iff the message was generated because
 412        of a synthetic Java event, rather than a native event. */
 413     virtual MsgRouting HandleEvent(MSG *msg, BOOL synthetic);
 414 
 415     /* Post a WM_AWT_HANDLE_EVENT message which invokes HandleEvent
 416        on the toolkit thread. This method may pre-filter the messages. */
 417     virtual BOOL PostHandleEventMessage(MSG *msg, BOOL synthetic);
 418 
 419     /* Event->message synthesizer methods. */
 420     void SynthesizeKeyMessage(JNIEnv *env, jobject keyEvent);
 421     void SynthesizeMouseMessage(JNIEnv *env, jobject mouseEvent);
 422 
 423     /* Components which inherit native mouse wheel behavior will
 424      * return TRUE.  These are TextArea, Choice, FileDialog, and
 425      * List.  All other Components return FALSE.
 426      */
 427     virtual BOOL InheritsNativeMouseWheelBehavior();
 428 
 429     /* Determines whether the component is obscured by another window */
 430     // Called on Toolkit thread
 431     static jboolean _IsObscured(void *param);
 432 
 433     /* Invalidate the specified rectangle. */
 434     virtual void Invalidate(RECT* r);
 435 
 436     /* Begin and end deferred window positioning. */
 437     virtual void BeginValidate();
 438     virtual void EndValidate();
 439 
 440     /* Keyboard conversion routines. */
 441     static void InitDynamicKeyMapTable();
 442     static void BuildDynamicKeyMapTable();
 443     static jint GetJavaModifiers();
 444     static jint GetButton(int mouseButton);
 445     static UINT GetButtonMK(int mouseButton);
 446     static UINT WindowsKeyToJavaKey(UINT windowsKey, UINT modifiers, UINT character, BOOL isDeadKey);
 447     static void JavaKeyToWindowsKey(UINT javaKey, UINT *windowsKey, UINT *modifiers, UINT originalWindowsKey);
 448     static void UpdateDynPrimaryKeymap(UINT wkey, UINT jkeyLegacy, jint keyLocation, UINT modifiers);
 449 
 450     INLINE static void AwtComponent::JavaKeyToWindowsKey(UINT javaKey,
 451                                        UINT *windowsKey, UINT *modifiers)
 452     {
 453         JavaKeyToWindowsKey(javaKey, windowsKey, modifiers, IGNORE_KEY);
 454     }
 455 
 456     enum TransOps {NONE, LOAD, SAVE};
 457 
 458     UINT WindowsKeyToJavaChar(UINT wkey, UINT modifiers, TransOps ops, BOOL &isDeadKey);
 459 
 460     /* routines used for input method support */
 461     void SetInputMethod(jobject im, BOOL useNativeCompWindow);
 462     void SendInputMethodEvent(jint id, jstring text, int cClause,
 463                               int *rgClauseBoundary, jstring *rgClauseReading,
 464                               int cAttrBlock, int *rgAttrBoundary,
 465                               BYTE *rgAttrValue, int commitedTextLength,
 466                               int caretPos, int visiblePos);
 467     void InquireCandidatePosition();
 468     INLINE LPARAM GetCandidateType() { return m_bitsCandType; }
 469     HWND ImmGetHWnd();
 470     HIMC ImmAssociateContext(HIMC himc);
 471     HWND GetProxyFocusOwner();
 472 
 473     INLINE HWND GetProxyToplevelContainer() {
 474         HWND proxyHWnd = GetProxyFocusOwner();
 475         return ::GetAncestor(proxyHWnd, GA_ROOT); // a browser in case of EmbeddedFrame
 476     }
 477 
 478     void CallProxyDefWindowProc(UINT message,
 479                                 WPARAM wParam,
 480                                 LPARAM lParam,
 481                                 LRESULT &retVal,
 482                                 MsgRouting &mr);
 483 
 484     /*
 485      * Windows message handler functions
 486      */
 487     virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
 488     virtual LRESULT DefWindowProc(UINT msg, WPARAM wParam, LPARAM lParam);
 489 
 490     /* return true if msg is processed */
 491     virtual MsgRouting PreProcessMsg(MSG& msg);
 492 
 493     virtual MsgRouting WmCreate() {return mrDoDefault;}
 494     virtual MsgRouting WmClose() {return mrDoDefault;}
 495     virtual MsgRouting WmDestroy();
 496     virtual MsgRouting WmNcDestroy();
 497 
 498     virtual MsgRouting WmActivate(UINT nState, BOOL fMinimized, HWND opposite)
 499     {
 500         return mrDoDefault;
 501     }
 502 
 503     virtual MsgRouting WmEraseBkgnd(HDC hDC, BOOL& didErase)
 504     {
 505         return mrDoDefault;
 506     }
 507 
 508     virtual MsgRouting WmPaint(HDC hDC);
 509     virtual MsgRouting WmGetMinMaxInfo(LPMINMAXINFO lpmmi);
 510     virtual MsgRouting WmMove(int x, int y);
 511     virtual MsgRouting WmSize(UINT type, int w, int h);
 512     virtual MsgRouting WmSizing();
 513     virtual MsgRouting WmShowWindow(BOOL show, UINT status);
 514     virtual MsgRouting WmSetFocus(HWND hWndLost);
 515     virtual MsgRouting WmKillFocus(HWND hWndGot);
 516     virtual MsgRouting WmCtlColor(HDC hDC, HWND hCtrl,
 517                                   UINT ctlColor, HBRUSH& retBrush);
 518     virtual MsgRouting WmHScroll(UINT scrollCode, UINT pos, HWND hScrollBar);
 519     virtual MsgRouting WmVScroll(UINT scrollCode, UINT pos, HWND hScrollBar);
 520 
 521     virtual MsgRouting WmMouseEnter(UINT flags, int x, int y);
 522     virtual MsgRouting WmMouseDown(UINT flags, int x, int y, int button);
 523     virtual MsgRouting WmMouseUp(UINT flags, int x, int y, int button);
 524     virtual MsgRouting WmMouseMove(UINT flags, int x, int y);
 525     virtual MsgRouting WmMouseExit(UINT flags, int x, int y);
 526     virtual MsgRouting WmMouseWheel(UINT flags, int x, int y,
 527                                     int wheelRotation);
 528     virtual MsgRouting WmNcMouseDown(WPARAM hitTest, int x, int y, int button);
 529     virtual MsgRouting WmNcMouseUp(WPARAM hitTest, int x, int y, int button);
 530     virtual MsgRouting WmWindowPosChanging(LPARAM windowPos);
 531     virtual MsgRouting WmWindowPosChanged(LPARAM windowPos);
 532     virtual void WmTouch(WPARAM wParam, LPARAM lParam);
 533 
 534     // NB: 64-bit: vkey is wParam of the message, but other API's take
 535     // vkey parameters of type UINT, so we do the cast before dispatching.
 536     virtual MsgRouting WmKeyDown(UINT vkey, UINT repCnt, UINT flags, BOOL system);
 537     virtual MsgRouting WmKeyUp(UINT vkey, UINT repCnt, UINT flags, BOOL system);
 538 
 539     virtual MsgRouting WmChar(UINT character, UINT repCnt, UINT flags, BOOL system);
 540     virtual MsgRouting WmIMEChar(UINT character, UINT repCnt, UINT flags, BOOL system);
 541     virtual MsgRouting WmInputLangChange(UINT charset, HKL hKeyBoardLayout);
 542     virtual MsgRouting WmForwardChar(WCHAR character, LPARAM lParam,
 543                                      BOOL synthethic);
 544     virtual MsgRouting WmPaste();
 545 
 546     virtual void SetCompositionWindow(RECT &r);
 547     virtual void OpenCandidateWindow(int x, int y);
 548     virtual void SetCandidateWindow(int iCandType, int x, int y);
 549     virtual MsgRouting WmImeSetContext(BOOL fSet, LPARAM *lplParam);
 550     virtual MsgRouting WmImeNotify(WPARAM subMsg, LPARAM bitsCandType);
 551     virtual MsgRouting WmImeStartComposition();
 552     virtual MsgRouting WmImeEndComposition();
 553     virtual MsgRouting WmImeComposition(WORD wChar, LPARAM flags);
 554 
 555     virtual MsgRouting WmTimer(UINT_PTR timerID) {return mrDoDefault;}
 556 
 557     virtual MsgRouting WmCommand(UINT id, HWND hWndCtrl, UINT notifyCode);
 558 
 559     /* reflected WmCommand from parent */
 560     virtual MsgRouting WmNotify(UINT notifyCode);
 561 
 562     virtual MsgRouting WmCompareItem(UINT /*ctrlId*/,
 563                                      COMPAREITEMSTRUCT &compareInfo,
 564                                      LRESULT &result);
 565     virtual MsgRouting WmDeleteItem(UINT /*ctrlId*/,
 566                                     DELETEITEMSTRUCT &deleteInfo);
 567     virtual MsgRouting WmDrawItem(UINT ctrlId,
 568                                   DRAWITEMSTRUCT &drawInfo);
 569     virtual MsgRouting WmMeasureItem(UINT ctrlId,
 570                                      MEASUREITEMSTRUCT &measureInfo);
 571     /* Fix 4181790 & 4223341 : These functions get overridden in owner-drawn
 572      * components instead of the Wm... versions.
 573      */
 574     virtual MsgRouting OwnerDrawItem(UINT ctrlId,
 575                                      DRAWITEMSTRUCT &drawInfo);
 576     virtual MsgRouting OwnerMeasureItem(UINT ctrlId,
 577                                         MEASUREITEMSTRUCT &measureInfo);
 578 
 579     virtual MsgRouting WmPrint(HDC hDC, LPARAM flags);
 580     virtual MsgRouting WmPrintClient(HDC hDC, LPARAM flags);
 581 
 582     virtual MsgRouting WmNcCalcSize(BOOL fCalcValidRects,
 583                                     LPNCCALCSIZE_PARAMS lpncsp,
 584                                     LRESULT &retVal);
 585     virtual MsgRouting WmNcPaint(HRGN hrgn);
 586     virtual MsgRouting WmNcHitTest(UINT x, UINT y, LRESULT &retVal);
 587     virtual MsgRouting WmSysCommand(UINT uCmdType, int xPos, int yPos);
 588     virtual MsgRouting WmExitSizeMove();
 589     virtual MsgRouting WmEnterMenuLoop(BOOL isTrackPopupMenu);
 590     virtual MsgRouting WmExitMenuLoop(BOOL isTrackPopupMenu);
 591 
 592     virtual MsgRouting WmQueryNewPalette(LRESULT &retVal);
 593     virtual MsgRouting WmPaletteChanged(HWND hwndPalChg);
 594     virtual MsgRouting WmPaletteIsChanging(HWND hwndPalChg);
 595     virtual MsgRouting WmStyleChanged(int wStyleType, LPSTYLESTRUCT lpss);
 596     virtual MsgRouting WmSettingChange(UINT wFlag, LPCTSTR pszSection);
 597 
 598     virtual MsgRouting WmContextMenu(HWND hCtrl, UINT xPos, UINT yPos) {
 599         return mrDoDefault;
 600     }
 601 
 602     void UpdateColorModel();
 603 
 604     jintArray CreatePrintedPixels(SIZE &loc, SIZE &size, int alpha);
 605 
 606     /*
 607      * HWND, AwtComponent and Java Peer interaction
 608      *
 609      * Link the C++, Java peer, and HWNDs together.
 610      */
 611     void LinkObjects(JNIEnv *env, jobject peer);
 612 
 613     void UnlinkObjects();
 614 
 615     static BOOL QueryNewPaletteCalled() { return m_QueryNewPaletteCalled; }
 616 
 617 #ifdef DEBUG
 618     virtual void VerifyState(); /* verify component and peer are in sync. */
 619 #else
 620     void VerifyState() {}       /* no-op */
 621 #endif
 622 
 623     virtual AwtDropTarget* CreateDropTarget(JNIEnv* env);
 624     virtual void DestroyDropTarget();
 625 
 626     INLINE virtual HWND GetDBCSEditHandle() { return NULL; }
 627     // State for native drawing API
 628     INLINE jint GetDrawState() { return GetDrawState(m_hwnd); }
 629     INLINE void SetDrawState(jint state) { SetDrawState(m_hwnd, state); }    // State for native drawing API
 630 
 631     INLINE virtual BOOL IsTopLevel() { return FALSE; }
 632     INLINE virtual BOOL IsEmbeddedFrame() { return FALSE; }
 633     INLINE virtual BOOL IsScrollbar() { return FALSE; }
 634 
 635     static INLINE BOOL IsTopLevelHWnd(HWND hwnd) {
 636         AwtComponent *comp = AwtComponent::GetComponent(hwnd);
 637         return (comp != NULL && comp->IsTopLevel());
 638     }
 639     static INLINE BOOL IsEmbeddedFrameHWnd(HWND hwnd) {
 640         AwtComponent *comp = AwtComponent::GetComponent(hwnd);
 641         return (comp != NULL && comp->IsEmbeddedFrame());
 642     }
 643 
 644     static jint GetDrawState(HWND hwnd);
 645     static void SetDrawState(HWND hwnd, jint state);
 646 
 647     static HWND GetHWnd(JNIEnv* env, jobject target);
 648 
 649     static MSG* CreateMessage(UINT message, WPARAM wParam, LPARAM lParam, int x, int y);
 650     static void InitMessage(MSG* msg, UINT message, WPARAM wParam, LPARAM lParam, int x, int y);
 651 
 652     // Some methods to be called on Toolkit thread via Toolkit.InvokeFunction()
 653     static void _Show(void *param);
 654     static void _Hide(void *param);
 655     static void _Enable(void *param);
 656     static void _Disable(void *param);
 657     static jobject _GetLocationOnScreen(void *param);
 658     static void _Reshape(void *param);
 659     static void _ReshapeNoCheck(void *param);
 660     static void _NativeHandleEvent(void *param);
 661     static void _SetForeground(void *param);
 662     static void _SetBackground(void *param);
 663     static void _SetFont(void *param);
 664     static void _Start(void *param);
 665     static void _BeginValidate(void *param);
 666     static void _EndValidate(void *param);
 667     static void _UpdateWindow(void *param);
 668     static jlong _AddNativeDropTarget(void *param);
 669     static void _RemoveNativeDropTarget(void *param);
 670     static jintArray _CreatePrintedPixels(void *param);
 671     static jboolean _NativeHandlesWheelScrolling(void *param);
 672     static void _SetRectangularShape(void *param);
 673     static void _SetZOrder(void *param);
 674 
 675     static HWND sm_focusOwner;
 676 
 677 private:
 678     static HWND sm_focusedWindow;
 679 
 680 public:
 681     static inline HWND GetFocusedWindow() { return sm_focusedWindow; }
 682     static void SetFocusedWindow(HWND window);
 683 
 684     static void _SetFocus(void *param);
 685 
 686     static void *SetNativeFocusOwner(void *self);
 687     static void *GetNativeFocusedWindow();
 688     static void *GetNativeFocusOwner();
 689 
 690     static BOOL sm_inSynthesizeFocus;
 691 
 692     // Execute on Toolkit only.
 693     INLINE static LRESULT SynthesizeWmSetFocus(HWND targetHWnd, HWND oppositeHWnd) {
 694         sm_inSynthesizeFocus = TRUE;
 695         LRESULT res = ::SendMessage(targetHWnd, WM_SETFOCUS, (WPARAM)oppositeHWnd, 0);
 696         sm_inSynthesizeFocus = FALSE;
 697         return res;
 698     }
 699     // Execute on Toolkit only.
 700     INLINE static LRESULT SynthesizeWmKillFocus(HWND targetHWnd, HWND oppositeHWnd) {
 701         sm_inSynthesizeFocus = TRUE;
 702         LRESULT res = ::SendMessage(targetHWnd, WM_KILLFOCUS, (WPARAM)oppositeHWnd, 0);
 703         sm_inSynthesizeFocus = FALSE;
 704         return res;
 705     }
 706 
 707     static BOOL sm_bMenuLoop;
 708     static INLINE BOOL isMenuLoopActive() {
 709         return sm_bMenuLoop;
 710     }
 711 
 712     // when this component is being destroyed, this method is called
 713     // to find out if there are any messages being processed, and if
 714     // there are some then disposal of this component is postponed
 715     virtual BOOL CanBeDeleted() {
 716         return m_MessagesProcessing == 0;
 717     }
 718 
 719     BOOL IsDestroyPaused() const {
 720         return m_bPauseDestroy;
 721     }
 722 
 723 protected:
 724     static AwtComponent* GetComponentImpl(HWND hWnd);
 725 
 726     static int GetClickCount();
 727 
 728     HWND     m_hwnd;
 729     UINT     m_myControlID;     /* its own ID from the view point of parent */
 730     BOOL     m_backgroundColorSet;
 731     BOOL     m_visible;         /* copy of Component.visible */
 732 
 733     static BOOL sm_suppressFocusAndActivation;
 734     static BOOL sm_restoreFocusAndActivation;
 735 
 736     /*
 737      * The function sets the focus-restore flag ON/OFF.
 738      * When the flag is ON, focus is restored immidiately after the proxy loses it.
 739      * All focus messages are suppressed. It's also assumed that sm_focusedWindow and
 740      * sm_focusOwner don't change after the flag is set ON and before it's set OFF.
 741      */
 742     static INLINE void SetRestoreFocus(BOOL doSet) {
 743         sm_suppressFocusAndActivation = doSet;
 744         sm_restoreFocusAndActivation = doSet;
 745     }
 746 
 747     virtual void SetDragCapture(UINT flags);
 748     virtual void ReleaseDragCapture(UINT flags);
 749 
 750     virtual void FillBackground(HDC hMemoryDC, SIZE &size);
 751     virtual void FillAlpha(void *bitmapBits, SIZE &size, BYTE alpha);
 752 
 753 private:
 754     /* A bitmask keeps the button's numbers as MK_LBUTTON, MK_MBUTTON, MK_RBUTTON
 755      * which are allowed to
 756      * generate the CLICK event after the RELEASE has happened.
 757      * There are conditions that must be true for that sending CLICK event:
 758      * 1) button was initially PRESSED
 759      * 2) no movement or drag has happened until RELEASE
 760     */
 761     UINT m_mouseButtonClickAllowed;
 762 
 763     BOOL m_touchDownOccurred;
 764     BOOL m_touchUpOccurred;
 765     POINT m_touchDownPoint;
 766     POINT m_touchUpPoint;
 767 
 768     BOOL m_bSubclassed;
 769     BOOL m_bPauseDestroy;
 770 
 771     COLORREF m_colorForeground;
 772     COLORREF m_colorBackground;
 773 
 774     AwtPen*  m_penForeground;
 775     AwtBrush* m_brushBackground;
 776 
 777     WNDPROC  m_DefWindowProc;
 778     // counter for messages being processed by this component
 779     UINT     m_MessagesProcessing;
 780 
 781     // provides a unique ID for child controls
 782     UINT     m_nextControlID;
 783 
 784     // DeferWindowPos handle for batched-up window positioning
 785     HDWP     m_hdwp;
 786     // Counter to handle nested calls to Begin/EndValidate
 787     UINT     m_validationNestCount;
 788 
 789     AwtDropTarget* m_dropTarget; // associated DropTarget object
 790 
 791     // When we process WM_INPUTLANGCHANGE we remember the keyboard
 792     // layout handle and associated input language and codepage.
 793     // We also invalidate VK translation table for VK_OEM_* codes
 794     static HKL    m_hkl;
 795     static UINT   m_CodePage;
 796     static LANGID m_idLang;
 797 
 798     static BOOL sm_rtl;
 799     static BOOL sm_rtlReadingOrder;
 800 
 801     static BOOL sm_PrimaryDynamicTableBuilt;
 802 
 803     jobject m_InputMethod;
 804     BOOL    m_useNativeCompWindow;
 805     LPARAM  m_bitsCandType;
 806     UINT    m_PendingLeadByte;
 807 
 808     void SetComponentInHWND();
 809 
 810     // Determines whether a given virtual key is on the numpad
 811     static BOOL IsNumPadKey(UINT vkey, BOOL extended);
 812 
 813     // Determines the keyLocation of a given key
 814     static jint GetKeyLocation(UINT wkey, UINT flags);
 815     static jint GetShiftKeyLocation(UINT wkey, UINT flags);
 816 
 817     // Cache for FindComponent
 818     static HWND sm_cursorOn;
 819 
 820     static BOOL m_QueryNewPaletteCalled;
 821 
 822     static AwtComponent* sm_getComponentCache; // a cache for the GetComponent(..) method.
 823 
 824     int windowMoveLockPosX;
 825     int windowMoveLockPosY;
 826     int windowMoveLockPosCX;
 827     int windowMoveLockPosCY;
 828 
 829     // 6524352: support finer-resolution
 830     int m_wheelRotationAmount;
 831 
 832     BOOL deadKeyActive;
 833 
 834     /*
 835      * The association list of children's IDs and corresponding components.
 836      * Some components like Choice or List are required their sizes while
 837      * the creations of themselfs are in progress.
 838      */
 839     class ChildListItem {
 840     public:
 841         ChildListItem(UINT id, AwtComponent* component) {
 842             m_ID = id;
 843             m_Component = component;
 844             m_next = NULL;
 845         }
 846         ~ChildListItem() {
 847             if (m_next != NULL)
 848                 delete m_next;
 849         }
 850 
 851         UINT m_ID;
 852         AwtComponent* m_Component;
 853         ChildListItem* m_next;
 854     };
 855 
 856 public:
 857     INLINE void PushChild(UINT id, AwtComponent* component) {
 858         ChildListItem* child = new ChildListItem(id, component);
 859         child->m_next = m_childList;
 860         m_childList = child;
 861     }
 862 
 863     static void SetParent(void * param);
 864 private:
 865     AwtComponent* SearchChild(UINT id);
 866     void RemoveChild(UINT id) ;
 867     static BOOL IsNavigationKey(UINT wkey);
 868     static void BuildPrimaryDynamicTable();
 869 
 870     ChildListItem* m_childList;
 871 
 872     HCURSOR m_hCursorCache; // the latest cursor which has been active within the heavyweight component
 873 public:
 874     inline void setCursorCache(HCURSOR hCursor) {
 875         m_hCursorCache = hCursor;
 876     }
 877     inline HCURSOR getCursorCache() {
 878         return m_hCursorCache;
 879     }
 880 };
 881 
 882 class CounterHelper {
 883 private:
 884     UINT *m_counter;
 885 public:
 886     explicit CounterHelper(UINT *counter) {
 887         m_counter = counter;
 888         (*m_counter)++;
 889     }
 890     ~CounterHelper() {
 891         (*m_counter)--;
 892         m_counter = NULL;
 893     }
 894 };
 895 
 896 // DC management objects; these classes are used to track the list of
 897 // DC's associated with a given Component.  Then DC's can be released
 898 // appropriately on demand or on window destruction to avoid resource
 899 // leakage.
 900 class DCItem {
 901 public:
 902     HDC             hDC;
 903     HWND            hWnd;
 904     DCItem          *next;
 905 };
 906 class DCList {
 907     DCItem          *head;
 908     CriticalSection listLock;
 909 public:
 910     DCList() { head = NULL; }
 911 
 912     void            AddDC(HDC hDC, HWND hWnd);
 913     void            AddDCItem(DCItem *newItem);
 914     DCItem          *RemoveDC(HDC hDC, HWND hWnd);
 915     DCItem          *RemoveAllDCs(HWND hWnd);
 916     void            RealizePalettes(int screen);
 917 };
 918 
 919 void ReleaseDCList(HWND hwnd, DCList &list);
 920 void MoveDCToPassiveList(HDC hDC, HWND hWnd);
 921 
 922 namespace TimeHelper{
 923     jlong getMessageTimeUTC();
 924     jlong windowsToUTC(DWORD event_offset);
 925 }
 926 
 927 #include "ObjectList.h"
 928 
 929 #endif /* AWT_COMPONENT_H */