#include "wx/menu.h"
#include "wx/log.h"
#include "wx/intl.h"
+#include "wx/app.h"
#include "gdk/gdk.h"
#include "gtk/gtk.h"
IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
+wxMenuBar::wxMenuBar( long style )
+{
+ m_needParent = FALSE; // hmmm
+
+ PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" );
+
+ m_menus.DeleteContents( TRUE );
+
+ m_menubar = gtk_menu_bar_new();
+
+ if (style & wxMB_DOCKABLE)
+ {
+ m_widget = gtk_handle_box_new();
+ gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
+ gtk_widget_show( GTK_WIDGET(m_menubar) );
+ }
+ else
+ {
+ m_widget = GTK_WIDGET(m_menubar);
+ }
+
+ PostCreation();
+
+ Show( TRUE );
+}
+
wxMenuBar::wxMenuBar()
{
m_needParent = FALSE; // hmmm
if (pos != -1) menu->m_title.Remove( pos, 1 );
} while (pos != -1);
- GtkWidget *root_menu;
- root_menu = gtk_menu_item_new_with_label( WXSTRINGCAST(menu->m_title) );
- gtk_widget_show( root_menu );
- gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu), menu->m_menu );
+ menu->m_owner = gtk_menu_item_new_with_label( WXSTRINGCAST(menu->m_title) );
+ gtk_widget_show( menu->m_owner );
+ gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
- gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), root_menu );
+ gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner );
}
static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
wxMenuItem *item = (wxMenuItem*)node->Data();
if (item->IsSubMenu())
return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
+
node = node->Next();
}
{
wxMenuItem* item = FindMenuItemById( id );
if (item) return item->IsEnabled();
+
return FALSE;
}
+wxString wxMenuBar::GetLabel( int id ) const
+{
+ wxMenuItem* item = FindMenuItemById( id );
+
+ if (item) return item->GetText();
+
+ return wxString("");
+}
+
+void wxMenuBar::SetLabel( int id, const wxString &label )
+{
+ wxMenuItem* item = FindMenuItemById( id );
+
+ if (item) item->SetText( label );
+}
+
+void wxMenuBar::EnableTop( int pos, bool flag )
+{
+ wxNode *node = m_menus.Nth( pos );
+
+ wxCHECK_RET( node, "menu not found" );
+
+ wxMenu* menu = (wxMenu*)node->Data();
+
+ if (menu->m_owner) gtk_widget_set_sensitive( menu->m_owner, flag );
+}
+
+wxString wxMenuBar::GetLabelTop( int pos ) const
+{
+ wxNode *node = m_menus.Nth( pos );
+
+ wxCHECK_MSG( node, "invalid", "menu not found" );
+
+ wxMenu* menu = (wxMenu*)node->Data();
+
+ return menu->GetTitle();
+}
+
+void wxMenuBar::SetLabelTop( int pos, const wxString& label )
+{
+ wxNode *node = m_menus.Nth( pos );
+
+ wxCHECK_RET( node, "menu not found" );
+
+ wxMenu* menu = (wxMenu*)node->Data();
+
+ menu->SetTitle( label );
+}
+
+void wxMenuBar::SetHelpString( int id, const wxString& helpString )
+{
+ wxMenuItem* item = FindMenuItemById( id );
+
+ if (item) item->SetHelp( helpString );
+}
+
+wxString wxMenuBar::GetHelpString( int id ) const
+{
+ wxMenuItem* item = FindMenuItemById( id );
+
+ if (item)
+ return item->GetHelp();
+ else
+ return wxString("");
+}
+
//-----------------------------------------------------------------------------
// "activate"
//-----------------------------------------------------------------------------
else
{
/* the user pressed on the menu item -> report */
- item->m_isChecked; /* make consistent again */
+ item->m_isChecked = item->IsChecked(); /* make consistent again */
}
}
if (!menu->IsEnabled(id)) return;
- wxCommandEvent event( wxEVT_MENU_HIGHLIGHT, id );
+ wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
event.SetEventObject( menu );
- event.SetInt(id );
/* wxMSW doesn't call callback here either
if (win) win->GetEventHandler()->ProcessEvent( event );
}
+//-----------------------------------------------------------------------------
+// "deselect"
+//-----------------------------------------------------------------------------
+
+static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
+{
+ int id = menu->FindMenuIdByMenuItem(widget);
+
+ wxASSERT( id != -1 ); // should find it!
+
+ if (!menu->IsEnabled(id)) return;
+
+ wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
+ event.SetEventObject( menu );
+
+ if (menu->GetEventHandler()->ProcessEvent(event)) return;
+
+ wxWindow *win = menu->GetInvokingWindow();
+ if (win) win->GetEventHandler()->ProcessEvent( event );
+}
+
//-----------------------------------------------------------------------------
// wxMenuItem
//-----------------------------------------------------------------------------
Append(-2, m_title);
AppendSeparator();
}
+
+ m_owner = (GtkWidget*) NULL;
}
void wxMenu::SetTitle( const wxString& title )
GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
(gpointer*)this );
+ gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
+ GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
+ (gpointer*)this );
+
gtk_menu_append( GTK_MENU(m_menu), menuItem );
gtk_widget_show( menuItem );
m_items.Append( mitem );
wxMenuItem *mitem = new wxMenuItem();
mitem->SetId(id);
mitem->SetText(text);
+ mitem->SetHelp(helpStr);
GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText());
- mitem->SetHelp(helpStr);
mitem->SetMenuItem(menuItem);
mitem->SetSubMenu(subMenu);
+ gtk_signal_connect( GTK_OBJECT(menuItem), "select",
+ GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
+ (gpointer*)this );
+
+ gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
+ GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
+ (gpointer*)this );
+
gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
gtk_menu_append( GTK_MENU(m_menu), menuItem );
gtk_widget_show( menuItem );
m_items.Append( mitem );
}
+void wxMenu::Append( wxMenuItem *item )
+{
+ m_items.Append( item );
+
+ GtkWidget *menuItem = (GtkWidget*) NULL;
+
+ if (item->IsSeparator())
+ menuItem = gtk_menu_item_new();
+ else if (item->IsSubMenu())
+ menuItem = gtk_menu_item_new_with_label(item->GetText());
+ else
+ menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText())
+ : gtk_menu_item_new_with_label(item->GetText());
+
+ if (!item->IsSeparator())
+ {
+ gtk_signal_connect( GTK_OBJECT(menuItem), "select",
+ GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
+ (gpointer*)this );
+
+ gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
+ GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
+ (gpointer*)this );
+
+ if (!item->IsSubMenu())
+ {
+ gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
+ GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
+ (gpointer*)this );
+ }
+ }
+
+ gtk_menu_append( GTK_MENU(m_menu), menuItem );
+ gtk_widget_show( menuItem );
+ item->SetMenuItem(menuItem);
+}
+
int wxMenu::FindItem( const wxString itemString ) const
{
wxString s( itemString );
return m_invokingWindow;
}
+// Update a menu and all submenus recursively.
+// source is the object that has the update event handlers
+// defined for it. If NULL, the menu or associated window
+// will be used.
+void wxMenu::UpdateUI(wxEvtHandler* source)
+{
+ if (!source && GetInvokingWindow())
+ source = GetInvokingWindow()->GetEventHandler();
+ if (!source)
+ source = GetEventHandler();
+ if (!source)
+ source = this;
+
+ wxNode* node = GetItems().First();
+ while (node)
+ {
+ wxMenuItem* item = (wxMenuItem*) node->Data();
+ if ( !item->IsSeparator() )
+ {
+ wxWindowID id = item->GetId();
+ wxUpdateUIEvent event(id);
+ event.SetEventObject( source );
+
+ if (source->ProcessEvent(event))
+ {
+ if (event.GetSetText())
+ SetLabel(id, event.GetText());
+ if (event.GetSetChecked())
+ Check(id, event.GetChecked());
+ if (event.GetSetEnabled())
+ Enable(id, event.GetEnabled());
+ }
+
+ if (item->GetSubMenu())
+ item->GetSubMenu()->UpdateUI(source);
+ }
+ node = node->Next();
+ }
+}
+