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