]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt4 - Small interface and control layout fix.
[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     updateGeometry();
290     settings->endGroup();
291 }
292
293 MainInterface::~MainInterface()
294 {
295     if( playlistWidget ) playlistWidget->savingSettings( settings );
296     if( ExtendedDialog::exists() )
297         ExtendedDialog::getInstance( p_intf )->savingSettings();
298
299     settings->beginGroup( "MainWindow" );
300     settings->setValue( "playlist-floats", dockPL->isFloating() );
301     settings->setValue( "adv-controls", 
302                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
303     settings->setValue( "pos", pos() );
304
305     settings->endGroup();
306     delete settings;
307
308     /* Unregister callback for the intf-popupmenu variable */
309     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
310                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
311     if( p_playlist != NULL )
312     {
313         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
314         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
315         vlc_object_release( p_playlist );
316     }
317
318     p_intf->b_interaction = VLC_FALSE;
319     var_DelCallback( p_intf, "interaction", InteractCallback, this );
320
321     p_intf->pf_request_window = NULL;
322     p_intf->pf_release_window = NULL;
323     p_intf->pf_control_window = NULL;
324 }
325
326 /*****************************
327  *   Main UI handling        *
328  *****************************/
329
330 /**
331  * Give the decorations of the Main Window a correct Name.
332  * If nothing is given, set it to VLC...
333  **/
334 void MainInterface::setVLCWindowsTitle( QString aTitle )
335 {
336     if( aTitle.isEmpty() )
337     {
338         setWindowTitle( qtr( "VLC media player" ) );
339     }
340     else
341     {
342         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
343     }
344 }
345
346 void MainInterface::handleMainUi( QSettings *settings )
347 {
348     /* Create the main Widget and the mainLayout */
349     QWidget *main = new QWidget;
350     setCentralWidget( main );
351     mainLayout = new QVBoxLayout( main );
352
353     /* Margins, spacing */
354     main->setContentsMargins( 0, 0, 0, 0 );
355     main->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
356     mainLayout->setMargin( 0 );
357
358     /* Create the CONTROLS Widget */
359     /* bool b_shiny = config_GetInt( p_intf, "qt-blingbling" ); */
360     controls = new ControlsWidget( p_intf, this,
361                    settings->value( "adv-controls", false ).toBool(),
362                    config_GetInt( p_intf, "qt-blingbling" ) );
363
364     /* Add the controls Widget to the main Widget */
365     mainLayout->insertWidget( 0, controls );
366
367     /* Create the Speed Control Widget */
368     speedControl = new SpeedControlWidget( p_intf );
369     speedControlMenu = new QMenu( this );
370
371     QWidgetAction *widgetAction = new QWidgetAction( speedControl );
372     widgetAction->setDefaultWidget( speedControl );
373     speedControlMenu->addAction( widgetAction );
374
375     /* Visualisation */
376     /* Disabled for now, they SUCK */
377     #if 0
378     visualSelector = new VisualSelector( p_intf );
379     mainLayout->insertWidget( 0, visualSelector );
380     visualSelector->hide();
381     #endif
382
383     /* And video Outputs */
384     if( alwaysVideoFlag )
385     {
386         bgWidget = new BackgroundWidget( p_intf );
387         bgWidget->widgetSize = settings->value( "backgroundSize",
388                                            QSize( 300, 200 ) ).toSize();
389         bgWidget->resize( bgWidget->widgetSize );
390         bgWidget->updateGeometry();
391         mainLayout->insertWidget( 0, bgWidget );
392         CONNECT( this, askBgWidgetToToggle(), bgWidget, toggle() );
393     }
394
395     if( videoEmbeddedFlag )
396     {
397         videoWidget = new VideoWidget( p_intf );
398         videoWidget->widgetSize = QSize( 1, 1 );
399         //videoWidget->resize( videoWidget->widgetSize );
400         mainLayout->insertWidget( 0, videoWidget );
401
402         p_intf->pf_request_window  = ::DoRequest;
403         p_intf->pf_release_window  = ::DoRelease;
404         p_intf->pf_control_window  = ::DoControl;
405     }
406
407     /* Finish the sizing */
408     updateGeometry();
409 }
410
411 int MainInterface::privacyDialog( QList<ConfigControl *> controls )
412 {
413     QDialog *privacy = new QDialog( this );
414
415     privacy->setWindowTitle( qtr( "Privacy and Network policies" ) );
416
417     QGridLayout *gLayout = new QGridLayout( privacy );
418
419     QGroupBox *blabla = new QGroupBox( qtr( "Privacy and Network Warning" ) );
420     QGridLayout *blablaLayout = new QGridLayout( blabla );
421     QLabel *text = new QLabel( qtr(
422         "<p>The <i>VideoLAN Team</i> doesn't like when an application goes "
423         "online without authorisation.</p>\n "
424         "<p><i>VLC media player</i> can request limited information on "
425         "Internet, espically to get CD Covers and songs metadata or to know "
426         "if updates are available.</p>\n"
427         "<p><i>VLC media player</i> <b>DOES NOT</b> send or collect <b>ANY</b> "
428         "information, even anonymously about your "
429         "usage.</p>\n"
430         "<p>Therefore please check the following options, the default being "
431         "almost no access on the web.</p>\n") );
432     text->setWordWrap( true );
433     text->setTextFormat( Qt::RichText );
434
435     blablaLayout->addWidget( text, 0, 0 ) ;
436
437     QGroupBox *options = new QGroupBox;
438     QGridLayout *optionsLayout = new QGridLayout( options );
439
440     gLayout->addWidget( blabla, 0, 0, 1, 3 );
441     gLayout->addWidget( options, 1, 0, 1, 3 );
442     module_config_t *p_config;
443     ConfigControl *control;
444     int line = 0;
445
446 #define CONFIG_GENERIC( option, type )                            \
447     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
448     if( p_config )                                                \
449     {                                                             \
450         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
451                 p_config, options, false, optionsLayout, line );  \
452         controls.append( control );                               \
453     }
454
455 #define CONFIG_GENERIC_NOBOOL( option, type )                     \
456     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
457     if( p_config )                                                \
458     {                                                             \
459         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
460                 p_config, options, optionsLayout, line );  \
461         controls.append( control );                               \
462     }
463
464     CONFIG_GENERIC( "album-art", IntegerList ); line++;
465     CONFIG_GENERIC_NOBOOL( "fetch-meta", Bool ); line++;
466     CONFIG_GENERIC_NOBOOL( "qt-updates-notif", Bool );
467
468     QPushButton *ok = new QPushButton( qtr( "Ok" ) );
469
470     gLayout->addWidget( ok, 2, 2 );
471
472     CONNECT( ok, clicked(), privacy, accept() );
473     return privacy->exec();
474 }
475
476 //FIXME remove me at the end...
477 void MainInterface::debug()
478 {
479     msg_Dbg( p_intf, "size: %i - %i", controls->size().height(), controls->size().width() );
480     msg_Dbg( p_intf, "sizeHint: %i - %i", controls->sizeHint().height(), controls->sizeHint().width() );
481 }
482
483 /**********************************************************************
484  * Handling of sizing of the components
485  **********************************************************************/
486
487 /* This function is probably wrong, but we don't have many many choices...
488    Since we can't know from the playlist Widget if we are inside a dock or not,
489    because the playlist Widget can be called by THEDP, as a separate windows for
490    the skins.
491    Maybe the other solution is to redefine the sizeHint() of the playlist and
492    ask _parent->isFloating()...
493    If you think this would be better, please FIXME it...
494 */
495 QSize MainInterface::sizeHint() const
496 {
497     QSize tempSize = controls->sizeHint() +
498         QSize( 100, menuBar()->size().height() + statusBar()->size().height() );
499
500     if( VISIBLE( bgWidget ) )
501         tempSize += bgWidget->sizeHint();
502     else if( videoIsActive )
503         tempSize += videoWidget->size();
504
505     if( !dockPL->isFloating() && dockPL->widget() )
506         tempSize += dockPL->widget()->size();
507
508     return tempSize;
509 }
510
511 #if 0
512 /* FIXME This is dead code and need to be removed AT THE END */
513 void MainInterface::resizeEvent( QResizeEvent *e )
514 {
515     if( videoWidget )
516         videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
517     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
518     {
519         SET_WH( videoWidget, e->size().width() - addSize.width(),
520                              e->size().height()  - addSize.height() );
521         videoWidget->updateGeometry();
522     }
523     if( VISIBLE( playlistWidget ) )
524     {
525 //        SET_WH( playlistWidget , e->size().width() - addSize.width(),
526               //                   e->size().height() - addSize.height() );
527         playlistWidget->updateGeometry();
528     }
529 }
530 #endif
531
532 /****************************************************************************
533  * Small right-click menu for rate control
534  ****************************************************************************/
535 void MainInterface::showSpeedMenu( QPoint pos )
536 {
537     speedControlMenu->exec( QCursor::pos() - pos
538                           + QPoint( 0, speedLabel->height() ) );
539 }
540
541 /****************************************************************************
542  * Video Handling
543  ****************************************************************************/
544 class SetVideoOnTopQtEvent : public QEvent
545 {
546 public:
547     SetVideoOnTopQtEvent( bool _onTop ) :
548       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
549     {}
550
551     bool OnTop() const
552     {
553         return onTop;
554     }
555
556 private:
557     bool onTop;
558 };
559
560 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
561                                    int *pi_y, unsigned int *pi_width,
562                                    unsigned int *pi_height )
563 {
564     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
565     if( ret )
566     {
567         videoIsActive = true;
568         bool bgWasVisible = false;
569
570         /* Did we have a bg ? */
571         if( VISIBLE( bgWidget) )
572         {
573             bgWasVisible = true;
574             emit askBgWidgetToToggle();
575         }
576
577         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
578         {
579             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
580         }
581         else /* Background widget available, use its size */
582         {
583             /* Ok, our visualizations are bad, so don't do this for the moment
584              * use the requested size anyway */
585             // videoWidget->widgetSize = bgWidget->widgeTSize;
586             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
587         }
588         videoWidget->updateGeometry(); // Needed for deinterlace
589         updateGeometry();
590     }
591     return ret;
592 }
593
594 void MainInterface::releaseVideo( void *p_win )
595 {
596     emit askReleaseVideo( p_win );
597 }
598
599 void MainInterface::releaseVideoSlot( void *p_win )
600 {
601     videoWidget->release( p_win );
602     videoWidget->hide();
603
604     if( bgWidget )
605         bgWidget->show();
606
607     videoIsActive = false;
608     updateGeometry();
609 }
610
611 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
612 {
613     int i_ret = VLC_EGENERIC;
614     switch( i_query )
615     {
616         case VOUT_GET_SIZE:
617         {
618             unsigned int *pi_width  = va_arg( args, unsigned int * );
619             unsigned int *pi_height = va_arg( args, unsigned int * );
620             *pi_width = videoWidget->widgetSize.width();
621             *pi_height = videoWidget->widgetSize.height();
622             i_ret = VLC_SUCCESS;
623             break;
624         }
625         case VOUT_SET_SIZE:
626         {
627             unsigned int i_width  = va_arg( args, unsigned int );
628             unsigned int i_height = va_arg( args, unsigned int );
629             videoWidget->widgetSize = QSize( i_width, i_height );
630             videoWidget->updateGeometry();
631             updateGeometry();
632             i_ret = VLC_SUCCESS;
633             break;
634         }
635         case VOUT_SET_STAY_ON_TOP:
636         {
637             int i_arg = va_arg( args, int );
638             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
639             i_ret = VLC_SUCCESS;
640             break;
641         }
642         default:
643             msg_Warn( p_intf, "unsupported control query" );
644             break;
645     }
646     return i_ret;
647 }
648
649 /*****************************************************************************
650  * Playlist, Visualisation and Menus handling
651  *****************************************************************************/
652 /**
653  * Toggle the playlist widget or dialog
654  **/
655 void MainInterface::togglePlaylist()
656 {
657     /* CREATION
658     If no playlist exist, then create one and attach it to the DockPL*/
659     if( !playlistWidget )
660     {
661         msg_Dbg( p_intf, "Creating a new playlist" );
662         playlistWidget = new PlaylistWidget( p_intf, settings );
663
664         /* Add it to the parent DockWidget */
665         dockPL->setWidget( playlistWidget );
666
667         /* Add the dock to the main Interface */
668         addDockWidget( Qt::BottomDockWidgetArea, dockPL );
669
670         /* Make the playlist floating is requested. Default is not. */
671         if( settings->value( "playlist-floats", false ).toBool() );
672         {
673             msg_Dbg( p_intf, "we don't want it inside");
674             dockPL->setFloating( true );
675         }
676     }
677     else
678     {
679     /* toggle the visibility of the playlist */
680        TOGGLEV( dockPL );
681        //resize(sizeHint());
682     }
683 #if 0
684     doComponentsUpdate();
685 #endif
686     updateGeometry();
687 }
688
689 /* Function called from the menu to undock the playlist */
690 void MainInterface::undockPlaylist()
691 {
692     dockPL->setFloating( true );
693     updateGeometry();
694 }
695
696 void MainInterface::toggleMinimalView()
697 {
698     TOGGLEV( menuBar() );
699     TOGGLEV( controls );
700     TOGGLEV( statusBar() );
701     updateGeometry();
702 }
703
704 /* Video widget cannot do this synchronously as it runs in another thread */
705 /* Well, could it, actually ? Probably dangerous ... */
706 void MainInterface::doComponentsUpdate()
707 {
708     updateGeometry();
709 }
710
711 /* toggling advanced controls buttons */
712 void MainInterface::toggleAdvanced()
713 {
714     controls->toggleAdvanced();
715 }
716
717 /* Get the visibility status of the controls (hidden or not, advanced or not) */
718 int MainInterface::getControlsVisibilityStatus()
719 {
720     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
721                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
722 }
723
724 #if 0
725 void MainInterface::visual()
726 {
727     if( !VISIBLE( visualSelector) )
728     {
729         visualSelector->show();
730         if( !THEMIM->getIM()->hasVideo() )
731         {
732             /* Show the background widget */
733         }
734         visualSelectorEnabled = true;
735     }
736     else
737     {
738         /* Stop any currently running visualization */
739         visualSelector->hide();
740         visualSelectorEnabled = false;
741     }
742     doComponentsUpdate();
743 }
744 #endif
745
746 /************************************************************************
747  * Other stuff
748  ************************************************************************/
749 void MainInterface::setDisplayPosition( float pos, int time, int length )
750 {
751     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
752     secstotimestr( psz_length, length );
753     secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
754                                                            : time );
755
756     QString title;
757     title.sprintf( "%s/%s", psz_time,
758                             ( !length && time ) ? "--:--" : psz_length );
759
760     /* Add a minus to remaining time*/
761     if( b_remainingTime && length ) timeLabel->setText( " -"+title+" " );
762     else timeLabel->setText( " "+title+" " );
763 }
764
765 void MainInterface::toggleTimeDisplay()
766 {
767     b_remainingTime = !b_remainingTime;
768     //b_remainingTime = ( b_remainingTime ? false : true );
769 }
770
771 void MainInterface::setName( QString name )
772 {
773     input_name = name; /* store it for the QSystray use */
774     /* Display it in the status bar, but also as a Tooltip in case it doesn't
775        fit in the label */
776     nameLabel->setText( " " + name + " " );
777     nameLabel->setToolTip( " " + name +" " );
778 }
779
780 void MainInterface::setStatus( int status )
781 {
782     /* Forward the status to the controls to toggle Play/Pause */
783     controls->setStatus( status );
784     /* And in the systray for the menu */
785     if( sysTray )
786         QVLCMenu::updateSystrayMenu( this, p_intf );
787 }
788
789 void MainInterface::setRate( int rate )
790 {
791     QString str;
792     str.setNum( ( 1000 / (double)rate ), 'f', 2 );
793     str.append( "x" );
794     speedLabel->setText( str );
795     speedControl->updateControls( rate );
796 }
797
798 void MainInterface::updateOnTimer()
799 {
800 #if 0
801     if( intf_ShouldDie( p_intf ) )
802     {
803         QApplication::closeAllWindows();
804         QApplication::quit();
805     }
806     if( need_components_update )
807     {
808         doComponentsUpdate();
809         need_components_update = false;
810     }
811 #endif
812
813     controls->updateOnTimer();
814 }
815
816 /*****************************************************************************
817  * Systray Icon and Systray Menu
818  *****************************************************************************/
819
820 /**
821  * Create a SystemTray icon and a menu that would go with it.
822  * Connects to a click handler on the icon.
823  **/
824 void MainInterface::createSystray()
825 {
826     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
827     sysTray = new QSystemTrayIcon( iconVLC, this );
828     sysTray->setToolTip( qtr( "VLC media player" ));
829
830     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
831     systrayMenu->setIcon( iconVLC );
832
833     QVLCMenu::updateSystrayMenu( this, p_intf, true );
834     sysTray->show();
835
836     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
837             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
838 }
839
840 /**
841  * Updates the Systray Icon's menu and toggle the main interface
842  */
843 void MainInterface::toggleUpdateSystrayMenu()
844 {
845     /* If hidden, show it */
846     if( isHidden() )
847     {
848         show();
849         activateWindow();
850     }
851     else if( isMinimized() )
852     {
853         /* Minimized */
854         showNormal();
855         activateWindow();
856     }
857     else
858     {
859         /* Visible */
860 #ifdef WIN32
861         /* check if any visible window is above vlc in the z-order,
862          * but ignore the ones always on top */
863         WINDOWINFO wi;
864         HWND hwnd;
865         wi.cbSize = sizeof( WINDOWINFO );
866         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
867                 hwnd && !IsWindowVisible( hwnd );
868                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
869         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
870                 (wi.dwExStyle&WS_EX_TOPMOST) )
871 #else
872         if( isActiveWindow() )
873 #endif
874         {
875             hide();
876         }
877         else
878         {
879             activateWindow();
880         }
881     }
882     QVLCMenu::updateSystrayMenu( this, p_intf );
883 }
884
885 void MainInterface::handleSystrayClick(
886                                     QSystemTrayIcon::ActivationReason reason )
887 {
888     switch( reason )
889     {
890         case QSystemTrayIcon::Trigger:
891             toggleUpdateSystrayMenu();
892             break;
893         case QSystemTrayIcon::MiddleClick:
894             sysTray->showMessage( qtr( "VLC media player" ),
895                     qtr( "Control menu for the player" ),
896                     QSystemTrayIcon::Information, 3000 );
897             break;
898     }
899 }
900
901 /**
902  * Updates the name of the systray Icon tooltip.
903  * Doesn't check if the systray exists, check before you call it.
904  **/
905 void MainInterface::updateSystrayTooltipName( QString name )
906 {
907     if( name.isEmpty() )
908     {
909         sysTray->setToolTip( qtr( "VLC media player" ) );
910     }
911     else
912     {
913         sysTray->setToolTip( name );
914         if( notificationEnabled && ( isHidden() || isMinimized() ) )
915         {
916             sysTray->showMessage( qtr( "VLC media player" ), name,
917                     QSystemTrayIcon::NoIcon, 3000 );
918         }
919     }
920 }
921
922 /**
923  * Updates the status of the systray Icon tooltip.
924  * Doesn't check if the systray exists, check before you call it.
925  **/
926 void MainInterface::updateSystrayTooltipStatus( int i_status )
927 {
928     switch( i_status )
929     {
930         case  0:
931         case  END_S:
932             {
933                 sysTray->setToolTip( qtr( "VLC media player" ) );
934                 break;
935             }
936         case PLAYING_S:
937             {
938                 sysTray->setToolTip( input_name );
939                 break;
940             }
941         case PAUSE_S:
942             {
943                 sysTray->setToolTip( input_name + " - "
944                         + qtr( "Paused") );
945                 break;
946             }
947     }
948 }
949
950 /************************************************************************
951  * D&D Events
952  ************************************************************************/
953 void MainInterface::dropEvent(QDropEvent *event)
954 {
955      const QMimeData *mimeData = event->mimeData();
956
957      /* D&D of a subtitles file, add it on the fly */
958      if( mimeData->urls().size() == 1 )
959      {
960         if( THEMIM->getIM()->hasInput() )
961         {
962             if( input_AddSubtitles( THEMIM->getInput(),
963                                     qtu( mimeData->urls()[0].toString() ),
964                                     VLC_TRUE ) )
965             {
966                 event->acceptProposedAction();
967                 return;
968             }
969         }
970      }
971      bool first = true;
972      foreach( QUrl url, mimeData->urls() ) {
973         QString s = url.toString();
974         if( s.length() > 0 ) {
975             playlist_Add( THEPL, qtu(s), NULL,
976                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
977                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
978             first = false;
979         }
980      }
981      event->acceptProposedAction();
982 }
983 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
984 {
985      event->acceptProposedAction();
986 }
987 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
988 {
989      event->acceptProposedAction();
990 }
991 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
992 {
993      event->accept();
994 }
995
996 /************************************************************************
997  * Events stuff
998  ************************************************************************/
999 void MainInterface::customEvent( QEvent *event )
1000 {
1001 #if 0
1002     if( event->type() == PLDockEvent_Type )
1003     {
1004         PlaylistDialog::killInstance();
1005         playlistEmbeddedFlag = true;
1006         menuBar()->clear();
1007         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1008         togglePlaylist();
1009     }
1010 #endif
1011     /*else */
1012     if ( event->type() == SetVideoOnTopEvent_Type )
1013     {
1014         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1015         if( p_event->OnTop() )
1016             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1017         else
1018             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
1019         show(); /* necessary to apply window flags?? */
1020     }
1021 }
1022
1023 void MainInterface::keyPressEvent( QKeyEvent *e )
1024 {
1025     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() & Qt::Key_H )
1026           && menuBar()->isHidden() )
1027     {
1028         toggleMinimalView();
1029         e->accept();
1030     }
1031
1032     int i_vlck = qtEventToVLCKey( e );
1033     if( i_vlck > 0 )
1034     {
1035         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1036         e->accept();
1037     }
1038     else
1039         e->ignore();
1040 }
1041
1042 void MainInterface::wheelEvent( QWheelEvent *e )
1043 {
1044     int i_vlckey = qtWheelEventToVLCKey( e );
1045     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1046     e->accept();
1047 }
1048
1049 void MainInterface::closeEvent( QCloseEvent *e )
1050 {
1051     hide();
1052     vlc_object_kill( p_intf );
1053     QApplication::closeAllWindows();
1054     QApplication::quit();
1055 }
1056
1057 /*****************************************************************************
1058  * Callbacks
1059  *****************************************************************************/
1060 static int InteractCallback( vlc_object_t *p_this,
1061                              const char *psz_var, vlc_value_t old_val,
1062                              vlc_value_t new_val, void *param )
1063 {
1064     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1065     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1066     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1067     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1068     return VLC_SUCCESS;
1069 }
1070
1071 /*****************************************************************************
1072  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1073  *  We don't show the menu directly here because we don't want the
1074  *  caller to block for a too long time.
1075  *****************************************************************************/
1076 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1077                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1078 {
1079     intf_thread_t *p_intf = (intf_thread_t *)param;
1080
1081     if( p_intf->pf_show_dialog )
1082     {
1083         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1084                                 new_val.b_bool, 0 );
1085     }
1086
1087     return VLC_SUCCESS;
1088 }
1089
1090 /*****************************************************************************
1091  * IntfShowCB: callback triggered by the intf-show playlist variable.
1092  *****************************************************************************/
1093 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1094                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1095 {
1096     intf_thread_t *p_intf = (intf_thread_t *)param;
1097     //p_intf->p_sys->b_intf_show = VLC_TRUE;
1098
1099     return VLC_SUCCESS;
1100 }