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