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