]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/sample/plugin.cpp
* added filter api to create new filters from within plugins
[xonotic/netradiant.git] / plugins / sample / plugin.cpp
1 /*
2 Copyright (C) 1999-2007 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 "plugin.h"
23
24 #define CMD_SEP "-" 
25 #define CMD_ABOUT "About..."
26 // =============================================================================
27 // Globals
28
29 // function tables
30 _QERFuncTable_1 g_FuncTable;
31 _QERQglTable g_QglTable;
32 _QERFileSystemTable g_FileSystemTable;
33 _QEREntityTable g_EntityTable;
34 _QERAppDataTable g_DataTable;
35
36 // the gtk widget
37 void *g_pMainWidget;
38
39 // =============================================================================
40 // plugin implementation
41
42 #define PLUGIN_NAME "Sample plugin"
43
44 //backwards for some reason
45 static const char *PLUGIN_COMMANDS = CMD_ABOUT ";" CMD_SEP;
46 static const char *PLUGIN_ABOUT = "Sample plugin\n";
47
48 void DoSample (void)
49 {
50         Sys_Printf("Sample button hit");
51 }
52
53 #define NUM_TOOLBAR_BUTTONS 1
54 typedef struct toolbar_button_info_s
55 {
56         char *image;
57         char *text;
58         char *tip;
59         void (*func)();
60         IToolbarButton::EType type;
61 } toolbar_button_info_t;
62
63 static const toolbar_button_info_t toolbar_buttons[NUM_TOOLBAR_BUTTONS] = 
64 {
65         {
66                 "sample.bmp",
67                 "Sample",
68                 "Sample image",
69                 DoSample,
70                 IToolbarButton::eToggleButton
71         },
72 };
73
74 class SampleButton : public IToolbarButton
75 {
76 public:
77         const toolbar_button_info_s *bi;
78         virtual const char* getImage() const
79         {
80                 return bi->image;
81         }
82         virtual const char* getText() const
83         {
84                 return bi->text;
85         }
86         virtual const char* getTooltip() const
87         {
88                 return bi->tip;
89         }
90         virtual void activate() const
91         {
92                 bi->func();
93                 return ;
94         }
95         virtual EType getType() const
96         {
97                 return bi->type;
98         }
99 };
100
101 SampleButton g_samplebuttons[NUM_TOOLBAR_BUTTONS];
102
103 unsigned int ToolbarButtonCount (void)
104 {
105         return NUM_TOOLBAR_BUTTONS;
106 }
107
108 const IToolbarButton* GetToolbarButton (unsigned int index)
109 {
110         g_samplebuttons[index].bi = &toolbar_buttons[index];
111         return &g_samplebuttons[index];
112 }
113
114 extern "C" const char* QERPlug_Init (void *hApp, void* pMainWidget)
115 {
116         g_pMainWidget = pMainWidget;
117
118         return PLUGIN_NAME;
119 }
120
121 extern "C" const char* QERPlug_GetName (void)
122 {
123         return (char *) PLUGIN_NAME;
124 }
125
126 extern "C" const char* QERPlug_GetCommandList (void)
127 {
128         return (char *) PLUGIN_COMMANDS;
129 }
130
131 extern "C" void QERPlug_Dispatch (const char *p, vec3_t vMin, vec3_t vMax, bool bSingleBrush)
132 {
133         if (!strcmp(p, CMD_ABOUT)) {
134                 g_FuncTable.m_pfnMessageBox(NULL, PLUGIN_ABOUT, "About", MB_OK, NULL);
135         } else {
136                 Sys_Printf("Message: %s\n", p);
137         }
138 }
139
140 // =============================================================================
141 // SYNAPSE
142
143 CSynapseServer* g_pSynapseServer = NULL;
144 CSynapseClientSample g_SynapseClient;
145
146 #if __GNUC__ >= 4
147 #pragma GCC visibility push(default)
148 #endif
149 extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces (const char *version, CSynapseServer *pServer) 
150 {
151 #if __GNUC__ >= 4
152 #pragma GCC visibility pop
153 #endif
154         if (strcmp(version, SYNAPSE_VERSION)) {
155                 Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
156                 return NULL;
157         }
158         g_pSynapseServer = pServer;
159         g_pSynapseServer->IncRef();
160         Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
161
162         g_SynapseClient.AddAPI(TOOLBAR_MAJOR, SAMPLE_MINOR, sizeof(_QERPlugToolbarTable));
163         g_SynapseClient.AddAPI(PLUGIN_MAJOR, SAMPLE_MINOR, sizeof(_QERPluginTable));
164         
165         g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(g_FuncTable), SYN_REQUIRE, &g_FuncTable);
166         g_SynapseClient.AddAPI(QGL_MAJOR, NULL, sizeof(g_QglTable), SYN_REQUIRE, &g_QglTable);
167         g_SynapseClient.AddAPI(VFS_MAJOR, "*", sizeof(g_FileSystemTable), SYN_REQUIRE, &g_FileSystemTable);
168         // get worldspawn
169         g_SynapseClient.AddAPI(ENTITY_MAJOR, NULL, sizeof(g_EntityTable), SYN_REQUIRE, &g_EntityTable);
170         // selected brushes
171         g_SynapseClient.AddAPI(DATA_MAJOR, NULL, sizeof(g_DataTable), SYN_REQUIRE, &g_DataTable);
172
173         return &g_SynapseClient;
174 }
175
176 bool CSynapseClientSample::RequestAPI (APIDescriptor_t *pAPI)
177 {
178         if (!strcmp(pAPI->major_name, PLUGIN_MAJOR)) {
179                 _QERPluginTable* pTable= static_cast<_QERPluginTable*>(pAPI->mpTable);
180         
181                 pTable->m_pfnQERPlug_Init = QERPlug_Init;
182                 pTable->m_pfnQERPlug_GetName = QERPlug_GetName;
183                 pTable->m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
184                 pTable->m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
185                 return true;
186         } else if (!strcmp(pAPI->major_name, TOOLBAR_MAJOR)) {
187                 _QERPlugToolbarTable* pTable= static_cast<_QERPlugToolbarTable*>(pAPI->mpTable);
188         
189                 pTable->m_pfnToolbarButtonCount = &ToolbarButtonCount;
190                 pTable->m_pfnGetToolbarButton = &GetToolbarButton;
191                 return true;
192         }
193         
194         Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
195         return false;
196 }
197
198 #include "version.h"
199
200 const char* CSynapseClientSample::GetInfo()
201 {
202         return PLUGIN_NAME " plugin built " __DATE__ " " RADIANT_VERSION;
203 }
204
205 const char* CSynapseClientSample::GetName()
206 {
207         return PLUGIN_NAME;
208 }
209