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