1 /*
2  * Copyright (c) 1996, 2020, 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_WINDOW_H
27 #define AWT_WINDOW_H
28 
29 #include "awt_Canvas.h"
30 
31 #include "java_awt_Window.h"
32 #include "sun_awt_windows_WWindowPeer.h"
33 
34 // property name tagging windows disabled by modality
35 static LPCTSTR ModalBlockerProp = TEXT("SunAwtModalBlockerProp");
36 static LPCTSTR ModalDialogPeerProp = TEXT("SunAwtModalDialogPeerProp");
37 static LPCTSTR NativeDialogWndProcProp = TEXT("SunAwtNativeDialogWndProcProp");
38 
39 #ifndef WH_MOUSE_LL
40 #define WH_MOUSE_LL 14
41 #endif
42 
43 class AwtFrame;
44 
45 /************************************************************************
46  * AwtWindow class
47  */
48 
49 class AwtWindow : public AwtCanvas {
50 public:
51 
52     /* java.awt.Window field ids */
53     static jfieldID warningStringID;
54     static jfieldID locationByPlatformID;
55     static jfieldID screenID; /* screen number passed over from WindowPeer */
56     static jfieldID autoRequestFocusID;
57     static jfieldID securityWarningWidthID;
58     static jfieldID securityWarningHeightID;
59 
60     /* sun.awt.windows.WWindowPeer field and method IDs */
61     // The coordinates at the peer.
62     static jfieldID sysXID;
63     static jfieldID sysYID;
64     static jfieldID sysWID;
65     static jfieldID sysHID;
66 
67     static jfieldID windowTypeID;
68     static jmethodID notifyWindowStateChangedMID;
69 
70     /* java.awt.Window method IDs */
71     static jmethodID getWarningStringMID;
72     static jmethodID calculateSecurityWarningPositionMID;
73     static jmethodID windowTypeNameMID;
74 
75     AwtWindow();
76     virtual ~AwtWindow();
77 
78     virtual void Dispose();
79 
80     virtual LPCTSTR GetClassName();
81     virtual void FillClassInfo(WNDCLASSEX *lpwc);
82 
83     static AwtWindow* Create(jobject self, jobject parent);
84 
85     // Returns TRUE if this Window is equal to or one of owners of wnd
86     BOOL IsOneOfOwnersOf(AwtWindow * wnd);
87 
88     /* Update the insets for this Window (container), its peer &
89      * optional other
90      */
91     BOOL UpdateInsets(jobject insets = 0);
92     BOOL HasValidRect();
93 
94     static BOOL CALLBACK UpdateOwnedIconCallback(HWND hwnd, LPARAM param);
95 
GetOwningFrameOrDialog()96     INLINE AwtFrame * GetOwningFrameOrDialog() { return m_owningFrameDialog; }
97 
98     HWND GetTopLevelHWnd();
99 
100     /* Subtract inset values from a window origin. */
SubtractInsetPoint(int & x,int & y)101     INLINE void SubtractInsetPoint(int& x, int& y) {
102         x -= m_insets.left;
103         y -= m_insets.top;
104     }
105 
GetInsets(RECT * rect)106     virtual void GetInsets(RECT* rect) {
107         VERIFY(::CopyRect(rect, &m_insets));
108     }
109 
110     /* to make embedded frames easier */
IsEmbeddedFrame()111     virtual BOOL IsEmbeddedFrame() { return FALSE;}
112 
113     /* We can hold children */
IsContainer()114     virtual BOOL IsContainer() { return TRUE;}
115 
IsUndecorated()116     virtual BOOL IsUndecorated() { return TRUE; }
117 
IsSimpleWindow()118     INLINE virtual BOOL IsSimpleWindow() { return TRUE; }
119 
IsRetainingHierarchyZOrder()120     INLINE BOOL IsRetainingHierarchyZOrder() { return m_isRetainingHierarchyZOrder; }
121 
122     /* WARNING: don't invoke on Toolkit thread! */
IsAutoRequestFocus()123     INLINE BOOL IsAutoRequestFocus() {
124         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
125         return env->GetBooleanField(GetTarget(env), AwtWindow::autoRequestFocusID);
126     }
127 
IsFocusedWindowModalBlocker()128     INLINE virtual BOOL IsFocusedWindowModalBlocker() {
129         return FALSE;
130     }
131 
132     virtual void Invalidate(RECT* r);
133     virtual void Show();
134     virtual void SetResizable(BOOL isResizable);
135     BOOL IsResizable();
136     virtual void RecalcNonClient();
137     virtual void RedrawNonClient();
138     virtual int  GetScreenImOn();
139     virtual void CheckIfOnNewScreen();
140     virtual void Grab();
141     virtual void Ungrab();
142     virtual void Ungrab(BOOL doPost);
143     virtual void SetIconData(JNIEnv* env, jintArray iconData, jint w, jint h,
144                              jintArray smallIconData, jint smw, jint smh);
145     virtual void DoUpdateIcon();
GetHIcon()146     INLINE HICON GetHIcon() {return m_hIcon;};
GetHIconSm()147     INLINE HICON GetHIconSm() {return m_hIconSm;};
IsIconInherited()148     INLINE BOOL IsIconInherited() {return m_iconInherited;};
IsLightweightFrame()149     INLINE virtual BOOL IsLightweightFrame() {return FALSE;}
150 
151     /* Post events to the EventQueue */
152     void SendComponentEvent(jint eventId);
153     void SendWindowEvent(jint id, HWND opposite = NULL,
154                          jint oldState = 0, jint newState = 0);
155     void NotifyWindowStateChanged(jint oldState, jint newState);
156 
157     BOOL IsFocusableWindow();
158 
159     /* some helper methods about blocking windows by modal dialogs */
GetModalBlocker(HWND window)160     INLINE static HWND GetModalBlocker(HWND window) {
161         return reinterpret_cast<HWND>(::GetProp(window, ModalBlockerProp));
162     }
163     static void SetModalBlocker(HWND window, HWND blocker);
164     static void SetAndActivateModalBlocker(HWND window, HWND blocker);
165 
166     static HWND GetTopmostModalBlocker(HWND window);
167 
168     /*
169      * Windows message handler functions
170      */
171     virtual MsgRouting WmActivate(UINT nState, BOOL fMinimized, HWND opposite);
172     virtual MsgRouting WmCreate();
173     virtual MsgRouting WmClose();
174     virtual MsgRouting WmDestroy();
175     virtual MsgRouting WmShowWindow(BOOL show, UINT status);
176     virtual MsgRouting WmGetMinMaxInfo(LPMINMAXINFO lpmmi);
177     virtual MsgRouting WmMove(int x, int y);
178     virtual MsgRouting WmSize(UINT type, int w, int h);
179     virtual MsgRouting WmSizing();
180     virtual MsgRouting WmPaint(HDC hDC);
181     virtual MsgRouting WmSettingChange(UINT wFlag, LPCTSTR pszSection);
182     virtual MsgRouting WmNcCalcSize(BOOL fCalcValidRects,
183                                     LPNCCALCSIZE_PARAMS lpncsp, LRESULT& retVal);
184     virtual MsgRouting WmNcHitTest(UINT x, UINT y, LRESULT& retVal);
185     virtual MsgRouting WmNcMouseDown(WPARAM hitTest, int x, int y, int button);
186     virtual MsgRouting WmGetIcon(WPARAM iconType, LRESULT& retVal);
187     virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
188     virtual MsgRouting WmWindowPosChanging(LPARAM windowPos);
189     virtual MsgRouting WmWindowPosChanged(LPARAM windowPos);
190     virtual MsgRouting WmTimer(UINT_PTR timerID);
191 
192     virtual MsgRouting HandleEvent(MSG *msg, BOOL synthetic);
193     virtual void WindowResized();
194 
195     static jboolean _RequestWindowFocus(void *param);
196 
197     virtual BOOL AwtSetActiveWindow(BOOL isMouseEventCause = FALSE, UINT hittest = HTCLIENT);
198 
199     // Execute on Toolkit only.
SynthesizeWmActivate(BOOL doActivate,HWND targetHWnd,HWND oppositeHWnd)200     INLINE static LRESULT SynthesizeWmActivate(BOOL doActivate, HWND targetHWnd, HWND oppositeHWnd) {
201         AwtWindow *win = static_cast<AwtWindow*>(AwtComponent::GetComponent(targetHWnd));
202         if (doActivate &&
203             (!::IsWindowVisible(targetHWnd) || ::IsIconic(::GetAncestor(targetHWnd, GA_ROOT))) &&
204             (win == NULL || !win->IsLightweightFrame()))
205         {
206             // The activation is rejected if either:
207             // - The toplevel is not visible
208             // - The toplevel (or its embedder) is minimised
209             return 1;
210         }
211         return ::SendMessage(targetHWnd, WM_ACTIVATE,
212                              MAKEWPARAM(doActivate ? WA_ACTIVE : WA_INACTIVE, FALSE),
213                              (LPARAM) oppositeHWnd);
214     }
215 
216     void moveToDefaultLocation(); /* moves Window to X,Y specified by Window Manger */
217 
218     void UpdateWindow(JNIEnv* env, jintArray data, int width, int height,
219                       HBITMAP hNewBitmap = NULL);
220 
IsTopLevel()221     INLINE virtual BOOL IsTopLevel() { return TRUE; }
GetGrabbedWindow()222     static AwtWindow * GetGrabbedWindow() { return m_grabbedWindow; }
223 
224     static void FlashWindowEx(HWND hWnd, UINT count, DWORD timeout, DWORD flags);
225 
226     // some methods invoked on Toolkit thread
227     static void _ToFront(void *param);
228     static void _ToBack(void *param);
229     static void _Grab(void *param);
230     static void _Ungrab(void *param);
231     static void _SetAlwaysOnTop(void *param);
232     static void _SetTitle(void *param);
233     static void _SetResizable(void *param);
234     static void _UpdateInsets(void *param);
235     static void _ReshapeFrame(void *param);
236     static void _SetIconImagesData(void * param);
237     static void _SetMinSize(void* param);
238     static jint _GetScreenImOn(void *param);
239     static void _SetFocusableWindow(void *param);
240     static void _SetModalExcludedNativeProp(void *param);
241     static void _ModalDisable(void *param);
242     static void _ModalEnable(void *param);
243     static void _SetOpacity(void* param);
244     static void _SetOpaque(void* param);
245     static void _UpdateWindow(void* param);
246     static void _RepositionSecurityWarning(void* param);
247     static void _SetFullScreenExclusiveModeState(void* param);
248     static void _OverrideHandle(void *param);
249 
IsResizing()250     inline static BOOL IsResizing() {
251         return sm_resizing;
252     }
253 
254     virtual void CreateHWnd(JNIEnv *env, LPCWSTR title,
255             DWORD windowStyle, DWORD windowExStyle,
256             int x, int y, int w, int h,
257             HWND hWndParent, HMENU hMenu,
258             COLORREF colorForeground, COLORREF colorBackground,
259             jobject peer);
260     virtual void DestroyHWnd();
261 
262     static void FocusedWindowChanged(HWND from, HWND to);
263 
GetOverriddenHWnd()264     inline HWND GetOverriddenHWnd() { return m_overriddenHwnd; }
OverrideHWnd(HWND hwnd)265     inline void OverrideHWnd(HWND hwnd) { m_overriddenHwnd = hwnd; }
266 
267 private:
268     static int ms_instanceCounter;
269     static HHOOK ms_hCBTFilter;
270     static LRESULT CALLBACK CBTFilter(int nCode, WPARAM wParam, LPARAM lParam);
271     static BOOL sm_resizing;        /* in the middle of a resizing operation */
272 
273     RECT m_insets;          /* a cache of the insets being used */
274     RECT m_old_insets;      /* help determine if insets change */
275     POINT m_sizePt;         /* the last value of WM_SIZE */
276     RECT m_warningRect;     /* The window's warning banner area, if any. */
277     AwtFrame *m_owningFrameDialog; /* The nearest Frame/Dialog which owns us */
278     BOOL m_isFocusableWindow; /* a cache of Window.isFocusableWindow() return value */
279     POINT m_minSize;          /* Minimum size of the window for WM_GETMINMAXINFO message */
280     BOOL m_grabbed; // Whether the current window is grabbed
281     BOOL m_isRetainingHierarchyZOrder; // Is this a window that shouldn't change z-order of any window
282                                        // from its hierarchy when shown. Currently applied to instances of
283                                        // javax/swing/Popup$HeavyWeightWindow class.
284 
285     // SetTranslucency() is the setter for the following two fields
286     BYTE m_opacity;         // The opacity level. == 0xff by default (when opacity mode is disabled)
287     BOOL m_opaque;          // Whether the window uses the perpixel translucency (false), or not (true).
288 
getOpacity()289     inline BYTE getOpacity() {
290         return m_opacity;
291     }
292 
isOpaque()293     inline BOOL isOpaque() {
294         return m_opaque;
295     }
296 
297     CRITICAL_SECTION contentBitmapCS;
298     HBITMAP hContentBitmap;
299     UINT contentWidth;
300     UINT contentHeight;
301 
302     void SetTranslucency(BYTE opacity, BOOL opaque, BOOL setValues = TRUE,
303             BOOL useDefaultForOldValues = FALSE);
304     void UpdateWindow(int width, int height, HBITMAP hBitmap);
305     void UpdateWindowImpl(int width, int height, HBITMAP hBitmap);
306     void RedrawWindow();
307     void DeleteContentBitmap();
308 
309     static UINT untrustedWindowsCounter;
310 
311     WCHAR * warningString;
312 
313     // The warning icon
314     HWND warningWindow;
315     // The tooltip that appears when hovering the icon
316     HWND securityTooltipWindow;
317 
318     //Allows substitute parent window with JavaFX stage to make it below a dialog
319     HWND m_overriddenHwnd;
320 
321     UINT warningWindowWidth;
322     UINT warningWindowHeight;
323     void InitSecurityWarningSize(JNIEnv *env);
324     HICON GetSecurityWarningIcon();
325 
326     void CreateWarningWindow(JNIEnv *env);
327     void DestroyWarningWindow();
328     static LPCTSTR GetWarningWindowClassName();
329     void FillWarningWindowClassInfo(WNDCLASS *lpwc);
330     void RegisterWarningWindowClass();
331     void UnregisterWarningWindowClass();
332     static LRESULT CALLBACK WarningWindowProc(
333             HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
334 
335     static void PaintWarningWindow(HWND warningWindow);
336     static void PaintWarningWindow(HWND warningWindow, HDC hdc);
337     void RepaintWarningWindow();
338     void CalculateWarningWindowBounds(JNIEnv *env, LPRECT rect);
339 
340     void AnimateSecurityWarning(bool enable);
341     UINT securityWarningAnimationStage;
342 
343     enum AnimationKind {
344         akNone, akShow, akPreHide, akHide
345     };
346 
347     AnimationKind securityAnimationKind;
348 
349     void StartSecurityAnimation(AnimationKind kind);
350     void StopSecurityAnimation();
351 
352     void RepositionSecurityWarning(JNIEnv *env);
353 
354     static void SetLayered(HWND window, bool layered);
355     static bool IsLayered(HWND window);
356 
357     BOOL fullScreenExclusiveModeState;
setFullScreenExclusiveModeState(BOOL isEntered)358     inline void setFullScreenExclusiveModeState(BOOL isEntered) {
359         fullScreenExclusiveModeState = isEntered;
360         UpdateSecurityWarningVisibility();
361     }
isFullScreenExclusiveMode()362     inline BOOL isFullScreenExclusiveMode() {
363         return fullScreenExclusiveModeState;
364     }
365 
366 
367 public:
368     void UpdateSecurityWarningVisibility();
369     static bool IsWarningWindow(HWND hWnd);
370 
371 protected:
372     BOOL m_isResizable;
373     static AwtWindow* m_grabbedWindow; // Current grabbing window
374     HICON m_hIcon;            /* Icon for this window. It can be set explicitely or inherited from the owner */
375     HICON m_hIconSm;          /* Small icon for this window. It can be set explicitely or inherited from the owner */
376     BOOL m_iconInherited;     /* TRUE if icon is inherited from the owner */
377     BOOL m_filterFocusAndActivation; /* Used in the WH_CBT hook */
378 
IsUntrusted()379     inline BOOL IsUntrusted() {
380         return warningString != NULL;
381     }
382 
383     UINT currentWmSizeState;
384 
385     void EnableTranslucency(BOOL enable);
386 
387     // Native representation of the java.awt.Window.Type enum
388     enum Type {
389         NORMAL, UTILITY, POPUP
390     };
391 
GetType()392     inline Type GetType() { return m_windowType; }
393 
394 private:
395     int m_screenNum;
396 
397     void InitOwner(AwtWindow *owner);
398 
399     Type m_windowType;
400     void InitType(JNIEnv *env, jobject peer);
401 
402     // Tweak the style according to the type of the window
403     void TweakStyle(DWORD & style, DWORD & exStyle);
404 
405     // Set in _SetAlwaysOnTop()
406     bool m_alwaysOnTop;
407 public:
IsAlwaysOnTop()408     inline bool IsAlwaysOnTop() { return m_alwaysOnTop; }
409 };
410 
411 #endif /* AWT_WINDOW_H */
412