]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Qt: put Win32 Main Interface function in its own file.
[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 void MainInterface::toggleMinimalView( bool b_switch )
864 {
865     if( i_visualmode == 0 )
866     { /* NORMAL MODE then */
867         if( !videoWidget || stackCentralW->currentWidget() != videoWidget )
868         {
869             showBg();
870         }
871         else
872         {
873             /* If video is visible, then toggle the status of bgWidget */
874             //FIXME
875             //bgWasVisible = !bgWasVisible;
876             /* if( stackCentralOldState == BACK G_TAB )
877                 stackCentralOldState = HID DEN_TAB;
878             else
879                 stackCentralOldState = BACK G_TAB;
880
881                 */
882         }
883     }
884
885     i_bg_height = stackCentralW->height();
886
887     menuBar()->setVisible( !b_switch );
888     controls->setVisible( !b_switch );
889     statusBar()->setVisible( !b_switch );
890     inputC->setVisible( !b_switch );
891
892     doComponentsUpdate();
893
894     emit minimalViewToggled( b_switch );
895 }
896
897 /* toggling advanced controls buttons */
898 void MainInterface::toggleAdvanced()
899 {
900     controls->toggleAdvanced();
901 //    if( fullscreenControls ) fullscreenControls->toggleAdvanced();
902 }
903
904 /* Get the visibility status of the controls (hidden or not, advanced or not) */
905 int MainInterface::getControlsVisibilityStatus()
906 {
907     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
908                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
909 }
910
911 #if 0
912 void MainInterface::visual()
913 {
914     if( !VISIBLE( visualSelector) )
915     {
916         visualSelector->show();
917         if( !THEMIM->getIM()->hasVideo() )
918         {
919             /* Show the background widget */
920         }
921         visualSelectorEnabled = true;
922     }
923     else
924     {
925         /* Stop any currently running visualization */
926         visualSelector->hide();
927         visualSelectorEnabled = false;
928     }
929     doComponentsUpdate();
930 }
931 #endif
932
933 /************************************************************************
934  * Other stuff
935  ************************************************************************/
936 void MainInterface::setName( const QString& name )
937 {
938     input_name = name; /* store it for the QSystray use */
939     /* Display it in the status bar, but also as a Tooltip in case it doesn't
940        fit in the label */
941     nameLabel->setText( " " + name + " " );
942     nameLabel->setToolTip( " " + name +" " );
943 }
944
945 /**
946  * Give the decorations of the Main Window a correct Name.
947  * If nothing is given, set it to VLC...
948  **/
949 void MainInterface::setVLCWindowsTitle( const QString& aTitle )
950 {
951     if( aTitle.isEmpty() )
952     {
953         setWindowTitle( qtr( "VLC media player" ) );
954     }
955     else
956     {
957         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
958     }
959 }
960
961 void MainInterface::showCryptedLabel( bool b_show )
962 {
963     if( cryptedLabel == NULL )
964     {
965         cryptedLabel = new QLabel;
966         // The lock icon is not the right one for DRM protection/scrambled.
967         //cryptedLabel->setPixmap( QPixmap( ":/lock" ) );
968         cryptedLabel->setText( "DRM" );
969         statusBar()->addWidget( cryptedLabel );
970     }
971
972     cryptedLabel->setVisible( b_show );
973 }
974
975 void MainInterface::showBuffering( float f_cache )
976 {
977     QString amount = QString("Buffering: %1%").arg( (int)(100*f_cache) );
978     statusBar()->showMessage( amount, 1000 );
979 }
980
981 /*****************************************************************************
982  * Systray Icon and Systray Menu
983  *****************************************************************************/
984 #ifndef HAVE_MAEMO
985 /**
986  * Create a SystemTray icon and a menu that would go with it.
987  * Connects to a click handler on the icon.
988  **/
989 void MainInterface::createSystray()
990 {
991     QIcon iconVLC;
992     if( QDate::currentDate().dayOfYear() >= 354 )
993         iconVLC =  QIcon( ":/logo/vlc128-christmas.png" );
994     else
995         iconVLC =  QIcon( ":/logo/vlc128.png" );
996     sysTray = new QSystemTrayIcon( iconVLC, this );
997     sysTray->setToolTip( qtr( "VLC media player" ));
998
999     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
1000     systrayMenu->setIcon( iconVLC );
1001
1002     QVLCMenu::updateSystrayMenu( this, p_intf, true );
1003     sysTray->show();
1004
1005     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
1006             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
1007 }
1008
1009 /**
1010  * Updates the Systray Icon's menu and toggle the main interface
1011  */
1012 void MainInterface::toggleUpdateSystrayMenu()
1013 {
1014     /* If hidden, show it */
1015     if( isHidden() )
1016     {
1017         show();
1018         activateWindow();
1019     }
1020     else if( isMinimized() )
1021     {
1022         /* Minimized */
1023         showNormal();
1024         activateWindow();
1025     }
1026     else
1027     {
1028         /* Visible (possibly under other windows) */
1029 #ifdef WIN32
1030         /* check if any visible window is above vlc in the z-order,
1031          * but ignore the ones always on top
1032          * and the ones which can't be activated */
1033         WINDOWINFO wi;
1034         HWND hwnd;
1035         wi.cbSize = sizeof( WINDOWINFO );
1036         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
1037                 hwnd && ( !IsWindowVisible( hwnd ) ||
1038                     ( GetWindowInfo( hwnd, &wi ) &&
1039                       (wi.dwExStyle&WS_EX_NOACTIVATE) ) );
1040                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
1041             if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
1042                 (wi.dwExStyle&WS_EX_TOPMOST) )
1043             {
1044                 hide();
1045             }
1046             else
1047             {
1048                 activateWindow();
1049             }
1050 #else
1051         hide();
1052 #endif
1053     }
1054     QVLCMenu::updateSystrayMenu( this, p_intf );
1055 }
1056
1057 void MainInterface::handleSystrayClick(
1058                                     QSystemTrayIcon::ActivationReason reason )
1059 {
1060     switch( reason )
1061     {
1062         case QSystemTrayIcon::Trigger:
1063         case QSystemTrayIcon::DoubleClick:
1064             toggleUpdateSystrayMenu();
1065             break;
1066         case QSystemTrayIcon::MiddleClick:
1067             sysTray->showMessage( qtr( "VLC media player" ),
1068                     qtr( "Control menu for the player" ),
1069                     QSystemTrayIcon::Information, 3000 );
1070             break;
1071         default:
1072             break;
1073     }
1074 }
1075
1076 /**
1077  * Updates the name of the systray Icon tooltip.
1078  * Doesn't check if the systray exists, check before you call it.
1079  **/
1080 void MainInterface::updateSystrayTooltipName( const QString& name )
1081 {
1082     if( name.isEmpty() )
1083     {
1084         sysTray->setToolTip( qtr( "VLC media player" ) );
1085     }
1086     else
1087     {
1088         sysTray->setToolTip( name );
1089         if( b_notificationEnabled && ( isHidden() || isMinimized() ) )
1090         {
1091             sysTray->showMessage( qtr( "VLC media player" ), name,
1092                     QSystemTrayIcon::NoIcon, 3000 );
1093         }
1094     }
1095
1096     QVLCMenu::updateSystrayMenu( this, p_intf );
1097 }
1098
1099 /**
1100  * Updates the status of the systray Icon tooltip.
1101  * Doesn't check if the systray exists, check before you call it.
1102  **/
1103 void MainInterface::updateSystrayTooltipStatus( int i_status )
1104 {
1105     switch( i_status )
1106     {
1107         case  0:
1108         case  END_S:
1109             {
1110                 sysTray->setToolTip( qtr( "VLC media player" ) );
1111                 break;
1112             }
1113         case PLAYING_S:
1114             {
1115                 sysTray->setToolTip( input_name );
1116                 break;
1117             }
1118         case PAUSE_S:
1119             {
1120                 sysTray->setToolTip( input_name + " - "
1121                         + qtr( "Paused") );
1122                 break;
1123             }
1124     }
1125     QVLCMenu::updateSystrayMenu( this, p_intf );
1126 }
1127 #endif
1128
1129 /************************************************************************
1130  * D&D Events
1131  ************************************************************************/
1132 void MainInterface::dropEvent(QDropEvent *event)
1133 {
1134     dropEventPlay( event, true );
1135 }
1136
1137 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
1138 {
1139     event->setDropAction( Qt::CopyAction );
1140     if( !event->possibleActions() & Qt::CopyAction )
1141         return;
1142
1143     const QMimeData *mimeData = event->mimeData();
1144
1145     /* D&D of a subtitles file, add it on the fly */
1146     if( mimeData->urls().size() == 1 && THEMIM->getIM()->hasInput() )
1147     {
1148         if( !input_AddSubtitle( THEMIM->getInput(),
1149                  qtu( toNativeSeparators( mimeData->urls()[0].toLocalFile() ) ),
1150                  true ) )
1151         {
1152             event->accept();
1153             return;
1154         }
1155     }
1156
1157     bool first = b_play;
1158     foreach( const QUrl &url, mimeData->urls() )
1159     {
1160         QString s = toNativeSeparators( url.toLocalFile() );
1161
1162         if( s.length() > 0 ) {
1163             char* psz_uri = make_URI( qtu(s) );
1164             playlist_Add( THEPL, psz_uri, NULL,
1165                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: PLAYLIST_PREPARSE),
1166                           PLAYLIST_END, true, pl_Unlocked );
1167             free( psz_uri );
1168             first = false;
1169             RecentsMRL::getInstance( p_intf )->addRecent( s );
1170         }
1171     }
1172     event->accept();
1173 }
1174 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1175 {
1176      event->acceptProposedAction();
1177 }
1178 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1179 {
1180      event->acceptProposedAction();
1181 }
1182 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1183 {
1184      event->accept();
1185 }
1186
1187 /************************************************************************
1188  * Events stuff
1189  ************************************************************************/
1190 void MainInterface::customEvent( QEvent *event )
1191 {
1192 #if 0
1193     if( event->type() == PLDockEvent_Type )
1194     {
1195         PlaylistDialog::killInstance();
1196         playlistEmbeddedFlag = true;
1197         menuBar()->clear();
1198         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1199         togglePlaylist();
1200     }
1201 #endif
1202     /*else */
1203     if ( event->type() == (int)SetVideoOnTopEvent_Type )
1204     {
1205         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1206         if( p_event->OnTop() )
1207             setWindowFlags( windowFlags() | Qt::WindowStaysOnTopHint );
1208         else
1209             setWindowFlags( windowFlags() & ~Qt::WindowStaysOnTopHint );
1210         show(); /* necessary to apply window flags */
1211     }
1212 }
1213
1214 void MainInterface::keyPressEvent( QKeyEvent *e )
1215 {
1216     handleKeyPress( e );
1217 }
1218
1219 void MainInterface::handleKeyPress( QKeyEvent *e )
1220 {
1221     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1222           && !menuBar()->isVisible() )
1223     {
1224         toggleMinimalView( false );
1225         e->accept();
1226     }
1227
1228     int i_vlck = qtEventToVLCKey( e );
1229     if( i_vlck > 0 )
1230     {
1231         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1232         e->accept();
1233     }
1234     else
1235         e->ignore();
1236 }
1237
1238 void MainInterface::resizeEvent( QResizeEvent * event )
1239 {
1240 #if 0
1241     if( b_keep_size )
1242     {
1243         if( i_visualmode )
1244         {
1245                 mainVideoSize = size();
1246         }
1247         else
1248         {
1249             if( VISIBLE( bgWidget) ||
1250                 ( videoIsActive && videoWidget->isVisible() )
1251               )
1252                 mainVideoSize = size();
1253             else
1254                 mainBasedSize = size();
1255         }
1256     }
1257 #endif
1258     QVLCMW::resizeEvent( event );
1259     msg_Dbg( p_intf, "Resize Event, height: %i", size().height() );
1260     debug();
1261 }
1262
1263 void MainInterface::wheelEvent( QWheelEvent *e )
1264 {
1265     int i_vlckey = qtWheelEventToVLCKey( e );
1266     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1267     e->accept();
1268 }
1269
1270 void MainInterface::closeEvent( QCloseEvent *e )
1271 {
1272     e->accept();
1273     hide();
1274     THEDP->quit();
1275 }
1276
1277 void MainInterface::toggleFullScreen( void )
1278 {
1279     if( isFullScreen() )
1280     {
1281         showNormal();
1282         emit askUpdate(); // Needed if video was launched after the F11
1283         emit fullscreenInterfaceToggled( false );
1284     }
1285     else
1286     {
1287         showFullScreen();
1288         emit fullscreenInterfaceToggled( true );
1289     }
1290
1291 }
1292
1293 /*****************************************************************************
1294  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1295  *  We don't show the menu directly here because we don't want the
1296  *  caller to block for a too long time.
1297  *****************************************************************************/
1298 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1299                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1300 {
1301     intf_thread_t *p_intf = (intf_thread_t *)param;
1302
1303     if( p_intf->pf_show_dialog )
1304     {
1305         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1306                                 new_val.b_bool, NULL );
1307     }
1308
1309     return VLC_SUCCESS;
1310 }
1311
1312 /*****************************************************************************
1313  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1314  *****************************************************************************/
1315 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1316                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1317 {
1318     intf_thread_t *p_intf = (intf_thread_t *)param;
1319     p_intf->p_sys->p_mi->toggleFSC();
1320
1321     /* Show event */
1322      return VLC_SUCCESS;
1323 }