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