changeset 20339:4bafd180b961 draft

(svn r25291) -Add: WindowDesc ability to load and store information in a config file.
author frosch <frosch@openttd.org>
date Sun, 26 May 2013 19:27:22 +0000
parents 491ddf49f744
children 32f421dff989
files projects/settings_vs100.vcxproj projects/settings_vs100.vcxproj.filters projects/settings_vs80.vcproj projects/settings_vs90.vcproj src/fileio.cpp src/openttd.cpp src/settings.cpp src/settings_func.h src/table/window_settings.ini src/window.cpp src/window_gui.h
diffstat 11 files changed, 140 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/projects/settings_vs100.vcxproj
+++ b/projects/settings_vs100.vcxproj
@@ -23,7 +23,7 @@
   </ImportGroup>
   <PropertyGroup Label="UserMacros">
     <SettingsCommandLine>
-..\objs\settings\settings_gen.exe -o ..\objs\settings\table\settings.h -b ..\src\table\settings.h.preamble -a ..\src\table\settings.h.postamble ..\src\table\company_settings.ini ..\src\table\currency_settings.ini ..\src\table\gameopt_settings.ini ..\src\table\misc_settings.ini ..\src\table\settings.ini ..\src\table\win32_settings.ini
+..\objs\settings\settings_gen.exe -o ..\objs\settings\table\settings.h -b ..\src\table\settings.h.preamble -a ..\src\table\settings.h.postamble ..\src\table\company_settings.ini ..\src\table\currency_settings.ini ..\src\table\gameopt_settings.ini ..\src\table\misc_settings.ini ..\src\table\settings.ini ..\src\table\win32_settings.ini ..\src\table\window_settings.ini
     </SettingsCommandLine>
   </PropertyGroup>
   <PropertyGroup>
@@ -42,6 +42,7 @@
     <None Include="..\src\table\misc_settings.ini" />
     <None Include="..\src\table\settings.ini" />
     <None Include="..\src\table\win32_settings.ini" />
+    <None Include="..\src\table\window_settings.ini" />
   </ItemGroup>
   <ItemGroup>
     <None Include="..\src\table\settings.h.postamble" />
--- a/projects/settings_vs100.vcxproj.filters
+++ b/projects/settings_vs100.vcxproj.filters
@@ -24,6 +24,9 @@
     <None Include="..\src\table\win32_settings.ini">
       <Filter>INI</Filter>
     </None>
+    <None Include="..\src\table\window_settings.ini">
+      <Filter>INI</Filter>
+    </None>
   </ItemGroup>
   <ItemGroup>
     <None Include="..\src\table\settings.h.postamble" />
--- a/projects/settings_vs80.vcproj
+++ b/projects/settings_vs80.vcproj
@@ -24,7 +24,7 @@
 				Name="VCPreBuildEventTool"
 				Description="Generating settings.h"
 				CommandLine="
-..\objs\settings\settings_gen.exe -o ..\objs\settings\table\settings.h -b ..\src\table\settings.h.preamble -a ..\src\table\settings.h.postamble ..\src\table\company_settings.ini ..\src\table\currency_settings.ini ..\src\table\gameopt_settings.ini ..\src\table\misc_settings.ini ..\src\table\settings.ini ..\src\table\win32_settings.ini
+..\objs\settings\settings_gen.exe -o ..\objs\settings\table\settings.h -b ..\src\table\settings.h.preamble -a ..\src\table\settings.h.postamble ..\src\table\company_settings.ini ..\src\table\currency_settings.ini ..\src\table\gameopt_settings.ini ..\src\table\misc_settings.ini ..\src\table\settings.ini ..\src\table\win32_settings.ini ..\src\table\window_settings.ini
 				"
 			/>
 			<Tool
@@ -68,6 +68,10 @@
 			RelativePath="..\src\table\win32_settings.ini"
 			>
 		</File>
+		<File
+			RelativePath="..\src\table\window_settings.ini"
+			>
+		</File>
 		</Filter>
 		<File
 			RelativePath="..\src\table\settings.h.postamble"
--- a/projects/settings_vs90.vcproj
+++ b/projects/settings_vs90.vcproj
@@ -25,7 +25,7 @@
 				Name="VCPreBuildEventTool"
 				Description="Generating settings.h"
 				CommandLine="
-..\objs\settings\settings_gen.exe -o ..\objs\settings\table\settings.h -b ..\src\table\settings.h.preamble -a ..\src\table\settings.h.postamble ..\src\table\company_settings.ini ..\src\table\currency_settings.ini ..\src\table\gameopt_settings.ini ..\src\table\misc_settings.ini ..\src\table\settings.ini ..\src\table\win32_settings.ini
+..\objs\settings\settings_gen.exe -o ..\objs\settings\table\settings.h -b ..\src\table\settings.h.preamble -a ..\src\table\settings.h.postamble ..\src\table\company_settings.ini ..\src\table\currency_settings.ini ..\src\table\gameopt_settings.ini ..\src\table\misc_settings.ini ..\src\table\settings.ini ..\src\table\win32_settings.ini ..\src\table\window_settings.ini
 				"
 			/>
 			<Tool
@@ -69,6 +69,10 @@
 			RelativePath="..\src\table\win32_settings.ini"
 			>
 		</File>
+		<File
+			RelativePath="..\src\table\window_settings.ini"
+			>
+		</File>
 		</Filter>
 		<File
 			RelativePath="..\src\table\settings.h.postamble"
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -1196,6 +1196,8 @@
 	_highscore_file = str_fmt("%shs.dat", _personal_dir);
 	extern char *_hotkeys_file;
 	_hotkeys_file = str_fmt("%shotkeys.cfg",  _personal_dir);
+	extern char *_windows_file;
+	_windows_file = str_fmt("%swindows.cfg",  _personal_dir);
 
 	/* Make the necessary folders */
 #if !defined(__MORPHOS__) && !defined(__AMIGA__) && defined(WITH_PERSONAL_DIR)
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -425,6 +425,7 @@
 		CheckConfig();
 		LoadFromHighScore();
 		LoadHotkeysFromConfig();
+		WindowDesc::LoadFromConfig();
 
 		/* We have loaded the config, so we may possibly save it. */
 		*save_config_ptr = save_config;
@@ -852,6 +853,7 @@
 	if (save_config) {
 		SaveToConfig();
 		SaveHotkeysToConfig();
+		WindowDesc::SaveToConfig();
 		SaveToHighScore();
 	}
 
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -733,6 +733,28 @@
 }
 
 /**
+ * Load a WindowDesc from config.
+ * @param ini IniFile handle to the ini file with the source data
+ * @param grpname character string identifying the section-header of the ini file that will be parsed
+ * @param desc Destination WindowDesc
+ */
+void IniLoadWindowSettings(IniFile *ini, const char *grpname, void *desc)
+{
+	IniLoadSettings(ini, _window_settings, grpname, desc);
+}
+
+/**
+ * Save a WindowDesc to config.
+ * @param ini IniFile handle to the ini file where the destination data is saved
+ * @param grpname character string identifying the section-header of the ini file
+ * @param desc Source WindowDesc
+ */
+void IniSaveWindowSettings(IniFile *ini, const char *grpname, void *desc)
+{
+	IniSaveSettings(ini, _window_settings, grpname, desc);
+}
+
+/**
  * Check whether the setting is editable in the current gamemode.
  * @param do_command true if this is about checking a command from the server.
  * @return true if editable.
--- a/src/settings_func.h
+++ b/src/settings_func.h
@@ -15,6 +15,8 @@
 #include "core/smallvec_type.hpp"
 #include "company_type.h"
 
+struct IniFile;
+
 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame = false);
 void IConsoleSetSetting(const char *name, int32 value);
 void IConsoleGetSetting(const char *name, bool force_newgame = false);
@@ -24,6 +26,9 @@
 void SaveToConfig();
 void CheckConfig();
 
+void IniLoadWindowSettings(IniFile *ini, const char *grpname, void *desc);
+void IniSaveWindowSettings(IniFile *ini, const char *grpname, void *desc);
+
 /* Functions to load and save NewGRF settings to a separate
  * configuration file, used for presets. */
 typedef AutoFreeSmallVector<char *, 4> GRFPresetList;
new file mode 100644
--- /dev/null
+++ b/src/table/window_settings.ini
@@ -0,0 +1,37 @@
+; $Id$
+;
+; This file is part of OpenTTD.
+; OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
+; OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+; See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
+;
+
+[pre-amble]
+
+static const SettingDesc _window_settings[] = {
+[post-amble]
+};
+[templates]
+SDT_BOOL = SDT_BOOL($base, $var,        $flags, $guiflags, $def,                        $str, $strhelp, $strval, $proc, $from, $to, $cat),
+SDT_VAR  =  SDT_VAR($base, $var, $type, $flags, $guiflags, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat),
+SDT_END  = SDT_END()
+
+[defaults]
+base     = WindowDesc
+flags    = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC
+guiflags = 0
+interval = 0
+str      = STR_NULL
+strhelp  = STR_CONFIG_SETTING_NO_EXPLANATION_AVAILABLE_HELPTEXT
+strval   = STR_NULL
+proc     = NULL
+load     = NULL
+from     = 0
+to       = SL_MAX_VERSION
+cat      = SC_ADVANCED
+
+
+
+[SDT_END]
+
+};
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -27,6 +27,8 @@
 #include "widgets/dropdown_func.h"
 #include "strings_func.h"
 #include "settings_type.h"
+#include "settings_func.h"
+#include "ini_type.h"
 #include "newgrf_debug.h"
 #include "hotkeys.h"
 #include "toolbar_gui.h"
@@ -72,6 +74,15 @@
 
 SpecialMouseMode _special_mouse_mode; ///< Mode of the mouse.
 
+/**
+ * List of all WindowDescs.
+ * This is a pointer to ensure initialisation order with the various static WindowDesc instances.
+ */
+static SmallVector<WindowDesc*, 16> *_window_descs = NULL;
+
+/** Config file to store WindowDesc */
+char *_windows_file;
+
 /** Window description constructor. */
 WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_width, int16 def_height,
 			WindowClass window_class, WindowClass parent_class, uint32 flags,
@@ -86,6 +97,8 @@
 	nwid_parts(nwid_parts),
 	nwid_length(nwid_length)
 {
+	if (_window_descs == NULL) _window_descs = new SmallVector<WindowDesc*, 16>();
+	*_window_descs->Append() = this;
 }
 
 WindowDesc::~WindowDesc()
@@ -93,6 +106,47 @@
 }
 
 /**
+ * Load all WindowDesc settings from _windows_file.
+ */
+void WindowDesc::LoadFromConfig()
+{
+	IniFile *ini = new IniFile();
+	ini->LoadFromDisk(_windows_file, BASE_DIR);
+	for (WindowDesc **it = _window_descs->Begin(); it != _window_descs->End(); ++it) {
+		if ((*it)->ini_key == NULL) continue;
+		IniLoadWindowSettings(ini, (*it)->ini_key, *it);
+	}
+	delete ini;
+}
+
+/**
+ * Sort WindowDesc by ini_key.
+ */
+static int DescSorter(WindowDesc * const *a, WindowDesc * const *b)
+{
+	if ((*a)->ini_key != NULL && (*b)->ini_key != NULL) return strcmp((*a)->ini_key, (*b)->ini_key);
+	return ((*b)->ini_key != NULL ? 1 : 0) - ((*a)->ini_key != NULL ? 1 : 0);
+}
+
+/**
+ * Save all WindowDesc settings to _windows_file.
+ */
+void WindowDesc::SaveToConfig()
+{
+	/* Sort the stuff to get a nice ini file on first write */
+	QSortT(_window_descs->Begin(), _window_descs->Length(), DescSorter);
+
+	IniFile *ini = new IniFile();
+	ini->LoadFromDisk(_windows_file, BASE_DIR);
+	for (WindowDesc **it = _window_descs->Begin(); it != _window_descs->End(); ++it) {
+		if ((*it)->ini_key == NULL) continue;
+		IniSaveWindowSettings(ini, (*it)->ini_key, *it);
+	}
+	ini->SaveToDisk(_windows_file);
+	delete ini;
+}
+
+/**
  * Compute the row of a widget that a user clicked in.
  * @param clickpos    Vertical position of the mouse click.
  * @param widget      Widget number of the widget clicked in.
--- a/src/window_gui.h
+++ b/src/window_gui.h
@@ -180,6 +180,9 @@
 	uint32 flags;                  ///< Flags. @see WindowDefaultFlag
 	const NWidgetPart *nwid_parts; ///< Nested widget parts describing the window.
 	int16 nwid_length;             ///< Length of the #nwid_parts array.
+
+	static void LoadFromConfig();
+	static void SaveToConfig();
 };
 
 /**