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