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