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 /src/window.cpp | |
parent | 8157a8afd814cb24aaaa4c725817c864e0c583c6 (diff) | |
download | openttd-23ba42b66ccf915230cab163cdeca19db6e95f76.tar.xz |
(svn r25291) -Add: WindowDesc ability to load and store information in a config file.
Diffstat (limited to 'src/window.cpp')
-rw-r--r-- | src/window.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
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. |