/
MainFrame.cpp
641 lines (512 loc) · 15.6 KB
/
MainFrame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#include "MainFrame.h"
#include "i18n.h"
#include "ui/igroupdialog.h"
#include "ui/iusercontrol.h"
#include "imap.h"
#include "ui/ieventmanager.h"
#include "ipreferencesystem.h"
#include "ieclass.h"
#include "iorthoview.h"
#include "iregistry.h"
#include "iradiant.h"
#include "xyview/GlobalXYWnd.h"
#include "camera/CameraWndManager.h"
#include "registry/registry.h"
#include "wxutil/MultiMonitor.h"
#include "ui/mainframe/ScreenUpdateBlocker.h"
#include "ui/mainframe/AuiLayout.h"
#include "ui/mainframe/TopLevelFrame.h"
#include "textool/TexTool.h"
#include "module/StaticModule.h"
#include "messages/ApplicationShutdownRequest.h"
#include <functional>
#include <fmt/format.h>
#include <sigc++/functors/mem_fun.h>
#include <wx/display.h>
#ifdef WIN32
#include <windows.h>
#endif
namespace
{
const std::string RKEY_WINDOW_LAYOUT = "user/ui/mainFrame/windowLayout";
const std::string RKEY_WINDOW_STATE = "user/ui/mainFrame/window";
const std::string RKEY_MULTIMON_START_MONITOR = "user/ui/multiMonitor/startMonitorNum";
const std::string RKEY_DISABLE_WIN_DESKTOP_COMP = "user/ui/compatibility/disableWindowsDesktopComposition";
const std::string RKEY_ACTIVE_LAYOUT = "user/ui/mainFrame/activeLayout";
}
namespace ui
{
MainFrame::MainFrame() :
_topLevelWindow(NULL),
_screenUpdatesEnabled(false), // not enabled until constructed
_defLoadingBlocksUpdates(false),
_mapLoadingBlocksUpdates(false)
{}
// RegisterableModule implementation
const std::string& MainFrame::getName() const
{
static std::string _name(MODULE_MAINFRAME);
return _name;
}
const StringSet& MainFrame::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_MAINFRAME_LAYOUT_MANAGER);
_dependencies.insert(MODULE_XMLREGISTRY);
_dependencies.insert(MODULE_PREFERENCESYSTEM);
_dependencies.insert(MODULE_COMMANDSYSTEM);
_dependencies.insert(MODULE_ORTHOVIEWMANAGER);
_dependencies.insert(MODULE_ECLASSMANAGER);
_dependencies.insert(MODULE_MAP);
}
return _dependencies;
}
void MainFrame::initialiseModule(const IApplicationContext& ctx)
{
// Add another page for Multi-Monitor stuff
IPreferencePage& page = GlobalPreferenceSystem().getPage(_("Settings/Multi Monitor"));
// Initialise the registry, if no key is set
if (GlobalRegistry().get(RKEY_MULTIMON_START_MONITOR).empty())
{
GlobalRegistry().set(RKEY_MULTIMON_START_MONITOR, "0");
}
ComboBoxValueList list;
for (unsigned int i = 0; i < wxutil::MultiMonitor::getNumMonitors(); ++i)
{
wxRect rect = wxutil::MultiMonitor::getMonitor(i);
list.push_back(
fmt::format("Monitor {0:d} ({1:d}x{2:d})", i, rect.GetWidth(), rect.GetHeight())
);
}
page.appendCombo(_("Start DarkRadiant on monitor"), RKEY_MULTIMON_START_MONITOR, list);
// Add the toggle max/min command for floating windows
GlobalCommandSystem().addCommand("ToggleFullScreenCamera",
std::bind(&MainFrame::toggleFullscreenCameraView, this, std::placeholders::_1)
);
GlobalCommandSystem().addCommand("Exit", sigc::mem_fun(this, &MainFrame::exitCmd));
#ifdef WIN32
HMODULE lib = LoadLibrary(L"dwmapi.dll");
if (lib != NULL)
{
void (WINAPI *dwmEnableComposition) (bool) =
(void (WINAPI *) (bool)) GetProcAddress(lib, "DwmEnableComposition");
if (dwmEnableComposition)
{
// Add a page for Desktop Composition stuff
IPreferencePage& compatPage = GlobalPreferenceSystem().getPage(_("Settings/Compatibility"));
compatPage.appendCheckBox(_("Disable Windows Desktop Composition"),
RKEY_DISABLE_WIN_DESKTOP_COMP);
GlobalRegistry().signalForKey(RKEY_DISABLE_WIN_DESKTOP_COMP).connect(
sigc::mem_fun(this, &MainFrame::keyChanged)
);
}
FreeLibrary(lib);
}
// Load the value and act
setDesktopCompositionEnabled(!registry::getValue<bool>(RKEY_DISABLE_WIN_DESKTOP_COMP));
#endif
_mapNameChangedConn = GlobalMapModule().signal_mapNameChanged().connect(
sigc::mem_fun(this, &MainFrame::updateTitle)
);
_mapModifiedChangedConn = GlobalMapModule().signal_modifiedChanged().connect(
sigc::mem_fun(this, &MainFrame::updateTitle)
);
// When the eclass defs are in progress of being loaded, block all updates
_defsLoadingSignal = GlobalDeclarationManager().signal_DeclsReloading(decl::Type::EntityDef).connect(
[this]() { _defLoadingBlocksUpdates = true; }
);
_defsLoadedSignal = GlobalDeclarationManager().signal_DeclsReloaded(decl::Type::EntityDef).connect(
[this]() { _defLoadingBlocksUpdates = false; }
);
// Block updates when a map is loading
_mapEventSignal = GlobalMapModule().signal_mapEvent().connect(
[this](IMap::MapEvent ev)
{
if (ev == IMap::MapLoading)
{
_mapLoadingBlocksUpdates = true;
}
else if (ev == IMap::MapLoaded)
{
_mapLoadingBlocksUpdates = false;
}
}
);
// Subscribe for the post-module init event
module::GlobalModuleRegistry().signal_allModulesInitialised().connect(
sigc::mem_fun(this, &MainFrame::postModuleInitialisation));
}
void MainFrame::shutdownModule()
{
rMessage() << "MainFrame::shutdownModule called." << std::endl;
_mapEventSignal.disconnect();
_mapNameChangedConn.disconnect();
_mapModifiedChangedConn.disconnect();
_defsLoadingSignal.disconnect();
_defsLoadedSignal.disconnect();
disableScreenUpdates();
}
void MainFrame::exitCmd(const cmd::ArgumentList& args)
{
// Just tell the main application window to close, which will invoke
// appropriate event handlers.
if (getWxTopLevelWindow() != nullptr)
{
getWxTopLevelWindow()->Close(false /* don't force */);
}
}
void MainFrame::postModuleInitialisation()
{
// Initialise the mainframe
construct();
// Load the shortcuts from the registry
GlobalEventManager().loadAccelerators();
// Show the top level window as late as possible
getWxTopLevelWindow()->Show();
signal_MainFrameReady().emit();
signal_MainFrameReady().clear();
}
void MainFrame::keyChanged()
{
#ifdef WIN32
setDesktopCompositionEnabled(!registry::getValue<bool>(RKEY_DISABLE_WIN_DESKTOP_COMP));
#endif
}
#ifdef WIN32
// Pulled those defs from Dwmapi.h
#define DWM_EC_DISABLECOMPOSITION 0
#define DWM_EC_ENABLECOMPOSITION 1
void MainFrame::setDesktopCompositionEnabled(bool enabled)
{
HMODULE lib = LoadLibrary(L"dwmapi.dll");
if (lib != NULL)
{
HRESULT (WINAPI *dwmEnableComposition) (UINT) =
(HRESULT (WINAPI *) (UINT)) GetProcAddress(lib, "DwmEnableComposition");
if (dwmEnableComposition)
{
HRESULT result = dwmEnableComposition(enabled ? DWM_EC_ENABLECOMPOSITION : DWM_EC_DISABLECOMPOSITION);
if (!SUCCEEDED(result))
{
rError() << "Could not disable desktop composition" << std::endl;
}
}
FreeLibrary(lib);
}
}
#endif
void MainFrame::toggleFullscreenCameraView(const cmd::ArgumentList& args)
{
if (_currentLayout == NULL) return;
// Issue the call
_currentLayout->toggleFullscreenCameraView();
}
void MainFrame::construct()
{
// Create the base window and the default widgets
create();
#if 0
std::string activeLayout = GlobalRegistry().get(RKEY_ACTIVE_LAYOUT);
if (activeLayout.empty())
{
activeLayout = AUI_LAYOUT_NAME; // fall back to hardcoded layout
}
// Apply the layout
applyLayout(activeLayout);
if (_currentLayout == NULL)
{
// Layout is still empty, this is not good
rError() << "Could not restore layout " << activeLayout << std::endl;
if (activeLayout != AUI_LAYOUT_NAME)
{
// Try to fallback to floating layout
applyLayout(AUI_LAYOUT_NAME);
}
}
#endif
#ifdef __linux__
// #4526: In Linux, do another restore after the top level window has been shown
// After startup, GTK emits onSizeAllocate events which trigger a Layout() sequence
// messing up the pane positions, so do the restore() one more time after the main
// window came up.
_topLevelWindow->Bind(wxEVT_SHOW, [&](wxShowEvent& ev)
{
if (_currentLayout && ev.IsShown())
{
_currentLayout->restoreStateFromRegistry();
}
ev.Skip();
});
#endif
// register the commands
GlobalMainFrameLayoutManager().registerCommands();
// Emit the "constructed" signal to give modules a chance to register
// their UI parts. Clear the signal afterwards.
signal_MainFrameConstructed().emit();
signal_MainFrameConstructed().clear();
enableScreenUpdates();
updateAllWindows();
}
void MainFrame::removeLayout()
{
// Sanity check
if (!_currentLayout) return;
_currentLayout->deactivate();
_currentLayout.reset();
}
void MainFrame::preDestructionCleanup()
{
saveWindowPosition();
// Free the layout
if (_currentLayout)
{
removeLayout();
}
}
void MainFrame::updateTitle()
{
if (_topLevelWindow == nullptr)
{
return;
}
std::string title = GlobalMapModule().getMapName();
if (GlobalMapModule().isModified())
{
title += " *";
}
_topLevelWindow->SetTitle(title);
}
void MainFrame::onTopLevelFrameClose(wxCloseEvent& ev)
{
// If the event is vetoable, issue the shutdown message and get the green light
if (ev.CanVeto())
{
radiant::ApplicationShutdownRequest request;
GlobalRadiantCore().getMessageBus().sendMessage(request);
if (request.isDenied())
{
// Keep running
ev.Veto();
return;
}
}
wxASSERT(wxTheApp->GetTopWindow() == _topLevelWindow);
_topLevelWindow->Hide();
// Invoke cleanup code which still needs the GUI hierarchy to be
// present
preDestructionCleanup();
// Broadcast shutdown event
signal_MainFrameShuttingDown().emit();
signal_MainFrameShuttingDown().clear();
// Destroy the actual window
_topLevelWindow->Destroy();
_topLevelWindow = nullptr;
// wxWidgets is supposed to quit when the main window is destroyed, but
// it doesn't so we need to exit the main loop manually. Probably we
// are keeping some other window around internally which makes wx think
// that the application is still needed.
wxTheApp->ExitMainLoop();
}
wxFrame* MainFrame::getWxTopLevelWindow()
{
return _topLevelWindow;
}
wxBoxSizer* MainFrame::getWxMainContainer()
{
return _topLevelWindow != nullptr ? _topLevelWindow->getMainContainer() : nullptr;
}
bool MainFrame::isActiveApp()
{
return wxTheApp->IsActive();
}
void MainFrame::createTopLevelWindow()
{
// Destroy any previous toplevel window
if (_topLevelWindow)
{
_topLevelWindow->Destroy();
}
// Create a new window
_topLevelWindow = new TopLevelFrame;
wxTheApp->SetTopWindow(_topLevelWindow);
// Listen for close events
_topLevelWindow->Bind(wxEVT_CLOSE_WINDOW,
&MainFrame::onTopLevelFrameClose, this);
}
void MainFrame::restoreWindowPosition()
{
// We start out maximised by default
bool isMaximised = true;
// Load and sanitise the monitor number, the number of displays might have changed
unsigned int startMonitor = registry::getValue<unsigned int>(RKEY_MULTIMON_START_MONITOR);
if (startMonitor >= wxutil::MultiMonitor::getNumMonitors())
{
startMonitor = 0;
}
// Set up the size/position from registry or the defaults
if (GlobalRegistry().keyExists(RKEY_WINDOW_STATE))
{
_windowPosition.loadFromPath(RKEY_WINDOW_STATE);
isMaximised = string::convert<bool>(GlobalRegistry().getAttribute(RKEY_WINDOW_STATE, "state"), true);
}
else
{
// If no state was found in the registry, fit the window into the start monitor rectangle
_windowPosition.fitToScreen(wxutil::MultiMonitor::getMonitor(startMonitor), 0.8f, 0.8f);
}
// Connect the tracker which will also apply the stored size/position
_windowPosition.connect(_topLevelWindow);
if (isMaximised)
{
_topLevelWindow->Maximize(true);
}
}
wxToolBar* MainFrame::getToolbar(IMainFrame::Toolbar type)
{
// Pass any request for the camera view toolbar to the active CamWnd, if
// any
if (type == Toolbar::CAMERA)
{
auto cw = GlobalCamera().getActiveCamWnd();
return cw ? cw->getToolbar() : nullptr;
}
// Main window toolbar
return _topLevelWindow->getToolbar(type);
}
void MainFrame::create()
{
// Create the topmost window first
createTopLevelWindow();
applyLayout(AUI_LAYOUT_NAME);
addControl(UserControl::Console, ControlSettings{ Location::PropertyPanel, true });
addControl(UserControl::EntityInspector, ControlSettings{ Location::PropertyPanel, true });
addControl(UserControl::MediaBrowser, ControlSettings{ Location::PropertyPanel, true });
// Load the previous window settings from the registry
restoreWindowPosition();
}
void MainFrame::saveWindowPosition()
{
// Tell the position tracker to save the information
_windowPosition.saveToPath(RKEY_WINDOW_STATE);
if (_topLevelWindow)
{
GlobalRegistry().setAttribute(
RKEY_WINDOW_STATE,
"state",
string::to_string(_topLevelWindow->IsMaximized())
);
// Save the monitor number the window is currently displayed on
registry::setValue(RKEY_MULTIMON_START_MONITOR,
wxutil::MultiMonitor::getMonitorNumForWindow(_topLevelWindow));
}
}
bool MainFrame::screenUpdatesEnabled()
{
return _screenUpdatesEnabled && !_defLoadingBlocksUpdates && !_mapLoadingBlocksUpdates;
}
void MainFrame::enableScreenUpdates() {
_screenUpdatesEnabled = true;
}
void MainFrame::disableScreenUpdates() {
_screenUpdatesEnabled = false;
}
void MainFrame::updateAllWindows(bool force)
{
if (!_screenUpdatesEnabled) return;
if (force)
{
GlobalCamera().forceDraw();
}
else
{
GlobalCamera().update();
}
GlobalXYWndManager().updateAllViews(force);
if (TexTool::Instance().IsShown())
{
if (force)
{
TexTool::Instance().forceRedraw();
}
else
{
TexTool::Instance().queueDraw();
}
}
}
void MainFrame::setActiveLayoutName(const std::string& name)
{
GlobalRegistry().set(RKEY_ACTIVE_LAYOUT, name);
}
void MainFrame::applyLayout(const std::string& name)
{
_currentLayout = std::make_shared<AuiLayout>();
_currentLayout->activate();
#if 0
if (getCurrentLayout() == name)
{
// nothing to do
rMessage() << "MainFrame: Won't activate layout " << name
<< ", is already active." << std::endl;
return;
}
// Set or clear?
if (!name.empty())
{
// Try to find that new layout
IMainFrameLayoutPtr layout = GlobalMainFrameLayoutManager().getLayout(name);
if (layout == NULL) {
rError() << "MainFrame: Could not find layout with name " << name << std::endl;
return;
}
// Found a new layout, remove the old one
removeLayout();
rMessage() << "MainFrame: Activating layout " << name << std::endl;
// Store and activate the new layout
_currentLayout = layout;
_currentLayout->activate();
}
else {
// Empty layout name => remove
removeLayout();
}
#endif
}
std::string MainFrame::getCurrentLayout()
{
return (_currentLayout != NULL) ? _currentLayout->getName() : "";
}
IScopedScreenUpdateBlockerPtr MainFrame::getScopedScreenUpdateBlocker(const std::string& title,
const std::string& message, bool forceDisplay)
{
return IScopedScreenUpdateBlockerPtr(new ScreenUpdateBlocker(title, message, forceDisplay));
}
void MainFrame::addControl(const std::string& controlName, ControlSettings defaultSettings)
{
if (defaultSettings.location == Location::PropertyPanel)
{
_currentLayout->getNotebook()->addControl(controlName);
}
}
void MainFrame::createFloatingControl(const std::string& controlName)
{
if (!_currentLayout) return;
_currentLayout->createFloatingControl(controlName);
}
sigc::signal<void>& MainFrame::signal_MainFrameConstructed()
{
return _sigMainFrameConstructed;
}
sigc::signal<void>& MainFrame::signal_MainFrameReady()
{
return _sigMainFrameReady;
}
sigc::signal<void>& MainFrame::signal_MainFrameShuttingDown()
{
return _sigMainFrameShuttingDown;
}
// Define the static MainFrame module
module::StaticModuleRegistration<MainFrame> mainFrameModule;
} // namespace ui