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