]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/menu.cpp
468ed194e416ea665b4c7704656550596b87d95b
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
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"
31 #include <Xm/LabelG.h>
32 #include <Xm/CascadeBG.h>
33 #include <Xm/CascadeB.h>
34 #include <Xm/SeparatoG.h>
35 #include <Xm/PushBG.h>
36 #include <Xm/ToggleB.h>
37 #include <Xm/ToggleBG.h>
38 #include <Xm/RowColumn.h>
40 // other standard headers
41 // ----------------------
44 void wxMenuItemCallback (Widget w
, XtPointer clientData
,
46 void wxMenuItemArmCallback (Widget w
, XtPointer clientData
,
48 void wxMenuItemDisarmCallback (Widget w
, XtPointer clientData
,
51 #if !USE_SHARED_LIBRARY
52 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
53 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
56 // ============================================================================
58 // ============================================================================
62 // Construct a menu with optional title (then use append)
63 wxMenu::wxMenu(const wxString
& title
, const wxFunction func
)
66 m_parent
= (wxEvtHandler
*) NULL
;
67 m_eventHandler
= this;
71 //// Motif-specific members
73 m_menuWidget
= (WXWidget
) NULL
;
74 m_popupShell
= (WXWidget
) NULL
;
75 m_buttonWidget
= (WXWidget
) NULL
;
77 m_topMenu
= (wxMenu
*) NULL
;
78 m_ownedByMenuBar
= FALSE
;
79 m_menuParent
= (wxMenu
*) NULL
;
92 // The wxWindow destructor will take care of deleting the submenus.
95 // TODO destroy menu and children
97 wxNode
*node
= m_menuItems
.First();
100 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
102 // Delete child menus.
103 // Beware: they must not be appended to children list!!!
104 // (because order of delete is significant)
105 if (item
->GetSubMenu())
106 item
->DeleteSubMenu();
108 wxNode
*next
= node
->Next();
120 // function appends a new item or submenu to the menu
121 void wxMenu::Append(wxMenuItem
*pItem
)
125 wxCHECK_RET( pItem
!= NULL
, "can't append NULL item to the menu" );
127 m_menuItems
.Append(pItem
);
132 void wxMenu::AppendSeparator()
135 Append(new wxMenuItem(this, ID_SEPARATOR
));
139 void wxMenu::Append(int Id
, const wxString
& label
, wxMenu
*SubMenu
,
140 const wxString
& helpString
)
142 Append(new wxMenuItem(this, Id
, label
, helpString
, FALSE
, SubMenu
));
145 // Ordinary menu item
146 void wxMenu::Append(int Id
, const wxString
& label
,
147 const wxString
& helpString
, bool checkable
)
149 // 'checkable' parameter is useless for Windows.
150 Append(new wxMenuItem(this, Id
, label
, helpString
, checkable
));
153 void wxMenu::Delete(int id
)
159 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++) {
160 item
= (wxMenuItem
*)node
->Data();
161 if (item
->GetId() == id
)
168 m_menuItems
.DeleteNode(node
);
174 void wxMenu::Enable(int Id
, bool Flag
)
176 wxMenuItem
*item
= FindItemForId(Id
);
177 wxCHECK_RET( item
!= NULL
, "can't enable non-existing menu item" );
182 bool wxMenu::Enabled(int Id
) const
184 wxMenuItem
*item
= FindItemForId(Id
);
185 wxCHECK( item
!= NULL
, FALSE
);
187 return item
->IsEnabled();
190 void wxMenu::Check(int Id
, bool Flag
)
192 wxMenuItem
*item
= FindItemForId(Id
);
193 wxCHECK_RET( item
!= NULL
, "can't get status of non-existing menu item" );
198 bool wxMenu::Checked(int Id
) const
200 wxMenuItem
*item
= FindItemForId(Id
);
201 wxCHECK( item
!= NULL
, FALSE
);
203 return item
->IsChecked();
206 void wxMenu::SetTitle(const wxString
& label
)
212 const wxString
wxMenu::GetTitle() const
217 void wxMenu::SetLabel(int id
, const wxString
& label
)
219 wxMenuItem
*item
= FindItemForId(id
) ;
223 if (item
->GetSubMenu()==NULL
)
231 item
->SetName(label
);
234 wxString
wxMenu::GetLabel(int Id
) const
237 return wxString("") ;
240 // Finds the item id matching the given string, -1 if not found.
241 int wxMenu::FindItem (const wxString
& itemString
) const
245 wxStripMenuCodes ((char *)(const char *)itemString
, buf1
);
247 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
249 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
250 if (item
->GetSubMenu())
252 int ans
= item
->GetSubMenu()->FindItem(itemString
);
256 if ( !item
->IsSeparator() )
258 wxStripMenuCodes((char *)item
->GetName().c_str(), buf2
);
259 if (strcmp(buf1
, buf2
) == 0)
260 return item
->GetId();
267 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
271 for (wxNode
* node
= m_menuItems
.First (); node
; node
= node
->Next ())
273 wxMenuItem
*item
= (wxMenuItem
*) node
->Data ();
275 if (item
->GetId() == itemId
)
278 *itemMenu
= (wxMenu
*) this;
282 if (item
->GetSubMenu())
284 wxMenuItem
*ans
= item
->GetSubMenu()->FindItemForId (itemId
, itemMenu
);
295 void wxMenu::SetHelpString(int itemId
, const wxString
& helpString
)
297 wxMenuItem
*item
= FindItemForId (itemId
);
299 item
->SetHelp(helpString
);
302 wxString
wxMenu::GetHelpString (int itemId
) const
304 wxMenuItem
*item
= FindItemForId (itemId
);
306 return (item
== NULL
) ? str
: item
->GetHelp();
309 void wxMenu::ProcessCommand(wxCommandEvent
& event
)
311 bool processed
= FALSE
;
316 (void) (*(m_callback
)) (*this, event
);
320 // Try the menu's event handler
321 if ( !processed
&& GetEventHandler())
323 processed
= GetEventHandler()->ProcessEvent(event
);
326 // Try the window the menu was popped up from (and up
327 // through the hierarchy)
328 if ( !processed && GetInvokingWindow())
329 processed = GetInvokingWindow()->ProcessEvent(event);
333 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
340 wxMenuBar::wxMenuBar()
342 m_eventHandler
= this;
346 m_menuBarFrame
= NULL
;
351 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
353 m_eventHandler
= this;
356 m_titles
= new wxString
[n
];
358 for ( i
= 0; i
< n
; i
++ )
359 m_titles
[i
] = titles
[i
];
360 m_menuBarFrame
= NULL
;
365 wxMenuBar::~wxMenuBar()
368 for (i
= 0; i
< m_menuCount
; i
++)
378 // Must only be used AFTER menu has been attached to frame,
379 // otherwise use individual menus to enable/disable items
380 void wxMenuBar::Enable(int id
, bool flag
)
382 wxMenu
*itemMenu
= NULL
;
383 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
390 void wxMenuBar::EnableTop(int pos
, bool flag
)
395 // Must only be used AFTER menu has been attached to frame,
396 // otherwise use individual menus
397 void wxMenuBar::Check(int id
, bool flag
)
399 wxMenu
*itemMenu
= NULL
;
400 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
404 if (!item
->IsCheckable())
410 bool wxMenuBar::Checked(int id
) const
412 wxMenu
*itemMenu
= NULL
;
413 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
421 bool wxMenuBar::Enabled(int id
) const
423 wxMenu
*itemMenu
= NULL
;
424 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
433 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
435 wxMenu
*itemMenu
= NULL
;
436 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
444 wxString
wxMenuBar::GetLabel(int id
) const
446 wxMenu
*itemMenu
= NULL
;
447 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
453 return wxString("") ;
456 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
461 wxString
wxMenuBar::GetLabelTop(int pos
) const
467 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
473 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const char *title
)
479 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
481 if (!OnAppend(menu
, title
))
485 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
486 wxString
*new_titles
= new wxString
[m_menuCount
];
489 for (i
= 0; i
< m_menuCount
- 1; i
++)
491 new_menus
[i
] = m_menus
[i
];
493 new_titles
[i
] = m_titles
[i
];
502 m_titles
= new_titles
;
504 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
505 m_titles
[m_menuCount
- 1] = title
;
510 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
517 for (ii
= 0; ii
< m_menuCount
; ii
++)
519 if (m_menus
[ii
] == menu
)
522 if (ii
>= m_menuCount
)
526 if (ii
< 0 || ii
>= m_menuCount
)
531 if (!OnDelete(menu
, ii
))
534 menu
->SetParent(NULL
);
537 for (j
= ii
; j
< m_menuCount
; j
++)
539 m_menus
[j
] = m_menus
[j
+ 1];
540 m_titles
[j
] = m_titles
[j
+ 1];
544 // Find the menu menuString, item itemString, and return the item id.
545 // Returns -1 if none found.
546 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
550 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
552 for (i
= 0; i
< m_menuCount
; i
++)
554 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
555 if (strcmp (buf1
, buf2
) == 0)
556 return m_menus
[i
]->FindItem (itemString
);
561 wxMenuItem
*wxMenuBar::FindItemForId (int Id
, wxMenu
** itemMenu
) const
566 wxMenuItem
*item
= NULL
;
568 for (i
= 0; i
< m_menuCount
; i
++)
569 if ((item
= m_menus
[i
]->FindItemForId (Id
, itemMenu
)))
574 void wxMenuBar::SetHelpString (int Id
, const wxString
& helpString
)
577 for (i
= 0; i
< m_menuCount
; i
++)
579 if (m_menus
[i
]->FindItemForId (Id
))
581 m_menus
[i
]->SetHelpString (Id
, helpString
);
587 wxString
wxMenuBar::GetHelpString (int Id
) const
590 for (i
= 0; i
< m_menuCount
; i
++)
592 if (m_menus
[i
]->FindItemForId (Id
))
593 return wxString(m_menus
[i
]->GetHelpString (Id
));