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