date_gui.cpp

Go to the documentation of this file.
00001 /* $Id: date_gui.cpp 18966 2010-01-30 18:34:48Z frosch $ */
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 OnPaint()
00150   {
00151     this->DrawWidgets();
00152   }
00153 
00154   virtual void OnClick(Point pt, int widget, int click_count)
00155   {
00156     switch (widget) {
00157       case SDW_DAY:
00158       case SDW_MONTH:
00159       case SDW_YEAR:
00160         ShowDateDropDown(widget);
00161         break;
00162 
00163       case SDW_SET_DATE:
00164         if (this->callback != NULL) this->callback(this->parent, ConvertYMDToDate(this->date.year, this->date.month, this->date.day));
00165         delete this;
00166         break;
00167     }
00168   }
00169 
00170   virtual void OnDropdownSelect(int widget, int index)
00171   {
00172     switch (widget) {
00173       case SDW_DAY:
00174         this->date.day = index;
00175         break;
00176 
00177       case SDW_MONTH:
00178         this->date.month = index;
00179         break;
00180 
00181       case SDW_YEAR:
00182         this->date.year = index;
00183         break;
00184     }
00185     this->SetDirty();
00186   }
00187 };
00188 
00189 static const NWidgetPart _nested_set_date_widgets[] = {
00190   NWidget(NWID_HORIZONTAL),
00191     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00192     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_DATE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00193   EndContainer(),
00194   NWidget(WWT_PANEL, COLOUR_BROWN),
00195     NWidget(NWID_VERTICAL), SetPIP(6, 6, 6),
00196       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(6, 6, 6),
00197         NWidget(WWT_DROPDOWN, COLOUR_ORANGE, SDW_DAY), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_DAY_TOOLTIP),
00198         NWidget(WWT_DROPDOWN, COLOUR_ORANGE, SDW_MONTH), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_MONTH_TOOLTIP),
00199         NWidget(WWT_DROPDOWN, COLOUR_ORANGE, SDW_YEAR), SetFill(1, 0), SetDataTip(STR_JUST_INT, STR_DATE_YEAR_TOOLTIP),
00200       EndContainer(),
00201       NWidget(NWID_HORIZONTAL),
00202         NWidget(NWID_SPACER), SetFill(1, 0),
00203         NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, SDW_SET_DATE), SetMinimalSize(100, 12), SetDataTip(STR_DATE_SET_DATE, STR_DATE_SET_DATE_TOOLTIP),
00204         NWidget(NWID_SPACER), SetFill(1, 0),
00205       EndContainer(),
00206     EndContainer(),
00207   EndContainer()
00208 };
00209 
00210 static const WindowDesc _set_date_desc(
00211   WDP_CENTER, 0, 0,
00212   WC_SET_DATE, WC_NONE,
00213   WDF_UNCLICK_BUTTONS,
00214   _nested_set_date_widgets, lengthof(_nested_set_date_widgets)
00215 );
00216 
00226 void ShowSetDateWindow(Window *parent, int window_number, Date initial_date, Year min_year, Year max_year, SetDateCallback *callback)
00227 {
00228   DeleteWindowByClass(WC_SET_DATE);
00229   new SetDateWindow(&_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback);
00230 }

Generated on Tue Sep 14 17:06:48 2010 for OpenTTD by  doxygen 1.6.1