1 /*
   2  * Copyright (c) 1996, 2012, 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_MENUITEM_H
  27 #define AWT_MENUITEM_H
  28 
  29 #include "awt_Object.h"
  30 #include "awt_Component.h"
  31 
  32 #include <java_awt_MenuItem.h>
  33 #include <sun_awt_windows_WMenuItemPeer.h>
  34 #include <java_awt_Menu.h>
  35 #include <sun_awt_windows_WMenuPeer.h>
  36 #include <java_awt_MenuComponent.h>
  37 #include <java_awt_FontMetrics.h>
  38 
  39 class AwtMenu;
  40 
  41 
  42 /************************************************************************
  43  * MenuItem class
  44  */
  45 
  46 class AwtMenuItem : public AwtObject {
  47 public:
  48     /* java.awt.MenuComponent fields */
  49     static jfieldID fontID;
  50     static jfieldID appContextID;
  51 
  52     /* java.awt.MenuItem fields */
  53     static jfieldID labelID;
  54     static jfieldID enabledID;
  55 
  56     /* java.awt.CheckboxMenuItem fields */
  57     static jfieldID stateID;
  58 
  59     /* sun.awt.windows.WMenuItemPeer fields */
  60     static jfieldID isCheckboxID;
  61     static jfieldID shortcutLabelID;
  62 
  63     static jmethodID getDefaultFontMID;
  64 
  65     AwtMenuItem();
  66     virtual ~AwtMenuItem();
  67 
  68     virtual void Dispose();
  69 
  70     virtual LPCTSTR GetClassName();
  71 
  72     static AwtMenuItem* Create(jobject self, jobject menu);
  73 
  74     INLINE AwtMenu* GetMenuContainer() { return m_menuContainer; }
  75     INLINE void SetMenuContainer(AwtMenu* menu) { m_menuContainer = menu; }
  76     INLINE UINT GetID() { return m_Id; }
  77     INLINE void SetID(UINT id) { m_Id = id; }
  78     INLINE void SetNewID() {
  79         DASSERT(!m_freeId);
  80         m_Id = AwtToolkit::GetInstance().CreateCmdID(this);
  81         m_freeId = TRUE;
  82     }
  83 
  84     // Convert Language ID to CodePage
  85     static UINT LangToCodePage(LANGID idLang);
  86     /* Execute the command associated with this item. */
  87     virtual void DoCommand();
  88 
  89     void LinkObjects(JNIEnv *env, jobject peer);
  90 
  91     /* for multifont menuitem */
  92     INLINE jstring GetJavaString(JNIEnv *env) {
  93         if (env->EnsureLocalCapacity(2) < 0) {
  94             return NULL;
  95         }
  96         jobject target = GetTarget(env);
  97         jstring res = (jstring)env->GetObjectField(target,
  98                                                    AwtMenuItem::labelID);
  99         env->DeleteLocalRef(target);
 100         return res;
 101     }
 102 // Added by waleed for BIDI Support
 103     // returns the right to left status
 104     INLINE static BOOL GetRTLReadingOrder() {
 105         return sm_rtlReadingOrder;
 106     }
 107     // returns the right to left status
 108     INLINE static BOOL GetRTL() {
 109         return sm_rtl;
 110     }
 111     INLINE static LANGID GetSubLanguage() {
 112         return SUBLANGID(m_idLang);
 113     }
 114     // returns the current code page that should be used in
 115     // all MultiByteToWideChar and WideCharToMultiByte calls.
 116     // This code page should also be use in IsDBCSLeadByteEx.
 117     INLINE static UINT GetCodePage() {
 118         return m_CodePage;
 119     }
 120     INLINE static LANGID GetInputLanguage() {
 121         return m_idLang;
 122     }
 123 // end waleed
 124 
 125     virtual void DrawItem(DRAWITEMSTRUCT& drawInfo);
 126     void DrawSelf(DRAWITEMSTRUCT& drawInfo);
 127     static void AdjustCheckWidth(int& checkWidth);
 128 
 129     virtual void MeasureItem(HDC hDC, MEASUREITEMSTRUCT& measureInfo);
 130     void MeasureSelf(HDC hDC, MEASUREITEMSTRUCT& measureInfo);
 131 
 132     jobject GetFont(JNIEnv *env);
 133     jobject GetFontMetrics(JNIEnv *env, jobject font);
 134     jobject GetDefaultFont(JNIEnv *env);
 135 
 136     virtual BOOL IsTopMenu();
 137     void DrawCheck(HDC hDC, RECT rect);
 138 
 139     void SetLabel(LPCTSTR sb);
 140     virtual void Enable(BOOL isEnabled);
 141     virtual void UpdateContainerLayout();
 142     virtual void RedrawMenuBar();
 143     void SetState(BOOL isChecked);
 144 
 145     /*
 146      * Windows message handler functions
 147      */
 148     MsgRouting WmNotify(UINT notifyCode);
 149 
 150     virtual BOOL IsDisabledAndPopup() {
 151         return FALSE;
 152     }
 153     virtual BOOL IsSeparator();
 154 
 155     // invoked on Toolkit thread
 156     static void _SetState(void *param);
 157     static void _SetEnable(void *param);
 158     static void _SetLabel(void *param);
 159     static void _UpdateLayout(void *param);
 160 
 161 protected:
 162     AwtMenu* m_menuContainer;  /* The menu object containing this item */
 163     UINT m_Id;                 /* The id of this item */
 164 
 165     static BOOL CheckMenuCreation(JNIEnv *env, jobject self, HMENU hMenu);
 166     virtual void RemoveCmdID();
 167 
 168 private:
 169     INLINE BOOL IsCheckbox() { return m_isCheckbox; }
 170     INLINE void SetCheckbox() { m_isCheckbox = TRUE; }
 171     BOOL m_isCheckbox;
 172     BOOL m_freeId;
 173 
 174     // Added for bi-di support By Waleed
 175     static UINT m_CodePage;
 176     // Current input language (=low word of keyboardlayout handle)
 177     // m_idLang is shared by all instance of AwtComponent because
 178     // keyboardlayout is shared.
 179     static LANGID m_idLang;
 180     static BOOL m_isWin95;
 181 
 182     static BOOL sm_rtl;
 183     static BOOL sm_rtlReadingOrder;
 184 
 185 public:
 186     static HBITMAP bmpCheck;
 187     static jobject systemFont;
 188 };
 189 
 190 #endif /* AWT_MENUITEM_H */