Attempt at getting mingw32 working again... still doesn't work
[wxWidgets.git] / src / msw / menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 11.11.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "menuitem.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/font.h"
33 #include "wx/bitmap.h"
34 #include "wx/settings.h"
35 #include "wx/font.h"
36 #include "wx/accel.h"
37 #include "wx/menu.h"
38 #include "wx/string.h"
39 #include "wx/window.h"
40 #endif
41
42 #include "wx/ownerdrw.h"
43 #include "wx/menuitem.h"
44 #include "wx/log.h"
45
46 #include "wx/msw/private.h"
47
48 // ---------------------------------------------------------------------------
49 // convenience macro
50 // ---------------------------------------------------------------------------
51
52 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // dynamic classes implementation
60 // ----------------------------------------------------------------------------
61
62 #if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY
63 #if wxUSE_OWNER_DRAWN
64 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem, wxObject, wxOwnerDrawn)
65 #else //!USE_OWNER_DRAWN
66 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
67 #endif //USE_OWNER_DRAWN
68
69 #endif //USE_SHARED_LIBRARY
70
71 // ----------------------------------------------------------------------------
72 // wxMenuItem
73 // ----------------------------------------------------------------------------
74
75 // ctor & dtor
76 // -----------
77
78 wxMenuItem::wxMenuItem(wxMenu *pParentMenu, int id,
79 const wxString& strName, const wxString& strHelp,
80 bool bCheckable,
81 wxMenu *pSubMenu) :
82 #if wxUSE_OWNER_DRAWN
83 wxOwnerDrawn(strName, bCheckable),
84 #else //no owner drawn support
85 m_bCheckable(bCheckable),
86 m_strName(strName),
87 #endif //owner drawn
88 m_strHelp(strHelp)
89 {
90 wxASSERT_MSG( pParentMenu != NULL, "a menu item should have a parent" );
91
92 #if wxUSE_OWNER_DRAWN
93 // set default menu colors
94 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
95
96 SetTextColour(SYS_COLOR(MENUTEXT));
97 SetBackgroundColour(SYS_COLOR(MENU));
98
99 // we don't want normal items be owner-drawn
100 ResetOwnerDrawn();
101
102 #undef SYS_COLOR
103 #endif
104
105 m_pParentMenu = pParentMenu;
106 m_pSubMenu = pSubMenu;
107 m_bEnabled = TRUE;
108 m_idItem = id;
109 }
110
111 wxMenuItem::~wxMenuItem()
112 {
113 }
114
115 // misc
116 // ----
117
118 // return the id for calling Win32 API functions
119 int wxMenuItem::GetRealId() const
120 {
121 return m_pSubMenu ? (int)m_pSubMenu->GetHMenu() : GetId();
122 }
123
124 // delete the sub menu
125 // -------------------
126 void wxMenuItem::DeleteSubMenu()
127 {
128 delete m_pSubMenu;
129 m_pSubMenu = NULL;
130 }
131
132 // change item state
133 // -----------------
134
135 void wxMenuItem::Enable(bool bDoEnable)
136 {
137 if ( m_bEnabled != bDoEnable ) {
138 long rc = EnableMenuItem(GetHMenuOf(m_pParentMenu),
139 GetRealId(),
140 MF_BYCOMMAND |
141 (bDoEnable ? MF_ENABLED : MF_GRAYED));
142
143 if ( rc == -1 ) {
144 wxLogLastError("EnableMenuItem");
145 }
146
147 m_bEnabled = bDoEnable;
148 }
149 }
150
151 void wxMenuItem::Check(bool bDoCheck)
152 {
153 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
154
155 if ( m_bChecked != bDoCheck ) {
156 long rc = CheckMenuItem(GetHMenuOf(m_pParentMenu),
157 GetId(),
158 MF_BYCOMMAND |
159 (bDoCheck ? MF_CHECKED : MF_UNCHECKED));
160
161 if ( rc == -1 ) {
162 wxLogLastError("CheckMenuItem");
163 }
164
165 m_bChecked = bDoCheck;
166 }
167 }
168
169 void wxMenuItem::SetName(const wxString& strName)
170 {
171 // don't do anything if label didn't change
172 if ( m_strName == strName )
173 return;
174
175 m_strName = strName;
176
177 HMENU hMenu = GetHMenuOf(m_pParentMenu);
178
179 UINT id = GetRealId();
180 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
181 if ( flagsOld == 0xFFFFFFFF )
182 {
183 wxLogLastError("GetMenuState");
184 }
185 else
186 {
187 if ( IsSubMenu() )
188 {
189 // high byte contains the number of items in a submenu for submenus
190 flagsOld &= 0xFF;
191 flagsOld |= MF_POPUP;
192 }
193
194 LPCSTR data;
195 #if wxUSE_OWNER_DRAWN
196 if ( IsOwnerDrawn() )
197 {
198 flagsOld |= MF_OWNERDRAW;
199 data = (LPCSTR)this;
200 }
201 else
202 #endif //owner drawn
203 {
204 flagsOld |= MF_STRING;
205 data = strName;
206 }
207
208 if ( ::ModifyMenu(hMenu, id,
209 MF_BYCOMMAND | flagsOld,
210 id, data) == 0xFFFFFFFF )
211 {
212 wxLogLastError("ModifyMenu");
213 }
214 }
215 }
216