2009-11-25 23:38:59 +00:00
|
|
|
/* $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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file date_gui.cpp Graphical selection of a date. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "strings_func.h"
|
|
|
|
#include "date_func.h"
|
|
|
|
#include "window_func.h"
|
|
|
|
#include "window_gui.h"
|
|
|
|
#include "date_gui.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "core/geometry_func.hpp"
|
2009-11-25 23:38:59 +00:00
|
|
|
|
|
|
|
#include "widgets/dropdown_type.h"
|
2011-12-15 22:22:55 +00:00
|
|
|
#include "widgets/date_widget.h"
|
2009-11-25 23:38:59 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2009-11-25 23:38:59 +00:00
|
|
|
|
|
|
|
/** Window to select a date graphically by using dropdowns */
|
|
|
|
struct SetDateWindow : Window {
|
|
|
|
SetDateCallback *callback; ///< Callback to call when a date has been selected
|
|
|
|
YearMonthDay date; ///< The currently selected date
|
|
|
|
Year min_year; ///< The minimum year in the year dropdown
|
|
|
|
Year max_year; ///< The maximum year (inclusive) in the year dropdown
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the new 'set date' window
|
|
|
|
* @param desc the window description
|
|
|
|
* @param window_number number of the window
|
|
|
|
* @param parent the parent window, i.e. if this closes we should close too
|
|
|
|
* @param initial_date the initial date to show
|
|
|
|
* @param min_year the minimum year to show in the year dropdown
|
|
|
|
* @param max_year the maximum year (inclusive) to show in the year dropdown
|
|
|
|
* @param callback the callback to call once a date has been selected
|
|
|
|
*/
|
2013-05-26 19:23:42 +00:00
|
|
|
SetDateWindow(WindowDesc *desc, WindowNumber window_number, Window *parent, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback) :
|
|
|
|
Window(desc),
|
2009-11-25 23:38:59 +00:00
|
|
|
callback(callback),
|
|
|
|
min_year(max(MIN_YEAR, min_year)),
|
|
|
|
max_year(min(MAX_YEAR, max_year))
|
|
|
|
{
|
|
|
|
assert(this->min_year <= this->max_year);
|
|
|
|
this->parent = parent;
|
2013-05-26 19:23:42 +00:00
|
|
|
this->InitNested(window_number);
|
2009-11-25 23:38:59 +00:00
|
|
|
|
|
|
|
if (initial_date == 0) initial_date = _date;
|
|
|
|
ConvertDateToYMD(initial_date, &this->date);
|
|
|
|
this->date.year = Clamp(this->date.year, min_year, max_year);
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
|
2009-11-25 23:38:59 +00:00
|
|
|
{
|
|
|
|
Point pt = { this->parent->left + this->parent->width / 2 - sm_width / 2, this->parent->top + this->parent->height / 2 - sm_height / 2 };
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to construct the dropdown.
|
|
|
|
* @param widget the dropdown widget to create the dropdown for
|
|
|
|
*/
|
|
|
|
void ShowDateDropDown(int widget)
|
|
|
|
{
|
|
|
|
int selected;
|
2019-04-02 19:31:24 +00:00
|
|
|
DropDownList list;
|
2009-11-25 23:38:59 +00:00
|
|
|
|
|
|
|
switch (widget) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_DAY:
|
2009-11-25 23:38:59 +00:00
|
|
|
for (uint i = 0; i < 31; i++) {
|
2019-04-02 19:31:24 +00:00
|
|
|
list.emplace_back(new DropDownListStringItem(STR_DAY_NUMBER_1ST + i, i + 1, false));
|
2009-11-25 23:38:59 +00:00
|
|
|
}
|
|
|
|
selected = this->date.day;
|
|
|
|
break;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_MONTH:
|
2009-11-25 23:38:59 +00:00
|
|
|
for (uint i = 0; i < 12; i++) {
|
2019-04-02 19:31:24 +00:00
|
|
|
list.emplace_back(new DropDownListStringItem(STR_MONTH_JAN + i, i, false));
|
2009-11-25 23:38:59 +00:00
|
|
|
}
|
|
|
|
selected = this->date.month;
|
|
|
|
break;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_YEAR:
|
2009-11-25 23:38:59 +00:00
|
|
|
for (Year i = this->min_year; i <= this->max_year; i++) {
|
|
|
|
DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
|
|
|
|
item->SetParam(0, i);
|
2019-04-02 19:31:24 +00:00
|
|
|
list.emplace_back(item);
|
2009-11-25 23:38:59 +00:00
|
|
|
}
|
|
|
|
selected = this->date.year;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-02 19:31:24 +00:00
|
|
|
ShowDropDownList(this, std::move(list), selected, widget);
|
2009-11-25 23:38:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
|
2009-11-25 23:38:59 +00:00
|
|
|
{
|
|
|
|
Dimension d = {0, 0};
|
|
|
|
switch (widget) {
|
|
|
|
default: return;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_DAY:
|
2009-11-25 23:38:59 +00:00
|
|
|
for (uint i = 0; i < 31; i++) {
|
2014-06-20 20:57:32 +00:00
|
|
|
d = maxdim(d, GetStringBoundingBox(STR_DAY_NUMBER_1ST + i));
|
2009-11-25 23:38:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_MONTH:
|
2009-11-25 23:38:59 +00:00
|
|
|
for (uint i = 0; i < 12; i++) {
|
|
|
|
d = maxdim(d, GetStringBoundingBox(STR_MONTH_JAN + i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_YEAR:
|
2012-12-08 17:18:51 +00:00
|
|
|
SetDParamMaxValue(0, this->max_year);
|
|
|
|
d = maxdim(d, GetStringBoundingBox(STR_JUST_INT));
|
2009-11-25 23:38:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
d.width += padding.width;
|
|
|
|
d.height += padding.height;
|
|
|
|
*size = d;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void SetStringParameters(int widget) const override
|
2009-11-25 23:38:59 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2014-06-20 20:57:32 +00:00
|
|
|
case WID_SD_DAY: SetDParam(0, this->date.day - 1 + STR_DAY_NUMBER_1ST); break;
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_MONTH: SetDParam(0, this->date.month + STR_MONTH_JAN); break;
|
|
|
|
case WID_SD_YEAR: SetDParam(0, this->date.year); break;
|
2009-11-25 23:38:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnClick(Point pt, int widget, int click_count) override
|
2009-11-25 23:38:59 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_DAY:
|
|
|
|
case WID_SD_MONTH:
|
|
|
|
case WID_SD_YEAR:
|
2009-11-25 23:38:59 +00:00
|
|
|
ShowDateDropDown(widget);
|
|
|
|
break;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_SET_DATE:
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->callback != nullptr) this->callback(this, ConvertYMDToDate(this->date.year, this->date.month, this->date.day));
|
2009-11-25 23:38:59 +00:00
|
|
|
delete this;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnDropdownSelect(int widget, int index) override
|
2009-11-25 23:38:59 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_DAY:
|
2009-11-25 23:38:59 +00:00
|
|
|
this->date.day = index;
|
|
|
|
break;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_MONTH:
|
2009-11-25 23:38:59 +00:00
|
|
|
this->date.month = index;
|
|
|
|
break;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_SD_YEAR:
|
2009-11-25 23:38:59 +00:00
|
|
|
this->date.year = index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-02 17:42:12 +00:00
|
|
|
/** Widgets for the date setting window. */
|
2009-11-25 23:38:59 +00:00
|
|
|
static const NWidgetPart _nested_set_date_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
|
|
|
|
NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_DATE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(WWT_PANEL, COLOUR_BROWN),
|
|
|
|
NWidget(NWID_VERTICAL), SetPIP(6, 6, 6),
|
|
|
|
NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(6, 6, 6),
|
2011-12-16 16:27:45 +00:00
|
|
|
NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_DAY), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_DAY_TOOLTIP),
|
|
|
|
NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_MONTH), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_MONTH_TOOLTIP),
|
|
|
|
NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_YEAR), SetFill(1, 0), SetDataTip(STR_JUST_INT, STR_DATE_YEAR_TOOLTIP),
|
2009-11-25 23:38:59 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(NWID_SPACER), SetFill(1, 0),
|
2011-12-16 16:27:45 +00:00
|
|
|
NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SD_SET_DATE), SetMinimalSize(100, 12), SetDataTip(STR_DATE_SET_DATE, STR_DATE_SET_DATE_TOOLTIP),
|
2009-11-25 23:38:59 +00:00
|
|
|
NWidget(NWID_SPACER), SetFill(1, 0),
|
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
EndContainer()
|
|
|
|
};
|
|
|
|
|
2011-05-02 17:42:12 +00:00
|
|
|
/** Description of the date setting window. */
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _set_date_desc(
|
2019-04-10 21:07:06 +00:00
|
|
|
WDP_CENTER, nullptr, 0, 0,
|
2009-11-25 23:38:59 +00:00
|
|
|
WC_SET_DATE, WC_NONE,
|
2012-11-11 16:10:43 +00:00
|
|
|
0,
|
2009-11-25 23:38:59 +00:00
|
|
|
_nested_set_date_widgets, lengthof(_nested_set_date_widgets)
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the new 'set date' window
|
|
|
|
* @param window_number number for the window
|
|
|
|
* @param parent the parent window, i.e. if this closes we should close too
|
|
|
|
* @param initial_date the initial date to show
|
|
|
|
* @param min_year the minimum year to show in the year dropdown
|
|
|
|
* @param max_year the maximum year (inclusive) to show in the year dropdown
|
|
|
|
* @param callback the callback to call once a date has been selected
|
|
|
|
*/
|
|
|
|
void ShowSetDateWindow(Window *parent, int window_number, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback)
|
|
|
|
{
|
|
|
|
DeleteWindowByClass(WC_SET_DATE);
|
|
|
|
new SetDateWindow(&_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback);
|
|
|
|
}
|