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