File: FrameAui.cpp

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,052 kB
  • sloc: cpp: 213,146; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (1037 lines) | stat: -rw-r--r-- 25,674 bytes parent folder | download | duplicates (2)
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
// Copyright 2009 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <cstddef>
#include <string>
#include <vector>
#include <wx/frame.h>
#include <wx/list.h>
#include <wx/menu.h>
#include <wx/msgdlg.h>
#include <wx/panel.h>
#include <wx/rtti.h>
#include <wx/sizer.h>
#include <wx/statusbr.h>
#include <wx/textdlg.h>
#include <wx/toplevel.h>
#include <wx/aui/auibar.h>
#include <wx/aui/auibook.h>
#include <wx/aui/framemanager.h>

#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/MathUtil.h"
#include "Common/StringUtil.h"
#include "Common/Logging/ConsoleListener.h"
#include "Core/ConfigManager.h"
#include "DolphinWX/Frame.h"
#include "DolphinWX/Globals.h"
#include "DolphinWX/LogConfigWindow.h"
#include "DolphinWX/LogWindow.h"
#include "DolphinWX/WxUtils.h"
#include "DolphinWX/Debugger/CodeWindow.h"

// ------------
// Aui events

void CFrame::OnManagerResize(wxAuiManagerEvent& event)
{
	if (!g_pCodeWindow && m_LogWindow &&
			m_Mgr->GetPane("Pane 1").IsShown() &&
			!m_Mgr->GetPane("Pane 1").IsFloating())
	{
		m_LogWindow->x = m_Mgr->GetPane("Pane 1").rect.GetWidth();
		m_LogWindow->y = m_Mgr->GetPane("Pane 1").rect.GetHeight();
		m_LogWindow->winpos = m_Mgr->GetPane("Pane 1").dock_direction;
	}
	event.Skip();
}

void CFrame::OnPaneClose(wxAuiManagerEvent& event)
{
	event.Veto();

	wxAuiNotebook * nb = (wxAuiNotebook*)event.pane->window;
	if (!nb) return;

	if (!g_pCodeWindow)
	{
		if (nb->GetPage(0)->GetId() == IDM_LOG_WINDOW ||
		    nb->GetPage(0)->GetId() == IDM_LOG_CONFIG_WINDOW)
		{

			SConfig::GetInstance().m_InterfaceLogWindow = false;
			SConfig::GetInstance().m_InterfaceLogConfigWindow = false;
			ToggleLogWindow(false);
			ToggleLogConfigWindow(false);
		}
	}
	else
	{
		if (GetNotebookCount() == 1)
		{
			wxMessageBox(_("At least one pane must remain open."),
					_("Notice"), wxOK, this);
		}
		else if (nb->GetPageCount() != 0 && !nb->GetPageText(0).IsSameAs("<>"))
		{
			wxMessageBox(_("You can't close panes that have pages in them."),
					_("Notice"), wxOK, this);
		}
		else
		{
			// Detach and delete the empty notebook
			event.pane->DestroyOnClose(true);
			m_Mgr->ClosePane(*event.pane);
		}
	}

	m_Mgr->Update();
}

void CFrame::ToggleLogWindow(bool bShow)
{
	if (!m_LogWindow)
		return;

	GetMenuBar()->FindItem(IDM_LOG_WINDOW)->Check(bShow);

	if (bShow)
	{
		// Create a new log window if it doesn't exist.
		if (!m_LogWindow)
		{
			m_LogWindow = new CLogWindow(this, IDM_LOG_WINDOW);
		}

		m_LogWindow->Enable();

		DoAddPage(m_LogWindow,
				g_pCodeWindow ? g_pCodeWindow->iNbAffiliation[0] : 0,
				g_pCodeWindow ? bFloatWindow[0] : false);
	}
	else
	{
		// Hiding the log window, so disable it and remove it.
		m_LogWindow->Disable();
		DoRemovePage(m_LogWindow, true);
	}

	// Hide or Show the pane
	if (!g_pCodeWindow)
		TogglePane();
}

void CFrame::ToggleLogConfigWindow(bool bShow)
{
	GetMenuBar()->FindItem(IDM_LOG_CONFIG_WINDOW)->Check(bShow);

	if (bShow)
	{
		if (!m_LogConfigWindow)
			m_LogConfigWindow = new LogConfigWindow(this, IDM_LOG_CONFIG_WINDOW);

		const int nbIndex = IDM_LOG_CONFIG_WINDOW - IDM_LOG_WINDOW;
		DoAddPage(m_LogConfigWindow,
				g_pCodeWindow ? g_pCodeWindow->iNbAffiliation[nbIndex] : 0,
				g_pCodeWindow ? bFloatWindow[nbIndex] : false);
	}
	else
	{
		DoRemovePage(m_LogConfigWindow, false);
		m_LogConfigWindow = nullptr;
	}

	// Hide or Show the pane
	if (!g_pCodeWindow)
		TogglePane();
}

void CFrame::OnToggleWindow(wxCommandEvent& event)
{
	bool bShow = GetMenuBar()->IsChecked(event.GetId());

	switch (event.GetId())
	{
		case IDM_LOG_WINDOW:
			if (!g_pCodeWindow)
				SConfig::GetInstance().m_InterfaceLogWindow = bShow;
			ToggleLogWindow(bShow);
			break;
		case IDM_LOG_CONFIG_WINDOW:
			if (!g_pCodeWindow)
				SConfig::GetInstance().m_InterfaceLogConfigWindow = bShow;
			ToggleLogConfigWindow(bShow);
			break;
		case IDM_REGISTER_WINDOW:
			g_pCodeWindow->ToggleRegisterWindow(bShow);
			break;
		case IDM_WATCH_WINDOW:
			g_pCodeWindow->ToggleWatchWindow(bShow);
			break;
		case IDM_BREAKPOINT_WINDOW:
			g_pCodeWindow->ToggleBreakPointWindow(bShow);
			break;
		case IDM_MEMORY_WINDOW:
			g_pCodeWindow->ToggleMemoryWindow(bShow);
			break;
		case IDM_JIT_WINDOW:
			g_pCodeWindow->ToggleJitWindow(bShow);
			break;
		case IDM_SOUND_WINDOW:
			g_pCodeWindow->ToggleSoundWindow(bShow);
			break;
		case IDM_VIDEO_WINDOW:
			g_pCodeWindow->ToggleVideoWindow(bShow);
			break;
	}
}

// Notebooks
// ---------------------
void CFrame::ClosePages()
{
	ToggleLogWindow(false);
	ToggleLogConfigWindow(false);

	if (g_pCodeWindow)
	{
		g_pCodeWindow->ToggleCodeWindow(false);
		g_pCodeWindow->ToggleRegisterWindow(false);
		g_pCodeWindow->ToggleWatchWindow(false);
		g_pCodeWindow->ToggleBreakPointWindow(false);
		g_pCodeWindow->ToggleMemoryWindow(false);
		g_pCodeWindow->ToggleJitWindow(false);
		g_pCodeWindow->ToggleSoundWindow(false);
		g_pCodeWindow->ToggleVideoWindow(false);
	}
}

void CFrame::OnNotebookPageChanged(wxAuiNotebookEvent& event)
{
	event.Skip();

	if (!g_pCodeWindow)
		return;

	// Remove the blank page if any
	AddRemoveBlankPage();

	// Update the notebook affiliation
	for (int i = IDM_LOG_WINDOW; i <= IDM_CODE_WINDOW; i++)
	{
		if (GetNotebookAffiliation(i) >= 0)
			g_pCodeWindow->iNbAffiliation[i - IDM_LOG_WINDOW] = GetNotebookAffiliation(i);
	}
}

void CFrame::OnNotebookPageClose(wxAuiNotebookEvent& event)
{
	// Override event
	event.Veto();

	wxAuiNotebook* Ctrl = (wxAuiNotebook*)event.GetEventObject();

	if (Ctrl->GetPage(event.GetSelection())->GetId() == IDM_LOG_WINDOW)
		ToggleLogWindow(false);
	if (Ctrl->GetPage(event.GetSelection())->GetId() == IDM_LOG_CONFIG_WINDOW)
		ToggleLogConfigWindow(false);
	if (Ctrl->GetPage(event.GetSelection())->GetId() == IDM_REGISTER_WINDOW)
		g_pCodeWindow->ToggleRegisterWindow(false);
	if (Ctrl->GetPage(event.GetSelection())->GetId() == IDM_WATCH_WINDOW)
		g_pCodeWindow->ToggleWatchWindow(false);
	if (Ctrl->GetPage(event.GetSelection())->GetId() == IDM_BREAKPOINT_WINDOW)
		g_pCodeWindow->ToggleBreakPointWindow(false);
	if (Ctrl->GetPage(event.GetSelection())->GetId() == IDM_JIT_WINDOW)
		g_pCodeWindow->ToggleJitWindow(false);
	if (Ctrl->GetPage(event.GetSelection())->GetId() == IDM_MEMORY_WINDOW)
		g_pCodeWindow->ToggleMemoryWindow(false);
	if (Ctrl->GetPage(event.GetSelection())->GetId() == IDM_SOUND_WINDOW)
		g_pCodeWindow->ToggleSoundWindow(false);
	if (Ctrl->GetPage(event.GetSelection())->GetId() == IDM_VIDEO_WINDOW)
		g_pCodeWindow->ToggleVideoWindow(false);
}

void CFrame::OnFloatingPageClosed(wxCloseEvent& event)
{
	ToggleFloatWindow(event.GetId() - IDM_LOG_WINDOW_PARENT + IDM_FLOAT_LOG_WINDOW);
}

void CFrame::OnFloatingPageSize(wxSizeEvent& event)
{
	event.Skip();
}

void CFrame::OnFloatWindow(wxCommandEvent& event)
{
	ToggleFloatWindow(event.GetId());
}

void CFrame::ToggleFloatWindow(int Id)
{
	wxWindowID WinId = Id - IDM_FLOAT_LOG_WINDOW + IDM_LOG_WINDOW;
	if (GetNotebookPageFromId(WinId))
	{
		DoFloatNotebookPage(WinId);
		bFloatWindow[WinId - IDM_LOG_WINDOW] = true;
	}
	else
	{
		if (FindWindowById(WinId))
			DoUnfloatPage(WinId - IDM_LOG_WINDOW + IDM_LOG_WINDOW_PARENT);
		bFloatWindow[WinId - IDM_LOG_WINDOW] = false;
	}
}

void CFrame::DoFloatNotebookPage(wxWindowID Id)
{
	wxPanel *Win = (wxPanel*)FindWindowById(Id);
	if (!Win) return;

	for (int i = 0; i < GetNotebookCount(); i++)
	{
		wxAuiNotebook *nb = GetNotebookFromId(i);
		if (nb->GetPageIndex(Win) != wxNOT_FOUND)
		{
			nb->RemovePage(nb->GetPageIndex(Win));
			// Create the parent frame and reparent the window
			CreateParentFrame(Win->GetId() + IDM_LOG_WINDOW_PARENT - IDM_LOG_WINDOW,
					Win->GetName(), Win);
			if (nb->GetPageCount() == 0)
				AddRemoveBlankPage();
		}
	}
}

void CFrame::DoUnfloatPage(int Id)
{
	wxFrame * Win = (wxFrame*)FindWindowById(Id);
	if (!Win) return;

	wxWindow * Child = Win->GetChildren().Item(0)->GetData();
	Child->Reparent(this);
	DoAddPage(Child, g_pCodeWindow->iNbAffiliation[Child->GetId() - IDM_LOG_WINDOW], false);
	Win->Destroy();
}

void CFrame::OnTab(wxAuiNotebookEvent& event)
{
	event.Skip();
	if (!g_pCodeWindow) return;

	// Create the popup menu
	wxMenu MenuPopup;

	wxMenuItem* Item = new wxMenuItem(&MenuPopup, wxID_ANY, _("Select floating windows"));
	MenuPopup.Append(Item);
	Item->Enable(false);
	MenuPopup.Append(new wxMenuItem(&MenuPopup));

	for (int i = IDM_LOG_WINDOW; i <= IDM_CODE_WINDOW; i++)
	{
		wxWindow *Win = FindWindowById(i);
		if (Win && Win->IsEnabled())
		{
			Item = new wxMenuItem(&MenuPopup, i + IDM_FLOAT_LOG_WINDOW - IDM_LOG_WINDOW,
					Win->GetName(), "", wxITEM_CHECK);
			MenuPopup.Append(Item);
			Item->Check(!!FindWindowById(i + IDM_LOG_WINDOW_PARENT - IDM_LOG_WINDOW));
		}
	}

	// Line up our menu with the cursor
	wxPoint Pt = ::wxGetMousePosition();
	Pt = ScreenToClient(Pt);

	// Show
	PopupMenu(&MenuPopup, Pt);
}

void CFrame::OnAllowNotebookDnD(wxAuiNotebookEvent& event)
{
	event.Skip();
	event.Allow();
}

void CFrame::ShowResizePane()
{
	if (!m_LogWindow) return;

	// Make sure the size is sane
	if (m_LogWindow->x > GetClientRect().GetWidth())
		m_LogWindow->x = GetClientRect().GetWidth() / 2;
	if (m_LogWindow->y > GetClientRect().GetHeight())
		m_LogWindow->y = GetClientRect().GetHeight() / 2;

	wxAuiPaneInfo &pane = m_Mgr->GetPane("Pane 1");

	// Hide first otherwise a resize doesn't work
	pane.Hide();
	m_Mgr->Update();

	pane.BestSize(m_LogWindow->x, m_LogWindow->y)
		.MinSize(m_LogWindow->x, m_LogWindow->y)
		.Direction(m_LogWindow->winpos).Show();
	m_Mgr->Update();

	// Reset the minimum size of the pane
	pane.MinSize(-1, -1);
	m_Mgr->Update();
}

void CFrame::TogglePane()
{
	// Get the first notebook
	wxAuiNotebook * NB = nullptr;
	for (u32 i = 0; i < m_Mgr->GetAllPanes().GetCount(); i++)
	{
		if (m_Mgr->GetAllPanes()[i].window->IsKindOf(CLASSINFO(wxAuiNotebook)))
			NB = (wxAuiNotebook*)m_Mgr->GetAllPanes()[i].window;
	}

	if (NB)
	{
		if (NB->GetPageCount() == 0)
		{
			m_Mgr->GetPane("Pane 1").Hide();
			m_Mgr->Update();
		}
		else
		{
			ShowResizePane();
		}
	}
}

void CFrame::DoRemovePage(wxWindow *Win, bool bHide)
{
	if (!Win) return;

	wxWindow *Parent = FindWindowById(Win->GetId() +
			IDM_LOG_WINDOW_PARENT - IDM_LOG_WINDOW);

	if (Parent)
	{
		if (bHide)
		{
			Win->Hide();
			Win->Reparent(this);
		}
		else
		{
			Win->Destroy();
		}

		Parent->Destroy();
	}
	else
	{
		for (int i = 0; i < GetNotebookCount(); i++)
		{
			int PageIndex = GetNotebookFromId(i)->GetPageIndex(Win);
			if (PageIndex != wxNOT_FOUND)
			{
				GetNotebookFromId(i)->RemovePage(PageIndex);
				if (bHide)
				{
					Win->Hide();
					Win->Reparent(this);
				}
				else
				{
					Win->Destroy();
				}
			}
		}
	}

	if (g_pCodeWindow)
		AddRemoveBlankPage();
}

void CFrame::DoAddPage(wxWindow *Win, int i, bool Float)
{
	if (!Win) return;

	// Ensure accessor remains within valid bounds.
	if (i < 0 || i > GetNotebookCount() - 1)
		i = 0;

	// The page was already previously added, no need to add it again.
	if (Win && GetNotebookFromId(i)->GetPageIndex(Win) != wxNOT_FOUND)
		return;

	if (!Float)
		GetNotebookFromId(i)->AddPage(Win, Win->GetName(), true);
	else
		CreateParentFrame(Win->GetId() + IDM_LOG_WINDOW_PARENT - IDM_LOG_WINDOW,
				Win->GetName(), Win);
}

void CFrame::PopulateSavedPerspectives()
{
	// If the perspective submenu hasn't been created yet, return
	if (!m_SavedPerspectives) return;

	// Delete all saved perspective menu items
	while (m_SavedPerspectives->GetMenuItemCount() != 0)
	{
		// Delete the first menu item in the list (while there are menu items)
		m_SavedPerspectives->Delete(m_SavedPerspectives->FindItemByPosition(0));
	}

	if (Perspectives.size() > 0)
	{
		for (u32 i = 0; i < Perspectives.size(); i++)
		{
			wxMenuItem* mItem = new wxMenuItem(m_SavedPerspectives, IDM_PERSPECTIVES_0 + i,
					StrToWxStr(Perspectives[i].Name), "", wxITEM_CHECK);

			m_SavedPerspectives->Append(mItem);

			if (i == ActivePerspective)
			{
				mItem->Check(true);
			}
		}
	}
}

void CFrame::OnPerspectiveMenu(wxCommandEvent& event)
{
	ClearStatusBar();

	switch (event.GetId())
	{
		case IDM_SAVE_PERSPECTIVE:
			if (Perspectives.size() == 0)
			{
				wxMessageBox(_("Please create a perspective before saving"),
						_("Notice"), wxOK, this);
				return;
			}
			SaveIniPerspectives();
			GetStatusBar()->SetStatusText(StrToWxStr(std::string
						("Saved " + Perspectives[ActivePerspective].Name)), 0);
			break;
		case IDM_PERSPECTIVES_ADD_PANE_TOP:
			AddPane(ADD_PANE_TOP);
			break;
		case IDM_PERSPECTIVES_ADD_PANE_BOTTOM:
			AddPane(ADD_PANE_BOTTOM);
			break;
		case IDM_PERSPECTIVES_ADD_PANE_LEFT:
			AddPane(ADD_PANE_LEFT);
			break;
		case IDM_PERSPECTIVES_ADD_PANE_RIGHT:
			AddPane(ADD_PANE_RIGHT);
			break;
		case IDM_PERSPECTIVES_ADD_PANE_CENTER:
			AddPane(ADD_PANE_CENTER);
			break;
		case IDM_EDIT_PERSPECTIVES:
			m_bEdit = event.IsChecked();
			TogglePaneStyle(m_bEdit, IDM_EDIT_PERSPECTIVES);
			break;
		case IDM_ADD_PERSPECTIVE:
			{
				wxTextEntryDialog dlg(this,
						_("Enter a name for the new perspective:"),
						_("Create new perspective"));
				wxString DefaultValue = wxString::Format(_("Perspective %d"), (int)(Perspectives.size() + 1));
				dlg.SetValue(DefaultValue);

				int Return = 0;
				bool DlgOk = false;

				while (!DlgOk)
				{
					Return = dlg.ShowModal();
					if (Return == wxID_CANCEL)
					{
						return;
					}
					else if (dlg.GetValue().Find(",") != -1)
					{
						wxMessageBox(_("The name cannot contain the character ','"),
								_("Notice"), wxOK, this);
						wxString Str = dlg.GetValue();
						Str.Replace(",", "", true);
						dlg.SetValue(Str);
					}
					else if (dlg.GetValue().IsSameAs(""))
					{
						wxMessageBox(_("The name cannot be empty"),
								_("Notice"), wxOK, this);
						dlg.SetValue(DefaultValue);
					}
					else
					{
						DlgOk = true;
					}
				}

				SPerspectives Tmp;
				Tmp.Name = WxStrToStr(dlg.GetValue());
				Tmp.Perspective = m_Mgr->SavePerspective();

				ActivePerspective = (u32)Perspectives.size();
				Perspectives.push_back(Tmp);

				UpdateCurrentPerspective();
				PopulateSavedPerspectives();
			}
			break;
		case IDM_TAB_SPLIT:
			m_bTabSplit = event.IsChecked();
			ToggleNotebookStyle(m_bTabSplit, wxAUI_NB_TAB_SPLIT);
			break;
		case IDM_NO_DOCKING:
			m_bNoDocking = event.IsChecked();
			TogglePaneStyle(m_bNoDocking, IDM_NO_DOCKING);
			break;
	}
}

void CFrame::TogglePaneStyle(bool On, int EventId)
{
	wxAuiPaneInfoArray& AllPanes = m_Mgr->GetAllPanes();
	for (u32 i = 0; i < AllPanes.GetCount(); ++i)
	{
		wxAuiPaneInfo& Pane = AllPanes[i];
		if (Pane.window->IsKindOf(CLASSINFO(wxAuiNotebook)))
		{
			// Default
			Pane.CloseButton(true);
			Pane.MaximizeButton(true);
			Pane.MinimizeButton(true);
			Pane.PinButton(true);
			Pane.Show();

			switch (EventId)
			{
				case IDM_EDIT_PERSPECTIVES:
					Pane.CaptionVisible(On);
					Pane.Movable(On);
					Pane.Floatable(On);
					Pane.Dockable(On);
					break;
			}
			Pane.Dockable(!m_bNoDocking);
		}
	}
	m_Mgr->Update();
}

void CFrame::ToggleNotebookStyle(bool On, long Style)
{
	wxAuiPaneInfoArray& AllPanes = m_Mgr->GetAllPanes();
	for (int i = 0, Count = (int)AllPanes.GetCount(); i < Count; ++i)
	{
		wxAuiPaneInfo& Pane = AllPanes[i];
		if (Pane.window->IsKindOf(CLASSINFO(wxAuiNotebook)))
		{
			wxAuiNotebook* NB = (wxAuiNotebook*)Pane.window;

			if (On)
				NB->SetWindowStyleFlag(NB->GetWindowStyleFlag() | Style);
			else
				NB->SetWindowStyleFlag(NB->GetWindowStyleFlag() &~ Style);

			NB->Refresh();
		}
	}
}

void CFrame::OnSelectPerspective(wxCommandEvent& event)
{
	u32 _Selection = event.GetId() - IDM_PERSPECTIVES_0;
	if (Perspectives.size() <= _Selection) _Selection = 0;
	ActivePerspective = _Selection;
	DoLoadPerspective();
}

void CFrame::SetPaneSize()
{
	if (Perspectives.size() <= ActivePerspective)
		return;

	int iClientX = GetSize().GetX();
	int iClientY = GetSize().GetY();

	for (u32 i = 0, j = 0; i < m_Mgr->GetAllPanes().GetCount(); i++)
	{
		if (!m_Mgr->GetAllPanes()[i].window->IsKindOf(CLASSINFO(wxAuiToolBar)))
		{
			if (!m_Mgr->GetAllPanes()[i].IsOk())
				return;

			if (Perspectives[ActivePerspective].Width.size() <= j ||
					Perspectives[ActivePerspective].Height.size() <= j)
				continue;

			// Width and height of the active perspective
			u32 W = Perspectives[ActivePerspective].Width[j],
				H = Perspectives[ActivePerspective].Height[j];

			// Check limits
			W = MathUtil::Clamp<u32>(W, 5, 95);
			H = MathUtil::Clamp<u32>(H, 5, 95);

			// Convert percentages to pixel lengths
			W = (W * iClientX) / 100;
			H = (H * iClientY) / 100;
			m_Mgr->GetAllPanes()[i].BestSize(W, H).MinSize(W, H);

			j++;
		}
	}
	m_Mgr->Update();

	for (u32 i = 0; i < m_Mgr->GetAllPanes().GetCount(); i++)
	{
		if (!m_Mgr->GetAllPanes()[i].window->IsKindOf(CLASSINFO(wxAuiToolBar)))
		{
			m_Mgr->GetAllPanes()[i].MinSize(-1, -1);
		}
	}
}

void CFrame::ReloadPanes()
{
	// Close all pages
	ClosePages();

	CloseAllNotebooks();

	// Create new panes with notebooks
	for (u32 i = 0; i < Perspectives[ActivePerspective].Width.size() - 1; i++)
	{
		wxString PaneName = wxString::Format("Pane %i", i + 1);
		m_Mgr->AddPane(CreateEmptyNotebook(), wxAuiPaneInfo().Hide()
				.CaptionVisible(m_bEdit).Dockable(!m_bNoDocking).Position(i)
				.Name(PaneName).Caption(PaneName));
	}

	// Perspectives
	m_Mgr->LoadPerspective(Perspectives[ActivePerspective].Perspective, false);
	// Restore settings
	TogglePaneStyle(m_bNoDocking, IDM_NO_DOCKING);
	TogglePaneStyle(m_bEdit, IDM_EDIT_PERSPECTIVES);

	// Load GUI settings
	g_pCodeWindow->Load();
	// Open notebook pages
	AddRemoveBlankPage();
	g_pCodeWindow->OpenPages();

	// Repopulate perspectives
	PopulateSavedPerspectives();
}

void CFrame::DoLoadPerspective()
{
	ReloadPanes();
	// Restore the exact window sizes, which LoadPerspective doesn't always do
	SetPaneSize();

	m_Mgr->Update();
}

// Update the local perspectives array
void CFrame::LoadIniPerspectives()
{
	Perspectives.clear();
	std::vector<std::string> VPerspectives;
	std::string _Perspectives;

	IniFile ini;
	ini.Load(File::GetUserPath(F_DEBUGGERCONFIG_IDX));

	IniFile::Section* perspectives = ini.GetOrCreateSection("Perspectives");
	perspectives->Get("Perspectives", &_Perspectives, "Perspective 1");
	perspectives->Get("Active", &ActivePerspective, 0);
	SplitString(_Perspectives, ',', VPerspectives);

	for (auto& VPerspective : VPerspectives)
	{
		SPerspectives Tmp;
		std::string _Section, _Perspective, _Widths, _Heights;
		std::vector<std::string> _SWidth, _SHeight;
		Tmp.Name = VPerspective;

		// Don't save a blank perspective
		if (Tmp.Name.empty())
		{
			continue;
		}

		_Section = StringFromFormat("P - %s", Tmp.Name.c_str());

		IniFile::Section* perspec_section = ini.GetOrCreateSection(_Section);
		perspec_section->Get("Perspective", &_Perspective,
		                     "layout2|"
		                     "name=Pane 0;caption=Pane 0;state=768;dir=5;prop=100000;|"
		                     "name=Pane 1;caption=Pane 1;state=31458108;dir=4;prop=100000;|"
		                     "dock_size(5,0,0)=22|dock_size(4,0,0)=333|");
		perspec_section->Get("Width", &_Widths, "70,25");
		perspec_section->Get("Height", &_Heights, "80,80");

		Tmp.Perspective = StrToWxStr(_Perspective);

		SplitString(_Widths, ',', _SWidth);
		SplitString(_Heights, ',', _SHeight);
		for (auto& Width : _SWidth)
		{
			int _Tmp;
			if (TryParse(Width, &_Tmp))
				Tmp.Width.push_back(_Tmp);
		}
		for (auto& Height : _SHeight)
		{
			int _Tmp;
			if (TryParse(Height, &_Tmp))
				Tmp.Height.push_back(_Tmp);
		}
		Perspectives.push_back(Tmp);
	}
}

void CFrame::UpdateCurrentPerspective()
{
	SPerspectives *current = &Perspectives[ActivePerspective];
	current->Perspective = m_Mgr->SavePerspective();

	// Get client size
	int iClientX = GetSize().GetX(), iClientY = GetSize().GetY();
	current->Width.clear();
	current->Height.clear();
	for (u32 i = 0; i < m_Mgr->GetAllPanes().GetCount(); i++)
	{
		if (!m_Mgr->GetAllPanes()[i].window->
				IsKindOf(CLASSINFO(wxAuiToolBar)))
		{
			// Save width and height as a percentage of the client width and height
			current->Width.push_back(
					(m_Mgr->GetAllPanes()[i].window->GetClientSize().GetX() * 100) /
					iClientX);
			current->Height.push_back(
					(m_Mgr->GetAllPanes()[i].window->GetClientSize().GetY() * 100) /
					iClientY);
		}
	}
}

void CFrame::SaveIniPerspectives()
{
	if (Perspectives.size() == 0) return;
	if (ActivePerspective >= Perspectives.size()) ActivePerspective = 0;

	// Turn off edit before saving
	TogglePaneStyle(false, IDM_EDIT_PERSPECTIVES);

	UpdateCurrentPerspective();

	IniFile ini;
	ini.Load(File::GetUserPath(F_DEBUGGERCONFIG_IDX));

	// Save perspective names
	std::string STmp = "";
	for (auto& Perspective : Perspectives)
	{
		STmp += Perspective.Name + ",";
	}
	STmp = STmp.substr(0, STmp.length() - 1);

	IniFile::Section* perspectives = ini.GetOrCreateSection("Perspectives");
	perspectives->Set("Perspectives", STmp);
	perspectives->Set("Active", ActivePerspective);

	// Save the perspectives
	for (auto& Perspective : Perspectives)
	{
		std::string _Section = "P - " + Perspective.Name;
		IniFile::Section* perspec_section = ini.GetOrCreateSection(_Section);
		perspec_section->Set("Perspective", WxStrToStr(Perspective.Perspective));

		std::string SWidth = "", SHeight = "";
		for (u32 j = 0; j < Perspective.Width.size(); j++)
		{
			SWidth += StringFromFormat("%i,", Perspective.Width[j]);
			SHeight += StringFromFormat("%i,", Perspective.Height[j]);
		}
		// Remove the ending ","
		SWidth = SWidth.substr(0, SWidth.length() - 1);
		SHeight = SHeight.substr(0, SHeight.length() - 1);

		perspec_section->Set("Width", SWidth);
		perspec_section->Set("Height", SHeight);
	}

	ini.Save(File::GetUserPath(F_DEBUGGERCONFIG_IDX));

	// Save notebook affiliations
	g_pCodeWindow->Save();

	TogglePaneStyle(m_bEdit, IDM_EDIT_PERSPECTIVES);
}

void CFrame::AddPane(int dir)
{
	int PaneNum = GetNotebookCount() + 1;
	wxString PaneName = wxString::Format("Pane %i", PaneNum);
	wxAuiPaneInfo PaneInfo = wxAuiPaneInfo()
		.CaptionVisible(m_bEdit).Dockable(!m_bNoDocking)
		.Name(PaneName).Caption(PaneName)
		.Position(GetNotebookCount());

	switch (dir)
	{
		case ADD_PANE_TOP:
			PaneInfo = PaneInfo.Top();
			break;
		case ADD_PANE_BOTTOM:
			PaneInfo = PaneInfo.Bottom();
			break;
		case ADD_PANE_LEFT:
			PaneInfo = PaneInfo.Left();
			break;
		case ADD_PANE_RIGHT:
			PaneInfo = PaneInfo.Right();
			break;
		case ADD_PANE_CENTER:
			PaneInfo = PaneInfo.Center();
			break;
		default:
			break;
	}

	m_Mgr->AddPane(CreateEmptyNotebook(), PaneInfo);

	AddRemoveBlankPage();
	m_Mgr->Update();
}

wxWindow * CFrame::GetNotebookPageFromId(wxWindowID Id)
{
	for (u32 i = 0; i < m_Mgr->GetAllPanes().GetCount(); i++)
	{
		if (!m_Mgr->GetAllPanes()[i].window->IsKindOf(CLASSINFO(wxAuiNotebook)))
			continue;

		wxAuiNotebook * NB = (wxAuiNotebook*)m_Mgr->GetAllPanes()[i].window;
		for (u32 j = 0; j < NB->GetPageCount(); j++)
		{
			if (NB->GetPage(j)->GetId() == Id)
				return NB->GetPage(j);
		}
	}
	return nullptr;
}

wxFrame* CFrame::CreateParentFrame(wxWindowID Id, const wxString& Title, wxWindow* Child)
{
	wxFrame* Frame = new wxFrame(this, Id, Title);

	Child->Reparent(Frame);

	wxBoxSizer* m_MainSizer = new wxBoxSizer(wxHORIZONTAL);

	m_MainSizer->Add(Child, 1, wxEXPAND);

	Frame->Bind(wxEVT_CLOSE_WINDOW, &CFrame::OnFloatingPageClosed, this);

	// Main sizer
	Frame->SetSizer(m_MainSizer);
	// Minimum frame size
	Frame->SetMinSize(wxSize(200, 200));
	Frame->Show();
	return Frame;
}

wxAuiNotebook* CFrame::CreateEmptyNotebook()
{
	const long NOTEBOOK_STYLE = wxAUI_NB_TOP | wxAUI_NB_TAB_SPLIT |
		wxAUI_NB_TAB_MOVE |
		wxAUI_NB_TAB_EXTERNAL_MOVE | wxAUI_NB_SCROLL_BUTTONS |
		wxAUI_NB_WINDOWLIST_BUTTON | wxNO_BORDER;

	return new wxAuiNotebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, NOTEBOOK_STYLE);
}

void CFrame::AddRemoveBlankPage()
{
	for (u32 i = 0; i < m_Mgr->GetAllPanes().GetCount(); i++)
	{
		if (!m_Mgr->GetAllPanes()[i].window->IsKindOf(CLASSINFO(wxAuiNotebook)))
			continue;

		wxAuiNotebook * NB = (wxAuiNotebook*)m_Mgr->GetAllPanes()[i].window;
		for (u32 j = 0; j < NB->GetPageCount(); j++)
		{
			if (NB->GetPageText(j).IsSameAs("<>") && NB->GetPageCount() > 1)
				NB->DeletePage(j);
		}

		if (NB->GetPageCount() == 0)
			NB->AddPage(new wxPanel(this, wxID_ANY), "<>", true);
	}
}

int CFrame::GetNotebookAffiliation(wxWindowID Id)
{
	for (u32 i = 0, j = 0; i < m_Mgr->GetAllPanes().GetCount(); i++)
	{
		if (!m_Mgr->GetAllPanes()[i].window->IsKindOf(CLASSINFO(wxAuiNotebook)))
			continue;

		wxAuiNotebook * NB = (wxAuiNotebook*)m_Mgr->GetAllPanes()[i].window;
		for (u32 k = 0; k < NB->GetPageCount(); k++)
		{
			if (NB->GetPage(k)->GetId() == Id)
				return j;
		}
		j++;
	}
	return -1;
}

// Close all panes with notebooks
void CFrame::CloseAllNotebooks()
{
	wxAuiPaneInfoArray AllPanes = m_Mgr->GetAllPanes();
	for (u32 i = 0; i < AllPanes.GetCount(); i++)
	{
		if (AllPanes[i].window->IsKindOf(CLASSINFO(wxAuiNotebook)))
		{
			AllPanes[i].DestroyOnClose(true);
			m_Mgr->ClosePane(AllPanes[i]);
		}
	}
}

int CFrame::GetNotebookCount()
{
	int Ret = 0;
	for (u32 i = 0; i < m_Mgr->GetAllPanes().GetCount(); i++)
	{
		if (m_Mgr->GetAllPanes()[i].window->IsKindOf(CLASSINFO(wxAuiNotebook)))
			Ret++;
	}
	return Ret;
}

wxAuiNotebook * CFrame::GetNotebookFromId(u32 NBId)
{
	for (u32 i = 0, j = 0; i < m_Mgr->GetAllPanes().GetCount(); i++)
	{
		if (!m_Mgr->GetAllPanes()[i].window->IsKindOf(CLASSINFO(wxAuiNotebook)))
			continue;
		if (j == NBId)
			return (wxAuiNotebook*)m_Mgr->GetAllPanes()[i].window;
		j++;
	}
	return nullptr;
}