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