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