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