]>
git.saurik.com Git - wxWidgets.git/blob - src/stubs/menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
14 // headers & declarations
15 // ============================================================================
21 #pragma implementation "menu.h"
22 #pragma implementation "menuitem.h"
26 #include "wx/menuitem.h"
30 // other standard headers
31 // ----------------------
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
36 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
39 // ============================================================================
41 // ============================================================================
45 // Construct a menu with optional title (then use append)
46 wxMenu::wxMenu(const wxString
& title
, const wxFunction func
)
50 m_eventHandler
= this;
64 // The wxWindow destructor will take care of deleting the submenus.
67 // TODO destroy menu and children
69 wxNode
*node
= m_menuItems
.First();
72 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
74 // Delete child menus.
75 // Beware: they must not be appended to children list!!!
76 // (because order of delete is significant)
77 if (item
->GetSubMenu())
78 item
->DeleteSubMenu();
80 wxNode
*next
= node
->Next();
92 // function appends a new item or submenu to the menu
93 void wxMenu::Append(wxMenuItem
*pItem
)
97 wxCHECK_RET( pItem
!= NULL
, "can't append NULL item to the menu" );
99 m_menuItems
.Append(pItem
);
104 void wxMenu::AppendSeparator()
107 Append(new wxMenuItem(this, ID_SEPARATOR
));
111 void wxMenu::Append(int Id
, const wxString
& label
, wxMenu
*SubMenu
,
112 const wxString
& helpString
)
114 Append(new wxMenuItem(this, Id
, label
, helpString
, FALSE
, SubMenu
));
117 // Ordinary menu item
118 void wxMenu::Append(int Id
, const wxString
& label
,
119 const wxString
& helpString
, bool checkable
)
121 // 'checkable' parameter is useless for Windows.
122 Append(new wxMenuItem(this, Id
, label
, helpString
, checkable
));
125 void wxMenu::Delete(int id
)
131 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++) {
132 item
= (wxMenuItem
*)node
->Data();
133 if (item
->GetId() == id
)
140 m_menuItems
.DeleteNode(node
);
146 void wxMenu::Enable(int Id
, bool Flag
)
148 wxMenuItem
*item
= FindItemForId(Id
);
149 wxCHECK_RET( item
!= NULL
, "can't enable non-existing menu item" );
154 bool wxMenu::Enabled(int Id
) const
156 wxMenuItem
*item
= FindItemForId(Id
);
157 wxCHECK( item
!= NULL
, FALSE
);
159 return item
->IsEnabled();
162 void wxMenu::Check(int Id
, bool Flag
)
164 wxMenuItem
*item
= FindItemForId(Id
);
165 wxCHECK_RET( item
!= NULL
, "can't get status of non-existing menu item" );
170 bool wxMenu::Checked(int Id
) const
172 wxMenuItem
*item
= FindItemForId(Id
);
173 wxCHECK( item
!= NULL
, FALSE
);
175 return item
->IsChecked();
178 void wxMenu::SetTitle(const wxString
& label
)
184 const wxString
wxMenu::GetTitle() const
189 void wxMenu::SetLabel(int id
, const wxString
& label
)
191 wxMenuItem
*item
= FindItemForId(id
) ;
195 if (item
->GetSubMenu()==NULL
)
203 item
->SetName(label
);
206 wxString
wxMenu::GetLabel(int Id
) const
209 return wxString("") ;
212 // Finds the item id matching the given string, -1 if not found.
213 int wxMenu::FindItem (const wxString
& itemString
) const
217 wxStripMenuCodes ((char *)(const char *)itemString
, buf1
);
219 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
221 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
222 if (item
->GetSubMenu())
224 int ans
= item
->GetSubMenu()->FindItem(itemString
);
228 if ( !item
->IsSeparator() )
230 wxStripMenuCodes((char *)item
->GetName().c_str(), buf2
);
231 if (strcmp(buf1
, buf2
) == 0)
232 return item
->GetId();
239 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
243 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
245 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
247 if (item
->GetId() == itemId
)
250 *itemMenu
= (wxMenu
*) this;
254 if (item
->GetSubMenu())
256 wxMenuItem
*ans
= item
->GetSubMenu()->FindItemForId (itemId
, itemMenu
);
267 void wxMenu::SetHelpString(int itemId
, const wxString
& helpString
)
269 wxMenuItem
*item
= FindItemForId (itemId
);
271 item
->SetHelp(helpString
);
274 wxString
wxMenu::GetHelpString (int itemId
) const
276 wxMenuItem
*item
= FindItemForId (itemId
);
278 return (item
== NULL
) ? str
: item
->GetHelp();
281 void wxMenu::ProcessCommand(wxCommandEvent
& event
)
283 bool processed
= FALSE
;
288 (void) (*(m_callback
)) (*this, event
);
292 // Try the menu's event handler
293 if ( !processed
&& GetEventHandler())
295 processed
= GetEventHandler()->ProcessEvent(event
);
298 // Try the window the menu was popped up from (and up
299 // through the hierarchy)
300 if ( !processed && GetInvokingWindow())
301 processed = GetInvokingWindow()->ProcessEvent(event);
305 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
312 wxMenuBar::wxMenuBar()
314 m_eventHandler
= this;
318 m_menuBarFrame
= NULL
;
323 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
325 m_eventHandler
= this;
328 m_titles
= new wxString
[n
];
330 for ( i
= 0; i
< n
; i
++ )
331 m_titles
[i
] = titles
[i
];
332 m_menuBarFrame
= NULL
;
337 wxMenuBar::~wxMenuBar()
340 for (i
= 0; i
< m_menuCount
; i
++)
350 // Must only be used AFTER menu has been attached to frame,
351 // otherwise use individual menus to enable/disable items
352 void wxMenuBar::Enable(int id
, bool flag
)
354 wxMenu
*itemMenu
= NULL
;
355 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
362 void wxMenuBar::EnableTop(int pos
, bool flag
)
367 // Must only be used AFTER menu has been attached to frame,
368 // otherwise use individual menus
369 void wxMenuBar::Check(int id
, bool flag
)
371 wxMenu
*itemMenu
= NULL
;
372 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
376 if (!item
->IsCheckable())
382 bool wxMenuBar::Checked(int id
) const
384 wxMenu
*itemMenu
= NULL
;
385 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
393 bool wxMenuBar::Enabled(int id
) const
395 wxMenu
*itemMenu
= NULL
;
396 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
405 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
407 wxMenu
*itemMenu
= NULL
;
408 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
416 wxString
wxMenuBar::GetLabel(int id
) const
418 wxMenu
*itemMenu
= NULL
;
419 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
425 return wxString("") ;
428 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
433 wxString
wxMenuBar::GetLabelTop(int pos
) const
439 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
445 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const char *title
)
451 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
453 if (!OnAppend(menu
, title
))
457 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
458 wxString
*new_titles
= new wxString
[m_menuCount
];
461 for (i
= 0; i
< m_menuCount
- 1; i
++)
463 new_menus
[i
] = m_menus
[i
];
465 new_titles
[i
] = m_titles
[i
];
474 m_titles
= new_titles
;
476 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
477 m_titles
[m_menuCount
- 1] = title
;
482 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
489 for (ii
= 0; ii
< m_menuCount
; ii
++)
491 if (m_menus
[ii
] == menu
)
494 if (ii
>= m_menuCount
)
498 if (ii
< 0 || ii
>= m_menuCount
)
503 if (!OnDelete(menu
, ii
))
506 menu
->SetParent(NULL
);
509 for (j
= ii
; j
< m_menuCount
; j
++)
511 m_menus
[j
] = m_menus
[j
+ 1];
512 m_titles
[j
] = m_titles
[j
+ 1];
516 // Find the menu menuString, item itemString, and return the item id.
517 // Returns -1 if none found.
518 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
522 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
524 for (i
= 0; i
< m_menuCount
; i
++)
526 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
527 if (strcmp (buf1
, buf2
) == 0)
528 return m_menus
[i
]->FindItem (itemString
);
533 wxMenuItem
*wxMenuBar::FindItemForId (int Id
, wxMenu
** itemMenu
) const
538 wxMenuItem
*item
= NULL
;
540 for (i
= 0; i
< m_menuCount
; i
++)
541 if ((item
= m_menus
[i
]->FindItemForId (Id
, itemMenu
)))
546 void wxMenuBar::SetHelpString (int Id
, const wxString
& helpString
)
549 for (i
= 0; i
< m_menuCount
; i
++)
551 if (m_menus
[i
]->FindItemForId (Id
))
553 m_menus
[i
]->SetHelpString (Id
, helpString
);
559 wxString
wxMenuBar::GetHelpString (int Id
) const
562 for (i
= 0; i
< m_menuCount
; i
++)
564 if (m_menus
[i
]->FindItemForId (Id
))
565 return wxString(m_menus
[i
]->GetHelpString (Id
));