]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt: kill a fixme for Fullscreen Control recreation
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2010 VideoLAN and AUTHORS
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 Foundation, Inc.,
23  * 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"                    // Creation
34 #include "actions_manager.hpp"                  // killInstance
35 #include "extensions_manager.hpp"               // killInstance
36
37 #include "util/customwidgets.hpp"               // qtEventToVLCKey, QVLCStackedWidget
38 #include "util/qt_dirs.hpp"                     // toNativeSeparators
39
40 #include "components/interface_widgets.hpp"     // bgWidget, videoWidget
41 #include "components/controller.hpp"            // controllers
42 #include "components/playlist/playlist.hpp"     // plWidget
43 #include "dialogs/firstrun.hpp"                 // First Run
44
45 #include "menus.hpp"                            // Menu creation
46 #include "recents.hpp"                          // RecentItems when DnD
47
48 #include <QCloseEvent>
49 #include <QKeyEvent>
50
51 #include <QUrl>
52 #include <QSize>
53 #include <QDate>
54
55 #include <QMenu>
56 #include <QMenuBar>
57 #include <QStatusBar>
58 #include <QLabel>
59 #include <QStackedWidget>
60
61 #include <vlc_keys.h>                       /* Wheel event */
62 #include <vlc_vout_display.h>               /* vout_thread_t and VOUT_ events */
63
64 // #define DEBUG_INTF
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
72 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
73 {
74     /* Variables initialisation */
75     bgWidget             = NULL;
76     videoWidget          = NULL;
77     playlistWidget       = NULL;
78     stackCentralOldWidget= NULL;
79 #ifndef HAVE_MAEMO
80     sysTray              = NULL;
81 #endif
82     fullscreenControls   = NULL;
83     cryptedLabel         = NULL;
84     controls             = NULL;
85     inputC               = NULL;
86
87     b_hideAfterCreation  = false; // --qt-start-minimized
88     playlistVisible      = false;
89     input_name           = "";
90
91
92     /* Ask for Privacy */
93     FirstRun::CheckAndRun( this, p_intf );
94
95     /**
96      *  Configuration and settings
97      *  Pre-building of interface
98      **/
99     /* Main settings */
100     setFocusPolicy( Qt::StrongFocus );
101     setAcceptDrops( true );
102     setWindowRole( "vlc-main" );
103     setWindowIcon( QApplication::windowIcon() );
104     setWindowOpacity( var_InheritFloat( p_intf, "qt-opacity" ) );
105
106     /* Is video in embedded in the UI or not */
107     b_videoEmbedded = var_InheritBool( p_intf, "embedded-video" );
108
109     /* Does the interface resize to video size or the opposite */
110     b_keep_size = !var_InheritBool( p_intf, "qt-video-autoresize" );
111
112     /* Are we in the enhanced always-video mode or not ? */
113     i_visualmode = var_InheritInteger( p_intf, "qt-minimal-view" );
114
115     /* Do we want anoying popups or not */
116     b_notificationEnabled = var_InheritBool( p_intf, "qt-notification" );
117
118     /* Set the other interface settings */
119     settings = getSettings();
120     settings->beginGroup( "MainWindow" );
121
122     /* */
123     b_plDocked = getSettings()->value( "pl-dock-status", true ).toBool();
124
125
126     /**
127      * Retrieve saved sizes for main window
128      *   mainBasedSize = based window size for normal mode
129      *                  (no video, no background)
130      *   mainVideoSize = window size with video (all modes)
131      **/
132     mainBasedSize = settings->value( "mainBasedSize", QSize( 350, 120 ) ).toSize();
133     mainVideoSize = settings->value( "mainVideoSize", QSize( 400, 300 ) ).toSize();
134     settings->endGroup( );
135
136     /**************
137      * Status Bar *
138      **************/
139     createStatusBar();
140
141     /**************************
142      *  UI and Widgets design
143      **************************/
144     setVLCWindowsTitle();
145     createMainWidget( settings );
146
147     /************
148      * Menu Bar *
149      ************/
150     QVLCMenu::createMenuBar( this, p_intf );
151     CONNECT( THEMIM->getIM(), voutListChanged( vout_thread_t **, int ),
152              this, destroyPopupMenu() );
153
154     /*********************************
155      * Create the Systray Management *
156      *********************************/
157     initSystray();
158
159     /********************
160      * Input Manager    *
161      ********************/
162     MainInputManager::getInstance( p_intf );
163
164 #ifdef WIN32
165     himl = NULL;
166     p_taskbl = NULL;
167     taskbar_wmsg = RegisterWindowMessage("TaskbarButtonCreated");
168 #endif
169
170     /************************************************************
171      * Connect the input manager to the GUI elements it manages *
172      ************************************************************/
173     /**
174      * Connects on nameChanged()
175      * Those connects are different because options can impeach them to trigger.
176      **/
177     /* Main Interface statusbar */
178     CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
179              this, setName( const QString& ) );
180     /* and systray */
181 #ifndef HAVE_MAEMO
182     if( sysTray )
183     {
184         CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
185                  this, updateSystrayTooltipName( const QString& ) );
186     }
187 #endif
188     /* and title of the Main Interface*/
189     if( var_InheritBool( p_intf, "qt-name-in-title" ) )
190     {
191         CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
192                  this, setVLCWindowsTitle( const QString& ) );
193     }
194
195     /**
196      * CONNECTS on PLAY_STATUS
197      **/
198     /* Status on the systray */
199 #ifndef HAVE_MAEMO
200     if( sysTray )
201     {
202         CONNECT( THEMIM->getIM(), statusChanged( int ),
203                  this, updateSystrayTooltipStatus( int ) );
204     }
205 #endif
206
207     /* END CONNECTS ON IM */
208
209     /* VideoWidget connects for asynchronous calls */
210     connect( this, SIGNAL(askGetVideo(WId*,int*,int*,unsigned*,unsigned *)),
211              this, SLOT(getVideoSlot(WId*,int*,int*,unsigned*,unsigned*)),
212              Qt::BlockingQueuedConnection );
213     connect( this, SIGNAL(askReleaseVideo( void )),
214              this, SLOT(releaseVideoSlot( void )),
215              Qt::BlockingQueuedConnection );
216
217     if( videoWidget )
218     {
219         CONNECT( this, askVideoToResize( unsigned int, unsigned int ),
220                  videoWidget, SetSizing( unsigned int, unsigned int ) );
221         CONNECT( videoWidget, sizeChanged( int, int ),
222                  this, resizeStack( int,  int ) );
223         CONNECT( this, askVideoSetFullScreen( bool ),
224                  videoWidget, SetFullScreen( bool ) );
225         CONNECT( videoWidget, keyPressed( QKeyEvent * ),
226                  this, handleKeyPress( QKeyEvent * ) );
227     }
228
229     CONNECT( THEDP, toolBarConfUpdated(), this, recreateToolbars() );
230
231     /** END of CONNECTS**/
232
233
234     /************
235      * Callbacks
236      ************/
237     var_AddCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
238
239     /* Register callback for the intf-popupmenu variable */
240     var_AddCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
241
242     /**** FINAL SIZING and placement of interface */
243     settings->beginGroup( "MainWindow" );
244     QVLCTools::restoreWidgetPosition( settings, this, QSize(400, 100) );
245
246     /* resize to previously saved main window size if appicable */
247     //FIXME remove.
248     if( b_keep_size )
249     {
250        if( i_visualmode )
251            resize( mainVideoSize );
252        else
253            resize( mainBasedSize );
254     }
255
256     /* Playlist */
257     int i_plVis = settings->value( "playlist-visible", false ).toBool();
258     settings->endGroup();
259
260     if( i_plVis ) togglePlaylist();
261
262     /* Final sizing and showing */
263     setVisible( !b_hideAfterCreation );
264
265     setMinimumWidth( __MAX( controls->sizeHint().width(),
266                             menuBar()->sizeHint().width() ) + 30 );
267
268     /* Switch to minimal view if needed, must be called after the show() */
269     if( i_visualmode )
270         toggleMinimalView( true );
271 }
272
273 MainInterface::~MainInterface()
274 {
275     /* Unsure we hide the videoWidget before destroying it */
276     if( stackCentralOldWidget == videoWidget )
277         showBg();
278
279 #ifdef WIN32
280     if( himl )
281         ImageList_Destroy( himl );
282     if(p_taskbl)
283         p_taskbl->vt->Release(p_taskbl);
284     CoUninitialize();
285 #endif
286
287     /* Be sure to kill the actionsManager... Only used in the MI and control */
288     ActionsManager::killInstance();
289
290     /* Idem */
291     ExtensionsManager::killInstance();
292
293     /* Delete the FSC controller */
294     delete fullscreenControls;
295
296     /* Save states */
297     settings->beginGroup( "MainWindow" );
298
299     settings->setValue( "pl-dock-status", b_plDocked );
300     /* Save playlist state */
301     if( playlistWidget )
302     {
303         settings->setValue( "playlist-visible",
304                             isPlDocked() ?
305                             playlistVisible :
306                             playlistWidget->isVisible() /* FIXME */ );
307     }
308
309     settings->setValue( "adv-controls",
310                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
311
312     settings->setValue( "mainBasedSize", mainBasedSize );
313     settings->setValue( "mainVideoSize", mainVideoSize );
314
315     settings->setValue( "backgroundSize", bgWidget->size() );
316
317     /* Save this size */
318     QVLCTools::saveWidgetPosition(settings, this);
319
320     settings->endGroup();
321
322     /* Save undocked playlist size */
323     if( playlistWidget && !isPlDocked() )
324         QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
325
326     delete playlistWidget;
327
328     delete statusBar();
329
330     /* Unregister callbacks */
331     var_DelCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
332     var_DelCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
333
334     p_intf->p_sys->p_mi = NULL;
335 }
336
337 /*****************************
338  *   Main UI handling        *
339  *****************************/
340 void MainInterface::recreateToolbars()
341 {
342     settings->beginGroup( "MainWindow" );
343     delete controls;
344     delete inputC;
345
346     controls = new ControlsWidget( p_intf, false, this );
347     inputC = new InputControlsWidget( p_intf, this );
348
349     if( fullscreenControls )
350     {
351         delete fullscreenControls;
352         fullscreenControls = new FullscreenControllerWidget( p_intf, this );
353         CONNECT( fullscreenControls, keyPressed( QKeyEvent * ),
354                  this, handleKeyPress( QKeyEvent * ) );
355     }
356     mainLayout->insertWidget( 2, inputC );
357     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
358                               controls );
359     settings->endGroup();
360 }
361
362 void MainInterface::createMainWidget( QSettings *settings )
363 {
364     /* Create the main Widget and the mainLayout */
365     QWidget *main = new QWidget;
366     setCentralWidget( main );
367     mainLayout = new QVBoxLayout( main );
368     main->setContentsMargins( 0, 0, 0, 0 );
369     mainLayout->setSpacing( 0 ); mainLayout->setMargin( 0 );
370
371     /* */
372     stackCentralW = new QVLCStackedWidget( main );
373
374     /* Bg Cone */
375     bgWidget = new BackgroundWidget( p_intf );
376     stackCentralW->addWidget( bgWidget );
377
378     /* And video Outputs */
379     if( b_videoEmbedded )
380     {
381         videoWidget = new VideoWidget( p_intf );
382         stackCentralW->addWidget( videoWidget );
383     }
384     mainLayout->insertWidget( 1, stackCentralW );
385
386     settings->beginGroup( "MainWindow" );
387
388     /* Create the CONTROLS Widget */
389     controls = new ControlsWidget( p_intf,
390                    settings->value( "adv-controls", false ).toBool(), this );
391     inputC = new InputControlsWidget( p_intf, this );
392
393     mainLayout->insertWidget( 2, inputC );
394     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
395                               controls );
396
397     /* Visualisation, disabled for now, they SUCK */
398     #if 0
399     visualSelector = new VisualSelector( p_intf );
400     mainLayout->insertWidget( 0, visualSelector );
401     visualSelector->hide();
402     #endif
403
404     settings->endGroup();
405
406     /* Enable the popup menu in the MI */
407     main->setContextMenuPolicy( Qt::CustomContextMenu );
408     CONNECT( main, customContextMenuRequested( const QPoint& ),
409              this, popupMenu( const QPoint& ) );
410
411     if ( depth() > 8 ) /* 8bit depth has too many issues with opacity */
412         /* Create the FULLSCREEN CONTROLS Widget */
413         if( var_InheritBool( p_intf, "qt-fs-controller" ) )
414         {
415             fullscreenControls = new FullscreenControllerWidget( p_intf, this );
416             CONNECT( fullscreenControls, keyPressed( QKeyEvent * ),
417                      this, handleKeyPress( QKeyEvent * ) );
418         }
419 }
420
421 inline void MainInterface::initSystray()
422 {
423 #ifndef HAVE_MAEMO
424     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
425     bool b_systrayWanted = var_InheritBool( p_intf, "qt-system-tray" );
426
427     if( var_InheritBool( p_intf, "qt-start-minimized") )
428     {
429         if( b_systrayAvailable )
430         {
431             b_systrayWanted = true;
432             b_hideAfterCreation = true;
433         }
434         else
435             msg_Err( p_intf, "cannot start minimized without system tray bar" );
436     }
437
438     if( b_systrayAvailable && b_systrayWanted )
439         createSystray();
440 #endif
441 }
442
443 inline void MainInterface::createStatusBar()
444 {
445     /****************
446      *  Status Bar  *
447      ****************/
448     /* Widgets Creation*/
449     QStatusBar *statusBarr = statusBar();
450
451     TimeLabel *timeLabel = new TimeLabel( p_intf );
452     nameLabel = new QLabel( this );
453     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
454                                       | Qt::TextSelectableByKeyboard );
455     SpeedLabel *speedLabel = new SpeedLabel( p_intf, this );
456
457     /* Styling those labels */
458     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
459     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
460     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
461     timeLabel->setStyleSheet(
462             "QLabel:hover { background-color: rgba(255, 255, 255, 50%) }" );
463     speedLabel->setStyleSheet(
464             "QLabel:hover { background-color: rgba(255, 255, 255, 50%) }" );
465
466     /* and adding those */
467     statusBarr->addWidget( nameLabel, 8 );
468     statusBarr->addPermanentWidget( speedLabel, 0 );
469     statusBarr->addPermanentWidget( timeLabel, 0 );
470
471     /* timeLabel behaviour:
472        - double clicking opens the goto time dialog
473        - right-clicking and clicking just toggle between remaining and
474          elapsed time.*/
475     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
476
477     CONNECT( THEMIM->getIM(), encryptionChanged( bool ),
478              this, showCryptedLabel( bool ) );
479
480     CONNECT( THEMIM->getIM(), seekRequested( float ),
481              timeLabel, setDisplayPosition( float ) );
482 }
483
484 /**********************************************************************
485  * Handling of sizing of the components
486  **********************************************************************/
487
488 void MainInterface::debug()
489 {
490 #ifdef DEBUG_INTF
491     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
492     msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
493     msg_Dbg( p_intf, "minimumsize: %i - %i", minimumSize().height(), minimumSize().width() );
494
495     msg_Dbg( p_intf, "Stack size: %i - %i", stackCentralW->size().height(), stackCentralW->size().width() );
496     msg_Dbg( p_intf, "Stack sizeHint: %i - %i", stackCentralW->sizeHint().height(), stackCentralW->sizeHint().width() );
497     msg_Dbg( p_intf, "Central size: %i - %i", centralWidget()->size().height(), centralWidget()->size().width() );
498 #endif
499 }
500
501 inline void MainInterface::showVideo() { showTab( videoWidget ); }
502 inline void MainInterface::showBg() { showTab( bgWidget ); }
503
504 inline void MainInterface::showTab( QWidget *widget )
505 {
506 #ifdef DEBUG_INTF
507     msg_Warn( p_intf, "Old stackCentralOldWidget %i", stackCentralW->indexOf( stackCentralOldWidget ) );
508 #endif
509     stackCentralOldWidget = stackCentralW->currentWidget();
510     stackCentralW->setCurrentWidget( widget );
511
512 #ifdef DEBUG_INTF
513     msg_Warn( p_intf, "State change %i",  stackCentralW->currentIndex() );
514     msg_Warn( p_intf, "New stackCentralOldWidget %i", stackCentralW->indexOf( stackCentralOldWidget ) );
515 #endif
516 }
517
518 inline void MainInterface::restoreStackOldWidget()
519 {
520 #ifdef DEBUG_INTF
521     msg_Warn( p_intf, "New stackCentralOldWidget %i", stackCentralW->indexOf( stackCentralOldWidget ) );
522 #endif
523     QWidget *wTemp = stackCentralW->currentWidget();
524
525     stackCentralW->setCurrentWidget( stackCentralOldWidget );
526
527     stackCentralOldWidget = wTemp;
528 #ifdef DEBUG_INTF
529     msg_Warn( p_intf, "Debug %i %i",stackCentralW->indexOf( wTemp ), stackCentralW->indexOf( stackCentralW->currentWidget() ) );
530 #endif
531 }
532
533 void MainInterface::destroyPopupMenu()
534 {
535     QVLCMenu::PopupMenu( p_intf, false, this );
536 }
537
538 void MainInterface::popupMenu( const QPoint &p )
539 {
540     QVLCMenu::PopupMenu( p_intf, true, this );
541 }
542
543 void MainInterface::toggleFSC()
544 {
545    if( !fullscreenControls ) return;
546
547    IMEvent *eShow = new IMEvent( FullscreenControlToggle_Type, 0 );
548    QApplication::postEvent( fullscreenControls, eShow );
549 }
550
551 /****************************************************************************
552  * Video Handling
553  ****************************************************************************/
554
555 /* This event is used to deal with the fullscreen and always on top
556    issue conflict (bug in wx) */
557 class SetVideoOnTopQtEvent : public QEvent
558 {
559 public:
560     SetVideoOnTopQtEvent( bool _onTop ) :
561       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
562     {}
563
564     bool OnTop() const { return onTop; }
565
566 private:
567     bool onTop;
568 };
569
570 /**
571  * NOTE:
572  * You must not change the state of this object or other Qt4 UI objects,
573  * from the video output thread - only from the Qt4 UI main loop thread.
574  * All window provider queries must be handled through signals or events.
575  * That's why we have all those emit statements...
576  */
577 WId MainInterface::getVideo( int *pi_x, int *pi_y,
578                              unsigned int *pi_width, unsigned int *pi_height )
579 {
580     if( !videoWidget )
581         return 0;
582
583     /* This is a blocking call signal. Results are returned through pointers.
584      * Beware of deadlocks! */
585     WId id;
586     emit askGetVideo( &id, pi_x, pi_y, pi_width, pi_height );
587     return id;
588 }
589
590 void MainInterface::getVideoSlot( WId *p_id, int *pi_x, int *pi_y,
591                                   unsigned *pi_width, unsigned *pi_height )
592 {
593     /* Request the videoWidget */
594     WId ret = videoWidget->request( pi_x, pi_y,
595                                     pi_width, pi_height, b_keep_size );
596     *p_id = ret;
597     if( ret ) /* The videoWidget is available */
598     {
599         /* Ask videoWidget to resize correctly, if we are in normal mode */
600         if( !isFullScreen() && !isMaximized() )
601             videoWidget->SetSizing( *pi_width, *pi_height );
602
603         /* Consider the video active now */
604         showVideo();
605     }
606 }
607
608 /* Asynchronous call from the WindowClose function */
609 void MainInterface::releaseVideo( void )
610 {
611     emit askReleaseVideo();
612 }
613
614 /* Function that is CONNECTED to the previous emit */
615 void MainInterface::releaseVideoSlot( void )
616 {
617     videoWidget->release();
618
619     if( stackCentralW->currentWidget() == videoWidget )
620         restoreStackOldWidget();
621
622     /* We don't want to have a blank video to popup */
623     stackCentralOldWidget = bgWidget;
624 }
625
626 /* Asynchronous call from WindowControl function */
627 int MainInterface::controlVideo( int i_query, va_list args )
628 {
629     switch( i_query )
630     {
631     case VOUT_WINDOW_SET_SIZE:
632     {
633         unsigned int i_width  = va_arg( args, unsigned int );
634         unsigned int i_height = va_arg( args, unsigned int );
635         if( isFullScreen() || isMaximized() )
636             showNormal();
637         emit askVideoToResize( i_width, i_height );
638         return VLC_SUCCESS;
639     }
640     case VOUT_WINDOW_SET_STATE:
641     {
642         unsigned i_arg = va_arg( args, unsigned );
643         unsigned on_top = i_arg & VOUT_WINDOW_STATE_ABOVE;
644         QApplication::postEvent( this, new SetVideoOnTopQtEvent( on_top ) );
645         return VLC_SUCCESS;
646     }
647     case VOUT_WINDOW_SET_FULLSCREEN:
648     {
649         bool b_fs = va_arg( args, int );
650         emit askVideoSetFullScreen( b_fs );
651         return VLC_SUCCESS;
652     }
653     default:
654         msg_Warn( p_intf, "unsupported control query" );
655         return VLC_EGENERIC;
656     }
657 }
658
659 /*****************************************************************************
660  * Playlist, Visualisation and Menus handling
661  *****************************************************************************/
662 /**
663  * Toggle the playlist widget or dialog
664  **/
665 void MainInterface::createPlaylist()
666 {
667     playlistWidget = new PlaylistWidget( p_intf, this );
668
669     if( !b_plDocked )
670     {
671         playlistWidget->setWindowFlags( Qt::Window );
672
673         /* This will restore the geometry but will not work for position,
674            because of parenting */
675         QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
676                 playlistWidget, QSize( 600, 300 ) );
677     }
678     else
679     {
680         stackCentralW->addWidget( playlistWidget );
681     }
682 }
683
684 void MainInterface::togglePlaylist()
685 {
686     if( !playlistWidget )
687         createPlaylist();
688
689     if( b_plDocked )
690     {
691         /* Playlist is not visible, show it */
692         if( stackCentralW->currentWidget() != playlistWidget )
693         {
694             showTab( playlistWidget );
695         }
696         else /* Hide it! */
697         {
698             restoreStackOldWidget();
699         }
700         playlistVisible = ( stackCentralW->currentWidget() == playlistWidget );
701     }
702     else
703     {
704         playlistWidget->setWindowFlags( Qt::Window );
705         playlistVisible = !playlistVisible;
706         playlistWidget->setVisible( playlistVisible );
707     }
708     debug();
709 }
710
711 void MainInterface::dockPlaylist( bool p_docked )
712 {
713     if( b_plDocked == p_docked ) return;
714     b_plDocked = p_docked;
715
716     if( !playlistWidget ) return; /* Playlist wasn't created yet */
717     if( !p_docked )
718     {
719         stackCentralW->removeWidget( playlistWidget );
720         playlistWidget->setWindowFlags( Qt::Window );
721         QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
722                 playlistWidget, QSize( 600, 300 ) );
723         playlistWidget->show();
724         restoreStackOldWidget();
725     }
726     else
727     {
728         QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
729         playlistWidget->setWindowFlags( Qt::Widget ); // Probably a Qt bug here
730         // It would be logical that QStackWidget::addWidget reset the flags...
731         stackCentralW->addWidget( playlistWidget );
732         showTab( playlistWidget );
733     }
734     playlistVisible = true;
735 }
736
737 /*
738   If b_switch is false, then we are normalView
739  */
740 void MainInterface::toggleMinimalView( bool b_switch )
741 {
742     if( i_visualmode == 0 )
743     {
744         if( stackCentralW->currentWidget() == bgWidget )
745         {
746             if( stackCentralW->height() < 16 )
747             {
748                 resizeStack( stackCentralW->width(), 100 );
749             }
750         }
751     }
752
753     menuBar()->setVisible( !b_switch );
754     controls->setVisible( !b_switch );
755     statusBar()->setVisible( !b_switch );
756     inputC->setVisible( !b_switch );
757
758     emit minimalViewToggled( b_switch );
759 }
760
761 /* toggling advanced controls buttons */
762 void MainInterface::toggleAdvancedButtons()
763 {
764     controls->toggleAdvanced();
765 //    if( fullscreenControls ) fullscreenControls->toggleAdvanced();
766 }
767
768 /* Get the visibility status of the controls (hidden or not, advanced or not) */
769 int MainInterface::getControlsVisibilityStatus()
770 {
771     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
772                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
773 }
774
775 #if 0
776 void MainInterface::visual()
777 {
778     if( !VISIBLE( visualSelector) )
779     {
780         visualSelector->show();
781         if( !THEMIM->getIM()->hasVideo() )
782         {
783             /* Show the background widget */
784         }
785         visualSelectorEnabled = true;
786     }
787     else
788     {
789         /* Stop any currently running visualization */
790         visualSelector->hide();
791         visualSelectorEnabled = false;
792     }
793 }
794 #endif
795
796 /************************************************************************
797  * Other stuff
798  ************************************************************************/
799 void MainInterface::setName( const QString& name )
800 {
801     input_name = name; /* store it for the QSystray use */
802     /* Display it in the status bar, but also as a Tooltip in case it doesn't
803        fit in the label */
804     nameLabel->setText( " " + name + " " );
805     nameLabel->setToolTip( " " + name +" " );
806 }
807
808 /**
809  * Give the decorations of the Main Window a correct Name.
810  * If nothing is given, set it to VLC...
811  **/
812 void MainInterface::setVLCWindowsTitle( const QString& aTitle )
813 {
814     if( aTitle.isEmpty() )
815     {
816         setWindowTitle( qtr( "VLC media player" ) );
817     }
818     else
819     {
820         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
821     }
822 }
823
824 void MainInterface::showCryptedLabel( bool b_show )
825 {
826     if( cryptedLabel == NULL )
827     {
828         cryptedLabel = new QLabel;
829         // The lock icon is not the right one for DRM protection/scrambled.
830         //cryptedLabel->setPixmap( QPixmap( ":/lock" ) );
831         cryptedLabel->setText( "DRM" );
832         statusBar()->addWidget( cryptedLabel );
833     }
834
835     cryptedLabel->setVisible( b_show );
836 }
837
838 void MainInterface::showBuffering( float f_cache )
839 {
840     QString amount = QString("Buffering: %1%").arg( (int)(100*f_cache) );
841     statusBar()->showMessage( amount, 1000 );
842 }
843
844 /*****************************************************************************
845  * Systray Icon and Systray Menu
846  *****************************************************************************/
847 #ifndef HAVE_MAEMO
848 /**
849  * Create a SystemTray icon and a menu that would go with it.
850  * Connects to a click handler on the icon.
851  **/
852 void MainInterface::createSystray()
853 {
854     QIcon iconVLC;
855     if( QDate::currentDate().dayOfYear() >= 354 )
856         iconVLC =  QIcon( ":/logo/vlc128-christmas.png" );
857     else
858         iconVLC =  QIcon( ":/logo/vlc128.png" );
859     sysTray = new QSystemTrayIcon( iconVLC, this );
860     sysTray->setToolTip( qtr( "VLC media player" ));
861
862     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
863     systrayMenu->setIcon( iconVLC );
864
865     QVLCMenu::updateSystrayMenu( this, p_intf, true );
866     sysTray->show();
867
868     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
869             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
870 }
871
872 /**
873  * Updates the Systray Icon's menu and toggle the main interface
874  */
875 void MainInterface::toggleUpdateSystrayMenu()
876 {
877     /* If hidden, show it */
878     if( isHidden() )
879     {
880         show();
881         activateWindow();
882     }
883     else if( isMinimized() )
884     {
885         /* Minimized */
886         showNormal();
887         activateWindow();
888     }
889     else
890     {
891         /* Visible (possibly under other windows) */
892 #ifdef WIN32
893         /* check if any visible window is above vlc in the z-order,
894          * but ignore the ones always on top
895          * and the ones which can't be activated */
896         WINDOWINFO wi;
897         HWND hwnd;
898         wi.cbSize = sizeof( WINDOWINFO );
899         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
900                 hwnd && ( !IsWindowVisible( hwnd ) ||
901                     ( GetWindowInfo( hwnd, &wi ) &&
902                       (wi.dwExStyle&WS_EX_NOACTIVATE) ) );
903                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
904             if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
905                 (wi.dwExStyle&WS_EX_TOPMOST) )
906             {
907                 hide();
908             }
909             else
910             {
911                 activateWindow();
912             }
913 #else
914         hide();
915 #endif
916     }
917     QVLCMenu::updateSystrayMenu( this, p_intf );
918 }
919
920 void MainInterface::handleSystrayClick(
921                                     QSystemTrayIcon::ActivationReason reason )
922 {
923     switch( reason )
924     {
925         case QSystemTrayIcon::Trigger:
926         case QSystemTrayIcon::DoubleClick:
927             toggleUpdateSystrayMenu();
928             break;
929         case QSystemTrayIcon::MiddleClick:
930             sysTray->showMessage( qtr( "VLC media player" ),
931                     qtr( "Control menu for the player" ),
932                     QSystemTrayIcon::Information, 3000 );
933             break;
934         default:
935             break;
936     }
937 }
938
939 /**
940  * Updates the name of the systray Icon tooltip.
941  * Doesn't check if the systray exists, check before you call it.
942  **/
943 void MainInterface::updateSystrayTooltipName( const QString& name )
944 {
945     if( name.isEmpty() )
946     {
947         sysTray->setToolTip( qtr( "VLC media player" ) );
948     }
949     else
950     {
951         sysTray->setToolTip( name );
952         if( b_notificationEnabled && ( isHidden() || isMinimized() ) )
953         {
954             sysTray->showMessage( qtr( "VLC media player" ), name,
955                     QSystemTrayIcon::NoIcon, 3000 );
956         }
957     }
958
959     QVLCMenu::updateSystrayMenu( this, p_intf );
960 }
961
962 /**
963  * Updates the status of the systray Icon tooltip.
964  * Doesn't check if the systray exists, check before you call it.
965  **/
966 void MainInterface::updateSystrayTooltipStatus( int i_status )
967 {
968     switch( i_status )
969     {
970     case PLAYING_S:
971         sysTray->setToolTip( input_name );
972         break;
973     case PAUSE_S:
974         sysTray->setToolTip( input_name + " - " + qtr( "Paused") );
975         break;
976     default:
977         sysTray->setToolTip( qtr( "VLC media player" ) );
978         break;
979     }
980     QVLCMenu::updateSystrayMenu( this, p_intf );
981 }
982 #endif
983
984 /************************************************************************
985  * D&D Events
986  ************************************************************************/
987 void MainInterface::dropEvent(QDropEvent *event)
988 {
989     dropEventPlay( event, true );
990 }
991
992 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
993 {
994     event->setDropAction( Qt::CopyAction );
995     if( !event->possibleActions() & Qt::CopyAction )
996         return;
997
998     const QMimeData *mimeData = event->mimeData();
999
1000     /* D&D of a subtitles file, add it on the fly */
1001     if( mimeData->urls().size() == 1 && THEMIM->getIM()->hasInput() )
1002     {
1003         if( !input_AddSubtitle( THEMIM->getInput(),
1004                  qtu( toNativeSeparators( mimeData->urls()[0].toLocalFile() ) ),
1005                  true ) )
1006         {
1007             event->accept();
1008             return;
1009         }
1010     }
1011
1012     bool first = b_play;
1013     foreach( const QUrl &url, mimeData->urls() )
1014     {
1015         QString s = toNativeSeparators( url.toLocalFile() );
1016
1017         if( s.length() > 0 ) {
1018             char* psz_uri = make_URI( qtu(s) );
1019             playlist_Add( THEPL, psz_uri, NULL,
1020                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: PLAYLIST_PREPARSE),
1021                           PLAYLIST_END, true, pl_Unlocked );
1022             free( psz_uri );
1023             first = false;
1024             RecentsMRL::getInstance( p_intf )->addRecent( s );
1025         }
1026     }
1027     event->accept();
1028 }
1029 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1030 {
1031      event->acceptProposedAction();
1032 }
1033 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1034 {
1035      event->acceptProposedAction();
1036 }
1037 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1038 {
1039      event->accept();
1040 }
1041
1042 /************************************************************************
1043  * Events stuff
1044  ************************************************************************/
1045 void MainInterface::customEvent( QEvent *event )
1046 {
1047     if ( event->type() == (int)SetVideoOnTopEvent_Type )
1048     {
1049         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1050         if( p_event->OnTop() )
1051             setWindowFlags( windowFlags() | Qt::WindowStaysOnTopHint );
1052         else
1053             setWindowFlags( windowFlags() & ~Qt::WindowStaysOnTopHint );
1054         show(); /* necessary to apply window flags */
1055     }
1056 }
1057
1058 void MainInterface::keyPressEvent( QKeyEvent *e )
1059 {
1060     handleKeyPress( e );
1061 }
1062
1063 void MainInterface::handleKeyPress( QKeyEvent *e )
1064 {
1065     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1066           && !menuBar()->isVisible() )
1067     {
1068         toggleMinimalView( false );
1069         e->accept();
1070     }
1071
1072     int i_vlck = qtEventToVLCKey( e );
1073     if( i_vlck > 0 )
1074     {
1075         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1076         e->accept();
1077     }
1078     else
1079         e->ignore();
1080 }
1081
1082 void MainInterface::wheelEvent( QWheelEvent *e )
1083 {
1084     int i_vlckey = qtWheelEventToVLCKey( e );
1085     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1086     e->accept();
1087 }
1088
1089 void MainInterface::closeEvent( QCloseEvent *e )
1090 {
1091     e->accept();
1092     hide();
1093     THEDP->quit();
1094 }
1095
1096 void MainInterface::toggleFullScreen()
1097 {
1098     if( isFullScreen() )
1099     {
1100         showNormal();
1101         emit fullscreenInterfaceToggled( false );
1102     }
1103     else
1104     {
1105         showFullScreen();
1106         emit fullscreenInterfaceToggled( true );
1107     }
1108 }
1109
1110 /*****************************************************************************
1111  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1112  *  We don't show the menu directly here because we don't want the
1113  *  caller to block for a too long time.
1114  *****************************************************************************/
1115 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1116                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1117 {
1118     intf_thread_t *p_intf = (intf_thread_t *)param;
1119
1120     if( p_intf->pf_show_dialog )
1121     {
1122         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1123                                 new_val.b_bool, NULL );
1124     }
1125
1126     return VLC_SUCCESS;
1127 }
1128
1129 /*****************************************************************************
1130  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1131  *****************************************************************************/
1132 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1133                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1134 {
1135     intf_thread_t *p_intf = (intf_thread_t *)param;
1136     p_intf->p_sys->p_mi->toggleFSC();
1137
1138     /* Show event */
1139      return VLC_SUCCESS;
1140 }