]> git.saurik.com Git - wxWidgets.git/blob - src/common/menucmn.cpp
1. added wxMenuBarBase
[wxWidgets.git] / src / common / menucmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/menucmn.cpp
3 // Purpose: wxMenu and wxMenuBar methods common to all ports
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 26.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "menubase.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/menu.h"
33 #endif
34
35 // ----------------------------------------------------------------------------
36 // template lists
37 // ----------------------------------------------------------------------------
38
39 #include "wx/listimpl.cpp"
40 WX_DEFINE_LIST(wxMenuList);
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // ctor and dtor
48 // ----------------------------------------------------------------------------
49
50 wxMenuBarBase::wxMenuBarBase()
51 {
52 // we own the menus when we get them
53 m_menus.DeleteContents(TRUE);
54 }
55
56 wxMenuBarBase::~wxMenuBarBase()
57 {
58 // nothing to do, the list will delete the menus because of the call to
59 // DeleteContents() above
60 }
61
62 // ----------------------------------------------------------------------------
63 // wxMenuBar item access: the base class versions manage m_menus list, the
64 // derived class should reflect the changes in the real menubar
65 // ----------------------------------------------------------------------------
66
67 wxMenu *wxMenuBarBase::GetMenu(size_t pos) const
68 {
69 wxMenuList::Node *node = m_menus.Item(pos);
70 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") );
71
72 return node->GetData();
73 }
74
75 bool wxMenuBarBase::Append(wxMenu *menu, const wxString& WXUNUSED(title))
76 {
77 wxCHECK_MSG( menu, FALSE, wxT("can't append NULL menu") );
78
79 m_menus.Append(menu);
80
81 return TRUE;
82 }
83
84 bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu,
85 const wxString& WXUNUSED(title))
86 {
87 wxCHECK_MSG( menu, FALSE, wxT("can't insert NULL menu") );
88
89 wxMenuList::Node *node = m_menus.Item(pos);
90 wxCHECK_MSG( node, FALSE, wxT("bad index in wxMenuBar::Insert()") );
91
92 m_menus.Insert(node, menu);
93
94 return TRUE;
95 }
96
97 wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu,
98 const wxString& WXUNUSED(title))
99 {
100 wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") );
101
102 wxMenuList::Node *node = m_menus.Item(pos);
103 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") );
104
105 wxMenu *menuOld = node->GetData();
106 node->SetData(menu);
107
108 return menuOld;
109 }
110
111 wxMenu *wxMenuBarBase::Remove(size_t pos)
112 {
113 wxMenuList::Node *node = m_menus.Item(pos);
114 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") );
115
116 node = m_menus.DetachNode(node);
117 wxCHECK( node, NULL ); // unexpected
118 wxMenu *menu = node->GetData();
119
120 delete node;
121
122 return menu;
123 }
124
125 // ---------------------------------------------------------------------------
126 // wxMenuBar functions forwarded to wxMenuItem
127 // ---------------------------------------------------------------------------
128
129 void wxMenuBarBase::Enable(int id, bool enable)
130 {
131 wxMenuItem *item = FindItem(id);
132
133 wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") );
134
135 item->Enable(enable);
136 }
137
138 void wxMenuBarBase::Check(int id, bool check)
139 {
140 wxMenuItem *item = FindItem(id);
141
142 wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") );
143 wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") );
144
145 item->Check(check);
146 }
147
148 bool wxMenuBarBase::IsChecked(int id) const
149 {
150 wxMenuItem *item = FindItem(id);
151
152 wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked(): no such item") );
153
154 return item->IsChecked();
155 }
156
157 bool wxMenuBarBase::IsEnabled(int id) const
158 {
159 wxMenuItem *item = FindItem(id);
160
161 wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled(): no such item") );
162
163 return item->IsEnabled();
164 }
165
166 void wxMenuBarBase::SetLabel(int id, const wxString& label)
167 {
168 wxMenuItem *item = FindItem(id);
169
170 wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") );
171
172 item->SetText(label);
173 }
174
175 wxString wxMenuBarBase::GetLabel(int id) const
176 {
177 wxMenuItem *item = FindItem(id);
178
179 wxCHECK_MSG( item, wxEmptyString,
180 wxT("wxMenuBar::GetLabel(): no such item") );
181
182 return item->GetText();
183 }
184
185 void wxMenuBarBase::SetHelpString(int id, const wxString& helpString)
186 {
187 wxMenuItem *item = FindItem(id);
188
189 wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") );
190
191 item->SetHelp(helpString);
192 }
193
194 wxString wxMenuBarBase::GetHelpString(int id) const
195 {
196 wxMenuItem *item = FindItem(id);
197
198 wxCHECK_MSG( item, wxEmptyString,
199 wxT("wxMenuBar::GetHelpString(): no such item") );
200
201 return item->GetHelp();
202 }
203