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