]>
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;
53 m_clientData
= (void*) NULL
;
65 // The wxWindow destructor will take care of deleting the submenus.
68 // TODO destroy menu and children
70 wxNode
*node
= m_menuItems
.First();
73 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
75 // Delete child menus.
76 // Beware: they must not be appended to children list!!!
77 // (because order of delete is significant)
78 if (item
->GetSubMenu())
79 item
->DeleteSubMenu();
81 wxNode
*next
= node
->Next();
93 // function appends a new item or submenu to the menu
94 void wxMenu::Append(wxMenuItem
*pItem
)
98 wxCHECK_RET( pItem
!= NULL
, "can't append NULL item to the menu" );
100 m_menuItems
.Append(pItem
);
105 void wxMenu::AppendSeparator()
108 Append(new wxMenuItem(this, ID_SEPARATOR
));
112 void wxMenu::Append(int Id
, const wxString
& label
, wxMenu
*SubMenu
,
113 const wxString
& helpString
)
115 Append(new wxMenuItem(this, Id
, label
, helpString
, FALSE
, SubMenu
));
118 // Ordinary menu item
119 void wxMenu::Append(int Id
, const wxString
& label
,
120 const wxString
& helpString
, bool checkable
)
122 // 'checkable' parameter is useless for Windows.
123 Append(new wxMenuItem(this, Id
, label
, helpString
, checkable
));
126 void wxMenu::Delete(int id
)
132 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++) {
133 item
= (wxMenuItem
*)node
->Data();
134 if (item
->GetId() == id
)
141 m_menuItems
.DeleteNode(node
);
147 void wxMenu::Enable(int Id
, bool Flag
)
149 wxMenuItem
*item
= FindItemForId(Id
);
150 wxCHECK_RET( item
!= NULL
, "can't enable non-existing menu item" );
155 bool wxMenu::Enabled(int Id
) const
157 wxMenuItem
*item
= FindItemForId(Id
);
158 wxCHECK( item
!= NULL
, FALSE
);
160 return item
->IsEnabled();
163 void wxMenu::Check(int Id
, bool Flag
)
165 wxMenuItem
*item
= FindItemForId(Id
);
166 wxCHECK_RET( item
!= NULL
, "can't get status of non-existing menu item" );
171 bool wxMenu::Checked(int Id
) const
173 wxMenuItem
*item
= FindItemForId(Id
);
174 wxCHECK( item
!= NULL
, FALSE
);
176 return item
->IsChecked();
179 void wxMenu::SetTitle(const wxString
& label
)
185 const wxString
wxMenu::GetTitle() const
190 void wxMenu::SetLabel(int id
, const wxString
& label
)
192 wxMenuItem
*item
= FindItemForId(id
) ;
196 if (item
->GetSubMenu()==NULL
)
204 item
->SetName(label
);
207 wxString
wxMenu::GetLabel(int Id
) const
210 return wxString("") ;
213 // Finds the item id matching the given string, -1 if not found.
214 int wxMenu::FindItem (const wxString
& itemString
) const
218 wxStripMenuCodes ((char *)(const char *)itemString
, buf1
);
220 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
222 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
223 if (item
->GetSubMenu())
225 int ans
= item
->GetSubMenu()->FindItem(itemString
);
229 if ( !item
->IsSeparator() )
231 wxStripMenuCodes((char *)item
->GetName().c_str(), buf2
);
232 if (strcmp(buf1
, buf2
) == 0)
233 return item
->GetId();
240 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
244 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
246 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
248 if (item
->GetId() == itemId
)
251 *itemMenu
= (wxMenu
*) this;
255 if (item
->GetSubMenu())
257 wxMenuItem
*ans
= item
->GetSubMenu()->FindItemForId (itemId
, itemMenu
);
268 void wxMenu::SetHelpString(int itemId
, const wxString
& helpString
)
270 wxMenuItem
*item
= FindItemForId (itemId
);
272 item
->SetHelp(helpString
);
275 wxString
wxMenu::GetHelpString (int itemId
) const
277 wxMenuItem
*item
= FindItemForId (itemId
);
279 return (item
== NULL
) ? str
: item
->GetHelp();
282 void wxMenu::ProcessCommand(wxCommandEvent
& event
)
284 bool processed
= FALSE
;
289 (void) (*(m_callback
)) (*this, event
);
293 // Try the menu's event handler
294 if ( !processed
&& GetEventHandler())
296 processed
= GetEventHandler()->ProcessEvent(event
);
299 // Try the window the menu was popped up from (and up
300 // through the hierarchy)
301 if ( !processed && GetInvokingWindow())
302 processed = GetInvokingWindow()->ProcessEvent(event);
306 // Update a menu and all submenus recursively.
307 // source is the object that has the update event handlers
308 // defined for it. If NULL, the menu or associated window
310 void wxMenu::UpdateUI(wxEvtHandler
* source
)
312 if (!source
&& GetInvokingWindow())
313 source
= GetInvokingWindow()->GetEventHandler();
315 source
= GetEventHandler();
319 wxNode
* node
= GetItems().First();
322 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
323 if ( !item
->IsSeparator() )
325 wxWindowID id
= item
->GetId();
326 wxUpdateUIEvent
event(id
);
327 event
.SetEventObject( source
);
329 if (source
->ProcessEvent(event
))
331 if (event
.GetSetText())
332 SetLabel(id
, event
.GetText());
333 if (event
.GetSetChecked())
334 Check(id
, event
.GetChecked());
335 if (event
.GetSetEnabled())
336 Enable(id
, event
.GetEnabled());
339 if (item
->GetSubMenu())
340 item
->GetSubMenu()->UpdateUI(source
);
346 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
348 menu
->SetInvokingWindow(this);
356 wxMenuBar::wxMenuBar()
358 m_eventHandler
= this;
362 m_menuBarFrame
= NULL
;
367 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
369 m_eventHandler
= this;
372 m_titles
= new wxString
[n
];
374 for ( i
= 0; i
< n
; i
++ )
375 m_titles
[i
] = titles
[i
];
376 m_menuBarFrame
= NULL
;
381 wxMenuBar::~wxMenuBar()
384 for (i
= 0; i
< m_menuCount
; i
++)
394 // Must only be used AFTER menu has been attached to frame,
395 // otherwise use individual menus to enable/disable items
396 void wxMenuBar::Enable(int id
, bool flag
)
398 wxMenu
*itemMenu
= NULL
;
399 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
406 void wxMenuBar::EnableTop(int pos
, bool flag
)
411 // Must only be used AFTER menu has been attached to frame,
412 // otherwise use individual menus
413 void wxMenuBar::Check(int id
, bool flag
)
415 wxMenu
*itemMenu
= NULL
;
416 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
420 if (!item
->IsCheckable())
426 bool wxMenuBar::Checked(int id
) const
428 wxMenu
*itemMenu
= NULL
;
429 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
437 bool wxMenuBar::Enabled(int id
) const
439 wxMenu
*itemMenu
= NULL
;
440 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
449 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
451 wxMenu
*itemMenu
= NULL
;
452 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
460 wxString
wxMenuBar::GetLabel(int id
) const
462 wxMenu
*itemMenu
= NULL
;
463 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
469 return wxString("") ;
472 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
477 wxString
wxMenuBar::GetLabelTop(int pos
) const
483 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
489 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const char *title
)
495 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
497 if (!OnAppend(menu
, title
))
501 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
502 wxString
*new_titles
= new wxString
[m_menuCount
];
505 for (i
= 0; i
< m_menuCount
- 1; i
++)
507 new_menus
[i
] = m_menus
[i
];
509 new_titles
[i
] = m_titles
[i
];
518 m_titles
= new_titles
;
520 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
521 m_titles
[m_menuCount
- 1] = title
;
526 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
533 for (ii
= 0; ii
< m_menuCount
; ii
++)
535 if (m_menus
[ii
] == menu
)
538 if (ii
>= m_menuCount
)
542 if (ii
< 0 || ii
>= m_menuCount
)
547 if (!OnDelete(menu
, ii
))
550 menu
->SetParent(NULL
);
553 for (j
= ii
; j
< m_menuCount
; j
++)
555 m_menus
[j
] = m_menus
[j
+ 1];
556 m_titles
[j
] = m_titles
[j
+ 1];
560 // Find the menu menuString, item itemString, and return the item id.
561 // Returns -1 if none found.
562 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
566 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
568 for (i
= 0; i
< m_menuCount
; i
++)
570 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
571 if (strcmp (buf1
, buf2
) == 0)
572 return m_menus
[i
]->FindItem (itemString
);
577 wxMenuItem
*wxMenuBar::FindItemForId (int Id
, wxMenu
** itemMenu
) const
582 wxMenuItem
*item
= NULL
;
584 for (i
= 0; i
< m_menuCount
; i
++)
585 if ((item
= m_menus
[i
]->FindItemForId (Id
, itemMenu
)))
590 void wxMenuBar::SetHelpString (int Id
, const wxString
& helpString
)
593 for (i
= 0; i
< m_menuCount
; i
++)
595 if (m_menus
[i
]->FindItemForId (Id
))
597 m_menus
[i
]->SetHelpString (Id
, helpString
);
603 wxString
wxMenuBar::GetHelpString (int Id
) const
606 for (i
= 0; i
< m_menuCount
; i
++)
608 if (m_menus
[i
]->FindItemForId (Id
))
609 return wxString(m_menus
[i
]->GetHelpString (Id
));