OpenTTD
date_gui.cpp
Go to the documentation of this file.
1 /* $Id: date_gui.cpp 26657 2014-06-20 20:57:32Z planetmaker $ */
2 
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 <http://www.gnu.org/licenses/>.
8  */
9 
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"
19 
20 #include "widgets/dropdown_type.h"
21 #include "widgets/date_widget.h"
22 
23 #include "safeguards.h"
24 
25 
32 
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);
52 
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  }
57 
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  }
63 
68  void ShowDateDropDown(int widget)
69  {
70  int selected;
71  DropDownList *list = new DropDownList();
72 
73  switch (widget) {
74  default: NOT_REACHED();
75 
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->date.day;
81  break;
82 
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;
89 
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  }
99 
100  ShowDropDownList(this, list, selected, widget);
101  }
102 
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;
108 
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;
114 
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;
120 
121  case WID_SD_YEAR:
122  SetDParamMaxValue(0, this->max_year);
123  d = maxdim(d, GetStringBoundingBox(STR_JUST_INT));
124  break;
125  }
126 
127  d.width += padding.width;
128  d.height += padding.height;
129  *size = d;
130  }
131 
132  virtual void SetStringParameters(int widget) const
133  {
134  switch (widget) {
135  case WID_SD_DAY: SetDParam(0, this->date.day - 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  }
140 
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;
149 
150  case WID_SD_SET_DATE:
151  if (this->callback != NULL) this->callback(this, ConvertYMDToDate(this->date.year, this->date.month, this->date.day));
152  delete this;
153  break;
154  }
155  }
156 
157  virtual void OnDropdownSelect(int widget, int index)
158  {
159  switch (widget) {
160  case WID_SD_DAY:
161  this->date.day = index;
162  break;
163 
164  case WID_SD_MONTH:
165  this->date.month = index;
166  break;
167 
168  case WID_SD_YEAR:
169  this->date.year = index;
170  break;
171  }
172  this->SetDirty();
173  }
174 };
175 
179  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
180  NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_DATE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
181  EndContainer(),
182  NWidget(WWT_PANEL, COLOUR_BROWN),
183  NWidget(NWID_VERTICAL), SetPIP(6, 6, 6),
185  NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_DAY), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_DAY_TOOLTIP),
186  NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_MONTH), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_MONTH_TOOLTIP),
187  NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_YEAR), SetFill(1, 0), SetDataTip(STR_JUST_INT, STR_DATE_YEAR_TOOLTIP),
188  EndContainer(),
190  NWidget(NWID_SPACER), SetFill(1, 0),
191  NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SD_SET_DATE), SetMinimalSize(100, 12), SetDataTip(STR_DATE_SET_DATE, STR_DATE_SET_DATE_TOOLTIP),
192  NWidget(NWID_SPACER), SetFill(1, 0),
193  EndContainer(),
194  EndContainer(),
195  EndContainer()
196 };
197 
200  WDP_CENTER, NULL, 0, 0,
202  0,
203  _nested_set_date_widgets, lengthof(_nested_set_date_widgets)
204 );
205 
215 void ShowSetDateWindow(Window *parent, int window_number, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback)
216 {
218  new SetDateWindow(&_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback);
219 }