]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt: speed and time button actionable visual feedback
[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
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", 0 ).toInt();
258
259     settings->endGroup();
260
261     if( i_plVis ) togglePlaylist();
262
263     /* Final sizing and showing */
264     setVisible( !b_hideAfterCreation );
265
266     setMinimumWidth( __MAX( controls->sizeHint().width(),
267                             menuBar()->sizeHint().width() ) + 30 );
268
269     /* Switch to minimal view if needed, must be called after the show() */
270     if( i_visualmode )
271         toggleMinimalView( true );
272 }
273
274 MainInterface::~MainInterface()
275 {
276     /* Unsure we hide the videoWidget before destroying it */
277     if( stackCentralOldWidget == playlistWidget )
278         showBg();
279
280     /* Save playlist state */
281     if( playlistWidget )
282     {
283         if( !isPlDocked() )
284             QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
285
286         delete playlistWidget;
287     }
288
289 #ifdef WIN32
290     if( himl )
291         ImageList_Destroy( himl );
292     if(p_taskbl)
293         p_taskbl->vt->Release(p_taskbl);
294     CoUninitialize();
295 #endif
296
297     /* Be sure to kill the actionsManager... Only used in the MI and control */
298     ActionsManager::killInstance();
299
300     /* Idem */
301     ExtensionsManager::killInstance();
302
303     /* Delete the FSC controller */
304     delete fullscreenControls;
305
306     /* Save states */
307     settings->beginGroup( "MainWindow" );
308     settings->setValue( "pl-dock-status", b_plDocked );
309     settings->setValue( "playlist-visible", (int)playlistVisible );
310     settings->setValue( "adv-controls",
311                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
312
313     settings->setValue( "mainBasedSize", mainBasedSize );
314     settings->setValue( "mainVideoSize", mainVideoSize );
315
316     settings->setValue( "backgroundSize", bgWidget->size() );
317
318     /* Save this size */
319     QVLCTools::saveWidgetPosition(settings, this);
320     settings->endGroup();
321
322     delete statusBar();
323
324     /* Unregister callbacks */
325     var_DelCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
326     var_DelCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
327
328     p_intf->p_sys->p_mi = NULL;
329 }
330
331 /*****************************
332  *   Main UI handling        *
333  *****************************/
334 void MainInterface::recreateToolbars()
335 {
336     // FIXME: do the same for the FSC
337     settings->beginGroup( "MainWindow" );
338     delete controls;
339     delete inputC;
340
341     controls = new ControlsWidget( p_intf, false, this ); /* FIXME */
342     CONNECT( controls, advancedControlsToggled( bool ),
343              this, adaptGeometry() );
344     CONNECT( controls, sizeChanged(),
345              this, adaptGeometry() );
346
347     inputC = new InputControlsWidget( p_intf, this );
348
349     mainLayout->insertWidget( 2, inputC );
350     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
351                               controls );
352     settings->endGroup();
353 }
354
355 void MainInterface::createMainWidget( QSettings *settings )
356 {
357     /* Create the main Widget and the mainLayout */
358     QWidget *main = new QWidget;
359     setCentralWidget( main );
360     mainLayout = new QVBoxLayout( main );
361     main->setContentsMargins( 0, 0, 0, 0 );
362     mainLayout->setSpacing( 0 ); mainLayout->setMargin( 0 );
363
364     /* */
365     stackCentralW = new QVLCStackedWidget( main );
366
367     /* Bg Cone */
368     bgWidget = new BackgroundWidget( p_intf );
369     stackCentralW->addWidget( bgWidget );
370
371     /* And video Outputs */
372     if( b_videoEmbedded )
373     {
374         videoWidget = new VideoWidget( p_intf );
375         stackCentralW->addWidget( videoWidget );
376     }
377     mainLayout->insertWidget( 1, stackCentralW );
378
379     /* Create the CONTROLS Widget */
380     controls = new ControlsWidget( p_intf,
381                    settings->value( "adv-controls", false ).toBool(), this );
382     CONNECT( controls, advancedControlsToggled( bool ),
383              this, adaptGeometry() );
384     CONNECT( controls, sizeChanged(),
385              this, adaptGeometry() );
386     inputC = new InputControlsWidget( p_intf, this );
387
388     mainLayout->insertWidget( 2, inputC );
389     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
390                               controls );
391
392     /* Visualisation, disabled for now, they SUCK */
393     #if 0
394     visualSelector = new VisualSelector( p_intf );
395     mainLayout->insertWidget( 0, visualSelector );
396     visualSelector->hide();
397     #endif
398
399     getSettings()->endGroup();
400
401     /* Enable the popup menu in the MI */
402     main->setContextMenuPolicy( Qt::CustomContextMenu );
403     CONNECT( main, customContextMenuRequested( const QPoint& ),
404              this, popupMenu( const QPoint& ) );
405
406     if ( depth() > 8 ) /* 8bit depth has too many issues with opacity */
407         /* Create the FULLSCREEN CONTROLS Widget */
408         if( var_InheritBool( p_intf, "qt-fs-controller" ) )
409         {
410             fullscreenControls = new FullscreenControllerWidget( p_intf, this );
411             CONNECT( fullscreenControls, keyPressed( QKeyEvent * ),
412                      this, handleKeyPress( QKeyEvent * ) );
413         }
414 }
415
416 inline void MainInterface::initSystray()
417 {
418 #ifndef HAVE_MAEMO
419     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
420     bool b_systrayWanted = var_InheritBool( p_intf, "qt-system-tray" );
421
422     if( var_InheritBool( p_intf, "qt-start-minimized") )
423     {
424         if( b_systrayAvailable )
425         {
426             b_systrayWanted = true;
427             b_hideAfterCreation = true;
428         }
429         else
430             msg_Err( p_intf, "cannot start minimized without system tray bar" );
431     }
432
433     if( b_systrayAvailable && b_systrayWanted )
434         createSystray();
435 #endif
436 }
437
438 inline void MainInterface::createStatusBar()
439 {
440     /****************
441      *  Status Bar  *
442      ****************/
443     /* Widgets Creation*/
444     QStatusBar *statusBarr = statusBar();
445
446     TimeLabel *timeLabel = new TimeLabel( p_intf );
447     nameLabel = new QLabel( this );
448     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
449                                       | Qt::TextSelectableByKeyboard );
450     SpeedLabel *speedLabel = new SpeedLabel( p_intf, this );
451
452     /* Styling those labels */
453     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
454     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
455     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
456     timeLabel->setStyleSheet(
457             "QLabel:hover { background-color: rgba(255, 255, 255, 50%) }" );
458     speedLabel->setStyleSheet(
459             "QLabel:hover { background-color: rgba(255, 255, 255, 50%) }" );
460
461     /* and adding those */
462     statusBarr->addWidget( nameLabel, 8 );
463     statusBarr->addPermanentWidget( speedLabel, 0 );
464     statusBarr->addPermanentWidget( timeLabel, 0 );
465
466     /* timeLabel behaviour:
467        - double clicking opens the goto time dialog
468        - right-clicking and clicking just toggle between remaining and
469          elapsed time.*/
470     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
471
472     CONNECT( THEMIM->getIM(), encryptionChanged( bool ),
473              this, showCryptedLabel( bool ) );
474
475     CONNECT( THEMIM->getIM(), seekRequested( float ),
476              timeLabel, setDisplayPosition( float ) );
477 }
478
479 /**********************************************************************
480  * Handling of sizing of the components
481  **********************************************************************/
482
483 /* This function is called:
484    - Advanced buttons toggled
485    - Toolbar geom changed
486  */
487 void MainInterface::adaptGeometry()
488 {
489   resize( sizeHint() );
490
491 #ifdef DEBUG_INTF
492     debug();
493 #endif
494 }
495
496 void MainInterface::debug()
497 {
498 #ifdef DEBUG_INTF
499     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
500     msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
501     msg_Dbg( p_intf, "minimumsize: %i - %i", minimumSize().height(), minimumSize().width() );
502
503     msg_Dbg( p_intf, "Stack size: %i - %i", stackCentralW->size().height(), stackCentralW->size().width() );
504     msg_Dbg( p_intf, "Stack sizeHint: %i - %i", stackCentralW->sizeHint().height(), stackCentralW->sizeHint().width() );
505     msg_Dbg( p_intf, "Central size: %i - %i", centralWidget()->size().height(), centralWidget()->size().width() );
506 #endif
507 }
508
509 inline void MainInterface::showVideo() { showTab( videoWidget ); }
510 inline void MainInterface::showBg() { showTab( bgWidget ); }
511
512 inline void MainInterface::showTab( QWidget *widget )
513 {
514 #ifdef DEBUG_INTF
515     msg_Warn( p_intf, "Old stackCentralOldWidget %i", stackCentralW->indexOf( stackCentralOldWidget ) );
516 #endif
517     stackCentralOldWidget = stackCentralW->currentWidget();
518     stackCentralW->setCurrentWidget( widget );
519
520 #ifdef DEBUG_INTF
521     msg_Warn( p_intf, "State change %i",  stackCentralW->currentIndex() );
522     msg_Warn( p_intf, "New stackCentralOldWidget %i", stackCentralW->indexOf( stackCentralOldWidget ) );
523 #endif
524 }
525
526 inline void MainInterface::restoreStackOldWidget()
527 {
528 #ifdef DEBUG_INTF
529     msg_Warn( p_intf, "New stackCentralOldWidget %i", stackCentralW->indexOf( stackCentralOldWidget ) );
530 #endif
531     QWidget *wTemp = stackCentralW->currentWidget();
532
533     stackCentralW->setCurrentWidget( stackCentralOldWidget );
534
535     stackCentralOldWidget = wTemp;
536 #ifdef DEBUG_INTF
537     msg_Warn( p_intf, "Debug %i %i",stackCentralW->indexOf( wTemp ), stackCentralW->indexOf( stackCentralW->currentWidget() ) );
538 #endif
539 }
540
541 void MainInterface::destroyPopupMenu()
542 {
543     QVLCMenu::PopupMenu( p_intf, false );
544 }
545
546 void MainInterface::popupMenu( const QPoint &p )
547 {
548     QVLCMenu::PopupMenu( p_intf, true );
549 }
550
551 void MainInterface::toggleFSC()
552 {
553    if( !fullscreenControls ) return;
554
555    IMEvent *eShow = new IMEvent( FullscreenControlToggle_Type, 0 );
556    QApplication::postEvent( fullscreenControls, eShow );
557 }
558
559 /****************************************************************************
560  * Video Handling
561  ****************************************************************************/
562
563 /* This event is used to deal with the fullscreen and always on top
564    issue conflict (bug in wx) */
565 class SetVideoOnTopQtEvent : public QEvent
566 {
567 public:
568     SetVideoOnTopQtEvent( bool _onTop ) :
569       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
570     {}
571
572     bool OnTop() const { return onTop; }
573
574 private:
575     bool onTop;
576 };
577
578 /**
579  * NOTE:
580  * You must not change the state of this object or other Qt4 UI objects,
581  * from the video output thread - only from the Qt4 UI main loop thread.
582  * All window provider queries must be handled through signals or events.
583  * That's why we have all those emit statements...
584  */
585 WId MainInterface::getVideo( int *pi_x, int *pi_y,
586                              unsigned int *pi_width, unsigned int *pi_height )
587 {
588     if( !videoWidget )
589         return 0;
590
591     /* This is a blocking call signal. Results are returned through pointers.
592      * Beware of deadlocks! */
593     WId id;
594     emit askGetVideo( &id, pi_x, pi_y, pi_width, pi_height );
595     return id;
596 }
597
598 void MainInterface::getVideoSlot( WId *p_id, int *pi_x, int *pi_y,
599                                   unsigned *pi_width, unsigned *pi_height )
600 {
601     /* Request the videoWidget */
602     WId ret = videoWidget->request( pi_x, pi_y,
603                                     pi_width, pi_height, b_keep_size );
604     *p_id = ret;
605     if( ret ) /* The videoWidget is available */
606     {
607         /* ask videoWidget to show */
608         videoWidget->SetSizing( *pi_width, *pi_height );
609
610         /* Consider the video active now */
611         showVideo();
612     }
613 }
614
615 /* Asynchronous call from the WindowClose function */
616 void MainInterface::releaseVideo( void )
617 {
618     emit askReleaseVideo();
619 }
620
621 /* Function that is CONNECTED to the previous emit */
622 void MainInterface::releaseVideoSlot( void )
623 {
624     videoWidget->release();
625
626     restoreStackOldWidget();
627
628     /* We don't want to have a blank video to popup */
629     stackCentralOldWidget = bgWidget;
630 }
631
632 /* Asynchronous call from WindowControl function */
633 int MainInterface::controlVideo( int i_query, va_list args )
634 {
635     switch( i_query )
636     {
637     case VOUT_WINDOW_SET_SIZE:
638     {
639         unsigned int i_width  = va_arg( args, unsigned int );
640         unsigned int i_height = va_arg( args, unsigned int );
641         emit askVideoToResize( i_width, i_height );
642         return VLC_SUCCESS;
643     }
644     case VOUT_WINDOW_SET_STATE:
645     {
646         unsigned i_arg = va_arg( args, unsigned );
647         unsigned on_top = i_arg & VOUT_WINDOW_STATE_ABOVE;
648         QApplication::postEvent( this, new SetVideoOnTopQtEvent( on_top ) );
649         return VLC_SUCCESS;
650     }
651     case VOUT_WINDOW_SET_FULLSCREEN:
652     {
653         bool b_fs = va_arg( args, int );
654         emit askVideoSetFullScreen( b_fs );
655         return VLC_SUCCESS;
656     }
657     default:
658         msg_Warn( p_intf, "unsupported control query" );
659         return VLC_EGENERIC;
660     }
661 }
662
663 /*****************************************************************************
664  * Playlist, Visualisation and Menus handling
665  *****************************************************************************/
666 /**
667  * Toggle the playlist widget or dialog
668  **/
669 void MainInterface::createPlaylist()
670 {
671     playlistWidget = new PlaylistWidget( p_intf, this );
672
673     if( !b_plDocked )
674     {
675         playlistWidget->setWindowFlags( Qt::Window );
676
677         /* This will restore the geometry but will not work for position,
678            because of parenting */
679         QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
680                 playlistWidget, QSize( 600, 300 ) );
681     }
682     else
683     {
684         stackCentralW->addWidget( playlistWidget );
685     }
686 }
687
688 void MainInterface::togglePlaylist()
689 {
690     if( !playlistWidget )
691         createPlaylist();
692
693     if( b_plDocked )
694     {
695         /* Playlist is not visible, show it */
696         if( stackCentralW->currentWidget() != playlistWidget )
697         {
698             showTab( playlistWidget );
699         }
700         else /* Hide it! */
701         {
702             restoreStackOldWidget();
703         }
704         playlistVisible = ( stackCentralW->currentWidget() == playlistWidget );
705     }
706     else
707     {
708         playlistWidget->setWindowFlags( Qt::Window );
709         playlistVisible = !playlistVisible;
710         playlistWidget->setVisible( playlistVisible );
711     }
712     debug();
713 }
714
715 void MainInterface::dockPlaylist( bool p_docked )
716 {
717     if( b_plDocked == p_docked ) return;
718     b_plDocked = p_docked;
719
720     if( !playlistWidget ) return; /* Playlist wasn't created yet */
721     if( !p_docked )
722     {
723         stackCentralW->removeWidget( playlistWidget );
724         playlistWidget->setWindowFlags( Qt::Window );
725         QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
726                 playlistWidget, QSize( 600, 300 ) );
727         playlistWidget->show();
728         restoreStackOldWidget();
729     }
730     else
731     {
732         playlistWidget->setWindowFlags( Qt::Widget ); // Probably a Qt bug here
733         // It would be logical that QStackWidget::addWidget reset the flags...
734         stackCentralW->addWidget( playlistWidget );
735         stackCentralW->setCurrentWidget( playlistWidget );
736     }
737 }
738
739 /*
740   If b_switch is false, then we are normalView
741  */
742 void MainInterface::toggleMinimalView( bool b_switch )
743 {
744     if( i_visualmode == 0 )
745     { /* NORMAL MODE then */
746         if( !videoWidget || stackCentralW->currentWidget() != videoWidget )
747         {
748             showBg();
749         }
750         else
751         {
752             /* If video is visible, then toggle the status of bgWidget */
753             //FIXME
754             //bgWasVisible = !bgWasVisible;
755             /* if( stackCentralOldState == BACK G_TAB )
756                 stackCentralOldState = HID DEN_TAB;
757             else
758                 stackCentralOldState = BACK G_TAB;
759
760                 */
761         }
762     }
763
764     menuBar()->setVisible( !b_switch );
765     controls->setVisible( !b_switch );
766     statusBar()->setVisible( !b_switch );
767     inputC->setVisible( !b_switch );
768
769     emit minimalViewToggled( b_switch );
770 }
771
772 /* toggling advanced controls buttons */
773 void MainInterface::toggleAdvancedButtons()
774 {
775     controls->toggleAdvanced();
776 //    if( fullscreenControls ) fullscreenControls->toggleAdvanced();
777 }
778
779 /* Get the visibility status of the controls (hidden or not, advanced or not) */
780 int MainInterface::getControlsVisibilityStatus()
781 {
782     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
783                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
784 }
785
786 #if 0
787 void MainInterface::visual()
788 {
789     if( !VISIBLE( visualSelector) )
790     {
791         visualSelector->show();
792         if( !THEMIM->getIM()->hasVideo() )
793         {
794             /* Show the background widget */
795         }
796         visualSelectorEnabled = true;
797     }
798     else
799     {
800         /* Stop any currently running visualization */
801         visualSelector->hide();
802         visualSelectorEnabled = false;
803     }
804 }
805 #endif
806
807 /************************************************************************
808  * Other stuff
809  ************************************************************************/
810 void MainInterface::setName( const QString& name )
811 {
812     input_name = name; /* store it for the QSystray use */
813     /* Display it in the status bar, but also as a Tooltip in case it doesn't
814        fit in the label */
815     nameLabel->setText( " " + name + " " );
816     nameLabel->setToolTip( " " + name +" " );
817 }
818
819 /**
820  * Give the decorations of the Main Window a correct Name.
821  * If nothing is given, set it to VLC...
822  **/
823 void MainInterface::setVLCWindowsTitle( const QString& aTitle )
824 {
825     if( aTitle.isEmpty() )
826     {
827         setWindowTitle( qtr( "VLC media player" ) );
828     }
829     else
830     {
831         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
832     }
833 }
834
835 void MainInterface::showCryptedLabel( bool b_show )
836 {
837     if( cryptedLabel == NULL )
838     {
839         cryptedLabel = new QLabel;
840         // The lock icon is not the right one for DRM protection/scrambled.
841         //cryptedLabel->setPixmap( QPixmap( ":/lock" ) );
842         cryptedLabel->setText( "DRM" );
843         statusBar()->addWidget( cryptedLabel );
844     }
845
846     cryptedLabel->setVisible( b_show );
847 }
848
849 void MainInterface::showBuffering( float f_cache )
850 {
851     QString amount = QString("Buffering: %1%").arg( (int)(100*f_cache) );
852     statusBar()->showMessage( amount, 1000 );
853 }
854
855 /*****************************************************************************
856  * Systray Icon and Systray Menu
857  *****************************************************************************/
858 #ifndef HAVE_MAEMO
859 /**
860  * Create a SystemTray icon and a menu that would go with it.
861  * Connects to a click handler on the icon.
862  **/
863 void MainInterface::createSystray()
864 {
865     QIcon iconVLC;
866     if( QDate::currentDate().dayOfYear() >= 354 )
867         iconVLC =  QIcon( ":/logo/vlc128-christmas.png" );
868     else
869         iconVLC =  QIcon( ":/logo/vlc128.png" );
870     sysTray = new QSystemTrayIcon( iconVLC, this );
871     sysTray->setToolTip( qtr( "VLC media player" ));
872
873     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
874     systrayMenu->setIcon( iconVLC );
875
876     QVLCMenu::updateSystrayMenu( this, p_intf, true );
877     sysTray->show();
878
879     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
880             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
881 }
882
883 /**
884  * Updates the Systray Icon's menu and toggle the main interface
885  */
886 void MainInterface::toggleUpdateSystrayMenu()
887 {
888     /* If hidden, show it */
889     if( isHidden() )
890     {
891         show();
892         activateWindow();
893     }
894     else if( isMinimized() )
895     {
896         /* Minimized */
897         showNormal();
898         activateWindow();
899     }
900     else
901     {
902         /* Visible (possibly under other windows) */
903 #ifdef WIN32
904         /* check if any visible window is above vlc in the z-order,
905          * but ignore the ones always on top
906          * and the ones which can't be activated */
907         WINDOWINFO wi;
908         HWND hwnd;
909         wi.cbSize = sizeof( WINDOWINFO );
910         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
911                 hwnd && ( !IsWindowVisible( hwnd ) ||
912                     ( GetWindowInfo( hwnd, &wi ) &&
913                       (wi.dwExStyle&WS_EX_NOACTIVATE) ) );
914                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
915             if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
916                 (wi.dwExStyle&WS_EX_TOPMOST) )
917             {
918                 hide();
919             }
920             else
921             {
922                 activateWindow();
923             }
924 #else
925         hide();
926 #endif
927     }
928     QVLCMenu::updateSystrayMenu( this, p_intf );
929 }
930
931 void MainInterface::handleSystrayClick(
932                                     QSystemTrayIcon::ActivationReason reason )
933 {
934     switch( reason )
935     {
936         case QSystemTrayIcon::Trigger:
937         case QSystemTrayIcon::DoubleClick:
938             toggleUpdateSystrayMenu();
939             break;
940         case QSystemTrayIcon::MiddleClick:
941             sysTray->showMessage( qtr( "VLC media player" ),
942                     qtr( "Control menu for the player" ),
943                     QSystemTrayIcon::Information, 3000 );
944             break;
945         default:
946             break;
947     }
948 }
949
950 /**
951  * Updates the name of the systray Icon tooltip.
952  * Doesn't check if the systray exists, check before you call it.
953  **/
954 void MainInterface::updateSystrayTooltipName( const QString& name )
955 {
956     if( name.isEmpty() )
957     {
958         sysTray->setToolTip( qtr( "VLC media player" ) );
959     }
960     else
961     {
962         sysTray->setToolTip( name );
963         if( b_notificationEnabled && ( isHidden() || isMinimized() ) )
964         {
965             sysTray->showMessage( qtr( "VLC media player" ), name,
966                     QSystemTrayIcon::NoIcon, 3000 );
967         }
968     }
969
970     QVLCMenu::updateSystrayMenu( this, p_intf );
971 }
972
973 /**
974  * Updates the status of the systray Icon tooltip.
975  * Doesn't check if the systray exists, check before you call it.
976  **/
977 void MainInterface::updateSystrayTooltipStatus( int i_status )
978 {
979     switch( i_status )
980     {
981         case  0:
982         case  END_S:
983             {
984                 sysTray->setToolTip( qtr( "VLC media player" ) );
985                 break;
986             }
987         case PLAYING_S:
988             {
989                 sysTray->setToolTip( input_name );
990                 break;
991             }
992         case PAUSE_S:
993             {
994                 sysTray->setToolTip( input_name + " - "
995                         + qtr( "Paused") );
996                 break;
997             }
998     }
999     QVLCMenu::updateSystrayMenu( this, p_intf );
1000 }
1001 #endif
1002
1003 /************************************************************************
1004  * D&D Events
1005  ************************************************************************/
1006 void MainInterface::dropEvent(QDropEvent *event)
1007 {
1008     dropEventPlay( event, true );
1009 }
1010
1011 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
1012 {
1013     event->setDropAction( Qt::CopyAction );
1014     if( !event->possibleActions() & Qt::CopyAction )
1015         return;
1016
1017     const QMimeData *mimeData = event->mimeData();
1018
1019     /* D&D of a subtitles file, add it on the fly */
1020     if( mimeData->urls().size() == 1 && THEMIM->getIM()->hasInput() )
1021     {
1022         if( !input_AddSubtitle( THEMIM->getInput(),
1023                  qtu( toNativeSeparators( mimeData->urls()[0].toLocalFile() ) ),
1024                  true ) )
1025         {
1026             event->accept();
1027             return;
1028         }
1029     }
1030
1031     bool first = b_play;
1032     foreach( const QUrl &url, mimeData->urls() )
1033     {
1034         QString s = toNativeSeparators( url.toLocalFile() );
1035
1036         if( s.length() > 0 ) {
1037             char* psz_uri = make_URI( qtu(s) );
1038             playlist_Add( THEPL, psz_uri, NULL,
1039                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: PLAYLIST_PREPARSE),
1040                           PLAYLIST_END, true, pl_Unlocked );
1041             free( psz_uri );
1042             first = false;
1043             RecentsMRL::getInstance( p_intf )->addRecent( s );
1044         }
1045     }
1046     event->accept();
1047 }
1048 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1049 {
1050      event->acceptProposedAction();
1051 }
1052 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1053 {
1054      event->acceptProposedAction();
1055 }
1056 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1057 {
1058      event->accept();
1059 }
1060
1061 /************************************************************************
1062  * Events stuff
1063  ************************************************************************/
1064 void MainInterface::customEvent( QEvent *event )
1065 {
1066 #if 0
1067     if( event->type() == PLDockEvent_Type )
1068     {
1069         PlaylistDialog::killInstance();
1070         playlistEmbeddedFlag = true;
1071         menuBar()->clear();
1072         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1073         togglePlaylist();
1074     }
1075 #endif
1076     /*else */
1077     if ( event->type() == (int)SetVideoOnTopEvent_Type )
1078     {
1079         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1080         if( p_event->OnTop() )
1081             setWindowFlags( windowFlags() | Qt::WindowStaysOnTopHint );
1082         else
1083             setWindowFlags( windowFlags() & ~Qt::WindowStaysOnTopHint );
1084         show(); /* necessary to apply window flags */
1085     }
1086 }
1087
1088 void MainInterface::keyPressEvent( QKeyEvent *e )
1089 {
1090     handleKeyPress( e );
1091 }
1092
1093 void MainInterface::handleKeyPress( QKeyEvent *e )
1094 {
1095     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1096           && !menuBar()->isVisible() )
1097     {
1098         toggleMinimalView( false );
1099         e->accept();
1100     }
1101
1102     int i_vlck = qtEventToVLCKey( e );
1103     if( i_vlck > 0 )
1104     {
1105         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1106         e->accept();
1107     }
1108     else
1109         e->ignore();
1110 }
1111
1112 void MainInterface::resizeEvent( QResizeEvent * event )
1113 {
1114 #if 0
1115     if( b_keep_size )
1116     {
1117         if( i_visualmode )
1118         {
1119                 mainVideoSize = size();
1120         }
1121         else
1122         {
1123             if( VISIBLE( bgWidget) ||
1124                 ( videoIsActive && videoWidget->isVisible() )
1125               )
1126                 mainVideoSize = size();
1127             else
1128                 mainBasedSize = size();
1129         }
1130     }
1131 #endif
1132     QVLCMW::resizeEvent( event );
1133     msg_Dbg( p_intf, "Resize Event, height: %i", size().height() );
1134     debug();
1135 }
1136
1137 void MainInterface::wheelEvent( QWheelEvent *e )
1138 {
1139     int i_vlckey = qtWheelEventToVLCKey( e );
1140     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1141     e->accept();
1142 }
1143
1144 void MainInterface::closeEvent( QCloseEvent *e )
1145 {
1146     e->accept();
1147     hide();
1148     THEDP->quit();
1149 }
1150
1151 void MainInterface::toggleFullScreen()
1152 {
1153     if( isFullScreen() )
1154     {
1155         showNormal();
1156         emit fullscreenInterfaceToggled( false );
1157     }
1158     else
1159     {
1160         showFullScreen();
1161         emit fullscreenInterfaceToggled( true );
1162     }
1163 }
1164
1165 /*****************************************************************************
1166  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1167  *  We don't show the menu directly here because we don't want the
1168  *  caller to block for a too long time.
1169  *****************************************************************************/
1170 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1171                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1172 {
1173     intf_thread_t *p_intf = (intf_thread_t *)param;
1174
1175     if( p_intf->pf_show_dialog )
1176     {
1177         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1178                                 new_val.b_bool, NULL );
1179     }
1180
1181     return VLC_SUCCESS;
1182 }
1183
1184 /*****************************************************************************
1185  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1186  *****************************************************************************/
1187 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1188                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1189 {
1190     intf_thread_t *p_intf = (intf_thread_t *)param;
1191     p_intf->p_sys->p_mi->toggleFSC();
1192
1193     /* Show event */
1194      return VLC_SUCCESS;
1195 }