📄 browserlist.cpp
字号:
/*____________________________________________________________________________
FreeAmp - The Free MP3 Player
Portions Copyright (C) 1999-2000 EMusic.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: browserlist.cpp,v 1.18 2001/01/24 20:47:25 ijr Exp $
____________________________________________________________________________*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "gtkmusicbrowser.h"
#include "utility.h"
#include "player.h"
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <algorithm>
using namespace std;
struct GtkDragDestSite
{
GtkDestDefaults flags;
GtkTargetList *target_list;
GdkDragAction actions;
GdkWindow *proxy_window;
GdkDragProtocol proxy_protocol;
gboolean do_proxy : 1;
gboolean proxy_coords : 1;
gboolean have_drag : 1;
};
PlaylistManager *BADPLM = NULL;
class comp_pi_pos {
public:
bool operator()(PlaylistItem *a, PlaylistItem *b)
{
if (!BADPLM)
return false;
return (BADPLM->IndexOf(a) < BADPLM->IndexOf(b));
}
};
void int_destroy(int *oldint)
{
delete oldint;
}
void GTKMusicBrowser::RenumberPlaylistList(int starting)
{
if (starting > GTK_CLIST(playlistList)->rows)
return;
gtk_clist_freeze(GTK_CLIST(playlistList));
int totaltime = 0;
int32 maxCDtime = 0;
if (m_bCDMode) {
if (starting >= 1)
totaltime = *(int *)(gtk_clist_get_row_data(GTK_CLIST(playlistList),
starting - 1));
m_context->prefs->GetPrefInt32(kAudioCDLengthPref, &maxCDtime);
}
for (int i = starting; i < GTK_CLIST(playlistList)->rows; i++) {
char location[50];
sprintf(location, "%d", i + 1);
gtk_clist_set_text(GTK_CLIST(playlistList), i, 0, location);
if (m_bCDMode) {
PlaylistItem *item = m_plm->ItemAt(i);
totaltime += item->GetMetaData().Time();
if (totaltime > maxCDtime)
gtk_clist_set_row_style(GTK_CLIST(playlistList), i, redStyle);
else
gtk_clist_set_row_style(GTK_CLIST(playlistList), i,
greenStyle);
int *totTime = (int *)gtk_clist_get_row_data(
GTK_CLIST(playlistList), i);
*totTime = totaltime;
}
}
gtk_clist_thaw(GTK_CLIST(playlistList));
}
void GTKMusicBrowser::UpdatePlaylistItems(const vector<PlaylistItem *> *items)
{
if (!items)
return;
vector<PlaylistItem *>::const_iterator i = items->begin();
uint32 minpos = kInvalidIndex;
for (; i != items->end(); i++) {
uint32 pos = m_plm->IndexOf(*i);
if (pos == kInvalidIndex)
continue;
if (pos < minpos)
minpos = pos;
MetaData mdata = (*i)->GetMetaData();
char *iText[9];
char position[40];
char *title;
char *artist;
char *album;
char *genre;
char location[_MAX_PATH];
uint32 loclength = _MAX_PATH;
char *comment;
char year[10];
char length[50];
string empty = " ";
sprintf(position, "%d", pos + 1);
title = (char *)mdata.Title().c_str();
artist = (char *)mdata.Artist().c_str();
album = (char *)mdata.Album().c_str();
genre = (char *)mdata.Genre().c_str();
comment = (char *)mdata.Comment().c_str();
URLToFilePath((*i)->URL().c_str(), location, &loclength);
if (mdata.Year() == 0)
sprintf(year, "Unknown");
else
sprintf(year, "%d", mdata.Year());
if (mdata.Time() == 0)
sprintf(length, "Unknown");
else {
int secs = mdata.Time();
if (secs > 3600)
sprintf(length, "%d:%02d:%02d", secs / 3600, (secs / 60) % 60,
secs % 60);
else
sprintf(length, "%d:%02d", (secs / 60) % 60, secs % 60);
}
for (int i = 0; i < 9; i++)
{
switch (playlistCols[i])
{
case kArtistColumn:
iText[i] = artist;
break;
case kAlbumColumn:
iText[i] = album;
break;
case kCommentColumn:
iText[i] = comment;
break;
case kGenreColumn:
iText[i] = genre;
break;
case kLocationColumn:
iText[i] = location;
break;
case kPositionColumn:
iText[i] = position;
break;
case kTitleColumn:
iText[i] = title;
break;
case kTimeColumn:
iText[i] = length;
break;
case kYearColumn:
iText[i] = year;
break;
default:
iText[i] = (char *)empty.c_str();
break;
}
}
for (uint32 count = 0; count < 9; count++)
gtk_clist_set_text(GTK_CLIST(playlistList), pos, count, iText[count]);
}
if (m_bCDMode)
RenumberPlaylistList(minpos);
}
void GTKMusicBrowser::AddPlaylistItems(vector<PlaylistItem *> *items)
{
if (!items)
return;
uint32 minpos = (uint32)-1;
// hack hack hack hack
BADPLM = m_plm;
sort(items->begin(), items->end(), comp_pi_pos());
BADPLM = NULL;
gtk_clist_freeze(GTK_CLIST(playlistList));
vector<PlaylistItem *>::iterator i = items->begin();
for (; i != items->end(); i++) {
PlaylistItem *item = *i;
if (!item)
continue;
MetaData mdata = item->GetMetaData();
char *iText[9];
char position[40];
char *title;
char *artist;
char *album;
char *genre;
char location[_MAX_PATH];
uint32 loclength = _MAX_PATH;
char *comment;
char length[50];
string empty = " ";
uint32 pos = m_plm->IndexOf(item);
sprintf(position, "%d", pos + 1);
title = (char *)mdata.Title().c_str();
artist = (char *)mdata.Artist().c_str();
album = (char *)mdata.Album().c_str();
genre = (char *)mdata.Genre().c_str();
comment = (char *)mdata.Comment().c_str();
URLToFilePath(item->URL().c_str(), location, &loclength);
if (mdata.Time() == 0)
sprintf(length, "Unknown");
else {
int secs = mdata.Time();
if (secs > 3600)
sprintf(length, "%d:%02d:%02d", secs / 3600, (secs / 60) % 60,
secs % 60);
else
sprintf(length, "%d:%02d", (secs / 60) % 60, secs % 60);
}
for (int i = 0; i < 9; i++)
{
switch (playlistCols[i])
{
case kArtistColumn:
iText[i] = artist;
break;
case kAlbumColumn:
iText[i] = album;
break;
case kCommentColumn:
iText[i] = comment;
break;
case kGenreColumn:
iText[i] = genre;
break;
case kLocationColumn:
iText[i] = location;
break;
case kPositionColumn:
iText[i] = position;
break;
case kTitleColumn:
iText[i] = title;
break;
case kTimeColumn:
iText[i] = length;
break;
default:
iText[i] = (char *)empty.c_str();
break;
}
}
gtk_clist_insert(GTK_CLIST(playlistList), pos, iText);
int *totTime = new int;
*totTime = 0;
gtk_clist_set_row_data_full(GTK_CLIST(playlistList), pos, totTime,
(GtkDestroyNotify)int_destroy);
if (pos < minpos)
minpos = pos;
}
RenumberPlaylistList(minpos);
gtk_clist_columns_autosize(GTK_CLIST(playlistList));
gtk_clist_select_row(GTK_CLIST(playlistList), m_lastindex, 0);
gtk_clist_moveto(GTK_CLIST(playlistList), m_lastindex, 0, 0.5, -1);
gtk_clist_thaw(GTK_CLIST(playlistList));
}
void GTKMusicBrowser::RemovePlaylistItems(vector<uint32> *indices)
{
if (!indices)
return;
uint32 minpos = (uint32)-1;
sort(indices->begin(), indices->end(), greater<uint32>());
gtk_clist_freeze(GTK_CLIST(playlistList));
vector<uint32>::iterator i = indices->begin();
for (; i != indices->end(); i++) {
gtk_clist_remove(GTK_CLIST(playlistList), *i);
if (*i < minpos)
minpos = *i;
}
RenumberPlaylistList(minpos);
gtk_clist_columns_autosize(GTK_CLIST(playlistList));
gtk_clist_select_row(GTK_CLIST(playlistList), m_lastindex, 0);
gtk_clist_moveto(GTK_CLIST(playlistList), m_lastindex, 0, 0.5, -1);
gtk_clist_thaw(GTK_CLIST(playlistList));
}
void GTKMusicBrowser::ParsePlaylistCols(void)
{
unsigned int size = 100;
char *buffer = (char *)malloc( size );
if (kError_BufferTooSmall == m_context->prefs->GetPrefString(
kPlaylistHeaderColumnsPref,
buffer, &size))
{
int bufferSize = size;
buffer = (char*)realloc(buffer, bufferSize);
m_context->prefs->GetPrefString(kPlaylistHeaderColumnsPref, buffer, &size);
}
int column = 1;
char *token = strtok(buffer, "|");
while (token != NULL && column < 9)
{
PlaylistColumns newcol = kEmptyColumn;
if (!strcmp(token, "Artist"))
newcol = kArtistColumn;
else if (!strcmp(token, "Album"))
newcol = kAlbumColumn;
else if (!strcmp(token, "Comment"))
newcol = kCommentColumn;
else if (!strcmp(token, "Genre"))
newcol = kGenreColumn;
else if (!strcmp(token, "Location"))
newcol = kLocationColumn;
else if (!strcmp(token, "Title"))
newcol = kTitleColumn;
else if (!strcmp(token, "Time"))
newcol = kTimeColumn;
else if (!strcmp(token, "Year"))
newcol = kYearColumn;
else
newcol = kEmptyColumn;
if (playlistCols[column] != newcol)
playlistColsChanged = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -