]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/pluginmenu.cpp
Remove <gtk/gtk.h> from gtkutil/window.h
[xonotic/netradiant.git] / radiant / pluginmenu.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include "pluginmenu.h"
23
24 #include "stream/textstream.h"
25
26 #include "gtkutil/pointer.h"
27 #include "gtkutil/menu.h"
28
29 #include "pluginmanager.h"
30 #include "mainframe.h"
31 #include "preferences.h"
32
33
34 int m_nNextPlugInID = 0;
35
36 void plugin_activated( ui::Widget widget, gpointer data ){
37         const char* str = (const char*)g_object_get_data( G_OBJECT( widget ),"command" );
38         GetPlugInMgr().Dispatch( gpointer_to_int( data ), str );
39 }
40
41 #include <stack>
42
43 void PlugInMenu_Add( ui::Menu plugin_menu, IPlugIn* pPlugIn ){
44         ui::Widget item, parent;
45         ui::Menu menu{nullptr}, subMenu{nullptr};
46         const char *menuText, *menuCommand;
47         std::stack<ui::Menu> menuStack;
48
49         parent = ui::MenuItem( pPlugIn->getMenuName() );
50         parent.show();
51         plugin_menu.add(parent);
52
53         std::size_t nCount = pPlugIn->getCommandCount();
54         if ( nCount > 0 ) {
55                 menu = ui::Menu();
56                 if ( g_Layout_enableDetachableMenus.m_value ) {
57                         menu_tearoff( menu );
58                 }
59                 while ( nCount > 0 )
60                 {
61                         menuText = pPlugIn->getCommandTitle( --nCount );
62                         menuCommand = pPlugIn->getCommand( nCount );
63
64                         if ( menuText != 0 && strlen( menuText ) > 0 ) {
65                                 if ( !strcmp( menuText, "-" ) ) {
66                                         item = ui::Widget(gtk_menu_item_new());
67                                         gtk_widget_set_sensitive( item, FALSE );
68                                 }
69                                 else if ( !strcmp( menuText, ">" ) ) {
70                                         menuText = pPlugIn->getCommandTitle( --nCount );
71                                         menuCommand = pPlugIn->getCommand( nCount );
72                                         if ( !strcmp( menuText, "-" ) || !strcmp( menuText, ">" ) || !strcmp( menuText, "<" ) ) {
73                                                 globalErrorStream() << pPlugIn->getMenuName() << " Invalid title (" << menuText << ") for submenu.\n";
74                                                 continue;
75                                         }
76
77                                         item = ui::MenuItem( menuText );
78                                         item.show();
79                                         menu.add(item);
80
81                                         subMenu = ui::Menu();
82                                         gtk_menu_item_set_submenu( GTK_MENU_ITEM( item ), subMenu );
83                                         menuStack.push( menu );
84                                         menu = subMenu;
85                                         continue;
86                                 }
87                                 else if ( !strcmp( menuText, "<" ) ) {
88                                         if ( !menuStack.empty() ) {
89                                                 menu = menuStack.top();
90                                                 menuStack.pop();
91                                         }
92                                         else
93                                         {
94                                                 globalErrorStream() << pPlugIn->getMenuName() << ": Attempt to end non-existent submenu ignored.\n";
95                                         }
96                                         continue;
97                                 }
98                                 else
99                                 {
100                                         item = ui::MenuItem( menuText );
101                                         g_object_set_data( G_OBJECT( item ),"command", const_cast<gpointer>( static_cast<const void*>( menuCommand ) ) );
102                                         g_signal_connect( G_OBJECT( item ), "activate", G_CALLBACK( plugin_activated ), gint_to_pointer( m_nNextPlugInID ) );
103                                 }
104                                 item.show();
105                                 menu.add(item);
106                                 pPlugIn->addMenuID( m_nNextPlugInID++ );
107                         }
108                 }
109                 if ( !menuStack.empty() ) {
110                         std::size_t size = menuStack.size();
111                         if ( size != 0 ) {
112                                 globalErrorStream() << pPlugIn->getMenuName() << " mismatched > <. " << Unsigned( size ) << " submenu(s) not closed.\n";
113                         }
114                         for ( std::size_t i = 0; i < ( size - 1 ); i++ )
115                         {
116                                 menuStack.pop();
117                         }
118                         menu = menuStack.top();
119                         menuStack.pop();
120                 }
121                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( parent ), menu );
122         }
123 }
124
125 ui::Menu g_plugins_menu{ui::null};
126 GtkMenuItem* g_plugins_menu_separator = 0;
127
128 void PluginsMenu_populate(){
129         class PluginsMenuConstructor : public PluginsVisitor
130         {
131         ui::Menu m_menu;
132 public:
133         PluginsMenuConstructor( ui::Menu menu ) : m_menu( menu ){
134         }
135         void visit( IPlugIn& plugin ){
136                 PlugInMenu_Add( m_menu, &plugin );
137         }
138         };
139
140         PluginsMenuConstructor constructor( g_plugins_menu );
141         GetPlugInMgr().constructMenu( constructor );
142 }
143
144 void PluginsMenu_clear(){
145         m_nNextPlugInID = 0;
146
147         GList* lst = g_list_find( gtk_container_get_children( GTK_CONTAINER( g_plugins_menu ) ), GTK_WIDGET( g_plugins_menu_separator ) );
148         while ( lst->next )
149         {
150                 gtk_container_remove( GTK_CONTAINER( g_plugins_menu ), GTK_WIDGET( lst->next->data ) );
151                 lst = g_list_find( gtk_container_get_children( GTK_CONTAINER( g_plugins_menu ) ),  GTK_WIDGET( g_plugins_menu_separator ) );
152         }
153 }
154
155 ui::MenuItem create_plugins_menu(){
156         // Plugins menu
157         auto plugins_menu_item = new_sub_menu_item_with_mnemonic( "_Plugins" );
158         auto menu = ui::Menu(GTK_MENU( gtk_menu_item_get_submenu( plugins_menu_item ) ));
159         if ( g_Layout_enableDetachableMenus.m_value ) {
160                 menu_tearoff( menu );
161         }
162
163         g_plugins_menu = menu;
164
165         //TODO: some modules/plugins do not yet support refresh
166 #if 0
167         create_menu_item_with_mnemonic( menu, "Refresh", FreeCaller<Restart>() );
168
169         // NOTE: the seperator is used when doing a refresh of the list, everything past the seperator is removed
170         g_plugins_menu_separator = menu_separator( menu );
171 #endif
172
173         PluginsMenu_populate();
174
175         return plugins_menu_item;
176 }