discard column colors if item is selected (optional)
[ncmpcpp.git] / src / playlist_editor.cpp
blob91f4ed95b3d3ee09bc72fa7eef6ebb1a3127d17a
1 /***************************************************************************
2 * Copyright (C) 2008-2009 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 "charset.h"
24 #include "display.h"
25 #include "global.h"
26 #include "helpers.h"
27 #include "playlist.h"
28 #include "playlist_editor.h"
29 #include "mpdpp.h"
30 #include "status.h"
31 #include "tag_editor.h"
33 using Global::MainHeight;
34 using Global::MainStartY;
36 PlaylistEditor *myPlaylistEditor = new PlaylistEditor;
38 size_t PlaylistEditor::LeftColumnWidth;
39 size_t PlaylistEditor::RightColumnStartX;
40 size_t PlaylistEditor::RightColumnWidth;
42 void PlaylistEditor::Init()
44 LeftColumnWidth = COLS/3-1;
45 RightColumnStartX = LeftColumnWidth+1;
46 RightColumnWidth = COLS-LeftColumnWidth-1;
48 Playlists = new Menu<std::string>(0, MainStartY, LeftColumnWidth, MainHeight, "Playlists", Config.main_color, brNone);
49 Playlists->HighlightColor(Config.active_column_color);
50 Playlists->CyclicScrolling(Config.use_cyclic_scrolling);
51 Playlists->SetItemDisplayer(Display::Generic);
53 Content = new Menu<MPD::Song>(RightColumnStartX, MainStartY, RightColumnWidth, MainHeight, "Playlist's content", Config.main_color, brNone);
54 Content->HighlightColor(Config.main_highlight_color);
55 Content->CyclicScrolling(Config.use_cyclic_scrolling);
56 Content->SetSelectPrefix(&Config.selected_item_prefix);
57 Content->SetSelectSuffix(&Config.selected_item_suffix);
58 Content->SetItemDisplayer(Display::Songs);
59 Content->SetItemDisplayerUserData(&Config.song_list_format);
60 Content->SetGetStringFunction(Playlist::SongToString);
61 Content->SetGetStringFunctionUserData(&Config.song_list_format);
63 w = Playlists;
64 isInitialized = 1;
67 void PlaylistEditor::Resize()
69 LeftColumnWidth = COLS/3-1;
70 RightColumnStartX = LeftColumnWidth+1;
71 RightColumnWidth = COLS-LeftColumnWidth-1;
73 Playlists->Resize(LeftColumnWidth, MainHeight);
74 Content->Resize(RightColumnWidth, MainHeight);
76 Playlists->MoveTo(0, MainStartY);
77 Content->MoveTo(RightColumnStartX, MainStartY);
79 hasToBeResized = 0;
82 std::basic_string<my_char_t> PlaylistEditor::Title()
84 return U("Playlist editor");
87 void PlaylistEditor::Refresh()
89 Playlists->Display();
90 mvvline(MainStartY, RightColumnStartX-1, 0, MainHeight);
91 Content->Display();
94 void PlaylistEditor::SwitchTo()
96 using Global::myScreen;
98 if (myScreen == this)
99 return;
101 if (!isInitialized)
102 Init();
104 if (hasToBeResized)
105 Resize();
107 if (myScreen != this && myScreen->isTabbable())
108 Global::myPrevScreen = myScreen;
109 myScreen = this;
110 Global::RedrawHeader = 1;
111 Refresh();
112 UpdateSongList(Content);
115 void PlaylistEditor::Update()
117 if (Playlists->Empty())
119 Content->Clear();
120 MPD::TagList list;
121 Mpd.GetPlaylists(list);
122 sort(list.begin(), list.end(), CaseInsensitiveSorting());
123 for (MPD::TagList::iterator it = list.begin(); it != list.end(); ++it)
125 utf_to_locale(*it);
126 Playlists->AddOption(*it);
128 Playlists->Window::Clear();
129 Playlists->Refresh();
132 if (!Playlists->Empty() && Content->Empty())
134 Content->Reset();
135 MPD::SongList list;
136 Mpd.GetPlaylistContent(locale_to_utf_cpy(Playlists->Current()), list);
137 if (!list.empty())
138 Content->SetTitle("Playlist's content (" + IntoStr(list.size()) + " item" + (list.size() == 1 ? ")" : "s)"));
139 else
140 Content->SetTitle("Playlist's content");
141 bool bold = 0;
142 for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it)
144 for (size_t j = 0; j < myPlaylist->Items->Size(); ++j)
146 if ((*it)->GetHash() == myPlaylist->Items->at(j).GetHash())
148 bold = 1;
149 break;
152 Content->AddOption(**it, bold);
153 bold = 0;
155 MPD::FreeSongList(list);
156 Content->Window::Clear();
157 Content->Display();
160 if (w == Content && Content->Empty())
162 Content->HighlightColor(Config.main_highlight_color);
163 Playlists->HighlightColor(Config.active_column_color);
164 w = Playlists;
167 if (Content->Empty())
169 *Content << XY(0, 0) << "Playlist is empty.";
170 Content->Window::Refresh();
174 void PlaylistEditor::NextColumn()
176 if (w == Playlists)
178 Playlists->HighlightColor(Config.main_highlight_color);
179 w->Refresh();
180 w = Content;
181 Content->HighlightColor(Config.active_column_color);
185 void PlaylistEditor::PrevColumn()
187 if (w == Content)
189 Content->HighlightColor(Config.main_highlight_color);
190 w->Refresh();
191 w = Playlists;
192 Playlists->HighlightColor(Config.active_column_color);
196 void PlaylistEditor::AddToPlaylist(bool add_n_play)
198 MPD::SongList list;
200 if (w == Playlists && !Playlists->Empty())
202 Mpd.GetPlaylistContent(locale_to_utf_cpy(Playlists->Current()), list);
203 if (myPlaylist->Add(list, add_n_play))
204 ShowMessage("Loading playlist %s...", Playlists->Current().c_str());
206 else if (w == Content && !Content->Empty())
207 Content->Bold(Content->Choice(), myPlaylist->Add(Content->Current(), Content->isBold(), add_n_play));
209 FreeSongList(list);
210 if (!add_n_play)
211 w->Scroll(wDown);
214 void PlaylistEditor::SpacePressed()
216 if (Config.space_selects && w == Content)
218 Content->Select(Content->Choice(), !Content->isSelected());
219 w->Scroll(wDown);
221 else
222 AddToPlaylist(0);
225 void PlaylistEditor::MouseButtonPressed(MEVENT me)
227 if (!Playlists->Empty() && Playlists->hasCoords(me.x, me.y))
229 if (w != Playlists)
230 PrevColumn();
231 if (size_t(me.y) < Playlists->Size() && (me.bstate & (BUTTON1_PRESSED | BUTTON3_PRESSED)))
233 Playlists->Goto(me.y);
234 if (me.bstate & BUTTON3_PRESSED)
236 size_t pos = Playlists->Choice();
237 SpacePressed();
238 if (pos < Playlists->Size()-1)
239 Playlists->Scroll(wUp);
242 else
243 Screen<Window>::MouseButtonPressed(me);
244 Content->Clear();
246 else if (!Content->Empty() && Content->hasCoords(me.x, me.y))
248 if (w != Content)
249 NextColumn();
250 if (size_t(me.y) < Content->Size() && (me.bstate & (BUTTON1_PRESSED | BUTTON3_PRESSED)))
252 Content->Goto(me.y);
253 if (me.bstate & BUTTON1_PRESSED)
255 size_t pos = Content->Choice();
256 SpacePressed();
257 if (pos < Content->Size()-1)
258 Content->Scroll(wUp);
260 else
261 EnterPressed();
263 else
264 Screen<Window>::MouseButtonPressed(me);
268 MPD::Song *PlaylistEditor::CurrentSong()
270 return w == Content && !Content->Empty() ? &Content->Current() : 0;
273 void PlaylistEditor::GetSelectedSongs(MPD::SongList &v)
275 std::vector<size_t> selected;
276 Content->GetSelected(selected);
277 if (selected.empty())
278 selected.push_back(Content->Choice());
279 for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it)
281 v.push_back(new MPD::Song(Content->at(*it)));
285 void PlaylistEditor::ApplyFilter(const std::string &s)
287 GetList()->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
290 List *PlaylistEditor::GetList()
292 if (w == Playlists)
293 return Playlists;
294 else if (w == Content)
295 return Content;
296 else // silence compiler
297 return 0;