2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "pluginmenu.h"
24 #include "stream/textstream.h"
26 #include <gtk/gtkmenu.h>
27 #include <gtk/gtkmenuitem.h>
29 #include "gtkutil/pointer.h"
30 #include "gtkutil/menu.h"
32 #include "pluginmanager.h"
33 #include "mainframe.h"
34 #include "preferences.h"
37 int m_nNextPlugInID = 0;
39 void plugin_activated( GtkWidget* widget, gpointer data ){
40 const char* str = (const char*)g_object_get_data( G_OBJECT( widget ),"command" );
41 GetPlugInMgr().Dispatch( gpointer_to_int( data ), str );
45 typedef std::stack<GtkWidget*> WidgetStack;
47 void PlugInMenu_Add( GtkMenu* plugin_menu, IPlugIn* pPlugIn ){
48 GtkWidget *menu, *item, *parent, *subMenu;
49 const char *menuText, *menuCommand;
50 WidgetStack menuStack;
52 parent = gtk_menu_item_new_with_label( pPlugIn->getMenuName() );
53 gtk_widget_show( parent );
54 gtk_container_add( GTK_CONTAINER( plugin_menu ), parent );
56 std::size_t nCount = pPlugIn->getCommandCount();
58 menu = gtk_menu_new();
59 if ( g_Layout_enableDetachableMenus.m_value ) {
60 menu_tearoff( GTK_MENU( menu ) );
64 menuText = pPlugIn->getCommandTitle( --nCount );
65 menuCommand = pPlugIn->getCommand( nCount );
67 if ( menuText != 0 && strlen( menuText ) > 0 ) {
68 if ( !strcmp( menuText, "-" ) ) {
69 item = gtk_menu_item_new();
70 gtk_widget_set_sensitive( item, FALSE );
72 else if ( !strcmp( menuText, ">" ) ) {
73 menuText = pPlugIn->getCommandTitle( --nCount );
74 menuCommand = pPlugIn->getCommand( nCount );
75 if ( !strcmp( menuText, "-" ) || !strcmp( menuText, ">" ) || !strcmp( menuText, "<" ) ) {
76 globalErrorStream() << pPlugIn->getMenuName() << " Invalid title (" << menuText << ") for submenu.\n";
80 item = gtk_menu_item_new_with_label( menuText );
81 gtk_widget_show( item );
82 gtk_container_add( GTK_CONTAINER( menu ), item );
84 subMenu = gtk_menu_new();
85 gtk_menu_item_set_submenu( GTK_MENU_ITEM( item ), subMenu );
86 menuStack.push( menu );
90 else if ( !strcmp( menuText, "<" ) ) {
91 if ( !menuStack.empty() ) {
92 menu = menuStack.top();
97 globalErrorStream() << pPlugIn->getMenuName() << ": Attempt to end non-existent submenu ignored.\n";
103 item = gtk_menu_item_new_with_label( menuText );
104 g_object_set_data( G_OBJECT( item ),"command", const_cast<gpointer>( static_cast<const void*>( menuCommand ) ) );
105 g_signal_connect( G_OBJECT( item ), "activate", G_CALLBACK( plugin_activated ), gint_to_pointer( m_nNextPlugInID ) );
107 gtk_widget_show( item );
108 gtk_container_add( GTK_CONTAINER( menu ), item );
109 pPlugIn->addMenuID( m_nNextPlugInID++ );
112 if ( !menuStack.empty() ) {
113 std::size_t size = menuStack.size();
115 globalErrorStream() << pPlugIn->getMenuName() << " mismatched > <. " << Unsigned( size ) << " submenu(s) not closed.\n";
117 for ( std::size_t i = 0; i < ( size - 1 ); i++ )
121 menu = menuStack.top();
124 gtk_menu_item_set_submenu( GTK_MENU_ITEM( parent ), menu );
128 GtkMenu* g_plugins_menu = 0;
129 GtkMenuItem* g_plugins_menu_separator = 0;
131 void PluginsMenu_populate(){
132 class PluginsMenuConstructor : public PluginsVisitor
136 PluginsMenuConstructor( GtkMenu* menu ) : m_menu( menu ){
138 void visit( IPlugIn& plugin ){
139 PlugInMenu_Add( m_menu, &plugin );
143 PluginsMenuConstructor constructor( g_plugins_menu );
144 GetPlugInMgr().constructMenu( constructor );
147 void PluginsMenu_clear(){
150 GList* lst = g_list_find( gtk_container_children( GTK_CONTAINER( g_plugins_menu ) ), GTK_WIDGET( g_plugins_menu_separator ) );
153 gtk_container_remove( GTK_CONTAINER( g_plugins_menu ), GTK_WIDGET( lst->next->data ) );
154 lst = g_list_find( gtk_container_children( GTK_CONTAINER( g_plugins_menu ) ), GTK_WIDGET( g_plugins_menu_separator ) );
158 GtkMenuItem* create_plugins_menu(){
160 GtkMenuItem* plugins_menu_item = new_sub_menu_item_with_mnemonic( "_Plugins" );
161 GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( plugins_menu_item ) );
162 if ( g_Layout_enableDetachableMenus.m_value ) {
163 menu_tearoff( menu );
166 g_plugins_menu = menu;
168 //TODO: some modules/plugins do not yet support refresh
170 create_menu_item_with_mnemonic( menu, "Refresh", FreeCaller<Restart>() );
172 // NOTE: the seperator is used when doing a refresh of the list, everything past the seperator is removed
173 g_plugins_menu_separator = menu_separator( menu );
176 PluginsMenu_populate();
178 return plugins_menu_item;