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