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