]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
36c0b033a9536addbca09dcc11562a54d6fcd67e
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "qt4.hpp"
26 #include "main_interface.hpp"
27 #include "input_manager.hpp"
28 #include "util/qvlcframe.hpp"
29 #include "util/customwidgets.hpp"
30 #include "dialogs_provider.hpp"
31 #include "components/interface_widgets.hpp"
32 #include "dialogs/playlist.hpp"
33 #include "menus.hpp"
34
35 #include <QMenuBar>
36 #include <QCloseEvent>
37 #include <QPushButton>
38 #include <QStatusBar>
39 #include <QKeyEvent>
40 #include <QUrl>
41 #include <QSystemTrayIcon>
42 #include <QSize>
43 #include <QMenu>
44 #include <QLabel>
45
46 #include <assert.h>
47 #include <vlc_keys.h>
48 #include <vlc_vout.h>
49
50 #ifdef WIN32
51     #define PREF_W 410
52     #define PREF_H 121
53 #else
54     #define PREF_W 450
55     #define PREF_H 160
56 #endif
57
58 #define SET_WIDTH(i,j) i->widgetSize.setWidth(j)
59 #define SET_HEIGHT(i,j) i->widgetSize.setHeight(j)
60 #define SET_WH( i,j,k) i->widgetSize.setWidth(j); i->widgetSize.setHeight(k);
61
62 #define DS(i) i.width(),i.height()
63
64 /* Callback prototypes */
65 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
66                         vlc_value_t old_val, vlc_value_t new_val, void *param );
67 static int IntfShowCB( 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 InteractCallback( vlc_object_t *, const char *, vlc_value_t,
70                              vlc_value_t, void *);
71 /* Video handling */
72 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
73                         int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
74 {
75     return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
76 }
77 static void DoRelease( intf_thread_t *p_intf, void *p_win )
78 {
79     return p_intf->p_sys->p_mi->releaseVideo( p_win );
80 }
81 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
82 {
83     return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
84 }
85
86 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
87 {
88     /* Variables initialisation */
89     need_components_update = false;
90     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
91     embeddedPlaylistWasActive = videoIsActive = false;
92     input_name = "";
93
94     /**
95      *  Configuration and settings
96      **/
97     settings = new QSettings( "VideoLAN", "VLC" );
98     settings->beginGroup( "MainWindow" );
99
100     /* Main settings */
101     setFocusPolicy( Qt::StrongFocus );
102     setAcceptDrops(true);
103     setWindowIcon( QApplication::windowIcon() );
104
105     /* Set The Video In emebedded Mode or not */
106     videoEmbeddedFlag = false;
107     if( config_GetInt( p_intf, "embedded-video" ) )
108         videoEmbeddedFlag = true;
109
110     alwaysVideoFlag = false;
111     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ) )
112         alwaysVideoFlag = true;
113
114     /* Set the other interface settings */
115     playlistEmbeddedFlag = settings->value("playlist-embedded", true).toBool();
116     visualSelectorEnabled= settings->value( "visual-selector", false ).toBool();
117
118     /**************************
119      *  UI and Widgets design
120      **************************/
121     setVLCWindowsTitle();
122     handleMainUi( settings );
123
124     /* Menu Bar */
125     QVLCMenu::createMenuBar( this, p_intf, playlistEmbeddedFlag,
126                              isAdvancedVisible(), visualSelectorEnabled );
127
128     /* Status Bar */
129     /**
130      * TODO: clicking on the elapsed time should switch to the remaining time
131      **/
132     /**
133      * TODO: do we add a label for the current Volume ?
134      **/
135     timeLabel = new QLabel;
136     nameLabel = new QLabel;
137     speedLabel = new QLabel( "1.0x" );
138     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
139     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
140     statusBar()->addWidget( nameLabel, 8 );
141     statusBar()->addPermanentWidget( speedLabel, 0 );
142     statusBar()->addPermanentWidget( timeLabel, 2 );
143     speedLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
144     timeLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
145     CONNECT( speedLabel, customContextMenuRequested( QPoint ),
146              this, showSpeedMenu( QPoint ) );
147     CONNECT( timeLabel, customContextMenuRequested( QPoint ),
148              this, showTimeMenu( QPoint ) );
149
150     /**********************
151      * Systray Management *
152      **********************/
153     sysTray = NULL;
154     bool b_createSystray = false;
155     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
156     if( config_GetInt( p_intf, "qt-start-minimized") )
157     {
158         if( b_systrayAvailable ){
159             b_createSystray = true;
160             hide(); //FIXME
161         }
162         else msg_Warn( p_intf, "You can't minize if you haven't a system "
163                 "tray bar" );
164     }
165     if( config_GetInt( p_intf, "qt-system-tray") )
166         b_createSystray = true;
167
168     if( b_systrayAvailable && b_createSystray )
169             createSystray();
170
171     /* Init input manager */
172     MainInputManager::getInstance( p_intf );
173     ON_TIMEOUT( updateOnTimer() );
174
175     /**
176      * Various CONNECTs
177      **/
178
179     /* Connect the input manager to the GUI elements it manages */
180     /* It is also connected to the control->slider, see the ControlsWidget */
181     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
182              this, setDisplay( float, int, int ) );
183
184     /** Connects on nameChanged() */
185     /* Naming in the controller statusbar */
186     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
187              setName( QString ) );
188     /* and in the systray */
189     if( sysTray )
190     {
191         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
192                  updateSystrayTooltipName( QString ) );
193     }
194     /* and in the title of the controller */
195     if( config_GetInt( p_intf, "qt-name-in-title" ) )
196     {
197         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
198              setVLCWindowsTitle( QString ) );
199     }
200
201     /** CONNECTS on PLAY_STATUS **/
202     /* Status on the main controller */
203     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
204     /* and in the systray */
205     if( sysTray )
206     {
207         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
208                  updateSystrayTooltipStatus( int ) );
209     }
210
211     /**
212      * Callbacks
213      **/
214     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
215     var_AddCallback( p_intf, "interaction", InteractCallback, this );
216     p_intf->b_interaction = VLC_TRUE;
217
218     /* Register callback for the intf-popupmenu variable */
219     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
220                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
221     if( p_playlist != NULL )
222     {
223         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
224         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
225         vlc_object_release( p_playlist );
226     }
227 }
228
229 MainInterface::~MainInterface()
230 {
231     /* Unregister callback for the intf-popupmenu variable */
232     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
233                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
234     if( p_playlist != NULL )
235     {
236         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
237         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
238         vlc_object_release( p_playlist );
239     }
240
241     settings->setValue( "playlist-embedded", playlistEmbeddedFlag );
242     settings->setValue( "adv-controls", isAdvancedVisible() );
243     settings->setValue( "pos", pos() );
244     settings->endGroup();
245     delete settings;
246     p_intf->b_interaction = VLC_FALSE;
247     var_DelCallback( p_intf, "interaction", InteractCallback, this );
248
249     p_intf->pf_request_window = NULL;
250     p_intf->pf_release_window = NULL;
251     p_intf->pf_control_window = NULL;
252 }
253
254 /*****************************
255  *   Main UI handling        *
256  *****************************/
257
258 /**
259  * Give the decorations of the Main Window a correct Name.
260  * If nothing is given, set it to VLC...
261  **/
262 void MainInterface::setVLCWindowsTitle( QString aTitle )
263 {
264     if( aTitle.isEmpty() )
265     {
266         setWindowTitle( qtr( "VLC media player" ) );
267     }
268     else
269     {
270         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
271     }
272 }
273
274 void MainInterface::handleMainUi( QSettings *settings )
275 {
276     /* Create the main Widget and the mainLayout */
277     QWidget *main = new QWidget( this );
278     mainLayout = new QVBoxLayout( main );
279     setCentralWidget( main );
280
281     /* Margins, spacing */
282     main->setContentsMargins( 0, 0, 0, 0 );
283     mainLayout->setMargin( 0 );
284
285     /* Create the CONTROLS Widget */
286     controls = new ControlsWidget( p_intf,
287                    settings->value( "adv-controls", false ).toBool() );
288
289     /* Configure the Controls */
290     BUTTON_SET_IMG( controls->playlistButton, "" , playlist_icon.png,
291                     playlistEmbeddedFlag ?  qtr( "Show playlist" ) :
292                                             qtr( "Open playlist" ) );
293     BUTTONACT( controls->playlistButton, togglePlaylist() );
294
295     /* Add the controls Widget to the main Widget */
296     mainLayout->addWidget( controls );
297
298
299     /* Set initial size */
300     resize( PREF_W, PREF_H );
301     addSize = QSize( mainLayout->margin() * 2, PREF_H );
302
303     /* Visualisation */
304     visualSelector = new VisualSelector( p_intf );
305     mainLayout->insertWidget( 0, visualSelector );
306     visualSelector->hide();
307
308     /* And video Outputs */
309     if( alwaysVideoFlag )
310     {
311         bgWidget = new BackgroundWidget( p_intf );
312         bgWidget->widgetSize = settings->value( "backgroundSize",
313                                            QSize( 300, 300 ) ).toSize();
314         bgWidget->resize( bgWidget->widgetSize );
315         bgWidget->updateGeometry();
316         mainLayout->insertWidget( 0, bgWidget );
317     }
318
319     if( videoEmbeddedFlag )
320     {
321         videoWidget = new VideoWidget( p_intf );
322         videoWidget->widgetSize = QSize( 1, 1 );
323         //videoWidget->resize( videoWidget->widgetSize );
324         mainLayout->insertWidget( 0, videoWidget );
325
326         p_intf->pf_request_window  = ::DoRequest;
327         p_intf->pf_release_window  = ::DoRelease;
328         p_intf->pf_control_window  = ::DoControl;
329     }
330
331     /* Finish the sizing */
332     setMinimumSize( PREF_W, addSize.height() );
333 }
334
335 /**********************************************************************
336  * Handling of sizing of the components
337  **********************************************************************/
338 void MainInterface::calculateInterfaceSize()
339 {
340     int width = 0, height = 0;
341     if( VISIBLE( bgWidget ) )
342     {
343         width = bgWidget->widgetSize.width();
344         height = bgWidget->widgetSize.height();
345         assert( !(playlistWidget && playlistWidget->isVisible() ) );
346     }
347     else if( VISIBLE( playlistWidget ) )
348     {
349         width = playlistWidget->widgetSize.width();
350         height = playlistWidget->widgetSize.height();
351     }
352     else if( videoIsActive )
353     {
354         width =  videoWidget->widgetSize.width() ;
355         height = videoWidget->widgetSize.height();
356     }
357     else
358     {
359         width = PREF_W - addSize.width();
360         height = PREF_H - addSize.height();
361     }
362     if( VISIBLE( visualSelector ) )
363         height += visualSelector->height();
364 /*    if( VISIBLE( advControls) )
365     {
366         height += advControls->sizeHint().height();
367     }*/
368     mainSize = QSize( width + addSize.width(), height + addSize.height() );
369 }
370
371 void MainInterface::resizeEvent( QResizeEvent *e )
372 {
373     videoWidget->widgetSize.setWidth(  e->size().width() - addSize.width() );
374     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
375     {
376         SET_WH( videoWidget, e->size().width() - addSize.width(),
377                              e->size().height()  - addSize.height() );
378         videoWidget->updateGeometry();
379     }
380     if( VISIBLE( playlistWidget ) )
381     {
382         SET_WH( playlistWidget , e->size().width() - addSize.width(),
383                                  e->size().height() - addSize.height() );
384         playlistWidget->updateGeometry();
385     }
386 }
387
388 /****************************************************************************
389  * Small right-click menus
390  ****************************************************************************/
391 void MainInterface::showSpeedMenu( QPoint pos )
392 {
393     QMenu menu( this );
394     menu.addAction( "Not Implemented Yet" );
395     menu.exec( QCursor::pos() );
396 }
397
398 void MainInterface::showTimeMenu( QPoint pos )
399 {
400     QMenu menu( this );
401     menu.addAction( "Not Implemented Yet" );
402     menu.exec( QCursor::pos() );
403 }
404
405 /****************************************************************************
406  * Video Handling
407  ****************************************************************************/
408 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
409                                    int *pi_y, unsigned int *pi_width,
410                                    unsigned int *pi_height )
411 {
412     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
413     if( ret )
414     {
415         videoIsActive = true;
416         if( VISIBLE( playlistWidget ) )
417         {
418             embeddedPlaylistWasActive = true;
419 //            playlistWidget->hide();
420         }
421         bool bgWasVisible = false;
422         if( VISIBLE( bgWidget) )
423         {
424             bgWasVisible = true;
425             bgWidget->hide();
426         }
427         if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
428         {
429             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
430         }
431         else /* Background widget available, use its size */
432         {
433             /* Ok, our visualizations are bad, so don't do this for the moment
434              * use the requested size anyway */
435             // videoWidget->widgetSize = bgWidget->widgeTSize;
436             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
437         }
438 //        videoWidget->updateGeometry(); /// FIXME: Needed ?
439         need_components_update = true;
440     }
441     return ret;
442 }
443
444 void MainInterface::releaseVideo( void *p_win )
445 {
446     videoWidget->release( p_win );
447     videoWidget->widgetSize = QSize( 0, 0 );
448     videoWidget->resize( videoWidget->widgetSize );
449
450     if( embeddedPlaylistWasActive )
451         playlistWidget->show();
452     else if( bgWidget )
453         bgWidget->show();
454
455     videoIsActive = false;
456     need_components_update = true;
457 }
458
459 class SetVideoOnTopQtEvent : public QEvent
460 {
461 public:
462     SetVideoOnTopQtEvent( bool _onTop ) :
463       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
464     {
465     }
466
467     bool OnTop() const
468     {
469         return onTop;
470     }
471
472 private:
473     bool onTop;
474 };
475
476 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
477 {
478     int i_ret = VLC_EGENERIC;
479     switch( i_query )
480     {
481         case VOUT_GET_SIZE:
482         {
483             unsigned int *pi_width  = va_arg( args, unsigned int * );
484             unsigned int *pi_height = va_arg( args, unsigned int * );
485             *pi_width = videoWidget->widgetSize.width();
486             *pi_height = videoWidget->widgetSize.height();
487             i_ret = VLC_SUCCESS;
488             break;
489         }
490         case VOUT_SET_SIZE:
491         {
492             unsigned int i_width  = va_arg( args, unsigned int );
493             unsigned int i_height = va_arg( args, unsigned int );
494             videoWidget->widgetSize = QSize( i_width, i_height );
495             // videoWidget->updateGeometry();
496             need_components_update = true;
497             i_ret = VLC_SUCCESS;
498             break;
499         }
500         case VOUT_SET_STAY_ON_TOP:
501         {
502             int i_arg = va_arg( args, int );
503             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
504             i_ret = VLC_SUCCESS;
505             break;
506         }
507         default:
508             msg_Warn( p_intf, "unsupported control query" );
509             break;
510     }
511     return i_ret;
512 }
513
514 /*****************************************************************************
515  * Playlist, Visualisation and Menus handling
516  *****************************************************************************/
517 /**
518  * Toggle the playlist widget or dialog
519  **/
520 void MainInterface::togglePlaylist()
521 {
522     // Toggle the playlist dialog if not embedded and return
523     if( !playlistEmbeddedFlag )
524     {
525         if( playlistWidget )
526         {
527             /// \todo Destroy it
528         }
529         THEDP->playlistDialog();
530         return;
531     }
532
533     // Create the playlist Widget and destroy the existing dialog
534     if( !playlistWidget )
535     {
536         PlaylistDialog::killInstance();
537         playlistWidget = new PlaylistWidget( p_intf );
538         mainLayout->insertWidget( 0, playlistWidget );
539         playlistWidget->widgetSize = settings->value( "playlistSize",
540                                                QSize( 650, 310 ) ).toSize();
541         playlistWidget->hide();
542         if(bgWidget)
543         CONNECT( playlistWidget, artSet( QString ), bgWidget, setArt(QString) );
544     }
545
546     // And toggle visibility
547     if( VISIBLE( playlistWidget ) )
548     {
549         playlistWidget->hide();
550         if( bgWidget ) bgWidget->show();
551         if( videoIsActive )
552         {
553             videoWidget->widgetSize = savedVideoSize;
554             videoWidget->resize( videoWidget->widgetSize );
555             videoWidget->updateGeometry();
556             if( bgWidget ) bgWidget->hide();
557         }
558     }
559     else
560     {
561         playlistWidget->show();
562         if( videoIsActive )
563         {
564             savedVideoSize = videoWidget->widgetSize;
565             videoWidget->widgetSize.setHeight( 0 );
566             videoWidget->resize( videoWidget->widgetSize );
567             videoWidget->updateGeometry();
568         }
569         if( VISIBLE( bgWidget ) ) bgWidget->hide();
570     }
571
572     doComponentsUpdate();
573 }
574
575 void MainInterface::undockPlaylist()
576 {
577     if( playlistWidget )
578     {
579         playlistWidget->hide();
580         playlistWidget->deleteLater();
581         mainLayout->removeWidget( playlistWidget );
582         playlistWidget = NULL;
583         playlistEmbeddedFlag = false;
584
585         menuBar()->clear();
586         QVLCMenu::createMenuBar( this, p_intf, false, isAdvancedVisible(),
587                                  visualSelectorEnabled);
588
589         if( videoIsActive )
590         {
591             videoWidget->widgetSize = savedVideoSize;
592             videoWidget->resize( videoWidget->widgetSize );
593             videoWidget->updateGeometry();
594         }
595
596         doComponentsUpdate();
597         THEDP->playlistDialog();
598     }
599 }
600
601 #if 0
602 void MainInterface::visual()
603 {
604     if( !VISIBLE( visualSelector) )
605     {
606         visualSelector->show();
607         if( !THEMIM->getIM()->hasVideo() )
608         {
609             /* Show the background widget */
610         }
611         visualSelectorEnabled = true;
612     }
613     else
614     {
615         /* Stop any currently running visualization */
616         visualSelector->hide();
617         visualSelectorEnabled = false;
618     }
619     doComponentsUpdate();
620 }
621 #endif
622
623 void MainInterface::toggleMenus()
624 {
625     if( menuBar()->isVisible() ) menuBar()->hide();
626         else menuBar()->show();
627     msg_Dbg( p_intf, "I was there: \\_o<~~ coin coin" );
628 }
629
630 /* Video widget cannot do this synchronously as it runs in another thread */
631 /* Well, could it, actually ? Probably dangerous ... */
632 void MainInterface::doComponentsUpdate()
633 {
634     calculateInterfaceSize();
635     resize( mainSize );
636 }
637
638 void MainInterface::toggleAdvanced()
639 {
640     controls->toggleAdvanced();
641 }
642
643 bool MainInterface::isAdvancedVisible()
644 {
645     return controls->b_advancedVisible;
646 }
647
648 /************************************************************************
649  * Other stuff
650  ************************************************************************/
651 void MainInterface::setDisplay( float pos, int time, int length )
652 {
653     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
654     secstotimestr( psz_length, length );
655     secstotimestr( psz_time, time );
656     QString title;
657     title.sprintf( "%s/%s", psz_time, psz_length );
658     timeLabel->setText( " "+title+" " );
659 }
660
661 void MainInterface::setName( QString name )
662 {
663     input_name = name;
664     nameLabel->setText( " " + name+" " );
665 }
666
667 void MainInterface::setStatus( int status )
668 {
669     controls->setStatus( status );
670     if( sysTray )
671         updateSystrayMenu( status );
672 }
673
674 void MainInterface::updateOnTimer()
675 {
676     /* \todo Make this event-driven */
677     if( intf_ShouldDie( p_intf ) )
678     {
679         QApplication::closeAllWindows();
680         QApplication::quit();
681     }
682     if( need_components_update )
683     {
684         doComponentsUpdate();
685         need_components_update = false;
686     }
687
688     controls->updateOnTimer();
689 }
690
691 /*****************************************************************************
692  * Systray Icon and Systray Menu
693  *****************************************************************************/
694
695 /**
696  * Create a SystemTray icon and a menu that would go with it.
697  * Connects to a click handler on the icon.
698  **/
699 void MainInterface::createSystray()
700 {
701     QIcon iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
702     sysTray = new QSystemTrayIcon( iconVLC, this );
703     sysTray->setToolTip( qtr( "VLC media player" ));
704
705     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
706     systrayMenu->setIcon( iconVLC );
707
708     QVLCMenu::updateSystrayMenu( this, p_intf, true );
709     sysTray->show();
710
711     CONNECT( sysTray, activated(  QSystemTrayIcon::ActivationReason ),
712             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
713 }
714
715 /**
716  * Update the menu of the Systray Icon.
717  * May be unneedded, since it just calls QVLCMenu::update
718  * FIXME !!!
719  **/
720 void MainInterface::updateSystrayMenu( int status )
721 {
722     QVLCMenu::updateSystrayMenu( this, p_intf ) ;
723 }
724
725 /**
726  * Updates the Systray Icon's menu and toggle the main interface
727  */
728 void MainInterface::toggleUpdateSystrayMenu()
729 {
730     if( isHidden() )
731     {
732         show();
733         activateWindow();
734     }
735     else
736     {
737 #ifdef WIN32
738         /* check if any visible window is above vlc in the z-order,
739          * but ignore the ones always on top */
740         WINDOWINFO wi;
741         HWND hwnd;
742         wi.cbSize = sizeof( WINDOWINFO );
743         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
744                 hwnd && !IsWindowVisible( hwnd );
745                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
746         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
747                 (wi.dwExStyle&WS_EX_TOPMOST) )
748 #else
749         if( isActiveWindow() )
750 #endif
751         {
752             hide();
753         }
754         else
755         {
756             activateWindow();
757         }
758     }
759     QVLCMenu::updateSystrayMenu( this, p_intf );
760 }
761
762 void MainInterface::handleSystrayClick(
763                                     QSystemTrayIcon::ActivationReason reason )
764 {
765     switch( reason )
766     {
767         case QSystemTrayIcon::Trigger:
768             toggleUpdateSystrayMenu();
769             break;
770         case QSystemTrayIcon::MiddleClick:
771             sysTray->showMessage( qtr( "VLC media player" ),
772                     qtr( "Control menu for the player" ),
773                     QSystemTrayIcon::Information, 4000 );
774             break;
775     }
776 }
777
778 /**
779  * Updates the name of the systray Icon tooltip.
780  * Doesn't check if the systray exists, check before you call it.
781  * FIXME !!! Fusion with next function ?
782  **/
783 void MainInterface::updateSystrayTooltipName( QString name )
784 {
785     if( name.isEmpty() )
786     {
787         sysTray->setToolTip( qtr( "VLC media player" ) );
788     }
789     else
790     {
791         sysTray->setToolTip( name );
792         sysTray->showMessage( qtr( "VLC media player" ), name,
793                 QSystemTrayIcon::NoIcon, 4000 );
794     }
795 }
796
797 /**
798  * Updates the status of the systray Icon tooltip.
799  * Doesn't check if the systray exists, check before you call it.
800  **/
801 void MainInterface::updateSystrayTooltipStatus( int i_status )
802 {
803     switch( i_status )
804     {
805         case  0:
806             {
807                 sysTray->setToolTip( qtr( "VLC media player" ) );
808                 break;
809             }
810         case PLAYING_S:
811             {
812                 sysTray->setToolTip( input_name );
813                 //+ " - " + qtr( "Playing" ) );
814                 break;
815             }
816         case PAUSE_S:
817             {
818                 sysTray->setToolTip( input_name + " - "
819                         + qtr( "Paused") );
820                 break;
821             }
822     }
823 }
824
825 /************************************************************************
826  * D&D Events
827  ************************************************************************/
828 void MainInterface::dropEvent(QDropEvent *event)
829 {
830      const QMimeData *mimeData = event->mimeData();
831
832      /* D&D of a subtitles file, add it on the fly */
833      if( mimeData->urls().size() == 1 )
834      {
835         if( THEMIM->getIM()->hasInput() )
836         {
837             if( input_AddSubtitles( THEMIM->getInput(),
838                                     qtu( mimeData->urls()[0].toString() ),
839                                     VLC_TRUE ) )
840             {
841                 event->acceptProposedAction();
842                 return;
843             }
844         }
845      }
846      bool first = true;
847      foreach( QUrl url, mimeData->urls() ) {
848         QString s = url.toString();
849         if( s.length() > 0 ) {
850             playlist_Add( THEPL, qtu(s), NULL,
851                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
852                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
853             first = false;
854         }
855      }
856      event->acceptProposedAction();
857 }
858 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
859 {
860      event->acceptProposedAction();
861 }
862 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
863 {
864      event->acceptProposedAction();
865 }
866 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
867 {
868      event->accept();
869 }
870
871 /************************************************************************
872  * Events stuff
873  ************************************************************************/
874 void MainInterface::customEvent( QEvent *event )
875 {
876     if( event->type() == PLDockEvent_Type )
877     {
878         PlaylistDialog::killInstance();
879         playlistEmbeddedFlag = true;
880         menuBar()->clear();
881         QVLCMenu::createMenuBar(this, p_intf, true, isAdvancedVisible(),
882                                 visualSelectorEnabled);
883         togglePlaylist();
884     }
885     else if ( event->type() == SetVideoOnTopEvent_Type )
886     {
887         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
888         if( p_event->OnTop() )
889             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
890         else
891             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
892         show(); /* necessary to apply window flags?? */
893     }
894 }
895
896 void MainInterface::keyPressEvent( QKeyEvent *e )
897 {
898     int i_vlck = qtEventToVLCKey( e );
899     if( i_vlck >= 0 )
900     {
901         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
902         e->accept();
903     }
904     else
905         e->ignore();
906 }
907
908 void MainInterface::wheelEvent( QWheelEvent *e )
909 {
910     int i_vlckey = qtWheelEventToVLCKey( e );
911     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
912     e->accept();
913 }
914
915 void MainInterface::closeEvent( QCloseEvent *e )
916 {
917     hide();
918     vlc_object_kill( p_intf );
919 }
920
921 /*****************************************************************************
922  * Callbacks
923  *****************************************************************************/
924 static int InteractCallback( vlc_object_t *p_this,
925                              const char *psz_var, vlc_value_t old_val,
926                              vlc_value_t new_val, void *param )
927 {
928     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
929     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
930     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
931     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
932     return VLC_SUCCESS;
933 }
934
935 /*****************************************************************************
936  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
937  *  We don't show the menu directly here because we don't want the
938  *  caller to block for a too long time.
939  *****************************************************************************/
940 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
941                         vlc_value_t old_val, vlc_value_t new_val, void *param )
942 {
943     intf_thread_t *p_intf = (intf_thread_t *)param;
944
945     if( p_intf->pf_show_dialog )
946     {
947         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
948                                 new_val.b_bool, 0 );
949     }
950
951     return VLC_SUCCESS;
952 }
953
954 /*****************************************************************************
955  * IntfShowCB: callback triggered by the intf-show playlist variable.
956  *****************************************************************************/
957 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
958                        vlc_value_t old_val, vlc_value_t new_val, void *param )
959 {
960     intf_thread_t *p_intf = (intf_thread_t *)param;
961     //p_intf->p_sys->b_intf_show = VLC_TRUE;
962
963     return VLC_SUCCESS;
964 }