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