]> git.sesse.net Git - vlc/blobdiff - modules/gui/wxwindows/playlist.cpp
Sanity check
[vlc] / modules / gui / wxwindows / playlist.cpp
index be0a9797f7d71bb2ba8cd646c31797f15a33cd3c..c428b33f338b1e088bd4de30743235747644785b 100644 (file)
@@ -2,7 +2,7 @@
  * playlist.cpp : wxWindows plugin for vlc
  *****************************************************************************
  * Copyright (C) 2000-2004 VideoLAN
- * $Id: playlist.cpp,v 1.42 2004/02/23 12:17:24 gbazin Exp $
+ * $Id: playlist.cpp,v 1.49 2004/02/29 14:05:45 zorglub Exp $
  *
  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
  *
@@ -171,14 +171,14 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
     wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
              wxDefaultSize, wxDEFAULT_FRAME_STYLE )
 {
+    vlc_value_t val;
+
     /* Initializations */
     iteminfo_dialog = NULL;
     p_intf = _p_intf;
-    vlc_value_t  val;
     i_update_counter = 0;
     i_sort_mode = MODE_NONE;
     b_need_update = VLC_FALSE;
-    vlc_mutex_init( p_intf, &lock );
     SetIcon( *p_intf->p_sys->p_icon );
 
     i_title_sorted = 0;
@@ -186,7 +186,6 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
     i_group_sorted = 0;
     i_duration_sorted = 0;
 
-
     var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
     var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
     var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
@@ -340,8 +339,10 @@ Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
     top_sizer->Layout();
 
     wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
-    bottom_sizer->Add( updown_sizer, 0, wxEXPAND |wxRIGHT | wxLEFT | wxALIGN_LEFT, 4);
-    bottom_sizer->Add( button_sizer , 0, wxEXPAND|wxLEFT | wxRIGHT | wxALIGN_RIGHT, 4 );
+    bottom_sizer->Add( updown_sizer, 0,
+                       wxEXPAND |wxRIGHT | wxLEFT | wxALIGN_LEFT, 4 );
+    bottom_sizer->Add( button_sizer , 0,
+                       wxEXPAND|wxLEFT | wxRIGHT | wxALIGN_RIGHT, 4 );
     bottom_sizer->Layout();
 
     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
@@ -411,6 +412,8 @@ Playlist::~Playlist()
  **********************************************************************/
 void Playlist::UpdateItem( int i )
 {
+    if( i < 0 ) return; /* Sanity check */
+
     playlist_t *p_playlist =
         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                        FIND_ANYWHERE );
@@ -494,12 +497,12 @@ void Playlist::Rebuild()
     }
     vlc_mutex_unlock( &p_playlist->object_lock );
 
-    if( i_focused )
+    if( i_focused >= 0 && i_focused < p_playlist->i_size )
     {
         listview->Focus( i_focused );
         listview->Select( i_focused );
     }
-    else
+    else if( p_playlist->i_index >= 0 )
     {
         listview->Focus( p_playlist->i_index );
     }
@@ -515,22 +518,14 @@ void Playlist::ShowPlaylist( bool show )
 
 void Playlist::UpdatePlaylist()
 {
-    vlc_bool_t b_need_update = VLC_FALSE;
     i_update_counter++;
 
     /* If the playlist isn't show there's no need to update it */
     if( !IsShown() ) return;
 
-    vlc_mutex_lock( &lock );
     if( this->b_need_update )
     {
-        b_need_update =VLC_TRUE;
         this->b_need_update = VLC_FALSE;
-    }
-    vlc_mutex_unlock( &lock );
-
-    if( b_need_update )
-    {
         Rebuild();
     }
 
@@ -546,12 +541,11 @@ void Playlist::UpdatePlaylist()
     }
 
     /* Update the colour of items */
-
-    vlc_mutex_lock( &p_playlist->object_lock );
-    if( p_intf->p_sys->i_playing != p_playlist->i_index )
+    int i_playlist_index = p_playlist->i_index;
+    if( p_intf->p_sys->i_playing != i_playlist_index )
     {
         wxListItem listitem;
-        listitem.m_itemId = p_playlist->i_index;
+        listitem.m_itemId = i_playlist_index;
         listitem.SetTextColour( *wxRED );
         listview->SetItem( listitem );
 
@@ -561,9 +555,9 @@ void Playlist::UpdatePlaylist()
             listitem.SetTextColour( *wxBLACK );
             listview->SetItem( listitem );
         }
-        p_intf->p_sys->i_playing = p_playlist->i_index;
+        p_intf->p_sys->i_playing = i_playlist_index;
     }
-    vlc_mutex_unlock( &p_playlist->object_lock );
+
     vlc_object_release( p_playlist );
 }
 
@@ -641,8 +635,8 @@ void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
         return;
     }
 
-    wxFileDialog dialog( this, wxU(_("Open playlist")),
-                         wxT(""), wxT(""), wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
+    wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
+        wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
 
     if( dialog.ShowModal() == wxID_OK )
     {
@@ -673,7 +667,7 @@ void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
 /********************************************************************
  * Move functions
  ********************************************************************/
-void Playlist::OnUp( wxCommandEvent& event)
+void Playlist::OnUp( wxCommandEvent& event )
 {
     playlist_t *p_playlist =
         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
@@ -684,25 +678,18 @@ void Playlist::OnUp( wxCommandEvent& event)
     }
 
     /* We use the first selected item, so find it */
-    long i_item = listview->GetNextItem( i_item, wxLIST_NEXT_ALL,
+    long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
                                          wxLIST_STATE_SELECTED);
     if( i_item > 0 && i_item < p_playlist->i_size )
     {
-        playlist_Move( p_playlist , i_item, i_item - 1);
-        if( i_item > 1 )
-        {
-            listview->Focus( i_item - 1 );
-        }
-        else
-        {
-            listview->Focus(0);
-        }
+        playlist_Move( p_playlist, i_item, i_item - 1 );
+        listview->Focus( i_item - 1 );
     }
     vlc_object_release( p_playlist );
     return;
 }
 
-void Playlist::OnDown( wxCommandEvent& event)
+void Playlist::OnDown( wxCommandEvent& event )
 {
     playlist_t *p_playlist =
         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
@@ -713,7 +700,7 @@ void Playlist::OnDown( wxCommandEvent& event)
     }
 
     /* We use the first selected item, so find it */
-    long i_item = listview->GetNextItem( i_item, wxLIST_NEXT_ALL,
+    long i_item = listview->GetNextItem( -1, wxLIST_NEXT_ALL,
                                          wxLIST_STATE_SELECTED );
     if( i_item >= 0 && i_item < p_playlist->i_size - 1 )
     {
@@ -846,24 +833,30 @@ void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
 
 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
 {
-    wxString search_string= search_text->GetValue();
+    wxString search_string = search_text->GetValue();
 
+    bool b_ok = false;
     int i_current;
     int i_first = 0 ;
     int i_item = -1;
 
-    for( i_current = 0 ; i_current <= listview->GetItemCount() ; i_current++ )
+    for( i_current = 0; i_current < listview->GetItemCount(); i_current++ )
     {
-        if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED )
-                   == wxLIST_STATE_SELECTED )
+        if( listview->GetItemState( i_current, wxLIST_STATE_SELECTED ) ==
+              wxLIST_STATE_SELECTED )
         {
             i_first = i_current;
             break;
         }
     }
 
-    for ( i_current = i_first + 1; i_current <= listview->GetItemCount() ;
-          i_current++ )
+    if( i_first == listview->GetItemCount() )
+    {
+        i_first = -1;
+    }
+
+    for( i_current = i_first + 1; i_current < listview->GetItemCount();
+         i_current++ )
     {
         wxListItem listitem;
         listitem.SetId( i_current );
@@ -871,9 +864,45 @@ void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
         if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
         {
             i_item = i_current;
+            b_ok = true;
             break;
         }
+        listitem.SetColumn( 1 );
+        listview->GetItem( listitem );
+        if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
+        {
+            i_item = i_current;
+            b_ok = true;
+            break;
+        }
+    }
+    if( !b_ok )
+    {
+        for( i_current = -1 ; i_current < i_first - 1;
+             i_current++ )
+        {
+            wxListItem listitem;
+            listitem.SetId( i_current );
+            listview->GetItem( listitem );
+            if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
+            {
+                i_item = i_current;
+                b_ok = true;
+                break;
+            }
+            listitem.SetColumn( 1 );
+            listview->GetItem( listitem );
+            if( listitem.m_text.Lower().Contains( search_string.Lower() ) )
+            {
+                i_item = i_current;
+                b_ok = true;
+                break;
+            }
+        }
     }
+
+    if( i_item < 0 || i_item >= listview->GetItemCount() ) return;
+
     for( long item = 0; item < listview->GetItemCount(); item++ )
     {
         listview->Select( item, FALSE );
@@ -1049,6 +1078,12 @@ void Playlist::ShowInfos( int i_item )
     {
         return;
     }
+   
+    if( i_item == -1 )
+    {
+        return;
+    }
     if( iteminfo_dialog == NULL )
     {
         vlc_mutex_lock( &p_playlist->object_lock);
@@ -1118,7 +1153,6 @@ void Playlist::OnPopup( wxListEvent& event )
     Playlist::PopupMenu( popup_menu , ScreenToClient( wxGetMousePosition() ) );
 }
 
-
 void Playlist::OnPopupPlay( wxMenuEvent& event )
 {
     playlist_t *p_playlist =
@@ -1190,9 +1224,7 @@ int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
                      vlc_value_t old_val, vlc_value_t new_val, void *param )
 {
     Playlist *p_playlist_dialog = (Playlist *)param;
-    vlc_mutex_lock( &p_playlist_dialog->lock );
     p_playlist_dialog->b_need_update = VLC_TRUE;
-    vlc_mutex_unlock( &p_playlist_dialog->lock );
     return VLC_SUCCESS;
 }