]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/entityinspector.cpp
701a77baccc66738eaeaaf188679cbd6d6bbecdc
[xonotic/netradiant.git] / radiant / entityinspector.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 #include "entityinspector.h"
23
24 #include "debugging/debugging.h"
25 #include <gtk/gtk.h>
26
27 #include "ientity.h"
28 #include "ifilesystem.h"
29 #include "imodel.h"
30 #include "iscenegraph.h"
31 #include "iselection.h"
32 #include "iundo.h"
33
34 #include <map>
35 #include <set>
36 #include <gdk/gdkkeysyms.h>
37 #include <uilib/uilib.h>
38
39
40 #include "os/path.h"
41 #include "eclasslib.h"
42 #include "scenelib.h"
43 #include "generic/callback.h"
44 #include "os/file.h"
45 #include "stream/stringstream.h"
46 #include "moduleobserver.h"
47 #include "convert.h"
48 #include "stringio.h"
49
50 #include "gtkutil/accelerator.h"
51 #include "gtkutil/dialog.h"
52 #include "gtkutil/filechooser.h"
53 #include "gtkutil/messagebox.h"
54 #include "gtkutil/nonmodal.h"
55 #include "gtkutil/button.h"
56 #include "gtkutil/entry.h"
57 #include "gtkutil/container.h"
58
59 #include "qe3.h"
60 #include "gtkmisc.h"
61 #include "gtkdlgs.h"
62 #include "entity.h"
63 #include "mainframe.h"
64 #include "textureentry.h"
65 #include "groupdialog.h"
66
67 ui::Entry numeric_entry_new(){
68         auto entry = ui::Entry();
69         entry.show();
70         gtk_widget_set_size_request( GTK_WIDGET( entry ), 64, -1 );
71         return entry;
72 }
73
74 namespace
75 {
76 typedef std::map<CopiedString, CopiedString> KeyValues;
77 KeyValues g_selectedKeyValues;
78 KeyValues g_selectedDefaultKeyValues;
79 }
80
81 const char* SelectedEntity_getValueForKey( const char* key ){
82         {
83                 KeyValues::const_iterator i = g_selectedKeyValues.find( key );
84                 if ( i != g_selectedKeyValues.end() ) {
85                         return ( *i ).second.c_str();
86                 }
87         }
88         {
89                 KeyValues::const_iterator i = g_selectedDefaultKeyValues.find( key );
90                 if ( i != g_selectedDefaultKeyValues.end() ) {
91                         return ( *i ).second.c_str();
92                 }
93         }
94         return "";
95 }
96
97 void Scene_EntitySetKeyValue_Selected_Undoable( const char* key, const char* value ){
98         StringOutputStream command( 256 );
99         command << "entitySetKeyValue -key " << makeQuoted( key ) << " -value " << makeQuoted( value );
100         UndoableCommand undo( command.c_str() );
101         Scene_EntitySetKeyValue_Selected( key, value );
102 }
103
104 class EntityAttribute
105 {
106 public:
107 virtual ui::Widget getWidget() const = 0;
108 virtual void update() = 0;
109 virtual void release() = 0;
110 };
111
112 class BooleanAttribute : public EntityAttribute
113 {
114 CopiedString m_key;
115 GtkCheckButton* m_check;
116
117 static gboolean toggled( ui::Widget widget, BooleanAttribute* self ){
118         self->apply();
119         return FALSE;
120 }
121 public:
122 BooleanAttribute( const char* key ) :
123         m_key( key ),
124         m_check( 0 ){
125         auto check = ui::CheckButton(GTK_CHECK_BUTTON( gtk_check_button_new() ));
126         check.show();
127
128         m_check = check;
129
130         guint handler = check.connect( "toggled", G_CALLBACK( toggled ), this );
131         g_object_set_data( G_OBJECT( check ), "handler", gint_to_pointer( handler ) );
132
133         update();
134 }
135 ui::Widget getWidget() const {
136         return ui::Widget(GTK_WIDGET( m_check ));
137 }
138 void release(){
139         delete this;
140 }
141 void apply(){
142         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( m_check ) ) ? "1" : "0" );
143 }
144 typedef MemberCaller<BooleanAttribute, &BooleanAttribute::apply> ApplyCaller;
145
146 void update(){
147         const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
148         if ( !string_empty( value ) ) {
149                 toggle_button_set_active_no_signal( ui::ToggleButton(GTK_TOGGLE_BUTTON( m_check )), atoi( value ) != 0 );
150         }
151         else
152         {
153                 toggle_button_set_active_no_signal( ui::ToggleButton(GTK_TOGGLE_BUTTON( m_check )), false );
154         }
155 }
156 typedef MemberCaller<BooleanAttribute, &BooleanAttribute::update> UpdateCaller;
157 };
158
159
160 class StringAttribute : public EntityAttribute
161 {
162 CopiedString m_key;
163 ui::Entry m_entry;
164 NonModalEntry m_nonModal;
165 public:
166 StringAttribute( const char* key ) :
167         m_key( key ),
168         m_entry( nullptr ),
169         m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
170         auto entry = ui::Entry();
171         entry.show();
172         gtk_widget_set_size_request( GTK_WIDGET( entry ), 50, -1 );
173
174         m_entry = entry;
175         m_nonModal.connect( m_entry );
176 }
177 ui::Widget getWidget() const {
178         return ui::Widget(GTK_WIDGET( m_entry ));
179 }
180 ui::Entry getEntry() const {
181         return m_entry;
182 }
183
184 void release(){
185         delete this;
186 }
187 void apply(){
188         StringOutputStream value( 64 );
189         value << gtk_entry_get_text( m_entry );
190         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), value.c_str() );
191 }
192 typedef MemberCaller<StringAttribute, &StringAttribute::apply> ApplyCaller;
193
194 void update(){
195         StringOutputStream value( 64 );
196         value << SelectedEntity_getValueForKey( m_key.c_str() );
197         gtk_entry_set_text( m_entry, value.c_str() );
198 }
199 typedef MemberCaller<StringAttribute, &StringAttribute::update> UpdateCaller;
200 };
201
202 class ShaderAttribute : public StringAttribute
203 {
204 public:
205 ShaderAttribute( const char* key ) : StringAttribute( key ){
206         GlobalShaderEntryCompletion::instance().connect( StringAttribute::getEntry() );
207 }
208 };
209
210
211 class ModelAttribute : public EntityAttribute
212 {
213 CopiedString m_key;
214 BrowsedPathEntry m_entry;
215 NonModalEntry m_nonModal;
216 public:
217 ModelAttribute( const char* key ) :
218         m_key( key ),
219         m_entry( BrowseCaller( *this ) ),
220         m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
221         m_nonModal.connect( m_entry.m_entry.m_entry );
222 }
223 void release(){
224         delete this;
225 }
226 ui::Widget getWidget() const {
227         return ui::Widget(GTK_WIDGET( m_entry.m_entry.m_frame ));
228 }
229 void apply(){
230         StringOutputStream value( 64 );
231         value << gtk_entry_get_text( GTK_ENTRY( m_entry.m_entry.m_entry ) );
232         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), value.c_str() );
233 }
234 typedef MemberCaller<ModelAttribute, &ModelAttribute::apply> ApplyCaller;
235 void update(){
236         StringOutputStream value( 64 );
237         value << SelectedEntity_getValueForKey( m_key.c_str() );
238         gtk_entry_set_text( GTK_ENTRY( m_entry.m_entry.m_entry ), value.c_str() );
239 }
240 typedef MemberCaller<ModelAttribute, &ModelAttribute::update> UpdateCaller;
241 void browse( const BrowsedPathEntry::SetPathCallback& setPath ){
242         const char *filename = misc_model_dialog( ui::Widget(gtk_widget_get_toplevel( GTK_WIDGET( m_entry.m_entry.m_frame ) ) ));
243
244         if ( filename != 0 ) {
245                 setPath( filename );
246                 apply();
247         }
248 }
249 typedef MemberCaller1<ModelAttribute, const BrowsedPathEntry::SetPathCallback&, &ModelAttribute::browse> BrowseCaller;
250 };
251
252 const char* browse_sound( ui::Widget parent ){
253         StringOutputStream buffer( 1024 );
254
255         buffer << g_qeglobals.m_userGamePath.c_str() << "sound/";
256
257         if ( !file_readable( buffer.c_str() ) ) {
258                 // just go to fsmain
259                 buffer.clear();
260                 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
261         }
262
263         const char* filename = parent.file_dialog(TRUE, "Open Wav File", buffer.c_str(), "sound" );
264         if ( filename != 0 ) {
265                 const char* relative = path_make_relative( filename, GlobalFileSystem().findRoot( filename ) );
266                 if ( relative == filename ) {
267                         globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
268                 }
269                 return relative;
270         }
271         return filename;
272 }
273
274 class SoundAttribute : public EntityAttribute
275 {
276 CopiedString m_key;
277 BrowsedPathEntry m_entry;
278 NonModalEntry m_nonModal;
279 public:
280 SoundAttribute( const char* key ) :
281         m_key( key ),
282         m_entry( BrowseCaller( *this ) ),
283         m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
284         m_nonModal.connect( m_entry.m_entry.m_entry );
285 }
286 void release(){
287         delete this;
288 }
289 ui::Widget getWidget() const {
290         return ui::Widget(GTK_WIDGET( m_entry.m_entry.m_frame ));
291 }
292 void apply(){
293         StringOutputStream value( 64 );
294         value << gtk_entry_get_text( GTK_ENTRY( m_entry.m_entry.m_entry ) );
295         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), value.c_str() );
296 }
297 typedef MemberCaller<SoundAttribute, &SoundAttribute::apply> ApplyCaller;
298 void update(){
299         StringOutputStream value( 64 );
300         value << SelectedEntity_getValueForKey( m_key.c_str() );
301         gtk_entry_set_text( GTK_ENTRY( m_entry.m_entry.m_entry ), value.c_str() );
302 }
303 typedef MemberCaller<SoundAttribute, &SoundAttribute::update> UpdateCaller;
304 void browse( const BrowsedPathEntry::SetPathCallback& setPath ){
305         const char *filename = browse_sound( ui::Widget(gtk_widget_get_toplevel( GTK_WIDGET( m_entry.m_entry.m_frame ) )) );
306
307         if ( filename != 0 ) {
308                 setPath( filename );
309                 apply();
310         }
311 }
312 typedef MemberCaller1<SoundAttribute, const BrowsedPathEntry::SetPathCallback&, &SoundAttribute::browse> BrowseCaller;
313 };
314
315 inline double angle_normalised( double angle ){
316         return float_mod( angle, 360.0 );
317 }
318
319 class AngleAttribute : public EntityAttribute
320 {
321 CopiedString m_key;
322 ui::Entry m_entry;
323 NonModalEntry m_nonModal;
324 public:
325 AngleAttribute( const char* key ) :
326         m_key( key ),
327         m_entry( nullptr ),
328         m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
329         auto entry = numeric_entry_new();
330         m_entry = entry;
331         m_nonModal.connect( m_entry );
332 }
333 void release(){
334         delete this;
335 }
336 ui::Widget getWidget() const {
337         return ui::Widget(GTK_WIDGET( m_entry ));
338 }
339 void apply(){
340         StringOutputStream angle( 32 );
341         angle << angle_normalised( entry_get_float( m_entry ) );
342         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), angle.c_str() );
343 }
344 typedef MemberCaller<AngleAttribute, &AngleAttribute::apply> ApplyCaller;
345
346 void update(){
347         const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
348         if ( !string_empty( value ) ) {
349                 StringOutputStream angle( 32 );
350                 angle << angle_normalised( atof( value ) );
351                 gtk_entry_set_text( m_entry, angle.c_str() );
352         }
353         else
354         {
355                 gtk_entry_set_text( m_entry, "0" );
356         }
357 }
358 typedef MemberCaller<AngleAttribute, &AngleAttribute::update> UpdateCaller;
359 };
360
361 namespace
362 {
363 typedef const char* String;
364 const String buttons[] = { "up", "down", "z-axis" };
365 }
366
367 class DirectionAttribute : public EntityAttribute
368 {
369 CopiedString m_key;
370 ui::Entry m_entry;
371 NonModalEntry m_nonModal;
372 RadioHBox m_radio;
373 NonModalRadio m_nonModalRadio;
374 ui::HBox m_hbox{nullptr};
375 public:
376 DirectionAttribute( const char* key ) :
377         m_key( key ),
378         m_entry( nullptr ),
379         m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ),
380         m_radio( RadioHBox_new( STRING_ARRAY_RANGE( buttons ) ) ),
381         m_nonModalRadio( ApplyRadioCaller( *this ) ){
382         auto entry = numeric_entry_new();
383         m_entry = entry;
384         m_nonModal.connect( m_entry );
385
386         m_nonModalRadio.connect( m_radio.m_radio );
387
388         m_hbox = ui::HBox( FALSE, 4 );
389         m_hbox.show();
390
391         gtk_box_pack_start( GTK_BOX( m_hbox ), GTK_WIDGET( m_radio.m_hbox ), TRUE, TRUE, 0 );
392         gtk_box_pack_start( GTK_BOX( m_hbox ), GTK_WIDGET( m_entry ), TRUE, TRUE, 0 );
393 }
394 void release(){
395         delete this;
396 }
397 ui::Widget getWidget() const {
398         return ui::Widget(GTK_WIDGET( m_hbox ));
399 }
400 void apply(){
401         StringOutputStream angle( 32 );
402         angle << angle_normalised( entry_get_float( m_entry ) );
403         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), angle.c_str() );
404 }
405 typedef MemberCaller<DirectionAttribute, &DirectionAttribute::apply> ApplyCaller;
406
407 void update(){
408         const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
409         if ( !string_empty( value ) ) {
410                 float f = float(atof( value ) );
411                 if ( f == -1 ) {
412                         gtk_widget_set_sensitive( GTK_WIDGET( m_entry ), FALSE );
413                         radio_button_set_active_no_signal( m_radio.m_radio, 0 );
414                         gtk_entry_set_text( m_entry, "" );
415                 }
416                 else if ( f == -2 ) {
417                         gtk_widget_set_sensitive( GTK_WIDGET( m_entry ), FALSE );
418                         radio_button_set_active_no_signal( m_radio.m_radio, 1 );
419                         gtk_entry_set_text( m_entry, "" );
420                 }
421                 else
422                 {
423                         gtk_widget_set_sensitive( GTK_WIDGET( m_entry ), TRUE );
424                         radio_button_set_active_no_signal( m_radio.m_radio, 2 );
425                         StringOutputStream angle( 32 );
426                         angle << angle_normalised( f );
427                         gtk_entry_set_text( m_entry, angle.c_str() );
428                 }
429         }
430         else
431         {
432                 gtk_entry_set_text( m_entry, "0" );
433         }
434 }
435 typedef MemberCaller<DirectionAttribute, &DirectionAttribute::update> UpdateCaller;
436
437 void applyRadio(){
438         int index = radio_button_get_active( m_radio.m_radio );
439         if ( index == 0 ) {
440                 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), "-1" );
441         }
442         else if ( index == 1 ) {
443                 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), "-2" );
444         }
445         else if ( index == 2 ) {
446                 apply();
447         }
448 }
449 typedef MemberCaller<DirectionAttribute, &DirectionAttribute::applyRadio> ApplyRadioCaller;
450 };
451
452
453 class AnglesEntry
454 {
455 public:
456 ui::Entry m_roll;
457 ui::Entry m_pitch;
458 ui::Entry m_yaw;
459 AnglesEntry() : m_roll( nullptr ), m_pitch( nullptr ), m_yaw( nullptr ){
460 }
461 };
462
463 typedef BasicVector3<double> DoubleVector3;
464
465 class AnglesAttribute : public EntityAttribute
466 {
467 CopiedString m_key;
468 AnglesEntry m_angles;
469 NonModalEntry m_nonModal;
470 ui::HBox m_hbox;
471 public:
472 AnglesAttribute( const char* key ) :
473         m_key( key ),
474         m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ),
475         m_hbox(ui::HBox( TRUE, 4 ))
476 {
477         m_hbox.show();
478         {
479                 auto entry = numeric_entry_new();
480                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
481                 m_angles.m_pitch = entry;
482                 m_nonModal.connect( m_angles.m_pitch );
483         }
484         {
485                 auto entry = numeric_entry_new();
486                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
487                 m_angles.m_yaw = entry;
488                 m_nonModal.connect( m_angles.m_yaw );
489         }
490         {
491                 auto entry = numeric_entry_new();
492                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
493                 m_angles.m_roll = entry;
494                 m_nonModal.connect( m_angles.m_roll );
495         }
496 }
497 void release(){
498         delete this;
499 }
500 ui::Widget getWidget() const {
501         return ui::Widget(GTK_WIDGET( m_hbox ));
502 }
503 void apply(){
504         StringOutputStream angles( 64 );
505         angles << angle_normalised( entry_get_float( m_angles.m_pitch ) )
506                    << " " << angle_normalised( entry_get_float( m_angles.m_yaw ) )
507                    << " " << angle_normalised( entry_get_float( m_angles.m_roll ) );
508         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), angles.c_str() );
509 }
510 typedef MemberCaller<AnglesAttribute, &AnglesAttribute::apply> ApplyCaller;
511
512 void update(){
513         StringOutputStream angle( 32 );
514         const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
515         if ( !string_empty( value ) ) {
516                 DoubleVector3 pitch_yaw_roll;
517                 if ( !string_parse_vector3( value, pitch_yaw_roll ) ) {
518                         pitch_yaw_roll = DoubleVector3( 0, 0, 0 );
519                 }
520
521                 angle << angle_normalised( pitch_yaw_roll.x() );
522                 gtk_entry_set_text( m_angles.m_pitch, angle.c_str() );
523                 angle.clear();
524
525                 angle << angle_normalised( pitch_yaw_roll.y() );
526                 gtk_entry_set_text( m_angles.m_yaw, angle.c_str() );
527                 angle.clear();
528
529                 angle << angle_normalised( pitch_yaw_roll.z() );
530                 gtk_entry_set_text( m_angles.m_roll, angle.c_str() );
531                 angle.clear();
532         }
533         else
534         {
535                 gtk_entry_set_text( m_angles.m_pitch, "0" );
536                 gtk_entry_set_text( m_angles.m_yaw, "0" );
537                 gtk_entry_set_text( m_angles.m_roll, "0" );
538         }
539 }
540 typedef MemberCaller<AnglesAttribute, &AnglesAttribute::update> UpdateCaller;
541 };
542
543 class Vector3Entry
544 {
545 public:
546 ui::Entry m_x;
547 ui::Entry m_y;
548 ui::Entry m_z;
549 Vector3Entry() : m_x( nullptr ), m_y( nullptr ), m_z( nullptr ){
550 }
551 };
552
553 class Vector3Attribute : public EntityAttribute
554 {
555 CopiedString m_key;
556 Vector3Entry m_vector3;
557 NonModalEntry m_nonModal;
558 ui::Box m_hbox{nullptr};
559 public:
560 Vector3Attribute( const char* key ) :
561         m_key( key ),
562         m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
563         m_hbox = ui::HBox( TRUE, 4 );
564         m_hbox.show();
565         {
566                 auto entry = numeric_entry_new();
567                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
568                 m_vector3.m_x = entry;
569                 m_nonModal.connect( m_vector3.m_x );
570         }
571         {
572                 auto entry = numeric_entry_new();
573                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
574                 m_vector3.m_y = entry;
575                 m_nonModal.connect( m_vector3.m_y );
576         }
577         {
578                 auto entry = numeric_entry_new();
579                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
580                 m_vector3.m_z = entry;
581                 m_nonModal.connect( m_vector3.m_z );
582         }
583 }
584 void release(){
585         delete this;
586 }
587 ui::Widget getWidget() const {
588         return ui::Widget(GTK_WIDGET( m_hbox ));
589 }
590 void apply(){
591         StringOutputStream vector3( 64 );
592         vector3 << entry_get_float( m_vector3.m_x )
593                         << " " << entry_get_float( m_vector3.m_y )
594                         << " " << entry_get_float( m_vector3.m_z );
595         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), vector3.c_str() );
596 }
597 typedef MemberCaller<Vector3Attribute, &Vector3Attribute::apply> ApplyCaller;
598
599 void update(){
600         StringOutputStream buffer( 32 );
601         const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
602         if ( !string_empty( value ) ) {
603                 DoubleVector3 x_y_z;
604                 if ( !string_parse_vector3( value, x_y_z ) ) {
605                         x_y_z = DoubleVector3( 0, 0, 0 );
606                 }
607
608                 buffer << x_y_z.x();
609                 gtk_entry_set_text( m_vector3.m_x, buffer.c_str() );
610                 buffer.clear();
611
612                 buffer << x_y_z.y();
613                 gtk_entry_set_text( m_vector3.m_y, buffer.c_str() );
614                 buffer.clear();
615
616                 buffer << x_y_z.z();
617                 gtk_entry_set_text( m_vector3.m_z, buffer.c_str() );
618                 buffer.clear();
619         }
620         else
621         {
622                 gtk_entry_set_text( m_vector3.m_x, "0" );
623                 gtk_entry_set_text( m_vector3.m_y, "0" );
624                 gtk_entry_set_text( m_vector3.m_z, "0" );
625         }
626 }
627 typedef MemberCaller<Vector3Attribute, &Vector3Attribute::update> UpdateCaller;
628 };
629
630 class NonModalComboBox
631 {
632 Callback m_changed;
633 guint m_changedHandler;
634
635 static gboolean changed( GtkComboBox *widget, NonModalComboBox* self ){
636         self->m_changed();
637         return FALSE;
638 }
639
640 public:
641 NonModalComboBox( const Callback& changed ) : m_changed( changed ), m_changedHandler( 0 ){
642 }
643 void connect( ui::ComboBox combo ){
644         m_changedHandler = combo.connect( "changed", G_CALLBACK( changed ), this );
645 }
646 void setActive( ui::ComboBox combo, int value ){
647         g_signal_handler_disconnect( G_OBJECT( combo ), m_changedHandler );
648         gtk_combo_box_set_active( combo, value );
649         connect( combo );
650 }
651 };
652
653 class ListAttribute : public EntityAttribute
654 {
655 CopiedString m_key;
656 ui::ComboBox m_combo;
657 NonModalComboBox m_nonModal;
658 const ListAttributeType& m_type;
659 public:
660 ListAttribute( const char* key, const ListAttributeType& type ) :
661         m_key( key ),
662         m_combo( 0 ),
663         m_nonModal( ApplyCaller( *this ) ),
664         m_type( type ){
665         auto combo = ui::ComboBoxText();
666
667         for ( ListAttributeType::const_iterator i = type.begin(); i != type.end(); ++i )
668         {
669                 gtk_combo_box_text_append_text( GTK_COMBO_BOX_TEXT( combo ), ( *i ).first.c_str() );
670         }
671
672         combo.show();
673         m_nonModal.connect( combo );
674
675         m_combo = combo;
676 }
677 void release(){
678         delete this;
679 }
680 ui::Widget getWidget() const {
681         return ui::Widget(GTK_WIDGET( m_combo ));
682 }
683 void apply(){
684         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), m_type[gtk_combo_box_get_active( m_combo )].second.c_str() );
685 }
686 typedef MemberCaller<ListAttribute, &ListAttribute::apply> ApplyCaller;
687
688 void update(){
689         const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
690         ListAttributeType::const_iterator i = m_type.findValue( value );
691         if ( i != m_type.end() ) {
692                 m_nonModal.setActive( m_combo, static_cast<int>( std::distance( m_type.begin(), i ) ) );
693         }
694         else
695         {
696                 m_nonModal.setActive( m_combo, 0 );
697         }
698 }
699 typedef MemberCaller<ListAttribute, &ListAttribute::update> UpdateCaller;
700 };
701
702
703 namespace
704 {
705 ui::Widget g_entity_split1;
706 ui::Widget g_entity_split2;
707 int g_entitysplit1_position;
708 int g_entitysplit2_position;
709
710 bool g_entityInspector_windowConstructed = false;
711
712 GtkTreeView* g_entityClassList;
713 GtkTextView* g_entityClassComment;
714
715 GtkCheckButton* g_entitySpawnflagsCheck[MAX_FLAGS];
716
717 GtkEntry* g_entityKeyEntry;
718 GtkEntry* g_entityValueEntry;
719
720 ui::ListStore g_entlist_store{nullptr};
721 ui::ListStore g_entprops_store{nullptr};
722 const EntityClass* g_current_flags = 0;
723 const EntityClass* g_current_comment = 0;
724 const EntityClass* g_current_attributes = 0;
725
726 // the number of active spawnflags
727 int g_spawnflag_count;
728 // table: index, match spawnflag item to the spawnflag index (i.e. which bit)
729 int spawn_table[MAX_FLAGS];
730 // we change the layout depending on how many spawn flags we need to display
731 // the table is a 4x4 in which we need to put the comment box g_entityClassComment and the spawn flags..
732 GtkTable* g_spawnflagsTable;
733
734 ui::VBox g_attributeBox{nullptr};
735 typedef std::vector<EntityAttribute*> EntityAttributes;
736 EntityAttributes g_entityAttributes;
737 }
738
739 void GlobalEntityAttributes_clear(){
740         for ( EntityAttributes::iterator i = g_entityAttributes.begin(); i != g_entityAttributes.end(); ++i )
741         {
742                 ( *i )->release();
743         }
744         g_entityAttributes.clear();
745 }
746
747 class GetKeyValueVisitor : public Entity::Visitor
748 {
749 KeyValues& m_keyvalues;
750 public:
751 GetKeyValueVisitor( KeyValues& keyvalues )
752         : m_keyvalues( keyvalues ){
753 }
754
755 void visit( const char* key, const char* value ){
756         m_keyvalues.insert( KeyValues::value_type( CopiedString( key ), CopiedString( value ) ) );
757 }
758
759 };
760
761 void Entity_GetKeyValues( const Entity& entity, KeyValues& keyvalues, KeyValues& defaultValues ){
762         GetKeyValueVisitor visitor( keyvalues );
763
764         entity.forEachKeyValue( visitor );
765
766         const EntityClassAttributes& attributes = entity.getEntityClass().m_attributes;
767
768         for ( EntityClassAttributes::const_iterator i = attributes.begin(); i != attributes.end(); ++i )
769         {
770                 defaultValues.insert( KeyValues::value_type( ( *i ).first, ( *i ).second.m_value ) );
771         }
772 }
773
774 void Entity_GetKeyValues_Selected( KeyValues& keyvalues, KeyValues& defaultValues ){
775         class EntityGetKeyValues : public SelectionSystem::Visitor
776         {
777         KeyValues& m_keyvalues;
778         KeyValues& m_defaultValues;
779         mutable std::set<Entity*> m_visited;
780 public:
781         EntityGetKeyValues( KeyValues& keyvalues, KeyValues& defaultValues )
782                 : m_keyvalues( keyvalues ), m_defaultValues( defaultValues ){
783         }
784         void visit( scene::Instance& instance ) const {
785                 Entity* entity = Node_getEntity( instance.path().top() );
786                 if ( entity == 0 && instance.path().size() != 1 ) {
787                         entity = Node_getEntity( instance.path().parent() );
788                 }
789                 if ( entity != 0 && m_visited.insert( entity ).second ) {
790                         Entity_GetKeyValues( *entity, m_keyvalues, m_defaultValues );
791                 }
792         }
793         } visitor( keyvalues, defaultValues );
794         GlobalSelectionSystem().foreachSelected( visitor );
795 }
796
797 const char* keyvalues_valueforkey( KeyValues& keyvalues, const char* key ){
798         KeyValues::iterator i = keyvalues.find( CopiedString( key ) );
799         if ( i != keyvalues.end() ) {
800                 return ( *i ).second.c_str();
801         }
802         return "";
803 }
804
805 class EntityClassListStoreAppend : public EntityClassVisitor
806 {
807 ui::ListStore store;
808 public:
809 EntityClassListStoreAppend( ui::ListStore store_ ) : store( store_ ){
810 }
811 void visit( EntityClass* e ){
812         GtkTreeIter iter;
813         gtk_list_store_append( store, &iter );
814         gtk_list_store_set( store, &iter, 0, e->name(), 1, e, -1 );
815 }
816 };
817
818 void EntityClassList_fill(){
819         EntityClassListStoreAppend append( g_entlist_store );
820         GlobalEntityClassManager().forEach( append );
821 }
822
823 void EntityClassList_clear(){
824         gtk_list_store_clear( g_entlist_store );
825 }
826
827 void SetComment( EntityClass* eclass ){
828         if ( eclass == g_current_comment ) {
829                 return;
830         }
831
832         g_current_comment = eclass;
833
834         GtkTextBuffer* buffer = gtk_text_view_get_buffer( g_entityClassComment );
835         gtk_text_buffer_set_text( buffer, eclass->comments(), -1 );
836 }
837
838 void SurfaceFlags_setEntityClass( EntityClass* eclass ){
839         if ( eclass == g_current_flags ) {
840                 return;
841         }
842
843         g_current_flags = eclass;
844
845         int spawnflag_count = 0;
846
847         {
848                 // do a first pass to count the spawn flags, don't touch the widgets, we don't know in what state they are
849                 for ( int i = 0 ; i < MAX_FLAGS ; i++ )
850                 {
851                         if ( eclass->flagnames[i] && eclass->flagnames[i][0] != 0 && strcmp( eclass->flagnames[i],"-" ) ) {
852                                 spawn_table[spawnflag_count] = i;
853                                 spawnflag_count++;
854                         }
855                 }
856         }
857
858         // disable all remaining boxes
859         // NOTE: these boxes might not even be on display
860         {
861                 for ( int i = 0; i < g_spawnflag_count; ++i )
862                 {
863                         ui::Widget widget = ui::Widget(GTK_WIDGET( g_entitySpawnflagsCheck[i] ));
864                         gtk_label_set_text( GTK_LABEL( gtk_bin_get_child(GTK_BIN(widget)) ), " " );
865                         gtk_widget_hide( widget );
866                         g_object_ref( widget );
867                         gtk_container_remove( GTK_CONTAINER( g_spawnflagsTable ), widget );
868                 }
869         }
870
871         g_spawnflag_count = spawnflag_count;
872
873         {
874                 for ( int i = 0; i < g_spawnflag_count; ++i )
875                 {
876                         ui::Widget widget = ui::Widget(GTK_WIDGET( g_entitySpawnflagsCheck[i] ));
877                         widget.show();
878
879                         StringOutputStream str( 16 );
880                         str << LowerCase( eclass->flagnames[spawn_table[i]] );
881
882                         gtk_table_attach( g_spawnflagsTable, widget, i % 4, i % 4 + 1, i / 4, i / 4 + 1,
883                                                           (GtkAttachOptions)( GTK_FILL ),
884                                                           (GtkAttachOptions)( GTK_FILL ), 0, 0 );
885                         widget.unref();
886
887                         gtk_label_set_text( GTK_LABEL( gtk_bin_get_child(GTK_BIN(widget)) ), str.c_str() );
888                 }
889         }
890 }
891
892 void EntityClassList_selectEntityClass( EntityClass* eclass ){
893         GtkTreeModel* model = GTK_TREE_MODEL( g_entlist_store );
894         GtkTreeIter iter;
895         for ( gboolean good = gtk_tree_model_get_iter_first( model, &iter ); good != FALSE; good = gtk_tree_model_iter_next( model, &iter ) )
896         {
897                 char* text;
898                 gtk_tree_model_get( model, &iter, 0, &text, -1 );
899                 if ( strcmp( text, eclass->name() ) == 0 ) {
900                         GtkTreeView* view = g_entityClassList;
901                         GtkTreePath* path = gtk_tree_model_get_path( model, &iter );
902                         gtk_tree_selection_select_path( gtk_tree_view_get_selection( view ), path );
903                         if ( gtk_widget_get_realized( GTK_WIDGET(view) ) ) {
904                                 gtk_tree_view_scroll_to_cell( view, path, 0, FALSE, 0, 0 );
905                         }
906                         gtk_tree_path_free( path );
907                         good = FALSE;
908                 }
909                 g_free( text );
910         }
911 }
912
913 void EntityInspector_appendAttribute( const char* name, EntityAttribute& attribute ){
914         auto row = DialogRow_new( name, attribute.getWidget() );
915         DialogVBox_packRow( ui::VBox(g_attributeBox), row );
916 }
917
918
919 template<typename Attribute>
920 class StatelessAttributeCreator
921 {
922 public:
923 static EntityAttribute* create( const char* name ){
924         return new Attribute( name );
925 }
926 };
927
928 class EntityAttributeFactory
929 {
930 typedef EntityAttribute* ( *CreateFunc )( const char* name );
931 typedef std::map<const char*, CreateFunc, RawStringLess> Creators;
932 Creators m_creators;
933 public:
934 EntityAttributeFactory(){
935         m_creators.insert( Creators::value_type( "string", &StatelessAttributeCreator<StringAttribute>::create ) );
936         m_creators.insert( Creators::value_type( "color", &StatelessAttributeCreator<StringAttribute>::create ) );
937         m_creators.insert( Creators::value_type( "integer", &StatelessAttributeCreator<StringAttribute>::create ) );
938         m_creators.insert( Creators::value_type( "real", &StatelessAttributeCreator<StringAttribute>::create ) );
939         m_creators.insert( Creators::value_type( "shader", &StatelessAttributeCreator<ShaderAttribute>::create ) );
940         m_creators.insert( Creators::value_type( "boolean", &StatelessAttributeCreator<BooleanAttribute>::create ) );
941         m_creators.insert( Creators::value_type( "angle", &StatelessAttributeCreator<AngleAttribute>::create ) );
942         m_creators.insert( Creators::value_type( "direction", &StatelessAttributeCreator<DirectionAttribute>::create ) );
943         m_creators.insert( Creators::value_type( "angles", &StatelessAttributeCreator<AnglesAttribute>::create ) );
944         m_creators.insert( Creators::value_type( "model", &StatelessAttributeCreator<ModelAttribute>::create ) );
945         m_creators.insert( Creators::value_type( "sound", &StatelessAttributeCreator<SoundAttribute>::create ) );
946         m_creators.insert( Creators::value_type( "vector3", &StatelessAttributeCreator<Vector3Attribute>::create ) );
947         m_creators.insert( Creators::value_type( "real3", &StatelessAttributeCreator<Vector3Attribute>::create ) );
948 }
949 EntityAttribute* create( const char* type, const char* name ){
950         Creators::iterator i = m_creators.find( type );
951         if ( i != m_creators.end() ) {
952                 return ( *i ).second( name );
953         }
954         const ListAttributeType* listType = GlobalEntityClassManager().findListType( type );
955         if ( listType != 0 ) {
956                 return new ListAttribute( name, *listType );
957         }
958         return 0;
959 }
960 };
961
962 typedef Static<EntityAttributeFactory> GlobalEntityAttributeFactory;
963
964 void EntityInspector_setEntityClass( EntityClass *eclass ){
965         EntityClassList_selectEntityClass( eclass );
966         SurfaceFlags_setEntityClass( eclass );
967
968         if ( eclass != g_current_attributes ) {
969                 g_current_attributes = eclass;
970
971                 container_remove_all( g_attributeBox );
972                 GlobalEntityAttributes_clear();
973
974                 for ( EntityClassAttributes::const_iterator i = eclass->m_attributes.begin(); i != eclass->m_attributes.end(); ++i )
975                 {
976                         EntityAttribute* attribute = GlobalEntityAttributeFactory::instance().create( ( *i ).second.m_type.c_str(), ( *i ).first.c_str() );
977                         if ( attribute != 0 ) {
978                                 g_entityAttributes.push_back( attribute );
979                                 EntityInspector_appendAttribute( EntityClassAttributePair_getName( *i ), *g_entityAttributes.back() );
980                         }
981                 }
982         }
983 }
984
985 void EntityInspector_updateSpawnflags(){
986         {
987                 int f = atoi( SelectedEntity_getValueForKey( "spawnflags" ) );
988                 for ( int i = 0; i < g_spawnflag_count; ++i )
989                 {
990                         int v = !!( f & ( 1 << spawn_table[i] ) );
991
992                         toggle_button_set_active_no_signal( ui::ToggleButton(GTK_TOGGLE_BUTTON( g_entitySpawnflagsCheck[i] )), v );
993                 }
994         }
995         {
996                 // take care of the remaining ones
997                 for ( int i = g_spawnflag_count; i < MAX_FLAGS; ++i )
998                 {
999                         toggle_button_set_active_no_signal( ui::ToggleButton(GTK_TOGGLE_BUTTON( g_entitySpawnflagsCheck[i] )), FALSE );
1000                 }
1001         }
1002 }
1003
1004 void EntityInspector_applySpawnflags(){
1005         int f, i, v;
1006         char sz[32];
1007
1008         f = 0;
1009         for ( i = 0; i < g_spawnflag_count; ++i )
1010         {
1011                 v = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( g_entitySpawnflagsCheck[i] ) );
1012                 f |= v << spawn_table[i];
1013         }
1014
1015         sprintf( sz, "%i", f );
1016         const char* value = ( f == 0 ) ? "" : sz;
1017
1018         {
1019                 StringOutputStream command;
1020                 command << "entitySetFlags -flags " << f;
1021                 UndoableCommand undo( "entitySetSpawnflags" );
1022
1023                 Scene_EntitySetKeyValue_Selected( "spawnflags", value );
1024         }
1025 }
1026
1027
1028 void EntityInspector_updateKeyValues(){
1029         g_selectedKeyValues.clear();
1030         g_selectedDefaultKeyValues.clear();
1031         Entity_GetKeyValues_Selected( g_selectedKeyValues, g_selectedDefaultKeyValues );
1032
1033         EntityInspector_setEntityClass( GlobalEntityClassManager().findOrInsert( keyvalues_valueforkey( g_selectedKeyValues, "classname" ), false ) );
1034
1035         EntityInspector_updateSpawnflags();
1036
1037         ui::ListStore store = g_entprops_store;
1038
1039         // save current key/val pair around filling epair box
1040         // row_select wipes it and sets to first in list
1041         CopiedString strKey( gtk_entry_get_text( g_entityKeyEntry ) );
1042         CopiedString strVal( gtk_entry_get_text( g_entityValueEntry ) );
1043
1044         gtk_list_store_clear( store );
1045         // Walk through list and add pairs
1046         for ( KeyValues::iterator i = g_selectedKeyValues.begin(); i != g_selectedKeyValues.end(); ++i )
1047         {
1048                 GtkTreeIter iter;
1049                 gtk_list_store_append( store, &iter );
1050                 StringOutputStream key( 64 );
1051                 key << ( *i ).first.c_str();
1052                 StringOutputStream value( 64 );
1053                 value << ( *i ).second.c_str();
1054                 gtk_list_store_set( store, &iter, 0, key.c_str(), 1, value.c_str(), -1 );
1055         }
1056
1057         gtk_entry_set_text( g_entityKeyEntry, strKey.c_str() );
1058         gtk_entry_set_text( g_entityValueEntry, strVal.c_str() );
1059
1060         for ( EntityAttributes::const_iterator i = g_entityAttributes.begin(); i != g_entityAttributes.end(); ++i )
1061         {
1062                 ( *i )->update();
1063         }
1064 }
1065
1066 class EntityInspectorDraw
1067 {
1068 IdleDraw m_idleDraw;
1069 public:
1070 EntityInspectorDraw() : m_idleDraw( FreeCaller<EntityInspector_updateKeyValues>( ) ){
1071 }
1072 void queueDraw(){
1073         m_idleDraw.queueDraw();
1074 }
1075 };
1076
1077 EntityInspectorDraw g_EntityInspectorDraw;
1078
1079
1080 void EntityInspector_keyValueChanged(){
1081         g_EntityInspectorDraw.queueDraw();
1082 }
1083 void EntityInspector_selectionChanged( const Selectable& ){
1084         EntityInspector_keyValueChanged();
1085 }
1086
1087 // Creates a new entity based on the currently selected brush and entity type.
1088 //
1089 void EntityClassList_createEntity(){
1090         GtkTreeView* view = g_entityClassList;
1091
1092         // find out what type of entity we are trying to create
1093         GtkTreeModel* model;
1094         GtkTreeIter iter;
1095         if ( gtk_tree_selection_get_selected( gtk_tree_view_get_selection( view ), &model, &iter ) == FALSE ) {
1096                 ui::Widget(gtk_widget_get_toplevel( GTK_WIDGET( g_entityClassList ) )).alert( "You must have a selected class to create an entity", "info" );
1097                 return;
1098         }
1099
1100         char* text;
1101         gtk_tree_model_get( model, &iter, 0, &text, -1 );
1102
1103         {
1104                 StringOutputStream command;
1105                 command << "entityCreate -class " << text;
1106
1107                 UndoableCommand undo( command.c_str() );
1108
1109                 Entity_createFromSelection( text, g_vector3_identity );
1110         }
1111         g_free( text );
1112 }
1113
1114 void EntityInspector_applyKeyValue(){
1115         // Get current selection text
1116         StringOutputStream key( 64 );
1117         key << gtk_entry_get_text( g_entityKeyEntry );
1118         StringOutputStream value( 64 );
1119         value << gtk_entry_get_text( g_entityValueEntry );
1120
1121
1122         // TTimo: if you change the classname to worldspawn you won't merge back in the structural brushes but create a parasite entity
1123         if ( !strcmp( key.c_str(), "classname" ) && !strcmp( value.c_str(), "worldspawn" ) ) {
1124                 ui::Widget(gtk_widget_get_toplevel( GTK_WIDGET( g_entityKeyEntry )) ).alert( "Cannot change \"classname\" key back to worldspawn.", 0, ui::alert_type::OK );
1125                 return;
1126         }
1127
1128
1129         // RR2DO2: we don't want spaces in entity keys
1130         if ( strstr( key.c_str(), " " ) ) {
1131                 ui::Widget(gtk_widget_get_toplevel( GTK_WIDGET( g_entityKeyEntry )) ).alert( "No spaces are allowed in entity keys.", 0, ui::alert_type::OK );
1132                 return;
1133         }
1134
1135         if ( strcmp( key.c_str(), "classname" ) == 0 ) {
1136                 StringOutputStream command;
1137                 command << "entitySetClass -class " << value.c_str();
1138                 UndoableCommand undo( command.c_str() );
1139                 Scene_EntitySetClassname_Selected( value.c_str() );
1140         }
1141         else
1142         {
1143                 Scene_EntitySetKeyValue_Selected_Undoable( key.c_str(), value.c_str() );
1144         }
1145 }
1146
1147 void EntityInspector_clearKeyValue(){
1148         // Get current selection text
1149         StringOutputStream key( 64 );
1150         key << gtk_entry_get_text( g_entityKeyEntry );
1151
1152         if ( strcmp( key.c_str(), "classname" ) != 0 ) {
1153                 StringOutputStream command;
1154                 command << "entityDeleteKey -key " << key.c_str();
1155                 UndoableCommand undo( command.c_str() );
1156                 Scene_EntitySetKeyValue_Selected( key.c_str(), "" );
1157         }
1158 }
1159
1160 void EntityInspector_clearAllKeyValues(){
1161         UndoableCommand undo( "entityClear" );
1162
1163         // remove all keys except classname
1164         for ( KeyValues::iterator i = g_selectedKeyValues.begin(); i != g_selectedKeyValues.end(); ++i )
1165         {
1166                 if ( strcmp( ( *i ).first.c_str(), "classname" ) != 0 ) {
1167                         Scene_EntitySetKeyValue_Selected( ( *i ).first.c_str(), "" );
1168                 }
1169         }
1170 }
1171
1172 // =============================================================================
1173 // callbacks
1174
1175 static void EntityClassList_selection_changed( GtkTreeSelection* selection, gpointer data ){
1176         GtkTreeModel* model;
1177         GtkTreeIter selected;
1178         if ( gtk_tree_selection_get_selected( selection, &model, &selected ) ) {
1179                 EntityClass* eclass;
1180                 gtk_tree_model_get( model, &selected, 1, &eclass, -1 );
1181                 if ( eclass != 0 ) {
1182                         SetComment( eclass );
1183                 }
1184         }
1185 }
1186
1187 static gint EntityClassList_button_press( ui::Widget widget, GdkEventButton *event, gpointer data ){
1188         if ( event->type == GDK_2BUTTON_PRESS ) {
1189                 EntityClassList_createEntity();
1190                 return TRUE;
1191         }
1192         return FALSE;
1193 }
1194
1195 static gint EntityClassList_keypress( ui::Widget widget, GdkEventKey* event, gpointer data ){
1196         unsigned int code = gdk_keyval_to_upper( event->keyval );
1197
1198         if ( event->keyval == GDK_KEY_Return ) {
1199                 EntityClassList_createEntity();
1200                 return TRUE;
1201         }
1202
1203         // select the entity that starts with the key pressed
1204         if ( code <= 'Z' && code >= 'A' ) {
1205                 GtkTreeView* view = g_entityClassList;
1206                 GtkTreeModel* model;
1207                 GtkTreeIter iter;
1208                 if ( gtk_tree_selection_get_selected( gtk_tree_view_get_selection( view ), &model, &iter ) == FALSE
1209                          || gtk_tree_model_iter_next( model, &iter ) == FALSE ) {
1210                         gtk_tree_model_get_iter_first( model, &iter );
1211                 }
1212
1213                 for ( std::size_t count = gtk_tree_model_iter_n_children( model, 0 ); count > 0; --count )
1214                 {
1215                         char* text;
1216                         gtk_tree_model_get( model, &iter, 0, &text, -1 );
1217
1218                         if ( toupper( text[0] ) == (int)code ) {
1219                                 GtkTreePath* path = gtk_tree_model_get_path( model, &iter );
1220                                 gtk_tree_selection_select_path( gtk_tree_view_get_selection( view ), path );
1221                                 if ( gtk_widget_get_realized( GTK_WIDGET(view) ) ) {
1222                                         gtk_tree_view_scroll_to_cell( view, path, 0, FALSE, 0, 0 );
1223                                 }
1224                                 gtk_tree_path_free( path );
1225                                 count = 1;
1226                         }
1227
1228                         g_free( text );
1229
1230                         if ( gtk_tree_model_iter_next( model, &iter ) == FALSE ) {
1231                                 gtk_tree_model_get_iter_first( model, &iter );
1232                         }
1233                 }
1234
1235                 return TRUE;
1236         }
1237         return FALSE;
1238 }
1239
1240 static void EntityProperties_selection_changed( GtkTreeSelection* selection, gpointer data ){
1241         // find out what type of entity we are trying to create
1242         GtkTreeModel* model;
1243         GtkTreeIter iter;
1244         if ( gtk_tree_selection_get_selected( selection, &model, &iter ) == FALSE ) {
1245                 return;
1246         }
1247
1248         char* key;
1249         char* val;
1250         gtk_tree_model_get( model, &iter, 0, &key, 1, &val, -1 );
1251
1252         gtk_entry_set_text( g_entityKeyEntry, key );
1253         gtk_entry_set_text( g_entityValueEntry, val );
1254
1255         g_free( key );
1256         g_free( val );
1257 }
1258
1259 static void SpawnflagCheck_toggled( ui::Widget widget, gpointer data ){
1260         EntityInspector_applySpawnflags();
1261 }
1262
1263 static gint EntityEntry_keypress( GtkEntry* widget, GdkEventKey* event, gpointer data ){
1264         if ( event->keyval == GDK_KEY_Return ) {
1265                 if ( widget == g_entityKeyEntry ) {
1266                         gtk_entry_set_text( g_entityValueEntry, "" );
1267                         gtk_window_set_focus( GTK_WINDOW( gtk_widget_get_toplevel( GTK_WIDGET( widget ) ) ), GTK_WIDGET( g_entityValueEntry ) );
1268                 }
1269                 else
1270                 {
1271                         EntityInspector_applyKeyValue();
1272                 }
1273                 return TRUE;
1274         }
1275         if ( event->keyval == GDK_KEY_Escape ) {
1276                 gtk_window_set_focus( GTK_WINDOW( gtk_widget_get_toplevel( GTK_WIDGET( widget ) ) ), NULL );
1277                 return TRUE;
1278         }
1279
1280         return FALSE;
1281 }
1282
1283 void EntityInspector_destroyWindow( ui::Widget widget, gpointer data ){
1284         g_entitysplit1_position = gtk_paned_get_position( GTK_PANED( g_entity_split1 ) );
1285         g_entitysplit2_position = gtk_paned_get_position( GTK_PANED( g_entity_split2 ) );
1286
1287         g_entityInspector_windowConstructed = false;
1288         GlobalEntityAttributes_clear();
1289 }
1290
1291 ui::Widget EntityInspector_constructWindow( ui::Window toplevel ){
1292         ui::Widget vbox = ui::VBox( FALSE, 2 );
1293         vbox.show();
1294         gtk_container_set_border_width( GTK_CONTAINER( vbox ), 2 );
1295
1296         vbox.connect( "destroy", G_CALLBACK( EntityInspector_destroyWindow ), 0 );
1297
1298         {
1299                 ui::Widget split1 = ui::VPaned();
1300                 gtk_box_pack_start( GTK_BOX( vbox ), split1, TRUE, TRUE, 0 );
1301                 split1.show();
1302
1303                 g_entity_split1 = split1;
1304
1305                 {
1306                         ui::Widget split2 = ui::VPaned();
1307                         gtk_paned_add1( GTK_PANED( split1 ), split2 );
1308                         split2.show();
1309
1310                         g_entity_split2 = split2;
1311
1312                         {
1313                                 // class list
1314                                 auto scr = ui::ScrolledWindow();
1315                                 scr.show();
1316                                 gtk_paned_add1( GTK_PANED( split2 ), scr );
1317                                 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
1318                                 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
1319
1320                                 {
1321                                         ui::ListStore store = ui::ListStore(gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_POINTER ));
1322
1323                                         auto view = ui::TreeView( ui::TreeModel( GTK_TREE_MODEL( store ) ));
1324                                         gtk_tree_view_set_enable_search( GTK_TREE_VIEW( view ), FALSE );
1325                                         gtk_tree_view_set_headers_visible( view, FALSE );
1326                                         view.connect( "button_press_event", G_CALLBACK( EntityClassList_button_press ), 0 );
1327                                         view.connect( "key_press_event", G_CALLBACK( EntityClassList_keypress ), 0 );
1328
1329                                         {
1330                                                 auto renderer = ui::CellRendererText();
1331                                                 GtkTreeViewColumn* column = ui::TreeViewColumn( "Key", renderer, {{"text", 0}} );
1332                                                 gtk_tree_view_append_column( view, column );
1333                                         }
1334
1335                                         {
1336                                                 auto selection = ui::TreeSelection(gtk_tree_view_get_selection( view ));
1337                                                 selection.connect( "changed", G_CALLBACK( EntityClassList_selection_changed ), 0 );
1338                                         }
1339
1340                                         view.show();
1341
1342                                         scr.add(view);
1343
1344                                         store.unref();
1345                                         g_entityClassList = view;
1346                                         g_entlist_store = store;
1347                                 }
1348                         }
1349
1350                         {
1351                                 auto scr = ui::ScrolledWindow();
1352                                 scr.show();
1353                                 gtk_paned_add2( GTK_PANED( split2 ), scr );
1354                                 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
1355                                 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
1356
1357                                 {
1358                                         auto text = ui::TextView();
1359                                         gtk_widget_set_size_request( GTK_WIDGET( text ), 0, -1 ); // allow shrinking
1360                                         gtk_text_view_set_wrap_mode( text, GTK_WRAP_WORD );
1361                                         gtk_text_view_set_editable( text, FALSE );
1362                                         text.show();
1363                                         scr.add(text);
1364                                         g_entityClassComment = text;
1365                                 }
1366                         }
1367                 }
1368
1369                 {
1370                         ui::Widget split2 = ui::VPaned();
1371                         gtk_paned_add2( GTK_PANED( split1 ), split2 );
1372                         split2.show();
1373
1374                         {
1375                                 ui::Widget vbox2 = ui::VBox( FALSE, 2 );
1376                                 vbox2.show();
1377                                 gtk_paned_pack1( GTK_PANED( split2 ), vbox2, FALSE, FALSE );
1378
1379                                 {
1380                                         // Spawnflags (4 colums wide max, or window gets too wide.)
1381                                         auto table = ui::Table( 4, 4, FALSE );
1382                                         gtk_box_pack_start( GTK_BOX( vbox2 ), GTK_WIDGET( table ), FALSE, TRUE, 0 );
1383                                         table.show();
1384
1385                                         g_spawnflagsTable = table;
1386
1387                                         for ( int i = 0; i < MAX_FLAGS; i++ )
1388                                         {
1389                                                 auto check = ui::CheckButton( "" );
1390                                                 g_object_ref( GTK_WIDGET( check ) );
1391                                                 g_object_set_data( G_OBJECT( check ), "handler", gint_to_pointer( check.connect( "toggled", G_CALLBACK( SpawnflagCheck_toggled ), 0 ) ) );
1392                                                 g_entitySpawnflagsCheck[i] = check;
1393                                         }
1394                                 }
1395
1396                                 {
1397                                         // key/value list
1398                                         auto scr = ui::ScrolledWindow();
1399                                         scr.show();
1400                                         gtk_box_pack_start( GTK_BOX( vbox2 ), scr, TRUE, TRUE, 0 );
1401                                         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
1402                                         gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
1403
1404                                         {
1405                                                 ui::ListStore store = ui::ListStore(gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_STRING ));
1406
1407                                                 ui::Widget view = ui::TreeView(ui::TreeModel( GTK_TREE_MODEL( store ) ));
1408                                                 gtk_tree_view_set_enable_search( GTK_TREE_VIEW( view ), FALSE );
1409                                                 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW( view ), FALSE );
1410
1411                                                 {
1412                                                         auto renderer = ui::CellRendererText();
1413                                                         GtkTreeViewColumn* column = ui::TreeViewColumn( "", renderer, {{"text", 0}} );
1414                                                         gtk_tree_view_append_column( GTK_TREE_VIEW( view ), column );
1415                                                 }
1416
1417                                                 {
1418                                                         auto renderer = ui::CellRendererText();
1419                                                         GtkTreeViewColumn* column = ui::TreeViewColumn( "", renderer, {{"text", 1}} );
1420                                                         gtk_tree_view_append_column( GTK_TREE_VIEW( view ), column );
1421                                                 }
1422
1423                                                 {
1424                                                         auto selection = ui::TreeSelection(gtk_tree_view_get_selection( GTK_TREE_VIEW( view ) ));
1425                                                         selection.connect( "changed", G_CALLBACK( EntityProperties_selection_changed ), 0 );
1426                                                 }
1427
1428                                                 view.show();
1429
1430                                                 scr.add(view);
1431
1432                                                 store.unref();
1433
1434                                                 g_entprops_store = store;
1435                                         }
1436                                 }
1437
1438                                 {
1439                                         // key/value entry
1440                                         auto table = ui::Table( 2, 2, FALSE );
1441                                         table.show();
1442                                         gtk_box_pack_start( GTK_BOX( vbox2 ), GTK_WIDGET( table ), FALSE, TRUE, 0 );
1443                                         gtk_table_set_row_spacings( table, 3 );
1444                                         gtk_table_set_col_spacings( table, 5 );
1445
1446                                         {
1447                                                 auto entry = ui::Entry();
1448                                                 entry.show();
1449                                                 gtk_table_attach( table, GTK_WIDGET( entry ), 1, 2, 0, 1,
1450                                                                                   (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1451                                                                                   (GtkAttachOptions)( 0 ), 0, 0 );
1452                                                 gtk_widget_set_events( GTK_WIDGET( entry ), GDK_KEY_PRESS_MASK );
1453                                                 entry.connect( "key_press_event", G_CALLBACK( EntityEntry_keypress ), 0 );
1454                                                 g_entityKeyEntry = entry;
1455                                         }
1456
1457                                         {
1458                                                 auto entry = ui::Entry();
1459                                                 entry.show();
1460                                                 gtk_table_attach( table, GTK_WIDGET( entry ), 1, 2, 1, 2,
1461                                                                                   (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1462                                                                                   (GtkAttachOptions)( 0 ), 0, 0 );
1463                                                 gtk_widget_set_events( GTK_WIDGET( entry ), GDK_KEY_PRESS_MASK );
1464                                                 entry.connect( "key_press_event", G_CALLBACK( EntityEntry_keypress ), 0 );
1465                                                 g_entityValueEntry = entry;
1466                                         }
1467
1468                                         {
1469                                                 auto label = ui::Label( "Value" );
1470                                                 label.show();
1471                                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 1, 2,
1472                                                                                   (GtkAttachOptions)( GTK_FILL ),
1473                                                                                   (GtkAttachOptions)( 0 ), 0, 0 );
1474                                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1475                                         }
1476
1477                                         {
1478                                                 auto label = ui::Label( "Key" );
1479                                                 label.show();
1480                                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
1481                                                                                   (GtkAttachOptions)( GTK_FILL ),
1482                                                                                   (GtkAttachOptions)( 0 ), 0, 0 );
1483                                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1484                                         }
1485                                 }
1486
1487                                 {
1488                                         auto hbox = ui::HBox( TRUE, 4 );
1489                                         hbox.show();
1490                                         gtk_box_pack_start( GTK_BOX( vbox2 ), GTK_WIDGET( hbox ), FALSE, TRUE, 0 );
1491
1492                                         {
1493                                                 auto button = ui::Button( "Clear All" );
1494                                                 button.show();
1495                                                 button.connect( "clicked", G_CALLBACK( EntityInspector_clearAllKeyValues ), 0 );
1496                                                 gtk_box_pack_start( hbox, GTK_WIDGET( button ), TRUE, TRUE, 0 );
1497                                         }
1498                                         {
1499                                                 auto button = ui::Button( "Delete Key" );
1500                                                 button.show();
1501                                                 button.connect( "clicked", G_CALLBACK( EntityInspector_clearKeyValue ), 0 );
1502                                                 gtk_box_pack_start( hbox, GTK_WIDGET( button ), TRUE, TRUE, 0 );
1503                                         }
1504                                 }
1505                         }
1506
1507                         {
1508                                 auto scr = ui::ScrolledWindow();
1509                                 scr.show();
1510                                 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
1511
1512                                 auto viewport = ui::Container(GTK_CONTAINER(gtk_viewport_new( 0, 0 )));
1513                                 viewport.show();
1514                                 gtk_viewport_set_shadow_type( GTK_VIEWPORT( viewport ), GTK_SHADOW_NONE );
1515
1516                                 g_attributeBox = ui::VBox( FALSE, 2 );
1517                                 g_attributeBox.show();
1518
1519                                 viewport.add(g_attributeBox);
1520                                 scr.add(viewport);
1521                                 gtk_paned_pack2( GTK_PANED( split2 ), scr, FALSE, FALSE );
1522                         }
1523                 }
1524         }
1525
1526
1527         {
1528                 // show the sliders in any case
1529                 if ( g_entitysplit2_position > 22 ) {
1530                         gtk_paned_set_position( GTK_PANED( g_entity_split2 ), g_entitysplit2_position );
1531                 }
1532                 else {
1533                         g_entitysplit2_position = 22;
1534                         gtk_paned_set_position( GTK_PANED( g_entity_split2 ), 22 );
1535                 }
1536                 if ( ( g_entitysplit1_position - g_entitysplit2_position ) > 27 ) {
1537                         gtk_paned_set_position( GTK_PANED( g_entity_split1 ), g_entitysplit1_position );
1538                 }
1539                 else {
1540                         gtk_paned_set_position( GTK_PANED( g_entity_split1 ), g_entitysplit2_position + 27 );
1541                 }
1542         }
1543
1544         g_entityInspector_windowConstructed = true;
1545         EntityClassList_fill();
1546
1547         typedef FreeCaller1<const Selectable&, EntityInspector_selectionChanged> EntityInspectorSelectionChangedCaller;
1548         GlobalSelectionSystem().addSelectionChangeCallback( EntityInspectorSelectionChangedCaller() );
1549         GlobalEntityCreator().setKeyValueChangedFunc( EntityInspector_keyValueChanged );
1550
1551         // hack
1552         gtk_container_set_focus_chain( GTK_CONTAINER( vbox ), NULL );
1553
1554         return vbox;
1555 }
1556
1557 class EntityInspector : public ModuleObserver
1558 {
1559 std::size_t m_unrealised;
1560 public:
1561 EntityInspector() : m_unrealised( 1 ){
1562 }
1563 void realise(){
1564         if ( --m_unrealised == 0 ) {
1565                 if ( g_entityInspector_windowConstructed ) {
1566                         //globalOutputStream() << "Entity Inspector: realise\n";
1567                         EntityClassList_fill();
1568                 }
1569         }
1570 }
1571 void unrealise(){
1572         if ( ++m_unrealised == 1 ) {
1573                 if ( g_entityInspector_windowConstructed ) {
1574                         //globalOutputStream() << "Entity Inspector: unrealise\n";
1575                         EntityClassList_clear();
1576                 }
1577         }
1578 }
1579 };
1580
1581 EntityInspector g_EntityInspector;
1582
1583 #include "preferencesystem.h"
1584 #include "stringio.h"
1585
1586 void EntityInspector_construct(){
1587         GlobalEntityClassManager().attach( g_EntityInspector );
1588
1589         GlobalPreferenceSystem().registerPreference( "EntitySplit1", IntImportStringCaller( g_entitysplit1_position ), IntExportStringCaller( g_entitysplit1_position ) );
1590         GlobalPreferenceSystem().registerPreference( "EntitySplit2", IntImportStringCaller( g_entitysplit2_position ), IntExportStringCaller( g_entitysplit2_position ) );
1591
1592 }
1593
1594 void EntityInspector_destroy(){
1595         GlobalEntityClassManager().detach( g_EntityInspector );
1596 }
1597
1598 const char *EntityInspector_getCurrentKey(){
1599         if ( !GroupDialog_isShown() ) {
1600                 return 0;
1601         }
1602         if ( GroupDialog_getPage() != g_page_entity ) {
1603                 return 0;
1604         }
1605         return gtk_entry_get_text( g_entityKeyEntry );
1606 }