]>
git.saurik.com Git - wxWidgets.git/blob - src/qt/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"
29 // other standard headers
30 // ----------------------
33 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
34 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
36 // ============================================================================
38 // ============================================================================
42 // Construct a menu with optional title (then use append)
43 wxMenu::wxMenu(const wxString
& title
, const wxFunction func
)
47 m_eventHandler
= this;
61 // The wxWindow destructor will take care of deleting the submenus.
64 // TODO destroy menu and children
66 wxNode
*node
= m_menuItems
.First();
69 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
71 // Delete child menus.
72 // Beware: they must not be appended to children list!!!
73 // (because order of delete is significant)
74 if (item
->GetSubMenu())
75 item
->DeleteSubMenu();
77 wxNode
*next
= node
->Next();
89 // function appends a new item or submenu to the menu
90 void wxMenu::Append(wxMenuItem
*pItem
)
94 wxCHECK_RET( pItem
!= NULL
, "can't append NULL item to the menu" );
96 m_menuItems
.Append(pItem
);
101 void wxMenu::AppendSeparator()
104 Append(new wxMenuItem(this, ID_SEPARATOR
));
108 void wxMenu::Append(int Id
, const wxString
& label
, wxMenu
*SubMenu
,
109 const wxString
& helpString
)
111 Append(new wxMenuItem(this, Id
, label
, helpString
, FALSE
, SubMenu
));
114 // Ordinary menu item
115 void wxMenu::Append(int Id
, const wxString
& label
,
116 const wxString
& helpString
, bool checkable
)
118 // 'checkable' parameter is useless for Windows.
119 Append(new wxMenuItem(this, Id
, label
, helpString
, checkable
));
122 void wxMenu::Delete(int id
)
128 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++) {
129 item
= (wxMenuItem
*)node
->Data();
130 if (item
->GetId() == id
)
137 m_menuItems
.DeleteNode(node
);
143 void wxMenu::Enable(int Id
, bool Flag
)
145 wxMenuItem
*item
= FindItemForId(Id
);
146 wxCHECK_RET( item
!= NULL
, "can't enable non-existing menu item" );
151 bool wxMenu::Enabled(int Id
) const
153 wxMenuItem
*item
= FindItemForId(Id
);
154 wxCHECK( item
!= NULL
, FALSE
);
156 return item
->IsEnabled();
159 void wxMenu::Check(int Id
, bool Flag
)
161 wxMenuItem
*item
= FindItemForId(Id
);
162 wxCHECK_RET( item
!= NULL
, "can't get status of non-existing menu item" );
167 bool wxMenu::Checked(int Id
) const
169 wxMenuItem
*item
= FindItemForId(Id
);
170 wxCHECK( item
!= NULL
, FALSE
);
172 return item
->IsChecked();
175 void wxMenu::SetTitle(const wxString
& label
)
181 const wxString
& wxMenu::GetTitle() const
186 void wxMenu::SetLabel(int id
, const wxString
& label
)
188 wxMenuItem
*item
= FindItemForId(id
) ;
192 if (item
->GetSubMenu()==NULL
)
200 item
->SetName(label
);
203 wxString
wxMenu::GetLabel(int Id
) const
206 return wxString("") ;
209 // Finds the item id matching the given string, -1 if not found.
210 int wxMenu::FindItem (const wxString
& itemString
) const
214 wxStripMenuCodes ((char *)(const char *)itemString
, buf1
);
216 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
218 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
219 if (item
->GetSubMenu())
221 int ans
= item
->GetSubMenu()->FindItem(itemString
);
225 if ( !item
->IsSeparator() )
227 wxStripMenuCodes((char *)item
->GetName().c_str(), buf2
);
228 if (strcmp(buf1
, buf2
) == 0)
229 return item
->GetId();
236 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
240 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
242 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
244 if (item
->GetId() == itemId
)
247 *itemMenu
= (wxMenu
*) this;
251 if (item
->GetSubMenu())
253 wxMenuItem
*ans
= item
->GetSubMenu()->FindItemForId (itemId
, itemMenu
);
264 void wxMenu::SetHelpString(int itemId
, const wxString
& helpString
)
266 wxMenuItem
*item
= FindItemForId (itemId
);
268 item
->SetHelp(helpString
);
271 wxString
wxMenu::GetHelpString (int itemId
) const
273 wxMenuItem
*item
= FindItemForId (itemId
);
275 return (item
== NULL
) ? str
: item
->GetHelp();
278 void wxMenu::ProcessCommand(wxCommandEvent
& event
)
280 bool processed
= FALSE
;
285 (void) (*(m_callback
)) (*this, event
);
289 // Try the menu's event handler
290 if ( !processed
&& GetEventHandler())
292 processed
= GetEventHandler()->ProcessEvent(event
);
295 // Try the window the menu was popped up from (and up
296 // through the hierarchy)
297 if ( !processed
&& GetInvokingWindow())
298 processed
= GetInvokingWindow()->ProcessEvent(event
);
301 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
308 wxMenuBar::wxMenuBar()
310 m_eventHandler
= this;
314 m_menuBarFrame
= NULL
;
319 wxMenuBar::wxMenuBar(int n
, wxMenu
*Mmnus
[], const wxString titles
[])
321 m_eventHandler
= this;
324 m_titles
= new wxString
[n
];
326 for ( i
= 0; i
< n
; i
++ )
327 m_titles
[i
] = titles
[i
];
328 m_menuBarFrame
= NULL
;
333 wxMenuBar::~wxMenuBar()
336 for (i
= 0; i
< m_menuCount
; i
++)
346 // Must only be used AFTER menu has been attached to frame,
347 // otherwise use individual menus to enable/disable items
348 void wxMenuBar::Enable(int id
, bool flag
)
350 wxMenu
*itemMenu
= NULL
;
351 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
358 void wxMenuBar::EnableTop(int pos
, bool flag
)
363 // Must only be used AFTER menu has been attached to frame,
364 // otherwise use individual menus
365 void wxMenuBar::Check(int id
, bool flag
)
367 wxMenu
*itemMenu
= NULL
;
368 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
372 if (!item
->IsCheckable())
378 bool wxMenuBar::Checked(int id
) const
380 wxMenu
*itemMenu
= NULL
;
381 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
389 bool wxMenuBar::Enabled(int id
) const
391 wxMenu
*itemMenu
= NULL
;
392 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
401 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
403 wxMenu
*itemMenu
= NULL
;
404 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
412 wxString
wxMenuBar::GetLabel(int id
) const
414 wxMenu
*itemMenu
= NULL
;
415 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
421 return wxString("") ;
424 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
429 wxString
wxMenuBar::GetLabelTop(int pos
) const
435 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
441 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const char *title
)
447 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
449 if (!OnAppend(menu
, title
))
453 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
454 wxString
*new_titles
= new wxString
[m_menuCount
];
457 for (i
= 0; i
< m_menuCount
- 1; i
++)
459 new_menus
[i
] = m_menus
[i
];
461 new_titles
[i
] = m_titles
[i
];
470 m_titles
= new_titles
;
472 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
473 m_titles
[m_menuCount
- 1] = title
;
478 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
485 for (ii
= 0; ii
< m_menuCount
; ii
++)
487 if (m_menus
[ii
] == menu
)
490 if (ii
>= m_menuCount
)
494 if (ii
< 0 || ii
>= m_menuCount
)
499 if (!OnDelete(menu
, ii
))
502 menu
->SetParent(NULL
);
505 for (j
= ii
; j
< m_menuCount
; j
++)
507 m_menus
[j
] = m_menus
[j
+ 1];
508 m_titles
[j
] = m_titles
[j
+ 1];
512 // Find the menu menuString, item itemString, and return the item id.
513 // Returns -1 if none found.
514 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
518 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
520 for (i
= 0; i
< m_menuCount
; i
++)
522 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
523 if (strcmp (buf1
, buf2
) == 0)
524 return m_menus
[i
]->FindItem (itemString
);
529 wxMenuItem
*wxMenuBar::FindItemForId (int Id
, wxMenu
** itemMenu
) const
534 wxMenuItem
*item
= NULL
;
536 for (i
= 0; i
< m_menuCount
; i
++)
537 if ((item
= m_menus
[i
]->FindItemForId (Id
, itemMenu
)))
542 void wxMenuBar::SetHelpString (int Id
, const wxString
& helpString
)
545 for (i
= 0; i
< m_menuCount
; i
++)
547 if (m_menus
[i
]->FindItemForId (Id
))
549 m_menus
[i
]->SetHelpString (Id
, helpString
);
555 wxString
wxMenuBar::GetHelpString (int Id
) const
558 for (i
= 0; i
< m_menuCount
; i
++)
560 if (m_menus
[i
]->FindItemForId (Id
))
561 eturn
wxString(m_menus
[i
]->GetHelpString (Id
));