]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/mainframe.cpp
it's better to close file and return on non-void function
[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 refresg 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, "Hollow", "selection_makehollow.png", "CSGHollow");
2610 }
2611
2612 void ComponentModes_constructToolbar(ui::Toolbar toolbar)
2613 {
2614     toolbar_append_toggle_button(toolbar, "Select Vertices (V)", "modify_vertices.png", "DragVertices");
2615     toolbar_append_toggle_button(toolbar, "Select Edges (E)", "modify_edges.png", "DragEdges");
2616     toolbar_append_toggle_button(toolbar, "Select Faces (F)", "modify_faces.png", "DragFaces");
2617 }
2618
2619 void Clipper_constructToolbar(ui::Toolbar toolbar)
2620 {
2621
2622     toolbar_append_toggle_button(toolbar, "Clipper (X)", "view_clipper.png", "ToggleClipper");
2623 }
2624
2625 void XYWnd_constructToolbar(ui::Toolbar toolbar)
2626 {
2627     toolbar_append_button(toolbar, "Change views", "view_change.png", "NextView");
2628 }
2629
2630 void Manipulators_constructToolbar(ui::Toolbar toolbar)
2631 {
2632     toolbar_append_toggle_button(toolbar, "Translate (W)", "select_mousetranslate.png", "MouseTranslate");
2633     toolbar_append_toggle_button(toolbar, "Rotate (R)", "select_mouserotate.png", "MouseRotate");
2634     toolbar_append_toggle_button(toolbar, "Scale", "select_mousescale.png", "MouseScale");
2635     toolbar_append_toggle_button(toolbar, "Resize (Q)", "select_mouseresize.png", "MouseDrag");
2636
2637     Clipper_constructToolbar(toolbar);
2638 }
2639
2640 ui::Toolbar create_main_toolbar(MainFrame::EViewStyle style)
2641 {
2642     auto toolbar = ui::Toolbar::from(gtk_toolbar_new());
2643     gtk_orientable_set_orientation(GTK_ORIENTABLE(toolbar), GTK_ORIENTATION_HORIZONTAL);
2644     gtk_toolbar_set_style(toolbar, GTK_TOOLBAR_ICONS);
2645
2646     toolbar.show();
2647
2648     auto space = [&]() {
2649         auto btn = ui::ToolItem::from(gtk_separator_tool_item_new());
2650         btn.show();
2651         toolbar.add(btn);
2652     };
2653
2654     File_constructToolbar(toolbar);
2655
2656     space();
2657
2658     UndoRedo_constructToolbar(toolbar);
2659
2660     space();
2661
2662     RotateFlip_constructToolbar(toolbar);
2663
2664     space();
2665
2666     Select_constructToolbar(toolbar);
2667
2668     space();
2669
2670     CSG_constructToolbar(toolbar);
2671
2672     space();
2673
2674     ComponentModes_constructToolbar(toolbar);
2675
2676     if (style == MainFrame::eRegular || style == MainFrame::eRegularLeft || style == MainFrame::eFloating) {
2677         space();
2678
2679         XYWnd_constructToolbar(toolbar);
2680     }
2681
2682     space();
2683
2684     CamWnd_constructToolbar(toolbar);
2685
2686     space();
2687
2688     Manipulators_constructToolbar(toolbar);
2689
2690     if (g_Layout_enablePatchToolbar.m_value) {
2691         space();
2692
2693         Patch_constructToolbar(toolbar);
2694     }
2695
2696     space();
2697
2698     toolbar_append_toggle_button(toolbar, "Texture Lock (SHIFT +T)", "texture_lock.png", "TogTexLock");
2699
2700     space();
2701
2702     /*auto g_view_entities_button =*/ toolbar_append_button(toolbar, "Entities (N)", "entities.png",
2703                                                             "ToggleEntityInspector");
2704     auto g_view_console_button = toolbar_append_button(toolbar, "Console (O)", "console.png", "ToggleConsole");
2705     auto g_view_textures_button = toolbar_append_button(toolbar, "Texture Browser (T)", "texture_browser.png",
2706                                                         "ToggleTextures");
2707     // TODO: call light inspector
2708     //GtkButton* g_view_lightinspector_button = toolbar_append_button(toolbar, "Light Inspector", "lightinspector.png", "ToggleLightInspector");
2709
2710     space();
2711     /*auto g_refresh_models_button =*/ toolbar_append_button(toolbar, "Refresh Models", "refresh_models.png",
2712                                                              "RefreshReferences");
2713
2714
2715     // disable the console and texture button in the regular layouts
2716     if (style == MainFrame::eRegular || style == MainFrame::eRegularLeft) {
2717         gtk_widget_set_sensitive(g_view_console_button, FALSE);
2718         gtk_widget_set_sensitive(g_view_textures_button, FALSE);
2719     }
2720
2721     return toolbar;
2722 }
2723
2724 ui::Widget create_main_statusbar(ui::Widget pStatusLabel[c_count_status])
2725 {
2726     auto table = ui::Table(1, c_count_status, FALSE);
2727     table.show();
2728
2729     {
2730         auto label = ui::Label("Label");
2731         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
2732         gtk_misc_set_padding(GTK_MISC(label), 4, 2);
2733         label.show();
2734         table.attach(label, {0, 1, 0, 1});
2735         pStatusLabel[c_command_status] = ui::Widget(label);
2736     }
2737
2738     for (unsigned int i = 1; (int) i < c_count_status; ++i) {
2739         auto frame = ui::Frame();
2740         frame.show();
2741         table.attach(frame, {i, i + 1, 0, 1});
2742         gtk_frame_set_shadow_type(frame, GTK_SHADOW_IN);
2743
2744         auto label = ui::Label("Label");
2745         gtk_label_set_ellipsize(label, PANGO_ELLIPSIZE_END);
2746         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
2747         gtk_misc_set_padding(GTK_MISC(label), 4, 2);
2748         label.show();
2749         frame.add(label);
2750         pStatusLabel[i] = ui::Widget(label);
2751     }
2752
2753     return ui::Widget(table);
2754 }
2755
2756 #if 0
2757
2758
2759                                                                                                                         WidgetFocusPrinter g_mainframeWidgetFocusPrinter( "mainframe" );
2760
2761 class WindowFocusPrinter
2762 {
2763 const char* m_name;
2764
2765 static gboolean frame_event( ui::Widget widget, GdkEvent* event, WindowFocusPrinter* self ){
2766         globalOutputStream() << self->m_name << " frame_event\n";
2767         return FALSE;
2768 }
2769 static gboolean keys_changed( ui::Widget widget, WindowFocusPrinter* self ){
2770         globalOutputStream() << self->m_name << " keys_changed\n";
2771         return FALSE;
2772 }
2773 static gboolean notify( ui::Window window, gpointer dummy, WindowFocusPrinter* self ){
2774         if ( gtk_window_is_active( window ) ) {
2775                 globalOutputStream() << self->m_name << " takes toplevel focus\n";
2776         }
2777         else
2778         {
2779                 globalOutputStream() << self->m_name << " loses toplevel focus\n";
2780         }
2781         return FALSE;
2782 }
2783 public:
2784 WindowFocusPrinter( const char* name ) : m_name( name ){
2785 }
2786 void connect( ui::Window toplevel_window ){
2787         toplevel_window.connect( "notify::has_toplevel_focus", G_CALLBACK( notify ), this );
2788         toplevel_window.connect( "notify::is_active", G_CALLBACK( notify ), this );
2789         toplevel_window.connect( "keys_changed", G_CALLBACK( keys_changed ), this );
2790         toplevel_window.connect( "frame_event", G_CALLBACK( frame_event ), this );
2791 }
2792 };
2793
2794 WindowFocusPrinter g_mainframeFocusPrinter( "mainframe" );
2795
2796 #endif
2797
2798 class MainWindowActive {
2799     static gboolean notify(ui::Window window, gpointer dummy, MainWindowActive *self)
2800     {
2801         if (g_wait.m_window && gtk_window_is_active(window) && !g_wait.m_window.visible()) {
2802             g_wait.m_window.show();
2803         }
2804
2805         return FALSE;
2806     }
2807
2808 public:
2809     void connect(ui::Window toplevel_window)
2810     {
2811         toplevel_window.connect("notify::is-active", G_CALLBACK(notify), this);
2812     }
2813 };
2814
2815 MainWindowActive g_MainWindowActive;
2816
2817 SignalHandlerId XYWindowDestroyed_connect(const SignalHandler &handler)
2818 {
2819     return g_pParentWnd->GetXYWnd()->onDestroyed.connectFirst(handler);
2820 }
2821
2822 void XYWindowDestroyed_disconnect(SignalHandlerId id)
2823 {
2824     g_pParentWnd->GetXYWnd()->onDestroyed.disconnect(id);
2825 }
2826
2827 MouseEventHandlerId XYWindowMouseDown_connect(const MouseEventHandler &handler)
2828 {
2829     return g_pParentWnd->GetXYWnd()->onMouseDown.connectFirst(handler);
2830 }
2831
2832 void XYWindowMouseDown_disconnect(MouseEventHandlerId id)
2833 {
2834     g_pParentWnd->GetXYWnd()->onMouseDown.disconnect(id);
2835 }
2836
2837 // =============================================================================
2838 // MainFrame class
2839
2840 MainFrame *g_pParentWnd = 0;
2841
2842 ui::Window MainFrame_getWindow()
2843 {
2844     return g_pParentWnd ? g_pParentWnd->m_window : ui::Window{ui::null};
2845 }
2846
2847 std::vector<ui::Widget> g_floating_windows;
2848
2849 MainFrame::MainFrame() : m_idleRedrawStatusText(RedrawStatusTextCaller(*this))
2850 {
2851     m_pXYWnd = 0;
2852     m_pCamWnd = 0;
2853     m_pZWnd = 0;
2854     m_pYZWnd = 0;
2855     m_pXZWnd = 0;
2856     m_pActiveXY = 0;
2857
2858     for (auto &n : m_pStatusLabel) {
2859         n = NULL;
2860     }
2861
2862     m_bSleeping = false;
2863
2864     Create();
2865 }
2866
2867 MainFrame::~MainFrame()
2868 {
2869     SaveWindowInfo();
2870
2871     m_window.hide();
2872
2873     Shutdown();
2874
2875     for (std::vector<ui::Widget>::iterator i = g_floating_windows.begin(); i != g_floating_windows.end(); ++i) {
2876         i->destroy();
2877     }
2878
2879     m_window.destroy();
2880 }
2881
2882 void MainFrame::SetActiveXY(XYWnd *p)
2883 {
2884     if (m_pActiveXY) {
2885         m_pActiveXY->SetActive(false);
2886     }
2887
2888     m_pActiveXY = p;
2889
2890     if (m_pActiveXY) {
2891         m_pActiveXY->SetActive(true);
2892     }
2893
2894 }
2895
2896 void MainFrame::ReleaseContexts()
2897 {
2898 #if 0
2899                                                                                                                             if ( m_pXYWnd ) {
2900                 m_pXYWnd->DestroyContext();
2901         }
2902         if ( m_pYZWnd ) {
2903                 m_pYZWnd->DestroyContext();
2904         }
2905         if ( m_pXZWnd ) {
2906                 m_pXZWnd->DestroyContext();
2907         }
2908         if ( m_pCamWnd ) {
2909                 m_pCamWnd->DestroyContext();
2910         }
2911         if ( m_pTexWnd ) {
2912                 m_pTexWnd->DestroyContext();
2913         }
2914         if ( m_pZWnd ) {
2915                 m_pZWnd->DestroyContext();
2916         }
2917 #endif
2918 }
2919
2920 void MainFrame::CreateContexts()
2921 {
2922 #if 0
2923                                                                                                                             if ( m_pCamWnd ) {
2924                 m_pCamWnd->CreateContext();
2925         }
2926         if ( m_pXYWnd ) {
2927                 m_pXYWnd->CreateContext();
2928         }
2929         if ( m_pYZWnd ) {
2930                 m_pYZWnd->CreateContext();
2931         }
2932         if ( m_pXZWnd ) {
2933                 m_pXZWnd->CreateContext();
2934         }
2935         if ( m_pTexWnd ) {
2936                 m_pTexWnd->CreateContext();
2937         }
2938         if ( m_pZWnd ) {
2939                 m_pZWnd->CreateContext();
2940         }
2941 #endif
2942 }
2943
2944 #if GDEF_DEBUG
2945 //#define DBG_SLEEP
2946 #endif
2947
2948 void MainFrame::OnSleep()
2949 {
2950 #if 0
2951                                                                                                                             m_bSleeping ^= 1;
2952         if ( m_bSleeping ) {
2953                 // useful when trying to debug crashes in the sleep code
2954                 globalOutputStream() << "Going into sleep mode..\n";
2955
2956                 globalOutputStream() << "Dispatching sleep msg...";
2957                 DispatchRadiantMsg( RADIANT_SLEEP );
2958                 globalOutputStream() << "Done.\n";
2959
2960                 gtk_window_iconify( m_window );
2961                 GlobalSelectionSystem().setSelectedAll( false );
2962
2963                 GlobalShaderCache().unrealise();
2964                 Shaders_Free();
2965                 GlobalOpenGL_debugAssertNoErrors();
2966                 ScreenUpdates_Disable();
2967
2968                 // release contexts
2969                 globalOutputStream() << "Releasing contexts...";
2970                 ReleaseContexts();
2971                 globalOutputStream() << "Done.\n";
2972         }
2973         else
2974         {
2975                 globalOutputStream() << "Waking up\n";
2976
2977                 gtk_window_deiconify( m_window );
2978
2979                 // create contexts
2980                 globalOutputStream() << "Creating contexts...";
2981                 CreateContexts();
2982                 globalOutputStream() << "Done.\n";
2983
2984                 globalOutputStream() << "Making current on camera...";
2985                 m_pCamWnd->MakeCurrent();
2986                 globalOutputStream() << "Done.\n";
2987
2988                 globalOutputStream() << "Reloading shaders...";
2989                 Shaders_Load();
2990                 GlobalShaderCache().realise();
2991                 globalOutputStream() << "Done.\n";
2992
2993                 ScreenUpdates_Enable();
2994
2995                 globalOutputStream() << "Dispatching wake msg...";
2996                 DispatchRadiantMsg( RADIANT_WAKEUP );
2997                 globalOutputStream() << "Done\n";
2998         }
2999 #endif
3000 }
3001
3002
3003 ui::Window create_splash()
3004 {
3005     auto window = ui::Window(ui::window_type::TOP);
3006     gtk_window_set_decorated(window, false);
3007     gtk_window_set_resizable(window, false);
3008     gtk_window_set_modal(window, true);
3009     gtk_window_set_default_size(window, -1, -1);
3010     gtk_window_set_position(window, GTK_WIN_POS_CENTER);
3011     gtk_container_set_border_width(window, 0);
3012
3013     auto image = new_local_image("splash.png");
3014     image.show();
3015     window.add(image);
3016
3017     window.dimensions(-1, -1);
3018     window.show();
3019
3020     return window;
3021 }
3022
3023 static ui::Window splash_screen{ui::null};
3024
3025 void show_splash()
3026 {
3027     splash_screen = create_splash();
3028
3029     ui::process();
3030 }
3031
3032 void hide_splash()
3033 {
3034     splash_screen.destroy();
3035 }
3036
3037 WindowPositionTracker g_posCamWnd;
3038 WindowPositionTracker g_posXYWnd;
3039 WindowPositionTracker g_posXZWnd;
3040 WindowPositionTracker g_posYZWnd;
3041
3042 static gint mainframe_delete(ui::Widget widget, GdkEvent *event, gpointer data)
3043 {
3044     if (ConfirmModified("Exit Radiant")) {
3045         gtk_main_quit();
3046     }
3047
3048     return TRUE;
3049 }
3050
3051 void MainFrame::Create()
3052 {
3053     ui::Window window = ui::Window(ui::window_type::TOP);
3054
3055     GlobalWindowObservers_connectTopLevel(window);
3056
3057     gtk_window_set_transient_for(splash_screen, window);
3058
3059 #if !GDEF_OS_WINDOWS
3060     {
3061         GdkPixbuf *pixbuf = pixbuf_new_from_file_with_mask("bitmaps/icon.png");
3062         if (pixbuf != 0) {
3063             gtk_window_set_icon(window, pixbuf);
3064             g_object_unref(pixbuf);
3065         }
3066     }
3067 #endif
3068
3069     gtk_widget_add_events(window, GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_FOCUS_CHANGE_MASK);
3070     window.connect("delete_event", G_CALLBACK(mainframe_delete), this);
3071
3072     m_position_tracker.connect(window);
3073
3074 #if 0
3075                                                                                                                             g_mainframeWidgetFocusPrinter.connect( window );
3076         g_mainframeFocusPrinter.connect( window );
3077 #endif
3078
3079     g_MainWindowActive.connect(window);
3080
3081     GetPlugInMgr().Init(window);
3082
3083     auto vbox = ui::VBox(FALSE, 0);
3084     window.add(vbox);
3085     vbox.show();
3086
3087     global_accel_connect_window(window);
3088
3089     m_nCurrentStyle = (EViewStyle) g_Layout_viewStyle.m_value;
3090
3091     register_shortcuts();
3092
3093     auto main_menu = create_main_menu(CurrentStyle());
3094     vbox.pack_start(main_menu, FALSE, FALSE, 0);
3095
3096     auto main_toolbar = create_main_toolbar(CurrentStyle());
3097     vbox.pack_start(main_toolbar, FALSE, FALSE, 0);
3098
3099     auto plugin_toolbar = create_plugin_toolbar();
3100     if (!g_Layout_enablePluginToolbar.m_value) {
3101         plugin_toolbar.hide();
3102     }
3103     vbox.pack_start(plugin_toolbar, FALSE, FALSE, 0);
3104
3105     ui::Widget main_statusbar = create_main_statusbar(reinterpret_cast<ui::Widget *>(m_pStatusLabel));
3106     vbox.pack_end(main_statusbar, FALSE, TRUE, 2);
3107
3108     GroupDialog_constructWindow(window);
3109     g_page_entity = GroupDialog_addPage("Entities", EntityInspector_constructWindow(GroupDialog_getWindow()),
3110                                         RawStringExportCaller("Entities"));
3111
3112     if (FloatingGroupDialog()) {
3113         g_page_console = GroupDialog_addPage("Console", Console_constructWindow(GroupDialog_getWindow()),
3114                                              RawStringExportCaller("Console"));
3115     }
3116
3117 #if GDEF_OS_WINDOWS
3118                                                                                                                             if ( g_multimon_globals.m_bStartOnPrimMon ) {
3119                 PositionWindowOnPrimaryScreen( g_layout_globals.m_position );
3120                 window_set_position( window, g_layout_globals.m_position );
3121         }
3122         else
3123 #endif
3124     if (g_layout_globals.nState & GDK_WINDOW_STATE_MAXIMIZED) {
3125         gtk_window_maximize(window);
3126         WindowPosition default_position(-1, -1, 640, 480);
3127         window_set_position(window, default_position);
3128     } else {
3129         window_set_position(window, g_layout_globals.m_position);
3130     }
3131
3132     m_window = window;
3133
3134     window.show();
3135
3136     if (CurrentStyle() == eRegular || CurrentStyle() == eRegularLeft) {
3137         {
3138             ui::Widget vsplit = ui::VPaned(ui::New);
3139             m_vSplit = vsplit;
3140             vbox.pack_start(vsplit, TRUE, TRUE, 0);
3141             vsplit.show();
3142
3143             // console
3144             ui::Widget console_window = Console_constructWindow(window);
3145             gtk_paned_pack2(GTK_PANED(vsplit), console_window, FALSE, TRUE);
3146
3147             {
3148                 ui::Widget hsplit = ui::HPaned(ui::New);
3149                 hsplit.show();
3150                 m_hSplit = hsplit;
3151                 gtk_paned_add1(GTK_PANED(vsplit), hsplit);
3152
3153                 // xy
3154                 m_pXYWnd = new XYWnd();
3155                 m_pXYWnd->SetViewType(XY);
3156                 ui::Widget xy_window = ui::Widget(create_framed_widget(m_pXYWnd->GetWidget()));
3157
3158                 {
3159                     ui::Widget vsplit2 = ui::VPaned(ui::New);
3160                     vsplit2.show();
3161                     m_vSplit2 = vsplit2;
3162
3163                     if (CurrentStyle() == eRegular) {
3164                         gtk_paned_add1(GTK_PANED(hsplit), xy_window);
3165                         gtk_paned_add2(GTK_PANED(hsplit), vsplit2);
3166                     } else {
3167                         gtk_paned_add1(GTK_PANED(hsplit), vsplit2);
3168                         gtk_paned_add2(GTK_PANED(hsplit), xy_window);
3169                     }
3170
3171
3172                     // camera
3173                     m_pCamWnd = NewCamWnd();
3174                     GlobalCamera_setCamWnd(*m_pCamWnd);
3175                     CamWnd_setParent(*m_pCamWnd, window);
3176                     auto camera_window = create_framed_widget(CamWnd_getWidget(*m_pCamWnd));
3177
3178                     gtk_paned_add1(GTK_PANED(vsplit2), camera_window);
3179
3180                     // textures
3181                     auto texture_window = create_framed_widget(TextureBrowser_constructWindow(window));
3182
3183                     gtk_paned_add2(GTK_PANED(vsplit2), texture_window);
3184                 }
3185             }
3186         }
3187
3188         gtk_paned_set_position(GTK_PANED(m_vSplit), g_layout_globals.nXYHeight);
3189
3190         if (CurrentStyle() == eRegular) {
3191             gtk_paned_set_position(GTK_PANED(m_hSplit), g_layout_globals.nXYWidth);
3192         } else {
3193             gtk_paned_set_position(GTK_PANED(m_hSplit), g_layout_globals.nCamWidth);
3194         }
3195
3196         gtk_paned_set_position(GTK_PANED(m_vSplit2), g_layout_globals.nCamHeight);
3197     } else if (CurrentStyle() == eFloating) {
3198         {
3199             ui::Window window = ui::Window(create_persistent_floating_window("Camera", m_window));
3200             global_accel_connect_window(window);
3201             g_posCamWnd.connect(window);
3202
3203             window.show();
3204
3205             m_pCamWnd = NewCamWnd();
3206             GlobalCamera_setCamWnd(*m_pCamWnd);
3207
3208             {
3209                 auto frame = create_framed_widget(CamWnd_getWidget(*m_pCamWnd));
3210                 window.add(frame);
3211             }
3212             CamWnd_setParent(*m_pCamWnd, window);
3213
3214             g_floating_windows.push_back(window);
3215         }
3216
3217         {
3218             ui::Window window = ui::Window(create_persistent_floating_window(ViewType_getTitle(XY), m_window));
3219             global_accel_connect_window(window);
3220             g_posXYWnd.connect(window);
3221
3222             m_pXYWnd = new XYWnd();
3223             m_pXYWnd->m_parent = window;
3224             m_pXYWnd->SetViewType(XY);
3225
3226
3227             {
3228                 auto frame = create_framed_widget(m_pXYWnd->GetWidget());
3229                 window.add(frame);
3230             }
3231             XY_Top_Shown_Construct(window);
3232
3233             g_floating_windows.push_back(window);
3234         }
3235
3236         {
3237             ui::Window window = ui::Window(create_persistent_floating_window(ViewType_getTitle(XZ), m_window));
3238             global_accel_connect_window(window);
3239             g_posXZWnd.connect(window);
3240
3241             m_pXZWnd = new XYWnd();
3242             m_pXZWnd->m_parent = window;
3243             m_pXZWnd->SetViewType(XZ);
3244
3245             {
3246                 auto frame = create_framed_widget(m_pXZWnd->GetWidget());
3247                 window.add(frame);
3248             }
3249
3250             XZ_Front_Shown_Construct(window);
3251
3252             g_floating_windows.push_back(window);
3253         }
3254
3255         {
3256             ui::Window window = ui::Window(create_persistent_floating_window(ViewType_getTitle(YZ), m_window));
3257             global_accel_connect_window(window);
3258             g_posYZWnd.connect(window);
3259
3260             m_pYZWnd = new XYWnd();
3261             m_pYZWnd->m_parent = window;
3262             m_pYZWnd->SetViewType(YZ);
3263
3264             {
3265                 auto frame = create_framed_widget(m_pYZWnd->GetWidget());
3266                 window.add(frame);
3267             }
3268
3269             YZ_Side_Shown_Construct(window);
3270
3271             g_floating_windows.push_back(window);
3272         }
3273
3274         {
3275             auto frame = create_framed_widget(TextureBrowser_constructWindow(GroupDialog_getWindow()));
3276             g_page_textures = GroupDialog_addPage("Textures", frame, TextureBrowserExportTitleCaller());
3277         }
3278
3279         GroupDialog_show();
3280     } else // 4 way
3281     {
3282         m_pCamWnd = NewCamWnd();
3283         GlobalCamera_setCamWnd(*m_pCamWnd);
3284         CamWnd_setParent(*m_pCamWnd, window);
3285
3286         ui::Widget camera = CamWnd_getWidget(*m_pCamWnd);
3287
3288         m_pYZWnd = new XYWnd();
3289         m_pYZWnd->SetViewType(YZ);
3290
3291         ui::Widget yz = m_pYZWnd->GetWidget();
3292
3293         m_pXYWnd = new XYWnd();
3294         m_pXYWnd->SetViewType(XY);
3295
3296         ui::Widget xy = m_pXYWnd->GetWidget();
3297
3298         m_pXZWnd = new XYWnd();
3299         m_pXZWnd->SetViewType(XZ);
3300
3301         ui::Widget xz = m_pXZWnd->GetWidget();
3302
3303         auto split = create_split_views(camera, yz, xy, xz);
3304         vbox.pack_start(split, TRUE, TRUE, 0);
3305
3306         {
3307             auto frame = create_framed_widget(TextureBrowser_constructWindow(window));
3308             g_page_textures = GroupDialog_addPage("Textures", frame, TextureBrowserExportTitleCaller());
3309         }
3310     }
3311
3312     EntityList_constructWindow(window);
3313     PreferencesDialog_constructWindow(window);
3314     FindTextureDialog_constructWindow(window);
3315     SurfaceInspector_constructWindow(window);
3316     PatchInspector_constructWindow(window);
3317
3318     SetActiveXY(m_pXYWnd);
3319
3320     AddGridChangeCallback(SetGridStatusCaller(*this));
3321     AddGridChangeCallback(ReferenceCaller<MainFrame, void(), XY_UpdateAllWindows>(*this));
3322
3323     g_defaultToolMode = DragMode;
3324     g_defaultToolMode();
3325     SetStatusText(m_command_status, c_TranslateMode_status);
3326
3327     EverySecondTimer_enable();
3328
3329     //GlobalShortcuts_reportUnregistered();
3330 }
3331
3332 void MainFrame::SaveWindowInfo()
3333 {
3334     if (!FloatingGroupDialog()) {
3335         g_layout_globals.nXYHeight = gtk_paned_get_position(GTK_PANED(m_vSplit));
3336
3337         if (CurrentStyle() != eRegular) {
3338             g_layout_globals.nCamWidth = gtk_paned_get_position(GTK_PANED(m_hSplit));
3339         } else {
3340             g_layout_globals.nXYWidth = gtk_paned_get_position(GTK_PANED(m_hSplit));
3341         }
3342
3343         g_layout_globals.nCamHeight = gtk_paned_get_position(GTK_PANED(m_vSplit2));
3344     }
3345
3346     g_layout_globals.m_position = m_position_tracker.getPosition();
3347
3348     g_layout_globals.nState = gdk_window_get_state(gtk_widget_get_window(m_window));
3349 }
3350
3351 void MainFrame::Shutdown()
3352 {
3353     EverySecondTimer_disable();
3354
3355     EntityList_destroyWindow();
3356
3357     delete m_pXYWnd;
3358     m_pXYWnd = 0;
3359     delete m_pYZWnd;
3360     m_pYZWnd = 0;
3361     delete m_pXZWnd;
3362     m_pXZWnd = 0;
3363
3364     TextureBrowser_destroyWindow();
3365
3366     DeleteCamWnd(m_pCamWnd);
3367     m_pCamWnd = 0;
3368
3369     PreferencesDialog_destroyWindow();
3370     SurfaceInspector_destroyWindow();
3371     FindTextureDialog_destroyWindow();
3372     PatchInspector_destroyWindow();
3373
3374     g_DbgDlg.destroyWindow();
3375
3376     // destroying group-dialog last because it may contain texture-browser
3377     GroupDialog_destroyWindow();
3378 }
3379
3380 void MainFrame::RedrawStatusText()
3381 {
3382     ui::Label::from(m_pStatusLabel[c_command_status]).text(m_command_status.c_str());
3383     ui::Label::from(m_pStatusLabel[c_position_status]).text(m_position_status.c_str());
3384     ui::Label::from(m_pStatusLabel[c_brushcount_status]).text(m_brushcount_status.c_str());
3385     ui::Label::from(m_pStatusLabel[c_texture_status]).text(m_texture_status.c_str());
3386     ui::Label::from(m_pStatusLabel[c_grid_status]).text(m_grid_status.c_str());
3387 }
3388
3389 void MainFrame::UpdateStatusText()
3390 {
3391     m_idleRedrawStatusText.queueDraw();
3392 }
3393
3394 void MainFrame::SetStatusText(CopiedString &status_text, const char *pText)
3395 {
3396     status_text = pText;
3397     UpdateStatusText();
3398 }
3399
3400 void Sys_Status(const char *status)
3401 {
3402     if (g_pParentWnd != 0) {
3403         g_pParentWnd->SetStatusText(g_pParentWnd->m_command_status, status);
3404     }
3405 }
3406
3407 int getRotateIncrement()
3408 {
3409     return static_cast<int>( g_si_globals.rotate );
3410 }
3411
3412 int getFarClipDistance()
3413 {
3414     return g_camwindow_globals.m_nCubicScale;
3415 }
3416
3417 float ( *GridStatus_getGridSize )() = GetGridSize;
3418
3419 int ( *GridStatus_getRotateIncrement )() = getRotateIncrement;
3420
3421 int ( *GridStatus_getFarClipDistance )() = getFarClipDistance;
3422
3423 bool ( *GridStatus_getTextureLockEnabled )();
3424
3425 void MainFrame::SetGridStatus()
3426 {
3427     StringOutputStream status(64);
3428     const char *lock = (GridStatus_getTextureLockEnabled()) ? "ON" : "OFF";
3429     status << (GetSnapGridSize() > 0 ? "G:" : "g:") << GridStatus_getGridSize()
3430            << "  R:" << GridStatus_getRotateIncrement()
3431            << "  C:" << GridStatus_getFarClipDistance()
3432            << "  L:" << lock;
3433     SetStatusText(m_grid_status, status.c_str());
3434 }
3435
3436 void GridStatus_onTextureLockEnabledChanged()
3437 {
3438     if (g_pParentWnd != 0) {
3439         g_pParentWnd->SetGridStatus();
3440     }
3441 }
3442
3443 void GlobalGL_sharedContextCreated()
3444 {
3445     GLFont *g_font = NULL;
3446
3447     // report OpenGL information
3448     globalOutputStream() << "GL_VENDOR: " << reinterpret_cast<const char *>( glGetString(GL_VENDOR)) << "\n";
3449     globalOutputStream() << "GL_RENDERER: " << reinterpret_cast<const char *>( glGetString(GL_RENDERER)) << "\n";
3450     globalOutputStream() << "GL_VERSION: " << reinterpret_cast<const char *>( glGetString(GL_VERSION)) << "\n";
3451     const auto extensions = reinterpret_cast<const char *>( glGetString(GL_EXTENSIONS));
3452     globalOutputStream() << "GL_EXTENSIONS: " << (extensions ? extensions : "") << "\n";
3453
3454     QGL_sharedContextCreated(GlobalOpenGL());
3455
3456     ShaderCache_extensionsInitialised();
3457
3458     GlobalShaderCache().realise();
3459     Textures_Realise();
3460
3461 #if GDEF_OS_WINDOWS
3462                                                                                                                             /* win32 is dodgy here, just use courier new then */
3463         g_font = glfont_create( "arial 9" );
3464 #else
3465     auto settings = gtk_settings_get_default();
3466     gchar *fontname;
3467     g_object_get(settings, "gtk-font-name", &fontname, NULL);
3468     g_font = glfont_create(fontname);
3469 #endif
3470
3471     GlobalOpenGL().m_font = g_font;
3472 }
3473
3474 void GlobalGL_sharedContextDestroyed()
3475 {
3476     Textures_Unrealise();
3477     GlobalShaderCache().unrealise();
3478
3479     QGL_sharedContextDestroyed(GlobalOpenGL());
3480 }
3481
3482
3483 void Layout_constructPreferences(PreferencesPage &page)
3484 {
3485     {
3486         const char *layouts[] = {"window1.png", "window2.png", "window3.png", "window4.png"};
3487         page.appendRadioIcons(
3488                 "Window Layout",
3489                 STRING_ARRAY_RANGE(layouts),
3490                 make_property(g_Layout_viewStyle)
3491         );
3492     }
3493     page.appendCheckBox(
3494             "", "Detachable Menus",
3495             make_property(g_Layout_enableDetachableMenus)
3496     );
3497     if (!string_empty(g_pGameDescription->getKeyValue("no_patch"))) {
3498         page.appendCheckBox(
3499                 "", "Patch Toolbar",
3500                 make_property(g_Layout_enablePatchToolbar)
3501         );
3502     }
3503     page.appendCheckBox(
3504             "", "Plugin Toolbar",
3505             make_property(g_Layout_enablePluginToolbar)
3506     );
3507 }
3508
3509 void Layout_constructPage(PreferenceGroup &group)
3510 {
3511     PreferencesPage page(group.createPage("Layout", "Layout Preferences"));
3512     Layout_constructPreferences(page);
3513 }
3514
3515 void Layout_registerPreferencesPage()
3516 {
3517     PreferencesDialog_addInterfacePage(makeCallbackF(Layout_constructPage));
3518 }
3519
3520
3521 #include "preferencesystem.h"
3522 #include "stringio.h"
3523
3524 void MainFrame_Construct()
3525 {
3526     GlobalCommands_insert("OpenManual", makeCallbackF(OpenHelpURL), Accelerator(GDK_KEY_F1));
3527
3528     GlobalCommands_insert("Sleep", makeCallbackF(thunk_OnSleep),
3529                           Accelerator('P', (GdkModifierType) (GDK_SHIFT_MASK | GDK_CONTROL_MASK)));
3530     GlobalCommands_insert("NewMap", makeCallbackF(NewMap));
3531     GlobalCommands_insert("OpenMap", makeCallbackF(OpenMap), Accelerator('O', (GdkModifierType) GDK_CONTROL_MASK));
3532     GlobalCommands_insert("ImportMap", makeCallbackF(ImportMap));
3533     GlobalCommands_insert("SaveMap", makeCallbackF(SaveMap), Accelerator('S', (GdkModifierType) GDK_CONTROL_MASK));
3534     GlobalCommands_insert("SaveMapAs", makeCallbackF(SaveMapAs));
3535     GlobalCommands_insert("ExportSelected", makeCallbackF(ExportMap));
3536     GlobalCommands_insert("SaveRegion", makeCallbackF(SaveRegion));
3537     GlobalCommands_insert("RefreshReferences", makeCallbackF(VFS_Refresh));
3538     GlobalCommands_insert("ProjectSettings", makeCallbackF(DoProjectSettings));
3539     GlobalCommands_insert("Exit", makeCallbackF(Exit));
3540
3541     GlobalCommands_insert("Undo", makeCallbackF(Undo), Accelerator('Z', (GdkModifierType) GDK_CONTROL_MASK));
3542     GlobalCommands_insert("Redo", makeCallbackF(Redo), Accelerator('Y', (GdkModifierType) GDK_CONTROL_MASK));
3543     GlobalCommands_insert("Copy", makeCallbackF(Copy), Accelerator('C', (GdkModifierType) GDK_CONTROL_MASK));
3544     GlobalCommands_insert("Paste", makeCallbackF(Paste), Accelerator('V', (GdkModifierType) GDK_CONTROL_MASK));
3545     GlobalCommands_insert("PasteToCamera", makeCallbackF(PasteToCamera),
3546                           Accelerator('V', (GdkModifierType) GDK_MOD1_MASK));
3547     GlobalCommands_insert("CloneSelection", makeCallbackF(Selection_Clone), Accelerator(GDK_KEY_space));
3548     GlobalCommands_insert("CloneSelectionAndMakeUnique", makeCallbackF(Selection_Clone_MakeUnique),
3549                           Accelerator(GDK_KEY_space, (GdkModifierType) GDK_SHIFT_MASK));
3550     GlobalCommands_insert("DeleteSelection", makeCallbackF(deleteSelection), Accelerator(GDK_KEY_BackSpace));
3551     GlobalCommands_insert("ParentSelection", makeCallbackF(Scene_parentSelected));
3552     GlobalCommands_insert("UnSelectSelection", makeCallbackF(Selection_Deselect), Accelerator(GDK_KEY_Escape));
3553     GlobalCommands_insert("InvertSelection", makeCallbackF(Select_Invert), Accelerator('I'));
3554     GlobalCommands_insert("SelectInside", makeCallbackF(Select_Inside));
3555     GlobalCommands_insert("SelectTouching", makeCallbackF(Select_Touching));
3556     GlobalCommands_insert("ExpandSelectionToEntities", makeCallbackF(Scene_ExpandSelectionToEntities),
3557                           Accelerator('E', (GdkModifierType) (GDK_MOD1_MASK | GDK_CONTROL_MASK)));
3558     GlobalCommands_insert("Preferences", makeCallbackF(PreferencesDialog_showDialog), Accelerator('P'));
3559
3560     GlobalCommands_insert("ToggleConsole", makeCallbackF(Console_ToggleShow), Accelerator('O'));
3561     GlobalCommands_insert("ToggleEntityInspector", makeCallbackF(EntityInspector_ToggleShow), Accelerator('N'));
3562     GlobalCommands_insert("EntityList", makeCallbackF(EntityList_toggleShown), Accelerator('L'));
3563
3564     GlobalCommands_insert("ShowHidden", makeCallbackF(Select_ShowAllHidden),
3565                           Accelerator('H', (GdkModifierType) GDK_SHIFT_MASK));
3566     GlobalCommands_insert("HideSelected", makeCallbackF(HideSelected), Accelerator('H'));
3567
3568     GlobalToggles_insert("DragVertices", makeCallbackF(SelectVertexMode),
3569                          ToggleItem::AddCallbackCaller(g_vertexMode_button), Accelerator('V'));
3570     GlobalToggles_insert("DragEdges", makeCallbackF(SelectEdgeMode), ToggleItem::AddCallbackCaller(g_edgeMode_button),
3571                          Accelerator('E'));
3572     GlobalToggles_insert("DragFaces", makeCallbackF(SelectFaceMode), ToggleItem::AddCallbackCaller(g_faceMode_button),
3573                          Accelerator('F'));
3574
3575     GlobalCommands_insert("MirrorSelectionX", makeCallbackF(Selection_Flipx));
3576     GlobalCommands_insert("RotateSelectionX", makeCallbackF(Selection_Rotatex));
3577     GlobalCommands_insert("MirrorSelectionY", makeCallbackF(Selection_Flipy));
3578     GlobalCommands_insert("RotateSelectionY", makeCallbackF(Selection_Rotatey));
3579     GlobalCommands_insert("MirrorSelectionZ", makeCallbackF(Selection_Flipz));
3580     GlobalCommands_insert("RotateSelectionZ", makeCallbackF(Selection_Rotatez));
3581
3582     GlobalCommands_insert("ArbitraryRotation", makeCallbackF(DoRotateDlg));
3583     GlobalCommands_insert("ArbitraryScale", makeCallbackF(DoScaleDlg));
3584
3585     GlobalCommands_insert("BuildMenuCustomize", makeCallbackF(DoBuildMenu));
3586
3587     GlobalCommands_insert("FindBrush", makeCallbackF(DoFind));
3588
3589     GlobalCommands_insert("MapInfo", makeCallbackF(DoMapInfo), Accelerator('M'));
3590
3591
3592     GlobalToggles_insert("ToggleClipper", makeCallbackF(ClipperMode), ToggleItem::AddCallbackCaller(g_clipper_button),
3593                          Accelerator('X'));
3594
3595     GlobalToggles_insert("MouseTranslate", makeCallbackF(TranslateMode),
3596                          ToggleItem::AddCallbackCaller(g_translatemode_button), Accelerator('W'));
3597     GlobalToggles_insert("MouseRotate", makeCallbackF(RotateMode), ToggleItem::AddCallbackCaller(g_rotatemode_button),
3598                          Accelerator('R'));
3599     GlobalToggles_insert("MouseScale", makeCallbackF(ScaleMode), ToggleItem::AddCallbackCaller(g_scalemode_button));
3600     GlobalToggles_insert("MouseDrag", makeCallbackF(DragMode), ToggleItem::AddCallbackCaller(g_dragmode_button),
3601                          Accelerator('Q'));
3602
3603     GlobalCommands_insert("ColorSchemeOriginal", makeCallbackF(ColorScheme_Original));
3604     GlobalCommands_insert("ColorSchemeQER", makeCallbackF(ColorScheme_QER));
3605     GlobalCommands_insert("ColorSchemeBlackAndGreen", makeCallbackF(ColorScheme_Black));
3606     GlobalCommands_insert("ColorSchemeYdnar", makeCallbackF(ColorScheme_Ydnar));
3607     GlobalCommands_insert("ChooseTextureBackgroundColor", makeCallback(g_ColoursMenu.m_textureback));
3608     GlobalCommands_insert("ChooseGridBackgroundColor", makeCallback(g_ColoursMenu.m_xyback));
3609     GlobalCommands_insert("ChooseGridMajorColor", makeCallback(g_ColoursMenu.m_gridmajor));
3610     GlobalCommands_insert("ChooseGridMinorColor", makeCallback(g_ColoursMenu.m_gridminor));
3611     GlobalCommands_insert("ChooseSmallGridMajorColor", makeCallback(g_ColoursMenu.m_gridmajor_alt));
3612     GlobalCommands_insert("ChooseSmallGridMinorColor", makeCallback(g_ColoursMenu.m_gridminor_alt));
3613     GlobalCommands_insert("ChooseGridTextColor", makeCallback(g_ColoursMenu.m_gridtext));
3614     GlobalCommands_insert("ChooseGridBlockColor", makeCallback(g_ColoursMenu.m_gridblock));
3615     GlobalCommands_insert("ChooseBrushColor", makeCallback(g_ColoursMenu.m_brush));
3616     GlobalCommands_insert("ChooseCameraBackgroundColor", makeCallback(g_ColoursMenu.m_cameraback));
3617     GlobalCommands_insert("ChooseSelectedBrushColor", makeCallback(g_ColoursMenu.m_selectedbrush));
3618     GlobalCommands_insert("ChooseCameraSelectedBrushColor", makeCallback(g_ColoursMenu.m_selectedbrush3d));
3619     GlobalCommands_insert("ChooseClipperColor", makeCallback(g_ColoursMenu.m_clipper));
3620     GlobalCommands_insert("ChooseOrthoViewNameColor", makeCallback(g_ColoursMenu.m_viewname));
3621
3622
3623     GlobalCommands_insert("CSGSubtract", makeCallbackF(CSG_Subtract),
3624                           Accelerator('U', (GdkModifierType) GDK_SHIFT_MASK));
3625     GlobalCommands_insert("CSGMerge", makeCallbackF(CSG_Merge), Accelerator('U', (GdkModifierType) GDK_CONTROL_MASK));
3626     GlobalCommands_insert("CSGHollow", makeCallbackF(CSG_MakeHollow));
3627
3628     Grid_registerCommands();
3629
3630     GlobalCommands_insert("SnapToGrid", makeCallbackF(Selection_SnapToGrid),
3631                           Accelerator('G', (GdkModifierType) GDK_CONTROL_MASK));
3632
3633     GlobalCommands_insert("SelectAllOfType", makeCallbackF(Select_AllOfType),
3634                           Accelerator('A', (GdkModifierType) GDK_SHIFT_MASK));
3635
3636     GlobalCommands_insert("TexRotateClock", makeCallbackF(Texdef_RotateClockwise),
3637                           Accelerator(GDK_KEY_Next, (GdkModifierType) GDK_SHIFT_MASK));
3638     GlobalCommands_insert("TexRotateCounter", makeCallbackF(Texdef_RotateAntiClockwise),
3639                           Accelerator(GDK_KEY_Prior, (GdkModifierType) GDK_SHIFT_MASK));
3640     GlobalCommands_insert("TexScaleUp", makeCallbackF(Texdef_ScaleUp),
3641                           Accelerator(GDK_KEY_Up, (GdkModifierType) GDK_CONTROL_MASK));
3642     GlobalCommands_insert("TexScaleDown", makeCallbackF(Texdef_ScaleDown),
3643                           Accelerator(GDK_KEY_Down, (GdkModifierType) GDK_CONTROL_MASK));
3644     GlobalCommands_insert("TexScaleLeft", makeCallbackF(Texdef_ScaleLeft),
3645                           Accelerator(GDK_KEY_Left, (GdkModifierType) GDK_CONTROL_MASK));
3646     GlobalCommands_insert("TexScaleRight", makeCallbackF(Texdef_ScaleRight),
3647                           Accelerator(GDK_KEY_Right, (GdkModifierType) GDK_CONTROL_MASK));
3648     GlobalCommands_insert("TexShiftUp", makeCallbackF(Texdef_ShiftUp),
3649                           Accelerator(GDK_KEY_Up, (GdkModifierType) GDK_SHIFT_MASK));
3650     GlobalCommands_insert("TexShiftDown", makeCallbackF(Texdef_ShiftDown),
3651                           Accelerator(GDK_KEY_Down, (GdkModifierType) GDK_SHIFT_MASK));
3652     GlobalCommands_insert("TexShiftLeft", makeCallbackF(Texdef_ShiftLeft),
3653                           Accelerator(GDK_KEY_Left, (GdkModifierType) GDK_SHIFT_MASK));
3654     GlobalCommands_insert("TexShiftRight", makeCallbackF(Texdef_ShiftRight),
3655                           Accelerator(GDK_KEY_Right, (GdkModifierType) GDK_SHIFT_MASK));
3656
3657     GlobalCommands_insert("MoveSelectionDOWN", makeCallbackF(Selection_MoveDown), Accelerator(GDK_KEY_KP_Subtract));
3658     GlobalCommands_insert("MoveSelectionUP", makeCallbackF(Selection_MoveUp), Accelerator(GDK_KEY_KP_Add));
3659
3660     GlobalCommands_insert("SelectNudgeLeft", makeCallbackF(Selection_NudgeLeft),
3661                           Accelerator(GDK_KEY_Left, (GdkModifierType) GDK_MOD1_MASK));
3662     GlobalCommands_insert("SelectNudgeRight", makeCallbackF(Selection_NudgeRight),
3663                           Accelerator(GDK_KEY_Right, (GdkModifierType) GDK_MOD1_MASK));
3664     GlobalCommands_insert("SelectNudgeUp", makeCallbackF(Selection_NudgeUp),
3665                           Accelerator(GDK_KEY_Up, (GdkModifierType) GDK_MOD1_MASK));
3666     GlobalCommands_insert("SelectNudgeDown", makeCallbackF(Selection_NudgeDown),
3667                           Accelerator(GDK_KEY_Down, (GdkModifierType) GDK_MOD1_MASK));
3668
3669     Patch_registerCommands();
3670     XYShow_registerCommands();
3671
3672     typedef FreeCaller<void(const Selectable &), ComponentMode_SelectionChanged> ComponentModeSelectionChangedCaller;
3673     GlobalSelectionSystem().addSelectionChangeCallback(ComponentModeSelectionChangedCaller());
3674
3675     GlobalPreferenceSystem().registerPreference("DetachableMenus",
3676                                                 make_property_string(g_Layout_enableDetachableMenus.m_latched));
3677     GlobalPreferenceSystem().registerPreference("PatchToolBar",
3678                                                 make_property_string(g_Layout_enablePatchToolbar.m_latched));
3679     GlobalPreferenceSystem().registerPreference("PluginToolBar",
3680                                                 make_property_string(g_Layout_enablePluginToolbar.m_latched));
3681     GlobalPreferenceSystem().registerPreference("QE4StyleWindows", make_property_string(g_Layout_viewStyle.m_latched));
3682     GlobalPreferenceSystem().registerPreference("XYHeight", make_property_string(g_layout_globals.nXYHeight));
3683     GlobalPreferenceSystem().registerPreference("XYWidth", make_property_string(g_layout_globals.nXYWidth));
3684     GlobalPreferenceSystem().registerPreference("CamWidth", make_property_string(g_layout_globals.nCamWidth));
3685     GlobalPreferenceSystem().registerPreference("CamHeight", make_property_string(g_layout_globals.nCamHeight));
3686
3687     GlobalPreferenceSystem().registerPreference("State", make_property_string(g_layout_globals.nState));
3688     GlobalPreferenceSystem().registerPreference("PositionX", make_property_string(g_layout_globals.m_position.x));
3689     GlobalPreferenceSystem().registerPreference("PositionY", make_property_string(g_layout_globals.m_position.y));
3690     GlobalPreferenceSystem().registerPreference("Width", make_property_string(g_layout_globals.m_position.w));
3691     GlobalPreferenceSystem().registerPreference("Height", make_property_string(g_layout_globals.m_position.h));
3692
3693     GlobalPreferenceSystem().registerPreference("CamWnd", make_property<WindowPositionTracker_String>(g_posCamWnd));
3694     GlobalPreferenceSystem().registerPreference("XYWnd", make_property<WindowPositionTracker_String>(g_posXYWnd));
3695     GlobalPreferenceSystem().registerPreference("YZWnd", make_property<WindowPositionTracker_String>(g_posYZWnd));
3696     GlobalPreferenceSystem().registerPreference("XZWnd", make_property<WindowPositionTracker_String>(g_posXZWnd));
3697
3698     {
3699         const char *ENGINEPATH_ATTRIBUTE =
3700 #if GDEF_OS_WINDOWS
3701                 "enginepath_win32"
3702 #elif GDEF_OS_MACOS
3703                 "enginepath_macos"
3704 #elif GDEF_OS_LINUX || GDEF_OS_BSD
3705                 "enginepath_linux"
3706 #else
3707 #error "unknown platform"
3708 #endif
3709         ;
3710         StringOutputStream path(256);
3711         path << DirectoryCleaned(g_pGameDescription->getRequiredKeyValue(ENGINEPATH_ATTRIBUTE));
3712         g_strEnginePath = path.c_str();
3713     }
3714
3715     GlobalPreferenceSystem().registerPreference("EnginePath", make_property_string(g_strEnginePath));
3716
3717     GlobalPreferenceSystem().registerPreference("DisableEnginePath", make_property_string(g_disableEnginePath));
3718     GlobalPreferenceSystem().registerPreference("DisableHomePath", make_property_string(g_disableHomePath));
3719
3720     for (int i = 0; i < g_pakPathCount; i++) {
3721         std::string label = "PakPath" + std::to_string(i);
3722         GlobalPreferenceSystem().registerPreference(label.c_str(), make_property_string(g_strPakPath[i]));
3723     }
3724
3725     g_Layout_viewStyle.useLatched();
3726     g_Layout_enableDetachableMenus.useLatched();
3727     g_Layout_enablePatchToolbar.useLatched();
3728     g_Layout_enablePluginToolbar.useLatched();
3729
3730     Layout_registerPreferencesPage();
3731     Paths_registerPreferencesPage();
3732
3733     g_brushCount.setCountChangedCallback(makeCallbackF(QE_brushCountChanged));
3734     g_entityCount.setCountChangedCallback(makeCallbackF(QE_entityCountChanged));
3735     GlobalEntityCreator().setCounter(&g_entityCount);
3736
3737     GLWidget_sharedContextCreated = GlobalGL_sharedContextCreated;
3738     GLWidget_sharedContextDestroyed = GlobalGL_sharedContextDestroyed;
3739
3740     GlobalEntityClassManager().attach(g_WorldspawnColourEntityClassObserver);
3741 }
3742
3743 void MainFrame_Destroy()
3744 {
3745     GlobalEntityClassManager().detach(g_WorldspawnColourEntityClassObserver);
3746
3747     GlobalEntityCreator().setCounter(0);
3748     g_entityCount.setCountChangedCallback(Callback<void()>());
3749     g_brushCount.setCountChangedCallback(Callback<void()>());
3750 }
3751
3752
3753 void GLWindow_Construct()
3754 {
3755     GlobalPreferenceSystem().registerPreference("MouseButtons", make_property_string(g_glwindow_globals.m_nMouseType));
3756 }
3757
3758 void GLWindow_Destroy()
3759 {
3760 }