]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/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 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
313 wxMenuBar::wxMenuBar()
315 m_eventHandler
= this;
319 m_menuBarFrame
= NULL
;
324 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
326 m_eventHandler
= this;
329 m_titles
= new wxString
[n
];
331 for ( i
= 0; i
< n
; i
++ )
332 m_titles
[i
] = titles
[i
];
333 m_menuBarFrame
= NULL
;
338 wxMenuBar::~wxMenuBar()
341 for (i
= 0; i
< m_menuCount
; i
++)
351 // Must only be used AFTER menu has been attached to frame,
352 // otherwise use individual menus to enable/disable items
353 void wxMenuBar::Enable(int id
, bool flag
)
355 wxMenu
*itemMenu
= NULL
;
356 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
363 void wxMenuBar::EnableTop(int pos
, bool flag
)
368 // Must only be used AFTER menu has been attached to frame,
369 // otherwise use individual menus
370 void wxMenuBar::Check(int id
, bool flag
)
372 wxMenu
*itemMenu
= NULL
;
373 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
377 if (!item
->IsCheckable())
383 bool wxMenuBar::Checked(int id
) const
385 wxMenu
*itemMenu
= NULL
;
386 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
394 bool wxMenuBar::Enabled(int id
) const
396 wxMenu
*itemMenu
= NULL
;
397 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
406 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
408 wxMenu
*itemMenu
= NULL
;
409 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
417 wxString
wxMenuBar::GetLabel(int id
) const
419 wxMenu
*itemMenu
= NULL
;
420 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
426 return wxString("") ;
429 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
434 wxString
wxMenuBar::GetLabelTop(int pos
) const
440 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
446 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const char *title
)
452 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
454 if (!OnAppend(menu
, title
))
458 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
459 wxString
*new_titles
= new wxString
[m_menuCount
];
462 for (i
= 0; i
< m_menuCount
- 1; i
++)
464 new_menus
[i
] = m_menus
[i
];
466 new_titles
[i
] = m_titles
[i
];
475 m_titles
= new_titles
;
477 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
478 m_titles
[m_menuCount
- 1] = title
;
483 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
490 for (ii
= 0; ii
< m_menuCount
; ii
++)
492 if (m_menus
[ii
] == menu
)
495 if (ii
>= m_menuCount
)
499 if (ii
< 0 || ii
>= m_menuCount
)
504 if (!OnDelete(menu
, ii
))
507 menu
->SetParent(NULL
);
510 for (j
= ii
; j
< m_menuCount
; j
++)
512 m_menus
[j
] = m_menus
[j
+ 1];
513 m_titles
[j
] = m_titles
[j
+ 1];
517 // Find the menu menuString, item itemString, and return the item id.
518 // Returns -1 if none found.
519 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
523 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
525 for (i
= 0; i
< m_menuCount
; i
++)
527 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
528 if (strcmp (buf1
, buf2
) == 0)
529 return m_menus
[i
]->FindItem (itemString
);
534 wxMenuItem
*wxMenuBar::FindItemForId (int Id
, wxMenu
** itemMenu
) const
539 wxMenuItem
*item
= NULL
;
541 for (i
= 0; i
< m_menuCount
; i
++)
542 if ((item
= m_menus
[i
]->FindItemForId (Id
, itemMenu
)))
547 void wxMenuBar::SetHelpString (int Id
, const wxString
& helpString
)
550 for (i
= 0; i
< m_menuCount
; i
++)
552 if (m_menus
[i
]->FindItemForId (Id
))
554 m_menus
[i
]->SetHelpString (Id
, helpString
);
560 wxString
wxMenuBar::GetHelpString (int Id
) const
563 for (i
= 0; i
< m_menuCount
; i
++)
565 if (m_menus
[i
]->FindItemForId (Id
))
566 return wxString(m_menus
[i
]->GetHelpString (Id
));