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