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