signs_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 "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 #include "hotkeys.h"
00029 #include "transparency.h"
00030 
00031 #include "table/strings.h"
00032 #include "table/sprites.h"
00033 
00039 struct FilterInfo {
00040   const char *string;  
00041   bool case_sensitive; 
00042 };
00043 
00044 struct SignList {
00049   typedef GUIList<const Sign *, FilterInfo> GUISignList;
00050 
00051   static const Sign *last_sign;
00052   GUISignList signs;
00053 
00054   char filter_string[MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH]; 
00055   static bool match_case;                                           
00056 
00060   SignList()
00061   {
00062     filter_string[0] = '\0';
00063   }
00064 
00065   void BuildSignsList()
00066   {
00067     if (!this->signs.NeedRebuild()) return;
00068 
00069     DEBUG(misc, 3, "Building sign list");
00070 
00071     this->signs.Clear();
00072 
00073     const Sign *si;
00074     FOR_ALL_SIGNS(si) *this->signs.Append() = si;
00075 
00076     this->FilterSignList();
00077     this->signs.Compact();
00078     this->signs.RebuildDone();
00079   }
00080 
00082   static int CDECL SignNameSorter(const Sign * const *a, const Sign * const *b)
00083   {
00084     static char buf_cache[64];
00085     char buf[64];
00086 
00087     SetDParam(0, (*a)->index);
00088     GetString(buf, STR_SIGN_NAME, lastof(buf));
00089 
00090     if (*b != last_sign) {
00091       last_sign = *b;
00092       SetDParam(0, (*b)->index);
00093       GetString(buf_cache, STR_SIGN_NAME, lastof(buf_cache));
00094     }
00095 
00096     int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
00097 
00098     return r != 0 ? r : ((*a)->index - (*b)->index);
00099   }
00100 
00101   void SortSignsList()
00102   {
00103     if (!this->signs.Sort(&SignNameSorter)) return;
00104 
00105     /* Reset the name sorter sort cache */
00106     this->last_sign = NULL;
00107   }
00108 
00110   static bool CDECL SignNameFilter(const Sign * const *a, FilterInfo filter_info)
00111   {
00112     /* Get sign string */
00113     char buf1[MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH];
00114     SetDParam(0, (*a)->index);
00115     GetString(buf1, STR_SIGN_NAME, lastof(buf1));
00116 
00117     return (filter_info.case_sensitive ? strstr(buf1, filter_info.string) : strcasestr(buf1, filter_info.string)) != NULL;
00118   }
00119 
00121   static bool CDECL OwnerVisibilityFilter(const Sign * const *a, FilterInfo filter_info)
00122   {
00123     assert(!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS));
00124     /* Hide sign if non-own signs are hidden in the viewport */
00125     return (*a)->owner == _local_company;
00126   }
00127 
00129   void FilterSignList()
00130   {
00131     FilterInfo filter_info = {this->filter_string, this->match_case};
00132     this->signs.Filter(&SignNameFilter, filter_info);
00133     if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS)) {
00134       this->signs.Filter(&OwnerVisibilityFilter, filter_info);
00135     }
00136   }
00137 };
00138 
00139 const Sign *SignList::last_sign = NULL;
00140 bool SignList::match_case = false;
00141 
00143 enum SignListWidgets {
00144   SLW_CAPTION,
00145   SLW_LIST,
00146   SLW_SCROLLBAR,
00147   SLW_FILTER_TEXT,           
00148   SLW_FILTER_MATCH_CASE_BTN, 
00149   SLW_FILTER_CLEAR_BTN,      
00150 };
00151 
00153 enum SignListHotkeys {
00154   SLHK_FOCUS_FILTER_BOX, 
00155 };
00156 
00157 struct SignListWindow : QueryStringBaseWindow, SignList {
00158   int text_offset; 
00159   Scrollbar *vscroll;
00160 
00161   SignListWindow(const WindowDesc *desc, WindowNumber window_number) : QueryStringBaseWindow(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
00162   {
00163     this->CreateNestedTree(desc);
00164     this->vscroll = this->GetScrollbar(SLW_SCROLLBAR);
00165     this->FinishInitNested(desc, window_number);
00166     this->SetWidgetLoweredState(SLW_FILTER_MATCH_CASE_BTN, SignList::match_case);
00167 
00168     /* Initialize the text edit widget */
00169     this->afilter = CS_ALPHANUMERAL;
00170     InitializeTextBuffer(&this->text, this->edit_str_buf, MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS);
00171     ClearFilterTextWidget();
00172 
00173     /* Initialize the filtering variables */
00174     this->SetFilterString("");
00175 
00176     /* Create initial list. */
00177     this->signs.ForceRebuild();
00178     this->signs.ForceResort();
00179     this->BuildSortSignList();
00180   }
00181 
00186   void ClearFilterTextWidget()
00187   {
00188     this->edit_str_buf[0] = '\0';
00189     UpdateTextBufferSize(&this->text);
00190 
00191     this->SetWidgetDirty(SLW_FILTER_TEXT);
00192   }
00193 
00200   void SetFilterString(const char *new_filter_string)
00201   {
00202     /* check if there is a new filter string */
00203     if (!StrEmpty(new_filter_string)) {
00204       /* Copy new filter string */
00205       strecpy(this->filter_string, new_filter_string, lastof(this->filter_string));
00206 
00207       this->signs.SetFilterState(true);
00208 
00209       this->EnableWidget(SLW_FILTER_CLEAR_BTN);
00210     } else {
00211       /* There is no new string -> clear this->filter_string */
00212       this->filter_string[0] = '\0';
00213 
00214       this->signs.SetFilterState(!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS)); // keep sign list filtering active if competitor signs should be hidden
00215       this->DisableWidget(SLW_FILTER_CLEAR_BTN);
00216     }
00217 
00218     /* Repaint the clear button since its disabled state may have changed */
00219     this->SetWidgetDirty(SLW_FILTER_CLEAR_BTN);
00220 
00221     /* Rebuild the list of signs */
00222     this->InvalidateData();
00223   }
00224 
00225   virtual void OnPaint()
00226   {
00227     if (this->signs.NeedRebuild()) this->BuildSortSignList();
00228     this->DrawWidgets();
00229     if (!this->IsShaded()) this->DrawEditBox(SLW_FILTER_TEXT);
00230   }
00231 
00232   virtual void DrawWidget(const Rect &r, int widget) const
00233   {
00234     switch (widget) {
00235       case SLW_LIST: {
00236         uint y = r.top + WD_FRAMERECT_TOP; // Offset from top of widget.
00237         /* No signs? */
00238         if (this->vscroll->GetCount() == 0) {
00239           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right, y, STR_STATION_LIST_NONE);
00240           return;
00241         }
00242 
00243         bool rtl = _current_text_dir == TD_RTL;
00244         int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 + 1;
00245         uint icon_left  = 4 + (rtl ? r.right - this->text_offset : r.left);
00246         uint text_left  = r.left + (rtl ? WD_FRAMERECT_LEFT : this->text_offset);
00247         uint text_right = r.right - (rtl ? this->text_offset : WD_FRAMERECT_RIGHT);
00248 
00249         /* At least one sign available. */
00250         for (uint16 i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
00251           const Sign *si = this->signs[i];
00252 
00253           if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, y + sprite_offset_y);
00254 
00255           SetDParam(0, si->index);
00256           DrawString(text_left, text_right, y, STR_SIGN_NAME, TC_YELLOW);
00257           y += this->resize.step_height;
00258         }
00259         break;
00260       }
00261     }
00262   }
00263 
00264   virtual void SetStringParameters(int widget) const
00265   {
00266     if (widget == SLW_CAPTION) SetDParam(0, this->vscroll->GetCount());
00267   }
00268 
00269   virtual void OnClick(Point pt, int widget, int click_count)
00270   {
00271     switch (widget) {
00272       case SLW_LIST: {
00273         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, SLW_LIST, WD_FRAMERECT_TOP);
00274         if (id_v == INT_MAX) return;
00275 
00276         const Sign *si = this->signs[id_v];
00277         ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00278         break;
00279       }
00280       case SLW_FILTER_CLEAR_BTN:
00281         this->ClearFilterTextWidget(); // Empty the text in the EditBox widget
00282         this->SetFilterString("");     // Use empty text as filter text (= view all signs)
00283         break;
00284 
00285       case SLW_FILTER_MATCH_CASE_BTN:
00286         SignList::match_case = !SignList::match_case; // Toggle match case
00287         this->SetWidgetLoweredState(SLW_FILTER_MATCH_CASE_BTN, SignList::match_case); // Toggle button pushed state
00288         this->InvalidateData(); // Rebuild the list of signs
00289         break;
00290     }
00291   }
00292 
00293   virtual void OnResize()
00294   {
00295     this->vscroll->SetCapacityFromWidget(this, SLW_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
00296   }
00297 
00298   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00299   {
00300     switch (widget) {
00301       case SLW_LIST: {
00302         Dimension spr_dim = GetSpriteSize(SPR_COMPANY_ICON);
00303         this->text_offset = WD_FRAMETEXT_LEFT + spr_dim.width + 2; // 2 pixels space between icon and the sign text.
00304         resize->height = max<uint>(FONT_HEIGHT_NORMAL, spr_dim.height);
00305         Dimension d = {this->text_offset + WD_FRAMETEXT_RIGHT, WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM};
00306         *size = maxdim(*size, d);
00307         break;
00308       }
00309 
00310       case SLW_CAPTION:
00311         SetDParam(0, max<size_t>(1000, Sign::GetPoolSize()));
00312         *size = GetStringBoundingBox(STR_SIGN_LIST_CAPTION);
00313         size->height += padding.height;
00314         size->width  += padding.width;
00315         break;
00316     }
00317   }
00318 
00319   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00320   {
00321     EventState state = ES_NOT_HANDLED;
00322     switch (this->HandleEditBoxKey(SLW_FILTER_TEXT, key, keycode, state)) {
00323       case HEBR_EDITING:
00324         this->SetFilterString(this->text.buf);
00325         break;
00326 
00327       case HEBR_CONFIRM: // Enter pressed -> goto first sign in list
00328         if (this->signs.Length() >= 1) {
00329           const Sign *si = this->signs[0];
00330           ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00331         }
00332         return state;
00333 
00334       case HEBR_CANCEL: // ESC pressed, clear filter.
00335         this->OnClick(Point(), SLW_FILTER_CLEAR_BTN, 1); // Simulate click on clear button.
00336         this->UnfocusFocusedWidget();                    // Unfocus the text box.
00337         return state;
00338 
00339       case HEBR_NOT_FOCUSED: // The filter text box is not globaly focused.
00340         if (CheckHotkeyMatch(signlist_hotkeys, keycode, this) == SLHK_FOCUS_FILTER_BOX) {
00341           this->SetFocusedWidget(SLW_FILTER_TEXT);
00342           SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
00343           state = ES_HANDLED;
00344         }
00345         break;
00346 
00347       default:
00348         NOT_REACHED();
00349     }
00350 
00351     if (state == ES_HANDLED) OnOSKInput(SLW_FILTER_TEXT);
00352 
00353     return state;
00354   }
00355 
00356   virtual void OnOSKInput(int widget)
00357   {
00358     if (widget == SLW_FILTER_TEXT) this->SetFilterString(this->text.buf);
00359   }
00360 
00361   virtual void OnMouseLoop()
00362   {
00363     this->HandleEditBox(SLW_FILTER_TEXT);
00364   }
00365 
00366   void BuildSortSignList()
00367   {
00368     if (this->signs.NeedRebuild()) {
00369       this->BuildSignsList();
00370       this->vscroll->SetCount(this->signs.Length());
00371       this->SetWidgetDirty(SLW_CAPTION);
00372     }
00373     this->SortSignsList();
00374   }
00375 
00376   virtual void OnHundredthTick()
00377   {
00378     this->BuildSortSignList();
00379     this->SetDirty();
00380   }
00381 
00387   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00388   {
00389     if (data == -1) {
00390       /* The DO_SHOW_COMPETITOR_SIGNS display option has changed */
00391       this->signs.SetFilterState(!StrEmpty(this->filter_string) || !HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS));
00392     }
00393 
00394     /* When there is a filter string, we always need to rebuild the list even if
00395      * the amount of signs in total is unchanged, as the subset of signs that is
00396      * accepted by the filter might has changed. */
00397     if (data == 0 || data == -1 || !StrEmpty(this->filter_string)) { // New or deleted sign, changed visibility setting or there is a filter string
00398       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
00399       this->signs.ForceRebuild();
00400     } else { // Change of sign contents while there is no filter string
00401       this->signs.ForceResort();
00402     }
00403   }
00404 
00405   static Hotkey<SignListWindow> signlist_hotkeys[];
00406 };
00407 
00408 Hotkey<SignListWindow> SignListWindow::signlist_hotkeys[] = {
00409   Hotkey<SignListWindow>('F', "focus_filter_box", SLHK_FOCUS_FILTER_BOX),
00410   HOTKEY_LIST_END(SignListWindow)
00411 };
00412 Hotkey<SignListWindow> *_signlist_hotkeys = SignListWindow::signlist_hotkeys;
00413 
00414 static const NWidgetPart _nested_sign_list_widgets[] = {
00415   NWidget(NWID_HORIZONTAL),
00416     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00417     NWidget(WWT_CAPTION, COLOUR_GREY, SLW_CAPTION), SetDataTip(STR_SIGN_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00418     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00419     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00420   EndContainer(),
00421   NWidget(NWID_HORIZONTAL),
00422     NWidget(NWID_VERTICAL),
00423       NWidget(WWT_PANEL, COLOUR_GREY, SLW_LIST), SetMinimalSize(WD_FRAMETEXT_LEFT + 16 + 255 + WD_FRAMETEXT_RIGHT, 50),
00424                 SetResize(1, 10), SetFill(1, 0), SetScrollbar(SLW_SCROLLBAR), EndContainer(),
00425       NWidget(NWID_HORIZONTAL),
00426         NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1),
00427           NWidget(WWT_EDITBOX, COLOUR_GREY, SLW_FILTER_TEXT), SetMinimalSize(80, 12), SetResize(1, 0), SetFill(1, 0), SetPadding(2, 2, 2, 2),
00428               SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
00429         EndContainer(),
00430         NWidget(WWT_TEXTBTN, COLOUR_GREY, SLW_FILTER_MATCH_CASE_BTN), SetDataTip(STR_SIGN_LIST_MATCH_CASE, STR_SIGN_LIST_MATCH_CASE_TOOLTIP),
00431         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, SLW_FILTER_CLEAR_BTN), SetDataTip(STR_SIGN_LIST_CLEAR, STR_SIGN_LIST_CLEAR_TOOLTIP),
00432       EndContainer(),
00433     EndContainer(),
00434     NWidget(NWID_VERTICAL),
00435       NWidget(NWID_VERTICAL), SetFill(0, 1),
00436         NWidget(NWID_VSCROLLBAR, COLOUR_GREY, SLW_SCROLLBAR),
00437       EndContainer(),
00438       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00439     EndContainer(),
00440   EndContainer(),
00441 };
00442 
00443 static const WindowDesc _sign_list_desc(
00444   WDP_AUTO, 358, 138,
00445   WC_SIGN_LIST, WC_NONE,
00446   WDF_UNCLICK_BUTTONS,
00447   _nested_sign_list_widgets, lengthof(_nested_sign_list_widgets)
00448 );
00449 
00455 Window *ShowSignList()
00456 {
00457   return AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
00458 }
00459 
00460 EventState SignListGlobalHotkeys(uint16 key, uint16 keycode)
00461 {
00462   int num = CheckHotkeyMatch<SignListWindow>(_signlist_hotkeys, keycode, NULL, true);
00463   if (num == -1) return ES_NOT_HANDLED;
00464   Window *w = ShowSignList();
00465   if (w == NULL) return ES_NOT_HANDLED;
00466   return w->OnKeyPress(key, keycode);
00467 }
00468 
00475 static bool RenameSign(SignID index, const char *text)
00476 {
00477   bool remove = StrEmpty(text);
00478   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);
00479   return remove;
00480 }
00481 
00483 enum QueryEditSignWidgets {
00484   QUERY_EDIT_SIGN_WIDGET_CAPTION,
00485   QUERY_EDIT_SIGN_WIDGET_TEXT,
00486   QUERY_EDIT_SIGN_WIDGET_OK,
00487   QUERY_EDIT_SIGN_WIDGET_CANCEL,
00488   QUERY_EDIT_SIGN_WIDGET_DELETE,
00489   QUERY_EDIT_SIGN_WIDGET_PREVIOUS,
00490   QUERY_EDIT_SIGN_WIDGET_NEXT,
00491 };
00492 
00493 struct SignWindow : QueryStringBaseWindow, SignList {
00494   SignID cur_sign;
00495 
00496   SignWindow(const WindowDesc *desc, const Sign *si) : QueryStringBaseWindow(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
00497   {
00498     this->caption = STR_EDIT_SIGN_CAPTION;
00499     this->afilter = CS_ALPHANUMERAL;
00500 
00501     this->InitNested(desc);
00502 
00503     this->LowerWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
00504     UpdateSignEditWindow(si);
00505     this->SetFocusedWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
00506   }
00507 
00508   void UpdateSignEditWindow(const Sign *si)
00509   {
00510     char *last_of = &this->edit_str_buf[this->edit_str_size - 1]; // points to terminating '\0'
00511 
00512     /* Display an empty string when the sign hasnt been edited yet */
00513     if (si->name != NULL) {
00514       SetDParam(0, si->index);
00515       GetString(this->edit_str_buf, STR_SIGN_NAME, last_of);
00516     } else {
00517       GetString(this->edit_str_buf, STR_EMPTY, last_of);
00518     }
00519     *last_of = '\0';
00520 
00521     this->cur_sign = si->index;
00522     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, this->max_chars);
00523 
00524     this->SetWidgetDirty(QUERY_EDIT_SIGN_WIDGET_TEXT);
00525     this->SetFocusedWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
00526   }
00527 
00533   const Sign *PrevNextSign(bool next)
00534   {
00535     /* Rebuild the sign list */
00536     this->signs.ForceRebuild();
00537     this->signs.NeedResort();
00538     this->BuildSignsList();
00539     this->SortSignsList();
00540 
00541     /* Search through the list for the current sign, excluding
00542      * - the first sign if we want the previous sign or
00543      * - the last sign if we want the next sign */
00544     uint end = this->signs.Length() - (next ? 1 : 0);
00545     for (uint i = next ? 0 : 1; i < end; i++) {
00546       if (this->cur_sign == this->signs[i]->index) {
00547         /* We've found the current sign, so return the sign before/after it */
00548         return this->signs[i + (next ? 1 : -1)];
00549       }
00550     }
00551     /* If we haven't found the current sign by now, return the last/first sign */
00552     return this->signs[next ? 0 : this->signs.Length() - 1];
00553   }
00554 
00555   virtual void SetStringParameters(int widget) const
00556   {
00557     switch (widget) {
00558       case QUERY_EDIT_SIGN_WIDGET_CAPTION:
00559         SetDParam(0, this->caption);
00560         break;
00561     }
00562   }
00563 
00564   virtual void OnPaint()
00565   {
00566     this->DrawWidgets();
00567     if (!this->IsShaded()) this->DrawEditBox(QUERY_EDIT_SIGN_WIDGET_TEXT);
00568   }
00569 
00570   virtual void OnClick(Point pt, int widget, int click_count)
00571   {
00572     switch (widget) {
00573       case QUERY_EDIT_SIGN_WIDGET_PREVIOUS:
00574       case QUERY_EDIT_SIGN_WIDGET_NEXT: {
00575         const Sign *si = this->PrevNextSign(widget == QUERY_EDIT_SIGN_WIDGET_NEXT);
00576 
00577         /* Rebuild the sign list */
00578         this->signs.ForceRebuild();
00579         this->signs.NeedResort();
00580         this->BuildSignsList();
00581         this->SortSignsList();
00582 
00583         /* Scroll to sign and reopen window */
00584         ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00585         UpdateSignEditWindow(si);
00586         break;
00587       }
00588 
00589       case QUERY_EDIT_SIGN_WIDGET_DELETE:
00590         /* Only need to set the buffer to null, the rest is handled as the OK button */
00591         RenameSign(this->cur_sign, "");
00592         /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
00593         break;
00594 
00595       case QUERY_EDIT_SIGN_WIDGET_OK:
00596         if (RenameSign(this->cur_sign, this->text.buf)) break;
00597         /* FALL THROUGH */
00598 
00599       case QUERY_EDIT_SIGN_WIDGET_CANCEL:
00600         delete this;
00601         break;
00602     }
00603   }
00604 
00605   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00606   {
00607     EventState state = ES_NOT_HANDLED;
00608     switch (this->HandleEditBoxKey(QUERY_EDIT_SIGN_WIDGET_TEXT, key, keycode, state)) {
00609       default: break;
00610 
00611       case HEBR_CONFIRM:
00612         if (RenameSign(this->cur_sign, this->text.buf)) break;
00613         /* FALL THROUGH */
00614 
00615       case HEBR_CANCEL: // close window, abandon changes
00616         delete this;
00617         break;
00618     }
00619     return state;
00620   }
00621 
00622   virtual void OnMouseLoop()
00623   {
00624     this->HandleEditBox(QUERY_EDIT_SIGN_WIDGET_TEXT);
00625   }
00626 
00627   virtual void OnOpenOSKWindow(int wid)
00628   {
00629     ShowOnScreenKeyboard(this, wid, QUERY_EDIT_SIGN_WIDGET_CANCEL, QUERY_EDIT_SIGN_WIDGET_OK);
00630   }
00631 };
00632 
00633 static const NWidgetPart _nested_query_sign_edit_widgets[] = {
00634   NWidget(NWID_HORIZONTAL),
00635     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00636     NWidget(WWT_CAPTION, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00637   EndContainer(),
00638   NWidget(WWT_PANEL, COLOUR_GREY),
00639     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),
00640   EndContainer(),
00641   NWidget(NWID_HORIZONTAL),
00642     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_OK), SetMinimalSize(61, 12), SetDataTip(STR_BUTTON_OK, STR_NULL),
00643     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_CANCEL), SetMinimalSize(60, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
00644     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_DELETE), SetMinimalSize(60, 12), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_NULL),
00645     NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
00646     NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_PREVIOUS), SetMinimalSize(11, 12), SetDataTip(AWV_DECREASE, STR_EDIT_SIGN_PREVIOUS_SIGN_TOOLTIP),
00647     NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_NEXT), SetMinimalSize(11, 12), SetDataTip(AWV_INCREASE, STR_EDIT_SIGN_NEXT_SIGN_TOOLTIP),
00648   EndContainer(),
00649 };
00650 
00651 static const WindowDesc _query_sign_edit_desc(
00652   WDP_AUTO, 0, 0,
00653   WC_QUERY_STRING, WC_NONE,
00654   WDF_CONSTRUCTION | WDF_UNCLICK_BUTTONS,
00655   _nested_query_sign_edit_widgets, lengthof(_nested_query_sign_edit_widgets)
00656 );
00657 
00662 void HandleClickOnSign(const Sign *si)
00663 {
00664   if (_ctrl_pressed && si->owner == _local_company) {
00665     RenameSign(si->index, NULL);
00666     return;
00667   }
00668   ShowRenameSignWindow(si);
00669 }
00670 
00675 void ShowRenameSignWindow(const Sign *si)
00676 {
00677   /* Delete all other edit windows */
00678   DeleteWindowById(WC_QUERY_STRING, 0);
00679 
00680   new SignWindow(&_query_sign_edit_desc, si);
00681 }
00682 
00687 void DeleteRenameSignWindow(SignID sign)
00688 {
00689   SignWindow *w = dynamic_cast<SignWindow *>(FindWindowById(WC_QUERY_STRING, 0));
00690 
00691   if (w != NULL && w->cur_sign == sign) delete w;
00692 }