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