1 /*
   2  * Copyright (c) 2011, 2015, 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 _GLASS_APPLICATION_
  27 #define _GLASS_APPLICATION_
  28 
  29 #include "BaseWnd.h"
  30 
  31 
  32 class Action {
  33 public:
  34     virtual void Do() = 0;
  35     virtual ~Action() {}
  36 };
  37 
  38 #define ENTER_MAIN_THREAD_AND_RETURN(RetType) \
  39     class _MyAction : public Action {    \
  40         public: \
  41                 RetType _retValue;  \
  42                 virtual void Do() {   \
  43                     _retValue = _UserDo(); \
  44                 }   \
  45                 RetType _UserDo()
  46 
  47 #define ENTER_MAIN_THREAD() \
  48     class _MyAction : public Action {    \
  49         public: \
  50                 virtual void Do()
  51 
  52 #define LEAVE_MAIN_THREAD   \
  53     } _action;
  54 
  55 #define LEAVE_MAIN_THREAD_LATER   \
  56     };                                      \
  57     _MyAction * _pAction = new _MyAction(); \
  58     _MyAction & _action = *_pAction;
  59 
  60 #define ARG(var) _action.var
  61 #define DECL_JREF(T, var) JGlobalRef<T> var
  62 #define DECL_jobject(var) DECL_JREF(jobject, var)
  63 
  64 #define PERFORM() GlassApplication::ExecAction(&_action)
  65 
  66 #define PERFORM_AND_RETURN() (PERFORM(), _action._retValue)
  67 
  68 #define PERFORM_LATER() GlassApplication::ExecActionLater(_pAction)
  69 
  70 #define WM_DO_ACTION        (WM_USER+1)
  71 #define WM_DO_ACTION_LATER  (WM_USER+2)
  72 
  73 class GlassApplication : protected BaseWnd {
  74 public:
  75     GlassApplication(jobject jrefThis);
  76     virtual ~GlassApplication();
  77 
  78     static HWND GetToolkitHWND() { return  (NULL == pInstance) ? NULL : pInstance->GetHWND(); }
  79     static GlassApplication *GetInstance() { return pInstance; }
  80     static void ExecAction(Action *action);
  81     static void ExecActionLater(Action *action);
  82     void RegisterClipboardViewer(jobject clipboard);
  83     void UnregisterClipboardViewer();
  84     static jstring GetThemeName(JNIEnv* env);
  85 
  86     inline static DWORD GetMainThreadId()
  87     {
  88         return pInstance == NULL ? 0 : pInstance->m_mainThreadId;
  89     }
  90 
  91     static jobject EnterNestedEventLoop(JNIEnv * env);
  92     static void LeaveNestedEventLoop(JNIEnv * env, jobject retValue);
  93     static void SetGlassClassLoader(JNIEnv *env, jobject classLoader);
  94     static jclass ClassForName(JNIEnv *env, char *className);
  95 
  96     static void SetHInstance(HINSTANCE hInstace) { GlassApplication::hInstace = hInstace; }
  97     static HINSTANCE GetHInstance() { return GlassApplication::hInstace; }
  98 
  99     /* Maintains a counter. Must be balanced with UninstallMouseLLHook. */
 100     static void InstallMouseLLHook();
 101     static void UninstallMouseLLHook();
 102 
 103     static ULONG IncrementAccessibility();
 104     static ULONG DecrementAccessibility();
 105     static ULONG GetAccessibilityCount();
 106 
 107     static jfloat overrideUIScale;
 108 
 109     inline static jboolean IsUIScaleOverridden()
 110     {
 111         return (overrideUIScale > 0.0f);
 112     }
 113 
 114     inline static jfloat GetUIScale(UINT dpi)
 115     {
 116         return IsUIScaleOverridden() ? overrideUIScale : dpi / 96.0f;
 117     }
 118 
 119 protected:
 120     virtual LRESULT WindowProc(UINT msg, WPARAM wParam, LPARAM lParam);
 121     virtual LPCTSTR GetWindowClassNameSuffix();
 122 
 123 private:
 124     jobject m_grefThis;
 125     jobject m_clipboard;
 126     static GlassApplication *pInstance;
 127     HWND    m_hNextClipboardView;
 128     DWORD m_mainThreadId;
 129     static jobject sm_glassClassLoader;
 130 
 131     // These are static because the GlassApplication instance may be
 132     // destroyed while the nested loop is spinning
 133     static bool sm_shouldLeaveNestedLoop;
 134     static JGlobalRef<jobject> sm_nestedLoopReturnValue;
 135 
 136     static HINSTANCE hInstace;
 137     static unsigned int sm_mouseLLHookCounter;
 138     static HHOOK sm_hMouseLLHook;
 139     static LRESULT CALLBACK MouseLLHook(int nCode, WPARAM wParam, LPARAM lParam);
 140     static ULONG s_accessibilityCount;
 141 };
 142 
 143 
 144 #endif //_GLASS_APPLICATION_