diff options
author | frosch <frosch@openttd.org> | 2013-05-26 19:27:22 +0000 |
---|---|---|
committer | frosch <frosch@openttd.org> | 2013-05-26 19:27:22 +0000 |
commit | 23ba42b66ccf915230cab163cdeca19db6e95f76 (patch) | |
tree | eea2f6daffe6fc9283487ed3736dde80ebc87825 | |
parent | 8157a8afd814cb24aaaa4c725817c864e0c583c6 (diff) | |
download | openttd-23ba42b66ccf915230cab163cdeca19db6e95f76.tar.xz |
(svn r25291) -Add: WindowDesc ability to load and store information in a config file.
-rw-r--r-- | projects/settings_vs100.vcxproj | 3 | ||||
-rw-r--r-- | projects/settings_vs100.vcxproj.filters | 3 | ||||
-rw-r--r-- | projects/settings_vs80.vcproj | 6 | ||||
-rw-r--r-- | projects/settings_vs90.vcproj | 6 | ||||
-rw-r--r-- | src/fileio.cpp | 2 | ||||
-rw-r--r-- | src/openttd.cpp | 2 | ||||
-rw-r--r-- | src/settings.cpp | 22 | ||||
-rw-r--r-- | src/settings_func.h | 5 | ||||
-rw-r--r-- | src/table/window_settings.ini | 37 | ||||
-rw-r--r-- | src/window.cpp | 54 | ||||
-rw-r--r-- | src/window_gui.h | 3 |
11 files changed, 140 insertions, 3 deletions
diff --git a/projects/settings_vs100.vcxproj b/projects/settings_vs100.vcxproj index 51f1f5b5e..61b289757 100644 --- 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" /> diff --git a/projects/settings_vs100.vcxproj.filters b/projects/settings_vs100.vcxproj.filters index 2c579391b..8caf9fa0a 100644 --- 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" /> diff --git a/projects/settings_vs80.vcproj b/projects/settings_vs80.vcproj index 9ecc87097..0084dcbc5 100644 --- 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" diff --git a/projects/settings_vs90.vcproj b/projects/settings_vs90.vcproj index ac6a75ee8..4361e8df5 100644 --- 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" diff --git a/src/fileio.cpp b/src/fileio.cpp index e97565262..8105e4ad0 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -1196,6 +1196,8 @@ void DeterminePaths(const char *exe) _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) diff --git a/src/openttd.cpp b/src/openttd.cpp index ffecade0a..2386e4798 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -425,6 +425,7 @@ struct AfterNewGRFScan : NewGRFScanCallback { 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 @@ int ttd_main(int argc, char *argv[]) if (save_config) { SaveToConfig(); SaveHotkeysToConfig(); + WindowDesc::SaveToConfig(); SaveToHighScore(); } diff --git a/src/settings.cpp b/src/settings.cpp index 293ce406d..4a2b66e6b 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -733,6 +733,28 @@ static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *li } /** + * 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. diff --git a/src/settings_func.h b/src/settings_func.h index c6f0892c7..3b3387b5f 100644 --- 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 LoadFromConfig(bool minimal = false); 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; diff --git a/src/table/window_settings.ini b/src/table/window_settings.ini new file mode 100644 index 000000000..766ab5b5b --- /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] + +}; diff --git a/src/window.cpp b/src/window.cpp index 035050bcb..a50890d42 100644 --- 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 @@ bool _mouse_hovering; ///< The mouse is hovering over the same point. 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 @@ WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_wi 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 @@ WindowDesc::~WindowDesc() } /** + * 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. diff --git a/src/window_gui.h b/src/window_gui.h index 59df38052..918c3374a 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -180,6 +180,9 @@ struct WindowDesc : ZeroedMemoryAllocator { 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(); }; /** |