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