1 /***************************************************************************
2 * Copyright (C) 2008-2010 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 ***************************************************************************/
28 #include "playlist_editor.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
);
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
);
84 std::basic_string
<my_char_t
> PlaylistEditor::Title()
86 return U("Playlist editor");
89 void PlaylistEditor::Refresh()
92 mvvline(MainStartY
, RightColumnStartX
-1, 0, MainHeight
);
96 void PlaylistEditor::SwitchTo()
98 using Global::myScreen
;
100 if (myScreen
== this)
109 if (myScreen
!= this && myScreen
->isTabbable())
110 Global::myPrevScreen
= myScreen
;
112 Global::RedrawHeader
= 1;
114 UpdateSongList(Content
);
117 void PlaylistEditor::Update()
119 if (Playlists
->Empty())
123 Mpd
.GetPlaylists(list
);
124 sort(list
.begin(), list
.end(), CaseInsensitiveSorting());
125 for (MPD::TagList::iterator it
= list
.begin(); it
!= list
.end(); ++it
)
128 Playlists
->AddOption(*it
);
130 Playlists
->Window::Clear();
131 Playlists
->Refresh();
134 if (!Playlists
->Empty() && Content
->Empty())
138 Mpd
.GetPlaylistContent(locale_to_utf_cpy(Playlists
->Current()), list
);
140 Content
->SetTitle("Playlist's content (" + IntoStr(list
.size()) + " item" + (list
.size() == 1 ? ")" : "s)"));
142 Content
->SetTitle("Playlist's content");
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())
154 Content
->AddOption(**it
, bold
);
157 MPD::FreeSongList(list
);
158 Content
->Window::Clear();
162 if (w
== Content
&& Content
->Empty())
164 Content
->HighlightColor(Config
.main_highlight_color
);
165 Playlists
->HighlightColor(Config
.active_column_color
);
169 if (Content
->Empty())
171 *Content
<< XY(0, 0) << "Playlist is empty.";
172 Content
->Window::Refresh();
176 void PlaylistEditor::NextColumn()
180 Playlists
->HighlightColor(Config
.main_highlight_color
);
183 Content
->HighlightColor(Config
.active_column_color
);
187 void PlaylistEditor::PrevColumn()
191 Content
->HighlightColor(Config
.main_highlight_color
);
194 Playlists
->HighlightColor(Config
.active_column_color
);
198 void PlaylistEditor::AddToPlaylist(bool add_n_play
)
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
));
216 void PlaylistEditor::SpacePressed()
218 if (Config
.space_selects
&& w
== Content
)
220 Content
->Select(Content
->Choice(), !Content
->isSelected());
227 void PlaylistEditor::MouseButtonPressed(MEVENT me
)
229 if (!Playlists
->Empty() && Playlists
->hasCoords(me
.x
, me
.y
))
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();
240 if (pos
< Playlists
->Size()-1)
241 Playlists
->Scroll(wUp
);
245 Screen
<Window
>::MouseButtonPressed(me
);
248 else if (!Content
->Empty() && Content
->hasCoords(me
.x
, me
.y
))
252 if (size_t(me
.y
) < Content
->Size() && (me
.bstate
& (BUTTON1_PRESSED
| BUTTON3_PRESSED
)))
255 if (me
.bstate
& BUTTON1_PRESSED
)
257 size_t pos
= Content
->Choice();
259 if (pos
< Content
->Size()-1)
260 Content
->Scroll(wUp
);
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()
296 else if (w
== Content
)
298 else // silence compiler