Changeset - r20284:215a33b6727c
[Not reviewed]
master
0 10 1
frosch - 11 years ago 2013-05-26 19:27:22
frosch@openttd.org
(svn r25291) -Add: WindowDesc ability to load and store information in a config file.
11 files changed with 140 insertions and 3 deletions:
0 comments (0 inline, 0 general)
projects/settings_vs100.vcxproj
Show inline comments
 
@@ -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" />
projects/settings_vs100.vcxproj.filters
Show inline comments
 
@@ -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" />
projects/settings_vs80.vcproj
Show inline comments
 
@@ -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"
projects/settings_vs90.vcproj
Show inline comments
 
@@ -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"
src/fileio.cpp
Show inline comments
 
@@ -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)
src/openttd.cpp
Show inline comments
 
@@ -425,6 +425,7 @@ struct AfterNewGRFScan : NewGRFScanCallb
 
		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();
 
	}
 

	
src/settings.cpp
Show inline comments
 
@@ -733,6 +733,28 @@ static void IniSaveSettingList(IniFile *
 
}
 

	
 
/**
 
 * 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.
src/settings_func.h
Show inline comments
 
@@ -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;
src/table/window_settings.ini
Show inline comments
 
new file 100644
 
; $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]
 

	
 
};
src/window.cpp
Show inline comments
 
@@ -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 mous
 

	
 
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 de
 
	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.
src/window_gui.h
Show inline comments
 
@@ -180,6 +180,9 @@ struct WindowDesc : ZeroedMemoryAllocato
 
	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();
 
};
 

	
 
/**
0 comments (0 inline, 0 general)