status: store status fields seperately
[ncmpcpp.git] / src / statusbar.cpp
blob4b45482ea5dba0182e4dd2646c627868030699de
1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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 2 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, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "global.h"
22 #include "settings.h"
23 #include "status.h"
24 #include "statusbar.h"
25 #include "bindings.h"
26 #include "playlist.h"
27 #include "utility/wide_string.h"
29 using Global::wFooter;
31 namespace {//
33 boost::posix_time::ptime statusbarLockTime;
34 boost::posix_time::seconds statusbarLockDelay(-1);
36 bool statusbarBlockUpdate = false;
37 bool progressbarBlockUpdate = false;
38 bool statusbarAllowUnlock = true;
42 void Progressbar::lock()
44 progressbarBlockUpdate = true;
47 void Progressbar::unlock()
49 progressbarBlockUpdate = false;
52 bool Progressbar::isUnlocked()
54 return !progressbarBlockUpdate;
57 void Progressbar::draw(unsigned int elapsed, unsigned int time)
59 unsigned pb_width = wFooter->getWidth();
60 unsigned howlong = time ? pb_width*elapsed/time : 0;
61 if (Config.progressbar_boldness)
62 *wFooter << NC::Format::Bold;
63 *wFooter << Config.progressbar_color;
64 if (Config.progressbar[2] != '\0')
66 wFooter->goToXY(0, 0);
67 for (unsigned i = 0; i < pb_width; ++i)
68 *wFooter << Config.progressbar[2];
69 wFooter->goToXY(0, 0);
71 else
72 mvwhline(wFooter->raw(), 0, 0, 0, pb_width);
73 if (time)
75 *wFooter << Config.progressbar_elapsed_color;
76 pb_width = std::min(size_t(howlong), wFooter->getWidth());
77 for (unsigned i = 0; i < pb_width; ++i)
78 *wFooter << Config.progressbar[0];
79 if (howlong < wFooter->getWidth())
80 *wFooter << Config.progressbar[1];
81 *wFooter << NC::Color::End;
83 *wFooter << NC::Color::End;
84 if (Config.progressbar_boldness)
85 *wFooter << NC::Format::NoBold;
88 void Statusbar::lock()
90 if (Config.statusbar_visibility)
91 statusbarBlockUpdate = true;
92 else
93 progressbarBlockUpdate = true;
94 statusbarAllowUnlock = false;
97 void Statusbar::unlock()
99 statusbarAllowUnlock = true;
100 if (statusbarLockDelay.is_negative())
102 if (Config.statusbar_visibility)
103 statusbarBlockUpdate = false;
104 else
105 progressbarBlockUpdate = false;
107 if (Status::State::player() == MPD::psStop)
109 switch (Config.design)
111 case Design::Classic:
112 put() << wclrtoeol;
113 break;
114 case Design::Alternative:
115 Progressbar::draw(Status::State::elapsedTime(), Status::State::totalTime());
116 break;
118 wFooter->refresh();
122 bool Statusbar::isUnlocked()
124 return !statusbarBlockUpdate;
127 void Statusbar::tryRedraw()
129 using Global::Timer;
130 if (statusbarLockDelay > boost::posix_time::seconds(0)
131 && Timer - statusbarLockTime > statusbarLockDelay)
133 statusbarLockDelay = boost::posix_time::seconds(-1);
135 if (Config.statusbar_visibility)
136 statusbarBlockUpdate = !statusbarAllowUnlock;
137 else
138 progressbarBlockUpdate = !statusbarAllowUnlock;
140 if (Status::State::player() != MPD::psStop && !statusbarBlockUpdate && !progressbarBlockUpdate)
142 switch (Config.design)
144 case Design::Classic:
145 Status::Changes::elapsedTime(false);
146 break;
147 case Design::Alternative:
148 Progressbar::draw(Status::State::elapsedTime(), Status::State::totalTime());
149 break;
151 wFooter->refresh();
156 NC::Window &Statusbar::put()
158 *wFooter << NC::XY(0, Config.statusbar_visibility ? 1 : 0) << wclrtoeol;
159 return *wFooter;
162 void Statusbar::print(int delay, const std::string &message)
164 if (statusbarAllowUnlock)
166 statusbarLockTime = Global::Timer;
167 statusbarLockDelay = boost::posix_time::seconds(delay);
168 if (Config.statusbar_visibility)
169 statusbarBlockUpdate = true;
170 else
171 progressbarBlockUpdate = true;
172 wFooter->goToXY(0, Config.statusbar_visibility);
173 *wFooter << message << wclrtoeol;
174 wFooter->refresh();
178 void Statusbar::Helpers::mpd()
180 Status::update(Mpd.noidle());
183 bool Statusbar::Helpers::getString(const char *)
185 Status::trace();
186 return true;
189 bool Statusbar::Helpers::ApplyFilterImmediately::operator()(const char *s)
191 using Global::myScreen;
192 // if input queue is not empty, we don't want to update filter since next
193 // character will be taken from queue immediately, trigering this function
194 // again and thus making it inefficient, so let's apply filter only if
195 // "real" user input arrived. however, we want to apply filter if ENTER
196 // is next in queue, so its effects will be seen.
197 if (wFooter->inputQueue().empty() || wFooter->inputQueue().front() == KEY_ENTER)
199 if (m_s != s)
201 m_s = s;
202 m_f->applyFilter(m_s);
203 myScreen->refreshWindow();
205 Status::trace();
207 return true;
210 bool Statusbar::Helpers::TryExecuteImmediateCommand::operator()(const char *s)
212 bool continue_ = true;
213 if (m_s != s)
215 m_s = s;
216 auto cmd = Bindings.findCommand(m_s);
217 if (cmd && cmd->immediate())
218 continue_ = false;
220 Status::trace();
221 return continue_;