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