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