From 23ba42b66ccf915230cab163cdeca19db6e95f76 Mon Sep 17 00:00:00 2001 From: frosch Date: Sun, 26 May 2013 19:27:22 +0000 Subject: (svn r25291) -Add: WindowDesc ability to load and store information in a config file. --- src/fileio.cpp | 2 ++ src/openttd.cpp | 2 ++ src/settings.cpp | 22 ++++++++++++++++++ src/settings_func.h | 5 ++++ src/table/window_settings.ini | 37 +++++++++++++++++++++++++++++ src/window.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++ src/window_gui.h | 3 +++ 7 files changed, 125 insertions(+) create mode 100644 src/table/window_settings.ini (limited to 'src') 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 @@ -732,6 +732,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. 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 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 . +; + +[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 *_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,12 +97,55 @@ 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(); + *_window_descs->Append() = this; } 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. 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(); }; /** -- cgit v1.2.3-70-g09d2