# signs_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: signs_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 "company_gui.h"
    00014 #include "company_func.h"
    00015 #include "signs_base.h"
    00016 #include "signs_func.h"
    00017 #include "debug.h"
    00018 #include "command_func.h"
    00019 #include "strings_func.h"
    00020 #include "window_func.h"
    00021 #include "map_func.h"
    00022 #include "gfx_func.h"
    00023 #include "viewport_func.h"
    00024 #include "querystring_gui.h"
    00025 #include "sortlist_type.h"
    00026 #include "string_func.h"
    00027 #include "core/geometry_func.hpp"
    00028
    00029 #include "table/strings.h"
    00030 #include "table/sprites.h"
    00031
    00032 struct SignList {
    00033   typedef GUIList<const Sign *> GUISignList;
    00034
    00035   static const Sign *last_sign;
    00036   GUISignList signs;
    00037
    00038   void BuildSignsList()
    00039   {
    00040     if (!this->signs.NeedRebuild()) return;
    00041
    00042     DEBUG(misc, 3, "Building sign list");
    00043
    00044     this->signs.Clear();
    00045
    00046     const Sign *si;
    00047     FOR_ALL_SIGNS(si) *this->signs.Append() = si;
    00048
    00049     this->signs.Compact();
    00050     this->signs.RebuildDone();
    00051   }
    00052
    00054   static int CDECL SignNameSorter(const Sign * const *a, const Sign * const *b)
    00055   {
    00056     static char buf_cache[64];
    00057     char buf[64];
    00058
    00059     SetDParam(0, (*a)->index);
    00060     GetString(buf, STR_SIGN_NAME, lastof(buf));
    00061
    00062     if (*b != last_sign) {
    00063       last_sign = *b;
    00064       SetDParam(0, (*b)->index);
    00065       GetString(buf_cache, STR_SIGN_NAME, lastof(buf_cache));
    00066     }
    00067
    00068     return strcasecmp(buf, buf_cache);
    00069   }
    00070
    00071   void SortSignsList()
    00072   {
    00073     if (!this->signs.Sort(&SignNameSorter)) return;
    00074
    00075     /* Reset the name sorter sort cache */
    00076     this->last_sign = NULL;
    00077   }
    00078 };
    00079
    00080 const Sign *SignList::last_sign = NULL;
    00081
    00083 enum SignListWidgets {
    00084   SLW_CAPTION,
    00085   SLW_LIST,
    00086   SLW_SCROLLBAR,
    00087 };
    00088
    00089 struct SignListWindow : Window, SignList {
    00090   int text_offset; // Offset of the sign text relative to the left edge of the SLW_LIST widget.
    00091
    00092   SignListWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00093   {
    00094     this->InitNested(desc, window_number);
    00095
    00096     /* Create initial list. */
    00097     this->signs.ForceRebuild();
    00098     this->signs.ForceResort();
    00099     this->BuildSignsList();
    00100     this->SortSignsList();
    00101     this->vscroll.SetCount(this->signs.Length());
    00102   }
    00103
    00104   virtual void OnPaint()
    00105   {
    00106     this->DrawWidgets();
    00107   }
    00108
    00109   virtual void DrawWidget(const Rect &r, int widget) const
    00110   {
    00111     switch (widget) {
    00112       case SLW_LIST: {
    00113         uint y = r.top + WD_FRAMERECT_TOP; // Offset from top of widget.
    00114         /* No signs? */
    00115         if (this->vscroll.GetCount() == 0) {
    00116           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right, y, STR_STATION_LIST_NONE);
    00117           return;
    00118         }
    00119
    00120         bool rtl = _dynlang.text_dir == TD_RTL;
    00121         int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 + 1;
    00122         uint icon_left  = 4 + (rtl ? r.right - this->text_offset : r.left);
    00123         uint text_left  = r.left + (rtl ? WD_FRAMERECT_LEFT : this->text_offset);
    00124         uint text_right = r.right - (rtl ? this->text_offset : WD_FRAMERECT_RIGHT);
    00125
    00126         /* At least one sign available. */
    00127         for (uint16 i = this->vscroll.GetPosition(); this->vscroll.IsVisible(i) && i < this->vscroll.GetCount(); i++) {
    00128           const Sign *si = this->signs[i];
    00129
    00130           if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, y + sprite_offset_y);
    00131
    00132           SetDParam(0, si->index);
    00133           DrawString(text_left, text_right, y, STR_SIGN_NAME, TC_YELLOW);
    00134           y += this->resize.step_height;
    00135         }
    00136         break;
    00137       }
    00138     }
    00139   }
    00140
    00141   virtual void SetStringParameters(int widget) const
    00142   {
    00143     if (widget == SLW_CAPTION) SetDParam(0, this->vscroll.GetCount());
    00144   }
    00145
    00146   virtual void OnClick(Point pt, int widget, int click_count)
    00147   {
    00148     if (widget == SLW_LIST) {
    00149       uint id_v = (pt.y - this->GetWidget<NWidgetBase>(SLW_LIST)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height;
    00150
    00151       if (id_v >= this->vscroll.GetCapacity()) return;
    00152       id_v += this->vscroll.GetPosition();
    00153       if (id_v >= this->vscroll.GetCount()) return;
    00154
    00155       const Sign *si = this->signs[id_v];
    00156       ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
    00157     }
    00158   }
    00159
    00160   virtual void OnResize()
    00161   {
    00162     this->vscroll.SetCapacityFromWidget(this, SLW_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    00163   }
    00164
    00165   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00166   {
    00167     switch (widget) {
    00168       case SLW_LIST: {
    00169         Dimension spr_dim = GetSpriteSize(SPR_COMPANY_ICON);
    00170         this->text_offset = WD_FRAMETEXT_LEFT + spr_dim.width + 2; // 2 pixels space between icon and the sign text.
    00171         resize->height = max<uint>(FONT_HEIGHT_NORMAL, GetSpriteSize(SPR_COMPANY_ICON).height);
    00172         Dimension d = {this->text_offset + MAX_LENGTH_SIGN_NAME_PIXELS + WD_FRAMETEXT_RIGHT, WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM};
    00173         *size = maxdim(*size, d);
    00174       } break;
    00175
    00176       case SLW_CAPTION:
    00177         SetDParam(0, max<size_t>(1000, Sign::GetPoolSize()));
    00178         *size = GetStringBoundingBox(STR_SIGN_LIST_CAPTION);
    00179         size->height += padding.height;
    00180         size->width  += padding.width;
    00181         break;
    00182     }
    00183   }
    00184
    00185   virtual void OnInvalidateData(int data)
    00186   {
    00187     if (data == 0) { // New or deleted sign.
    00188       this->signs.ForceRebuild();
    00189       this->BuildSignsList();
    00190       this->SetWidgetDirty(SLW_CAPTION);
    00191       this->vscroll.SetCount(this->signs.Length());
    00192     } else { // Change of sign contents.
    00193       this->signs.ForceResort();
    00194     }
    00195
    00196     this->SortSignsList();
    00197   }
    00198 };
    00199
    00200 static const NWidgetPart _nested_sign_list_widgets[] = {
    00201   NWidget(NWID_HORIZONTAL),
    00202     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00203     NWidget(WWT_CAPTION, COLOUR_GREY, SLW_CAPTION), SetDataTip(STR_SIGN_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00204     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00205     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00206   EndContainer(),
    00207   NWidget(NWID_HORIZONTAL),
    00208     NWidget(WWT_PANEL, COLOUR_GREY, SLW_LIST), SetMinimalSize(WD_FRAMETEXT_LEFT + 16 + MAX_LENGTH_SIGN_NAME_PIXELS + WD_FRAMETEXT_RIGHT, 50),
    00209               SetResize(1, 10), SetFill(1, 0), EndContainer(),
    00210     NWidget(NWID_VERTICAL),
    00211       NWidget(WWT_SCROLLBAR, COLOUR_GREY, SLW_SCROLLBAR),
    00212       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00213     EndContainer(),
    00214   EndContainer(),
    00215 };
    00216
    00217 static const WindowDesc _sign_list_desc(
    00218   WDP_AUTO, 358, 138,
    00219   WC_SIGN_LIST, WC_NONE,
    00220   0,
    00221   _nested_sign_list_widgets, lengthof(_nested_sign_list_widgets)
    00222 );
    00223
    00224
    00225 void ShowSignList()
    00226 {
    00227   AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
    00228 }
    00229
    00236 static bool RenameSign(SignID index, const char *text)
    00237 {
    00238   bool remove = StrEmpty(text);
    00239   DoCommandP(0, index, 0, CMD_RENAME_SIGN | (StrEmpty(text) ? CMD_MSG(STR_ERROR_CAN_T_DELETE_SIGN) : CMD_MSG(STR_ERROR_CAN_T_CHANGE_SIGN_NAME)), NULL, text);
    00240   return remove;
    00241 }
    00242
    00244 enum QueryEditSignWidgets {
    00245   QUERY_EDIT_SIGN_WIDGET_CAPTION,
    00246   QUERY_EDIT_SIGN_WIDGET_TEXT,
    00247   QUERY_EDIT_SIGN_WIDGET_OK,
    00248   QUERY_EDIT_SIGN_WIDGET_CANCEL,
    00249   QUERY_EDIT_SIGN_WIDGET_DELETE,
    00250   QUERY_EDIT_SIGN_WIDGET_PREVIOUS,
    00251   QUERY_EDIT_SIGN_WIDGET_NEXT,
    00252 };
    00253
    00254 struct SignWindow : QueryStringBaseWindow, SignList {
    00255   SignID cur_sign;
    00256
    00257   SignWindow(const WindowDesc *desc, const Sign *si) : QueryStringBaseWindow(MAX_LENGTH_SIGN_NAME_BYTES)
    00258   {
    00259     this->caption = STR_EDIT_SIGN_CAPTION;
    00260     this->afilter = CS_ALPHANUMERAL;
    00261
    00262     this->InitNested(desc);
    00263
    00264     this->LowerWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
    00265     UpdateSignEditWindow(si);
    00266     this->SetFocusedWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
    00267   }
    00268
    00269   void UpdateSignEditWindow(const Sign *si)
    00270   {
    00271     char *last_of = &this->edit_str_buf[this->edit_str_size - 1]; // points to terminating '\0'
    00272
    00273     /* Display an empty string when the sign hasnt been edited yet */
    00274     if (si->name != NULL) {
    00275       SetDParam(0, si->index);
    00276       GetString(this->edit_str_buf, STR_SIGN_NAME, last_of);
    00277     } else {
    00278       GetString(this->edit_str_buf, STR_EMPTY, last_of);
    00279     }
    00280     *last_of = '\0';
    00281
    00282     this->cur_sign = si->index;
    00283     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, MAX_LENGTH_SIGN_NAME_PIXELS);
    00284
    00285     this->SetWidgetDirty(QUERY_EDIT_SIGN_WIDGET_TEXT);
    00286     this->SetFocusedWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
    00287   }
    00288
    00294   const Sign *PrevNextSign(bool next)
    00295   {
    00296     /* Rebuild the sign list */
    00297     this->signs.ForceRebuild();
    00298     this->signs.NeedResort();
    00299     this->BuildSignsList();
    00300     this->SortSignsList();
    00301
    00302     /* Search through the list for the current sign, excluding
    00303      * - the first sign if we want the previous sign or
    00304      * - the last sign if we want the next sign */
    00305     uint end = this->signs.Length() - (next ? 1 : 0);
    00306     for (uint i = next ? 0 : 1; i < end; i++) {
    00307       if (this->cur_sign == this->signs[i]->index) {
    00308         /* We've found the current sign, so return the sign before/after it */
    00309         return this->signs[i + (next ? 1 : -1)];
    00310       }
    00311     }
    00312     /* If we haven't found the current sign by now, return the last/first sign */
    00313     return this->signs[next ? 0 : this->signs.Length() - 1];
    00314   }
    00315
    00316   virtual void SetStringParameters(int widget) const
    00317   {
    00318     switch (widget) {
    00319       case QUERY_EDIT_SIGN_WIDGET_CAPTION:
    00320         SetDParam(0, this->caption);
    00321         break;
    00322     }
    00323   }
    00324
    00325   virtual void OnPaint()
    00326   {
    00327     this->DrawWidgets();
    00328     if (!this->IsShaded()) this->DrawEditBox(QUERY_EDIT_SIGN_WIDGET_TEXT);
    00329   }
    00330
    00331   virtual void OnClick(Point pt, int widget, int click_count)
    00332   {
    00333     switch (widget) {
    00334       case QUERY_EDIT_SIGN_WIDGET_PREVIOUS:
    00335       case QUERY_EDIT_SIGN_WIDGET_NEXT: {
    00336         const Sign *si = this->PrevNextSign(widget == QUERY_EDIT_SIGN_WIDGET_NEXT);
    00337
    00338         /* Rebuild the sign list */
    00339         this->signs.ForceRebuild();
    00340         this->signs.NeedResort();
    00341         this->BuildSignsList();
    00342         this->SortSignsList();
    00343
    00344         /* Scroll to sign and reopen window */
    00345         ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
    00346         UpdateSignEditWindow(si);
    00347         break;
    00348       }
    00349
    00350       case QUERY_EDIT_SIGN_WIDGET_DELETE:
    00351         /* Only need to set the buffer to null, the rest is handled as the OK button */
    00352         RenameSign(this->cur_sign, "");
    00353         /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
    00354         break;
    00355
    00356       case QUERY_EDIT_SIGN_WIDGET_OK:
    00357         if (RenameSign(this->cur_sign, this->text.buf)) break;
    00358         /* FALL THROUGH */
    00359
    00360       case QUERY_EDIT_SIGN_WIDGET_CANCEL:
    00361         delete this;
    00362         break;
    00363     }
    00364   }
    00365
    00366   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00367   {
    00368     EventState state = ES_NOT_HANDLED;
    00369     switch (this->HandleEditBoxKey(QUERY_EDIT_SIGN_WIDGET_TEXT, key, keycode, state)) {
    00370       default: break;
    00371
    00372       case HEBR_CONFIRM:
    00373         if (RenameSign(this->cur_sign, this->text.buf)) break;
    00374         /* FALL THROUGH */
    00375
    00376       case HEBR_CANCEL: // close window, abandon changes
    00377         delete this;
    00378         break;
    00379     }
    00380     return state;
    00381   }
    00382
    00383   virtual void OnMouseLoop()
    00384   {
    00385     this->HandleEditBox(QUERY_EDIT_SIGN_WIDGET_TEXT);
    00386   }
    00387
    00388   virtual void OnOpenOSKWindow(int wid)
    00389   {
    00390     ShowOnScreenKeyboard(this, wid, QUERY_EDIT_SIGN_WIDGET_CANCEL, QUERY_EDIT_SIGN_WIDGET_OK);
    00391   }
    00392 };
    00393
    00394 static const NWidgetPart _nested_query_sign_edit_widgets[] = {
    00395   NWidget(NWID_HORIZONTAL),
    00396     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00397     NWidget(WWT_CAPTION, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00398   EndContainer(),
    00399   NWidget(WWT_PANEL, COLOUR_GREY),
    00400     NWidget(WWT_EDITBOX, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_TEXT), SetMinimalSize(256, 12), SetDataTip(STR_EDIT_SIGN_SIGN_OSKTITLE, STR_NULL), SetPadding(2, 2, 2, 2),
    00401   EndContainer(),
    00402   NWidget(NWID_HORIZONTAL),
    00403     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_OK), SetMinimalSize(61, 12), SetDataTip(STR_BUTTON_OK, STR_NULL),
    00404     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_CANCEL), SetMinimalSize(60, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
    00405     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_DELETE), SetMinimalSize(60, 12), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_NULL),
    00406     NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
    00407     NWidget(NWID_BUTTON_ARROW, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_PREVIOUS), SetMinimalSize(11, 12), SetDataTip(AWV_DECREASE, STR_EDIT_SIGN_PREVIOUS_SIGN_TOOLTIP),
    00408     NWidget(NWID_BUTTON_ARROW, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_NEXT), SetMinimalSize(11, 12), SetDataTip(AWV_INCREASE, STR_EDIT_SIGN_NEXT_SIGN_TOOLTIP),
    00409   EndContainer(),
    00410 };
    00411
    00412 static const WindowDesc _query_sign_edit_desc(
    00413   WDP_AUTO, 0, 0,
    00414   WC_QUERY_STRING, WC_NONE,
    00415   WDF_CONSTRUCTION | WDF_UNCLICK_BUTTONS,
    00416   _nested_query_sign_edit_widgets, lengthof(_nested_query_sign_edit_widgets)
    00417 );
    00418
    00419 void HandleClickOnSign(const Sign *si)
    00420 {
    00421   if (_ctrl_pressed && si->owner == _local_company) {
    00422     RenameSign(si->index, NULL);
    00423     return;
    00424   }
    00425   ShowRenameSignWindow(si);
    00426 }
    00427
    00428 void ShowRenameSignWindow(const Sign *si)
    00429 {
    00430   /* Delete all other edit windows */
    00431   DeleteWindowById(WC_QUERY_STRING, 0);
    00432
    00433   new SignWindow(&_query_sign_edit_desc, si);
    00434 }
    00435
    00436 void DeleteRenameSignWindow(SignID sign)
    00437 {
    00438   SignWindow *w = dynamic_cast<SignWindow *>(FindWindowById(WC_QUERY_STRING, 0));
    00439
    00440   if (w != NULL && w->cur_sign == sign) delete w;
    00441 }