# date_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: date_gui.cpp 26086 2013-11-24 14:46:26Z rubidium $ */
    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(WindowDesc *desc, WindowNumber window_number, Window *parent, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback) :
    00042       Window(desc),
    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(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(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->Append() = 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->Append() = 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->Append() = 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         SetDParamMaxValue(0, this->max_year);
    00121         d = maxdim(d, GetStringBoundingBox(STR_JUST_INT));
    00122         break;
    00123     }
    00124
    00125     d.width += padding.width;
    00126     d.height += padding.height;
    00127     *size = d;
    00128   }
    00129
    00130   virtual void SetStringParameters(int widget) const
    00131   {
    00132     switch (widget) {
    00133       case WID_SD_DAY:   SetDParam(0, this->date.day - 1 + STR_ORDINAL_NUMBER_1ST); break;
    00134       case WID_SD_MONTH: SetDParam(0, this->date.month + STR_MONTH_JAN); break;
    00135       case WID_SD_YEAR:  SetDParam(0, this->date.year); break;
    00136     }
    00137   }
    00138
    00139   virtual void OnClick(Point pt, int widget, int click_count)
    00140   {
    00141     switch (widget) {
    00142       case WID_SD_DAY:
    00143       case WID_SD_MONTH:
    00144       case WID_SD_YEAR:
    00145         ShowDateDropDown(widget);
    00146         break;
    00147
    00148       case WID_SD_SET_DATE:
    00149         if (this->callback != NULL) this->callback(this, ConvertYMDToDate(this->date.year, this->date.month, this->date.day));
    00150         delete this;
    00151         break;
    00152     }
    00153   }
    00154
    00155   virtual void OnDropdownSelect(int widget, int index)
    00156   {
    00157     switch (widget) {
    00158       case WID_SD_DAY:
    00159         this->date.day = index;
    00160         break;
    00161
    00162       case WID_SD_MONTH:
    00163         this->date.month = index;
    00164         break;
    00165
    00166       case WID_SD_YEAR:
    00167         this->date.year = index;
    00168         break;
    00169     }
    00170     this->SetDirty();
    00171   }
    00172 };
    00173
    00175 static const NWidgetPart _nested_set_date_widgets[] = {
    00176   NWidget(NWID_HORIZONTAL),
    00177     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
    00178     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_DATE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00179   EndContainer(),
    00180   NWidget(WWT_PANEL, COLOUR_BROWN),
    00181     NWidget(NWID_VERTICAL), SetPIP(6, 6, 6),
    00182       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(6, 6, 6),
    00183         NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_DAY), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_DAY_TOOLTIP),
    00184         NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_MONTH), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_MONTH_TOOLTIP),
    00185         NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_YEAR), SetFill(1, 0), SetDataTip(STR_JUST_INT, STR_DATE_YEAR_TOOLTIP),
    00186       EndContainer(),
    00187       NWidget(NWID_HORIZONTAL),
    00188         NWidget(NWID_SPACER), SetFill(1, 0),
    00189         NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SD_SET_DATE), SetMinimalSize(100, 12), SetDataTip(STR_DATE_SET_DATE, STR_DATE_SET_DATE_TOOLTIP),
    00190         NWidget(NWID_SPACER), SetFill(1, 0),
    00191       EndContainer(),
    00192     EndContainer(),
    00193   EndContainer()
    00194 };
    00195
    00197 static WindowDesc _set_date_desc(
    00198   WDP_CENTER, NULL, 0, 0,
    00199   WC_SET_DATE, WC_NONE,
    00200   0,
    00201   _nested_set_date_widgets, lengthof(_nested_set_date_widgets)
    00202 );
    00203
    00213 void ShowSetDateWindow(Window *parent, int window_number, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback)
    00214 {
    00215   DeleteWindowByClass(WC_SET_DATE);
    00216   new SetDateWindow(&_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback);
    00217 }