]>
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 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
35 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
37 // ============================================================================
39 // ============================================================================
43 // Construct a menu with optional title (then use append)
44 wxMenu::wxMenu(const wxString
& title
, const wxFunction func
)
48 m_eventHandler
= this;
51 m_clientData
= (void*) NULL
;
63 // The wxWindow destructor will take care of deleting the submenus.
66 // TODO destroy menu and children
68 wxNode
*node
= m_menuItems
.First();
71 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
73 // Delete child menus.
74 // Beware: they must not be appended to children list!!!
75 // (because order of delete is significant)
76 if (item
->GetSubMenu())
77 item
->DeleteSubMenu();
79 wxNode
*next
= node
->Next();
91 // function appends a new item or submenu to the menu
92 void wxMenu::Append(wxMenuItem
*pItem
)
96 wxCHECK_RET( pItem
!= NULL
, "can't append NULL item to the menu" );
98 m_menuItems
.Append(pItem
);
103 void wxMenu::AppendSeparator()
106 Append(new wxMenuItem(this, ID_SEPARATOR
));
110 void wxMenu::Append(int Id
, const wxString
& label
, wxMenu
*SubMenu
,
111 const wxString
& helpString
)
113 Append(new wxMenuItem(this, Id
, label
, helpString
, FALSE
, SubMenu
));
116 // Ordinary menu item
117 void wxMenu::Append(int Id
, const wxString
& label
,
118 const wxString
& helpString
, bool checkable
)
120 // 'checkable' parameter is useless for Windows.
121 Append(new wxMenuItem(this, Id
, label
, helpString
, checkable
));
124 void wxMenu::Delete(int id
)
130 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++) {
131 item
= (wxMenuItem
*)node
->Data();
132 if (item
->GetId() == id
)
139 m_menuItems
.DeleteNode(node
);
145 void wxMenu::Enable(int Id
, bool Flag
)
147 wxMenuItem
*item
= FindItemForId(Id
);
148 wxCHECK_RET( item
!= NULL
, "can't enable non-existing menu item" );
153 bool wxMenu::Enabled(int Id
) const
155 wxMenuItem
*item
= FindItemForId(Id
);
156 wxCHECK( item
!= NULL
, FALSE
);
158 return item
->IsEnabled();
161 void wxMenu::Check(int Id
, bool Flag
)
163 wxMenuItem
*item
= FindItemForId(Id
);
164 wxCHECK_RET( item
!= NULL
, "can't get status of non-existing menu item" );
169 bool wxMenu::Checked(int Id
) const
171 wxMenuItem
*item
= FindItemForId(Id
);
172 wxCHECK( item
!= NULL
, FALSE
);
174 return item
->IsChecked();
177 void wxMenu::SetTitle(const wxString
& label
)
183 const wxString
wxMenu::GetTitle() const
188 void wxMenu::SetLabel(int id
, const wxString
& label
)
190 wxMenuItem
*item
= FindItemForId(id
) ;
194 if (item
->GetSubMenu()==NULL
)
202 item
->SetName(label
);
205 wxString
wxMenu::GetLabel(int Id
) const
208 return wxString("") ;
211 // Finds the item id matching the given string, -1 if not found.
212 int wxMenu::FindItem (const wxString
& itemString
) const
216 wxStripMenuCodes ((char *)(const char *)itemString
, buf1
);
218 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
220 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
221 if (item
->GetSubMenu())
223 int ans
= item
->GetSubMenu()->FindItem(itemString
);
227 if ( !item
->IsSeparator() )
229 wxStripMenuCodes((char *)item
->GetName().c_str(), buf2
);
230 if (strcmp(buf1
, buf2
) == 0)
231 return item
->GetId();
238 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
242 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
244 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
246 if (item
->GetId() == itemId
)
249 *itemMenu
= (wxMenu
*) this;
253 if (item
->GetSubMenu())
255 wxMenuItem
*ans
= item
->GetSubMenu()->FindItemForId (itemId
, itemMenu
);
266 void wxMenu::SetHelpString(int itemId
, const wxString
& helpString
)
268 wxMenuItem
*item
= FindItemForId (itemId
);
270 item
->SetHelp(helpString
);
273 wxString
wxMenu::GetHelpString (int itemId
) const
275 wxMenuItem
*item
= FindItemForId (itemId
);
277 return (item
== NULL
) ? str
: item
->GetHelp();
280 void wxMenu::ProcessCommand(wxCommandEvent
& event
)
282 bool processed
= FALSE
;
287 (void) (*(m_callback
)) (*this, event
);
291 // Try the menu's event handler
292 if ( !processed
&& GetEventHandler())
294 processed
= GetEventHandler()->ProcessEvent(event
);
297 // Try the window the menu was popped up from (and up
298 // through the hierarchy)
299 if ( !processed && GetInvokingWindow())
300 processed = GetInvokingWindow()->ProcessEvent(event);
304 // Update a menu and all submenus recursively.
305 // source is the object that has the update event handlers
306 // defined for it. If NULL, the menu or associated window
308 void wxMenu::UpdateUI(wxEvtHandler
* source
)
310 if (!source
&& GetInvokingWindow())
311 source
= GetInvokingWindow()->GetEventHandler();
313 source
= GetEventHandler();
317 wxNode
* node
= GetItems().First();
320 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
321 if ( !item
->IsSeparator() )
323 wxWindowID id
= item
->GetId();
324 wxUpdateUIEvent
event(id
);
325 event
.SetEventObject( source
);
327 if (source
->ProcessEvent(event
))
329 if (event
.GetSetText())
330 SetLabel(id
, event
.GetText());
331 if (event
.GetSetChecked())
332 Check(id
, event
.GetChecked());
333 if (event
.GetSetEnabled())
334 Enable(id
, event
.GetEnabled());
337 if (item
->GetSubMenu())
338 item
->GetSubMenu()->UpdateUI(source
);
344 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
346 menu
->SetInvokingWindow(this);
354 wxMenuBar::wxMenuBar()
356 m_eventHandler
= this;
360 m_menuBarFrame
= NULL
;
365 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
367 m_eventHandler
= this;
370 m_titles
= new wxString
[n
];
372 for ( i
= 0; i
< n
; i
++ )
373 m_titles
[i
] = titles
[i
];
374 m_menuBarFrame
= NULL
;
379 wxMenuBar::~wxMenuBar()
382 for (i
= 0; i
< m_menuCount
; i
++)
392 // Must only be used AFTER menu has been attached to frame,
393 // otherwise use individual menus to enable/disable items
394 void wxMenuBar::Enable(int id
, bool flag
)
396 wxMenu
*itemMenu
= NULL
;
397 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
404 void wxMenuBar::EnableTop(int pos
, bool flag
)
409 // Must only be used AFTER menu has been attached to frame,
410 // otherwise use individual menus
411 void wxMenuBar::Check(int id
, bool flag
)
413 wxMenu
*itemMenu
= NULL
;
414 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
418 if (!item
->IsCheckable())
424 bool wxMenuBar::Checked(int id
) const
426 wxMenu
*itemMenu
= NULL
;
427 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
435 bool wxMenuBar::Enabled(int id
) const
437 wxMenu
*itemMenu
= NULL
;
438 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
447 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
449 wxMenu
*itemMenu
= NULL
;
450 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
458 wxString
wxMenuBar::GetLabel(int id
) const
460 wxMenu
*itemMenu
= NULL
;
461 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
467 return wxString("") ;
470 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
475 wxString
wxMenuBar::GetLabelTop(int pos
) const
481 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
487 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const char *title
)
493 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
495 if (!OnAppend(menu
, title
))
499 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
500 wxString
*new_titles
= new wxString
[m_menuCount
];
503 for (i
= 0; i
< m_menuCount
- 1; i
++)
505 new_menus
[i
] = m_menus
[i
];
507 new_titles
[i
] = m_titles
[i
];
516 m_titles
= new_titles
;
518 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
519 m_titles
[m_menuCount
- 1] = title
;
524 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
531 for (ii
= 0; ii
< m_menuCount
; ii
++)
533 if (m_menus
[ii
] == menu
)
536 if (ii
>= m_menuCount
)
540 if (ii
< 0 || ii
>= m_menuCount
)
545 if (!OnDelete(menu
, ii
))
548 menu
->SetParent(NULL
);
551 for (j
= ii
; j
< m_menuCount
; j
++)
553 m_menus
[j
] = m_menus
[j
+ 1];
554 m_titles
[j
] = m_titles
[j
+ 1];
558 // Find the menu menuString, item itemString, and return the item id.
559 // Returns -1 if none found.
560 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
564 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
566 for (i
= 0; i
< m_menuCount
; i
++)
568 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
569 if (strcmp (buf1
, buf2
) == 0)
570 return m_menus
[i
]->FindItem (itemString
);
575 wxMenuItem
*wxMenuBar::FindItemForId (int Id
, wxMenu
** itemMenu
) const
580 wxMenuItem
*item
= NULL
;
582 for (i
= 0; i
< m_menuCount
; i
++)
583 if ((item
= m_menus
[i
]->FindItemForId (Id
, itemMenu
)))
588 void wxMenuBar::SetHelpString (int Id
, const wxString
& helpString
)
591 for (i
= 0; i
< m_menuCount
; i
++)
593 if (m_menus
[i
]->FindItemForId (Id
))
595 m_menus
[i
]->SetHelpString (Id
, helpString
);
601 wxString
wxMenuBar::GetHelpString (int Id
) const
604 for (i
= 0; i
< m_menuCount
; i
++)
606 if (m_menus
[i
]->FindItemForId (Id
))
607 return wxString(m_menus
[i
]->GetHelpString (Id
));