]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt: Simplify includes.
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Ilkka Ollakka <ileoo@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31
32 #include "main_interface.hpp"
33 #include "input_manager.hpp"
34
35 #include "util/customwidgets.hpp"
36 #include "util/qvlcapp.hpp" /* DialogEvent defintion */
37
38 #include "components/interface_widgets.hpp"
39 #include "components/controller.hpp"
40 #include "components/playlist/playlist.hpp"
41
42 #include "menus.hpp"
43 #include "recents.hpp"
44
45 #include <QCloseEvent>
46 #include <QKeyEvent>
47
48 #include <QUrl>
49 #include <QSize>
50 #include <QDate>
51
52 #include <QMenuBar>
53 #include <QStatusBar>
54 #include <QLabel>
55 #include <QGroupBox>
56 #include <QPushButton>
57 #include <QWidgetAction>
58
59 #include <assert.h>
60
61 #include <vlc_keys.h> /* Wheel event */
62 #include <vlc_vout.h>
63
64 /* Callback prototypes */
65 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
66                         vlc_value_t old_val, vlc_value_t new_val, void *param );
67 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
68                        vlc_value_t old_val, vlc_value_t new_val, void *param );
69 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
70                              vlc_value_t, void *);
71
72 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
73 {
74     /* Variables initialisation */
75     // need_components_update = false;
76     bgWidget             = NULL;
77     videoWidget          = NULL;
78     playlistWidget       = NULL;
79     sysTray              = NULL;
80     videoIsActive        = false;
81     playlistVisible      = false;
82     input_name           = "";
83     fullscreenControls   = NULL;
84
85     /* Ask for privacy */
86     askForPrivacy();
87
88     /**
89      *  Configuration and settings
90      *  Pre-building of interface
91      **/
92     /* Main settings */
93     setFocusPolicy( Qt::StrongFocus );
94     setAcceptDrops( true );
95     setWindowIcon( QApplication::windowIcon() );
96     setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
97
98     /* Set The Video In emebedded Mode or not */
99     videoEmbeddedFlag = config_GetInt( p_intf, "embedded-video" );
100
101     /* Are we in the enhanced always-video mode or not ? */
102     i_visualmode = config_GetInt( p_intf, "qt-display-mode" );
103
104     /* Set the other interface settings */
105     settings = getSettings();
106     settings->beginGroup( "MainWindow" );
107
108     /* Visualisation, not really used yet */
109     visualSelectorEnabled = settings->value( "visual-selector", false).toBool();
110
111     /* Do we want anoying popups or not */
112     notificationEnabled = (bool)config_GetInt( p_intf, "qt-notification" );
113
114     /**************************
115      *  UI and Widgets design
116      **************************/
117     setVLCWindowsTitle();
118     handleMainUi( settings );
119
120 #if 0
121     /* Create a Dock to get the playlist */
122     dockPL = new QDockWidget( qtr( "Playlist" ), this );
123     dockPL->setSizePolicy( QSizePolicy::Preferred,
124                            QSizePolicy::Expanding );
125     dockPL->setFeatures( QDockWidget::AllDockWidgetFeatures );
126     dockPL->setAllowedAreas( Qt::LeftDockWidgetArea
127                            | Qt::RightDockWidgetArea
128                            | Qt::BottomDockWidgetArea );
129     dockPL->hide();
130 #endif
131
132     /**************************
133      * Menu Bar and Status Bar
134      **************************/
135     QVLCMenu::createMenuBar( this, p_intf, visualSelectorEnabled );
136
137     /* StatusBar Creation */
138     createStatusBar();
139
140     /* Recents menu updates */
141     CONNECT( RecentsMRL::getInstance( p_intf ), updated(),
142              this, updateRecentsMenu() );
143
144     /********************
145      * Input Manager    *
146      ********************/
147     MainInputManager::getInstance( p_intf );
148
149     /**************************
150      * Various CONNECTs on IM *
151      **************************/
152     /* Connect the input manager to the GUI elements it manages */
153
154     /* Change the SpeedRate in the Status Bar */
155     CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
156
157     /**
158      * Connects on nameChanged()
159      * Those connects are not merged because different options can trigger
160      * them down.
161      */
162     /* Naming in the controller statusbar */
163     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
164              setName( QString ) );
165     /* and in the systray */
166     if( sysTray )
167     {
168         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
169                  updateSystrayTooltipName( QString ) );
170     }
171     /* and in the title of the controller */
172     if( config_GetInt( p_intf, "qt-name-in-title" ) )
173     {
174         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
175              setVLCWindowsTitle( QString ) );
176     }
177
178     /**
179      * CONNECTS on PLAY_STATUS
180      **/
181     /* Status on the main controller */
182     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
183     /* and in the systray */
184     if( sysTray )
185     {
186         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
187                  updateSystrayTooltipStatus( int ) );
188     }
189
190     /* END CONNECTS ON IM */
191
192
193     /************
194      * Callbacks
195      ************/
196     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
197     var_AddCallback( p_intf, "interaction", InteractCallback, this );
198     p_intf->b_interaction = true;
199
200     var_AddCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
201
202     /* Register callback for the intf-popupmenu variable */
203     var_AddCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
204
205
206     /* VideoWidget connects to avoid different threads speaking to each other */
207     CONNECT( this, askReleaseVideo( void ),
208              this, releaseVideoSlot( void ) );
209     if( videoWidget )
210         CONNECT( this, askVideoToResize( unsigned int, unsigned int ),
211                  videoWidget, SetSizing( unsigned int, unsigned int ) );
212
213     CONNECT( this, askUpdate(), this, doComponentsUpdate() );
214
215     /* Size and placement of interface */
216     settings->beginGroup( "MainWindow" );
217     QVLCTools::restoreWidgetPosition( settings, this, QSize(380, 60) );
218
219     bool b_visible = settings->value( "playlist-visible", 0 ).toInt();
220     settings->endGroup();
221
222     /* Playlist */
223     if( b_visible ) togglePlaylist();
224
225     /* Final sizing and showing */
226     setMinimumWidth( __MAX( controls->sizeHint().width(),
227                             menuBar()->sizeHint().width() ) );
228     show();
229
230     /* And switch to minimal view if needed
231        Must be called after the show() */
232     if( i_visualmode == QT_MINIMAL_MODE )
233         toggleMinimalView();
234
235     /* Update the geometry : It is useful if you switch between
236        qt-display-modes ?*/
237     updateGeometry();
238     resize( sizeHint() );
239
240     /*****************************************************
241      * End everything by creating the Systray Management *
242      *****************************************************/
243     initSystray();
244 }
245
246 MainInterface::~MainInterface()
247 {
248     msg_Dbg( p_intf, "Destroying the main interface" );
249
250     if( videoIsActive ) videoWidget->hide();
251
252     if( playlistWidget )
253     {
254         if( !isDocked() )
255             QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
256
257         delete playlistWidget;
258     }
259
260     settings->beginGroup( "MainWindow" );
261
262     settings->setValue( "pl-dock-status", (int)i_pl_dock );
263     settings->setValue( "playlist-visible", (int)playlistVisible );
264     settings->setValue( "adv-controls",
265                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
266
267     if( bgWidget )
268         settings->setValue( "backgroundSize", bgWidget->size() );
269
270     QVLCTools::saveWidgetPosition(settings, this);
271     settings->endGroup();
272
273     var_DelCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
274
275     /* Unregister callback for the intf-popupmenu variable */
276     var_DelCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
277
278     p_intf->b_interaction = false;
279     var_DelCallback( p_intf, "interaction", InteractCallback, this );
280
281     p_intf->p_sys->p_mi = NULL;
282 }
283
284 /*****************************
285  *   Main UI handling        *
286  *****************************/
287
288 inline void MainInterface::createStatusBar()
289 {
290     /****************
291      *  Status Bar  *
292      ****************/
293     /* Widgets Creation*/
294     timeLabel = new TimeLabel( p_intf );
295     nameLabel = new QLabel;
296     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
297                                       | Qt::TextSelectableByKeyboard );
298     speedLabel = new SpeedLabel( p_intf, "1.00x" );
299     speedLabel->setToolTip(
300             qtr( "Current playback speed.\nRight click to adjust" ) );
301     speedLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
302
303     /* Styling those labels */
304     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
305     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
306     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
307
308     /* and adding those */
309     statusBar()->addWidget( nameLabel, 8 );
310     statusBar()->addPermanentWidget( speedLabel, 0 );
311     statusBar()->addPermanentWidget( timeLabel, 0 );
312
313     /* timeLabel behaviour:
314        - double clicking opens the goto time dialog
315        - right-clicking and clicking just toggle between remaining and
316          elapsed time.*/
317     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
318
319     /* Speed Label behaviour:
320        - right click gives the vertical speed slider */
321     CONNECT( speedLabel, customContextMenuRequested( QPoint ),
322              this, showSpeedMenu( QPoint ) );
323 }
324
325 inline void MainInterface::initSystray()
326 {
327     bool b_createSystray = false;
328     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
329     if( config_GetInt( p_intf, "qt-start-minimized") )
330     {
331         if( b_systrayAvailable )
332         {
333             b_createSystray = true;
334             hide();
335         }
336         else msg_Err( p_intf, "You can't minimize if you haven't a system "
337                 "tray bar" );
338     }
339     if( config_GetInt( p_intf, "qt-system-tray") )
340         b_createSystray = true;
341
342     if( b_systrayAvailable && b_createSystray )
343             createSystray();
344 }
345
346 /**
347  * Give the decorations of the Main Window a correct Name.
348  * If nothing is given, set it to VLC...
349  **/
350 void MainInterface::setVLCWindowsTitle( QString aTitle )
351 {
352     if( aTitle.isEmpty() )
353     {
354         setWindowTitle( qtr( "VLC media player" ) );
355     }
356     else
357     {
358         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
359     }
360 }
361
362 void MainInterface::handleMainUi( QSettings *settings )
363 {
364     /* Create the main Widget and the mainLayout */
365     QWidget *main = new QWidget;
366     setCentralWidget( main );
367     mainLayout = new QVBoxLayout( main );
368
369     /* Margins, spacing */
370     main->setContentsMargins( 0, 0, 0, 0 );
371     main->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
372     mainLayout->setSpacing( 0 );
373     mainLayout->setMargin( 0 );
374
375     /* Create the CONTROLS Widget */
376     controls = new ControlsWidget( p_intf,
377                    settings->value( "adv-controls", false ).toBool(), this );
378     CONNECT( controls, advancedControlsToggled( bool ),
379              this, doComponentsUpdate() );
380     InputControlsWidget *inputC = new InputControlsWidget( p_intf, this );
381
382     /* Add the controls Widget to the main Widget */
383     mainLayout->insertWidget( 0, controls, 0, Qt::AlignBottom );
384     mainLayout->insertWidget( 0, inputC, 0, Qt::AlignBottom );
385
386     /* Create the Speed Control Widget */
387     speedControl = new SpeedControlWidget( p_intf );
388     speedControlMenu = new QMenu( this );
389
390     QWidgetAction *widgetAction = new QWidgetAction( speedControl );
391     widgetAction->setDefaultWidget( speedControl );
392     speedControlMenu->addAction( widgetAction );
393
394     /* Visualisation */
395     /* Disabled for now, they SUCK */
396     #if 0
397     visualSelector = new VisualSelector( p_intf );
398     mainLayout->insertWidget( 0, visualSelector );
399     visualSelector->hide();
400     #endif
401
402     /* Bg Cone */
403     bgWidget = new BackgroundWidget( p_intf );
404     bgWidget->resize(
405             settings->value( "backgroundSize", QSize( 300, 200 ) ).toSize() );
406     bgWidget->updateGeometry();
407     mainLayout->insertWidget( 0, bgWidget );
408     CONNECT( this, askBgWidgetToToggle(), bgWidget, toggle() );
409
410     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
411         i_visualmode != QT_MINIMAL_MODE )
412     {
413         bgWidget->hide();
414     }
415
416     /* And video Outputs */
417     if( videoEmbeddedFlag )
418     {
419         videoWidget = new VideoWidget( p_intf );
420         mainLayout->insertWidget( 0, videoWidget, 10 );
421     }
422
423     /* Finish the sizing */
424     main->updateGeometry();
425
426     getSettings()->endGroup();
427 #ifdef WIN32
428     if ( depth() > 8 )
429 #endif
430     /* Create the FULLSCREEN CONTROLS Widget */
431     if( config_GetInt( p_intf, "qt-fs-controller" ) )
432     {
433         fullscreenControls = new FullscreenControllerWidget( p_intf );
434     }
435 }
436
437 inline void MainInterface::askForPrivacy()
438 {
439     /**
440      * Ask for the network policy on FIRST STARTUP
441      **/
442     if( config_GetInt( p_intf, "qt-privacy-ask") )
443     {
444         QList<ConfigControl *> controls;
445         if( privacyDialog( &controls ) == QDialog::Accepted )
446         {
447             QList<ConfigControl *>::Iterator i;
448             for(  i = controls.begin() ; i != controls.end() ; i++ )
449             {
450                 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
451                 c->doApply( p_intf );
452             }
453
454             config_PutInt( p_intf,  "qt-privacy-ask" , 0 );
455             /* We have to save here because the user may not launch Prefs */
456             config_SaveConfigFile( p_intf, NULL );
457         }
458     }
459 }
460
461 int MainInterface::privacyDialog( QList<ConfigControl *> *controls )
462 {
463     QDialog *privacy = new QDialog();
464
465     privacy->setWindowTitle( qtr( "Privacy and Network Policies" ) );
466
467     QGridLayout *gLayout = new QGridLayout( privacy );
468
469     QGroupBox *blabla = new QGroupBox( qtr( "Privacy and Network Warning" ) );
470     QGridLayout *blablaLayout = new QGridLayout( blabla );
471     QLabel *text = new QLabel( qtr(
472         "<p>The <i>VideoLAN Team</i> doesn't like when an application goes "
473         "online without authorization.</p>\n "
474         "<p><i>VLC media player</i> can retreive limited information from "
475         "the Internet in order to get CD covers or to check "
476         "for available updates.</p>\n"
477         "<p><i>VLC media player</i> <b>DOES NOT</b> send or collect <b>ANY</b> "
478         "information, even anonymously, about your usage.</p>\n"
479         "<p>Therefore please select from the following options, the default being "
480         "almost no access to the web.</p>\n") );
481     text->setWordWrap( true );
482     text->setTextFormat( Qt::RichText );
483
484     blablaLayout->addWidget( text, 0, 0 ) ;
485
486     QGroupBox *options = new QGroupBox;
487     QGridLayout *optionsLayout = new QGridLayout( options );
488
489     gLayout->addWidget( blabla, 0, 0, 1, 3 );
490     gLayout->addWidget( options, 1, 0, 1, 3 );
491     module_config_t *p_config;
492     ConfigControl *control;
493     int line = 0;
494
495 #define CONFIG_GENERIC( option, type )                            \
496     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
497     if( p_config )                                                \
498     {                                                             \
499         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
500                 p_config, options, false, optionsLayout, line );  \
501         controls->append( control );                               \
502     }
503
504 #define CONFIG_GENERIC_NOBOOL( option, type )                     \
505     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
506     if( p_config )                                                \
507     {                                                             \
508         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
509                 p_config, options, optionsLayout, line );  \
510         controls->append( control );                               \
511     }
512
513     CONFIG_GENERIC( "album-art", IntegerList ); line++;
514 #ifdef UPDATE_CHECK
515     CONFIG_GENERIC_NOBOOL( "qt-updates-notif", Bool ); line++;
516     CONFIG_GENERIC_NOBOOL( "qt-updates-days", Integer ); line++;
517 #endif
518
519     QPushButton *ok = new QPushButton( qtr( "OK" ) );
520
521     gLayout->addWidget( ok, 2, 2 );
522
523     CONNECT( ok, clicked(), privacy, accept() );
524     return privacy->exec();
525 }
526
527
528 /**********************************************************************
529  * Handling of sizing of the components
530  **********************************************************************/
531
532 /* This function is probably wrong, but we don't have many many choices...
533    Since we can't know from the playlist Widget if we are inside a dock or not,
534    because the playlist Widget can be called by THEDP, as a separate windows for
535    the skins.
536    Maybe the other solution is to redefine the sizeHint() of the playlist and
537    ask _parent->isFloating()...
538    If you think this would be better, please FIXME it...
539 */
540
541 QSize MainInterface::sizeHint() const
542 {
543     int nwidth  = controls->sizeHint().width();
544     int nheight = controls->isVisible() ?
545                   controls->size().height()
546                   + menuBar()->size().height()
547                   + statusBar()->size().height()
548                   : 0 ;
549
550     if( VISIBLE( bgWidget ) )
551     {
552         nheight += bgWidget->size().height();
553         nwidth  = bgWidget->size().width();
554     }
555     else if( videoIsActive && videoWidget->isVisible() )
556     {
557         nheight += videoWidget->sizeHint().height();
558         nwidth  = videoWidget->sizeHint().width();
559     }
560 #if 0
561     if( !dockPL->isFloating() && dockPL->isVisible() && dockPL->widget()  )
562     {
563         nheight += dockPL->size().height();
564         nwidth = __MAX( nwidth, dockPL->size().width() );
565         msg_Warn( p_intf, "3 %i %i", nheight, nwidth );
566     }
567 #endif
568     return QSize( nwidth, nheight );
569 }
570
571 void MainInterface::toggleFSC()
572 {
573    if( !fullscreenControls ) return;
574
575    IMEvent *eShow = new IMEvent( FullscreenControlToggle_Type, 0 );
576    QApplication::postEvent( fullscreenControls, static_cast<QEvent *>(eShow) );
577 }
578
579 void MainInterface::debug()
580 {
581 #ifndef NDEBUG
582     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
583     msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
584     if( videoWidget && videoWidget->isVisible() )
585     {
586         msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
587         msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
588     }
589 #endif
590 }
591
592 /****************************************************************************
593  * Small right-click menu for rate control
594  ****************************************************************************/
595 void MainInterface::showSpeedMenu( QPoint pos )
596 {
597     speedControlMenu->exec( QCursor::pos() - pos
598                           + QPoint( 0, speedLabel->height() ) );
599 }
600
601 /****************************************************************************
602  * Video Handling
603  ****************************************************************************/
604
605 /* This event is used to deal with the fullscreen and always on top
606    issue conflict (bug in wx) */
607 class SetVideoOnTopQtEvent : public QEvent
608 {
609 public:
610     SetVideoOnTopQtEvent( bool _onTop ) :
611       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
612     {}
613
614     bool OnTop() const { return onTop; }
615
616 private:
617     bool onTop;
618 };
619
620 /**
621  * README
622  * Thou shall not call/resize/hide widgets from on another thread.
623  * This is wrong, and this is TEH reason to emit signals on those Video Functions
624  **/
625 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
626                                    int *pi_y, unsigned int *pi_width,
627                                    unsigned int *pi_height )
628 {
629     /* Request the videoWidget */
630     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
631     if( ret ) /* The videoWidget is available */
632     {
633         /* Did we have a bg ? Hide it! */
634         if( VISIBLE( bgWidget) )
635         {
636             bgWasVisible = true;
637             emit askBgWidgetToToggle();
638         }
639         else
640             bgWasVisible = false;
641
642         /* Consider the video active now */
643         videoIsActive = true;
644
645         emit askUpdate();
646
647         if( fullscreenControls ) fullscreenControls->attachVout( p_nvout );
648     }
649     return ret;
650 }
651
652 /* Call from the WindowClose function */
653 void MainInterface::releaseVideo( void )
654 {
655     if( fullscreenControls ) fullscreenControls->detachVout();
656     emit askReleaseVideo( );
657 }
658
659 /* Function that is CONNECTED to the previous emit */
660 void MainInterface::releaseVideoSlot( void )
661 {
662     videoWidget->release( );
663
664     if( bgWasVisible )
665     {
666         /* Reset the bg state */
667         bgWasVisible = false;
668         bgWidget->show();
669     }
670
671     videoIsActive = false;
672
673     /* Try to resize, except when you are in Fullscreen mode */
674     if( !isFullScreen() ) doComponentsUpdate();
675 }
676
677 /* Call from WindowControl function */
678 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
679 {
680     int i_ret = VLC_SUCCESS;
681     switch( i_query )
682     {
683         case VOUT_GET_SIZE:
684         {
685             unsigned int *pi_width  = va_arg( args, unsigned int * );
686             unsigned int *pi_height = va_arg( args, unsigned int * );
687             *pi_width = videoWidget->videoSize.width();
688             *pi_height = videoWidget->videoSize.height();
689             break;
690         }
691         case VOUT_SET_SIZE:
692         {
693             unsigned int i_width  = va_arg( args, unsigned int );
694             unsigned int i_height = va_arg( args, unsigned int );
695             emit askVideoToResize( i_width, i_height );
696             emit askUpdate();
697             break;
698         }
699         case VOUT_SET_STAY_ON_TOP:
700         {
701             int i_arg = va_arg( args, int );
702             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
703             break;
704         }
705         default:
706             i_ret = VLC_EGENERIC;
707             msg_Warn( p_intf, "unsupported control query" );
708             break;
709     }
710     return i_ret;
711 }
712
713 /*****************************************************************************
714  * Playlist, Visualisation and Menus handling
715  *****************************************************************************/
716 /**
717  * Toggle the playlist widget or dialog
718  **/
719 void MainInterface::togglePlaylist()
720 {
721     /* CREATION
722     If no playlist exist, then create one and attach it to the DockPL*/
723     if( !playlistWidget )
724     {
725         playlistWidget = new PlaylistWidget( p_intf );
726
727         i_pl_dock = PL_UNDOCKED;
728 /*        i_pl_dock = (pl_dock_e)getSettings()
729                          ->value( "pl-dock-status", PL_UNDOCKED ).toInt(); */
730
731         if( i_pl_dock == PL_UNDOCKED )
732         {
733             playlistWidget->setWindowFlags( Qt::Window );
734
735             /* This will restore the geometry but will not work for position,
736                because of parenting */
737             QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
738                     playlistWidget, QSize( 600, 300 ) );
739         }
740         else
741         {
742             mainLayout->insertWidget( 4, playlistWidget );
743         }
744         playlistVisible = true;
745
746         playlistWidget->show();
747     }
748     else
749     {
750     /* toggle the visibility of the playlist */
751        TOGGLEV( playlistWidget );
752        playlistVisible = !playlistVisible;
753        //doComponentsUpdate(); //resize( sizeHint() );
754     }
755 }
756
757 /* Function called from the menu to undock the playlist */
758 void MainInterface::undockPlaylist()
759 {
760 //    dockPL->setFloating( true );
761 //    adjustSize();
762 }
763
764 void MainInterface::dockPlaylist( pl_dock_e i_pos )
765 {
766 }
767
768 void MainInterface::toggleMinimalView()
769 {
770     /* HACK for minimalView, see menus.cpp */
771     if( !menuBar()->isVisible() ) QVLCMenu::minimalViewAction->toggle();
772
773     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
774         i_visualmode != QT_MINIMAL_MODE )
775     { /* NORMAL MODE then */
776         if( !videoWidget || videoWidget->isHidden() ) emit askBgWidgetToToggle();
777         else
778         {
779             /* If video is visible, then toggle the status of bgWidget */
780             bgWasVisible = !bgWasVisible;
781         }
782     }
783
784     TOGGLEV( menuBar() );
785     TOGGLEV( controls );
786     TOGGLEV( statusBar() );
787     doComponentsUpdate();
788 }
789
790 /* Video widget cannot do this synchronously as it runs in another thread */
791 /* Well, could it, actually ? Probably dangerous ... */
792
793 /* This function is called:
794    - toggling of minimal View
795    - through askUpdate() by Vout thread request video and resize video (zoom)
796    - Advanced buttons toggled
797  */
798 void MainInterface::doComponentsUpdate()
799 {
800     msg_Dbg( p_intf, "Updating the geometry" );
801     /* Here we resize to sizeHint() and not adjustsize because we want
802        the videoWidget to be exactly the correctSize */
803     resize( sizeHint() );
804     //    adjustSize()  ;
805 #ifndef NDEBUG
806     debug();
807 #endif
808 }
809
810 /* toggling advanced controls buttons */
811 void MainInterface::toggleAdvanced()
812 {
813     controls->toggleAdvanced();
814 //    if( fullscreenControls ) fullscreenControls->toggleAdvanced();
815 }
816
817 /* Get the visibility status of the controls (hidden or not, advanced or not) */
818 int MainInterface::getControlsVisibilityStatus()
819 {
820     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
821                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
822 }
823
824 #if 0
825 void MainInterface::visual()
826 {
827     if( !VISIBLE( visualSelector) )
828     {
829         visualSelector->show();
830         if( !THEMIM->getIM()->hasVideo() )
831         {
832             /* Show the background widget */
833         }
834         visualSelectorEnabled = true;
835     }
836     else
837     {
838         /* Stop any currently running visualization */
839         visualSelector->hide();
840         visualSelectorEnabled = false;
841     }
842     doComponentsUpdate();
843 }
844 #endif
845
846 /************************************************************************
847  * Other stuff
848  ************************************************************************/
849 void MainInterface::setName( QString name )
850 {
851     input_name = name; /* store it for the QSystray use */
852     /* Display it in the status bar, but also as a Tooltip in case it doesn't
853        fit in the label */
854     nameLabel->setText( " " + name + " " );
855     nameLabel->setToolTip( " " + name +" " );
856 }
857
858 void MainInterface::setStatus( int status )
859 {
860     msg_Dbg( p_intf, "Updating the stream status: %i", status );
861
862     speedControl->setEnable( THEMIM->getIM()->hasInput() );
863
864     /* And in the systray for the menu */
865     if( sysTray )
866         QVLCMenu::updateSystrayMenu( this, p_intf );
867 }
868
869 void MainInterface::setRate( int rate )
870 {
871     QString str;
872     str.setNum( ( 1000 / (double)rate ), 'f', 2 );
873     str.append( "x" );
874     speedLabel->setText( str );
875     speedLabel->setToolTip( str );
876     speedControl->updateControls( rate );
877 }
878
879 /*****************************************************************************
880  * Systray Icon and Systray Menu
881  *****************************************************************************/
882
883 /**
884  * Create a SystemTray icon and a menu that would go with it.
885  * Connects to a click handler on the icon.
886  **/
887 void MainInterface::createSystray()
888 {
889     QIcon iconVLC;
890     if( QDate::currentDate().dayOfYear() >= 354 )
891         iconVLC =  QIcon( QPixmap( ":/vlc128-christmas.png" ) );
892     else
893         iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
894     sysTray = new QSystemTrayIcon( iconVLC, this );
895     sysTray->setToolTip( qtr( "VLC media player" ));
896
897     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
898     systrayMenu->setIcon( iconVLC );
899
900     QVLCMenu::updateSystrayMenu( this, p_intf, true );
901     sysTray->show();
902
903     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
904             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
905 }
906
907 /**
908  * Updates the Systray Icon's menu and toggle the main interface
909  */
910 void MainInterface::toggleUpdateSystrayMenu()
911 {
912     /* If hidden, show it */
913     if( isHidden() )
914     {
915         show();
916         activateWindow();
917     }
918     else if( isMinimized() )
919     {
920         /* Minimized */
921         showNormal();
922         activateWindow();
923     }
924     else
925     {
926         /* Visible */
927 #ifdef WIN32
928         /* check if any visible window is above vlc in the z-order,
929          * but ignore the ones always on top
930          * and the ones which can't be activated */
931         WINDOWINFO wi;
932         HWND hwnd;
933         wi.cbSize = sizeof( WINDOWINFO );
934         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
935                 hwnd && ( !IsWindowVisible( hwnd ) ||
936                     ( GetWindowInfo( hwnd, &wi ) &&
937                       (wi.dwExStyle&WS_EX_NOACTIVATE) ) );
938                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
939         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
940                 (wi.dwExStyle&WS_EX_TOPMOST) )
941 #else
942         if( isActiveWindow() )
943 #endif
944         {
945             hide();
946         }
947         else
948         {
949             activateWindow();
950         }
951     }
952     QVLCMenu::updateSystrayMenu( this, p_intf );
953 }
954
955 void MainInterface::handleSystrayClick(
956                                     QSystemTrayIcon::ActivationReason reason )
957 {
958     switch( reason )
959     {
960         case QSystemTrayIcon::Trigger:
961             toggleUpdateSystrayMenu();
962             break;
963         case QSystemTrayIcon::MiddleClick:
964             sysTray->showMessage( qtr( "VLC media player" ),
965                     qtr( "Control menu for the player" ),
966                     QSystemTrayIcon::Information, 3000 );
967             break;
968     }
969 }
970
971 /**
972  * Updates the name of the systray Icon tooltip.
973  * Doesn't check if the systray exists, check before you call it.
974  **/
975 void MainInterface::updateSystrayTooltipName( QString name )
976 {
977     if( name.isEmpty() )
978     {
979         sysTray->setToolTip( qtr( "VLC media player" ) );
980     }
981     else
982     {
983         sysTray->setToolTip( name );
984         if( notificationEnabled && ( isHidden() || isMinimized() ) )
985         {
986             sysTray->showMessage( qtr( "VLC media player" ), name,
987                     QSystemTrayIcon::NoIcon, 3000 );
988         }
989     }
990 }
991
992 /**
993  * Updates the status of the systray Icon tooltip.
994  * Doesn't check if the systray exists, check before you call it.
995  **/
996 void MainInterface::updateSystrayTooltipStatus( int i_status )
997 {
998     switch( i_status )
999     {
1000         case  0:
1001         case  END_S:
1002             {
1003                 sysTray->setToolTip( qtr( "VLC media player" ) );
1004                 break;
1005             }
1006         case PLAYING_S:
1007             {
1008                 sysTray->setToolTip( input_name );
1009                 break;
1010             }
1011         case PAUSE_S:
1012             {
1013                 sysTray->setToolTip( input_name + " - "
1014                         + qtr( "Paused") );
1015                 break;
1016             }
1017     }
1018 }
1019
1020 /************************************************************************
1021  * D&D Events
1022  ************************************************************************/
1023 void MainInterface::dropEvent(QDropEvent *event)
1024 {
1025     dropEventPlay( event, true );
1026 }
1027
1028 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
1029 {
1030      const QMimeData *mimeData = event->mimeData();
1031
1032      /* D&D of a subtitles file, add it on the fly */
1033      if( mimeData->urls().size() == 1 )
1034      {
1035         if( THEMIM->getIM()->hasInput() )
1036         {
1037             if( !input_AddSubtitle( THEMIM->getInput(),
1038                                     qtu( toNativeSeparators(
1039                                          mimeData->urls()[0].toLocalFile() ) ),
1040                                     true ) )
1041             {
1042                 event->acceptProposedAction();
1043                 return;
1044             }
1045         }
1046      }
1047      bool first = b_play;
1048      foreach( QUrl url, mimeData->urls() )
1049      {
1050         QString s = toNativeSeparators( url.toLocalFile() );
1051
1052         if( s.length() > 0 ) {
1053             playlist_Add( THEPL, qtu(s), NULL,
1054                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: 0),
1055                           PLAYLIST_END, true, false );
1056             first = false;
1057             RecentsMRL::getInstance( p_intf )->addRecent( s );
1058         }
1059      }
1060      event->acceptProposedAction();
1061 }
1062 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1063 {
1064      event->acceptProposedAction();
1065 }
1066 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1067 {
1068      event->acceptProposedAction();
1069 }
1070 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1071 {
1072      event->accept();
1073 }
1074
1075 /************************************************************************
1076  * Events stuff
1077  ************************************************************************/
1078 void MainInterface::customEvent( QEvent *event )
1079 {
1080 #if 0
1081     if( event->type() == PLDockEvent_Type )
1082     {
1083         PlaylistDialog::killInstance();
1084         playlistEmbeddedFlag = true;
1085         menuBar()->clear();
1086         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1087         togglePlaylist();
1088     }
1089 #endif
1090     /*else */
1091     if ( event->type() == (int)SetVideoOnTopEvent_Type )
1092     {
1093         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1094         if( p_event->OnTop() )
1095             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1096         else
1097             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
1098         show(); /* necessary to apply window flags?? */
1099     }
1100 }
1101
1102 void MainInterface::keyPressEvent( QKeyEvent *e )
1103 {
1104     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1105           && menuBar()->isHidden() )
1106     {
1107         toggleMinimalView();
1108         e->accept();
1109     }
1110
1111     int i_vlck = qtEventToVLCKey( e );
1112     if( i_vlck > 0 )
1113     {
1114         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1115         e->accept();
1116     }
1117     else
1118         e->ignore();
1119 }
1120
1121 void MainInterface::wheelEvent( QWheelEvent *e )
1122 {
1123     int i_vlckey = qtWheelEventToVLCKey( e );
1124     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1125     e->accept();
1126 }
1127
1128 void MainInterface::closeEvent( QCloseEvent *e )
1129 {
1130     hide();
1131     THEDP->quit();
1132 }
1133
1134 void MainInterface::toggleFullScreen( void )
1135 {
1136     if( isFullScreen() )
1137     {
1138         showNormal();
1139         emit askUpdate(); // Needed if video was launched after the F11
1140     }
1141     else
1142         showFullScreen();
1143 }
1144
1145 /*****************************************************************************
1146  * Callbacks
1147  *****************************************************************************/
1148 static int InteractCallback( vlc_object_t *p_this,
1149                              const char *psz_var, vlc_value_t old_val,
1150                              vlc_value_t new_val, void *param )
1151 {
1152     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1153     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1154     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1155     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1156     return VLC_SUCCESS;
1157 }
1158
1159 /*****************************************************************************
1160  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1161  *  We don't show the menu directly here because we don't want the
1162  *  caller to block for a too long time.
1163  *****************************************************************************/
1164 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1165                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1166 {
1167     intf_thread_t *p_intf = (intf_thread_t *)param;
1168
1169     if( p_intf->pf_show_dialog )
1170     {
1171         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1172                                 new_val.b_bool, 0 );
1173     }
1174
1175     return VLC_SUCCESS;
1176 }
1177
1178 /*****************************************************************************
1179  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1180  *****************************************************************************/
1181 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1182                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1183 {
1184     intf_thread_t *p_intf = (intf_thread_t *)param;
1185     p_intf->p_sys->p_mi->toggleFSC();
1186
1187     /* Show event */
1188      return VLC_SUCCESS;
1189 }
1190
1191 /*****************************************************************************
1192  * updateRecentsMenu: event called by RecentsMRL
1193  *****************************************************************************/
1194
1195 void MainInterface::updateRecentsMenu()
1196 {
1197     QVLCMenu::updateRecents( p_intf );
1198 }