]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
17 #pragma implementation "menuitem.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
25 #include "wx/bitmap.h"
26 #include "wx/settings.h"
28 #include "wx/window.h"
31 #include "wx/string.h"
34 #include "wx/menuitem.h"
41 #include "wx/os2/private.h"
43 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
48 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
50 // conditional compilation
52 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
53 #else // !wxUSE_OWNER_DRAWN
54 #define OWNER_DRAWN_ONLY( code )
55 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
57 // ----------------------------------------------------------------------------
58 // static function for translating menu labels
59 // ----------------------------------------------------------------------------
61 static wxString
TextToLabel(const wxString
& rTitle
)
65 for (pc
= rTitle
.c_str(); *pc
!= wxT('\0'); pc
++ )
69 if (*(pc
+1) == wxT('&'))
79 if ( *pc
== wxT('~') )
81 // tildes must be doubled to prevent them from being
82 // interpreted as accelerator character prefix by PM ???
91 // ============================================================================
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // dynamic classes implementation
97 // ----------------------------------------------------------------------------
99 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
108 wxMenuItem::wxMenuItem(
111 , const wxString
& rText
112 , const wxString
& rStrHelp
116 #if wxUSE_OWNER_DRAWN
117 : wxOwnerDrawn( TextToLabel(rText
)
120 #endif // owner drawn
122 wxASSERT_MSG(pParentMenu
!= NULL
, wxT("a menu item should have a parent"));
124 #if wxUSE_OWNER_DRAWN
127 // Set default menu colors
129 #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c))
131 SetTextColour(SYS_COLOR(MENUTEXT
));
132 SetBackgroundColour(SYS_COLOR(MENU
));
135 // We don't want normal items be owner-drawn
140 #endif // wxUSE_OWNER_DRAWN
142 m_parentMenu
= pParentMenu
;
143 m_subMenu
= pSubMenu
;
147 m_text
= TextToLabel(rText
);
148 m_isCheckable
= bCheckable
;
150 memset(&m_vMenuData
, '\0', sizeof(m_vMenuData
));
152 } // end of wxMenuItem::wxMenuItem
154 wxMenuItem::~wxMenuItem()
156 } // end of wxMenuItem::~wxMenuItem
163 // Return the id for calling Win32 API functions
165 int wxMenuItem::GetRealId() const
167 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
168 } // end of wxMenuItem::GetRealId
173 bool wxMenuItem::IsChecked() const
175 USHORT uFlag
= SHORT1FROMMR(::WinSendMsg( GetHMenuOf(m_parentMenu
)
177 ,MPFROM2SHORT(GetId(), TRUE
)
178 ,MPFROMSHORT(MIA_CHECKED
)
181 return (uFlag
& MIA_CHECKED
);
182 } // end of wxMenuItem::IsChecked
184 wxString
wxMenuItemBase::GetLabelFromText(
185 const wxString
& rText
189 for ( const wxChar
*pc
= rText
.c_str(); *pc
; pc
++ )
191 if ( *pc
== wxT('~') || *pc
== wxT('&') )
193 // '~' is the escape character for GTK+ and '&' is the one for
194 // wxWindows - skip both of them
206 void wxMenuItem::Enable(
212 if (m_isEnabled
== bEnable
)
215 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
217 ,MPFROM2SHORT(GetRealId(), TRUE
)
218 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
221 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
223 ,MPFROM2SHORT(GetRealId(), TRUE
)
224 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
228 wxLogLastError("EnableMenuItem");
230 wxMenuItemBase::Enable(bEnable
);
231 } // end of wxMenuItem::Enable
233 void wxMenuItem::Check(
239 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
240 if (m_isChecked
== bCheck
)
243 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
245 ,MPFROM2SHORT(GetRealId(), TRUE
)
246 ,MPFROM2SHORT(MIA_CHECKED
, MIA_CHECKED
)
249 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
251 ,MPFROM2SHORT(GetRealId(), TRUE
)
252 ,MPFROM2SHORT(MIA_CHECKED
, FALSE
)
256 wxLogLastError("CheckMenuItem");
258 wxMenuItemBase::Check(bCheck
);
259 } // end of wxMenuItem::Check
261 void wxMenuItem::SetText(
262 const wxString
& rText
266 // Don't do anything if label didn't change
269 wxString Text
= TextToLabel(rText
);
273 wxMenuItemBase::SetText(Text
);
274 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(Text
));
276 HWND hMenu
= GetHMenuOf(m_parentMenu
);
278 wxCHECK_RET(hMenu
, wxT("menuitem without menu"));
281 m_parentMenu
->UpdateAccel(this);
282 #endif // wxUSE_ACCEL
284 USHORT uId
= GetRealId();
288 if (!::WinSendMsg( hMenu
290 ,MPFROM2SHORT(uId
, TRUE
)
294 wxLogLastError("GetMenuState");
298 uFlagsOld
= vItem
.afStyle
;
301 uFlagsOld
|= MIS_SUBMENU
;
306 #if wxUSE_OWNER_DRAWN
309 uFlagsOld
|= MIS_OWNERDRAW
;
315 uFlagsOld
|= MIS_TEXT
;
316 pData
= (BYTE
*)Text
.c_str();
322 if (!::WinSendMsg( hMenu
324 ,MPFROM2SHORT(uId
, TRUE
)
328 wxLogLastError(wxT("ModifyMenu"));
334 if (::WinSendMsg( hMenu
340 wxLogLastError(wxT("ModifyMenu"));
343 } // end of wxMenuItem::SetText
345 void wxMenuItem::SetCheckable(
349 wxMenuItemBase::SetCheckable(bCheckable
);
350 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable
));
351 } // end of wxMenuItem::SetCheckable
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 wxMenuItem
* wxMenuItemBase::New(
360 , const wxString
& rName
361 , const wxString
& rHelp
366 return new wxMenuItem( pParentMenu
373 } // end of wxMenuItemBase::New