#pragma implementation "ListBox.h" #include "ListBox.h" #include #include #include bmsListBox::bmsListBox(const char *title,TextArray &l): txt(l), FramedWin(title) { topline = 0; cursel = 0; matchlen = 0; curtype(1); } bmsListBox::~bmsListBox() { } void bmsListBox::draw(const Rect &r) { for (int i = r.r1; i <= r.r2; ++i) { int idx = topline + i; const char *p = txt[idx]; if (!p) p = ""; write(i,0,r.width(),p,(idx == cursel)?A_REVERSE:A_NORMAL); } } void bmsListBox::set(int sel) { if (txt[sel]) { cursel = sel; if (cursel < topline) { topline = cursel; } else if (cursel - topline >= loc.height()) { topline = cursel - loc.height() + 1; } redraw(); curpos(cursel - topline,matchlen); sync(); } } void bmsListBox::key(int ky) { switch (ky) { case KEY_HOME: set(0); return; case KEY_UP: case KEY_LEFT: if (cursel > 0) set(--cursel); return; case KEY_DOWN: case KEY_RIGHT: if (txt[cursel+1]) set(++cursel); return; case KEY_F(3): case KEY_SF: cursel += loc.height(); if (txt[cursel]) { set(cursel); return; } /* cursel = txt.size(); */ /* fall through to page back from end */ case KEY_F(2): case KEY_SR: cursel -= loc.height(); if (cursel < 0) cursel = 0; set(cursel); return; } if (isascii(ky) && isprint(ky)) { ky = toupper(ky); for (int numtry = 0; ; ++numtry) { const char *p; for (int i = cursel; p = txt[i]; ++i) { if (*p == 0) continue; if (strlen(p) <= matchlen) break; if (strncasecmp(p,txt[cursel],matchlen)) break; if (toupper(p[matchlen]) == ky) { set(cursel = i); ++matchlen; return; } } if (numtry) break; matchlen = 0; cursel = 0; } set(cursel); return; } /* FramedWin::key(ky); */ setfocus(0); return; } void bmsListBox::setpos(Point p) { int max = screen.r2 - 1; int w = frame->title ? strlen(frame->title) + 2 : 0; const char *s; int i; for (i = 0; i < max && (s = txt[i]); ++i) { int len = strlen(s) + 2; if (len > w) w = len; } if (i + p.row > screen.r2) { p.row = screen.r2 - i; if (p.row < 0) p.row = 0; } if (w + p.col > screen.c2) { p.col = screen.c2 - w; if (p.col < 0) p.col = 0; } Rect r(p,w,i); setphys(r.intersect(screen.expand(-1))); }