]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: menuitem.cpp | |
3 | // Purpose: wxMenuItem implementation | |
4 | // Author: Vadim Zeitlin | |
c2dcfdef | 5 | // Modified by: |
2bda0e17 KB |
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 | ||
c2dcfdef VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
a3b46648 | 20 | #ifdef __GNUG__ |
c2dcfdef | 21 | #pragma implementation "menuitem.h" |
a3b46648 | 22 | #endif |
2bda0e17 KB |
23 | |
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
c2dcfdef | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
c2dcfdef VZ |
32 | #include "wx/font.h" |
33 | #include "wx/bitmap.h" | |
34 | #include "wx/settings.h" | |
35 | #include "wx/font.h" | |
0c589ad0 BM |
36 | #include "wx/accel.h" |
37 | #include "wx/menu.h" | |
38 | #include "wx/string.h" | |
39 | #include "wx/window.h" | |
2bda0e17 KB |
40 | #endif |
41 | ||
42 | #include "wx/ownerdrw.h" | |
43 | #include "wx/menuitem.h" | |
6776a0b2 | 44 | #include "wx/log.h" |
2bda0e17 | 45 | |
42e69d6b | 46 | #include "wx/msw/private.h" |
ce3ed50d | 47 | |
c2dcfdef VZ |
48 | // --------------------------------------------------------------------------- |
49 | // convenience macro | |
50 | // --------------------------------------------------------------------------- | |
51 | ||
52 | #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu()) | |
53 | ||
2bda0e17 KB |
54 | // ============================================================================ |
55 | // implementation | |
56 | // ============================================================================ | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // dynamic classes implementation | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | #if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY | |
47d67540 | 63 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
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, | |
3aadbb82 | 79 | const wxString& strName, const wxString& strHelp, |
2bda0e17 KB |
80 | bool bCheckable, |
81 | wxMenu *pSubMenu) : | |
47d67540 | 82 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
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 | { | |
c2dcfdef | 90 | wxASSERT_MSG( pParentMenu != NULL, "a menu item should have a parent" ); |
2bda0e17 | 91 | |
47d67540 | 92 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
93 | // set default menu colors |
94 | #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c)) | |
c2dcfdef | 95 | |
2bda0e17 KB |
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 | ||
c2dcfdef VZ |
105 | m_pParentMenu = pParentMenu; |
106 | m_pSubMenu = pSubMenu; | |
107 | m_bEnabled = TRUE; | |
108 | m_idItem = id; | |
2bda0e17 KB |
109 | } |
110 | ||
c2dcfdef | 111 | wxMenuItem::~wxMenuItem() |
2bda0e17 KB |
112 | { |
113 | } | |
114 | ||
115 | // misc | |
116 | // ---- | |
117 | ||
c2dcfdef VZ |
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 | ||
2bda0e17 | 124 | // delete the sub menu |
c2dcfdef | 125 | // ------------------- |
2bda0e17 KB |
126 | void wxMenuItem::DeleteSubMenu() |
127 | { | |
c2dcfdef VZ |
128 | delete m_pSubMenu; |
129 | m_pSubMenu = NULL; | |
2bda0e17 KB |
130 | } |
131 | ||
132 | // change item state | |
133 | // ----------------- | |
134 | ||
135 | void wxMenuItem::Enable(bool bDoEnable) | |
136 | { | |
c2dcfdef VZ |
137 | if ( m_bEnabled != bDoEnable ) { |
138 | long rc = EnableMenuItem(GetHMenuOf(m_pParentMenu), | |
139 | GetRealId(), | |
140 | MF_BYCOMMAND | | |
141 | (bDoEnable ? MF_ENABLED : MF_GRAYED)); | |
2bda0e17 | 142 | |
c2dcfdef VZ |
143 | if ( rc == -1 ) { |
144 | wxLogLastError("EnableMenuItem"); | |
145 | } | |
146 | ||
147 | m_bEnabled = bDoEnable; | |
148 | } | |
2bda0e17 KB |
149 | } |
150 | ||
151 | void wxMenuItem::Check(bool bDoCheck) | |
152 | { | |
c2dcfdef VZ |
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); | |
2bda0e17 | 178 | |
c2dcfdef VZ |
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 | } | |
2bda0e17 | 216 |