another code clean-ups

This commit is contained in:
unK
2008-10-07 23:04:26 +02:00
parent fc00baa300
commit 48ba7cbcf2
13 changed files with 111 additions and 176 deletions

View File

@@ -19,6 +19,8 @@
***************************************************************************/
#include <sys/stat.h>
#include <fstream>
#include "helpers.h"
#include "lyrics.h"
#include "settings.h"

View File

@@ -23,7 +23,6 @@
#include "ncmpcpp.h"
#include <fstream>
#ifdef HAVE_CURL_CURL_H
# include <pthread.h>
# include "curl/curl.h"

View File

@@ -48,7 +48,7 @@ class Menu : public Window
typedef string (*ItemDisplayer) (const T &, void *, const Menu<T> *);
public:
Menu(int startx, int starty, int width, int height, string title, Color color, Border border) : Window(startx, starty, width, height, title, color, border), itsItemDisplayer(0), itsItemDisplayerUserdata(0), itsSelectedPrefix("[.r]"), itsSelectedSuffix("[/r]"), itsStaticsNumber(0), itsBeginning(0), itsHighlight(0), itsHighlightColor(itsBaseColor), itsHighlightEnabled(1) { }
Menu(int, int, int, int, const string &, Color, Border);
Menu(const Menu &);
virtual ~Menu();
@@ -126,6 +126,19 @@ class Menu : public Window
bool itsHighlightEnabled;
};
template <class T>
Menu<T>::Menu(int startx, int starty, int width, int height, const string &title, Color color, Border border) :
Window(startx, starty, width, height, title, color, border),
itsItemDisplayer(0),
itsItemDisplayerUserdata(0),
itsSelectedPrefix("[.r]"),
itsSelectedSuffix("[/r]"),
itsStaticsNumber(0),
itsBeginning(0),
itsHighlight(0),
itsHighlightColor(itsBaseColor),
itsHighlightEnabled(1) { }
template <class T>
Menu<T>::Menu(const Menu &m) : Window(m)
{
@@ -355,7 +368,13 @@ void Menu<T>::Refresh(bool redraw_whole_window)
int ch = itsOptions[*it]->have_separator ? 0 : 32;
mvwhline(itsWindow,line, 0, ch, itsWidth);
string option = DisplayOption(itsOptions[*it]->item);
string option;
if (itsOptions[*it]->selected)
option += itsSelectedPrefix;
option += DisplayOption(itsOptions[*it]->item);
if (itsOptions[*it]->selected)
option += itsSelectedSuffix;
int strlength = itsOptions[*it]->location != lLeft && BBEnabled ? Window::RealLength(option) : option.length();
@@ -386,10 +405,7 @@ void Menu<T>::Refresh(bool redraw_whole_window)
}
}
if (itsOptions[*it]->selected)
WriteXY(x, line, itsWidth, TO_WSTRING(itsSelectedPrefix + option + itsSelectedSuffix), 0);
else
WriteXY(x, line, itsWidth, TO_WSTRING(option), 0);
WriteXY(x, line, itsWidth, TO_WSTRING(option), 0);
if (!ch && (itsOptions[*it]->location == lCenter || itsOptions[*it]->location == lLeft))
{
@@ -415,7 +431,9 @@ void Menu<T>::Refresh(bool redraw_whole_window)
template <class T>
void Menu<T>::Go(Where where)
{
if (Empty()) return;
if (Empty())
return;
int MaxHighlight = itsOptions.size()-1;
int MaxBeginning = itsOptions.size() < itsHeight ? 0 : itsOptions.size()-itsHeight;
int MaxCurrentHighlight = itsBeginning+itsHeight-1;
@@ -431,7 +449,9 @@ void Menu<T>::Go(Where where)
wscrl(itsWindow, -1);
}
if (itsHighlight == 0)
{
break;
}
else
{
NeedsRedraw.push_back(itsHighlight--);
@@ -439,10 +459,7 @@ void Menu<T>::Go(Where where)
}
if (is_static())
{
if (itsHighlight == 0)
Go(wDown);
else
Go(wUp);
itsHighlight == 0 ? Go(wDown) : Go(wUp);
}
break;
}
@@ -454,7 +471,9 @@ void Menu<T>::Go(Where where)
wscrl(itsWindow, 1);
}
if (itsHighlight == MaxHighlight)
{
break;
}
else
{
NeedsRedraw.push_back(itsHighlight++);
@@ -462,10 +481,7 @@ void Menu<T>::Go(Where where)
}
if (is_static())
{
if (itsHighlight == MaxHighlight)
Go(wUp);
else
Go(wDown);
itsHighlight == MaxHighlight ? Go(wUp) : Go(wDown);
}
break;
}
@@ -476,14 +492,12 @@ void Menu<T>::Go(Where where)
if (itsBeginning < 0)
{
itsBeginning = 0;
if (itsHighlight < 0) itsHighlight = 0;
if (itsHighlight < 0)
itsHighlight = 0;
}
if (is_static())
{
if (itsHighlight == 0)
Go(wDown);
else
Go(wUp);
itsHighlight == 0 ? Go(wDown) : Go(wUp);
}
redraw_screen();
break;
@@ -495,14 +509,12 @@ void Menu<T>::Go(Where where)
if (itsBeginning > MaxBeginning)
{
itsBeginning = MaxBeginning;
if (itsHighlight > MaxHighlight) itsHighlight = MaxHighlight;
if (itsHighlight > MaxHighlight)
itsHighlight = MaxHighlight;
}
if (is_static())
{
if (itsHighlight == MaxHighlight)
Go(wUp);
else
Go(wDown);
itsHighlight == MaxHighlight ? Go(wUp) : Go(wDown);
}
redraw_screen();
break;
@@ -513,10 +525,7 @@ void Menu<T>::Go(Where where)
itsBeginning = 0;
if (is_static())
{
if (itsHighlight == 0)
Go(wDown);
else
Go(wUp);
itsHighlight == 0 ? Go(wDown) : Go(wUp);
}
redraw_screen();
break;
@@ -527,10 +536,7 @@ void Menu<T>::Go(Where where)
itsBeginning = MaxBeginning;
if (is_static())
{
if (itsHighlight == MaxHighlight)
Go(wUp);
else
Go(wDown);
itsHighlight == MaxHighlight ? Go(wUp) : Go(wDown);
}
redraw_screen();
break;
@@ -698,7 +704,7 @@ bool Menu<T>::IsStatic(int option) const
template <class T>
Window * Menu<T>::EmptyClone() const
{
return new Menu(GetStartX(),GetStartY(),GetWidth(),GetHeight(),itsTitle,itsBaseColor,itsBorder);
return new Menu(GetStartX(), GetStartY(), GetWidth(), GetHeight(), itsTitle, itsBaseColor, itsBorder);
}
template <class T>

View File

@@ -21,8 +21,6 @@
#include <algorithm>
#include "misc.h"
using std::stringstream;
int Abs(int num)
{
return (num < 0 ? -num : num);
@@ -38,17 +36,17 @@ int StrToInt(string str)
return atoi(str.c_str());
}
string IntoStr(int liczba)
string IntoStr(int l)
{
stringstream ss;
ss << liczba;
std::stringstream ss;
ss << l;
return ss.str();
}
string IntoStr(double liczba, int precision)
string IntoStr(double l, int precision)
{
stringstream ss;
ss << std::fixed << std::setprecision(precision) << liczba;
std::stringstream ss;
ss << std::fixed << std::setprecision(precision) << l;
return ss.str();
}

View File

@@ -101,7 +101,6 @@ MPDConnection *Mpd;
time_t timer;
int now_playing = -1;
int playing_song_scroll_begin = 0;
int browsed_dir_scroll_begin = 0;
int stats_scroll_begin = 0;
@@ -182,7 +181,7 @@ int main(int argc, char *argv[])
if (!ConnectToMPD())
return -1;
setlocale(LC_ALL,"");
setlocale(LC_ALL, "");
initscr();
noecho();
cbreak();
@@ -476,7 +475,6 @@ int main(int argc, char *argv[])
Mpd->AddSearch(Config.media_lib_primary_tag, mLibArtists->GetOption());
Mpd->StartFieldSearch(MPD_TAG_ITEM_ALBUM);
Mpd->CommitSearch(list);
//sort(list.begin(), list.end());
}
for (TagList::const_iterator it = list.begin(); it != list.end(); it++)
{

View File

@@ -20,6 +20,18 @@
#include "scrollpad.h"
Scrollpad::Scrollpad(int startx, int starty, int width, int height, const string &title, Color color, Border border) :
Window(startx, starty, width, height, title, color, border),
itsBeginning(0),
itsRealHeight(1),
itsXPos(0)
{
delwin(itsWindow);
itsWindow = newpad(itsHeight, itsWidth);
SetColor(itsColor);
keypad(itsWindow, 1);
}
Scrollpad::Scrollpad(const Scrollpad &s) : Window(s)
{
itsContent = s.itsContent;
@@ -126,12 +138,13 @@ void Scrollpad::Recreate()
itsWindow = newpad(itsRealHeight, itsWidth);
SetTimeout(itsWindowTimeout);
SetColor(itsBaseColor, itsBgColor);
keypad(itsWindow, 1);
Write(itsContent.c_str());
}
void Scrollpad::Refresh(bool)
{
prefresh(itsWindow,itsBeginning,0,itsStartY,itsStartX,itsStartY+itsHeight-1,itsStartX+itsWidth);
prefresh(itsWindow, itsBeginning, 0, itsStartY, itsStartX, itsStartY+itsHeight-1, itsStartX+itsWidth);
}
void Scrollpad::Resize(int width, int height)
@@ -140,8 +153,8 @@ void Scrollpad::Resize(int width, int height)
{
delwin(itsWinBorder);
itsWinBorder = newpad(height, width);
wattron(itsWinBorder,COLOR_PAIR(itsBorder));
box(itsWinBorder,0,0);
wattron(itsWinBorder, COLOR_PAIR(itsBorder));
box(itsWinBorder, 0, 0);
width -= 2;
height -= 2;
}
@@ -215,14 +228,15 @@ void Scrollpad::Clear(bool clear_screen)
wclear(itsWindow);
delwin(itsWindow);
itsWindow = newpad(itsHeight, itsWidth);
SetColor(itsColor, itsBgColor);
SetTimeout(itsWindowTimeout);
SetColor(itsColor, itsBgColor);
keypad(itsWindow, 1);
if (clear_screen)
Window::Clear();
}
Window * Scrollpad::EmptyClone() const
{
return new Scrollpad(GetStartX(),GetStartY(),GetWidth(),GetHeight(),itsTitle,itsBaseColor,itsBorder);
return new Scrollpad(GetStartX(), GetStartY(), GetWidth(), GetHeight(), itsTitle, itsBaseColor, itsBorder);
}

View File

@@ -26,7 +26,7 @@
class Scrollpad: public Window
{
public:
Scrollpad(int startx, int starty, int width, int height, string title, Color color, Border border) : Window(startx, starty, width, height, title, color, border), itsBeginning(0), itsRealHeight(1), itsXPos(0) { delwin(itsWindow); itsWindow = newpad(itsHeight,itsWidth); }
Scrollpad(int, int, int, int, const string &, Color, Border);
Scrollpad(const Scrollpad &);
virtual ~Scrollpad() {}
virtual void Add(string);
@@ -36,6 +36,7 @@ class Scrollpad: public Window
virtual void Clear(bool clear_screen = 1);
virtual Window * Clone() const { return new Scrollpad(*this); }
virtual Window * EmptyClone() const;
protected:
virtual void Recreate();
string itsContent;

View File

@@ -19,6 +19,8 @@
***************************************************************************/
#include <sys/stat.h>
#include <fstream>
#include "settings.h"
const string config_file = config_dir + "config";

View File

@@ -21,8 +21,6 @@
#ifndef HAVE_SETTINGS_H
#define HAVE_SETTINGS_H
#include <fstream>
#include "libmpdclient.h"
#include "ncmpcpp.h"

View File

@@ -40,26 +40,13 @@ extern Menu<string> *mEditorDirs;
extern Window *wHeader;
extern Window *wFooter;
extern SongList vSearched;
extern time_t timer;
extern int now_playing;
extern int playing_song_scroll_begin;
extern int lock_statusbar_delay;
extern string browsed_dir;
extern string player_state;
extern string volume_state;
extern string switch_state;
extern string mpd_repeat;
extern string mpd_random;
extern string mpd_crossfade;
extern string mpd_db_updating;
extern NcmpcppScreen current_screen;
extern NcmpcppScreen prev_screen;
@@ -80,6 +67,7 @@ bool repeat_one_allowed = 0;
long long playlist_old_id = -1;
int old_playing;
int playing_song_scroll_begin = 0;
time_t time_of_statusbar_lock;
time_t now;

View File

@@ -22,6 +22,8 @@
#ifdef HAVE_TAGLIB_H
#include <fstream>
#include "id3v2tag.h"
#include "textidentificationframe.h"
#include "mpegfile.h"

View File

@@ -20,7 +20,23 @@
#include "window.h"
Window::Window(int startx, int starty, int width, int height, const string &title, Color color, Border border) : itsWindow(0), itsWinBorder(0), itsGetStringHelper(0), itsStartX(startx), itsStartY(starty), itsWidth(width), itsHeight(height), itsWindowTimeout(-1), BBEnabled(1), AutoRefreshEnabled(1), itsTitle(title), itsColor(color), itsBaseColor(color), itsBgColor(clDefault), itsBaseBgColor(clDefault), itsBorder(border)
Window::Window(int startx, int starty, int width, int height, const string &title, Color color, Border border) :
itsWindow(0),
itsWinBorder(0),
itsGetStringHelper(0),
itsStartX(startx),
itsStartY(starty),
itsWidth(width),
itsHeight(height),
itsWindowTimeout(-1),
BBEnabled(1),
AutoRefreshEnabled(1),
itsTitle(title),
itsColor(color),
itsBaseColor(color),
itsBgColor(clDefault),
itsBaseBgColor(clDefault),
itsBorder(border)
{
if (itsStartX < 0) itsStartX = 0;
if (itsStartY < 0) itsStartY = 0;
@@ -28,8 +44,8 @@ Window::Window(int startx, int starty, int width, int height, const string &titl
if (itsBorder != brNone)
{
itsWinBorder = newpad(itsHeight, itsWidth);
wattron(itsWinBorder,COLOR_PAIR(itsBorder));
box(itsWinBorder,0,0);
wattron(itsWinBorder, COLOR_PAIR(itsBorder));
box(itsWinBorder, 0, 0);
itsStartX++;
itsStartY++;
itsWidth -= 2;
@@ -47,6 +63,7 @@ Window::Window(int startx, int starty, int width, int height, const string &titl
itsWindow = newwin(0, 0, 0, 0);
SetColor(itsColor);
keypad(itsWindow, 1);
}
Window::Window(const Window &w)
@@ -78,9 +95,9 @@ Window::~Window()
void Window::SetColor(Color col, Color background)
{
if (col != clDefault)
wattron(itsWindow,COLOR_PAIR(background*8+col));
wattron(itsWindow, COLOR_PAIR(background*8+col));
else
wattroff(itsWindow,COLOR_PAIR(itsColor));
wattroff(itsWindow, COLOR_PAIR(itsColor));
itsColor = col;
itsBgColor = background;
}
@@ -104,8 +121,8 @@ void Window::SetBorder(Border border)
}
else if (border != brNone && itsBorder == brNone)
{
itsWinBorder = newpad(itsHeight,itsWidth);
wattron(itsWinBorder,COLOR_PAIR(border));
itsWinBorder = newpad(itsHeight, itsWidth);
wattron(itsWinBorder, COLOR_PAIR(border));
box(itsWinBorder,0,0);
itsStartX++;
itsStartY++;
@@ -116,7 +133,7 @@ void Window::SetBorder(Border border)
else
{
wattron(itsWinBorder,COLOR_PAIR(border));
box(itsWinBorder,0,0);
box(itsWinBorder, 0, 0);
}
itsBorder = border;
}
@@ -148,6 +165,7 @@ void Window::Recreate()
itsWindow = newwin(itsHeight, itsWidth, itsStartY, itsStartX);
SetTimeout(itsWindowTimeout);
SetColor(itsColor, itsBgColor);
keypad(itsWindow, 1);
}
void Window::MoveTo(int newx, int newy)
@@ -175,8 +193,8 @@ void Window::Resize(int width, int height)
{
delwin(itsWinBorder);
itsWinBorder = newpad(height, width);
wattron(itsWinBorder,COLOR_PAIR(itsBorder));
box(itsWinBorder,0,0);
wattron(itsWinBorder, COLOR_PAIR(itsBorder));
box(itsWinBorder, 0, 0);
width -= 2;
height -= 2;
}
@@ -195,7 +213,7 @@ void Window::ShowBorder() const
if (itsBorder != brNone)
{
refresh();
prefresh(itsWinBorder,0,0,GetStartY(),GetStartX(),itsStartY+itsHeight,itsStartX+itsWidth);
prefresh(itsWinBorder, 0, 0, GetStartY(), GetStartX(), itsStartY+itsHeight, itsStartX+itsWidth);
}
if (!itsTitle.empty())
{
@@ -264,7 +282,7 @@ void Window::SetTimeout(int timeout)
void Window::ReadKey(int &input) const
{
keypad(itsWindow, TRUE); input = wgetch(itsWindow); keypad(itsWindow, FALSE);
input = wgetch(itsWindow);
}
void Window::ReadKey() const
@@ -288,8 +306,7 @@ void Window::Write(int limit, const string &str, bool clrtoeol)
tmp += *it;
limit--;
}
if (collect)
else
{
if (*it != '[')
{
@@ -361,8 +378,7 @@ void Window::Write(int limit, const wstring &str, bool clrtoeol)
tmp += *it;
limit -= wcwidth(*it);
}
if (collect)
else
{
if (*it != '[')
{
@@ -419,21 +435,21 @@ void Window::Write(int limit, const wstring &str, bool clrtoeol)
void Window::WriteXY(int x, int y, int limit, const wstring &str, bool cleartoeol)
{
wmove(itsWindow,y,x);
wmove(itsWindow, y, x);
Write(limit, str, cleartoeol);
}
#endif
void Window::WriteXY(int x, int y, int limit, const string &str, bool cleartoeol)
{
wmove(itsWindow,y,x);
wmove(itsWindow, y, x);
Write(limit, str, cleartoeol);
}
string Window::GetString(const string &base, unsigned int length, int width) const
{
int input, beginning, maxbeginning, minx, x, y, maxx;
getyx(itsWindow,y,x);
getyx(itsWindow, y, x);
minx = maxx = x;
width--;
@@ -443,7 +459,6 @@ string Window::GetString(const string &base, unsigned int length, int width) con
return "";
curs_set(1);
keypad(itsWindow, 1);
wstring tmp = ToWString(base);
string tmp_in;
@@ -470,7 +485,7 @@ string Window::GetString(const string &base, unsigned int length, int width) con
if (itsGetStringHelper)
itsGetStringHelper();
wmove(itsWindow,y,x);
wmove(itsWindow, y, x);
input = wgetch(itsWindow);
switch (input)
@@ -554,7 +569,6 @@ string Window::GetString(const string &base, unsigned int length, int width) con
}
}
while (input != 10);
keypad(itsWindow, 0);
curs_set(0);
return ToString(tmp);
}
@@ -814,85 +828,3 @@ size_t Window::Length(const wstring &ws)
return length;
}
/*int CountBBCodes(const string &str)
{
if (str.empty())
return 0;
bool collect = false;
int length = 0;
string color;
for (string::const_iterator it = str.begin(); it != str.end(); it++)
{
if (*it != '[' && !collect);
else
collect = 1;
if (collect)
{
if (*it != '[')
{
color += *it;
length++;
}
else
{
length -= color.length();
length++;
color = *it;
}
}
if (*it == ']' || it+1 == str.end())
collect = 0;
if (!collect)
{
if (!IsValidColor(color))
length -= color.length();
color.clear();
}
}
return length;
}
int CountBBCodes(const wstring &str)
{
if (str.empty())
return 0;
bool collect = false;
int length = 0;
wstring color;
for (wstring::const_iterator it = str.begin(); it != str.end(); it++)
{
if (*it != '[' && !collect);
else
collect = 1;
if (collect)
{
if (*it != '[')
{
color += *it;
length++;
}
else
{
length -= color.length();
length++;
color = *it;
}
}
if (*it == ']' || it+1 == str.end())
collect = 0;
if (!collect)
{
if (!IsValidColor(ToString(color)))
length -= color.length();
color.clear();
}
}
return length;
}*/

View File

@@ -57,14 +57,9 @@ typedef void (*GetStringHelper)();
typedef std::pair<Color, Color> ColorPair;
typedef std::pair<int, int> Coordinates;
//char * ToString(const wchar_t *);
//wchar_t * ToWString(const char *);
string ToString(const wstring &);
wstring ToWString(const string &);
//int CountBBCodes(const string &);
//int CountBBCodes(const wstring &);
class Window
{
public: