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