1 /***************************************************************************
2 * Copyright (C) 2008-2009 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
->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
);
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
);
82 std::basic_string
<my_char_t
> PlaylistEditor::Title()
84 return U("Playlist editor");
87 void PlaylistEditor::Refresh()
90 mvvline(MainStartY
, RightColumnStartX
-1, 0, MainHeight
);
94 void PlaylistEditor::SwitchTo()
96 using Global::myScreen
;
107 if (myScreen
!= this && myScreen
->isTabbable())
108 Global::myPrevScreen
= myScreen
;
110 Global::RedrawHeader
= 1;
112 UpdateSongList(Content
);
115 void PlaylistEditor::Update()
117 if (Playlists
->Empty())
121 Mpd
.GetPlaylists(list
);
122 sort(list
.begin(), list
.end(), CaseInsensitiveSorting());
123 for (MPD::TagList::iterator it
= list
.begin(); it
!= list
.end(); ++it
)
126 Playlists
->AddOption(*it
);
128 Playlists
->Window::Clear();
129 Playlists
->Refresh();
132 if (!Playlists
->Empty() && Content
->Empty())
136 Mpd
.GetPlaylistContent(locale_to_utf_cpy(Playlists
->Current()), list
);
138 Content
->SetTitle("Playlist's content (" + IntoStr(list
.size()) + " item" + (list
.size() == 1 ? ")" : "s)"));
140 Content
->SetTitle("Playlist's content");
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())
152 Content
->AddOption(**it
, bold
);
155 MPD::FreeSongList(list
);
156 Content
->Window::Clear();
160 if (w
== Content
&& Content
->Empty())
162 Content
->HighlightColor(Config
.main_highlight_color
);
163 Playlists
->HighlightColor(Config
.active_column_color
);
167 if (Content
->Empty())
169 *Content
<< XY(0, 0) << "Playlist is empty.";
170 Content
->Window::Refresh();
174 void PlaylistEditor::NextColumn()
178 Playlists
->HighlightColor(Config
.main_highlight_color
);
181 Content
->HighlightColor(Config
.active_column_color
);
185 void PlaylistEditor::PrevColumn()
189 Content
->HighlightColor(Config
.main_highlight_color
);
192 Playlists
->HighlightColor(Config
.active_column_color
);
196 void PlaylistEditor::AddToPlaylist(bool add_n_play
)
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
));
214 void PlaylistEditor::SpacePressed()
216 if (Config
.space_selects
&& w
== Content
)
218 Content
->Select(Content
->Choice(), !Content
->isSelected());
225 void PlaylistEditor::MouseButtonPressed(MEVENT me
)
227 if (!Playlists
->Empty() && Playlists
->hasCoords(me
.x
, me
.y
))
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();
238 if (pos
< Playlists
->Size()-1)
239 Playlists
->Scroll(wUp
);
243 Screen
<Window
>::MouseButtonPressed(me
);
246 else if (!Content
->Empty() && Content
->hasCoords(me
.x
, me
.y
))
250 if (size_t(me
.y
) < Content
->Size() && (me
.bstate
& (BUTTON1_PRESSED
| BUTTON3_PRESSED
)))
253 if (me
.bstate
& BUTTON1_PRESSED
)
255 size_t pos
= Content
->Choice();
257 if (pos
< Content
->Size()-1)
258 Content
->Scroll(wUp
);
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()
294 else if (w
== Content
)
296 else // silence compiler