]>
git.saurik.com Git - wxWidgets.git/blob - src/qt/menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "menu.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
25 wxMenuBar::wxMenuBar()
29 void wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
31 m_menus
.Append( menu
);
32 menu
->m_title
= title
; // ??????
36 pos
= menu
->m_title
.First( '&' );
37 if (pos
!= -1) menu
->m_title
.Remove( pos
, 1 );
42 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
44 if (menu
->m_title
== menuString
)
46 int res
= menu
->FindItem( itemString
);
47 if (res
!= -1) return res
;
49 wxNode
*node
= menu
->m_items
.First();
52 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
53 if (item
->IsSubMenu())
54 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
60 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
62 wxNode
*node
= m_menus
.First();
65 wxMenu
*menu
= (wxMenu
*)node
->Data();
66 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
67 if (res
!= -1) return res
;
73 // Find a wxMenuItem using its id. Recurses down into sub-menus
74 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
76 wxMenuItem
* result
= menu
->FindItem(id
);
78 wxNode
*node
= menu
->m_items
.First();
79 while ( node
&& result
== NULL
) {
80 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
81 if ( item
->IsSubMenu() )
82 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
89 wxMenuItem
* wxMenuBar::FindMenuItemById( int id
) const
91 wxMenuItem
* result
= 0;
92 wxNode
*node
= m_menus
.First();
93 while (node
&& result
== 0)
95 wxMenu
*menu
= (wxMenu
*)node
->Data();
96 result
= FindMenuItemByIdRecursive( menu
, id
);
102 void wxMenuBar::Check( int id
, bool check
)
104 wxMenuItem
* item
= FindMenuItemById( id
);
105 if (item
) item
->Check(check
);
108 bool wxMenuBar::Checked( int id
) const
110 wxMenuItem
* item
= FindMenuItemById( id
);
111 if (item
) return item
->IsChecked();
115 void wxMenuBar::Enable( int id
, bool enable
)
117 wxMenuItem
* item
= FindMenuItemById( id
);
118 if (item
) item
->Enable(enable
);
121 bool wxMenuBar::Enabled( int id
) const
123 wxMenuItem
* item
= FindMenuItemById( id
);
124 if (item
) return item
->IsEnabled();
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
133 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
135 wxMenuItem::wxMenuItem()
138 m_isCheckMenu
= FALSE
;
144 void wxMenuItem::SetText(const wxString
& str
)
146 for ( const char *pc
= str
; *pc
!= '\0'; pc
++ ) {
154 void wxMenuItem::Check( bool check
)
156 wxCHECK_RET( IsCheckable(), "can't check uncheckable item!" )
161 bool wxMenuItem::IsChecked() const
163 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
168 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
170 wxMenu::wxMenu( const wxString
&title
)
173 m_items
.DeleteContents( TRUE
);
174 m_invokingWindow
= NULL
;
177 void wxMenu::AppendSeparator()
179 wxMenuItem
*mitem
= new wxMenuItem();
180 mitem
->SetId(ID_SEPARATOR
);
182 m_items
.Append( mitem
);
185 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
187 wxMenuItem
*mitem
= new wxMenuItem();
189 mitem
->SetText(item
);
190 mitem
->SetHelpString(helpStr
);
191 mitem
->SetCheckable(checkable
);
193 m_items
.Append( mitem
);
196 void wxMenu::Append( int id
, const wxString
&text
, wxMenu
*subMenu
, const wxString
&helpStr
)
198 wxMenuItem
*mitem
= new wxMenuItem();
200 mitem
->SetText(text
);
201 mitem
->SetHelpString(helpStr
);
202 mitem
->SetSubMenu(subMenu
);
204 m_items
.Append( mitem
);
207 int wxMenu::FindItem( const wxString itemString
) const
209 wxString
s( itemString
);
213 pos
= s
.First( '&' );
214 if (pos
!= -1) s
.Remove( pos
, 1 );
217 wxNode
*node
= m_items
.First();
220 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
221 if (item
->GetText() == s
)
222 return item
->GetId();
229 void wxMenu::Enable( int id
, bool enable
)
231 wxMenuItem
*item
= FindItem(id
);
233 item
->Enable(enable
);
236 bool wxMenu::IsEnabled( int id
) const
238 wxMenuItem
*item
= FindItem(id
);
240 return item
->IsEnabled();
245 void wxMenu::Check( int id
, bool enable
)
247 wxMenuItem
*item
= FindItem(id
);
252 bool wxMenu::IsChecked( int id
) const
254 wxMenuItem
*item
= FindItem(id
);
256 return item
->IsChecked();
261 void wxMenu::SetLabel( int id
, const wxString
&label
)
263 wxMenuItem
*item
= FindItem(id
);
265 item
->SetText(label
);
268 wxMenuItem
*wxMenu::FindItem(int id
) const
270 wxNode
*node
= m_items
.First();
272 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
273 if ( item
->GetId() == id
)
278 wxLogDebug("wxMenu::FindItem: item %d not found.", id
);
283 void wxMenu::SetInvokingWindow( wxWindow
*win
)
285 m_invokingWindow
= win
;
288 wxWindow
*wxMenu::GetInvokingWindow()
290 return m_invokingWindow
;