]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/xywindow.cpp
Q3map2:
[xonotic/netradiant.git] / radiant / xywindow.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 //
23 // XY Window
24 //
25 // Leonardo Zide (leo@lokigames.com)
26 //
27
28 #include "xywindow.h"
29
30 #include "debugging/debugging.h"
31
32 #include "ientity.h"
33 #include "igl.h"
34 #include "ibrush.h"
35 #include "iundo.h"
36 #include "iimage.h"
37 #include "ifilesystem.h"
38 #include "os/path.h"
39 #include "image.h"
40 #include "gtkutil/messagebox.h"
41
42 #include <gtk/gtklabel.h>
43 #include <gtk/gtkmenuitem.h>
44
45 #include "generic/callback.h"
46 #include "string/string.h"
47 #include "stream/stringstream.h"
48
49 #include "scenelib.h"
50 #include "eclasslib.h"
51 #include "renderer.h"
52 #include "moduleobserver.h"
53
54 #include "gtkutil/menu.h"
55 #include "gtkutil/container.h"
56 #include "gtkutil/widget.h"
57 #include "gtkutil/glwidget.h"
58 #include "gtkutil/filechooser.h"
59 #include "gtkmisc.h"
60 #include "select.h"
61 #include "csg.h"
62 #include "brushmanip.h"
63 #include "selection.h"
64 #include "entity.h"
65 #include "camwindow.h"
66 #include "texwindow.h"
67 #include "mainframe.h"
68 #include "preferences.h"
69 #include "commands.h"
70 #include "feedback.h"
71 #include "grid.h"
72 #include "windowobservers.h"
73
74 void LoadTextureRGBA( qtexture_t* q, unsigned char* pPixels, int nWidth, int nHeight );
75
76 // d1223m
77 extern bool g_brush_always_caulk;
78
79 //!\todo Rewrite.
80 class ClipPoint
81 {
82 public:
83 Vector3 m_ptClip;        // the 3d point
84 bool m_bSet;
85
86 ClipPoint(){
87         Reset();
88 };
89 void Reset(){
90         m_ptClip[0] = m_ptClip[1] = m_ptClip[2] = 0.0;
91         m_bSet = false;
92 }
93 bool Set(){
94         return m_bSet;
95 }
96 void Set( bool b ){
97         m_bSet = b;
98 }
99 operator Vector3&()
100 {
101         return m_ptClip;
102 };
103
104 /*! Draw clip/path point with rasterized number label */
105 void Draw( int num, float scale );
106 /*! Draw clip/path point with rasterized string label */
107 void Draw( const char *label, float scale );
108 };
109
110 VIEWTYPE g_clip_viewtype;
111 bool g_bSwitch = true;
112 bool g_clip_useCaulk = false;
113 bool g_quick_clipper = false;
114 ClipPoint g_Clip1;
115 ClipPoint g_Clip2;
116 ClipPoint g_Clip3;
117 ClipPoint* g_pMovingClip = 0;
118
119 /* Drawing clip points */
120 void ClipPoint::Draw( int num, float scale ){
121         StringOutputStream label( 4 );
122         label << num;
123         Draw( label.c_str(), scale );
124 }
125
126 void ClipPoint::Draw( const char *label, float scale ){
127         // draw point
128         glPointSize( 4 );
129         glColor3fv( vector3_to_array( g_xywindow_globals.color_clipper ) );
130         glBegin( GL_POINTS );
131         glVertex3fv( vector3_to_array( m_ptClip ) );
132         glEnd();
133         glPointSize( 1 );
134
135         float offset = 2.0f / scale;
136
137         // draw label
138         glRasterPos3f( m_ptClip[0] + offset, m_ptClip[1] + offset, m_ptClip[2] + offset );
139         //glCallLists( GLsizei( strlen( label ) ), GL_UNSIGNED_BYTE, label );   //fails with GCC
140         //glCallLists( GLsizei( strlen( label ) ), GL_UNSIGNED_BYTE, reinterpret_cast<const GLubyte*>( label ) );       //worx
141         GlobalOpenGL().drawString( label );
142 }
143
144 float fDiff( float f1, float f2 ){
145         if ( f1 > f2 ) {
146                 return f1 - f2;
147         }
148         else{
149                 return f2 - f1;
150         }
151 }
152
153 inline double ClipPoint_Intersect( const ClipPoint& clip, const Vector3& point, VIEWTYPE viewtype, float scale ){
154         int nDim1 = ( viewtype == YZ ) ? 1 : 0;
155         int nDim2 = ( viewtype == XY ) ? 1 : 2;
156         double screenDistanceSquared( vector2_length_squared( Vector2( fDiff( clip.m_ptClip[nDim1], point[nDim1] ) * scale, fDiff( clip.m_ptClip[nDim2], point[nDim2] )  * scale ) ) );
157         if ( screenDistanceSquared < 8 * 8 ) {
158                 return screenDistanceSquared;
159         }
160         return FLT_MAX;
161 }
162
163 inline void ClipPoint_testSelect( ClipPoint& clip, const Vector3& point, VIEWTYPE viewtype, float scale, double& bestDistance, ClipPoint*& bestClip ){
164         if ( clip.Set() ) {
165                 double distance = ClipPoint_Intersect( clip, point, viewtype, scale );
166                 if ( distance < bestDistance ) {
167                         bestDistance = distance;
168                         bestClip = &clip;
169                 }
170         }
171 }
172
173 inline ClipPoint* GlobalClipPoints_Find( const Vector3& point, VIEWTYPE viewtype, float scale ){
174         double bestDistance = FLT_MAX;
175         ClipPoint* bestClip = 0;
176         ClipPoint_testSelect( g_Clip1, point, viewtype, scale, bestDistance, bestClip );
177         ClipPoint_testSelect( g_Clip2, point, viewtype, scale, bestDistance, bestClip );
178         ClipPoint_testSelect( g_Clip3, point, viewtype, scale, bestDistance, bestClip );
179         return bestClip;
180 }
181
182 inline void GlobalClipPoints_Draw( float scale ){
183         // Draw clip points
184         if ( g_Clip1.Set() ) {
185                 g_Clip1.Draw( 1, scale );
186         }
187         if ( g_Clip2.Set() ) {
188                 g_Clip2.Draw( 2, scale );
189         }
190         if ( g_Clip3.Set() ) {
191                 g_Clip3.Draw( 3, scale );
192         }
193 }
194
195 inline bool GlobalClipPoints_valid(){
196         return g_Clip1.Set() && g_Clip2.Set();
197 }
198
199 void PlanePointsFromClipPoints( Vector3 planepts[3], const AABB& bounds, int viewtype ){
200         ASSERT_MESSAGE( GlobalClipPoints_valid(), "clipper points not initialised" );
201         planepts[0] = g_Clip1.m_ptClip;
202         planepts[1] = g_Clip2.m_ptClip;
203         planepts[2] = g_Clip3.m_ptClip;
204         Vector3 maxs( vector3_added( bounds.origin, bounds.extents ) );
205         Vector3 mins( vector3_subtracted( bounds.origin, bounds.extents ) );
206         if ( !g_Clip3.Set() ) {
207                 int n = ( viewtype == XY ) ? 2 : ( viewtype == YZ ) ? 0 : 1;
208                 int x = ( n == 0 ) ? 1 : 0;
209                 int y = ( n == 2 ) ? 1 : 2;
210
211                 if ( n == 1 ) { // on viewtype XZ, flip clip points
212                         planepts[0][n] = maxs[n];
213                         planepts[1][n] = maxs[n];
214                         planepts[2][x] = g_Clip1.m_ptClip[x];
215                         planepts[2][y] = g_Clip1.m_ptClip[y];
216                         planepts[2][n] = mins[n];
217                 }
218                 else
219                 {
220                         planepts[0][n] = mins[n];
221                         planepts[1][n] = mins[n];
222                         planepts[2][x] = g_Clip1.m_ptClip[x];
223                         planepts[2][y] = g_Clip1.m_ptClip[y];
224                         planepts[2][n] = maxs[n];
225                 }
226         }
227 }
228
229 void Clip_Update(){
230         Vector3 planepts[3];
231         if ( !GlobalClipPoints_valid() ) {
232                 planepts[0] = Vector3( 0, 0, 0 );
233                 planepts[1] = Vector3( 0, 0, 0 );
234                 planepts[2] = Vector3( 0, 0, 0 );
235                 Scene_BrushSetClipPlane( GlobalSceneGraph(), Plane3( 0, 0, 0, 0 ) );
236         }
237         else
238         {
239                 AABB bounds( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
240                 PlanePointsFromClipPoints( planepts, bounds, g_clip_viewtype );
241                 if ( g_bSwitch ) {
242                         std::swap( planepts[0], planepts[1] );
243                 }
244                 Scene_BrushSetClipPlane( GlobalSceneGraph(), plane3_for_points( planepts[0], planepts[1], planepts[2] ) );
245         }
246         ClipperChangeNotify();
247 }
248
249 const char* Clip_getShader(){
250         return g_clip_useCaulk ? "textures/common/caulk" : TextureBrowser_GetSelectedShader( GlobalTextureBrowser() );
251 }
252
253 void Clip(){
254         if ( ClipMode() && GlobalClipPoints_valid() ) {
255                 Vector3 planepts[3];
256                 AABB bounds( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
257                 PlanePointsFromClipPoints( planepts, bounds, g_clip_viewtype );
258                 Scene_BrushSplitByPlane( GlobalSceneGraph(), planepts[0], planepts[1], planepts[2], Clip_getShader(), ( !g_bSwitch ) ? eFront : eBack );
259                 g_Clip1.Reset();
260                 g_Clip2.Reset();
261                 g_Clip3.Reset();
262                 Clip_Update();
263                 ClipperChangeNotify();
264                 if( g_quick_clipper ){
265                         g_quick_clipper = false;
266                         ClipperMode();
267                 }
268         }
269 }
270
271 void SplitClip(){
272         if ( ClipMode() && GlobalClipPoints_valid() ) {
273                 Vector3 planepts[3];
274                 AABB bounds( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
275                 PlanePointsFromClipPoints( planepts, bounds, g_clip_viewtype );
276                 Scene_BrushSplitByPlane( GlobalSceneGraph(), planepts[0], planepts[1], planepts[2], Clip_getShader(), eFrontAndBack );
277                 g_Clip1.Reset();
278                 g_Clip2.Reset();
279                 g_Clip3.Reset();
280                 Clip_Update();
281                 ClipperChangeNotify();
282                 if( g_quick_clipper ){
283                         g_quick_clipper = false;
284                         ClipperMode();
285                 }
286         }
287 }
288
289 void FlipClip(){
290         g_bSwitch = !g_bSwitch;
291         Clip_Update();
292         ClipperChangeNotify();
293 }
294
295 void OnClipMode( bool enabled ){
296         g_Clip1.Reset();
297         g_Clip2.Reset();
298         g_Clip3.Reset();
299
300         if ( !enabled && g_pMovingClip ) {
301                 g_pMovingClip = 0;
302         }
303
304         Clip_Update();
305         ClipperChangeNotify();
306 }
307
308 bool ClipMode(){
309         return GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip;
310 }
311
312 void NewClipPoint( const Vector3& point ){
313         if ( g_Clip1.Set() == false ) {
314                 g_Clip1.m_ptClip = point;
315                 g_Clip1.Set( true );
316         }
317         else if ( g_Clip2.Set() == false ) {
318                 g_Clip2.m_ptClip = point;
319                 g_Clip2.Set( true );
320         }
321         else if ( g_Clip3.Set() == false ) {
322                 g_Clip3.m_ptClip = point;
323                 g_Clip3.Set( true );
324         }
325         else
326         {
327                 g_Clip1.Reset();
328                 g_Clip2.Reset();
329                 g_Clip3.Reset();
330                 g_Clip1.m_ptClip = point;
331                 g_Clip1.Set( true );
332         }
333
334         Clip_Update();
335         ClipperChangeNotify();
336 }
337
338
339
340 struct xywindow_globals_private_t
341 {
342         bool d_showgrid;
343
344         // these are in the View > Show menu with Show coordinates
345         bool show_names;
346         bool show_coordinates;
347         bool show_angles;
348         bool show_outline;
349         bool show_axis;
350
351         bool d_show_work;
352
353         bool show_blocks;
354         int blockSize;
355
356 //      bool m_bCamXYUpdate;
357         bool m_bChaseMouse;
358         bool m_bSizePaint;
359
360         xywindow_globals_private_t() :
361                 d_showgrid( true ),
362
363                 show_names( false ),
364                 show_coordinates( false ),
365                 show_angles( true ),
366                 show_outline( true ),
367                 show_axis( true ),
368
369                 d_show_work( false ),
370
371                 show_blocks( false ),
372
373 //              m_bCamXYUpdate( true ),
374                 m_bChaseMouse( true ),
375                 m_bSizePaint( true ){
376         }
377
378 };
379
380 xywindow_globals_t g_xywindow_globals;
381 xywindow_globals_private_t g_xywindow_globals_private;
382
383 const unsigned int RAD_NONE =    0x00;
384 const unsigned int RAD_SHIFT =   0x01;
385 const unsigned int RAD_ALT =     0x02;
386 const unsigned int RAD_CONTROL = 0x04;
387 const unsigned int RAD_PRESS   = 0x08;
388 const unsigned int RAD_LBUTTON = 0x10;
389 const unsigned int RAD_MBUTTON = 0x20;
390 const unsigned int RAD_RBUTTON = 0x40;
391
392 inline ButtonIdentifier button_for_flags( unsigned int flags ){
393         if ( flags & RAD_LBUTTON ) {
394                 return c_buttonLeft;
395         }
396         if ( flags & RAD_RBUTTON ) {
397                 return c_buttonRight;
398         }
399         if ( flags & RAD_MBUTTON ) {
400                 return c_buttonMiddle;
401         }
402         return c_buttonInvalid;
403 }
404
405 inline ModifierFlags modifiers_for_flags( unsigned int flags ){
406         ModifierFlags modifiers = c_modifierNone;
407         if ( flags & RAD_SHIFT ) {
408                 modifiers |= c_modifierShift;
409         }
410         if ( flags & RAD_CONTROL ) {
411                 modifiers |= c_modifierControl;
412         }
413         if ( flags & RAD_ALT ) {
414                 modifiers |= c_modifierAlt;
415         }
416         return modifiers;
417 }
418
419 inline unsigned int buttons_for_button_and_modifiers( ButtonIdentifier button, ModifierFlags flags ){
420         unsigned int buttons = 0;
421
422         switch ( button.get() )
423         {
424         case ButtonEnumeration::LEFT: buttons |= RAD_LBUTTON; break;
425         case ButtonEnumeration::MIDDLE: buttons |= RAD_MBUTTON; break;
426         case ButtonEnumeration::RIGHT: buttons |= RAD_RBUTTON; break;
427         }
428
429         if ( bitfield_enabled( flags, c_modifierControl ) ) {
430                 buttons |= RAD_CONTROL;
431         }
432
433         if ( bitfield_enabled( flags, c_modifierShift ) ) {
434                 buttons |= RAD_SHIFT;
435         }
436
437         if ( bitfield_enabled( flags, c_modifierAlt ) ) {
438                 buttons |= RAD_ALT;
439         }
440
441         return buttons;
442 }
443
444 inline unsigned int buttons_for_event_button( GdkEventButton* event ){
445         unsigned int flags = 0;
446
447         switch ( event->button )
448         {
449         case 1: flags |= RAD_LBUTTON; break;
450         case 2: flags |= RAD_MBUTTON; break;
451         case 3: flags |= RAD_RBUTTON; break;
452         }
453
454         if ( ( event->state & GDK_CONTROL_MASK ) != 0 ) {
455                 flags |= RAD_CONTROL;
456         }
457
458         if ( ( event->state & GDK_SHIFT_MASK ) != 0 ) {
459                 flags |= RAD_SHIFT;
460         }
461
462         if ( ( event->state & GDK_MOD1_MASK ) != 0 ) {
463                 flags |= RAD_ALT;
464         }
465
466         return flags;
467 }
468
469 inline unsigned int buttons_for_state( guint state ){
470         unsigned int flags = 0;
471
472         if ( ( state & GDK_BUTTON1_MASK ) != 0 ) {
473                 flags |= RAD_LBUTTON;
474         }
475
476         if ( ( state & GDK_BUTTON2_MASK ) != 0 ) {
477                 flags |= RAD_MBUTTON;
478         }
479
480         if ( ( state & GDK_BUTTON3_MASK ) != 0 ) {
481                 flags |= RAD_RBUTTON;
482         }
483
484         if ( ( state & GDK_CONTROL_MASK ) != 0 ) {
485                 flags |= RAD_CONTROL;
486         }
487
488         if ( ( state & GDK_SHIFT_MASK ) != 0 ) {
489                 flags |= RAD_SHIFT;
490         }
491
492         if ( ( state & GDK_MOD1_MASK ) != 0 ) {
493                 flags |= RAD_ALT;
494         }
495
496         return flags;
497 }
498
499
500 void XYWnd::SetScale( float f ){
501         m_fScale = f;
502         updateProjection();
503         updateModelview();
504         XYWnd_Update( *this );
505 }
506
507 void XYWnd::ZoomIn(){
508         float max_scale = 64;
509         float scale = Scale() * 5.0f / 4.0f;
510         if ( scale > max_scale ) {
511                 if ( Scale() != max_scale ) {
512                         SetScale( max_scale );
513                 }
514         }
515         else
516         {
517                 SetScale( scale );
518         }
519 }
520
521
522 // NOTE: the zoom out factor is 4/5, we could think about customizing it
523 //  we don't go below a zoom factor corresponding to 10% of the max world size
524 //  (this has to be computed against the window size)
525 void XYWnd::ZoomOut(){
526         float min_scale = MIN( Width(), Height() ) / ( 1.1f * ( g_MaxWorldCoord - g_MinWorldCoord ) );
527         float scale = Scale() * 4.0f / 5.0f;
528         if ( scale < min_scale ) {
529                 if ( Scale() != min_scale ) {
530                         SetScale( min_scale );
531                 }
532         }
533         else
534         {
535                 SetScale( scale );
536         }
537 }
538
539 void XYWnd::ZoomInWithMouse( int pointx, int pointy ){
540         float old_scale = Scale();
541         ZoomIn();
542         if ( g_xywindow_globals.m_bZoomInToPointer ) {
543                 float scale_diff = 1.0 / old_scale - 1.0 / Scale();
544                 int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
545                 int nDim2 = ( m_viewType == XY ) ? 1 : 2;
546                 Vector3 origin = GetOrigin();
547                 origin[nDim1] += scale_diff * (pointx - 0.5 * Width());
548                 origin[nDim2] -= scale_diff * (pointy - 0.5 * Height());
549                 SetOrigin( origin );
550         }
551 }
552
553 VIEWTYPE GlobalXYWnd_getCurrentViewType(){
554         ASSERT_NOTNULL( g_pParentWnd );
555         ASSERT_NOTNULL( g_pParentWnd->ActiveXY() );
556         return g_pParentWnd->ActiveXY()->GetViewType();
557 }
558
559 // =============================================================================
560 // variables
561
562 bool g_bCrossHairs = false;
563
564 GtkMenu* XYWnd::m_mnuDrop = 0;
565
566 // this is disabled, and broken
567 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=394
568 #if 0
569 void WXY_Print(){
570         long width, height;
571         width = g_pParentWnd->ActiveXY()->Width();
572         height = g_pParentWnd->ActiveXY()->Height();
573         unsigned char* img;
574         const char* filename;
575
576         filename = file_dialog( GTK_WIDGET( MainFrame_getWindow() ), FALSE, "Save Image", 0, FILTER_BMP );
577         if ( !filename ) {
578                 return;
579         }
580
581         g_pParentWnd->ActiveXY()->MakeCurrent();
582         img = (unsigned char*)malloc( width * height * 3 );
583         glReadPixels( 0,0,width,height,GL_RGB,GL_UNSIGNED_BYTE,img );
584
585         FILE *fp;
586         fp = fopen( filename, "wb" );
587         if ( fp ) {
588                 unsigned short bits;
589                 unsigned long cmap, bfSize;
590
591                 bits = 24;
592                 cmap = 0;
593                 bfSize = 54 + width * height * 3;
594
595                 long byteswritten = 0;
596                 long pixoff = 54 + cmap * 4;
597                 short res = 0;
598                 char m1 = 'B', m2 = 'M';
599                 fwrite( &m1, 1, 1, fp );      byteswritten++; // B
600                 fwrite( &m2, 1, 1, fp );      byteswritten++; // M
601                 fwrite( &bfSize, 4, 1, fp );  byteswritten += 4; // bfSize
602                 fwrite( &res, 2, 1, fp );     byteswritten += 2; // bfReserved1
603                 fwrite( &res, 2, 1, fp );     byteswritten += 2; // bfReserved2
604                 fwrite( &pixoff, 4, 1, fp );  byteswritten += 4; // bfOffBits
605
606                 unsigned long biSize = 40, compress = 0, size = 0;
607                 long pixels = 0;
608                 unsigned short planes = 1;
609                 fwrite( &biSize, 4, 1, fp );  byteswritten += 4; // biSize
610                 fwrite( &width, 4, 1, fp );   byteswritten += 4; // biWidth
611                 fwrite( &height, 4, 1, fp );  byteswritten += 4; // biHeight
612                 fwrite( &planes, 2, 1, fp );  byteswritten += 2; // biPlanes
613                 fwrite( &bits, 2, 1, fp );    byteswritten += 2; // biBitCount
614                 fwrite( &compress, 4, 1, fp ); byteswritten += 4; // biCompression
615                 fwrite( &size, 4, 1, fp );    byteswritten += 4; // biSizeImage
616                 fwrite( &pixels, 4, 1, fp );  byteswritten += 4; // biXPelsPerMeter
617                 fwrite( &pixels, 4, 1, fp );  byteswritten += 4; // biYPelsPerMeter
618                 fwrite( &cmap, 4, 1, fp );    byteswritten += 4; // biClrUsed
619                 fwrite( &cmap, 4, 1, fp );    byteswritten += 4; // biClrImportant
620
621                 unsigned long widthDW = ( ( ( width * 24 ) + 31 ) / 32 * 4 );
622                 long row, row_size = width * 3;
623                 for ( row = 0; row < height; row++ )
624                 {
625                         unsigned char* buf = img + row * row_size;
626
627                         // write a row
628                         int col;
629                         for ( col = 0; col < row_size; col += 3 )
630                         {
631                                 putc( buf[col + 2], fp );
632                                 putc( buf[col + 1], fp );
633                                 putc( buf[col], fp );
634                         }
635                         byteswritten += row_size;
636
637                         unsigned long count;
638                         for ( count = row_size; count < widthDW; count++ )
639                         {
640                                 putc( 0, fp ); // dummy
641                                 byteswritten++;
642                         }
643                 }
644
645                 fclose( fp );
646         }
647
648         free( img );
649 }
650 #endif
651
652
653 #include "timer.h"
654
655 Timer g_chasemouse_timer;
656
657 void XYWnd::ChaseMouse(){
658         float multiplier = g_chasemouse_timer.elapsed_msec() / 10.0f;
659         Scroll( float_to_integer( multiplier * m_chasemouse_delta_x ), float_to_integer( multiplier * -m_chasemouse_delta_y ) );
660
661         //globalOutputStream() << "chasemouse: multiplier=" << multiplier << " x=" << m_chasemouse_delta_x << " y=" << m_chasemouse_delta_y << '\n';
662
663         XY_MouseMoved( m_chasemouse_current_x, m_chasemouse_current_y, getButtonState() );
664         g_chasemouse_timer.start();
665 }
666
667 gboolean xywnd_chasemouse( gpointer data ){
668         reinterpret_cast<XYWnd*>( data )->ChaseMouse();
669         return TRUE;
670 }
671
672 inline const int& min_int( const int& left, const int& right ){
673         return std::min( left, right );
674 }
675
676 bool XYWnd::chaseMouseMotion( int pointx, int pointy ){
677         m_chasemouse_delta_x = 0;
678         m_chasemouse_delta_y = 0;
679
680         if ( g_xywindow_globals_private.m_bChaseMouse && getButtonState() == RAD_LBUTTON ) {
681                 const int epsilon = 16;
682
683                 if ( pointx < epsilon ) {
684                         m_chasemouse_delta_x = std::max( pointx, 0 ) - epsilon;
685                 }
686                 else if ( ( pointx - m_nWidth ) > -epsilon ) {
687                         m_chasemouse_delta_x = min_int( ( pointx - m_nWidth ), 0 ) + epsilon;
688                 }
689
690                 if ( pointy < epsilon ) {
691                         m_chasemouse_delta_y = std::max( pointy, 0 ) - epsilon;
692                 }
693                 else if ( ( pointy - m_nHeight ) > -epsilon ) {
694                         m_chasemouse_delta_y = min_int( ( pointy - m_nHeight ), 0 ) + epsilon;
695                 }
696
697                 if ( m_chasemouse_delta_y != 0 || m_chasemouse_delta_x != 0 ) {
698                         //globalOutputStream() << "chasemouse motion: x=" << pointx << " y=" << pointy << "... ";
699                         m_chasemouse_current_x = pointx;
700                         m_chasemouse_current_y = pointy;
701                         if ( m_chasemouse_handler == 0 ) {
702                                 //globalOutputStream() << "chasemouse timer start... ";
703                                 g_chasemouse_timer.start();
704                                 m_chasemouse_handler = g_idle_add( xywnd_chasemouse, this );
705                         }
706                         return true;
707                 }
708                 else
709                 {
710                         if ( m_chasemouse_handler != 0 ) {
711                                 //globalOutputStream() << "chasemouse cancel\n";
712                                 g_source_remove( m_chasemouse_handler );
713                                 m_chasemouse_handler = 0;
714                         }
715                 }
716         }
717         else
718         {
719                 if ( m_chasemouse_handler != 0 ) {
720                         //globalOutputStream() << "chasemouse cancel\n";
721                         g_source_remove( m_chasemouse_handler );
722                         m_chasemouse_handler = 0;
723                 }
724         }
725         return false;
726 }
727
728 // =============================================================================
729 // XYWnd class
730 Shader* XYWnd::m_state_selected = 0;
731
732 void xy_update_xor_rectangle( XYWnd& self, rect_t area ){
733         if ( GTK_WIDGET_VISIBLE( self.GetWidget() ) ) {
734                 rectangle_t rect = rectangle_from_area( area.min, area.max, self.Width(), self.Height() );
735 //              int nDim1 = ( self.GetViewType() == YZ ) ? 1 : 0;
736 //              int nDim2 = ( self.GetViewType() == XY ) ? 1 : 2;
737 //              rect.x /= self.Scale();
738 //              rect.y /= self.Scale();
739 //              rect.w /= self.Scale();
740 //              rect.h /= self.Scale();
741 //              rect.x += self.GetOrigin()[nDim1];
742 //              rect.y += self.GetOrigin()[nDim2];
743                 self.m_XORRectangle.set( rect );
744         }
745 }
746
747 gboolean xywnd_button_press( GtkWidget* widget, GdkEventButton* event, XYWnd* xywnd ){
748         if ( event->type == GDK_BUTTON_PRESS ) {
749                 gtk_widget_grab_focus( xywnd->GetWidget() );
750
751                 if( !xywnd->Active() ){
752                         g_pParentWnd->SetActiveXY( xywnd );
753                 }
754
755                 xywnd->ButtonState_onMouseDown( buttons_for_event_button( event ) );
756
757                 xywnd->onMouseDown( WindowVector( event->x, event->y ), button_for_button( event->button ), modifiers_for_state( event->state ) );
758         }
759         return FALSE;
760 }
761
762 gboolean xywnd_button_release( GtkWidget* widget, GdkEventButton* event, XYWnd* xywnd ){
763         if ( event->type == GDK_BUTTON_RELEASE ) {
764                 xywnd->XY_MouseUp( static_cast<int>( event->x ), static_cast<int>( event->y ), buttons_for_event_button( event ) );
765
766                 xywnd->ButtonState_onMouseUp( buttons_for_event_button( event ) );
767         }
768         return FALSE;
769 }
770
771 gboolean xywnd_focus_in( GtkWidget* widget, GdkEventFocus* event, XYWnd* xywnd ){
772         if ( event->type == GDK_FOCUS_CHANGE ) {
773                 if ( event->in ) {
774                         if( !xywnd->Active() ){
775                                 g_pParentWnd->SetActiveXY( xywnd );
776                         }
777                 }
778         }
779         return FALSE;
780 }
781
782 void xywnd_motion( gdouble x, gdouble y, guint state, void* data ){
783         if ( reinterpret_cast<XYWnd*>( data )->chaseMouseMotion( static_cast<int>( x ), static_cast<int>( y ) ) ) {
784                 return;
785         }
786         reinterpret_cast<XYWnd*>( data )->XY_MouseMoved( static_cast<int>( x ), static_cast<int>( y ), buttons_for_state( state ) );
787 }
788
789 gboolean xywnd_wheel_scroll( GtkWidget* widget, GdkEventScroll* event, XYWnd* xywnd ){
790         gtk_widget_grab_focus( xywnd->GetWidget() );
791         GtkWindow* window = xywnd->m_parent != 0 ? xywnd->m_parent : MainFrame_getWindow();
792         if( !gtk_window_is_active( window ) )
793                 gtk_window_present( window );
794
795         if( !xywnd->Active() ){
796                 g_pParentWnd->SetActiveXY( xywnd );
797         }
798         if ( event->direction == GDK_SCROLL_UP ) {
799                 xywnd->ZoomInWithMouse( (int)event->x, (int)event->y );
800         }
801         else if ( event->direction == GDK_SCROLL_DOWN ) {
802                 xywnd->ZoomOut();
803         }
804         return FALSE;
805 }
806
807 gboolean xywnd_size_allocate( GtkWidget* widget, GtkAllocation* allocation, XYWnd* xywnd ){
808         xywnd->m_nWidth = allocation->width;
809         xywnd->m_nHeight = allocation->height;
810         xywnd->updateProjection();
811         xywnd->m_window_observer->onSizeChanged( xywnd->Width(), xywnd->Height() );
812         return FALSE;
813 }
814
815 gboolean xywnd_expose( GtkWidget* widget, GdkEventExpose* event, XYWnd* xywnd ){
816         if ( glwidget_make_current( xywnd->GetWidget() ) != FALSE ) {
817                 if ( Map_Valid( g_map ) && ScreenUpdates_Enabled() ) {
818                         GlobalOpenGL_debugAssertNoErrors();
819                         xywnd->XY_Draw();
820                         GlobalOpenGL_debugAssertNoErrors();
821
822                         xywnd->m_XORRectangle.set( rectangle_t() );
823                 }
824                 glwidget_swap_buffers( xywnd->GetWidget() );
825         }
826         return FALSE;
827 }
828
829
830 void XYWnd_CameraMoved( XYWnd& xywnd ){
831 //      if ( g_xywindow_globals_private.m_bCamXYUpdate ) {
832                 //XYWnd_Update( xywnd );
833                 xywnd.UpdateCameraIcon();
834 //      }
835 }
836
837 XYWnd::XYWnd() :
838         m_gl_widget( glwidget_new( FALSE ) ),
839         m_deferredDraw( WidgetQueueDrawCaller( *m_gl_widget ) ),
840         m_deferred_motion( xywnd_motion, this ),
841         m_parent( 0 ),
842         m_window_observer( NewWindowObserver() ),
843         m_XORRectangle( m_gl_widget ),
844         m_chasemouse_handler( 0 ){
845         m_bActive = false;
846         m_buttonstate = 0;
847
848         m_bNewBrushDrag = false;
849         m_move_started = false;
850         m_zoom_started = false;
851
852         m_nWidth = 0;
853         m_nHeight = 0;
854
855         m_vOrigin[0] = 0;
856         m_vOrigin[1] = 20;
857         m_vOrigin[2] = 46;
858         m_fScale = 1;
859         m_viewType = XY;
860
861         m_backgroundActivated = false;
862         m_alpha = 1.0f;
863         m_xmin = 0.0f;
864         m_ymin = 0.0f;
865         m_xmax = 0.0f;
866         m_ymax = 0.0f;
867
868         m_entityCreate = false;
869
870         m_mnuDrop = 0;
871
872         GlobalWindowObservers_add( m_window_observer );
873         GlobalWindowObservers_connectWidget( m_gl_widget );
874
875         m_window_observer->setRectangleDrawCallback( ReferenceCaller1<XYWnd, rect_t, xy_update_xor_rectangle>( *this ) );
876         m_window_observer->setView( m_view );
877
878         gtk_widget_ref( m_gl_widget );
879
880         gtk_widget_set_events( m_gl_widget, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK );
881         GTK_WIDGET_SET_FLAGS( m_gl_widget, GTK_CAN_FOCUS );
882
883         m_sizeHandler = g_signal_connect( G_OBJECT( m_gl_widget ), "size_allocate", G_CALLBACK( xywnd_size_allocate ), this );
884         m_exposeHandler = g_signal_connect( G_OBJECT( m_gl_widget ), "expose_event", G_CALLBACK( xywnd_expose ), this );
885
886         g_signal_connect( G_OBJECT( m_gl_widget ), "button_press_event", G_CALLBACK( xywnd_button_press ), this );
887         g_signal_connect( G_OBJECT( m_gl_widget ), "button_release_event", G_CALLBACK( xywnd_button_release ), this );
888         g_signal_connect( G_OBJECT( m_gl_widget ), "focus_in_event", G_CALLBACK( xywnd_focus_in ), this );      //works only in floating views layout
889         g_signal_connect( G_OBJECT( m_gl_widget ), "motion_notify_event", G_CALLBACK( DeferredMotion::gtk_motion ), &m_deferred_motion );
890
891         g_signal_connect( G_OBJECT( m_gl_widget ), "scroll_event", G_CALLBACK( xywnd_wheel_scroll ), this );
892
893         Map_addValidCallback( g_map, DeferredDrawOnMapValidChangedCaller( m_deferredDraw ) );
894
895         updateProjection();
896         updateModelview();
897
898         AddSceneChangeCallback( ReferenceCaller<XYWnd, &XYWnd_Update>( *this ) );
899         AddCameraMovedCallback( ReferenceCaller<XYWnd, &XYWnd_CameraMoved>( *this ) );
900
901         PressedButtons_connect( g_pressedButtons, m_gl_widget );
902
903         onMouseDown.connectLast( makeSignalHandler3( MouseDownCaller(), *this ) );
904 }
905
906 XYWnd::~XYWnd(){
907         onDestroyed();
908
909         if ( m_mnuDrop != 0 ) {
910                 gtk_widget_destroy( GTK_WIDGET( m_mnuDrop ) );
911                 m_mnuDrop = 0;
912         }
913
914         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_sizeHandler );
915         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_exposeHandler );
916
917         gtk_widget_unref( m_gl_widget );
918
919         m_window_observer->release();
920 }
921
922 void XYWnd::captureStates(){
923         m_state_selected = GlobalShaderCache().capture( "$XY_OVERLAY" );
924 }
925
926 void XYWnd::releaseStates(){
927         GlobalShaderCache().release( "$XY_OVERLAY" );
928 }
929
930 const Vector3& XYWnd::GetOrigin(){
931         return m_vOrigin;
932 }
933
934 void XYWnd::SetOrigin( const Vector3& origin ){
935         m_vOrigin = origin;
936         updateModelview();
937 }
938
939 void XYWnd::Scroll( int x, int y ){
940         int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
941         int nDim2 = ( m_viewType == XY ) ? 1 : 2;
942         m_vOrigin[nDim1] += x / m_fScale;
943         m_vOrigin[nDim2] += y / m_fScale;
944         updateModelview();
945         queueDraw();
946 }
947
948 unsigned int Clipper_buttons(){
949         return RAD_LBUTTON;
950 }
951
952 unsigned int Clipper_quick_buttons(){
953         return RAD_LBUTTON | RAD_CONTROL;
954 }
955
956 void XYWnd::DropClipPoint( int pointx, int pointy ){
957         Vector3 point;
958
959         XY_ToPoint( pointx, pointy, point );
960
961         Vector3 mid;
962         Select_GetMid( mid );
963         g_clip_viewtype = static_cast<VIEWTYPE>( GetViewType() );
964         const int nDim = ( g_clip_viewtype == YZ ) ? 0 : ( ( g_clip_viewtype == XZ ) ? 1 : 2 );
965         point[nDim] = mid[nDim];
966         vector3_snap( point, GetSnapGridSize() );
967         NewClipPoint( point );
968 }
969
970 void XYWnd::Clipper_OnLButtonDown( int x, int y ){
971         Vector3 mousePosition;
972         XY_ToPoint( x, y, mousePosition );
973         g_pMovingClip = GlobalClipPoints_Find( mousePosition, (VIEWTYPE)m_viewType, m_fScale );
974         if ( !g_pMovingClip ) {
975                 DropClipPoint( x, y );
976         }
977 }
978
979 void XYWnd::Clipper_OnLButtonUp( int x, int y ){
980         if ( g_pMovingClip ) {
981                 g_pMovingClip = 0;
982         }
983 }
984
985 void XYWnd::Clipper_OnMouseMoved( int x, int y ){
986         if ( g_pMovingClip ) {
987                 XY_ToPoint( x, y, g_pMovingClip->m_ptClip );
988                 XY_SnapToGrid( g_pMovingClip->m_ptClip );
989                 Clip_Update();
990                 ClipperChangeNotify();
991         }
992 }
993
994 //#include "gtkutil/image.h"
995
996 /* is called on every mouse move fraction; ain't good! */
997 void XYWnd::Clipper_Crosshair_OnMouseMoved( int x, int y ){
998         Vector3 mousePosition;
999         XY_ToPoint( x, y, mousePosition );
1000         if ( ClipMode() ) {
1001                 if( GlobalClipPoints_Find( mousePosition, (VIEWTYPE)m_viewType, m_fScale ) != 0 ){
1002                         GdkCursor *cursor;
1003                         cursor = gdk_cursor_new( GDK_CROSSHAIR );
1004                         //cursor = gdk_cursor_new( GDK_FLEUR );
1005                         gdk_window_set_cursor( m_gl_widget->window, cursor );
1006                         gdk_cursor_unref( cursor );
1007                 }
1008                 else{
1009                         GdkCursor *cursor;
1010                         cursor = gdk_cursor_new( GDK_HAND2 );
1011 //                      GdkPixbuf* pixbuf = pixbuf_new_from_file_with_mask( "bitmaps/icon.png" );
1012 //                      cursor = gdk_cursor_new_from_pixbuf( gdk_display_get_default(), pixbuf, 0, 0 );
1013 //                      g_object_unref( pixbuf );
1014                         gdk_window_set_cursor( m_gl_widget->window, cursor );
1015                         gdk_cursor_unref( cursor );
1016
1017                 }
1018         }
1019         else
1020         {
1021                 gdk_window_set_cursor( m_gl_widget->window, 0 );
1022         }
1023 }
1024
1025 void XYWnd::SetCustomPivotOrigin( int pointx, int pointy ){
1026         Vector3 point;
1027         XY_ToPoint( pointx, pointy, point );
1028         VIEWTYPE viewtype = static_cast<VIEWTYPE>( GetViewType() );
1029         const int nDim = ( viewtype == YZ ) ? 0 : ( ( viewtype == XZ ) ? 1 : 2 );
1030         //vector3_snap( point, GetSnapGridSize() );
1031         point[nDim] = 999999;
1032
1033         GlobalSelectionSystem().setCustomPivotOrigin( point );
1034         SceneChangeNotify();
1035 }
1036
1037 unsigned int MoveCamera_buttons(){
1038 //      return RAD_CONTROL | ( g_glwindow_globals.m_nMouseType == ETwoButton ? RAD_RBUTTON : RAD_MBUTTON );
1039         return RAD_CONTROL | RAD_MBUTTON;
1040 }
1041
1042 void XYWnd_PositionCamera( XYWnd* xywnd, int x, int y, CamWnd& camwnd ){
1043         Vector3 origin( Camera_getOrigin( camwnd ) );
1044         xywnd->XY_ToPoint( x, y, origin );
1045         xywnd->XY_SnapToGrid( origin );
1046         Camera_setOrigin( camwnd, origin );
1047 }
1048
1049 unsigned int OrientCamera_buttons(){
1050 //      if ( g_glwindow_globals.m_nMouseType == ETwoButton ) {
1051 //              return RAD_RBUTTON | RAD_SHIFT | RAD_CONTROL;
1052 //      }
1053         return RAD_MBUTTON;
1054 }
1055
1056 void XYWnd_OrientCamera( XYWnd* xywnd, int x, int y, CamWnd& camwnd ){
1057         //globalOutputStream() << Camera_getAngles( camwnd ) << "  b4\n";
1058         Vector3 point = g_vector3_identity;
1059         xywnd->XY_ToPoint( x, y, point );
1060         //xywnd->XY_SnapToGrid( point );
1061         vector3_subtract( point, Camera_getOrigin( camwnd ) );
1062
1063         int n1 = ( xywnd->GetViewType() == XY ) ? 1 : 2;
1064         int n2 = ( xywnd->GetViewType() == YZ ) ? 1 : 0;
1065         int nAngle = ( xywnd->GetViewType() == XY ) ? CAMERA_YAW : CAMERA_PITCH;
1066         if ( point[n1] || point[n2] ) {
1067                 Vector3 angles( Camera_getAngles( camwnd ) );
1068                 angles[nAngle] = static_cast<float>( radians_to_degrees( atan2( point[n1], point[n2] ) ) );
1069                 if( angles[CAMERA_YAW] < 0 )
1070                         angles[CAMERA_YAW] = angles[CAMERA_YAW] + 360;
1071                 if ( nAngle == CAMERA_PITCH ){
1072                         if( fabs( angles[CAMERA_PITCH] ) > 90 ){
1073                                 angles[CAMERA_PITCH] = ( angles[CAMERA_PITCH] > 0 ) ? ( -angles[CAMERA_PITCH] + 180 ) : ( -angles[CAMERA_PITCH] - 180 );
1074                                 if( xywnd->GetViewType() == YZ ){
1075                                         if( angles[CAMERA_YAW] < 180 ){
1076                                                 angles[CAMERA_YAW] = 360 - angles[CAMERA_YAW];
1077                                         }
1078                                 }
1079                                 else if( angles[CAMERA_YAW] < 90 || angles[CAMERA_YAW] > 270 ){
1080                                         angles[CAMERA_YAW] = 180 - angles[CAMERA_YAW];
1081                                 }
1082                         }
1083                         else{
1084                                 if( xywnd->GetViewType() == YZ ){
1085                                         if( angles[CAMERA_YAW] > 180 ){
1086                                                 angles[CAMERA_YAW] = 360 - angles[CAMERA_YAW];
1087                                         }
1088                                 }
1089                                 else if( angles[CAMERA_YAW] > 90 && angles[CAMERA_YAW] < 270 ){
1090                                         angles[CAMERA_YAW] = 180 - angles[CAMERA_YAW];
1091                                 }
1092                         }
1093                 }
1094                 Camera_setAngles( camwnd, angles );
1095         }
1096         //globalOutputStream() << Camera_getAngles( camwnd ) << "\n";
1097 }
1098
1099 unsigned int SetCustomPivotOrigin_buttons(){
1100         return RAD_MBUTTON | RAD_SHIFT;
1101 }
1102
1103 /*
1104    ==============
1105    NewBrushDrag
1106    ==============
1107  */
1108 unsigned int NewBrushDrag_buttons(){
1109         return RAD_LBUTTON;
1110 }
1111
1112 void XYWnd::NewBrushDrag_Begin( int x, int y ){
1113         m_NewBrushDrag = 0;
1114         m_nNewBrushPressx = x;
1115         m_nNewBrushPressy = y;
1116
1117         m_bNewBrushDrag = true;
1118 }
1119
1120 void XYWnd::NewBrushDrag_End( int x, int y ){
1121         if ( m_NewBrushDrag != 0 ) {
1122                 GlobalUndoSystem().finish( "brushDragNew" );
1123         }
1124 }
1125
1126 void XYWnd::NewBrushDrag( int x, int y ){
1127         Vector3 mins, maxs;
1128         XY_ToPoint( m_nNewBrushPressx, m_nNewBrushPressy, mins );
1129         XY_SnapToGrid( mins );
1130         XY_ToPoint( x, y, maxs );
1131         XY_SnapToGrid( maxs );
1132
1133         int nDim = ( m_viewType == XY ) ? 2 : ( m_viewType == YZ ) ? 0 : 1;
1134
1135         mins[nDim] = float_snapped( Select_getWorkZone().d_work_min[nDim], GetSnapGridSize() );
1136         maxs[nDim] = float_snapped( Select_getWorkZone().d_work_max[nDim], GetSnapGridSize() );
1137
1138         if ( maxs[nDim] <= mins[nDim] ) {
1139                 maxs[nDim] = mins[nDim] + GetGridSize();
1140         }
1141
1142         for ( int i = 0 ; i < 3 ; i++ )
1143         {
1144                 if ( mins[i] == maxs[i] ) {
1145                         return; // don't create a degenerate brush
1146                 }
1147                 if ( mins[i] > maxs[i] ) {
1148                         float temp = mins[i];
1149                         mins[i] = maxs[i];
1150                         maxs[i] = temp;
1151                 }
1152         }
1153
1154         if ( m_NewBrushDrag == 0 ) {
1155                 GlobalUndoSystem().start();
1156                 NodeSmartReference node( GlobalBrushCreator().createBrush() );
1157                 Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->insert( node );
1158
1159                 scene::Path brushpath( makeReference( GlobalSceneGraph().root() ) );
1160                 brushpath.push( makeReference( *Map_GetWorldspawn( g_map ) ) );
1161                 brushpath.push( makeReference( node.get() ) );
1162                 selectPath( brushpath, true );
1163
1164                 m_NewBrushDrag = node.get_pointer();
1165         }
1166
1167         // d1223m
1168         //Scene_BrushResize_Selected(GlobalSceneGraph(), aabb_for_minmax(mins, maxs), TextureBrowser_GetSelectedShader(GlobalTextureBrowser()));
1169         Scene_BrushResize_Selected( GlobalSceneGraph(), aabb_for_minmax( mins, maxs ),
1170                                                                 g_brush_always_caulk ?
1171                                                                 "textures/common/caulk" : TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
1172 }
1173
1174 void entitycreate_activated( GtkWidget* item ){
1175         scene::Node* world_node = Map_FindWorldspawn( g_map );
1176         const char* entity_name = gtk_label_get_text( GTK_LABEL( GTK_BIN( item )->child ) );
1177
1178         if ( !( world_node && string_equal( entity_name, "worldspawn" ) ) ) {
1179                 g_pParentWnd->ActiveXY()->OnEntityCreate( entity_name );
1180         }
1181         else {
1182                 GlobalRadiant().m_pfnMessageBox( GTK_WIDGET( MainFrame_getWindow() ), "There's already a worldspawn in your map!"
1183                                                                                                                                                           "",
1184                                                                                  "Info",
1185                                                                                  eMB_OK,
1186                                                                                  eMB_ICONDEFAULT );
1187         }
1188 }
1189
1190 void EntityClassMenu_addItem( GtkMenu* menu, const char* name ){
1191         GtkMenuItem* item = GTK_MENU_ITEM( gtk_menu_item_new_with_label( name ) );
1192         g_signal_connect( G_OBJECT( item ), "activate", G_CALLBACK( entitycreate_activated ), item );
1193         gtk_widget_show( GTK_WIDGET( item ) );
1194         menu_add_item( menu, item );
1195 }
1196
1197 class EntityClassMenuInserter : public EntityClassVisitor
1198 {
1199 typedef std::pair<GtkMenu*, CopiedString> MenuPair;
1200 typedef std::vector<MenuPair> MenuStack;
1201 MenuStack m_stack;
1202 CopiedString m_previous;
1203 public:
1204 EntityClassMenuInserter( GtkMenu* menu ){
1205         m_stack.reserve( 2 );
1206         m_stack.push_back( MenuPair( menu, "" ) );
1207 }
1208 ~EntityClassMenuInserter(){
1209         if ( !string_empty( m_previous.c_str() ) ) {
1210                 addItem( m_previous.c_str(), "" );
1211         }
1212 }
1213 void visit( EntityClass* e ){
1214         ASSERT_MESSAGE( !string_empty( e->name() ), "entity-class has no name" );
1215         if ( !string_empty( m_previous.c_str() ) ) {
1216                 addItem( m_previous.c_str(), e->name() );
1217         }
1218         m_previous = e->name();
1219 }
1220 void pushMenu( const CopiedString& name ){
1221         GtkMenuItem* item = GTK_MENU_ITEM( gtk_menu_item_new_with_label( name.c_str() ) );
1222         gtk_widget_show( GTK_WIDGET( item ) );
1223         container_add_widget( GTK_CONTAINER( m_stack.back().first ), GTK_WIDGET( item ) );
1224
1225         GtkMenu* submenu = GTK_MENU( gtk_menu_new() );
1226         gtk_menu_item_set_submenu( item, GTK_WIDGET( submenu ) );
1227
1228         m_stack.push_back( MenuPair( submenu, name ) );
1229 }
1230 void popMenu(){
1231         m_stack.pop_back();
1232 }
1233 void addItem( const char* name, const char* next ){
1234         const char* underscore = strchr( name, '_' );
1235
1236         if ( underscore != 0 && underscore != name ) {
1237                 bool nextEqual = string_equal_n( name, next, ( underscore + 1 ) - name );
1238                 const char* parent = m_stack.back().second.c_str();
1239
1240                 if ( !string_empty( parent )
1241                          && string_length( parent ) == std::size_t( underscore - name )
1242                          && string_equal_n( name, parent, underscore - name ) ) { // this is a child
1243                 }
1244                 else if ( nextEqual ) {
1245                         if ( m_stack.size() == 2 ) {
1246                                 popMenu();
1247                         }
1248                         pushMenu( CopiedString( StringRange( name, underscore ) ) );
1249                 }
1250                 else if ( m_stack.size() == 2 ) {
1251                         popMenu();
1252                 }
1253         }
1254         else if ( m_stack.size() == 2 ) {
1255                 popMenu();
1256         }
1257
1258         EntityClassMenu_addItem( m_stack.back().first, name );
1259 }
1260 };
1261
1262 void XYWnd::OnContextMenu(){
1263 //      if ( g_xywindow_globals.m_bRightClick == false ) {
1264 //              return;
1265 //      }
1266
1267         if ( m_mnuDrop == 0 ) { // first time, load it up
1268                 GtkMenu* menu = m_mnuDrop = GTK_MENU( gtk_menu_new() );
1269
1270                 EntityClassMenuInserter inserter( menu );
1271                 GlobalEntityClassManager().forEach( inserter );
1272         }
1273
1274         gtk_menu_popup( m_mnuDrop, 0, 0, 0, 0, 1, GDK_CURRENT_TIME );
1275 }
1276
1277 FreezePointer g_xywnd_freezePointer;
1278
1279 unsigned int Move_buttons(){
1280         return RAD_RBUTTON;
1281 }
1282
1283 void XYWnd_moveDelta( int x, int y, unsigned int state, void* data ){
1284         reinterpret_cast<XYWnd*>( data )->EntityCreate_MouseMove( x, y );
1285         reinterpret_cast<XYWnd*>( data )->Scroll( -x, y );
1286 }
1287
1288 gboolean XYWnd_Move_focusOut( GtkWidget* widget, GdkEventFocus* event, XYWnd* xywnd ){
1289         xywnd->Move_End();
1290         return FALSE;
1291 }
1292
1293 void XYWnd::Move_Begin(){
1294         if ( m_move_started ) {
1295                 Move_End();
1296         }
1297         m_move_started = true;
1298         g_xywnd_freezePointer.freeze_pointer( m_parent != 0 ? m_parent : MainFrame_getWindow(), m_gl_widget, XYWnd_moveDelta, this );
1299         m_move_focusOut = g_signal_connect( G_OBJECT( m_gl_widget ), "focus_out_event", G_CALLBACK( XYWnd_Move_focusOut ), this );
1300 }
1301
1302 void XYWnd::Move_End(){
1303         m_move_started = false;
1304         g_xywnd_freezePointer.unfreeze_pointer( m_parent != 0 ? m_parent : MainFrame_getWindow(), false );
1305         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_move_focusOut );
1306 }
1307
1308 unsigned int Zoom_buttons(){
1309         return RAD_RBUTTON | RAD_ALT;
1310 }
1311
1312 int g_dragZoom = 0;
1313 int g_zoom2x = 0;
1314 int g_zoom2y = 0;
1315
1316 void XYWnd_zoomDelta( int x, int y, unsigned int state, void* data ){
1317         if ( y != 0 ) {
1318                 g_dragZoom += y;
1319                 while ( abs( g_dragZoom ) > 8 )
1320                 {
1321                         if ( g_dragZoom > 0 ) {
1322                                 reinterpret_cast<XYWnd*>( data )->ZoomOut();
1323                                 g_dragZoom -= 8;
1324                         }
1325                         else
1326                         {
1327                                 if ( g_xywindow_globals.m_bZoomInToPointer ) {
1328                                         reinterpret_cast<XYWnd*>( data )->ZoomInWithMouse( g_zoom2x, g_zoom2y );
1329                                 }
1330                                 else{
1331                                         reinterpret_cast<XYWnd*>( data )->ZoomIn();
1332                                 }
1333                                 g_dragZoom += 8;
1334                         }
1335                 }
1336         }
1337 }
1338
1339 gboolean XYWnd_Zoom_focusOut( GtkWidget* widget, GdkEventFocus* event, XYWnd* xywnd ){
1340         xywnd->Zoom_End();
1341         return FALSE;
1342 }
1343
1344 void XYWnd::Zoom_Begin( int x, int y ){
1345         if ( m_zoom_started ) {
1346                 Zoom_End();
1347         }
1348         m_zoom_started = true;
1349         g_dragZoom = 0;
1350         g_zoom2x = x;
1351         g_zoom2y = y;
1352         g_xywnd_freezePointer.freeze_pointer( m_parent != 0 ? m_parent : MainFrame_getWindow(), m_gl_widget, XYWnd_zoomDelta, this );
1353         m_zoom_focusOut = g_signal_connect( G_OBJECT( m_gl_widget ), "focus_out_event", G_CALLBACK( XYWnd_Zoom_focusOut ), this );
1354 }
1355
1356 void XYWnd::Zoom_End(){
1357         m_zoom_started = false;
1358         g_xywnd_freezePointer.unfreeze_pointer( m_parent != 0 ? m_parent : MainFrame_getWindow(), false );
1359         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_zoom_focusOut );
1360 }
1361
1362 // makes sure the selected brush or camera is in view
1363 void XYWnd::PositionView( const Vector3& position ){
1364         int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
1365         int nDim2 = ( m_viewType == XY ) ? 1 : 2;
1366
1367         m_vOrigin[nDim1] = position[nDim1];
1368         m_vOrigin[nDim2] = position[nDim2];
1369
1370         updateModelview();
1371
1372         XYWnd_Update( *this );
1373 }
1374
1375 void XYWnd::SetViewType( VIEWTYPE viewType ){
1376         m_viewType = viewType;
1377         updateModelview();
1378
1379         if ( m_parent != 0 ) {
1380                 gtk_window_set_title( m_parent, ViewType_getTitle( m_viewType ) );
1381         }
1382 }
1383
1384
1385 inline WindowVector WindowVector_forInteger( int x, int y ){
1386         return WindowVector( static_cast<float>( x ), static_cast<float>( y ) );
1387 }
1388
1389 void XYWnd::mouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
1390         XY_MouseDown( static_cast<int>( position.x() ), static_cast<int>( position.y() ), buttons_for_button_and_modifiers( button, modifiers ) );
1391 }
1392 void XYWnd::XY_MouseDown( int x, int y, unsigned int buttons ){
1393         if ( buttons == Move_buttons() ) {
1394                 Move_Begin();
1395                 EntityCreate_MouseDown( x, y );
1396         }
1397         else if ( buttons == Zoom_buttons() ) {
1398                 Zoom_Begin( x, y );
1399         }
1400         else if ( ClipMode() && ( buttons == Clipper_buttons() || buttons == Clipper_quick_buttons() ) ) {
1401                 Clipper_OnLButtonDown( x, y );
1402         }
1403         else if ( !ClipMode() && buttons == Clipper_quick_buttons() ) {
1404                 ClipperMode();
1405                 g_quick_clipper = true;
1406                 Clipper_OnLButtonDown( x, y );
1407         }
1408         else if ( buttons == NewBrushDrag_buttons() && GlobalSelectionSystem().countSelected() == 0 ) {
1409                 NewBrushDrag_Begin( x, y );
1410         }
1411         // control mbutton = move camera
1412         else if ( buttons == MoveCamera_buttons() ) {
1413                 XYWnd_PositionCamera( this, x, y, *g_pParentWnd->GetCamWnd() );
1414         }
1415         // mbutton = angle camera
1416         else if ( buttons == OrientCamera_buttons() ) {
1417                 XYWnd_OrientCamera( this, x, y, *g_pParentWnd->GetCamWnd() );
1418         }
1419         else if ( buttons == SetCustomPivotOrigin_buttons() ) {
1420                 SetCustomPivotOrigin( x, y );
1421         }
1422         else
1423         {
1424                 m_window_observer->onMouseDown( WindowVector_forInteger( x, y ), button_for_flags( buttons ), modifiers_for_flags( buttons ) );
1425         }
1426 }
1427
1428 void XYWnd::XY_MouseUp( int x, int y, unsigned int buttons ){
1429         if ( m_move_started ) {
1430                 Move_End();
1431                 EntityCreate_MouseUp( x, y );
1432         }
1433         else if ( m_zoom_started ) {
1434                 Zoom_End();
1435         }
1436         else if ( ClipMode() && ( buttons == Clipper_buttons() || buttons == Clipper_quick_buttons() ) ) {
1437                 Clipper_OnLButtonUp( x, y );
1438         }
1439         else if ( m_bNewBrushDrag ) {
1440                 m_bNewBrushDrag = false;
1441                 NewBrushDrag_End( x, y );
1442                 if ( m_NewBrushDrag == 0 ) {
1443                         //L button w/o created brush = tunnel selection
1444                         m_window_observer->onMouseUp( WindowVector_forInteger( x, y ), button_for_flags( buttons ), modifiers_for_flags( buttons ) );
1445                 }
1446         }
1447         else
1448         {
1449                 m_window_observer->onMouseUp( WindowVector_forInteger( x, y ), button_for_flags( buttons ), modifiers_for_flags( buttons ) );
1450         }
1451 }
1452
1453 void XYWnd::XY_MouseMoved( int x, int y, unsigned int buttons ){
1454         // rbutton = drag xy origin
1455         if ( m_move_started ) {
1456         }
1457         // zoom in/out
1458         else if ( m_zoom_started ) {
1459         }
1460
1461         else if ( ClipMode() && g_pMovingClip != 0 ) {
1462                 Clipper_OnMouseMoved( x, y );
1463         }
1464         // lbutton without selection = drag new brush
1465         else if ( m_bNewBrushDrag ) {
1466                 NewBrushDrag( x, y );
1467         }
1468
1469         // control mbutton = move camera
1470         else if ( buttons == MoveCamera_buttons() ) {
1471                 XYWnd_PositionCamera( this, x, y, *g_pParentWnd->GetCamWnd() );
1472         }
1473
1474         // mbutton = angle camera
1475         else if ( buttons == OrientCamera_buttons() ) {
1476                 XYWnd_OrientCamera( this, x, y, *g_pParentWnd->GetCamWnd() );
1477         }
1478
1479         else if ( buttons == SetCustomPivotOrigin_buttons() ) {
1480                 SetCustomPivotOrigin( x, y );
1481         }
1482
1483         else
1484         {
1485                 m_window_observer->onMouseMotion( WindowVector_forInteger( x, y ), modifiers_for_flags( buttons ) );
1486
1487                 m_mousePosition[0] = m_mousePosition[1] = m_mousePosition[2] = 0.0;
1488                 XY_ToPoint( x, y, m_mousePosition );
1489                 XY_SnapToGrid( m_mousePosition );
1490
1491                 StringOutputStream status( 64 );
1492                 status << "x:: " << FloatFormat( m_mousePosition[0], 6, 1 )
1493                            << "  y:: " << FloatFormat( m_mousePosition[1], 6, 1 )
1494                            << "  z:: " << FloatFormat( m_mousePosition[2], 6, 1 );
1495                 g_pParentWnd->SetStatusText( g_pParentWnd->m_position_status, status.c_str() );
1496
1497                 if ( g_bCrossHairs ) {
1498                         XYWnd_Update( *this );
1499                 }
1500
1501                 Clipper_Crosshair_OnMouseMoved( x, y );
1502         }
1503 }
1504
1505 void XYWnd::EntityCreate_MouseDown( int x, int y ){
1506         m_entityCreate = true;
1507         m_entityCreate_x = x;
1508         m_entityCreate_y = y;
1509 }
1510
1511 void XYWnd::EntityCreate_MouseMove( int x, int y ){
1512         if ( m_entityCreate && ( m_entityCreate_x != x || m_entityCreate_y != y ) ) {
1513                 m_entityCreate = false;
1514         }
1515 }
1516
1517 void XYWnd::EntityCreate_MouseUp( int x, int y ){
1518         if ( m_entityCreate ) {
1519                 m_entityCreate = false;
1520                 OnContextMenu();
1521         }
1522 }
1523
1524 inline float screen_normalised( int pos, unsigned int size ){
1525         return ( ( 2.0f * pos ) / size ) - 1.0f;
1526 }
1527
1528 inline float normalised_to_world( float normalised, float world_origin, float normalised2world_scale ){
1529         return world_origin + normalised * normalised2world_scale;
1530 }
1531
1532
1533 // TTimo: watch it, this doesn't init one of the 3 coords
1534 void XYWnd::XY_ToPoint( int x, int y, Vector3& point ){
1535         float normalised2world_scale_x = m_nWidth / 2 / m_fScale;
1536         float normalised2world_scale_y = m_nHeight / 2 / m_fScale;
1537         if ( m_viewType == XY ) {
1538                 point[0] = normalised_to_world( screen_normalised( x, m_nWidth ), m_vOrigin[0], normalised2world_scale_x );
1539                 point[1] = normalised_to_world( -screen_normalised( y, m_nHeight ), m_vOrigin[1], normalised2world_scale_y );
1540         }
1541         else if ( m_viewType == YZ ) {
1542                 point[1] = normalised_to_world( screen_normalised( x, m_nWidth ), m_vOrigin[1], normalised2world_scale_x );
1543                 point[2] = normalised_to_world( -screen_normalised( y, m_nHeight ), m_vOrigin[2], normalised2world_scale_y );
1544         }
1545         else
1546         {
1547                 point[0] = normalised_to_world( screen_normalised( x, m_nWidth ), m_vOrigin[0], normalised2world_scale_x );
1548                 point[2] = normalised_to_world( -screen_normalised( y, m_nHeight ), m_vOrigin[2], normalised2world_scale_y );
1549         }
1550 }
1551
1552 void XYWnd::XY_SnapToGrid( Vector3& point ){
1553         if ( m_viewType == XY ) {
1554                 point[0] = float_snapped( point[0], GetSnapGridSize() );
1555                 point[1] = float_snapped( point[1], GetSnapGridSize() );
1556         }
1557         else if ( m_viewType == YZ ) {
1558                 point[1] = float_snapped( point[1], GetSnapGridSize() );
1559                 point[2] = float_snapped( point[2], GetSnapGridSize() );
1560         }
1561         else
1562         {
1563                 point[0] = float_snapped( point[0], GetSnapGridSize() );
1564                 point[2] = float_snapped( point[2], GetSnapGridSize() );
1565         }
1566 }
1567
1568 void XYWnd::XY_LoadBackgroundImage( const char *name ){
1569         const char* relative = path_make_relative( name, GlobalFileSystem().findRoot( name ) );
1570         if ( relative == name ) {
1571                 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
1572         }
1573
1574         char fileNameWithoutExt[512];
1575         strncpy( fileNameWithoutExt, relative, sizeof( fileNameWithoutExt ) - 1 );
1576         fileNameWithoutExt[512 - 1] = '\0';
1577         fileNameWithoutExt[strlen( fileNameWithoutExt ) - 4] = '\0';
1578
1579         Image *image = QERApp_LoadImage( 0, fileNameWithoutExt );
1580         if ( !image ) {
1581                 globalOutputStream() << "Could not load texture " << fileNameWithoutExt << "\n";
1582                 return;
1583         }
1584         g_pParentWnd->ActiveXY()->m_tex = (qtexture_t*)malloc( sizeof( qtexture_t ) );
1585         LoadTextureRGBA( g_pParentWnd->ActiveXY()->XYWnd::m_tex, image->getRGBAPixels(), image->getWidth(), image->getHeight() );
1586         globalOutputStream() << "Loaded background texture " << relative << "\n";
1587         g_pParentWnd->ActiveXY()->m_backgroundActivated = true;
1588
1589         int m_ix, m_iy;
1590         switch ( g_pParentWnd->ActiveXY()->m_viewType )
1591         {
1592         case XY:
1593                 m_ix = 0;
1594                 m_iy = 1;
1595                 break;
1596         case XZ:
1597                 m_ix = 0;
1598                 m_iy = 2;
1599                 break;
1600         case YZ:
1601                 m_ix = 1;
1602                 m_iy = 2;
1603                 break;
1604         }
1605
1606         Vector3 min, max;
1607         Select_GetBounds( min, max );
1608         g_pParentWnd->ActiveXY()->m_xmin = min[m_ix];
1609         g_pParentWnd->ActiveXY()->m_ymin = min[m_iy];
1610         g_pParentWnd->ActiveXY()->m_xmax = max[m_ix];
1611         g_pParentWnd->ActiveXY()->m_ymax = max[m_iy];
1612 }
1613
1614 void XYWnd::XY_DisableBackground( void ){
1615         g_pParentWnd->ActiveXY()->m_backgroundActivated = false;
1616         if ( g_pParentWnd->ActiveXY()->m_tex ) {
1617                 free( g_pParentWnd->ActiveXY()->m_tex );
1618         }
1619         g_pParentWnd->ActiveXY()->m_tex = NULL;
1620 }
1621
1622 void WXY_BackgroundSelect( void ){
1623         bool brushesSelected = Scene_countSelectedBrushes( GlobalSceneGraph() ) != 0;
1624         if ( !brushesSelected ) {
1625                 gtk_MessageBox( 0, "You have to select some brushes to get the bounding box for.\n",
1626                                                 "No selection", eMB_OK, eMB_ICONERROR );
1627                 return;
1628         }
1629
1630         const char *filename = file_dialog( GTK_WIDGET( MainFrame_getWindow() ), TRUE, "Background Image", NULL, NULL );
1631         g_pParentWnd->ActiveXY()->XY_DisableBackground();
1632         if ( filename ) {
1633                 g_pParentWnd->ActiveXY()->XY_LoadBackgroundImage( filename );
1634         }
1635 }
1636
1637 /*
1638    ============================================================================
1639
1640    DRAWING
1641
1642    ============================================================================
1643  */
1644
1645 /*
1646    ==============
1647    XY_DrawGrid
1648    ==============
1649  */
1650
1651 double two_to_the_power( int power ){
1652         return pow( 2.0f, power );
1653 }
1654
1655 void XYWnd::XY_DrawAxis( void ){
1656         if ( g_xywindow_globals_private.show_axis ) {
1657                 const char g_AxisName[3] = { 'X', 'Y', 'Z' };
1658                 const int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
1659                 const int nDim2 = ( m_viewType == XY ) ? 1 : 2;
1660                 const int w = ( m_nWidth / 2 / m_fScale );
1661                 const int h = ( m_nHeight / 2 / m_fScale );
1662
1663                 Vector3 colourX = ( m_viewType == YZ ) ? g_xywindow_globals.AxisColorY : g_xywindow_globals.AxisColorX;
1664                 Vector3 colourY = ( m_viewType == XY ) ? g_xywindow_globals.AxisColorY : g_xywindow_globals.AxisColorZ;
1665                 if( !Active() ){
1666                         float grayX = vector3_dot( colourX, Vector3( 0.2989, 0.5870, 0.1140 ) );
1667                         float grayY = vector3_dot( colourY, Vector3( 0.2989, 0.5870, 0.1140 ) );
1668                         colourX[0] = colourX[1] = colourX[2] = grayX;
1669                         colourY[0] = colourY[1] = colourY[2] = grayY;
1670                 }
1671
1672                 // draw two lines with corresponding axis colors to highlight current view
1673                 // horizontal line: nDim1 color
1674                 glLineWidth( 2 );
1675                 glBegin( GL_LINES );
1676                 glColor3fv( vector3_to_array( colourX ) );
1677                 glVertex2f( m_vOrigin[nDim1] - w + 40 / m_fScale, m_vOrigin[nDim2] + h - 45 / m_fScale );
1678                 glVertex2f( m_vOrigin[nDim1] - w + 65 / m_fScale, m_vOrigin[nDim2] + h - 45 / m_fScale );
1679                 glVertex2f( 0, 0 );
1680                 glVertex2f( 32 / m_fScale, 0 );
1681                 glColor3fv( vector3_to_array( colourY ) );
1682                 glVertex2f( m_vOrigin[nDim1] - w + 40 / m_fScale, m_vOrigin[nDim2] + h - 45 / m_fScale );
1683                 glVertex2f( m_vOrigin[nDim1] - w + 40 / m_fScale, m_vOrigin[nDim2] + h - 20 / m_fScale );
1684                 glVertex2f( 0, 0 );
1685                 glVertex2f( 0, 32 / m_fScale );
1686                 glEnd();
1687                 glLineWidth( 1 );
1688                 // now print axis symbols
1689                 glColor3fv( vector3_to_array( colourX ) );
1690                 glRasterPos2f( m_vOrigin[nDim1] - w + 55 / m_fScale, m_vOrigin[nDim2] + h - 55 / m_fScale );
1691                 GlobalOpenGL().drawChar( g_AxisName[nDim1] );
1692                 glRasterPos2f( 28 / m_fScale, -10 / m_fScale );
1693                 GlobalOpenGL().drawChar( g_AxisName[nDim1] );
1694                 glColor3fv( vector3_to_array( colourY ) );
1695                 glRasterPos2f( m_vOrigin[nDim1] - w + 25 / m_fScale, m_vOrigin[nDim2] + h - 30 / m_fScale );
1696                 GlobalOpenGL().drawChar( g_AxisName[nDim2] );
1697                 glRasterPos2f( -10 / m_fScale, 28 / m_fScale );
1698                 GlobalOpenGL().drawChar( g_AxisName[nDim2] );
1699         }
1700 }
1701
1702 void XYWnd::RenderActive( void ){
1703         if ( glwidget_make_current( m_gl_widget ) != FALSE ) {
1704                 if ( Map_Valid( g_map ) && ScreenUpdates_Enabled() ) {
1705                         GlobalOpenGL_debugAssertNoErrors();
1706                         glDrawBuffer( GL_FRONT );
1707
1708                         if ( g_xywindow_globals_private.show_outline ) {
1709                                 glMatrixMode( GL_PROJECTION );
1710                                 glLoadIdentity();
1711                                 glOrtho( 0, m_nWidth, 0, m_nHeight, 0, 1 );
1712
1713                                 glMatrixMode( GL_MODELVIEW );
1714                                 glLoadIdentity();
1715
1716                                 if( !Active() ){ //sorta erase
1717                                         glColor3fv( vector3_to_array( g_xywindow_globals.color_gridmajor ) );
1718                                 }
1719                                 // four view mode doesn't colorize
1720                                 else if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit ) {
1721                                         glColor3fv( vector3_to_array( g_xywindow_globals.color_viewname ) );
1722                                 }
1723                                 else
1724                                 {
1725                                         switch ( m_viewType )
1726                                         {
1727                                         case YZ:
1728                                                 glColor3fv( vector3_to_array( g_xywindow_globals.AxisColorX ) );
1729                                                 break;
1730                                         case XZ:
1731                                                 glColor3fv( vector3_to_array( g_xywindow_globals.AxisColorY ) );
1732                                                 break;
1733                                         case XY:
1734                                                 glColor3fv( vector3_to_array( g_xywindow_globals.AxisColorZ ) );
1735                                                 break;
1736                                         }
1737                                 }
1738                                 glBegin( GL_LINE_LOOP );
1739                                 glVertex2f( 0.5, 0.5 );
1740                                 glVertex2f( m_nWidth - 0.5, 1 );
1741                                 glVertex2f( m_nWidth - 0.5, m_nHeight - 0.5 );
1742                                 glVertex2f( 0.5, m_nHeight - 0.5 );
1743                                 glEnd();
1744                         }
1745                         // we do this part (the old way) only if show_axis is disabled
1746                         if ( !g_xywindow_globals_private.show_axis ) {
1747                                 glMatrixMode( GL_PROJECTION );
1748                                 glLoadIdentity();
1749                                 glOrtho( 0, m_nWidth, 0, m_nHeight, 0, 1 );
1750
1751                                 glMatrixMode( GL_MODELVIEW );
1752                                 glLoadIdentity();
1753
1754                                 if ( Active() ) {
1755                                         glColor3fv( vector3_to_array( g_xywindow_globals.color_viewname ) );
1756                                 }
1757                                 else{
1758                                         glColor4fv( vector4_to_array( Vector4( g_xywindow_globals.color_gridtext, 1.0f ) ) );
1759                                 }
1760
1761                                 glDisable( GL_BLEND );
1762                                 glRasterPos2f( 35, m_nHeight - 20 );
1763
1764                                 GlobalOpenGL().drawString( ViewType_getTitle( m_viewType ) );
1765                         }
1766                         else{
1767                                 // clear
1768                                 glViewport( 0, 0, m_nWidth, m_nHeight );
1769                                 // set up viewpoint
1770                                 glMatrixMode( GL_PROJECTION );
1771                                 glLoadMatrixf( reinterpret_cast<const float*>( &m_projection ) );
1772
1773                                 glMatrixMode( GL_MODELVIEW );
1774                                 glLoadIdentity();
1775                                 glScalef( m_fScale, m_fScale, 1 );
1776                                 int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
1777                                 int nDim2 = ( m_viewType == XY ) ? 1 : 2;
1778                                 glTranslatef( -m_vOrigin[nDim1], -m_vOrigin[nDim2], 0 );
1779
1780                                 glDisable( GL_LINE_STIPPLE );
1781                                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1782                                 glDisableClientState( GL_NORMAL_ARRAY );
1783                                 glDisableClientState( GL_COLOR_ARRAY );
1784                                 glDisable( GL_TEXTURE_2D );
1785                                 glDisable( GL_LIGHTING );
1786                                 glDisable( GL_COLOR_MATERIAL );
1787                                 glDisable( GL_DEPTH_TEST );
1788                                 glDisable( GL_TEXTURE_1D );
1789                                 glDisable( GL_BLEND );
1790
1791                                 XYWnd::XY_DrawAxis();
1792                         }
1793
1794                         glDrawBuffer( GL_BACK );
1795                         GlobalOpenGL_debugAssertNoErrors();
1796                         glwidget_make_current( m_gl_widget );
1797                 }
1798         }
1799 }
1800
1801 void XYWnd::XY_DrawBackground( void ){
1802         glPushAttrib( GL_ALL_ATTRIB_BITS );
1803
1804         glEnable( GL_TEXTURE_2D );
1805         glEnable( GL_BLEND );
1806         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1807         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1808         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
1809         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
1810
1811         glPolygonMode( GL_FRONT, GL_FILL );
1812
1813         glBindTexture( GL_TEXTURE_2D, m_tex->texture_number );
1814         glBegin( GL_QUADS );
1815
1816         glColor4f( 1.0, 1.0, 1.0, m_alpha );
1817         glTexCoord2f( 0.0, 1.0 );
1818         glVertex2f( m_xmin, m_ymin );
1819
1820         glTexCoord2f( 1.0, 1.0 );
1821         glVertex2f( m_xmax, m_ymin );
1822
1823         glTexCoord2f( 1.0, 0.0 );
1824         glVertex2f( m_xmax, m_ymax );
1825
1826         glTexCoord2f( 0.0, 0.0 );
1827         glVertex2f( m_xmin, m_ymax );
1828
1829         glEnd();
1830         glBindTexture( GL_TEXTURE_2D, 0 );
1831
1832         glPopAttrib();
1833 }
1834
1835 void XYWnd::XY_DrawGrid( void ) {
1836         float x, y, xb, xe, yb, ye;
1837         float w, h, a;
1838         char text[32];
1839         float step, minor_step, stepx, stepy;
1840         step = minor_step = stepx = stepy = GetGridSize();
1841
1842         int minor_power = Grid_getPower();
1843         int mask;
1844
1845         while ( ( minor_step * m_fScale ) <= 4.0f ) { // make sure minor grid spacing is at least 4 pixels on the screen
1846                 ++minor_power;
1847                 minor_step *= 2;
1848         }
1849         int power = minor_power;
1850         while ( ( power % 3 ) != 0 || ( step * m_fScale ) <= 32.0f ) { // make sure major grid spacing is at least 32 pixels on the screen
1851                 ++power;
1852                 step = float(two_to_the_power( power ) );
1853         }
1854         mask = ( 1 << ( power - minor_power ) ) - 1;
1855         while ( ( stepx * m_fScale ) <= 32.0f ) // text step x must be at least 32
1856                 stepx *= 2;
1857         while ( ( stepy * m_fScale ) <= 32.0f ) // text step y must be at least 32
1858                 stepy *= 2;
1859
1860         a = ( ( GetSnapGridSize() > 0.0f ) ? 1.0f : 0.3f );
1861
1862         glDisable( GL_TEXTURE_2D );
1863         glDisable( GL_TEXTURE_1D );
1864         glDisable( GL_DEPTH_TEST );
1865         glDisable( GL_BLEND );
1866         glLineWidth( 1 );
1867
1868         w = ( m_nWidth / 2 / m_fScale );
1869         h = ( m_nHeight / 2 / m_fScale );
1870
1871         const int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
1872         const int nDim2 = ( m_viewType == XY ) ? 1 : 2;
1873
1874         xb = m_vOrigin[nDim1] - w;
1875         if ( xb < region_mins[nDim1] ) {
1876                 xb = region_mins[nDim1];
1877         }
1878         xb = step * floor( xb / step );
1879
1880         xe = m_vOrigin[nDim1] + w;
1881         if ( xe > region_maxs[nDim1] ) {
1882                 xe = region_maxs[nDim1];
1883         }
1884         xe = step * ceil( xe / step );
1885
1886         yb = m_vOrigin[nDim2] - h;
1887         if ( yb < region_mins[nDim2] ) {
1888                 yb = region_mins[nDim2];
1889         }
1890         yb = step * floor( yb / step );
1891
1892         ye = m_vOrigin[nDim2] + h;
1893         if ( ye > region_maxs[nDim2] ) {
1894                 ye = region_maxs[nDim2];
1895         }
1896         ye = step * ceil( ye / step );
1897
1898 #define COLORS_DIFFER( a,b ) \
1899         ( ( a )[0] != ( b )[0] || \
1900           ( a )[1] != ( b )[1] || \
1901           ( a )[2] != ( b )[2] )
1902
1903         // djbob
1904         // draw minor blocks
1905         if ( g_xywindow_globals_private.d_showgrid || a < 1.0f ) {
1906                 if ( a < 1.0f ) {
1907                         glEnable( GL_BLEND );
1908                 }
1909
1910                 if ( COLORS_DIFFER( g_xywindow_globals.color_gridminor, g_xywindow_globals.color_gridback ) ) {
1911                         glColor4fv( vector4_to_array( Vector4( g_xywindow_globals.color_gridminor, a ) ) );
1912
1913                         glBegin( GL_LINES );
1914                         int i = 0;
1915                         for ( x = xb ; x < xe ; x += minor_step, ++i ) {
1916                                 if ( ( i & mask ) != 0 ) {
1917                                         glVertex2f( x, yb );
1918                                         glVertex2f( x, ye );
1919                                 }
1920                         }
1921                         i = 0;
1922                         for ( y = yb ; y < ye ; y += minor_step, ++i ) {
1923                                 if ( ( i & mask ) != 0 ) {
1924                                         glVertex2f( xb, y );
1925                                         glVertex2f( xe, y );
1926                                 }
1927                         }
1928                         glEnd();
1929                 }
1930
1931                 // draw major blocks
1932                 if ( COLORS_DIFFER( g_xywindow_globals.color_gridmajor, g_xywindow_globals.color_gridminor ) ) {
1933                         glColor4fv( vector4_to_array( Vector4( g_xywindow_globals.color_gridmajor, a ) ) );
1934
1935                         glBegin( GL_LINES );
1936                         for ( x = xb ; x <= xe ; x += step ) {
1937                                 glVertex2f( x, yb );
1938                                 glVertex2f( x, ye );
1939                         }
1940                         for ( y = yb ; y <= ye ; y += step ) {
1941                                 glVertex2f( xb, y );
1942                                 glVertex2f( xe, y );
1943                         }
1944                         glEnd();
1945                 }
1946
1947                 if ( a < 1.0f ) {
1948                         glDisable( GL_BLEND );
1949                 }
1950         }
1951
1952         // draw coordinate text if needed
1953         if ( g_xywindow_globals_private.show_coordinates ) {
1954                 glColor4fv( vector4_to_array( Vector4( g_xywindow_globals.color_gridtext, 1.0f ) ) );
1955                 float offx = m_vOrigin[nDim2] + h - ( 4 + GlobalOpenGL().m_font->getPixelAscent() ) / m_fScale;
1956                 float offy = m_vOrigin[nDim1] - w +  4                                            / m_fScale;
1957                 for ( x = xb - fmod( xb, stepx ); x <= xe ; x += stepx ) {
1958                         glRasterPos2f( x, offx );
1959                         sprintf( text, "%g", x );
1960                         GlobalOpenGL().drawString( text );
1961                 }
1962                 for ( y = yb - fmod( yb, stepy ); y <= ye ; y += stepy ) {
1963                         glRasterPos2f( offy, y );
1964                         sprintf( text, "%g", y );
1965                         GlobalOpenGL().drawString( text );
1966                 }
1967
1968         }
1969         // we do this part (the old way) only if show_axis is disabled
1970         if ( !g_xywindow_globals_private.show_axis ) {
1971                 if ( Active() ) {
1972                         glColor3fv( vector3_to_array( g_xywindow_globals.color_viewname ) );
1973                 }
1974                 else{
1975                         glColor4fv( vector4_to_array( Vector4( g_xywindow_globals.color_gridtext, 1.0f ) ) );
1976                 }
1977
1978                 glRasterPos2f( m_vOrigin[nDim1] - w + 35 / m_fScale, m_vOrigin[nDim2] + h - 20 / m_fScale );
1979
1980                 GlobalOpenGL().drawString( ViewType_getTitle( m_viewType ) );
1981         }
1982
1983         XYWnd::XY_DrawAxis();
1984
1985         // show current work zone?
1986         // the work zone is used to place dropped points and brushes
1987         if ( g_xywindow_globals_private.d_show_work ) {
1988                 glColor4f( 1.0f, 0.0f, 0.0f, 1.0f );
1989                 glBegin( GL_LINES );
1990                 glVertex2f( xb, Select_getWorkZone().d_work_min[nDim2] );
1991                 glVertex2f( xe, Select_getWorkZone().d_work_min[nDim2] );
1992                 glVertex2f( xb, Select_getWorkZone().d_work_max[nDim2] );
1993                 glVertex2f( xe, Select_getWorkZone().d_work_max[nDim2] );
1994                 glVertex2f( Select_getWorkZone().d_work_min[nDim1], yb );
1995                 glVertex2f( Select_getWorkZone().d_work_min[nDim1], ye );
1996                 glVertex2f( Select_getWorkZone().d_work_max[nDim1], yb );
1997                 glVertex2f( Select_getWorkZone().d_work_max[nDim1], ye );
1998                 glEnd();
1999         }
2000 }
2001
2002 /*
2003    ==============
2004    XY_DrawBlockGrid
2005    ==============
2006  */
2007 void XYWnd::XY_DrawBlockGrid(){
2008         if ( Map_FindWorldspawn( g_map ) == 0 ) {
2009                 return;
2010         }
2011         const char *value = Node_getEntity( *Map_GetWorldspawn( g_map ) )->getKeyValue( "_blocksize" );
2012         if ( strlen( value ) ) {
2013                 sscanf( value, "%i", &g_xywindow_globals_private.blockSize );
2014         }
2015
2016         if ( !g_xywindow_globals_private.blockSize || g_xywindow_globals_private.blockSize > 65536 || g_xywindow_globals_private.blockSize < 1024 ) {
2017                 // don't use custom blocksize if it is less than the default, or greater than the maximum world coordinate
2018                 g_xywindow_globals_private.blockSize = 1024;
2019         }
2020
2021         float x, y, xb, xe, yb, ye;
2022         float w, h;
2023         char text[32];
2024
2025         glDisable( GL_TEXTURE_2D );
2026         glDisable( GL_TEXTURE_1D );
2027         glDisable( GL_DEPTH_TEST );
2028         glDisable( GL_BLEND );
2029
2030         w = ( m_nWidth / 2 / m_fScale );
2031         h = ( m_nHeight / 2 / m_fScale );
2032
2033         int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
2034         int nDim2 = ( m_viewType == XY ) ? 1 : 2;
2035
2036         xb = m_vOrigin[nDim1] - w;
2037         if ( xb < region_mins[nDim1] ) {
2038                 xb = region_mins[nDim1];
2039         }
2040         xb = static_cast<float>( g_xywindow_globals_private.blockSize * floor( xb / g_xywindow_globals_private.blockSize ) );
2041
2042         xe = m_vOrigin[nDim1] + w;
2043         if ( xe > region_maxs[nDim1] ) {
2044                 xe = region_maxs[nDim1];
2045         }
2046         xe = static_cast<float>( g_xywindow_globals_private.blockSize * ceil( xe / g_xywindow_globals_private.blockSize ) );
2047
2048         yb = m_vOrigin[nDim2] - h;
2049         if ( yb < region_mins[nDim2] ) {
2050                 yb = region_mins[nDim2];
2051         }
2052         yb = static_cast<float>( g_xywindow_globals_private.blockSize * floor( yb / g_xywindow_globals_private.blockSize ) );
2053
2054         ye = m_vOrigin[nDim2] + h;
2055         if ( ye > region_maxs[nDim2] ) {
2056                 ye = region_maxs[nDim2];
2057         }
2058         ye = static_cast<float>( g_xywindow_globals_private.blockSize * ceil( ye / g_xywindow_globals_private.blockSize ) );
2059
2060         // draw major blocks
2061
2062         glColor3fv( vector3_to_array( g_xywindow_globals.color_gridblock ) );
2063         glLineWidth( 2 );
2064
2065         glBegin( GL_LINES );
2066
2067         for ( x = xb ; x <= xe ; x += g_xywindow_globals_private.blockSize )
2068         {
2069                 glVertex2f( x, yb );
2070                 glVertex2f( x, ye );
2071         }
2072
2073         if ( m_viewType == XY ) {
2074                 for ( y = yb ; y <= ye ; y += g_xywindow_globals_private.blockSize )
2075                 {
2076                         glVertex2f( xb, y );
2077                         glVertex2f( xe, y );
2078                 }
2079         }
2080
2081         glEnd();
2082         glLineWidth( 1 );
2083
2084         // draw coordinate text if needed
2085
2086         if ( m_viewType == XY && m_fScale > .1 ) {
2087                 for ( x = xb ; x < xe ; x += g_xywindow_globals_private.blockSize )
2088                         for ( y = yb ; y < ye ; y += g_xywindow_globals_private.blockSize )
2089                         {
2090                                 glRasterPos2f( x + ( g_xywindow_globals_private.blockSize / 2 ), y + ( g_xywindow_globals_private.blockSize / 2 ) );
2091                                 sprintf( text, "%i,%i",(int)floor( x / g_xywindow_globals_private.blockSize ), (int)floor( y / g_xywindow_globals_private.blockSize ) );
2092                                 GlobalOpenGL().drawString( text );
2093                         }
2094         }
2095
2096         glColor4f( 0, 0, 0, 0 );
2097 }
2098
2099 void XYWnd::DrawCameraIcon( const Vector3& origin, const Vector3& angles ){
2100         Cam.fov = 48 / m_fScale;
2101         Cam.box = 16 / m_fScale;
2102 //      globalOutputStream() << "pitch " << angles[CAMERA_PITCH] << "   yaw " << angles[CAMERA_YAW] << "\n";
2103
2104         if ( m_viewType == XY ) {
2105                 Cam.x = origin[0];
2106                 Cam.y = origin[1];
2107                 Cam.a = degrees_to_radians( angles[CAMERA_YAW] );
2108         }
2109         else if ( m_viewType == YZ ) {
2110                 Cam.x = origin[1];
2111                 Cam.y = origin[2];
2112                 Cam.a = degrees_to_radians( ( angles[CAMERA_YAW] > 180 ) ? ( 180.0f - angles[CAMERA_PITCH] ) : angles[CAMERA_PITCH] );
2113         }
2114         else
2115         {
2116                 Cam.x = origin[0];
2117                 Cam.y = origin[2];
2118                 Cam.a = degrees_to_radians( ( angles[CAMERA_YAW] < 270 && angles[CAMERA_YAW] > 90 ) ? ( 180.0f - angles[CAMERA_PITCH] ) : angles[CAMERA_PITCH] );
2119         }
2120
2121         //glColor3f( 0.0, 0.0, 1.0 );
2122         glColor3f( 1.0, 1.0, 1.0 );
2123         glBegin( GL_LINE_STRIP );
2124         glVertex3f( Cam.x - Cam.box,Cam.y,0 );
2125         glVertex3f( Cam.x,Cam.y + ( Cam.box / 2 ),0 );
2126         glVertex3f( Cam.x + Cam.box,Cam.y,0 );
2127         glVertex3f( Cam.x,Cam.y - ( Cam.box / 2 ),0 );
2128         glVertex3f( Cam.x - Cam.box,Cam.y,0 );
2129         glVertex3f( Cam.x + Cam.box,Cam.y,0 );
2130         glEnd();
2131
2132         glBegin( GL_LINE_STRIP );
2133         glVertex3f( Cam.x + static_cast<float>( Cam.fov * cos( Cam.a + c_pi / 4 ) ), Cam.y + static_cast<float>( Cam.fov * sin( Cam.a + c_pi / 4 ) ), 0 );
2134         glVertex3f( Cam.x, Cam.y, 0 );
2135         glVertex3f( Cam.x + static_cast<float>( Cam.fov * cos( Cam.a - c_pi / 4 ) ), Cam.y + static_cast<float>( Cam.fov * sin( Cam.a - c_pi / 4 ) ), 0 );
2136         glEnd();
2137
2138 }
2139
2140 void XYWnd::UpdateCameraIcon( void ){
2141         if ( glwidget_make_current( m_gl_widget ) != FALSE ) {
2142                 if ( Map_Valid( g_map ) && ScreenUpdates_Enabled() ) {
2143                         GlobalOpenGL_debugAssertNoErrors();
2144                         glDrawBuffer( GL_FRONT );
2145                         {
2146                                 // clear
2147                                 glViewport( 0, 0, m_nWidth, m_nHeight );
2148                                 // set up viewpoint
2149                                 glMatrixMode( GL_PROJECTION );
2150                                 glLoadMatrixf( reinterpret_cast<const float*>( &m_projection ) );
2151
2152                                 glMatrixMode( GL_MODELVIEW );
2153                                 glLoadIdentity();
2154                                 glScalef( m_fScale, m_fScale, 1 );
2155                                 int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
2156                                 int nDim2 = ( m_viewType == XY ) ? 1 : 2;
2157                                 glTranslatef( -m_vOrigin[nDim1], -m_vOrigin[nDim2], 0 );
2158
2159                                 glDisable( GL_LINE_STIPPLE );
2160                                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
2161                                 glDisableClientState( GL_NORMAL_ARRAY );
2162                                 glDisableClientState( GL_COLOR_ARRAY );
2163                                 glDisable( GL_TEXTURE_2D );
2164                                 glDisable( GL_LIGHTING );
2165                                 glDisable( GL_COLOR_MATERIAL );
2166                                 glDisable( GL_DEPTH_TEST );
2167                                 glDisable( GL_TEXTURE_1D );
2168
2169                                 glEnable( GL_BLEND );
2170                                 glBlendFunc( GL_ONE_MINUS_DST_COLOR, GL_ZERO );
2171
2172                                 //glColor3f( 0.0, 0.0, 1.0 );
2173                                 glColor3f( 1.0, 1.0, 1.0 );
2174                                 glBegin( GL_LINE_STRIP );
2175                                 glVertex3f( Cam.x - Cam.box,Cam.y,0 );
2176                                 glVertex3f( Cam.x,Cam.y + ( Cam.box / 2 ),0 );
2177                                 glVertex3f( Cam.x + Cam.box,Cam.y,0 );
2178                                 glVertex3f( Cam.x,Cam.y - ( Cam.box / 2 ),0 );
2179                                 glVertex3f( Cam.x - Cam.box,Cam.y,0 );
2180                                 glVertex3f( Cam.x + Cam.box,Cam.y,0 );
2181                                 glEnd();
2182
2183                                 glBegin( GL_LINE_STRIP );
2184                                 glVertex3f( Cam.x + static_cast<float>( Cam.fov * cos( Cam.a + c_pi / 4 ) ), Cam.y + static_cast<float>( Cam.fov * sin( Cam.a + c_pi / 4 ) ), 0 );
2185                                 glVertex3f( Cam.x, Cam.y, 0 );
2186                                 glVertex3f( Cam.x + static_cast<float>( Cam.fov * cos( Cam.a - c_pi / 4 ) ), Cam.y + static_cast<float>( Cam.fov * sin( Cam.a - c_pi / 4 ) ), 0 );
2187                                 glEnd();
2188
2189                                 XYWnd::DrawCameraIcon( Camera_getOrigin( *g_pParentWnd->GetCamWnd() ), Camera_getAngles( *g_pParentWnd->GetCamWnd() ) );
2190
2191                                 glDisable( GL_BLEND );
2192                                 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
2193                         }
2194
2195                         glDrawBuffer( GL_BACK );
2196                         GlobalOpenGL_debugAssertNoErrors();
2197                         glwidget_make_current( m_gl_widget );
2198                 }
2199         }
2200 }
2201
2202
2203 float Betwixt( float f1, float f2 ){
2204         if ( f1 > f2 ) {
2205                 return f2 + ( ( f1 - f2 ) / 2 );
2206         }
2207         else{
2208                 return f1 + ( ( f2 - f1 ) / 2 );
2209         }
2210 }
2211
2212
2213 // can be greatly simplified but per usual i am in a hurry
2214 // which is not an excuse, just a fact
2215 void XYWnd::PaintSizeInfo( int nDim1, int nDim2, Vector3& vMinBounds, Vector3& vMaxBounds ){
2216         if ( vector3_equal( vMinBounds, vMaxBounds ) ) {
2217                 return;
2218         }
2219         const char* g_pDimStrings[] = {"x:", "y:", "z:"};
2220         typedef const char* OrgStrings[2];
2221         const OrgStrings g_pOrgStrings[] = { { "x:", "y:", }, { "x:", "z:", }, { "y:", "z:", } };
2222
2223         Vector3 vSize( vector3_subtracted( vMaxBounds, vMinBounds ) );
2224
2225         glColor3f( g_xywindow_globals.color_selbrushes[0] * .65f,
2226                            g_xywindow_globals.color_selbrushes[1] * .65f,
2227                            g_xywindow_globals.color_selbrushes[2] * .65f );
2228
2229         StringOutputStream dimensions( 16 );
2230
2231         if ( m_viewType == XY ) {
2232                 glBegin( GL_LINES );
2233
2234                 glVertex3f( vMinBounds[nDim1], vMinBounds[nDim2] - 6.0f  / m_fScale, 0.0f );
2235                 glVertex3f( vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale, 0.0f );
2236
2237                 glVertex3f( vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f  / m_fScale, 0.0f );
2238                 glVertex3f( vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f  / m_fScale, 0.0f );
2239
2240                 glVertex3f( vMaxBounds[nDim1], vMinBounds[nDim2] - 6.0f  / m_fScale, 0.0f );
2241                 glVertex3f( vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale, 0.0f );
2242
2243
2244                 glVertex3f( vMaxBounds[nDim1] + 6.0f  / m_fScale, vMinBounds[nDim2], 0.0f );
2245                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, vMinBounds[nDim2], 0.0f );
2246
2247                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, vMinBounds[nDim2], 0.0f );
2248                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, vMaxBounds[nDim2], 0.0f );
2249
2250                 glVertex3f( vMaxBounds[nDim1] + 6.0f  / m_fScale, vMaxBounds[nDim2], 0.0f );
2251                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, vMaxBounds[nDim2], 0.0f );
2252
2253                 glEnd();
2254
2255                 glRasterPos3f( Betwixt( vMinBounds[nDim1], vMaxBounds[nDim1] ),  vMinBounds[nDim2] - 20.0f  / m_fScale, 0.0f );
2256                 dimensions << g_pDimStrings[nDim1] << vSize[nDim1];
2257                 GlobalOpenGL().drawString( dimensions.c_str() );
2258                 dimensions.clear();
2259
2260                 glRasterPos3f( vMaxBounds[nDim1] + 16.0f  / m_fScale, Betwixt( vMinBounds[nDim2], vMaxBounds[nDim2] ), 0.0f );
2261                 dimensions << g_pDimStrings[nDim2] << vSize[nDim2];
2262                 GlobalOpenGL().drawString( dimensions.c_str() );
2263                 dimensions.clear();
2264
2265                 glRasterPos3f( vMinBounds[nDim1] + 4, vMaxBounds[nDim2] + 8 / m_fScale, 0.0f );
2266                 dimensions << "(" << g_pOrgStrings[0][0] << vMinBounds[nDim1] << "  " << g_pOrgStrings[0][1] << vMaxBounds[nDim2] << ")";
2267                 GlobalOpenGL().drawString( dimensions.c_str() );
2268         }
2269         else if ( m_viewType == XZ ) {
2270                 glBegin( GL_LINES );
2271
2272                 glVertex3f( vMinBounds[nDim1], 0, vMinBounds[nDim2] - 6.0f  / m_fScale );
2273                 glVertex3f( vMinBounds[nDim1], 0, vMinBounds[nDim2] - 10.0f / m_fScale );
2274
2275                 glVertex3f( vMinBounds[nDim1], 0,vMinBounds[nDim2] - 10.0f  / m_fScale );
2276                 glVertex3f( vMaxBounds[nDim1], 0,vMinBounds[nDim2] - 10.0f  / m_fScale );
2277
2278                 glVertex3f( vMaxBounds[nDim1], 0,vMinBounds[nDim2] - 6.0f  / m_fScale );
2279                 glVertex3f( vMaxBounds[nDim1], 0,vMinBounds[nDim2] - 10.0f / m_fScale );
2280
2281
2282                 glVertex3f( vMaxBounds[nDim1] + 6.0f  / m_fScale, 0,vMinBounds[nDim2] );
2283                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, 0,vMinBounds[nDim2] );
2284
2285                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, 0,vMinBounds[nDim2] );
2286                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, 0,vMaxBounds[nDim2] );
2287
2288                 glVertex3f( vMaxBounds[nDim1] + 6.0f  / m_fScale, 0,vMaxBounds[nDim2] );
2289                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, 0,vMaxBounds[nDim2] );
2290
2291                 glEnd();
2292
2293                 glRasterPos3f( Betwixt( vMinBounds[nDim1], vMaxBounds[nDim1] ), 0, vMinBounds[nDim2] - 20.0f  / m_fScale );
2294                 dimensions << g_pDimStrings[nDim1] << vSize[nDim1];
2295                 GlobalOpenGL().drawString( dimensions.c_str() );
2296                 dimensions.clear();
2297
2298                 glRasterPos3f( vMaxBounds[nDim1] + 16.0f  / m_fScale, 0, Betwixt( vMinBounds[nDim2], vMaxBounds[nDim2] ) );
2299                 dimensions << g_pDimStrings[nDim2] << vSize[nDim2];
2300                 GlobalOpenGL().drawString( dimensions.c_str() );
2301                 dimensions.clear();
2302
2303                 glRasterPos3f( vMinBounds[nDim1] + 4, 0, vMaxBounds[nDim2] + 8 / m_fScale );
2304                 dimensions << "(" << g_pOrgStrings[1][0] << vMinBounds[nDim1] << "  " << g_pOrgStrings[1][1] << vMaxBounds[nDim2] << ")";
2305                 GlobalOpenGL().drawString( dimensions.c_str() );
2306         }
2307         else
2308         {
2309                 glBegin( GL_LINES );
2310
2311                 glVertex3f( 0, vMinBounds[nDim1], vMinBounds[nDim2] - 6.0f  / m_fScale );
2312                 glVertex3f( 0, vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale );
2313
2314                 glVertex3f( 0, vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f  / m_fScale );
2315                 glVertex3f( 0, vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f  / m_fScale );
2316
2317                 glVertex3f( 0, vMaxBounds[nDim1], vMinBounds[nDim2] - 6.0f  / m_fScale );
2318                 glVertex3f( 0, vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale );
2319
2320
2321                 glVertex3f( 0, vMaxBounds[nDim1] + 6.0f  / m_fScale, vMinBounds[nDim2] );
2322                 glVertex3f( 0, vMaxBounds[nDim1] + 10.0f  / m_fScale, vMinBounds[nDim2] );
2323
2324                 glVertex3f( 0, vMaxBounds[nDim1] + 10.0f  / m_fScale, vMinBounds[nDim2] );
2325                 glVertex3f( 0, vMaxBounds[nDim1] + 10.0f  / m_fScale, vMaxBounds[nDim2] );
2326
2327                 glVertex3f( 0, vMaxBounds[nDim1] + 6.0f  / m_fScale, vMaxBounds[nDim2] );
2328                 glVertex3f( 0, vMaxBounds[nDim1] + 10.0f  / m_fScale, vMaxBounds[nDim2] );
2329
2330                 glEnd();
2331
2332                 glRasterPos3f( 0, Betwixt( vMinBounds[nDim1], vMaxBounds[nDim1] ),  vMinBounds[nDim2] - 20.0f  / m_fScale );
2333                 dimensions << g_pDimStrings[nDim1] << vSize[nDim1];
2334                 GlobalOpenGL().drawString( dimensions.c_str() );
2335                 dimensions.clear();
2336
2337                 glRasterPos3f( 0, vMaxBounds[nDim1] + 16.0f  / m_fScale, Betwixt( vMinBounds[nDim2], vMaxBounds[nDim2] ) );
2338                 dimensions << g_pDimStrings[nDim2] << vSize[nDim2];
2339                 GlobalOpenGL().drawString( dimensions.c_str() );
2340                 dimensions.clear();
2341
2342                 glRasterPos3f( 0, vMinBounds[nDim1] + 4.0f, vMaxBounds[nDim2] + 8 / m_fScale );
2343                 dimensions << "(" << g_pOrgStrings[2][0] << vMinBounds[nDim1] << "  " << g_pOrgStrings[2][1] << vMaxBounds[nDim2] << ")";
2344                 GlobalOpenGL().drawString( dimensions.c_str() );
2345         }
2346 }
2347
2348 class XYRenderer : public Renderer
2349 {
2350 struct state_type
2351 {
2352         state_type() :
2353                 m_highlight( 0 ),
2354                 m_state( 0 ){
2355         }
2356         unsigned int m_highlight;
2357         Shader* m_state;
2358 };
2359 public:
2360 XYRenderer( RenderStateFlags globalstate, Shader* selected ) :
2361         m_globalstate( globalstate ),
2362         m_state_selected( selected ){
2363         ASSERT_NOTNULL( selected );
2364         m_state_stack.push_back( state_type() );
2365 }
2366
2367 void SetState( Shader* state, EStyle style ){
2368         ASSERT_NOTNULL( state );
2369         if ( style == eWireframeOnly ) {
2370                 m_state_stack.back().m_state = state;
2371         }
2372 }
2373 const EStyle getStyle() const {
2374         return eWireframeOnly;
2375 }
2376 void PushState(){
2377         m_state_stack.push_back( m_state_stack.back() );
2378 }
2379 void PopState(){
2380         ASSERT_MESSAGE( !m_state_stack.empty(), "popping empty stack" );
2381         m_state_stack.pop_back();
2382 }
2383 void Highlight( EHighlightMode mode, bool bEnable = true ){
2384         ( bEnable )
2385         ? m_state_stack.back().m_highlight |= mode
2386                                                                                   : m_state_stack.back().m_highlight &= ~mode;
2387 }
2388 void addRenderable( const OpenGLRenderable& renderable, const Matrix4& localToWorld ){
2389         if ( m_state_stack.back().m_highlight & ePrimitive ) {
2390                 m_state_selected->addRenderable( renderable, localToWorld );
2391         }
2392         else
2393         {
2394                 m_state_stack.back().m_state->addRenderable( renderable, localToWorld );
2395         }
2396 }
2397
2398 void render( const Matrix4& modelview, const Matrix4& projection ){
2399         GlobalShaderCache().render( m_globalstate, modelview, projection );
2400 }
2401 private:
2402 std::vector<state_type> m_state_stack;
2403 RenderStateFlags m_globalstate;
2404 Shader* m_state_selected;
2405 };
2406
2407 void XYWnd::updateProjection(){
2408         m_projection[0] = 1.0f / static_cast<float>( m_nWidth / 2 );
2409         m_projection[5] = 1.0f / static_cast<float>( m_nHeight / 2 );
2410         m_projection[10] = 1.0f / ( g_MaxWorldCoord * m_fScale );
2411
2412         m_projection[12] = 0.0f;
2413         m_projection[13] = 0.0f;
2414         m_projection[14] = -1.0f;
2415
2416         m_projection[1] =
2417                 m_projection[2] =
2418                         m_projection[3] =
2419
2420                                 m_projection[4] =
2421                                         m_projection[6] =
2422                                                 m_projection[7] =
2423
2424                                                         m_projection[8] =
2425                                                                 m_projection[9] =
2426                                                                         m_projection[11] = 0.0f;
2427
2428         m_projection[15] = 1.0f;
2429
2430         m_view.Construct( m_projection, m_modelview, m_nWidth, m_nHeight );
2431 }
2432
2433 // note: modelview matrix must have a uniform scale, otherwise strange things happen when rendering the rotation manipulator.
2434 void XYWnd::updateModelview(){
2435         int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
2436         int nDim2 = ( m_viewType == XY ) ? 1 : 2;
2437
2438         // translation
2439         m_modelview[12] = -m_vOrigin[nDim1] * m_fScale;
2440         m_modelview[13] = -m_vOrigin[nDim2] * m_fScale;
2441         m_modelview[14] = g_MaxWorldCoord * m_fScale;
2442
2443         // axis base
2444         switch ( m_viewType )
2445         {
2446         case XY:
2447                 m_modelview[0]  =  m_fScale;
2448                 m_modelview[1]  =  0;
2449                 m_modelview[2]  =  0;
2450
2451                 m_modelview[4]  =  0;
2452                 m_modelview[5]  =  m_fScale;
2453                 m_modelview[6]  =  0;
2454
2455                 m_modelview[8]  =  0;
2456                 m_modelview[9]  =  0;
2457                 m_modelview[10] = -m_fScale;
2458                 break;
2459         case XZ:
2460                 m_modelview[0]  =  m_fScale;
2461                 m_modelview[1]  =  0;
2462                 m_modelview[2]  =  0;
2463
2464                 m_modelview[4]  =  0;
2465                 m_modelview[5]  =  0;
2466                 m_modelview[6]  =  m_fScale;
2467
2468                 m_modelview[8]  =  0;
2469                 m_modelview[9]  =  m_fScale;
2470                 m_modelview[10] =  0;
2471                 break;
2472         case YZ:
2473                 m_modelview[0]  =  0;
2474                 m_modelview[1]  =  0;
2475                 m_modelview[2]  = -m_fScale;
2476
2477                 m_modelview[4]  =  m_fScale;
2478                 m_modelview[5]  =  0;
2479                 m_modelview[6]  =  0;
2480
2481                 m_modelview[8]  =  0;
2482                 m_modelview[9]  =  m_fScale;
2483                 m_modelview[10] =  0;
2484                 break;
2485         }
2486
2487         m_modelview[3] = m_modelview[7] = m_modelview[11] = 0;
2488         m_modelview[15] = 1;
2489
2490         m_view.Construct( m_projection, m_modelview, m_nWidth, m_nHeight );
2491 }
2492
2493 /*
2494    ==============
2495    XY_Draw
2496    ==============
2497  */
2498
2499 //#define DBG_SCENEDUMP
2500
2501 void XYWnd::XY_Draw(){
2502         //
2503         // clear
2504         //
2505         glViewport( 0, 0, m_nWidth, m_nHeight );
2506         glClearColor( g_xywindow_globals.color_gridback[0],
2507                                   g_xywindow_globals.color_gridback[1],
2508                                   g_xywindow_globals.color_gridback[2],0 );
2509
2510         glClear( GL_COLOR_BUFFER_BIT );
2511
2512         //
2513         // set up viewpoint
2514         //
2515
2516         glMatrixMode( GL_PROJECTION );
2517         glLoadMatrixf( reinterpret_cast<const float*>( &m_projection ) );
2518
2519         glMatrixMode( GL_MODELVIEW );
2520         glLoadIdentity();
2521         glScalef( m_fScale, m_fScale, 1 );
2522         int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
2523         int nDim2 = ( m_viewType == XY ) ? 1 : 2;
2524         glTranslatef( -m_vOrigin[nDim1], -m_vOrigin[nDim2], 0 );
2525
2526         glDisable( GL_LINE_STIPPLE );
2527         glLineWidth( 1 );
2528         glDisableClientState( GL_TEXTURE_COORD_ARRAY );
2529         glDisableClientState( GL_NORMAL_ARRAY );
2530         glDisableClientState( GL_COLOR_ARRAY );
2531         glDisable( GL_TEXTURE_2D );
2532         glDisable( GL_LIGHTING );
2533         glDisable( GL_COLOR_MATERIAL );
2534         glDisable( GL_DEPTH_TEST );
2535
2536         if ( m_backgroundActivated ) {
2537                 XY_DrawBackground();
2538         }
2539         XY_DrawGrid();
2540
2541         if ( g_xywindow_globals_private.show_blocks ) {
2542                 XY_DrawBlockGrid();
2543         }
2544
2545         glLoadMatrixf( reinterpret_cast<const float*>( &m_modelview ) );
2546
2547         unsigned int globalstate = RENDER_COLOURARRAY | RENDER_COLOURWRITE | RENDER_POLYGONSMOOTH | RENDER_LINESMOOTH;
2548         if ( !g_xywindow_globals.m_bNoStipple ) {
2549                 globalstate |= RENDER_LINESTIPPLE;
2550         }
2551
2552         {
2553                 XYRenderer renderer( globalstate, m_state_selected );
2554
2555                 Scene_Render( renderer, m_view );
2556
2557                 GlobalOpenGL_debugAssertNoErrors();
2558                 renderer.render( m_modelview, m_projection );
2559                 GlobalOpenGL_debugAssertNoErrors();
2560         }
2561
2562         glDepthMask( GL_FALSE );
2563
2564         GlobalOpenGL_debugAssertNoErrors();
2565
2566         glLoadMatrixf( reinterpret_cast<const float*>( &m_modelview ) );
2567
2568         GlobalOpenGL_debugAssertNoErrors();
2569         glDisable( GL_LINE_STIPPLE );
2570         GlobalOpenGL_debugAssertNoErrors();
2571         glLineWidth( 1 );
2572         GlobalOpenGL_debugAssertNoErrors();
2573         if ( GlobalOpenGL().GL_1_3() ) {
2574                 glActiveTexture( GL_TEXTURE0 );
2575                 glClientActiveTexture( GL_TEXTURE0 );
2576         }
2577         glDisableClientState( GL_TEXTURE_COORD_ARRAY );
2578         GlobalOpenGL_debugAssertNoErrors();
2579         glDisableClientState( GL_NORMAL_ARRAY );
2580         GlobalOpenGL_debugAssertNoErrors();
2581         glDisableClientState( GL_COLOR_ARRAY );
2582         GlobalOpenGL_debugAssertNoErrors();
2583         glDisable( GL_TEXTURE_2D );
2584         GlobalOpenGL_debugAssertNoErrors();
2585         glDisable( GL_LIGHTING );
2586         GlobalOpenGL_debugAssertNoErrors();
2587         glDisable( GL_COLOR_MATERIAL );
2588         GlobalOpenGL_debugAssertNoErrors();
2589
2590         GlobalOpenGL_debugAssertNoErrors();
2591
2592
2593         // size info
2594         if ( g_xywindow_globals_private.m_bSizePaint && GlobalSelectionSystem().countSelected() != 0 ) {
2595                 Vector3 min, max;
2596                 Select_GetBounds( min, max );
2597                 PaintSizeInfo( nDim1, nDim2, min, max );
2598         }
2599
2600         if ( g_bCrossHairs ) {
2601                 glColor4f( 0.2f, 0.9f, 0.2f, 0.8f );
2602                 glBegin( GL_LINES );
2603                 if ( m_viewType == XY ) {
2604                         glVertex2f( 2.0f * g_MinWorldCoord, m_mousePosition[1] );
2605                         glVertex2f( 2.0f * g_MaxWorldCoord, m_mousePosition[1] );
2606                         glVertex2f( m_mousePosition[0], 2.0f * g_MinWorldCoord );
2607                         glVertex2f( m_mousePosition[0], 2.0f * g_MaxWorldCoord );
2608                 }
2609                 else if ( m_viewType == YZ ) {
2610                         glVertex3f( m_mousePosition[0], 2.0f * g_MinWorldCoord, m_mousePosition[2] );
2611                         glVertex3f( m_mousePosition[0], 2.0f * g_MaxWorldCoord, m_mousePosition[2] );
2612                         glVertex3f( m_mousePosition[0], m_mousePosition[1], 2.0f * g_MinWorldCoord );
2613                         glVertex3f( m_mousePosition[0], m_mousePosition[1], 2.0f * g_MaxWorldCoord );
2614                 }
2615                 else
2616                 {
2617                         glVertex3f( 2.0f * g_MinWorldCoord, m_mousePosition[1], m_mousePosition[2] );
2618                         glVertex3f( 2.0f * g_MaxWorldCoord, m_mousePosition[1], m_mousePosition[2] );
2619                         glVertex3f( m_mousePosition[0], m_mousePosition[1], 2.0f * g_MinWorldCoord );
2620                         glVertex3f( m_mousePosition[0], m_mousePosition[1], 2.0f * g_MaxWorldCoord );
2621                 }
2622                 glEnd();
2623         }
2624
2625         if ( ClipMode() ) {
2626                 GlobalClipPoints_Draw( m_fScale );
2627         }
2628
2629         GlobalOpenGL_debugAssertNoErrors();
2630
2631         // reset modelview
2632         glLoadIdentity();
2633         glScalef( m_fScale, m_fScale, 1 );
2634         glTranslatef( -m_vOrigin[nDim1], -m_vOrigin[nDim2], 0 );
2635
2636         glEnable( GL_BLEND );
2637         glBlendFunc( GL_ONE_MINUS_DST_COLOR, GL_ZERO );
2638         DrawCameraIcon( Camera_getOrigin( *g_pParentWnd->GetCamWnd() ), Camera_getAngles( *g_pParentWnd->GetCamWnd() ) );
2639         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
2640         glDisable( GL_BLEND );
2641
2642         Feedback_draw2D( m_viewType );
2643
2644         if ( g_xywindow_globals_private.show_outline ) {
2645                 if ( Active() ) {
2646                         glMatrixMode( GL_PROJECTION );
2647                         glLoadIdentity();
2648                         glOrtho( 0, m_nWidth, 0, m_nHeight, 0, 1 );
2649
2650                         glMatrixMode( GL_MODELVIEW );
2651                         glLoadIdentity();
2652
2653                         // four view mode doesn't colorize
2654                         if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit ) {
2655                                 glColor3fv( vector3_to_array( g_xywindow_globals.color_viewname ) );
2656                         }
2657                         else
2658                         {
2659                                 switch ( m_viewType )
2660                                 {
2661                                 case YZ:
2662                                         glColor3fv( vector3_to_array( g_xywindow_globals.AxisColorX ) );
2663                                         break;
2664                                 case XZ:
2665                                         glColor3fv( vector3_to_array( g_xywindow_globals.AxisColorY ) );
2666                                         break;
2667                                 case XY:
2668                                         glColor3fv( vector3_to_array( g_xywindow_globals.AxisColorZ ) );
2669                                         break;
2670                                 }
2671                         }
2672                         glBegin( GL_LINE_LOOP );
2673                         glVertex2f( 0.5, 0.5 );
2674                         glVertex2f( m_nWidth - 0.5, 1 );
2675                         glVertex2f( m_nWidth - 0.5, m_nHeight - 0.5 );
2676                         glVertex2f( 0.5, m_nHeight - 0.5 );
2677                         glEnd();
2678                 }
2679         }
2680
2681         GlobalOpenGL_debugAssertNoErrors();
2682
2683         glFinish();
2684 }
2685
2686 void XYWnd_MouseToPoint( XYWnd* xywnd, int x, int y, Vector3& point ){
2687         xywnd->XY_ToPoint( x, y, point );
2688         xywnd->XY_SnapToGrid( point );
2689
2690         int nDim = ( xywnd->GetViewType() == XY ) ? 2 : ( xywnd->GetViewType() == YZ ) ? 0 : 1;
2691         float fWorkMid = float_mid( Select_getWorkZone().d_work_min[nDim], Select_getWorkZone().d_work_max[nDim] );
2692         point[nDim] = float_snapped( fWorkMid, GetGridSize() );
2693 }
2694
2695 void XYWnd::OnEntityCreate( const char* item ){
2696         StringOutputStream command;
2697         command << "entityCreate -class " << item;
2698         UndoableCommand undo( command.c_str() );
2699         Vector3 point;
2700         XYWnd_MouseToPoint( this, m_entityCreate_x, m_entityCreate_y, point );
2701         Entity_createFromSelection( item, point );
2702 }
2703
2704
2705
2706 void GetFocusPosition( Vector3& position ){
2707         if ( GlobalSelectionSystem().countSelected() != 0 ) {
2708                 Select_GetMid( position );
2709         }
2710         else
2711         {
2712                 position = Camera_getOrigin( *g_pParentWnd->GetCamWnd() );
2713         }
2714 }
2715
2716 void XYWnd_Focus( XYWnd* xywnd ){
2717         Vector3 position;
2718         GetFocusPosition( position );
2719         xywnd->PositionView( position );
2720 }
2721
2722 void XY_Split_Focus(){
2723         Vector3 position;
2724         GetFocusPosition( position );
2725         if ( g_pParentWnd->GetXYWnd() ) {
2726                 g_pParentWnd->GetXYWnd()->PositionView( position );
2727         }
2728         if ( g_pParentWnd->GetXZWnd() ) {
2729                 g_pParentWnd->GetXZWnd()->PositionView( position );
2730         }
2731         if ( g_pParentWnd->GetYZWnd() ) {
2732                 g_pParentWnd->GetYZWnd()->PositionView( position );
2733         }
2734 }
2735
2736 void XY_Focus(){
2737         if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit || g_pParentWnd->CurrentStyle() == MainFrame::eFloating ) {
2738                 // cannot do this in a split window
2739                 // do something else that the user may want here
2740                 XY_Split_Focus();
2741                 return;
2742         }
2743
2744         XYWnd* xywnd = g_pParentWnd->GetXYWnd();
2745         XYWnd_Focus( xywnd );
2746 }
2747
2748 void XY_TopFrontSide( VIEWTYPE viewtype ){
2749         if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit ) {
2750                 // cannot do this in a split window
2751                 // do something else that the user may want here
2752                 XY_Split_Focus();
2753                 return;
2754         }
2755         XYWnd* xywnd = g_pParentWnd->CurrentStyle() == MainFrame::eFloating ? g_pParentWnd->ActiveXY() : g_pParentWnd->GetXYWnd();
2756         xywnd->SetViewType( viewtype );
2757         XYWnd_Focus( xywnd );
2758 }
2759
2760 void XY_Top(){
2761         XY_TopFrontSide( XY );
2762 }
2763
2764 void XY_Side(){
2765         XY_TopFrontSide( XZ );
2766 }
2767
2768 void XY_Front(){
2769         XY_TopFrontSide( YZ );
2770 }
2771
2772 void XY_NextView( XYWnd* xywnd ){
2773         if ( xywnd->GetViewType() == XY ) {
2774                 xywnd->SetViewType( XZ );
2775         }
2776         else if ( xywnd->GetViewType() ==  XZ ) {
2777                 xywnd->SetViewType( YZ );
2778         }
2779         else{
2780                 xywnd->SetViewType( XY );
2781         }
2782         XYWnd_Focus( xywnd );
2783 }
2784
2785 void XY_Next(){
2786         if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit ) {
2787                 // cannot do this in a split window
2788                 // do something else that the user may want here
2789                 XY_Split_Focus();
2790                 return;
2791         }
2792         XYWnd* xywnd = g_pParentWnd->CurrentStyle() == MainFrame::eFloating ? g_pParentWnd->ActiveXY() : g_pParentWnd->GetXYWnd();
2793         XY_NextView( xywnd );
2794 }
2795
2796 void XY_Zoom100(){
2797         if ( g_pParentWnd->GetXYWnd() ) {
2798                 g_pParentWnd->GetXYWnd()->SetScale( 1 );
2799         }
2800         if ( g_pParentWnd->GetXZWnd() ) {
2801                 g_pParentWnd->GetXZWnd()->SetScale( 1 );
2802         }
2803         if ( g_pParentWnd->GetYZWnd() ) {
2804                 g_pParentWnd->GetYZWnd()->SetScale( 1 );
2805         }
2806 }
2807
2808 void XY_ZoomIn(){
2809         g_pParentWnd->ActiveXY()->ZoomIn();
2810 }
2811
2812 // NOTE: the zoom out factor is 4/5, we could think about customizing it
2813 //  we don't go below a zoom factor corresponding to 10% of the max world size
2814 //  (this has to be computed against the window size)
2815 void XY_ZoomOut(){
2816         g_pParentWnd->ActiveXY()->ZoomOut();
2817 }
2818
2819
2820
2821 ToggleShown g_xy_top_shown( true );
2822
2823 void XY_Top_Shown_Construct( GtkWindow* parent ){
2824         g_xy_top_shown.connect( GTK_WIDGET( parent ) );
2825 }
2826
2827 ToggleShown g_yz_side_shown( false );
2828
2829 void YZ_Side_Shown_Construct( GtkWindow* parent ){
2830         g_yz_side_shown.connect( GTK_WIDGET( parent ) );
2831 }
2832
2833 ToggleShown g_xz_front_shown( false );
2834
2835 void XZ_Front_Shown_Construct( GtkWindow* parent ){
2836         g_xz_front_shown.connect( GTK_WIDGET( parent ) );
2837 }
2838
2839
2840 class EntityClassMenu : public ModuleObserver
2841 {
2842 std::size_t m_unrealised;
2843 public:
2844 EntityClassMenu() : m_unrealised( 1 ){
2845 }
2846 void realise(){
2847         if ( --m_unrealised == 0 ) {
2848         }
2849 }
2850 void unrealise(){
2851         if ( ++m_unrealised == 1 ) {
2852                 if ( XYWnd::m_mnuDrop != 0 ) {
2853                         gtk_widget_destroy( GTK_WIDGET( XYWnd::m_mnuDrop ) );
2854                         XYWnd::m_mnuDrop = 0;
2855                 }
2856         }
2857 }
2858 };
2859
2860 EntityClassMenu g_EntityClassMenu;
2861
2862
2863
2864
2865 void ShowNamesExport( const BoolImportCallback& importer ){
2866         importer( GlobalEntityCreator().getShowNames() );
2867 }
2868 typedef FreeCaller1<const BoolImportCallback&, ShowNamesExport> ShowNamesExportCaller;
2869 ShowNamesExportCaller g_show_names_caller;
2870 ToggleItem g_show_names( g_show_names_caller );
2871 void ShowNamesToggle(){
2872         GlobalEntityCreator().setShowNames( !GlobalEntityCreator().getShowNames() );
2873         g_show_names.update();
2874         XY_UpdateAllWindows();
2875 }
2876
2877 void ShowTargetNamesExport( const BoolImportCallback& importer ){
2878         importer( GlobalEntityCreator().getShowTargetNames() );
2879 }
2880 typedef FreeCaller1<const BoolImportCallback&, ShowTargetNamesExport> ShowTargetNamesExportCaller;
2881 ShowTargetNamesExportCaller g_show_targetnames_caller;
2882 ToggleItem g_show_targetnames( g_show_targetnames_caller );
2883 void ShowTargetNamesToggle(){
2884         GlobalEntityCreator().setShowTargetNames( !GlobalEntityCreator().getShowTargetNames() );
2885         g_show_targetnames.update();
2886         XY_UpdateAllWindows();
2887 }
2888
2889 void ShowAnglesExport( const BoolImportCallback& importer ){
2890         importer( GlobalEntityCreator().getShowAngles() );
2891 }
2892 typedef FreeCaller1<const BoolImportCallback&, ShowAnglesExport> ShowAnglesExportCaller;
2893 ShowAnglesExportCaller g_show_angles_caller;
2894 ToggleItem g_show_angles( g_show_angles_caller );
2895 void ShowAnglesToggle(){
2896         GlobalEntityCreator().setShowAngles( !GlobalEntityCreator().getShowAngles() );
2897         g_show_angles.update();
2898         XY_UpdateAllWindows();
2899 }
2900
2901 BoolExportCaller g_show_blocks_caller( g_xywindow_globals_private.show_blocks );
2902 ToggleItem g_show_blocks( g_show_blocks_caller );
2903 void ShowBlocksToggle(){
2904         g_xywindow_globals_private.show_blocks ^= 1;
2905         g_show_blocks.update();
2906         XY_UpdateAllWindows();
2907 }
2908
2909 BoolExportCaller g_show_coordinates_caller( g_xywindow_globals_private.show_coordinates );
2910 ToggleItem g_show_coordinates( g_show_coordinates_caller );
2911 void ShowCoordinatesToggle(){
2912         g_xywindow_globals_private.show_coordinates ^= 1;
2913         g_show_coordinates.update();
2914         XY_UpdateAllWindows();
2915 }
2916
2917 BoolExportCaller g_show_outline_caller( g_xywindow_globals_private.show_outline );
2918 ToggleItem g_show_outline( g_show_outline_caller );
2919 void ShowOutlineToggle(){
2920         g_xywindow_globals_private.show_outline ^= 1;
2921         g_show_outline.update();
2922         XY_UpdateAllWindows();
2923 }
2924
2925 BoolExportCaller g_show_axes_caller( g_xywindow_globals_private.show_axis );
2926 ToggleItem g_show_axes( g_show_axes_caller );
2927 void ShowAxesToggle(){
2928         g_xywindow_globals_private.show_axis ^= 1;
2929         g_show_axes.update();
2930         XY_UpdateAllWindows();
2931 }
2932
2933
2934 BoolExportCaller g_show_workzone_caller( g_xywindow_globals_private.d_show_work );
2935 ToggleItem g_show_workzone( g_show_workzone_caller );
2936 void ShowWorkzoneToggle(){
2937         g_xywindow_globals_private.d_show_work ^= 1;
2938         g_show_workzone.update();
2939         XY_UpdateAllWindows();
2940 }
2941
2942 /*
2943 void ShowAxesToggle(){
2944         g_xywindow_globals_private.show_axis ^= 1;
2945         XY_UpdateAllWindows();
2946 }
2947 typedef FreeCaller<ShowAxesToggle> ShowAxesToggleCaller;
2948 void ShowAxesExport( const BoolImportCallback& importer ){
2949         importer( g_xywindow_globals_private.show_axis );
2950 }
2951 typedef FreeCaller1<const BoolImportCallback&, ShowAxesExport> ShowAxesExportCaller;
2952
2953 ShowAxesExportCaller g_show_axes_caller;
2954 BoolExportCallback g_show_axes_callback( g_show_axes_caller );
2955 ToggleItem g_show_axes( g_show_axes_callback );
2956 */
2957
2958 /*
2959 BoolExportCaller g_texdef_movelock_caller( g_brush_texturelock_enabled );
2960 ToggleItem g_texdef_movelock_item( g_texdef_movelock_caller );
2961
2962 void Texdef_ToggleMoveLock(){
2963         g_brush_texturelock_enabled = !g_brush_texturelock_enabled;
2964         g_texdef_movelock_item.update();
2965 }
2966 */
2967
2968 BoolExportCaller g_show_size_caller( g_xywindow_globals_private.m_bSizePaint );
2969 ToggleItem g_show_size_item( g_show_size_caller );
2970 void ToggleShowSizeInfo(){
2971         g_xywindow_globals_private.m_bSizePaint = !g_xywindow_globals_private.m_bSizePaint;
2972         g_show_size_item.update();
2973         XY_UpdateAllWindows();
2974 }
2975
2976 BoolExportCaller g_show_crosshair_caller( g_bCrossHairs );
2977 ToggleItem g_show_crosshair_item( g_show_crosshair_caller );
2978 void ToggleShowCrosshair(){
2979         g_bCrossHairs ^= 1;
2980         g_show_crosshair_item.update();
2981         XY_UpdateAllWindows();
2982 }
2983
2984 BoolExportCaller g_show_grid_caller( g_xywindow_globals_private.d_showgrid );
2985 ToggleItem g_show_grid_item( g_show_grid_caller );
2986 void ToggleShowGrid(){
2987         g_xywindow_globals_private.d_showgrid = !g_xywindow_globals_private.d_showgrid;
2988         g_show_grid_item.update();
2989         XY_UpdateAllWindows();
2990 }
2991
2992
2993 void XYShow_registerCommands(){
2994         GlobalToggles_insert( "ToggleSizePaint", FreeCaller<ToggleShowSizeInfo>(), ToggleItem::AddCallbackCaller( g_show_size_item ), Accelerator( 'J' ) );
2995         GlobalToggles_insert( "ToggleCrosshairs", FreeCaller<ToggleShowCrosshair>(), ToggleItem::AddCallbackCaller( g_show_crosshair_item ), Accelerator( 'X', (GdkModifierType)GDK_SHIFT_MASK ) );
2996         GlobalToggles_insert( "ToggleGrid", FreeCaller<ToggleShowGrid>(), ToggleItem::AddCallbackCaller( g_show_grid_item ), Accelerator( '0' ) );
2997
2998         GlobalToggles_insert( "ShowAngles", FreeCaller<ShowAnglesToggle>(), ToggleItem::AddCallbackCaller( g_show_angles ) );
2999         GlobalToggles_insert( "ShowNames", FreeCaller<ShowNamesToggle>(), ToggleItem::AddCallbackCaller( g_show_names ) );
3000         GlobalToggles_insert( "ShowTargetNames", FreeCaller<ShowTargetNamesToggle>(), ToggleItem::AddCallbackCaller( g_show_targetnames ) );
3001         GlobalToggles_insert( "ShowBlocks", FreeCaller<ShowBlocksToggle>(), ToggleItem::AddCallbackCaller( g_show_blocks ) );
3002         GlobalToggles_insert( "ShowCoordinates", FreeCaller<ShowCoordinatesToggle>(), ToggleItem::AddCallbackCaller( g_show_coordinates ) );
3003         GlobalToggles_insert( "ShowWindowOutline", FreeCaller<ShowOutlineToggle>(), ToggleItem::AddCallbackCaller( g_show_outline ) );
3004         GlobalToggles_insert( "ShowAxes", FreeCaller<ShowAxesToggle>(), ToggleItem::AddCallbackCaller( g_show_axes ) );
3005         GlobalToggles_insert( "ShowWorkzone", FreeCaller<ShowWorkzoneToggle>(), ToggleItem::AddCallbackCaller( g_show_workzone ) );
3006 }
3007
3008 void XYWnd_registerShortcuts(){
3009         command_connect_accelerator( "ToggleCrosshairs" );
3010         command_connect_accelerator( "ToggleSizePaint" );
3011 }
3012
3013
3014
3015 void Orthographic_constructPreferences( PreferencesPage& page ){
3016         page.appendCheckBox( "", "Solid selection boxes ( no stipple )", g_xywindow_globals.m_bNoStipple );
3017         //page.appendCheckBox( "", "Display size info", g_xywindow_globals_private.m_bSizePaint );
3018         page.appendCheckBox( "", "Chase mouse during drags", g_xywindow_globals_private.m_bChaseMouse );
3019 //      page.appendCheckBox( "", "Update views on camera move", g_xywindow_globals_private.m_bCamXYUpdate );
3020         page.appendCheckBox( "", "Zoom In to Mouse pointer", g_xywindow_globals.m_bZoomInToPointer );
3021 }
3022 void Orthographic_constructPage( PreferenceGroup& group ){
3023         PreferencesPage page( group.createPage( "Orthographic", "Orthographic View Preferences" ) );
3024         Orthographic_constructPreferences( page );
3025 }
3026 void Orthographic_registerPreferencesPage(){
3027         PreferencesDialog_addSettingsPage( FreeCaller1<PreferenceGroup&, Orthographic_constructPage>() );
3028 }
3029
3030 void Clipper_constructPreferences( PreferencesPage& page ){
3031         page.appendCheckBox( "", "Clipper tool uses caulk", g_clip_useCaulk );
3032 }
3033 void Clipper_constructPage( PreferenceGroup& group ){
3034         PreferencesPage page( group.createPage( "Clipper", "Clipper Tool Settings" ) );
3035         Clipper_constructPreferences( page );
3036 }
3037 void Clipper_registerPreferencesPage(){
3038         PreferencesDialog_addSettingsPage( FreeCaller1<PreferenceGroup&, Clipper_constructPage>() );
3039 }
3040
3041
3042 #include "preferencesystem.h"
3043 #include "stringio.h"
3044
3045
3046
3047
3048 void ToggleShown_importBool( ToggleShown& self, bool value ){
3049         self.set( value );
3050 }
3051 typedef ReferenceCaller1<ToggleShown, bool, ToggleShown_importBool> ToggleShownImportBoolCaller;
3052 void ToggleShown_exportBool( const ToggleShown& self, const BoolImportCallback& importer ){
3053         importer( self.active() );
3054 }
3055 typedef ConstReferenceCaller1<ToggleShown, const BoolImportCallback&, ToggleShown_exportBool> ToggleShownExportBoolCaller;
3056
3057
3058 void XYWindow_Construct(){
3059 //      GlobalCommands_insert( "ToggleCrosshairs", FreeCaller<ToggleShowCrosshair>(), Accelerator( 'X', (GdkModifierType)GDK_SHIFT_MASK ) );
3060 //      GlobalCommands_insert( "ToggleSizePaint", FreeCaller<ToggleShowSizeInfo>(), Accelerator( 'J' ) );
3061 //      GlobalCommands_insert( "ToggleGrid", FreeCaller<ToggleShowGrid>(), Accelerator( '0' ) );
3062
3063         GlobalToggles_insert( "ToggleView", ToggleShown::ToggleCaller( g_xy_top_shown ), ToggleItem::AddCallbackCaller( g_xy_top_shown.m_item ), Accelerator( 'V', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
3064         GlobalToggles_insert( "ToggleSideView", ToggleShown::ToggleCaller( g_yz_side_shown ), ToggleItem::AddCallbackCaller( g_yz_side_shown.m_item ) );
3065         GlobalToggles_insert( "ToggleFrontView", ToggleShown::ToggleCaller( g_xz_front_shown ), ToggleItem::AddCallbackCaller( g_xz_front_shown.m_item ) );
3066         GlobalCommands_insert( "NextView", FreeCaller<XY_Next>(), Accelerator( GDK_Tab, (GdkModifierType)GDK_CONTROL_MASK ) );
3067         GlobalCommands_insert( "ZoomIn", FreeCaller<XY_ZoomIn>(), Accelerator( GDK_Delete ) );
3068         GlobalCommands_insert( "ZoomOut", FreeCaller<XY_ZoomOut>(), Accelerator( GDK_Insert ) );
3069         GlobalCommands_insert( "ViewTop", FreeCaller<XY_Top>(), Accelerator( GDK_KP_Home ) );
3070         GlobalCommands_insert( "ViewSide", FreeCaller<XY_Side>(), Accelerator( GDK_KP_Page_Down ) );
3071         GlobalCommands_insert( "ViewFront", FreeCaller<XY_Front>(), Accelerator( GDK_KP_End ) );
3072         GlobalCommands_insert( "Zoom100", FreeCaller<XY_Zoom100>() );
3073         GlobalCommands_insert( "CenterXYView", FreeCaller<XY_Focus>(), Accelerator( GDK_Tab, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
3074
3075         GlobalPreferenceSystem().registerPreference( "ClipCaulk", BoolImportStringCaller( g_clip_useCaulk ), BoolExportStringCaller( g_clip_useCaulk ) );
3076
3077 //      GlobalPreferenceSystem().registerPreference( "NewRightClick", BoolImportStringCaller( g_xywindow_globals.m_bRightClick ), BoolExportStringCaller( g_xywindow_globals.m_bRightClick ) );
3078         GlobalPreferenceSystem().registerPreference( "2DZoomInToPointer", BoolImportStringCaller( g_xywindow_globals.m_bZoomInToPointer ), BoolExportStringCaller( g_xywindow_globals.m_bZoomInToPointer ) );
3079         GlobalPreferenceSystem().registerPreference( "ChaseMouse", BoolImportStringCaller( g_xywindow_globals_private.m_bChaseMouse ), BoolExportStringCaller( g_xywindow_globals_private.m_bChaseMouse ) );
3080         GlobalPreferenceSystem().registerPreference( "SizePainting", BoolImportStringCaller( g_xywindow_globals_private.m_bSizePaint ), BoolExportStringCaller( g_xywindow_globals_private.m_bSizePaint ) );
3081         GlobalPreferenceSystem().registerPreference( "ShowCrosshair", BoolImportStringCaller( g_bCrossHairs ), BoolExportStringCaller( g_bCrossHairs ) );
3082         GlobalPreferenceSystem().registerPreference( "NoStipple", BoolImportStringCaller( g_xywindow_globals.m_bNoStipple ), BoolExportStringCaller( g_xywindow_globals.m_bNoStipple ) );
3083         GlobalPreferenceSystem().registerPreference( "SI_ShowCoords", BoolImportStringCaller( g_xywindow_globals_private.show_coordinates ), BoolExportStringCaller( g_xywindow_globals_private.show_coordinates ) );
3084         GlobalPreferenceSystem().registerPreference( "SI_ShowOutlines", BoolImportStringCaller( g_xywindow_globals_private.show_outline ), BoolExportStringCaller( g_xywindow_globals_private.show_outline ) );
3085         GlobalPreferenceSystem().registerPreference( "SI_ShowAxis", BoolImportStringCaller( g_xywindow_globals_private.show_axis ), BoolExportStringCaller( g_xywindow_globals_private.show_axis ) );
3086 //      GlobalPreferenceSystem().registerPreference( "CamXYUpdate", BoolImportStringCaller( g_xywindow_globals_private.m_bCamXYUpdate ), BoolExportStringCaller( g_xywindow_globals_private.m_bCamXYUpdate ) );
3087         GlobalPreferenceSystem().registerPreference( "ShowWorkzone", BoolImportStringCaller( g_xywindow_globals_private.d_show_work ), BoolExportStringCaller( g_xywindow_globals_private.d_show_work ) );
3088
3089         GlobalPreferenceSystem().registerPreference( "SI_AxisColors0", Vector3ImportStringCaller( g_xywindow_globals.AxisColorX ), Vector3ExportStringCaller( g_xywindow_globals.AxisColorX ) );
3090         GlobalPreferenceSystem().registerPreference( "SI_AxisColors1", Vector3ImportStringCaller( g_xywindow_globals.AxisColorY ), Vector3ExportStringCaller( g_xywindow_globals.AxisColorY ) );
3091         GlobalPreferenceSystem().registerPreference( "SI_AxisColors2", Vector3ImportStringCaller( g_xywindow_globals.AxisColorZ ), Vector3ExportStringCaller( g_xywindow_globals.AxisColorZ ) );
3092         GlobalPreferenceSystem().registerPreference( "SI_Colors1", Vector3ImportStringCaller( g_xywindow_globals.color_gridback ), Vector3ExportStringCaller( g_xywindow_globals.color_gridback ) );
3093         GlobalPreferenceSystem().registerPreference( "SI_Colors2", Vector3ImportStringCaller( g_xywindow_globals.color_gridminor ), Vector3ExportStringCaller( g_xywindow_globals.color_gridminor ) );
3094         GlobalPreferenceSystem().registerPreference( "SI_Colors3", Vector3ImportStringCaller( g_xywindow_globals.color_gridmajor ), Vector3ExportStringCaller( g_xywindow_globals.color_gridmajor ) );
3095         GlobalPreferenceSystem().registerPreference( "SI_Colors6", Vector3ImportStringCaller( g_xywindow_globals.color_gridblock ), Vector3ExportStringCaller( g_xywindow_globals.color_gridblock ) );
3096         GlobalPreferenceSystem().registerPreference( "SI_Colors7", Vector3ImportStringCaller( g_xywindow_globals.color_gridtext ), Vector3ExportStringCaller( g_xywindow_globals.color_gridtext ) );
3097         GlobalPreferenceSystem().registerPreference( "SI_Colors8", Vector3ImportStringCaller( g_xywindow_globals.color_brushes ), Vector3ExportStringCaller( g_xywindow_globals.color_brushes ) );
3098         GlobalPreferenceSystem().registerPreference( "SI_Colors9", Vector3ImportStringCaller( g_xywindow_globals.color_viewname ), Vector3ExportStringCaller( g_xywindow_globals.color_viewname ) );
3099         GlobalPreferenceSystem().registerPreference( "SI_Colors10", Vector3ImportStringCaller( g_xywindow_globals.color_clipper ), Vector3ExportStringCaller( g_xywindow_globals.color_clipper ) );
3100         GlobalPreferenceSystem().registerPreference( "SI_Colors11", Vector3ImportStringCaller( g_xywindow_globals.color_selbrushes ), Vector3ExportStringCaller( g_xywindow_globals.color_selbrushes ) );
3101
3102
3103         GlobalPreferenceSystem().registerPreference( "XZVIS", makeBoolStringImportCallback( ToggleShownImportBoolCaller( g_xz_front_shown ) ), makeBoolStringExportCallback( ToggleShownExportBoolCaller( g_xz_front_shown ) ) );
3104         GlobalPreferenceSystem().registerPreference( "YZVIS", makeBoolStringImportCallback( ToggleShownImportBoolCaller( g_yz_side_shown ) ), makeBoolStringExportCallback( ToggleShownExportBoolCaller( g_yz_side_shown ) ) );
3105
3106         Orthographic_registerPreferencesPage();
3107         Clipper_registerPreferencesPage();
3108
3109         XYWnd::captureStates();
3110         GlobalEntityClassManager().attach( g_EntityClassMenu );
3111 }
3112
3113 void XYWindow_Destroy(){
3114         GlobalEntityClassManager().detach( g_EntityClassMenu );
3115         XYWnd::releaseStates();
3116 }