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