don't use empty attribute name in wxIconXmlHandler too (should have been part of...
[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/log.h"
25 #include "wx/menu.h"
26 #endif
27
28 IMPLEMENT_DYNAMIC_CLASS(wxMenuXmlHandler, wxXmlResourceHandler)
29
30 wxMenuXmlHandler::wxMenuXmlHandler() :
31 wxXmlResourceHandler(), m_insideMenu(false)
32 {
33 XRC_ADD_STYLE(wxMENU_TEAROFF);
34 }
35
36 wxObject *wxMenuXmlHandler::DoCreateResource()
37 {
38 if (m_class == wxT("wxMenu"))
39 {
40 wxMenu *menu = m_instance ? wxStaticCast(m_instance, wxMenu)
41 : new wxMenu(GetStyle());
42
43 wxString title = GetText(wxT("label"));
44 wxString help = GetText(wxT("help"));
45
46 bool oldins = m_insideMenu;
47 m_insideMenu = true;
48 CreateChildren(menu, true/*only this handler*/);
49 m_insideMenu = oldins;
50
51 wxMenuBar *p_bar = wxDynamicCast(m_parent, wxMenuBar);
52 if (p_bar)
53 {
54 p_bar->Append(menu, title);
55 }
56 else
57 {
58 wxMenu *p_menu = wxDynamicCast(m_parent, wxMenu);
59 if (p_menu)
60 {
61 p_menu->Append(GetID(), title, menu, help);
62 if (HasParam(wxT("enabled")))
63 p_menu->Enable(GetID(), GetBool(wxT("enabled")));
64 }
65 }
66
67 return menu;
68 }
69
70 else
71 {
72 wxMenu *p_menu = wxDynamicCast(m_parent, wxMenu);
73
74 if (m_class == wxT("separator"))
75 p_menu->AppendSeparator();
76 else if (m_class == wxT("break"))
77 p_menu->Break();
78 else /*wxMenuItem*/
79 {
80 int id = GetID();
81 wxString label = GetText(wxT("label"));
82 wxString accel = GetText(wxT("accel"), false);
83 wxString fullLabel = label;
84 if (!accel.empty())
85 fullLabel << wxT("\t") << accel;
86
87 wxItemKind kind = wxITEM_NORMAL;
88 if (GetBool(wxT("radio")))
89 kind = wxITEM_RADIO;
90 if (GetBool(wxT("checkable")))
91 {
92 if ( kind != wxITEM_NORMAL )
93 {
94 ReportParamError
95 (
96 "checkable",
97 "menu item can't have both <radio> and <checkable> properties"
98 );
99 }
100
101 kind = wxITEM_CHECK;
102 }
103
104 wxMenuItem *mitem = new wxMenuItem(p_menu, id, fullLabel,
105 GetText(wxT("help")), kind);
106
107 #if (!defined(__WXMSW__) && !defined(__WXPM__)) || wxUSE_OWNER_DRAWN
108 if (HasParam(wxT("bitmap")))
109 {
110 // currently only wxMSW has support for using different checked
111 // and unchecked bitmaps for menu items
112 #ifdef __WXMSW__
113 if (HasParam(wxT("bitmap2")))
114 mitem->SetBitmaps(GetBitmap(wxT("bitmap2"), wxART_MENU),
115 GetBitmap(wxT("bitmap"), wxART_MENU));
116 else
117 #endif // __WXMSW__
118 mitem->SetBitmap(GetBitmap(wxT("bitmap"), wxART_MENU));
119 }
120 #endif
121 p_menu->Append(mitem);
122 mitem->Enable(GetBool(wxT("enabled"), true));
123 if (kind == wxITEM_CHECK)
124 mitem->Check(GetBool(wxT("checked")));
125 }
126 return NULL;
127 }
128 }
129
130
131
132 bool wxMenuXmlHandler::CanHandle(wxXmlNode *node)
133 {
134 return IsOfClass(node, wxT("wxMenu")) ||
135 (m_insideMenu &&
136 (IsOfClass(node, wxT("wxMenuItem")) ||
137 IsOfClass(node, wxT("break")) ||
138 IsOfClass(node, wxT("separator")))
139 );
140 }
141
142 IMPLEMENT_DYNAMIC_CLASS(wxMenuBarXmlHandler, wxXmlResourceHandler)
143
144 wxMenuBarXmlHandler::wxMenuBarXmlHandler() : wxXmlResourceHandler()
145 {
146 XRC_ADD_STYLE(wxMB_DOCKABLE);
147 }
148
149 wxObject *wxMenuBarXmlHandler::DoCreateResource()
150 {
151 wxMenuBar *menubar = new wxMenuBar(GetStyle());
152 CreateChildren(menubar);
153
154 if (m_parentAsWindow)
155 {
156 wxFrame *parentFrame = wxDynamicCast(m_parent, wxFrame);
157 if (parentFrame)
158 parentFrame->SetMenuBar(menubar);
159 }
160
161 return menubar;
162 }
163
164
165
166 bool wxMenuBarXmlHandler::CanHandle(wxXmlNode *node)
167 {
168 return IsOfClass(node, wxT("wxMenuBar"));
169 }
170
171 #endif // wxUSE_XRC && wxUSE_MENUS