a1eab6c36a392a395b814816bc45c7eaa002c087
[xonotic/netradiant.git] / radiant / build.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
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 "build.h"
23 #include "debugging/debugging.h"
24
25 #include <gtk/gtk.h>
26 #include <map>
27 #include <list>
28 #include "stream/stringstream.h"
29 #include "versionlib.h"
30
31 #include "mainframe.h"
32
33 typedef std::map<CopiedString, CopiedString> Variables;
34 Variables g_build_variables;
35
36 void build_clear_variables(){
37         g_build_variables.clear();
38 }
39
40 void build_set_variable( const char* name, const char* value ){
41         g_build_variables[name] = value;
42 }
43
44 const char* build_get_variable( const char* name ){
45         Variables::iterator i = g_build_variables.find( name );
46         if ( i != g_build_variables.end() ) {
47                 return ( *i ).second.c_str();
48         }
49         globalErrorStream() << "undefined build variable: " << makeQuoted( name ) << "\n";
50         return "";
51 }
52
53 #include "xml/ixml.h"
54 #include "xml/xmlelement.h"
55
56 class Evaluatable
57 {
58 public:
59 virtual ~Evaluatable() = default;
60 virtual void evaluate( StringBuffer& output ) = 0;
61 virtual void exportXML( XMLImporter& importer ) = 0;
62 };
63
64 class VariableString : public Evaluatable
65 {
66 CopiedString m_string;
67 public:
68 VariableString() : m_string(){
69 }
70 VariableString( const char* string ) : m_string( string ){
71 }
72 const char* c_str() const {
73         return m_string.c_str();
74 }
75 void setString( const char* string ){
76         m_string = string;
77 }
78 void evaluate( StringBuffer& output ){
79         StringBuffer variable;
80         bool in_variable = false;
81         for ( const char* i = m_string.c_str(); *i != '\0'; ++i )
82         {
83                 if ( !in_variable ) {
84                         switch ( *i )
85                         {
86                         case '[':
87                                 in_variable = true;
88                                 break;
89                         default:
90                                 output.push_back( *i );
91                                 break;
92                         }
93                 }
94                 else
95                 {
96                         switch ( *i )
97                         {
98                         case ']':
99                                 in_variable = false;
100                                 output.push_string( build_get_variable( variable.c_str() ) );
101                                 variable.clear();
102                                 break;
103                         default:
104                                 variable.push_back( *i );
105                                 break;
106                         }
107                 }
108         }
109 }
110 void exportXML( XMLImporter& importer ){
111         importer << c_str();
112 }
113 };
114
115 class Conditional : public Evaluatable
116 {
117 VariableString* m_test;
118 public:
119 Evaluatable* m_result;
120 Conditional( VariableString* test ) : m_test( test ){
121 }
122 ~Conditional(){
123         delete m_test;
124         delete m_result;
125 }
126 void evaluate( StringBuffer& output ){
127         StringBuffer buffer;
128         m_test->evaluate( buffer );
129         if ( !string_empty( buffer.c_str() ) ) {
130                 m_result->evaluate( output );
131         }
132 }
133 void exportXML( XMLImporter& importer ){
134         StaticElement conditionElement( "cond" );
135         conditionElement.insertAttribute( "value", m_test->c_str() );
136         importer.pushElement( conditionElement );
137         m_result->exportXML( importer );
138         importer.popElement( conditionElement.name() );
139 }
140 };
141
142 typedef std::vector<Evaluatable*> Evaluatables;
143
144 class Tool : public Evaluatable
145 {
146 Evaluatables m_evaluatables;
147 public:
148 ~Tool(){
149         for ( Evaluatables::iterator i = m_evaluatables.begin(); i != m_evaluatables.end(); ++i )
150         {
151                 delete ( *i );
152         }
153 }
154 void push_back( Evaluatable* evaluatable ){
155         m_evaluatables.push_back( evaluatable );
156 }
157 void evaluate( StringBuffer& output ){
158         for ( Evaluatables::iterator i = m_evaluatables.begin(); i != m_evaluatables.end(); ++i )
159         {
160                 ( *i )->evaluate( output );
161         }
162 }
163 void exportXML( XMLImporter& importer ){
164         for ( Evaluatables::iterator i = m_evaluatables.begin(); i != m_evaluatables.end(); ++i )
165         {
166                 ( *i )->exportXML( importer );
167         }
168 }
169 };
170
171 #include "xml/ixml.h"
172
173 class XMLElementParser : public TextOutputStream
174 {
175 public:
176 virtual ~XMLElementParser() = default;
177 virtual XMLElementParser& pushElement( const XMLElement& element ) = 0;
178 virtual void popElement( const char* name ) = 0;
179 };
180
181 class VariableStringXMLConstructor : public XMLElementParser
182 {
183 StringBuffer m_buffer;
184 VariableString& m_variableString;
185 public:
186 VariableStringXMLConstructor( VariableString& variableString ) : m_variableString( variableString ){
187 }
188 ~VariableStringXMLConstructor(){
189         m_variableString.setString( m_buffer.c_str() );
190 }
191 std::size_t write( const char* buffer, std::size_t length ){
192         m_buffer.push_range( buffer, buffer + length );
193         return length;
194 }
195 XMLElementParser& pushElement( const XMLElement& element ){
196         ERROR_MESSAGE( "parse error: invalid element \"" << element.name() << "\"" );
197         return *this;
198 }
199 void popElement( const char* name ){
200 }
201 };
202
203 class ConditionalXMLConstructor : public XMLElementParser
204 {
205 StringBuffer m_buffer;
206 Conditional& m_conditional;
207 public:
208 ConditionalXMLConstructor( Conditional& conditional ) : m_conditional( conditional ){
209 }
210 ~ConditionalXMLConstructor(){
211         m_conditional.m_result = new VariableString( m_buffer.c_str() );
212 }
213 std::size_t write( const char* buffer, std::size_t length ){
214         m_buffer.push_range( buffer, buffer + length );
215         return length;
216 }
217 XMLElementParser& pushElement( const XMLElement& element ){
218         ERROR_MESSAGE( "parse error: invalid element \"" << element.name() << "\"" );
219         return *this;
220 }
221 void popElement( const char* name ){
222 }
223 };
224
225 class ToolXMLConstructor : public XMLElementParser
226 {
227 StringBuffer m_buffer;
228 Tool& m_tool;
229 ConditionalXMLConstructor* m_conditional;
230 public:
231 ToolXMLConstructor( Tool& tool ) : m_tool( tool ){
232 }
233 ~ToolXMLConstructor(){
234         flush();
235 }
236 std::size_t write( const char* buffer, std::size_t length ){
237         m_buffer.push_range( buffer, buffer + length );
238         return length;
239 }
240 XMLElementParser& pushElement( const XMLElement& element ){
241         if ( string_equal( element.name(), "cond" ) ) {
242                 flush();
243                 Conditional* conditional = new Conditional( new VariableString( element.attribute( "value" ) ) );
244                 m_tool.push_back( conditional );
245                 m_conditional = new ConditionalXMLConstructor( *conditional );
246                 return *m_conditional;
247         }
248         else
249         {
250                 ERROR_MESSAGE( "parse error: invalid element \"" << element.name() << "\"" );
251                 return *this;
252         }
253 }
254 void popElement( const char* name ){
255         if ( string_equal( name, "cond" ) ) {
256                 delete m_conditional;
257         }
258 }
259
260 void flush(){
261         if ( !m_buffer.empty() ) {
262                 m_tool.push_back( new VariableString( m_buffer.c_str() ) );
263                 m_buffer.clear();
264         }
265 }
266 };
267
268 typedef VariableString BuildCommand;
269 typedef std::list<BuildCommand> Build;
270
271 class BuildXMLConstructor : public XMLElementParser
272 {
273 VariableStringXMLConstructor* m_variableString;
274 Build& m_build;
275 public:
276 BuildXMLConstructor( Build& build ) : m_build( build ){
277 }
278 std::size_t write( const char* buffer, std::size_t length ){
279         return length;
280 }
281 XMLElementParser& pushElement( const XMLElement& element ){
282         if ( string_equal( element.name(), "command" ) ) {
283                 m_build.push_back( BuildCommand() );
284                 m_variableString = new VariableStringXMLConstructor( m_build.back() );
285                 return *m_variableString;
286         }
287         else
288         {
289                 ERROR_MESSAGE( "parse error: invalid element" );
290                 return *this;
291         }
292 }
293 void popElement( const char* name ){
294         delete m_variableString;
295 }
296 };
297
298 typedef std::pair<CopiedString, Build> BuildPair;
299 const char *SEPARATOR_STRING = "-";
300 static bool is_separator( const BuildPair &p ){
301         if ( !string_equal( p.first.c_str(), SEPARATOR_STRING ) ) {
302                 return false;
303         }
304         for ( Build::const_iterator j = p.second.begin(); j != p.second.end(); ++j )
305         {
306                 if ( !string_equal( ( *j ).c_str(), "" ) ) {
307                         return false;
308                 }
309         }
310         return true;
311 }
312
313
314 typedef std::list<BuildPair> Project;
315
316 Project::iterator Project_find( Project& project, const char* name ){
317         return std::find_if(project.begin(), project.end(), [&](const BuildPair &self) {
318                 return string_equal(self.first.c_str(), name);
319         });
320 }
321
322 Project::iterator Project_find( Project& project, std::size_t index ){
323         Project::iterator i = project.begin();
324         while ( index-- != 0 && i != project.end() )
325         {
326                 ++i;
327         }
328         return i;
329 }
330
331 Build& project_find( Project& project, const char* build ){
332         Project::iterator i = Project_find( project, build );
333         ASSERT_MESSAGE( i != project.end(), "error finding build command" );
334         return ( *i ).second;
335 }
336
337 Build::iterator Build_find( Build& build, std::size_t index ){
338         Build::iterator i = build.begin();
339         while ( index-- != 0 && i != build.end() )
340         {
341                 ++i;
342         }
343         return i;
344 }
345
346 typedef std::map<CopiedString, Tool> Tools;
347
348 class ProjectXMLConstructor : public XMLElementParser
349 {
350 ToolXMLConstructor* m_tool;
351 BuildXMLConstructor* m_build;
352 Project& m_project;
353 Tools& m_tools;
354 public:
355 ProjectXMLConstructor( Project& project, Tools& tools ) : m_project( project ), m_tools( tools ){
356 }
357 std::size_t write( const char* buffer, std::size_t length ){
358         return length;
359 }
360 XMLElementParser& pushElement( const XMLElement& element ){
361         if ( string_equal( element.name(), "var" ) ) {
362                 Tools::iterator i = m_tools.insert( Tools::value_type( element.attribute( "name" ), Tool() ) ).first;
363                 m_tool = new ToolXMLConstructor( ( *i ).second );
364                 return *m_tool;
365         }
366         else if ( string_equal( element.name(), "build" ) ) {
367                 m_project.push_back( Project::value_type( element.attribute( "name" ), Build() ) );
368                 m_build = new BuildXMLConstructor( m_project.back().second );
369                 return *m_build;
370         }
371         else if ( string_equal( element.name(), "separator" ) ) {
372                 m_project.push_back( Project::value_type( SEPARATOR_STRING, Build() ) );
373                 return *this;
374         }
375         else
376         {
377                 ERROR_MESSAGE( "parse error: invalid element" );
378                 return *this;
379         }
380 }
381 void popElement( const char* name ){
382         if ( string_equal( name, "var" ) ) {
383                 delete m_tool;
384         }
385         else if ( string_equal( name, "build" ) ) {
386                 delete m_build;
387         }
388 }
389 };
390
391 class SkipAllParser : public XMLElementParser
392 {
393 public:
394 std::size_t write( const char* buffer, std::size_t length ){
395         return length;
396 }
397 XMLElementParser& pushElement( const XMLElement& element ){
398         return *this;
399 }
400 void popElement( const char* name ){
401 }
402 };
403
404 class RootXMLConstructor : public XMLElementParser
405 {
406 CopiedString m_elementName;
407 XMLElementParser& m_parser;
408 SkipAllParser m_skip;
409 Version m_version;
410 bool m_compatible;
411 public:
412 RootXMLConstructor( const char* elementName, XMLElementParser& parser, const char* version ) :
413         m_elementName( elementName ),
414         m_parser( parser ),
415         m_version( version_parse( version ) ),
416         m_compatible( false ){
417 }
418 std::size_t write( const char* buffer, std::size_t length ){
419         return length;
420 }
421 XMLElementParser& pushElement( const XMLElement& element ){
422         if ( string_equal( element.name(), m_elementName.c_str() ) ) {
423                 Version dataVersion( version_parse( element.attribute( "version" ) ) );
424                 if ( version_compatible( m_version, dataVersion ) ) {
425                         m_compatible = true;
426                         return m_parser;
427                 }
428                 else
429                 {
430                         return m_skip;
431                 }
432         }
433         else
434         {
435                 //ERROR_MESSAGE("parse error: invalid element \"" << element.name() << "\"");
436                 return *this;
437         }
438 }
439 void popElement( const char* name ){
440 }
441
442 bool versionCompatible() const {
443         return m_compatible;
444 }
445 };
446
447 namespace
448 {
449 Project g_build_project;
450 Tools g_build_tools;
451 bool g_build_changed = false;
452 }
453
454 void build_error_undefined_tool( const char* build, const char* tool ){
455         globalErrorStream() << "build " << makeQuoted( build ) << " refers to undefined tool " << makeQuoted( tool ) << '\n';
456 }
457
458 void project_verify( Project& project, Tools& tools ){
459 #if 0
460         for ( Project::iterator i = project.begin(); i != project.end(); ++i )
461         {
462                 Build& build = ( *i ).second;
463                 for ( Build::iterator j = build.begin(); j != build.end(); ++j )
464                 {
465                         Tools::iterator k = tools.find( ( *j ).first );
466                         if ( k == g_build_tools.end() ) {
467                                 build_error_undefined_tool( ( *i ).first.c_str(), ( *j ).first.c_str() );
468                         }
469                 }
470         }
471 #endif
472 }
473
474 void build_run( const char* name, CommandListener& listener ){
475         for ( Tools::iterator i = g_build_tools.begin(); i != g_build_tools.end(); ++i )
476         {
477                 StringBuffer output;
478                 ( *i ).second.evaluate( output );
479                 build_set_variable( ( *i ).first.c_str(), output.c_str() );
480         }
481
482         {
483                 Project::iterator i = Project_find( g_build_project, name );
484                 if ( i != g_build_project.end() ) {
485                         Build& build = ( *i ).second;
486                         for ( Build::iterator j = build.begin(); j != build.end(); ++j )
487                         {
488                                 StringBuffer output;
489                                 ( *j ).evaluate( output );
490                                 listener.execute( output.c_str() );
491                         }
492                 }
493                 else
494                 {
495                         globalErrorStream() << "build " << makeQuoted( name ) << " not defined";
496                 }
497         }
498 }
499
500
501 typedef std::vector<XMLElementParser*> XMLElementStack;
502
503 class XMLParser : public XMLImporter
504 {
505 XMLElementStack m_stack;
506 public:
507 XMLParser( XMLElementParser& parser ){
508         m_stack.push_back( &parser );
509 }
510 std::size_t write( const char* buffer, std::size_t length ){
511         return m_stack.back()->write( buffer, length );
512 }
513 void pushElement( const XMLElement& element ){
514         m_stack.push_back( &m_stack.back()->pushElement( element ) );
515 }
516 void popElement( const char* name ){
517         m_stack.pop_back();
518         m_stack.back()->popElement( name );
519 }
520 };
521
522 #include "stream/textfilestream.h"
523 #include "xml/xmlparser.h"
524
525 const char* const BUILDMENU_VERSION = "2.0";
526
527 bool build_commands_parse( const char* filename ){
528         TextFileInputStream projectFile( filename );
529         if ( !projectFile.failed() ) {
530                 ProjectXMLConstructor projectConstructor( g_build_project, g_build_tools );
531                 RootXMLConstructor rootConstructor( "project", projectConstructor, BUILDMENU_VERSION );
532                 XMLParser importer( rootConstructor );
533                 XMLStreamParser parser( projectFile );
534                 parser.exportXML( importer );
535
536                 if ( rootConstructor.versionCompatible() ) {
537                         project_verify( g_build_project, g_build_tools );
538
539                         return true;
540                 }
541                 globalErrorStream() << "failed to parse build menu: " << makeQuoted( filename ) << "\n";
542         }
543         return false;
544 }
545
546 void build_commands_clear(){
547         g_build_project.clear();
548         g_build_tools.clear();
549 }
550
551 class BuildXMLExporter
552 {
553 Build& m_build;
554 public:
555 BuildXMLExporter( Build& build ) : m_build( build ){
556 }
557 void exportXML( XMLImporter& importer ){
558         importer << "\n";
559         for ( Build::iterator i = m_build.begin(); i != m_build.end(); ++i )
560         {
561                 StaticElement commandElement( "command" );
562                 importer.pushElement( commandElement );
563                 ( *i ).exportXML( importer );
564                 importer.popElement( commandElement.name() );
565                 importer << "\n";
566         }
567 }
568 };
569
570 class ProjectXMLExporter
571 {
572 Project& m_project;
573 Tools& m_tools;
574 public:
575 ProjectXMLExporter( Project& project, Tools& tools ) : m_project( project ), m_tools( tools ){
576 }
577 void exportXML( XMLImporter& importer ){
578         StaticElement projectElement( "project" );
579         projectElement.insertAttribute( "version", BUILDMENU_VERSION );
580         importer.pushElement( projectElement );
581         importer << "\n";
582
583         for ( Tools::iterator i = m_tools.begin(); i != m_tools.end(); ++i )
584         {
585                 StaticElement toolElement( "var" );
586                 toolElement.insertAttribute( "name", ( *i ).first.c_str() );
587                 importer.pushElement( toolElement );
588                 ( *i ).second.exportXML( importer );
589                 importer.popElement( toolElement.name() );
590                 importer << "\n";
591         }
592         for ( Project::iterator i = m_project.begin(); i != m_project.end(); ++i )
593         {
594                 if ( is_separator( *i ) ) {
595                         StaticElement buildElement( "separator" );
596                         importer.pushElement( buildElement );
597                         importer.popElement( buildElement.name() );
598                         importer << "\n";
599                 }
600                 else
601                 {
602                         StaticElement buildElement( "build" );
603                         buildElement.insertAttribute( "name", ( *i ).first.c_str() );
604                         importer.pushElement( buildElement );
605                         BuildXMLExporter buildExporter( ( *i ).second );
606                         buildExporter.exportXML( importer );
607                         importer.popElement( buildElement.name() );
608                         importer << "\n";
609                 }
610         }
611         importer.popElement( projectElement.name() );
612 }
613 };
614
615 #include "xml/xmlwriter.h"
616
617 void build_commands_write( const char* filename ){
618         TextFileOutputStream projectFile( filename );
619         if ( !projectFile.failed() ) {
620                 XMLStreamWriter writer( projectFile );
621                 ProjectXMLExporter projectExporter( g_build_project, g_build_tools );
622                 writer << "\n";
623                 projectExporter.exportXML( writer );
624                 writer << "\n";
625         }
626 }
627
628
629 #include <gdk/gdkkeysyms.h>
630
631 #include "gtkutil/dialog.h"
632 #include "gtkutil/closure.h"
633 #include "gtkutil/window.h"
634 #include "gtkdlgs.h"
635
636 void Build_refreshMenu( ui::Menu menu );
637
638
639 void BSPCommandList_Construct( ui::ListStore store, Project& project ){
640         store.clear();
641
642         for ( Project::iterator i = project.begin(); i != project.end(); ++i )
643         {
644                 store.append(0, (*i).first.c_str());
645         }
646
647         store.append();
648 }
649
650 class ProjectList
651 {
652 public:
653 Project& m_project;
654 ui::ListStore m_store{ui::null};
655 bool m_changed;
656 ProjectList( Project& project ) : m_project( project ), m_changed( false ){
657 }
658 };
659
660 gboolean project_cell_edited( GtkCellRendererText* cell, gchar* path_string, gchar* new_text, ProjectList* projectList ){
661         Project& project = projectList->m_project;
662
663         auto path = ui::TreePath( path_string );
664
665         ASSERT_MESSAGE( gtk_tree_path_get_depth( path ) == 1, "invalid path length" );
666
667         GtkTreeIter iter;
668         gtk_tree_model_get_iter(projectList->m_store, &iter, path );
669
670         Project::iterator i = Project_find( project, gtk_tree_path_get_indices( path )[0] );
671         if ( i != project.end() ) {
672                 projectList->m_changed = true;
673                 if ( string_empty( new_text ) ) {
674                         project.erase( i );
675                         gtk_list_store_remove( projectList->m_store, &iter );
676                 }
677                 else
678                 {
679                         ( *i ).first = new_text;
680                         gtk_list_store_set( projectList->m_store, &iter, 0, new_text, -1 );
681                 }
682         }
683         else if ( !string_empty( new_text ) ) {
684                 projectList->m_changed = true;
685                 project.push_back( Project::value_type( new_text, Build() ) );
686
687                 gtk_list_store_set( projectList->m_store, &iter, 0, new_text, -1 );
688                 projectList->m_store.append();
689         }
690
691         gtk_tree_path_free( path );
692
693         Build_refreshMenu( g_bsp_menu );
694
695         return FALSE;
696 }
697
698 gboolean project_key_press( ui::TreeView widget, GdkEventKey* event, ProjectList* projectList ){
699         Project& project = projectList->m_project;
700
701         if ( event->keyval == GDK_KEY_Delete ) {
702                 auto selection = ui::TreeSelection(gtk_tree_view_get_selection(widget));
703                 GtkTreeIter iter;
704                 GtkTreeModel* model;
705                 if ( gtk_tree_selection_get_selected( selection, &model, &iter ) ) {
706                         auto path = gtk_tree_model_get_path( model, &iter );
707                         Project::iterator x = Project_find( project, gtk_tree_path_get_indices( path )[0] );
708                         gtk_tree_path_free( path );
709
710                         if ( x != project.end() ) {
711                                 projectList->m_changed = true;
712                                 project.erase( x );
713                                 Build_refreshMenu( g_bsp_menu );
714
715                                 gtk_list_store_remove( projectList->m_store, &iter );
716                         }
717                 }
718         }
719         return FALSE;
720 }
721
722
723 Build* g_current_build = 0;
724
725 gboolean project_selection_changed( ui::TreeSelection selection, ui::ListStore store ){
726         Project& project = g_build_project;
727
728         store.clear();
729
730         GtkTreeIter iter;
731         GtkTreeModel* model;
732         if ( gtk_tree_selection_get_selected( selection, &model, &iter ) ) {
733                 auto path = gtk_tree_model_get_path( model, &iter );
734                 Project::iterator x = Project_find( project, gtk_tree_path_get_indices( path )[0] );
735                 gtk_tree_path_free( path );
736
737                 if ( x != project.end() ) {
738                         Build& build = ( *x ).second;
739                         g_current_build = &build;
740
741                         for ( Build::iterator i = build.begin(); i != build.end(); ++i )
742                         {
743                                 store.append(0, (*i).c_str());
744                         }
745                         store.append();
746                 }
747                 else
748                 {
749                         g_current_build = 0;
750                 }
751         }
752         else
753         {
754                 g_current_build = 0;
755         }
756
757         return FALSE;
758 }
759
760 gboolean commands_cell_edited( GtkCellRendererText* cell, gchar* path_string, gchar* new_text, ui::ListStore store ){
761         if ( g_current_build == 0 ) {
762                 return FALSE;
763         }
764         Build& build = *g_current_build;
765
766         auto path = ui::TreePath( path_string );
767
768         ASSERT_MESSAGE( gtk_tree_path_get_depth( path ) == 1, "invalid path length" );
769
770         GtkTreeIter iter;
771         gtk_tree_model_get_iter(store, &iter, path );
772
773         Build::iterator i = Build_find( build, gtk_tree_path_get_indices( path )[0] );
774         if ( i != build.end() ) {
775                 g_build_changed = true;
776                 ( *i ).setString( new_text );
777
778                 gtk_list_store_set( store, &iter, 0, new_text, -1 );
779         }
780         else if ( !string_empty( new_text ) ) {
781                 g_build_changed = true;
782                 build.push_back( Build::value_type( VariableString( new_text ) ) );
783
784                 gtk_list_store_set( store, &iter, 0, new_text, -1 );
785
786                 store.append();
787         }
788
789         gtk_tree_path_free( path );
790
791         Build_refreshMenu( g_bsp_menu );
792
793         return FALSE;
794 }
795
796 gboolean commands_key_press( ui::TreeView widget, GdkEventKey* event, ui::ListStore store ){
797         if ( g_current_build == 0 ) {
798                 return FALSE;
799         }
800         Build& build = *g_current_build;
801
802         if ( event->keyval == GDK_KEY_Delete ) {
803                 auto selection = gtk_tree_view_get_selection(widget );
804                 GtkTreeIter iter;
805                 GtkTreeModel* model;
806                 if ( gtk_tree_selection_get_selected( selection, &model, &iter ) ) {
807                         auto path = gtk_tree_model_get_path( model, &iter );
808                         Build::iterator i = Build_find( build, gtk_tree_path_get_indices( path )[0] );
809                         gtk_tree_path_free( path );
810
811                         if ( i != build.end() ) {
812                                 g_build_changed = true;
813                                 build.erase( i );
814
815                                 gtk_list_store_remove( store, &iter );
816                         }
817                 }
818         }
819         return FALSE;
820 }
821
822
823 ui::Window BuildMenuDialog_construct( ModalDialog& modal, ProjectList& projectList ){
824         ui::Window window = MainFrame_getWindow().create_dialog_window("Build Menu", G_CALLBACK(dialog_delete_callback ), &modal, -1, 400 );
825
826         {
827                 auto table1 = create_dialog_table( 2, 2, 4, 4, 4 );
828                 window.add(table1);
829                 {
830                         auto vbox = create_dialog_vbox( 4 );
831             table1.attach(vbox, {1, 2, 0, 1}, {GTK_FILL, GTK_FILL});
832                         {
833                                 auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &modal );
834                                 vbox.pack_start( button, FALSE, FALSE, 0 );
835                         }
836                         {
837                                 auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &modal );
838                                 vbox.pack_start( button, FALSE, FALSE, 0 );
839                         }
840                 }
841                 auto buildViewStore = ui::ListStore(gtk_list_store_new( 1, G_TYPE_STRING ));
842                 auto buildView = ui::TreeView( ui::TreeModel( buildViewStore ));
843                 {
844                         auto frame = create_dialog_frame( "Build menu" );
845             table1.attach(frame, {0, 1, 0, 1});
846                         {
847                                 auto scr = create_scrolled_window( ui::Policy::NEVER, ui::Policy::AUTOMATIC, 4 );
848                                 frame.add(scr);
849
850                                 {
851                                         auto view = buildView;
852                                         auto store = buildViewStore;
853                                         gtk_tree_view_set_headers_visible(view, FALSE );
854
855                                         auto renderer = ui::CellRendererText(ui::New);
856                                         object_set_boolean_property( G_OBJECT( renderer ), "editable", TRUE );
857                                         renderer.connect("edited", G_CALLBACK( project_cell_edited ), &projectList );
858
859                                         auto column = ui::TreeViewColumn( "", renderer, {{"text", 0}} );
860                                         gtk_tree_view_append_column(view, column );
861
862                                         auto selection = gtk_tree_view_get_selection(view );
863                                         gtk_tree_selection_set_mode( selection, GTK_SELECTION_BROWSE );
864
865                                         view.show();
866
867                                         projectList.m_store = store;
868                                         scr.add(view);
869
870                                         view.connect( "key_press_event", G_CALLBACK( project_key_press ), &projectList );
871
872                                         store.unref();
873                                 }
874                         }
875                 }
876                 {
877                         auto frame = create_dialog_frame( "Commandline" );
878             table1.attach(frame, {0, 1, 1, 2});
879                         {
880                                 auto scr = create_scrolled_window( ui::Policy::NEVER, ui::Policy::AUTOMATIC, 4 );
881                                 frame.add(scr);
882
883                                 {
884                                         auto store = ui::ListStore(gtk_list_store_new( 1, G_TYPE_STRING ));
885
886                                         auto view = ui::TreeView(ui::TreeModel( store ));
887                                         gtk_tree_view_set_headers_visible(view, FALSE );
888
889                                         auto renderer = ui::CellRendererText(ui::New);
890                                         object_set_boolean_property( G_OBJECT( renderer ), "editable", TRUE );
891                                         renderer.connect( "edited", G_CALLBACK( commands_cell_edited ), store );
892
893                                         auto column = ui::TreeViewColumn( "", renderer, {{"text", 0}} );
894                                         gtk_tree_view_append_column(view, column );
895
896                                         auto selection = gtk_tree_view_get_selection(view );
897                                         gtk_tree_selection_set_mode( selection, GTK_SELECTION_BROWSE );
898
899                                         view.show();
900
901                                         scr.add(view);
902
903                                         store.unref();
904
905                                         view.connect( "key_press_event", G_CALLBACK( commands_key_press ), store );
906
907                                         auto sel = ui::TreeSelection(gtk_tree_view_get_selection(buildView ));
908                                         sel.connect( "changed", G_CALLBACK( project_selection_changed ), store );
909                                 }
910                         }
911                 }
912         }
913
914         BSPCommandList_Construct( projectList.m_store, g_build_project );
915
916         return window;
917 }
918
919 namespace
920 {
921 CopiedString g_buildMenu;
922 }
923
924 void LoadBuildMenu();
925
926 void DoBuildMenu(){
927         ModalDialog modal;
928
929         ProjectList projectList( g_build_project );
930
931         ui::Window window = BuildMenuDialog_construct( modal, projectList );
932
933         if ( modal_dialog_show( window, modal ) == eIDCANCEL ) {
934                 build_commands_clear();
935                 LoadBuildMenu();
936
937                 Build_refreshMenu( g_bsp_menu );
938         }
939         else if ( projectList.m_changed ) {
940                 g_build_changed = true;
941         }
942
943         window.destroy();
944 }
945
946
947
948 #include "gtkutil/menu.h"
949 #include "mainframe.h"
950 #include "preferences.h"
951 #include "qe3.h"
952
953 class BuildMenuItem
954 {
955 const char* m_name;
956 public:
957 ui::MenuItem m_item;
958 BuildMenuItem( const char* name, ui::MenuItem item )
959         : m_name( name ), m_item( item ){
960 }
961 void run(){
962         RunBSP( m_name );
963 }
964 typedef MemberCaller<BuildMenuItem, void(), &BuildMenuItem::run> RunCaller;
965 };
966
967 typedef std::list<BuildMenuItem> BuildMenuItems;
968 BuildMenuItems g_BuildMenuItems;
969
970
971 ui::Menu g_bsp_menu{ui::null};
972
973 void Build_constructMenu( ui::Menu menu ){
974         for ( Project::iterator i = g_build_project.begin(); i != g_build_project.end(); ++i )
975         {
976                 g_BuildMenuItems.push_back( BuildMenuItem( ( *i ).first.c_str(), ui::MenuItem(ui::null) ) );
977                 if ( is_separator( *i ) ) {
978                         g_BuildMenuItems.back().m_item = menu_separator( menu );
979                 }
980                 else
981                 {
982                         g_BuildMenuItems.back().m_item = create_menu_item_with_mnemonic( menu, ( *i ).first.c_str(), BuildMenuItem::RunCaller( g_BuildMenuItems.back() ) );
983                 }
984         }
985 }
986
987
988 void Build_refreshMenu( ui::Menu menu ){
989         for (auto i = g_BuildMenuItems.begin(); i != g_BuildMenuItems.end(); ++i )
990         {
991                 menu.remove(ui::MenuItem(i->m_item));
992         }
993
994         g_BuildMenuItems.clear();
995
996         Build_constructMenu( menu );
997 }
998
999
1000 void LoadBuildMenu(){
1001         if ( string_empty( g_buildMenu.c_str() ) || !build_commands_parse( g_buildMenu.c_str() ) ) {
1002                 {
1003                         StringOutputStream buffer( 256 );
1004                         buffer << GameToolsPath_get() << "default_build_menu.xml";
1005
1006                         bool success = build_commands_parse( buffer.c_str() );
1007                         ASSERT_MESSAGE( success, "failed to parse default build commands: " << buffer.c_str() );
1008                 }
1009                 {
1010                         StringOutputStream buffer( 256 );
1011                         buffer << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << "/build_menu.xml";
1012
1013                         g_buildMenu = buffer.c_str();
1014                 }
1015         }
1016 }
1017
1018 void SaveBuildMenu(){
1019         if ( g_build_changed ) {
1020                 g_build_changed = false;
1021                 build_commands_write( g_buildMenu.c_str() );
1022         }
1023 }
1024
1025 #include "preferencesystem.h"
1026 #include "stringio.h"
1027
1028 void BuildMenu_Construct(){
1029         GlobalPreferenceSystem().registerPreference( "BuildMenu", CopiedStringImportStringCaller( g_buildMenu ), CopiedStringExportStringCaller( g_buildMenu ) );
1030         LoadBuildMenu();
1031 }
1032 void BuildMenu_Destroy(){
1033         SaveBuildMenu();
1034 }