#include "wx/accel.h"
#endif // wxUSE_ACCEL
-#include "gdk/gdk.h"
-#include "gtk/gtk.h"
+#include <gdk/gdk.h>
+#include <gtk/gtk.h>
//-----------------------------------------------------------------------------
// idle system
static wxString GetHotKey( const wxMenuItem& item );
#endif
+//-----------------------------------------------------------------------------
+// idle system
+//-----------------------------------------------------------------------------
+
+static wxString wxReplaceUnderscore( const wxString& title )
+{
+ const wxChar *pc;
+
+ /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
+ wxString str;
+ for ( pc = title; *pc != wxT('\0'); pc++ )
+ {
+ if (*pc == wxT('&'))
+ {
+#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
+ str << wxT('_');
+ }
+ else if (*pc == wxT('/'))
+ {
+ str << wxT('\\');
+#endif
+ }
+ else
+ {
+#if __WXGTK12__
+ if ( *pc == wxT('_') )
+ {
+ // underscores must be doubled to prevent them from being
+ // interpreted as accelerator character prefix by GTK
+ str << *pc;
+ }
+#endif // GTK+ 1.2
+
+ str << *pc;
+ }
+ }
+ return str;
+}
+
//-----------------------------------------------------------------------------
// wxMenuBar
//-----------------------------------------------------------------------------
}
PostCreation();
+
+ ApplyWidgetStyle();
}
wxMenuBar::wxMenuBar()
m_widget = GTK_WIDGET(m_menubar);
PostCreation();
+
+ ApplyWidgetStyle();
}
wxMenuBar::~wxMenuBar()
#if (GTK_MINOR_VERSION > 0)
wxWindow *top_frame = win;
- while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
+ while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
top_frame = top_frame->GetParent();
/* support for native hot keys */
gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
#endif
- wxNode *node = menu->GetItems().First();
+ wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
while (node)
{
- wxMenuItem *menuitem = (wxMenuItem*)node->Data();
+ wxMenuItem *menuitem = node->GetData();
if (menuitem->IsSubMenu())
wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
- node = node->Next();
+ node = node->GetNext();
}
}
{
menu->SetInvokingWindow( win );
-#if (GTK_MINOR_VERSION > 0)
+#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
wxWindow *top_frame = win;
- while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
+ while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
top_frame = top_frame->GetParent();
/* support for native hot keys */
- gtk_accel_group_attach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
+ GtkObject *obj = GTK_OBJECT(top_frame->m_widget);
+ if ( !g_slist_find( menu->m_accel->attach_objects, obj ) )
+ gtk_accel_group_attach( menu->m_accel, obj );
#endif
- wxNode *node = menu->GetItems().First();
+ wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
while (node)
{
- wxMenuItem *menuitem = (wxMenuItem*)node->Data();
+ wxMenuItem *menuitem = node->GetData();
if (menuitem->IsSubMenu())
wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
- node = node->Next();
+ node = node->GetNext();
}
}
m_invokingWindow = win;
#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
wxWindow *top_frame = win;
- while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
+ while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
top_frame = top_frame->GetParent();
/* support for native key accelerators indicated by underscroes */
- gtk_accel_group_attach( m_accel, GTK_OBJECT(top_frame->m_widget) );
+ GtkObject *obj = GTK_OBJECT(top_frame->m_widget);
+ if ( !g_slist_find( m_accel->attach_objects, obj ) )
+ gtk_accel_group_attach( m_accel, obj );
#endif
- wxNode *node = m_menus.First();
+ wxMenuList::Node *node = m_menus.GetFirst();
while (node)
{
- wxMenu *menu = (wxMenu*)node->Data();
+ wxMenu *menu = node->GetData();
wxMenubarSetInvokingWindow( menu, win );
- node = node->Next();
+ node = node->GetNext();
}
}
m_invokingWindow = (wxWindow*) NULL;
#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
wxWindow *top_frame = win;
- while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
+ while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
top_frame = top_frame->GetParent();
/* support for native key accelerators indicated by underscroes */
gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) );
#endif
- wxNode *node = m_menus.First();
+ wxMenuList::Node *node = m_menus.GetFirst();
while (node)
{
- wxMenu *menu = (wxMenu*)node->Data();
+ wxMenu *menu = node->GetData();
wxMenubarUnsetInvokingWindow( menu, win );
- node = node->Next();
+ node = node->GetNext();
}
}
bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
{
- m_menus.Append( menu );
-
- const wxChar *pc;
+ if ( !wxMenuBarBase::Append( menu, title ) )
+ return FALSE;
- /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
- wxString str;
- for ( pc = title; *pc != wxT('\0'); pc++ )
- {
- if (*pc == wxT('&'))
- {
-#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
- str << wxT('_');
- }
- else if (*pc == wxT('/'))
- {
- str << wxT('\\');
-#endif
- }
- else
- {
-#if __WXGTK12__
- if ( *pc == wxT('_') )
- {
- // underscores must be doubled to prevent them from being
- // interpreted as accelerator character prefix by GTK
- str << *pc;
- }
-#endif // GTK+ 1.2
+ return GtkAppend(menu, title);
+}
- str << *pc;
- }
- }
+bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title)
+{
+ wxString str( wxReplaceUnderscore( title ) );
/* this doesn't have much effect right now */
menu->SetTitle( str );
gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
/* in order to get the pointer to the item we need the item text _without_ underscores */
wxString tmp = wxT("<main>/");
+ const wxChar *pc;
for ( pc = str; *pc != wxT('\0'); pc++ )
{
// contrary to the common sense, we must throw out _all_ underscores,
if ( !wxMenuBarBase::Insert(pos, menu, title) )
return FALSE;
- wxFAIL_MSG(wxT("TODO"));
+#if __WXGTK12__
+ // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
+ // of version 1.2.6), so we first append the item and then change its
+ // index
+ if ( !GtkAppend(menu, title) )
+ return FALSE;
+
+ if (pos+1 >= m_menus.GetCount())
+ return TRUE;
+
+ GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
+ gpointer data = g_list_last(menu_shell->children)->data;
+ menu_shell->children = g_list_remove(menu_shell->children, data);
+ menu_shell->children = g_list_insert(menu_shell->children, data, pos);
+
+ return TRUE;
+#else // GTK < 1.2
+ // this should be easy to do with GTK 1.0 - can use standard functions for
+ // this and don't need any hacks like above, but as I don't have GTK 1.0
+ // any more I can't do it
+ wxFAIL_MSG( wxT("TODO") );
return FALSE;
+#endif // GTK 1.2/1.0
}
wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
{
- if ( !wxMenuBarBase::Replace(pos, menu, title) )
- return FALSE;
-
- wxFAIL_MSG(wxT("TODO"));
+ // remove the old item and insert a new one
+ wxMenu *menuOld = Remove(pos);
+ if ( menuOld && !Insert(pos, menu, title) )
+ {
+ return (wxMenu*) NULL;
+ }
- return NULL;
+ // either Insert() succeeded or Remove() failed and menuOld is NULL
+ return menuOld;
}
wxMenu *wxMenuBar::Remove(size_t pos)
{
- if ( !wxMenuBarBase::Remove(pos) )
- return FALSE;
+ wxMenu *menu = wxMenuBarBase::Remove(pos);
+ if ( !menu )
+ return (wxMenu*) NULL;
- wxFAIL_MSG(wxT("TODO"));
+/*
+ GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
- return NULL;
+ printf( "factory entries before %d\n", (int)g_slist_length(m_factory->items) );
+ printf( "menu shell entries before %d\n", (int)g_list_length( menu_shell->children ) );
+*/
+
+ // unparent calls unref() and that would delete the widget so we raise
+ // the ref count to 2 artificially before invoking unparent.
+ gtk_widget_ref( menu->m_menu );
+ gtk_widget_unparent( menu->m_menu );
+
+ gtk_widget_destroy( menu->m_owner );
+
+/*
+ printf( "factory entries after %d\n", (int)g_slist_length(m_factory->items) );
+ printf( "menu shell entries after %d\n", (int)g_list_length( menu_shell->children ) );
+*/
+
+ return menu;
}
static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
{
- if (menu->GetTitle() == menuString)
+ if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString))
{
int res = menu->FindItem( itemString );
if (res != wxNOT_FOUND)
return res;
}
- wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast
+ wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
while (node)
{
- wxMenuItem *item = (wxMenuItem*)node->Data();
+ wxMenuItem *item = node->GetData();
if (item->IsSubMenu())
return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
- node = node->Next();
+ node = node->GetNext();
}
return wxNOT_FOUND;
int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
{
- wxNode *node = m_menus.First();
+ wxMenuList::Node *node = m_menus.GetFirst();
while (node)
{
- wxMenu *menu = (wxMenu*)node->Data();
+ wxMenu *menu = node->GetData();
int res = FindMenuItemRecursive( menu, menuString, itemString);
- if (res != -1) return res;
- node = node->Next();
+ if (res != -1)
+ return res;
+ node = node->GetNext();
}
- return -1;
+
+ return wxNOT_FOUND;
}
// Find a wxMenuItem using its id. Recurses down into sub-menus
{
wxMenuItem* result = menu->FindChildItem(id);
- wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast
+ wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
while ( node && result == NULL )
{
- wxMenuItem *item = (wxMenuItem*)node->Data();
+ wxMenuItem *item = node->GetData();
if (item->IsSubMenu())
{
result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
}
- node = node->Next();
+ node = node->GetNext();
}
return result;
wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
{
wxMenuItem* result = 0;
- wxNode *node = m_menus.First();
+ wxMenuList::Node *node = m_menus.GetFirst();
while (node && result == 0)
{
- wxMenu *menu = (wxMenu*)node->Data();
+ wxMenu *menu = node->GetData();
result = FindMenuItemByIdRecursive( menu, id );
- node = node->Next();
+ node = node->GetNext();
}
if ( menuForItem )
wxMenu* menu = node->GetData();
- return menu->GetTitle();
+ wxString label;
+ wxString text( menu->GetTitle() );
+#if (GTK_MINOR_VERSION > 0)
+ for ( const wxChar *pc = text.c_str(); *pc; pc++ )
+ {
+ if ( *pc == wxT('_') || *pc == wxT('&') )
+ {
+ // '_' is the escape character for GTK+ and '&' is the one for
+ // wxWindows - skip both of them
+ continue;
+ }
+
+ label += *pc;
+ }
+#else // GTK+ 1.0
+ label = text;
+#endif // GTK+ 1.2/1.0
+
+ return label;
}
void wxMenuBar::SetLabelTop( size_t pos, const wxString& label )
wxMenu* menu = node->GetData();
- menu->SetTitle( label );
+ wxString str( wxReplaceUnderscore( label ) );
+
+ menu->SetTitle( str );
+
+ if (menu->m_owner)
+ {
+ GtkLabel *label = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
+
+ /* set new text */
+ gtk_label_set( label, str.mb_str());
+
+ /* reparse key accel */
+ (void)gtk_label_parse_uline (GTK_LABEL(label), str.mb_str() );
+ gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
+ }
+
}
//-----------------------------------------------------------------------------
wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
event.SetEventObject( menu );
- event.SetInt(id );
+ if (item->IsCheckable())
+ event.SetInt( item->IsChecked() );
+#if wxUSE_MENU_CALLBACK
if (menu->GetCallback())
{
(void) (*(menu->GetCallback())) (*menu, event);
return;
}
+#endif // wxUSE_MENU_CALLBACK
if (menu->GetEventHandler()->ProcessEvent(event))
return;
}
// return the menu item text without any menu accels
-wxString wxMenuItem::GetLabel() const
+/* static */
+wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
{
wxString label;
#if (GTK_MINOR_VERSION > 0)
- for ( const wxChar *pc = m_text.c_str(); *pc; pc++ )
+ for ( const wxChar *pc = text.c_str(); *pc; pc++ )
{
- if ( *pc == wxT('_') )
+ if ( *pc == wxT('_') || *pc == wxT('&') )
{
- // this is the escape character for GTK+ - skip it
+ // '_' is the escape character for GTK+ and '&' is the one for
+ // wxWindows - skip both of them
continue;
}
label += *pc;
}
#else // GTK+ 1.0
- label = m_text;
+ label = text;
#endif // GTK+ 1.2/1.0
return label;
gtk_label_set( label, m_text.mb_str());
/* reparse key accel */
- guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), m_text.mb_str() );
+ (void)gtk_label_parse_uline (GTK_LABEL(label), m_text.mb_str() );
gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
}
}
wxAcceleratorEntry *wxMenuItem::GetAccel() const
{
- if ( !item.GetHotKey() )
+ if ( !GetHotKey() )
{
// nothing
return (wxAcceleratorEntry *)NULL;
// as wxGetAccelFromString() looks for TAB, insert a dummy one here
wxString label;
- label << wxT('\t') << item.GetHotKey();
+ label << wxT('\t') << GetHotKey();
return wxGetAccelFromString(label);
}
wxString wxMenuItem::GetFactoryPath() const
{
- /* in order to get the pointer to the item we need the item text _without_ underscores */
+ /* in order to get the pointer to the item we need the item text _without_
+ underscores */
wxString path( wxT("<main>/") );
path += GetLabel();
wxMenu::~wxMenu()
{
+ m_items.Clear();
+
gtk_widget_destroy( m_menu );
gtk_object_unref( GTK_OBJECT(m_factory) );
-
- // the menu items are deleted by the base class dtor
}
-virtual bool wxMenu::DoAppend(wxMenuItem *mitem)
+bool wxMenu::GtkAppend(wxMenuItem *mitem)
{
GtkWidget *menuItem;
entry.callback = (GtkItemFactoryCallback) 0;
entry.callback_action = 0;
entry.item_type = "<Branch>";
+ entry.accelerator = (gchar*) NULL;
gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
wxString path( mitem->GetFactoryPath() );
- GtkWidget *menuItem = gtk_item_factory_get_item( m_factory, path.mb_str() );
+ menuItem = gtk_item_factory_get_item( m_factory, path.mb_str() );
#else // GTK+ 1.0
- GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
+ menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
#endif // GTK 1.2/1.0
- gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
+ gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
+
+ // if adding a submenu to a menu already existing in the menu bar, we
+ // must set invoking window to allow processing events from this
+ // submenu
+ if ( m_invokingWindow )
+ wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
}
else // a normal item
{
entry.path = buf;
entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
entry.callback_action = 0;
- if (checkable)
+ if ( mitem->IsCheckable() )
entry.item_type = "<CheckItem>";
else
entry.item_type = "<Item>";
+ entry.accelerator = (gchar*) NULL;
#if wxUSE_ACCEL
// due to an apparent bug in GTK+, we have to use a static buffer here -
// otherwise GTK+ 1.2.2 manages to override the memory we pass to it
// somehow! (VZ)
- static char s_accel[32]; // must be big enough for <control><alt><shift>F12
- strncpy(s_accel, GetHotKey(*mitem).mb_str(), WXSIZEOF(s_accel));
+ static char s_accel[50]; // must be big enougg
+ wxString tmp( GetHotKey(*mitem) );
+ strncpy(s_accel, tmp.mb_str(), WXSIZEOF(s_accel));
entry.accelerator = s_accel;
#else // !wxUSE_ACCEL
entry.accelerator = (char*) NULL;
gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
wxString path( mitem->GetFactoryPath() );
- GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, path.mb_str() );
+ menuItem = gtk_item_factory_get_widget( m_factory, path.mb_str() );
#else // GTK+ 1.0
- GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() )
- : gtk_menu_item_new_with_label( mitem->GetText().mb_str() );
+ menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() )
+ : gtk_menu_item_new_with_label( mitem->GetText().mb_str() );
gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
mitem->SetMenuItem(menuItem);
- return wxMenuBase::DoAppend(mitem);
+ return TRUE;
}
-// VZ: this seems to be GTK+ 1.0 only code, I don't understand why there were
-// both specialized versions of Append() and this one before my changes,
-// but it seems that the others are better...
-#if 0
-void wxMenu::Append( wxMenuItem *item )
+bool wxMenu::DoAppend(wxMenuItem *mitem)
{
- 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().mbc_str());
- else
- menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str())
- : gtk_menu_item_new_with_label(item->GetText().mbc_str());
-
- 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);
+ return GtkAppend(mitem) && wxMenuBase::DoAppend(mitem);
}
-#endif // 0
bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
{
if ( !wxMenuBase::DoInsert(pos, item) )
return FALSE;
- wxFAIL_MSG(wxT("not implemented"));
+#ifdef __WXGTK12__
+ // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
+ // of version 1.2.6), so we first append the item and then change its
+ // index
+ if ( !GtkAppend(item) )
+ return FALSE;
+
+ if ( m_style & wxMENU_TEAROFF )
+ {
+ // change the position as the first item is the tear-off marker
+ pos++;
+ }
+
+ GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
+ gpointer data = g_list_last(menu_shell->children)->data;
+ menu_shell->children = g_list_remove(menu_shell->children, data);
+ menu_shell->children = g_list_insert(menu_shell->children, data, pos);
+
+ return TRUE;
+#else // GTK < 1.2
+ // this should be easy to do...
+ wxFAIL_MSG( wxT("not implemented") );
return FALSE;
+#endif // GTK 1.2/1.0
}
wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
case WXK_F12:
hotkey << wxT('F') << code - WXK_F1 + 1;
break;
+
+ // GTK seems to use XStringToKeySym here
+ case WXK_NUMPAD_INSERT:
+ hotkey << wxT("KP_Insert" );
+ break;
+ case WXK_NUMPAD_DELETE:
+ hotkey << wxT("KP_Delete" );
+ break;
+ case WXK_INSERT:
+ hotkey << wxT("Insert" );
+ break;
+ case WXK_DELETE:
+ hotkey << wxT("Delete" );
+ break;
// if there are any other keys wxGetAccelFromString() may return,
// we should process them here