replace asserts in XRC code with wxLogError/Warning() calls as XRC can come from...
[wxWidgets.git] / src / xrc / xh_menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xrc/xh_menu.cpp
3 // Purpose: XRC resource for menus and menubars
4 // Author: Vaclav Slavik
5 // Created: 2000/03/05
6 // RCS-ID: $Id$
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_XRC && wxUSE_MENUS
19
20 #include "wx/xrc/xh_menu.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/frame.h"
24 #include "wx/menu.h"
25 #endif
26
27 IMPLEMENT_DYNAMIC_CLASS(wxMenuXmlHandler, wxXmlResourceHandler)
28
29 wxMenuXmlHandler::wxMenuXmlHandler() :
30 wxXmlResourceHandler(), m_insideMenu(false)
31 {
32 XRC_ADD_STYLE(wxMENU_TEAROFF);
33 }
34
35 wxObject *wxMenuXmlHandler::DoCreateResource()
36 {
37 if (m_class == wxT("wxMenu"))
38 {
39 wxMenu *menu = m_instance ? wxStaticCast(m_instance, wxMenu)
40 : new wxMenu(GetStyle());
41
42 wxString title = GetText(wxT("label"));
43 wxString help = GetText(wxT("help"));
44
45 bool oldins = m_insideMenu;
46 m_insideMenu = true;
47 CreateChildren(menu, true/*only this handler*/);
48 m_insideMenu = oldins;
49
50 wxMenuBar *p_bar = wxDynamicCast(m_parent, wxMenuBar);
51 if (p_bar)
52 {
53 p_bar->Append(menu, title);
54 }
55 else
56 {
57 wxMenu *p_menu = wxDynamicCast(m_parent, wxMenu);
58 if (p_menu)
59 {
60 p_menu->Append(GetID(), title, menu, help);
61 if (HasParam(wxT("enabled")))
62 p_menu->Enable(GetID(), GetBool(wxT("enabled")));
63 }
64 }
65
66 return menu;
67 }
68
69 else
70 {
71 wxMenu *p_menu = wxDynamicCast(m_parent, wxMenu);
72
73 if (m_class == wxT("separator"))
74 p_menu->AppendSeparator();
75 else if (m_class == wxT("break"))
76 p_menu->Break();
77 else /*wxMenuItem*/
78 {
79 int id = GetID();
80 wxString label = GetText(wxT("label"));
81 wxString accel = GetText(wxT("accel"), false);
82 wxString fullLabel = label;
83 if (!accel.empty())
84 fullLabel << wxT("\t") << accel;
85
86 wxItemKind kind = wxITEM_NORMAL;
87 if (GetBool(wxT("radio")))
88 kind = wxITEM_RADIO;
89 if (GetBool(wxT("checkable")))
90 {
91 if ( kind != wxITEM_NORMAL )
92 {
93 wxLogWarning(_("XRC syntax error: a menu item can't have "
94 "both \"radio\" and \"checkable\" "
95 "properties, ignoring the former."));
96 }
97
98 kind = wxITEM_CHECK;
99 }
100
101 wxMenuItem *mitem = new wxMenuItem(p_menu, id, fullLabel,
102 GetText(wxT("help")), kind);
103
104 #if (!defined(__WXMSW__) && !defined(__WXPM__)) || wxUSE_OWNER_DRAWN
105 if (HasParam(wxT("bitmap")))
106 {
107 // currently only wxMSW has support for using different checked
108 // and unchecked bitmaps for menu items
109 #ifdef __WXMSW__
110 if (HasParam(wxT("bitmap2")))
111 mitem->SetBitmaps(GetBitmap(wxT("bitmap2"), wxART_MENU),
112 GetBitmap(wxT("bitmap"), wxART_MENU));
113 else
114 #endif // __WXMSW__
115 mitem->SetBitmap(GetBitmap(wxT("bitmap"), wxART_MENU));
116 }
117 #endif
118 p_menu->Append(mitem);
119 mitem->Enable(GetBool(wxT("enabled"), true));
120 if (kind == wxITEM_CHECK)
121 mitem->Check(GetBool(wxT("checked")));
122 }
123 return NULL;
124 }
125 }
126
127
128
129 bool wxMenuXmlHandler::CanHandle(wxXmlNode *node)
130 {
131 return IsOfClass(node, wxT("wxMenu")) ||
132 (m_insideMenu &&
133 (IsOfClass(node, wxT("wxMenuItem")) ||
134 IsOfClass(node, wxT("break")) ||
135 IsOfClass(node, wxT("separator")))
136 );
137 }
138
139 IMPLEMENT_DYNAMIC_CLASS(wxMenuBarXmlHandler, wxXmlResourceHandler)
140
141 wxMenuBarXmlHandler::wxMenuBarXmlHandler() : wxXmlResourceHandler()
142 {
143 XRC_ADD_STYLE(wxMB_DOCKABLE);
144 }
145
146 wxObject *wxMenuBarXmlHandler::DoCreateResource()
147 {
148 wxMenuBar *menubar = new wxMenuBar(GetStyle());
149 CreateChildren(menubar);
150
151 if (m_parentAsWindow)
152 {
153 wxFrame *parentFrame = wxDynamicCast(m_parent, wxFrame);
154 if (parentFrame)
155 parentFrame->SetMenuBar(menubar);
156 }
157
158 return menubar;
159 }
160
161
162
163 bool wxMenuBarXmlHandler::CanHandle(wxXmlNode *node)
164 {
165 return IsOfClass(node, wxT("wxMenuBar"));
166 }
167
168 #endif // wxUSE_XRC && wxUSE_MENUS