]> 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 = GTK_ENTRY( gtk_entry_new() );
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 = GTK_ENTRY( gtk_entry_new() );
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 = GTK_HBOX( gtk_hbox_new( 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 GtkBox* m_hbox;
487 public:
488 AnglesAttribute( const char* key ) :
489         m_key( key ),
490         m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
491         m_hbox = GTK_BOX( gtk_hbox_new( TRUE, 4 ) );
492         gtk_widget_show( GTK_WIDGET( m_hbox ) );
493         {
494                 GtkEntry* entry = numeric_entry_new();
495                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
496                 m_angles.m_pitch = entry;
497                 m_nonModal.connect( m_angles.m_pitch );
498         }
499         {
500                 GtkEntry* entry = numeric_entry_new();
501                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
502                 m_angles.m_yaw = entry;
503                 m_nonModal.connect( m_angles.m_yaw );
504         }
505         {
506                 GtkEntry* entry = numeric_entry_new();
507                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
508                 m_angles.m_roll = entry;
509                 m_nonModal.connect( m_angles.m_roll );
510         }
511 }
512 void release(){
513         delete this;
514 }
515 ui::Widget getWidget() const {
516         return ui::Widget(GTK_WIDGET( m_hbox ));
517 }
518 void apply(){
519         StringOutputStream angles( 64 );
520         angles << angle_normalised( entry_get_float( m_angles.m_pitch ) )
521                    << " " << angle_normalised( entry_get_float( m_angles.m_yaw ) )
522                    << " " << angle_normalised( entry_get_float( m_angles.m_roll ) );
523         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), angles.c_str() );
524 }
525 typedef MemberCaller<AnglesAttribute, &AnglesAttribute::apply> ApplyCaller;
526
527 void update(){
528         StringOutputStream angle( 32 );
529         const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
530         if ( !string_empty( value ) ) {
531                 DoubleVector3 pitch_yaw_roll;
532                 if ( !string_parse_vector3( value, pitch_yaw_roll ) ) {
533                         pitch_yaw_roll = DoubleVector3( 0, 0, 0 );
534                 }
535
536                 angle << angle_normalised( pitch_yaw_roll.x() );
537                 gtk_entry_set_text( m_angles.m_pitch, angle.c_str() );
538                 angle.clear();
539
540                 angle << angle_normalised( pitch_yaw_roll.y() );
541                 gtk_entry_set_text( m_angles.m_yaw, angle.c_str() );
542                 angle.clear();
543
544                 angle << angle_normalised( pitch_yaw_roll.z() );
545                 gtk_entry_set_text( m_angles.m_roll, angle.c_str() );
546                 angle.clear();
547         }
548         else
549         {
550                 gtk_entry_set_text( m_angles.m_pitch, "0" );
551                 gtk_entry_set_text( m_angles.m_yaw, "0" );
552                 gtk_entry_set_text( m_angles.m_roll, "0" );
553         }
554 }
555 typedef MemberCaller<AnglesAttribute, &AnglesAttribute::update> UpdateCaller;
556 };
557
558 class Vector3Entry
559 {
560 public:
561 GtkEntry* m_x;
562 GtkEntry* m_y;
563 GtkEntry* m_z;
564 Vector3Entry() : m_x( 0 ), m_y( 0 ), m_z( 0 ){
565 }
566 };
567
568 class Vector3Attribute : public EntityAttribute
569 {
570 std::string m_key;
571 Vector3Entry m_vector3;
572 NonModalEntry m_nonModal;
573 GtkBox* m_hbox;
574 public:
575 Vector3Attribute( const char* key ) :
576         m_key( key ),
577         m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
578         m_hbox = GTK_BOX( gtk_hbox_new( TRUE, 4 ) );
579         gtk_widget_show( GTK_WIDGET( m_hbox ) );
580         {
581                 GtkEntry* entry = numeric_entry_new();
582                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
583                 m_vector3.m_x = entry;
584                 m_nonModal.connect( m_vector3.m_x );
585         }
586         {
587                 GtkEntry* entry = numeric_entry_new();
588                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
589                 m_vector3.m_y = entry;
590                 m_nonModal.connect( m_vector3.m_y );
591         }
592         {
593                 GtkEntry* entry = numeric_entry_new();
594                 gtk_box_pack_start( m_hbox, GTK_WIDGET( entry ), TRUE, TRUE, 0 );
595                 m_vector3.m_z = entry;
596                 m_nonModal.connect( m_vector3.m_z );
597         }
598 }
599 void release(){
600         delete this;
601 }
602 ui::Widget getWidget() const {
603         return ui::Widget(GTK_WIDGET( m_hbox ));
604 }
605 void apply(){
606         StringOutputStream vector3( 64 );
607         vector3 << entry_get_float( m_vector3.m_x )
608                         << " " << entry_get_float( m_vector3.m_y )
609                         << " " << entry_get_float( m_vector3.m_z );
610         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), vector3.c_str() );
611 }
612 typedef MemberCaller<Vector3Attribute, &Vector3Attribute::apply> ApplyCaller;
613
614 void update(){
615         StringOutputStream buffer( 32 );
616         const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
617         if ( !string_empty( value ) ) {
618                 DoubleVector3 x_y_z;
619                 if ( !string_parse_vector3( value, x_y_z ) ) {
620                         x_y_z = DoubleVector3( 0, 0, 0 );
621                 }
622
623                 buffer << x_y_z.x();
624                 gtk_entry_set_text( m_vector3.m_x, buffer.c_str() );
625                 buffer.clear();
626
627                 buffer << x_y_z.y();
628                 gtk_entry_set_text( m_vector3.m_y, buffer.c_str() );
629                 buffer.clear();
630
631                 buffer << x_y_z.z();
632                 gtk_entry_set_text( m_vector3.m_z, buffer.c_str() );
633                 buffer.clear();
634         }
635         else
636         {
637                 gtk_entry_set_text( m_vector3.m_x, "0" );
638                 gtk_entry_set_text( m_vector3.m_y, "0" );
639                 gtk_entry_set_text( m_vector3.m_z, "0" );
640         }
641 }
642 typedef MemberCaller<Vector3Attribute, &Vector3Attribute::update> UpdateCaller;
643 };
644
645 class NonModalComboBox
646 {
647 Callback m_changed;
648 guint m_changedHandler;
649
650 static gboolean changed( GtkComboBox *widget, NonModalComboBox* self ){
651         self->m_changed();
652         return FALSE;
653 }
654
655 public:
656 NonModalComboBox( const Callback& changed ) : m_changed( changed ), m_changedHandler( 0 ){
657 }
658 void connect( GtkComboBox* combo ){
659         m_changedHandler = g_signal_connect( G_OBJECT( combo ), "changed", G_CALLBACK( changed ), this );
660 }
661 void setActive( GtkComboBox* combo, int value ){
662         g_signal_handler_disconnect( G_OBJECT( combo ), m_changedHandler );
663         gtk_combo_box_set_active( combo, value );
664         connect( combo );
665 }
666 };
667
668 class ListAttribute : public EntityAttribute
669 {
670 std::string m_key;
671 GtkComboBox* m_combo;
672 NonModalComboBox m_nonModal;
673 const ListAttributeType& m_type;
674 public:
675 ListAttribute( const char* key, const ListAttributeType& type ) :
676         m_key( key ),
677         m_combo( 0 ),
678         m_nonModal( ApplyCaller( *this ) ),
679         m_type( type ){
680         GtkComboBox* combo = GTK_COMBO_BOX( gtk_combo_box_new_text() );
681
682         for ( ListAttributeType::const_iterator i = type.begin(); i != type.end(); ++i )
683         {
684                 gtk_combo_box_append_text( GTK_COMBO_BOX( combo ), ( *i ).first.c_str() );
685         }
686
687         gtk_widget_show( GTK_WIDGET( combo ) );
688         m_nonModal.connect( combo );
689
690         m_combo = combo;
691 }
692 void release(){
693         delete this;
694 }
695 ui::Widget getWidget() const {
696         return ui::Widget(GTK_WIDGET( m_combo ));
697 }
698 void apply(){
699         Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), m_type[gtk_combo_box_get_active( m_combo )].second.c_str() );
700 }
701 typedef MemberCaller<ListAttribute, &ListAttribute::apply> ApplyCaller;
702
703 void update(){
704         const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
705         ListAttributeType::const_iterator i = m_type.findValue( value );
706         if ( i != m_type.end() ) {
707                 m_nonModal.setActive( m_combo, static_cast<int>( std::distance( m_type.begin(), i ) ) );
708         }
709         else
710         {
711                 m_nonModal.setActive( m_combo, 0 );
712         }
713 }
714 typedef MemberCaller<ListAttribute, &ListAttribute::update> UpdateCaller;
715 };
716
717
718 namespace
719 {
720 ui::Widget g_entity_split1;
721 ui::Widget g_entity_split2;
722 int g_entitysplit1_position;
723 int g_entitysplit2_position;
724
725 bool g_entityInspector_windowConstructed = false;
726
727 GtkTreeView* g_entityClassList;
728 GtkTextView* g_entityClassComment;
729
730 GtkCheckButton* g_entitySpawnflagsCheck[MAX_FLAGS];
731
732 GtkEntry* g_entityKeyEntry;
733 GtkEntry* g_entityValueEntry;
734
735 GtkListStore* g_entlist_store;
736 GtkListStore* g_entprops_store;
737 const EntityClass* g_current_flags = 0;
738 const EntityClass* g_current_comment = 0;
739 const EntityClass* g_current_attributes = 0;
740
741 // the number of active spawnflags
742 int g_spawnflag_count;
743 // table: index, match spawnflag item to the spawnflag index (i.e. which bit)
744 int spawn_table[MAX_FLAGS];
745 // we change the layout depending on how many spawn flags we need to display
746 // the table is a 4x4 in which we need to put the comment box g_entityClassComment and the spawn flags..
747 GtkTable* g_spawnflagsTable;
748
749 GtkVBox* g_attributeBox = 0;
750 typedef std::vector<EntityAttribute*> EntityAttributes;
751 EntityAttributes g_entityAttributes;
752 }
753
754 void GlobalEntityAttributes_clear(){
755         for ( EntityAttributes::iterator i = g_entityAttributes.begin(); i != g_entityAttributes.end(); ++i )
756         {
757                 ( *i )->release();
758         }
759         g_entityAttributes.clear();
760 }
761
762 class GetKeyValueVisitor : public Entity::Visitor
763 {
764 KeyValues& m_keyvalues;
765 public:
766 GetKeyValueVisitor( KeyValues& keyvalues )
767         : m_keyvalues( keyvalues ){
768 }
769
770 void visit( const char* key, const char* value ){
771         m_keyvalues.insert( KeyValues::value_type( std::string( key ), std::string( value ) ) );
772 }
773
774 };
775
776 void Entity_GetKeyValues( const Entity& entity, KeyValues& keyvalues, KeyValues& defaultValues ){
777         GetKeyValueVisitor visitor( keyvalues );
778
779         entity.forEachKeyValue( visitor );
780
781         const EntityClassAttributes& attributes = entity.getEntityClass().m_attributes;
782
783         for ( EntityClassAttributes::const_iterator i = attributes.begin(); i != attributes.end(); ++i )
784         {
785                 defaultValues.insert( KeyValues::value_type( ( *i ).first, ( *i ).second.m_value ) );
786         }
787 }
788
789 void Entity_GetKeyValues_Selected( KeyValues& keyvalues, KeyValues& defaultValues ){
790         class EntityGetKeyValues : public SelectionSystem::Visitor
791         {
792         KeyValues& m_keyvalues;
793         KeyValues& m_defaultValues;
794         mutable std::set<Entity*> m_visited;
795 public:
796         EntityGetKeyValues( KeyValues& keyvalues, KeyValues& defaultValues )
797                 : m_keyvalues( keyvalues ), m_defaultValues( defaultValues ){
798         }
799         void visit( scene::Instance& instance ) const {
800                 Entity* entity = Node_getEntity( instance.path().top() );
801                 if ( entity == 0 && instance.path().size() != 1 ) {
802                         entity = Node_getEntity( instance.path().parent() );
803                 }
804                 if ( entity != 0 && m_visited.insert( entity ).second ) {
805                         Entity_GetKeyValues( *entity, m_keyvalues, m_defaultValues );
806                 }
807         }
808         } visitor( keyvalues, defaultValues );
809         GlobalSelectionSystem().foreachSelected( visitor );
810 }
811
812 const char* keyvalues_valueforkey( KeyValues& keyvalues, const char* key ){
813         KeyValues::iterator i = keyvalues.find( std::string( key ) );
814         if ( i != keyvalues.end() ) {
815                 return ( *i ).second.c_str();
816         }
817         return "";
818 }
819
820 class EntityClassListStoreAppend : public EntityClassVisitor
821 {
822 GtkListStore* store;
823 public:
824 EntityClassListStoreAppend( GtkListStore* store_ ) : store( store_ ){
825 }
826 void visit( EntityClass* e ){
827         GtkTreeIter iter;
828         gtk_list_store_append( store, &iter );
829         gtk_list_store_set( store, &iter, 0, e->name(), 1, e, -1 );
830 }
831 };
832
833 void EntityClassList_fill(){
834         EntityClassListStoreAppend append( g_entlist_store );
835         GlobalEntityClassManager().forEach( append );
836 }
837
838 void EntityClassList_clear(){
839         gtk_list_store_clear( g_entlist_store );
840 }
841
842 void SetComment( EntityClass* eclass ){
843         if ( eclass == g_current_comment ) {
844                 return;
845         }
846
847         g_current_comment = eclass;
848
849         GtkTextBuffer* buffer = gtk_text_view_get_buffer( g_entityClassComment );
850         gtk_text_buffer_set_text( buffer, eclass->comments(), -1 );
851 }
852
853 void SurfaceFlags_setEntityClass( EntityClass* eclass ){
854         if ( eclass == g_current_flags ) {
855                 return;
856         }
857
858         g_current_flags = eclass;
859
860         int spawnflag_count = 0;
861
862         {
863                 // do a first pass to count the spawn flags, don't touch the widgets, we don't know in what state they are
864                 for ( int i = 0 ; i < MAX_FLAGS ; i++ )
865                 {
866                         if ( eclass->flagnames[i] && eclass->flagnames[i][0] != 0 && strcmp( eclass->flagnames[i],"-" ) ) {
867                                 spawn_table[spawnflag_count] = i;
868                                 spawnflag_count++;
869                         }
870                 }
871         }
872
873         // disable all remaining boxes
874         // NOTE: these boxes might not even be on display
875         {
876                 for ( int i = 0; i < g_spawnflag_count; ++i )
877                 {
878                         ui::Widget widget = ui::Widget(GTK_WIDGET( g_entitySpawnflagsCheck[i] ));
879                         gtk_label_set_text( GTK_LABEL( GTK_BIN( widget )->child ), " " );
880                         gtk_widget_hide( widget );
881                         gtk_widget_ref( widget );
882                         gtk_container_remove( GTK_CONTAINER( g_spawnflagsTable ), widget );
883                 }
884         }
885
886         g_spawnflag_count = spawnflag_count;
887
888         {
889                 for ( int i = 0; i < g_spawnflag_count; ++i )
890                 {
891                         ui::Widget widget = ui::Widget(GTK_WIDGET( g_entitySpawnflagsCheck[i] ));
892                         gtk_widget_show( widget );
893
894                         StringOutputStream str( 16 );
895                         str << LowerCase( eclass->flagnames[spawn_table[i]] );
896
897                         gtk_table_attach( g_spawnflagsTable, widget, i % 4, i % 4 + 1, i / 4, i / 4 + 1,
898                                                           (GtkAttachOptions)( GTK_FILL ),
899                                                           (GtkAttachOptions)( GTK_FILL ), 0, 0 );
900                         gtk_widget_unref( widget );
901
902                         gtk_label_set_text( GTK_LABEL( GTK_BIN( widget )->child ), str.c_str() );
903                 }
904         }
905 }
906
907 void EntityClassList_selectEntityClass( EntityClass* eclass ){
908         GtkTreeModel* model = GTK_TREE_MODEL( g_entlist_store );
909         GtkTreeIter iter;
910         for ( gboolean good = gtk_tree_model_get_iter_first( model, &iter ); good != FALSE; good = gtk_tree_model_iter_next( model, &iter ) )
911         {
912                 char* text;
913                 gtk_tree_model_get( model, &iter, 0, &text, -1 );
914                 if ( strcmp( text, eclass->name() ) == 0 ) {
915                         GtkTreeView* view = g_entityClassList;
916                         GtkTreePath* path = gtk_tree_model_get_path( model, &iter );
917                         gtk_tree_selection_select_path( gtk_tree_view_get_selection( view ), path );
918                         if ( GTK_WIDGET_REALIZED( view ) ) {
919                                 gtk_tree_view_scroll_to_cell( view, path, 0, FALSE, 0, 0 );
920                         }
921                         gtk_tree_path_free( path );
922                         good = FALSE;
923                 }
924                 g_free( text );
925         }
926 }
927
928 void EntityInspector_appendAttribute( const char* name, EntityAttribute& attribute ){
929         GtkTable* row = DialogRow_new( name, attribute.getWidget() );
930         DialogVBox_packRow( g_attributeBox, GTK_WIDGET( row ) );
931 }
932
933
934 template<typename Attribute>
935 class StatelessAttributeCreator
936 {
937 public:
938 static EntityAttribute* create( const char* name ){
939         return new Attribute( name );
940 }
941 };
942
943 class EntityAttributeFactory
944 {
945 typedef EntityAttribute* ( *CreateFunc )( const char* name );
946 typedef std::map<const char*, CreateFunc, RawStringLess> Creators;
947 Creators m_creators;
948 public:
949 EntityAttributeFactory(){
950         m_creators.insert( Creators::value_type( "string", &StatelessAttributeCreator<StringAttribute>::create ) );
951         m_creators.insert( Creators::value_type( "color", &StatelessAttributeCreator<StringAttribute>::create ) );
952         m_creators.insert( Creators::value_type( "integer", &StatelessAttributeCreator<StringAttribute>::create ) );
953         m_creators.insert( Creators::value_type( "real", &StatelessAttributeCreator<StringAttribute>::create ) );
954         m_creators.insert( Creators::value_type( "shader", &StatelessAttributeCreator<ShaderAttribute>::create ) );
955         m_creators.insert( Creators::value_type( "boolean", &StatelessAttributeCreator<BooleanAttribute>::create ) );
956         m_creators.insert( Creators::value_type( "angle", &StatelessAttributeCreator<AngleAttribute>::create ) );
957         m_creators.insert( Creators::value_type( "direction", &StatelessAttributeCreator<DirectionAttribute>::create ) );
958         m_creators.insert( Creators::value_type( "angles", &StatelessAttributeCreator<AnglesAttribute>::create ) );
959         m_creators.insert( Creators::value_type( "model", &StatelessAttributeCreator<ModelAttribute>::create ) );
960         m_creators.insert( Creators::value_type( "sound", &StatelessAttributeCreator<SoundAttribute>::create ) );
961         m_creators.insert( Creators::value_type( "vector3", &StatelessAttributeCreator<Vector3Attribute>::create ) );
962         m_creators.insert( Creators::value_type( "real3", &StatelessAttributeCreator<Vector3Attribute>::create ) );
963 }
964 EntityAttribute* create( const char* type, const char* name ){
965         Creators::iterator i = m_creators.find( type );
966         if ( i != m_creators.end() ) {
967                 return ( *i ).second( name );
968         }
969         const ListAttributeType* listType = GlobalEntityClassManager().findListType( type );
970         if ( listType != 0 ) {
971                 return new ListAttribute( name, *listType );
972         }
973         return 0;
974 }
975 };
976
977 typedef Static<EntityAttributeFactory> GlobalEntityAttributeFactory;
978
979 void EntityInspector_setEntityClass( EntityClass *eclass ){
980         EntityClassList_selectEntityClass( eclass );
981         SurfaceFlags_setEntityClass( eclass );
982
983         if ( eclass != g_current_attributes ) {
984                 g_current_attributes = eclass;
985
986                 container_remove_all( GTK_CONTAINER( g_attributeBox ) );
987                 GlobalEntityAttributes_clear();
988
989                 for ( EntityClassAttributes::const_iterator i = eclass->m_attributes.begin(); i != eclass->m_attributes.end(); ++i )
990                 {
991                         EntityAttribute* attribute = GlobalEntityAttributeFactory::instance().create( ( *i ).second.m_type.c_str(), ( *i ).first.c_str() );
992                         if ( attribute != 0 ) {
993                                 g_entityAttributes.push_back( attribute );
994                                 EntityInspector_appendAttribute( EntityClassAttributePair_getName( *i ), *g_entityAttributes.back() );
995                         }
996                 }
997         }
998 }
999
1000 void EntityInspector_updateSpawnflags(){
1001         {
1002                 int f = atoi( SelectedEntity_getValueForKey( "spawnflags" ) );
1003                 for ( int i = 0; i < g_spawnflag_count; ++i )
1004                 {
1005                         int v = !!( f & ( 1 << spawn_table[i] ) );
1006
1007                         toggle_button_set_active_no_signal( GTK_TOGGLE_BUTTON( g_entitySpawnflagsCheck[i] ), v );
1008                 }
1009         }
1010         {
1011                 // take care of the remaining ones
1012                 for ( int i = g_spawnflag_count; i < MAX_FLAGS; ++i )
1013                 {
1014                         toggle_button_set_active_no_signal( GTK_TOGGLE_BUTTON( g_entitySpawnflagsCheck[i] ), FALSE );
1015                 }
1016         }
1017 }
1018
1019 void EntityInspector_applySpawnflags(){
1020         int f, i, v;
1021         char sz[32];
1022
1023         f = 0;
1024         for ( i = 0; i < g_spawnflag_count; ++i )
1025         {
1026                 v = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( g_entitySpawnflagsCheck[i] ) );
1027                 f |= v << spawn_table[i];
1028         }
1029
1030         sprintf( sz, "%i", f );
1031         const char* value = ( f == 0 ) ? "" : sz;
1032
1033         {
1034                 StringOutputStream command;
1035                 command << "entitySetFlags -flags " << f;
1036                 UndoableCommand undo( "entitySetSpawnflags" );
1037
1038                 Scene_EntitySetKeyValue_Selected( "spawnflags", value );
1039         }
1040 }
1041
1042
1043 void EntityInspector_updateKeyValues(){
1044         g_selectedKeyValues.clear();
1045         g_selectedDefaultKeyValues.clear();
1046         Entity_GetKeyValues_Selected( g_selectedKeyValues, g_selectedDefaultKeyValues );
1047
1048         EntityInspector_setEntityClass( GlobalEntityClassManager().findOrInsert( keyvalues_valueforkey( g_selectedKeyValues, "classname" ), false ) );
1049
1050         EntityInspector_updateSpawnflags();
1051
1052         GtkListStore* store = g_entprops_store;
1053
1054         // save current key/val pair around filling epair box
1055         // row_select wipes it and sets to first in list
1056         std::string strKey( gtk_entry_get_text( g_entityKeyEntry ) );
1057         std::string strVal( gtk_entry_get_text( g_entityValueEntry ) );
1058
1059         gtk_list_store_clear( store );
1060         // Walk through list and add pairs
1061         for ( KeyValues::iterator i = g_selectedKeyValues.begin(); i != g_selectedKeyValues.end(); ++i )
1062         {
1063                 GtkTreeIter iter;
1064                 gtk_list_store_append( store, &iter );
1065                 StringOutputStream key( 64 );
1066                 key << ( *i ).first.c_str();
1067                 StringOutputStream value( 64 );
1068                 value << ( *i ).second.c_str();
1069                 gtk_list_store_set( store, &iter, 0, key.c_str(), 1, value.c_str(), -1 );
1070         }
1071
1072         gtk_entry_set_text( g_entityKeyEntry, strKey.c_str() );
1073         gtk_entry_set_text( g_entityValueEntry, strVal.c_str() );
1074
1075         for ( EntityAttributes::const_iterator i = g_entityAttributes.begin(); i != g_entityAttributes.end(); ++i )
1076         {
1077                 ( *i )->update();
1078         }
1079 }
1080
1081 class EntityInspectorDraw
1082 {
1083 IdleDraw m_idleDraw;
1084 public:
1085 EntityInspectorDraw() : m_idleDraw( FreeCaller<EntityInspector_updateKeyValues>( ) ){
1086 }
1087 void queueDraw(){
1088         m_idleDraw.queueDraw();
1089 }
1090 };
1091
1092 EntityInspectorDraw g_EntityInspectorDraw;
1093
1094
1095 void EntityInspector_keyValueChanged(){
1096         g_EntityInspectorDraw.queueDraw();
1097 }
1098 void EntityInspector_selectionChanged( const Selectable& ){
1099         EntityInspector_keyValueChanged();
1100 }
1101
1102 // Creates a new entity based on the currently selected brush and entity type.
1103 //
1104 void EntityClassList_createEntity(){
1105         GtkTreeView* view = g_entityClassList;
1106
1107         // find out what type of entity we are trying to create
1108         GtkTreeModel* model;
1109         GtkTreeIter iter;
1110         if ( gtk_tree_selection_get_selected( gtk_tree_view_get_selection( view ), &model, &iter ) == FALSE ) {
1111                 ui::Widget(gtk_widget_get_toplevel( GTK_WIDGET( g_entityClassList ) )).alert( "You must have a selected class to create an entity", "info" );
1112                 return;
1113         }
1114
1115         char* text;
1116         gtk_tree_model_get( model, &iter, 0, &text, -1 );
1117
1118         {
1119                 StringOutputStream command;
1120                 command << "entityCreate -class " << text;
1121
1122                 UndoableCommand undo( command.c_str() );
1123
1124                 Entity_createFromSelection( text, g_vector3_identity );
1125         }
1126         g_free( text );
1127 }
1128
1129 void EntityInspector_applyKeyValue(){
1130         // Get current selection text
1131         StringOutputStream key( 64 );
1132         key << gtk_entry_get_text( g_entityKeyEntry );
1133         StringOutputStream value( 64 );
1134         value << gtk_entry_get_text( g_entityValueEntry );
1135
1136
1137         // TTimo: if you change the classname to worldspawn you won't merge back in the structural brushes but create a parasite entity
1138         if ( !strcmp( key.c_str(), "classname" ) && !strcmp( value.c_str(), "worldspawn" ) ) {
1139                 ui::Widget(gtk_widget_get_toplevel( GTK_WIDGET( g_entityKeyEntry )) ).alert( "Cannot change \"classname\" key back to worldspawn.", 0, ui::alert_type::OK );
1140                 return;
1141         }
1142
1143
1144         // RR2DO2: we don't want spaces in entity keys
1145         if ( strstr( key.c_str(), " " ) ) {
1146                 ui::Widget(gtk_widget_get_toplevel( GTK_WIDGET( g_entityKeyEntry )) ).alert( "No spaces are allowed in entity keys.", 0, ui::alert_type::OK );
1147                 return;
1148         }
1149
1150         if ( strcmp( key.c_str(), "classname" ) == 0 ) {
1151                 StringOutputStream command;
1152                 command << "entitySetClass -class " << value.c_str();
1153                 UndoableCommand undo( command.c_str() );
1154                 Scene_EntitySetClassname_Selected( value.c_str() );
1155         }
1156         else
1157         {
1158                 Scene_EntitySetKeyValue_Selected_Undoable( key.c_str(), value.c_str() );
1159         }
1160 }
1161
1162 void EntityInspector_clearKeyValue(){
1163         // Get current selection text
1164         StringOutputStream key( 64 );
1165         key << gtk_entry_get_text( g_entityKeyEntry );
1166
1167         if ( strcmp( key.c_str(), "classname" ) != 0 ) {
1168                 StringOutputStream command;
1169                 command << "entityDeleteKey -key " << key.c_str();
1170                 UndoableCommand undo( command.c_str() );
1171                 Scene_EntitySetKeyValue_Selected( key.c_str(), "" );
1172         }
1173 }
1174
1175 void EntityInspector_clearAllKeyValues(){
1176         UndoableCommand undo( "entityClear" );
1177
1178         // remove all keys except classname
1179         for ( KeyValues::iterator i = g_selectedKeyValues.begin(); i != g_selectedKeyValues.end(); ++i )
1180         {
1181                 if ( strcmp( ( *i ).first.c_str(), "classname" ) != 0 ) {
1182                         Scene_EntitySetKeyValue_Selected( ( *i ).first.c_str(), "" );
1183                 }
1184         }
1185 }
1186
1187 // =============================================================================
1188 // callbacks
1189
1190 static void EntityClassList_selection_changed( GtkTreeSelection* selection, gpointer data ){
1191         GtkTreeModel* model;
1192         GtkTreeIter selected;
1193         if ( gtk_tree_selection_get_selected( selection, &model, &selected ) ) {
1194                 EntityClass* eclass;
1195                 gtk_tree_model_get( model, &selected, 1, &eclass, -1 );
1196                 if ( eclass != 0 ) {
1197                         SetComment( eclass );
1198                 }
1199         }
1200 }
1201
1202 static gint EntityClassList_button_press( ui::Widget widget, GdkEventButton *event, gpointer data ){
1203         if ( event->type == GDK_2BUTTON_PRESS ) {
1204                 EntityClassList_createEntity();
1205                 return TRUE;
1206         }
1207         return FALSE;
1208 }
1209
1210 static gint EntityClassList_keypress( ui::Widget widget, GdkEventKey* event, gpointer data ){
1211         unsigned int code = gdk_keyval_to_upper( event->keyval );
1212
1213         if ( event->keyval == GDK_Return ) {
1214                 EntityClassList_createEntity();
1215                 return TRUE;
1216         }
1217
1218         // select the entity that starts with the key pressed
1219         if ( code <= 'Z' && code >= 'A' ) {
1220                 GtkTreeView* view = g_entityClassList;
1221                 GtkTreeModel* model;
1222                 GtkTreeIter iter;
1223                 if ( gtk_tree_selection_get_selected( gtk_tree_view_get_selection( view ), &model, &iter ) == FALSE
1224                          || gtk_tree_model_iter_next( model, &iter ) == FALSE ) {
1225                         gtk_tree_model_get_iter_first( model, &iter );
1226                 }
1227
1228                 for ( std::size_t count = gtk_tree_model_iter_n_children( model, 0 ); count > 0; --count )
1229                 {
1230                         char* text;
1231                         gtk_tree_model_get( model, &iter, 0, &text, -1 );
1232
1233                         if ( toupper( text[0] ) == (int)code ) {
1234                                 GtkTreePath* path = gtk_tree_model_get_path( model, &iter );
1235                                 gtk_tree_selection_select_path( gtk_tree_view_get_selection( view ), path );
1236                                 if ( GTK_WIDGET_REALIZED( view ) ) {
1237                                         gtk_tree_view_scroll_to_cell( view, path, 0, FALSE, 0, 0 );
1238                                 }
1239                                 gtk_tree_path_free( path );
1240                                 count = 1;
1241                         }
1242
1243                         g_free( text );
1244
1245                         if ( gtk_tree_model_iter_next( model, &iter ) == FALSE ) {
1246                                 gtk_tree_model_get_iter_first( model, &iter );
1247                         }
1248                 }
1249
1250                 return TRUE;
1251         }
1252         return FALSE;
1253 }
1254
1255 static void EntityProperties_selection_changed( GtkTreeSelection* selection, gpointer data ){
1256         // find out what type of entity we are trying to create
1257         GtkTreeModel* model;
1258         GtkTreeIter iter;
1259         if ( gtk_tree_selection_get_selected( selection, &model, &iter ) == FALSE ) {
1260                 return;
1261         }
1262
1263         char* key;
1264         char* val;
1265         gtk_tree_model_get( model, &iter, 0, &key, 1, &val, -1 );
1266
1267         gtk_entry_set_text( g_entityKeyEntry, key );
1268         gtk_entry_set_text( g_entityValueEntry, val );
1269
1270         g_free( key );
1271         g_free( val );
1272 }
1273
1274 static void SpawnflagCheck_toggled( ui::Widget widget, gpointer data ){
1275         EntityInspector_applySpawnflags();
1276 }
1277
1278 static gint EntityEntry_keypress( GtkEntry* widget, GdkEventKey* event, gpointer data ){
1279         if ( event->keyval == GDK_Return ) {
1280                 if ( widget == g_entityKeyEntry ) {
1281                         gtk_entry_set_text( g_entityValueEntry, "" );
1282                         gtk_window_set_focus( GTK_WINDOW( gtk_widget_get_toplevel( GTK_WIDGET( widget ) ) ), GTK_WIDGET( g_entityValueEntry ) );
1283                 }
1284                 else
1285                 {
1286                         EntityInspector_applyKeyValue();
1287                 }
1288                 return TRUE;
1289         }
1290         if ( event->keyval == GDK_Escape ) {
1291                 gtk_window_set_focus( GTK_WINDOW( gtk_widget_get_toplevel( GTK_WIDGET( widget ) ) ), NULL );
1292                 return TRUE;
1293         }
1294
1295         return FALSE;
1296 }
1297
1298 void EntityInspector_destroyWindow( ui::Widget widget, gpointer data ){
1299         g_entitysplit1_position = gtk_paned_get_position( GTK_PANED( g_entity_split1 ) );
1300         g_entitysplit2_position = gtk_paned_get_position( GTK_PANED( g_entity_split2 ) );
1301
1302         g_entityInspector_windowConstructed = false;
1303         GlobalEntityAttributes_clear();
1304 }
1305
1306 ui::Widget EntityInspector_constructWindow( ui::Window toplevel ){
1307         ui::Widget vbox = ui::Widget(gtk_vbox_new( FALSE, 2 ));
1308         gtk_widget_show( vbox );
1309         gtk_container_set_border_width( GTK_CONTAINER( vbox ), 2 );
1310
1311         g_signal_connect( G_OBJECT( vbox ), "destroy", G_CALLBACK( EntityInspector_destroyWindow ), 0 );
1312
1313         {
1314                 ui::Widget split1 = ui::Widget(gtk_vpaned_new());
1315                 gtk_box_pack_start( GTK_BOX( vbox ), split1, TRUE, TRUE, 0 );
1316                 gtk_widget_show( split1 );
1317
1318                 g_entity_split1 = split1;
1319
1320                 {
1321                         ui::Widget split2 = ui::Widget(gtk_vpaned_new());
1322                         gtk_paned_add1( GTK_PANED( split1 ), split2 );
1323                         gtk_widget_show( split2 );
1324
1325                         g_entity_split2 = split2;
1326
1327                         {
1328                                 // class list
1329                                 ui::Widget scr = ui::Widget(gtk_scrolled_window_new( 0, 0 ));
1330                                 gtk_widget_show( scr );
1331                                 gtk_paned_add1( GTK_PANED( split2 ), scr );
1332                                 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
1333                                 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
1334
1335                                 {
1336                                         GtkListStore* store = gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_POINTER );
1337
1338                                         GtkTreeView* view = GTK_TREE_VIEW( gtk_tree_view_new_with_model( GTK_TREE_MODEL( store ) ) );
1339                                         gtk_tree_view_set_enable_search( GTK_TREE_VIEW( view ), FALSE );
1340                                         gtk_tree_view_set_headers_visible( view, FALSE );
1341                                         g_signal_connect( G_OBJECT( view ), "button_press_event", G_CALLBACK( EntityClassList_button_press ), 0 );
1342                                         g_signal_connect( G_OBJECT( view ), "key_press_event", G_CALLBACK( EntityClassList_keypress ), 0 );
1343
1344                                         {
1345                                                 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
1346                                                 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes( "Key", renderer, "text", 0, 0 );
1347                                                 gtk_tree_view_append_column( view, column );
1348                                         }
1349
1350                                         {
1351                                                 GtkTreeSelection* selection = gtk_tree_view_get_selection( view );
1352                                                 g_signal_connect( G_OBJECT( selection ), "changed", G_CALLBACK( EntityClassList_selection_changed ), 0 );
1353                                         }
1354
1355                                         gtk_widget_show( GTK_WIDGET( view ) );
1356
1357                                         gtk_container_add( GTK_CONTAINER( scr ), GTK_WIDGET( view ) );
1358
1359                                         g_object_unref( G_OBJECT( store ) );
1360                                         g_entityClassList = view;
1361                                         g_entlist_store = store;
1362                                 }
1363                         }
1364
1365                         {
1366                                 ui::Widget scr = ui::Widget(gtk_scrolled_window_new( 0, 0 ));
1367                                 gtk_widget_show( scr );
1368                                 gtk_paned_add2( GTK_PANED( split2 ), scr );
1369                                 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
1370                                 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
1371
1372                                 {
1373                                         GtkTextView* text = GTK_TEXT_VIEW( gtk_text_view_new() );
1374                                         gtk_widget_set_size_request( GTK_WIDGET( text ), 0, -1 ); // allow shrinking
1375                                         gtk_text_view_set_wrap_mode( text, GTK_WRAP_WORD );
1376                                         gtk_text_view_set_editable( text, FALSE );
1377                                         gtk_widget_show( GTK_WIDGET( text ) );
1378                                         gtk_container_add( GTK_CONTAINER( scr ), GTK_WIDGET( text ) );
1379                                         g_entityClassComment = text;
1380                                 }
1381                         }
1382                 }
1383
1384                 {
1385                         ui::Widget split2 = ui::Widget(gtk_vpaned_new());
1386                         gtk_paned_add2( GTK_PANED( split1 ), split2 );
1387                         gtk_widget_show( split2 );
1388
1389                         {
1390                                 ui::Widget vbox2 = ui::Widget(gtk_vbox_new( FALSE, 2 ));
1391                                 gtk_widget_show( vbox2 );
1392                                 gtk_paned_pack1( GTK_PANED( split2 ), vbox2, FALSE, FALSE );
1393
1394                                 {
1395                                         // Spawnflags (4 colums wide max, or window gets too wide.)
1396                                         GtkTable* table = GTK_TABLE( gtk_table_new( 4, 4, FALSE ) );
1397                                         gtk_box_pack_start( GTK_BOX( vbox2 ), GTK_WIDGET( table ), FALSE, TRUE, 0 );
1398                                         gtk_widget_show( GTK_WIDGET( table ) );
1399
1400                                         g_spawnflagsTable = table;
1401
1402                                         for ( int i = 0; i < MAX_FLAGS; i++ )
1403                                         {
1404                                                 GtkCheckButton* check = GTK_CHECK_BUTTON( gtk_check_button_new_with_label( "" ) );
1405                                                 gtk_widget_ref( GTK_WIDGET( check ) );
1406                                                 g_object_set_data( G_OBJECT( check ), "handler", gint_to_pointer( g_signal_connect( G_OBJECT( check ), "toggled", G_CALLBACK( SpawnflagCheck_toggled ), 0 ) ) );
1407                                                 g_entitySpawnflagsCheck[i] = check;
1408                                         }
1409                                 }
1410
1411                                 {
1412                                         // key/value list
1413                                         ui::Widget scr = ui::Widget(gtk_scrolled_window_new( 0, 0 ));
1414                                         gtk_widget_show( scr );
1415                                         gtk_box_pack_start( GTK_BOX( vbox2 ), scr, TRUE, TRUE, 0 );
1416                                         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
1417                                         gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
1418
1419                                         {
1420                                                 GtkListStore* store = gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_STRING );
1421
1422                                                 ui::Widget view = ui::TreeView(ui::TreeModel( GTK_TREE_MODEL( store ) ));
1423                                                 gtk_tree_view_set_enable_search( GTK_TREE_VIEW( view ), FALSE );
1424                                                 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW( view ), FALSE );
1425
1426                                                 {
1427                                                         GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
1428                                                         GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes( "", renderer, "text", 0, 0 );
1429                                                         gtk_tree_view_append_column( GTK_TREE_VIEW( view ), column );
1430                                                 }
1431
1432                                                 {
1433                                                         GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
1434                                                         GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes( "", renderer, "text", 1, 0 );
1435                                                         gtk_tree_view_append_column( GTK_TREE_VIEW( view ), column );
1436                                                 }
1437
1438                                                 {
1439                                                         GtkTreeSelection* selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( view ) );
1440                                                         g_signal_connect( G_OBJECT( selection ), "changed", G_CALLBACK( EntityProperties_selection_changed ), 0 );
1441                                                 }
1442
1443                                                 gtk_widget_show( view );
1444
1445                                                 gtk_container_add( GTK_CONTAINER( scr ), view );
1446
1447                                                 g_object_unref( G_OBJECT( store ) );
1448
1449                                                 g_entprops_store = store;
1450                                         }
1451                                 }
1452
1453                                 {
1454                                         // key/value entry
1455                                         GtkTable* table = GTK_TABLE( gtk_table_new( 2, 2, FALSE ) );
1456                                         gtk_widget_show( GTK_WIDGET( table ) );
1457                                         gtk_box_pack_start( GTK_BOX( vbox2 ), GTK_WIDGET( table ), FALSE, TRUE, 0 );
1458                                         gtk_table_set_row_spacings( table, 3 );
1459                                         gtk_table_set_col_spacings( table, 5 );
1460
1461                                         {
1462                                                 GtkEntry* entry = GTK_ENTRY( gtk_entry_new() );
1463                                                 gtk_widget_show( GTK_WIDGET( entry ) );
1464                                                 gtk_table_attach( table, GTK_WIDGET( entry ), 1, 2, 0, 1,
1465                                                                                   (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1466                                                                                   (GtkAttachOptions)( 0 ), 0, 0 );
1467                                                 gtk_widget_set_events( GTK_WIDGET( entry ), GDK_KEY_PRESS_MASK );
1468                                                 g_signal_connect( G_OBJECT( entry ), "key_press_event", G_CALLBACK( EntityEntry_keypress ), 0 );
1469                                                 g_entityKeyEntry = entry;
1470                                         }
1471
1472                                         {
1473                                                 GtkEntry* entry = GTK_ENTRY( gtk_entry_new() );
1474                                                 gtk_widget_show( GTK_WIDGET( entry ) );
1475                                                 gtk_table_attach( table, GTK_WIDGET( entry ), 1, 2, 1, 2,
1476                                                                                   (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1477                                                                                   (GtkAttachOptions)( 0 ), 0, 0 );
1478                                                 gtk_widget_set_events( GTK_WIDGET( entry ), GDK_KEY_PRESS_MASK );
1479                                                 g_signal_connect( G_OBJECT( entry ), "key_press_event", G_CALLBACK( EntityEntry_keypress ), 0 );
1480                                                 g_entityValueEntry = entry;
1481                                         }
1482
1483                                         {
1484                                                 GtkLabel* label = GTK_LABEL( ui::Label( "Value" ) );
1485                                                 gtk_widget_show( GTK_WIDGET( label ) );
1486                                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 1, 2,
1487                                                                                   (GtkAttachOptions)( GTK_FILL ),
1488                                                                                   (GtkAttachOptions)( 0 ), 0, 0 );
1489                                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1490                                         }
1491
1492                                         {
1493                                                 GtkLabel* label = GTK_LABEL( ui::Label( "Key" ) );
1494                                                 gtk_widget_show( GTK_WIDGET( label ) );
1495                                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
1496                                                                                   (GtkAttachOptions)( GTK_FILL ),
1497                                                                                   (GtkAttachOptions)( 0 ), 0, 0 );
1498                                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1499                                         }
1500                                 }
1501
1502                                 {
1503                                         GtkBox* hbox = GTK_BOX( gtk_hbox_new( TRUE, 4 ) );
1504                                         gtk_widget_show( GTK_WIDGET( hbox ) );
1505                                         gtk_box_pack_start( GTK_BOX( vbox2 ), GTK_WIDGET( hbox ), FALSE, TRUE, 0 );
1506
1507                                         {
1508                                                 GtkButton* button = GTK_BUTTON( gtk_button_new_with_label( "Clear All" ) );
1509                                                 gtk_widget_show( GTK_WIDGET( button ) );
1510                                                 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( EntityInspector_clearAllKeyValues ), 0 );
1511                                                 gtk_box_pack_start( hbox, GTK_WIDGET( button ), TRUE, TRUE, 0 );
1512                                         }
1513                                         {
1514                                                 GtkButton* button = GTK_BUTTON( gtk_button_new_with_label( "Delete Key" ) );
1515                                                 gtk_widget_show( GTK_WIDGET( button ) );
1516                                                 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( EntityInspector_clearKeyValue ), 0 );
1517                                                 gtk_box_pack_start( hbox, GTK_WIDGET( button ), TRUE, TRUE, 0 );
1518                                         }
1519                                 }
1520                         }
1521
1522                         {
1523                                 ui::Widget scr = ui::Widget(gtk_scrolled_window_new( 0, 0 ));
1524                                 gtk_widget_show( scr );
1525                                 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
1526
1527                                 ui::Widget viewport = ui::Widget(gtk_viewport_new( 0, 0 ));
1528                                 gtk_widget_show( viewport );
1529                                 gtk_viewport_set_shadow_type( GTK_VIEWPORT( viewport ), GTK_SHADOW_NONE );
1530
1531                                 g_attributeBox = GTK_VBOX( gtk_vbox_new( FALSE, 2 ) );
1532                                 gtk_widget_show( GTK_WIDGET( g_attributeBox ) );
1533
1534                                 gtk_container_add( GTK_CONTAINER( viewport ), GTK_WIDGET( g_attributeBox ) );
1535                                 gtk_container_add( GTK_CONTAINER( scr ), viewport );
1536                                 gtk_paned_pack2( GTK_PANED( split2 ), scr, FALSE, FALSE );
1537                         }
1538                 }
1539         }
1540
1541
1542         {
1543                 // show the sliders in any case
1544                 if ( g_entitysplit2_position > 22 ) {
1545                         gtk_paned_set_position( GTK_PANED( g_entity_split2 ), g_entitysplit2_position );
1546                 }
1547                 else {
1548                         g_entitysplit2_position = 22;
1549                         gtk_paned_set_position( GTK_PANED( g_entity_split2 ), 22 );
1550                 }
1551                 if ( ( g_entitysplit1_position - g_entitysplit2_position ) > 27 ) {
1552                         gtk_paned_set_position( GTK_PANED( g_entity_split1 ), g_entitysplit1_position );
1553                 }
1554                 else {
1555                         gtk_paned_set_position( GTK_PANED( g_entity_split1 ), g_entitysplit2_position + 27 );
1556                 }
1557         }
1558
1559         g_entityInspector_windowConstructed = true;
1560         EntityClassList_fill();
1561
1562         typedef FreeCaller1<const Selectable&, EntityInspector_selectionChanged> EntityInspectorSelectionChangedCaller;
1563         GlobalSelectionSystem().addSelectionChangeCallback( EntityInspectorSelectionChangedCaller() );
1564         GlobalEntityCreator().setKeyValueChangedFunc( EntityInspector_keyValueChanged );
1565
1566         // hack
1567         gtk_container_set_focus_chain( GTK_CONTAINER( vbox ), NULL );
1568
1569         return vbox;
1570 }
1571
1572 class EntityInspector : public ModuleObserver
1573 {
1574 std::size_t m_unrealised;
1575 public:
1576 EntityInspector() : m_unrealised( 1 ){
1577 }
1578 void realise(){
1579         if ( --m_unrealised == 0 ) {
1580                 if ( g_entityInspector_windowConstructed ) {
1581                         //globalOutputStream() << "Entity Inspector: realise\n";
1582                         EntityClassList_fill();
1583                 }
1584         }
1585 }
1586 void unrealise(){
1587         if ( ++m_unrealised == 1 ) {
1588                 if ( g_entityInspector_windowConstructed ) {
1589                         //globalOutputStream() << "Entity Inspector: unrealise\n";
1590                         EntityClassList_clear();
1591                 }
1592         }
1593 }
1594 };
1595
1596 EntityInspector g_EntityInspector;
1597
1598 #include "preferencesystem.h"
1599 #include "stringio.h"
1600
1601 void EntityInspector_construct(){
1602         GlobalEntityClassManager().attach( g_EntityInspector );
1603
1604         GlobalPreferenceSystem().registerPreference( "EntitySplit1", IntImportStringCaller( g_entitysplit1_position ), IntExportStringCaller( g_entitysplit1_position ) );
1605         GlobalPreferenceSystem().registerPreference( "EntitySplit2", IntImportStringCaller( g_entitysplit2_position ), IntExportStringCaller( g_entitysplit2_position ) );
1606
1607 }
1608
1609 void EntityInspector_destroy(){
1610         GlobalEntityClassManager().detach( g_EntityInspector );
1611 }
1612
1613 const char *EntityInspector_getCurrentKey(){
1614         if ( !GroupDialog_isShown() ) {
1615                 return 0;
1616         }
1617         if ( GroupDialog_getPage() != g_page_entity ) {
1618                 return 0;
1619         }
1620         return gtk_entry_get_text( g_entityKeyEntry );
1621 }