75c6473a49040cd86199d5f66d7594cb4da71d1e
[vlc.git] / modules / gui / wince / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : WinCE gui plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Marodon Cedric <cedric_marodon@yahoo.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31 #include <vlc/vlc.h>
32 #include <vlc/intf.h>
33
34 #include "wince.h"
35
36 #include <commctrl.h>
37 #include <commdlg.h>
38
39 #ifndef NMAXFILE
40 #define NMAXFILE 512 // at least 256
41 #endif
42
43 #ifndef TEXTMAXBUF
44 #define TEXTMAXBUF 512 // at least 500
45 #endif
46
47 #define LONG2POINT(l, pt)  ((pt).x = (SHORT)LOWORD(l), (pt).y = (SHORT)HIWORD(l))
48
49 #define NUMIMAGES     11   // Number of buttons in the toolbar           
50 #define IMAGEWIDTH    16   // Width of the buttons in the toolbar  
51 #define IMAGEHEIGHT   16   // Height of the buttons in the toolbar  
52 #define BUTTONWIDTH   0    // Width of the button images in the toolbar
53 #define BUTTONHEIGHT  0    // Height of the button images in the toolbar
54 #define ID_TOOLBAR    2000 // Identifier of the main tool bar
55
56 enum      
57 {
58   Infos_Event = 1000,
59   Up_Event,
60   Down_Event,
61   Random_Event,
62   Loop_Event,
63   Repeat_Event,
64   PopupPlay_Event,
65   PopupDel_Event,
66   PopupEna_Event,
67   PopupInfo_Event  
68 };
69
70 // Help strings
71 #define HELP_OPENPL _T("Open playlist")
72 #define HELP_SAVEPL _T("Save playlist")
73 #define HELP_ADDFILE _T("Add File")
74 #define HELP_ADDMRL _T("Add MRL")
75 #define HELP_DELETE _T("Delete selection")
76 #define HELP_INFOS _T("Item info")
77 #define HELP_UP _T("Up")
78 #define HELP_DOWN _T("Down")
79 #define HELP_RANDOM _T("Random")
80 #define HELP_LOOP _T("Repeat all")
81 #define HELP_REPEAT _T("Repeat one")
82
83 // The TBBUTTON structure contains information the toolbar buttons.
84 static TBBUTTON tbButton2[] =
85 {
86   {0, ID_MANAGE_OPENPL,        TBSTATE_ENABLED, TBSTYLE_BUTTON},
87   {1, ID_MANAGE_SAVEPL,       TBSTATE_ENABLED, TBSTYLE_BUTTON},
88   {0, 0,              TBSTATE_ENABLED, TBSTYLE_SEP},
89   {2, ID_MANAGE_ADDFILE,       TBSTATE_ENABLED, TBSTYLE_BUTTON},
90   {3, ID_MANAGE_ADDMRL,        TBSTATE_ENABLED, TBSTYLE_BUTTON},
91   {4, ID_SEL_DELETE,       TBSTATE_ENABLED, TBSTYLE_BUTTON},
92   {0, 0,              TBSTATE_ENABLED, TBSTYLE_SEP},
93   {5, Infos_Event,      TBSTATE_ENABLED, TBSTYLE_BUTTON},
94   {0, 0,              TBSTATE_ENABLED, TBSTYLE_SEP},
95   {6, Up_Event,      TBSTATE_ENABLED, TBSTYLE_BUTTON},
96   {7, Down_Event,      TBSTATE_ENABLED, TBSTYLE_BUTTON},
97   {0, 0,              TBSTATE_ENABLED, TBSTYLE_SEP},
98   {8, Random_Event,      TBSTATE_ENABLED, TBSTYLE_CHECK},
99   {9, Loop_Event,       TBSTATE_ENABLED, TBSTYLE_CHECK},
100   {10, Repeat_Event,       TBSTATE_ENABLED, TBSTYLE_CHECK}
101 };
102
103 // Toolbar ToolTips
104 TCHAR * szToolTips2[] = 
105 {
106     HELP_OPENPL,
107     HELP_SAVEPL,
108     HELP_ADDFILE,
109     HELP_ADDMRL,
110     HELP_DELETE,
111     HELP_INFOS,
112     HELP_UP,
113     HELP_DOWN,
114     HELP_RANDOM,
115     HELP_LOOP,
116     HELP_REPEAT
117 };
118
119 /*****************************************************************************
120  * Event Table.
121  *****************************************************************************/
122
123 /*****************************************************************************
124  * Constructor.
125  *****************************************************************************/
126 Playlist::Playlist( intf_thread_t *_p_intf, HINSTANCE _hInst )
127 {
128     /* Initializations */
129     p_intf = _p_intf;
130         hInst = _hInst;
131         hListView = NULL;
132
133     i_title_sorted = 1;
134     i_author_sorted = 1;
135
136     b_need_update = VLC_TRUE;
137 }
138
139 /***********************************************************************
140 FUNCTION: 
141   CreateMenuBar
142
143 PURPOSE: 
144   Creates a menu bar.
145 ***********************************************************************/
146 static HWND CreateMenuBar( HWND hwnd, HINSTANCE hInst )
147 {
148 #ifdef UNDER_CE
149     SHMENUBARINFO mbi;
150     memset( &mbi, 0, sizeof(SHMENUBARINFO) );
151     mbi.cbSize     = sizeof(SHMENUBARINFO);
152     mbi.hwndParent = hwnd;
153     mbi.hInstRes   = hInst;
154     mbi.nToolBarId = IDR_MENUBAR2;
155
156     if( !SHCreateMenuBar( &mbi ) )
157     {
158         MessageBox(hwnd, _T("SHCreateMenuBar Failed"), _T("Error"), MB_OK);
159         return 0;
160     }
161
162     TBBUTTONINFO tbbi;
163     tbbi.cbSize = sizeof(tbbi);
164     tbbi.dwMask = TBIF_LPARAM;
165
166     SendMessage( mbi.hwndMB, TB_GETBUTTONINFO, IDM_MANAGE, (LPARAM)&tbbi );
167     HMENU hmenu_file = (HMENU)tbbi.lParam;
168     RemoveMenu( hmenu_file, 0, MF_BYPOSITION );
169     SendMessage( mbi.hwndMB, TB_GETBUTTONINFO, IDM_SORT, (LPARAM)&tbbi );
170     HMENU hmenu_sort = (HMENU)tbbi.lParam;
171     RemoveMenu( hmenu_sort, 0, MF_BYPOSITION );
172     SendMessage( mbi.hwndMB, TB_GETBUTTONINFO, IDM_SEL, (LPARAM)&tbbi );
173     HMENU hmenu_sel = (HMENU)tbbi.lParam;
174     RemoveMenu( hmenu_sel, 0, MF_BYPOSITION );
175
176 #else
177     HMENU hmenu_file = CreatePopupMenu();
178     HMENU hmenu_sort = CreatePopupMenu();
179     HMENU hmenu_sel = CreatePopupMenu();
180 #endif
181
182     AppendMenu( hmenu_file, MF_STRING, ID_MANAGE_ADDFILE,
183                 _T("&Add File...") );
184     AppendMenu( hmenu_file, MF_STRING, ID_MANAGE_ADDDIRECTORY,
185                 _T("Add Directory...") );
186     AppendMenu( hmenu_file, MF_STRING, ID_MANAGE_ADDMRL,
187                 _T("Add MRL...") );
188     AppendMenu( hmenu_file, MF_SEPARATOR, 0, 0 );
189     AppendMenu( hmenu_file, MF_STRING, ID_MANAGE_OPENPL,
190                 _T("Open &Playlist") );
191     AppendMenu( hmenu_file, MF_STRING, ID_MANAGE_SAVEPL,
192                 _T("Save Playlist") );
193
194     AppendMenu( hmenu_sort, MF_STRING, ID_SORT_TITLE,
195                 _T("Sort by &title") );
196     AppendMenu( hmenu_sort, MF_STRING, ID_SORT_RTITLE,
197                 _T("&Reverse sort by title") );
198     AppendMenu( hmenu_sort, MF_SEPARATOR, 0, 0 );
199     AppendMenu( hmenu_sort, MF_STRING, ID_SORT_AUTHOR,
200                 _T("Sort by &author") );
201     AppendMenu( hmenu_sort, MF_STRING, ID_SORT_RAUTHOR,
202                 _T("Reverse sort by &author") );
203     AppendMenu( hmenu_sort, MF_SEPARATOR, 0, 0 );
204     AppendMenu( hmenu_sort, MF_STRING, ID_SORT_SHUFFLE,
205                 _T("&Shuffle Playlist") );
206
207     AppendMenu( hmenu_sel, MF_STRING, ID_SEL_ENABLE,
208                 _T("&Enable") );
209     AppendMenu( hmenu_sel, MF_STRING, ID_SEL_DISABLE,
210                 _T("&Disable") );
211     AppendMenu( hmenu_sel, MF_SEPARATOR, 0, 0 );
212     AppendMenu( hmenu_sel, MF_STRING, ID_SEL_INVERT,
213                 _T("&Invert") );
214     AppendMenu( hmenu_sel, MF_STRING, ID_SEL_DELETE,
215                 _T("D&elete") );
216     AppendMenu( hmenu_sel, MF_SEPARATOR, 0, 0 );
217     AppendMenu( hmenu_sel, MF_STRING, ID_SEL_SELECTALL,
218                 _T("&Select All") );
219
220
221 #ifdef UNDER_CE
222     return mbi.hwndMB;
223
224 #else
225     HMENU hmenu = CreateMenu();
226
227     AppendMenu( hmenu, MF_POPUP|MF_STRING, (UINT)hmenu_file, _T("Manage") );
228     AppendMenu( hmenu, MF_POPUP|MF_STRING, (UINT)hmenu_sort, _T("Sort") );
229     AppendMenu( hmenu, MF_POPUP|MF_STRING, (UINT)hmenu_sel, _T("Selection") );
230
231     SetMenu( hwnd, hmenu );
232     return hwnd;
233
234 #endif
235 }
236
237 /***********************************************************************
238 FUNCTION: 
239   WndProc
240
241 PURPOSE: 
242   Processes messages sent to the main window.
243 ***********************************************************************/
244 LRESULT Playlist::WndProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
245 {
246     SHINITDLGINFO shidi;
247     SHMENUBARINFO mbi;
248     INITCOMMONCONTROLSEX iccex;
249     RECT rect, rectTB;
250     DWORD dwStyle;
251
252     int bState;
253     playlist_t *p_playlist;
254
255     switch( msg )
256     {
257     case WM_INITDIALOG: 
258         shidi.dwMask = SHIDIM_FLAGS;
259         shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN |
260             SHIDIF_FULLSCREENNOMENUBAR;//SHIDIF_SIZEDLGFULLSCREEN;
261         shidi.hDlg = hwnd;
262         SHInitDialog( &shidi );
263
264         hwndCB = CreateMenuBar( hwnd, hInst );
265
266         iccex.dwSize = sizeof (INITCOMMONCONTROLSEX);
267         iccex.dwICC = ICC_BAR_CLASSES;
268
269         // Registers TOOLBAR control classes from the common control dll
270         InitCommonControlsEx (&iccex);
271
272         //  Create the toolbar control.
273         dwStyle = WS_VISIBLE | WS_CHILD | TBSTYLE_TOOLTIPS |
274             WS_EX_OVERLAPPEDWINDOW | CCS_NOPARENTALIGN;
275
276         hwndTB = CreateToolbarEx( hwnd, dwStyle, 0, NUMIMAGES,
277                                   hInst, IDB_BITMAP3, tbButton2,
278                                   sizeof (tbButton2) / sizeof (TBBUTTON),
279                                   BUTTONWIDTH, BUTTONHEIGHT,
280                                   IMAGEWIDTH, IMAGEHEIGHT, sizeof(TBBUTTON) );
281         if( !hwndTB ) break;
282   
283         // Add ToolTips to the toolbar.
284         SendMessage( hwndTB, TB_SETTOOLTIPS, (WPARAM) NUMIMAGES,
285                      (LPARAM)szToolTips2 );
286
287         // Reposition the toolbar.
288         GetClientRect( hwnd, &rect );
289         GetWindowRect( hwndTB, &rectTB );
290         MoveWindow( hwndTB, rect.left, rect.top - 2, rect.right - rect.left, 
291                     MENU_HEIGHT /*rectTB.bottom - rectTB.top */, TRUE);
292
293         // random, loop, repeat buttons states
294         vlc_value_t val; 
295         p_playlist = (playlist_t *)
296             vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
297         if( !p_playlist ) break;
298
299         var_Get( p_playlist , "random", &val );
300         bState = val.b_bool ? TBSTATE_CHECKED : 0;
301         SendMessage( hwndTB, TB_SETSTATE, Random_Event,
302                      MAKELONG(bState | TBSTATE_ENABLED, 0) );
303         var_Get( p_playlist , "loop", &val );
304         bState = val.b_bool ? TBSTATE_CHECKED : 0;
305         SendMessage( hwndTB, TB_SETSTATE, Loop_Event,
306                      MAKELONG(bState | TBSTATE_ENABLED, 0) );
307         var_Get( p_playlist , "repeat", &val );
308         bState = val.b_bool ? TBSTATE_CHECKED : 0;
309         SendMessage( hwndTB, TB_SETSTATE, Repeat_Event,
310                      MAKELONG(bState | TBSTATE_ENABLED, 0) );
311         vlc_object_release( p_playlist );
312
313         GetClientRect( hwnd, &rect );
314         hListView = CreateWindow( WC_LISTVIEW, NULL, WS_VISIBLE | WS_CHILD |
315             LVS_REPORT | LVS_SHOWSELALWAYS | WS_VSCROLL | WS_HSCROLL,
316             rect.left, rect.top + 2*(MENU_HEIGHT+1), rect.right - rect.left, 
317             rect.bottom - ( rect.top + 2*MENU_HEIGHT) - MENU_HEIGHT, 
318             hwnd, NULL, hInst, NULL );
319         ListView_SetExtendedListViewStyle( hListView, LVS_EX_FULLROWSELECT );
320
321         LVCOLUMN lv;
322         lv.mask = LVCF_WIDTH | LVCF_FMT | LVCF_TEXT;
323         lv.fmt = LVCFMT_LEFT ;
324         GetClientRect( hwnd, &rect );
325         lv.cx = 120;
326         lv.pszText = _T("Name");
327         lv.cchTextMax = 9;
328         ListView_InsertColumn( hListView, 0, &lv);
329         lv.cx = 55;
330         lv.pszText = _T("Author");
331         lv.cchTextMax = 9;
332         ListView_InsertColumn( hListView, 1, &lv);
333         lv.cx = rect.right - rect.left - 180;
334         lv.pszText = _T("Duration");
335         lv.cchTextMax = 9;
336         ListView_InsertColumn( hListView, 2, &lv);
337
338         SetTimer( hwnd, 1, 500 /*milliseconds*/, NULL );
339
340         SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
341         break;
342
343     case WM_TIMER:
344         UpdatePlaylist();
345         break;
346
347     case WM_CLOSE:
348         EndDialog( hwnd, LOWORD( wp ) );
349         break;
350
351     case WM_COMMAND:    
352         switch( LOWORD(wp) )
353         {
354         case IDOK:
355             EndDialog( hwnd, LOWORD( wp ) );
356             break;
357
358         case ID_MANAGE_OPENPL:
359             OnOpen();
360             b_need_update = VLC_TRUE;
361             break;
362
363         case ID_MANAGE_SAVEPL:
364             SHFullScreen( GetForegroundWindow(), SHFS_SHOWSIPBUTTON );
365             OnSave();
366             SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
367             break;
368
369         case ID_MANAGE_ADDFILE:
370             SHFullScreen( GetForegroundWindow(), SHFS_SHOWSIPBUTTON );
371             OnAddFile();
372             SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
373             b_need_update = VLC_TRUE;
374             break;
375
376         case ID_MANAGE_ADDDIRECTORY:
377             SHFullScreen( GetForegroundWindow(), SHFS_SHOWSIPBUTTON );
378             OnAddFile();
379             SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
380             b_need_update = VLC_TRUE;
381             break;
382
383         case ID_MANAGE_ADDMRL:
384             SHFullScreen( GetForegroundWindow(), SHFS_SHOWSIPBUTTON );
385             OnAddMRL();
386             SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
387             b_need_update = VLC_TRUE;
388             break;
389
390         case ID_SEL_DELETE:
391             OnDeleteSelection();
392             b_need_update = VLC_TRUE;
393             break;
394
395         case Infos_Event:
396             SHFullScreen( GetForegroundWindow(), SHFS_SHOWSIPBUTTON );
397             OnPopupInfo( hwnd );
398             SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
399             b_need_update = VLC_TRUE;
400             break;
401
402         case Up_Event:
403             OnUp();
404             b_need_update = VLC_TRUE;
405             break;
406
407         case Down_Event:
408             OnDown();
409             b_need_update = VLC_TRUE;
410             break;
411
412         case Random_Event:
413             OnRandom();
414             break;
415
416         case Loop_Event:
417             OnLoop();
418             break;
419
420         case Repeat_Event:
421             OnRepeat();
422             break;
423
424         case ID_SORT_TITLE:
425             OnSort( ID_SORT_TITLE );
426             break;
427
428         case ID_SORT_RTITLE:
429             OnSort( ID_SORT_RTITLE );
430             break;
431
432         case ID_SORT_AUTHOR:
433             OnSort( ID_SORT_AUTHOR );
434             break;
435
436         case ID_SORT_RAUTHOR:
437             OnSort( ID_SORT_RAUTHOR );
438             break;
439
440         case ID_SORT_SHUFFLE:
441             OnSort( ID_SORT_SHUFFLE );
442             break;
443
444         case ID_SEL_ENABLE:
445             OnEnableSelection();
446             break;
447
448         case ID_SEL_DISABLE:
449             OnDisableSelection();
450             break;
451
452         case ID_SEL_INVERT:
453             OnInvertSelection();
454             break;
455
456         case ID_SEL_SELECTALL:
457             OnSelectAll();
458             break;
459
460         case PopupPlay_Event:
461             OnPopupPlay();
462             b_need_update = VLC_TRUE;
463             break;
464
465         case PopupDel_Event:
466             OnPopupDel();
467             b_need_update = VLC_TRUE;
468             break;
469
470         case PopupEna_Event:
471             OnPopupEna();
472             b_need_update = VLC_TRUE;
473             break;
474
475         case PopupInfo_Event:
476             OnPopupInfo( hwnd );
477             SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
478             b_need_update = VLC_TRUE;
479             break;
480
481         default:
482             break;
483         }
484         break;
485
486     case WM_NOTIFY:
487         if( ( ((LPNMHDR)lp)->hwndFrom == hListView ) &&
488             ( ((LPNMHDR)lp)->code == NM_CUSTOMDRAW ) )
489         {
490             SetWindowLong( hwnd, DWL_MSGRESULT,
491                            (LONG)ProcessCustomDraw(lp) );
492         }
493         else if( ( ((LPNMHDR)lp)->hwndFrom == hListView ) &&
494                  ( ((LPNMHDR)lp)->code == GN_CONTEXTMENU  ) )
495         {                       
496             HandlePopupMenu( hwnd, ((PNMRGINFO)lp)->ptAction );
497         }
498         else if( ( ((LPNMHDR)lp)->hwndFrom == hListView ) &&
499                  ( ((LPNMHDR)lp)->code == LVN_COLUMNCLICK  ) )
500         {
501             OnColSelect( ((LPNMLISTVIEW)lp)->iSubItem );
502         }
503         else if( ( ((LPNMHDR)lp)->hwndFrom == hListView ) &&
504                  ( ((LPNMHDR)lp)->code == LVN_ITEMACTIVATE  ) )
505         {
506             OnActivateItem( ((LPNMLISTVIEW)lp)->iSubItem );
507         }
508         break;
509
510     default:
511          // the message was not processed
512          // indicate if the base class handled it
513         break;
514     }
515
516     return FALSE;
517 }
518
519 LRESULT Playlist::ProcessCustomDraw( LPARAM lParam )
520 {
521     LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
522
523     switch( lplvcd->nmcd.dwDrawStage )
524     {
525     case CDDS_PREPAINT : //Before the paint cycle begins
526         //request notifications for individual listview items
527         return CDRF_NOTIFYITEMDRAW;
528
529     case CDDS_ITEMPREPAINT: //Before an item is drawn
530         playlist_t *p_playlist = (playlist_t *)
531             vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
532         if( p_playlist == NULL ) return CDRF_DODEFAULT;
533         if( (int)lplvcd->nmcd.dwItemSpec == p_playlist->i_index )
534         {
535             lplvcd->clrText = RGB(255,0,0);
536             vlc_object_release(p_playlist);
537             return CDRF_NEWFONT;
538         }
539         
540         playlist_item_t *p_item = playlist_ItemGetByPos( p_playlist,
541                                         (int)lplvcd->nmcd.dwItemSpec );
542         if( !p_item )
543         {
544             vlc_object_release(p_playlist);
545             return CDRF_DODEFAULT;
546         }
547         if( p_item->b_enabled == VLC_FALSE )
548         {
549             lplvcd->clrText = RGB(192,192,192);
550             vlc_object_release(p_playlist);
551             return CDRF_NEWFONT;
552         }
553     }
554
555     return CDRF_DODEFAULT;
556 }
557
558 /**********************************************************************
559  * Handles the display of the "floating" popup
560  **********************************************************************/
561 void Playlist::HandlePopupMenu( HWND hwnd, POINT point )
562 {
563     HMENU hMenuTrackPopup;
564
565     // Create the popup menu.
566     hMenuTrackPopup = CreatePopupMenu();
567
568     // Append some items.
569     AppendMenu( hMenuTrackPopup, MF_STRING, PopupPlay_Event, _T("Play") );
570     AppendMenu( hMenuTrackPopup, MF_STRING, PopupDel_Event, _T("Delete") );
571     AppendMenu( hMenuTrackPopup, MF_STRING, PopupEna_Event,
572                 _T("Toggle enabled") );
573     AppendMenu( hMenuTrackPopup, MF_STRING, PopupInfo_Event, _T("Info") );
574
575     /* Draw and track the "floating" popup */
576     TrackPopupMenu( hMenuTrackPopup, 0, point.x, point.y, 0, hwnd, NULL );
577
578     /* Destroy the menu since were are done with it. */
579     DestroyMenu( hMenuTrackPopup );
580 }
581
582 /**********************************************************************
583  * Update the playlist
584  **********************************************************************/
585 void Playlist::UpdatePlaylist()
586 {
587     if( b_need_update )
588     {
589         Rebuild();
590         b_need_update = VLC_FALSE;
591     }
592         
593     playlist_t *p_playlist = (playlist_t *)
594         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
595     if( p_playlist == NULL ) return;
596         
597     /* Update the colour of items */
598
599     vlc_mutex_lock( &p_playlist->object_lock );
600     if( p_intf->p_sys->i_playing != p_playlist->i_index )
601     {
602         // p_playlist->i_index in RED
603         Rebuild();
604
605         // if exists, p_intf->p_sys->i_playing in BLACK
606         p_intf->p_sys->i_playing = p_playlist->i_index;
607     }
608     vlc_mutex_unlock( &p_playlist->object_lock );
609
610     vlc_object_release( p_playlist );
611 }
612
613 /**********************************************************************
614  * Rebuild the playlist
615  **********************************************************************/
616 void Playlist::Rebuild()
617 {
618     playlist_t *p_playlist = (playlist_t *)
619         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
620     if( p_playlist == NULL ) return;
621
622     int i_focused =
623         ListView_GetNextItem( hListView, -1, LVIS_SELECTED | LVNI_ALL );
624
625     /* Clear the list... */
626     ListView_DeleteAllItems( hListView );
627
628     /* ...and rebuild it */
629     vlc_mutex_lock( &p_playlist->object_lock );
630     for( int i = 0; i < p_playlist->i_size; i++ )
631     {
632         LVITEM lv;
633         lv.mask = LVIF_TEXT;
634         lv.pszText = _T("");
635         lv.cchTextMax = 1;
636         lv.iSubItem = 0;
637         lv.iItem = i;
638         ListView_InsertItem( hListView, &lv );
639         ListView_SetItemText( hListView, lv.iItem, 0,
640             _FROMMB(p_playlist->pp_items[i]->input.psz_name) );
641         UpdateItem( i );
642     }
643     vlc_mutex_unlock( &p_playlist->object_lock );
644
645     if ( i_focused )
646         ListView_SetItemState( hListView, i_focused, LVIS_FOCUSED |
647                                LVIS_SELECTED, LVIS_STATEIMAGEMASK )
648     else
649         ListView_SetItemState( hListView, i_focused, LVIS_FOCUSED,
650                                LVIS_STATEIMAGEMASK );
651
652     vlc_object_release( p_playlist );
653 }
654
655 /**********************************************************************
656  * Update one playlist item
657  **********************************************************************/
658 void Playlist::UpdateItem( int i )
659 {
660     playlist_t *p_playlist = (playlist_t *)
661         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
662
663     if( p_playlist == NULL ) return;
664
665     playlist_item_t *p_item = playlist_ItemGetByPos( p_playlist, i );
666
667     if( !p_item )
668     {
669         vlc_object_release(p_playlist);
670         return;
671     }
672
673     ListView_SetItemText( hListView, i, 0, _FROMMB(p_item->input.psz_name) );
674     ListView_SetItemText( hListView, i, 1,
675                           _FROMMB( vlc_input_item_GetInfo( &p_item->input,
676                                    _("General") , _("Author") ) ) );
677
678     char psz_duration[MSTRTIME_MAX_SIZE];
679     mtime_t dur = p_item->input.i_duration;
680     if( dur != -1 ) secstotimestr( psz_duration, dur/1000000 );
681     else memcpy( psz_duration , "-:--:--", sizeof("-:--:--") );
682
683     ListView_SetItemText( hListView, i, 3, _FROMMB(psz_duration) );
684
685     vlc_object_release(p_playlist);
686 }
687
688 /**********************************************************************
689  * Private functions
690  **********************************************************************/
691 void Playlist::DeleteItem( int item )
692 {
693     playlist_t *p_playlist = (playlist_t *)
694         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
695     if( p_playlist == NULL ) return;
696
697     playlist_Delete( p_playlist, item );
698     ListView_DeleteItem( hListView, item );
699
700     vlc_object_release( p_playlist );
701 }
702
703 /**********************************************************************
704  * I/O functions
705  **********************************************************************/
706 void Playlist::OnOpen()
707 {
708     OPENFILENAME ofn;
709     TCHAR DateiName[80+1] = _T("\0");
710     static TCHAR szFilter[] = _T("All playlists (*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u)\0*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u\0");
711
712     playlist_t *p_playlist = (playlist_t *)
713         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
714     if( p_playlist == NULL ) return;
715
716     memset( &ofn, 0, sizeof(OPENFILENAME) );
717     ofn.lStructSize = sizeof (OPENFILENAME);
718     ofn.hwndOwner = NULL;
719     ofn.hInstance = hInst;
720     ofn.lpstrFilter = szFilter;
721     ofn.lpstrCustomFilter = NULL;
722     ofn.nMaxCustFilter = 0;
723     ofn.nFilterIndex = 1;     
724     ofn.lpstrFile = (LPTSTR) DateiName; 
725     ofn.nMaxFile = 80;
726     ofn.lpstrFileTitle = NULL; 
727     ofn.nMaxFileTitle = 40;
728     ofn.lpstrInitialDir = NULL;
729     ofn.lpstrTitle = _T("Open playlist");
730     ofn.Flags = 0; 
731     ofn.nFileOffset = 0;
732     ofn.nFileExtension = 0;
733     ofn.lpstrDefExt = NULL;
734     ofn.lCustData = 0L;
735     ofn.lpfnHook = NULL;
736     ofn.lpTemplateName = NULL;
737
738     if( GetOpenFileName((LPOPENFILENAME)&ofn) )
739     {
740         playlist_Import( p_playlist, _TOMB(ofn.lpstrFile) );
741     }
742
743     vlc_object_release( p_playlist );
744 }
745
746 void Playlist::OnSave()
747 {
748     TCHAR szFile[NMAXFILE] = _T("\0");
749     OPENFILENAME ofn;
750     TCHAR psz_filters[1000];
751
752     struct
753     {
754         char *psz_desc;
755         char *psz_filter;
756         char *psz_module;
757
758     } formats[] =
759     { { "M3U file", "*.m3u", "export-m3u" },       
760       { "PLS file", "*.pls", "export-pls" }
761     };
762
763     for( int i_len = 0, i = 0; i < sizeof(formats)/sizeof(formats[0]); i++ )
764     {
765         _tcscpy( psz_filters + i_len, _FROMMB(formats[i].psz_desc) );
766         i_len = i_len + _tcslen( psz_filters + i_len );
767         psz_filters[i_len++] = '\0';
768         _tcscpy( psz_filters + i_len, _FROMMB(formats[i].psz_filter) );
769         i_len = i_len + _tcslen( psz_filters + i_len );
770         psz_filters[i_len++] = '\0';
771         if( i == sizeof(formats)/sizeof(formats[0]) -1 )
772             psz_filters[i_len] = '\0';
773     }
774
775     memset( &(ofn), 0, sizeof(ofn));
776     ofn.lStructSize     = sizeof(ofn);
777     ofn.hwndOwner = NULL;
778     ofn.lpstrFile = szFile;
779     ofn.nMaxFile = NMAXFILE;    
780     ofn.lpstrFilter = psz_filters;
781     ofn.lpstrTitle = _T("Save playlist");
782     ofn.Flags = OFN_HIDEREADONLY; 
783
784     if( GetSaveFileName( (LPOPENFILENAME)&ofn ) )
785     {
786         playlist_t * p_playlist = (playlist_t *)
787             vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
788
789         if( p_playlist && ofn.lpstrFile )
790         {
791             playlist_Export( p_playlist, _TOMB(ofn.lpstrFile),
792                              formats[ofn.nFilterIndex ?
793                                      ofn.nFilterIndex - 1 : 0].psz_module );
794         }
795
796         if( p_playlist ) vlc_object_release( p_playlist );
797     }
798 }
799
800 void Playlist::OnAddFile()
801 {
802     // Same code as in Interface
803     OPENFILENAME ofn;
804     TCHAR DateiName[80+1] = _T("\0");
805     static TCHAR szFilter[] = _T("All (*.*)\0*.*\0");
806
807     memset( &ofn, 0, sizeof(OPENFILENAME) );
808     ofn.lStructSize = sizeof(OPENFILENAME);
809     ofn.hwndOwner = NULL;
810     ofn.hInstance = hInst;
811     ofn.lpstrFilter = szFilter;
812     ofn.lpstrCustomFilter = NULL;
813     ofn.nMaxCustFilter = 0;
814     ofn.nFilterIndex = 1;     
815     ofn.lpstrFile = (LPTSTR)DateiName; 
816     ofn.nMaxFile = 80;
817     ofn.lpstrFileTitle = NULL; 
818     ofn.nMaxFileTitle = 40;
819     ofn.lpstrInitialDir = NULL;
820     ofn.lpstrTitle = _T("Add File");
821     ofn.Flags = 0; 
822     ofn.nFileOffset = 0;
823     ofn.nFileExtension = 0;
824     ofn.lpstrDefExt = NULL;
825     ofn.lCustData = 0L;
826     ofn.lpfnHook = NULL;
827     ofn.lpTemplateName = NULL;
828
829     SHFullScreen( GetForegroundWindow(), SHFS_HIDESIPBUTTON );
830
831     if( GetOpenFileName( (LPOPENFILENAME)&ofn ) )
832     {
833         playlist_t *p_playlist = (playlist_t *)
834             vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
835         if( p_playlist == NULL ) return;
836
837         char *psz_filename = _TOMB(ofn.lpstrFile);
838         playlist_Add( p_playlist, psz_filename, psz_filename,
839                       PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
840         vlc_object_release( p_playlist );
841     }
842 }
843
844 void Playlist::OnAddMRL()
845 {
846 }
847
848 /**********************************************************************
849  * Selection functions
850  **********************************************************************/
851 void Playlist::OnDeleteSelection()
852 {
853     /* Delete from the end to the beginning, to avoid a shift of indices */
854     for( long item = ((int) ListView_GetItemCount( hListView ) - 1);
855          item >= 0; item-- )
856     {
857         if( ListView_GetItemState( hListView, item, LVIS_SELECTED ) )
858         {
859             DeleteItem( item );
860         }
861     }
862 }
863
864 void Playlist::OnInvertSelection()
865 {
866     UINT iState;
867
868     for( long item = 0; item < ListView_GetItemCount( hListView ); item++ )
869     {
870         iState = ListView_GetItemState( hListView, item, LVIS_STATEIMAGEMASK );
871         ListView_SetItemState( hListView, item, iState ^ LVIS_SELECTED,
872                                LVIS_STATEIMAGEMASK );
873     }
874 }
875
876 void Playlist::OnEnableSelection()
877 {
878     playlist_t *p_playlist = (playlist_t *)
879         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
880     if( p_playlist == NULL ) return;
881
882     for( long item = ListView_GetItemCount( hListView ) - 1;
883          item >= 0; item-- )
884     {
885         if( ListView_GetItemState( hListView, item, LVIS_SELECTED ) )
886         {
887             playlist_item_t *p_item =
888                 playlist_ItemGetByPos( p_playlist, item );
889             playlist_Enable( p_playlist, p_item );
890             UpdateItem( item );
891         }
892     }
893     vlc_object_release( p_playlist);
894 }
895
896 void Playlist::OnDisableSelection()
897 {
898     playlist_t *p_playlist = (playlist_t *)
899         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
900     if( p_playlist == NULL ) return;
901
902     for( long item = ListView_GetItemCount( hListView ) - 1;
903          item >= 0; item-- )
904     {
905         if( ListView_GetItemState( hListView, item, LVIS_SELECTED ) )
906         {
907             /*XXX*/
908             playlist_item_t *p_item =
909                 playlist_ItemGetByPos( p_playlist, item );
910             playlist_Disable( p_playlist, p_item );
911             UpdateItem( item );
912         }
913     }
914     vlc_object_release( p_playlist);
915 }
916
917 void Playlist::OnSelectAll()
918 {
919     for( long item = 0; item < ListView_GetItemCount( hListView ); item++ )
920     {
921         ListView_SetItemState( hListView, item, LVIS_FOCUSED | LVIS_SELECTED,
922                                LVIS_STATEIMAGEMASK );
923     }
924 }
925
926 void Playlist::OnActivateItem( int i_item )
927 {
928     playlist_t *p_playlist = (playlist_t *)
929         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
930     if( p_playlist == NULL ) return;
931
932     playlist_Goto( p_playlist, i_item );
933
934     vlc_object_release( p_playlist );
935 }
936
937 void Playlist::ShowInfos( HWND hwnd, int i_item )
938 {
939     playlist_t *p_playlist = (playlist_t *)
940         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
941     if( p_playlist == NULL ) return;
942
943     vlc_mutex_lock( &p_playlist->object_lock);
944     playlist_item_t *p_item = playlist_ItemGetByPos( p_playlist, i_item );
945     vlc_mutex_unlock( &p_playlist->object_lock );
946
947     if( p_item )
948     {
949         ItemInfoDialog *iteminfo_dialog =
950             new ItemInfoDialog( p_intf, hInst, p_item );
951         CreateDialogBox( hwnd, iteminfo_dialog );                
952         UpdateItem( i_item );
953         delete iteminfo_dialog;
954     }
955
956     vlc_object_release( p_playlist );
957 }
958
959 /********************************************************************
960  * Move functions
961  ********************************************************************/
962 void Playlist::OnUp()
963 {
964     playlist_t *p_playlist = (playlist_t *)
965         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
966     if( p_playlist == NULL ) return;
967
968     /* We use the first selected item, so find it */
969     long i_item =
970         ListView_GetNextItem( hListView, -1, LVIS_SELECTED | LVNI_ALL );
971
972     if( i_item > 0 && i_item < p_playlist->i_size )
973     {
974         playlist_Move( p_playlist , i_item, i_item - 1);
975         if( i_item > 1 )
976         {
977             ListView_SetItemState( hListView, i_item - 1, LVIS_FOCUSED,
978                                    LVIS_STATEIMAGEMASK );
979         }
980         else
981         {
982             ListView_SetItemState( hListView, 0, LVIS_FOCUSED,
983                                    LVIS_STATEIMAGEMASK );
984         }
985     }
986     vlc_object_release( p_playlist );
987
988     return;
989 }
990
991 void Playlist::OnDown()
992 {
993     playlist_t *p_playlist = (playlist_t *)
994         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
995     if( p_playlist == NULL ) return;
996
997     /* We use the first selected item, so find it */
998     long i_item =
999         ListView_GetNextItem( hListView, -1, LVIS_SELECTED | LVNI_ALL );
1000
1001     if( i_item >= 0 && i_item < p_playlist->i_size - 1 )
1002     {
1003         playlist_Move( p_playlist , i_item, i_item + 2 );
1004         ListView_SetItemState( hListView, i_item + 1, LVIS_FOCUSED,
1005                                LVIS_STATEIMAGEMASK );
1006     }
1007     vlc_object_release( p_playlist );
1008
1009     return;
1010 }
1011
1012 /**********************************************************************
1013  * Playlist mode functions
1014  **********************************************************************/
1015 void Playlist::OnRandom()
1016 {
1017     vlc_value_t val;
1018     int bState = SendMessage( hwndTB, TB_GETSTATE, Random_Event, 0 ); 
1019     val.b_bool = (bState & TBSTATE_CHECKED) ? VLC_TRUE : VLC_FALSE;
1020
1021     playlist_t *p_playlist = (playlist_t *)
1022         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1023     if( p_playlist == NULL ) return;
1024
1025     var_Set( p_playlist , "random", val );
1026     vlc_object_release( p_playlist );
1027 }
1028
1029 void Playlist::OnLoop ()
1030 {
1031     vlc_value_t val;
1032     int bState = SendMessage( hwndTB, TB_GETSTATE, Loop_Event, 0 ); 
1033     val.b_bool = (bState & TBSTATE_CHECKED) ? VLC_TRUE : VLC_FALSE;
1034
1035     playlist_t *p_playlist = (playlist_t *)
1036         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1037     if( p_playlist == NULL ) return;
1038
1039     var_Set( p_playlist , "loop", val );
1040     vlc_object_release( p_playlist );
1041 }
1042
1043 void Playlist::OnRepeat ()
1044 {
1045     vlc_value_t val;
1046     int bState = SendMessage( hwndTB, TB_GETSTATE, Repeat_Event, 0 );  
1047     val.b_bool = (bState & TBSTATE_CHECKED) ? VLC_TRUE : VLC_FALSE;
1048
1049     playlist_t *p_playlist = (playlist_t *)
1050         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1051     if( p_playlist == NULL ) return;
1052
1053     var_Set( p_playlist , "repeat", val );
1054     vlc_object_release( p_playlist );
1055 }
1056
1057 /********************************************************************
1058  * Sorting functions
1059  ********************************************************************/
1060 void Playlist::OnSort( UINT event )
1061 {
1062     playlist_t *p_playlist = (playlist_t *)
1063         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1064     if( p_playlist == NULL ) return;
1065
1066     switch( event )
1067     {
1068     case ID_SORT_TITLE:
1069         playlist_SortTitle( p_playlist , ORDER_NORMAL );
1070         break;
1071     case ID_SORT_RTITLE:
1072         playlist_SortTitle( p_playlist , ORDER_REVERSE );
1073         break;
1074     case ID_SORT_AUTHOR:
1075         playlist_SortAuthor(p_playlist , ORDER_NORMAL );
1076         break;
1077     case ID_SORT_RAUTHOR:
1078         playlist_SortAuthor( p_playlist , ORDER_REVERSE );
1079         break;
1080     case ID_SORT_SHUFFLE:
1081         playlist_Sort( p_playlist , SORT_RANDOM, ORDER_NORMAL );
1082         break;
1083     }
1084
1085     vlc_object_release( p_playlist );
1086
1087     b_need_update = VLC_TRUE;
1088
1089     return;
1090 }
1091
1092 void Playlist::OnColSelect( int iSubItem )
1093 {
1094     playlist_t *p_playlist = (playlist_t *)
1095         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1096     if( p_playlist == NULL ) return;
1097
1098     switch( iSubItem )
1099     {
1100     case 0:
1101         if( i_title_sorted != 1 )
1102         {
1103             playlist_SortTitle( p_playlist, ORDER_NORMAL );
1104             i_title_sorted = 1;
1105         }
1106         else
1107         {
1108             playlist_SortTitle( p_playlist, ORDER_REVERSE );
1109             i_title_sorted = -1;
1110         }
1111         break;
1112     case 1:
1113         if( i_author_sorted != 1 )
1114         {
1115             playlist_SortAuthor( p_playlist, ORDER_NORMAL );
1116             i_author_sorted = 1;
1117         }
1118         else
1119         {
1120             playlist_SortAuthor( p_playlist, ORDER_REVERSE );
1121             i_author_sorted = -1;
1122         }
1123         break;
1124     default:
1125         break;
1126     }
1127
1128     vlc_object_release( p_playlist );
1129
1130     b_need_update = VLC_TRUE;
1131
1132     return;
1133 }
1134
1135 /*****************************************************************************
1136  * Popup management functions
1137  *****************************************************************************/
1138 void Playlist::OnPopupPlay()
1139 {
1140     int i_popup_item =
1141         ListView_GetNextItem( hListView, -1, LVIS_SELECTED | LVNI_ALL );
1142
1143     playlist_t *p_playlist = (playlist_t *)
1144         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1145     if( p_playlist == NULL ) return;
1146
1147     if( i_popup_item != -1 )
1148     {
1149         playlist_Goto( p_playlist, i_popup_item );
1150     }
1151
1152     vlc_object_release( p_playlist );
1153 }
1154
1155 void Playlist::OnPopupDel()
1156 {
1157     int i_popup_item =
1158         ListView_GetNextItem( hListView, -1, LVIS_SELECTED | LVNI_ALL );
1159
1160     DeleteItem( i_popup_item );
1161 }
1162
1163 void Playlist::OnPopupEna()
1164 {
1165     int i_popup_item =
1166         ListView_GetNextItem( hListView, -1, LVIS_SELECTED | LVNI_ALL );
1167
1168     playlist_t *p_playlist = (playlist_t *)
1169         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1170     if( p_playlist == NULL ) return;
1171
1172     playlist_item_t *p_item =
1173         playlist_ItemGetByPos( p_playlist, i_popup_item );
1174
1175     if( p_playlist->pp_items[i_popup_item]->b_enabled )
1176         //playlist_IsEnabled( p_playlist, i_popup_item ) )
1177     {
1178         playlist_Disable( p_playlist, p_item );
1179     }
1180     else
1181     {
1182         playlist_Enable( p_playlist, p_item );
1183     }
1184
1185     vlc_object_release( p_playlist);
1186     UpdateItem( i_popup_item );
1187 }
1188
1189 void Playlist::OnPopupInfo( HWND hwnd )
1190 {
1191     int i_popup_item =
1192         ListView_GetNextItem( hListView, -1, LVIS_SELECTED | LVNI_ALL );
1193
1194     ShowInfos( hwnd, i_popup_item );
1195 }