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