@@ -44,6 +44,7 @@ private void btnCancel_Click(object sender, EventArgs e)
4444 private void btnApply_Click ( object sender , EventArgs e )
4545 {
4646 SaveSettings ( ) ;
47+ btnApply . Enabled = false ;
4748 }
4849
4950 private void btnOK_Click ( object sender , EventArgs e )
@@ -54,18 +55,18 @@ private void btnOK_Click(object sender, EventArgs e)
5455
5556 private void txt_TextChanged ( object sender , EventArgs e )
5657 {
57- if ( string . IsNullOrWhiteSpace ( txtCFGdir . Text ) || string . IsNullOrWhiteSpace ( txtEXEdir . Text ) )
58+ if ( string . IsNullOrWhiteSpace ( txtEXEdir . Text ) || string . IsNullOrWhiteSpace ( txtCFGdir . Text ) )
5859 {
5960 btnOK . Enabled = false ;
6061 }
6162 else
6263 {
63- //btnApply.Enabled = true;
64- settingsChanged = true ;
64+ settingsChanged = CheckForChanges ( ) ;
6565 btnOK . Enabled = true ;
6666 }
6767 }
6868
69+ //TODO: Rewrite
6970 //Save the settings to the registry
7071 private void SaveSettings ( )
7172 {
@@ -76,6 +77,7 @@ private void SaveSettings()
7677 {
7778 Registry . CurrentUser . CreateSubKey ( @"SOFTWARE\86Box" ) ;
7879 regkey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\86Box" , true ) ;
80+ regkey . CreateSubKey ( "Virtual Machines" ) ;
7981 }
8082
8183 //Store the new values, close the key, changes are saved
@@ -86,29 +88,41 @@ private void SaveSettings()
8688 regkey . SetValue ( "MinimizeToTray" , cbxMinimizeTray . Checked , RegistryValueKind . DWord ) ;
8789 regkey . SetValue ( "CloseToTray" , cbxCloseTray . Checked , RegistryValueKind . DWord ) ;
8890 regkey . Close ( ) ;
89- settingsChanged = false ;
91+
92+ settingsChanged = CheckForChanges ( ) ;
9093 }
9194 catch ( Exception ex )
9295 {
9396 MessageBox . Show ( "An error has occurred. Please provide the following information to the developer:\n " + ex . Message + "\n " + ex . StackTrace , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
9497 }
9598 }
9699
100+ //TODO: Rewrite
97101 //Read the settings from the registry
98102 private void LoadSettings ( )
99103 {
100104 try
101105 {
102- RegistryKey regkey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\86Box" ) ; //Open the key as read only
106+ RegistryKey regkey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\86Box" , false ) ; //Open the key as read only
107+
108+ //If the key doesn't exist yet, fallback to defaults
103109 if ( regkey == null )
104- { //Key doesn't exist yet, fallback to defaults
110+ {
111+ MessageBox . Show ( "86Box Manager settings could not be loaded. This is normal if you're running 86Box Manager for the first time. Default values will be used." , "Warning" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
112+
113+ //Create the key and reopen it for write access
105114 Registry . CurrentUser . CreateSubKey ( @"SOFTWARE\86Box" ) ;
106- txtCFGdir . Text = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) + @"\86Box VMs" ;
107- txtEXEdir . Text = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFilesX86 ) + @"\86Box" ;
115+ regkey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\86Box" , true ) ;
116+ regkey . CreateSubKey ( "Virtual Machines" ) ;
117+
118+ txtCFGdir . Text = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) + @"\86Box VMs\" ;
119+ txtEXEdir . Text = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFilesX86 ) + @"\86Box\" ;
108120 cbxMinimize . Checked = false ;
109121 cbxShowConsole . Checked = true ;
110122 cbxMinimizeTray . Checked = false ;
111123 cbxCloseTray . Checked = false ;
124+
125+ SaveSettings ( ) ; //This will write the default values to the registry
112126 }
113127 else
114128 {
@@ -118,17 +132,18 @@ private void LoadSettings()
118132 cbxShowConsole . Checked = Convert . ToBoolean ( regkey . GetValue ( "ShowConsole" ) ) ;
119133 cbxMinimizeTray . Checked = Convert . ToBoolean ( regkey . GetValue ( "MinimizeToTray" ) ) ;
120134 cbxCloseTray . Checked = Convert . ToBoolean ( regkey . GetValue ( "CloseToTray" ) ) ;
121-
122- //This line is needed because storing the values into the textboxes (above code) triggers textchanged event
123- settingsChanged = false ;
124-
125- regkey . Close ( ) ;
126135 }
136+
137+ regkey . Close ( ) ;
127138 }
128139 catch ( Exception ex )
129140 {
130141 txtCFGdir . Text = Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) + @"\86Box Virtual Machines" ;
131142 txtEXEdir . Text = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFilesX86 ) + @"\86Box" ;
143+ cbxMinimize . Checked = false ;
144+ cbxShowConsole . Checked = true ;
145+ cbxMinimizeTray . Checked = false ;
146+ cbxCloseTray . Checked = false ;
132147 }
133148 }
134149
@@ -168,12 +183,12 @@ private void btnBrowse2_Click(object sender, EventArgs e)
168183
169184 private void cbxMinimize_CheckedChanged ( object sender , EventArgs e )
170185 {
171- settingsChanged = true ;
186+ settingsChanged = CheckForChanges ( ) ;
172187 }
173188
174189 private void cbxShowConsole_CheckedChanged ( object sender , EventArgs e )
175190 {
176- settingsChanged = true ;
191+ settingsChanged = CheckForChanges ( ) ;
177192 }
178193
179194 private void btnDefaults_Click ( object sender , EventArgs e )
@@ -184,29 +199,60 @@ private void btnDefaults_Click(object sender, EventArgs e)
184199 //Resets the settings to their default values
185200 private void ResetSettings ( )
186201 {
187- try
202+ RegistryKey regkey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\86Box" , true ) ;
203+
204+ if ( regkey == null )
188205 {
189- RegistryKey regkey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE" , true ) ; //Open the key as read only
190- Registry . CurrentUser . DeleteSubKeyTree ( @"86Box" ) ;
206+ Registry . CurrentUser . CreateSubKey ( @"SOFTWARE\86Box" ) ;
207+ regkey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\86Box" , true ) ;
208+ regkey . CreateSubKey ( "Virtual Machines" ) ;
191209 }
192- catch ( Exception ex ) { /*Do nothing, key doesn't exist anyway*/ }
193210
194- txtCFGdir . Text = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) + @"\86Box VMs" ;
195- txtEXEdir . Text = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFilesX86 ) + @"\86Box" ;
211+ txtCFGdir . Text = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) + @"\86Box VMs\ " ;
212+ txtEXEdir . Text = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFilesX86 ) + @"\86Box\ " ;
196213 cbxMinimize . Checked = false ;
197214 cbxShowConsole . Checked = true ;
198215 cbxMinimizeTray . Checked = false ;
199216 cbxCloseTray . Checked = false ;
217+
218+ SaveSettings ( ) ;
219+ regkey . Close ( ) ;
200220 }
201221
202222 private void cbxCloseTray_CheckedChanged ( object sender , EventArgs e )
203223 {
204- settingsChanged = true ;
224+ settingsChanged = CheckForChanges ( ) ;
205225 }
206226
207227 private void cbxMinimizeTray_CheckedChanged ( object sender , EventArgs e )
208228 {
209- settingsChanged = true ;
229+ settingsChanged = CheckForChanges ( ) ;
230+ }
231+
232+ //Checks if all controls match the currently saved settings to determine if any changes were made
233+ private bool CheckForChanges ( )
234+ {
235+ RegistryKey regkey = Registry . CurrentUser . OpenSubKey ( @"SOFTWARE\86Box" ) ;
236+
237+ try
238+ {
239+ btnApply . Enabled = ( txtEXEdir . Text != regkey . GetValue ( "EXEdir" ) . ToString ( ) ||
240+ txtCFGdir . Text != regkey . GetValue ( "CFGdir" ) . ToString ( ) ||
241+ cbxMinimize . Checked != Convert . ToBoolean ( regkey . GetValue ( "MinimizeOnVMStart" ) ) ||
242+ cbxShowConsole . Checked != Convert . ToBoolean ( regkey . GetValue ( "ShowConsole" ) ) ||
243+ cbxMinimizeTray . Checked != Convert . ToBoolean ( regkey . GetValue ( "MinimizeToTray" ) ) ||
244+ cbxCloseTray . Checked != Convert . ToBoolean ( regkey . GetValue ( "CloseToTray" ) ) ) ;
245+
246+ return btnApply . Enabled ;
247+ }
248+ catch ( Exception ex )
249+ {
250+ return true ; //For now let's just return true if anything goes wrong
251+ }
252+ finally
253+ {
254+ regkey . Close ( ) ;
255+ }
210256 }
211257 }
212258}
0 commit comments