]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Remove unused VOUT_GET_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
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     setStatusBar( 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 QStatusBar * MainInterface::createStatusBar()
315 {
316     QStatusBar *statusBar = new QStatusBar;
317
318     /****************
319      *  Status Bar  *
320      ****************/
321     /* Widgets Creation*/
322     TimeLabel *timeLabel = new TimeLabel( p_intf );
323     nameLabel = new QLabel( this );
324     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
325                                       | Qt::TextSelectableByKeyboard );
326     SpeedLabel *speedLabel = new SpeedLabel( p_intf, "1.00x", this );
327
328     /* Styling those labels */
329     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
330     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
331     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
332
333     /* and adding those */
334     statusBar->addWidget( nameLabel, 8 );
335     statusBar->addPermanentWidget( speedLabel, 0 );
336     statusBar->addPermanentWidget( timeLabel, 0 );
337
338     /* timeLabel behaviour:
339        - double clicking opens the goto time dialog
340        - right-clicking and clicking just toggle between remaining and
341          elapsed time.*/
342     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
343     return statusBar;
344 }
345
346 inline void MainInterface::initSystray()
347 {
348     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
349     bool b_systrayWanted = config_GetInt( p_intf, "qt-system-tray" );
350
351     if( config_GetInt( p_intf, "qt-start-minimized") > 0 )
352     {
353         if( b_systrayAvailable )
354         {
355             b_systrayWanted = true;
356             hide();
357         }
358         else
359             msg_Err( p_intf, "cannot start minimized without system tray bar" );
360     }
361
362     if( b_systrayAvailable && b_systrayWanted )
363             createSystray();
364 }
365
366 /**
367  * Give the decorations of the Main Window a correct Name.
368  * If nothing is given, set it to VLC...
369  **/
370 void MainInterface::setVLCWindowsTitle( QString aTitle )
371 {
372     if( aTitle.isEmpty() )
373     {
374         setWindowTitle( qtr( "VLC media player" ) );
375     }
376     else
377     {
378         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
379     }
380 }
381
382 void MainInterface::handleMainUi( QSettings *settings )
383 {
384     /* Create the main Widget and the mainLayout */
385     QWidget *main = new QWidget;
386     setCentralWidget( main );
387     mainLayout = new QVBoxLayout( main );
388
389     /* Margins, spacing */
390     main->setContentsMargins( 0, 0, 0, 0 );
391     main->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
392     mainLayout->setSpacing( 0 );
393     mainLayout->setMargin( 0 );
394
395     /* Create the CONTROLS Widget */
396     controls = new ControlsWidget( p_intf,
397                    settings->value( "adv-controls", false ).toBool(), this );
398     CONNECT( controls, advancedControlsToggled( bool ),
399              this, doComponentsUpdate() );
400     inputC = new InputControlsWidget( p_intf, this );
401
402         /* Visualisation */
403     /* Disabled for now, they SUCK */
404     #if 0
405     visualSelector = new VisualSelector( p_intf );
406     mainLayout->insertWidget( 0, visualSelector );
407     visualSelector->hide();
408     #endif
409
410     /* Bg Cone */
411     bgWidget = new BackgroundWidget( p_intf );
412     bgWidget->resize(
413             settings->value( "backgroundSize", QSize( 300, 200 ) ).toSize() );
414     bgWidget->updateGeometry();
415     CONNECT( this, askBgWidgetToToggle(), bgWidget, toggle() );
416
417     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
418         i_visualmode != QT_MINIMAL_MODE )
419     {
420         bgWidget->hide();
421     }
422
423     /* And video Outputs */
424     if( videoEmbeddedFlag )
425         videoWidget = new VideoWidget( p_intf );
426
427     /* Add the controls Widget to the main Widget */
428     mainLayout->insertWidget( 0, bgWidget );
429     if( videoWidget ) mainLayout->insertWidget( 0, videoWidget, 10 );
430     mainLayout->insertWidget( 2, inputC, 0, Qt::AlignBottom );
431     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
432                               controls, 0, Qt::AlignBottom );
433
434     /* Finish the sizing */
435     main->updateGeometry();
436
437     getSettings()->endGroup();
438 #ifdef WIN32
439     if ( depth() > 8 )
440 #endif
441     /* Create the FULLSCREEN CONTROLS Widget */
442     if( config_GetInt( p_intf, "qt-fs-controller" ) )
443     {
444         fullscreenControls = new FullscreenControllerWidget( p_intf );
445     }
446 }
447
448 inline void MainInterface::askForPrivacy()
449 {
450     /**
451      * Ask for the network policy on FIRST STARTUP
452      **/
453     if( config_GetInt( p_intf, "qt-privacy-ask") )
454     {
455         QList<ConfigControl *> controls;
456         if( privacyDialog( &controls ) == QDialog::Accepted )
457         {
458             QList<ConfigControl *>::Iterator i;
459             for(  i = controls.begin() ; i != controls.end() ; i++ )
460             {
461                 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
462                 c->doApply( p_intf );
463             }
464
465             config_PutInt( p_intf,  "qt-privacy-ask" , 0 );
466             /* We have to save here because the user may not launch Prefs */
467             config_SaveConfigFile( p_intf, NULL );
468         }
469     }
470 }
471
472 int MainInterface::privacyDialog( QList<ConfigControl *> *controls )
473 {
474     QDialog *privacy = new QDialog( this );
475
476     privacy->setWindowTitle( qtr( "Privacy and Network Policies" ) );
477
478     QGridLayout *gLayout = new QGridLayout( privacy );
479
480     QGroupBox *blabla = new QGroupBox( qtr( "Privacy and Network Warning" ) );
481     QGridLayout *blablaLayout = new QGridLayout( blabla );
482     QLabel *text = new QLabel( qtr(
483         "<p>The <i>VideoLAN Team</i> doesn't like when an application goes "
484         "online without authorization.</p>\n "
485         "<p><i>VLC media player</i> can retreive limited information from "
486         "the Internet in order to get CD covers or to check "
487         "for available updates.</p>\n"
488         "<p><i>VLC media player</i> <b>DOES NOT</b> send or collect <b>ANY</b> "
489         "information, even anonymously, about your usage.</p>\n"
490         "<p>Therefore please select from the following options, the default being "
491         "almost no access to the web.</p>\n") );
492     text->setWordWrap( true );
493     text->setTextFormat( Qt::RichText );
494
495     blablaLayout->addWidget( text, 0, 0 ) ;
496
497     QGroupBox *options = new QGroupBox;
498     QGridLayout *optionsLayout = new QGridLayout( options );
499
500     gLayout->addWidget( blabla, 0, 0, 1, 3 );
501     gLayout->addWidget( options, 1, 0, 1, 3 );
502     module_config_t *p_config;
503     ConfigControl *control;
504     int line = 0;
505
506 #define CONFIG_GENERIC( option, type )                            \
507     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
508     if( p_config )                                                \
509     {                                                             \
510         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
511                 p_config, options, false, optionsLayout, line );  \
512         controls->append( control );                               \
513     }
514
515 #define CONFIG_GENERIC_NOBOOL( option, type )                     \
516     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
517     if( p_config )                                                \
518     {                                                             \
519         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
520                 p_config, options, optionsLayout, line );  \
521         controls->append( control );                               \
522     }
523
524     CONFIG_GENERIC( "album-art", IntegerList ); line++;
525 #ifdef UPDATE_CHECK
526     CONFIG_GENERIC_NOBOOL( "qt-updates-notif", Bool ); line++;
527     CONFIG_GENERIC_NOBOOL( "qt-updates-days", Integer ); line++;
528 #endif
529
530     QPushButton *ok = new QPushButton( qtr( "OK" ) );
531
532     gLayout->addWidget( ok, 2, 2 );
533
534     CONNECT( ok, clicked(), privacy, accept() );
535     return privacy->exec();
536 }
537
538
539 /**********************************************************************
540  * Handling of sizing of the components
541  **********************************************************************/
542
543 /* This function is probably wrong, but we don't have many many choices...
544    Since we can't know from the playlist Widget if we are inside a dock or not,
545    because the playlist Widget can be called by THEDP, as a separate windows for
546    the skins.
547    Maybe the other solution is to redefine the sizeHint() of the playlist and
548    ask _parent->isFloating()...
549    If you think this would be better, please FIXME it...
550 */
551
552 QSize MainInterface::sizeHint() const
553 {
554     if( b_keep_size )
555     {
556         if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
557             i_visualmode == QT_MINIMAL_MODE )
558         {
559                 return mainVideoSize;
560         }
561         else
562         {
563             if( VISIBLE( bgWidget) ||
564                 ( videoIsActive && videoWidget->isVisible() )
565               )
566                 return mainVideoSize;
567             else
568                 return mainBasedSize;
569         }
570     }
571
572     int nwidth  = controls->sizeHint().width();
573     int nheight = controls->isVisible() ?
574                   controls->size().height()
575                   + inputC->size().height()
576                   + menuBar()->size().height()
577                   + statusBar()->size().height()
578                   : 0 ;
579
580     if( VISIBLE( bgWidget ) )
581     {
582         nheight += bgWidget->size().height();
583         nwidth  = bgWidget->size().width();
584     }
585     else if( videoIsActive && videoWidget->isVisible() )
586     {
587         nheight += videoWidget->sizeHint().height();
588         nwidth  = videoWidget->sizeHint().width();
589     }
590 #if 0
591     if( !dockPL->isFloating() && dockPL->isVisible() && dockPL->widget()  )
592     {
593         nheight += dockPL->size().height();
594         nwidth = __MAX( nwidth, dockPL->size().width() );
595         msg_Warn( p_intf, "3 %i %i", nheight, nwidth );
596     }
597 #endif
598     return QSize( nwidth, nheight );
599 }
600
601 void MainInterface::toggleFSC()
602 {
603    if( !fullscreenControls ) return;
604
605    IMEvent *eShow = new IMEvent( FullscreenControlToggle_Type, 0 );
606    QApplication::postEvent( fullscreenControls, eShow );
607 }
608
609 void MainInterface::debug()
610 {
611 #ifndef NDEBUG
612     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
613     msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
614     if( videoWidget && videoWidget->isVisible() )
615     {
616         msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
617         msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
618     }
619 #endif
620 }
621
622 /****************************************************************************
623  * Video Handling
624  ****************************************************************************/
625
626 /* This event is used to deal with the fullscreen and always on top
627    issue conflict (bug in wx) */
628 class SetVideoOnTopQtEvent : public QEvent
629 {
630 public:
631     SetVideoOnTopQtEvent( bool _onTop ) :
632       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
633     {}
634
635     bool OnTop() const { return onTop; }
636
637 private:
638     bool onTop;
639 };
640
641 /**
642  * README
643  * Thou shall not call/resize/hide widgets from on another thread.
644  * This is wrong, and this is THE reason to emit signals on those Video Functions
645  **/
646 WId MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
647                                  int *pi_y, unsigned int *pi_width,
648                                  unsigned int *pi_height )
649 {
650     /* Request the videoWidget */
651     WId ret = videoWidget->request( p_nvout,pi_x, pi_y,
652                                     pi_width, pi_height, b_keep_size );
653     if( ret ) /* The videoWidget is available */
654     {
655         /* Did we have a bg ? Hide it! */
656         if( VISIBLE( bgWidget) )
657         {
658             bgWasVisible = true;
659             emit askBgWidgetToToggle();
660         }
661         else
662             bgWasVisible = false;
663
664         /* Consider the video active now */
665         videoIsActive = true;
666
667         emit askUpdate();
668     }
669     return ret;
670 }
671
672 /* Call from the WindowClose function */
673 void MainInterface::releaseVideo( void )
674 {
675     emit askReleaseVideo( );
676 }
677
678 /* Function that is CONNECTED to the previous emit */
679 void MainInterface::releaseVideoSlot( void )
680 {
681     videoWidget->release( );
682
683     if( bgWasVisible )
684     {
685         /* Reset the bg state */
686         bgWasVisible = false;
687         bgWidget->show();
688     }
689
690     videoIsActive = false;
691
692     /* Try to resize, except when you are in Fullscreen mode */
693     if( !isFullScreen() ) doComponentsUpdate();
694 }
695
696 /* Call from WindowControl function */
697 int MainInterface::controlVideo( int i_query, va_list args )
698 {
699     int i_ret = VLC_SUCCESS;
700     switch( i_query )
701     {
702         case VOUT_SET_SIZE:
703         {
704             unsigned int i_width  = va_arg( args, unsigned int );
705             unsigned int i_height = va_arg( args, unsigned int );
706             emit askVideoToResize( i_width, i_height );
707             emit askUpdate();
708             break;
709         }
710         case VOUT_SET_STAY_ON_TOP:
711         {
712             int i_arg = va_arg( args, int );
713             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
714             break;
715         }
716         default:
717             i_ret = VLC_EGENERIC;
718             msg_Warn( p_intf, "unsupported control query" );
719             break;
720     }
721     return i_ret;
722 }
723
724 /*****************************************************************************
725  * Playlist, Visualisation and Menus handling
726  *****************************************************************************/
727 /**
728  * Toggle the playlist widget or dialog
729  **/
730 void MainInterface::togglePlaylist()
731 {
732     /* CREATION
733     If no playlist exist, then create one and attach it to the DockPL*/
734     if( !playlistWidget )
735     {
736         playlistWidget = new PlaylistWidget( p_intf );
737
738         i_pl_dock = PL_UNDOCKED;
739 /*        i_pl_dock = (pl_dock_e)getSettings()
740                          ->value( "pl-dock-status", PL_UNDOCKED ).toInt(); */
741
742         if( i_pl_dock == PL_UNDOCKED )
743         {
744             playlistWidget->setWindowFlags( Qt::Window );
745
746             /* This will restore the geometry but will not work for position,
747                because of parenting */
748             QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
749                     playlistWidget, QSize( 600, 300 ) );
750         }
751         else
752         {
753             mainLayout->insertWidget( 4, playlistWidget );
754         }
755         playlistVisible = true;
756
757         playlistWidget->show();
758     }
759     else
760     {
761     /* toggle the visibility of the playlist */
762        TOGGLEV( playlistWidget );
763        playlistVisible = !playlistVisible;
764        //doComponentsUpdate(); //resize( sizeHint() );
765     }
766 }
767
768 /* Function called from the menu to undock the playlist */
769 void MainInterface::undockPlaylist()
770 {
771 //    dockPL->setFloating( true );
772 //    adjustSize();
773 }
774
775 void MainInterface::dockPlaylist( pl_dock_e i_pos )
776 {
777 }
778
779 void MainInterface::toggleMinimalView()
780 {
781     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
782         i_visualmode != QT_MINIMAL_MODE )
783     { /* NORMAL MODE then */
784         if( !videoWidget || videoWidget->isHidden() ) emit askBgWidgetToToggle();
785         else
786         {
787             /* If video is visible, then toggle the status of bgWidget */
788             bgWasVisible = !bgWasVisible;
789         }
790     }
791
792     TOGGLEV( menuBar() );
793     TOGGLEV( controls );
794     TOGGLEV( statusBar() );
795     TOGGLEV( inputC );
796     doComponentsUpdate();
797
798     QVLCMenu::minimalViewAction->setChecked( bgWasVisible );
799 }
800
801 /* Video widget cannot do this synchronously as it runs in another thread */
802 /* Well, could it, actually ? Probably dangerous ... */
803
804 /* This function is called:
805    - toggling of minimal View
806    - through askUpdate() by Vout thread request video and resize video (zoom)
807    - Advanced buttons toggled
808  */
809 void MainInterface::doComponentsUpdate()
810 {
811     msg_Dbg( p_intf, "Updating the geometry" );
812     /* Here we resize to sizeHint() and not adjustsize because we want
813        the videoWidget to be exactly the correctSize */
814     resize( sizeHint() );
815     //    adjustSize()  ;
816 #ifndef NDEBUG
817     debug();
818 #endif
819 }
820
821 /* toggling advanced controls buttons */
822 void MainInterface::toggleAdvanced()
823 {
824     controls->toggleAdvanced();
825 //    if( fullscreenControls ) fullscreenControls->toggleAdvanced();
826 }
827
828 /* Get the visibility status of the controls (hidden or not, advanced or not) */
829 int MainInterface::getControlsVisibilityStatus()
830 {
831     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
832                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
833 }
834
835 #if 0
836 void MainInterface::visual()
837 {
838     if( !VISIBLE( visualSelector) )
839     {
840         visualSelector->show();
841         if( !THEMIM->getIM()->hasVideo() )
842         {
843             /* Show the background widget */
844         }
845         visualSelectorEnabled = true;
846     }
847     else
848     {
849         /* Stop any currently running visualization */
850         visualSelector->hide();
851         visualSelectorEnabled = false;
852     }
853     doComponentsUpdate();
854 }
855 #endif
856
857 /************************************************************************
858  * Other stuff
859  ************************************************************************/
860 void MainInterface::setName( QString name )
861 {
862     input_name = name; /* store it for the QSystray use */
863     /* Display it in the status bar, but also as a Tooltip in case it doesn't
864        fit in the label */
865     nameLabel->setText( " " + name + " " );
866     nameLabel->setToolTip( " " + name +" " );
867 }
868
869 /*****************************************************************************
870  * Systray Icon and Systray Menu
871  *****************************************************************************/
872
873 /**
874  * Create a SystemTray icon and a menu that would go with it.
875  * Connects to a click handler on the icon.
876  **/
877 void MainInterface::createSystray()
878 {
879     QIcon iconVLC;
880     if( QDate::currentDate().dayOfYear() >= 354 )
881         iconVLC =  QIcon( QPixmap( ":/vlc128-christmas.png" ) );
882     else
883         iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
884     sysTray = new QSystemTrayIcon( iconVLC, this );
885     sysTray->setToolTip( qtr( "VLC media player" ));
886
887     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
888     systrayMenu->setIcon( iconVLC );
889
890     QVLCMenu::updateSystrayMenu( this, p_intf, true );
891     sysTray->show();
892
893     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
894             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
895 }
896
897 /**
898  * Updates the Systray Icon's menu and toggle the main interface
899  */
900 void MainInterface::toggleUpdateSystrayMenu()
901 {
902     /* If hidden, show it */
903     if( isHidden() )
904     {
905         show();
906         activateWindow();
907     }
908     else if( isMinimized() )
909     {
910         /* Minimized */
911         showNormal();
912         activateWindow();
913     }
914     else
915     {
916         /* Visible (possibly under other windows) */
917 #ifdef WIN32
918         /* check if any visible window is above vlc in the z-order,
919          * but ignore the ones always on top
920          * and the ones which can't be activated */
921         WINDOWINFO wi;
922         HWND hwnd;
923         wi.cbSize = sizeof( WINDOWINFO );
924         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
925                 hwnd && ( !IsWindowVisible( hwnd ) ||
926                     ( GetWindowInfo( hwnd, &wi ) &&
927                       (wi.dwExStyle&WS_EX_NOACTIVATE) ) );
928                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
929             if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
930                 (wi.dwExStyle&WS_EX_TOPMOST) )
931             {
932                 hide();
933             }
934             else
935             {
936                 activateWindow();
937             }
938 #else
939         hide();
940 #endif
941     }
942     QVLCMenu::updateSystrayMenu( this, p_intf );
943 }
944
945 void MainInterface::handleSystrayClick(
946                                     QSystemTrayIcon::ActivationReason reason )
947 {
948     switch( reason )
949     {
950         case QSystemTrayIcon::Trigger:
951         case QSystemTrayIcon::DoubleClick:
952             toggleUpdateSystrayMenu();
953             break;
954         case QSystemTrayIcon::MiddleClick:
955         case QSystemTrayIcon::Context:
956             sysTray->showMessage( qtr( "VLC media player" ),
957                     qtr( "Control menu for the player" ),
958                     QSystemTrayIcon::Information, 3000 );
959             break;
960         default:
961             break;
962     }
963 }
964
965 /**
966  * Updates the name of the systray Icon tooltip.
967  * Doesn't check if the systray exists, check before you call it.
968  **/
969 void MainInterface::updateSystrayTooltipName( QString name )
970 {
971     if( name.isEmpty() )
972     {
973         sysTray->setToolTip( qtr( "VLC media player" ) );
974     }
975     else
976     {
977         sysTray->setToolTip( name );
978         if( notificationEnabled && ( isHidden() || isMinimized() ) )
979         {
980             sysTray->showMessage( qtr( "VLC media player" ), name,
981                     QSystemTrayIcon::NoIcon, 3000 );
982         }
983     }
984
985     QVLCMenu::updateSystrayMenu( this, p_intf );
986 }
987
988 /**
989  * Updates the status of the systray Icon tooltip.
990  * Doesn't check if the systray exists, check before you call it.
991  **/
992 void MainInterface::updateSystrayTooltipStatus( int i_status )
993 {
994     switch( i_status )
995     {
996         case  0:
997         case  END_S:
998             {
999                 sysTray->setToolTip( qtr( "VLC media player" ) );
1000                 break;
1001             }
1002         case PLAYING_S:
1003             {
1004                 sysTray->setToolTip( input_name );
1005                 break;
1006             }
1007         case PAUSE_S:
1008             {
1009                 sysTray->setToolTip( input_name + " - "
1010                         + qtr( "Paused") );
1011                 break;
1012             }
1013     }
1014 }
1015
1016 /************************************************************************
1017  * D&D Events
1018  ************************************************************************/
1019 void MainInterface::dropEvent(QDropEvent *event)
1020 {
1021     dropEventPlay( event, true );
1022 }
1023
1024 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
1025 {
1026      const QMimeData *mimeData = event->mimeData();
1027
1028      /* D&D of a subtitles file, add it on the fly */
1029      if( mimeData->urls().size() == 1 )
1030      {
1031         if( THEMIM->getIM()->hasInput() )
1032         {
1033             if( !input_AddSubtitle( THEMIM->getInput(),
1034                                     qtu( toNativeSeparators(
1035                                          mimeData->urls()[0].toLocalFile() ) ),
1036                                     true ) )
1037             {
1038                 event->acceptProposedAction();
1039                 return;
1040             }
1041         }
1042      }
1043      bool first = b_play;
1044      foreach( const QUrl &url, mimeData->urls() )
1045      {
1046         QString s = toNativeSeparators( url.toLocalFile() );
1047
1048         if( s.length() > 0 ) {
1049             playlist_Add( THEPL, qtu(s), NULL,
1050                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: 0),
1051                           PLAYLIST_END, true, false );
1052             first = false;
1053             RecentsMRL::getInstance( p_intf )->addRecent( s );
1054         }
1055      }
1056      event->acceptProposedAction();
1057 }
1058 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1059 {
1060      event->acceptProposedAction();
1061 }
1062 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1063 {
1064      event->acceptProposedAction();
1065 }
1066 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1067 {
1068      event->accept();
1069 }
1070
1071 /************************************************************************
1072  * Events stuff
1073  ************************************************************************/
1074 void MainInterface::customEvent( QEvent *event )
1075 {
1076 #if 0
1077     if( event->type() == PLDockEvent_Type )
1078     {
1079         PlaylistDialog::killInstance();
1080         playlistEmbeddedFlag = true;
1081         menuBar()->clear();
1082         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1083         togglePlaylist();
1084     }
1085 #endif
1086     /*else */
1087     if ( event->type() == (int)SetVideoOnTopEvent_Type )
1088     {
1089         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1090         if( p_event->OnTop() )
1091             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1092         else
1093             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
1094         show(); /* necessary to apply window flags?? */
1095     }
1096 }
1097
1098 void MainInterface::keyPressEvent( QKeyEvent *e )
1099 {
1100     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1101           && menuBar()->isHidden() )
1102     {
1103         toggleMinimalView();
1104         e->accept();
1105     }
1106
1107     int i_vlck = qtEventToVLCKey( e );
1108     if( i_vlck > 0 )
1109     {
1110         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1111         e->accept();
1112     }
1113     else
1114         e->ignore();
1115 }
1116
1117 void MainInterface::resizeEvent( QResizeEvent * event )
1118 {
1119     if( b_keep_size )
1120     {
1121         if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
1122             i_visualmode == QT_MINIMAL_MODE )
1123         {
1124                 mainVideoSize = size();
1125         }
1126         else
1127         {
1128             if( VISIBLE( bgWidget) ||
1129                 ( videoIsActive && videoWidget->isVisible() )
1130               )
1131                 mainVideoSize = size();
1132             else
1133                 mainBasedSize = size();
1134         }
1135     }
1136 }
1137
1138 void MainInterface::wheelEvent( QWheelEvent *e )
1139 {
1140     int i_vlckey = qtWheelEventToVLCKey( e );
1141     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1142     e->accept();
1143 }
1144
1145 void MainInterface::closeEvent( QCloseEvent *e )
1146 {
1147     e->accept();
1148     hide();
1149     THEDP->quit();
1150 }
1151
1152 void MainInterface::toggleFullScreen( void )
1153 {
1154     if( isFullScreen() )
1155     {
1156         showNormal();
1157         emit askUpdate(); // Needed if video was launched after the F11
1158         QVLCMenu::fullscreenViewAction->setChecked( false );
1159     }
1160     else
1161     {
1162         showFullScreen();
1163         QVLCMenu::fullscreenViewAction->setChecked( true );
1164     }
1165
1166 }
1167
1168 /*****************************************************************************
1169  * Callbacks
1170  *****************************************************************************/
1171 static int InteractCallback( vlc_object_t *p_this,
1172                              const char *psz_var, vlc_value_t old_val,
1173                              vlc_value_t new_val, void *param )
1174 {
1175     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1176     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1177     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1178     QApplication::postEvent( THEDP, event );
1179     return VLC_SUCCESS;
1180 }
1181
1182 /*****************************************************************************
1183  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1184  *  We don't show the menu directly here because we don't want the
1185  *  caller to block for a too long time.
1186  *****************************************************************************/
1187 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1188                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1189 {
1190     intf_thread_t *p_intf = (intf_thread_t *)param;
1191
1192     if( p_intf->pf_show_dialog )
1193     {
1194         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1195                                 new_val.b_bool, NULL );
1196     }
1197
1198     return VLC_SUCCESS;
1199 }
1200
1201 /*****************************************************************************
1202  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1203  *****************************************************************************/
1204 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1205                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1206 {
1207     intf_thread_t *p_intf = (intf_thread_t *)param;
1208     p_intf->p_sys->p_mi->toggleFSC();
1209
1210     /* Show event */
1211      return VLC_SUCCESS;
1212 }
1213