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