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 
  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     void ReshapeNoScale(int x, int y, int w, int h);
 279 
 280     /*
 281      * Fix for 4046446.
 282      * Component size/position helper, for the values above the short int limit.
 283      */
 284     static BOOL SetWindowPos(HWND wnd, HWND after,
 285                              int x, int y, int w, int h, UINT flags);
 286 
 287     /*
 288      * Sets the scrollbar values.  'bar' can be either SB_VERT or
 289      * SB_HORZ.  'min', 'value', and 'max' can have the value INT_MAX
 290      * which means that the value should not be changed.
 291      */
 292     void SetScrollValues(UINT bar, int min, int value, int max);
 293 
 294     INLINE LRESULT SendMessage(UINT msg, WPARAM wParam=0, LPARAM lParam=0) {
 295         DASSERT(GetHWnd());
 296         return ::SendMessage(GetHWnd(), msg, wParam, lParam);
 297     }
 298 
 299     void PostUngrabEvent();
 300 
 301     INLINE virtual LONG GetStyle() {
 302         DASSERT(GetHWnd());
 303         return ::GetWindowLong(GetHWnd(), GWL_STYLE);
 304     }
 305     INLINE virtual void SetStyle(LONG style) {
 306         DASSERT(GetHWnd());
 307         // SetWindowLong() error handling as recommended by Win32 API doc.
 308         ::SetLastError(0);
 309         DWORD ret = ::SetWindowLong(GetHWnd(), GWL_STYLE, style);
 310         DASSERT(ret != 0 || ::GetLastError() == 0);
 311     }
 312     INLINE virtual LONG GetStyleEx() {
 313         DASSERT(GetHWnd());
 314         return ::GetWindowLong(GetHWnd(), GWL_EXSTYLE);
 315     }
 316     INLINE virtual void SetStyleEx(LONG style) {
 317         DASSERT(GetHWnd());
 318         // SetWindowLong() error handling as recommended by Win32 API doc.
 319         ::SetLastError(0);
 320         DWORD ret = ::SetWindowLong(GetHWnd(), GWL_EXSTYLE, style);
 321         DASSERT(ret != 0 || ::GetLastError() == 0);
 322     }
 323 
 324     virtual BOOL NeedDblClick() { return FALSE; }
 325 
 326     /* for multifont component */
 327     static void DrawWindowText(HDC hDC, jobject font, jstring text,
 328                                int x, int y);
 329     static void DrawGrayText(HDC hDC, jobject font, jstring text,
 330                              int x, int y);
 331 
 332     void DrawListItem(JNIEnv *env, DRAWITEMSTRUCT &drawInfo);
 333 
 334     void MeasureListItem(JNIEnv *env, MEASUREITEMSTRUCT &measureInfo);
 335 
 336     jstring GetItemString(JNIEnv *env, jobject target, jint index);
 337 
 338     jint GetFontHeight(JNIEnv *env);
 339 
 340     virtual jobject PreferredItemSize(JNIEnv *env) {DASSERT(FALSE); return NULL; }
 341 
 342     INLINE BOOL isEnabled() {
 343         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 344         if (env->EnsureLocalCapacity(2) < 0) {
 345             return NULL;
 346         }
 347         jobject self = GetPeer(env);
 348         jobject target = env->GetObjectField(self, AwtObject::targetID);
 349         BOOL e = env->CallBooleanMethod(target, AwtComponent::isEnabledMID);
 350         DASSERT(!safe_ExceptionOccurred(env));
 351 
 352         env->DeleteLocalRef(target);
 353 
 354         return e;
 355     }
 356 
 357     INLINE BOOL isRecursivelyEnabled() {
 358         AwtComponent* p = this;
 359         do {
 360             if (!p->isEnabled()) {
 361                 return FALSE;
 362             }
 363         } while (!p->IsTopLevel() &&
 364             (p = p->GetParent()) != NULL);
 365         return TRUE;
 366     }
 367 
 368     void SendKeyEventToFocusOwner(jint id, jlong when, jint raw, jint cooked,
 369                                   jint modifiers, jint keyLocation, jlong nativeCode,
 370                                   MSG *msg = NULL);
 371     /*
 372      * Allocate and initialize a new java.awt.event.KeyEvent, and
 373      * post it to the peer's target object.  No response is expected
 374      * from the target.
 375      */
 376     void SendKeyEvent(jint id, jlong when, jint raw, jint cooked,
 377                       jint modifiers, jint keyLocation, jlong nativeCode,
 378                       MSG *msg = NULL);
 379 
 380     /*
 381      * Allocate and initialize a new java.awt.event.MouseEvent, and
 382      * post it to the peer's target object.  No response is expected
 383      * from the target.
 384      */
 385     void SendMouseEvent(jint id, jlong when, jint x, jint y,
 386                         jint modifiers, jint clickCount,
 387                         jboolean popupTrigger, jint button = 0,
 388                         MSG *msg = NULL);
 389 
 390     /*
 391      * Allocate and initialize a new java.awt.event.MouseWheelEvent, and
 392      * post it to the peer's target object.  No response is expected
 393      * from the target.
 394      */
 395     void SendMouseWheelEvent(jint id, jlong when, jint x, jint y,
 396                              jint modifiers, jint clickCount,
 397                              jboolean popupTrigger, jint scrollType,
 398                              jint scrollAmount, jint wheelRotation,
 399                              jdouble preciseWheelRotation, MSG *msg = NULL);
 400 
 401     /*
 402      * Allocate and initialize a new java.awt.event.FocusEvent, and
 403      * post it to the peer's target object.  No response is expected
 404      * from the target.
 405      */
 406     void SendFocusEvent(jint id, HWND opposite);
 407 
 408     /* Forward a filtered event directly to the subclassed window.
 409        synthetic should be TRUE iff the message was generated because
 410        of a synthetic Java event, rather than a native event. */
 411     virtual MsgRouting HandleEvent(MSG *msg, BOOL synthetic);
 412 
 413     /* Post a WM_AWT_HANDLE_EVENT message which invokes HandleEvent
 414        on the toolkit thread. This method may pre-filter the messages. */
 415     virtual BOOL PostHandleEventMessage(MSG *msg, BOOL synthetic);
 416 
 417     /* Event->message synthesizer methods. */
 418     void SynthesizeKeyMessage(JNIEnv *env, jobject keyEvent);
 419     void SynthesizeMouseMessage(JNIEnv *env, jobject mouseEvent);
 420 
 421     /* Components which inherit native mouse wheel behavior will
 422      * return TRUE.  These are TextArea, Choice, FileDialog, and
 423      * List.  All other Components return FALSE.
 424      */
 425     virtual BOOL InheritsNativeMouseWheelBehavior();
 426 
 427     /* Determines whether the component is obscured by another window */
 428     // Called on Toolkit thread
 429     static jboolean _IsObscured(void *param);
 430 
 431     /* Invalidate the specified rectangle. */
 432     virtual void Invalidate(RECT* r);
 433 
 434     /* Begin and end deferred window positioning. */
 435     virtual void BeginValidate();
 436     virtual void EndValidate();
 437 
 438     /* Keyboard conversion routines. */
 439     static void InitDynamicKeyMapTable();
 440     static void BuildDynamicKeyMapTable();
 441     static jint GetJavaModifiers();
 442     static jint GetActionModifiers();
 443     static jint GetButton(int mouseButton);
 444     static UINT GetButtonMK(int mouseButton);
 445     static UINT WindowsKeyToJavaKey(UINT windowsKey, UINT modifiers, UINT character, BOOL isDeadKey);
 446     static void JavaKeyToWindowsKey(UINT javaKey, UINT *windowsKey, UINT *modifiers, UINT originalWindowsKey);
 447     static void UpdateDynPrimaryKeymap(UINT wkey, UINT jkeyLegacy, jint keyLocation, UINT modifiers);
 448 
 449     INLINE static void AwtComponent::JavaKeyToWindowsKey(UINT javaKey,
 450                                        UINT *windowsKey, UINT *modifiers)
 451     {
 452         JavaKeyToWindowsKey(javaKey, windowsKey, modifiers, IGNORE_KEY);
 453     }
 454 
 455     enum TransOps {NONE, LOAD, SAVE};
 456 
 457     UINT WindowsKeyToJavaChar(UINT wkey, UINT modifiers, TransOps ops, BOOL &isDeadKey);
 458 
 459     /* routines used for input method support */
 460     void SetInputMethod(jobject im, BOOL useNativeCompWindow);
 461     void SendInputMethodEvent(jint id, jstring text, int cClause,
 462                               int *rgClauseBoundary, jstring *rgClauseReading,
 463                               int cAttrBlock, int *rgAttrBoundary,
 464                               BYTE *rgAttrValue, int commitedTextLength,
 465                               int caretPos, int visiblePos);
 466     void InquireCandidatePosition();
 467     INLINE LPARAM GetCandidateType() { return m_bitsCandType; }
 468     HWND ImmGetHWnd();
 469     HIMC ImmAssociateContext(HIMC himc);
 470     HWND GetProxyFocusOwner();
 471 
 472     INLINE HWND GetProxyToplevelContainer() {
 473         HWND proxyHWnd = GetProxyFocusOwner();
 474         return ::GetAncestor(proxyHWnd, GA_ROOT); // a browser in case of EmbeddedFrame
 475     }
 476 
 477     void CallProxyDefWindowProc(UINT message,
 478                                 WPARAM wParam,
 479                                 LPARAM lParam,
 480                                 LRESULT &retVal,
 481                                 MsgRouting &mr);
 482 
 483     /*
 484      * Windows message handler functions
 485      */
 486     virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
 487     virtual LRESULT DefWindowProc(UINT msg, WPARAM wParam, LPARAM lParam);
 488 
 489     /* return true if msg is processed */
 490     virtual MsgRouting PreProcessMsg(MSG& msg);
 491 
 492     virtual MsgRouting WmCreate() {return mrDoDefault;}
 493     virtual MsgRouting WmClose() {return mrDoDefault;}
 494     virtual MsgRouting WmDestroy();
 495     virtual MsgRouting WmNcDestroy();
 496 
 497     virtual MsgRouting WmActivate(UINT nState, BOOL fMinimized, HWND opposite)
 498     {
 499         return mrDoDefault;
 500     }
 501 
 502     virtual MsgRouting WmEraseBkgnd(HDC hDC, BOOL& didErase)
 503     {
 504         return mrDoDefault;
 505     }
 506 
 507     virtual MsgRouting WmPaint(HDC hDC);
 508     virtual MsgRouting WmGetMinMaxInfo(LPMINMAXINFO lpmmi);
 509     virtual MsgRouting WmMove(int x, int y);
 510     virtual MsgRouting WmSize(UINT type, int w, int h);
 511     virtual MsgRouting WmSizing();
 512     virtual MsgRouting WmShowWindow(BOOL show, UINT status);
 513     virtual MsgRouting WmSetFocus(HWND hWndLost);
 514     virtual MsgRouting WmKillFocus(HWND hWndGot);
 515     virtual MsgRouting WmCtlColor(HDC hDC, HWND hCtrl,
 516                                   UINT ctlColor, HBRUSH& retBrush);
 517     virtual MsgRouting WmHScroll(UINT scrollCode, UINT pos, HWND hScrollBar);
 518     virtual MsgRouting WmVScroll(UINT scrollCode, UINT pos, HWND hScrollBar);
 519 
 520     virtual MsgRouting WmMouseEnter(UINT flags, int x, int y);
 521     virtual MsgRouting WmMouseDown(UINT flags, int x, int y, int button);
 522     virtual MsgRouting WmMouseUp(UINT flags, int x, int y, int button);
 523     virtual MsgRouting WmMouseMove(UINT flags, int x, int y);
 524     virtual MsgRouting WmMouseExit(UINT flags, int x, int y);
 525     virtual MsgRouting WmMouseWheel(UINT flags, int x, int y,
 526                                     int wheelRotation, BOOL isHorizontal);
 527     virtual MsgRouting WmNcMouseDown(WPARAM hitTest, int x, int y, int button);
 528     virtual MsgRouting WmNcMouseUp(WPARAM hitTest, int x, int y, int button);
 529     virtual MsgRouting WmWindowPosChanging(LPARAM windowPos);
 530     virtual MsgRouting WmWindowPosChanged(LPARAM windowPos);
 531 
 532     // NB: 64-bit: vkey is wParam of the message, but other API's take
 533     // vkey parameters of type UINT, so we do the cast before dispatching.
 534     virtual MsgRouting WmKeyDown(UINT vkey, UINT repCnt, UINT flags, BOOL system);
 535     virtual MsgRouting WmKeyUp(UINT vkey, UINT repCnt, UINT flags, BOOL system);
 536 
 537     virtual MsgRouting WmChar(UINT character, UINT repCnt, UINT flags, BOOL system);
 538     virtual MsgRouting WmIMEChar(UINT character, UINT repCnt, UINT flags, BOOL system);
 539     virtual MsgRouting WmInputLangChange(UINT charset, HKL hKeyBoardLayout);
 540     virtual MsgRouting WmForwardChar(WCHAR character, LPARAM lParam,
 541                                      BOOL synthethic);
 542     virtual MsgRouting WmPaste();
 543 
 544     virtual void SetCompositionWindow(RECT &r);
 545     virtual void OpenCandidateWindow(int x, int y);
 546     virtual void SetCandidateWindow(int iCandType, int x, int y);
 547     virtual MsgRouting WmImeSetContext(BOOL fSet, LPARAM *lplParam);
 548     virtual MsgRouting WmImeNotify(WPARAM subMsg, LPARAM bitsCandType);
 549     virtual MsgRouting WmImeStartComposition();
 550     virtual MsgRouting WmImeEndComposition();
 551     virtual MsgRouting WmImeComposition(WORD wChar, LPARAM flags);
 552 
 553     virtual MsgRouting WmTimer(UINT_PTR timerID) {return mrDoDefault;}
 554 
 555     virtual MsgRouting WmCommand(UINT id, HWND hWndCtrl, UINT notifyCode);
 556 
 557     /* reflected WmCommand from parent */
 558     virtual MsgRouting WmNotify(UINT notifyCode);
 559 
 560     virtual MsgRouting WmCompareItem(UINT /*ctrlId*/,
 561                                      COMPAREITEMSTRUCT &compareInfo,
 562                                      LRESULT &result);
 563     virtual MsgRouting WmDeleteItem(UINT /*ctrlId*/,
 564                                     DELETEITEMSTRUCT &deleteInfo);
 565     virtual MsgRouting WmDrawItem(UINT ctrlId,
 566                                   DRAWITEMSTRUCT &drawInfo);
 567     virtual MsgRouting WmMeasureItem(UINT ctrlId,
 568                                      MEASUREITEMSTRUCT &measureInfo);
 569     /* Fix 4181790 & 4223341 : These functions get overridden in owner-drawn
 570      * components instead of the Wm... versions.
 571      */
 572     virtual MsgRouting OwnerDrawItem(UINT ctrlId,
 573                                      DRAWITEMSTRUCT &drawInfo);
 574     virtual MsgRouting OwnerMeasureItem(UINT ctrlId,
 575                                         MEASUREITEMSTRUCT &measureInfo);
 576 
 577     virtual MsgRouting WmPrint(HDC hDC, LPARAM flags);
 578     virtual MsgRouting WmPrintClient(HDC hDC, LPARAM flags);
 579 
 580     virtual MsgRouting WmNcCalcSize(BOOL fCalcValidRects,
 581                                     LPNCCALCSIZE_PARAMS lpncsp,
 582                                     LRESULT &retVal);
 583     virtual MsgRouting WmNcPaint(HRGN hrgn);
 584     virtual MsgRouting WmNcHitTest(UINT x, UINT y, LRESULT &retVal);
 585     virtual MsgRouting WmSysCommand(UINT uCmdType, int xPos, int yPos);
 586     virtual MsgRouting WmEnterSizeMove();
 587     virtual MsgRouting WmExitSizeMove();
 588     virtual MsgRouting WmEnterMenuLoop(BOOL isTrackPopupMenu);
 589     virtual MsgRouting WmExitMenuLoop(BOOL isTrackPopupMenu);
 590 
 591     virtual MsgRouting WmQueryNewPalette(LRESULT &retVal);
 592     virtual MsgRouting WmPaletteChanged(HWND hwndPalChg);
 593     virtual MsgRouting WmPaletteIsChanging(HWND hwndPalChg);
 594     virtual MsgRouting WmStyleChanged(int wStyleType, LPSTYLESTRUCT lpss);
 595     virtual MsgRouting WmSettingChange(UINT wFlag, LPCTSTR pszSection);
 596 
 597     virtual MsgRouting WmContextMenu(HWND hCtrl, UINT xPos, UINT yPos) {
 598         return mrDoDefault;
 599     }
 600 
 601     void UpdateColorModel();
 602 
 603     jintArray CreatePrintedPixels(SIZE &loc, SIZE &size, int alpha);
 604 
 605     /*
 606      * HWND, AwtComponent and Java Peer interaction
 607      *
 608      * Link the C++, Java peer, and HWNDs together.
 609      */
 610     void LinkObjects(JNIEnv *env, jobject peer);
 611 
 612     void UnlinkObjects();
 613 
 614     static BOOL QueryNewPaletteCalled() { return m_QueryNewPaletteCalled; }
 615 
 616 #ifdef DEBUG
 617     virtual void VerifyState(); /* verify component and peer are in sync. */
 618 #else
 619     void VerifyState() {}       /* no-op */
 620 #endif
 621 
 622     virtual AwtDropTarget* CreateDropTarget(JNIEnv* env);
 623     virtual void DestroyDropTarget();
 624 
 625     INLINE virtual HWND GetDBCSEditHandle() { return NULL; }
 626     // State for native drawing API
 627     INLINE jint GetDrawState() { return GetDrawState(m_hwnd); }
 628     INLINE void SetDrawState(jint state) { SetDrawState(m_hwnd, state); }    // State for native drawing API
 629 
 630     INLINE virtual BOOL IsTopLevel() { return FALSE; }
 631     INLINE virtual BOOL IsEmbeddedFrame() { return FALSE; }
 632     INLINE virtual BOOL IsScrollbar() { return FALSE; }
 633 
 634     static INLINE BOOL IsTopLevelHWnd(HWND hwnd) {
 635         AwtComponent *comp = AwtComponent::GetComponent(hwnd);
 636         return (comp != NULL && comp->IsTopLevel());
 637     }
 638     static INLINE BOOL IsEmbeddedFrameHWnd(HWND hwnd) {
 639         AwtComponent *comp = AwtComponent::GetComponent(hwnd);
 640         return (comp != NULL && comp->IsEmbeddedFrame());
 641     }
 642 
 643     static jint GetDrawState(HWND hwnd);
 644     static void SetDrawState(HWND hwnd, jint state);
 645 
 646     static HWND GetHWnd(JNIEnv* env, jobject target);
 647 
 648     static MSG* CreateMessage(UINT message, WPARAM wParam, LPARAM lParam, int x, int y);
 649     static void InitMessage(MSG* msg, UINT message, WPARAM wParam, LPARAM lParam, int x, int y);
 650 
 651     // Some methods to be called on Toolkit thread via Toolkit.InvokeFunction()
 652     static void _Show(void *param);
 653     static void _Hide(void *param);
 654     static void _Enable(void *param);
 655     static void _Disable(void *param);
 656     static jobject _GetLocationOnScreen(void *param);
 657     static void _Reshape(void *param);
 658     static void _ReshapeNoCheck(void *param);
 659     static void _NativeHandleEvent(void *param);
 660     static void _SetForeground(void *param);
 661     static void _SetBackground(void *param);
 662     static void _SetFont(void *param);
 663     static void _Start(void *param);
 664     static void _BeginValidate(void *param);
 665     static void _EndValidate(void *param);
 666     static void _UpdateWindow(void *param);
 667     static jlong _AddNativeDropTarget(void *param);
 668     static void _RemoveNativeDropTarget(void *param);
 669     static jintArray _CreatePrintedPixels(void *param);
 670     static jboolean _NativeHandlesWheelScrolling(void *param);
 671     static void _SetRectangularShape(void *param);
 672     static void _SetZOrder(void *param);
 673 
 674     static HWND sm_focusOwner;
 675 
 676 private:
 677     static HWND sm_focusedWindow;
 678 
 679 public:
 680     static inline HWND GetFocusedWindow() { return sm_focusedWindow; }
 681     static void SetFocusedWindow(HWND window);
 682 
 683     static void _SetFocus(void *param);
 684 
 685     static void *SetNativeFocusOwner(void *self);
 686     static void *GetNativeFocusedWindow();
 687     static void *GetNativeFocusOwner();
 688 
 689     static BOOL sm_inSynthesizeFocus;
 690 
 691     // Execute on Toolkit only.
 692     INLINE static LRESULT SynthesizeWmSetFocus(HWND targetHWnd, HWND oppositeHWnd) {
 693         sm_inSynthesizeFocus = TRUE;
 694         LRESULT res = ::SendMessage(targetHWnd, WM_SETFOCUS, (WPARAM)oppositeHWnd, 0);
 695         sm_inSynthesizeFocus = FALSE;
 696         return res;
 697     }
 698     // Execute on Toolkit only.
 699     INLINE static LRESULT SynthesizeWmKillFocus(HWND targetHWnd, HWND oppositeHWnd) {
 700         sm_inSynthesizeFocus = TRUE;
 701         LRESULT res = ::SendMessage(targetHWnd, WM_KILLFOCUS, (WPARAM)oppositeHWnd, 0);
 702         sm_inSynthesizeFocus = FALSE;
 703         return res;
 704     }
 705 
 706     static BOOL sm_bMenuLoop;
 707     static INLINE BOOL isMenuLoopActive() {
 708         return sm_bMenuLoop;
 709     }
 710 
 711     // when this component is being destroyed, this method is called
 712     // to find out if there are any messages being processed, and if
 713     // there are some then disposal of this component is postponed
 714     virtual BOOL CanBeDeleted() {
 715         return m_MessagesProcessing == 0;
 716     }
 717 
 718     BOOL IsDestroyPaused() const {
 719         return m_bPauseDestroy;
 720     }
 721 
 722 protected:
 723     static AwtComponent* GetComponentImpl(HWND hWnd);
 724 
 725     static int GetClickCount();
 726 
 727     HWND     m_hwnd;
 728     UINT     m_myControlID;     /* its own ID from the view point of parent */
 729     BOOL     m_backgroundColorSet;
 730     BOOL     m_visible;         /* copy of Component.visible */
 731 
 732     static BOOL sm_suppressFocusAndActivation;
 733     static BOOL sm_restoreFocusAndActivation;
 734 
 735     /*
 736      * The function sets the focus-restore flag ON/OFF.
 737      * When the flag is ON, focus is restored immidiately after the proxy loses it.
 738      * All focus messages are suppressed. It's also assumed that sm_focusedWindow and
 739      * sm_focusOwner don't change after the flag is set ON and before it's set OFF.
 740      */
 741     static INLINE void SetRestoreFocus(BOOL doSet) {
 742         sm_suppressFocusAndActivation = doSet;
 743         sm_restoreFocusAndActivation = doSet;
 744     }
 745 
 746     virtual void SetDragCapture(UINT flags);
 747     virtual void ReleaseDragCapture(UINT flags);
 748 
 749     virtual void FillBackground(HDC hMemoryDC, SIZE &size);
 750     virtual void FillAlpha(void *bitmapBits, SIZE &size, BYTE alpha);
 751 
 752     int ScaleUpX(int x);
 753     int ScaleUpY(int y);
 754     int ScaleDownX(int x);
 755     int ScaleDownY(int y);
 756 
 757 private:
 758     /* A bitmask keeps the button's numbers as MK_LBUTTON, MK_MBUTTON, MK_RBUTTON
 759      * which are allowed to
 760      * generate the CLICK event after the RELEASE has happened.
 761      * There are conditions that must be true for that sending CLICK event:
 762      * 1) button was initially PRESSED
 763      * 2) no movement or drag has happened until RELEASE
 764     */
 765     UINT m_mouseButtonClickAllowed;
 766 
 767     BOOL m_bSubclassed;
 768     BOOL m_bPauseDestroy;
 769 
 770     COLORREF m_colorForeground;
 771     COLORREF m_colorBackground;
 772 
 773     AwtPen*  m_penForeground;
 774     AwtBrush* m_brushBackground;
 775 
 776     WNDPROC  m_DefWindowProc;
 777     // counter for messages being processed by this component
 778     UINT     m_MessagesProcessing;
 779 
 780     // provides a unique ID for child controls
 781     UINT     m_nextControlID;
 782 
 783     // DeferWindowPos handle for batched-up window positioning
 784     HDWP     m_hdwp;
 785     // Counter to handle nested calls to Begin/EndValidate
 786     UINT     m_validationNestCount;
 787 
 788     AwtDropTarget* m_dropTarget; // associated DropTarget object
 789 
 790     // When we process WM_INPUTLANGCHANGE we remember the keyboard
 791     // layout handle and associated input language and codepage.
 792     // We also invalidate VK translation table for VK_OEM_* codes
 793     static HKL    m_hkl;
 794     static UINT   m_CodePage;
 795     static LANGID m_idLang;
 796 
 797     static BOOL sm_rtl;
 798     static BOOL sm_rtlReadingOrder;
 799 
 800     static BOOL sm_PrimaryDynamicTableBuilt;
 801 
 802     jobject m_InputMethod;
 803     BOOL    m_useNativeCompWindow;
 804     LPARAM  m_bitsCandType;
 805     UINT    m_PendingLeadByte;
 806 
 807     void SetComponentInHWND();
 808 
 809     // Determines whether a given virtual key is on the numpad
 810     static BOOL IsNumPadKey(UINT vkey, BOOL extended);
 811 
 812     // Determines the keyLocation of a given key
 813     static jint GetKeyLocation(UINT wkey, UINT flags);
 814     static jint GetShiftKeyLocation(UINT wkey, UINT flags);
 815 
 816     // Cache for FindComponent
 817     static HWND sm_cursorOn;
 818 
 819     static BOOL m_QueryNewPaletteCalled;
 820 
 821     static AwtComponent* sm_getComponentCache; // a cache for the GetComponent(..) method.
 822 
 823     int windowMoveLockPosX;
 824     int windowMoveLockPosY;
 825     int windowMoveLockPosCX;
 826     int windowMoveLockPosCY;
 827 
 828     // 6524352: support finer-resolution
 829     int m_wheelRotationAmountX;
 830     int m_wheelRotationAmountY;
 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 #include "ObjectList.h"
 923 
 924 #endif /* AWT_COMPONENT_H */