bool ListControl::OnKeyDown(unsigned key_code) { scroll_bar.DragEnd(this); kinetic_timer.Cancel(); switch (key_code) { case KEY_RETURN: if (CanActivateItem()) ActivateItem(); return true; case KEY_UP: // previous item if (GetCursorIndex() <= 0) break; MoveCursor(-1); return true; case KEY_DOWN: // next item if (GetCursorIndex() +1 >= length) break; MoveCursor(1); return true; case KEY_LEFT: // page up MoveCursor(-(int)items_visible); return true; case KEY_RIGHT: // page down MoveCursor(items_visible); return true; case KEY_HOME: SetCursorIndex(0); return true; case KEY_END: if (length > 0) { SetCursorIndex(length - 1); } return true; case KEY_PRIOR: MoveCursor(-(int)items_visible); return true; case KEY_NEXT: MoveCursor(items_visible); return true; } return PaintWindow::OnKeyDown(key_code); }
void ListControl::SetLength(unsigned n) { if (n == length) return; unsigned cursor = GetCursorIndex(); length = n; if (n == 0) cursor = 0; else if (cursor >= n) cursor = n - 1; items_visible = GetHeight() / item_height; if (n <= items_visible) origin = 0; else if (origin + items_visible > n) origin = n - items_visible; else if (cursor < origin) origin = cursor; show_or_hide_scroll_bar(); Invalidate(); SetCursorIndex(cursor); }
void ListControl::MoveCursor(int delta) { if (length == 0) return; int new_cursor = cursor + delta; if (new_cursor < 0) new_cursor = 0; else if ((unsigned)new_cursor >= length) new_cursor = length - 1; SetCursorIndex(new_cursor); }
bool ListControl::OnMouseDown(PixelScalar x, PixelScalar y) { // End any previous drag scroll_bar.DragEnd(this); drag_end(); #ifndef _WIN32_WCE kinetic_timer.Cancel(); #endif RasterPoint Pos; Pos.x = x; Pos.y = y; // If possible -> Give focus to the Control const bool had_focus = HasFocus(); if (!had_focus) SetFocus(); if (scroll_bar.IsInsideSlider(Pos)) { // if click is on scrollbar handle // -> start mouse drag scroll_bar.DragBegin(this, Pos.y); } else if (scroll_bar.IsInside(Pos)) { // if click in scroll bar up/down/pgup/pgdn if (scroll_bar.IsInsideUpArrow(Pos.y)) // up MoveOrigin(-1); else if (scroll_bar.IsInsideDownArrow(Pos.y)) // down MoveOrigin(1); else if (scroll_bar.IsAboveSlider(Pos.y)) // page up MoveOrigin(-(int)items_visible); else if (scroll_bar.IsBelowSlider(Pos.y)) // page down MoveOrigin(items_visible); } else { // if click in ListBox area // -> select appropriate item int index = ItemIndexAt(y); // If mouse was clicked outside the list items -> cancel if (index < 0) return false; drag_y = GetPixelOrigin() + y; drag_y_window = y; if (had_focus && (unsigned)index == GetCursorIndex() && CanActivateItem()) { drag_mode = DragMode::CURSOR; Invalidate_item(cursor); } else { // If item was not selected before // -> select it SetCursorIndex(index); drag_mode = DragMode::SCROLL; } #ifndef _WIN32_WCE kinetic.MouseDown(GetPixelOrigin()); #endif SetCapture(); } return true; }
bool ListControl::OnKeyDown(unsigned key_code) { scroll_bar.DragEnd(this); #ifndef _WIN32_WCE kinetic_timer.Cancel(); #endif switch (key_code) { #ifdef GNAV // JMW added this to make data entry easier case KEY_APP4: #endif case KEY_RETURN: if (CanActivateItem()) ActivateItem(); return true; case KEY_UP: case KEY_LEFT: if (!HasPointer() ^ (key_code == KEY_LEFT)) { // page up MoveCursor(-(int)items_visible); return true; } else { // previous item if (GetCursorIndex() <= 0) break; MoveCursor(-1); return true; } case KEY_DOWN: case KEY_RIGHT: if (!HasPointer() ^ (key_code == KEY_RIGHT)) { // page down MoveCursor(items_visible); return true; } else { // next item if (GetCursorIndex() +1 >= length) break; MoveCursor(1); return true; } case KEY_HOME: SetCursorIndex(0); return true; case KEY_END: if (length > 0) { SetCursorIndex(length - 1); } return true; case KEY_PRIOR: MoveCursor(-(int)items_visible); return true; case KEY_NEXT: MoveCursor(items_visible); return true; } return PaintWindow::OnKeyDown(key_code); }