From 072aa94bc5ad1c65dde767ef977c7045665442b8 Mon Sep 17 00:00:00 2001 From: casey Date: Mon, 16 May 2016 22:55:55 -0700 Subject: [PATCH] Moved up/down/pageup/pagedown/home/end key handling into ScrollableWindow and out of the main loop. --- src/musikbox/Main.cpp | 60 ++++++---------------- src/musikbox/app/window/CommandWindow.cpp | 4 +- src/musikbox/app/window/TrackListView.cpp | 5 +- src/musikbox/app/window/TrackListView.h | 2 +- src/musikbox/cursespp/ScrollableWindow.cpp | 11 ++++ src/musikbox/cursespp/ScrollableWindow.h | 5 +- 6 files changed, 37 insertions(+), 50 deletions(-) diff --git a/src/musikbox/Main.cpp b/src/musikbox/Main.cpp index c8702c330..35de34d0a 100644 --- a/src/musikbox/Main.cpp +++ b/src/musikbox/Main.cpp @@ -187,56 +187,26 @@ int main(int argc, char* argv[]) else { ch = getch(); } - if (ch == -1) { - /* idle timeout */ - } - else if (ch == 9) { /* tab */ - focusNextInLayout(state); - } - else if (ch >= KEY_F(0) && ch <= KEY_F(12)) { - if (ch == KEY_F(1)) { - changeLayout(state, &mainLayout); + + if (ch != -1) { /* -1 = idle timeout */ + if (ch == '\t') { /* tab */ + focusNextInLayout(state); } - else if (ch == KEY_F(2)) { - changeLayout(state, &libraryLayout); + else if (ch >= KEY_F(0) && ch <= KEY_F(12)) { + if (ch == KEY_F(1)) { + changeLayout(state, &mainLayout); + } + else if (ch == KEY_F(2)) { + changeLayout(state, &libraryLayout); + } } - } - else if (ch == KEY_NPAGE) { - if (state.scrollable) { - state.scrollable->PageDown(); + else if (state.input) { + state.input->WriteChar(ch); } - } - else if (ch == KEY_PPAGE) { - if (state.scrollable) { - state.scrollable->PageUp(); + else if (state.keyHandler) { + state.keyHandler->KeyPress(ch); } } - else if (ch == KEY_DOWN) { - if (state.scrollable) { - state.scrollable->ScrollDown(); - } - } - else if (ch == KEY_UP) { - if (state.scrollable) { - state.scrollable->ScrollUp(); - } - } - else if (ch == KEY_HOME) { - if (state.scrollable) { - state.scrollable->ScrollToTop(); - } - } - else if (ch == KEY_END) { - if (state.scrollable) { - state.scrollable->ScrollToBottom(); - } - } - else if (state.input) { - state.input->WriteChar(ch); - } - else if (state.keyHandler) { - state.keyHandler->KeyPress(ch); - } Window::WriteToScreen(); WindowMessageQueue::Instance().Dispatch(); diff --git a/src/musikbox/app/window/CommandWindow.cpp b/src/musikbox/app/window/CommandWindow.cpp index 67c09d879..3af0121c2 100755 --- a/src/musikbox/app/window/CommandWindow.cpp +++ b/src/musikbox/app/window/CommandWindow.cpp @@ -74,14 +74,14 @@ void CommandWindow::WriteChar(int64 ch) { waddch(this->GetContent(), ch); - if (ch == 8) { /* backspace */ + if (ch == '\b') { /* backspace */ wdelch(this->GetContent()); if (bufferPosition > 0) { --bufferPosition; } } - else if (ch == 10) { /* return */ + else if (ch == '\n') { /* return */ this->buffer[bufferPosition] = 0; std::string cmd(buffer); diff --git a/src/musikbox/app/window/TrackListView.cpp b/src/musikbox/app/window/TrackListView.cpp index efa2b349a..dfee654f4 100755 --- a/src/musikbox/app/window/TrackListView.cpp +++ b/src/musikbox/app/window/TrackListView.cpp @@ -46,7 +46,7 @@ void TrackListView::OnQueryCompleted(QueryPtr query) { } void TrackListView::KeyPress(int64 ch) { - if (ch == 10) { /* return */ + if (ch == '\n') { /* return */ size_t selected = this->GetSelectedIndex(); if (this->metadata->size() > selected) { TrackPtr track = this->metadata->at(selected); @@ -55,6 +55,9 @@ void TrackListView::KeyPress(int64 ch) { this->transport->Start(fn); } } + else { + ListWindow::KeyPress(ch); + } } void TrackListView::ProcessMessage(IWindowMessage &message) { diff --git a/src/musikbox/app/window/TrackListView.h b/src/musikbox/app/window/TrackListView.h index 7ae45f543..a4d501216 100755 --- a/src/musikbox/app/window/TrackListView.h +++ b/src/musikbox/app/window/TrackListView.h @@ -15,7 +15,7 @@ using musik::core::QueryPtr; using musik::core::LibraryPtr; using musik::core::audio::Transport; -class TrackListView : public ListWindow, public IKeyHandler, public sigslot::has_slots<> { +class TrackListView : public ListWindow, public sigslot::has_slots<> { public: TrackListView(Transport& transport, LibraryPtr library, IWindow *parent = NULL); ~TrackListView(); diff --git a/src/musikbox/cursespp/ScrollableWindow.cpp b/src/musikbox/cursespp/ScrollableWindow.cpp index e4ed5252f..1bf187382 100755 --- a/src/musikbox/cursespp/ScrollableWindow.cpp +++ b/src/musikbox/cursespp/ScrollableWindow.cpp @@ -25,6 +25,17 @@ ScrollPos& ScrollableWindow::GetScrollPosition() { return this->scrollPosition; } +void ScrollableWindow::KeyPress(int64 ch) { + switch (ch) { + case KEY_NPAGE: this->PageDown(); break; + case KEY_PPAGE: this->PageUp(); break; + case KEY_DOWN: this->ScrollDown(); break; + case KEY_UP: this->ScrollUp(); break; + case KEY_HOME: this->ScrollToTop(); break; + case KEY_END: this->ScrollToBottom(); break; + } +} + void ScrollableWindow::OnAdapterChanged() { IScrollAdapter *adapter = &GetScrollAdapter(); diff --git a/src/musikbox/cursespp/ScrollableWindow.h b/src/musikbox/cursespp/ScrollableWindow.h index 5eb621013..43239870f 100755 --- a/src/musikbox/cursespp/ScrollableWindow.h +++ b/src/musikbox/cursespp/ScrollableWindow.h @@ -4,8 +4,9 @@ #include "Window.h" #include "IScrollAdapter.h" #include "IScrollable.h" +#include "IKeyHandler.h" -class ScrollableWindow : public IScrollable, public Window { +class ScrollableWindow : public IScrollable, public IKeyHandler, public Window { public: ScrollableWindow(IWindow *parent = NULL); virtual ~ScrollableWindow(); @@ -13,6 +14,8 @@ class ScrollableWindow : public IScrollable, public Window { virtual void Show(); virtual void SetSize(int width, int height); + virtual void KeyPress(int64 ch); + virtual void ScrollToTop(); virtual void ScrollToBottom(); virtual void ScrollUp(int delta = 1);