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