date_gui.cpp

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