]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/pluginmanager.cpp
Merge branch 'optional_q3map2_type' into 'master'
[xonotic/netradiant.git] / radiant / pluginmanager.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 // PlugInManager.cpp: implementation of the CPlugInManager class.
23 //
24 //////////////////////////////////////////////////////////////////////
25
26 #include "pluginmanager.h"
27
28 #include "modulesystem.h"
29 #include "qerplugin.h"
30 #include "iplugin.h"
31
32 #include "math/vector.h"
33 #include "string/string.h"
34
35 #include "error.h"
36 #include "select.h"
37 #include "plugin.h"
38
39 #include "modulesystem.h"
40
41 #include <list>
42
43 /* plugin manager --------------------------------------- */
44 class CPluginSlot : public IPlugIn {
45     CopiedString m_menu_name;
46     const _QERPluginTable *mpTable;
47     std::list<CopiedString> m_CommandStrings;
48     std::list<CopiedString> m_CommandTitleStrings;
49     std::list<std::size_t> m_CommandIDs;
50
51 public:
52 /*!
53    build directly from a SYN_PROVIDE interface
54  */
55     CPluginSlot(ui::Widget main_window, const char *name, const _QERPluginTable &table);
56
57 /*!
58    dispatching a command by name to the plugin
59  */
60     void Dispatch(const char *p);
61
62 // IPlugIn ------------------------------------------------------------
63     const char *getMenuName();
64
65     std::size_t getCommandCount();
66
67     const char *getCommand(std::size_t n);
68
69     const char *getCommandTitle(std::size_t n);
70
71     void addMenuID(std::size_t n);
72
73     bool ownsCommandID(std::size_t n);
74
75 };
76
77 CPluginSlot::CPluginSlot(ui::Widget main_window, const char *name, const _QERPluginTable &table)
78 {
79     mpTable = &table;
80     m_menu_name = name;
81
82     const char *commands = mpTable->m_pfnQERPlug_GetCommandList();
83     const char *titles = mpTable->m_pfnQERPlug_GetCommandTitleList();
84
85     StringTokeniser commandTokeniser(commands, ",;");
86     StringTokeniser titleTokeniser(titles, ",;");
87
88     const char *cmdToken = commandTokeniser.getToken();
89     const char *titleToken = titleTokeniser.getToken();
90     while (!string_empty(cmdToken)) {
91         if (string_empty(titleToken)) {
92             m_CommandStrings.push_back(cmdToken);
93             m_CommandTitleStrings.push_back(cmdToken);
94             cmdToken = commandTokeniser.getToken();
95             titleToken = "";
96         } else {
97             m_CommandStrings.push_back(cmdToken);
98             m_CommandTitleStrings.push_back(titleToken);
99             cmdToken = commandTokeniser.getToken();
100             titleToken = titleTokeniser.getToken();
101         }
102     }
103     mpTable->m_pfnQERPlug_Init(0, (void *) main_window);
104 }
105
106 const char *CPluginSlot::getMenuName()
107 {
108     return m_menu_name.c_str();
109 }
110
111 std::size_t CPluginSlot::getCommandCount()
112 {
113     return m_CommandStrings.size();
114 }
115
116 const char *CPluginSlot::getCommand(std::size_t n)
117 {
118     std::list<CopiedString>::iterator i = m_CommandStrings.begin();
119     while (n-- != 0) {
120         ++i;
121     }
122     return (*i).c_str();
123 }
124
125 const char *CPluginSlot::getCommandTitle(std::size_t n)
126 {
127     std::list<CopiedString>::iterator i = m_CommandTitleStrings.begin();
128     while (n-- != 0) {
129         ++i;
130     }
131     return (*i).c_str();
132 }
133
134 void CPluginSlot::addMenuID(std::size_t n)
135 {
136     m_CommandIDs.push_back(n);
137 }
138
139 bool CPluginSlot::ownsCommandID(std::size_t n)
140 {
141     for (std::list<std::size_t>::iterator i = m_CommandIDs.begin(); i != m_CommandIDs.end(); ++i) {
142         if (*i == n) {
143             return true;
144         }
145     }
146     return false;
147 }
148
149 void CPluginSlot::Dispatch(const char *p)
150 {
151     Vector3 vMin, vMax;
152     Select_GetBounds(vMin, vMax);
153     mpTable->m_pfnQERPlug_Dispatch(p, reinterpret_cast<float *>( &vMin ), reinterpret_cast<float *>( &vMax ),
154                                    true); //QE_SingleBrush(true));
155 }
156
157
158 class CPluginSlots {
159     std::list<CPluginSlot *> mSlots;
160 public:
161     virtual ~CPluginSlots();
162
163     void AddPluginSlot(ui::Widget main_window, const char *name, const _QERPluginTable &table)
164     {
165         mSlots.push_back(new CPluginSlot(main_window, name, table));
166     }
167
168     void PopulateMenu(PluginsVisitor &menu);
169
170     bool Dispatch(std::size_t n, const char *p);
171 };
172
173 CPluginSlots::~CPluginSlots()
174 {
175     std::list<CPluginSlot *>::iterator iSlot;
176     for (iSlot = mSlots.begin(); iSlot != mSlots.end(); ++iSlot) {
177         delete *iSlot;
178         *iSlot = 0;
179     }
180 }
181
182 void CPluginSlots::PopulateMenu(PluginsVisitor &menu)
183 {
184     std::list<CPluginSlot *>::iterator iPlug;
185     for (iPlug = mSlots.begin(); iPlug != mSlots.end(); ++iPlug) {
186         menu.visit(*(*iPlug));
187     }
188 }
189
190 bool CPluginSlots::Dispatch(std::size_t n, const char *p)
191 {
192     std::list<CPluginSlot *>::iterator iPlug;
193     for (iPlug = mSlots.begin(); iPlug != mSlots.end(); ++iPlug) {
194         CPluginSlot *pPlug = *iPlug;
195         if (pPlug->ownsCommandID(n)) {
196             pPlug->Dispatch(p);
197             return true;
198         }
199     }
200     return false;
201 }
202
203 CPluginSlots g_plugin_slots;
204
205
206 void FillPluginSlots(CPluginSlots &slots, ui::Widget main_window)
207 {
208     class AddPluginVisitor : public PluginModules::Visitor {
209         CPluginSlots &m_slots;
210         ui::Widget m_main_window;
211     public:
212         AddPluginVisitor(CPluginSlots &slots, ui::Widget main_window)
213                 : m_slots(slots), m_main_window(main_window)
214         {
215         }
216
217         void visit(const char *name, const _QERPluginTable &table) const
218         {
219             m_slots.AddPluginSlot(m_main_window, name, table);
220         }
221     } visitor(slots, main_window);
222
223     Radiant_getPluginModules().foreachModule(visitor);
224 }
225
226
227 #include "pluginmanager.h"
228
229 CPlugInManager g_PlugInMgr;
230
231 CPlugInManager &GetPlugInMgr()
232 {
233     return g_PlugInMgr;
234 }
235
236 void CPlugInManager::Dispatch(std::size_t n, const char *p)
237 {
238     g_plugin_slots.Dispatch(n, p);
239 }
240
241 void CPlugInManager::Init(ui::Widget main_window)
242 {
243     FillPluginSlots(g_plugin_slots, main_window);
244 }
245
246 void CPlugInManager::constructMenu(PluginsVisitor &menu)
247 {
248     g_plugin_slots.PopulateMenu(menu);
249 }
250
251 void CPlugInManager::Shutdown()
252 {
253 }