From a84067d1ab87220ec448b2619bc6ffbda5e93e7e Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Fri, 19 Dec 2008 23:23:50 +0100 Subject: [PATCH] put scrolling code into function and use it also in lyrics screen --- src/helpers.cpp | 37 +++++++++++++++++++++++ src/helpers.h | 2 ++ src/ncmpcpp.cpp | 67 +++++++++++++++++++----------------------- src/status_checker.cpp | 24 ++------------- 4 files changed, 72 insertions(+), 58 deletions(-) diff --git a/src/helpers.cpp b/src/helpers.cpp index e0359918..411e0145 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -877,3 +877,40 @@ const basic_buffer &ShowTagInInfoScreen(const string &tag) return ShowTag(tag); # endif } + +void Scroller(Window &w, const string &string, size_t width, size_t &pos) +{ + std::basic_string s = TO_WSTRING(string); + size_t len; +# ifdef _UTF8 + len = Window::Length(s); +# else + len = s.length(); +# endif + + if (len > width) + { +# ifdef _UTF8 + s += L" ** "; +# else + s += " ** "; +# endif + len = 0; + std::basic_string::const_iterator b = s.begin(), e = s.end(); + for (std::basic_string::const_iterator it = b+pos; it != e && len < width; it++) + { + len += wcwidth(*it); + w << *it; + } + if (++pos >= s.length()) + pos = 0; + for (; len < width; b++) + { + len += wcwidth(*b); + w << *b; + } + } + else + w << s; +} + diff --git a/src/helpers.h b/src/helpers.h index 21211c7c..c5a47c6b 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -70,5 +70,7 @@ Window &Statusbar(); const Buffer &ShowTag(const string &); const basic_buffer &ShowTagInInfoScreen(const string &); +void Scroller(Window &, const string &, size_t, size_t &); + #endif diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 05bf8ab3..4fca9a81 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -337,13 +337,16 @@ int main(int argc, char *argv[]) int found_pos = 0; int input; + size_t lyrics_scroll_begin = 0; + + Song lyrics_song; Song edited_song; Song sought_pattern; bool main_exit = 0; bool title_allowed = !Config.display_screens_numbers_on_start; - string lyrics_title; + string screen_title; string info_title; // local variables end @@ -373,42 +376,42 @@ int main(int argc, char *argv[]) messages_allowed = 1; // header stuff - const size_t max_allowed_title_length = wHeader ? wHeader->GetWidth()-volume_state.length() : 0; - if (current_screen == csBrowser && input == ERR && browsed_dir.length() > max_allowed_title_length) + const size_t max_allowed_title_length = wHeader ? wHeader->GetWidth()-volume_state.length()-screen_title.length() : 0; + if (input == ERR + && ((current_screen == csBrowser && browsed_dir.length() > max_allowed_title_length) + || current_screen == csLyrics)) redraw_header = 1; if (Config.header_visibility && redraw_header) { - string title; - switch (current_screen) { case csHelp: - title = "Help"; + screen_title = "Help"; break; case csPlaylist: - title = "Playlist "; + screen_title = "Playlist "; break; case csBrowser: - title = "Browse: "; + screen_title = "Browse: "; break; case csTinyTagEditor: case csTagEditor: - title = "Tag editor"; + screen_title = "Tag editor"; break; case csInfo: - title = info_title; + screen_title = info_title; break; case csSearcher: - title = "Search engine"; + screen_title = "Search engine"; break; case csLibrary: - title = "Media library"; + screen_title = "Media library"; break; case csLyrics: - title = lyrics_title; + screen_title = "Lyrics: "; break; case csPlaylistEditor: - title = "Playlist editor"; + screen_title = "Playlist editor"; break; default: break; @@ -417,33 +420,24 @@ int main(int argc, char *argv[]) if (title_allowed) { wHeader->Bold(1); - wHeader->WriteXY(0, 0, 1, "%s", title.c_str()); + wHeader->WriteXY(0, 0, 1, "%s", screen_title.c_str()); wHeader->Bold(0); if (current_screen == csPlaylist) + { DisplayTotalPlaylistLength(*wHeader); + } else if (current_screen == csBrowser) { - size_t max_length_without_scroll = wHeader->GetWidth()-volume_state.length()-title.length(); - my_string_t wbrowseddir = TO_WSTRING(browsed_dir); wHeader->Bold(1); - if (browsed_dir.length() > max_length_without_scroll) - { -# ifdef _UTF8 - wbrowseddir += L" ** "; -# else - wbrowseddir += " ** "; -# endif - const size_t scrollsize = max_length_without_scroll; - my_string_t part = wbrowseddir.substr(browsed_dir_scroll_begin++, scrollsize); - if (part.length() < scrollsize) - part += wbrowseddir.substr(0, scrollsize-part.length()); - wHeader->WriteXY(title.length(), 0, 0, UTF_S_FMT, part.c_str()); - if (browsed_dir_scroll_begin >= wbrowseddir.length()) - browsed_dir_scroll_begin = 0; - } - else - wHeader->WriteXY(title.length(), 0, 0, "%s", browsed_dir.c_str()); + Scroller(*wHeader, browsed_dir, max_allowed_title_length, browsed_dir_scroll_begin); + wHeader->Bold(0); + } + else if (current_screen == csLyrics) + { + + wHeader->Bold(1); + Scroller(*wHeader, lyrics_song.toString("%a - %t"), max_allowed_title_length, lyrics_scroll_begin); wHeader->Bold(0); } } @@ -456,7 +450,7 @@ int main(int argc, char *argv[]) } wHeader->SetColor(Config.volume_color); - wHeader->WriteXY(max_allowed_title_length, 0, 0, "%s", volume_state.c_str()); + wHeader->WriteXY(wHeader->GetWidth()-volume_state.length(), 0, 0, "%s", volume_state.c_str()); wHeader->SetColor(Config.header_color); wHeader->Refresh(); redraw_header = 0; @@ -3430,13 +3424,14 @@ int main(int argc, char *argv[]) } if (!s->GetArtist().empty() && !s->GetTitle().empty()) { + lyrics_scroll_begin = 0; + lyrics_song = *s; wPrev = wCurrent; prev_screen = current_screen; wCurrent = sLyrics; redraw_header = 1; wCurrent->Clear(); current_screen = csLyrics; - lyrics_title = "Lyrics: " + s->GetArtist() + " - " + s->GetTitle(); sLyrics->WriteXY(0, 0, 0, "Fetching lyrics..."); sLyrics->Refresh(); # ifdef HAVE_CURL_CURL_H diff --git a/src/status_checker.cpp b/src/status_checker.cpp index ee23019e..ab21549f 100644 --- a/src/status_checker.cpp +++ b/src/status_checker.cpp @@ -343,29 +343,9 @@ void NcmpcppStatusChanged(Connection *Mpd, StatusChanges changed, void *) tracklength += Song::ShowTime(elapsed); tracklength += "]"; } - my_string_t playing_song = TO_WSTRING(s.toString(Config.song_status_format)); - - const size_t max_length_without_scroll = wFooter->GetWidth()-player_state.length()-tracklength.length(); - - wFooter->WriteXY(0, 1, 0, "%s", player_state.c_str()); + wFooter->WriteXY(0, 1, 1, "%s", player_state.c_str()); wFooter->Bold(0); - if (playing_song.length() > max_length_without_scroll) - { -# ifdef _UTF8 - playing_song += L" ** "; -# else - playing_song += " ** "; -# endif - const size_t scrollsize = max_length_without_scroll+playing_song.length(); - my_string_t part = playing_song.substr(playing_song_scroll_begin++, scrollsize); - if (part.length() < scrollsize) - part += playing_song.substr(0, scrollsize-part.length()); - wFooter->WriteXY(player_state.length(), 1, 0, UTF_S_FMT, part.c_str()); - if (playing_song_scroll_begin >= playing_song.length()) - playing_song_scroll_begin = 0; - } - else - wFooter->WriteXY(player_state.length(), 1, 1, "%s", s.toString(Config.song_status_format).c_str()); + Scroller(*wFooter, s.toString(Config.song_status_format), wFooter->GetWidth()-player_state.length()-tracklength.length(), playing_song_scroll_begin); wFooter->Bold(1); wFooter->WriteXY(wFooter->GetWidth()-tracklength.length(), 1, 1, "%s", tracklength.c_str());