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