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      */
GetHWnd()165     INLINE HWND GetHWnd() { return m_hwnd; }
SetHWnd(HWND 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      */
GetColor()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();
IsBackgroundColorSet()180     INLINE BOOL IsBackgroundColorSet() { return m_backgroundColorSet; }
181 
182     virtual void SetFont(AwtFont *pFont);
183 
SetText(LPCTSTR text)184     INLINE void SetText(LPCTSTR text) { ::SetWindowText(GetHWnd(), text); }
GetText(LPTSTR buffer,int size)185     INLINE int GetText(LPTSTR buffer, int size) {
186         return ::GetWindowText(GetHWnd(), buffer, size);
187     }
GetTextLength()188     INLINE int GetTextLength() { return ::GetWindowTextLength(GetHWnd()); }
189 
GetInsets(RECT * rect)190     virtual void GetInsets(RECT* rect) {
191         VERIFY(::SetRectEmpty(rect));
192     }
193 
IsVisible()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 */
IsContainer()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      */
CreateControlID()237     INLINE UINT CreateControlID() { return m_nextControlID++; }
238 
239     // returns the current keyboard layout
GetKeyboardLayout()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.
GetCodePage()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
GetRTLReadingOrder()254     INLINE static BOOL GetRTLReadingOrder() {
255         return sm_rtlReadingOrder;
256     }
257     // returns the right to left status
GetRTL()258     INLINE static BOOL GetRTL() {
259         return sm_rtl;
260     }
261     // returns the current sub language
GetSubLanguage()262     INLINE static LANGID GetSubLanguage() {
263         return SUBLANGID(m_idLang);
264     }
265 // end waleed
266 
267     // returns the current input language
GetInputLanguage()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 
GetStyle()303     INLINE virtual LONG GetStyle() {
304         DASSERT(GetHWnd());
305         return ::GetWindowLong(GetHWnd(), GWL_STYLE);
306     }
SetStyle(LONG style)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     }
GetStyleEx()314     INLINE virtual LONG GetStyleEx() {
315         DASSERT(GetHWnd());
316         return ::GetWindowLong(GetHWnd(), GWL_EXSTYLE);
317     }
SetStyleEx(LONG style)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 
NeedDblClick()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 
PreferredItemSize(JNIEnv * env)342     virtual jobject PreferredItemSize(JNIEnv *env) {DASSERT(FALSE); return NULL; }
343 
isEnabled()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 
isRecursivelyEnabled()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 
JavaKeyToWindowsKey(UINT javaKey,UINT * windowsKey,UINT * modifiers)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();
GetCandidateType()468     INLINE LPARAM GetCandidateType() { return m_bitsCandType; }
469     HWND ImmGetHWnd();
470     HIMC ImmAssociateContext(HIMC himc);
471     HWND GetProxyFocusOwner();
472 
GetProxyToplevelContainer()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 
WmCreate()493     virtual MsgRouting WmCreate() {return mrDoDefault;}
WmClose()494     virtual MsgRouting WmClose() {return mrDoDefault;}
495     virtual MsgRouting WmDestroy();
496     virtual MsgRouting WmNcDestroy();
497 
WmActivate(UINT nState,BOOL fMinimized,HWND opposite)498     virtual MsgRouting WmActivate(UINT nState, BOOL fMinimized, HWND opposite)
499     {
500         return mrDoDefault;
501     }
502 
WmEraseBkgnd(HDC hDC,BOOL & didErase)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 
WmTimer(UINT_PTR timerID)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 
WmContextMenu(HWND hCtrl,UINT xPos,UINT yPos)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 
QueryNewPaletteCalled()615     static BOOL QueryNewPaletteCalled() { return m_QueryNewPaletteCalled; }
616 
617 #ifdef DEBUG
618     virtual void VerifyState(); /* verify component and peer are in sync. */
619 #else
VerifyState()620     void VerifyState() {}       /* no-op */
621 #endif
622 
623     virtual AwtDropTarget* CreateDropTarget(JNIEnv* env);
624     virtual void DestroyDropTarget();
625 
GetDBCSEditHandle()626     INLINE virtual HWND GetDBCSEditHandle() { return NULL; }
627     // State for native drawing API
GetDrawState()628     INLINE jint GetDrawState() { return GetDrawState(m_hwnd); }
SetDrawState(jint state)629     INLINE void SetDrawState(jint state) { SetDrawState(m_hwnd, state); }    // State for native drawing API
630 
IsTopLevel()631     INLINE virtual BOOL IsTopLevel() { return FALSE; }
IsEmbeddedFrame()632     INLINE virtual BOOL IsEmbeddedFrame() { return FALSE; }
IsScrollbar()633     INLINE virtual BOOL IsScrollbar() { return FALSE; }
634 
IsTopLevelHWnd(HWND hwnd)635     static INLINE BOOL IsTopLevelHWnd(HWND hwnd) {
636         AwtComponent *comp = AwtComponent::GetComponent(hwnd);
637         return (comp != NULL && comp->IsTopLevel());
638     }
IsEmbeddedFrameHWnd(HWND hwnd)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 _SetParent(void * param);
673     static void _SetRectangularShape(void *param);
674     static void _SetZOrder(void *param);
675 
676     static HWND sm_focusOwner;
677 
678 private:
679     static HWND sm_focusedWindow;
680 
681 public:
GetFocusedWindow()682     static inline HWND GetFocusedWindow() { return sm_focusedWindow; }
683     static void SetFocusedWindow(HWND window);
684 
685     static void _SetFocus(void *param);
686 
687     static void *SetNativeFocusOwner(void *self);
688     static void *GetNativeFocusedWindow();
689     static void *GetNativeFocusOwner();
690 
691     static BOOL sm_inSynthesizeFocus;
692 
693     // Execute on Toolkit only.
SynthesizeWmSetFocus(HWND targetHWnd,HWND oppositeHWnd)694     INLINE static LRESULT SynthesizeWmSetFocus(HWND targetHWnd, HWND oppositeHWnd) {
695         sm_inSynthesizeFocus = TRUE;
696         LRESULT res = ::SendMessage(targetHWnd, WM_SETFOCUS, (WPARAM)oppositeHWnd, 0);
697         sm_inSynthesizeFocus = FALSE;
698         return res;
699     }
700     // Execute on Toolkit only.
SynthesizeWmKillFocus(HWND targetHWnd,HWND oppositeHWnd)701     INLINE static LRESULT SynthesizeWmKillFocus(HWND targetHWnd, HWND oppositeHWnd) {
702         sm_inSynthesizeFocus = TRUE;
703         LRESULT res = ::SendMessage(targetHWnd, WM_KILLFOCUS, (WPARAM)oppositeHWnd, 0);
704         sm_inSynthesizeFocus = FALSE;
705         return res;
706     }
707 
708     static BOOL sm_bMenuLoop;
isMenuLoopActive()709     static INLINE BOOL isMenuLoopActive() {
710         return sm_bMenuLoop;
711     }
712 
713     // when this component is being destroyed, this method is called
714     // to find out if there are any messages being processed, and if
715     // there are some then disposal of this component is postponed
CanBeDeleted()716     virtual BOOL CanBeDeleted() {
717         return m_MessagesProcessing == 0;
718     }
719 
IsDestroyPaused()720     BOOL IsDestroyPaused() const {
721         return m_bPauseDestroy;
722     }
723 
724 protected:
725     static AwtComponent* GetComponentImpl(HWND hWnd);
726 
727     static int GetClickCount();
728 
729     HWND     m_hwnd;
730     UINT     m_myControlID;     /* its own ID from the view point of parent */
731     BOOL     m_backgroundColorSet;
732     BOOL     m_visible;         /* copy of Component.visible */
733 
734     static BOOL sm_suppressFocusAndActivation;
735     static BOOL sm_restoreFocusAndActivation;
736 
737     /*
738      * The function sets the focus-restore flag ON/OFF.
739      * When the flag is ON, focus is restored immidiately after the proxy loses it.
740      * All focus messages are suppressed. It's also assumed that sm_focusedWindow and
741      * sm_focusOwner don't change after the flag is set ON and before it's set OFF.
742      */
SetRestoreFocus(BOOL doSet)743     static INLINE void SetRestoreFocus(BOOL doSet) {
744         sm_suppressFocusAndActivation = doSet;
745         sm_restoreFocusAndActivation = doSet;
746     }
747 
748     virtual void SetDragCapture(UINT flags);
749     virtual void ReleaseDragCapture(UINT flags);
750 
751     virtual void FillBackground(HDC hMemoryDC, SIZE &size);
752     virtual void FillAlpha(void *bitmapBits, SIZE &size, BYTE alpha);
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_touchDownOccurred;
765     BOOL m_touchUpOccurred;
766     POINT m_touchDownPoint;
767     POINT m_touchUpPoint;
768 
769     BOOL m_bSubclassed;
770     BOOL m_bPauseDestroy;
771 
772     COLORREF m_colorForeground;
773     COLORREF m_colorBackground;
774 
775     AwtPen*  m_penForeground;
776     AwtBrush* m_brushBackground;
777 
778     WNDPROC  m_DefWindowProc;
779     // counter for messages being processed by this component
780     UINT     m_MessagesProcessing;
781 
782     // provides a unique ID for child controls
783     UINT     m_nextControlID;
784 
785     // DeferWindowPos handle for batched-up window positioning
786     HDWP     m_hdwp;
787     // Counter to handle nested calls to Begin/EndValidate
788     UINT     m_validationNestCount;
789 
790     AwtDropTarget* m_dropTarget; // associated DropTarget object
791 
792     // When we process WM_INPUTLANGCHANGE we remember the keyboard
793     // layout handle and associated input language and codepage.
794     // We also invalidate VK translation table for VK_OEM_* codes
795     static HKL    m_hkl;
796     static UINT   m_CodePage;
797     static LANGID m_idLang;
798 
799     static BOOL sm_rtl;
800     static BOOL sm_rtlReadingOrder;
801 
802     static BOOL sm_PrimaryDynamicTableBuilt;
803 
804     jobject m_InputMethod;
805     BOOL    m_useNativeCompWindow;
806     LPARAM  m_bitsCandType;
807     UINT    m_PendingLeadByte;
808 
809     void SetComponentInHWND();
810 
811     // Determines whether a given virtual key is on the numpad
812     static BOOL IsNumPadKey(UINT vkey, BOOL extended);
813 
814     // Determines the keyLocation of a given key
815     static jint GetKeyLocation(UINT wkey, UINT flags);
816     static jint GetShiftKeyLocation(UINT wkey, UINT flags);
817 
818     // Cache for FindComponent
819     static HWND sm_cursorOn;
820 
821     static BOOL m_QueryNewPaletteCalled;
822 
823     static AwtComponent* sm_getComponentCache; // a cache for the GetComponent(..) method.
824 
825     int windowMoveLockPosX;
826     int windowMoveLockPosY;
827     int windowMoveLockPosCX;
828     int windowMoveLockPosCY;
829 
830     // 6524352: support finer-resolution
831     int m_wheelRotationAmount;
832 
833     BOOL deadKeyActive;
834 
835     /*
836      * The association list of children's IDs and corresponding components.
837      * Some components like Choice or List are required their sizes while
838      * the creations of themselfs are in progress.
839      */
840     class ChildListItem {
841     public:
ChildListItem(UINT id,AwtComponent * component)842         ChildListItem(UINT id, AwtComponent* component) {
843             m_ID = id;
844             m_Component = component;
845             m_next = NULL;
846         }
~ChildListItem()847         ~ChildListItem() {
848             if (m_next != NULL)
849                 delete m_next;
850         }
851 
852         UINT m_ID;
853         AwtComponent* m_Component;
854         ChildListItem* m_next;
855     };
856 
857 public:
PushChild(UINT id,AwtComponent * component)858     INLINE void PushChild(UINT id, AwtComponent* component) {
859         ChildListItem* child = new ChildListItem(id, component);
860         child->m_next = m_childList;
861         m_childList = child;
862     }
863 
864     static void SetParent(void * param);
865 private:
866     AwtComponent* SearchChild(UINT id);
867     void RemoveChild(UINT id) ;
868     static BOOL IsNavigationKey(UINT wkey);
869     static void BuildPrimaryDynamicTable();
870 
871     ChildListItem* m_childList;
872 
873     HCURSOR m_hCursorCache; // the latest cursor which has been active within the heavyweight component
874 public:
setCursorCache(HCURSOR hCursor)875     inline void setCursorCache(HCURSOR hCursor) {
876         m_hCursorCache = hCursor;
877     }
getCursorCache()878     inline HCURSOR getCursorCache() {
879         return m_hCursorCache;
880     }
881 };
882 
883 class CounterHelper {
884 private:
885     UINT *m_counter;
886 public:
CounterHelper(UINT * counter)887     explicit CounterHelper(UINT *counter) {
888         m_counter = counter;
889         (*m_counter)++;
890     }
~CounterHelper()891     ~CounterHelper() {
892         (*m_counter)--;
893         m_counter = NULL;
894     }
895 };
896 
897 // DC management objects; these classes are used to track the list of
898 // DC's associated with a given Component.  Then DC's can be released
899 // appropriately on demand or on window destruction to avoid resource
900 // leakage.
901 class DCItem {
902 public:
903     HDC             hDC;
904     HWND            hWnd;
905     DCItem          *next;
906 };
907 class DCList {
908     DCItem          *head;
909     CriticalSection listLock;
910 public:
DCList()911     DCList() { head = NULL; }
912 
913     void            AddDC(HDC hDC, HWND hWnd);
914     void            AddDCItem(DCItem *newItem);
915     DCItem          *RemoveDC(HDC hDC, HWND hWnd);
916     DCItem          *RemoveAllDCs(HWND hWnd);
917     void            RealizePalettes(int screen);
918 };
919 
920 void ReleaseDCList(HWND hwnd, DCList &list);
921 void MoveDCToPassiveList(HDC hDC, HWND hWnd);
922 
923 namespace TimeHelper{
924     jlong getMessageTimeUTC();
925     jlong windowsToUTC(DWORD event_offset);
926 }
927 
928 #include "ObjectList.h"
929 
930 #endif /* AWT_COMPONENT_H */
931