d3b8bd7a3513fcc852c520ce16674ae134dc300d
[vlc.git] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "qt4.hpp"
25 #include "main_interface.hpp"
26 #include "input_manager.hpp"
27 #include "util/input_slider.hpp"
28 #include "util/qvlcframe.hpp"
29 #include "util/customwidgets.hpp"
30 #include "dialogs_provider.hpp"
31 #include "components/interface_widgets.hpp"
32 #include "dialogs/playlist.hpp"
33 #include "menus.hpp"
34
35 #include <QMenuBar>
36 #include <QCloseEvent>
37 #include <QPushButton>
38 #include <QStatusBar>
39 #include <QKeyEvent>
40 #include <QUrl>
41 #include <QSystemTrayIcon>
42 #include <QSize>
43 #include <QMenu>
44
45 #include <assert.h>
46 #include <vlc_keys.h>
47 #include <vlc_vout.h>
48
49 #ifdef WIN32
50     #define PREF_W 410
51     #define PREF_H 121
52 #else
53     #define PREF_W 450
54     #define PREF_H 125
55 #endif
56
57 #define VISIBLE(i) (i && i->isVisible())
58
59 #define SET_WIDTH(i,j) i->widgetSize.setWidth(j)
60 #define SET_HEIGHT(i,j) i->widgetSize.setHeight(j)
61 #define SET_WH( i,j,k) i->widgetSize.setWidth(j); i->widgetSize.setHeight(k);
62
63 #define DS(i) i.width(),i.height()
64
65 #define DEBUG_COLOR 0
66
67 /* Callback prototypes */
68 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
69                         vlc_value_t old_val, vlc_value_t new_val, void *param );
70 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
71                        vlc_value_t old_val, vlc_value_t new_val, void *param );
72 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
73                              vlc_value_t, void *);
74 /* Video handling */
75 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
76                         int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
77 {
78     return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
79 }
80 static void DoRelease( intf_thread_t *p_intf, void *p_win )
81 {
82     return p_intf->p_sys->p_mi->releaseVideo( p_win );
83 }
84 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
85 {
86     return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
87 }
88
89 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
90 {
91     /* Variables initialisation */
92     need_components_update = false;
93     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
94     embeddedPlaylistWasActive = videoIsActive = false;
95
96     bool b_createSystray = false;
97     input_name = "";
98
99     /**
100      *  Configuration and settings
101      **/
102     settings = new QSettings( "VideoLAN", "VLC" );
103     settings->beginGroup( "MainWindow" );
104
105     /* Main settings */
106     setFocusPolicy( Qt::StrongFocus );
107     setAcceptDrops(true);
108     setWindowIcon( QApplication::windowIcon() );
109
110     /* Set The Video In emebedded Mode or not */
111     videoEmbeddedFlag = false;
112     if( config_GetInt( p_intf, "embedded-video" ) )
113         videoEmbeddedFlag = true;
114
115     alwaysVideoFlag = false;
116     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ) )
117         alwaysVideoFlag = true;
118
119     /* Set the other interface settings */
120     playlistEmbeddedFlag = settings->value("playlist-embedded", true).toBool();
121     advControlsEnabled= settings->value( "adv-controls", false ).toBool();
122     visualSelectorEnabled= settings->value( "visual-selector", false ).toBool();
123
124 #if DEBUG_COLOR
125     QPalette palette2;
126     palette2.setColor(this->backgroundRole(), Qt::magenta);
127     setPalette(palette2);
128 #endif
129
130     /**
131      *  UI design
132      **/
133     setVLCWindowsTitle();
134     handleMainUi( settings );
135
136     /* Menu Bar */
137     QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
138                              advControlsEnabled, visualSelectorEnabled );
139
140     /* Status Bar */
141     timeLabel = new QLabel;
142     nameLabel = new QLabel;
143     speedLabel = new QLabel( "1.0x" );
144     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
145     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
146     statusBar()->addWidget( nameLabel, 8 );
147     statusBar()->addPermanentWidget( speedLabel, 0 );
148     statusBar()->addPermanentWidget( timeLabel, 2 );
149
150     /* Systray */
151     sysTray = NULL;
152     if( config_GetInt( p_intf, "qt-start-minimized") )
153     {
154         hide();
155         b_createSystray = true;
156     }
157     if( config_GetInt( p_intf, "qt-system-tray") )
158         b_createSystray = true;
159     if (QSystemTrayIcon::isSystemTrayAvailable() && b_createSystray )
160             createSystray();
161
162     /* Init input manager */
163     MainInputManager::getInstance( p_intf );
164     ON_TIMEOUT( updateOnTimer() );
165
166     /**
167      * CONNECTs
168      **/
169
170     /* Volume control */
171     CONNECT( ui.volumeSlider, valueChanged(int), this, updateVolume(int) );
172
173     /* Connect the input manager to the GUI elements it manages */
174     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
175              slider, setPosition( float,int, int ) );
176     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
177              this, setDisplay( float, int, int ) );
178
179     /* Naming in the controller */
180     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
181              setName( QString ) );
182     if( config_GetInt( p_intf, "qt-system-tray" ) && sysTray )
183     {
184         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
185                  updateSystrayTooltipName( QString ) );
186     }
187     if( config_GetInt( p_intf, "qt-name-in-title" ) )
188     {
189         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
190              setVLCWindowsTitle( QString ) );
191     }
192
193     /* PLAY_STATUS */
194     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
195     CONNECT( THEMIM->getIM(), navigationChanged( int ),
196              this, setNavigation(int) );
197     if( config_GetInt( p_intf, "qt-system-tray" ) && sysTray )
198     {
199         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
200                  updateSystrayTooltipStatus( int ) );
201     }
202     CONNECT( slider, sliderDragged( float ),
203              THEMIM->getIM(), sliderUpdate( float ) );
204
205     /* Buttons */
206     CONNECT( ui.prevSectionButton, clicked(), THEMIM->getIM(),
207              sectionPrev() );
208     CONNECT( ui.nextSectionButton, clicked(), THEMIM->getIM(),
209              sectionNext() );
210     CONNECT( ui.menuButton, clicked(), THEMIM->getIM(),
211              sectionMenu() );
212
213     /**
214      * Callbacks
215      **/
216     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
217     var_AddCallback( p_intf, "interaction", InteractCallback, this );
218     p_intf->b_interaction = VLC_TRUE;
219
220     /* Register callback for the intf-popupmenu variable */
221     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
222                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
223     if( p_playlist != NULL )
224     {
225         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
226         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
227         vlc_object_release( p_playlist );
228     }
229 }
230
231 MainInterface::~MainInterface()
232 {
233     /* Unregister callback for the intf-popupmenu variable */
234     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
235                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
236     if( p_playlist != NULL )
237     {
238         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
239         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
240         vlc_object_release( p_playlist );
241     }
242
243     settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
244     settings->setValue( "adv-controls", advControlsEnabled );
245     settings->setValue( "pos", pos() );
246     settings->endGroup();
247     delete settings;
248     p_intf->b_interaction = VLC_FALSE;
249     var_DelCallback( p_intf, "interaction", InteractCallback, this );
250
251     p_intf->pf_request_window = NULL;
252     p_intf->pf_release_window = NULL;
253     p_intf->pf_control_window = NULL;
254 }
255
256 /*****************************
257  *   Main UI handling        *
258  *****************************/
259
260 /**
261  * Give the decorations of the Main Window a correct Name.
262  * If nothing is given, set it to VLC...
263  **/
264 void MainInterface::setVLCWindowsTitle( QString aTitle )
265 {
266     if( aTitle.isEmpty() )
267     {
268         setWindowTitle( qtr( "VLC media player" ) );
269     }
270     else
271     {
272         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
273     }
274 }
275
276 void MainInterface::handleMainUi( QSettings *settings )
277 {
278     QWidget *main = new QWidget( this );
279     QVBoxLayout *mainLayout = new QVBoxLayout( main );
280     setCentralWidget( main );
281
282     /* Margins, spacing */
283     main->setContentsMargins( 0, 0, 0, 0 );
284     mainLayout->setMargin( 0 );
285
286     /* CONTROLS */
287     QWidget *controls = new QWidget;
288     ui.setupUi( controls );
289
290     /* Configure the UI */
291     slider = new InputSlider( Qt::Horizontal, NULL );
292     mainLayout->insertWidget( 0, slider );
293     ui.discFrame->hide();
294
295     BUTTON_SET_IMG( ui.prevSectionButton, "", previous.png, "" );
296     BUTTON_SET_IMG( ui.nextSectionButton, "", next.png, "" );
297     BUTTON_SET_IMG( ui.menuButton, "", previous.png, "" );
298
299     BUTTON_SET_ACT_I( ui.prevButton, "" , previous.png,
300                       qtr("Previous"), prev() );
301     BUTTON_SET_ACT_I( ui.nextButton, "", next.png, qtr("Next"), next() );
302     BUTTON_SET_ACT_I( ui.playButton, "", play.png, qtr("Play"), play() );
303     BUTTON_SET_ACT_I( ui.stopButton, "", stop.png, qtr("Stop"), stop() );
304
305     /* Volume */
306     ui.volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
307     ui.volumeSlider->setMaximum( 100 );
308     ui.volMuteLabel->setToolTip( qtr( "Mute" ) );
309     VolumeClickHandler *h = new VolumeClickHandler( p_intf, this );
310     ui.volMuteLabel->installEventFilter(h);
311     ui.volumeSlider->setFocusPolicy( Qt::NoFocus );
312
313     BUTTON_SET_IMG( ui.playlistButton, "" , playlist_icon.png,
314                         playlistEmbeddedFlag ?  qtr( "Show playlist" ) :
315                                                 qtr( "Open playlist" ) );
316     BUTTONACT( ui.playlistButton, playlist() );
317
318 #if DEBUG_COLOR
319     QPalette palette;
320     palette.setColor(main->backgroundRole(), Qt::green);
321     main->setPalette(palette);
322 #endif
323
324     /* Add the controls Widget */
325     mainLayout->addWidget( controls );
326
327     /* Set initial size */
328     resize ( PREF_W, PREF_H );
329
330     addSize = QSize( mainLayout->margin() * 2, PREF_H );
331
332     /* advanced Controls handling */
333     advControls = new ControlsWidget( p_intf );
334     mainLayout->insertWidget( 0, advControls );
335     advControls->updateGeometry();
336     if( !advControlsEnabled ) advControls->hide();
337     need_components_update = true;
338
339     /* Visualisation */
340     visualSelector = new VisualSelector( p_intf );
341     mainLayout->insertWidget( 0, visualSelector );
342     visualSelector->hide();
343
344     /* And video Outputs */
345     if( alwaysVideoFlag )
346     {
347         bgWidget = new BackgroundWidget( p_intf );
348         bgWidget->widgetSize = settings->value( "backgroundSize",
349                                            QSize( 200, 200 ) ).toSize();
350         bgWidget->resize( bgWidget->widgetSize );
351         bgWidget->updateGeometry();
352         mainLayout->insertWidget( 0, bgWidget );
353     }
354
355     if( videoEmbeddedFlag )
356     {
357         videoWidget = new VideoWidget( p_intf );
358         videoWidget->widgetSize = QSize( 1, 1 );
359         //videoWidget->resize( videoWidget->widgetSize );
360         mainLayout->insertWidget( 0, videoWidget );
361
362         p_intf->pf_request_window  = ::DoRequest;
363         p_intf->pf_release_window  = ::DoRelease;
364         p_intf->pf_control_window  = ::DoControl;
365     }
366
367     /* Finish the sizing */
368     setMinimumSize( PREF_W, addSize.height() );
369 }
370
371 /**********************************************************************
372  * Handling of the components
373  **********************************************************************/
374 void MainInterface::calculateInterfaceSize()
375 {
376     int width = 0, height = 0;
377     if( VISIBLE( bgWidget ) )
378     {
379         width = bgWidget->widgetSize.width();
380         height = bgWidget->widgetSize.height();
381         assert( !(playlistWidget && playlistWidget->isVisible() ) );
382     }
383     else if( VISIBLE( playlistWidget ) )
384     {
385         width = playlistWidget->widgetSize.width();
386         height = playlistWidget->widgetSize.height();
387     }
388     else if( videoIsActive )
389     {
390         width =  videoWidget->widgetSize.width() ;
391         height = videoWidget->widgetSize.height();
392     }
393     else
394     {
395         width = PREF_W - addSize.width();
396         height = PREF_H - addSize.height();
397     }
398     if( VISIBLE( visualSelector ) )
399         height += visualSelector->height();
400     if( VISIBLE( advControls) )
401     {
402         height += advControls->sizeHint().height();
403     }
404     mainSize = QSize( width + addSize.width(), height + addSize.height() );
405 }
406
407 void MainInterface::resizeEvent( QResizeEvent *e )
408 {
409     videoWidget->widgetSize.setWidth(  e->size().width() - addSize.width() );
410     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
411     {
412         SET_WH( videoWidget, e->size().width() - addSize.width(),
413                              e->size().height()  - addSize.height() );
414         videoWidget->updateGeometry();
415     }
416     if( VISIBLE( playlistWidget ) )
417     {
418         SET_WH( playlistWidget , e->size().width() - addSize.width(),
419                                  e->size().height() - addSize.height() );
420         playlistWidget->updateGeometry();
421     }
422 }
423
424 /*****************************************************************************
425  * Systray Icon and Systray Menu
426  *****************************************************************************/
427
428 /**
429  * Create a SystemTray icon and a menu that would go with it.
430  * Connects to a click handler on the icon.
431  **/
432 void MainInterface::createSystray()
433 {
434     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
435     sysTray = new QSystemTrayIcon( iconVLC, this );
436     sysTray->setToolTip( qtr( "VLC media player" ));
437
438     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
439     systrayMenu->setIcon( iconVLC );
440
441     QVLCMenu::updateSystrayMenu( this, p_intf, true );
442     sysTray->show();
443
444     CONNECT( sysTray, activated(  QSystemTrayIcon::ActivationReason ),
445             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
446 }
447
448 /**
449  * Update the menu of the Systray Icon.
450  * May be unneedded, since it just calls QVLCMenu::update
451  * FIXME !!!
452  **/
453 void MainInterface::updateSystrayMenu( int status )
454 {
455     QVLCMenu::updateSystrayMenu( this, p_intf ) ;
456 }
457
458 /**
459  * Updates the Systray Icon's menu and toggle the main interface
460  */
461 void MainInterface::toggleUpdateSystrayMenu()
462 {
463     if( isHidden() )
464     {
465         show();
466     }
467     else
468     {
469         if( isActiveWindow() )
470         {
471             hide();
472         }
473         else
474         {
475             activateWindow();
476         }
477     }
478     QVLCMenu::updateSystrayMenu( this, p_intf );
479 }
480
481 void MainInterface::handleSystrayClick(
482                                     QSystemTrayIcon::ActivationReason reason )
483 {
484     switch( reason )
485     {
486         case QSystemTrayIcon::Trigger:
487             toggleUpdateSystrayMenu();
488             break;
489         case QSystemTrayIcon::MiddleClick:
490             sysTray->showMessage( qtr( "VLC media player" ),
491                     qtr( "Control menu for the player" ),
492                     QSystemTrayIcon::Information, 4000 );
493             break;
494     }
495 }
496
497 /**
498  * Updates the name of the systray Icon tooltip.
499  * Doesn't check if the systray exists, check before you call it.
500  * FIXME !!! Fusion with next function ?
501  **/
502 void MainInterface::updateSystrayTooltipName( QString name )
503 {
504     if( name.isEmpty() )
505     {
506         sysTray->setToolTip( qtr( "VLC media player" ) );
507     }
508     else
509     {
510         sysTray->setToolTip( name );
511     }
512 }
513
514 /**
515  * Updates the status of the systray Icon tooltip.
516  * Doesn't check if the systray exists, check before you call it.
517  **/
518 void MainInterface::updateSystrayTooltipStatus( int i_status )
519 {
520     switch( i_status )
521     {
522         case  0:
523             {
524                 sysTray->setToolTip( qtr( "VLC media player" ) );
525                 break;
526             }
527         case PLAYING_S:
528             {
529                 sysTray->setToolTip( input_name );
530                 //+ " - " + qtr( "Playing" ) );
531                 break;
532             }
533         case PAUSE_S:
534             {
535                 sysTray->setToolTip( input_name + " - "
536                         + qtr( "Paused") );
537                 break;
538             }
539     }
540 }
541 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
542                                    int *pi_y, unsigned int *pi_width,
543                                    unsigned int *pi_height )
544 {
545     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
546     if( ret )
547     {
548         videoIsActive = true;
549         if( VISIBLE( playlistWidget ) )
550         {
551             embeddedPlaylistWasActive = true;
552 //            playlistWidget->hide();
553         }
554         bool bgWasVisible = false;
555         if( VISIBLE( bgWidget) )
556         {
557             bgWasVisible = true;
558 //            bgWidget->hide();
559         }
560         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
561         {
562             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
563         }
564         else /* Background widget available, use its size */
565         {
566             /* Ok, our visualizations are bad, so don't do this for the moment
567              * use the requested size anyway */
568             // videoWidget->widgetSize = bgWidget->widgeTSize;
569             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
570         }
571 //        videoWidget->updateGeometry(); /// FIXME: Needed ?
572         need_components_update = true;
573     }
574     return ret;
575 }
576
577 void MainInterface::releaseVideo( void *p_win )
578 {
579     videoWidget->release( p_win );
580     videoWidget->widgetSize = QSize( 0, 0 );
581     videoWidget->resize( videoWidget->widgetSize );
582
583     if( embeddedPlaylistWasActive )
584         ;//playlistWidget->show();
585     else if( bgWidget )
586         ;//bgWidget->show();
587
588     videoIsActive = false;
589     need_components_update = true;
590 }
591
592 class SetVideoOnTopQtEvent : public QEvent
593 {
594 public:
595     SetVideoOnTopQtEvent( bool _onTop ) :
596       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
597     {
598     }
599
600     bool OnTop() const
601     {
602         return onTop;
603     }
604
605 private:
606     bool onTop;
607 };
608
609 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
610 {
611     int i_ret = VLC_EGENERIC;
612     switch( i_query )
613     {
614         case VOUT_GET_SIZE:
615         {
616             unsigned int *pi_width  = va_arg( args, unsigned int * );
617             unsigned int *pi_height = va_arg( args, unsigned int * );
618             *pi_width = videoWidget->widgetSize.width();
619             *pi_height = videoWidget->widgetSize.height();
620             i_ret = VLC_SUCCESS;
621             break;
622         }
623         case VOUT_SET_SIZE:
624         {
625             unsigned int i_width  = va_arg( args, unsigned int );
626             unsigned int i_height = va_arg( args, unsigned int );
627             videoWidget->widgetSize = QSize( i_width, i_height );
628             // videoWidget->updateGeometry();
629             need_components_update = true;
630             i_ret = VLC_SUCCESS;
631             break;
632         }
633         case VOUT_SET_STAY_ON_TOP:
634         {
635             int i_arg = va_arg( args, int );
636             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
637             i_ret = VLC_SUCCESS;
638             break;
639         }
640         default:
641             msg_Warn( p_intf, "unsupported control query" );
642             break;
643     }
644     return i_ret;
645 }
646
647 void MainInterface::advanced()
648 {
649     if( !VISIBLE( advControls ) )
650     {
651         advControls->show();
652         advControlsEnabled = true;
653     }
654     else
655     {
656         advControls->hide();
657         advControlsEnabled = false;
658     }
659     doComponentsUpdate();
660 }
661
662 void MainInterface::visual()
663 {
664     if( !VISIBLE( visualSelector) )
665     {
666         visualSelector->show();
667         if( !THEMIM->getIM()->hasVideo() )
668         {
669             /* Show the background widget */
670         }
671         visualSelectorEnabled = true;
672     }
673     else
674     {
675         /* Stop any currently running visualization */
676         visualSelector->hide();
677         visualSelectorEnabled = false;
678     }
679     doComponentsUpdate();
680 }
681
682 void MainInterface::playlist()
683 {
684     // Toggle the playlist dialog
685     if( !playlistEmbeddedFlag )
686     {
687         if( playlistWidget )
688         {
689             /// \todo Destroy it
690         }
691         THEDP->playlistDialog();
692         return;
693     }
694
695     if( !playlistWidget )
696     {
697         PlaylistDialog::killInstance();
698         playlistWidget = new PlaylistWidget( p_intf );
699         ui.vboxLayout->insertWidget( 0, playlistWidget );
700         playlistWidget->widgetSize = settings->value( "playlistSize",
701                                                QSize( 650, 310 ) ).toSize();
702         playlistWidget->hide();
703     }
704     if( VISIBLE( playlistWidget ) )
705     {
706         playlistWidget->hide();
707         if( videoIsActive )
708         {
709             videoWidget->widgetSize = savedVideoSize;
710             videoWidget->resize( videoWidget->widgetSize );
711             videoWidget->updateGeometry();
712         }
713         if( bgWidget ) bgWidget->show();
714     }
715     else
716     {
717         playlistWidget->show();
718         if( videoIsActive )
719         {
720             savedVideoSize = videoWidget->widgetSize;
721             videoWidget->widgetSize.setHeight( 0 );
722             videoWidget->resize( videoWidget->widgetSize );
723             videoWidget->updateGeometry();
724         }
725         if( VISIBLE( bgWidget ) ) bgWidget->hide();
726     }
727     doComponentsUpdate();
728 }
729
730 /* Video widget cannot do this synchronously as it runs in another thread */
731 /* Well, could it, actually ? Probably dangerous ... */
732 void MainInterface::doComponentsUpdate()
733 {
734     calculateInterfaceSize();
735     resize( mainSize );
736 }
737
738 void MainInterface::undockPlaylist()
739 {
740     if( playlistWidget )
741     {
742         playlistWidget->hide();
743         playlistWidget->deleteLater();
744         ui.vboxLayout->removeWidget( playlistWidget );
745         playlistWidget = NULL;
746         playlistEmbeddedFlag = false;
747
748         menuBar()->clear();
749         QVLCMenu::createMenuBar( this, p_intf, false, advControlsEnabled,
750                                  visualSelectorEnabled);
751
752         if( videoIsActive )
753         {
754             videoWidget->widgetSize = savedVideoSize;
755             videoWidget->resize( videoWidget->widgetSize );
756             videoWidget->updateGeometry();
757         }
758
759         doComponentsUpdate();
760         THEDP->playlistDialog();
761     }
762 }
763
764 void MainInterface::toggleMenus()
765 {
766     if( menuBar()->isVisible() ) menuBar()->hide();
767         else menuBar()->show();
768     msg_Dbg( p_intf, "I was there: \\_o<~~ coin coin" );
769 }
770
771 void MainInterface::customEvent( QEvent *event )
772 {
773     if( event->type() == PLDockEvent_Type )
774     {
775         PlaylistDialog::killInstance();
776         playlistEmbeddedFlag = true;
777         menuBar()->clear();
778         QVLCMenu::createMenuBar(this, p_intf, true, advControlsEnabled,
779                                 visualSelectorEnabled);
780         playlist();
781     }
782     else if ( event->type() == SetVideoOnTopEvent_Type )
783     {
784         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
785         if( p_event->OnTop() )
786             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
787         else
788             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
789         show(); /* necessary to apply window flags?? */
790     }
791 }
792
793
794 /************************************************************************
795  * D&D
796  ************************************************************************/
797 void MainInterface::dropEvent(QDropEvent *event)
798 {
799      const QMimeData *mimeData = event->mimeData();
800
801      /* D&D of a subtitles file, add it on the fly */
802      if( mimeData->urls().size() == 1 )
803      {
804         if( THEMIM->getIM()->hasInput() )
805         {
806             if( input_AddSubtitles( THEMIM->getInput(),
807                                     qtu( mimeData->urls()[0].toString() ),
808                                     VLC_TRUE ) )
809             {
810                 event->acceptProposedAction();
811                 return;
812             }
813         }
814      }
815      bool first = true;
816      foreach( QUrl url, mimeData->urls() ) {
817         QString s = url.toString();
818         if( s.length() > 0 ) {
819             playlist_Add( THEPL, qtu(s), NULL,
820                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
821                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
822             first = false;
823         }
824      }
825      event->acceptProposedAction();
826 }
827 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
828 {
829      event->acceptProposedAction();
830 }
831 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
832 {
833      event->acceptProposedAction();
834 }
835 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
836 {
837      event->accept();
838 }
839
840 /************************************************************************
841  * Other stuff
842  ************************************************************************/
843 void MainInterface::keyPressEvent( QKeyEvent *e )
844 {
845     int i_vlck = qtEventToVLCKey( e );
846     if( i_vlck >= 0 )
847     {
848         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
849         e->accept();
850     }
851     else
852         e->ignore();
853 }
854
855 void MainInterface::wheelEvent( QWheelEvent *e )
856 {
857     int i_vlckey = qtWheelEventToVLCKey( e );
858     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
859     e->accept();
860 }
861
862 void MainInterface::stop()
863 {
864     THEMIM->stop();
865 }
866
867 void MainInterface::play()
868 {
869     if( playlist_IsEmpty(THEPL) )
870     {
871         /* The playlist is empty, open a file requester */
872         THEDP->openFileDialog();
873         setStatus( 0 );
874         return;
875     }
876     THEMIM->togglePlayPause();
877 }
878
879 void MainInterface::prev()
880 {
881     THEMIM->prev();
882 }
883
884 void MainInterface::next()
885 {
886     THEMIM->next();
887 }
888
889 void MainInterface::setDisplay( float pos, int time, int length )
890 {
891     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
892     secstotimestr( psz_length, length );
893     secstotimestr( psz_time, time );
894     QString title;
895     title.sprintf( "%s/%s", psz_time, psz_length );
896     timeLabel->setText( " "+title+" " );
897 }
898
899 void MainInterface::setName( QString name )
900 {
901     input_name = name;
902     nameLabel->setText( " " + name+" " );
903 }
904
905 void MainInterface::setStatus( int status )
906 {
907     if( status == 1 ) // Playing
908         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
909     else
910         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
911     if( systrayMenu )
912         updateSystrayMenu( status );
913 }
914
915 #define HELP_MENU N_("Menu")
916 #define HELP_PCH N_("Previous chapter")
917 #define HELP_NCH N_("Next chapter")
918 #define HELP_PTR N_("Previous track")
919 #define HELP_NTR N_("Next track")
920
921 void MainInterface::setNavigation( int navigation )
922 {
923     // 1 = chapter, 2 = title, 0 = no
924     if( navigation == 0 )
925     {
926         ui.discFrame->hide();
927     } else if( navigation == 1 ) {
928         ui.prevSectionButton->show();
929         ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
930         ui.nextSectionButton->show();
931         ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
932         ui.menuButton->show();
933         ui.discFrame->show();
934     } else {
935         ui.prevSectionButton->show();
936         ui.prevSectionButton->setToolTip( qfu(HELP_PCH) );
937         ui.nextSectionButton->show();
938         ui.nextSectionButton->setToolTip( qfu(HELP_NCH) );
939         ui.menuButton->hide();
940         ui.discFrame->show();
941     }
942 }
943
944 static bool b_my_volume;
945
946 void MainInterface::updateOnTimer()
947 {
948     /* \todo Make this event-driven */
949     advControls->enableInput( THEMIM->getIM()->hasInput() );
950     advControls->enableVideo( THEMIM->getIM()->hasVideo() );
951
952     if( intf_ShouldDie( p_intf ) )
953     {
954         QApplication::closeAllWindows();
955         QApplication::quit();
956     }
957     if( need_components_update )
958     {
959         doComponentsUpdate();
960         need_components_update = false;
961     }
962
963     audio_volume_t i_volume;
964     aout_VolumeGet( p_intf, &i_volume );
965     i_volume = (i_volume *  200 )/ AOUT_VOLUME_MAX ;
966     int i_gauge = ui.volumeSlider->value();
967     b_my_volume = false;
968     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
969     {
970         b_my_volume = true;
971         ui.volumeSlider->setValue( i_volume );
972         b_my_volume = false;
973     }
974 }
975
976 void MainInterface::closeEvent( QCloseEvent *e )
977 {
978     hide();
979     vlc_object_kill( p_intf );
980 }
981
982 void MainInterface::updateVolume( int sliderVolume )
983 {
984     if( !b_my_volume )
985     {
986         int i_res = sliderVolume * AOUT_VOLUME_MAX /
987                             (2*ui.volumeSlider->maximum() );
988         aout_VolumeSet( p_intf, i_res );
989     }
990 }
991
992 static int InteractCallback( vlc_object_t *p_this,
993                              const char *psz_var, vlc_value_t old_val,
994                              vlc_value_t new_val, void *param )
995 {
996     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
997     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
998     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
999     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1000     return VLC_SUCCESS;
1001 }
1002
1003 /*****************************************************************************
1004  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1005  *  We don't show the menu directly here because we don't want the
1006  *  caller to block for a too long time.
1007  *****************************************************************************/
1008 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1009                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1010 {
1011     intf_thread_t *p_intf = (intf_thread_t *)param;
1012
1013     if( p_intf->pf_show_dialog )
1014     {
1015         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1016                                 new_val.b_bool, 0 );
1017     }
1018
1019     return VLC_SUCCESS;
1020 }
1021
1022 /*****************************************************************************
1023  * IntfShowCB: callback triggered by the intf-show playlist variable.
1024  *****************************************************************************/
1025 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1026                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1027 {
1028     intf_thread_t *p_intf = (intf_thread_t *)param;
1029     //p_intf->p_sys->b_intf_show = VLC_TRUE;
1030
1031     return VLC_SUCCESS;
1032 }