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

📄 listview.cpp

📁 WINCE XAUDIO可播放各种音乐格式内附SOURCE CODE可以再不同CPU 平台编译测试
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************
|
|      Xaudio Player for Windows CE      
|
|      (c) 1996-1998 MpegTV, LLC
|      Author: Gilles Boccon-Gibod (gilles@mpegtv.com)
|
 ****************************************************************/

/*----------------------------------------------------------------------
|       includes
+---------------------------------------------------------------------*/
#ifndef STRICT
#define STRICT
#endif
#include <windows.h>

#include "xaplayer.h"
#include "listview.h"
#include "files.h"
#include "resource.h"
#include "ceutils.h"

/*----------------------------------------------------------------------
|       constants
+---------------------------------------------------------------------*/
const TCHAR XA_LISTVIEW_CLASS_NAME[]          = TEXT("Xaudio List View");
const int XA_LISTVIEW_COMMAND_BANDS_ID = 7000;
const int XA_LISTVIEW_COMMAND_BAR1_ID  = 7001;
const int XA_LISTVIEW_COMMAND_BAR2_ID  = 7002;

const int XA_LISTVIEW_DELETE            = 100;
const int XA_LISTVIEW_ADD               = 101;
const int XA_LISTVIEW_NEXT              = 102;
const int XA_LISTVIEW_PREV              = 103;
const int XA_LISTVIEW_LOOP              = 104;
const int XA_LISTVIEW_RANDOM            = 105;

/*----------------------------------------------------------------------
|       ListViewWindowProc
+---------------------------------------------------------------------*/
LRESULT CALLBACK
ListViewWindowProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
{
	XA_HANDLE_WINDOW_MESSAGE(ListView, 
                             HandleWindowsMessage,
                             window, 
                             message, 
                             wparam, 
                             lparam);
}

/*----------------------------------------------------------------------
|       ListViewControlProc
+---------------------------------------------------------------------*/
LRESULT CALLBACK
ListViewControlProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
{                                                                  
	ListView *target;                                          
    target = (ListView *)GetWindowLong(window, GWL_USERDATA);   
    return target->HandleControlMessage(window,                         
	                                    message,                        
		        				        wparam,                         
				    			        lparam);
}

/*----------------------------------------------------------------------
|       ListViewCallback
+---------------------------------------------------------------------*/
void
ListViewCallback(void *client, 
                 PlayList *play_list, 
                 PlayListEventType event,
                 void *event_data)
{
    ListView *view = (ListView *)client;

    switch (event) {
      case PLAYLIST_EVENT_ITEM_CREATED:
          view->OnItemCreated((PlayListItem *)event_data);
          break;

      case PLAYLIST_EVENT_ITEM_DELETED:
          view->OnItemDeleted((PlayListItem *)event_data);
          break;

      case PLAYLIST_EVENT_CURRENT_CHANGED:
          view->OnCurrentChanged((PlayListCurrentChangedEvent *)event_data);
          break;

       case PLAYLIST_EVENT_STOP_PLAYING:
          view->OnActionStopPlaying();
          break;
    }
}

/*----------------------------------------------------------------------
|       ListView::ListView
+---------------------------------------------------------------------*/
ListView::ListView(HWND parent, WCEPlayer *player, 
                   int x, int y, int width, int height) 
{
    WNDCLASS wclass;

    wclass.style         = CS_HREDRAW | CS_VREDRAW;
    wclass.lpfnWndProc   = ListViewWindowProc;
    wclass.cbClsExtra    = 0;
    wclass.cbWndExtra    = 0;
    wclass.hInstance     = player->m_Instance;
    wclass.hIcon         = 0;
    wclass.hCursor       = NULL;
    wclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wclass.lpszMenuName  = NULL;
    wclass.lpszClassName = XA_LISTVIEW_CLASS_NAME;

    RegisterClass(&wclass);

    // initialize some variables
    m_Player     = player;
    m_Visible    = TRUE;
    m_Height     = height;
    m_Width      = width;
    
    // create the shell window
    m_Window = CreateWindow(
        XA_LISTVIEW_CLASS_NAME,            // class name
        NULL,                              // caption
        WS_CHILD | WS_VISIBLE,             // style
        x,                                 // x pos
        y,                                 // y pos
        m_Width,                           // x size
        m_Height,                          // y size
        parent,                            // parent
        NULL,                              // menu
        player->m_Instance,                // program instance
        this);                             // creation params


    // setup vertical command bars
    m_CommandBands = CommandBands_Create(player->m_Instance, m_Window, 
                                         XA_LISTVIEW_COMMAND_BANDS_ID,
                                         RBS_AUTOSIZE | CCS_VERT, 
                                         NULL);
    REBARBANDINFO info[2];
    info[0].cbSize = sizeof(REBARBANDINFO);
    info[0].fMask  = RBBIM_ID | RBBIM_STYLE;
    info[0].fStyle = RBBS_NOGRIPPER;
    info[0].wID    = XA_LISTVIEW_COMMAND_BAR1_ID;
    info[1].cbSize = sizeof(REBARBANDINFO);
    info[1].fMask  = RBBIM_ID | RBBIM_STYLE;
    info[1].fStyle = RBBS_NOGRIPPER | RBBS_BREAK;
    info[1].wID    = XA_LISTVIEW_COMMAND_BAR2_ID;
    CommandBands_AddBands(m_CommandBands, player->m_Instance, 2, info);

  	TBBUTTON toolbar1_buttons[] = {
		{0, XA_LISTVIEW_DELETE, TBSTATE_WRAP | TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
		{1, XA_LISTVIEW_NEXT,   TBSTATE_WRAP | TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
		{2, XA_LISTVIEW_LOOP,   TBSTATE_WRAP | TBSTATE_ENABLED, TBSTYLE_CHECK,  0, 0, 0},
	};
    m_CommandBar1 = CommandBands_GetCommandBar(m_CommandBands, 0);
    CommandBar_AddBitmap(m_CommandBar1, player->m_Instance, IDB_PLAYLIST_TOOLBAR1_BITMAPS, 3, 0, 0);
    CommandBar_AddButtons(m_CommandBar1, sizeof(toolbar1_buttons)/sizeof(TBBUTTON), toolbar1_buttons);

    TBBUTTON toolbar2_buttons[] = {
		{0, XA_LISTVIEW_ADD,    TBSTATE_WRAP | TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
		{1, XA_LISTVIEW_PREV,   TBSTATE_WRAP | TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
		{2, XA_LISTVIEW_RANDOM, TBSTATE_WRAP | TBSTATE_ENABLED, TBSTYLE_CHECK,  0, 0, 0},
	};
    m_CommandBar2 = CommandBands_GetCommandBar(m_CommandBands, 1);
    CommandBar_AddBitmap(m_CommandBar2, player->m_Instance, IDB_PLAYLIST_TOOLBAR2_BITMAPS, 3, 0, 0);
    CommandBar_AddButtons(m_CommandBar2, sizeof(toolbar2_buttons)/sizeof(TBBUTTON), toolbar2_buttons);

    // create the list view
    RECT bar1_bounds;
    RECT bar2_bounds;
    GetWindowRect(m_CommandBar1, &bar1_bounds);
    GetWindowRect(m_CommandBar2, &bar2_bounds);
    int bands_width = bar2_bounds.right-bar1_bounds.left;
    m_ListView = CreateWindow(
        WC_LISTVIEW,
        TEXT(""),
        WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL | LVS_NOCOLUMNHEADER,
        bands_width,
        0,
        width-bands_width,
        height,
        m_Window,
        NULL,
        player->m_Instance,
        this);
    
    // subclass the window to get double clicks
    SetWindowLong(m_ListView, GWL_USERDATA, (LONG)this);         
    m_OriginalControlProc = GetWindowLong(m_ListView, GWL_WNDPROC);
    SetWindowLong(m_ListView, GWL_WNDPROC, (LONG)ListViewControlProc);

    // make one column for the whole list
    LV_COLUMN column;
    column.mask     = LVCF_WIDTH | LVCF_TEXT;
    column.pszText  = TEXT("Name");
    column.cx       = width-bands_width-32;
    column.iSubItem = 0;
    ListView_InsertColumn(m_ListView, 0, &column);

    // create the state image list
    m_ImageList = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 2, 1);
	ImageList_AddIcon(m_ImageList, 
		              (HICON)LoadImage(player->m_Instance, 
		                               ID(IDI_CURRENT_TRACK),
                                       IMAGE_ICON,
                                       16, 16, 0));
	ListView_SetImageList(m_ListView, m_ImageList, TVSIL_STATE);

    // register the callback function
    m_PlayList.RegisterCallback(ListViewCallback, (void *)this);
}

/*----------------------------------------------------------------------
|       ListView::SetBounds
+---------------------------------------------------------------------*/
void 
ListView::SetBounds(int x, int y, int width, int height)
{
    MoveWindow(m_Window, x, y, width, height, TRUE);

    RECT bar1_bounds;
    RECT bar2_bounds;
    GetWindowRect(m_CommandBar1, &bar1_bounds);
    GetWindowRect(m_CommandBar2, &bar2_bounds);
    int bands_width = bar2_bounds.right-bar1_bounds.left;
    MoveWindow(m_ListView, 
               bands_width, 0, 
               width-bands_width, height, TRUE);
}

/*----------------------------------------------------------------------
|       ListView::HandleWindowsMessage
+---------------------------------------------------------------------*/
LRESULT 
ListView::HandleWindowsMessage(HWND window,
                               UINT message, 
                               WPARAM wparam, 
                               LPARAM lparam)
{
    switch (message) {
      case WM_COMMAND:
        switch (LOWORD(wparam)) {
          case XA_LISTVIEW_DELETE:
            DoDelete();
            break;

          case XA_LISTVIEW_ADD:
            DoAddFile();
            break;

          case XA_LISTVIEW_NEXT:
            DoNext();
            break;

          case XA_LISTVIEW_PREV:
            DoPrev();
            break;

          case XA_LISTVIEW_LOOP:
            DoLoop();
            break;

          case XA_LISTVIEW_RANDOM:
            DoRandom();
            break;
        }
        return 0;

      default:
        return DefWindowProc(window, message, wparam, lparam);
    }
}

/*----------------------------------------------------------------------
|       ListView::HandleControlMessage
+---------------------------------------------------------------------*/
LRESULT 
ListView::HandleControlMessage(HWND window,
                               UINT message, 
                               WPARAM wparam, 
                               LPARAM lparam)
{
    switch (message) {
      case WM_LBUTTONDBLCLK:
        DoDoubleClick(HIWORD(lparam), LOWORD(lparam));
        return 0;

      default:
        return CallWindowProc((WNDPROC)m_OriginalControlProc, 
                              window, message, wparam, lparam);
    }
}

/*----------------------------------------------------------------------
|       ListView::GetPlayListItem
+---------------------------------------------------------------------*/
PlayListItem *
ListView::GetPlayListItem(int index)
{
    LVITEM item;
    item.mask  = LVIF_PARAM;
    item.iItem = index;
    ListView_GetItem(m_ListView, &item);
    return (PlayListItem *)item.lParam;
}

/*----------------------------------------------------------------------
|       ListView::GetListViewItem
+---------------------------------------------------------------------*/
int
ListView::GetListViewItem(PlayListItem *item)
{
    LVFINDINFO info;

    info.flags  = LVFI_PARAM;
    info.lParam = (LPARAM)item;
    return ListView_FindItem(m_ListView, -1, &info);
}

/*----------------------------------------------------------------------
|       ListView::SetPlaying
+---------------------------------------------------------------------*/
void
ListView::SetPlaying(bool on_off)
{
    m_Playing = on_off;
    if (on_off) {
        m_PlayList.SetMode(PLAYLIST_MODE_PLAYING, PLAYLIST_MODE_PLAYING);
    } else {
        m_PlayList.SetMode(PLAYLIST_MODE_PLAYING, 0);
    }
}

/*----------------------------------------------------------------------
|       ListView::Add
+---------------------------------------------------------------------*/
void
ListView::Add(const char *name)
{
    // try to guess the type of the file based on the extension
    const char *extension = GetFilenameExtension(name);
    if (extension) {
        if (!strcmp(extension, "pls")) {
            AddPlayList(name);
            return;
        }
    } 
    AddFile(name);
}

/*----------------------------------------------------------------------
|       ListView::AddFile
+---------------------------------------------------------------------*/

⌨️ 快捷键说明

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