]>
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 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
35 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
38 // ============================================================================
40 // ============================================================================
44 // Construct a menu with optional title (then use append)
45 wxMenu::wxMenu(const wxString
& title
, const wxFunction func
)
49 m_eventHandler
= this;
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
);
303 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
310 wxMenuBar::wxMenuBar()
312 m_eventHandler
= this;
316 m_menuBarFrame
= NULL
;
321 wxMenuBar::wxMenuBar(int n
, wxMenu
*Mmnus
[], const wxString titles
[])
323 m_eventHandler
= this;
326 m_titles
= new wxString
[n
];
328 for ( i
= 0; i
< n
; i
++ )
329 m_titles
[i
] = titles
[i
];
330 m_menuBarFrame
= NULL
;
335 wxMenuBar::~wxMenuBar()
338 for (i
= 0; i
< m_menuCount
; i
++)
348 // Must only be used AFTER menu has been attached to frame,
349 // otherwise use individual menus to enable/disable items
350 void wxMenuBar::Enable(int id
, bool flag
)
352 wxMenu
*itemMenu
= NULL
;
353 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
360 void wxMenuBar::EnableTop(int pos
, bool flag
)
365 // Must only be used AFTER menu has been attached to frame,
366 // otherwise use individual menus
367 void wxMenuBar::Check(int id
, bool flag
)
369 wxMenu
*itemMenu
= NULL
;
370 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
374 if (!item
->IsCheckable())
380 bool wxMenuBar::Checked(int id
) const
382 wxMenu
*itemMenu
= NULL
;
383 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
391 bool wxMenuBar::Enabled(int id
) const
393 wxMenu
*itemMenu
= NULL
;
394 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
403 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
405 wxMenu
*itemMenu
= NULL
;
406 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
414 wxString
wxMenuBar::GetLabel(int id
) const
416 wxMenu
*itemMenu
= NULL
;
417 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
423 return wxString("") ;
426 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
431 wxString
wxMenuBar::GetLabelTop(int pos
) const
437 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
443 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const char *title
)
449 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
451 if (!OnAppend(menu
, title
))
455 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
456 wxString
*new_titles
= new wxString
[m_menuCount
];
459 for (i
= 0; i
< m_menuCount
- 1; i
++)
461 new_menus
[i
] = m_menus
[i
];
463 new_titles
[i
] = m_titles
[i
];
472 m_titles
= new_titles
;
474 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
475 m_titles
[m_menuCount
- 1] = title
;
480 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
487 for (ii
= 0; ii
< m_menuCount
; ii
++)
489 if (m_menus
[ii
] == menu
)
492 if (ii
>= m_menuCount
)
496 if (ii
< 0 || ii
>= m_menuCount
)
501 if (!OnDelete(menu
, ii
))
504 menu
->SetParent(NULL
);
507 for (j
= ii
; j
< m_menuCount
; j
++)
509 m_menus
[j
] = m_menus
[j
+ 1];
510 m_titles
[j
] = m_titles
[j
+ 1];
514 // Find the menu menuString, item itemString, and return the item id.
515 // Returns -1 if none found.
516 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
520 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
522 for (i
= 0; i
< m_menuCount
; i
++)
524 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
525 if (strcmp (buf1
, buf2
) == 0)
526 return m_menus
[i
]->FindItem (itemString
);
531 wxMenuItem
*wxMenuBar::FindItemForId (int Id
, wxMenu
** itemMenu
) const
536 wxMenuItem
*item
= NULL
;
538 for (i
= 0; i
< m_menuCount
; i
++)
539 if ((item
= m_menus
[i
]->FindItemForId (Id
, itemMenu
)))
544 void wxMenuBar::SetHelpString (int Id
, const wxString
& helpString
)
547 for (i
= 0; i
< m_menuCount
; i
++)
549 if (m_menus
[i
]->FindItemForId (Id
))
551 m_menus
[i
]->SetHelpString (Id
, helpString
);
557 wxString
wxMenuBar::GetHelpString (int Id
) const
560 for (i
= 0; i
< m_menuCount
; i
++)
562 if (m_menus
[i
]->FindItemForId (Id
))
563 eturn
wxString(m_menus
[i
]->GetHelpString (Id
));