]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/mainframe.cpp
Radiant:
[xonotic/netradiant.git] / radiant / mainframe.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 // Main Window for Q3Radiant
24 //
25 // Leonardo Zide (leo@lokigames.com)
26 //
27
28 #include "mainframe.h"
29
30 #include "debugging/debugging.h"
31 #include "version.h"
32
33 #include "ifilesystem.h"
34 #include "iundo.h"
35 #include "ifilter.h"
36 #include "itoolbar.h"
37 #include "editable.h"
38 #include "ientity.h"
39 #include "ishaders.h"
40 #include "igl.h"
41 #include "moduleobserver.h"
42
43 #include <ctime>
44
45 #include <gdk/gdkkeysyms.h>
46 #include <gtk/gtkhbox.h>
47 #include <gtk/gtkvbox.h>
48 #include <gtk/gtkframe.h>
49 #include <gtk/gtklabel.h>
50 #include <gtk/gtkhpaned.h>
51 #include <gtk/gtkvpaned.h>
52 #include <gtk/gtktoolbar.h>
53 #include <gtk/gtkmenubar.h>
54 #include <gtk/gtkimage.h>
55 #include <gtk/gtktable.h>
56
57
58 #include "cmdlib.h"
59 #include "scenelib.h"
60 #include "stream/stringstream.h"
61 #include "signal/isignal.h"
62 #include "os/path.h"
63 #include "os/file.h"
64 #include "eclasslib.h"
65 #include "moduleobservers.h"
66
67 #include "gtkutil/clipboard.h"
68 #include "gtkutil/container.h"
69 #include "gtkutil/frame.h"
70 #include "gtkutil/glfont.h"
71 #include "gtkutil/glwidget.h"
72 #include "gtkutil/image.h"
73 #include "gtkutil/menu.h"
74 #include "gtkutil/paned.h"
75 #include "gtkutil/widget.h"
76
77 #include "autosave.h"
78 #include "build.h"
79 #include "brushmanip.h"
80 #include "brushmodule.h"
81 #include "camwindow.h"
82 #include "csg.h"
83 #include "commands.h"
84 #include "console.h"
85 #include "entity.h"
86 #include "entityinspector.h"
87 #include "entitylist.h"
88 #include "filters.h"
89 #include "findtexturedialog.h"
90 #include "grid.h"
91 #include "groupdialog.h"
92 #include "gtkdlgs.h"
93 #include "gtkmisc.h"
94 #include "help.h"
95 #include "map.h"
96 #include "mru.h"
97 #include "multimon.h"
98 #include "patchdialog.h"
99 #include "patchmanip.h"
100 #include "plugin.h"
101 #include "pluginmanager.h"
102 #include "pluginmenu.h"
103 #include "plugintoolbar.h"
104 #include "points.h"
105 #include "preferences.h"
106 #include "qe3.h"
107 #include "qgl.h"
108 #include "select.h"
109 #include "server.h"
110 #include "surfacedialog.h"
111 #include "textures.h"
112 #include "texwindow.h"
113 #include "url.h"
114 #include "xywindow.h"
115 #include "windowobservers.h"
116 #include "renderstate.h"
117 #include "feedback.h"
118 #include "referencecache.h"
119
120 #include "filterbar.h"
121 #include "gtktheme.h"
122
123
124 struct layout_globals_t
125 {
126         WindowPosition m_position;
127
128
129         int nXYHeight;
130         int nXYWidth;
131         int nCamWidth;
132         int nCamHeight;
133         int nState;
134
135         layout_globals_t() :
136                 m_position( -1, -1, 640, 480 ),
137
138                 nXYHeight( 350 ),
139                 nXYWidth( 600 ),
140                 nCamWidth( 300 ),
141                 nCamHeight( 210 ),
142                 nState( 0 ){
143         }
144 };
145
146 layout_globals_t g_layout_globals;
147 glwindow_globals_t g_glwindow_globals;
148
149
150 // VFS
151 class VFSModuleObserver : public ModuleObserver
152 {
153 std::size_t m_unrealised;
154 public:
155 VFSModuleObserver() : m_unrealised( 1 ){
156 }
157 void realise(){
158         if ( --m_unrealised == 0 ) {
159                 QE_InitVFS();
160                 GlobalFileSystem().initialise();
161         }
162 }
163 void unrealise(){
164         if ( ++m_unrealised == 1 ) {
165                 GlobalFileSystem().shutdown();
166         }
167 }
168 };
169
170 VFSModuleObserver g_VFSModuleObserver;
171
172 void VFS_Construct(){
173         Radiant_attachHomePathsObserver( g_VFSModuleObserver );
174 }
175 void VFS_Destroy(){
176         Radiant_detachHomePathsObserver( g_VFSModuleObserver );
177 }
178
179 // Home Paths
180
181 #ifdef WIN32
182 #include <shlobj.h>
183 #include <objbase.h>
184 const GUID qFOLDERID_SavedGames = {0x4C5C32FF, 0xBB9D, 0x43b0, {0xB5, 0xB4, 0x2D, 0x72, 0xE5, 0x4E, 0xAA, 0xA4}};
185 #define qREFKNOWNFOLDERID GUID
186 #define qKF_FLAG_CREATE 0x8000
187 #define qKF_FLAG_NO_ALIAS 0x1000
188 typedef HRESULT ( WINAPI qSHGetKnownFolderPath_t )( qREFKNOWNFOLDERID rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath );
189 static qSHGetKnownFolderPath_t *qSHGetKnownFolderPath;
190 #endif
191 void HomePaths_Realise(){
192         do
193         {
194                 const char* prefix = g_pGameDescription->getKeyValue( "prefix" );
195                 if ( !string_empty( prefix ) ) {
196                         StringOutputStream path( 256 );
197
198 #if defined( __APPLE__ )
199                         path.clear();
200                         path << DirectoryCleaned( g_get_home_dir() ) << "Library/Application Support" << ( prefix + 1 ) << "/";
201                         if ( file_is_directory( path.c_str() ) ) {
202                                 g_qeglobals.m_userEnginePath = path.c_str();
203                                 break;
204                         }
205 #endif
206
207 #if defined( WIN32 )
208                         TCHAR mydocsdir[MAX_PATH + 1];
209                         wchar_t *mydocsdirw;
210                         HMODULE shfolder = LoadLibrary( "shfolder.dll" );
211                         if ( shfolder ) {
212                                 qSHGetKnownFolderPath = (qSHGetKnownFolderPath_t *) GetProcAddress( shfolder, "SHGetKnownFolderPath" );
213                         }
214                         else{
215                                 qSHGetKnownFolderPath = NULL;
216                         }
217                         CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
218                         if ( qSHGetKnownFolderPath && qSHGetKnownFolderPath( qFOLDERID_SavedGames, qKF_FLAG_CREATE | qKF_FLAG_NO_ALIAS, NULL, &mydocsdirw ) == S_OK ) {
219                                 memset( mydocsdir, 0, sizeof( mydocsdir ) );
220                                 wcstombs( mydocsdir, mydocsdirw, sizeof( mydocsdir ) - 1 );
221                                 CoTaskMemFree( mydocsdirw );
222                                 path.clear();
223                                 path << DirectoryCleaned( mydocsdir ) << ( prefix + 1 ) << "/";
224                                 if ( file_is_directory( path.c_str() ) ) {
225                                         g_qeglobals.m_userEnginePath = path.c_str();
226                                         CoUninitialize();
227                                         FreeLibrary( shfolder );
228                                         break;
229                                 }
230                         }
231                         CoUninitialize();
232                         if ( shfolder ) {
233                                 FreeLibrary( shfolder );
234                         }
235                         if ( SUCCEEDED( SHGetFolderPath( NULL, CSIDL_PERSONAL, NULL, 0, mydocsdir ) ) ) {
236                                 path.clear();
237                                 path << DirectoryCleaned( mydocsdir ) << "My Games/" << ( prefix + 1 ) << "/";
238                                 // win32: only add it if it already exists
239                                 if ( file_is_directory( path.c_str() ) ) {
240                                         g_qeglobals.m_userEnginePath = path.c_str();
241                                         break;
242                                 }
243                         }
244 #endif
245
246 #if defined( POSIX )
247                         path.clear();
248                         path << DirectoryCleaned( g_get_home_dir() ) << prefix << "/";
249                         g_qeglobals.m_userEnginePath = path.c_str();
250                         break;
251 #endif
252                 }
253
254                 g_qeglobals.m_userEnginePath = EnginePath_get();
255         }
256         while ( 0 );
257
258         Q_mkdir( g_qeglobals.m_userEnginePath.c_str() );
259
260         {
261                 StringOutputStream path( 256 );
262                 path << g_qeglobals.m_userEnginePath.c_str() << gamename_get() << '/';
263                 g_qeglobals.m_userGamePath = path.c_str();
264         }
265         ASSERT_MESSAGE( !string_empty( g_qeglobals.m_userGamePath.c_str() ), "HomePaths_Realise: user-game-path is empty" );
266         Q_mkdir( g_qeglobals.m_userGamePath.c_str() );
267 }
268
269 ModuleObservers g_homePathObservers;
270
271 void Radiant_attachHomePathsObserver( ModuleObserver& observer ){
272         g_homePathObservers.attach( observer );
273 }
274
275 void Radiant_detachHomePathsObserver( ModuleObserver& observer ){
276         g_homePathObservers.detach( observer );
277 }
278
279 class HomePathsModuleObserver : public ModuleObserver
280 {
281 std::size_t m_unrealised;
282 public:
283 HomePathsModuleObserver() : m_unrealised( 1 ){
284 }
285 void realise(){
286         if ( --m_unrealised == 0 ) {
287                 HomePaths_Realise();
288                 g_homePathObservers.realise();
289         }
290 }
291 void unrealise(){
292         if ( ++m_unrealised == 1 ) {
293                 g_homePathObservers.unrealise();
294         }
295 }
296 };
297
298 HomePathsModuleObserver g_HomePathsModuleObserver;
299
300 void HomePaths_Construct(){
301         Radiant_attachEnginePathObserver( g_HomePathsModuleObserver );
302 }
303 void HomePaths_Destroy(){
304         Radiant_detachEnginePathObserver( g_HomePathsModuleObserver );
305 }
306
307
308 // Engine Path
309
310 CopiedString g_strEnginePath;
311 ModuleObservers g_enginePathObservers;
312 std::size_t g_enginepath_unrealised = 1;
313
314 void Radiant_attachEnginePathObserver( ModuleObserver& observer ){
315         g_enginePathObservers.attach( observer );
316 }
317
318 void Radiant_detachEnginePathObserver( ModuleObserver& observer ){
319         g_enginePathObservers.detach( observer );
320 }
321
322
323 void EnginePath_Realise(){
324         if ( --g_enginepath_unrealised == 0 ) {
325                 g_enginePathObservers.realise();
326         }
327 }
328
329
330 const char* EnginePath_get(){
331         ASSERT_MESSAGE( g_enginepath_unrealised == 0, "EnginePath_get: engine path not realised" );
332         return g_strEnginePath.c_str();
333 }
334
335 void EnginePath_Unrealise(){
336         if ( ++g_enginepath_unrealised == 1 ) {
337                 g_enginePathObservers.unrealise();
338         }
339 }
340
341 void setEnginePath( const char* path ){
342         StringOutputStream buffer( 256 );
343         buffer << DirectoryCleaned( path );
344         if ( !path_equal( buffer.c_str(), g_strEnginePath.c_str() ) ) {
345 #if 0
346                 while ( !ConfirmModified( "Paths Changed" ) )
347                 {
348                         if ( Map_Unnamed( g_map ) ) {
349                                 Map_SaveAs();
350                         }
351                         else
352                         {
353                                 Map_Save();
354                         }
355                 }
356                 Map_RegionOff();
357 #endif
358
359                 ScopeDisableScreenUpdates disableScreenUpdates( "Processing...", "Changing Engine Path" );
360
361                 EnginePath_Unrealise();
362
363                 g_strEnginePath = buffer.c_str();
364
365                 EnginePath_Realise();
366         }
367 }
368
369
370 // App Path
371
372 CopiedString g_strAppPath;                 ///< holds the full path of the executable
373
374 const char* AppPath_get(){
375         return g_strAppPath.c_str();
376 }
377
378 /// the path to the local rc-dir
379 const char* LocalRcPath_get( void ){
380         static CopiedString rc_path;
381         if ( rc_path.empty() ) {
382                 StringOutputStream stream( 256 );
383                 stream << GlobalRadiant().getSettingsPath() << g_pGameDescription->mGameFile.c_str() << "/";
384                 rc_path = stream.c_str();
385         }
386         return rc_path.c_str();
387 }
388
389 /// directory for temp files
390 /// NOTE: on *nix this is were we check for .pid
391 CopiedString g_strSettingsPath;
392 const char* SettingsPath_get(){
393         return g_strSettingsPath.c_str();
394 }
395
396
397 /*!
398    points to the game tools directory, for instance
399    C:/Program Files/Quake III Arena/GtkRadiant
400    (or other games)
401    this is one of the main variables that are configured by the game selection on startup
402    [GameToolsPath]/plugins
403    [GameToolsPath]/modules
404    and also q3map, bspc
405  */
406 CopiedString g_strGameToolsPath;           ///< this is set by g_GamesDialog
407
408 const char* GameToolsPath_get(){
409         return g_strGameToolsPath.c_str();
410 }
411
412 void EnginePathImport( CopiedString& self, const char* value ){
413         setEnginePath( value );
414 }
415 typedef ReferenceCaller1<CopiedString, const char*, EnginePathImport> EnginePathImportCaller;
416
417 void Paths_constructPreferences( PreferencesPage& page ){
418         page.appendPathEntry( "Engine Path", true,
419                                                   StringImportCallback( EnginePathImportCaller( g_strEnginePath ) ),
420                                                   StringExportCallback( StringExportCaller( g_strEnginePath ) )
421                                                   );
422 }
423 void Paths_constructPage( PreferenceGroup& group ){
424         PreferencesPage page( group.createPage( "Paths", "Path Settings" ) );
425         Paths_constructPreferences( page );
426 }
427 void Paths_registerPreferencesPage(){
428         PreferencesDialog_addSettingsPage( FreeCaller1<PreferenceGroup&, Paths_constructPage>() );
429 }
430
431
432 class PathsDialog : public Dialog
433 {
434 public:
435 GtkWindow* BuildDialog(){
436         GtkFrame* frame = create_dialog_frame( "Path settings", GTK_SHADOW_ETCHED_IN );
437
438         GtkVBox* vbox2 = create_dialog_vbox( 0, 4 );
439         gtk_container_add( GTK_CONTAINER( frame ), GTK_WIDGET( vbox2 ) );
440
441         {
442                 PreferencesPage preferencesPage( *this, GTK_WIDGET( vbox2 ) );
443                 Paths_constructPreferences( preferencesPage );
444         }
445
446         return create_simple_modal_dialog_window( "Engine Path Not Found", m_modal, GTK_WIDGET( frame ) );
447 }
448 };
449
450 PathsDialog g_PathsDialog;
451
452 bool g_strEnginePath_was_empty_1st_start = false;
453
454 void EnginePath_verify(){
455         if ( !file_exists( g_strEnginePath.c_str() ) || g_strEnginePath_was_empty_1st_start ) {
456                 g_PathsDialog.Create();
457                 g_PathsDialog.DoModal();
458                 g_PathsDialog.Destroy();
459         }
460 }
461
462 namespace
463 {
464 CopiedString g_gamename;
465 CopiedString g_gamemode;
466 ModuleObservers g_gameNameObservers;
467 ModuleObservers g_gameModeObservers;
468 }
469
470 void Radiant_attachGameNameObserver( ModuleObserver& observer ){
471         g_gameNameObservers.attach( observer );
472 }
473
474 void Radiant_detachGameNameObserver( ModuleObserver& observer ){
475         g_gameNameObservers.detach( observer );
476 }
477
478 const char* basegame_get(){
479         return g_pGameDescription->getRequiredKeyValue( "basegame" );
480 }
481
482 const char* gamename_get(){
483         const char* gamename = g_gamename.c_str();
484         if ( string_empty( gamename ) ) {
485                 return basegame_get();
486         }
487         return gamename;
488 }
489
490 void gamename_set( const char* gamename ){
491         if ( !string_equal( gamename, g_gamename.c_str() ) ) {
492                 g_gameNameObservers.unrealise();
493                 g_gamename = gamename;
494                 g_gameNameObservers.realise();
495         }
496 }
497
498 void Radiant_attachGameModeObserver( ModuleObserver& observer ){
499         g_gameModeObservers.attach( observer );
500 }
501
502 void Radiant_detachGameModeObserver( ModuleObserver& observer ){
503         g_gameModeObservers.detach( observer );
504 }
505
506 const char* gamemode_get(){
507         return g_gamemode.c_str();
508 }
509
510 void gamemode_set( const char* gamemode ){
511         if ( !string_equal( gamemode, g_gamemode.c_str() ) ) {
512                 g_gameModeObservers.unrealise();
513                 g_gamemode = gamemode;
514                 g_gameModeObservers.realise();
515         }
516 }
517
518 #include "os/dir.h"
519
520 class CLoadModule
521 {
522 const char* m_path;
523 public:
524 CLoadModule( const char* path ) : m_path( path ){
525 }
526 void operator()( const char* name ) const {
527         char fullname[1024];
528         ASSERT_MESSAGE( strlen( m_path ) + strlen( name ) < 1024, "" );
529         strcpy( fullname, m_path );
530         strcat( fullname, name );
531         globalOutputStream() << "Found '" << fullname << "'\n";
532         GlobalModuleServer_loadModule( fullname );
533 }
534 };
535
536 const char* const c_library_extension =
537 #if defined( WIN32 )
538         "dll"
539 #elif defined ( __APPLE__ )
540         "dylib"
541 #elif defined( __linux__ ) || defined ( __FreeBSD__ )
542         "so"
543 #endif
544 ;
545
546 void Radiant_loadModules( const char* path ){
547         Directory_forEach( path, MatchFileExtension<CLoadModule>( c_library_extension, CLoadModule( path ) ) );
548 }
549
550 void Radiant_loadModulesFromRoot( const char* directory ){
551         {
552                 StringOutputStream path( 256 );
553                 path << directory << g_pluginsDir;
554                 Radiant_loadModules( path.c_str() );
555         }
556
557         if ( !string_equal( g_pluginsDir, g_modulesDir ) ) {
558                 StringOutputStream path( 256 );
559                 path << directory << g_modulesDir;
560                 Radiant_loadModules( path.c_str() );
561         }
562 }
563
564 //! Make COLOR_BRUSHES override worldspawn eclass colour.
565 void SetWorldspawnColour( const Vector3& colour ){
566         EntityClass* worldspawn = GlobalEntityClassManager().findOrInsert( "worldspawn", true );
567         eclass_release_state( worldspawn );
568         worldspawn->color = colour;
569         eclass_capture_state( worldspawn );
570 }
571
572
573 class WorldspawnColourEntityClassObserver : public ModuleObserver
574 {
575 std::size_t m_unrealised;
576 public:
577 WorldspawnColourEntityClassObserver() : m_unrealised( 1 ){
578 }
579 void realise(){
580         if ( --m_unrealised == 0 ) {
581                 SetWorldspawnColour( g_xywindow_globals.color_brushes );
582         }
583 }
584 void unrealise(){
585         if ( ++m_unrealised == 1 ) {
586         }
587 }
588 };
589
590 WorldspawnColourEntityClassObserver g_WorldspawnColourEntityClassObserver;
591
592
593 ModuleObservers g_gameToolsPathObservers;
594
595 void Radiant_attachGameToolsPathObserver( ModuleObserver& observer ){
596         g_gameToolsPathObservers.attach( observer );
597 }
598
599 void Radiant_detachGameToolsPathObserver( ModuleObserver& observer ){
600         g_gameToolsPathObservers.detach( observer );
601 }
602
603 void Radiant_Initialise(){
604         GlobalModuleServer_Initialise();
605
606         Radiant_loadModulesFromRoot( AppPath_get() );
607
608         Preferences_Load();
609
610         bool success = Radiant_Construct( GlobalModuleServer_get() );
611         ASSERT_MESSAGE( success, "module system failed to initialise - see radiant.log for error messages" );
612
613         g_gameToolsPathObservers.realise();
614         g_gameModeObservers.realise();
615         g_gameNameObservers.realise();
616 }
617
618 void Radiant_Shutdown(){
619         g_gameNameObservers.unrealise();
620         g_gameModeObservers.unrealise();
621         g_gameToolsPathObservers.unrealise();
622
623         if ( !g_preferences_globals.disable_ini ) {
624                 globalOutputStream() << "Start writing prefs\n";
625                 Preferences_Save();
626                 globalOutputStream() << "Done prefs\n";
627         }
628
629         Radiant_Destroy();
630
631         GlobalModuleServer_Shutdown();
632 }
633
634 void Exit(){
635         if ( ConfirmModified( "Exit Radiant" ) ) {
636                 gtk_main_quit();
637         }
638 }
639
640
641 void Undo(){
642         GlobalUndoSystem().undo();
643         SceneChangeNotify();
644 }
645
646 void Redo(){
647         GlobalUndoSystem().redo();
648         SceneChangeNotify();
649 }
650
651 void deleteSelection(){
652         UndoableCommand undo( "deleteSelected" );
653         Select_Delete();
654 }
655
656 void Map_ExportSelected( TextOutputStream& ostream ){
657         Map_ExportSelected( ostream, Map_getFormat( g_map ) );
658 }
659
660 void Map_ImportSelected( TextInputStream& istream ){
661         Map_ImportSelected( istream, Map_getFormat( g_map ) );
662 }
663
664 void Selection_Copy(){
665         clipboard_copy( Map_ExportSelected );
666 }
667
668 void Selection_Paste(){
669         clipboard_paste( Map_ImportSelected );
670 }
671
672 void Copy(){
673         if ( SelectedFaces_empty() ) {
674                 Selection_Copy();
675         }
676         else
677         {
678                 SelectedFaces_copyTexture();
679         }
680 }
681
682 void Paste(){
683         if ( SelectedFaces_empty() ) {
684                 UndoableCommand undo( "paste" );
685
686                 GlobalSelectionSystem().setSelectedAll( false );
687                 Selection_Paste();
688         }
689         else
690         {
691                 SelectedFaces_pasteTexture();
692         }
693 }
694
695 void PasteToCamera(){
696         CamWnd& camwnd = *g_pParentWnd->GetCamWnd();
697         GlobalSelectionSystem().setSelectedAll( false );
698
699         UndoableCommand undo( "pasteToCamera" );
700
701         Selection_Paste();
702
703         // Work out the delta
704         Vector3 mid;
705         Select_GetMid( mid );
706         Vector3 delta = vector3_subtracted( vector3_snapped( Camera_getOrigin( camwnd ), GetSnapGridSize() ), mid );
707
708         // Move to camera
709         GlobalSelectionSystem().translateSelected( delta );
710 }
711
712
713 void ColorScheme_Original(){
714         TextureBrowser_setBackgroundColour( GlobalTextureBrowser(), Vector3( 0.25f, 0.25f, 0.25f ) );
715
716         g_camwindow_globals.color_selbrushes3d = Vector3( 1.0f, 0.0f, 0.0f );
717         g_camwindow_globals.color_cameraback = Vector3( 0.25f, 0.25f, 0.25f );
718         CamWnd_Update( *g_pParentWnd->GetCamWnd() );
719
720         g_xywindow_globals.color_gridback = Vector3( 1.0f, 1.0f, 1.0f );
721         g_xywindow_globals.color_gridminor = Vector3( 0.75f, 0.75f, 0.75f );
722         g_xywindow_globals.color_gridmajor = Vector3( 0.5f, 0.5f, 0.5f );
723         g_xywindow_globals.color_gridminor_alt = Vector3( 0.5f, 0.0f, 0.0f );
724         g_xywindow_globals.color_gridmajor_alt = Vector3( 1.0f, 0.0f, 0.0f );
725         g_xywindow_globals.color_gridblock = Vector3( 0.0f, 0.0f, 1.0f );
726         g_xywindow_globals.color_gridtext = Vector3( 0.0f, 0.0f, 0.0f );
727         g_xywindow_globals.color_selbrushes = Vector3( 1.0f, 0.0f, 0.0f );
728         g_xywindow_globals.color_clipper = Vector3( 0.0f, 0.0f, 1.0f );
729         g_xywindow_globals.color_brushes = Vector3( 0.0f, 0.0f, 0.0f );
730         SetWorldspawnColour( g_xywindow_globals.color_brushes );
731         g_xywindow_globals.color_viewname = Vector3( 0.5f, 0.0f, 0.75f );
732         XY_UpdateAllWindows();
733 }
734
735 void ColorScheme_QER(){
736         TextureBrowser_setBackgroundColour( GlobalTextureBrowser(), Vector3( 0.25f, 0.25f, 0.25f ) );
737
738         g_camwindow_globals.color_cameraback = Vector3( 0.25f, 0.25f, 0.25f );
739         g_camwindow_globals.color_selbrushes3d = Vector3( 1.0f, 0.0f, 0.0f );
740         CamWnd_Update( *g_pParentWnd->GetCamWnd() );
741
742         g_xywindow_globals.color_gridback = Vector3( 1.0f, 1.0f, 1.0f );
743         g_xywindow_globals.color_gridminor = Vector3( 1.0f, 1.0f, 1.0f );
744         g_xywindow_globals.color_gridmajor = Vector3( 0.5f, 0.5f, 0.5f );
745         g_xywindow_globals.color_gridblock = Vector3( 0.0f, 0.0f, 1.0f );
746         g_xywindow_globals.color_gridtext = Vector3( 0.0f, 0.0f, 0.0f );
747         g_xywindow_globals.color_selbrushes = Vector3( 1.0f, 0.0f, 0.0f );
748         g_xywindow_globals.color_clipper = Vector3( 0.0f, 0.0f, 1.0f );
749         g_xywindow_globals.color_brushes = Vector3( 0.0f, 0.0f, 0.0f );
750         SetWorldspawnColour( g_xywindow_globals.color_brushes );
751         g_xywindow_globals.color_viewname = Vector3( 0.5f, 0.0f, 0.75f );
752         XY_UpdateAllWindows();
753 }
754
755 void ColorScheme_Black(){
756         TextureBrowser_setBackgroundColour( GlobalTextureBrowser(), Vector3( 0.25f, 0.25f, 0.25f ) );
757
758         g_camwindow_globals.color_cameraback = Vector3( 0.25f, 0.25f, 0.25f );
759         g_camwindow_globals.color_selbrushes3d = Vector3( 1.0f, 0.0f, 0.0f );
760         CamWnd_Update( *g_pParentWnd->GetCamWnd() );
761
762         g_xywindow_globals.color_gridback = Vector3( 0.0f, 0.0f, 0.0f );
763         g_xywindow_globals.color_gridminor = Vector3( 0.2f, 0.2f, 0.2f );
764         g_xywindow_globals.color_gridmajor = Vector3( 0.3f, 0.5f, 0.5f );
765         g_xywindow_globals.color_gridblock = Vector3( 0.0f, 0.0f, 1.0f );
766         g_xywindow_globals.color_gridtext = Vector3( 1.0f, 1.0f, 1.0f );
767         g_xywindow_globals.color_selbrushes = Vector3( 1.0f, 0.0f, 0.0f );
768         g_xywindow_globals.color_clipper = Vector3( 0.0f, 0.0f, 1.0f );
769         g_xywindow_globals.color_brushes = Vector3( 1.0f, 1.0f, 1.0f );
770         SetWorldspawnColour( g_xywindow_globals.color_brushes );
771         g_xywindow_globals.color_viewname = Vector3( 0.7f, 0.7f, 0.0f );
772         XY_UpdateAllWindows();
773 }
774
775 /* ydnar: to emulate maya/max/lightwave color schemes */
776 void ColorScheme_Ydnar(){
777         TextureBrowser_setBackgroundColour( GlobalTextureBrowser(), Vector3( 0.25f, 0.25f, 0.25f ) );
778
779         g_camwindow_globals.color_cameraback = Vector3( 0.25f, 0.25f, 0.25f );
780         g_camwindow_globals.color_selbrushes3d = Vector3( 1.0f, 0.0f, 0.0f );
781         CamWnd_Update( *g_pParentWnd->GetCamWnd() );
782
783         g_xywindow_globals.color_gridback = Vector3( 0.77f, 0.77f, 0.77f );
784         g_xywindow_globals.color_gridminor = Vector3( 0.83f, 0.83f, 0.83f );
785         g_xywindow_globals.color_gridmajor = Vector3( 0.89f, 0.89f, 0.89f );
786         g_xywindow_globals.color_gridblock = Vector3( 1.0f, 1.0f, 1.0f );
787         g_xywindow_globals.color_gridtext = Vector3( 0.0f, 0.0f, 0.0f );
788         g_xywindow_globals.color_selbrushes = Vector3( 1.0f, 0.0f, 0.0f );
789         g_xywindow_globals.color_clipper = Vector3( 0.0f, 0.0f, 1.0f );
790         g_xywindow_globals.color_brushes = Vector3( 0.0f, 0.0f, 0.0f );
791         SetWorldspawnColour( g_xywindow_globals.color_brushes );
792         g_xywindow_globals.color_viewname = Vector3( 0.5f, 0.0f, 0.75f );
793         XY_UpdateAllWindows();
794 }
795
796 typedef Callback1<Vector3&> GetColourCallback;
797 typedef Callback1<const Vector3&> SetColourCallback;
798
799 class ChooseColour
800 {
801 GetColourCallback m_get;
802 SetColourCallback m_set;
803 public:
804 ChooseColour( const GetColourCallback& get, const SetColourCallback& set )
805         : m_get( get ), m_set( set ){
806 }
807 void operator()(){
808         Vector3 colour;
809         m_get( colour );
810         color_dialog( GTK_WIDGET( MainFrame_getWindow() ), colour );
811         m_set( colour );
812 }
813 };
814
815
816
817 void Colour_get( const Vector3& colour, Vector3& other ){
818         other = colour;
819 }
820 typedef ConstReferenceCaller1<Vector3, Vector3&, Colour_get> ColourGetCaller;
821
822 void Colour_set( Vector3& colour, const Vector3& other ){
823         colour = other;
824         SceneChangeNotify();
825 }
826 typedef ReferenceCaller1<Vector3, const Vector3&, Colour_set> ColourSetCaller;
827
828 void BrushColour_set( const Vector3& other ){
829         g_xywindow_globals.color_brushes = other;
830         SetWorldspawnColour( g_xywindow_globals.color_brushes );
831         SceneChangeNotify();
832 }
833 typedef FreeCaller1<const Vector3&, BrushColour_set> BrushColourSetCaller;
834
835 void ClipperColour_set( const Vector3& other ){
836         g_xywindow_globals.color_clipper = other;
837         Brush_clipperColourChanged();
838         SceneChangeNotify();
839 }
840 typedef FreeCaller1<const Vector3&, ClipperColour_set> ClipperColourSetCaller;
841
842 void TextureBrowserColour_get( Vector3& other ){
843         other = TextureBrowser_getBackgroundColour( GlobalTextureBrowser() );
844 }
845 typedef FreeCaller1<Vector3&, TextureBrowserColour_get> TextureBrowserColourGetCaller;
846
847 void TextureBrowserColour_set( const Vector3& other ){
848         TextureBrowser_setBackgroundColour( GlobalTextureBrowser(), other );
849 }
850 typedef FreeCaller1<const Vector3&, TextureBrowserColour_set> TextureBrowserColourSetCaller;
851
852
853 class ColoursMenu
854 {
855 public:
856 ChooseColour m_textureback;
857 ChooseColour m_xyback;
858 ChooseColour m_gridmajor;
859 ChooseColour m_gridminor;
860 ChooseColour m_gridmajor_alt;
861 ChooseColour m_gridminor_alt;
862 ChooseColour m_gridtext;
863 ChooseColour m_gridblock;
864 ChooseColour m_cameraback;
865 ChooseColour m_brush;
866 ChooseColour m_selectedbrush;
867 ChooseColour m_selectedbrush3d;
868 ChooseColour m_clipper;
869 ChooseColour m_viewname;
870
871 ColoursMenu() :
872         m_textureback( TextureBrowserColourGetCaller(), TextureBrowserColourSetCaller() ),
873         m_xyback( ColourGetCaller( g_xywindow_globals.color_gridback ), ColourSetCaller( g_xywindow_globals.color_gridback ) ),
874         m_gridmajor( ColourGetCaller( g_xywindow_globals.color_gridmajor ), ColourSetCaller( g_xywindow_globals.color_gridmajor ) ),
875         m_gridminor( ColourGetCaller( g_xywindow_globals.color_gridminor ), ColourSetCaller( g_xywindow_globals.color_gridminor ) ),
876         m_gridmajor_alt( ColourGetCaller( g_xywindow_globals.color_gridmajor_alt ), ColourSetCaller( g_xywindow_globals.color_gridmajor_alt ) ),
877         m_gridminor_alt( ColourGetCaller( g_xywindow_globals.color_gridminor_alt ), ColourSetCaller( g_xywindow_globals.color_gridminor_alt ) ),
878         m_gridtext( ColourGetCaller( g_xywindow_globals.color_gridtext ), ColourSetCaller( g_xywindow_globals.color_gridtext ) ),
879         m_gridblock( ColourGetCaller( g_xywindow_globals.color_gridblock ), ColourSetCaller( g_xywindow_globals.color_gridblock ) ),
880         m_cameraback( ColourGetCaller( g_camwindow_globals.color_cameraback ), ColourSetCaller( g_camwindow_globals.color_cameraback ) ),
881         m_brush( ColourGetCaller( g_xywindow_globals.color_brushes ), BrushColourSetCaller() ),
882         m_selectedbrush( ColourGetCaller( g_xywindow_globals.color_selbrushes ), ColourSetCaller( g_xywindow_globals.color_selbrushes ) ),
883         m_selectedbrush3d( ColourGetCaller( g_camwindow_globals.color_selbrushes3d ), ColourSetCaller( g_camwindow_globals.color_selbrushes3d ) ),
884         m_clipper( ColourGetCaller( g_xywindow_globals.color_clipper ), ClipperColourSetCaller() ),
885         m_viewname( ColourGetCaller( g_xywindow_globals.color_viewname ), ColourSetCaller( g_xywindow_globals.color_viewname ) ){
886 }
887 };
888
889 ColoursMenu g_ColoursMenu;
890
891 GtkMenuItem* create_colours_menu(){
892         GtkMenuItem* colours_menu_item = new_sub_menu_item_with_mnemonic( "Colors" );
893         GtkMenu* menu_in_menu = GTK_MENU( gtk_menu_item_get_submenu( colours_menu_item ) );
894         if ( g_Layout_enableDetachableMenus.m_value ) {
895                 menu_tearoff( menu_in_menu );
896         }
897
898         GtkMenu* menu_3 = create_sub_menu_with_mnemonic( menu_in_menu, "Themes" );
899         if ( g_Layout_enableDetachableMenus.m_value ) {
900                 menu_tearoff( menu_3 );
901         }
902
903         create_menu_item_with_mnemonic( menu_3, "QE4 Original", "ColorSchemeOriginal" );
904         create_menu_item_with_mnemonic( menu_3, "Q3Radiant Original", "ColorSchemeQER" );
905         create_menu_item_with_mnemonic( menu_3, "Black and Green", "ColorSchemeBlackAndGreen" );
906         create_menu_item_with_mnemonic( menu_3, "Maya/Max/Lightwave Emulation", "ColorSchemeYdnar" );
907
908         create_menu_item_with_mnemonic( menu_in_menu, "GTK Theme...", "gtkThemeDlg" );
909
910         menu_separator( menu_in_menu );
911
912         create_menu_item_with_mnemonic( menu_in_menu, "_Texture Background...", "ChooseTextureBackgroundColor" );
913         create_menu_item_with_mnemonic( menu_in_menu, "Grid Background...", "ChooseGridBackgroundColor" );
914         create_menu_item_with_mnemonic( menu_in_menu, "Grid Major...", "ChooseGridMajorColor" );
915         create_menu_item_with_mnemonic( menu_in_menu, "Grid Minor...", "ChooseGridMinorColor" );
916         create_menu_item_with_mnemonic( menu_in_menu, "Grid Major Small...", "ChooseSmallGridMajorColor" );
917         create_menu_item_with_mnemonic( menu_in_menu, "Grid Minor Small...", "ChooseSmallGridMinorColor" );
918         create_menu_item_with_mnemonic( menu_in_menu, "Grid Text...", "ChooseGridTextColor" );
919         create_menu_item_with_mnemonic( menu_in_menu, "Grid Block...", "ChooseGridBlockColor" );
920         create_menu_item_with_mnemonic( menu_in_menu, "Default Brush...", "ChooseBrushColor" );
921         create_menu_item_with_mnemonic( menu_in_menu, "Camera Background...", "ChooseCameraBackgroundColor" );
922         create_menu_item_with_mnemonic( menu_in_menu, "Selected Brush...", "ChooseSelectedBrushColor" );
923         create_menu_item_with_mnemonic( menu_in_menu, "Selected Brush (Camera)...", "ChooseCameraSelectedBrushColor" );
924         create_menu_item_with_mnemonic( menu_in_menu, "Clipper...", "ChooseClipperColor" );
925         create_menu_item_with_mnemonic( menu_in_menu, "Active View name...", "ChooseOrthoViewNameColor" );
926
927         return colours_menu_item;
928 }
929
930
931 void Restart(){
932         PluginsMenu_clear();
933         PluginToolbar_clear();
934
935         Radiant_Shutdown();
936         Radiant_Initialise();
937
938         PluginsMenu_populate();
939
940         PluginToolbar_populate();
941 }
942
943
944 void thunk_OnSleep(){
945         g_pParentWnd->OnSleep();
946 }
947
948 void OpenUpdateURL(){
949         // build the URL
950         StringOutputStream URL( 256 );
951         URL << "http://www.icculus.org/netradiant/?cmd=update&data=dlupdate&query_dlup=1";
952 #ifdef WIN32
953         URL << "&OS_dlup=1";
954 #elif defined( __APPLE__ )
955         URL << "&OS_dlup=2";
956 #else
957         URL << "&OS_dlup=3";
958 #endif
959         URL << "&Version_dlup=" RADIANT_VERSION;
960         g_GamesDialog.AddPacksURL( URL );
961         OpenURL( URL.c_str() );
962 }
963
964 // open the Q3Rad manual
965 void OpenHelpURL(){
966         // at least on win32, AppPath + "docs/index.html"
967         StringOutputStream help( 256 );
968         help << AppPath_get() << "docs/index.html";
969         OpenURL( help.c_str() );
970 }
971
972 void OpenBugReportURL(){
973         OpenURL( "http://www.icculus.org/netradiant/?cmd=bugs" );
974 }
975
976
977 GtkWidget* g_page_console;
978
979 void Console_ToggleShow(){
980         GroupDialog_showPage( g_page_console );
981 }
982
983 GtkWidget* g_page_entity;
984
985 void EntityInspector_ToggleShow(){
986         GroupDialog_showPage( g_page_entity );
987 }
988
989
990
991 void SetClipMode( bool enable );
992 void ModeChangeNotify();
993
994 typedef void ( *ToolMode )();
995 ToolMode g_currentToolMode = 0;
996 bool g_currentToolModeSupportsComponentEditing = false;
997 ToolMode g_defaultToolMode = 0;
998
999
1000
1001 void SelectionSystem_DefaultMode(){
1002         GlobalSelectionSystem().SetMode( SelectionSystem::ePrimitive );
1003         GlobalSelectionSystem().SetComponentMode( SelectionSystem::eDefault );
1004         ModeChangeNotify();
1005 }
1006
1007
1008 bool EdgeMode(){
1009         return GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
1010                    && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eEdge;
1011 }
1012
1013 bool VertexMode(){
1014         return GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
1015                    && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex;
1016 }
1017
1018 bool FaceMode(){
1019         return GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
1020                    && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace;
1021 }
1022
1023 template<bool( *BoolFunction ) ( )>
1024 class BoolFunctionExport
1025 {
1026 public:
1027 static void apply( const BoolImportCallback& importCallback ){
1028         importCallback( BoolFunction() );
1029 }
1030 };
1031
1032 typedef FreeCaller1<const BoolImportCallback&, &BoolFunctionExport<EdgeMode>::apply> EdgeModeApplyCaller;
1033 EdgeModeApplyCaller g_edgeMode_button_caller;
1034 BoolExportCallback g_edgeMode_button_callback( g_edgeMode_button_caller );
1035 ToggleItem g_edgeMode_button( g_edgeMode_button_callback );
1036
1037 typedef FreeCaller1<const BoolImportCallback&, &BoolFunctionExport<VertexMode>::apply> VertexModeApplyCaller;
1038 VertexModeApplyCaller g_vertexMode_button_caller;
1039 BoolExportCallback g_vertexMode_button_callback( g_vertexMode_button_caller );
1040 ToggleItem g_vertexMode_button( g_vertexMode_button_callback );
1041
1042 typedef FreeCaller1<const BoolImportCallback&, &BoolFunctionExport<FaceMode>::apply> FaceModeApplyCaller;
1043 FaceModeApplyCaller g_faceMode_button_caller;
1044 BoolExportCallback g_faceMode_button_callback( g_faceMode_button_caller );
1045 ToggleItem g_faceMode_button( g_faceMode_button_callback );
1046
1047 void ComponentModeChanged(){
1048         g_edgeMode_button.update();
1049         g_vertexMode_button.update();
1050         g_faceMode_button.update();
1051 }
1052
1053 void ComponentMode_SelectionChanged( const Selectable& selectable ){
1054         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
1055                  && GlobalSelectionSystem().countSelected() == 0 ) {
1056                 SelectionSystem_DefaultMode();
1057                 ComponentModeChanged();
1058         }
1059 }
1060
1061 void SelectEdgeMode(){
1062 #if 0
1063         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1064                 GlobalSelectionSystem().Select( false );
1065         }
1066 #endif
1067
1068         if ( EdgeMode() ) {
1069                 SelectionSystem_DefaultMode();
1070         }
1071         else if ( GlobalSelectionSystem().countSelected() != 0 ) {
1072                 if ( !g_currentToolModeSupportsComponentEditing ) {
1073                         g_defaultToolMode();
1074                 }
1075
1076                 GlobalSelectionSystem().SetMode( SelectionSystem::eComponent );
1077                 GlobalSelectionSystem().SetComponentMode( SelectionSystem::eEdge );
1078         }
1079
1080         ComponentModeChanged();
1081
1082         ModeChangeNotify();
1083 }
1084
1085 void SelectVertexMode(){
1086 #if 0
1087         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1088                 GlobalSelectionSystem().Select( false );
1089         }
1090 #endif
1091
1092         if ( VertexMode() ) {
1093                 SelectionSystem_DefaultMode();
1094         }
1095         else if ( GlobalSelectionSystem().countSelected() != 0 ) {
1096                 if ( !g_currentToolModeSupportsComponentEditing ) {
1097                         g_defaultToolMode();
1098                 }
1099
1100                 GlobalSelectionSystem().SetMode( SelectionSystem::eComponent );
1101                 GlobalSelectionSystem().SetComponentMode( SelectionSystem::eVertex );
1102         }
1103
1104         ComponentModeChanged();
1105
1106         ModeChangeNotify();
1107 }
1108
1109 void SelectFaceMode(){
1110 #if 0
1111         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1112                 GlobalSelectionSystem().Select( false );
1113         }
1114 #endif
1115
1116         if ( FaceMode() ) {
1117                 SelectionSystem_DefaultMode();
1118         }
1119         else if ( GlobalSelectionSystem().countSelected() != 0 ) {
1120                 if ( !g_currentToolModeSupportsComponentEditing ) {
1121                         g_defaultToolMode();
1122                 }
1123
1124                 GlobalSelectionSystem().SetMode( SelectionSystem::eComponent );
1125                 GlobalSelectionSystem().SetComponentMode( SelectionSystem::eFace );
1126         }
1127
1128         ComponentModeChanged();
1129
1130         ModeChangeNotify();
1131 }
1132
1133
1134 class CloneSelected : public scene::Graph::Walker
1135 {
1136 bool doMakeUnique;
1137 NodeSmartReference worldspawn;
1138 public:
1139 CloneSelected( bool d ) : doMakeUnique( d ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
1140 }
1141 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1142         if ( path.size() == 1 ) {
1143                 return true;
1144         }
1145
1146         // ignore worldspawn, but keep checking children
1147         NodeSmartReference me( path.top().get() );
1148         if ( me == worldspawn ) {
1149                 return true;
1150         }
1151
1152         if ( !path.top().get().isRoot() ) {
1153                 Selectable* selectable = Instance_getSelectable( instance );
1154                 if ( selectable != 0
1155                          && selectable->isSelected() ) {
1156                         return false;
1157                 }
1158         }
1159
1160         return true;
1161 }
1162 void post( const scene::Path& path, scene::Instance& instance ) const {
1163         if ( path.size() == 1 ) {
1164                 return;
1165         }
1166
1167         // ignore worldspawn, but keep checking children
1168         NodeSmartReference me( path.top().get() );
1169         if ( me == worldspawn ) {
1170                 return;
1171         }
1172
1173         if ( !path.top().get().isRoot() ) {
1174                 Selectable* selectable = Instance_getSelectable( instance );
1175                 if ( selectable != 0
1176                          && selectable->isSelected() ) {
1177                         NodeSmartReference clone( Node_Clone( path.top() ) );
1178                         if ( doMakeUnique ) {
1179                                 Map_gatherNamespaced( clone );
1180                         }
1181                         Node_getTraversable( path.parent().get() )->insert( clone );
1182                 }
1183         }
1184 }
1185 };
1186
1187 void Scene_Clone_Selected( scene::Graph& graph, bool doMakeUnique ){
1188         graph.traverse( CloneSelected( doMakeUnique ) );
1189
1190         Map_mergeClonedNames();
1191 }
1192
1193 enum ENudgeDirection
1194 {
1195         eNudgeUp = 1,
1196         eNudgeDown = 3,
1197         eNudgeLeft = 0,
1198         eNudgeRight = 2,
1199 };
1200
1201 struct AxisBase
1202 {
1203         Vector3 x;
1204         Vector3 y;
1205         Vector3 z;
1206         AxisBase( const Vector3& x_, const Vector3& y_, const Vector3& z_ )
1207                 : x( x_ ), y( y_ ), z( z_ ){
1208         }
1209 };
1210
1211 AxisBase AxisBase_forViewType( VIEWTYPE viewtype ){
1212         switch ( viewtype )
1213         {
1214         case XY:
1215                 return AxisBase( g_vector3_axis_x, g_vector3_axis_y, g_vector3_axis_z );
1216         case XZ:
1217                 return AxisBase( g_vector3_axis_x, g_vector3_axis_z, g_vector3_axis_y );
1218         case YZ:
1219                 return AxisBase( g_vector3_axis_y, g_vector3_axis_z, g_vector3_axis_x );
1220         }
1221
1222         ERROR_MESSAGE( "invalid viewtype" );
1223         return AxisBase( Vector3( 0, 0, 0 ), Vector3( 0, 0, 0 ), Vector3( 0, 0, 0 ) );
1224 }
1225
1226 Vector3 AxisBase_axisForDirection( const AxisBase& axes, ENudgeDirection direction ){
1227         switch ( direction )
1228         {
1229         case eNudgeLeft:
1230                 return vector3_negated( axes.x );
1231         case eNudgeUp:
1232                 return axes.y;
1233         case eNudgeRight:
1234                 return axes.x;
1235         case eNudgeDown:
1236                 return vector3_negated( axes.y );
1237         }
1238
1239         ERROR_MESSAGE( "invalid direction" );
1240         return Vector3( 0, 0, 0 );
1241 }
1242
1243 void NudgeSelection( ENudgeDirection direction, float fAmount, VIEWTYPE viewtype ){
1244         AxisBase axes( AxisBase_forViewType( viewtype ) );
1245         Vector3 view_direction( vector3_negated( axes.z ) );
1246         Vector3 nudge( vector3_scaled( AxisBase_axisForDirection( axes, direction ), fAmount ) );
1247         GlobalSelectionSystem().NudgeManipulator( nudge, view_direction );
1248 }
1249
1250 void Selection_Clone(){
1251         if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
1252                 UndoableCommand undo( "cloneSelected" );
1253
1254                 Scene_Clone_Selected( GlobalSceneGraph(), false );
1255
1256                 //NudgeSelection(eNudgeRight, GetGridSize(), GlobalXYWnd_getCurrentViewType());
1257                 //NudgeSelection(eNudgeDown, GetGridSize(), GlobalXYWnd_getCurrentViewType());
1258         }
1259 }
1260
1261 void Selection_Clone_MakeUnique(){
1262         if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
1263                 UndoableCommand undo( "cloneSelectedMakeUnique" );
1264
1265                 Scene_Clone_Selected( GlobalSceneGraph(), true );
1266
1267                 //NudgeSelection(eNudgeRight, GetGridSize(), GlobalXYWnd_getCurrentViewType());
1268                 //NudgeSelection(eNudgeDown, GetGridSize(), GlobalXYWnd_getCurrentViewType());
1269         }
1270 }
1271
1272 // called when the escape key is used (either on the main window or on an inspector)
1273 void Selection_Deselect(){
1274         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1275                 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
1276                         GlobalSelectionSystem().setSelectedAllComponents( false );
1277                 }
1278                 else
1279                 {
1280                         SelectionSystem_DefaultMode();
1281                         ComponentModeChanged();
1282                 }
1283         }
1284         else
1285         {
1286                 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
1287                         GlobalSelectionSystem().setSelectedAllComponents( false );
1288                 }
1289                 else
1290                 {
1291                         GlobalSelectionSystem().setSelectedAll( false );
1292                 }
1293         }
1294 }
1295
1296
1297 void Selection_NudgeUp(){
1298         UndoableCommand undo( "nudgeSelectedUp" );
1299         NudgeSelection( eNudgeUp, GetGridSize(), GlobalXYWnd_getCurrentViewType() );
1300 }
1301
1302 void Selection_NudgeDown(){
1303         UndoableCommand undo( "nudgeSelectedDown" );
1304         NudgeSelection( eNudgeDown, GetGridSize(), GlobalXYWnd_getCurrentViewType() );
1305 }
1306
1307 void Selection_NudgeLeft(){
1308         UndoableCommand undo( "nudgeSelectedLeft" );
1309         NudgeSelection( eNudgeLeft, GetGridSize(), GlobalXYWnd_getCurrentViewType() );
1310 }
1311
1312 void Selection_NudgeRight(){
1313         UndoableCommand undo( "nudgeSelectedRight" );
1314         NudgeSelection( eNudgeRight, GetGridSize(), GlobalXYWnd_getCurrentViewType() );
1315 }
1316
1317
1318 void TranslateToolExport( const BoolImportCallback& importCallback ){
1319         importCallback( GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eTranslate );
1320 }
1321
1322 void RotateToolExport( const BoolImportCallback& importCallback ){
1323         importCallback( GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eRotate );
1324 }
1325
1326 void ScaleToolExport( const BoolImportCallback& importCallback ){
1327         importCallback( GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eScale );
1328 }
1329
1330 void DragToolExport( const BoolImportCallback& importCallback ){
1331         importCallback( GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eDrag );
1332 }
1333
1334 void ClipperToolExport( const BoolImportCallback& importCallback ){
1335         importCallback( GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip );
1336 }
1337
1338 FreeCaller1<const BoolImportCallback&, TranslateToolExport> g_translatemode_button_caller;
1339 BoolExportCallback g_translatemode_button_callback( g_translatemode_button_caller );
1340 ToggleItem g_translatemode_button( g_translatemode_button_callback );
1341
1342 FreeCaller1<const BoolImportCallback&, RotateToolExport> g_rotatemode_button_caller;
1343 BoolExportCallback g_rotatemode_button_callback( g_rotatemode_button_caller );
1344 ToggleItem g_rotatemode_button( g_rotatemode_button_callback );
1345
1346 FreeCaller1<const BoolImportCallback&, ScaleToolExport> g_scalemode_button_caller;
1347 BoolExportCallback g_scalemode_button_callback( g_scalemode_button_caller );
1348 ToggleItem g_scalemode_button( g_scalemode_button_callback );
1349
1350 FreeCaller1<const BoolImportCallback&, DragToolExport> g_dragmode_button_caller;
1351 BoolExportCallback g_dragmode_button_callback( g_dragmode_button_caller );
1352 ToggleItem g_dragmode_button( g_dragmode_button_callback );
1353
1354 FreeCaller1<const BoolImportCallback&, ClipperToolExport> g_clipper_button_caller;
1355 BoolExportCallback g_clipper_button_callback( g_clipper_button_caller );
1356 ToggleItem g_clipper_button( g_clipper_button_callback );
1357
1358 void ToolChanged(){
1359         g_translatemode_button.update();
1360         g_rotatemode_button.update();
1361         g_scalemode_button.update();
1362         g_dragmode_button.update();
1363         g_clipper_button.update();
1364 }
1365
1366 const char* const c_ResizeMode_status = "QE4 Drag Tool: move and resize objects";
1367
1368 void DragMode(){
1369         if ( g_currentToolMode == DragMode && g_defaultToolMode != DragMode ) {
1370                 g_defaultToolMode();
1371         }
1372         else
1373         {
1374                 g_currentToolMode = DragMode;
1375                 g_currentToolModeSupportsComponentEditing = true;
1376
1377                 OnClipMode( false );
1378
1379                 Sys_Status( c_ResizeMode_status );
1380                 GlobalSelectionSystem().SetManipulatorMode( SelectionSystem::eDrag );
1381                 ToolChanged();
1382                 ModeChangeNotify();
1383         }
1384 }
1385
1386
1387 const char* const c_TranslateMode_status = "Translate Tool: translate objects and components";
1388
1389 void TranslateMode(){
1390         if ( g_currentToolMode == TranslateMode && g_defaultToolMode != TranslateMode ) {
1391                 g_defaultToolMode();
1392         }
1393         else
1394         {
1395                 g_currentToolMode = TranslateMode;
1396                 g_currentToolModeSupportsComponentEditing = true;
1397
1398                 OnClipMode( false );
1399
1400                 Sys_Status( c_TranslateMode_status );
1401                 GlobalSelectionSystem().SetManipulatorMode( SelectionSystem::eTranslate );
1402                 ToolChanged();
1403                 ModeChangeNotify();
1404         }
1405 }
1406
1407 const char* const c_RotateMode_status = "Rotate Tool: rotate objects and components";
1408
1409 void RotateMode(){
1410         if ( g_currentToolMode == RotateMode && g_defaultToolMode != RotateMode ) {
1411                 g_defaultToolMode();
1412         }
1413         else
1414         {
1415                 g_currentToolMode = RotateMode;
1416                 g_currentToolModeSupportsComponentEditing = true;
1417
1418                 OnClipMode( false );
1419
1420                 Sys_Status( c_RotateMode_status );
1421                 GlobalSelectionSystem().SetManipulatorMode( SelectionSystem::eRotate );
1422                 ToolChanged();
1423                 ModeChangeNotify();
1424         }
1425 }
1426
1427 const char* const c_ScaleMode_status = "Scale Tool: scale objects and components";
1428
1429 void ScaleMode(){
1430         if ( g_currentToolMode == ScaleMode && g_defaultToolMode != ScaleMode ) {
1431                 g_defaultToolMode();
1432         }
1433         else
1434         {
1435                 g_currentToolMode = ScaleMode;
1436                 g_currentToolModeSupportsComponentEditing = true;
1437
1438                 OnClipMode( false );
1439
1440                 Sys_Status( c_ScaleMode_status );
1441                 GlobalSelectionSystem().SetManipulatorMode( SelectionSystem::eScale );
1442                 ToolChanged();
1443                 ModeChangeNotify();
1444         }
1445 }
1446
1447
1448 const char* const c_ClipperMode_status = "Clipper Tool: apply clip planes to objects";
1449
1450
1451 void ClipperMode(){
1452         if ( g_currentToolMode == ClipperMode && g_defaultToolMode != ClipperMode ) {
1453                 g_defaultToolMode();
1454         }
1455         else
1456         {
1457                 g_currentToolMode = ClipperMode;
1458                 g_currentToolModeSupportsComponentEditing = false;
1459
1460                 SelectionSystem_DefaultMode();
1461
1462                 OnClipMode( true );
1463
1464                 Sys_Status( c_ClipperMode_status );
1465                 GlobalSelectionSystem().SetManipulatorMode( SelectionSystem::eClip );
1466                 ToolChanged();
1467                 ModeChangeNotify();
1468         }
1469 }
1470
1471
1472 void ToggleRotateScaleModes(){
1473         if ( g_currentToolMode == RotateMode ) {
1474                 ScaleMode();
1475         }
1476         else
1477         {
1478                 RotateMode();
1479         }
1480 }
1481
1482 void ToggleDragScaleModes(){
1483         if ( g_currentToolMode == DragMode ) {
1484                 ScaleMode();
1485         }
1486         else
1487         {
1488                 DragMode();
1489         }
1490 }
1491
1492
1493 void Texdef_Rotate( float angle ){
1494         StringOutputStream command;
1495         command << "brushRotateTexture -angle " << angle;
1496         UndoableCommand undo( command.c_str() );
1497         Select_RotateTexture( angle );
1498 }
1499
1500 void Texdef_RotateClockwise(){
1501         Texdef_Rotate( static_cast<float>( fabs( g_si_globals.rotate ) ) );
1502 }
1503
1504 void Texdef_RotateAntiClockwise(){
1505         Texdef_Rotate( static_cast<float>( -fabs( g_si_globals.rotate ) ) );
1506 }
1507
1508 void Texdef_Scale( float x, float y ){
1509         StringOutputStream command;
1510         command << "brushScaleTexture -x " << x << " -y " << y;
1511         UndoableCommand undo( command.c_str() );
1512         Select_ScaleTexture( x, y );
1513 }
1514
1515 void Texdef_ScaleUp(){
1516         Texdef_Scale( 0, g_si_globals.scale[1] );
1517 }
1518
1519 void Texdef_ScaleDown(){
1520         Texdef_Scale( 0, -g_si_globals.scale[1] );
1521 }
1522
1523 void Texdef_ScaleLeft(){
1524         Texdef_Scale( -g_si_globals.scale[0],0 );
1525 }
1526
1527 void Texdef_ScaleRight(){
1528         Texdef_Scale( g_si_globals.scale[0],0 );
1529 }
1530
1531 void Texdef_Shift( float x, float y ){
1532         StringOutputStream command;
1533         command << "brushShiftTexture -x " << x << " -y " << y;
1534         UndoableCommand undo( command.c_str() );
1535         Select_ShiftTexture( x, y );
1536 }
1537
1538 void Texdef_ShiftLeft(){
1539         Texdef_Shift( -g_si_globals.shift[0], 0 );
1540 }
1541
1542 void Texdef_ShiftRight(){
1543         Texdef_Shift( g_si_globals.shift[0], 0 );
1544 }
1545
1546 void Texdef_ShiftUp(){
1547         Texdef_Shift( 0, g_si_globals.shift[1] );
1548 }
1549
1550 void Texdef_ShiftDown(){
1551         Texdef_Shift( 0, -g_si_globals.shift[1] );
1552 }
1553
1554
1555
1556 class SnappableSnapToGridSelected : public scene::Graph::Walker
1557 {
1558 float m_snap;
1559 public:
1560 SnappableSnapToGridSelected( float snap )
1561         : m_snap( snap ){
1562 }
1563 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1564         if ( path.top().get().visible() ) {
1565                 Snappable* snappable = Node_getSnappable( path.top() );
1566                 if ( snappable != 0
1567                          && Instance_getSelectable( instance )->isSelected() ) {
1568                         snappable->snapto( m_snap );
1569                 }
1570         }
1571         return true;
1572 }
1573 };
1574
1575 void Scene_SnapToGrid_Selected( scene::Graph& graph, float snap ){
1576         graph.traverse( SnappableSnapToGridSelected( snap ) );
1577 }
1578
1579 class ComponentSnappableSnapToGridSelected : public scene::Graph::Walker
1580 {
1581 float m_snap;
1582 public:
1583 ComponentSnappableSnapToGridSelected( float snap )
1584         : m_snap( snap ){
1585 }
1586 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1587         if ( path.top().get().visible() ) {
1588                 ComponentSnappable* componentSnappable = Instance_getComponentSnappable( instance );
1589                 if ( componentSnappable != 0
1590                          && Instance_getSelectable( instance )->isSelected() ) {
1591                         componentSnappable->snapComponents( m_snap );
1592                 }
1593         }
1594         return true;
1595 }
1596 };
1597
1598 void Scene_SnapToGrid_Component_Selected( scene::Graph& graph, float snap ){
1599         graph.traverse( ComponentSnappableSnapToGridSelected( snap ) );
1600 }
1601
1602 void Selection_SnapToGrid(){
1603         StringOutputStream command;
1604         command << "snapSelected -grid " << GetGridSize();
1605         UndoableCommand undo( command.c_str() );
1606
1607         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1608                 Scene_SnapToGrid_Component_Selected( GlobalSceneGraph(), GetGridSize() );
1609         }
1610         else
1611         {
1612                 Scene_SnapToGrid_Selected( GlobalSceneGraph(), GetGridSize() );
1613         }
1614 }
1615
1616
1617 static gint qe_every_second( gpointer data ){
1618         GdkModifierType mask;
1619
1620         gdk_window_get_pointer( 0, 0, 0, &mask );
1621
1622         if ( ( mask & ( GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK ) ) == 0 ) {
1623                 QE_CheckAutoSave();
1624         }
1625
1626         return TRUE;
1627 }
1628
1629 guint s_qe_every_second_id = 0;
1630
1631 void EverySecondTimer_enable(){
1632         if ( s_qe_every_second_id == 0 ) {
1633                 s_qe_every_second_id = gtk_timeout_add( 1000, qe_every_second, 0 );
1634         }
1635 }
1636
1637 void EverySecondTimer_disable(){
1638         if ( s_qe_every_second_id != 0 ) {
1639                 gtk_timeout_remove( s_qe_every_second_id );
1640                 s_qe_every_second_id = 0;
1641         }
1642 }
1643
1644 gint window_realize_remove_decoration( GtkWidget* widget, gpointer data ){
1645         gdk_window_set_decorations( widget->window, (GdkWMDecoration)( GDK_DECOR_ALL | GDK_DECOR_MENU | GDK_DECOR_MINIMIZE | GDK_DECOR_MAXIMIZE ) );
1646         return FALSE;
1647 }
1648
1649 class WaitDialog
1650 {
1651 public:
1652 GtkWindow* m_window;
1653 GtkLabel* m_label;
1654 };
1655
1656 WaitDialog create_wait_dialog( const char* title, const char* text ){
1657         WaitDialog dialog;
1658
1659         dialog.m_window = create_floating_window( title, MainFrame_getWindow() );
1660         gtk_window_set_resizable( dialog.m_window, FALSE );
1661         gtk_container_set_border_width( GTK_CONTAINER( dialog.m_window ), 0 );
1662         gtk_window_set_position( dialog.m_window, GTK_WIN_POS_CENTER_ON_PARENT );
1663
1664         g_signal_connect( G_OBJECT( dialog.m_window ), "realize", G_CALLBACK( window_realize_remove_decoration ), 0 );
1665
1666         {
1667                 dialog.m_label = GTK_LABEL( gtk_label_new( text ) );
1668                 gtk_misc_set_alignment( GTK_MISC( dialog.m_label ), 0.0, 0.5 );
1669                 gtk_label_set_justify( dialog.m_label, GTK_JUSTIFY_LEFT );
1670                 gtk_widget_show( GTK_WIDGET( dialog.m_label ) );
1671                 gtk_widget_set_size_request( GTK_WIDGET( dialog.m_label ), 200, -1 );
1672
1673                 gtk_container_add( GTK_CONTAINER( dialog.m_window ), GTK_WIDGET( dialog.m_label ) );
1674         }
1675         return dialog;
1676 }
1677
1678 namespace
1679 {
1680 clock_t g_lastRedrawTime = 0;
1681 const clock_t c_redrawInterval = clock_t( CLOCKS_PER_SEC / 10 );
1682
1683 bool redrawRequired(){
1684         clock_t currentTime = std::clock();
1685         if ( currentTime - g_lastRedrawTime >= c_redrawInterval ) {
1686                 g_lastRedrawTime = currentTime;
1687                 return true;
1688         }
1689         return false;
1690 }
1691 }
1692
1693 bool MainFrame_isActiveApp(){
1694         //globalOutputStream() << "listing\n";
1695         GList* list = gtk_window_list_toplevels();
1696         for ( GList* i = list; i != 0; i = g_list_next( i ) )
1697         {
1698                 //globalOutputStream() << "toplevel.. ";
1699                 if ( gtk_window_is_active( GTK_WINDOW( i->data ) ) ) {
1700                         //globalOutputStream() << "is active\n";
1701                         return true;
1702                 }
1703                 //globalOutputStream() << "not active\n";
1704         }
1705         return false;
1706 }
1707
1708 typedef std::list<CopiedString> StringStack;
1709 StringStack g_wait_stack;
1710 WaitDialog g_wait;
1711
1712 bool ScreenUpdates_Enabled(){
1713         return g_wait_stack.empty();
1714 }
1715
1716 void ScreenUpdates_process(){
1717         if ( redrawRequired() && GTK_WIDGET_VISIBLE( g_wait.m_window ) ) {
1718                 process_gui();
1719         }
1720 }
1721
1722
1723 void ScreenUpdates_Disable( const char* message, const char* title ){
1724         if ( g_wait_stack.empty() ) {
1725                 EverySecondTimer_disable();
1726
1727                 process_gui();
1728
1729                 bool isActiveApp = MainFrame_isActiveApp();
1730
1731                 g_wait = create_wait_dialog( title, message );
1732                 gtk_grab_add( GTK_WIDGET( g_wait.m_window ) );
1733
1734                 if ( isActiveApp ) {
1735                         gtk_widget_show( GTK_WIDGET( g_wait.m_window ) );
1736                         ScreenUpdates_process();
1737                 }
1738         }
1739         else if ( GTK_WIDGET_VISIBLE( g_wait.m_window ) ) {
1740                 gtk_label_set_text( g_wait.m_label, message );
1741                 ScreenUpdates_process();
1742         }
1743         g_wait_stack.push_back( message );
1744 }
1745
1746 void ScreenUpdates_Enable(){
1747         ASSERT_MESSAGE( !ScreenUpdates_Enabled(), "screen updates already enabled" );
1748         g_wait_stack.pop_back();
1749         if ( g_wait_stack.empty() ) {
1750                 EverySecondTimer_enable();
1751                 //gtk_widget_set_sensitive(GTK_WIDGET(MainFrame_getWindow()), TRUE);
1752
1753                 gtk_grab_remove( GTK_WIDGET( g_wait.m_window ) );
1754                 destroy_floating_window( g_wait.m_window );
1755                 g_wait.m_window = 0;
1756
1757                 //gtk_window_present(MainFrame_getWindow());
1758         }
1759         else if ( GTK_WIDGET_VISIBLE( g_wait.m_window ) ) {
1760                 gtk_label_set_text( g_wait.m_label, g_wait_stack.back().c_str() );
1761                 ScreenUpdates_process();
1762         }
1763 }
1764
1765
1766
1767 void GlobalCamera_UpdateWindow(){
1768         if ( g_pParentWnd != 0 ) {
1769                 CamWnd_Update( *g_pParentWnd->GetCamWnd() );
1770         }
1771 }
1772
1773 void XY_UpdateWindow( MainFrame& mainframe ){
1774         if ( mainframe.GetXYWnd() != 0 ) {
1775                 XYWnd_Update( *mainframe.GetXYWnd() );
1776         }
1777 }
1778
1779 void XZ_UpdateWindow( MainFrame& mainframe ){
1780         if ( mainframe.GetXZWnd() != 0 ) {
1781                 XYWnd_Update( *mainframe.GetXZWnd() );
1782         }
1783 }
1784
1785 void YZ_UpdateWindow( MainFrame& mainframe ){
1786         if ( mainframe.GetYZWnd() != 0 ) {
1787                 XYWnd_Update( *mainframe.GetYZWnd() );
1788         }
1789 }
1790
1791 void XY_UpdateAllWindows( MainFrame& mainframe ){
1792         XY_UpdateWindow( mainframe );
1793         XZ_UpdateWindow( mainframe );
1794         YZ_UpdateWindow( mainframe );
1795 }
1796
1797 void XY_UpdateAllWindows(){
1798         if ( g_pParentWnd != 0 ) {
1799                 XY_UpdateAllWindows( *g_pParentWnd );
1800         }
1801 }
1802
1803 void UpdateAllWindows(){
1804         GlobalCamera_UpdateWindow();
1805         XY_UpdateAllWindows();
1806 }
1807
1808
1809 void ModeChangeNotify(){
1810         SceneChangeNotify();
1811 }
1812
1813 void ClipperChangeNotify(){
1814         GlobalCamera_UpdateWindow();
1815         XY_UpdateAllWindows();
1816 }
1817
1818
1819 LatchedInt g_Layout_viewStyle( 0, "Window Layout" );
1820 LatchedBool g_Layout_enableDetachableMenus( true, "Detachable Menus" );
1821 LatchedBool g_Layout_enablePatchToolbar( true, "Patch Toolbar" );
1822 LatchedBool g_Layout_enablePluginToolbar( true, "Plugin Toolbar" );
1823 LatchedBool g_Layout_enableFilterToolbar( true, "Filter Toolbar" );
1824
1825
1826
1827 GtkMenuItem* create_file_menu(){
1828         // File menu
1829         GtkMenuItem* file_menu_item = new_sub_menu_item_with_mnemonic( "_File" );
1830         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( file_menu_item ) );
1831         if ( g_Layout_enableDetachableMenus.m_value ) {
1832                 menu_tearoff( menu );
1833         }
1834
1835         create_menu_item_with_mnemonic( menu, "_New Map", "NewMap" );
1836         menu_separator( menu );
1837
1838 #if 0
1839         //++timo temporary experimental stuff for sleep mode..
1840         create_menu_item_with_mnemonic( menu, "_Sleep", "Sleep" );
1841         menu_separator( menu );
1842         // end experimental
1843 #endif
1844
1845         create_menu_item_with_mnemonic( menu, "_Open...", "OpenMap" );
1846         create_menu_item_with_mnemonic( menu, "_Import...", "ImportMap" );
1847         menu_separator( menu );
1848         create_menu_item_with_mnemonic( menu, "_Save", "SaveMap" );
1849         create_menu_item_with_mnemonic( menu, "Save _as...", "SaveMapAs" );
1850         create_menu_item_with_mnemonic( menu, "Save s_elected...", "SaveSelected" );
1851         create_menu_item_with_mnemonic( menu, "Save re_gion...", "SaveRegion" );
1852         menu_separator( menu );
1853 //      menu_separator( menu );
1854 //      create_menu_item_with_mnemonic( menu, "_Refresh models", "RefreshReferences" );
1855 //      menu_separator( menu );
1856         create_menu_item_with_mnemonic( menu, "Pro_ject settings...", "ProjectSettings" );
1857         //menu_separator( menu );
1858         create_menu_item_with_mnemonic( menu, "_Pointfile...", "TogglePointfile" );
1859         menu_separator( menu );
1860         MRU_constructMenu( menu );
1861         menu_separator( menu );
1862 //      create_menu_item_with_mnemonic( menu, "Check for NetRadiant update (web)", "CheckForUpdate" ); // FIXME
1863         create_menu_item_with_mnemonic( menu, "E_xit", "Exit" );
1864
1865         return file_menu_item;
1866 }
1867
1868 GtkMenuItem* create_edit_menu(){
1869         // Edit menu
1870         GtkMenuItem* edit_menu_item = new_sub_menu_item_with_mnemonic( "_Edit" );
1871         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( edit_menu_item ) );
1872         if ( g_Layout_enableDetachableMenus.m_value ) {
1873                 menu_tearoff( menu );
1874         }
1875         create_menu_item_with_mnemonic( menu, "_Undo", "Undo" );
1876         create_menu_item_with_mnemonic( menu, "_Redo", "Redo" );
1877         menu_separator( menu );
1878         create_menu_item_with_mnemonic( menu, "_Copy", "Copy" );
1879         create_menu_item_with_mnemonic( menu, "_Paste", "Paste" );
1880         create_menu_item_with_mnemonic( menu, "P_aste To Camera", "PasteToCamera" );
1881         menu_separator( menu );
1882         create_menu_item_with_mnemonic( menu, "_Duplicate", "CloneSelection" );
1883         create_menu_item_with_mnemonic( menu, "Duplicate, make uni_que", "CloneSelectionAndMakeUnique" );
1884         create_menu_item_with_mnemonic( menu, "D_elete", "DeleteSelection" );
1885         //create_menu_item_with_mnemonic( menu, "Pa_rent", "ParentSelection" );
1886         menu_separator( menu );
1887         create_menu_item_with_mnemonic( menu, "C_lear Selection", "UnSelectSelection" );
1888         create_menu_item_with_mnemonic( menu, "_Invert Selection", "InvertSelection" );
1889         create_menu_item_with_mnemonic( menu, "Select i_nside", "SelectInside" );
1890         create_menu_item_with_mnemonic( menu, "Select _touching", "SelectTouching" );
1891
1892         menu_separator( menu );
1893
1894 //      GtkMenu* convert_menu = create_sub_menu_with_mnemonic( menu, "E_xpand Selection" );
1895 //      if ( g_Layout_enableDetachableMenus.m_value ) {
1896 //              menu_tearoff( convert_menu );
1897 //      }
1898         create_menu_item_with_mnemonic( menu, "Select All Of Type", "SelectAllOfType" );
1899         create_menu_item_with_mnemonic( menu, "_Expand Selection To Entities", "ExpandSelectionToEntities" );
1900
1901         menu_separator( menu );
1902         create_menu_item_with_mnemonic( menu, "Pre_ferences...", "Preferences" );
1903
1904         return edit_menu_item;
1905 }
1906
1907 void fill_view_xy_top_menu( GtkMenu* menu ){
1908         create_check_menu_item_with_mnemonic( menu, "XY (Top) View", "ToggleView" );
1909 }
1910
1911
1912 void fill_view_yz_side_menu( GtkMenu* menu ){
1913         create_check_menu_item_with_mnemonic( menu, "YZ (Side) View", "ToggleSideView" );
1914 }
1915
1916
1917 void fill_view_xz_front_menu( GtkMenu* menu ){
1918         create_check_menu_item_with_mnemonic( menu, "XZ (Front) View", "ToggleFrontView" );
1919 }
1920
1921
1922 GtkWidget* g_toggle_z_item = 0;
1923 GtkWidget* g_toggle_console_item = 0;
1924 GtkWidget* g_toggle_entity_item = 0;
1925 GtkWidget* g_toggle_entitylist_item = 0;
1926
1927 GtkMenuItem* create_view_menu( MainFrame::EViewStyle style ){
1928         // View menu
1929         GtkMenuItem* view_menu_item = new_sub_menu_item_with_mnemonic( "Vie_w" );
1930         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( view_menu_item ) );
1931         if ( g_Layout_enableDetachableMenus.m_value ) {
1932                 menu_tearoff( menu );
1933         }
1934
1935         if ( style == MainFrame::eFloating ) {
1936                 fill_view_camera_menu( menu );
1937                 fill_view_xy_top_menu( menu );
1938                 fill_view_yz_side_menu( menu );
1939                 fill_view_xz_front_menu( menu );
1940         }
1941         if ( style == MainFrame::eFloating || style == MainFrame::eSplit ) {
1942                 create_menu_item_with_mnemonic( menu, "Console View", "ToggleConsole" );
1943                 create_menu_item_with_mnemonic( menu, "Texture Browser", "ToggleTextures" );
1944                 create_menu_item_with_mnemonic( menu, "Entity Inspector", "ToggleEntityInspector" );
1945         }
1946         else
1947         {
1948                 create_menu_item_with_mnemonic( menu, "Entity Inspector", "ViewEntityInfo" );
1949         }
1950         create_menu_item_with_mnemonic( menu, "_Surface Inspector", "SurfaceInspector" );
1951         create_menu_item_with_mnemonic( menu, "_Patch Inspector", "PatchInspector" );
1952         create_menu_item_with_mnemonic( menu, "Entity List", "EntityList" );
1953
1954         menu_separator( menu );
1955         {
1956                 GtkMenu* camera_menu = create_sub_menu_with_mnemonic( menu, "Camera" );
1957                 if ( g_Layout_enableDetachableMenus.m_value ) {
1958                         menu_tearoff( camera_menu );
1959                 }
1960                 create_menu_item_with_mnemonic( camera_menu, "_Center", "CenterView" );
1961                 create_menu_item_with_mnemonic( camera_menu, "_Up Floor", "UpFloor" );
1962                 create_menu_item_with_mnemonic( camera_menu, "_Down Floor", "DownFloor" );
1963                 menu_separator( camera_menu );
1964                 create_menu_item_with_mnemonic( camera_menu, "Far Clip Plane In", "CubicClipZoomIn" );
1965                 create_menu_item_with_mnemonic( camera_menu, "Far Clip Plane Out", "CubicClipZoomOut" );
1966                 menu_separator( camera_menu );
1967                 create_menu_item_with_mnemonic( camera_menu, "Next leak spot", "NextLeakSpot" );
1968                 create_menu_item_with_mnemonic( camera_menu, "Previous leak spot", "PrevLeakSpot" );
1969                 menu_separator( camera_menu );
1970                 create_menu_item_with_mnemonic( camera_menu, "Look Through Selected", "LookThroughSelected" );
1971                 create_menu_item_with_mnemonic( camera_menu, "Look Through Camera", "LookThroughCamera" );
1972         }
1973         menu_separator( menu );
1974         {
1975                 GtkMenu* orthographic_menu = create_sub_menu_with_mnemonic( menu, "Orthographic" );
1976                 if ( g_Layout_enableDetachableMenus.m_value ) {
1977                         menu_tearoff( orthographic_menu );
1978                 }
1979                 if ( style == MainFrame::eRegular || style == MainFrame::eRegularLeft || style == MainFrame::eFloating ) {
1980                         create_menu_item_with_mnemonic( orthographic_menu, "_Next (XY, YZ, XY)", "NextView" );
1981                         create_menu_item_with_mnemonic( orthographic_menu, "XY (Top)", "ViewTop" );
1982                         create_menu_item_with_mnemonic( orthographic_menu, "YZ", "ViewSide" );
1983                         create_menu_item_with_mnemonic( orthographic_menu, "XZ", "ViewFront" );
1984                         menu_separator( orthographic_menu );
1985                 }
1986
1987                 create_menu_item_with_mnemonic( orthographic_menu, "Center on Selected", "CenterXYView" );
1988                 menu_separator( orthographic_menu );
1989                 create_menu_item_with_mnemonic( orthographic_menu, "_XY 100%", "Zoom100" );
1990                 create_menu_item_with_mnemonic( orthographic_menu, "XY Zoom _In", "ZoomIn" );
1991                 create_menu_item_with_mnemonic( orthographic_menu, "XY Zoom _Out", "ZoomOut" );
1992         }
1993
1994         menu_separator( menu );
1995
1996         {
1997                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Show" );
1998                 if ( g_Layout_enableDetachableMenus.m_value ) {
1999                         menu_tearoff( menu_in_menu );
2000                 }
2001                 create_menu_item_with_mnemonic( menu_in_menu, "Show Size Info", "ToggleSizePaint" );
2002                 create_menu_item_with_mnemonic( menu_in_menu, "Show Crosshair", "ToggleCrosshairs" );
2003                 create_menu_item_with_mnemonic( menu_in_menu, "Show Grid", "ToggleGrid" );
2004
2005                 menu_separator( menu_in_menu );
2006
2007                 create_check_menu_item_with_mnemonic( menu_in_menu, "Show _Angles", "ShowAngles" );
2008                 create_check_menu_item_with_mnemonic( menu_in_menu, "Show _Names", "ShowNames" );
2009                 create_check_menu_item_with_mnemonic( menu_in_menu, "Show Blocks", "ShowBlocks" );
2010                 create_check_menu_item_with_mnemonic( menu_in_menu, "Show C_oordinates", "ShowCoordinates" );
2011                 create_check_menu_item_with_mnemonic( menu_in_menu, "Show Window Outline", "ShowWindowOutline" );
2012                 create_check_menu_item_with_mnemonic( menu_in_menu, "Show Axes", "ShowAxes" );
2013                 create_check_menu_item_with_mnemonic( menu_in_menu, "Show Workzone", "ShowWorkzone" );
2014                 create_check_menu_item_with_mnemonic( menu_in_menu, "Show Stats", "ShowStats" );
2015         }
2016
2017         {
2018                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Filter" );
2019                 if ( g_Layout_enableDetachableMenus.m_value ) {
2020                         menu_tearoff( menu_in_menu );
2021                 }
2022                 Filters_constructMenu( menu_in_menu );
2023         }
2024         menu_separator( menu );
2025         {
2026 //              GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Hide/Show" );
2027 //              if ( g_Layout_enableDetachableMenus.m_value ) {
2028 //                      menu_tearoff( menu_in_menu );
2029 //              }
2030 //              create_menu_item_with_mnemonic( menu_in_menu, "Hide Selected", "HideSelected" );
2031 //              create_menu_item_with_mnemonic( menu_in_menu, "Show Hidden", "ShowHidden" );
2032                 create_menu_item_with_mnemonic( menu, "Hide Selected", "HideSelected" );
2033                 create_menu_item_with_mnemonic( menu, "Show Hidden", "ShowHidden" );
2034         }
2035         menu_separator( menu );
2036         {
2037                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Region" );
2038                 if ( g_Layout_enableDetachableMenus.m_value ) {
2039                         menu_tearoff( menu_in_menu );
2040                 }
2041                 create_menu_item_with_mnemonic( menu_in_menu, "_Off", "RegionOff" );
2042                 create_menu_item_with_mnemonic( menu_in_menu, "_Set XY", "RegionSetXY" );
2043                 create_menu_item_with_mnemonic( menu_in_menu, "Set _Brush", "RegionSetBrush" );
2044                 create_menu_item_with_mnemonic( menu_in_menu, "Set Se_lected Brushes", "RegionSetSelection" );
2045         }
2046
2047         command_connect_accelerator( "CenterXYView" );
2048
2049         return view_menu_item;
2050 }
2051
2052 GtkMenuItem* create_selection_menu(){
2053         // Selection menu
2054         GtkMenuItem* selection_menu_item = new_sub_menu_item_with_mnemonic( "M_odify" );
2055         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( selection_menu_item ) );
2056         if ( g_Layout_enableDetachableMenus.m_value ) {
2057                 menu_tearoff( menu );
2058         }
2059
2060         {
2061                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Components" );
2062                 if ( g_Layout_enableDetachableMenus.m_value ) {
2063                         menu_tearoff( menu_in_menu );
2064                 }
2065                 create_check_menu_item_with_mnemonic( menu_in_menu, "_Edges", "DragEdges" );
2066                 create_check_menu_item_with_mnemonic( menu_in_menu, "_Vertices", "DragVertices" );
2067                 create_check_menu_item_with_mnemonic( menu_in_menu, "_Faces", "DragFaces" );
2068         }
2069
2070         menu_separator( menu );
2071         create_menu_item_with_mnemonic( menu, "Snap To Grid", "SnapToGrid" );
2072
2073         menu_separator( menu );
2074
2075         {
2076                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Nudge" );
2077                 if ( g_Layout_enableDetachableMenus.m_value ) {
2078                         menu_tearoff( menu_in_menu );
2079                 }
2080                 create_menu_item_with_mnemonic( menu_in_menu, "Nudge Left", "SelectNudgeLeft" );
2081                 create_menu_item_with_mnemonic( menu_in_menu, "Nudge Right", "SelectNudgeRight" );
2082                 create_menu_item_with_mnemonic( menu_in_menu, "Nudge Up", "SelectNudgeUp" );
2083                 create_menu_item_with_mnemonic( menu_in_menu, "Nudge Down", "SelectNudgeDown" );
2084         }
2085         {
2086                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Rotate" );
2087                 if ( g_Layout_enableDetachableMenus.m_value ) {
2088                         menu_tearoff( menu_in_menu );
2089                 }
2090                 create_menu_item_with_mnemonic( menu_in_menu, "Rotate X", "RotateSelectionX" );
2091                 create_menu_item_with_mnemonic( menu_in_menu, "Rotate Y", "RotateSelectionY" );
2092                 create_menu_item_with_mnemonic( menu_in_menu, "Rotate Z", "RotateSelectionZ" );
2093         }
2094         {
2095                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Flip" );
2096                 if ( g_Layout_enableDetachableMenus.m_value ) {
2097                         menu_tearoff( menu_in_menu );
2098                 }
2099                 create_menu_item_with_mnemonic( menu_in_menu, "Flip _X", "MirrorSelectionX" );
2100                 create_menu_item_with_mnemonic( menu_in_menu, "Flip _Y", "MirrorSelectionY" );
2101                 create_menu_item_with_mnemonic( menu_in_menu, "Flip _Z", "MirrorSelectionZ" );
2102         }
2103         menu_separator( menu );
2104         create_menu_item_with_mnemonic( menu, "Arbitrary rotation...", "ArbitraryRotation" );
2105         create_menu_item_with_mnemonic( menu, "Arbitrary scale...", "ArbitraryScale" );
2106
2107         return selection_menu_item;
2108 }
2109
2110 GtkMenuItem* create_bsp_menu(){
2111         // BSP menu
2112         GtkMenuItem* bsp_menu_item = new_sub_menu_item_with_mnemonic( "_Build" );
2113         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( bsp_menu_item ) );
2114
2115         if ( g_Layout_enableDetachableMenus.m_value ) {
2116                 menu_tearoff( menu );
2117         }
2118
2119         create_menu_item_with_mnemonic( menu, "Customize...", "BuildMenuCustomize" );
2120         create_menu_item_with_mnemonic( menu, "Run recent build", "Build_runRecentExecutedBuild" );
2121
2122         menu_separator( menu );
2123
2124         Build_constructMenu( menu );
2125
2126         g_bsp_menu = menu;
2127
2128         return bsp_menu_item;
2129 }
2130
2131 GtkMenuItem* create_grid_menu(){
2132         // Grid menu
2133         GtkMenuItem* grid_menu_item = new_sub_menu_item_with_mnemonic( "_Grid" );
2134         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( grid_menu_item ) );
2135         if ( g_Layout_enableDetachableMenus.m_value ) {
2136                 menu_tearoff( menu );
2137         }
2138
2139         Grid_constructMenu( menu );
2140
2141         return grid_menu_item;
2142 }
2143
2144 GtkMenuItem* create_misc_menu(){
2145         // Misc menu
2146         GtkMenuItem* misc_menu_item = new_sub_menu_item_with_mnemonic( "M_isc" );
2147         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( misc_menu_item ) );
2148         if ( g_Layout_enableDetachableMenus.m_value ) {
2149                 menu_tearoff( menu );
2150         }
2151
2152 #if 0
2153         create_menu_item_with_mnemonic( menu, "_Benchmark", FreeCaller<GlobalCamera_Benchmark>() );
2154 #endif
2155         gtk_container_add( GTK_CONTAINER( menu ), GTK_WIDGET( create_colours_menu() ) );
2156
2157         create_menu_item_with_mnemonic( menu, "Find brush...", "FindBrush" );
2158         create_menu_item_with_mnemonic( menu, "Map Info...", "MapInfo" );
2159         // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=394
2160 //  create_menu_item_with_mnemonic(menu, "_Print XY View", FreeCaller<WXY_Print>());
2161         create_menu_item_with_mnemonic( menu, "_Background image...", FreeCaller<WXY_BackgroundSelect>() );
2162         create_menu_item_with_mnemonic( menu, "Fullscreen", "Fullscreen" );
2163         create_menu_item_with_mnemonic( menu, "Maximize view", "MaximizeView" );
2164         return misc_menu_item;
2165 }
2166
2167 GtkMenuItem* create_entity_menu(){
2168         // Brush menu
2169         GtkMenuItem* entity_menu_item = new_sub_menu_item_with_mnemonic( "E_ntity" );
2170         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( entity_menu_item ) );
2171         if ( g_Layout_enableDetachableMenus.m_value ) {
2172                 menu_tearoff( menu );
2173         }
2174
2175         Entity_constructMenu( menu );
2176
2177         return entity_menu_item;
2178 }
2179
2180 GtkMenuItem* create_brush_menu(){
2181         // Brush menu
2182         GtkMenuItem* brush_menu_item = new_sub_menu_item_with_mnemonic( "B_rush" );
2183         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( brush_menu_item ) );
2184         if ( g_Layout_enableDetachableMenus.m_value ) {
2185                 menu_tearoff( menu );
2186         }
2187
2188         Brush_constructMenu( menu );
2189
2190         return brush_menu_item;
2191 }
2192
2193 GtkMenuItem* create_patch_menu(){
2194         // Curve menu
2195         GtkMenuItem* patch_menu_item = new_sub_menu_item_with_mnemonic( "_Curve" );
2196         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( patch_menu_item ) );
2197         if ( g_Layout_enableDetachableMenus.m_value ) {
2198                 menu_tearoff( menu );
2199         }
2200
2201         Patch_constructMenu( menu );
2202
2203         return patch_menu_item;
2204 }
2205
2206 GtkMenuItem* create_help_menu(){
2207         // Help menu
2208         GtkMenuItem* help_menu_item = new_sub_menu_item_with_mnemonic( "_Help" );
2209         GtkMenu* menu = GTK_MENU( gtk_menu_item_get_submenu( help_menu_item ) );
2210         if ( g_Layout_enableDetachableMenus.m_value ) {
2211                 menu_tearoff( menu );
2212         }
2213
2214 //      create_menu_item_with_mnemonic( menu, "Manual", "OpenManual" );
2215
2216         // this creates all the per-game drop downs for the game pack helps
2217         // it will take care of hooking the Sys_OpenURL calls etc.
2218         create_game_help_menu( menu );
2219
2220 //      create_menu_item_with_mnemonic( menu, "Bug report", FreeCaller<OpenBugReportURL>() );
2221         create_menu_item_with_mnemonic( menu, "Shortcuts list", FreeCaller<DoCommandListDlg>() );
2222         create_menu_item_with_mnemonic( menu, "_About", FreeCaller<DoAbout>() );
2223
2224         return help_menu_item;
2225 }
2226
2227 GtkMenuBar* create_main_menu( MainFrame::EViewStyle style ){
2228         GtkMenuBar* menu_bar = GTK_MENU_BAR( gtk_menu_bar_new() );
2229         gtk_widget_show( GTK_WIDGET( menu_bar ) );
2230
2231         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_file_menu() ) );
2232         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_edit_menu() ) );
2233         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_view_menu( style ) ) );
2234         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_selection_menu() ) );
2235         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_bsp_menu() ) );
2236         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_grid_menu() ) );
2237         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_misc_menu() ) );
2238         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_entity_menu() ) );
2239         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_brush_menu() ) );
2240         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_patch_menu() ) );
2241         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_plugins_menu() ) );
2242         gtk_container_add( GTK_CONTAINER( menu_bar ), GTK_WIDGET( create_help_menu() ) );
2243
2244         return menu_bar;
2245 }
2246
2247
2248 void PatchInspector_registerShortcuts(){
2249         command_connect_accelerator( "PatchInspector" );
2250 }
2251
2252 void Patch_registerShortcuts(){
2253 //      command_connect_accelerator( "InvertCurveTextureX" );
2254 //      command_connect_accelerator( "InvertCurveTextureY" );
2255 //      command_connect_accelerator( "PatchInsertInsertColumn" );
2256 //      command_connect_accelerator( "PatchInsertInsertRow" );
2257 //      command_connect_accelerator( "PatchDeleteLastColumn" );
2258 //      command_connect_accelerator( "PatchDeleteLastRow" );
2259 //      command_connect_accelerator( "NaturalizePatch" );
2260         //command_connect_accelerator("CapCurrentCurve");
2261 }
2262
2263 void Manipulators_registerShortcuts(){
2264         toggle_add_accelerator( "MouseRotate" );
2265         toggle_add_accelerator( "MouseTranslate" );
2266         toggle_add_accelerator( "MouseScale" );
2267         toggle_add_accelerator( "MouseDrag" );
2268         toggle_add_accelerator( "ToggleClipper" );
2269 }
2270
2271 void TexdefNudge_registerShortcuts(){
2272         command_connect_accelerator( "TexRotateClock" );
2273         command_connect_accelerator( "TexRotateCounter" );
2274         command_connect_accelerator( "TexScaleUp" );
2275         command_connect_accelerator( "TexScaleDown" );
2276         command_connect_accelerator( "TexScaleLeft" );
2277         command_connect_accelerator( "TexScaleRight" );
2278         command_connect_accelerator( "TexShiftUp" );
2279         command_connect_accelerator( "TexShiftDown" );
2280         command_connect_accelerator( "TexShiftLeft" );
2281         command_connect_accelerator( "TexShiftRight" );
2282 }
2283
2284 void SelectNudge_registerShortcuts(){
2285         command_connect_accelerator( "MoveSelectionDOWN" );
2286         command_connect_accelerator( "MoveSelectionUP" );
2287         //command_connect_accelerator("SelectNudgeLeft");
2288         //command_connect_accelerator("SelectNudgeRight");
2289         //command_connect_accelerator("SelectNudgeUp");
2290         //command_connect_accelerator("SelectNudgeDown");
2291         command_connect_accelerator( "UnSelectSelection2" );
2292         command_connect_accelerator( "DeleteSelection2" );
2293 }
2294
2295 void SnapToGrid_registerShortcuts(){
2296         command_connect_accelerator( "SnapToGrid" );
2297 }
2298
2299 void SelectByType_registerShortcuts(){
2300         command_connect_accelerator( "SelectAllOfType" );
2301 }
2302
2303 void SurfaceInspector_registerShortcuts(){
2304         command_connect_accelerator( "FitTexture" );
2305 }
2306
2307 void TexBro_registerShortcuts(){
2308         command_connect_accelerator( "FindReplaceTextures" );
2309         command_connect_accelerator( "RefreshShaders" );
2310 }
2311
2312 void Misc_registerShortcuts(){
2313         //refresh models
2314         command_connect_accelerator( "RefreshReferences" );
2315         command_connect_accelerator( "MouseRotateOrScale" );
2316         command_connect_accelerator( "MouseDragOrScale" );
2317 }
2318
2319
2320 void register_shortcuts(){
2321 //      PatchInspector_registerShortcuts();
2322         //Patch_registerShortcuts();
2323         Grid_registerShortcuts();
2324 //      XYWnd_registerShortcuts();
2325         CamWnd_registerShortcuts();
2326         Manipulators_registerShortcuts();
2327         SurfaceInspector_registerShortcuts();
2328         TexdefNudge_registerShortcuts();
2329         SelectNudge_registerShortcuts();
2330 //      SnapToGrid_registerShortcuts();
2331 //      SelectByType_registerShortcuts();
2332         TexBro_registerShortcuts();
2333         Misc_registerShortcuts();
2334 }
2335
2336 void File_constructToolbar( GtkToolbar* toolbar ){
2337         toolbar_append_button( toolbar, "Open an existing map (CTRL + O)", "file_open.png", "OpenMap" );
2338         toolbar_append_button( toolbar, "Save the active map (CTRL + S)", "file_save.png", "SaveMap" );
2339 }
2340
2341 void UndoRedo_constructToolbar( GtkToolbar* toolbar ){
2342         toolbar_append_button( toolbar, "Undo (CTRL + Z)", "undo.png", "Undo" );
2343         toolbar_append_button( toolbar, "Redo (CTRL + Y)", "redo.png", "Redo" );
2344 }
2345
2346 void RotateFlip_constructToolbar( GtkToolbar* toolbar ){
2347         toolbar_append_button( toolbar, "x-axis Flip", "brush_flipx.png", "MirrorSelectionX" );
2348         toolbar_append_button( toolbar, "x-axis Rotate", "brush_rotatex.png", "RotateSelectionX" );
2349         toolbar_append_button( toolbar, "y-axis Flip", "brush_flipy.png", "MirrorSelectionY" );
2350         toolbar_append_button( toolbar, "y-axis Rotate", "brush_rotatey.png", "RotateSelectionY" );
2351         toolbar_append_button( toolbar, "z-axis Flip", "brush_flipz.png", "MirrorSelectionZ" );
2352         toolbar_append_button( toolbar, "z-axis Rotate", "brush_rotatez.png", "RotateSelectionZ" );
2353 }
2354
2355 void Select_constructToolbar( GtkToolbar* toolbar ){
2356         toolbar_append_button( toolbar, "Select touching", "selection_selecttouching.png", "SelectTouching" );
2357         toolbar_append_button( toolbar, "Select inside", "selection_selectinside.png", "SelectInside" );
2358 }
2359
2360 void CSG_constructToolbar( GtkToolbar* toolbar ){
2361         toolbar_append_button( toolbar, "CSG Subtract (SHIFT + U)", "selection_csgsubtract.png", "CSGSubtract" );
2362         toolbar_append_button( toolbar, "CSG Merge (CTRL + U)", "selection_csgmerge.png", "CSGMerge" );
2363         toolbar_append_button( toolbar, "Room", "selection_makeroom.png", "CSGroom" );
2364         toolbar_append_button( toolbar, "CSG Tool", "ellipsis.png", "CSGTool" );
2365 }
2366
2367 void ComponentModes_constructToolbar( GtkToolbar* toolbar ){
2368         toolbar_append_toggle_button( toolbar, "Select Vertices (V)", "modify_vertices.png", "DragVertices" );
2369         toolbar_append_toggle_button( toolbar, "Select Edges (E)", "modify_edges.png", "DragEdges" );
2370         toolbar_append_toggle_button( toolbar, "Select Faces (F)", "modify_faces.png", "DragFaces" );
2371 }
2372
2373 void Clipper_constructToolbar( GtkToolbar* toolbar ){
2374
2375         toolbar_append_toggle_button( toolbar, "Clipper (X)", "view_clipper.png", "ToggleClipper" );
2376 }
2377
2378 void XYWnd_constructToolbar( GtkToolbar* toolbar ){
2379         toolbar_append_button( toolbar, "Change views (CTRL + TAB)", "view_change.png", "NextView" );
2380 }
2381
2382 void Manipulators_constructToolbar( GtkToolbar* toolbar ){
2383         toolbar_append_toggle_button( toolbar, "Translate (W)", "select_mousetranslate.png", "MouseTranslate" );
2384         toolbar_append_toggle_button( toolbar, "Rotate (R)", "select_mouserotate.png", "MouseRotate" );
2385         toolbar_append_toggle_button( toolbar, "Scale (Q)", "select_mousescale.png", "MouseScale" );
2386         toolbar_append_toggle_button( toolbar, "Resize (Q)", "select_mouseresize.png", "MouseDrag" );
2387
2388         Clipper_constructToolbar( toolbar );
2389 }
2390
2391 GtkToolbar* create_main_toolbar( MainFrame::EViewStyle style ){
2392         GtkToolbar* toolbar = GTK_TOOLBAR( gtk_toolbar_new() );
2393         gtk_toolbar_set_orientation( toolbar, GTK_ORIENTATION_HORIZONTAL );
2394         gtk_toolbar_set_style( toolbar, GTK_TOOLBAR_ICONS );
2395
2396         gtk_widget_show( GTK_WIDGET( toolbar ) );
2397
2398         File_constructToolbar( toolbar );
2399
2400         gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2401
2402         UndoRedo_constructToolbar( toolbar );
2403
2404         gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2405
2406         RotateFlip_constructToolbar( toolbar );
2407
2408         gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2409
2410         Select_constructToolbar( toolbar );
2411
2412         gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2413
2414         CSG_constructToolbar( toolbar );
2415
2416         gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2417
2418         ComponentModes_constructToolbar( toolbar );
2419
2420         if ( style == MainFrame::eRegular || style == MainFrame::eRegularLeft ) {
2421                 gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2422
2423                 XYWnd_constructToolbar( toolbar );
2424         }
2425
2426         gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2427
2428         CamWnd_constructToolbar( toolbar );
2429
2430         gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2431
2432         Manipulators_constructToolbar( toolbar );
2433
2434         if ( g_Layout_enablePatchToolbar.m_value ) {
2435                 gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2436
2437                 Patch_constructToolbar( toolbar );
2438         }
2439
2440         gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2441
2442         toolbar_append_toggle_button( toolbar, "Texture Lock (SHIFT + T)", "texture_lock.png", "TogTexLock" );
2443
2444         gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2445
2446         GtkButton* g_view_entities_button = toolbar_append_button( toolbar, "Entities (N)", "entities.png", "ToggleEntityInspector" );
2447         // disable the console and texture button in the regular layouts
2448         if ( style != MainFrame::eRegular && style != MainFrame::eRegularLeft ) {
2449                 GtkButton* g_view_console_button = toolbar_append_button( toolbar, "Console (O)", "console.png", "ToggleConsole" );
2450                 GtkButton* g_view_textures_button = toolbar_append_button( toolbar, "Texture Browser (T)", "texture_browser.png", "ToggleTextures" );
2451         }
2452
2453         // TODO: call light inspector
2454         //GtkButton* g_view_lightinspector_button = toolbar_append_button(toolbar, "Light Inspector", "lightinspector.png", "ToggleLightInspector");
2455
2456         gtk_toolbar_append_space( GTK_TOOLBAR( toolbar ) );
2457         GtkButton* g_refresh_models_button = toolbar_append_button( toolbar, "Refresh Models", "refresh_models.png", "RefreshReferences" );
2458
2459         return toolbar;
2460 }
2461
2462 GtkWidget* create_main_statusbar( GtkWidget *pStatusLabel[c_count_status] ){
2463         GtkTable* table = GTK_TABLE( gtk_table_new( 1, c_count_status, FALSE ) );
2464         gtk_widget_show( GTK_WIDGET( table ) );
2465
2466         {
2467                 GtkLabel* label = GTK_LABEL( gtk_label_new( "Label" ) );
2468                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
2469                 gtk_misc_set_padding( GTK_MISC( label ), 4, 2 );
2470                 gtk_widget_show( GTK_WIDGET( label ) );
2471                 gtk_table_attach_defaults( table, GTK_WIDGET( label ), 0, 1, 0, 1 );
2472                 pStatusLabel[c_command_status] = GTK_WIDGET( label );
2473         }
2474
2475         for ( int i = 1; i < c_count_status; ++i )
2476         {
2477                 GtkFrame* frame = GTK_FRAME( gtk_frame_new( 0 ) );
2478                 gtk_widget_show( GTK_WIDGET( frame ) );
2479                 gtk_table_attach_defaults( table, GTK_WIDGET( frame ), i, i + 1, 0, 1 );
2480                 gtk_frame_set_shadow_type( frame, GTK_SHADOW_IN );
2481
2482                 GtkLabel* label = GTK_LABEL( gtk_label_new( "Label" ) );
2483                 gtk_label_set_ellipsize( label, PANGO_ELLIPSIZE_END );
2484                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
2485                 gtk_misc_set_padding( GTK_MISC( label ), 4, 2 );
2486                 gtk_widget_show( GTK_WIDGET( label ) );
2487                 gtk_container_add( GTK_CONTAINER( frame ), GTK_WIDGET( label ) );
2488                 pStatusLabel[i] = GTK_WIDGET( label );
2489         }
2490
2491         return GTK_WIDGET( table );
2492 }
2493
2494 #if 0
2495
2496
2497 WidgetFocusPrinter g_mainframeWidgetFocusPrinter( "mainframe" );
2498
2499 class WindowFocusPrinter
2500 {
2501 const char* m_name;
2502
2503 static gboolean frame_event( GtkWidget *widget, GdkEvent* event, WindowFocusPrinter* self ){
2504         globalOutputStream() << self->m_name << " frame_event\n";
2505         return FALSE;
2506 }
2507 static gboolean keys_changed( GtkWidget *widget, WindowFocusPrinter* self ){
2508         globalOutputStream() << self->m_name << " keys_changed\n";
2509         return FALSE;
2510 }
2511 static gboolean notify( GtkWindow* window, gpointer dummy, WindowFocusPrinter* self ){
2512         if ( gtk_window_is_active( window ) ) {
2513                 globalOutputStream() << self->m_name << " takes toplevel focus\n";
2514         }
2515         else
2516         {
2517                 globalOutputStream() << self->m_name << " loses toplevel focus\n";
2518         }
2519         return FALSE;
2520 }
2521 public:
2522 WindowFocusPrinter( const char* name ) : m_name( name ){
2523 }
2524 void connect( GtkWindow* toplevel_window ){
2525         g_signal_connect( G_OBJECT( toplevel_window ), "notify::has_toplevel_focus", G_CALLBACK( notify ), this );
2526         g_signal_connect( G_OBJECT( toplevel_window ), "notify::is_active", G_CALLBACK( notify ), this );
2527         g_signal_connect( G_OBJECT( toplevel_window ), "keys_changed", G_CALLBACK( keys_changed ), this );
2528         g_signal_connect( G_OBJECT( toplevel_window ), "frame_event", G_CALLBACK( frame_event ), this );
2529 }
2530 };
2531
2532 WindowFocusPrinter g_mainframeFocusPrinter( "mainframe" );
2533
2534 #endif
2535
2536 class MainWindowActive
2537 {
2538 static gboolean notify( GtkWindow* window, gpointer dummy, MainWindowActive* self ){
2539         if ( g_wait.m_window != 0 && gtk_window_is_active( window ) && !GTK_WIDGET_VISIBLE( g_wait.m_window ) ) {
2540                 gtk_widget_show( GTK_WIDGET( g_wait.m_window ) );
2541         }
2542
2543         return FALSE;
2544 }
2545 public:
2546 void connect( GtkWindow* toplevel_window ){
2547         g_signal_connect( G_OBJECT( toplevel_window ), "notify::is-active", G_CALLBACK( notify ), this );
2548 }
2549 };
2550
2551 MainWindowActive g_MainWindowActive;
2552
2553 SignalHandlerId XYWindowDestroyed_connect( const SignalHandler& handler ){
2554         return g_pParentWnd->GetXYWnd()->onDestroyed.connectFirst( handler );
2555 }
2556
2557 void XYWindowDestroyed_disconnect( SignalHandlerId id ){
2558         g_pParentWnd->GetXYWnd()->onDestroyed.disconnect( id );
2559 }
2560
2561 MouseEventHandlerId XYWindowMouseDown_connect( const MouseEventHandler& handler ){
2562         return g_pParentWnd->GetXYWnd()->onMouseDown.connectFirst( handler );
2563 }
2564
2565 void XYWindowMouseDown_disconnect( MouseEventHandlerId id ){
2566         g_pParentWnd->GetXYWnd()->onMouseDown.disconnect( id );
2567 }
2568
2569 // =============================================================================
2570 // MainFrame class
2571
2572 MainFrame* g_pParentWnd = 0;
2573
2574 GtkWindow* MainFrame_getWindow(){
2575         if ( g_pParentWnd == 0 ) {
2576                 return 0;
2577         }
2578         return g_pParentWnd->m_window;
2579 }
2580
2581 std::vector<GtkWidget*> g_floating_windows;
2582
2583 MainFrame::MainFrame() : m_window( 0 ), m_idleRedrawStatusText( RedrawStatusTextCaller( *this ) ){
2584         m_pXYWnd = 0;
2585         m_pCamWnd = 0;
2586         m_pZWnd = 0;
2587         m_pYZWnd = 0;
2588         m_pXZWnd = 0;
2589         m_pActiveXY = 0;
2590
2591         for ( int n = 0; n < c_count_status; n++ )
2592         {
2593                 m_pStatusLabel[n] = 0;
2594         }
2595
2596         m_bSleeping = false;
2597
2598         Create();
2599 }
2600
2601 MainFrame::~MainFrame(){
2602         SaveWindowInfo();
2603
2604         gtk_widget_hide( GTK_WIDGET( m_window ) );
2605
2606         Shutdown();
2607
2608         for ( std::vector<GtkWidget*>::iterator i = g_floating_windows.begin(); i != g_floating_windows.end(); ++i )
2609         {
2610                 gtk_widget_destroy( *i );
2611         }
2612
2613         gtk_widget_destroy( GTK_WIDGET( m_window ) );
2614 }
2615
2616 void MainFrame::SetActiveXY( XYWnd* p ){
2617         if ( m_pActiveXY ) {
2618                 m_pActiveXY->SetActive( false );
2619         }
2620
2621         m_pActiveXY = p;
2622
2623         if ( m_pActiveXY ) {
2624                 m_pActiveXY->SetActive( true );
2625         }
2626
2627 }
2628
2629 void MainFrame::ReleaseContexts(){
2630 #if 0
2631         if ( m_pXYWnd ) {
2632                 m_pXYWnd->DestroyContext();
2633         }
2634         if ( m_pYZWnd ) {
2635                 m_pYZWnd->DestroyContext();
2636         }
2637         if ( m_pXZWnd ) {
2638                 m_pXZWnd->DestroyContext();
2639         }
2640         if ( m_pCamWnd ) {
2641                 m_pCamWnd->DestroyContext();
2642         }
2643         if ( m_pTexWnd ) {
2644                 m_pTexWnd->DestroyContext();
2645         }
2646         if ( m_pZWnd ) {
2647                 m_pZWnd->DestroyContext();
2648         }
2649 #endif
2650 }
2651
2652 void MainFrame::CreateContexts(){
2653 #if 0
2654         if ( m_pCamWnd ) {
2655                 m_pCamWnd->CreateContext();
2656         }
2657         if ( m_pXYWnd ) {
2658                 m_pXYWnd->CreateContext();
2659         }
2660         if ( m_pYZWnd ) {
2661                 m_pYZWnd->CreateContext();
2662         }
2663         if ( m_pXZWnd ) {
2664                 m_pXZWnd->CreateContext();
2665         }
2666         if ( m_pTexWnd ) {
2667                 m_pTexWnd->CreateContext();
2668         }
2669         if ( m_pZWnd ) {
2670                 m_pZWnd->CreateContext();
2671         }
2672 #endif
2673 }
2674
2675 #ifdef _DEBUG
2676 //#define DBG_SLEEP
2677 #endif
2678
2679 void MainFrame::OnSleep(){
2680 #if 0
2681         m_bSleeping ^= 1;
2682         if ( m_bSleeping ) {
2683                 // useful when trying to debug crashes in the sleep code
2684                 globalOutputStream() << "Going into sleep mode..\n";
2685
2686                 globalOutputStream() << "Dispatching sleep msg...";
2687                 DispatchRadiantMsg( RADIANT_SLEEP );
2688                 globalOutputStream() << "Done.\n";
2689
2690                 gtk_window_iconify( m_window );
2691                 GlobalSelectionSystem().setSelectedAll( false );
2692
2693                 GlobalShaderCache().unrealise();
2694                 Shaders_Free();
2695                 GlobalOpenGL_debugAssertNoErrors();
2696                 ScreenUpdates_Disable();
2697
2698                 // release contexts
2699                 globalOutputStream() << "Releasing contexts...";
2700                 ReleaseContexts();
2701                 globalOutputStream() << "Done.\n";
2702         }
2703         else
2704         {
2705                 globalOutputStream() << "Waking up\n";
2706
2707                 gtk_window_deiconify( m_window );
2708
2709                 // create contexts
2710                 globalOutputStream() << "Creating contexts...";
2711                 CreateContexts();
2712                 globalOutputStream() << "Done.\n";
2713
2714                 globalOutputStream() << "Making current on camera...";
2715                 m_pCamWnd->MakeCurrent();
2716                 globalOutputStream() << "Done.\n";
2717
2718                 globalOutputStream() << "Reloading shaders...";
2719                 Shaders_Load();
2720                 GlobalShaderCache().realise();
2721                 globalOutputStream() << "Done.\n";
2722
2723                 ScreenUpdates_Enable();
2724
2725                 globalOutputStream() << "Dispatching wake msg...";
2726                 DispatchRadiantMsg( RADIANT_WAKEUP );
2727                 globalOutputStream() << "Done\n";
2728         }
2729 #endif
2730 }
2731
2732
2733 GtkWindow* create_splash(){
2734         GtkWindow* window = GTK_WINDOW( gtk_window_new( GTK_WINDOW_TOPLEVEL ) );
2735         gtk_window_set_decorated( window, FALSE );
2736         gtk_window_set_resizable( window, FALSE );
2737         gtk_window_set_modal( window, TRUE );
2738         gtk_window_set_default_size( window, -1, -1 );
2739         gtk_window_set_position( window, GTK_WIN_POS_CENTER );
2740         gtk_container_set_border_width( GTK_CONTAINER( window ), 0 );
2741
2742         GtkImage* image = new_local_image( "splash.png" );
2743         gtk_widget_show( GTK_WIDGET( image ) );
2744         gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( image ) );
2745
2746         gtk_widget_set_size_request( GTK_WIDGET( window ), -1, -1 );
2747         gtk_widget_show( GTK_WIDGET( window ) );
2748
2749         return window;
2750 }
2751
2752 static GtkWindow *splash_screen = 0;
2753
2754 void show_splash(){
2755         splash_screen = create_splash();
2756
2757         process_gui();
2758 }
2759
2760 void hide_splash(){
2761         gtk_widget_destroy( GTK_WIDGET( splash_screen ) );
2762 }
2763
2764 WindowPositionTracker g_posCamWnd;
2765 WindowPositionTracker g_posXYWnd;
2766 WindowPositionTracker g_posXZWnd;
2767 WindowPositionTracker g_posYZWnd;
2768
2769 static gint mainframe_delete( GtkWidget *widget, GdkEvent *event, gpointer data ){
2770         if ( ConfirmModified( "Exit Radiant" ) ) {
2771                 gtk_main_quit();
2772         }
2773
2774         return TRUE;
2775 }
2776
2777 void MainFrame::Create(){
2778         GtkWindow* window = GTK_WINDOW( gtk_window_new( GTK_WINDOW_TOPLEVEL ) );
2779
2780         GlobalWindowObservers_connectTopLevel( window );
2781
2782         gtk_window_set_transient_for( splash_screen, window );
2783
2784 #if !defined( WIN32 )
2785         {
2786                 GdkPixbuf* pixbuf = pixbuf_new_from_file_with_mask( "bitmaps/icon.png" );
2787                 if ( pixbuf != 0 ) {
2788                         gtk_window_set_icon( window, pixbuf );
2789                         g_object_unref( pixbuf );
2790                 }
2791         }
2792 #endif
2793
2794         gtk_widget_add_events( GTK_WIDGET( window ), GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_FOCUS_CHANGE_MASK );
2795         g_signal_connect( G_OBJECT( window ), "delete_event", G_CALLBACK( mainframe_delete ), this );
2796
2797         m_position_tracker.connect( window );
2798
2799 #if 0
2800         g_mainframeWidgetFocusPrinter.connect( window );
2801         g_mainframeFocusPrinter.connect( window );
2802 #endif
2803
2804         g_MainWindowActive.connect( window );
2805
2806         GetPlugInMgr().Init( GTK_WIDGET( window ) );
2807
2808         GtkWidget* vbox = gtk_vbox_new( FALSE, 0 );
2809         gtk_container_add( GTK_CONTAINER( window ), vbox );
2810         gtk_widget_show( vbox );
2811         gtk_container_set_focus_chain( GTK_CONTAINER( vbox ), NULL );
2812
2813         global_accel_connect_window( window );
2814
2815         m_nCurrentStyle = (EViewStyle)g_Layout_viewStyle.m_value;
2816
2817         register_shortcuts();
2818
2819         GtkMenuBar* main_menu = create_main_menu( CurrentStyle() );
2820         gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( main_menu ), FALSE, FALSE, 0 );
2821
2822         GtkToolbar* main_toolbar = create_main_toolbar( CurrentStyle() );
2823         gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( main_toolbar ), FALSE, FALSE, 0 );
2824
2825
2826
2827
2828         if ( g_Layout_enablePluginToolbar.m_value || g_Layout_enableFilterToolbar.m_value ){
2829                 GtkWidget* PFbox = gtk_hbox_new( FALSE, 3 );
2830                 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( PFbox ), FALSE, FALSE, 0 );
2831                 gtk_widget_show( PFbox );
2832                 if ( g_Layout_enablePluginToolbar.m_value ){
2833                         GtkToolbar* plugin_toolbar = create_plugin_toolbar();
2834                         if ( g_Layout_enableFilterToolbar.m_value ){
2835                                 gtk_box_pack_start( GTK_BOX( PFbox ), GTK_WIDGET( plugin_toolbar ), FALSE, FALSE, 0 );
2836                         }
2837                         else{
2838                                 gtk_box_pack_start( GTK_BOX( PFbox ), GTK_WIDGET( plugin_toolbar ), TRUE, TRUE, 0 );
2839                         }
2840                 }
2841                 if ( g_Layout_enableFilterToolbar.m_value ){
2842                         GtkToolbar* filter_toolbar = create_filter_toolbar();
2843                         gtk_box_pack_start( GTK_BOX( PFbox ), GTK_WIDGET( filter_toolbar ), TRUE, TRUE, 0 );
2844                 }
2845         }
2846
2847         /*GtkToolbar* plugin_toolbar = create_plugin_toolbar();
2848         if ( !g_Layout_enablePluginToolbar.m_value ) {
2849                 gtk_widget_hide( GTK_WIDGET( plugin_toolbar ) );
2850         }*/
2851
2852
2853
2854
2855         GtkWidget* main_statusbar = create_main_statusbar( m_pStatusLabel );
2856         gtk_box_pack_end( GTK_BOX( vbox ), main_statusbar, FALSE, TRUE, 2 );
2857
2858         GroupDialog_constructWindow( window );
2859
2860         /* want to realize it immediately; otherwise gtk paned splits positions wont be set correctly for floating group dlg */
2861         gtk_widget_realize ( GTK_WIDGET( GroupDialog_getWindow() ) );
2862
2863         g_page_entity = GroupDialog_addPage( "Entities", EntityInspector_constructWindow( GroupDialog_getWindow() ), RawStringExportCaller( "Entities" ) );
2864
2865         if ( FloatingGroupDialog() ) {
2866                 g_page_console = GroupDialog_addPage( "Console", Console_constructWindow( GroupDialog_getWindow() ), RawStringExportCaller( "Console" ) );
2867         }
2868
2869 #ifdef WIN32
2870         if ( g_multimon_globals.m_bStartOnPrimMon ) {
2871                 PositionWindowOnPrimaryScreen( g_layout_globals.m_position );
2872         }
2873 #endif
2874         window_set_position( window, g_layout_globals.m_position );
2875
2876         m_window = window;
2877
2878         gtk_widget_show( GTK_WIDGET( window ) );
2879
2880         if ( CurrentStyle() == eRegular || CurrentStyle() == eRegularLeft ) {
2881                 {
2882                         GtkWidget* hsplit = gtk_hpaned_new();
2883                         m_hSplit = hsplit;
2884                         gtk_box_pack_start( GTK_BOX( vbox ), hsplit, TRUE, TRUE, 0 );
2885                         gtk_widget_show( hsplit );
2886                         {
2887                                 GtkWidget* vsplit = gtk_vpaned_new();
2888                                 gtk_widget_show( vsplit );
2889                                 m_vSplit = vsplit;
2890                                 GtkWidget* vsplit2 = gtk_vpaned_new();
2891                                 gtk_widget_show( vsplit2 );
2892                                 m_vSplit2 = vsplit2;
2893                                 if ( CurrentStyle() == eRegular ){
2894                                         gtk_paned_pack1( GTK_PANED( hsplit ), vsplit, TRUE, TRUE );
2895                                         gtk_paned_pack2( GTK_PANED( hsplit ), vsplit2, TRUE, TRUE );
2896                                 }
2897                                 else{
2898                                         gtk_paned_pack2( GTK_PANED( hsplit ), vsplit, TRUE, TRUE );
2899                                         gtk_paned_pack1( GTK_PANED( hsplit ), vsplit2, TRUE, TRUE );
2900                                 }
2901                                 // console
2902                                 GtkWidget* console_window = Console_constructWindow( window );
2903                                 gtk_paned_pack2( GTK_PANED( vsplit ), console_window, TRUE, TRUE );
2904
2905                                 // xy
2906                                 m_pXYWnd = new XYWnd();
2907                                 m_pXYWnd->SetViewType( XY );
2908                                 GtkWidget* xy_window = GTK_WIDGET( create_framed_widget( m_pXYWnd->GetWidget() ) );
2909                                 gtk_paned_pack1( GTK_PANED( vsplit ), xy_window, TRUE, TRUE );
2910                                 {
2911                                         // camera
2912                                         m_pCamWnd = NewCamWnd();
2913                                         GlobalCamera_setCamWnd( *m_pCamWnd );
2914                                         CamWnd_setParent( *m_pCamWnd, window );
2915                                         GtkFrame* camera_window = create_framed_widget( CamWnd_getWidget( *m_pCamWnd ) );
2916
2917                                         gtk_paned_pack1( GTK_PANED( vsplit2 ), GTK_WIDGET( camera_window ), TRUE, TRUE );
2918
2919                                         // textures
2920                                         GtkFrame* texture_window = create_framed_widget( TextureBrowser_constructWindow( window ) );
2921
2922                                         gtk_paned_pack2( GTK_PANED( vsplit2 ), GTK_WIDGET( texture_window ), TRUE, TRUE );
2923                                 }
2924                         }
2925                 }
2926         }
2927         else if ( CurrentStyle() == eFloating ) {
2928                 {
2929                         GtkWindow* window = create_persistent_floating_window( "Camera", m_window );
2930                         global_accel_connect_window( window );
2931                         g_posCamWnd.connect( window );
2932
2933                         gtk_widget_show( GTK_WIDGET( window ) );
2934
2935                         m_pCamWnd = NewCamWnd();
2936                         GlobalCamera_setCamWnd( *m_pCamWnd );
2937
2938                         {
2939                                 GtkFrame* frame = create_framed_widget( CamWnd_getWidget( *m_pCamWnd ) );
2940                                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( frame ) );
2941                         }
2942                         CamWnd_setParent( *m_pCamWnd, window );
2943                         /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
2944                         g_object_set_data( G_OBJECT( window ), "glwidget", CamWnd_getWidget( *m_pCamWnd ) );
2945
2946                         g_floating_windows.push_back( GTK_WIDGET( window ) );
2947                 }
2948
2949                 {
2950                         GtkWindow* window = create_persistent_floating_window( ViewType_getTitle( XY ), m_window );
2951                         global_accel_connect_window( window );
2952                         g_posXYWnd.connect( window );
2953
2954                         m_pXYWnd = new XYWnd();
2955                         m_pXYWnd->m_parent = window;
2956                         m_pXYWnd->SetViewType( XY );
2957
2958
2959                         {
2960                                 GtkFrame* frame = create_framed_widget( m_pXYWnd->GetWidget() );
2961                                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( frame ) );
2962                         }
2963                         XY_Top_Shown_Construct( window );
2964                         /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
2965                         g_object_set_data( G_OBJECT( window ), "glwidget", m_pXYWnd->GetWidget() );
2966
2967                         g_floating_windows.push_back( GTK_WIDGET( window ) );
2968                 }
2969
2970                 {
2971                         GtkWindow* window = create_persistent_floating_window( ViewType_getTitle( XZ ), m_window );
2972                         global_accel_connect_window( window );
2973                         g_posXZWnd.connect( window );
2974
2975                         m_pXZWnd = new XYWnd();
2976                         m_pXZWnd->m_parent = window;
2977                         m_pXZWnd->SetViewType( XZ );
2978
2979                         {
2980                                 GtkFrame* frame = create_framed_widget( m_pXZWnd->GetWidget() );
2981                                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( frame ) );
2982                         }
2983
2984                         XZ_Front_Shown_Construct( window );
2985                         /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
2986                         g_object_set_data( G_OBJECT( window ), "glwidget", m_pXZWnd->GetWidget() );
2987
2988                         g_floating_windows.push_back( GTK_WIDGET( window ) );
2989                 }
2990
2991                 {
2992                         GtkWindow* window = create_persistent_floating_window( ViewType_getTitle( YZ ), m_window );
2993                         global_accel_connect_window( window );
2994                         g_posYZWnd.connect( window );
2995
2996                         m_pYZWnd = new XYWnd();
2997                         m_pYZWnd->m_parent = window;
2998                         m_pYZWnd->SetViewType( YZ );
2999
3000                         {
3001                                 GtkFrame* frame = create_framed_widget( m_pYZWnd->GetWidget() );
3002                                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( frame ) );
3003                         }
3004
3005                         YZ_Side_Shown_Construct( window );
3006                         /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
3007                         g_object_set_data( G_OBJECT( window ), "glwidget", m_pYZWnd->GetWidget() );
3008
3009                         g_floating_windows.push_back( GTK_WIDGET( window ) );
3010                 }
3011
3012                 {
3013                         GtkFrame* frame = create_framed_widget( TextureBrowser_constructWindow( GroupDialog_getWindow() ) );
3014                         g_page_textures = GroupDialog_addPage( "Textures", GTK_WIDGET( frame ), TextureBrowserExportTitleCaller() );
3015                         /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
3016                         g_object_set_data( G_OBJECT( GroupDialog_getWindow() ), "glwidget", TextureBrowser_getGLWidget() );
3017                 }
3018
3019                 m_vSplit = 0;
3020                 m_hSplit = 0;
3021                 m_vSplit2 = 0;
3022
3023                 GroupDialog_show();
3024         }
3025         else // 4 way
3026         {
3027                 m_pCamWnd = NewCamWnd();
3028                 GlobalCamera_setCamWnd( *m_pCamWnd );
3029                 CamWnd_setParent( *m_pCamWnd, window );
3030
3031                 GtkWidget* camera = CamWnd_getWidget( *m_pCamWnd );
3032
3033                 m_pYZWnd = new XYWnd();
3034                 m_pYZWnd->SetViewType( YZ );
3035
3036                 GtkWidget* yz = m_pYZWnd->GetWidget();
3037
3038                 m_pXYWnd = new XYWnd();
3039                 m_pXYWnd->SetViewType( XY );
3040
3041                 GtkWidget* xy = m_pXYWnd->GetWidget();
3042
3043                 m_pXZWnd = new XYWnd();
3044                 m_pXZWnd->SetViewType( XZ );
3045
3046                 GtkWidget* xz = m_pXZWnd->GetWidget();
3047
3048                 m_hSplit = create_split_views( camera, yz, xy, xz, m_vSplit, m_vSplit2 );
3049                 gtk_box_pack_start( GTK_BOX( vbox ), m_hSplit, TRUE, TRUE, 0 );
3050
3051                 {
3052                         GtkFrame* frame = create_framed_widget( TextureBrowser_constructWindow( GroupDialog_getWindow() ) );
3053                         g_page_textures = GroupDialog_addPage( "Textures", GTK_WIDGET( frame ), TextureBrowserExportTitleCaller() );
3054                         /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
3055                         g_object_set_data( G_OBJECT( GroupDialog_getWindow() ), "glwidget", TextureBrowser_getGLWidget() );
3056                 }
3057         }
3058
3059         EntityList_constructWindow( window );
3060         PreferencesDialog_constructWindow( window );
3061         FindTextureDialog_constructWindow( window );
3062         SurfaceInspector_constructWindow( window );
3063         PatchInspector_constructWindow( window );
3064
3065         SetActiveXY( m_pXYWnd );
3066
3067         AddGridChangeCallback( SetGridStatusCaller( *this ) );
3068         AddGridChangeCallback( ReferenceCaller<MainFrame, XY_UpdateAllWindows>( *this ) );
3069
3070         g_defaultToolMode = DragMode;
3071         g_defaultToolMode();
3072         SetStatusText( m_command_status, c_TranslateMode_status );
3073
3074         EverySecondTimer_enable();
3075
3076         if ( g_layout_globals.nState & GDK_WINDOW_STATE_MAXIMIZED ) {
3077                 gtk_window_maximize( window );
3078         }
3079         if ( g_layout_globals.nState & GDK_WINDOW_STATE_FULLSCREEN ) {
3080                 gtk_window_fullscreen( window );
3081         }
3082         if ( !FloatingGroupDialog() ) {
3083                 gtk_paned_set_position( GTK_PANED( m_vSplit ), g_layout_globals.nXYHeight );
3084
3085                 if ( CurrentStyle() == eRegular ) {
3086                         gtk_paned_set_position( GTK_PANED( m_hSplit ), g_layout_globals.nXYWidth );
3087                 }
3088                 else
3089                 {
3090                         gtk_paned_set_position( GTK_PANED( m_hSplit ), g_layout_globals.nCamWidth );
3091                 }
3092
3093                 gtk_paned_set_position( GTK_PANED( m_vSplit2 ), g_layout_globals.nCamHeight );
3094         }
3095         //GlobalShortcuts_reportUnregistered();
3096 }
3097
3098 void MainFrame::SaveWindowInfo(){
3099         if ( !FloatingGroupDialog() ) {
3100                 g_layout_globals.nXYHeight = gtk_paned_get_position( GTK_PANED( m_vSplit ) );
3101
3102                 if ( CurrentStyle() != eRegular ) {
3103                         g_layout_globals.nCamWidth = gtk_paned_get_position( GTK_PANED( m_hSplit ) );
3104                 }
3105                 else
3106                 {
3107                         g_layout_globals.nXYWidth = gtk_paned_get_position( GTK_PANED( m_hSplit ) );
3108                 }
3109
3110                 g_layout_globals.nCamHeight = gtk_paned_get_position( GTK_PANED( m_vSplit2 ) );
3111         }
3112
3113         if( gdk_window_get_state( GTK_WIDGET( m_window )->window ) == 0 ){
3114                 g_layout_globals.m_position = m_position_tracker.getPosition();
3115         }
3116
3117         g_layout_globals.nState = gdk_window_get_state( GTK_WIDGET( m_window )->window );
3118 }
3119
3120 void MainFrame::Shutdown(){
3121         EverySecondTimer_disable();
3122
3123         EntityList_destroyWindow();
3124
3125         delete m_pXYWnd;
3126         m_pXYWnd = 0;
3127         delete m_pYZWnd;
3128         m_pYZWnd = 0;
3129         delete m_pXZWnd;
3130         m_pXZWnd = 0;
3131
3132         TextureBrowser_destroyWindow();
3133
3134         DeleteCamWnd( m_pCamWnd );
3135         m_pCamWnd = 0;
3136
3137         PreferencesDialog_destroyWindow();
3138         SurfaceInspector_destroyWindow();
3139         FindTextureDialog_destroyWindow();
3140         PatchInspector_destroyWindow();
3141
3142         g_DbgDlg.destroyWindow();
3143
3144         // destroying group-dialog last because it may contain texture-browser
3145         GroupDialog_destroyWindow();
3146 }
3147
3148 void MainFrame::RedrawStatusText(){
3149         gtk_label_set_text( GTK_LABEL( m_pStatusLabel[c_command_status] ), m_command_status.c_str() );
3150         gtk_label_set_text( GTK_LABEL( m_pStatusLabel[c_position_status] ), m_position_status.c_str() );
3151         gtk_label_set_text( GTK_LABEL( m_pStatusLabel[c_brushcount_status] ), m_brushcount_status.c_str() );
3152         gtk_label_set_text( GTK_LABEL( m_pStatusLabel[c_texture_status] ), m_texture_status.c_str() );
3153         gtk_label_set_text( GTK_LABEL( m_pStatusLabel[c_grid_status] ), m_grid_status.c_str() );
3154 }
3155
3156 void MainFrame::UpdateStatusText(){
3157         m_idleRedrawStatusText.queueDraw();
3158 }
3159
3160 void MainFrame::SetStatusText( CopiedString& status_text, const char* pText ){
3161         status_text = pText;
3162         UpdateStatusText();
3163 }
3164
3165 void Sys_Status( const char* status ){
3166         if ( g_pParentWnd != 0 ) {
3167                 g_pParentWnd->SetStatusText( g_pParentWnd->m_command_status, status );
3168         }
3169 }
3170
3171 int getRotateIncrement(){
3172         return static_cast<int>( g_si_globals.rotate );
3173 }
3174
3175 int getFarClipDistance(){
3176         return g_camwindow_globals.m_nCubicScale;
3177 }
3178
3179 float ( *GridStatus_getGridSize )() = GetGridSize;
3180 int ( *GridStatus_getRotateIncrement )() = getRotateIncrement;
3181 int ( *GridStatus_getFarClipDistance )() = getFarClipDistance;
3182 bool ( *GridStatus_getTextureLockEnabled )();
3183
3184 void MainFrame::SetGridStatus(){
3185         StringOutputStream status( 64 );
3186         const char* lock = ( GridStatus_getTextureLockEnabled() ) ? "ON" : "OFF";
3187         status << ( GetSnapGridSize() > 0 ? "G:" : "g:" ) << GridStatus_getGridSize()
3188                    << "  R:" << GridStatus_getRotateIncrement()
3189                    << "  C:" << GridStatus_getFarClipDistance()
3190                    << "  L:" << lock;
3191         SetStatusText( m_grid_status, status.c_str() );
3192 }
3193
3194 void GridStatus_onTextureLockEnabledChanged(){
3195         if ( g_pParentWnd != 0 ) {
3196                 g_pParentWnd->SetGridStatus();
3197         }
3198 }
3199
3200 void GlobalGL_sharedContextCreated(){
3201         GLFont *g_font = NULL;
3202
3203         // report OpenGL information
3204         globalOutputStream() << "GL_VENDOR: " << reinterpret_cast<const char*>( glGetString( GL_VENDOR ) ) << "\n";
3205         globalOutputStream() << "GL_RENDERER: " << reinterpret_cast<const char*>( glGetString( GL_RENDERER ) ) << "\n";
3206         globalOutputStream() << "GL_VERSION: " << reinterpret_cast<const char*>( glGetString( GL_VERSION ) ) << "\n";
3207         globalOutputStream() << "GL_EXTENSIONS: " << reinterpret_cast<const char*>( glGetString( GL_EXTENSIONS ) ) << "\n";
3208
3209         QGL_sharedContextCreated( GlobalOpenGL() );
3210
3211         ShaderCache_extensionsInitialised();
3212
3213         GlobalShaderCache().realise();
3214         Textures_Realise();
3215
3216 #ifdef WIN32
3217         /* win32 is dodgy here, just use courier new then */
3218         g_font = glfont_create( "arial 9" );
3219 #else
3220         GtkSettings *settings = gtk_settings_get_default();
3221         gchar *fontname;
3222         g_object_get( settings, "gtk-font-name", &fontname, NULL );
3223         g_font = glfont_create( fontname );
3224 #endif
3225
3226         GlobalOpenGL().m_font = g_font;
3227 }
3228
3229 void GlobalGL_sharedContextDestroyed(){
3230         Textures_Unrealise();
3231         GlobalShaderCache().unrealise();
3232
3233         QGL_sharedContextDestroyed( GlobalOpenGL() );
3234 }
3235
3236
3237 void Layout_constructPreferences( PreferencesPage& page ){
3238         {
3239                 const char* layouts[] = { "window1.png", "window2.png", "window3.png", "window4.png" };
3240                 page.appendRadioIcons(
3241                         "Window Layout",
3242                         STRING_ARRAY_RANGE( layouts ),
3243                         LatchedIntImportCaller( g_Layout_viewStyle ),
3244                         IntExportCaller( g_Layout_viewStyle.m_latched )
3245                         );
3246         }
3247         page.appendCheckBox(
3248                 "", "Detachable Menus",
3249                 LatchedBoolImportCaller( g_Layout_enableDetachableMenus ),
3250                 BoolExportCaller( g_Layout_enableDetachableMenus.m_latched )
3251                 );
3252         if ( !string_empty( g_pGameDescription->getKeyValue( "no_patch" ) ) ) {
3253                 page.appendCheckBox(
3254                         "", "Patch Toolbar",
3255                         LatchedBoolImportCaller( g_Layout_enablePatchToolbar ),
3256                         BoolExportCaller( g_Layout_enablePatchToolbar.m_latched )
3257                         );
3258         }
3259         page.appendCheckBox(
3260                 "", "Plugin Toolbar",
3261                 LatchedBoolImportCaller( g_Layout_enablePluginToolbar ),
3262                 BoolExportCaller( g_Layout_enablePluginToolbar.m_latched )
3263                 );
3264         page.appendCheckBox(
3265                 "", "Filter Toolbar",
3266                 LatchedBoolImportCaller( g_Layout_enableFilterToolbar ),
3267                 BoolExportCaller( g_Layout_enableFilterToolbar.m_latched )
3268                 );
3269 }
3270
3271 void Layout_constructPage( PreferenceGroup& group ){
3272         PreferencesPage page( group.createPage( "Layout", "Layout Preferences" ) );
3273         Layout_constructPreferences( page );
3274 }
3275
3276 void Layout_registerPreferencesPage(){
3277         PreferencesDialog_addInterfacePage( FreeCaller1<PreferenceGroup&, Layout_constructPage>() );
3278 }
3279
3280 void MainFrame_toggleFullscreen(){
3281         GtkWindow* wnd = MainFrame_getWindow();
3282         if( gdk_window_get_state( GTK_WIDGET( wnd )->window ) & GDK_WINDOW_STATE_FULLSCREEN ){
3283                 //some portion of buttsex, because gtk_window_unfullscreen doesn't work correctly after calling some modal window
3284                 bool maximize = ( gdk_window_get_state( GTK_WIDGET( wnd )->window ) & GDK_WINDOW_STATE_MAXIMIZED );
3285                 gtk_window_unfullscreen( wnd );
3286                 if( maximize ){
3287                         gtk_window_unmaximize( wnd );
3288                         gtk_window_maximize( wnd );
3289                 }
3290                 else{
3291                         gtk_window_move( wnd, g_layout_globals.m_position.x, g_layout_globals.m_position.y );
3292                         gtk_window_resize( wnd, g_layout_globals.m_position.w, g_layout_globals.m_position.h );
3293                 }
3294         }
3295         else{
3296                 gtk_window_fullscreen( wnd );
3297         }
3298 }
3299
3300 class MaximizeView
3301 {
3302 public:
3303         MaximizeView(): m_maximized( false ){
3304         }
3305         void toggle(){
3306                 return m_maximized ? restore() : maximize();
3307         }
3308 private:
3309         bool m_maximized;
3310         int m_vSplitPos;
3311         int m_vSplit2Pos;
3312         int m_hSplitPos;
3313
3314         void restore(){
3315                 m_maximized = false;
3316                 gtk_paned_set_position( GTK_PANED( g_pParentWnd->m_vSplit ), m_vSplitPos );
3317                 gtk_paned_set_position( GTK_PANED( g_pParentWnd->m_vSplit2 ), m_vSplit2Pos );
3318                 gtk_paned_set_position( GTK_PANED( g_pParentWnd->m_hSplit ), m_hSplitPos );
3319         }
3320
3321         void maximize(){
3322                 m_maximized = true;
3323                 m_vSplitPos = gtk_paned_get_position( GTK_PANED( g_pParentWnd->m_vSplit ) );
3324                 m_vSplit2Pos = gtk_paned_get_position( GTK_PANED( g_pParentWnd->m_vSplit2 ) );
3325                 m_hSplitPos = gtk_paned_get_position( GTK_PANED( g_pParentWnd->m_hSplit ) );
3326
3327                 int vSplitX, vSplitY, vSplit2X, vSplit2Y, hSplitX, hSplitY;
3328                 gdk_window_get_origin( g_pParentWnd->m_vSplit->window, &vSplitX, &vSplitY );
3329                 gdk_window_get_origin( g_pParentWnd->m_vSplit2->window, &vSplit2X, &vSplit2Y );
3330                 gdk_window_get_origin( g_pParentWnd->m_hSplit->window, &hSplitX, &hSplitY );
3331
3332                 vSplitY += m_vSplitPos;
3333                 vSplit2Y += m_vSplit2Pos;
3334                 hSplitX += m_hSplitPos;
3335
3336                 int cur_x, cur_y;
3337                 Sys_GetCursorPos( MainFrame_getWindow(), &cur_x, &cur_y );
3338
3339                 if( cur_x > hSplitX ){
3340                         gtk_paned_set_position( GTK_PANED( g_pParentWnd->m_hSplit ), 0 );
3341                 }
3342                 else{
3343                         gtk_paned_set_position( GTK_PANED( g_pParentWnd->m_hSplit ), 9999 );
3344                 }
3345                 if( cur_y > vSplitY ){
3346                         gtk_paned_set_position( GTK_PANED( g_pParentWnd->m_vSplit ), 0 );
3347                 }
3348                 else{
3349                         gtk_paned_set_position( GTK_PANED( g_pParentWnd->m_vSplit ), 9999 );
3350                 }
3351                 if( cur_y > vSplit2Y ){
3352                         gtk_paned_set_position( GTK_PANED( g_pParentWnd->m_vSplit2 ), 0 );
3353                 }
3354                 else{
3355                         gtk_paned_set_position( GTK_PANED( g_pParentWnd->m_vSplit2 ), 9999 );
3356                 }
3357         }
3358 };
3359
3360 MaximizeView g_maximizeview;
3361
3362 void Maximize_View(){
3363         if( g_pParentWnd != 0 && g_pParentWnd->m_vSplit != 0 && g_pParentWnd->m_vSplit2 != 0 && g_pParentWnd->m_hSplit != 0 )
3364                 g_maximizeview.toggle();
3365 }
3366
3367
3368 #include "preferencesystem.h"
3369 #include "stringio.h"
3370
3371 void MainFrame_Construct(){
3372         GlobalCommands_insert( "OpenManual", FreeCaller<OpenHelpURL>(), Accelerator( GDK_F1 ) );
3373
3374         GlobalCommands_insert( "Sleep", FreeCaller<thunk_OnSleep>(), Accelerator( 'P', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
3375         GlobalCommands_insert( "NewMap", FreeCaller<NewMap>() );
3376         GlobalCommands_insert( "OpenMap", FreeCaller<OpenMap>(), Accelerator( 'O', (GdkModifierType)GDK_CONTROL_MASK ) );
3377         GlobalCommands_insert( "ImportMap", FreeCaller<ImportMap>() );
3378         GlobalCommands_insert( "SaveMap", FreeCaller<SaveMap>(), Accelerator( 'S', (GdkModifierType)GDK_CONTROL_MASK ) );
3379         GlobalCommands_insert( "SaveMapAs", FreeCaller<SaveMapAs>() );
3380         GlobalCommands_insert( "SaveSelected", FreeCaller<ExportMap>() );
3381         GlobalCommands_insert( "SaveRegion", FreeCaller<SaveRegion>() );
3382         GlobalCommands_insert( "RefreshReferences", FreeCaller<RefreshReferences>() );
3383         GlobalCommands_insert( "ProjectSettings", FreeCaller<DoProjectSettings>() );
3384         GlobalCommands_insert( "CheckForUpdate", FreeCaller<OpenUpdateURL>() );
3385         GlobalCommands_insert( "Exit", FreeCaller<Exit>() );
3386
3387         GlobalCommands_insert( "Undo", FreeCaller<Undo>(), Accelerator( 'Z', (GdkModifierType)GDK_CONTROL_MASK ) );
3388         GlobalCommands_insert( "Redo", FreeCaller<Redo>(), Accelerator( 'Y', (GdkModifierType)GDK_CONTROL_MASK ) );
3389         GlobalCommands_insert( "Copy", FreeCaller<Copy>(), Accelerator( 'C', (GdkModifierType)GDK_CONTROL_MASK ) );
3390         GlobalCommands_insert( "Paste", FreeCaller<Paste>(), Accelerator( 'V', (GdkModifierType)GDK_CONTROL_MASK ) );
3391         GlobalCommands_insert( "PasteToCamera", FreeCaller<PasteToCamera>(), Accelerator( 'V', (GdkModifierType)GDK_SHIFT_MASK ) );
3392         GlobalCommands_insert( "CloneSelection", FreeCaller<Selection_Clone>(), Accelerator( GDK_space ) );
3393         GlobalCommands_insert( "CloneSelectionAndMakeUnique", FreeCaller<Selection_Clone_MakeUnique>(), Accelerator( GDK_space, (GdkModifierType)GDK_SHIFT_MASK ) );
3394 //      GlobalCommands_insert( "DeleteSelection", FreeCaller<deleteSelection>(), Accelerator( GDK_BackSpace ) );
3395         GlobalCommands_insert( "DeleteSelection2", FreeCaller<deleteSelection>(), Accelerator( GDK_BackSpace ) );
3396         GlobalCommands_insert( "DeleteSelection", FreeCaller<deleteSelection>(), Accelerator( 'Z' ) );
3397         GlobalCommands_insert( "ParentSelection", FreeCaller<Scene_parentSelected>() );
3398 //      GlobalCommands_insert( "UnSelectSelection", FreeCaller<Selection_Deselect>(), Accelerator( GDK_Escape ) );
3399         GlobalCommands_insert( "UnSelectSelection2", FreeCaller<Selection_Deselect>(), Accelerator( GDK_Escape ) );
3400         GlobalCommands_insert( "UnSelectSelection", FreeCaller<Selection_Deselect>(), Accelerator( 'C' ) );
3401         GlobalCommands_insert( "InvertSelection", FreeCaller<Select_Invert>(), Accelerator( 'I' ) );
3402         GlobalCommands_insert( "SelectInside", FreeCaller<Select_Inside>() );
3403         GlobalCommands_insert( "SelectTouching", FreeCaller<Select_Touching>() );
3404         GlobalCommands_insert( "ExpandSelectionToEntities", FreeCaller<Scene_ExpandSelectionToEntities>(), Accelerator( 'E', (GdkModifierType)GDK_SHIFT_MASK ) );
3405         GlobalCommands_insert( "Preferences", FreeCaller<PreferencesDialog_showDialog>(), Accelerator( 'P' ) );
3406
3407         GlobalCommands_insert( "ToggleConsole", FreeCaller<Console_ToggleShow>(), Accelerator( 'O' ) );
3408         GlobalCommands_insert( "ToggleEntityInspector", FreeCaller<EntityInspector_ToggleShow>(), Accelerator( 'N' ) );
3409         GlobalCommands_insert( "EntityList", FreeCaller<EntityList_toggleShown>(), Accelerator( 'L' ) );
3410
3411         GlobalCommands_insert( "ShowHidden", FreeCaller<Select_ShowAllHidden>(), Accelerator( 'H', (GdkModifierType)GDK_SHIFT_MASK ) );
3412         GlobalCommands_insert( "HideSelected", FreeCaller<HideSelected>(), Accelerator( 'H' ) );
3413
3414         GlobalToggles_insert( "DragVertices", FreeCaller<SelectVertexMode>(), ToggleItem::AddCallbackCaller( g_vertexMode_button ), Accelerator( 'V' ) );
3415         GlobalToggles_insert( "DragEdges", FreeCaller<SelectEdgeMode>(), ToggleItem::AddCallbackCaller( g_edgeMode_button ), Accelerator( 'E' ) );
3416         GlobalToggles_insert( "DragFaces", FreeCaller<SelectFaceMode>(), ToggleItem::AddCallbackCaller( g_faceMode_button ), Accelerator( 'F' ) );
3417
3418         GlobalCommands_insert( "MirrorSelectionX", FreeCaller<Selection_Flipx>() );
3419         GlobalCommands_insert( "RotateSelectionX", FreeCaller<Selection_Rotatex>() );
3420         GlobalCommands_insert( "MirrorSelectionY", FreeCaller<Selection_Flipy>() );
3421         GlobalCommands_insert( "RotateSelectionY", FreeCaller<Selection_Rotatey>() );
3422         GlobalCommands_insert( "MirrorSelectionZ", FreeCaller<Selection_Flipz>() );
3423         GlobalCommands_insert( "RotateSelectionZ", FreeCaller<Selection_Rotatez>() );
3424
3425         GlobalCommands_insert( "ArbitraryRotation", FreeCaller<DoRotateDlg>(), Accelerator( 'R', (GdkModifierType)GDK_SHIFT_MASK ) );
3426         GlobalCommands_insert( "ArbitraryScale", FreeCaller<DoScaleDlg>(), Accelerator( 'S', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
3427
3428         GlobalCommands_insert( "BuildMenuCustomize", FreeCaller<DoBuildMenu>() );
3429         GlobalCommands_insert( "Build_runRecentExecutedBuild", FreeCaller<Build_runRecentExecutedBuild>(), Accelerator( GDK_F5 ) );
3430
3431         GlobalCommands_insert( "FindBrush", FreeCaller<DoFind>() );
3432
3433         GlobalCommands_insert( "MapInfo", FreeCaller<DoMapInfo>(), Accelerator( 'M' ) );
3434
3435
3436         GlobalToggles_insert( "ToggleClipper", FreeCaller<ClipperMode>(), ToggleItem::AddCallbackCaller( g_clipper_button ), Accelerator( 'X' ) );
3437
3438         GlobalToggles_insert( "MouseTranslate", FreeCaller<TranslateMode>(), ToggleItem::AddCallbackCaller( g_translatemode_button ), Accelerator( 'W' ) );
3439         GlobalToggles_insert( "MouseRotate", FreeCaller<RotateMode>(), ToggleItem::AddCallbackCaller( g_rotatemode_button ), Accelerator( 'R' ) );
3440         GlobalToggles_insert( "MouseScale", FreeCaller<ScaleMode>(), ToggleItem::AddCallbackCaller( g_scalemode_button ) );
3441         GlobalToggles_insert( "MouseDrag", FreeCaller<DragMode>(), ToggleItem::AddCallbackCaller( g_dragmode_button ) );
3442         GlobalCommands_insert( "MouseRotateOrScale", FreeCaller<ToggleRotateScaleModes>() );
3443         GlobalCommands_insert( "MouseDragOrScale", FreeCaller<ToggleDragScaleModes>(), Accelerator( 'Q' ) );
3444
3445         GlobalCommands_insert( "gtkThemeDlg", FreeCaller<gtkThemeDlg>() );
3446         GlobalCommands_insert( "ColorSchemeOriginal", FreeCaller<ColorScheme_Original>() );
3447         GlobalCommands_insert( "ColorSchemeQER", FreeCaller<ColorScheme_QER>() );
3448         GlobalCommands_insert( "ColorSchemeBlackAndGreen", FreeCaller<ColorScheme_Black>() );
3449         GlobalCommands_insert( "ColorSchemeYdnar", FreeCaller<ColorScheme_Ydnar>() );
3450         GlobalCommands_insert( "ChooseTextureBackgroundColor", makeCallback( g_ColoursMenu.m_textureback ) );
3451         GlobalCommands_insert( "ChooseGridBackgroundColor", makeCallback( g_ColoursMenu.m_xyback ) );
3452         GlobalCommands_insert( "ChooseGridMajorColor", makeCallback( g_ColoursMenu.m_gridmajor ) );
3453         GlobalCommands_insert( "ChooseGridMinorColor", makeCallback( g_ColoursMenu.m_gridminor ) );
3454         GlobalCommands_insert( "ChooseSmallGridMajorColor", makeCallback( g_ColoursMenu.m_gridmajor_alt ) );
3455         GlobalCommands_insert( "ChooseSmallGridMinorColor", makeCallback( g_ColoursMenu.m_gridminor_alt ) );
3456         GlobalCommands_insert( "ChooseGridTextColor", makeCallback( g_ColoursMenu.m_gridtext ) );
3457         GlobalCommands_insert( "ChooseGridBlockColor", makeCallback( g_ColoursMenu.m_gridblock ) );
3458         GlobalCommands_insert( "ChooseBrushColor", makeCallback( g_ColoursMenu.m_brush ) );
3459         GlobalCommands_insert( "ChooseCameraBackgroundColor", makeCallback( g_ColoursMenu.m_cameraback ) );
3460         GlobalCommands_insert( "ChooseSelectedBrushColor", makeCallback( g_ColoursMenu.m_selectedbrush ) );
3461         GlobalCommands_insert( "ChooseCameraSelectedBrushColor", makeCallback( g_ColoursMenu.m_selectedbrush3d ) );
3462         GlobalCommands_insert( "ChooseClipperColor", makeCallback( g_ColoursMenu.m_clipper ) );
3463         GlobalCommands_insert( "ChooseOrthoViewNameColor", makeCallback( g_ColoursMenu.m_viewname ) );
3464
3465         GlobalCommands_insert( "Fullscreen", FreeCaller<MainFrame_toggleFullscreen>(), Accelerator( GDK_F11 ) );
3466         GlobalCommands_insert( "MaximizeView", FreeCaller<Maximize_View>(), Accelerator( GDK_F12 ) );
3467
3468
3469         GlobalCommands_insert( "CSGSubtract", FreeCaller<CSG_Subtract>(), Accelerator( 'U', (GdkModifierType)GDK_SHIFT_MASK ) );
3470         GlobalCommands_insert( "CSGMerge", FreeCaller<CSG_Merge>(), Accelerator( 'U', (GdkModifierType)GDK_CONTROL_MASK ) );
3471         GlobalCommands_insert( "CSGroom", FreeCaller<CSG_MakeRoom>() );
3472         GlobalCommands_insert( "CSGTool", FreeCaller<CSG_Tool>() );
3473
3474         Grid_registerCommands();
3475
3476         GlobalCommands_insert( "SnapToGrid", FreeCaller<Selection_SnapToGrid>(), Accelerator( 'G', (GdkModifierType)GDK_CONTROL_MASK ) );
3477
3478         GlobalCommands_insert( "SelectAllOfType", FreeCaller<Select_AllOfType>(), Accelerator( 'A', (GdkModifierType)GDK_SHIFT_MASK ) );
3479
3480         GlobalCommands_insert( "TexRotateClock", FreeCaller<Texdef_RotateClockwise>(), Accelerator( GDK_Next, (GdkModifierType)GDK_SHIFT_MASK ) );
3481         GlobalCommands_insert( "TexRotateCounter", FreeCaller<Texdef_RotateAntiClockwise>(), Accelerator( GDK_Prior, (GdkModifierType)GDK_SHIFT_MASK ) );
3482         GlobalCommands_insert( "TexScaleUp", FreeCaller<Texdef_ScaleUp>(), Accelerator( GDK_Up, (GdkModifierType)GDK_CONTROL_MASK ) );
3483         GlobalCommands_insert( "TexScaleDown", FreeCaller<Texdef_ScaleDown>(), Accelerator( GDK_Down, (GdkModifierType)GDK_CONTROL_MASK ) );
3484         GlobalCommands_insert( "TexScaleLeft", FreeCaller<Texdef_ScaleLeft>(), Accelerator( GDK_Left, (GdkModifierType)GDK_CONTROL_MASK ) );
3485         GlobalCommands_insert( "TexScaleRight", FreeCaller<Texdef_ScaleRight>(), Accelerator( GDK_Right, (GdkModifierType)GDK_CONTROL_MASK ) );
3486         GlobalCommands_insert( "TexShiftUp", FreeCaller<Texdef_ShiftUp>(), Accelerator( GDK_Up, (GdkModifierType)GDK_SHIFT_MASK ) );
3487         GlobalCommands_insert( "TexShiftDown", FreeCaller<Texdef_ShiftDown>(), Accelerator( GDK_Down, (GdkModifierType)GDK_SHIFT_MASK ) );
3488         GlobalCommands_insert( "TexShiftLeft", FreeCaller<Texdef_ShiftLeft>(), Accelerator( GDK_Left, (GdkModifierType)GDK_SHIFT_MASK ) );
3489         GlobalCommands_insert( "TexShiftRight", FreeCaller<Texdef_ShiftRight>(), Accelerator( GDK_Right, (GdkModifierType)GDK_SHIFT_MASK ) );
3490
3491         GlobalCommands_insert( "MoveSelectionDOWN", FreeCaller<Selection_MoveDown>(), Accelerator( GDK_KP_Subtract ) );
3492         GlobalCommands_insert( "MoveSelectionUP", FreeCaller<Selection_MoveUp>(), Accelerator( GDK_KP_Add ) );
3493
3494         GlobalCommands_insert( "SelectNudgeLeft", FreeCaller<Selection_NudgeLeft>(), Accelerator( GDK_Left, (GdkModifierType)GDK_MOD1_MASK ) );
3495         GlobalCommands_insert( "SelectNudgeRight", FreeCaller<Selection_NudgeRight>(), Accelerator( GDK_Right, (GdkModifierType)GDK_MOD1_MASK ) );
3496         GlobalCommands_insert( "SelectNudgeUp", FreeCaller<Selection_NudgeUp>(), Accelerator( GDK_Up, (GdkModifierType)GDK_MOD1_MASK ) );
3497         GlobalCommands_insert( "SelectNudgeDown", FreeCaller<Selection_NudgeDown>(), Accelerator( GDK_Down, (GdkModifierType)GDK_MOD1_MASK ) );
3498
3499         Patch_registerCommands();
3500         XYShow_registerCommands();
3501
3502         typedef FreeCaller1<const Selectable&, ComponentMode_SelectionChanged> ComponentModeSelectionChangedCaller;
3503         GlobalSelectionSystem().addSelectionChangeCallback( ComponentModeSelectionChangedCaller() );
3504
3505         GlobalPreferenceSystem().registerPreference( "DetachableMenus", BoolImportStringCaller( g_Layout_enableDetachableMenus.m_latched ), BoolExportStringCaller( g_Layout_enableDetachableMenus.m_latched ) );
3506         GlobalPreferenceSystem().registerPreference( "PatchToolBar", BoolImportStringCaller( g_Layout_enablePatchToolbar.m_latched ), BoolExportStringCaller( g_Layout_enablePatchToolbar.m_latched ) );
3507         GlobalPreferenceSystem().registerPreference( "PluginToolBar", BoolImportStringCaller( g_Layout_enablePluginToolbar.m_latched ), BoolExportStringCaller( g_Layout_enablePluginToolbar.m_latched ) );
3508         GlobalPreferenceSystem().registerPreference( "FilterToolBar", BoolImportStringCaller( g_Layout_enableFilterToolbar.m_latched ), BoolExportStringCaller( g_Layout_enableFilterToolbar.m_latched ) );
3509         GlobalPreferenceSystem().registerPreference( "QE4StyleWindows", IntImportStringCaller( g_Layout_viewStyle.m_latched ), IntExportStringCaller( g_Layout_viewStyle.m_latched ) );
3510         GlobalPreferenceSystem().registerPreference( "XYHeight", IntImportStringCaller( g_layout_globals.nXYHeight ), IntExportStringCaller( g_layout_globals.nXYHeight ) );
3511         GlobalPreferenceSystem().registerPreference( "XYWidth", IntImportStringCaller( g_layout_globals.nXYWidth ), IntExportStringCaller( g_layout_globals.nXYWidth ) );
3512         GlobalPreferenceSystem().registerPreference( "CamWidth", IntImportStringCaller( g_layout_globals.nCamWidth ), IntExportStringCaller( g_layout_globals.nCamWidth ) );
3513         GlobalPreferenceSystem().registerPreference( "CamHeight", IntImportStringCaller( g_layout_globals.nCamHeight ), IntExportStringCaller( g_layout_globals.nCamHeight ) );
3514
3515         GlobalPreferenceSystem().registerPreference( "State", IntImportStringCaller( g_layout_globals.nState ), IntExportStringCaller( g_layout_globals.nState ) );
3516         GlobalPreferenceSystem().registerPreference( "PositionX", IntImportStringCaller( g_layout_globals.m_position.x ), IntExportStringCaller( g_layout_globals.m_position.x ) );
3517         GlobalPreferenceSystem().registerPreference( "PositionY", IntImportStringCaller( g_layout_globals.m_position.y ), IntExportStringCaller( g_layout_globals.m_position.y ) );
3518         GlobalPreferenceSystem().registerPreference( "Width", IntImportStringCaller( g_layout_globals.m_position.w ), IntExportStringCaller( g_layout_globals.m_position.w ) );
3519         GlobalPreferenceSystem().registerPreference( "Height", IntImportStringCaller( g_layout_globals.m_position.h ), IntExportStringCaller( g_layout_globals.m_position.h ) );
3520
3521         GlobalPreferenceSystem().registerPreference( "CamWnd", WindowPositionTrackerImportStringCaller( g_posCamWnd ), WindowPositionTrackerExportStringCaller( g_posCamWnd ) );
3522         GlobalPreferenceSystem().registerPreference( "XYWnd", WindowPositionTrackerImportStringCaller( g_posXYWnd ), WindowPositionTrackerExportStringCaller( g_posXYWnd ) );
3523         GlobalPreferenceSystem().registerPreference( "YZWnd", WindowPositionTrackerImportStringCaller( g_posYZWnd ), WindowPositionTrackerExportStringCaller( g_posYZWnd ) );
3524         GlobalPreferenceSystem().registerPreference( "XZWnd", WindowPositionTrackerImportStringCaller( g_posXZWnd ), WindowPositionTrackerExportStringCaller( g_posXZWnd ) );
3525
3526         GlobalPreferenceSystem().registerPreference( "EnginePath", CopiedStringImportStringCaller( g_strEnginePath ), CopiedStringExportStringCaller( g_strEnginePath ) );
3527         if ( g_strEnginePath.empty() )
3528         {
3529                 g_strEnginePath_was_empty_1st_start = true;
3530                 const char* ENGINEPATH_ATTRIBUTE =
3531 #if defined( WIN32 )
3532                         "enginepath_win32"
3533 #elif defined( __linux__ ) || defined ( __FreeBSD__ )
3534                         "enginepath_linux"
3535 #elif defined( __APPLE__ )
3536                         "enginepath_macos"
3537 #else
3538 #error "unknown platform"
3539 #endif
3540                 ;
3541                 StringOutputStream path( 256 );
3542                 path << DirectoryCleaned( g_pGameDescription->getRequiredKeyValue( ENGINEPATH_ATTRIBUTE ) );
3543                 g_strEnginePath = path.c_str();
3544                 GlobalPreferenceSystem().registerPreference( "EnginePath", CopiedStringImportStringCaller( g_strEnginePath ), CopiedStringExportStringCaller( g_strEnginePath ) );
3545         }
3546
3547
3548
3549         g_Layout_viewStyle.useLatched();
3550         g_Layout_enableDetachableMenus.useLatched();
3551         g_Layout_enablePatchToolbar.useLatched();
3552         g_Layout_enablePluginToolbar.useLatched();
3553         g_Layout_enableFilterToolbar.useLatched();
3554
3555         Layout_registerPreferencesPage();
3556         Paths_registerPreferencesPage();
3557
3558         g_brushCount.setCountChangedCallback( FreeCaller<QE_brushCountChanged>() );
3559         g_entityCount.setCountChangedCallback( FreeCaller<QE_entityCountChanged>() );
3560         GlobalEntityCreator().setCounter( &g_entityCount );
3561
3562         GLWidget_sharedContextCreated = GlobalGL_sharedContextCreated;
3563         GLWidget_sharedContextDestroyed = GlobalGL_sharedContextDestroyed;
3564
3565         GlobalEntityClassManager().attach( g_WorldspawnColourEntityClassObserver );
3566 }
3567
3568 void MainFrame_Destroy(){
3569         GlobalEntityClassManager().detach( g_WorldspawnColourEntityClassObserver );
3570
3571         GlobalEntityCreator().setCounter( 0 );
3572         g_entityCount.setCountChangedCallback( Callback() );
3573         g_brushCount.setCountChangedCallback( Callback() );
3574 }
3575
3576
3577 void GLWindow_Construct(){
3578         GlobalPreferenceSystem().registerPreference( "MouseButtons", IntImportStringCaller( g_glwindow_globals.m_nMouseType ), IntExportStringCaller( g_glwindow_globals.m_nMouseType ) );
3579 }
3580
3581 void GLWindow_Destroy(){
3582 }