]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
[Qt] Fix a crash when toggling minimalView when --no-embedded-video.
[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     /* It is also connected to the control->slider, see the ControlsWidget */
156     /* Change the SpeedRate in the Status */
157     CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
158
159     /**
160      * Connects on nameChanged()
161      * Those connects are not merged because different options can trigger
162      * them down.
163      */
164     /* Naming in the controller statusbar */
165     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
166              setName( QString ) );
167     /* and in the systray */
168     if( sysTray )
169     {
170         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
171                  updateSystrayTooltipName( QString ) );
172     }
173     /* and in the title of the controller */
174     if( config_GetInt( p_intf, "qt-name-in-title" ) )
175     {
176         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
177              setVLCWindowsTitle( QString ) );
178     }
179
180     /**
181      * CONNECTS on PLAY_STATUS
182      **/
183     /* Status on the main controller */
184     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
185     /* and in the systray */
186     if( sysTray )
187     {
188         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
189                  updateSystrayTooltipStatus( int ) );
190     }
191
192     /* END CONNECTS ON IM */
193
194
195     /************
196      * Callbacks
197      ************/
198     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
199     var_AddCallback( p_intf, "interaction", InteractCallback, this );
200     p_intf->b_interaction = true;
201
202     var_AddCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
203
204     /* Register callback for the intf-popupmenu variable */
205     var_AddCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
206
207
208     /* VideoWidget connects to avoid different threads speaking to each other */
209     CONNECT( this, askReleaseVideo( void * ),
210              this, releaseVideoSlot( void * ) );
211     if( videoWidget )
212         CONNECT( this, askVideoToResize( unsigned int, unsigned int ),
213                  videoWidget, SetSizing( unsigned int, unsigned int ) );
214
215     CONNECT( this, askUpdate(), this, doComponentsUpdate() );
216
217     /* Size and placement of interface */
218     settings->beginGroup( "MainWindow" );
219     QVLCTools::restoreWidgetPosition( settings, this, QSize(380, 60) );
220
221     bool b_visible = settings->value( "playlist-visible", 0 ).toInt();
222     settings->endGroup();
223
224     /* Playlist */
225     if( b_visible ) togglePlaylist();
226
227     /* Final sizing and showing */
228     setMinimumWidth( __MAX( controls->sizeHint().width(),
229                             menuBar()->sizeHint().width() ) );
230     show();
231
232     /* And switch to minimal view if needed
233        Must be called after the show() */
234     if( i_visualmode == QT_MINIMAL_MODE )
235         toggleMinimalView();
236
237     /* Update the geometry : It is useful if you switch between
238        qt-display-modes ?*/
239     updateGeometry();
240     resize( sizeHint() );
241
242     /*****************************************************
243      * End everything by creating the Systray Management *
244      *****************************************************/
245     initSystray();
246 }
247
248 MainInterface::~MainInterface()
249 {
250     msg_Dbg( p_intf, "Destroying the main interface" );
251
252     if( videoIsActive ) videoWidget->hide();
253
254     if( playlistWidget )
255     {
256         if( !isDocked() )
257             QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
258
259         delete playlistWidget;
260     }
261
262     settings->beginGroup( "MainWindow" );
263
264     settings->setValue( "pl-dock-status", (int)i_pl_dock );
265     settings->setValue( "playlist-visible", (int)playlistVisible );
266     settings->setValue( "adv-controls",
267                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
268
269     if( bgWidget )
270         settings->setValue( "backgroundSize", bgWidget->size() );
271
272     QVLCTools::saveWidgetPosition(settings, this);
273     settings->endGroup();
274
275     var_DelCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
276
277     /* Unregister callback for the intf-popupmenu variable */
278     var_DelCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
279
280     p_intf->b_interaction = false;
281     var_DelCallback( p_intf, "interaction", InteractCallback, this );
282
283     p_intf->p_sys->p_mi = NULL;
284 }
285
286 /*****************************
287  *   Main UI handling        *
288  *****************************/
289
290 inline void MainInterface::createStatusBar()
291 {
292     /****************
293      *  Status Bar  *
294      ****************/
295     /* Widgets Creation*/
296     timeLabel = new TimeLabel( p_intf );
297     nameLabel = new QLabel;
298     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
299                                       | Qt::TextSelectableByKeyboard );
300     speedLabel = new SpeedLabel( p_intf, "1.00x" );
301     speedLabel->setToolTip(
302             qtr( "Current playback speed.\nRight click to adjust" ) );
303     speedLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
304
305     /* Styling those labels */
306     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
307     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
308     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
309
310     /* and adding those */
311     statusBar()->addWidget( nameLabel, 8 );
312     statusBar()->addPermanentWidget( speedLabel, 0 );
313     statusBar()->addPermanentWidget( timeLabel, 0 );
314
315     /* timeLabel behaviour:
316        - double clicking opens the goto time dialog
317        - right-clicking and clicking just toggle between remaining and
318          elapsed time.*/
319     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
320
321     /* Speed Label behaviour:
322        - right click gives the vertical speed slider */
323     CONNECT( speedLabel, customContextMenuRequested( QPoint ),
324              this, showSpeedMenu( QPoint ) );
325 }
326
327 inline void MainInterface::initSystray()
328 {
329     bool b_createSystray = false;
330     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
331     if( config_GetInt( p_intf, "qt-start-minimized") )
332     {
333         if( b_systrayAvailable )
334         {
335             b_createSystray = true;
336             hide();
337         }
338         else msg_Err( p_intf, "You can't minimize if you haven't a system "
339                 "tray bar" );
340     }
341     if( config_GetInt( p_intf, "qt-system-tray") )
342         b_createSystray = true;
343
344     if( b_systrayAvailable && b_createSystray )
345             createSystray();
346 }
347
348 /**
349  * Give the decorations of the Main Window a correct Name.
350  * If nothing is given, set it to VLC...
351  **/
352 void MainInterface::setVLCWindowsTitle( QString aTitle )
353 {
354     if( aTitle.isEmpty() )
355     {
356         setWindowTitle( qtr( "VLC media player" ) );
357     }
358     else
359     {
360         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
361     }
362 }
363
364 void MainInterface::handleMainUi( QSettings *settings )
365 {
366     /* Create the main Widget and the mainLayout */
367     QWidget *main = new QWidget;
368     setCentralWidget( main );
369     mainLayout = new QVBoxLayout( main );
370
371     /* Margins, spacing */
372     main->setContentsMargins( 0, 0, 0, 0 );
373     main->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
374     mainLayout->setSpacing( 0 );
375     mainLayout->setMargin( 0 );
376
377     /* Create the CONTROLS Widget */
378     controls = new ControlsWidget( p_intf,
379                    settings->value( "adv-controls", false ).toBool() );
380     CONNECT( controls, advancedControlsToggled( bool ),
381              this, doComponentsUpdate() );
382
383     /* Add the controls Widget to the main Widget */
384     mainLayout->insertWidget( 0, controls, 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 *p_win )
654 {
655     if( fullscreenControls ) fullscreenControls->detachVout();
656     if( p_win )
657         emit askReleaseVideo( p_win );
658 }
659
660 /* Function that is CONNECTED to the previous emit */
661 void MainInterface::releaseVideoSlot( void *p_win )
662 {
663     videoWidget->release( p_win );
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         WINDOWINFO wi;
932         HWND hwnd;
933         wi.cbSize = sizeof( WINDOWINFO );
934         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
935                 hwnd && !IsWindowVisible( hwnd );
936                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
937         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
938                 (wi.dwExStyle&WS_EX_TOPMOST) )
939 #else
940         if( isActiveWindow() )
941 #endif
942         {
943             hide();
944         }
945         else
946         {
947             activateWindow();
948         }
949     }
950     QVLCMenu::updateSystrayMenu( this, p_intf );
951 }
952
953 void MainInterface::handleSystrayClick(
954                                     QSystemTrayIcon::ActivationReason reason )
955 {
956     switch( reason )
957     {
958         case QSystemTrayIcon::Trigger:
959             toggleUpdateSystrayMenu();
960             break;
961         case QSystemTrayIcon::MiddleClick:
962             sysTray->showMessage( qtr( "VLC media player" ),
963                     qtr( "Control menu for the player" ),
964                     QSystemTrayIcon::Information, 3000 );
965             break;
966     }
967 }
968
969 /**
970  * Updates the name of the systray Icon tooltip.
971  * Doesn't check if the systray exists, check before you call it.
972  **/
973 void MainInterface::updateSystrayTooltipName( QString name )
974 {
975     if( name.isEmpty() )
976     {
977         sysTray->setToolTip( qtr( "VLC media player" ) );
978     }
979     else
980     {
981         sysTray->setToolTip( name );
982         if( notificationEnabled && ( isHidden() || isMinimized() ) )
983         {
984             sysTray->showMessage( qtr( "VLC media player" ), name,
985                     QSystemTrayIcon::NoIcon, 3000 );
986         }
987     }
988 }
989
990 /**
991  * Updates the status of the systray Icon tooltip.
992  * Doesn't check if the systray exists, check before you call it.
993  **/
994 void MainInterface::updateSystrayTooltipStatus( int i_status )
995 {
996     switch( i_status )
997     {
998         case  0:
999         case  END_S:
1000             {
1001                 sysTray->setToolTip( qtr( "VLC media player" ) );
1002                 break;
1003             }
1004         case PLAYING_S:
1005             {
1006                 sysTray->setToolTip( input_name );
1007                 break;
1008             }
1009         case PAUSE_S:
1010             {
1011                 sysTray->setToolTip( input_name + " - "
1012                         + qtr( "Paused") );
1013                 break;
1014             }
1015     }
1016 }
1017
1018 /************************************************************************
1019  * D&D Events
1020  ************************************************************************/
1021 void MainInterface::dropEvent(QDropEvent *event)
1022 {
1023     dropEventPlay( event, true );
1024 }
1025
1026 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
1027 {
1028      const QMimeData *mimeData = event->mimeData();
1029
1030      /* D&D of a subtitles file, add it on the fly */
1031      if( mimeData->urls().size() == 1 )
1032      {
1033         if( THEMIM->getIM()->hasInput() )
1034         {
1035             if( input_AddSubtitles( THEMIM->getInput(),
1036                                     qtu( toNativeSeparators(
1037                                          mimeData->urls()[0].toLocalFile() ) ),
1038                                     true ) )
1039             {
1040                 event->acceptProposedAction();
1041                 return;
1042             }
1043         }
1044      }
1045      bool first = b_play;
1046      foreach( QUrl url, mimeData->urls() )
1047      {
1048         QString s = toNativeSeparators( url.toLocalFile() );
1049
1050         if( s.length() > 0 ) {
1051             playlist_Add( THEPL, qtu(s), NULL,
1052                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: 0),
1053                           PLAYLIST_END, true, false );
1054             first = false;
1055             RecentsMRL::getInstance( p_intf )->addRecent( s );
1056         }
1057      }
1058      event->acceptProposedAction();
1059 }
1060 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1061 {
1062      event->acceptProposedAction();
1063 }
1064 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1065 {
1066      event->acceptProposedAction();
1067 }
1068 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1069 {
1070      event->accept();
1071 }
1072
1073 /************************************************************************
1074  * Events stuff
1075  ************************************************************************/
1076 void MainInterface::customEvent( QEvent *event )
1077 {
1078 #if 0
1079     if( event->type() == PLDockEvent_Type )
1080     {
1081         PlaylistDialog::killInstance();
1082         playlistEmbeddedFlag = true;
1083         menuBar()->clear();
1084         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1085         togglePlaylist();
1086     }
1087 #endif
1088     /*else */
1089     if ( event->type() == (int)SetVideoOnTopEvent_Type )
1090     {
1091         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1092         if( p_event->OnTop() )
1093             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1094         else
1095             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
1096         show(); /* necessary to apply window flags?? */
1097     }
1098 }
1099
1100 void MainInterface::keyPressEvent( QKeyEvent *e )
1101 {
1102     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1103           && menuBar()->isHidden() )
1104     {
1105         toggleMinimalView();
1106         e->accept();
1107     }
1108
1109     int i_vlck = qtEventToVLCKey( e );
1110     if( i_vlck > 0 )
1111     {
1112         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1113         e->accept();
1114     }
1115     else
1116         e->ignore();
1117 }
1118
1119 void MainInterface::wheelEvent( QWheelEvent *e )
1120 {
1121     int i_vlckey = qtWheelEventToVLCKey( e );
1122     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1123     e->accept();
1124 }
1125
1126 void MainInterface::closeEvent( QCloseEvent *e )
1127 {
1128     hide();
1129     THEDP->quit();
1130 }
1131
1132 void MainInterface::toggleFullScreen( void )
1133 {
1134     if( isFullScreen() )
1135     {
1136         showNormal();
1137         emit askUpdate(); // Needed if video was launched after the F11
1138     }
1139     else
1140         showFullScreen();
1141 }
1142
1143 /*****************************************************************************
1144  * Callbacks
1145  *****************************************************************************/
1146 static int InteractCallback( vlc_object_t *p_this,
1147                              const char *psz_var, vlc_value_t old_val,
1148                              vlc_value_t new_val, void *param )
1149 {
1150     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1151     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1152     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1153     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1154     return VLC_SUCCESS;
1155 }
1156
1157 /*****************************************************************************
1158  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1159  *  We don't show the menu directly here because we don't want the
1160  *  caller to block for a too long time.
1161  *****************************************************************************/
1162 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1163                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1164 {
1165     intf_thread_t *p_intf = (intf_thread_t *)param;
1166
1167     if( p_intf->pf_show_dialog )
1168     {
1169         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1170                                 new_val.b_bool, 0 );
1171     }
1172
1173     return VLC_SUCCESS;
1174 }
1175
1176 /*****************************************************************************
1177  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1178  *****************************************************************************/
1179 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1180                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1181 {
1182     intf_thread_t *p_intf = (intf_thread_t *)param;
1183     p_intf->p_sys->p_mi->toggleFSC();
1184
1185     /* Show event */
1186      return VLC_SUCCESS;
1187 }
1188
1189 /*****************************************************************************
1190  * updateRecentsMenu: event called by RecentsMRL
1191  *****************************************************************************/
1192
1193 void MainInterface::updateRecentsMenu()
1194 {
1195     QVLCMenu::updateRecents( p_intf );
1196 }