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