1 /* Reverse Engineer's Hex Editor
2  * Copyright (C) 2017-2021 Daniel Collins <solemnwarning@solemnwarning.net>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 51
15  * Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 */
17 
18 #include "platform.hpp"
19 
20 #include "App.hpp"
21 #include "ArtProvider.hpp"
22 #include "mainwindow.hpp"
23 #include "Palette.hpp"
24 
25 /* These MUST come after any wxWidgets headers. */
26 #ifdef _WIN32
27 #include <objbase.h>
28 #endif
29 
30 IMPLEMENT_APP(REHex::App);
31 
OnInit()32 bool REHex::App::OnInit()
33 {
34 	locale = new wxLocale(wxLANGUAGE_DEFAULT);
35 	console = new ConsoleBuffer();
36 
37 	call_setup_hooks(SetupPhase::EARLY);
38 
39 	#ifdef _WIN32
40 	/* Needed for shell API calls. */
41 	CoInitialize(NULL);
42 	#endif
43 
44 	wxImage::AddHandler(new wxPNGHandler);
45 
46 	ArtProvider::init();
47 
48 	config = new wxConfig("REHex");
49 
50 	config->SetPath("/");
51 	last_directory = config->Read("last-directory", "");
52 	font_size_adjustment = config->ReadLong("font-size-adjustment", 0);
53 
54 	{
55 		wxFont default_font(wxFontInfo().Family(wxFONTFAMILY_MODERN));
56 		font_name = default_font.GetFaceName();
57 
58 		set_font_name(config->Read("font-name", font_name).ToStdString());
59 	}
60 
61 	/* Display default tool panels if a default view hasn't been configured. */
62 	if(!config->HasGroup("/default-view/"))
63 	{
64 		config->SetPath("/default-view/vtools/panels/0/tab/0");
65 		config->Write("name", "DecodePanel");
66 		config->Write("selected", true);
67 		config->Write("big-endian", false);
68 
69 		config->SetPath("/default-view/vtools/panels/0/tab/1");
70 		config->Write("name", "CommentTree");
71 		config->Write("selected", false);
72 	}
73 
74 	recent_files = new wxFileHistory();
75 
76 	config->SetPath("/recent-files/");
77 	recent_files->Load(*config);
78 
79 	config->SetPath("/");
80 
81 	std::string theme = config->Read("theme", "system").ToStdString();
82 	if(theme == "light")
83 	{
84 		active_palette = Palette::create_light_palette();
85 	}
86 	else if(theme == "dark")
87 	{
88 		active_palette = Palette::create_dark_palette();
89 	}
90 	else /* if(theme == "system") */
91 	{
92 		active_palette = Palette::create_system_palette();
93 	}
94 
95 	call_setup_hooks(SetupPhase::READY);
96 
97 	wxSize windowSize(740, 540);
98 
99 	#ifndef __APPLE__
100 	config->Read("/default-view/window-width", &windowSize.x, windowSize.x);
101 	config->Read("/default-view/window-height", &windowSize.y, windowSize.y);
102 	#endif
103 
104 	REHex::MainWindow *window = new REHex::MainWindow(windowSize);
105 
106 	#ifndef __APPLE__
107 	bool maximise = config->ReadBool("/default-view/window-maximised", false);
108 	window->Maximize(maximise);
109 	#endif
110 
111 	window->Show(true);
112 
113 	if(argc > 1)
114 	{
115 		for(int i = 1; i < argc; ++i)
116 		{
117 			window->open_file(argv[i].ToStdString());
118 		}
119 	}
120 	else{
121 		window->new_file();
122 	}
123 
124 	call_setup_hooks(SetupPhase::DONE);
125 
126 	return true;
127 }
128 
OnExit()129 int REHex::App::OnExit()
130 {
131 	call_setup_hooks(SetupPhase::SHUTDOWN);
132 
133 	config->SetPath("/recent-files/");
134 	recent_files->Save(*config);
135 
136 	config->SetPath("/");
137 	config->Write("last-directory", wxString(last_directory));
138 
139 	delete active_palette;
140 	delete recent_files;
141 	delete config;
142 
143 	#ifdef _WIN32
144 	CoUninitialize();
145 	#endif
146 
147 	call_setup_hooks(SetupPhase::SHUTDOWN_LATE);
148 
149 	delete console;
150 	console = NULL;
151 
152 	delete locale;
153 	locale = NULL;
154 
155 	return 0;
156 }
157