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