1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "menu.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
24 wxMenuBar::wxMenuBar(void)
26 m_needParent
= FALSE
; // hmmm
28 PreCreation( NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, "menu" );
30 m_menus
.DeleteContents( TRUE
);
32 m_widget
= gtk_handle_box_new();
34 m_menubar
= gtk_menu_bar_new();
36 gtk_container_add( GTK_CONTAINER(m_widget
), m_menubar
);
38 gtk_widget_show( m_menubar
);
45 void wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
47 m_menus
.Append( menu
);
48 menu
->m_title
= title
; // ??????
52 pos
= menu
->m_title
.First( '&' );
53 if (pos
!= wxString::npos
) menu
->m_title
.Remove( pos
, 1 );
54 } while (pos
!= wxString::npos
);
57 root_menu
= gtk_menu_item_new_with_label( WXSTRINGCAST(menu
->m_title
) );
58 gtk_widget_show( root_menu
);
59 gtk_menu_item_set_submenu( GTK_MENU_ITEM(root_menu
), menu
->m_menu
);
61 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), root_menu
);
64 int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
66 if (menu
->m_title
== menuString
)
68 int res
= menu
->FindItem( itemString
);
69 if (res
!= -1) return res
;
71 wxNode
*node
= menu
->m_items
.First();
74 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
75 if (item
->m_subMenu
) return FindMenuItemRecursive( item
->m_subMenu
, menuString
, itemString
);
81 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
83 wxNode
*node
= m_menus
.First();
86 wxMenu
*menu
= (wxMenu
*)node
->Data();
87 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
88 if (res
!= -1) return res
;
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 void gtk_menu_clicked_callback( GtkWidget
*widget
, gpointer data
)
101 wxMenu
*menu
= (wxMenu
*)data
;
102 int id
= menu
->FindMenuIdByMenuItem(widget
);
104 if (!menu
->Enabled(id
)) return;
106 wxCommandEvent
event( wxEVENT_TYPE_MENU_COMMAND
, id
);
107 event
.SetEventObject( menu
);
109 wxWindow
*win
= menu
->GetInvokingWindow();
110 if (win
) win
->ProcessEvent( event
);
113 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
115 wxMenuItem::wxMenuItem(void)
119 m_isCheckMenu
= FALSE
;
127 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
129 wxMenu::wxMenu( const wxString
&title
)
132 m_items
.DeleteContents( TRUE
);
133 m_invokingWindow
= NULL
;
134 m_menu
= gtk_menu_new(); // Do not show!
137 void wxMenu::AppendSeparator(void)
139 wxMenuItem
*mitem
= new wxMenuItem();
142 mitem
->m_helpStr
= "";
143 mitem
->m_isCheckMenu
= FALSE
;
144 mitem
->m_isEnabled
= TRUE
;
145 mitem
->m_menuItem
= gtk_menu_item_new();
146 gtk_menu_append( GTK_MENU(m_menu
), mitem
->m_menuItem
);
147 gtk_widget_show( mitem
->m_menuItem
);
148 m_items
.Append( mitem
);
151 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
153 wxMenuItem
*mitem
= new wxMenuItem();
156 mitem
->m_text
= item
;
159 pos
= mitem
->m_text
.First( '&' );
160 if (pos
!= wxString::npos
) mitem
->m_text
.Remove( pos
, 1 );
161 } while (pos
!= wxString::npos
);
163 mitem
->m_helpStr
= helpStr
;
164 mitem
->m_isCheckMenu
= checkable
;
165 mitem
->m_isEnabled
= TRUE
;
167 mitem
->m_menuItem
= gtk_check_menu_item_new_with_label( WXSTRINGCAST(mitem
->m_text
) );
169 mitem
->m_menuItem
= gtk_menu_item_new_with_label( WXSTRINGCAST(mitem
->m_text
) );
171 gtk_signal_connect( GTK_OBJECT(mitem
->m_menuItem
), "activate",
172 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
), (gpointer
*)this );
174 gtk_menu_append( GTK_MENU(m_menu
), mitem
->m_menuItem
);
175 gtk_widget_show( mitem
->m_menuItem
);
176 m_items
.Append( mitem
);
179 void wxMenu::Append( int id
, const wxString
&item
, wxMenu
*subMenu
, const wxString
&helpStr
)
181 wxMenuItem
*mitem
= new wxMenuItem();
183 mitem
->m_text
= item
;
184 mitem
->m_isEnabled
= TRUE
;
188 pos
= mitem
->m_text
.First( '&' );
189 if (pos
!= wxString::npos
) mitem
->m_text
.Remove( pos
, 1 );
190 } while (pos
!= wxString::npos
);
192 mitem
->m_helpStr
= helpStr
;
193 mitem
->m_menuItem
= gtk_menu_item_new_with_label( WXSTRINGCAST(mitem
->m_text
) );
195 mitem
->m_subMenu
= subMenu
;
196 gtk_menu_item_set_submenu( GTK_MENU_ITEM(mitem
->m_menuItem
), subMenu
->m_menu
);
197 gtk_menu_append( GTK_MENU(m_menu
), mitem
->m_menuItem
);
198 gtk_widget_show( mitem
->m_menuItem
);
199 m_items
.Append( mitem
);
202 int wxMenu::FindItem( const wxString itemString
) const
204 wxString
s( itemString
);
208 pos
= s
.First( '&' );
209 if (pos
!= wxString::npos
) s
.Remove( pos
, 1 );
210 } while (pos
!= wxString::npos
);
212 wxNode
*node
= m_items
.First();
215 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
216 if (item
->m_text
== s
) return item
->m_id
;
222 void wxMenu::Enable( int id
, bool enable
)
224 wxNode
*node
= m_items
.First();
227 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
228 if (item
->m_id
== id
)
230 item
->m_isEnabled
= enable
;
237 bool wxMenu::Enabled( int id
) const
239 wxNode
*node
= m_items
.First();
242 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
243 if (item
->m_id
== id
) return item
->m_isEnabled
;
249 void wxMenu::SetLabel( int id
, const wxString
&label
)
254 pos
= s
.First( '&' );
255 if (pos
!= wxString::npos
) s
.Remove( pos
, 1 );
256 } while (pos
!= wxString::npos
);
258 wxNode
*node
= m_items
.First();
261 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
263 if (item
->m_id
== id
)
265 gtk_label_set( GTK_LABEL( GTK_BIN(item
->m_menuItem
)->child
), WXSTRINGCAST(s
) );
271 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
273 wxNode
*node
= m_items
.First();
276 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
277 if (item
->m_menuItem
== menuItem
) return item
->m_id
;
283 void wxMenu::SetInvokingWindow( wxWindow
*win
)
285 m_invokingWindow
= win
;
288 wxWindow
*wxMenu::GetInvokingWindow(void)
290 return m_invokingWindow
;