⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beosmusicbrowser.cpp

📁 FreeAMP(MP3播放)程序源代码-用来研究MP3解码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    // Set the current selection which might have been unselected during
    // the list update process.
    m_playlistView->Select( selected );

    EndViewTransaction();
    Unlock();
}

void
BeOSMusicBrowser::ClearPlaylistView( void )
{
    for ( int32 i = m_playlistView->CountItems() - 1; i >= 0; i-- )
    {
        BListItem* item = m_playlistView->RemoveItem( i );
        delete item;
    }
}

// --------------------------------------------------------------------
// User has invoked (double clicked) an item in the music catalog tree
// --------------------------------------------------------------------
void
BeOSMusicBrowser::CatalogItemInvoked( CatalogItem* item )
{
    switch ( item->Type() )
    {
        case CatalogItem::ITEM_TRACK:
        {
            TrackItem* ti = dynamic_cast<TrackItem*>( item );
            ti->PrintToStream();
            m_plm->AddItem( ti->URL() );
            break;
        }
        case CatalogItem::ITEM_COLLECTION:
        {
            CollectionItem* ci = dynamic_cast<CollectionItem*>( item );
            ci->PrintToStream();
            vector<string> urls;
            uint32 len = GetURLsUnder( ci, &urls );
            PRINT(( "there are %d items\n", len ));
            m_plm->AddItems( urls );
            break;
        }
        case CatalogItem::ITEM_PLAYLIST:
        {
            PlaylistListItem* plli = dynamic_cast<PlaylistListItem*>( item );
            if ( !plli ) break;
            PRINT(( "playlist %s\n", plli->URL() ));
            m_plm->ReadPlaylist( plli->URL() );
            break;
        }
        default:
            break;
    }
}

uint32
BeOSMusicBrowser::GetURLsUnder( CollectionItem* item, vector<string>* urls )
{
    int32 count = m_musicTreeView->CountItemsUnder( item, false );

    for ( int32 i = 0; i < count; i++ )
    {
        BListItem* listItem = m_musicTreeView->ItemUnderAt( item, false, i );
        TrackItem* trackItem = dynamic_cast<TrackItem*>( listItem );
        if ( trackItem )
        {
            PRINT(( "url: %s\n", trackItem->URL() ));
            urls->push_back( string( trackItem->URL() ) );
        }
    }

    return urls->size();
}

void
BeOSMusicBrowser::SaveCurrentPlaylistPanel( BMessage* message, bool reply )
{
    if ( reply )
    {
        entry_ref dir;
        const char* name;
        string path;

        if ( message->FindRef( "directory", &dir ) < B_OK ) return;
        name =  message->FindString( "name" );
        if ( !name ) return;

        BEntry entry( &dir );
        BPath bpath;
        entry.GetPath( &bpath );

        path = string( bpath.Path() ) + string( "/" ) + string( name );
        PRINT(( "User chose the name %s\n", path.c_str() ));
        SaveCurrentPlaylist( path.c_str() );
    }
    else
    {
        BFilePanel* filePanel =
            new BFilePanel( B_SAVE_PANEL,
                            new BMessenger( this ),
                            NULL, // panel_directory
                            B_FILE_NODE,
                            false, // allow_multiple_selection
                            NULL // message to be set below
                            );
        BMessage reply( MBMSG_SAVE_CURRENT_PLAYLIST_REPLY );
        reply.AddPointer( "file_panel", filePanel );
        filePanel->SetMessage( &reply ); // message copied by file panel
        filePanel->Show();
    }
}

void
BeOSMusicBrowser::MovePlaylistItem( uint32 oldIndex, uint32 newIndex )
{
    PRINT(( "Moving %d to %d\n", oldIndex, newIndex ));
    if ( IsntError( m_plm->MoveItem( oldIndex, newIndex ) ) )
    {
        // Selection needs to follow this item.
        m_playlistView->Select( newIndex );
        // At this point, the selection and the actual item selected is NOT
        // in sync, but the list view will be updated shortly later
        // through PlaylistItemMoved event.
    }
}

// -----------------------------------------------------------------
// Init & build UI parts.
// -----------------------------------------------------------------
void
BeOSMusicBrowser::BuildMenu( BMenuBar* menuBar )
{
    BMenuItem* item;
    BMenu* menu;
    BMessage* message;

    // File Menu ------------------------------------------------------------
    menu = new BMenu( "File" );

    item = new BMenuItem( "New Playlist", 
                          new BMessage( MBMSG_CREATE_NEW_PLAYLIST ) , 'N' );
    menu->AddItem( item );
    item = new BMenuItem( "Open Playlist",
                          NULL, 'O' );
    menu->AddItem( item );
    item = new BMenuItem( "Save Playlist",
                          new BMessage( MBMSG_SAVE_CURRENT_PLAYLIST ), 'S' );
    menu->AddItem( item );
    item = new BMenuItem( "Save Playlist As",
                          new BMessage( MBMSG_SAVE_CURRENT_PLAYLIST_AS ),
                          'S', B_SHIFT_KEY );
    menu->AddItem( item );
    menu->AddSeparatorItem();
    item = new BMenuItem( "Import Tracks and Playlists",
                          new BMessage( MBMSG_IMPORT_ITEMS ), 'I' );
    menu->AddItem( item );
    item = new BMenuItem( "Export Playlist", NULL );
    menu->AddItem( item );
    menu->AddSeparatorItem();
    // Modified later if neccesary (if searching is in progress.)
    item = new BMenuItem( MENU_LABEL_START_SEARCH,
                          new BMessage( MBMSG_SEARCH_DIALOG ), 'M' );
    menu->AddItem( item );
    m_searchControl = item;
    menu->AddSeparatorItem();
    item = new BMenuItem( "Close", new BMessage( B_QUIT_REQUESTED ), 'Q' );
    menu->AddItem( item );

    menuBar->AddItem( menu );

    // Edit. ----------------------------------------------------------------
    menu = new BMenu( "Edit" );

    item = new BMenuItem( "Add Items to Playlist", NULL );
    menu->AddItem( item );
    item = new BMenuItem( "Add Tracks or Playlists from Disk", NULL );
    menu->AddItem( item );
    item = new BMenuItem( "Remove Items from My Music",
                           new BMessage( MBMSG_REMOVE_ITEMS ) );
    menu->AddItem( item );
    menu->AddSeparatorItem();
    item = new BMenuItem( "Move Up", new BMessage( MBMSG_MOVE_UP ) );
    menu->AddItem( item );
    item = new BMenuItem( "Move Down", new BMessage( MBMSG_MOVE_DOWN ) );
    menu->AddItem( item );
    menu->AddSeparatorItem();
    item = new BMenuItem( "Clear Playlist",
                          new BMessage( MBMSG_CLEAR_PLAYLIST ) );
    menu->AddItem( item );
    item = new BMenuItem( "Edit Info", new BMessage( MBMSG_EDIT_INFO ) );
    menu->AddItem( item );

    menuBar->AddItem( menu );

    // View. -----------------------------------------------------------------
    menu = new BMenu( "View" );

    item = new BMenuItem( "View Playlist Only", NULL );
    menu->AddItem( item );
    item = new BMenuItem( "Options", NULL );

    menuBar->AddItem( menu );

    // Control. --------------------------------------------------------------
    menu = new BMenu( "Control" );

    item = new BMenuItem( "Play", NULL );
    menu->AddItem( item );
    item = new BMenuItem( "Stop", NULL );
    menu->AddItem( item );
    menu->AddSeparatorItem();
    item = new BMenuItem( "Next Track", NULL );
    menu->AddItem( item );
    item = new BMenuItem( "Previous Track", NULL );
    menu->AddItem( item );
    menu->AddSeparatorItem();
    item = new BMenuItem( "Play Tracks in Normal Order", NULL );
    menu->AddItem( item );
    item = new BMenuItem( "Play Tracks in Random Order", NULL );
    menu->AddItem( item );
    menu->AddSeparatorItem();
    item = new BMenuItem( "Repeat No Tracks", NULL );
    menu->AddItem( item );
    item = new BMenuItem( "Repeat One Track", NULL );
    menu->AddItem( item );
    item = new BMenuItem( "Repeat All Tracks", NULL );
    menu->AddItem( item );

    menuBar->AddItem( menu );

    // Sort. ----------------------------------------------------------------
    menu = new BMenu( "Sort" );

    message = new BMessage( MBMSG_SORT_PLAYLIST );
    message->AddInt32( "PlaylistSortKey", kPlaylistSortKey_Artist );
    item = new BMenuItem( "Sort Playlist by Artist", message, 'A' );
    menu->AddItem( item );
    message = new BMessage( MBMSG_SORT_PLAYLIST );
    message->AddInt32( "PlaylistSortKey", kPlaylistSortKey_Album );
    item = new BMenuItem( "Sort Playlist by Album", message, 'L' );
    menu->AddItem( item );
    message = new BMessage( MBMSG_SORT_PLAYLIST );
    message->AddInt32( "PlaylistSortKey", kPlaylistSortKey_Title );
    item = new BMenuItem( "Sort Playlist by Title", message, 'T' );
    menu->AddItem( item );
    message = new BMessage( MBMSG_SORT_PLAYLIST );
    message->AddInt32( "PlaylistSortKey", kPlaylistSortKey_Year );
    item = new BMenuItem( "Sort Playlist by Year", message, 'Y' );
    menu->AddItem( item );
    message = new BMessage( MBMSG_SORT_PLAYLIST );
    message->AddInt32( "PlaylistSortKey", kPlaylistSortKey_Track );
    item = new BMenuItem( "Sort Playlist by Track Number", message, 'R' );
    menu->AddItem( item );
    message = new BMessage( MBMSG_SORT_PLAYLIST );
    message->AddInt32( "PlaylistSortKey", kPlaylistSortKey_Genre );
    item = new BMenuItem( "Sort Playlist by Genre", message );
    menu->AddItem( item );
    message = new BMessage( MBMSG_SORT_PLAYLIST );
    message->AddInt32( "PlaylistSortKey", kPlaylistSortKey_Time );
    item = new BMenuItem( "Sort Playlist by Length", message );
    menu->AddItem( item );
    message = new BMessage( MBMSG_SORT_PLAYLIST );
    message->AddInt32( "PlaylistSortKey", kPlaylistSortKey_Location );
    item = new BMenuItem( "Sort Playlist by Location", message );
    menu->AddItem( item );
    message = new BMessage( MBMSG_SORT_PLAYLIST );
    message->AddInt32( "PlaylistSortKey", kPlaylistSortKey_Random );
    item = new BMenuItem( "Sort Playlist Randomly", message );
    menu->AddItem( item );

    menuBar->AddItem( menu );

    // Help. -------------------------------------------------------------
    menu = new BMenu( "Help" );
    item = new BMenuItem( "FreeAmp Web Site", NULL );
    menu->AddItem( item );
    item = new BMenuItem( "EMusic.com Web Site", NULL );
    menu->AddItem( item );
    menuBar->AddItem( menu );

#if DEBUG_MENU
    menu = new BMenu( "Debug" );
    item = new BMenuItem( "Clear My Music", new BMessage( MBMSG_DEBUG_1 ) );
    menu->AddItem( item );
    item = new BMenuItem( "Clear My Playlist", new BMessage( MBMSG_DEBUG_2 ) );
    menu->AddItem( item );
    item = new BMenuItem( "Rebuild Catalog View", new BMessage( MBMSG_DEBUG_3 ) );
    menu->AddItem( item );
    menuBar->AddItem( menu );
#endif
}

void
BeOSMusicBrowser::BuildToolBar( ToolBar* toolBar )
{
    TooltipFilter* tip;
    ResourceManager rsrcs( "musicbrowser.ui" );

    ToolBarButton* button;
    BBitmap* bitmap;

    bitmap = rsrcs.GetBitmap( B_TRANSLATOR_BITMAP, TOOLBAR_NEW_PLAYLIST );
    button = new ToolBarButton( bitmap, "New Playlist",
                                new BMessage( MBMSG_CREATE_NEW_PLAYLIST ) );
    tip = new TooltipFilter( new BMessage( MBMSG_TOOLTIP_MESSAGE ),
                             "Create new playlist" );
    button->AddFilter( tip );
    toolBar->AddItem( button );

    bitmap = rsrcs.GetBitmap( B_TRANSLATOR_BITMAP, TOOLBAR_SAVE_PLAYLIST );
    button = new ToolBarButton( bitmap, "Save Playlist",
                                new BMessage( MBMSG_SAVE_CURRENT_PLAYLIST ) );
    tip = new TooltipFilter( new BMessage( MBMSG_TOOLTIP_MESSAGE ),
                             "Save playlist" );
    button->AddFilter( tip );
    toolBar->AddItem( button );

    bitmap = rsrcs.GetBitmap( B_TRANSLATOR_BITMAP, TOOLBAR_IMPORT_ITEMS );
    button = new ToolBarButton( bitmap, "Import Items",
                                new BMessage( MBMSG_IMPORT_ITEMS ) );
    tip = new TooltipFilter( new BMessage( MBMSG_TOOLTIP_MESSAGE ),
                             "Import items from disk" );
    button->AddFilter( tip );
    toolBar->AddItem( button );

    bitmap = rsrcs.GetBitmap( B_TRANSLATOR_BITMAP, TOOLBAR_REMOVE_ITEMS );
    button = new ToolBarButton( bitmap, "Remove Items",
                                new BMessage( MBMSG_REMOVE_ITEMS ) );
    tip = new TooltipFilter( new BMessage( MBMSG_TOOLTIP_MESSAGE ),
                             "Remove Items" );
    button->AddFilter( tip );
    toolBar->AddItem( button );

    bitmap = rsrcs.GetBitmap( B_TRANSLATOR_BITMAP, TOOLBAR_EDIT_INFO );
    button = new ToolBarButton( bitmap, "Edit Info",
                                new BMessage( MBMSG_EDIT_INFO ) );
    tip = new TooltipFilter( new BMessage( MBMSG_TOOLTIP_MESSAGE ),
                             "Edit Info" );
    button->AddFilter( tip );
    toolBar->AddItem( button );

    bitmap = rsrcs.GetBitmap( B_TRANSLATOR_BITMAP, TOOLBAR_ADD_TRACKS );
    button = new ToolBarButton( bitmap, "Add Tracks",
                                NULL );
    tip = new TooltipFilter( new BMessage( MBMSG_TOOLTIP_MESSAGE ),
                             "Add Tracks" );
    button->AddFilter( tip );
    toolBar->AddItem( button );

    bitmap = rsrcs.GetBitmap( B_TRANSLATOR_BITMAP, TOOLBAR_ADD_FILES );
    button = new ToolBarButton( bitmap, "Add Files",
                                NULL );
    tip = new TooltipFilter( new BMessage( MBMSG_TOOLTIP_MESSAGE ),
                             "Add Files" );
    button->AddFilter( tip );
    toolBar->AddItem( button );

    bitmap = rsrcs.GetBitmap( B_TRANSLATOR_BITMAP, TOOLBAR_UP );
    button = new ToolBarButton( bitmap, "Up",
                                new BMessage( MBMSG_MOVE_UP ) );
    tip = new TooltipFilter( new BMessage( MBMSG_TOOLTIP_MESSAGE ),
                             "Up" );
    button->AddFilter( tip );
    toolBar->AddItem( button );

    bitmap = rsrcs.GetBitmap( B_TRANSLATOR_BITMAP, TOOLBAR_DOWN );
    button = new ToolBarButton( bitmap, "Down",
                                new BMessage( MBMSG_MOVE_DOWN ) );
    tip = new TooltipFilter( new BMessage( MBMSG_TOOLTIP_MESSAGE ),
                             "Down" );
    button->AddFilter( tip );
    toolBar->AddItem( button );

    toolBar->ResizeToPreferred();
}

// vi: set ts=4:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -