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