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