Go to the documentation of this file.
1 /* $Id: date_gui.cpp 26657 2014-06-20 20:57:32Z planetmaker $ */
3 /*
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 <>.
8  */
12 #include "stdafx.h"
13 #include "strings_func.h"
14 #include "date_func.h"
15 #include "window_func.h"
16 #include "window_gui.h"
17 #include "date_gui.h"
18 #include "core/geometry_func.hpp"
20 #include "widgets/dropdown_type.h"
21 #include "widgets/date_widget.h"
23 #include "safeguards.h"
43  SetDateWindow(WindowDesc *desc, WindowNumber window_number, Window *parent, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback) :
44  Window(desc),
45  callback(callback),
46  min_year(max(MIN_YEAR, min_year)),
47  max_year(min(MAX_YEAR, max_year))
48  {
49  assert(this->min_year <= this->max_year);
50  this->parent = parent;
51  this->InitNested(window_number);
53  if (initial_date == 0) initial_date = _date;
54  ConvertDateToYMD(initial_date, &this->date);
55  this->date.year = Clamp(this->date.year, min_year, max_year);
56  }
58  virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
59  {
60  Point pt = { this->parent->left + this->parent->width / 2 - sm_width / 2, this->parent->top + this->parent->height / 2 - sm_height / 2 };
61  return pt;
62  }
68  void ShowDateDropDown(int widget)
69  {
70  int selected;
71  DropDownList *list = new DropDownList();
73  switch (widget) {
74  default: NOT_REACHED();
76  case WID_SD_DAY:
77  for (uint i = 0; i < 31; i++) {
78  *list->Append() = new DropDownListStringItem(STR_DAY_NUMBER_1ST + i, i + 1, false);
79  }
80  selected = this->;
81  break;
83  case WID_SD_MONTH:
84  for (uint i = 0; i < 12; i++) {
85  *list->Append() = new DropDownListStringItem(STR_MONTH_JAN + i, i, false);
86  }
87  selected = this->date.month;
88  break;
90  case WID_SD_YEAR:
91  for (Year i = this->min_year; i <= this->max_year; i++) {
92  DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
93  item->SetParam(0, i);
94  *list->Append() = item;
95  }
96  selected = this->date.year;
97  break;
98  }
100  ShowDropDownList(this, list, selected, widget);
101  }
103  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
104  {
105  Dimension d = {0, 0};
106  switch (widget) {
107  default: return;
109  case WID_SD_DAY:
110  for (uint i = 0; i < 31; i++) {
111  d = maxdim(d, GetStringBoundingBox(STR_DAY_NUMBER_1ST + i));
112  }
113  break;
115  case WID_SD_MONTH:
116  for (uint i = 0; i < 12; i++) {
117  d = maxdim(d, GetStringBoundingBox(STR_MONTH_JAN + i));
118  }
119  break;
121  case WID_SD_YEAR:
122  SetDParamMaxValue(0, this->max_year);
123  d = maxdim(d, GetStringBoundingBox(STR_JUST_INT));
124  break;
125  }
127  d.width += padding.width;
128  d.height += padding.height;
129  *size = d;
130  }
132  virtual void SetStringParameters(int widget) const
133  {
134  switch (widget) {
135  case WID_SD_DAY: SetDParam(0, this-> - 1 + STR_DAY_NUMBER_1ST); break;
136  case WID_SD_MONTH: SetDParam(0, this->date.month + STR_MONTH_JAN); break;
137  case WID_SD_YEAR: SetDParam(0, this->date.year); break;
138  }
139  }
141  virtual void OnClick(Point pt, int widget, int click_count)
142  {
143  switch (widget) {
144  case WID_SD_DAY:
145  case WID_SD_MONTH:
146  case WID_SD_YEAR:
147  ShowDateDropDown(widget);
148  break;
150  case WID_SD_SET_DATE:
151  if (this->callback != NULL) this->callback(this, ConvertYMDToDate(this->date.year, this->date.month, this->;
152  delete this;
153  break;
154  }
155  }
157  virtual void OnDropdownSelect(int widget, int index)
158  {
159  switch (widget) {
160  case WID_SD_DAY:
161  this-> = index;
162  break;
164  case WID_SD_MONTH:
165  this->date.month = index;
166  break;
168  case WID_SD_YEAR:
169  this->date.year = index;
170  break;
171  }
172  this->SetDirty();
173  }
174 };
181  EndContainer(),
183  NWidget(NWID_VERTICAL), SetPIP(6, 6, 6),
188  EndContainer(),
190  NWidget(NWID_SPACER), SetFill(1, 0),
192  NWidget(NWID_SPACER), SetFill(1, 0),
193  EndContainer(),
194  EndContainer(),
195  EndContainer()
196 };
200  WDP_CENTER, NULL, 0, 0,
202  0,
203  _nested_set_date_widgets, lengthof(_nested_set_date_widgets)
204 );
216 {
218  new SetDateWindow(&_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback);
219 }
Functions related to OTTD&#39;s strings.
ResizeInfo resize
Resize information.
Definition: window_gui.h:317
static WindowDesc _set_date_desc(WDP_CENTER, NULL, 0, 0, WC_SET_DATE, WC_NONE, 0, _nested_set_date_widgets, lengthof(_nested_set_date_widgets))
Description of the date setting window.
Types related to the date widgets.
Window * parent
Parent window.
Definition: window_gui.h:332
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
Update size and resize step of a widget in the window.
Definition: date_gui.cpp:103
High level window description.
Definition: window_gui.h:168
int left
x position of left edge of the window
Definition: window_gui.h:312
Actually set the date.
Definition: date_widget.h:20
Functions related to dates.
Day day
Day (1..31)
Definition: date_type.h:106
virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
Compute the initial position of the window.
Definition: date_gui.cpp:58
Horizontal container.
Definition: widget_type.h:75
SetDateWindow(WindowDesc *desc, WindowNumber window_number, Window *parent, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback)
Create the new &#39;set date&#39; window.
Definition: date_gui.cpp:43
int top
y position of top edge of the window
Definition: window_gui.h:313
Close box (at top-left of a window)
Definition: widget_type.h:69
int32 Year
Type for the year, note: 0 based, i.e. starts at the year 0.
Definition: date_type.h:20
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
Common string list item.
Definition: dropdown_type.h:41
Functions, definitions and such used only by the GUI.
Year min_year
The minimum year in the year dropdown.
Definition: date_gui.cpp:30
T * Append(uint to_add=1)
Append an item and return it.
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:910
Data structure for an opened window.
Definition: window_gui.h:271
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1833
virtual void SetStringParameters(int widget) const
Initialize string parameters for a widget.
Definition: date_gui.cpp:132
Invisible widget that takes some space.
Definition: widget_type.h:79
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1014
Simple vector template class, with automatic delete.
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:947
Definition of base types and functions in a cross-platform compatible way.
Data structure to convert between Date and triplet (year, month, and day).
Definition: date_type.h:103
Window to select a date graphically by using dropdowns.
Definition: date_gui.cpp:27
A number of safeguards to prevent using unsafe methods.
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:104
Geometry functions.
Simple depressed panel.
Definition: widget_type.h:50
void ShowSetDateWindow(Window *parent, int window_number, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback)
Create the new &#39;set date&#39; window.
Definition: date_gui.cpp:215
Set date; Window numbers:
Definition: window_type.h:163
Center the window.
Definition: window_gui.h:157
void SetDateCallback(const Window *w, Date date)
Callback for when a date has been chosen.
Definition: date_gui.h:23
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1114
Year year
Year (0...)
Definition: date_type.h:104
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
YearMonthDay date
The currently selected date.
Definition: date_gui.cpp:29
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1150
Month month
Month (0..11)
Definition: date_type.h:105
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:959
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:699
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:40
void ShowDateDropDown(int widget)
Helper function to construct the dropdown.
Definition: date_gui.cpp:68
SetDateCallback * callback
Callback to call when a date has been selected.
Definition: date_gui.cpp:28
Functions related to the graphical selection of a date.
Dropdown for the year.
Definition: date_widget.h:19
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
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:105
static const NWidgetPart _nested_set_date_widgets[]
Widgets for the date setting window.
Definition: date_gui.cpp:177
Vertical container.
Definition: widget_type.h:77
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:999
Coordinates of a point in 2D.
Drop down list.
Definition: widget_type.h:70
static const Year MIN_YEAR
The absolute minimum & maximum years in OTTD.
Definition: date_type.h:85
int32 Date
The type to store our dates in.
Definition: date_type.h:16
Year max_year
The maximum year (inclusive) in the year dropdown.
Definition: date_gui.cpp:31
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:314
static NWidgetPart SetFill(uint fill_x, uint fill_y)
Widget part function for setting filling.
Definition: widget_type.h:983
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:695
Dropdown for the month.
Definition: date_widget.h:18
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:307
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: date.cpp:149
Window functions not directly related to making/drawing windows.
String list item with parameters.
Definition: dropdown_type.h:59
Date _date
Current date in days (day counter)
Definition: date.cpp:28
virtual void OnClick(Point pt, int widget, int click_count)
A click with the left mouse button has been made on the window.
Definition: date_gui.cpp:141
void ConvertDateToYMD(Date date, YearMonthDay *ymd)
Converts a Date to a Year, Month & Day.
Definition: date.cpp:94
Dimensions (a width and height) of a rectangle in 2D.
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:429
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:94
static NWidgetPart SetPIP(uint8 pre, uint8 inter, uint8 post)
Widget part function for setting a pre/inter/post spaces.
Definition: widget_type.h:1076
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:315
Dropdown for the day.
Definition: date_widget.h:17
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:201
virtual void OnDropdownSelect(int widget, int index)
A dropdown option associated to this window has been selected.
Definition: date_gui.cpp:157