4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file date_gui.cpp Graphical selection of a date. */
13 #include "strings_func.h"
14 #include "date_func.h"
15 #include "window_func.h"
16 #include "window_gui.h"
18 #include "core/geometry_func.hpp"
20 #include "widgets/dropdown_type.h"
21 #include "widgets/date_widget.h"
24 /** Window to select a date graphically by using dropdowns */
25 struct SetDateWindow
: Window
{
26 SetDateCallback
*callback
; ///< Callback to call when a date has been selected
27 YearMonthDay date
; ///< The currently selected date
28 Year min_year
; ///< The minimum year in the year dropdown
29 Year max_year
; ///< The maximum year (inclusive) in the year dropdown
32 * Create the new 'set date' window
33 * @param desc the window description
34 * @param window_number number of the window
35 * @param parent the parent window, i.e. if this closes we should close too
36 * @param initial_date the initial date to show
37 * @param min_year the minimum year to show in the year dropdown
38 * @param max_year the maximum year (inclusive) to show in the year dropdown
39 * @param callback the callback to call once a date has been selected
41 SetDateWindow (const WindowDesc
*desc
, WindowNumber window_number
, Window
*parent
, Date initial_date
, Year min_year
, Year max_year
, SetDateCallback
*callback
) :
44 min_year(max(MIN_YEAR
, min_year
)),
45 max_year(min(MAX_YEAR
, max_year
))
47 assert(this->min_year
<= this->max_year
);
49 if (initial_date
== 0) initial_date
= _date
;
50 ConvertDateToYMD(initial_date
, &this->date
);
51 this->date
.year
= Clamp(this->date
.year
, min_year
, max_year
);
53 this->parent
= parent
;
54 this->InitNested(window_number
);
57 virtual Point
OnInitialPosition(int16 sm_width
, int16 sm_height
, int window_number
)
59 Point pt
= { this->parent
->left
+ this->parent
->width
/ 2 - sm_width
/ 2, this->parent
->top
+ this->parent
->height
/ 2 - sm_height
/ 2 };
64 * Helper function to construct the dropdown.
65 * @param widget the dropdown widget to create the dropdown for
67 void ShowDateDropDown(int widget
)
70 DropDownList
*list
= new DropDownList();
73 default: NOT_REACHED();
76 for (uint i
= 0; i
< 31; i
++) {
77 *list
->Append() = new DropDownListStringItem(STR_DAY_NUMBER_1ST
+ i
, i
+ 1, false);
79 selected
= this->date
.day
;
83 for (uint i
= 0; i
< 12; i
++) {
84 *list
->Append() = new DropDownListStringItem(STR_MONTH_JAN
+ i
, i
, false);
86 selected
= this->date
.month
;
90 for (Year i
= this->min_year
; i
<= this->max_year
; i
++) {
91 DropDownListParamStringItem
*item
= new DropDownListParamStringItem(STR_JUST_INT
, i
, false);
93 *list
->Append() = item
;
95 selected
= this->date
.year
;
99 ShowDropDownList(this, list
, selected
, widget
);
102 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
104 Dimension d
= {0, 0};
109 for (uint i
= 0; i
< 31; i
++) {
110 d
= maxdim(d
, GetStringBoundingBox(STR_DAY_NUMBER_1ST
+ i
));
115 for (uint i
= 0; i
< 12; i
++) {
116 d
= maxdim(d
, GetStringBoundingBox(STR_MONTH_JAN
+ i
));
121 SetDParamMaxValue(0, this->max_year
);
122 d
= maxdim(d
, GetStringBoundingBox(STR_JUST_INT
));
126 d
.width
+= padding
.width
;
127 d
.height
+= padding
.height
;
131 virtual void SetStringParameters(int widget
) const
134 case WID_SD_DAY
: SetDParam(0, this->date
.day
- 1 + STR_DAY_NUMBER_1ST
); break;
135 case WID_SD_MONTH
: SetDParam(0, this->date
.month
+ STR_MONTH_JAN
); break;
136 case WID_SD_YEAR
: SetDParam(0, this->date
.year
); break;
140 virtual void OnClick(Point pt
, int widget
, int click_count
)
146 ShowDateDropDown(widget
);
149 case WID_SD_SET_DATE
:
150 if (this->callback
!= NULL
) this->callback(this, ConvertYMDToDate(this->date
.year
, this->date
.month
, this->date
.day
));
156 virtual void OnDropdownSelect(int widget
, int index
)
160 this->date
.day
= index
;
164 this->date
.month
= index
;
168 this->date
.year
= index
;
175 /** Widgets for the date setting window. */
176 static const NWidgetPart _nested_set_date_widgets
[] = {
177 NWidget(NWID_HORIZONTAL
),
178 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
179 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_DATE_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
181 NWidget(WWT_PANEL
, COLOUR_BROWN
),
182 NWidget(NWID_VERTICAL
), SetPIP(6, 6, 6),
183 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(6, 6, 6),
184 NWidget(WWT_DROPDOWN
, COLOUR_ORANGE
, WID_SD_DAY
), SetFill(1, 0), SetDataTip(STR_JUST_STRING
, STR_DATE_DAY_TOOLTIP
),
185 NWidget(WWT_DROPDOWN
, COLOUR_ORANGE
, WID_SD_MONTH
), SetFill(1, 0), SetDataTip(STR_JUST_STRING
, STR_DATE_MONTH_TOOLTIP
),
186 NWidget(WWT_DROPDOWN
, COLOUR_ORANGE
, WID_SD_YEAR
), SetFill(1, 0), SetDataTip(STR_JUST_INT
, STR_DATE_YEAR_TOOLTIP
),
188 NWidget(NWID_HORIZONTAL
),
189 NWidget(NWID_SPACER
), SetFill(1, 0),
190 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_SD_SET_DATE
), SetMinimalSize(100, 12), SetDataTip(STR_DATE_SET_DATE
, STR_DATE_SET_DATE_TOOLTIP
),
191 NWidget(NWID_SPACER
), SetFill(1, 0),
197 /** Description of the date setting window. */
198 static const WindowDesc
_set_date_desc(
200 WC_SET_DATE
, WC_NONE
,
202 _nested_set_date_widgets
, lengthof(_nested_set_date_widgets
)
206 * Create the new 'set date' window
207 * @param window_number number for the window
208 * @param parent the parent window, i.e. if this closes we should close too
209 * @param initial_date the initial date to show
210 * @param min_year the minimum year to show in the year dropdown
211 * @param max_year the maximum year (inclusive) to show in the year dropdown
212 * @param callback the callback to call once a date has been selected
214 void ShowSetDateWindow(Window
*parent
, int window_number
, Date initial_date
, Year min_year
, Year max_year
, SetDateCallback
*callback
)
216 DeleteWindowByClass(WC_SET_DATE
);
217 new SetDateWindow(&_set_date_desc
, window_number
, parent
, initial_date
, min_year
, max_year
, callback
);