1 /*
2 Vimpc
3 Copyright (C) 2010 - 2011 Nathan Sweetman
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 lyricswindow.cpp - class representing a scrollable ncurses window
19 */
20
21 #include "lyricswindow.hpp"
22
23 #include "events.hpp"
24 #include "lyricsloader.hpp"
25 #include "mpdclient.hpp"
26 #include "screen.hpp"
27 #include "vimpc.hpp"
28 #include "buffer/playlist.hpp"
29 #include "mode/search.hpp"
30
31 #include <sstream>
32
33 using namespace Ui;
34 using namespace Main;
35
LyricsWindow(std::string const & URI,Main::Settings const & settings,Ui::Screen & screen,Mpc::Client & client,Mpc::ClientState & clientState,Ui::Search const & search,std::string name)36 LyricsWindow::LyricsWindow(std::string const & URI, Main::Settings const & settings, Ui::Screen & screen, Mpc::Client & client, Mpc::ClientState & clientState, Ui::Search const & search, std::string name) :
37 SelectWindow (settings, screen, name),
38 m_URI (URI),
39 settings_ (settings),
40 search_ (search),
41 lyrics_ ()
42 {
43 Vimpc::EventHandler(Event::LyricsLoaded, [this] (EventData const & Data) { Redraw(); });
44
45 Vimpc::EventHandler(Event::LyricsPercent, [this] (EventData const & Data)
46 {
47 uint32_t end = lyrics_.Size();
48 this->ScrollTo((end*(Data.value+10))/100);
49 });
50
51 LoadLyrics();
52 Redraw();
53 }
54
~LyricsWindow()55 LyricsWindow::~LyricsWindow()
56 {
57 }
58
Print(uint32_t line) const59 void LyricsWindow::Print(uint32_t line) const
60 {
61 WINDOW * window = N_WINDOW();
62
63 std::string const BlankLine(Columns(), ' ');
64 mvwprintw(window, line, 0, BlankLine.c_str());
65 wmove(window, line, 0);
66
67 if ((FirstLine() == 0) && (line == 0))
68 {
69 wattron(window, A_BOLD);
70 }
71
72 if ((FirstLine() + line) < lyrics_.Size())
73 {
74 std::string currentLine = lyrics_.Get(FirstLine() + line);
75
76 if ((search_.LastSearchString() != "") && (settings_.Get(Setting::HighlightSearch) == true) &&
77 (search_.HighlightSearch() == true))
78 {
79 Regex::RE const expression(".*" + search_.LastSearchString() + ".*", search_.LastSearchOptions());
80
81 if (expression.CompleteMatch(currentLine))
82 {
83 wattron(window, COLOR_PAIR(settings_.colours.SongMatch));
84 }
85 }
86
87 mvwaddstr(window, line, 0, currentLine.c_str());
88
89 if ((search_.LastSearchString() != "") && (settings_.Get(Setting::HighlightSearch) == true) &&
90 (search_.HighlightSearch() == true))
91 {
92 Regex::RE const expression(".*" + search_.LastSearchString() + ".*", search_.LastSearchOptions());
93
94 if (expression.CompleteMatch(currentLine))
95 {
96 wattroff(window, COLOR_PAIR(settings_.colours.SongMatch));
97 }
98 }
99 }
100
101 if ((FirstLine() == 0) && (line == 0))
102 {
103 wattroff(window, A_BOLD);
104 }
105 }
106
LoadLyrics()107 void LyricsWindow::LoadLyrics()
108 {
109 Mpc::Song * song = Main::Library().Song(m_URI);
110 Main::LyricsLoader::Instance().Load(song);
111 LyricsLoaded();
112 }
113
LyricsLoaded()114 void LyricsWindow::LyricsLoaded()
115 {
116 Clear();
117
118 if (Main::LyricsLoader::Instance().Loaded())
119 {
120 lyrics_.Add(Main::LyricsLoader::Instance().Artist() + " - " + Main::LyricsLoader::Instance().Title());
121 lyrics_.Add("");
122
123 if (Main::LyricsBuffer().Size() > 0)
124 {
125 for (int i = 0; i < Main::LyricsBuffer().Size(); ++i)
126 {
127 lyrics_.Add(Main::LyricsBuffer().Get(i));
128 }
129 }
130 else
131 {
132 lyrics_.Add("No lyrics were found.");
133 }
134 }
135 else
136 {
137 lyrics_.Add("Loading...");
138 }
139 }
140
Edit()141 void LyricsWindow::Edit()
142 {
143 int const LyricsWindowId = screen_.GetActiveWindow();
144 screen_.SetVisible(LyricsWindowId, false);
145 }
146
Redraw()147 void LyricsWindow::Redraw()
148 {
149 ScrollTo(0);
150 LyricsLoaded();
151 }
152
Clear()153 void LyricsWindow::Clear()
154 {
155 lyrics_.Clear();
156 }
157
Scroll(int32_t scrollCount)158 void LyricsWindow::Scroll(int32_t scrollCount)
159 {
160 currentLine_ += scrollCount;
161 LimitCurrentSelection();
162 ScrollWindow::Scroll(scrollCount);
163 }
164
ScrollTo(uint32_t scrollLine)165 void LyricsWindow::ScrollTo(uint32_t scrollLine)
166 {
167 int64_t oldSelection = currentLine_;
168 currentLine_ = (static_cast<int64_t>(scrollLine));
169 LimitCurrentSelection();
170
171 ScrollWindow::ScrollTo(scrollLine);
172 }
173
174 /* vim: set sw=3 ts=3: */
175