1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 ***************************************************************************/
24 #include "statusbar.h"
27 #include "utility/wide_string.h"
29 using Global::wFooter
;
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);
72 mvwhline(wFooter
->raw(), 0, 0, 0, pb_width
);
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;
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;
105 progressbarBlockUpdate
= false;
107 if (Status::State::player() == MPD::psStop
)
109 switch (Config
.design
)
111 case Design::Classic
:
114 case Design::Alternative
:
115 Progressbar::draw(Status::State::elapsedTime(), Status::State::totalTime());
122 bool Statusbar::isUnlocked()
124 return !statusbarBlockUpdate
;
127 void Statusbar::tryRedraw()
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
;
138 progressbarBlockUpdate
= !statusbarAllowUnlock
;
140 if (!statusbarBlockUpdate
&& !progressbarBlockUpdate
)
142 switch (Config
.design
)
144 case Design::Classic
:
145 switch (Status::State::player())
153 Status::Changes::elapsedTime(false);
157 case Design::Alternative
:
158 Progressbar::draw(Status::State::elapsedTime(), Status::State::totalTime());
166 NC::Window
&Statusbar::put()
168 *wFooter
<< NC::XY(0, Config
.statusbar_visibility
? 1 : 0) << wclrtoeol
;
172 void Statusbar::print(int delay
, const std::string
&message
)
174 if (statusbarAllowUnlock
)
176 statusbarLockTime
= Global::Timer
;
177 statusbarLockDelay
= boost::posix_time::seconds(delay
);
178 if (Config
.statusbar_visibility
)
179 statusbarBlockUpdate
= true;
181 progressbarBlockUpdate
= true;
182 wFooter
->goToXY(0, Config
.statusbar_visibility
);
183 *wFooter
<< message
<< wclrtoeol
;
188 void Statusbar::Helpers::mpd()
190 Status::update(Mpd
.noidle());
193 bool Statusbar::Helpers::getString(const char *)
199 bool Statusbar::Helpers::ApplyFilterImmediately::operator()(const char *s
)
201 using Global::myScreen
;
202 // if input queue is not empty, we don't want to update filter since next
203 // character will be taken from queue immediately, trigering this function
204 // again and thus making it inefficient, so let's apply filter only if
205 // "real" user input arrived. however, we want to apply filter if ENTER
206 // is next in queue, so its effects will be seen.
207 if (wFooter
->inputQueue().empty() || wFooter
->inputQueue().front() == KEY_ENTER
)
212 m_f
->applyFilter(m_s
);
213 myScreen
->refreshWindow();
220 bool Statusbar::Helpers::TryExecuteImmediateCommand::operator()(const char *s
)
222 bool continue_
= true;
226 auto cmd
= Bindings
.findCommand(m_s
);
227 if (cmd
&& cmd
->immediate())