From 23ba42b66ccf915230cab163cdeca19db6e95f76 Mon Sep 17 00:00:00 2001 From: frosch Date: Sun, 26 May 2013 19:27:22 +0000 Subject: [PATCH] (svn r25291) -Add: WindowDesc ability to load and store information in a config file. --- projects/settings_vs100.vcxproj | 3 +- projects/settings_vs100.vcxproj.filters | 3 ++ projects/settings_vs80.vcproj | 6 ++- projects/settings_vs90.vcproj | 6 ++- 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 ++ 11 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 src/table/window_settings.ini diff --git a/projects/settings_vs100.vcxproj b/projects/settings_vs100.vcxproj index 51f1f5b5e7..61b289757a 100644 --- a/projects/settings_vs100.vcxproj +++ b/projects/settings_vs100.vcxproj @@ -23,7 +23,7 @@ -..\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 @@ -42,6 +42,7 @@ + diff --git a/projects/settings_vs100.vcxproj.filters b/projects/settings_vs100.vcxproj.filters index 2c579391be..8caf9fa0a6 100644 --- a/projects/settings_vs100.vcxproj.filters +++ b/projects/settings_vs100.vcxproj.filters @@ -24,6 +24,9 @@ INI + + INI + diff --git a/projects/settings_vs80.vcproj b/projects/settings_vs80.vcproj index 9ecc870971..0084dcbc57 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 " /> + + + + GRFPresetList; diff --git a/src/table/window_settings.ini b/src/table/window_settings.ini new file mode 100644 index 0000000000..766ab5b5b6 --- /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 035050bcbe..a50890d421 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 59df380522..918c3374a2 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(); }; /**