* ./README.MacOSX.rtf, ./modules/gui/macosx/playlist.h, ./extras/MacOSX/*:
[vlc.git] / modules / gui / wxwindows / interface.cpp
1 /*****************************************************************************
2  * interface.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: interface.cpp,v 1.15 2003/01/26 13:37:09 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44
45 #include <vlc/intf.h>
46 #include "stream_control.h"
47
48 #include "wxwindows.h"
49
50 /* include the toolbar graphics */
51 #include "bitmaps/file.xpm"
52 #include "bitmaps/disc.xpm"
53 #include "bitmaps/net.xpm"
54 #if 0
55 #include "bitmaps/sat.xpm"
56 #endif
57 #include "bitmaps/play.xpm"
58 #include "bitmaps/pause.xpm"
59 #include "bitmaps/stop.xpm"
60 #include "bitmaps/previous.xpm"
61 #include "bitmaps/next.xpm"
62 #include "bitmaps/playlist.xpm"
63 #define TOOLBAR_BMP_WIDTH 24
64 #define TOOLBAR_BMP_HEIGHT 24
65
66 /* include the icon graphic */
67 #include "share/vlc32x32.xpm"
68
69 /*****************************************************************************
70  * Local class declarations.
71  *****************************************************************************/
72
73 /*****************************************************************************
74  * Event Table.
75  *****************************************************************************/
76
77 /* IDs for the controls and the menu commands */
78 enum
79 {
80     /* menu items */
81     Exit_Event = wxID_HIGHEST,
82     OpenFile_Event,
83     OpenDisc_Event,
84     OpenNet_Event,
85     OpenSat_Event,
86     EjectDisc_Event,
87
88     Playlist_Event,
89     Logs_Event,
90     FileInfo_Event,
91
92     Audio_Event,
93     Subtitles_Event,
94     Prefs_Event,
95
96     SliderScroll_Event,
97     StopStream_Event,
98     PlayStream_Event,
99     PrevStream_Event,
100     NextStream_Event,
101
102     /* it is important for the id corresponding to the "About" command to have
103      * this standard value as otherwise it won't be handled properly under Mac
104      * (where it is special and put into the "Apple" menu) */
105     About_Event = wxID_ABOUT
106 };
107
108 BEGIN_EVENT_TABLE(Interface, wxFrame)
109     /* Menu events */
110     EVT_MENU(Exit_Event, Interface::OnExit)
111     EVT_MENU(About_Event, Interface::OnAbout)
112     EVT_MENU(Playlist_Event, Interface::OnPlaylist)
113     EVT_MENU(Logs_Event, Interface::OnLogs)
114     EVT_MENU(FileInfo_Event, Interface::OnFileInfo)
115
116     /* Toolbar events */
117     EVT_MENU(OpenFile_Event, Interface::OnOpenFile)
118     EVT_MENU(OpenDisc_Event, Interface::OnOpenDisc)
119     EVT_MENU(OpenNet_Event, Interface::OnOpenNet)
120     EVT_MENU(OpenSat_Event, Interface::OnOpenSat)
121     EVT_MENU(StopStream_Event, Interface::OnStopStream)
122     EVT_MENU(PlayStream_Event, Interface::OnPlayStream)
123     EVT_MENU(PrevStream_Event, Interface::OnPrevStream)
124     EVT_MENU(NextStream_Event, Interface::OnNextStream)
125
126     /* Slider events */
127     EVT_COMMAND_SCROLL(SliderScroll_Event, Interface::OnSliderUpdate)
128 END_EVENT_TABLE()
129
130 /*****************************************************************************
131  * Constructor.
132  *****************************************************************************/
133 Interface::Interface( intf_thread_t *_p_intf ):
134     wxFrame( NULL, -1, VOUT_TITLE,
135              wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE )
136 {
137     /* Initializations */
138     p_intf = _p_intf;
139     i_playing_status = PAUSE_S;
140
141     /* Give our interface a nice little icon */
142     SetIcon( *new wxIcon( vlc_xpm ) );
143
144     /* Create a sizer for the main frame */
145     frame_sizer = new wxBoxSizer( wxHORIZONTAL );
146     SetSizer( frame_sizer );
147
148     /* Creation of the menu bar */
149     CreateOurMenuBar();
150
151     /* Creation of the tool bar */
152     CreateOurToolBar();
153
154     /* Creation of the slider sub-window */
155     CreateOurSlider();
156
157     /* Creation of the status bar
158      * Helptext for menu items and toolbar tools will automatically get
159      * displayed here. */
160     int i_status_width[2] = {-2,-3};
161     statusbar = CreateStatusBar( 2 );                            /* 2 fields */
162     statusbar->SetStatusWidths( 2, i_status_width );
163
164     /* Make sure we've got the right background colour */
165     SetBackgroundColour( slider_frame->GetBackgroundColour() );
166
167     /* Layout everything */
168     SetAutoLayout( TRUE );
169     frame_sizer->Layout();
170     frame_sizer->Fit(this);
171
172 #if !defined(__WXX11__)
173     /* Associate drop targets with the main interface */
174     SetDropTarget( new DragAndDrop( p_intf ) );
175 #endif
176 }
177
178 Interface::~Interface()
179 {
180 }
181
182 /*****************************************************************************
183  * Private methods.
184  *****************************************************************************/
185 void Interface::CreateOurMenuBar()
186 {
187 #define HELP_FILE  N_("Open a file")
188 #define HELP_DISC  N_("Open a DVD or (S)VCD")
189 #define HELP_NET   N_("Open a network stream")
190 #define HELP_SAT   N_("Open a satellite stream")
191 #define HELP_EJECT N_("Eject the DVD/CD")
192 #define HELP_EXIT  N_("Exit this program")
193
194 #define HELP_PLAYLIST   N_("Open the playlist")
195 #define HELP_LOGS       N_("Show the program logs")
196 #define HELP_FILEINFO       N_("Show information about the file being played")
197
198 #define HELP_AUDIO N_("Change the current audio track")
199 #define HELP_SUBS  N_("Change the current subtitles stream")
200 #define HELP_PREFS N_("Go to the preferences menu")
201
202 #define HELP_ABOUT N_("About this program")
203
204     /* Create the "File" menu */
205     wxMenu *file_menu = new wxMenu;
206     file_menu->Append( OpenFile_Event, _("&Open File..."), HELP_FILE );
207     file_menu->Append( OpenDisc_Event, _("Open &Disc..."), HELP_DISC );
208     file_menu->Append( OpenNet_Event, _("&Network Stream..."), HELP_NET );
209 #if 0
210     file_menu->Append( OpenSat_Event, _("&Satellite Stream..."), HELP_NET );
211 #endif
212     file_menu->AppendSeparator();
213     file_menu->Append( EjectDisc_Event, _("&Eject Disc"), HELP_EJECT );
214     file_menu->AppendSeparator();
215     file_menu->Append( Exit_Event, _("E&xit"), HELP_EXIT );
216
217     /* Create the "View" menu */
218     wxMenu *view_menu = new wxMenu;
219     view_menu->Append( Playlist_Event, _("&Playlist..."), HELP_PLAYLIST );
220     view_menu->Append( Logs_Event, _("&Logs..."), HELP_LOGS );
221     view_menu->Append( FileInfo_Event, _("&File info..."), HELP_FILEINFO );
222
223     /* Create the "Settings" menu */
224     wxMenu *settings_menu = new wxMenu;
225     settings_menu->Append( Audio_Event, _("&Audio"), HELP_AUDIO );
226     settings_menu->Append( Subtitles_Event, _("&Subtitles"), HELP_SUBS );
227     settings_menu->AppendSeparator();
228     settings_menu->Append( Prefs_Event, _("&Preferences..."), HELP_PREFS );
229
230     /* Create the "Help" menu */
231     wxMenu *help_menu = new wxMenu;
232     help_menu->Append( About_Event, _("&About..."), HELP_ABOUT );
233
234     /* Append the freshly created menus to the menu bar... */
235     wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
236     menubar->Append( file_menu, _("&File") );
237     menubar->Append( view_menu, _("&View") );
238     menubar->Append( settings_menu, _("&Settings") );
239     menubar->Append( help_menu, _("&Help") );
240
241     /* Attach the menu bar to the frame */
242     SetMenuBar( menubar );
243
244 #if !defined(__WXX11__)
245     /* Associate drop targets with the menubar */
246     menubar->SetDropTarget( new DragAndDrop( p_intf ) );
247 #endif
248 }
249
250 void Interface::CreateOurToolBar()
251 {
252 #define HELP_STOP N_("Stop current playlist item")
253 #define HELP_PLAY N_("Play current playlist item")
254 #define HELP_PAUSE N_("Pause current playlist item")
255 #define HELP_PLO N_("Open playlist")
256 #define HELP_PLP N_("Previous playlist item")
257 #define HELP_PLN N_("Next playlist item")
258
259     wxLogNull LogDummy; /* Hack to suppress annoying log message on the win32
260                          * version because we don't include wx.rc */
261
262     wxToolBar *toolbar = CreateToolBar(
263         wxTB_HORIZONTAL | wxTB_TEXT | wxTB_FLAT | wxTB_DOCKABLE );
264
265     toolbar->SetToolBitmapSize( wxSize(TOOLBAR_BMP_WIDTH,TOOLBAR_BMP_HEIGHT) );
266
267     toolbar->AddTool( OpenFile_Event, _("File"), wxBitmap( file_xpm ),
268                       HELP_FILE );
269     toolbar->AddTool( OpenDisc_Event, _("Disc"), wxBitmap( disc_xpm ),
270                       HELP_DISC );
271     toolbar->AddTool( OpenNet_Event, _("Net"), wxBitmap( net_xpm ),
272                       HELP_NET );
273 #if 0
274     toolbar->AddTool( OpenSat_Event, _("Sat"), wxBitmap( sat_xpm ),
275                       HELP_SAT );
276 #endif
277     toolbar->AddSeparator();
278     toolbar->AddTool( StopStream_Event, _("Stop"), wxBitmap( stop_xpm ),
279                       HELP_STOP );
280     toolbar->AddTool( PlayStream_Event, _("Play"), wxBitmap( play_xpm ),
281                       HELP_PLAY );
282     toolbar->AddSeparator();
283     toolbar->AddTool( Playlist_Event, _("Playlist"), wxBitmap( playlist_xpm ),
284                       HELP_PLO );
285     toolbar->AddTool( PrevStream_Event, _("Prev"), wxBitmap( previous_xpm ),
286                       HELP_PLP );
287     toolbar->AddTool( NextStream_Event, _("Next"), wxBitmap( next_xpm ),
288                       HELP_PLN );
289
290     toolbar->Realize();
291
292     /* Place the toolbar in a sizer, so we can calculate the width of the
293      * toolbar and set this as the minimum for the main frame size. */
294     wxBoxSizer *toolbar_sizer = new wxBoxSizer( wxHORIZONTAL );
295     toolbar_sizer->Add( toolbar, 0, 0, 0 );
296     toolbar_sizer->Layout();
297
298 #ifndef WIN32
299     frame_sizer->SetMinSize( toolbar_sizer->GetMinSize().GetWidth(), -1 );
300 #else
301     frame_sizer->SetMinSize( toolbar->GetToolSize().GetWidth() *
302                              toolbar->GetToolsCount(), -1 );
303 #endif
304
305 #if !defined(__WXX11__)
306     /* Associate drop targets with the toolbar */
307     toolbar->SetDropTarget( new DragAndDrop( p_intf ) );
308 #endif
309 }
310
311 void Interface::CreateOurSlider()
312 {
313     /* Create a new frame containing the slider */
314     slider_frame = new wxPanel( this, -1, wxDefaultPosition, wxDefaultSize );
315     slider_frame->SetAutoLayout( TRUE );
316     slider_frame->Hide();
317
318     /* Create static box to surround the slider */
319     slider_box = new wxStaticBox( slider_frame, -1, "" );
320
321     /* Create sizer for slider frame */
322     wxStaticBoxSizer *slider_sizer =
323         new wxStaticBoxSizer( slider_box, wxHORIZONTAL );
324     slider_frame->SetSizer( slider_sizer );
325     slider_sizer->SetMinSize( -1, 50 );
326
327     /* Create slider */
328     slider = new wxSlider( slider_frame, SliderScroll_Event, 0, 0,
329                            SLIDER_MAX_POS, wxDefaultPosition, wxDefaultSize );
330     slider_sizer->Add( slider, 1, wxGROW | wxALL, 5 );
331     slider_sizer->Layout();
332     slider_sizer->SetSizeHints(slider_frame);
333 }
334
335 void Interface::Open( int i_access_method )
336 {
337     /* Show/hide the open dialog */
338     OpenDialog dialog( p_intf, this, i_access_method );
339
340     if( dialog.ShowModal() == wxID_OK )
341     {
342         /* Update the playlist */
343         playlist_t *p_playlist =
344             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
345                                            FIND_ANYWHERE );
346         if( p_playlist == NULL )
347         {
348             return;
349         }
350
351         msg_Err( p_intf, "%s", (char *)dialog.mrl.c_str() );
352
353         playlist_Add( p_playlist, (char *)dialog.mrl.c_str(),
354                       PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
355
356         i_playing_status = PLAYING_S;
357         TogglePlayButton();
358
359         /* Rebuild the playlist */
360         p_intf->p_sys->p_playlist_window->Rebuild();
361
362         vlc_object_release( p_playlist );
363     }
364 }
365
366 /*****************************************************************************
367  * Event Handlers.
368  *****************************************************************************/
369 void Interface::OnExit( wxCommandEvent& WXUNUSED(event) )
370 {
371     /* TRUE is to force the frame to close. */
372     Close(TRUE);
373 }
374
375 void Interface::OnAbout( wxCommandEvent& WXUNUSED(event) )
376 {
377     wxString msg;
378     msg.Printf( VOUT_TITLE + wxString(_(" (wxWindows interface)\n\n")) +
379         wxString(_("(C) 1996-2003 - the VideoLAN Team\n\n")) +
380         wxString(_("The VideoLAN team <videolan@videolan.org>\n"
381                    "http://www.videolan.org/\n\n")) +
382         wxString(_("This is the VideoLAN Client, a DVD, MPEG and DivX player."
383           "\nIt can play MPEG and MPEG2 files from a file or from a "
384           "network source.")) );
385
386     wxMessageBox( msg, wxString(_("About ")) + VOUT_TITLE,
387                   wxOK | wxICON_INFORMATION, this );
388 }
389
390 void Interface::OnPlaylist( wxCommandEvent& WXUNUSED(event) )
391 {
392     /* Show/hide the playlist window */
393     wxFrame *p_playlist_window = p_intf->p_sys->p_playlist_window;
394     if( p_playlist_window )
395     {
396         p_playlist_window->Show( ! p_playlist_window->IsShown() );
397     }
398 }
399
400 void Interface::OnLogs( wxCommandEvent& WXUNUSED(event) )
401 {
402     /* Show/hide the log window */
403     wxFrame *p_messages_window = p_intf->p_sys->p_messages_window;
404     if( p_messages_window )
405     {
406         p_messages_window->Show( ! p_messages_window->IsShown() );
407     }
408 }
409
410 void Interface::OnFileInfo( wxCommandEvent& WXUNUSED(event) )
411 {
412     /* Show/hide the fileinfo window */
413     wxFrame *p_fileinfo_window = new FileInfo( p_intf, this );
414     if( p_fileinfo_window )
415     {
416         p_fileinfo_window->Show( true );//! p_messages_window->IsShown() );
417     }
418 }
419
420 void Interface::OnOpenFile( wxCommandEvent& WXUNUSED(event) )
421 {
422     Open( FILE_ACCESS );
423 }
424
425 void Interface::OnOpenDisc( wxCommandEvent& WXUNUSED(event) )
426 {
427     Open( DISC_ACCESS );
428 }
429
430 void Interface::OnOpenNet( wxCommandEvent& WXUNUSED(event) )
431 {
432     Open( NET_ACCESS );
433 }
434
435 void Interface::OnOpenSat( wxCommandEvent& WXUNUSED(event) )
436 {
437     Open( SAT_ACCESS );
438 }
439
440 void Interface::OnPlayStream( wxCommandEvent& WXUNUSED(event) )
441 {
442     wxCommandEvent dummy;
443     playlist_t *p_playlist =
444         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
445                                        FIND_ANYWHERE );
446     if( p_playlist == NULL )
447     {
448         OnOpenFile( dummy );
449         return;
450     }
451
452     /* If the playlist is empty, open a file requester instead */
453     vlc_mutex_lock( &p_playlist->object_lock );
454     if( p_playlist->i_size )
455     {
456         vlc_mutex_unlock( &p_playlist->object_lock );
457
458         input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_intf,
459                                                        VLC_OBJECT_INPUT,
460                                                        FIND_ANYWHERE );
461         if( p_input == NULL )
462         {
463             /* No stream was playing, start one */
464             playlist_Play( p_playlist );
465             i_playing_status = PLAYING_S;
466             TogglePlayButton();
467             vlc_object_release( p_playlist );
468             return;
469         }
470
471         if( p_input->stream.control.i_status != PAUSE_S )
472         {
473             /* A stream is being played, pause it */
474             input_SetStatus( p_input, INPUT_STATUS_PAUSE );
475             i_playing_status = PAUSE_S;
476             TogglePlayButton();
477             vlc_object_release( p_playlist );
478             vlc_object_release( p_input );
479             return;
480         }
481
482         /* Stream is paused, resume it */
483         playlist_Play( p_playlist );
484         i_playing_status = PLAYING_S;
485         TogglePlayButton();
486         vlc_object_release( p_input );
487         vlc_object_release( p_playlist );
488     }
489     else
490     {
491         vlc_mutex_unlock( &p_playlist->object_lock );
492         vlc_object_release( p_playlist );
493         OnOpenFile( dummy );
494     }
495 }
496
497 void Interface::OnStopStream( wxCommandEvent& WXUNUSED(event) )
498 {
499     playlist_t * p_playlist =
500         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
501                                        FIND_ANYWHERE );
502     if( p_playlist == NULL )
503     {
504         return;
505     }
506
507     playlist_Stop( p_playlist );
508     i_playing_status = PAUSE_S;
509     TogglePlayButton();
510     vlc_object_release( p_playlist );
511 }
512
513 void Interface::OnSliderUpdate( wxScrollEvent& event )
514 {
515     vlc_mutex_lock( &p_intf->change_lock );
516
517 #ifdef WIN32
518     if( event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE
519         || event.GetEventType() == wxEVT_SCROLL_ENDSCROLL )
520     {
521 #endif
522         if( p_intf->p_sys->i_slider_pos != event.GetPosition()
523             && p_intf->p_sys->p_input )
524         {
525             p_intf->p_sys->i_slider_pos = event.GetPosition();
526             input_Seek( p_intf->p_sys->p_input, p_intf->p_sys->i_slider_pos *
527                         100 / SLIDER_MAX_POS,
528                         INPUT_SEEK_PERCENT | INPUT_SEEK_SET );
529         }
530
531 #ifdef WIN32
532         p_intf->p_sys->b_slider_free = VLC_TRUE;
533     }
534     else
535     {
536         p_intf->p_sys->b_slider_free = VLC_FALSE;
537
538         if( p_intf->p_sys->p_input )
539         {
540             /* Update stream date */
541 #define p_area p_intf->p_sys->p_input->stream.p_selected_area
542             char psz_time[ OFFSETTOTIME_MAX_SIZE ];
543
544             slider_box->SetLabel(
545                 input_OffsetToTime( p_intf->p_sys->p_input,
546                                     psz_time,
547                                     p_area->i_size * event.GetPosition()
548                                     / SLIDER_MAX_POS ) );
549 #undef p_area
550         }
551     }
552 #endif
553
554     vlc_mutex_unlock( &p_intf->change_lock );
555 }
556
557 void Interface::OnPrevStream( wxCommandEvent& WXUNUSED(event) )
558 {
559     playlist_t * p_playlist =
560         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
561                                        FIND_ANYWHERE );
562     if( p_playlist == NULL )
563     {
564         return;
565     }
566
567     playlist_Prev( p_playlist );
568     vlc_object_release( p_playlist );
569 }
570
571 void Interface::OnNextStream( wxCommandEvent& WXUNUSED(event) )
572 {
573     playlist_t * p_playlist =
574         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
575                                        FIND_ANYWHERE );
576     if( p_playlist == NULL )
577     {
578         return;
579     }
580
581     playlist_Next( p_playlist );
582     vlc_object_release( p_playlist );
583 }
584
585 void Interface::TogglePlayButton( )
586 {
587     GetToolBar()->DeleteTool( PlayStream_Event );
588
589     if( i_playing_status == PLAYING_S )
590     {
591         GetToolBar()->InsertTool( 5, PlayStream_Event, _("Pause"),
592                                   wxBitmap( pause_xpm ) );
593     }
594     else
595     {
596         GetToolBar()->InsertTool( 5, PlayStream_Event, _("Play"),
597                                   wxBitmap( play_xpm ) );
598     }
599
600     GetToolBar()->Realize();
601 }
602
603 #if !defined(__WXX11__)
604 /*****************************************************************************
605  * Definition of DragAndDrop class.
606  *****************************************************************************/
607 DragAndDrop::DragAndDrop( intf_thread_t *_p_intf )
608 {
609     p_intf = _p_intf;
610 }
611
612 bool DragAndDrop::OnDropFiles( wxCoord, wxCoord,
613                                const wxArrayString& filenames )
614 {
615     unsigned int i;
616
617     /* Add dropped files to the playlist */
618
619     playlist_t *p_playlist =
620         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
621                                        FIND_ANYWHERE );
622     if( p_playlist == NULL )
623     {
624         return FALSE;
625     }
626
627     for( i = 0; i < filenames.GetCount(); i++ )
628         playlist_Add( p_playlist, (char *)filenames[i].c_str(),
629                       PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
630
631     /* Rebuild the playlist */
632     p_intf->p_sys->p_playlist_window->Rebuild();
633
634     vlc_object_release( p_playlist );
635
636     return TRUE;
637 }
638 #endif