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