date_gui.cpp

Go to the documentation of this file.
00001 /* $Id: date_gui.cpp 23531 2011-12-16 16:27:45Z truebrain $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "strings_func.h"
00014 #include "date_func.h"
00015 #include "window_func.h"
00016 #include "window_gui.h"
00017 #include "date_gui.h"
00018 #include "core/geometry_func.hpp"
00019 
00020 #include "widgets/dropdown_type.h"
00021 #include "widgets/date_widget.h"
00022 
00023 #include "table/strings.h"
00024 
00026 struct SetDateWindow : Window {
00027   SetDateCallback *callback; 
00028   YearMonthDay date; 
00029   Year min_year;     
00030   Year max_year;     
00031 
00042   SetDateWindow(const WindowDesc *desc, WindowNumber window_number, Window *parent, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback) :
00043       Window(),
00044       callback(callback),
00045       min_year(max(MIN_YEAR, min_year)),
00046       max_year(min(MAX_YEAR, max_year))
00047   {
00048     assert(this->min_year <= this->max_year);
00049     this->parent = parent;
00050     this->InitNested(desc, window_number);
00051 
00052     if (initial_date == 0) initial_date = _date;
00053     ConvertDateToYMD(initial_date, &this->date);
00054     this->date.year = Clamp(this->date.year, min_year, max_year);
00055   }
00056 
00057   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00058   {
00059     Point pt = { this->parent->left + this->parent->width / 2 - sm_width / 2, this->parent->top + this->parent->height / 2 - sm_height / 2 };
00060     return pt;
00061   }
00062 
00067   void ShowDateDropDown(int widget)
00068   {
00069     int selected;
00070     DropDownList *list = new DropDownList();
00071 
00072     switch (widget) {
00073       default: NOT_REACHED();
00074 
00075       case WID_SD_DAY:
00076         for (uint i = 0; i < 31; i++) {
00077           list->push_back(new DropDownListStringItem(STR_ORDINAL_NUMBER_1ST + i, i + 1, false));
00078         }
00079         selected = this->date.day;
00080         break;
00081 
00082       case WID_SD_MONTH:
00083         for (uint i = 0; i < 12; i++) {
00084           list->push_back(new DropDownListStringItem(STR_MONTH_JAN + i, i, false));
00085         }
00086         selected = this->date.month;
00087         break;
00088 
00089       case WID_SD_YEAR:
00090         for (Year i = this->min_year; i <= this->max_year; i++) {
00091           DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
00092           item->SetParam(0, i);
00093           list->push_back(item);
00094         }
00095         selected = this->date.year;
00096         break;
00097     }
00098 
00099     ShowDropDownList(this, list, selected, widget);
00100   }
00101 
00102   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00103   {
00104     Dimension d = {0, 0};
00105     switch (widget) {
00106       default: return;
00107 
00108       case WID_SD_DAY:
00109         for (uint i = 0; i < 31; i++) {
00110           d = maxdim(d, GetStringBoundingBox(STR_ORDINAL_NUMBER_1ST + i));
00111         }
00112         break;
00113 
00114       case WID_SD_MONTH:
00115         for (uint i = 0; i < 12; i++) {
00116           d = maxdim(d, GetStringBoundingBox(STR_MONTH_JAN + i));
00117         }
00118         break;
00119 
00120       case WID_SD_YEAR:
00121         for (Year i = this->min_year; i <= this->max_year; i++) {
00122           SetDParam(0, i);
00123           d = maxdim(d, GetStringBoundingBox(STR_JUST_INT));
00124         }
00125         break;
00126     }
00127 
00128     d.width += padding.width;
00129     d.height += padding.height;
00130     *size = d;
00131   }
00132 
00133   virtual void SetStringParameters(int widget) const
00134   {
00135     switch (widget) {
00136       case WID_SD_DAY:   SetDParam(0, this->date.day - 1 + STR_ORDINAL_NUMBER_1ST); break;
00137       case WID_SD_MONTH: SetDParam(0, this->date.month + STR_MONTH_JAN); break;
00138       case WID_SD_YEAR:  SetDParam(0, this->date.year); break;
00139     }
00140   }
00141 
00142   virtual void OnClick(Point pt, int widget, int click_count)
00143   {
00144     switch (widget) {
00145       case WID_SD_DAY:
00146       case WID_SD_MONTH:
00147       case WID_SD_YEAR:
00148         ShowDateDropDown(widget);
00149         break;
00150 
00151       case WID_SD_SET_DATE:
00152         if (this->callback != NULL) this->callback(this->parent, ConvertYMDToDate(this->date.year, this->date.month, this->date.day));
00153         delete this;
00154         break;
00155     }
00156   }
00157 
00158   virtual void OnDropdownSelect(int widget, int index)
00159   {
00160     switch (widget) {
00161       case WID_SD_DAY:
00162         this->date.day = index;
00163         break;
00164 
00165       case WID_SD_MONTH:
00166         this->date.month = index;
00167         break;
00168 
00169       case WID_SD_YEAR:
00170         this->date.year = index;
00171         break;
00172     }
00173     this->SetDirty();
00174   }
00175 };
00176 
00178 static const NWidgetPart _nested_set_date_widgets[] = {
00179   NWidget(NWID_HORIZONTAL),
00180     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00181     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_DATE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00182   EndContainer(),
00183   NWidget(WWT_PANEL, COLOUR_BROWN),
00184     NWidget(NWID_VERTICAL), SetPIP(6, 6, 6),
00185       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(6, 6, 6),
00186         NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_DAY), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_DAY_TOOLTIP),
00187         NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_MONTH), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_MONTH_TOOLTIP),
00188         NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_YEAR), SetFill(1, 0), SetDataTip(STR_JUST_INT, STR_DATE_YEAR_TOOLTIP),
00189       EndContainer(),
00190       NWidget(NWID_HORIZONTAL),
00191         NWidget(NWID_SPACER), SetFill(1, 0),
00192         NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SD_SET_DATE), SetMinimalSize(100, 12), SetDataTip(STR_DATE_SET_DATE, STR_DATE_SET_DATE_TOOLTIP),
00193         NWidget(NWID_SPACER), SetFill(1, 0),
00194       EndContainer(),
00195     EndContainer(),
00196   EndContainer()
00197 };
00198 
00200 static const WindowDesc _set_date_desc(
00201   WDP_CENTER, 0, 0,
00202   WC_SET_DATE, WC_NONE,
00203   WDF_UNCLICK_BUTTONS,
00204   _nested_set_date_widgets, lengthof(_nested_set_date_widgets)
00205 );
00206 
00216 void ShowSetDateWindow(Window *parent, int window_number, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback)
00217 {
00218   DeleteWindowByClass(WC_SET_DATE);
00219   new SetDateWindow(&_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback);
00220 }