media library: fix mouse event handler
[ncmpcpp.git] / src / helpers.cpp
blob0e93a99ae223f92c2a226fb0a648bb2eee09d0bf
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 <algorithm>
23 #include "helpers.h"
24 #include "playlist.h"
25 #include "statusbar.h"
27 bool addSongToPlaylist(const MPD::Song &s, bool play, int position)
29 bool result = false;
30 if (Config.space_add_mode == SpaceAddMode::AddRemove && myPlaylist->checkForSong(s))
32 auto &w = myPlaylist->main();
33 if (play)
35 auto song = std::find(w.beginV(), w.endV(), s);
36 assert(song != w.endV());
37 Mpd.PlayID(song->getID());
38 result = true;
40 else
42 Mpd.StartCommandsList();
43 for (auto it = w.rbeginV(); it != w.rendV(); ++it)
44 if (*it == s)
45 Mpd.Delete(it->getPosition());
46 Mpd.CommitCommandsList();
47 // we return false in this case
50 else
52 int id = Mpd.AddSong(s, position);
53 if (id >= 0)
55 Statusbar::printf("Added to playlist: %s",
56 s.toString(Config.song_status_format_no_colors, Config.tags_separator)
58 if (play)
59 Mpd.PlayID(id);
60 result = true;
63 return result;
66 std::string Timestamp(time_t t)
68 char result[32];
69 # ifdef WIN32
70 result[strftime(result, 31, "%x %X", localtime(&t))] = 0;
71 # else
72 tm info;
73 result[strftime(result, 31, "%x %X", localtime_r(&t, &info))] = 0;
74 # endif // WIN32
75 return result;
78 void markSongsInPlaylist(ProxySongList pl)
80 size_t list_size = pl.size();
81 for (size_t i = 0; i < list_size; ++i)
82 if (auto s = pl.getSong(i))
83 pl.setBold(i, myPlaylist->checkForSong(*s));
86 std::wstring Scroller(const std::wstring &str, size_t &pos, size_t width)
88 std::wstring s(str);
89 if (!Config.header_text_scrolling)
90 return s;
91 std::wstring result;
92 size_t len = wideLength(s);
94 if (len > width)
96 s += L" ** ";
97 len = 0;
98 auto b = s.begin(), e = s.end();
99 for (auto it = b+pos; it < e && len < width; ++it)
101 if ((len += wcwidth(*it)) > width)
102 break;
103 result += *it;
105 if (++pos >= s.length())
106 pos = 0;
107 for (; len < width; ++b)
109 if ((len += wcwidth(*b)) > width)
110 break;
111 result += *b;
114 else
115 result = s;
116 return result;
119 void writeCyclicBuffer(const NC::WBuffer &buf, NC::Window &w, size_t &start_pos,
120 size_t width, const std::wstring &separator)
122 const auto &s = buf.str();
123 size_t len = wideLength(s);
124 if (len > width)
126 len = 0;
127 const auto &ps = buf.properties();
128 auto p = ps.begin();
130 // load attributes from before starting pos
131 for (; p != ps.end() && p->position() < start_pos; ++p)
132 w << *p;
134 auto write_buffer = [&](size_t start) {
135 for (size_t i = start; i < s.length() && len < width; ++i)
137 for (; p != ps.end() && p->position() == i; ++p)
138 w << *p;
139 len += wcwidth(s[i]);
140 if (len > width)
141 break;
142 w << s[i];
144 for (; p != ps.end(); ++p)
145 w << *p;
146 p = ps.begin();
149 write_buffer(start_pos);
150 size_t i = 0;
151 if (start_pos > s.length())
152 i = start_pos - s.length();
153 for (; i < separator.length() && len < width; ++i)
155 len += wcwidth(separator[i]);
156 if (len > width)
157 break;
158 w << separator[i];
160 write_buffer(0);
162 ++start_pos;
163 if (start_pos >= s.length() + separator.length())
164 start_pos = 0;
166 else
167 w << buf;