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