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