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