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