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