center cursor directly in Menu class
[ncmpcpp.git] / src / playlist_editor.cpp
blob2554cb607ad5a96c770279bf41747e3de05430eb
1 /***************************************************************************
2 * Copyright (C) 2008-2010 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->CenteredCursor(Config.centered_cursor);
52 Playlists->SetItemDisplayer(Display::Generic);
54 Content = new Menu<MPD::Song>(RightColumnStartX, MainStartY, RightColumnWidth, MainHeight, "Playlist's content", Config.main_color, brNone);
55 Content->HighlightColor(Config.main_highlight_color);
56 Content->CyclicScrolling(Config.use_cyclic_scrolling);
57 Content->CenteredCursor(Config.centered_cursor);
58 Content->SetSelectPrefix(&Config.selected_item_prefix);
59 Content->SetSelectSuffix(&Config.selected_item_suffix);
60 Content->SetItemDisplayer(Display::Songs);
61 Content->SetItemDisplayerUserData(&Config.song_list_format);
62 Content->SetGetStringFunction(Playlist::SongToString);
63 Content->SetGetStringFunctionUserData(&Config.song_list_format);
65 w = Playlists;
66 isInitialized = 1;
69 void PlaylistEditor::Resize()
71 LeftColumnWidth = COLS/3-1;
72 RightColumnStartX = LeftColumnWidth+1;
73 RightColumnWidth = COLS-LeftColumnWidth-1;
75 Playlists->Resize(LeftColumnWidth, MainHeight);
76 Content->Resize(RightColumnWidth, MainHeight);
78 Playlists->MoveTo(0, MainStartY);
79 Content->MoveTo(RightColumnStartX, MainStartY);
81 hasToBeResized = 0;
84 std::basic_string<my_char_t> PlaylistEditor::Title()
86 return U("Playlist editor");
89 void PlaylistEditor::Refresh()
91 Playlists->Display();
92 mvvline(MainStartY, RightColumnStartX-1, 0, MainHeight);
93 Content->Display();
96 void PlaylistEditor::SwitchTo()
98 using Global::myScreen;
100 if (myScreen == this)
101 return;
103 if (!isInitialized)
104 Init();
106 if (hasToBeResized)
107 Resize();
109 if (myScreen != this && myScreen->isTabbable())
110 Global::myPrevScreen = myScreen;
111 myScreen = this;
112 Global::RedrawHeader = 1;
113 Refresh();
114 UpdateSongList(Content);
117 void PlaylistEditor::Update()
119 if (Playlists->Empty())
121 Content->Clear();
122 MPD::TagList list;
123 Mpd.GetPlaylists(list);
124 sort(list.begin(), list.end(), CaseInsensitiveSorting());
125 for (MPD::TagList::iterator it = list.begin(); it != list.end(); ++it)
127 utf_to_locale(*it);
128 Playlists->AddOption(*it);
130 Playlists->Window::Clear();
131 Playlists->Refresh();
134 if (!Playlists->Empty() && Content->Empty())
136 Content->Reset();
137 MPD::SongList list;
138 Mpd.GetPlaylistContent(locale_to_utf_cpy(Playlists->Current()), list);
139 if (!list.empty())
140 Content->SetTitle("Playlist's content (" + IntoStr(list.size()) + " item" + (list.size() == 1 ? ")" : "s)"));
141 else
142 Content->SetTitle("Playlist's content");
143 bool bold = 0;
144 for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it)
146 for (size_t j = 0; j < myPlaylist->Items->Size(); ++j)
148 if ((*it)->GetHash() == myPlaylist->Items->at(j).GetHash())
150 bold = 1;
151 break;
154 Content->AddOption(**it, bold);
155 bold = 0;
157 MPD::FreeSongList(list);
158 Content->Window::Clear();
159 Content->Display();
162 if (w == Content && Content->Empty())
164 Content->HighlightColor(Config.main_highlight_color);
165 Playlists->HighlightColor(Config.active_column_color);
166 w = Playlists;
169 if (Content->Empty())
171 *Content << XY(0, 0) << "Playlist is empty.";
172 Content->Window::Refresh();
176 void PlaylistEditor::NextColumn()
178 if (w == Playlists)
180 Playlists->HighlightColor(Config.main_highlight_color);
181 w->Refresh();
182 w = Content;
183 Content->HighlightColor(Config.active_column_color);
187 void PlaylistEditor::PrevColumn()
189 if (w == Content)
191 Content->HighlightColor(Config.main_highlight_color);
192 w->Refresh();
193 w = Playlists;
194 Playlists->HighlightColor(Config.active_column_color);
198 void PlaylistEditor::AddToPlaylist(bool add_n_play)
200 MPD::SongList list;
202 if (w == Playlists && !Playlists->Empty())
204 Mpd.GetPlaylistContent(locale_to_utf_cpy(Playlists->Current()), list);
205 if (myPlaylist->Add(list, add_n_play))
206 ShowMessage("Loading playlist %s...", Playlists->Current().c_str());
208 else if (w == Content && !Content->Empty())
209 Content->Bold(Content->Choice(), myPlaylist->Add(Content->Current(), Content->isBold(), add_n_play));
211 FreeSongList(list);
212 if (!add_n_play)
213 w->Scroll(wDown);
216 void PlaylistEditor::SpacePressed()
218 if (Config.space_selects && w == Content)
220 Content->Select(Content->Choice(), !Content->isSelected());
221 w->Scroll(wDown);
223 else
224 AddToPlaylist(0);
227 void PlaylistEditor::MouseButtonPressed(MEVENT me)
229 if (!Playlists->Empty() && Playlists->hasCoords(me.x, me.y))
231 if (w != Playlists)
232 PrevColumn();
233 if (size_t(me.y) < Playlists->Size() && (me.bstate & (BUTTON1_PRESSED | BUTTON3_PRESSED)))
235 Playlists->Goto(me.y);
236 if (me.bstate & BUTTON3_PRESSED)
238 size_t pos = Playlists->Choice();
239 SpacePressed();
240 if (pos < Playlists->Size()-1)
241 Playlists->Scroll(wUp);
244 else
245 Screen<Window>::MouseButtonPressed(me);
246 Content->Clear();
248 else if (!Content->Empty() && Content->hasCoords(me.x, me.y))
250 if (w != Content)
251 NextColumn();
252 if (size_t(me.y) < Content->Size() && (me.bstate & (BUTTON1_PRESSED | BUTTON3_PRESSED)))
254 Content->Goto(me.y);
255 if (me.bstate & BUTTON1_PRESSED)
257 size_t pos = Content->Choice();
258 SpacePressed();
259 if (pos < Content->Size()-1)
260 Content->Scroll(wUp);
262 else
263 EnterPressed();
265 else
266 Screen<Window>::MouseButtonPressed(me);
270 MPD::Song *PlaylistEditor::CurrentSong()
272 return w == Content && !Content->Empty() ? &Content->Current() : 0;
275 void PlaylistEditor::GetSelectedSongs(MPD::SongList &v)
277 std::vector<size_t> selected;
278 Content->GetSelected(selected);
279 if (selected.empty())
280 selected.push_back(Content->Choice());
281 for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it)
283 v.push_back(new MPD::Song(Content->at(*it)));
287 void PlaylistEditor::ApplyFilter(const std::string &s)
289 GetList()->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
292 List *PlaylistEditor::GetList()
294 if (w == Playlists)
295 return Playlists;
296 else if (w == Content)
297 return Content;
298 else // silence compiler
299 return 0;