]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/xywindow.cpp
Remove some dead/debug code
[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
542 #include "timer.h"
543
544 Timer g_chasemouse_timer;
545
546 void XYWnd::ChaseMouse(){
547         float multiplier = g_chasemouse_timer.elapsed_msec() / 10.0f;
548         Scroll( float_to_integer( multiplier * m_chasemouse_delta_x ), float_to_integer( multiplier * -m_chasemouse_delta_y ) );
549
550         //globalOutputStream() << "chasemouse: multiplier=" << multiplier << " x=" << m_chasemouse_delta_x << " y=" << m_chasemouse_delta_y << '\n';
551
552         XY_MouseMoved( m_chasemouse_current_x, m_chasemouse_current_y, getButtonState() );
553         g_chasemouse_timer.start();
554 }
555
556 gboolean xywnd_chasemouse( gpointer data ){
557         reinterpret_cast<XYWnd*>( data )->ChaseMouse();
558         return TRUE;
559 }
560
561 inline const int& min_int( const int& left, const int& right ){
562         return std::min( left, right );
563 }
564
565 bool XYWnd::chaseMouseMotion( int pointx, int pointy ){
566         m_chasemouse_delta_x = 0;
567         m_chasemouse_delta_y = 0;
568
569         if ( g_xywindow_globals_private.m_bChaseMouse && getButtonState() == RAD_LBUTTON ) {
570                 const int epsilon = 16;
571
572                 if ( pointx < epsilon ) {
573                         m_chasemouse_delta_x = std::max( pointx, 0 ) - epsilon;
574                 }
575                 else if ( ( pointx - m_nWidth ) > -epsilon ) {
576                         m_chasemouse_delta_x = min_int( ( pointx - m_nWidth ), 0 ) + epsilon;
577                 }
578
579                 if ( pointy < epsilon ) {
580                         m_chasemouse_delta_y = std::max( pointy, 0 ) - epsilon;
581                 }
582                 else if ( ( pointy - m_nHeight ) > -epsilon ) {
583                         m_chasemouse_delta_y = min_int( ( pointy - m_nHeight ), 0 ) + epsilon;
584                 }
585
586                 if ( m_chasemouse_delta_y != 0 || m_chasemouse_delta_x != 0 ) {
587                         //globalOutputStream() << "chasemouse motion: x=" << pointx << " y=" << pointy << "... ";
588                         m_chasemouse_current_x = pointx;
589                         m_chasemouse_current_y = pointy;
590                         if ( m_chasemouse_handler == 0 ) {
591                                 //globalOutputStream() << "chasemouse timer start... ";
592                                 g_chasemouse_timer.start();
593                                 m_chasemouse_handler = g_idle_add( xywnd_chasemouse, this );
594                         }
595                         return true;
596                 }
597                 else
598                 {
599                         if ( m_chasemouse_handler != 0 ) {
600                                 //globalOutputStream() << "chasemouse cancel\n";
601                                 g_source_remove( m_chasemouse_handler );
602                                 m_chasemouse_handler = 0;
603                         }
604                 }
605         }
606         else
607         {
608                 if ( m_chasemouse_handler != 0 ) {
609                         //globalOutputStream() << "chasemouse cancel\n";
610                         g_source_remove( m_chasemouse_handler );
611                         m_chasemouse_handler = 0;
612                 }
613         }
614         return false;
615 }
616
617 // =============================================================================
618 // XYWnd class
619 Shader* XYWnd::m_state_selected = 0;
620
621 void xy_update_xor_rectangle( XYWnd& self, rect_t area ){
622         if ( GTK_WIDGET_VISIBLE( self.GetWidget() ) ) {
623                 self.m_XORRectangle.set( rectangle_from_area( area.min, area.max, self.Width(), self.Height() ) );
624         }
625 }
626
627 gboolean xywnd_button_press( GtkWidget* widget, GdkEventButton* event, XYWnd* xywnd ){
628         if ( event->type == GDK_BUTTON_PRESS ) {
629                 g_pParentWnd->SetActiveXY( xywnd );
630
631                 xywnd->ButtonState_onMouseDown( buttons_for_event_button( event ) );
632
633                 xywnd->onMouseDown( WindowVector( event->x, event->y ), button_for_button( event->button ), modifiers_for_state( event->state ) );
634         }
635         return FALSE;
636 }
637
638 gboolean xywnd_button_release( GtkWidget* widget, GdkEventButton* event, XYWnd* xywnd ){
639         if ( event->type == GDK_BUTTON_RELEASE ) {
640                 xywnd->XY_MouseUp( static_cast<int>( event->x ), static_cast<int>( event->y ), buttons_for_event_button( event ) );
641
642                 xywnd->ButtonState_onMouseUp( buttons_for_event_button( event ) );
643         }
644         return FALSE;
645 }
646
647 gboolean xywnd_focus_in( GtkWidget* widget, GdkEventFocus* event, XYWnd* xywnd ){
648         if ( event->type == GDK_FOCUS_CHANGE ) {
649                 if ( event->in ) {
650                         g_pParentWnd->SetActiveXY( xywnd );
651                 }
652         }
653         return FALSE;
654 }
655
656 void xywnd_motion( gdouble x, gdouble y, guint state, void* data ){
657         if ( reinterpret_cast<XYWnd*>( data )->chaseMouseMotion( static_cast<int>( x ), static_cast<int>( y ) ) ) {
658                 return;
659         }
660         reinterpret_cast<XYWnd*>( data )->XY_MouseMoved( static_cast<int>( x ), static_cast<int>( y ), buttons_for_state( state ) );
661 }
662
663 gboolean xywnd_wheel_scroll( GtkWidget* widget, GdkEventScroll* event, XYWnd* xywnd ){
664         if ( event->direction == GDK_SCROLL_UP ) {
665                 XYWnd_ZoomIn( xywnd );
666         }
667         else if ( event->direction == GDK_SCROLL_DOWN ) {
668                 XYWnd_ZoomOut( xywnd );
669         }
670         return FALSE;
671 }
672
673 gboolean xywnd_size_allocate( GtkWidget* widget, GtkAllocation* allocation, XYWnd* xywnd ){
674         xywnd->m_nWidth = allocation->width;
675         xywnd->m_nHeight = allocation->height;
676         xywnd->updateProjection();
677         xywnd->m_window_observer->onSizeChanged( xywnd->Width(), xywnd->Height() );
678         return FALSE;
679 }
680
681 gboolean xywnd_expose( GtkWidget* widget, GdkEventExpose* event, XYWnd* xywnd ){
682         if ( glwidget_make_current( xywnd->GetWidget() ) != FALSE ) {
683                 if ( Map_Valid( g_map ) && ScreenUpdates_Enabled() ) {
684                         GlobalOpenGL_debugAssertNoErrors();
685                         xywnd->XY_Draw();
686                         GlobalOpenGL_debugAssertNoErrors();
687
688                         xywnd->m_XORRectangle.set( rectangle_t() );
689                 }
690                 glwidget_swap_buffers( xywnd->GetWidget() );
691         }
692         return FALSE;
693 }
694
695
696 void XYWnd_CameraMoved( XYWnd& xywnd ){
697         if ( g_xywindow_globals_private.m_bCamXYUpdate ) {
698                 XYWnd_Update( xywnd );
699         }
700 }
701
702 XYWnd::XYWnd() :
703         m_gl_widget( glwidget_new( FALSE ) ),
704         m_deferredDraw( WidgetQueueDrawCaller( *m_gl_widget ) ),
705         m_deferred_motion( xywnd_motion, this ),
706         m_parent( 0 ),
707         m_window_observer( NewWindowObserver() ),
708         m_XORRectangle( m_gl_widget ),
709         m_chasemouse_handler( 0 ){
710         m_bActive = false;
711         m_buttonstate = 0;
712
713         m_bNewBrushDrag = false;
714         m_move_started = false;
715         m_zoom_started = false;
716
717         m_nWidth = 0;
718         m_nHeight = 0;
719
720         m_vOrigin[0] = 0;
721         m_vOrigin[1] = 20;
722         m_vOrigin[2] = 46;
723         m_fScale = 1;
724         m_viewType = XY;
725
726         m_backgroundActivated = false;
727         m_alpha = 1.0f;
728         m_xmin = 0.0f;
729         m_ymin = 0.0f;
730         m_xmax = 0.0f;
731         m_ymax = 0.0f;
732
733         m_entityCreate = false;
734
735         m_mnuDrop = 0;
736
737         GlobalWindowObservers_add( m_window_observer );
738         GlobalWindowObservers_connectWidget( m_gl_widget );
739
740         m_window_observer->setRectangleDrawCallback( ReferenceCaller1<XYWnd, rect_t, xy_update_xor_rectangle>( *this ) );
741         m_window_observer->setView( m_view );
742
743         gtk_widget_ref( m_gl_widget );
744
745         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 );
746         GTK_WIDGET_SET_FLAGS( m_gl_widget, GTK_CAN_FOCUS );
747
748         m_sizeHandler = g_signal_connect( G_OBJECT( m_gl_widget ), "size_allocate", G_CALLBACK( xywnd_size_allocate ), this );
749         m_exposeHandler = g_signal_connect( G_OBJECT( m_gl_widget ), "expose_event", G_CALLBACK( xywnd_expose ), this );
750
751         g_signal_connect( G_OBJECT( m_gl_widget ), "button_press_event", G_CALLBACK( xywnd_button_press ), this );
752         g_signal_connect( G_OBJECT( m_gl_widget ), "button_release_event", G_CALLBACK( xywnd_button_release ), this );
753         g_signal_connect( G_OBJECT( m_gl_widget ), "focus_in_event", G_CALLBACK( xywnd_focus_in ), this );
754         g_signal_connect( G_OBJECT( m_gl_widget ), "motion_notify_event", G_CALLBACK( DeferredMotion::gtk_motion ), &m_deferred_motion );
755
756         g_signal_connect( G_OBJECT( m_gl_widget ), "scroll_event", G_CALLBACK( xywnd_wheel_scroll ), this );
757
758         Map_addValidCallback( g_map, DeferredDrawOnMapValidChangedCaller( m_deferredDraw ) );
759
760         updateProjection();
761         updateModelview();
762
763         AddSceneChangeCallback( ReferenceCaller<XYWnd, &XYWnd_Update>( *this ) );
764         AddCameraMovedCallback( ReferenceCaller<XYWnd, &XYWnd_CameraMoved>( *this ) );
765
766         PressedButtons_connect( g_pressedButtons, m_gl_widget );
767
768         onMouseDown.connectLast( makeSignalHandler3( MouseDownCaller(), *this ) );
769 }
770
771 XYWnd::~XYWnd(){
772         onDestroyed();
773
774         if ( m_mnuDrop != 0 ) {
775                 gtk_widget_destroy( GTK_WIDGET( m_mnuDrop ) );
776                 m_mnuDrop = 0;
777         }
778
779         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_sizeHandler );
780         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_exposeHandler );
781
782         gtk_widget_unref( m_gl_widget );
783
784         m_window_observer->release();
785 }
786
787 void XYWnd::captureStates(){
788         m_state_selected = GlobalShaderCache().capture( "$XY_OVERLAY" );
789 }
790
791 void XYWnd::releaseStates(){
792         GlobalShaderCache().release( "$XY_OVERLAY" );
793 }
794
795 const Vector3& XYWnd::GetOrigin(){
796         return m_vOrigin;
797 }
798
799 void XYWnd::SetOrigin( const Vector3& origin ){
800         m_vOrigin = origin;
801         updateModelview();
802 }
803
804 void XYWnd::Scroll( int x, int y ){
805         int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
806         int nDim2 = ( m_viewType == XY ) ? 1 : 2;
807         m_vOrigin[nDim1] += x / m_fScale;
808         m_vOrigin[nDim2] += y / m_fScale;
809         updateModelview();
810         queueDraw();
811 }
812
813 unsigned int Clipper_buttons(){
814         return RAD_LBUTTON;
815 }
816
817 void XYWnd::DropClipPoint( int pointx, int pointy ){
818         Vector3 point;
819
820         XY_ToPoint( pointx, pointy, point );
821
822         Vector3 mid;
823         Select_GetMid( mid );
824         g_clip_viewtype = static_cast<VIEWTYPE>( GetViewType() );
825         const int nDim = ( g_clip_viewtype == YZ ) ? 0 : ( ( g_clip_viewtype == XZ ) ? 1 : 2 );
826         point[nDim] = mid[nDim];
827         vector3_snap( point, GetSnapGridSize() );
828         NewClipPoint( point );
829 }
830
831 void XYWnd::Clipper_OnLButtonDown( int x, int y ){
832         Vector3 mousePosition;
833         XY_ToPoint( x, y, mousePosition );
834         g_pMovingClip = GlobalClipPoints_Find( mousePosition, (VIEWTYPE)m_viewType, m_fScale );
835         if ( !g_pMovingClip ) {
836                 DropClipPoint( x, y );
837         }
838 }
839
840 void XYWnd::Clipper_OnLButtonUp( int x, int y ){
841         if ( g_pMovingClip ) {
842                 g_pMovingClip = 0;
843         }
844 }
845
846 void XYWnd::Clipper_OnMouseMoved( int x, int y ){
847         if ( g_pMovingClip ) {
848                 XY_ToPoint( x, y, g_pMovingClip->m_ptClip );
849                 XY_SnapToGrid( g_pMovingClip->m_ptClip );
850                 Clip_Update();
851                 ClipperChangeNotify();
852         }
853 }
854
855 void XYWnd::Clipper_Crosshair_OnMouseMoved( int x, int y ){
856         Vector3 mousePosition;
857         XY_ToPoint( x, y, mousePosition );
858         if ( ClipMode() && GlobalClipPoints_Find( mousePosition, (VIEWTYPE)m_viewType, m_fScale ) != 0 ) {
859                 GdkCursor *cursor;
860                 cursor = gdk_cursor_new( GDK_CROSSHAIR );
861                 gdk_window_set_cursor( m_gl_widget->window, cursor );
862                 gdk_cursor_unref( cursor );
863         }
864         else
865         {
866                 gdk_window_set_cursor( m_gl_widget->window, 0 );
867         }
868 }
869
870 unsigned int MoveCamera_buttons(){
871         return RAD_CONTROL | ( g_glwindow_globals.m_nMouseType == ETwoButton ? RAD_RBUTTON : RAD_MBUTTON );
872 }
873
874 void XYWnd_PositionCamera( XYWnd* xywnd, int x, int y, CamWnd& camwnd ){
875         Vector3 origin( Camera_getOrigin( camwnd ) );
876         xywnd->XY_ToPoint( x, y, origin );
877         xywnd->XY_SnapToGrid( origin );
878         Camera_setOrigin( camwnd, origin );
879 }
880
881 unsigned int OrientCamera_buttons(){
882         if ( g_glwindow_globals.m_nMouseType == ETwoButton ) {
883                 return RAD_RBUTTON | RAD_SHIFT | RAD_CONTROL;
884         }
885         return RAD_MBUTTON;
886 }
887
888 void XYWnd_OrientCamera( XYWnd* xywnd, int x, int y, CamWnd& camwnd ){
889         Vector3 point = g_vector3_identity;
890         xywnd->XY_ToPoint( x, y, point );
891         xywnd->XY_SnapToGrid( point );
892         vector3_subtract( point, Camera_getOrigin( camwnd ) );
893
894         int n1 = ( xywnd->GetViewType() == XY ) ? 1 : 2;
895         int n2 = ( xywnd->GetViewType() == YZ ) ? 1 : 0;
896         int nAngle = ( xywnd->GetViewType() == XY ) ? CAMERA_YAW : CAMERA_PITCH;
897         if ( point[n1] || point[n2] ) {
898                 Vector3 angles( Camera_getAngles( camwnd ) );
899                 angles[nAngle] = static_cast<float>( radians_to_degrees( atan2( point[n1], point[n2] ) ) );
900                 Camera_setAngles( camwnd, angles );
901         }
902 }
903
904 /*
905    ==============
906    NewBrushDrag
907    ==============
908  */
909 unsigned int NewBrushDrag_buttons(){
910         return RAD_LBUTTON;
911 }
912
913 void XYWnd::NewBrushDrag_Begin( int x, int y ){
914         m_NewBrushDrag = 0;
915         m_nNewBrushPressx = x;
916         m_nNewBrushPressy = y;
917
918         m_bNewBrushDrag = true;
919         GlobalUndoSystem().start();
920 }
921
922 void XYWnd::NewBrushDrag_End( int x, int y ){
923         if ( m_NewBrushDrag != 0 ) {
924                 GlobalUndoSystem().finish( "brushDragNew" );
925         }
926 }
927
928 void XYWnd::NewBrushDrag( int x, int y ){
929         Vector3 mins, maxs;
930         XY_ToPoint( m_nNewBrushPressx, m_nNewBrushPressy, mins );
931         XY_SnapToGrid( mins );
932         XY_ToPoint( x, y, maxs );
933         XY_SnapToGrid( maxs );
934
935         int nDim = ( m_viewType == XY ) ? 2 : ( m_viewType == YZ ) ? 0 : 1;
936
937         mins[nDim] = float_snapped( Select_getWorkZone().d_work_min[nDim], GetSnapGridSize() );
938         maxs[nDim] = float_snapped( Select_getWorkZone().d_work_max[nDim], GetSnapGridSize() );
939
940         if ( maxs[nDim] <= mins[nDim] ) {
941                 maxs[nDim] = mins[nDim] + GetGridSize();
942         }
943
944         for ( int i = 0 ; i < 3 ; i++ )
945         {
946                 if ( mins[i] == maxs[i] ) {
947                         return; // don't create a degenerate brush
948                 }
949                 if ( mins[i] > maxs[i] ) {
950                         float temp = mins[i];
951                         mins[i] = maxs[i];
952                         maxs[i] = temp;
953                 }
954         }
955
956         if ( m_NewBrushDrag == 0 ) {
957                 NodeSmartReference node( GlobalBrushCreator().createBrush() );
958                 Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->insert( node );
959
960                 scene::Path brushpath( makeReference( GlobalSceneGraph().root() ) );
961                 brushpath.push( makeReference( *Map_GetWorldspawn( g_map ) ) );
962                 brushpath.push( makeReference( node.get() ) );
963                 selectPath( brushpath, true );
964
965                 m_NewBrushDrag = node.get_pointer();
966         }
967
968         // d1223m
969         //Scene_BrushResize_Selected(GlobalSceneGraph(), aabb_for_minmax(mins, maxs), TextureBrowser_GetSelectedShader(GlobalTextureBrowser()));
970         Scene_BrushResize_Selected( GlobalSceneGraph(), aabb_for_minmax( mins, maxs ),
971                                                                 g_brush_always_caulk ?
972                                                                 "textures/common/caulk" : TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
973 }
974
975 void entitycreate_activated( GtkWidget* item ){
976         scene::Node* world_node = Map_FindWorldspawn( g_map );
977         const char* entity_name = gtk_label_get_text( GTK_LABEL( GTK_BIN( item )->child ) );
978
979         if ( !( world_node && string_equal( entity_name, "worldspawn" ) ) ) {
980                 g_pParentWnd->ActiveXY()->OnEntityCreate( entity_name );
981         }
982         else {
983                 GlobalRadiant().m_pfnMessageBox( GTK_WIDGET( MainFrame_getWindow() ), "There's already a worldspawn in your map!"
984                                                                                                                                                           "",
985                                                                                  "Info",
986                                                                                  eMB_OK,
987                                                                                  eMB_ICONDEFAULT );
988         }
989 }
990
991 void EntityClassMenu_addItem( GtkMenu* menu, const char* name ){
992         GtkMenuItem* item = GTK_MENU_ITEM( gtk_menu_item_new_with_label( name ) );
993         g_signal_connect( G_OBJECT( item ), "activate", G_CALLBACK( entitycreate_activated ), item );
994         gtk_widget_show( GTK_WIDGET( item ) );
995         menu_add_item( menu, item );
996 }
997
998 class EntityClassMenuInserter : public EntityClassVisitor
999 {
1000 typedef std::pair<GtkMenu*, CopiedString> MenuPair;
1001 typedef std::vector<MenuPair> MenuStack;
1002 MenuStack m_stack;
1003 CopiedString m_previous;
1004 public:
1005 EntityClassMenuInserter( GtkMenu* menu ){
1006         m_stack.reserve( 2 );
1007         m_stack.push_back( MenuPair( menu, "" ) );
1008 }
1009 ~EntityClassMenuInserter(){
1010         if ( !string_empty( m_previous.c_str() ) ) {
1011                 addItem( m_previous.c_str(), "" );
1012         }
1013 }
1014 void visit( EntityClass* e ){
1015         ASSERT_MESSAGE( !string_empty( e->name() ), "entity-class has no name" );
1016         if ( !string_empty( m_previous.c_str() ) ) {
1017                 addItem( m_previous.c_str(), e->name() );
1018         }
1019         m_previous = e->name();
1020 }
1021 void pushMenu( const CopiedString& name ){
1022         GtkMenuItem* item = GTK_MENU_ITEM( gtk_menu_item_new_with_label( name.c_str() ) );
1023         gtk_widget_show( GTK_WIDGET( item ) );
1024         container_add_widget( GTK_CONTAINER( m_stack.back().first ), GTK_WIDGET( item ) );
1025
1026         GtkMenu* submenu = GTK_MENU( gtk_menu_new() );
1027         gtk_menu_item_set_submenu( item, GTK_WIDGET( submenu ) );
1028
1029         m_stack.push_back( MenuPair( submenu, name ) );
1030 }
1031 void popMenu(){
1032         m_stack.pop_back();
1033 }
1034 void addItem( const char* name, const char* next ){
1035         const char* underscore = strchr( name, '_' );
1036
1037         if ( underscore != 0 && underscore != name ) {
1038                 bool nextEqual = string_equal_n( name, next, ( underscore + 1 ) - name );
1039                 const char* parent = m_stack.back().second.c_str();
1040
1041                 if ( !string_empty( parent )
1042                          && string_length( parent ) == std::size_t( underscore - name )
1043                          && string_equal_n( name, parent, underscore - name ) ) { // this is a child
1044                 }
1045                 else if ( nextEqual ) {
1046                         if ( m_stack.size() == 2 ) {
1047                                 popMenu();
1048                         }
1049                         pushMenu( CopiedString( StringRange( name, underscore ) ) );
1050                 }
1051                 else if ( m_stack.size() == 2 ) {
1052                         popMenu();
1053                 }
1054         }
1055         else if ( m_stack.size() == 2 ) {
1056                 popMenu();
1057         }
1058
1059         EntityClassMenu_addItem( m_stack.back().first, name );
1060 }
1061 };
1062
1063 void XYWnd::OnContextMenu(){
1064         if ( g_xywindow_globals.m_bRightClick == false ) {
1065                 return;
1066         }
1067
1068         if ( m_mnuDrop == 0 ) { // first time, load it up
1069                 GtkMenu* menu = m_mnuDrop = GTK_MENU( gtk_menu_new() );
1070
1071                 EntityClassMenuInserter inserter( menu );
1072                 GlobalEntityClassManager().forEach( inserter );
1073         }
1074
1075         gtk_menu_popup( m_mnuDrop, 0, 0, 0, 0, 1, GDK_CURRENT_TIME );
1076 }
1077
1078 FreezePointer g_xywnd_freezePointer;
1079
1080 unsigned int Move_buttons(){
1081         return RAD_RBUTTON;
1082 }
1083
1084 void XYWnd_moveDelta( int x, int y, unsigned int state, void* data ){
1085         reinterpret_cast<XYWnd*>( data )->EntityCreate_MouseMove( x, y );
1086         reinterpret_cast<XYWnd*>( data )->Scroll( -x, y );
1087 }
1088
1089 gboolean XYWnd_Move_focusOut( GtkWidget* widget, GdkEventFocus* event, XYWnd* xywnd ){
1090         xywnd->Move_End();
1091         return FALSE;
1092 }
1093
1094 void XYWnd::Move_Begin(){
1095         if ( m_move_started ) {
1096                 Move_End();
1097         }
1098         m_move_started = true;
1099         g_xywnd_freezePointer.freeze_pointer( m_parent != 0 ? m_parent : MainFrame_getWindow(), XYWnd_moveDelta, this );
1100         m_move_focusOut = g_signal_connect( G_OBJECT( m_gl_widget ), "focus_out_event", G_CALLBACK( XYWnd_Move_focusOut ), this );
1101 }
1102
1103 void XYWnd::Move_End(){
1104         m_move_started = false;
1105         g_xywnd_freezePointer.unfreeze_pointer( m_parent != 0 ? m_parent : MainFrame_getWindow() );
1106         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_move_focusOut );
1107 }
1108
1109 unsigned int Zoom_buttons(){
1110         return RAD_RBUTTON | RAD_SHIFT;
1111 }
1112
1113 int g_dragZoom = 0;
1114
1115 void XYWnd_zoomDelta( int x, int y, unsigned int state, void* data ){
1116         if ( y != 0 ) {
1117                 g_dragZoom += y;
1118
1119                 while ( abs( g_dragZoom ) > 8 )
1120                 {
1121                         if ( g_dragZoom > 0 ) {
1122                                 XYWnd_ZoomOut( reinterpret_cast<XYWnd*>( data ) );
1123                                 g_dragZoom -= 8;
1124                         }
1125                         else
1126                         {
1127                                 XYWnd_ZoomIn( reinterpret_cast<XYWnd*>( data ) );
1128                                 g_dragZoom += 8;
1129                         }
1130                 }
1131         }
1132 }
1133
1134 gboolean XYWnd_Zoom_focusOut( GtkWidget* widget, GdkEventFocus* event, XYWnd* xywnd ){
1135         xywnd->Zoom_End();
1136         return FALSE;
1137 }
1138
1139 void XYWnd::Zoom_Begin(){
1140         if ( m_zoom_started ) {
1141                 Zoom_End();
1142         }
1143         m_zoom_started = true;
1144         g_dragZoom = 0;
1145         g_xywnd_freezePointer.freeze_pointer( m_parent != 0 ? m_parent : MainFrame_getWindow(), XYWnd_zoomDelta, this );
1146         m_zoom_focusOut = g_signal_connect( G_OBJECT( m_gl_widget ), "focus_out_event", G_CALLBACK( XYWnd_Zoom_focusOut ), this );
1147 }
1148
1149 void XYWnd::Zoom_End(){
1150         m_zoom_started = false;
1151         g_xywnd_freezePointer.unfreeze_pointer( m_parent != 0 ? m_parent : MainFrame_getWindow() );
1152         g_signal_handler_disconnect( G_OBJECT( m_gl_widget ), m_zoom_focusOut );
1153 }
1154
1155 // makes sure the selected brush or camera is in view
1156 void XYWnd::PositionView( const Vector3& position ){
1157         int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
1158         int nDim2 = ( m_viewType == XY ) ? 1 : 2;
1159
1160         m_vOrigin[nDim1] = position[nDim1];
1161         m_vOrigin[nDim2] = position[nDim2];
1162
1163         updateModelview();
1164
1165         XYWnd_Update( *this );
1166 }
1167
1168 void XYWnd::SetViewType( VIEWTYPE viewType ){
1169         m_viewType = viewType;
1170         updateModelview();
1171
1172         if ( m_parent != 0 ) {
1173                 gtk_window_set_title( m_parent, ViewType_getTitle( m_viewType ) );
1174         }
1175 }
1176
1177
1178 inline WindowVector WindowVector_forInteger( int x, int y ){
1179         return WindowVector( static_cast<float>( x ), static_cast<float>( y ) );
1180 }
1181
1182 void XYWnd::mouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
1183         XY_MouseDown( static_cast<int>( position.x() ), static_cast<int>( position.y() ), buttons_for_button_and_modifiers( button, modifiers ) );
1184 }
1185 void XYWnd::XY_MouseDown( int x, int y, unsigned int buttons ){
1186         if ( buttons == Move_buttons() ) {
1187                 Move_Begin();
1188                 EntityCreate_MouseDown( x, y );
1189         }
1190         else if ( buttons == Zoom_buttons() ) {
1191                 Zoom_Begin();
1192         }
1193         else if ( ClipMode() && buttons == Clipper_buttons() ) {
1194                 Clipper_OnLButtonDown( x, y );
1195         }
1196         else if ( buttons == NewBrushDrag_buttons() && GlobalSelectionSystem().countSelected() == 0 ) {
1197                 NewBrushDrag_Begin( x, y );
1198         }
1199         // control mbutton = move camera
1200         else if ( buttons == MoveCamera_buttons() ) {
1201                 XYWnd_PositionCamera( this, x, y, *g_pParentWnd->GetCamWnd() );
1202         }
1203         // mbutton = angle camera
1204         else if ( buttons == OrientCamera_buttons() ) {
1205                 XYWnd_OrientCamera( this, x, y, *g_pParentWnd->GetCamWnd() );
1206         }
1207         else
1208         {
1209                 m_window_observer->onMouseDown( WindowVector_forInteger( x, y ), button_for_flags( buttons ), modifiers_for_flags( buttons ) );
1210         }
1211 }
1212
1213 void XYWnd::XY_MouseUp( int x, int y, unsigned int buttons ){
1214         if ( m_move_started ) {
1215                 Move_End();
1216                 EntityCreate_MouseUp( x, y );
1217         }
1218         else if ( m_zoom_started ) {
1219                 Zoom_End();
1220         }
1221         else if ( ClipMode() && buttons == Clipper_buttons() ) {
1222                 Clipper_OnLButtonUp( x, y );
1223         }
1224         else if ( m_bNewBrushDrag ) {
1225                 m_bNewBrushDrag = false;
1226                 NewBrushDrag_End( x, y );
1227         }
1228         else
1229         {
1230                 m_window_observer->onMouseUp( WindowVector_forInteger( x, y ), button_for_flags( buttons ), modifiers_for_flags( buttons ) );
1231         }
1232 }
1233
1234 void XYWnd::XY_MouseMoved( int x, int y, unsigned int buttons ){
1235         // rbutton = drag xy origin
1236         if ( m_move_started ) {
1237         }
1238         // zoom in/out
1239         else if ( m_zoom_started ) {
1240         }
1241
1242         else if ( ClipMode() && g_pMovingClip != 0 ) {
1243                 Clipper_OnMouseMoved( x, y );
1244         }
1245         // lbutton without selection = drag new brush
1246         else if ( m_bNewBrushDrag ) {
1247                 NewBrushDrag( x, y );
1248         }
1249
1250         // control mbutton = move camera
1251         else if ( getButtonState() == MoveCamera_buttons() ) {
1252                 XYWnd_PositionCamera( this, x, y, *g_pParentWnd->GetCamWnd() );
1253         }
1254
1255         // mbutton = angle camera
1256         else if ( getButtonState() == OrientCamera_buttons() ) {
1257                 XYWnd_OrientCamera( this, x, y, *g_pParentWnd->GetCamWnd() );
1258         }
1259
1260         else
1261         {
1262                 m_window_observer->onMouseMotion( WindowVector_forInteger( x, y ), modifiers_for_flags( buttons ) );
1263
1264                 m_mousePosition[0] = m_mousePosition[1] = m_mousePosition[2] = 0.0;
1265                 XY_ToPoint( x, y, m_mousePosition );
1266                 XY_SnapToGrid( m_mousePosition );
1267
1268                 StringOutputStream status( 64 );
1269                 status << "x:: " << FloatFormat( m_mousePosition[0], 6, 1 )
1270                            << "  y:: " << FloatFormat( m_mousePosition[1], 6, 1 )
1271                            << "  z:: " << FloatFormat( m_mousePosition[2], 6, 1 );
1272                 g_pParentWnd->SetStatusText( g_pParentWnd->m_position_status, status.c_str() );
1273
1274                 if ( g_bCrossHairs ) {
1275                         XYWnd_Update( *this );
1276                 }
1277
1278                 Clipper_Crosshair_OnMouseMoved( x, y );
1279         }
1280 }
1281
1282 void XYWnd::EntityCreate_MouseDown( int x, int y ){
1283         m_entityCreate = true;
1284         m_entityCreate_x = x;
1285         m_entityCreate_y = y;
1286 }
1287
1288 void XYWnd::EntityCreate_MouseMove( int x, int y ){
1289         if ( m_entityCreate && ( m_entityCreate_x != x || m_entityCreate_y != y ) ) {
1290                 m_entityCreate = false;
1291         }
1292 }
1293
1294 void XYWnd::EntityCreate_MouseUp( int x, int y ){
1295         if ( m_entityCreate ) {
1296                 m_entityCreate = false;
1297                 OnContextMenu();
1298         }
1299 }
1300
1301 inline float screen_normalised( int pos, unsigned int size ){
1302         return ( ( 2.0f * pos ) / size ) - 1.0f;
1303 }
1304
1305 inline float normalised_to_world( float normalised, float world_origin, float normalised2world_scale ){
1306         return world_origin + normalised * normalised2world_scale;
1307 }
1308
1309
1310 // TTimo: watch it, this doesn't init one of the 3 coords
1311 void XYWnd::XY_ToPoint( int x, int y, Vector3& point ){
1312         float normalised2world_scale_x = m_nWidth / 2 / m_fScale;
1313         float normalised2world_scale_y = m_nHeight / 2 / m_fScale;
1314         if ( m_viewType == XY ) {
1315                 point[0] = normalised_to_world( screen_normalised( x, m_nWidth ), m_vOrigin[0], normalised2world_scale_x );
1316                 point[1] = normalised_to_world( -screen_normalised( y, m_nHeight ), m_vOrigin[1], normalised2world_scale_y );
1317         }
1318         else if ( m_viewType == YZ ) {
1319                 point[1] = normalised_to_world( screen_normalised( x, m_nWidth ), m_vOrigin[1], normalised2world_scale_x );
1320                 point[2] = normalised_to_world( -screen_normalised( y, m_nHeight ), m_vOrigin[2], normalised2world_scale_y );
1321         }
1322         else
1323         {
1324                 point[0] = normalised_to_world( screen_normalised( x, m_nWidth ), m_vOrigin[0], normalised2world_scale_x );
1325                 point[2] = normalised_to_world( -screen_normalised( y, m_nHeight ), m_vOrigin[2], normalised2world_scale_y );
1326         }
1327 }
1328
1329 void XYWnd::XY_SnapToGrid( Vector3& point ){
1330         if ( m_viewType == XY ) {
1331                 point[0] = float_snapped( point[0], GetSnapGridSize() );
1332                 point[1] = float_snapped( point[1], GetSnapGridSize() );
1333         }
1334         else if ( m_viewType == YZ ) {
1335                 point[1] = float_snapped( point[1], GetSnapGridSize() );
1336                 point[2] = float_snapped( point[2], GetSnapGridSize() );
1337         }
1338         else
1339         {
1340                 point[0] = float_snapped( point[0], GetSnapGridSize() );
1341                 point[2] = float_snapped( point[2], GetSnapGridSize() );
1342         }
1343 }
1344
1345 void XYWnd::XY_LoadBackgroundImage( const char *name ){
1346         const char* relative = path_make_relative( name, GlobalFileSystem().findRoot( name ) );
1347         if ( relative == name ) {
1348                 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
1349         }
1350
1351         char fileNameWithoutExt[512];
1352         strncpy( fileNameWithoutExt, relative, sizeof( fileNameWithoutExt ) - 1 );
1353         fileNameWithoutExt[512 - 1] = '\0';
1354         fileNameWithoutExt[strlen( fileNameWithoutExt ) - 4] = '\0';
1355
1356         Image *image = QERApp_LoadImage( 0, fileNameWithoutExt );
1357         if ( !image ) {
1358                 globalOutputStream() << "Could not load texture " << fileNameWithoutExt << "\n";
1359                 return;
1360         }
1361         g_pParentWnd->ActiveXY()->m_tex = (qtexture_t*)malloc( sizeof( qtexture_t ) );
1362         LoadTextureRGBA( g_pParentWnd->ActiveXY()->XYWnd::m_tex, image->getRGBAPixels(), image->getWidth(), image->getHeight() );
1363         globalOutputStream() << "Loaded background texture " << relative << "\n";
1364         g_pParentWnd->ActiveXY()->m_backgroundActivated = true;
1365
1366         int m_ix, m_iy;
1367         switch ( g_pParentWnd->ActiveXY()->m_viewType )
1368         {
1369         case XY:
1370                 m_ix = 0;
1371                 m_iy = 1;
1372                 break;
1373         case XZ:
1374                 m_ix = 0;
1375                 m_iy = 2;
1376                 break;
1377         case YZ:
1378                 m_ix = 1;
1379                 m_iy = 2;
1380                 break;
1381         }
1382
1383         Vector3 min, max;
1384         Select_GetBounds( min, max );
1385         g_pParentWnd->ActiveXY()->m_xmin = min[m_ix];
1386         g_pParentWnd->ActiveXY()->m_ymin = min[m_iy];
1387         g_pParentWnd->ActiveXY()->m_xmax = max[m_ix];
1388         g_pParentWnd->ActiveXY()->m_ymax = max[m_iy];
1389 }
1390
1391 void XYWnd::XY_DisableBackground( void ){
1392         g_pParentWnd->ActiveXY()->m_backgroundActivated = false;
1393         if ( g_pParentWnd->ActiveXY()->m_tex ) {
1394                 free( g_pParentWnd->ActiveXY()->m_tex );
1395         }
1396         g_pParentWnd->ActiveXY()->m_tex = NULL;
1397 }
1398
1399 void WXY_BackgroundSelect( void ){
1400         bool brushesSelected = Scene_countSelectedBrushes( GlobalSceneGraph() ) != 0;
1401         if ( !brushesSelected ) {
1402                 gtk_MessageBox( 0, "You have to select some brushes to get the bounding box for.\n",
1403                                                 "No selection", eMB_OK, eMB_ICONERROR );
1404                 return;
1405         }
1406
1407         const char *filename = file_dialog( GTK_WIDGET( MainFrame_getWindow() ), TRUE, "Background Image", NULL, NULL );
1408         g_pParentWnd->ActiveXY()->XY_DisableBackground();
1409         if ( filename ) {
1410                 g_pParentWnd->ActiveXY()->XY_LoadBackgroundImage( filename );
1411         }
1412 }
1413
1414 /*
1415    ============================================================================
1416
1417    DRAWING
1418
1419    ============================================================================
1420  */
1421
1422 /*
1423    ==============
1424    XY_DrawGrid
1425    ==============
1426  */
1427
1428 double two_to_the_power( int power ){
1429         return pow( 2.0f, power );
1430 }
1431
1432 void XYWnd::XY_DrawAxis( void ){
1433         if ( g_xywindow_globals_private.show_axis ) {
1434                 const char g_AxisName[3] = { 'X', 'Y', 'Z' };
1435                 const int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
1436                 const int nDim2 = ( m_viewType == XY ) ? 1 : 2;
1437                 const int w = ( m_nWidth / 2 / m_fScale );
1438                 const int h = ( m_nHeight / 2 / m_fScale );
1439
1440                 const Vector3& colourX = ( m_viewType == YZ ) ? g_xywindow_globals.AxisColorY : g_xywindow_globals.AxisColorX;
1441                 const Vector3& colourY = ( m_viewType == XY ) ? g_xywindow_globals.AxisColorY : g_xywindow_globals.AxisColorZ;
1442
1443                 // draw two lines with corresponding axis colors to highlight current view
1444                 // horizontal line: nDim1 color
1445                 glLineWidth( 2 );
1446                 glBegin( GL_LINES );
1447                 glColor3fv( vector3_to_array( colourX ) );
1448                 glVertex2f( m_vOrigin[nDim1] - w + 40 / m_fScale, m_vOrigin[nDim2] + h - 45 / m_fScale );
1449                 glVertex2f( m_vOrigin[nDim1] - w + 65 / m_fScale, m_vOrigin[nDim2] + h - 45 / m_fScale );
1450                 glVertex2f( 0, 0 );
1451                 glVertex2f( 32 / m_fScale, 0 );
1452                 glColor3fv( vector3_to_array( colourY ) );
1453                 glVertex2f( m_vOrigin[nDim1] - w + 40 / m_fScale, m_vOrigin[nDim2] + h - 45 / m_fScale );
1454                 glVertex2f( m_vOrigin[nDim1] - w + 40 / m_fScale, m_vOrigin[nDim2] + h - 20 / m_fScale );
1455                 glVertex2f( 0, 0 );
1456                 glVertex2f( 0, 32 / m_fScale );
1457                 glEnd();
1458                 glLineWidth( 1 );
1459                 // now print axis symbols
1460                 glColor3fv( vector3_to_array( colourX ) );
1461                 glRasterPos2f( m_vOrigin[nDim1] - w + 55 / m_fScale, m_vOrigin[nDim2] + h - 55 / m_fScale );
1462                 GlobalOpenGL().drawChar( g_AxisName[nDim1] );
1463                 glRasterPos2f( 28 / m_fScale, -10 / m_fScale );
1464                 GlobalOpenGL().drawChar( g_AxisName[nDim1] );
1465                 glColor3fv( vector3_to_array( colourY ) );
1466                 glRasterPos2f( m_vOrigin[nDim1] - w + 25 / m_fScale, m_vOrigin[nDim2] + h - 30 / m_fScale );
1467                 GlobalOpenGL().drawChar( g_AxisName[nDim2] );
1468                 glRasterPos2f( -10 / m_fScale, 28 / m_fScale );
1469                 GlobalOpenGL().drawChar( g_AxisName[nDim2] );
1470         }
1471 }
1472
1473 void XYWnd::XY_DrawBackground( void ){
1474         glPushAttrib( GL_ALL_ATTRIB_BITS );
1475
1476         glEnable( GL_TEXTURE_2D );
1477         glEnable( GL_BLEND );
1478         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1479         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1480         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
1481         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
1482
1483         glPolygonMode( GL_FRONT, GL_FILL );
1484
1485         glBindTexture( GL_TEXTURE_2D, m_tex->texture_number );
1486         glBegin( GL_QUADS );
1487
1488         glColor4f( 1.0, 1.0, 1.0, m_alpha );
1489         glTexCoord2f( 0.0, 1.0 );
1490         glVertex2f( m_xmin, m_ymin );
1491
1492         glTexCoord2f( 1.0, 1.0 );
1493         glVertex2f( m_xmax, m_ymin );
1494
1495         glTexCoord2f( 1.0, 0.0 );
1496         glVertex2f( m_xmax, m_ymax );
1497
1498         glTexCoord2f( 0.0, 0.0 );
1499         glVertex2f( m_xmin, m_ymax );
1500
1501         glEnd();
1502         glBindTexture( GL_TEXTURE_2D, 0 );
1503
1504         glPopAttrib();
1505 }
1506
1507 void XYWnd::XY_DrawGrid( void ) {
1508         float x, y, xb, xe, yb, ye;
1509         float w, h, a;
1510         char text[32];
1511         float step, minor_step, stepx, stepy;
1512         step = minor_step = stepx = stepy = GetGridSize();
1513
1514         int minor_power = Grid_getPower();
1515         int mask;
1516
1517         while ( ( minor_step * m_fScale ) <= 4.0f ) { // make sure minor grid spacing is at least 4 pixels on the screen
1518                 ++minor_power;
1519                 minor_step *= 2;
1520         }
1521         int power = minor_power;
1522         while ( ( power % 3 ) != 0 || ( step * m_fScale ) <= 32.0f ) { // make sure major grid spacing is at least 32 pixels on the screen
1523                 ++power;
1524                 step = float(two_to_the_power( power ) );
1525         }
1526         mask = ( 1 << ( power - minor_power ) ) - 1;
1527         while ( ( stepx * m_fScale ) <= 32.0f ) // text step x must be at least 32
1528                 stepx *= 2;
1529         while ( ( stepy * m_fScale ) <= 32.0f ) // text step y must be at least 32
1530                 stepy *= 2;
1531
1532         a = ( ( GetSnapGridSize() > 0.0f ) ? 1.0f : 0.3f );
1533
1534         glDisable( GL_TEXTURE_2D );
1535         glDisable( GL_TEXTURE_1D );
1536         glDisable( GL_DEPTH_TEST );
1537         glDisable( GL_BLEND );
1538         glLineWidth( 1 );
1539
1540         w = ( m_nWidth / 2 / m_fScale );
1541         h = ( m_nHeight / 2 / m_fScale );
1542
1543         const int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
1544         const int nDim2 = ( m_viewType == XY ) ? 1 : 2;
1545
1546         xb = m_vOrigin[nDim1] - w;
1547         if ( xb < region_mins[nDim1] ) {
1548                 xb = region_mins[nDim1];
1549         }
1550         xb = step * floor( xb / step );
1551
1552         xe = m_vOrigin[nDim1] + w;
1553         if ( xe > region_maxs[nDim1] ) {
1554                 xe = region_maxs[nDim1];
1555         }
1556         xe = step * ceil( xe / step );
1557
1558         yb = m_vOrigin[nDim2] - h;
1559         if ( yb < region_mins[nDim2] ) {
1560                 yb = region_mins[nDim2];
1561         }
1562         yb = step * floor( yb / step );
1563
1564         ye = m_vOrigin[nDim2] + h;
1565         if ( ye > region_maxs[nDim2] ) {
1566                 ye = region_maxs[nDim2];
1567         }
1568         ye = step * ceil( ye / step );
1569
1570 #define COLORS_DIFFER( a,b ) \
1571         ( ( a )[0] != ( b )[0] || \
1572           ( a )[1] != ( b )[1] || \
1573           ( a )[2] != ( b )[2] )
1574
1575         // djbob
1576         // draw minor blocks
1577         if ( g_xywindow_globals_private.d_showgrid || a < 1.0f ) {
1578                 if ( a < 1.0f ) {
1579                         glEnable( GL_BLEND );
1580                 }
1581
1582                 if ( COLORS_DIFFER( g_xywindow_globals.color_gridminor, g_xywindow_globals.color_gridback ) ) {
1583                         glColor4fv( vector4_to_array( Vector4( g_xywindow_globals.color_gridminor, a ) ) );
1584
1585                         glBegin( GL_LINES );
1586                         int i = 0;
1587                         for ( x = xb ; x < xe ; x += minor_step, ++i ) {
1588                                 if ( ( i & mask ) != 0 ) {
1589                                         glVertex2f( x, yb );
1590                                         glVertex2f( x, ye );
1591                                 }
1592                         }
1593                         i = 0;
1594                         for ( y = yb ; y < ye ; y += minor_step, ++i ) {
1595                                 if ( ( i & mask ) != 0 ) {
1596                                         glVertex2f( xb, y );
1597                                         glVertex2f( xe, y );
1598                                 }
1599                         }
1600                         glEnd();
1601                 }
1602
1603                 // draw major blocks
1604                 if ( COLORS_DIFFER( g_xywindow_globals.color_gridmajor, g_xywindow_globals.color_gridminor ) ) {
1605                         glColor4fv( vector4_to_array( Vector4( g_xywindow_globals.color_gridmajor, a ) ) );
1606
1607                         glBegin( GL_LINES );
1608                         for ( x = xb ; x <= xe ; x += step ) {
1609                                 glVertex2f( x, yb );
1610                                 glVertex2f( x, ye );
1611                         }
1612                         for ( y = yb ; y <= ye ; y += step ) {
1613                                 glVertex2f( xb, y );
1614                                 glVertex2f( xe, y );
1615                         }
1616                         glEnd();
1617                 }
1618
1619                 if ( a < 1.0f ) {
1620                         glDisable( GL_BLEND );
1621                 }
1622         }
1623
1624         // draw coordinate text if needed
1625         if ( g_xywindow_globals_private.show_coordinates ) {
1626                 glColor4fv( vector4_to_array( Vector4( g_xywindow_globals.color_gridtext, 1.0f ) ) );
1627                 float offx = m_vOrigin[nDim2] + h - ( 4 + GlobalOpenGL().m_font->getPixelAscent() ) / m_fScale;
1628                 float offy = m_vOrigin[nDim1] - w +  4                                            / m_fScale;
1629                 for ( x = xb - fmod( xb, stepx ); x <= xe ; x += stepx ) {
1630                         glRasterPos2f( x, offx );
1631                         sprintf( text, "%g", x );
1632                         GlobalOpenGL().drawString( text );
1633                 }
1634                 for ( y = yb - fmod( yb, stepy ); y <= ye ; y += stepy ) {
1635                         glRasterPos2f( offy, y );
1636                         sprintf( text, "%g", y );
1637                         GlobalOpenGL().drawString( text );
1638                 }
1639
1640                 if ( Active() ) {
1641                         glColor3fv( vector3_to_array( g_xywindow_globals.color_viewname ) );
1642                 }
1643
1644                 // we do this part (the old way) only if show_axis is disabled
1645                 if ( !g_xywindow_globals_private.show_axis ) {
1646                         glRasterPos2f( m_vOrigin[nDim1] - w + 35 / m_fScale, m_vOrigin[nDim2] + h - 20 / m_fScale );
1647
1648                         GlobalOpenGL().drawString( ViewType_getTitle( m_viewType ) );
1649                 }
1650         }
1651
1652         XYWnd::XY_DrawAxis();
1653
1654         // show current work zone?
1655         // the work zone is used to place dropped points and brushes
1656         if ( g_xywindow_globals_private.d_show_work ) {
1657                 glColor4f( 1.0f, 0.0f, 0.0f, 1.0f );
1658                 glBegin( GL_LINES );
1659                 glVertex2f( xb, Select_getWorkZone().d_work_min[nDim2] );
1660                 glVertex2f( xe, Select_getWorkZone().d_work_min[nDim2] );
1661                 glVertex2f( xb, Select_getWorkZone().d_work_max[nDim2] );
1662                 glVertex2f( xe, Select_getWorkZone().d_work_max[nDim2] );
1663                 glVertex2f( Select_getWorkZone().d_work_min[nDim1], yb );
1664                 glVertex2f( Select_getWorkZone().d_work_min[nDim1], ye );
1665                 glVertex2f( Select_getWorkZone().d_work_max[nDim1], yb );
1666                 glVertex2f( Select_getWorkZone().d_work_max[nDim1], ye );
1667                 glEnd();
1668         }
1669 }
1670
1671 /*
1672    ==============
1673    XY_DrawBlockGrid
1674    ==============
1675  */
1676 void XYWnd::XY_DrawBlockGrid(){
1677         if ( Map_FindWorldspawn( g_map ) == 0 ) {
1678                 return;
1679         }
1680         const char *value = Node_getEntity( *Map_GetWorldspawn( g_map ) )->getKeyValue( "_blocksize" );
1681         if ( strlen( value ) ) {
1682                 sscanf( value, "%i", &g_xywindow_globals_private.blockSize );
1683         }
1684
1685         if ( !g_xywindow_globals_private.blockSize || g_xywindow_globals_private.blockSize > 65536 || g_xywindow_globals_private.blockSize < 1024 ) {
1686                 // don't use custom blocksize if it is less than the default, or greater than the maximum world coordinate
1687                 g_xywindow_globals_private.blockSize = 1024;
1688         }
1689
1690         float x, y, xb, xe, yb, ye;
1691         float w, h;
1692         char text[32];
1693
1694         glDisable( GL_TEXTURE_2D );
1695         glDisable( GL_TEXTURE_1D );
1696         glDisable( GL_DEPTH_TEST );
1697         glDisable( GL_BLEND );
1698
1699         w = ( m_nWidth / 2 / m_fScale );
1700         h = ( m_nHeight / 2 / m_fScale );
1701
1702         int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
1703         int nDim2 = ( m_viewType == XY ) ? 1 : 2;
1704
1705         xb = m_vOrigin[nDim1] - w;
1706         if ( xb < region_mins[nDim1] ) {
1707                 xb = region_mins[nDim1];
1708         }
1709         xb = static_cast<float>( g_xywindow_globals_private.blockSize * floor( xb / g_xywindow_globals_private.blockSize ) );
1710
1711         xe = m_vOrigin[nDim1] + w;
1712         if ( xe > region_maxs[nDim1] ) {
1713                 xe = region_maxs[nDim1];
1714         }
1715         xe = static_cast<float>( g_xywindow_globals_private.blockSize * ceil( xe / g_xywindow_globals_private.blockSize ) );
1716
1717         yb = m_vOrigin[nDim2] - h;
1718         if ( yb < region_mins[nDim2] ) {
1719                 yb = region_mins[nDim2];
1720         }
1721         yb = static_cast<float>( g_xywindow_globals_private.blockSize * floor( yb / g_xywindow_globals_private.blockSize ) );
1722
1723         ye = m_vOrigin[nDim2] + h;
1724         if ( ye > region_maxs[nDim2] ) {
1725                 ye = region_maxs[nDim2];
1726         }
1727         ye = static_cast<float>( g_xywindow_globals_private.blockSize * ceil( ye / g_xywindow_globals_private.blockSize ) );
1728
1729         // draw major blocks
1730
1731         glColor3fv( vector3_to_array( g_xywindow_globals.color_gridblock ) );
1732         glLineWidth( 2 );
1733
1734         glBegin( GL_LINES );
1735
1736         for ( x = xb ; x <= xe ; x += g_xywindow_globals_private.blockSize )
1737         {
1738                 glVertex2f( x, yb );
1739                 glVertex2f( x, ye );
1740         }
1741
1742         if ( m_viewType == XY ) {
1743                 for ( y = yb ; y <= ye ; y += g_xywindow_globals_private.blockSize )
1744                 {
1745                         glVertex2f( xb, y );
1746                         glVertex2f( xe, y );
1747                 }
1748         }
1749
1750         glEnd();
1751         glLineWidth( 1 );
1752
1753         // draw coordinate text if needed
1754
1755         if ( m_viewType == XY && m_fScale > .1 ) {
1756                 for ( x = xb ; x < xe ; x += g_xywindow_globals_private.blockSize )
1757                         for ( y = yb ; y < ye ; y += g_xywindow_globals_private.blockSize )
1758                         {
1759                                 glRasterPos2f( x + ( g_xywindow_globals_private.blockSize / 2 ), y + ( g_xywindow_globals_private.blockSize / 2 ) );
1760                                 sprintf( text, "%i,%i",(int)floor( x / g_xywindow_globals_private.blockSize ), (int)floor( y / g_xywindow_globals_private.blockSize ) );
1761                                 GlobalOpenGL().drawString( text );
1762                         }
1763         }
1764
1765         glColor4f( 0, 0, 0, 0 );
1766 }
1767
1768 void XYWnd::DrawCameraIcon( const Vector3& origin, const Vector3& angles ){
1769         float x, y, fov, box;
1770         double a;
1771
1772         fov = 48 / m_fScale;
1773         box = 16 / m_fScale;
1774
1775         if ( m_viewType == XY ) {
1776                 x = origin[0];
1777                 y = origin[1];
1778                 a = degrees_to_radians( angles[CAMERA_YAW] );
1779         }
1780         else if ( m_viewType == YZ ) {
1781                 x = origin[1];
1782                 y = origin[2];
1783                 a = degrees_to_radians( angles[CAMERA_PITCH] );
1784         }
1785         else
1786         {
1787                 x = origin[0];
1788                 y = origin[2];
1789                 a = degrees_to_radians( angles[CAMERA_PITCH] );
1790         }
1791
1792         glColor3f( 0.0, 0.0, 1.0 );
1793         glBegin( GL_LINE_STRIP );
1794         glVertex3f( x - box,y,0 );
1795         glVertex3f( x,y + ( box / 2 ),0 );
1796         glVertex3f( x + box,y,0 );
1797         glVertex3f( x,y - ( box / 2 ),0 );
1798         glVertex3f( x - box,y,0 );
1799         glVertex3f( x + box,y,0 );
1800         glEnd();
1801
1802         glBegin( GL_LINE_STRIP );
1803         glVertex3f( x + static_cast<float>( fov * cos( a + c_pi / 4 ) ), y + static_cast<float>( fov * sin( a + c_pi / 4 ) ), 0 );
1804         glVertex3f( x, y, 0 );
1805         glVertex3f( x + static_cast<float>( fov * cos( a - c_pi / 4 ) ), y + static_cast<float>( fov * sin( a - c_pi / 4 ) ), 0 );
1806         glEnd();
1807
1808 }
1809
1810
1811 float Betwixt( float f1, float f2 ){
1812         if ( f1 > f2 ) {
1813                 return f2 + ( ( f1 - f2 ) / 2 );
1814         }
1815         else{
1816                 return f1 + ( ( f2 - f1 ) / 2 );
1817         }
1818 }
1819
1820
1821 // can be greatly simplified but per usual i am in a hurry
1822 // which is not an excuse, just a fact
1823 void XYWnd::PaintSizeInfo( int nDim1, int nDim2, Vector3& vMinBounds, Vector3& vMaxBounds ){
1824         if ( vector3_equal( vMinBounds, vMaxBounds ) ) {
1825                 return;
1826         }
1827         const char* g_pDimStrings[] = {"x:", "y:", "z:"};
1828         typedef const char* OrgStrings[2];
1829         const OrgStrings g_pOrgStrings[] = { { "x:", "y:", }, { "x:", "z:", }, { "y:", "z:", } };
1830
1831         Vector3 vSize( vector3_subtracted( vMaxBounds, vMinBounds ) );
1832
1833         glColor3f( g_xywindow_globals.color_selbrushes[0] * .65f,
1834                            g_xywindow_globals.color_selbrushes[1] * .65f,
1835                            g_xywindow_globals.color_selbrushes[2] * .65f );
1836
1837         StringOutputStream dimensions( 16 );
1838
1839         if ( m_viewType == XY ) {
1840                 glBegin( GL_LINES );
1841
1842                 glVertex3f( vMinBounds[nDim1], vMinBounds[nDim2] - 6.0f  / m_fScale, 0.0f );
1843                 glVertex3f( vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale, 0.0f );
1844
1845                 glVertex3f( vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f  / m_fScale, 0.0f );
1846                 glVertex3f( vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f  / m_fScale, 0.0f );
1847
1848                 glVertex3f( vMaxBounds[nDim1], vMinBounds[nDim2] - 6.0f  / m_fScale, 0.0f );
1849                 glVertex3f( vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale, 0.0f );
1850
1851
1852                 glVertex3f( vMaxBounds[nDim1] + 6.0f  / m_fScale, vMinBounds[nDim2], 0.0f );
1853                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, vMinBounds[nDim2], 0.0f );
1854
1855                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, vMinBounds[nDim2], 0.0f );
1856                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, vMaxBounds[nDim2], 0.0f );
1857
1858                 glVertex3f( vMaxBounds[nDim1] + 6.0f  / m_fScale, vMaxBounds[nDim2], 0.0f );
1859                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, vMaxBounds[nDim2], 0.0f );
1860
1861                 glEnd();
1862
1863                 glRasterPos3f( Betwixt( vMinBounds[nDim1], vMaxBounds[nDim1] ),  vMinBounds[nDim2] - 20.0f  / m_fScale, 0.0f );
1864                 dimensions << g_pDimStrings[nDim1] << vSize[nDim1];
1865                 GlobalOpenGL().drawString( dimensions.c_str() );
1866                 dimensions.clear();
1867
1868                 glRasterPos3f( vMaxBounds[nDim1] + 16.0f  / m_fScale, Betwixt( vMinBounds[nDim2], vMaxBounds[nDim2] ), 0.0f );
1869                 dimensions << g_pDimStrings[nDim2] << vSize[nDim2];
1870                 GlobalOpenGL().drawString( dimensions.c_str() );
1871                 dimensions.clear();
1872
1873                 glRasterPos3f( vMinBounds[nDim1] + 4, vMaxBounds[nDim2] + 8 / m_fScale, 0.0f );
1874                 dimensions << "(" << g_pOrgStrings[0][0] << vMinBounds[nDim1] << "  " << g_pOrgStrings[0][1] << vMaxBounds[nDim2] << ")";
1875                 GlobalOpenGL().drawString( dimensions.c_str() );
1876         }
1877         else if ( m_viewType == XZ ) {
1878                 glBegin( GL_LINES );
1879
1880                 glVertex3f( vMinBounds[nDim1], 0, vMinBounds[nDim2] - 6.0f  / m_fScale );
1881                 glVertex3f( vMinBounds[nDim1], 0, vMinBounds[nDim2] - 10.0f / m_fScale );
1882
1883                 glVertex3f( vMinBounds[nDim1], 0,vMinBounds[nDim2] - 10.0f  / m_fScale );
1884                 glVertex3f( vMaxBounds[nDim1], 0,vMinBounds[nDim2] - 10.0f  / m_fScale );
1885
1886                 glVertex3f( vMaxBounds[nDim1], 0,vMinBounds[nDim2] - 6.0f  / m_fScale );
1887                 glVertex3f( vMaxBounds[nDim1], 0,vMinBounds[nDim2] - 10.0f / m_fScale );
1888
1889
1890                 glVertex3f( vMaxBounds[nDim1] + 6.0f  / m_fScale, 0,vMinBounds[nDim2] );
1891                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, 0,vMinBounds[nDim2] );
1892
1893                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, 0,vMinBounds[nDim2] );
1894                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, 0,vMaxBounds[nDim2] );
1895
1896                 glVertex3f( vMaxBounds[nDim1] + 6.0f  / m_fScale, 0,vMaxBounds[nDim2] );
1897                 glVertex3f( vMaxBounds[nDim1] + 10.0f  / m_fScale, 0,vMaxBounds[nDim2] );
1898
1899                 glEnd();
1900
1901                 glRasterPos3f( Betwixt( vMinBounds[nDim1], vMaxBounds[nDim1] ), 0, vMinBounds[nDim2] - 20.0f  / m_fScale );
1902                 dimensions << g_pDimStrings[nDim1] << vSize[nDim1];
1903                 GlobalOpenGL().drawString( dimensions.c_str() );
1904                 dimensions.clear();
1905
1906                 glRasterPos3f( vMaxBounds[nDim1] + 16.0f  / m_fScale, 0, Betwixt( vMinBounds[nDim2], vMaxBounds[nDim2] ) );
1907                 dimensions << g_pDimStrings[nDim2] << vSize[nDim2];
1908                 GlobalOpenGL().drawString( dimensions.c_str() );
1909                 dimensions.clear();
1910
1911                 glRasterPos3f( vMinBounds[nDim1] + 4, 0, vMaxBounds[nDim2] + 8 / m_fScale );
1912                 dimensions << "(" << g_pOrgStrings[1][0] << vMinBounds[nDim1] << "  " << g_pOrgStrings[1][1] << vMaxBounds[nDim2] << ")";
1913                 GlobalOpenGL().drawString( dimensions.c_str() );
1914         }
1915         else
1916         {
1917                 glBegin( GL_LINES );
1918
1919                 glVertex3f( 0, vMinBounds[nDim1], vMinBounds[nDim2] - 6.0f  / m_fScale );
1920                 glVertex3f( 0, vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale );
1921
1922                 glVertex3f( 0, vMinBounds[nDim1], vMinBounds[nDim2] - 10.0f  / m_fScale );
1923                 glVertex3f( 0, vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f  / m_fScale );
1924
1925                 glVertex3f( 0, vMaxBounds[nDim1], vMinBounds[nDim2] - 6.0f  / m_fScale );
1926                 glVertex3f( 0, vMaxBounds[nDim1], vMinBounds[nDim2] - 10.0f / m_fScale );
1927
1928
1929                 glVertex3f( 0, vMaxBounds[nDim1] + 6.0f  / m_fScale, vMinBounds[nDim2] );
1930                 glVertex3f( 0, vMaxBounds[nDim1] + 10.0f  / m_fScale, vMinBounds[nDim2] );
1931
1932                 glVertex3f( 0, vMaxBounds[nDim1] + 10.0f  / m_fScale, vMinBounds[nDim2] );
1933                 glVertex3f( 0, vMaxBounds[nDim1] + 10.0f  / m_fScale, vMaxBounds[nDim2] );
1934
1935                 glVertex3f( 0, vMaxBounds[nDim1] + 6.0f  / m_fScale, vMaxBounds[nDim2] );
1936                 glVertex3f( 0, vMaxBounds[nDim1] + 10.0f  / m_fScale, vMaxBounds[nDim2] );
1937
1938                 glEnd();
1939
1940                 glRasterPos3f( 0, Betwixt( vMinBounds[nDim1], vMaxBounds[nDim1] ),  vMinBounds[nDim2] - 20.0f  / m_fScale );
1941                 dimensions << g_pDimStrings[nDim1] << vSize[nDim1];
1942                 GlobalOpenGL().drawString( dimensions.c_str() );
1943                 dimensions.clear();
1944
1945                 glRasterPos3f( 0, vMaxBounds[nDim1] + 16.0f  / m_fScale, Betwixt( vMinBounds[nDim2], vMaxBounds[nDim2] ) );
1946                 dimensions << g_pDimStrings[nDim2] << vSize[nDim2];
1947                 GlobalOpenGL().drawString( dimensions.c_str() );
1948                 dimensions.clear();
1949
1950                 glRasterPos3f( 0, vMinBounds[nDim1] + 4.0f, vMaxBounds[nDim2] + 8 / m_fScale );
1951                 dimensions << "(" << g_pOrgStrings[2][0] << vMinBounds[nDim1] << "  " << g_pOrgStrings[2][1] << vMaxBounds[nDim2] << ")";
1952                 GlobalOpenGL().drawString( dimensions.c_str() );
1953         }
1954 }
1955
1956 class XYRenderer : public Renderer
1957 {
1958 struct state_type
1959 {
1960         state_type() :
1961                 m_highlight( 0 ),
1962                 m_state( 0 ){
1963         }
1964         unsigned int m_highlight;
1965         Shader* m_state;
1966 };
1967 public:
1968 XYRenderer( RenderStateFlags globalstate, Shader* selected ) :
1969         m_globalstate( globalstate ),
1970         m_state_selected( selected ){
1971         ASSERT_NOTNULL( selected );
1972         m_state_stack.push_back( state_type() );
1973 }
1974
1975 void SetState( Shader* state, EStyle style ){
1976         ASSERT_NOTNULL( state );
1977         if ( style == eWireframeOnly ) {
1978                 m_state_stack.back().m_state = state;
1979         }
1980 }
1981 const EStyle getStyle() const {
1982         return eWireframeOnly;
1983 }
1984 void PushState(){
1985         m_state_stack.push_back( m_state_stack.back() );
1986 }
1987 void PopState(){
1988         ASSERT_MESSAGE( !m_state_stack.empty(), "popping empty stack" );
1989         m_state_stack.pop_back();
1990 }
1991 void Highlight( EHighlightMode mode, bool bEnable = true ){
1992         ( bEnable )
1993         ? m_state_stack.back().m_highlight |= mode
1994                                                                                   : m_state_stack.back().m_highlight &= ~mode;
1995 }
1996 void addRenderable( const OpenGLRenderable& renderable, const Matrix4& localToWorld ){
1997         if ( m_state_stack.back().m_highlight & ePrimitive ) {
1998                 m_state_selected->addRenderable( renderable, localToWorld );
1999         }
2000         else
2001         {
2002                 m_state_stack.back().m_state->addRenderable( renderable, localToWorld );
2003         }
2004 }
2005
2006 void render( const Matrix4& modelview, const Matrix4& projection ){
2007         GlobalShaderCache().render( m_globalstate, modelview, projection );
2008 }
2009 private:
2010 std::vector<state_type> m_state_stack;
2011 RenderStateFlags m_globalstate;
2012 Shader* m_state_selected;
2013 };
2014
2015 void XYWnd::updateProjection(){
2016         m_projection[0] = 1.0f / static_cast<float>( m_nWidth / 2 );
2017         m_projection[5] = 1.0f / static_cast<float>( m_nHeight / 2 );
2018         m_projection[10] = 1.0f / ( g_MaxWorldCoord * m_fScale );
2019
2020         m_projection[12] = 0.0f;
2021         m_projection[13] = 0.0f;
2022         m_projection[14] = -1.0f;
2023
2024         m_projection[1] =
2025                 m_projection[2] =
2026                         m_projection[3] =
2027
2028                                 m_projection[4] =
2029                                         m_projection[6] =
2030                                                 m_projection[7] =
2031
2032                                                         m_projection[8] =
2033                                                                 m_projection[9] =
2034                                                                         m_projection[11] = 0.0f;
2035
2036         m_projection[15] = 1.0f;
2037
2038         m_view.Construct( m_projection, m_modelview, m_nWidth, m_nHeight );
2039 }
2040
2041 // note: modelview matrix must have a uniform scale, otherwise strange things happen when rendering the rotation manipulator.
2042 void XYWnd::updateModelview(){
2043         int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
2044         int nDim2 = ( m_viewType == XY ) ? 1 : 2;
2045
2046         // translation
2047         m_modelview[12] = -m_vOrigin[nDim1] * m_fScale;
2048         m_modelview[13] = -m_vOrigin[nDim2] * m_fScale;
2049         m_modelview[14] = g_MaxWorldCoord * m_fScale;
2050
2051         // axis base
2052         switch ( m_viewType )
2053         {
2054         case XY:
2055                 m_modelview[0]  =  m_fScale;
2056                 m_modelview[1]  =  0;
2057                 m_modelview[2]  =  0;
2058
2059                 m_modelview[4]  =  0;
2060                 m_modelview[5]  =  m_fScale;
2061                 m_modelview[6]  =  0;
2062
2063                 m_modelview[8]  =  0;
2064                 m_modelview[9]  =  0;
2065                 m_modelview[10] = -m_fScale;
2066                 break;
2067         case XZ:
2068                 m_modelview[0]  =  m_fScale;
2069                 m_modelview[1]  =  0;
2070                 m_modelview[2]  =  0;
2071
2072                 m_modelview[4]  =  0;
2073                 m_modelview[5]  =  0;
2074                 m_modelview[6]  =  m_fScale;
2075
2076                 m_modelview[8]  =  0;
2077                 m_modelview[9]  =  m_fScale;
2078                 m_modelview[10] =  0;
2079                 break;
2080         case YZ:
2081                 m_modelview[0]  =  0;
2082                 m_modelview[1]  =  0;
2083                 m_modelview[2]  = -m_fScale;
2084
2085                 m_modelview[4]  =  m_fScale;
2086                 m_modelview[5]  =  0;
2087                 m_modelview[6]  =  0;
2088
2089                 m_modelview[8]  =  0;
2090                 m_modelview[9]  =  m_fScale;
2091                 m_modelview[10] =  0;
2092                 break;
2093         }
2094
2095         m_modelview[3] = m_modelview[7] = m_modelview[11] = 0;
2096         m_modelview[15] = 1;
2097
2098         m_view.Construct( m_projection, m_modelview, m_nWidth, m_nHeight );
2099 }
2100
2101 /*
2102    ==============
2103    XY_Draw
2104    ==============
2105  */
2106
2107 //#define DBG_SCENEDUMP
2108
2109 void XYWnd::XY_Draw(){
2110         //
2111         // clear
2112         //
2113         glViewport( 0, 0, m_nWidth, m_nHeight );
2114         glClearColor( g_xywindow_globals.color_gridback[0],
2115                                   g_xywindow_globals.color_gridback[1],
2116                                   g_xywindow_globals.color_gridback[2],0 );
2117
2118         glClear( GL_COLOR_BUFFER_BIT );
2119
2120         //
2121         // set up viewpoint
2122         //
2123
2124         glMatrixMode( GL_PROJECTION );
2125         glLoadMatrixf( reinterpret_cast<const float*>( &m_projection ) );
2126
2127         glMatrixMode( GL_MODELVIEW );
2128         glLoadIdentity();
2129         glScalef( m_fScale, m_fScale, 1 );
2130         int nDim1 = ( m_viewType == YZ ) ? 1 : 0;
2131         int nDim2 = ( m_viewType == XY ) ? 1 : 2;
2132         glTranslatef( -m_vOrigin[nDim1], -m_vOrigin[nDim2], 0 );
2133
2134         glDisable( GL_LINE_STIPPLE );
2135         glLineWidth( 1 );
2136         glDisableClientState( GL_TEXTURE_COORD_ARRAY );
2137         glDisableClientState( GL_NORMAL_ARRAY );
2138         glDisableClientState( GL_COLOR_ARRAY );
2139         glDisable( GL_TEXTURE_2D );
2140         glDisable( GL_LIGHTING );
2141         glDisable( GL_COLOR_MATERIAL );
2142         glDisable( GL_DEPTH_TEST );
2143
2144         if ( m_backgroundActivated ) {
2145                 XY_DrawBackground();
2146         }
2147         XY_DrawGrid();
2148
2149         if ( g_xywindow_globals_private.show_blocks ) {
2150                 XY_DrawBlockGrid();
2151         }
2152
2153         glLoadMatrixf( reinterpret_cast<const float*>( &m_modelview ) );
2154
2155         unsigned int globalstate = RENDER_COLOURARRAY | RENDER_COLOURWRITE | RENDER_POLYGONSMOOTH | RENDER_LINESMOOTH;
2156         if ( !g_xywindow_globals.m_bNoStipple ) {
2157                 globalstate |= RENDER_LINESTIPPLE;
2158         }
2159
2160         {
2161                 XYRenderer renderer( globalstate, m_state_selected );
2162
2163                 Scene_Render( renderer, m_view );
2164
2165                 GlobalOpenGL_debugAssertNoErrors();
2166                 renderer.render( m_modelview, m_projection );
2167                 GlobalOpenGL_debugAssertNoErrors();
2168         }
2169
2170         glDepthMask( GL_FALSE );
2171
2172         GlobalOpenGL_debugAssertNoErrors();
2173
2174         glLoadMatrixf( reinterpret_cast<const float*>( &m_modelview ) );
2175
2176         GlobalOpenGL_debugAssertNoErrors();
2177         glDisable( GL_LINE_STIPPLE );
2178         GlobalOpenGL_debugAssertNoErrors();
2179         glLineWidth( 1 );
2180         GlobalOpenGL_debugAssertNoErrors();
2181         if ( GlobalOpenGL().GL_1_3() ) {
2182                 glActiveTexture( GL_TEXTURE0 );
2183                 glClientActiveTexture( GL_TEXTURE0 );
2184         }
2185         glDisableClientState( GL_TEXTURE_COORD_ARRAY );
2186         GlobalOpenGL_debugAssertNoErrors();
2187         glDisableClientState( GL_NORMAL_ARRAY );
2188         GlobalOpenGL_debugAssertNoErrors();
2189         glDisableClientState( GL_COLOR_ARRAY );
2190         GlobalOpenGL_debugAssertNoErrors();
2191         glDisable( GL_TEXTURE_2D );
2192         GlobalOpenGL_debugAssertNoErrors();
2193         glDisable( GL_LIGHTING );
2194         GlobalOpenGL_debugAssertNoErrors();
2195         glDisable( GL_COLOR_MATERIAL );
2196         GlobalOpenGL_debugAssertNoErrors();
2197
2198         GlobalOpenGL_debugAssertNoErrors();
2199
2200
2201         // size info
2202         if ( g_xywindow_globals_private.m_bSizePaint && GlobalSelectionSystem().countSelected() != 0 ) {
2203                 Vector3 min, max;
2204                 Select_GetBounds( min, max );
2205                 PaintSizeInfo( nDim1, nDim2, min, max );
2206         }
2207
2208         if ( g_bCrossHairs ) {
2209                 glColor4f( 0.2f, 0.9f, 0.2f, 0.8f );
2210                 glBegin( GL_LINES );
2211                 if ( m_viewType == XY ) {
2212                         glVertex2f( 2.0f * g_MinWorldCoord, m_mousePosition[1] );
2213                         glVertex2f( 2.0f * g_MaxWorldCoord, m_mousePosition[1] );
2214                         glVertex2f( m_mousePosition[0], 2.0f * g_MinWorldCoord );
2215                         glVertex2f( m_mousePosition[0], 2.0f * g_MaxWorldCoord );
2216                 }
2217                 else if ( m_viewType == YZ ) {
2218                         glVertex3f( m_mousePosition[0], 2.0f * g_MinWorldCoord, m_mousePosition[2] );
2219                         glVertex3f( m_mousePosition[0], 2.0f * g_MaxWorldCoord, m_mousePosition[2] );
2220                         glVertex3f( m_mousePosition[0], m_mousePosition[1], 2.0f * g_MinWorldCoord );
2221                         glVertex3f( m_mousePosition[0], m_mousePosition[1], 2.0f * g_MaxWorldCoord );
2222                 }
2223                 else
2224                 {
2225                         glVertex3f( 2.0f * g_MinWorldCoord, m_mousePosition[1], m_mousePosition[2] );
2226                         glVertex3f( 2.0f * g_MaxWorldCoord, m_mousePosition[1], m_mousePosition[2] );
2227                         glVertex3f( m_mousePosition[0], m_mousePosition[1], 2.0f * g_MinWorldCoord );
2228                         glVertex3f( m_mousePosition[0], m_mousePosition[1], 2.0f * g_MaxWorldCoord );
2229                 }
2230                 glEnd();
2231         }
2232
2233         if ( ClipMode() ) {
2234                 GlobalClipPoints_Draw( m_fScale );
2235         }
2236
2237         GlobalOpenGL_debugAssertNoErrors();
2238
2239         // reset modelview
2240         glLoadIdentity();
2241         glScalef( m_fScale, m_fScale, 1 );
2242         glTranslatef( -m_vOrigin[nDim1], -m_vOrigin[nDim2], 0 );
2243
2244         DrawCameraIcon( Camera_getOrigin( *g_pParentWnd->GetCamWnd() ), Camera_getAngles( *g_pParentWnd->GetCamWnd() ) );
2245
2246         Feedback_draw2D( m_viewType );
2247
2248         if ( g_xywindow_globals_private.show_outline ) {
2249                 if ( Active() ) {
2250                         glMatrixMode( GL_PROJECTION );
2251                         glLoadIdentity();
2252                         glOrtho( 0, m_nWidth, 0, m_nHeight, 0, 1 );
2253
2254                         glMatrixMode( GL_MODELVIEW );
2255                         glLoadIdentity();
2256
2257                         // four view mode doesn't colorize
2258                         if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit ) {
2259                                 glColor3fv( vector3_to_array( g_xywindow_globals.color_viewname ) );
2260                         }
2261                         else
2262                         {
2263                                 switch ( m_viewType )
2264                                 {
2265                                 case YZ:
2266                                         glColor3fv( vector3_to_array( g_xywindow_globals.AxisColorX ) );
2267                                         break;
2268                                 case XZ:
2269                                         glColor3fv( vector3_to_array( g_xywindow_globals.AxisColorY ) );
2270                                         break;
2271                                 case XY:
2272                                         glColor3fv( vector3_to_array( g_xywindow_globals.AxisColorZ ) );
2273                                         break;
2274                                 }
2275                         }
2276                         glBegin( GL_LINE_LOOP );
2277                         glVertex2f( 0.5, 0.5 );
2278                         glVertex2f( m_nWidth - 0.5, 1 );
2279                         glVertex2f( m_nWidth - 0.5, m_nHeight - 0.5 );
2280                         glVertex2f( 0.5, m_nHeight - 0.5 );
2281                         glEnd();
2282                 }
2283         }
2284
2285         GlobalOpenGL_debugAssertNoErrors();
2286
2287         glFinish();
2288 }
2289
2290 void XYWnd_MouseToPoint( XYWnd* xywnd, int x, int y, Vector3& point ){
2291         xywnd->XY_ToPoint( x, y, point );
2292         xywnd->XY_SnapToGrid( point );
2293
2294         int nDim = ( xywnd->GetViewType() == XY ) ? 2 : ( xywnd->GetViewType() == YZ ) ? 0 : 1;
2295         float fWorkMid = float_mid( Select_getWorkZone().d_work_min[nDim], Select_getWorkZone().d_work_max[nDim] );
2296         point[nDim] = float_snapped( fWorkMid, GetGridSize() );
2297 }
2298
2299 void XYWnd::OnEntityCreate( const char* item ){
2300         StringOutputStream command;
2301         command << "entityCreate -class " << item;
2302         UndoableCommand undo( command.c_str() );
2303         Vector3 point;
2304         XYWnd_MouseToPoint( this, m_entityCreate_x, m_entityCreate_y, point );
2305         Entity_createFromSelection( item, point );
2306 }
2307
2308
2309
2310 void GetFocusPosition( Vector3& position ){
2311         if ( GlobalSelectionSystem().countSelected() != 0 ) {
2312                 Select_GetMid( position );
2313         }
2314         else
2315         {
2316                 position = Camera_getOrigin( *g_pParentWnd->GetCamWnd() );
2317         }
2318 }
2319
2320 void XYWnd_Focus( XYWnd* xywnd ){
2321         Vector3 position;
2322         GetFocusPosition( position );
2323         xywnd->PositionView( position );
2324 }
2325
2326 void XY_Split_Focus(){
2327         Vector3 position;
2328         GetFocusPosition( position );
2329         if ( g_pParentWnd->GetXYWnd() ) {
2330                 g_pParentWnd->GetXYWnd()->PositionView( position );
2331         }
2332         if ( g_pParentWnd->GetXZWnd() ) {
2333                 g_pParentWnd->GetXZWnd()->PositionView( position );
2334         }
2335         if ( g_pParentWnd->GetYZWnd() ) {
2336                 g_pParentWnd->GetYZWnd()->PositionView( position );
2337         }
2338 }
2339
2340 void XY_Focus(){
2341         if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit ) {
2342                 // cannot do this in a split window
2343                 // do something else that the user may want here
2344                 XY_Split_Focus();
2345                 return;
2346         }
2347
2348         XYWnd* xywnd = g_pParentWnd->GetXYWnd();
2349         XYWnd_Focus( xywnd );
2350 }
2351
2352 void XY_Top(){
2353         if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit || g_pParentWnd->CurrentStyle() == MainFrame::eFloating ) {
2354                 // cannot do this in a split window
2355                 // do something else that the user may want here
2356                 XY_Split_Focus();
2357                 return;
2358         }
2359
2360         XYWnd* xywnd = g_pParentWnd->GetXYWnd();
2361         xywnd->SetViewType( XY );
2362         XYWnd_Focus( xywnd );
2363 }
2364
2365 void XY_Side(){
2366         if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit || g_pParentWnd->CurrentStyle() == MainFrame::eFloating ) {
2367                 // cannot do this in a split window
2368                 // do something else that the user may want here
2369                 XY_Split_Focus();
2370                 return;
2371         }
2372
2373         XYWnd* xywnd = g_pParentWnd->GetXYWnd();
2374         xywnd->SetViewType( XZ );
2375         XYWnd_Focus( xywnd );
2376 }
2377
2378 void XY_Front(){
2379         if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit || g_pParentWnd->CurrentStyle() == MainFrame::eFloating ) {
2380                 // cannot do this in a split window
2381                 // do something else that the user may want here
2382                 XY_Split_Focus();
2383                 return;
2384         }
2385
2386         XYWnd* xywnd = g_pParentWnd->GetXYWnd();
2387         xywnd->SetViewType( YZ );
2388         XYWnd_Focus( xywnd );
2389 }
2390
2391 void XY_Next(){
2392         if ( g_pParentWnd->CurrentStyle() == MainFrame::eSplit || g_pParentWnd->CurrentStyle() == MainFrame::eFloating ) {
2393                 // cannot do this in a split window
2394                 // do something else that the user may want here
2395                 XY_Split_Focus();
2396                 return;
2397         }
2398
2399         XYWnd* xywnd = g_pParentWnd->GetXYWnd();
2400         if ( xywnd->GetViewType() == XY ) {
2401                 xywnd->SetViewType( XZ );
2402         }
2403         else if ( xywnd->GetViewType() ==  XZ ) {
2404                 xywnd->SetViewType( YZ );
2405         }
2406         else{
2407                 xywnd->SetViewType( XY );
2408         }
2409         XYWnd_Focus( xywnd );
2410 }
2411
2412 void XY_Zoom100(){
2413         if ( g_pParentWnd->GetXYWnd() ) {
2414                 g_pParentWnd->GetXYWnd()->SetScale( 1 );
2415         }
2416         if ( g_pParentWnd->GetXZWnd() ) {
2417                 g_pParentWnd->GetXZWnd()->SetScale( 1 );
2418         }
2419         if ( g_pParentWnd->GetYZWnd() ) {
2420                 g_pParentWnd->GetYZWnd()->SetScale( 1 );
2421         }
2422 }
2423
2424 void XY_ZoomIn(){
2425         XYWnd_ZoomIn( g_pParentWnd->ActiveXY() );
2426 }
2427
2428 // NOTE: the zoom out factor is 4/5, we could think about customizing it
2429 //  we don't go below a zoom factor corresponding to 10% of the max world size
2430 //  (this has to be computed against the window size)
2431 void XY_ZoomOut(){
2432         XYWnd_ZoomOut( g_pParentWnd->ActiveXY() );
2433 }
2434
2435
2436
2437 void ToggleShowCrosshair(){
2438         g_bCrossHairs ^= 1;
2439         XY_UpdateAllWindows();
2440 }
2441
2442 void ToggleShowSizeInfo(){
2443         g_xywindow_globals_private.m_bSizePaint = !g_xywindow_globals_private.m_bSizePaint;
2444         XY_UpdateAllWindows();
2445 }
2446
2447 void ToggleShowGrid(){
2448         g_xywindow_globals_private.d_showgrid = !g_xywindow_globals_private.d_showgrid;
2449         XY_UpdateAllWindows();
2450 }
2451
2452 ToggleShown g_xy_top_shown( true );
2453
2454 void XY_Top_Shown_Construct( GtkWindow* parent ){
2455         g_xy_top_shown.connect( GTK_WIDGET( parent ) );
2456 }
2457
2458 ToggleShown g_yz_side_shown( false );
2459
2460 void YZ_Side_Shown_Construct( GtkWindow* parent ){
2461         g_yz_side_shown.connect( GTK_WIDGET( parent ) );
2462 }
2463
2464 ToggleShown g_xz_front_shown( false );
2465
2466 void XZ_Front_Shown_Construct( GtkWindow* parent ){
2467         g_xz_front_shown.connect( GTK_WIDGET( parent ) );
2468 }
2469
2470
2471 class EntityClassMenu : public ModuleObserver
2472 {
2473 std::size_t m_unrealised;
2474 public:
2475 EntityClassMenu() : m_unrealised( 1 ){
2476 }
2477 void realise(){
2478         if ( --m_unrealised == 0 ) {
2479         }
2480 }
2481 void unrealise(){
2482         if ( ++m_unrealised == 1 ) {
2483                 if ( XYWnd::m_mnuDrop != 0 ) {
2484                         gtk_widget_destroy( GTK_WIDGET( XYWnd::m_mnuDrop ) );
2485                         XYWnd::m_mnuDrop = 0;
2486                 }
2487         }
2488 }
2489 };
2490
2491 EntityClassMenu g_EntityClassMenu;
2492
2493
2494
2495
2496 void ShowNamesToggle(){
2497         GlobalEntityCreator().setShowNames( !GlobalEntityCreator().getShowNames() );
2498         XY_UpdateAllWindows();
2499 }
2500 typedef FreeCaller<ShowNamesToggle> ShowNamesToggleCaller;
2501 void ShowNamesExport( const BoolImportCallback& importer ){
2502         importer( GlobalEntityCreator().getShowNames() );
2503 }
2504 typedef FreeCaller1<const BoolImportCallback&, ShowNamesExport> ShowNamesExportCaller;
2505
2506 void ShowAnglesToggle(){
2507         GlobalEntityCreator().setShowAngles( !GlobalEntityCreator().getShowAngles() );
2508         XY_UpdateAllWindows();
2509 }
2510 typedef FreeCaller<ShowAnglesToggle> ShowAnglesToggleCaller;
2511 void ShowAnglesExport( const BoolImportCallback& importer ){
2512         importer( GlobalEntityCreator().getShowAngles() );
2513 }
2514 typedef FreeCaller1<const BoolImportCallback&, ShowAnglesExport> ShowAnglesExportCaller;
2515
2516 void ShowBlocksToggle(){
2517         g_xywindow_globals_private.show_blocks ^= 1;
2518         XY_UpdateAllWindows();
2519 }
2520 typedef FreeCaller<ShowBlocksToggle> ShowBlocksToggleCaller;
2521 void ShowBlocksExport( const BoolImportCallback& importer ){
2522         importer( g_xywindow_globals_private.show_blocks );
2523 }
2524 typedef FreeCaller1<const BoolImportCallback&, ShowBlocksExport> ShowBlocksExportCaller;
2525
2526 void ShowCoordinatesToggle(){
2527         g_xywindow_globals_private.show_coordinates ^= 1;
2528         XY_UpdateAllWindows();
2529 }
2530 typedef FreeCaller<ShowCoordinatesToggle> ShowCoordinatesToggleCaller;
2531 void ShowCoordinatesExport( const BoolImportCallback& importer ){
2532         importer( g_xywindow_globals_private.show_coordinates );
2533 }
2534 typedef FreeCaller1<const BoolImportCallback&, ShowCoordinatesExport> ShowCoordinatesExportCaller;
2535
2536 void ShowOutlineToggle(){
2537         g_xywindow_globals_private.show_outline ^= 1;
2538         XY_UpdateAllWindows();
2539 }
2540 typedef FreeCaller<ShowOutlineToggle> ShowOutlineToggleCaller;
2541 void ShowOutlineExport( const BoolImportCallback& importer ){
2542         importer( g_xywindow_globals_private.show_outline );
2543 }
2544 typedef FreeCaller1<const BoolImportCallback&, ShowOutlineExport> ShowOutlineExportCaller;
2545
2546 void ShowAxesToggle(){
2547         g_xywindow_globals_private.show_axis ^= 1;
2548         XY_UpdateAllWindows();
2549 }
2550 typedef FreeCaller<ShowAxesToggle> ShowAxesToggleCaller;
2551 void ShowAxesExport( const BoolImportCallback& importer ){
2552         importer( g_xywindow_globals_private.show_axis );
2553 }
2554 typedef FreeCaller1<const BoolImportCallback&, ShowAxesExport> ShowAxesExportCaller;
2555
2556 void ShowWorkzoneToggle(){
2557         g_xywindow_globals_private.d_show_work ^= 1;
2558         XY_UpdateAllWindows();
2559 }
2560 typedef FreeCaller<ShowWorkzoneToggle> ShowWorkzoneToggleCaller;
2561 void ShowWorkzoneExport( const BoolImportCallback& importer ){
2562         importer( g_xywindow_globals_private.d_show_work );
2563 }
2564 typedef FreeCaller1<const BoolImportCallback&, ShowWorkzoneExport> ShowWorkzoneExportCaller;
2565
2566 ShowNamesExportCaller g_show_names_caller;
2567 BoolExportCallback g_show_names_callback( g_show_names_caller );
2568 ToggleItem g_show_names( g_show_names_callback );
2569
2570 ShowAnglesExportCaller g_show_angles_caller;
2571 BoolExportCallback g_show_angles_callback( g_show_angles_caller );
2572 ToggleItem g_show_angles( g_show_angles_callback );
2573
2574 ShowBlocksExportCaller g_show_blocks_caller;
2575 BoolExportCallback g_show_blocks_callback( g_show_blocks_caller );
2576 ToggleItem g_show_blocks( g_show_blocks_callback );
2577
2578 ShowCoordinatesExportCaller g_show_coordinates_caller;
2579 BoolExportCallback g_show_coordinates_callback( g_show_coordinates_caller );
2580 ToggleItem g_show_coordinates( g_show_coordinates_callback );
2581
2582 ShowOutlineExportCaller g_show_outline_caller;
2583 BoolExportCallback g_show_outline_callback( g_show_outline_caller );
2584 ToggleItem g_show_outline( g_show_outline_callback );
2585
2586 ShowAxesExportCaller g_show_axes_caller;
2587 BoolExportCallback g_show_axes_callback( g_show_axes_caller );
2588 ToggleItem g_show_axes( g_show_axes_callback );
2589
2590 ShowWorkzoneExportCaller g_show_workzone_caller;
2591 BoolExportCallback g_show_workzone_callback( g_show_workzone_caller );
2592 ToggleItem g_show_workzone( g_show_workzone_callback );
2593
2594 void XYShow_registerCommands(){
2595         GlobalToggles_insert( "ShowAngles", ShowAnglesToggleCaller(), ToggleItem::AddCallbackCaller( g_show_angles ) );
2596         GlobalToggles_insert( "ShowNames", ShowNamesToggleCaller(), ToggleItem::AddCallbackCaller( g_show_names ) );
2597         GlobalToggles_insert( "ShowBlocks", ShowBlocksToggleCaller(), ToggleItem::AddCallbackCaller( g_show_blocks ) );
2598         GlobalToggles_insert( "ShowCoordinates", ShowCoordinatesToggleCaller(), ToggleItem::AddCallbackCaller( g_show_coordinates ) );
2599         GlobalToggles_insert( "ShowWindowOutline", ShowOutlineToggleCaller(), ToggleItem::AddCallbackCaller( g_show_outline ) );
2600         GlobalToggles_insert( "ShowAxes", ShowAxesToggleCaller(), ToggleItem::AddCallbackCaller( g_show_axes ) );
2601         GlobalToggles_insert( "ShowWorkzone", ShowWorkzoneToggleCaller(), ToggleItem::AddCallbackCaller( g_show_workzone ) );
2602 }
2603
2604 void XYWnd_registerShortcuts(){
2605         command_connect_accelerator( "ToggleCrosshairs" );
2606         command_connect_accelerator( "ToggleSizePaint" );
2607 }
2608
2609
2610
2611 void Orthographic_constructPreferences( PreferencesPage& page ){
2612         page.appendCheckBox( "", "Solid selection boxes", g_xywindow_globals.m_bNoStipple );
2613         page.appendCheckBox( "", "Display size info", g_xywindow_globals_private.m_bSizePaint );
2614         page.appendCheckBox( "", "Chase mouse during drags", g_xywindow_globals_private.m_bChaseMouse );
2615         page.appendCheckBox( "", "Update views on camera move", g_xywindow_globals_private.m_bCamXYUpdate );
2616 }
2617 void Orthographic_constructPage( PreferenceGroup& group ){
2618         PreferencesPage page( group.createPage( "Orthographic", "Orthographic View Preferences" ) );
2619         Orthographic_constructPreferences( page );
2620 }
2621 void Orthographic_registerPreferencesPage(){
2622         PreferencesDialog_addSettingsPage( FreeCaller1<PreferenceGroup&, Orthographic_constructPage>() );
2623 }
2624
2625 void Clipper_constructPreferences( PreferencesPage& page ){
2626         page.appendCheckBox( "", "Clipper tool uses caulk", g_clip_useCaulk );
2627 }
2628 void Clipper_constructPage( PreferenceGroup& group ){
2629         PreferencesPage page( group.createPage( "Clipper", "Clipper Tool Settings" ) );
2630         Clipper_constructPreferences( page );
2631 }
2632 void Clipper_registerPreferencesPage(){
2633         PreferencesDialog_addSettingsPage( FreeCaller1<PreferenceGroup&, Clipper_constructPage>() );
2634 }
2635
2636
2637 #include "preferencesystem.h"
2638 #include "stringio.h"
2639
2640
2641
2642
2643 void ToggleShown_importBool( ToggleShown& self, bool value ){
2644         self.set( value );
2645 }
2646 typedef ReferenceCaller1<ToggleShown, bool, ToggleShown_importBool> ToggleShownImportBoolCaller;
2647 void ToggleShown_exportBool( const ToggleShown& self, const BoolImportCallback& importer ){
2648         importer( self.active() );
2649 }
2650 typedef ConstReferenceCaller1<ToggleShown, const BoolImportCallback&, ToggleShown_exportBool> ToggleShownExportBoolCaller;
2651
2652
2653 void XYWindow_Construct(){
2654         GlobalCommands_insert( "ToggleCrosshairs", FreeCaller<ToggleShowCrosshair>(), Accelerator( 'X', (GdkModifierType)GDK_SHIFT_MASK ) );
2655         GlobalCommands_insert( "ToggleSizePaint", FreeCaller<ToggleShowSizeInfo>(), Accelerator( 'J' ) );
2656         GlobalCommands_insert( "ToggleGrid", FreeCaller<ToggleShowGrid>(), Accelerator( '0' ) );
2657
2658         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 ) ) );
2659         GlobalToggles_insert( "ToggleSideView", ToggleShown::ToggleCaller( g_yz_side_shown ), ToggleItem::AddCallbackCaller( g_yz_side_shown.m_item ) );
2660         GlobalToggles_insert( "ToggleFrontView", ToggleShown::ToggleCaller( g_xz_front_shown ), ToggleItem::AddCallbackCaller( g_xz_front_shown.m_item ) );
2661         GlobalCommands_insert( "NextView", FreeCaller<XY_Next>(), Accelerator( GDK_Tab, (GdkModifierType)GDK_CONTROL_MASK ) );
2662         GlobalCommands_insert( "ZoomIn", FreeCaller<XY_ZoomIn>(), Accelerator( GDK_Delete ) );
2663         GlobalCommands_insert( "ZoomOut", FreeCaller<XY_ZoomOut>(), Accelerator( GDK_Insert ) );
2664         GlobalCommands_insert( "ViewTop", FreeCaller<XY_Top>(), Accelerator( GDK_KP_Home ) );
2665         GlobalCommands_insert( "ViewSide", FreeCaller<XY_Side>(), Accelerator( GDK_KP_Page_Down ) );
2666         GlobalCommands_insert( "ViewFront", FreeCaller<XY_Front>(), Accelerator( GDK_KP_End ) );
2667         GlobalCommands_insert( "Zoom100", FreeCaller<XY_Zoom100>() );
2668         GlobalCommands_insert( "CenterXYView", FreeCaller<XY_Focus>(), Accelerator( GDK_Tab, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
2669
2670         GlobalPreferenceSystem().registerPreference( "ClipCaulk", BoolImportStringCaller( g_clip_useCaulk ), BoolExportStringCaller( g_clip_useCaulk ) );
2671
2672         GlobalPreferenceSystem().registerPreference( "NewRightClick", BoolImportStringCaller( g_xywindow_globals.m_bRightClick ), BoolExportStringCaller( g_xywindow_globals.m_bRightClick ) );
2673         GlobalPreferenceSystem().registerPreference( "ChaseMouse", BoolImportStringCaller( g_xywindow_globals_private.m_bChaseMouse ), BoolExportStringCaller( g_xywindow_globals_private.m_bChaseMouse ) );
2674         GlobalPreferenceSystem().registerPreference( "SizePainting", BoolImportStringCaller( g_xywindow_globals_private.m_bSizePaint ), BoolExportStringCaller( g_xywindow_globals_private.m_bSizePaint ) );
2675         GlobalPreferenceSystem().registerPreference( "NoStipple", BoolImportStringCaller( g_xywindow_globals.m_bNoStipple ), BoolExportStringCaller( g_xywindow_globals.m_bNoStipple ) );
2676         GlobalPreferenceSystem().registerPreference( "SI_ShowCoords", BoolImportStringCaller( g_xywindow_globals_private.show_coordinates ), BoolExportStringCaller( g_xywindow_globals_private.show_coordinates ) );
2677         GlobalPreferenceSystem().registerPreference( "SI_ShowOutlines", BoolImportStringCaller( g_xywindow_globals_private.show_outline ), BoolExportStringCaller( g_xywindow_globals_private.show_outline ) );
2678         GlobalPreferenceSystem().registerPreference( "SI_ShowAxis", BoolImportStringCaller( g_xywindow_globals_private.show_axis ), BoolExportStringCaller( g_xywindow_globals_private.show_axis ) );
2679         GlobalPreferenceSystem().registerPreference( "CamXYUpdate", BoolImportStringCaller( g_xywindow_globals_private.m_bCamXYUpdate ), BoolExportStringCaller( g_xywindow_globals_private.m_bCamXYUpdate ) );
2680         GlobalPreferenceSystem().registerPreference( "ShowWorkzone", BoolImportStringCaller( g_xywindow_globals_private.d_show_work ), BoolExportStringCaller( g_xywindow_globals_private.d_show_work ) );
2681
2682         GlobalPreferenceSystem().registerPreference( "SI_AxisColors0", Vector3ImportStringCaller( g_xywindow_globals.AxisColorX ), Vector3ExportStringCaller( g_xywindow_globals.AxisColorX ) );
2683         GlobalPreferenceSystem().registerPreference( "SI_AxisColors1", Vector3ImportStringCaller( g_xywindow_globals.AxisColorY ), Vector3ExportStringCaller( g_xywindow_globals.AxisColorY ) );
2684         GlobalPreferenceSystem().registerPreference( "SI_AxisColors2", Vector3ImportStringCaller( g_xywindow_globals.AxisColorZ ), Vector3ExportStringCaller( g_xywindow_globals.AxisColorZ ) );
2685         GlobalPreferenceSystem().registerPreference( "SI_Colors1", Vector3ImportStringCaller( g_xywindow_globals.color_gridback ), Vector3ExportStringCaller( g_xywindow_globals.color_gridback ) );
2686         GlobalPreferenceSystem().registerPreference( "SI_Colors2", Vector3ImportStringCaller( g_xywindow_globals.color_gridminor ), Vector3ExportStringCaller( g_xywindow_globals.color_gridminor ) );
2687         GlobalPreferenceSystem().registerPreference( "SI_Colors3", Vector3ImportStringCaller( g_xywindow_globals.color_gridmajor ), Vector3ExportStringCaller( g_xywindow_globals.color_gridmajor ) );
2688         GlobalPreferenceSystem().registerPreference( "SI_Colors6", Vector3ImportStringCaller( g_xywindow_globals.color_gridblock ), Vector3ExportStringCaller( g_xywindow_globals.color_gridblock ) );
2689         GlobalPreferenceSystem().registerPreference( "SI_Colors7", Vector3ImportStringCaller( g_xywindow_globals.color_gridtext ), Vector3ExportStringCaller( g_xywindow_globals.color_gridtext ) );
2690         GlobalPreferenceSystem().registerPreference( "SI_Colors8", Vector3ImportStringCaller( g_xywindow_globals.color_brushes ), Vector3ExportStringCaller( g_xywindow_globals.color_brushes ) );
2691         GlobalPreferenceSystem().registerPreference( "SI_Colors14", Vector3ImportStringCaller( g_xywindow_globals.color_gridmajor_alt ), Vector3ExportStringCaller( g_xywindow_globals.color_gridmajor_alt ) );
2692
2693
2694         GlobalPreferenceSystem().registerPreference( "XZVIS", makeBoolStringImportCallback( ToggleShownImportBoolCaller( g_xz_front_shown ) ), makeBoolStringExportCallback( ToggleShownExportBoolCaller( g_xz_front_shown ) ) );
2695         GlobalPreferenceSystem().registerPreference( "YZVIS", makeBoolStringImportCallback( ToggleShownImportBoolCaller( g_yz_side_shown ) ), makeBoolStringExportCallback( ToggleShownExportBoolCaller( g_yz_side_shown ) ) );
2696
2697         Orthographic_registerPreferencesPage();
2698         Clipper_registerPreferencesPage();
2699
2700         XYWnd::captureStates();
2701         GlobalEntityClassManager().attach( g_EntityClassMenu );
2702 }
2703
2704 void XYWindow_Destroy(){
2705         GlobalEntityClassManager().detach( g_EntityClassMenu );
2706         XYWnd::releaseStates();
2707 }