OpenTTD Source  12.0-beta2
date_gui.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "strings_func.h"
12 #include "date_func.h"
13 #include "window_func.h"
14 #include "window_gui.h"
15 #include "date_gui.h"
16 #include "core/geometry_func.hpp"
17 
18 #include "widgets/dropdown_type.h"
19 #include "widgets/date_widget.h"
20 
21 #include "safeguards.h"
22 
23 
30 
42  Window(desc),
44  min_year(std::max(MIN_YEAR, min_year)),
45  max_year(std::min(MAX_YEAR, max_year))
46  {
47  assert(this->min_year <= this->max_year);
48  this->parent = parent;
49  this->InitNested(window_number);
50 
51  if (initial_date == 0) initial_date = _date;
52  ConvertDateToYMD(initial_date, &this->date);
53  this->date.year = Clamp(this->date.year, min_year, max_year);
54  }
55 
56  Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
57  {
58  Point pt = { this->parent->left + this->parent->width / 2 - sm_width / 2, this->parent->top + this->parent->height / 2 - sm_height / 2 };
59  return pt;
60  }
61 
66  void ShowDateDropDown(int widget)
67  {
68  int selected;
69  DropDownList list;
70 
71  switch (widget) {
72  default: NOT_REACHED();
73 
74  case WID_SD_DAY:
75  for (uint i = 0; i < 31; i++) {
76  list.emplace_back(new DropDownListStringItem(STR_DAY_NUMBER_1ST + i, i + 1, false));
77  }
78  selected = this->date.day;
79  break;
80 
81  case WID_SD_MONTH:
82  for (uint i = 0; i < 12; i++) {
83  list.emplace_back(new DropDownListStringItem(STR_MONTH_JAN + i, i, false));
84  }
85  selected = this->date.month;
86  break;
87 
88  case WID_SD_YEAR:
89  for (Year i = this->min_year; i <= this->max_year; i++) {
90  DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
91  item->SetParam(0, i);
92  list.emplace_back(item);
93  }
94  selected = this->date.year;
95  break;
96  }
97 
98  ShowDropDownList(this, std::move(list), selected, widget);
99  }
100 
101  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
102  {
103  Dimension d = {0, 0};
104  switch (widget) {
105  default: return;
106 
107  case WID_SD_DAY:
108  for (uint i = 0; i < 31; i++) {
109  d = maxdim(d, GetStringBoundingBox(STR_DAY_NUMBER_1ST + i));
110  }
111  break;
112 
113  case WID_SD_MONTH:
114  for (uint i = 0; i < 12; i++) {
115  d = maxdim(d, GetStringBoundingBox(STR_MONTH_JAN + i));
116  }
117  break;
118 
119  case WID_SD_YEAR:
120  SetDParamMaxValue(0, this->max_year);
121  d = maxdim(d, GetStringBoundingBox(STR_JUST_INT));
122  break;
123  }
124 
125  d.width += padding.width;
126  d.height += padding.height;
127  *size = d;
128  }
129 
130  void SetStringParameters(int widget) const override
131  {
132  switch (widget) {
133  case WID_SD_DAY: SetDParam(0, this->date.day - 1 + STR_DAY_NUMBER_1ST); break;
134  case WID_SD_MONTH: SetDParam(0, this->date.month + STR_MONTH_JAN); break;
135  case WID_SD_YEAR: SetDParam(0, this->date.year); break;
136  }
137  }
138 
139  void OnClick(Point pt, int widget, int click_count) override
140  {
141  switch (widget) {
142  case WID_SD_DAY:
143  case WID_SD_MONTH:
144  case WID_SD_YEAR:
145  ShowDateDropDown(widget);
146  break;
147 
148  case WID_SD_SET_DATE:
149  if (this->callback != nullptr) this->callback(this, ConvertYMDToDate(this->date.year, this->date.month, this->date.day));
150  this->Close();
151  break;
152  }
153  }
154 
155  void OnDropdownSelect(int widget, int index) override
156  {
157  switch (widget) {
158  case WID_SD_DAY:
159  this->date.day = index;
160  break;
161 
162  case WID_SD_MONTH:
163  this->date.month = index;
164  break;
165 
166  case WID_SD_YEAR:
167  this->date.year = index;
168  break;
169  }
170  this->SetDirty();
171  }
172 };
173 
177  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
178  NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_DATE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
179  EndContainer(),
180  NWidget(WWT_PANEL, COLOUR_BROWN),
181  NWidget(NWID_VERTICAL), SetPIP(6, 6, 6),
183  NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_DAY), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_DAY_TOOLTIP),
184  NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_MONTH), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_MONTH_TOOLTIP),
185  NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_YEAR), SetFill(1, 0), SetDataTip(STR_JUST_INT, STR_DATE_YEAR_TOOLTIP),
186  EndContainer(),
188  NWidget(NWID_SPACER), SetFill(1, 0),
189  NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SD_SET_DATE), SetMinimalSize(100, 12), SetDataTip(STR_DATE_SET_DATE, STR_DATE_SET_DATE_TOOLTIP),
190  NWidget(NWID_SPACER), SetFill(1, 0),
191  EndContainer(),
192  EndContainer(),
193  EndContainer()
194 };
195 
198  WDP_CENTER, nullptr, 0, 0,
200  0,
202 );
203 
213 void ShowSetDateWindow(Window *parent, int window_number, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback)
214 {
216  new SetDateWindow(&_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback);
217 }
YearMonthDay::day
Day day
Day (1..31)
Definition: date_type.h:107
DropDownListParamStringItem
String list item with parameters.
Definition: dropdown_type.h:56
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
SetDateWindow::callback
SetDateCallback * callback
Callback to call when a date has been selected.
Definition: date_gui.cpp:26
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:59
DropDownList
std::vector< std::unique_ptr< const DropDownListItem > > DropDownList
A drop down list is a collection of drop down list items.
Definition: dropdown_type.h:99
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:73
SetDateWindow::ShowDateDropDown
void ShowDateDropDown(int widget)
Helper function to construct the dropdown.
Definition: date_gui.cpp:66
maxdim
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Definition: geometry_func.cpp:22
SetDateWindow::OnInitialPosition
Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
Compute the initial position of the window.
Definition: date_gui.cpp:56
SetDateWindow::UpdateWidgetSize
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: date_gui.cpp:101
Year
int32 Year
Type for the year, note: 0 based, i.e. starts at the year 0.
Definition: date_type.h:18
WindowNumber
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:711
SetDateWindow::max_year
Year max_year
The maximum year (inclusive) in the year dropdown.
Definition: date_gui.cpp:29
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:196
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:971
SetDataTip
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1107
GetStringBoundingBox
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:888
SetDateWindow::OnDropdownSelect
void OnDropdownSelect(int widget, int index) override
A dropdown option associated to this window has been selected.
Definition: date_gui.cpp:155
WindowDesc
High level window description.
Definition: window_gui.h:168
window_gui.h
NC_EQUALSIZE
@ NC_EQUALSIZE
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:463
Window::resize
ResizeInfo resize
Resize information.
Definition: window_gui.h:317
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
_nested_set_date_widgets
static const NWidgetPart _nested_set_date_widgets[]
Widgets for the date setting window.
Definition: date_gui.cpp:175
Window::InitNested
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1789
MIN_YEAR
static const Year MIN_YEAR
The absolute minimum & maximum years in OTTD.
Definition: date_type.h:84
Window::height
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:315
WID_SD_MONTH
@ WID_SD_MONTH
Dropdown for the month.
Definition: date_widget.h:16
YearMonthDay::month
Month month
Month (0..11)
Definition: date_type.h:106
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:993
_set_date_desc
static WindowDesc _set_date_desc(WDP_CENTER, nullptr, 0, 0, WC_SET_DATE, WC_NONE, 0, _nested_set_date_widgets, lengthof(_nested_set_date_widgets))
Description of the date setting window.
ConvertYMDToDate
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: date.cpp:149
ShowSetDateWindow
void ShowSetDateWindow(Window *parent, int window_number, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback)
Create the new 'set date' window.
Definition: date_gui.cpp:213
WWT_PUSHTXTBTN
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:104
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
ShowDropDownList
void ShowDropDownList(Window *w, DropDownList &&list, int selected, int button, uint width, bool auto_width, bool instant_close)
Show a drop down list.
Definition: dropdown.cpp:443
dropdown_type.h
ConvertDateToYMD
void ConvertDateToYMD(Date date, YearMonthDay *ymd)
Converts a Date to a Year, Month & Day.
Definition: date.cpp:94
Window::parent
Window * parent
Parent window.
Definition: window_gui.h:332
DropDownListStringItem
Common string list item.
Definition: dropdown_type.h:39
safeguards.h
SetDateWindow::OnClick
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: date_gui.cpp:139
Window::left
int left
x position of left edge of the window
Definition: window_gui.h:312
WC_SET_DATE
@ WC_SET_DATE
Set date; Window numbers:
Definition: window_type.h:160
SetDateWindow
Window to select a date graphically by using dropdowns.
Definition: date_gui.cpp:25
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
SetDateWindow::min_year
Year min_year
The minimum year in the year dropdown.
Definition: date_gui.cpp:28
SetDateWindow::SetDateWindow
SetDateWindow(WindowDesc *desc, WindowNumber window_number, Window *parent, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback)
Create the new 'set date' window.
Definition: date_gui.cpp:41
date_func.h
stdafx.h
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:307
date_gui.h
WID_SD_YEAR
@ WID_SD_YEAR
Dropdown for the year.
Definition: date_widget.h:17
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:37
NWID_VERTICAL
@ NWID_VERTICAL
Vertical container.
Definition: widget_type.h:75
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:67
YearMonthDay::year
Year year
Year (0...)
Definition: date_type.h:105
EndContainer
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:1092
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
strings_func.h
WID_SD_DAY
@ WID_SD_DAY
Dropdown for the day.
Definition: date_widget.h:15
date_widget.h
NWidget
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1207
geometry_func.hpp
SetDParamMaxValue
void SetDParamMaxValue(uint n, uint64 max_value, uint min_count, FontSize size)
Set DParam n to some number that is suitable for string size computations.
Definition: strings.cpp:94
CloseWindowByClass
void CloseWindowByClass(WindowClass cls)
Close all windows of a given class.
Definition: window.cpp:1188
SetMinimalSize
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:1010
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:48
NWID_SPACER
@ NWID_SPACER
Invisible widget that takes some space.
Definition: widget_type.h:77
Window::top
int top
y position of top edge of the window
Definition: window_gui.h:313
WID_SD_SET_DATE
@ WID_SD_SET_DATE
Actually set the date.
Definition: date_widget.h:18
window_func.h
SetDateCallback
void SetDateCallback(const Window *w, Date date)
Callback for when a date has been chosen.
Definition: date_gui.h:21
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
Window::width
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:314
YearMonthDay
Data structure to convert between Date and triplet (year, month, and day).
Definition: date_type.h:104
SetPIP
static NWidgetPart SetPIP(uint8 pre, uint8 inter, uint8 post)
Widget part function for setting a pre/inter/post spaces.
Definition: widget_type.h:1169
SetFill
static NWidgetPart SetFill(uint fill_x, uint fill_y)
Widget part function for setting filling.
Definition: widget_type.h:1076
Window
Data structure for an opened window.
Definition: window_gui.h:279
SetDateWindow::date
YearMonthDay date
The currently selected date.
Definition: date_gui.cpp:27
WDP_CENTER
@ WDP_CENTER
Center the window.
Definition: window_gui.h:157
WWT_DROPDOWN
@ WWT_DROPDOWN
Drop down list.
Definition: widget_type.h:68
SetDateWindow::SetStringParameters
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
Definition: date_gui.cpp:130
MAX_YEAR
static const Year MAX_YEAR
MAX_YEAR, nicely rounded value of the number of years that can be encoded in a single 32 bits date,...
Definition: date_type.h:95
Window::Close
virtual void Close()
Hide the window and all its child windows, and mark them for a later deletion.
Definition: window.cpp:1092