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