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