tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@507: #include "table/strings.h" celestar@2187: #include "table/sprites.h" tron@2163: #include "functions.h" truelight@0: #include "window.h" truelight@0: #include "gui.h" truelight@0: #include "gfx.h" truelight@0: #include "player.h" truelight@1575: #include "signs.h" truelight@1575: #include "strings.h" truelight@1575: #include "debug.h" tron@2159: #include "variables.h" rubidium@5838: #include "helpers.hpp" truelight@0: tron@4353: static const Sign **_sign_sort; tron@4353: static uint _num_sign_sort; truelight@1575: truelight@1575: static char _bufcache[64]; tron@4353: static const Sign *_last_sign; truelight@1575: truelight@1575: static int CDECL SignNameSorter(const void *a, const void *b) truelight@1575: { tron@4353: const Sign *sign0 = *(const Sign**)a; tron@4353: const Sign *sign1 = *(const Sign**)b; truelight@1575: char buf1[64]; truelight@1575: Darkvater@4912: GetString(buf1, sign0->str, lastof(buf1)); truelight@1575: tron@4353: if (sign1 != _last_sign) { tron@4353: _last_sign = sign1; Darkvater@4912: GetString(_bufcache, sign1->str, lastof(_bufcache)); truelight@1575: } truelight@1575: belugas@3801: return strcmp(buf1, _bufcache); // sort by name truelight@1575: } truelight@1575: truelight@1575: static void GlobalSortSignList(void) truelight@1575: { truelight@4349: const Sign *si; tron@4353: uint n = 0; truelight@1575: truelight@1575: /* Create array for sorting */ KUDr@5860: _sign_sort = ReallocT(_sign_sort, GetMaxSignIndex() + 1); Darkvater@5568: if (_sign_sort == NULL) error("Could not allocate memory for the sign-sorting-list"); truelight@1575: tron@4353: FOR_ALL_SIGNS(si) _sign_sort[n++] = si; tron@4353: _num_sign_sort = n; tron@4353: tron@4353: qsort((void*)_sign_sort, n, sizeof(_sign_sort[0]), SignNameSorter); truelight@1575: truelight@1575: _sign_sort_dirty = false; truelight@1575: Darkvater@5568: DEBUG(misc, 3, "Resorting global signs list"); truelight@1575: } truelight@1575: truelight@1575: static void SignListWndProc(Window *w, WindowEvent *e) truelight@1575: { truelight@1575: switch (e->event) { truelight@1575: case WE_PAINT: { truelight@1575: int y = 16; // offset from top of widget truelight@1575: truelight@1575: if (_sign_sort_dirty) truelight@1575: GlobalSortSignList(); truelight@1575: truelight@1575: SetVScrollCount(w, _num_sign_sort); truelight@1575: truelight@1575: SetDParam(0, w->vscroll.count); bjarni@6241: w->DrawWidgets(); truelight@1575: truelight@1575: /* No signs? */ truelight@1575: if (w->vscroll.count == 0) { truelight@1575: DrawString(2, y, STR_304A_NONE, 0); truelight@1575: return; truelight@1575: } truelight@1575: truelight@4349: { Darkvater@2135: uint16 i; truelight@1575: truelight@1575: /* Start drawing the signs */ tron@2133: for (i = w->vscroll.pos; i < w->vscroll.cap + w->vscroll.pos && i < w->vscroll.count; i++) { tron@4353: const Sign *si = _sign_sort[i]; truelight@1575: truelight@4349: if (si->owner != OWNER_NONE) truelight@4349: DrawPlayerIcon(si->owner, 4, y + 1); truelight@1575: truelight@4349: DrawString(22, y, si->str, 8); truelight@1575: y += 10; truelight@1575: } truelight@1575: } truelight@1575: } break; truelight@1575: truelight@1575: case WE_CLICK: { belugas@4634: switch (e->we.click.widget) { truelight@1575: case 3: { belugas@4634: uint32 id_v = (e->we.click.pt.y - 15) / 10; truelight@4349: const Sign *si; truelight@1575: truelight@1575: if (id_v >= w->vscroll.cap) truelight@1575: return; truelight@1575: truelight@1575: id_v += w->vscroll.pos; truelight@1575: truelight@1575: if (id_v >= w->vscroll.count) truelight@1575: return; truelight@1575: tron@4353: si = _sign_sort[id_v]; truelight@4349: ScrollMainWindowToTile(TileVirtXY(si->x, si->y)); truelight@1575: } break; truelight@1575: } truelight@1575: } break; truelight@1575: truelight@1575: case WE_RESIZE: belugas@4634: w->vscroll.cap += e->we.sizing.diff.y / 10; truelight@1575: break; truelight@1575: } truelight@1575: } truelight@1575: truelight@1575: static const Widget _sign_list_widget[] = { rubidium@4344: { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, rubidium@4344: { WWT_CAPTION, RESIZE_RIGHT, 14, 11, 345, 0, 13, STR_SIGN_LIST_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS}, rubidium@4344: { WWT_STICKYBOX, RESIZE_LR, 14, 346, 357, 0, 13, 0x0, STR_STICKY_BUTTON}, rubidium@4344: { WWT_PANEL, RESIZE_RB, 14, 0, 345, 14, 137, 0x0, STR_NULL}, rubidium@4344: { WWT_SCROLLBAR, RESIZE_LRB, 14, 346, 357, 14, 125, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, rubidium@4344: { WWT_RESIZEBOX, RESIZE_LRTB, 14, 346, 357, 126, 137, 0x0, STR_RESIZE_BUTTON}, truelight@1575: { WIDGETS_END}, truelight@1575: }; truelight@1575: truelight@1575: static const WindowDesc _sign_list_desc = { Darkvater@5070: WDP_AUTO, WDP_AUTO, 358, 138, rubidium@6144: WC_SIGN_LIST, WC_NONE, truelight@1575: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON | WDF_RESIZABLE, truelight@1575: _sign_list_widget, truelight@1575: SignListWndProc truelight@1575: }; truelight@1575: truelight@1575: truelight@1575: void ShowSignList(void) truelight@1575: { truelight@1575: Window *w; truelight@1575: truelight@1575: w = AllocateWindowDescFront(&_sign_list_desc, 0); truelight@1575: if (w != NULL) { truelight@1575: w->vscroll.cap = 12; truelight@1575: w->resize.step_height = 10; truelight@1575: w->resize.height = w->height - 10 * 7; // minimum if 5 in the list truelight@1575: } truelight@1575: }