]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/entityinspector.cpp
fixed doom3 func_static creation bugs
[xonotic/netradiant.git] / radiant / entityinspector.cpp
1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "entityinspector.h"
23
24 #include "debugging/debugging.h"
25
26 #include "ientity.h"
27 #include "ifilesystem.h"
28 #include "imodel.h"
29 #include "iscenegraph.h"
30 #include "iselection.h"
31 #include "iundo.h"
32
33 #include <map>
34 #include <set>
35 #include <gdk/gdkkeysyms.h>
36 #include <gtk/gtktreemodel.h>
37 #include <gtk/gtktreeview.h>
38 #include <gtk/gtkcellrenderertext.h>
39 #include <gtk/gtktreeselection.h>
40 #include <gtk/gtkliststore.h>
41 #include <gtk/gtktextview.h>
42 #include <gtk/gtklabel.h>
43 #include <gtk/gtktable.h>
44 #include <gtk/gtktogglebutton.h>
45 #include <gtk/gtkcheckbutton.h>
46 #include <gtk/gtkhbox.h>
47 #include <gtk/gtkvbox.h>
48 #include <gtk/gtkvpaned.h>
49 #include <gtk/gtkscrolledwindow.h>
50 #include <gtk/gtkentry.h>
51 #include <gtk/gtkcombobox.h>
52
53
54 #include "os/path.h"
55 #include "eclasslib.h"
56 #include "scenelib.h"
57 #include "generic/callback.h"
58 #include "os/file.h"
59 #include "stream/stringstream.h"
60 #include "moduleobserver.h"
61 #include "convert.h"
62 #include "stringio.h"
63
64 #include "gtkutil/accelerator.h"
65 #include "gtkutil/dialog.h"
66 #include "gtkutil/filechooser.h"
67 #include "gtkutil/messagebox.h"
68 #include "gtkutil/nonmodal.h"
69 #include "gtkutil/button.h"
70 #include "gtkutil/entry.h"
71 #include "gtkutil/container.h"
72
73 #include "qe3.h"
74 #include "gtkmisc.h"
75 #include "gtkdlgs.h"
76 #include "entity.h"
77 #include "mainframe.h"
78 #include "textureentry.h"
79
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 << ConvertUTF8ToLocale(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 << ConvertLocaleToUTF8(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 << ConvertUTF8ToLocale(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 << ConvertLocaleToUTF8(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(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 << ConvertUTF8ToLocale(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 << ConvertLocaleToUTF8(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   }
1062   EntityAttribute* create(const char* type, const char* name)
1063   {
1064     Creators::iterator i = m_creators.find(type);
1065     if(i != m_creators.end())
1066     {
1067       return (*i).second(name);
1068     }
1069     const ListAttributeType* listType = GlobalEntityClassManager().findListType(type);
1070     if(listType != 0)
1071     {
1072       return new ListAttribute(name, *listType);
1073     }
1074     return 0;
1075   }
1076 };
1077
1078 typedef Static<EntityAttributeFactory> GlobalEntityAttributeFactory;
1079
1080 void EntityInspector_setEntityClass(EntityClass *eclass)
1081 {
1082   EntityClassList_selectEntityClass(eclass);
1083   SurfaceFlags_setEntityClass(eclass);
1084
1085   if(eclass != g_current_attributes)
1086   {
1087     g_current_attributes = eclass;
1088
1089     container_remove_all(GTK_CONTAINER(g_attributeBox));
1090     GlobalEntityAttributes_clear();
1091
1092     for(EntityClassAttributes::const_iterator i = eclass->m_attributes.begin(); i != eclass->m_attributes.end(); ++i)
1093     {
1094       EntityAttribute* attribute = GlobalEntityAttributeFactory::instance().create((*i).second.m_type.c_str(), (*i).first.c_str());
1095       if(attribute != 0)
1096       {
1097         g_entityAttributes.push_back(attribute);
1098         EntityInspector_appendAttribute(EntityClassAttributePair_getName(*i), *g_entityAttributes.back());
1099       }
1100     }
1101   }
1102 }
1103
1104 void EntityInspector_updateSpawnflags()
1105 {
1106   {
1107     int f = atoi(SelectedEntity_getValueForKey("spawnflags"));
1108     for (int i = 0; i < g_spawnflag_count; ++i)
1109     {
1110       int v = !!(f&(1<<spawn_table[i]));
1111       
1112       toggle_button_set_active_no_signal(GTK_TOGGLE_BUTTON(g_entitySpawnflagsCheck[i]), v);
1113     }
1114   }
1115   {
1116     // take care of the remaining ones
1117     for (int i = g_spawnflag_count; i < MAX_FLAGS; ++i)
1118     {
1119       toggle_button_set_active_no_signal(GTK_TOGGLE_BUTTON(g_entitySpawnflagsCheck[i]), FALSE);
1120     }
1121   }
1122 }
1123
1124 void EntityInspector_applySpawnflags()
1125 {
1126   int f, i, v;
1127   char sz[32];
1128
1129   f = 0;
1130   for (i = 0; i < g_spawnflag_count; ++i)
1131   {
1132     v = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (g_entitySpawnflagsCheck[i]));
1133     f |= v<<spawn_table[i];
1134   }
1135
1136   sprintf (sz, "%i", f);    
1137   const char* value = (f == 0) ? "" : sz;
1138
1139   {
1140     StringOutputStream command;
1141     command << "entitySetFlags -flags " << f;
1142     UndoableCommand undo("entitySetSpawnflags");
1143
1144     Scene_EntitySetKeyValue_Selected("spawnflags", value);
1145   }
1146 }
1147
1148
1149 void EntityInspector_updateKeyValues()
1150 {
1151   g_selectedKeyValues.clear();
1152   g_selectedDefaultKeyValues.clear();
1153   Entity_GetKeyValues_Selected(g_selectedKeyValues, g_selectedDefaultKeyValues);
1154
1155   EntityInspector_setEntityClass(GlobalEntityClassManager().findOrInsert(keyvalues_valueforkey(g_selectedKeyValues, "classname"), false));
1156
1157   EntityInspector_updateSpawnflags();
1158
1159   GtkListStore* store = g_entprops_store;
1160
1161   // save current key/val pair around filling epair box
1162   // row_select wipes it and sets to first in list
1163   CopiedString strKey(gtk_entry_get_text(g_entityKeyEntry));
1164   CopiedString strVal(gtk_entry_get_text(g_entityValueEntry));
1165
1166   gtk_list_store_clear(store);
1167   // Walk through list and add pairs
1168   for(KeyValues::iterator i = g_selectedKeyValues.begin(); i != g_selectedKeyValues.end(); ++i)
1169   {
1170     GtkTreeIter iter;
1171     gtk_list_store_append(store, &iter);
1172     StringOutputStream key(64);
1173     key << ConvertLocaleToUTF8((*i).first.c_str());
1174     StringOutputStream value(64);
1175     value << ConvertLocaleToUTF8((*i).second.c_str());
1176     gtk_list_store_set(store, &iter, 0, key.c_str(), 1, value.c_str(), -1);
1177   }
1178
1179   gtk_entry_set_text(g_entityKeyEntry, strKey.c_str());
1180   gtk_entry_set_text(g_entityValueEntry, strVal.c_str());
1181
1182   for(EntityAttributes::const_iterator i = g_entityAttributes.begin(); i != g_entityAttributes.end(); ++i)
1183   {
1184     (*i)->update();
1185   }
1186 }
1187
1188 class EntityInspectorDraw
1189 {
1190   IdleDraw m_idleDraw;
1191 public:
1192   EntityInspectorDraw() : m_idleDraw(FreeCaller<EntityInspector_updateKeyValues>())
1193   {
1194   }
1195   void queueDraw()
1196   {
1197     m_idleDraw.queueDraw();
1198   }
1199 };
1200
1201 EntityInspectorDraw g_EntityInspectorDraw;
1202
1203
1204 void EntityInspector_keyValueChanged()
1205 {
1206   g_EntityInspectorDraw.queueDraw();
1207 }
1208 void EntityInspector_selectionChanged(const Selectable&)
1209 {
1210   EntityInspector_keyValueChanged();
1211 }
1212
1213 // Creates a new entity based on the currently selected brush and entity type.
1214 //
1215 void EntityClassList_createEntity()
1216 {
1217   GtkTreeView* view = g_entityClassList;
1218
1219   // find out what type of entity we are trying to create
1220   GtkTreeModel* model;
1221   GtkTreeIter iter;
1222   if(gtk_tree_selection_get_selected(gtk_tree_view_get_selection(view), &model, &iter) == FALSE)
1223   {
1224     gtk_MessageBox(gtk_widget_get_toplevel(GTK_WIDGET(g_entityClassList)), "You must have a selected class to create an entity", "info");
1225     return;
1226   }
1227
1228   char* text;
1229   gtk_tree_model_get(model, &iter, 0, &text, -1);
1230
1231   {
1232     StringOutputStream command;
1233     command << "entityCreate -class " << text;
1234
1235     UndoableCommand undo(command.c_str());
1236
1237     Entity_createFromSelection(text, g_vector3_identity);
1238   }
1239   g_free(text);
1240 }
1241
1242 void EntityInspector_applyKeyValue()
1243 {
1244   // Get current selection text
1245   StringOutputStream key(64);
1246   key << ConvertUTF8ToLocale(gtk_entry_get_text(g_entityKeyEntry));
1247   StringOutputStream value(64);
1248   value << ConvertUTF8ToLocale(gtk_entry_get_text(g_entityValueEntry));
1249
1250
1251   // TTimo: if you change the classname to worldspawn you won't merge back in the structural brushes but create a parasite entity
1252   if (!strcmp(key.c_str(), "classname") && !strcmp(value.c_str(), "worldspawn"))
1253   {
1254     gtk_MessageBox(gtk_widget_get_toplevel(GTK_WIDGET(g_entityKeyEntry)),  "Cannot change \"classname\" key back to worldspawn.", 0, eMB_OK );
1255     return;
1256   }
1257
1258
1259   // RR2DO2: we don't want spaces in entity keys
1260   if (strstr( key.c_str(), " " ))
1261   {
1262     gtk_MessageBox(gtk_widget_get_toplevel(GTK_WIDGET(g_entityKeyEntry)), "No spaces are allowed in entity keys.", 0, eMB_OK );
1263     return;
1264   }
1265
1266   if(strcmp(key.c_str(), "classname") == 0)
1267   {
1268     StringOutputStream command;
1269     command << "entitySetClass -class " << value.c_str();
1270     UndoableCommand undo(command.c_str());
1271     Scene_EntitySetClassname_Selected(value.c_str());
1272   }
1273   else
1274   {
1275     Scene_EntitySetKeyValue_Selected_Undoable(key.c_str(), value.c_str());
1276   }
1277 }
1278
1279 void EntityInspector_clearKeyValue()
1280 {
1281   // Get current selection text
1282   StringOutputStream key(64);
1283   key << ConvertUTF8ToLocale(gtk_entry_get_text(g_entityKeyEntry));
1284
1285   if(strcmp(key.c_str(), "classname") != 0)
1286   {
1287     StringOutputStream command;
1288     command << "entityDeleteKey -key " << key.c_str();
1289     UndoableCommand undo(command.c_str());
1290     Scene_EntitySetKeyValue_Selected(key.c_str(), "");
1291   }
1292 }
1293
1294 void EntityInspector_clearAllKeyValues()
1295 {
1296   UndoableCommand undo("entityClear");
1297
1298   // remove all keys except classname
1299   for(KeyValues::iterator i = g_selectedKeyValues.begin(); i != g_selectedKeyValues.end(); ++i)
1300   {
1301     if(strcmp((*i).first.c_str(), "classname") != 0)
1302     {
1303       Scene_EntitySetKeyValue_Selected((*i).first.c_str(), "");
1304     }
1305   }
1306 }
1307
1308 // =============================================================================
1309 // callbacks
1310
1311 static void EntityClassList_selection_changed(GtkTreeSelection* selection, gpointer data)
1312 {
1313   GtkTreeModel* model;
1314   GtkTreeIter selected;
1315   if(gtk_tree_selection_get_selected(selection, &model, &selected))
1316   {
1317     EntityClass* eclass;
1318     gtk_tree_model_get(model, &selected, 1, &eclass, -1);
1319     if(eclass != 0)
1320     {
1321       SetComment(eclass);
1322     }
1323   }
1324 }
1325
1326 static gint EntityClassList_button_press(GtkWidget *widget, GdkEventButton *event, gpointer data)
1327 {
1328   if (event->type == GDK_2BUTTON_PRESS)
1329   {
1330     EntityClassList_createEntity();
1331     return TRUE;
1332   }
1333   return FALSE;
1334 }
1335
1336 static gint EntityClassList_keypress(GtkWidget* widget, GdkEventKey* event, gpointer data)
1337 {
1338   unsigned int code = gdk_keyval_to_upper (event->keyval);
1339
1340   if (event->keyval == GDK_Return)
1341   {
1342     EntityClassList_createEntity();
1343     return TRUE;
1344   }
1345
1346   // select the entity that starts with the key pressed
1347   if (code <= 'Z' && code >= 'A')
1348   {
1349     GtkTreeView* view = g_entityClassList;
1350     GtkTreeModel* model;
1351     GtkTreeIter iter;
1352     if(gtk_tree_selection_get_selected(gtk_tree_view_get_selection(view), &model, &iter) == FALSE
1353       || gtk_tree_model_iter_next(model, &iter) == FALSE)
1354     {
1355       gtk_tree_model_get_iter_first(model, &iter);
1356     }
1357
1358     for(std::size_t count = gtk_tree_model_iter_n_children(model, 0); count > 0; --count)
1359     {
1360       char* text;
1361       gtk_tree_model_get(model, &iter, 0, &text, -1);
1362
1363       if (toupper (text[0]) == (int)code)
1364       {
1365         GtkTreePath* path = gtk_tree_model_get_path(model, &iter);
1366         gtk_tree_selection_select_path(gtk_tree_view_get_selection(view), path);
1367         if(GTK_WIDGET_REALIZED(view))
1368         {
1369           gtk_tree_view_scroll_to_cell(view, path, 0, FALSE, 0, 0);
1370         }
1371         gtk_tree_path_free(path);
1372         count = 1;
1373       }
1374
1375       g_free(text);
1376
1377       if(gtk_tree_model_iter_next(model, &iter) == FALSE)
1378         gtk_tree_model_get_iter_first(model, &iter);
1379     }
1380
1381     return TRUE;
1382   }
1383   return FALSE;
1384 }
1385
1386 static void EntityProperties_selection_changed(GtkTreeSelection* selection, gpointer data)
1387 {
1388   // find out what type of entity we are trying to create
1389   GtkTreeModel* model;
1390   GtkTreeIter iter;
1391   if(gtk_tree_selection_get_selected(selection, &model, &iter) == FALSE)
1392   {
1393     return;
1394   }
1395
1396   char* key;
1397   char* val;
1398   gtk_tree_model_get(model, &iter, 0, &key, 1, &val, -1);
1399
1400   gtk_entry_set_text(g_entityKeyEntry, key);
1401   gtk_entry_set_text(g_entityValueEntry, val);
1402
1403   g_free(key);
1404   g_free(val);
1405 }
1406
1407 static void SpawnflagCheck_toggled(GtkWidget *widget, gpointer data)
1408 {
1409   EntityInspector_applySpawnflags();
1410 }
1411
1412 static gint EntityEntry_keypress(GtkEntry* widget, GdkEventKey* event, gpointer data)
1413 {
1414   if (event->keyval == GDK_Return)
1415   {
1416     if(widget == g_entityKeyEntry)
1417     {
1418       gtk_entry_set_text(g_entityValueEntry, "");
1419       gtk_window_set_focus(GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(widget))), GTK_WIDGET(g_entityValueEntry));
1420     }
1421     else
1422     {
1423       EntityInspector_applyKeyValue();
1424     }
1425     return TRUE;
1426   }
1427   if (event->keyval == GDK_Escape)
1428   {
1429     gtk_window_set_focus(GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(widget))), NULL);
1430     return TRUE;
1431   }
1432
1433   return FALSE;
1434 }
1435
1436 void EntityInspector_destroyWindow(GtkWidget* widget, gpointer data)
1437 {
1438   g_entitysplit1_position = gtk_paned_get_position(GTK_PANED(g_entity_split1));
1439   g_entitysplit2_position = gtk_paned_get_position(GTK_PANED(g_entity_split2));
1440
1441   g_entityInspector_windowConstructed = false;
1442   GlobalEntityAttributes_clear();
1443 }
1444
1445 GtkWidget* EntityInspector_constructWindow(GtkWindow* toplevel)
1446 {
1447   GtkWidget* vbox = gtk_vbox_new(FALSE, 2);
1448   gtk_widget_show (vbox);
1449   gtk_container_set_border_width(GTK_CONTAINER (vbox), 2);
1450
1451   g_signal_connect(G_OBJECT(vbox), "destroy", G_CALLBACK(EntityInspector_destroyWindow), 0);
1452
1453   {
1454     GtkWidget* split1 = gtk_vpaned_new();
1455     gtk_box_pack_start(GTK_BOX(vbox), split1, TRUE, TRUE, 0);
1456     gtk_widget_show (split1);
1457
1458     g_entity_split1 = split1;
1459
1460     {
1461       GtkWidget* split2 = gtk_vpaned_new();
1462       gtk_paned_add1 (GTK_PANED (split1), split2);
1463       gtk_widget_show (split2);
1464
1465       g_entity_split2 = split2;
1466
1467       {
1468         // class list
1469         GtkWidget* scr = gtk_scrolled_window_new (0, 0);
1470         gtk_widget_show (scr);
1471         gtk_paned_add1 (GTK_PANED (split2), scr);
1472         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scr), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1473         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scr), GTK_SHADOW_IN);
1474
1475         {
1476           GtkListStore* store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
1477
1478           GtkTreeView* view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1479           gtk_tree_view_set_headers_visible(view, FALSE);
1480           g_signal_connect(G_OBJECT(view), "button_press_event", G_CALLBACK(EntityClassList_button_press), 0);
1481           g_signal_connect(G_OBJECT(view), "key_press_event", G_CALLBACK(EntityClassList_keypress), 0);
1482
1483           {
1484             GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
1485             GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("Key", renderer, "text", 0, 0);
1486             gtk_tree_view_append_column(view, column);
1487           }
1488
1489           {
1490             GtkTreeSelection* selection = gtk_tree_view_get_selection(view);
1491             g_signal_connect(G_OBJECT(selection), "changed", G_CALLBACK(EntityClassList_selection_changed), 0);
1492           }
1493
1494           gtk_widget_show(GTK_WIDGET(view));
1495
1496           gtk_container_add(GTK_CONTAINER(scr), GTK_WIDGET(view));
1497
1498           g_object_unref(G_OBJECT(store));
1499           g_entityClassList = view;
1500           g_entlist_store = store;
1501         }
1502       }
1503
1504       {
1505         GtkWidget* scr = gtk_scrolled_window_new (0, 0);
1506         gtk_widget_show (scr);
1507         gtk_paned_add2 (GTK_PANED (split2), scr);
1508         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scr), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1509         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scr), GTK_SHADOW_IN);
1510
1511         {
1512           GtkTextView* text = GTK_TEXT_VIEW(gtk_text_view_new());
1513           gtk_widget_set_size_request(GTK_WIDGET(text), 0, -1); // allow shrinking
1514           gtk_text_view_set_wrap_mode(text, GTK_WRAP_WORD);
1515           gtk_text_view_set_editable(text, FALSE);
1516           gtk_widget_show(GTK_WIDGET(text));
1517           gtk_container_add(GTK_CONTAINER(scr), GTK_WIDGET(text));
1518           g_entityClassComment = text;
1519         }
1520       }
1521     }
1522
1523     {
1524       GtkWidget* split2 = gtk_vpaned_new();
1525       gtk_paned_add2 (GTK_PANED (split1), split2);
1526       gtk_widget_show(split2);
1527
1528       {
1529         GtkWidget* vbox2 = gtk_vbox_new (FALSE, 2);
1530         gtk_widget_show (vbox2);
1531         gtk_paned_pack1(GTK_PANED(split2), vbox2, FALSE, FALSE);
1532
1533         {
1534           // Spawnflags (4 colums wide max, or window gets too wide.)
1535           GtkTable* table = GTK_TABLE(gtk_table_new(4, 4, FALSE));
1536           gtk_box_pack_start (GTK_BOX (vbox2), GTK_WIDGET(table), FALSE, TRUE, 0);
1537           gtk_widget_show(GTK_WIDGET(table));
1538           
1539           g_spawnflagsTable = table;
1540
1541           for (int i = 0; i < MAX_FLAGS; i++)
1542           {
1543             GtkCheckButton* check = GTK_CHECK_BUTTON(gtk_check_button_new_with_label(""));
1544             gtk_widget_ref(GTK_WIDGET(check));
1545             g_object_set_data(G_OBJECT(check), "handler", gint_to_pointer(g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(SpawnflagCheck_toggled), 0)));
1546             g_entitySpawnflagsCheck[i] = check;
1547           }
1548         }
1549
1550         {
1551           // key/value list
1552           GtkWidget* scr = gtk_scrolled_window_new (0, 0);
1553           gtk_widget_show (scr);
1554           gtk_box_pack_start (GTK_BOX (vbox2), scr, TRUE, TRUE, 0);
1555           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scr), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1556           gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scr), GTK_SHADOW_IN);
1557
1558           {
1559             GtkListStore* store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
1560
1561             GtkWidget* view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
1562             gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
1563
1564             {
1565               GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
1566               GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("", renderer, "text", 0, 0);
1567               gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
1568             }
1569
1570             {
1571               GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
1572               GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("", renderer, "text", 1, 0);
1573               gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
1574             }
1575
1576             {
1577               GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
1578               g_signal_connect(G_OBJECT(selection), "changed", G_CALLBACK(EntityProperties_selection_changed), 0);
1579             }
1580
1581             gtk_widget_show(view);
1582
1583             gtk_container_add(GTK_CONTAINER (scr), view);
1584
1585             g_object_unref(G_OBJECT(store));
1586
1587             g_entprops_store = store;
1588           }
1589         }
1590
1591         {
1592           // key/value entry
1593           GtkTable* table = GTK_TABLE(gtk_table_new(2, 2, FALSE));
1594           gtk_widget_show(GTK_WIDGET(table));
1595           gtk_box_pack_start(GTK_BOX(vbox2), GTK_WIDGET(table), FALSE, TRUE, 0);
1596           gtk_table_set_row_spacings(table, 3);
1597           gtk_table_set_col_spacings(table, 5);
1598
1599           {
1600             GtkEntry* entry = GTK_ENTRY(gtk_entry_new());
1601             gtk_widget_show(GTK_WIDGET(entry));
1602             gtk_table_attach(table, GTK_WIDGET(entry), 1, 2, 0, 1,
1603                               (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
1604                               (GtkAttachOptions)(0), 0, 0);
1605             gtk_widget_set_events(GTK_WIDGET(entry), GDK_KEY_PRESS_MASK);
1606             g_signal_connect(G_OBJECT(entry), "key_press_event", G_CALLBACK(EntityEntry_keypress), 0);
1607             g_entityKeyEntry = entry;
1608           }
1609
1610           {
1611             GtkEntry* entry = GTK_ENTRY(gtk_entry_new());
1612             gtk_widget_show(GTK_WIDGET(entry));
1613             gtk_table_attach(table, GTK_WIDGET(entry), 1, 2, 1, 2,
1614                               (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
1615                               (GtkAttachOptions)(0), 0, 0);
1616             gtk_widget_set_events(GTK_WIDGET(entry), GDK_KEY_PRESS_MASK);
1617             g_signal_connect(G_OBJECT(entry), "key_press_event", G_CALLBACK(EntityEntry_keypress), 0);
1618             g_entityValueEntry = entry;
1619           }
1620
1621           {
1622             GtkLabel* label = GTK_LABEL(gtk_label_new("Value"));
1623             gtk_widget_show(GTK_WIDGET(label));
1624             gtk_table_attach(table, GTK_WIDGET(label), 0, 1, 1, 2,
1625                               (GtkAttachOptions)(GTK_FILL),
1626                               (GtkAttachOptions)(0), 0, 0);
1627             gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1628           }
1629
1630           {
1631             GtkLabel* label = GTK_LABEL(gtk_label_new("Key"));
1632             gtk_widget_show(GTK_WIDGET(label));
1633             gtk_table_attach(table, GTK_WIDGET(label), 0, 1, 0, 1,
1634                               (GtkAttachOptions)(GTK_FILL),
1635                               (GtkAttachOptions)(0), 0, 0);
1636             gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1637           }
1638         }
1639
1640         {
1641           GtkBox* hbox = GTK_BOX(gtk_hbox_new(TRUE, 4));
1642           gtk_widget_show(GTK_WIDGET(hbox));
1643           gtk_box_pack_start(GTK_BOX(vbox2), GTK_WIDGET(hbox), FALSE, TRUE, 0);
1644           
1645           {
1646             GtkButton* button = GTK_BUTTON(gtk_button_new_with_label("Clear All"));
1647             gtk_widget_show(GTK_WIDGET(button));
1648             g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(EntityInspector_clearAllKeyValues), 0);
1649             gtk_box_pack_start(hbox, GTK_WIDGET(button), TRUE, TRUE, 0);
1650           }
1651           {
1652             GtkButton* button = GTK_BUTTON(gtk_button_new_with_label("Delete Key"));
1653             gtk_widget_show(GTK_WIDGET(button));
1654             g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(EntityInspector_clearKeyValue), 0);
1655             gtk_box_pack_start(hbox, GTK_WIDGET(button), TRUE, TRUE, 0);
1656           }
1657         }
1658       }
1659
1660       {
1661         GtkWidget* scr = gtk_scrolled_window_new(0, 0);
1662         gtk_widget_show(scr);
1663         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scr), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1664
1665         GtkWidget* viewport = gtk_viewport_new(0, 0);
1666         gtk_widget_show(viewport);
1667         gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
1668
1669         g_attributeBox = GTK_VBOX(gtk_vbox_new(FALSE, 2));
1670         gtk_widget_show(GTK_WIDGET(g_attributeBox));
1671
1672         gtk_container_add(GTK_CONTAINER(viewport), GTK_WIDGET(g_attributeBox));
1673         gtk_container_add(GTK_CONTAINER(scr), viewport);
1674         gtk_paned_pack2(GTK_PANED(split2), scr, FALSE, FALSE);
1675       }
1676     }
1677   }
1678
1679
1680   {
1681     int x = g_entitysplit1_position;
1682     if (x != -1)
1683     {
1684       gtk_paned_set_position(GTK_PANED(g_entity_split1), x);
1685
1686       x = g_entitysplit2_position;
1687
1688       if (x != -1)
1689         gtk_paned_set_position (GTK_PANED(g_entity_split2), x);
1690     }
1691   }
1692
1693   g_entityInspector_windowConstructed = true;
1694   EntityClassList_fill();
1695
1696   typedef FreeCaller1<const Selectable&, EntityInspector_selectionChanged> EntityInspectorSelectionChangedCaller;
1697   GlobalSelectionSystem().addSelectionChangeCallback(EntityInspectorSelectionChangedCaller());
1698   GlobalEntityCreator().setKeyValueChangedFunc(EntityInspector_keyValueChanged);
1699
1700   // hack
1701   gtk_container_set_focus_chain(GTK_CONTAINER(vbox), NULL);
1702
1703   return vbox;
1704 }
1705
1706 class EntityInspector : public ModuleObserver
1707 {
1708   std::size_t m_unrealised;
1709 public:
1710   EntityInspector() : m_unrealised(1)
1711   {
1712   }
1713   void realise()
1714   {
1715     if(--m_unrealised == 0)
1716     {
1717       if(g_entityInspector_windowConstructed)
1718       {
1719         //globalOutputStream() << "Entity Inspector: realise\n";
1720         EntityClassList_fill();
1721       }
1722     }
1723   }
1724   void unrealise()
1725   {
1726     if(++m_unrealised == 1)
1727     {
1728       if(g_entityInspector_windowConstructed)
1729       {
1730         //globalOutputStream() << "Entity Inspector: unrealise\n";
1731         EntityClassList_clear();
1732       }
1733     }
1734   }
1735 };
1736
1737 EntityInspector g_EntityInspector;
1738
1739 #include "preferencesystem.h"
1740 #include "stringio.h"
1741
1742 void EntityInspector_construct()
1743 {
1744   GlobalEntityClassManager().attach(g_EntityInspector);
1745
1746   GlobalPreferenceSystem().registerPreference("EntitySplit1", IntImportStringCaller(g_entitysplit1_position), IntExportStringCaller(g_entitysplit1_position));
1747   GlobalPreferenceSystem().registerPreference("EntitySplit2", IntImportStringCaller(g_entitysplit2_position), IntExportStringCaller(g_entitysplit2_position));
1748
1749 }
1750
1751 void EntityInspector_destroy()
1752 {
1753   GlobalEntityClassManager().detach(g_EntityInspector);
1754 }
1755