# signs_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: signs_gui.cpp 14388 2008-09-22 20:20:46Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "gui.h"
    00008 #include "textbuf_gui.h"
    00009 #include "window_gui.h"
    00010 #include "player_gui.h"
    00011 #include "signs.h"
    00012 #include "debug.h"
    00013 #include "variables.h"
    00014 #include "command_func.h"
    00015 #include "strings_func.h"
    00016 #include "core/alloc_func.hpp"
    00017 #include "window_func.h"
    00018 #include "map_func.h"
    00019 #include "gfx_func.h"
    00020 #include "viewport_func.h"
    00021
    00022 #include "table/strings.h"
    00023 #include "table/sprites.h"
    00024
    00025 static const Sign **_sign_sort;
    00026 static uint _num_sign_sort;
    00027
    00028 static char _bufcache[64];
    00029 static const Sign *_last_sign;
    00030
    00031 static int CDECL SignNameSorter(const void *a, const void *b)
    00032 {
    00033   const Sign *sign0 = *(const Sign**)a;
    00034   const Sign *sign1 = *(const Sign**)b;
    00035   char buf1[64];
    00036
    00037   SetDParam(0, sign0->index);
    00038   GetString(buf1, STR_SIGN_NAME, lastof(buf1));
    00039
    00040   if (sign1 != _last_sign) {
    00041     _last_sign = sign1;
    00042     SetDParam(0, sign1->index);
    00043     GetString(_bufcache, STR_SIGN_NAME, lastof(_bufcache));
    00044   }
    00045
    00046   return strcmp(buf1, _bufcache); // sort by name
    00047 }
    00048
    00049 static void GlobalSortSignList()
    00050 {
    00051   const Sign *si;
    00052   uint n = 0;
    00053
    00054   /* Create array for sorting */
    00055   _sign_sort = ReallocT(_sign_sort, GetMaxSignIndex() + 1);
    00056
    00057   FOR_ALL_SIGNS(si) _sign_sort[n++] = si;
    00058   _num_sign_sort = n;
    00059
    00060   qsort((void*)_sign_sort, n, sizeof(_sign_sort[0]), SignNameSorter);
    00061
    00062   _sign_sort_dirty = false;
    00063
    00064   DEBUG(misc, 3, "Resorting global signs list");
    00065 }
    00066
    00067 static void SignListWndProc(Window *w, WindowEvent *e)
    00068 {
    00069   switch (e->event) {
    00070   case WE_PAINT: {
    00071     int y = 16; // offset from top of widget
    00072
    00073     if (_sign_sort_dirty)
    00074       GlobalSortSignList();
    00075
    00076     SetVScrollCount(w, _num_sign_sort);
    00077
    00078     SetDParam(0, w->vscroll.count);
    00079     DrawWindowWidgets(w);
    00080
    00081     /* No signs? */
    00082     if (w->vscroll.count == 0) {
    00083       DrawString(2, y, STR_304A_NONE, TC_FROMSTRING);
    00084       return;
    00085     }
    00086
    00087     {
    00088       uint16 i;
    00089
    00090       /* Start drawing the signs */
    00091       for (i = w->vscroll.pos; i < w->vscroll.cap + w->vscroll.pos && i < w->vscroll.count; i++) {
    00092         const Sign *si = _sign_sort[i];
    00093
    00094         if (si->owner != OWNER_NONE)
    00095           DrawPlayerIcon(si->owner, 4, y + 1);
    00096
    00097         SetDParam(0, si->index);
    00098         DrawString(22, y, STR_SIGN_NAME, TC_YELLOW);
    00099         y += 10;
    00100       }
    00101     }
    00102   } break;
    00103
    00104   case WE_CLICK: {
    00105     switch (e->we.click.widget) {
    00106     case 3: {
    00107       uint32 id_v = (e->we.click.pt.y - 15) / 10;
    00108       const Sign *si;
    00109
    00110       if (id_v >= w->vscroll.cap)
    00111         return;
    00112
    00113       id_v += w->vscroll.pos;
    00114
    00115       if (id_v >= w->vscroll.count)
    00116         return;
    00117
    00118       si = _sign_sort[id_v];
    00119       ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
    00120     } break;
    00121     }
    00122   } break;
    00123
    00124   case WE_RESIZE:
    00125     w->vscroll.cap += e->we.sizing.diff.y / 10;
    00126     break;
    00127   }
    00128 }
    00129
    00130 static const Widget _sign_list_widget[] = {
    00131 {   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,              STR_018B_CLOSE_WINDOW},
    00132 {    WWT_CAPTION,  RESIZE_RIGHT,    14,    11,   345,     0,    13, STR_SIGN_LIST_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS},
    00133 {  WWT_STICKYBOX,     RESIZE_LR,    14,   346,   357,     0,    13, 0x0,                   STR_STICKY_BUTTON},
    00134 {      WWT_PANEL,     RESIZE_RB,    14,     0,   345,    14,   137, 0x0,                   STR_NULL},
    00135 {  WWT_SCROLLBAR,    RESIZE_LRB,    14,   346,   357,    14,   125, 0x0,                   STR_0190_SCROLL_BAR_SCROLLS_LIST},
    00136 {  WWT_RESIZEBOX,   RESIZE_LRTB,    14,   346,   357,   126,   137, 0x0,                   STR_RESIZE_BUTTON},
    00137 {   WIDGETS_END},
    00138 };
    00139
    00140 static const WindowDesc _sign_list_desc = {
    00141   WDP_AUTO, WDP_AUTO, 358, 138, 358, 138,
    00142   WC_SIGN_LIST, WC_NONE,
    00143   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON | WDF_RESIZABLE,
    00144   _sign_list_widget,
    00145   SignListWndProc
    00146 };
    00147
    00148
    00149 void ShowSignList()
    00150 {
    00151   Window *w;
    00152
    00153   w = AllocateWindowDescFront(&_sign_list_desc, 0);
    00154   if (w != NULL) {
    00155     w->vscroll.cap = 12;
    00156     w->resize.step_height = 10;
    00157     w->resize.height = w->height - 10 * 7; // minimum if 5 in the list
    00158   }
    00159 }
    00160
    00161 /* Edit sign window stuff */
    00162
    00163 struct editsign_d : querystr_d {
    00164   SignID cur_sign;
    00165 };
    00166
    00167 static char _edit_str_buf[64];
    00168
    00169 enum QueryEditSignWidgets {
    00170   QUERY_EDIT_SIGN_WIDGET_TEXT = 3,
    00171   QUERY_EDIT_SIGN_WIDGET_OK,
    00172   QUERY_EDIT_SIGN_WIDGET_CANCEL,
    00173   QUERY_EDIT_SIGN_WIDGET_DELETE,
    00174   QUERY_EDIT_SIGN_WIDGET_PREVIOUS = QUERY_EDIT_SIGN_WIDGET_DELETE + 2,
    00175   QUERY_EDIT_SIGN_WIDGET_NEXT,
    00176 };
    00177
    00178 static void UpdateSignEditWindow(Window *w, const Sign *si)
    00179 {
    00180   /* Display an empty string when the sign hasnt been edited yet */
    00181   if (si->name != NULL) {
    00182     SetDParam(0, si->index);
    00183     GetString(_edit_str_buf, STR_SIGN_NAME, lastof(_edit_str_buf));
    00184   } else {
    00185     GetString(_edit_str_buf, STR_EMPTY, lastof(_edit_str_buf));
    00186   }
    00187   _edit_str_buf[lengthof(_edit_str_buf) - 1] = '\0';
    00188
    00189   WP(w, editsign_d).cur_sign = si->index;
    00190   InitializeTextBuffer(&WP(w, querystr_d).text, _edit_str_buf, 31, 255); // Allow 31 characters (including \0)
    00191
    00192   w->InvalidateWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
    00193 }
    00194
    00195 static void RenameSign(SignID index, const char *text)
    00196 {
    00197   _cmd_text = text;
    00198   DoCommandP(0, index, 0, NULL, CMD_RENAME_SIGN | CMD_MSG(STR_280C_CAN_T_CHANGE_SIGN_NAME));
    00199 }
    00200
    00201 static void QuerySignEditWndProc(Window *w, WindowEvent *e)
    00202 {
    00203   editsign_d *qs = &WP(w, editsign_d);
    00204   Sign       *si;
    00205   uint       sign_index = 0;
    00206
    00207   switch (e->event) {
    00208     case WE_CREATE:
    00209       SetBit(_no_scroll, SCROLL_EDIT);
    00210       break;
    00211
    00212     case WE_PAINT:
    00213       SetDParam(0, qs->caption);
    00214       DrawWindowWidgets(w);
    00215       DrawEditBox(w, qs, QUERY_EDIT_SIGN_WIDGET_TEXT);
    00216       break;
    00217
    00218     case WE_CLICK:
    00219       switch (e->we.click.widget) {
    00220         case QUERY_EDIT_SIGN_WIDGET_PREVIOUS:
    00221           if (_sign_sort_dirty) GlobalSortSignList();
    00222           sign_index = _sign_sort[_num_sign_sort - 1]->index;
    00223           for (uint i = 1; i < _num_sign_sort; i++) {
    00224             if (qs->cur_sign == _sign_sort[i]->index) {
    00225               sign_index = _sign_sort[i - 1]->index;
    00226               break;
    00227             }
    00228           }
    00229           si = GetSign(sign_index);
    00230
    00231           /* Scroll to sign and reopen window */
    00232           ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
    00233           UpdateSignEditWindow(w, si);
    00234           break;
    00235
    00236         case QUERY_EDIT_SIGN_WIDGET_NEXT:
    00237           if (_sign_sort_dirty) GlobalSortSignList();
    00238           sign_index = _sign_sort[0]->index;
    00239           for (uint i = 0; i < _num_sign_sort-1; i++) {
    00240             if (qs->cur_sign == _sign_sort[i]->index) {
    00241               sign_index = _sign_sort[i + 1]->index;
    00242               break;
    00243             }
    00244           }
    00245           si = GetSign(sign_index);
    00246
    00247           /* Scroll to sign and reopen window */
    00248           ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
    00249           UpdateSignEditWindow(w, si);
    00250           break;
    00251
    00252         case QUERY_EDIT_SIGN_WIDGET_DELETE:
    00253           /* Only need to set the buffer to null, the rest is handled as the OK button */
    00254           RenameSign(qs->cur_sign, "");
    00255           /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
    00256           break;
    00257
    00258         case QUERY_EDIT_SIGN_WIDGET_OK:
    00259           RenameSign(qs->cur_sign, qs->text.buf);
    00260           /* FALL THROUGH */
    00261
    00262         case QUERY_EDIT_SIGN_WIDGET_CANCEL:
    00263           DeleteWindow(w);
    00264           break;
    00265       }
    00266       break;
    00267
    00268     case WE_KEYPRESS:
    00269       switch (HandleEditBoxKey(w, qs, QUERY_EDIT_SIGN_WIDGET_TEXT, e)) {
    00270         case 1: // Enter pressed, confirms change
    00271           RenameSign(qs->cur_sign, qs->text.buf);
    00272           /* FALL THROUGH */
    00273
    00274         case 2: // ESC pressed, closes window, abandons changes
    00275           DeleteWindow(w);
    00276           break;
    00277       }
    00278       break;
    00279
    00280     case WE_MOUSELOOP:
    00281       HandleEditBox(w, qs, QUERY_EDIT_SIGN_WIDGET_TEXT);
    00282       break;
    00283
    00284     case WE_DESTROY:
    00285       ClrBit(_no_scroll, SCROLL_EDIT);
    00286       break;
    00287   }
    00288 }
    00289
    00290 static const Widget _query_sign_edit_widgets[] = {
    00291 { WWT_CLOSEBOX, RESIZE_NONE,  14,   0,  10,   0,  13, STR_00C5,        STR_018B_CLOSE_WINDOW},
    00292 {  WWT_CAPTION, RESIZE_NONE,  14,  11, 259,   0,  13, STR_012D,        STR_NULL },
    00293 {    WWT_PANEL, RESIZE_NONE,  14,   0, 259,  14,  29, STR_NULL,        STR_NULL },
    00294 {    WWT_PANEL, RESIZE_NONE,  14,   2, 257,  16,  27, STR_NULL,        STR_NULL },  // Text field
    00295 {  WWT_TEXTBTN, RESIZE_NONE,  14,   0,  60,  30,  41, STR_012F_OK,     STR_NULL },
    00296 {  WWT_TEXTBTN, RESIZE_NONE,  14,  61, 120,  30,  41, STR_012E_CANCEL, STR_NULL },
    00297 {  WWT_TEXTBTN, RESIZE_NONE,  14, 121, 180,  30,  41, STR_0290_DELETE, STR_NULL },
    00298 {    WWT_PANEL, RESIZE_NONE,  14, 181, 237,  30,  41, STR_NULL,        STR_NULL },
    00299 {  WWT_TEXTBTN, RESIZE_NONE,  14, 238, 248,  30,  41, STR_6819,        STR_PREVIOUS_SIGN_TOOLTIP },
    00300 {  WWT_TEXTBTN, RESIZE_NONE,  14, 249, 259,  30,  41, STR_681A,        STR_NEXT_SIGN_TOOLTIP },
    00301 { WIDGETS_END },
    00302 };
    00303
    00304 static const WindowDesc _query_sign_edit_desc = {
    00305   190, 170, 260, 42, 260, 42,
    00306   WC_QUERY_STRING, WC_NONE,
    00307   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
    00308   _query_sign_edit_widgets,
    00309   QuerySignEditWndProc
    00310 };
    00311
    00312 void ShowRenameSignWindow(const Sign *si)
    00313 {
    00314   Window *w;
    00315
    00316   /* Delete all other edit windows and the save window */
    00317   DeleteWindowById(WC_QUERY_STRING, 0);
    00318   DeleteWindowById(WC_SAVELOAD, 0);
    00319
    00320   w = AllocateWindowDesc(&_query_sign_edit_desc);
    00321
    00322   WP(w, editsign_d).caption = STR_280B_EDIT_SIGN_TEXT;
    00323   WP(w, editsign_d).afilter = CS_ALPHANUMERAL;
    00324   w->LowerWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
    00325
    00326   UpdateSignEditWindow(w, si);
    00327 }
    00328
    00329 void DeleteRenameSignWindow(SignID sign)
    00330 {
    00331   Window *w = FindWindowById(WC_QUERY_STRING, 0);
    00332
    00333   if (w != NULL && WP(w, editsign_d).caption == STR_280B_EDIT_SIGN_TEXT && WP(w, editsign_d).cur_sign == sign) DeleteWindow(w);
    00334 }