]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/interface_widgets.cpp
Fix leakage in FS controller (part 2)
[vlc] / modules / gui / qt4 / components / interface_widgets.cpp
1 /*****************************************************************************
2  * interface_widgets.cpp : Custom widgets for the main interface
3  ****************************************************************************
4  * Copyright ( C ) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Rafaël Carré <funman@videolanorg>
10  *          Ilkka Ollakka <ileoo@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * ( at your option ) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_vout.h>
32
33 #include "dialogs_provider.hpp"
34 #include "components/interface_widgets.hpp"
35 #include "main_interface.hpp"
36 #include "input_manager.hpp"
37 #include "menus.hpp"
38 #include "util/input_slider.hpp"
39 #include "util/customwidgets.hpp"
40
41 #include <QLabel>
42 #include <QSpacerItem>
43 #include <QCursor>
44 #include <QPushButton>
45 #include <QToolButton>
46 #include <QHBoxLayout>
47 #include <QMenu>
48 #include <QPalette>
49 #include <QResizeEvent>
50 #include <QDate>
51
52 #ifdef Q_WS_X11
53 # include <X11/Xlib.h>
54 # include <qx11info_x11.h>
55 #endif
56
57 #include <math.h>
58
59 /**********************************************************************
60  * Video Widget. A simple frame on which video is drawn
61  * This class handles resize issues
62  **********************************************************************/
63
64 VideoWidget::VideoWidget( intf_thread_t *_p_i ) : QFrame( NULL ), p_intf( _p_i )
65 {
66     /* Init */
67     i_vout = 0;
68     hide(); setMinimumSize( 16, 16 );
69     videoSize.rwidth() = -1;
70     videoSize.rheight() = -1;
71     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
72
73     /* Black background is more coherent for a Video Widget IMVHO */
74     QPalette plt =  palette();
75     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
76     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
77     setPalette( plt );
78     setAttribute( Qt::WA_PaintOnScreen, true );
79
80     /* The core can ask through a callback to show the video. */
81 #if HAS_QT43
82     connect( this, SIGNAL(askVideoWidgetToShow( unsigned int, unsigned int)),
83              this, SLOT(SetSizing(unsigned int, unsigned int )),
84              Qt::BlockingQueuedConnection );
85 #else
86 #error This is broken. Fix it with a QEventLoop with a processEvents () 
87     connect( this, SIGNAL(askVideoWidgetToShow( unsigned int, unsigned int)),
88              this, SLOT(SetSizing(unsigned int, unsigned int )) );
89 #endif
90 }
91
92 void VideoWidget::paintEvent(QPaintEvent *ev)
93 {
94     QFrame::paintEvent(ev);
95 #ifdef Q_WS_X11
96     XFlush( QX11Info::display() );
97 #endif
98 }
99
100 VideoWidget::~VideoWidget()
101 {
102     vout_thread_t *p_vout = i_vout
103         ? (vout_thread_t *)vlc_object_get( i_vout ) : NULL;
104
105     if( p_vout )
106     {
107         if( !p_intf->psz_switch_intf )
108         {
109             if( vout_Control( p_vout, VOUT_CLOSE ) != VLC_SUCCESS )
110                 vout_Control( p_vout, VOUT_REPARENT );
111         }
112         else
113         {
114             if( vout_Control( p_vout, VOUT_REPARENT ) != VLC_SUCCESS )
115                 vout_Control( p_vout, VOUT_CLOSE );
116         }
117         vlc_object_release( p_vout );
118     }
119 }
120
121 /**
122  * Request the video to avoid the conflicts
123  **/
124 void *VideoWidget::request( vout_thread_t *p_nvout, int *pi_x, int *pi_y,
125                             unsigned int *pi_width, unsigned int *pi_height )
126 {
127     msg_Dbg( p_intf, "Video was requested %i, %i", *pi_x, *pi_y );
128     emit askVideoWidgetToShow( *pi_width, *pi_height );
129     if( i_vout )
130     {
131         msg_Dbg( p_intf, "embedded video already in use" );
132         return NULL;
133     }
134     i_vout = p_nvout->i_object_id;
135     msg_Dbg( p_intf, "embedded video ready (handle %p)", winId() );
136     return ( void* )winId();
137 }
138
139 /* Set the Widget to the correct Size */
140 /* Function has to be called by the parent
141    Parent has to care about resizing himself*/
142 void VideoWidget::SetSizing( unsigned int w, unsigned int h )
143 {
144     msg_Dbg( p_intf, "Video is resizing to: %i %i", w, h );
145     videoSize.rwidth() = w;
146     videoSize.rheight() = h;
147     if( isHidden() ) show();
148     updateGeometry(); // Needed for deinterlace
149 }
150
151 void VideoWidget::release( void *p_win )
152 {
153     msg_Dbg( p_intf, "Video is not needed anymore" );
154     i_vout = 0;
155     videoSize.rwidth() = 0;
156     videoSize.rheight() = 0;
157     hide();
158     updateGeometry(); // Needed for deinterlace
159 }
160
161 QSize VideoWidget::sizeHint() const
162 {
163     return videoSize;
164 }
165
166 /**********************************************************************
167  * Background Widget. Show a simple image background. Currently,
168  * it's album art if present or cone.
169  **********************************************************************/
170 #define ICON_SIZE 128
171 #define MAX_BG_SIZE 400
172 #define MIN_BG_SIZE 64
173
174 BackgroundWidget::BackgroundWidget( intf_thread_t *_p_i )
175                  :QWidget( NULL ), p_intf( _p_i )
176 {
177     /* We should use that one to take the more size it can */
178     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
179
180     /* A dark background */
181     setAutoFillBackground( true );
182     plt =  palette();
183     plt.setColor( QPalette::Active, QPalette::Window , Qt::black );
184     plt.setColor( QPalette::Inactive, QPalette::Window , Qt::black );
185     setPalette( plt );
186
187     /* A cone in the middle */
188     label = new QLabel;
189     label->setMargin( 5 );
190     label->setMaximumHeight( MAX_BG_SIZE );
191     label->setMaximumWidth( MAX_BG_SIZE );
192     label->setMinimumHeight( MIN_BG_SIZE );
193     label->setMinimumWidth( MIN_BG_SIZE );
194     if( QDate::currentDate().dayOfYear() >= 354 )
195         label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
196     else
197         label->setPixmap( QPixmap( ":/vlc128.png" ) );
198
199     QGridLayout *backgroundLayout = new QGridLayout( this );
200     backgroundLayout->addWidget( label, 0, 1 );
201     backgroundLayout->setColumnStretch( 0, 1 );
202     backgroundLayout->setColumnStretch( 2, 1 );
203
204     CONNECT( THEMIM->getIM(), artChanged( QString ), this, updateArt( QString ) );
205 }
206
207 BackgroundWidget::~BackgroundWidget()
208 {}
209
210 void BackgroundWidget::resizeEvent( QResizeEvent * event )
211 {
212     if( event->size().height() <= MIN_BG_SIZE )
213         label->hide();
214     else
215         label->show();
216 }
217
218 void BackgroundWidget::updateArt( QString url )
219 {
220     if( url.isEmpty() )
221     {
222         if( QDate::currentDate().dayOfYear() >= 354 )
223             label->setPixmap( QPixmap( ":/vlc128-christmas.png" ) );
224         else
225             label->setPixmap( QPixmap( ":/vlc128.png" ) );
226         return;
227     }
228     else
229     {
230         label->setPixmap( QPixmap( url ) );
231     }
232 }
233
234 void BackgroundWidget::contextMenuEvent( QContextMenuEvent *event )
235 {
236     QVLCMenu::PopupMenu( p_intf, true );
237 }
238
239 /**********************************************************************
240  * Visualization selector panel
241  **********************************************************************/
242 VisualSelector::VisualSelector( intf_thread_t *_p_i ) :
243                                 QFrame( NULL ), p_intf( _p_i )
244 {
245     QHBoxLayout *layout = new QHBoxLayout( this );
246     layout->setMargin( 0 );
247     QPushButton *prevButton = new QPushButton( "Prev" );
248     QPushButton *nextButton = new QPushButton( "Next" );
249     layout->addWidget( prevButton );
250     layout->addWidget( nextButton );
251
252     layout->addStretch( 10 );
253     layout->addWidget( new QLabel( qtr( "Current visualization" ) ) );
254
255     current = new QLabel( qtr( "None" ) );
256     layout->addWidget( current );
257
258     BUTTONACT( prevButton, prev() );
259     BUTTONACT( nextButton, next() );
260
261     setLayout( layout );
262     setMaximumHeight( 35 );
263 }
264
265 VisualSelector::~VisualSelector()
266 {}
267
268 void VisualSelector::prev()
269 {
270     char *psz_new = aout_VisualPrev( p_intf );
271     if( psz_new )
272     {
273         current->setText( qfu( psz_new ) );
274         free( psz_new );
275     }
276 }
277
278 void VisualSelector::next()
279 {
280     char *psz_new = aout_VisualNext( p_intf );
281     if( psz_new )
282     {
283         current->setText( qfu( psz_new ) );
284         free( psz_new );
285     }
286 }
287
288 /**********************************************************************
289  * TEH controls
290  **********************************************************************/
291
292 #define setupSmallButton( aButton ){  \
293     aButton->setMaximumSize( QSize( 26, 26 ) ); \
294     aButton->setMinimumSize( QSize( 26, 26 ) ); \
295     aButton->setIconSize( QSize( 20, 20 ) ); }
296
297 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i ) :
298                                            QFrame( NULL ), p_intf( _p_i )
299 {
300     QHBoxLayout *advLayout = new QHBoxLayout( this );
301     advLayout->setMargin( 0 );
302     advLayout->setSpacing( 0 );
303     advLayout->setAlignment( Qt::AlignBottom );
304
305     /* A to B Button */
306     ABButton = new QPushButton( "AB" );
307     setupSmallButton( ABButton );
308     advLayout->addWidget( ABButton );
309     BUTTON_SET_ACT( ABButton, "AB", qtr( "A to B" ), fromAtoB() );
310     timeA = timeB = 0;
311     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
312              this, AtoBLoop( float, int, int ) );
313 #if 0
314     frameButton = new QPushButton( "Fr" );
315     frameButton->setMaximumSize( QSize( 26, 26 ) );
316     frameButton->setIconSize( QSize( 20, 20 ) );
317     advLayout->addWidget( frameButton );
318     BUTTON_SET_ACT( frameButton, "Fr", qtr( "Frame by frame" ), frame() );
319 #endif
320
321     recordButton = new QPushButton( "R" );
322     setupSmallButton( recordButton );
323     advLayout->addWidget( recordButton );
324     BUTTON_SET_ACT_I( recordButton, "", record_16px.png,
325             qtr( "Record" ), record() );
326
327     /* Snapshot Button */
328     snapshotButton = new QPushButton( "S" );
329     setupSmallButton( snapshotButton );
330     advLayout->addWidget( snapshotButton );
331     BUTTON_SET_ACT( snapshotButton, "S", qtr( "Take a snapshot" ), snapshot() );
332 }
333
334 AdvControlsWidget::~AdvControlsWidget()
335 {}
336
337 void AdvControlsWidget::enableInput( bool enable )
338 {
339     ABButton->setEnabled( enable );
340     recordButton->setEnabled( enable );
341 }
342
343 void AdvControlsWidget::enableVideo( bool enable )
344 {
345     snapshotButton->setEnabled( enable );
346 #if 0
347     frameButton->setEnabled( enable );
348 #endif
349 }
350
351 void AdvControlsWidget::snapshot()
352 {
353     vout_thread_t *p_vout =
354         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
355     if( p_vout ) vout_Control( p_vout, VOUT_SNAPSHOT );
356 }
357
358 /* Function called when the button is clicked() */
359 void AdvControlsWidget::fromAtoB()
360 {
361     if( !timeA )
362     {
363         timeA = var_GetTime( THEMIM->getInput(), "time"  );
364         ABButton->setText( "A->..." );
365         return;
366     }
367     if( !timeB )
368     {
369         timeB = var_GetTime( THEMIM->getInput(), "time"  );
370         var_SetTime( THEMIM->getInput(), "time" , timeA );
371         ABButton->setText( "A<=>B" );
372         return;
373     }
374     timeA = 0;
375     timeB = 0;
376     ABButton->setText( "AB" );
377 }
378
379 /* Function called regularly when in an AtoB loop */
380 void AdvControlsWidget::AtoBLoop( float f_pos, int i_time, int i_length )
381 {
382     if( timeB )
383     {
384         if( i_time >= (int)(timeB/1000000) )
385             var_SetTime( THEMIM->getInput(), "time" , timeA );
386     }
387 }
388
389 /* FIXME Record function */
390 void AdvControlsWidget::record(){}
391
392 #if 0
393 //FIXME Frame by frame function
394 void AdvControlsWidget::frame(){}
395 #endif
396
397 /*****************************
398  * DA Control Widget !
399  *****************************/
400 ControlsWidget::ControlsWidget( intf_thread_t *_p_i,
401                                 MainInterface *_p_mi,
402                                 bool b_advControls,
403                                 bool b_shiny,
404                                 bool b_fsCreation) :
405                                 QFrame( _p_mi ), p_intf( _p_i )
406 {
407     setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Maximum );
408
409     /** The main Slider **/
410     slider = new InputSlider( Qt::Horizontal, NULL );
411     /* Update the position when the IM has changed */
412     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
413              slider, setPosition( float, int, int ) );
414     /* And update the IM, when the position has changed */
415     CONNECT( slider, sliderDragged( float ),
416              THEMIM->getIM(), sliderUpdate( float ) );
417
418     /** Slower and faster Buttons **/
419     slowerButton = new QToolButton;
420     slowerButton->setAutoRaise( true );
421     slowerButton->setMaximumSize( QSize( 26, 20 ) );
422
423     BUTTON_SET_ACT( slowerButton, "-", qtr( "Slower" ), slower() );
424
425     fasterButton = new QToolButton;
426     fasterButton->setAutoRaise( true );
427     fasterButton->setMaximumSize( QSize( 26, 20 ) );
428
429     BUTTON_SET_ACT( fasterButton, "+", qtr( "Faster" ), faster() );
430
431     /* advanced Controls handling */
432     b_advancedVisible = b_advControls;
433
434     advControls = new AdvControlsWidget( p_intf );
435     if( !b_advancedVisible ) advControls->hide();
436
437     /** Disc and Menus handling */
438     discFrame = new QWidget( this );
439
440     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
441     discLayout->setSpacing( 0 );
442     discLayout->setMargin( 0 );
443
444     prevSectionButton = new QPushButton( discFrame );
445     setupSmallButton( prevSectionButton );
446     discLayout->addWidget( prevSectionButton );
447
448     menuButton = new QPushButton( discFrame );
449     setupSmallButton( menuButton );
450     discLayout->addWidget( menuButton );
451
452     nextSectionButton = new QPushButton( discFrame );
453     setupSmallButton( nextSectionButton );
454     discLayout->addWidget( nextSectionButton );
455
456     BUTTON_SET_IMG( prevSectionButton, "", previous.png, "" );
457     BUTTON_SET_IMG( nextSectionButton, "", next.png, "" );
458     BUTTON_SET_IMG( menuButton, "", previous.png, qtr( "Menu" ) );
459
460     discFrame->hide();
461
462     /* Change the navigation button display when the IM navigation changes */
463     CONNECT( THEMIM->getIM(), navigationChanged( int ),
464              this, setNavigation( int ) );
465     /* Changes the IM navigation when triggered on the nav buttons */
466     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
467              sectionPrev() );
468     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
469              sectionNext() );
470     CONNECT( menuButton, clicked(), THEMIM->getIM(),
471              sectionMenu() );
472
473     /**
474      * Telextext QFrame
475      * TODO: Merge with upper menu in a StackLayout
476      **/
477     telexFrame = new QWidget( this );
478     QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
479     telexLayout->setSpacing( 0 );
480     telexLayout->setMargin( 0 );
481
482     telexOn = new QPushButton;
483     setupSmallButton( telexOn );
484     telexLayout->addWidget( telexOn );
485
486     telexTransparent = new QPushButton;
487     setupSmallButton( telexTransparent );
488     telexLayout->addWidget( telexTransparent );
489     b_telexTransparent = false;
490
491     telexPage = new QSpinBox;
492     telexPage->setRange( 0, 999 );
493     telexPage->setValue( 100 );
494     telexPage->setAccelerated( true );
495     telexPage->setWrapping( true );
496     telexPage->setAlignment( Qt::AlignRight );
497     telexPage->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
498     telexLayout->addWidget( telexPage );
499
500     telexFrame->hide(); /* default hidden */
501
502     CONNECT( telexPage, valueChanged( int ), THEMIM->getIM(),
503              telexGotoPage( int ) );
504     CONNECT( THEMIM->getIM(), setNewTelexPage( int ),
505               telexPage, setValue( int ) );
506
507     BUTTON_SET_IMG( telexOn, "", tv.png, qtr( "Teletext on" ) );
508
509     CONNECT( telexOn, clicked(), THEMIM->getIM(),
510              telexToggleButtons() );
511     CONNECT( telexOn, clicked( bool ), THEMIM->getIM(),
512              telexToggle( bool ) );
513     CONNECT( THEMIM->getIM(), toggleTelexButtons(),
514               this, toggleTeletext() );
515     b_telexEnabled = false;
516     telexTransparent->setEnabled( false );
517     telexPage->setEnabled( false );
518
519     BUTTON_SET_IMG( telexTransparent, "", tvtelx.png, qtr( "Teletext" ) );
520     CONNECT( telexTransparent, clicked( bool ),
521              THEMIM->getIM(), telexSetTransparency() );
522     CONNECT( THEMIM->getIM(), toggleTelexTransparency(),
523               this, toggleTeletextTransparency() );
524     CONNECT( THEMIM->getIM(), teletextEnabled( bool ),
525              this, enableTeletext( bool ) );
526
527     /** Play Buttons **/
528     QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
529     sizePolicy.setHorizontalStretch( 0 );
530     sizePolicy.setVerticalStretch( 0 );
531
532     /* Play */
533     playButton = new QPushButton;
534     playButton->setSizePolicy( sizePolicy );
535     playButton->setMaximumSize( QSize( 36, 36 ) );
536     playButton->setMinimumSize( QSize( 36, 36 ) );
537     playButton->setIconSize( QSize( 30, 30 ) );
538
539
540     /** Prev + Stop + Next Block **/
541     controlButLayout = new QHBoxLayout;
542     controlButLayout->setSpacing( 0 ); /* Don't remove that, will be useful */
543
544     /* Prev */
545     QPushButton *prevButton = new QPushButton;
546     prevButton->setSizePolicy( sizePolicy );
547     setupSmallButton( prevButton );
548
549     controlButLayout->addWidget( prevButton );
550
551     /* Stop */
552     QPushButton *stopButton = new QPushButton;
553     stopButton->setSizePolicy( sizePolicy );
554     setupSmallButton( stopButton );
555
556     controlButLayout->addWidget( stopButton );
557
558     /* next */
559     QPushButton *nextButton = new QPushButton;
560     nextButton->setSizePolicy( sizePolicy );
561     setupSmallButton( nextButton );
562
563     controlButLayout->addWidget( nextButton );
564
565     /* Add this block to the main layout */
566
567     BUTTON_SET_ACT_I( playButton, "", play.png, qtr( "Play" ), play() );
568     BUTTON_SET_ACT_I( prevButton, "" , previous.png,
569                       qtr( "Previous" ), prev() );
570     BUTTON_SET_ACT_I( nextButton, "", next.png, qtr( "Next" ), next() );
571     BUTTON_SET_ACT_I( stopButton, "", stop.png, qtr( "Stop" ), stop() );
572
573     /*
574      * Other first Line buttons
575      */
576     /** Fullscreen/Visualisation **/
577     fullscreenButton = new QPushButton( "F" );
578     BUTTON_SET_ACT( fullscreenButton, "F", qtr( "Fullscreen" ), fullscreen() );
579     setupSmallButton( fullscreenButton );
580
581     if( !b_fsCreation )
582     {
583         /** Playlist Button **/
584         playlistButton = new QPushButton;
585         setupSmallButton( playlistButton );
586         BUTTON_SET_IMG( playlistButton, "" , playlist.png, qtr( "Show playlist" ) );
587         CONNECT( playlistButton, clicked(), _p_mi, togglePlaylist() );
588
589         /** extended Settings **/
590         extSettingsButton = new QPushButton;
591         BUTTON_SET_ACT( extSettingsButton, "Ex", qtr( "Extended settings" ),
592                 extSettings() );
593         setupSmallButton( extSettingsButton );
594     }
595
596     /* Volume */
597     hVolLabel = new VolumeClickHandler( p_intf, this );
598
599     volMuteLabel = new QLabel;
600     volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-medium.png" ) );
601     volMuteLabel->setToolTip( qtr( "Mute" ) );
602     volMuteLabel->installEventFilter( hVolLabel );
603
604     if( b_shiny )
605     {
606         volumeSlider = new SoundSlider( this,
607             config_GetInt( p_intf, "volume-step" ),
608             config_GetInt( p_intf, "qt-volume-complete" ),
609             config_GetPsz( p_intf, "qt-slider-colours" ) );
610     }
611     else
612     {
613         volumeSlider = new QSlider( this );
614         volumeSlider->setOrientation( Qt::Horizontal );
615     }
616     volumeSlider->setMaximumSize( QSize( 200, 40 ) );
617     volumeSlider->setMinimumSize( QSize( 106, 30 ) );
618     volumeSlider->setFocusPolicy( Qt::NoFocus );
619
620     /* Set the volume from the config */
621     volumeSlider->setValue( ( config_GetInt( p_intf, "volume" ) ) *
622                               VOLUME_MAX / (AOUT_VOLUME_MAX/2) );
623
624     /* Force the update at build time in order to have a muted icon if needed */
625     updateVolume( volumeSlider->value() );
626
627     /* Volume control connection */
628     CONNECT( volumeSlider, valueChanged( int ), this, updateVolume( int ) );
629     CONNECT( THEMIM, volumeChanged( void ), this, updateVolume( void ) );
630
631     if( !b_fsCreation )
632     {
633         controlLayout = new QGridLayout( this );
634
635         controlLayout->setSpacing( 0 );
636         controlLayout->setLayoutMargins( 7, 5, 7, 3, 6 );
637
638         controlLayout->addWidget( slider, 0, 1, 1, 16 );
639         controlLayout->addWidget( slowerButton, 0, 0 );
640         controlLayout->addWidget( fasterButton, 0, 17 );
641
642         controlLayout->addWidget( advControls, 1, 3, 2, 4, Qt::AlignBottom );
643         controlLayout->addWidget( discFrame, 1, 10, 2, 3, Qt::AlignBottom );
644         controlLayout->addWidget( telexFrame, 1, 10, 2, 4, Qt::AlignBottom );
645
646         controlLayout->addWidget( playButton, 2, 0, 2, 2 );
647         controlLayout->setColumnMinimumWidth( 2, 20 );
648         controlLayout->setColumnStretch( 2, 0 );
649
650         controlLayout->addLayout( controlButLayout, 3, 3, 1, 3 );
651         controlLayout->setColumnMinimumWidth( 7, 20 );
652         controlLayout->setColumnStretch( 7, 0 );
653         controlLayout->setColumnStretch( 8, 0 );
654         controlLayout->setColumnStretch( 9, 0 );
655
656         controlLayout->addWidget( fullscreenButton, 3, 10, Qt::AlignBottom );
657         controlLayout->addWidget( playlistButton, 3, 11, Qt::AlignBottom );
658         controlLayout->addWidget( extSettingsButton, 3, 12, Qt::AlignBottom );
659
660         controlLayout->setColumnStretch( 13, 0 );
661         controlLayout->setColumnMinimumWidth( 13, 24 );
662         controlLayout->setColumnStretch( 14, 5 );
663
664         controlLayout->addWidget( volMuteLabel, 3, 15, Qt::AlignBottom );
665         controlLayout->addWidget( volumeSlider, 2, 16, 2 , 2, Qt::AlignBottom );
666     }
667
668     updateInput();
669 }
670
671 ControlsWidget::~ControlsWidget()
672 {}
673
674 void ControlsWidget::toggleTeletext()
675 {
676     bool b_enabled = THEMIM->teletextState();
677     if( b_telexEnabled )
678     {
679         telexTransparent->setEnabled( false );
680         telexPage->setEnabled( false );
681         b_telexEnabled = false;
682     }
683     else if( b_enabled )
684     {
685         telexTransparent->setEnabled( true );
686         telexPage->setEnabled( true );
687         b_telexEnabled = true;
688     }
689 }
690
691 void ControlsWidget::enableTeletext( bool b_enable )
692 {
693     telexFrame->setVisible( b_enable );
694     bool b_on = THEMIM->teletextState();
695
696     telexOn->setChecked( b_on );
697     telexTransparent->setEnabled( b_on );
698     telexPage->setEnabled( b_on );
699     b_telexEnabled = b_on;
700 }
701
702 void ControlsWidget::toggleTeletextTransparency()
703 {
704     if( b_telexTransparent )
705     {
706         telexTransparent->setIcon( QIcon( ":/pixmaps/tvtelx.png" ) );
707         telexTransparent->setToolTip( qtr( "Teletext" ) );
708         b_telexTransparent = false;
709     }
710     else
711     {
712         telexTransparent->setIcon( QIcon( ":/pixmaps/tvtelx-transparent.png" ) );
713         telexTransparent->setToolTip( qtr( "Transparent" ) );
714         b_telexTransparent = true;
715     }
716 }
717
718 void ControlsWidget::stop()
719 {
720     THEMIM->stop();
721 }
722
723 void ControlsWidget::play()
724 {
725     if( THEPL->current.i_size == 0 )
726     {
727         /* The playlist is empty, open a file requester */
728         THEDP->openFileDialog();
729         setStatus( 0 );
730         return;
731     }
732     THEMIM->togglePlayPause();
733 }
734
735 void ControlsWidget::prev()
736 {
737     THEMIM->prev();
738 }
739
740 void ControlsWidget::next()
741 {
742     THEMIM->next();
743 }
744
745 void ControlsWidget::setNavigation( int navigation )
746 {
747 #define HELP_PCH N_( "Previous chapter" )
748 #define HELP_NCH N_( "Next chapter" )
749
750     // 1 = chapter, 2 = title, 0 = no
751     if( navigation == 0 )
752     {
753         discFrame->hide();
754     } else if( navigation == 1 ) {
755         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
756         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
757         menuButton->show();
758         discFrame->show();
759     } else {
760         prevSectionButton->setToolTip( qfu( HELP_PCH ) );
761         nextSectionButton->setToolTip( qfu( HELP_NCH ) );
762         menuButton->hide();
763         discFrame->show();
764     }
765 }
766
767 static bool b_my_volume;
768 void ControlsWidget::updateVolume( int i_sliderVolume )
769 {
770     if( !b_my_volume )
771     {
772         int i_res = i_sliderVolume  * (AOUT_VOLUME_MAX / 2) / VOLUME_MAX;
773         aout_VolumeSet( p_intf, i_res );
774     }
775     if( i_sliderVolume == 0 )
776         volMuteLabel->setPixmap( QPixmap(":/pixmaps/volume-muted.png" ) );
777     else if( i_sliderVolume < VOLUME_MAX / 3 )
778         volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
779     else if( i_sliderVolume > (VOLUME_MAX * 2 / 3 ) )
780         volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-high.png" ) );
781     else volMuteLabel->setPixmap( QPixmap( ":/pixmaps/volume-medium.png" ) );
782 }
783
784 void ControlsWidget::updateVolume()
785 {
786     /* Audio part */
787     audio_volume_t i_volume;
788     aout_VolumeGet( p_intf, &i_volume );
789     i_volume = ( i_volume *  VOLUME_MAX )/ (AOUT_VOLUME_MAX/2);
790     int i_gauge = volumeSlider->value();
791     b_my_volume = false;
792     if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
793     {
794         b_my_volume = true;
795         volumeSlider->setValue( i_volume );
796         b_my_volume = false;
797     }
798 }
799
800 void ControlsWidget::updateInput()
801 {
802     /* Activate the interface buttons according to the presence of the input */
803     enableInput( THEMIM->getIM()->hasInput() );
804     enableVideo( THEMIM->getIM()->hasVideo() && THEMIM->getIM()->hasInput() );
805 }
806
807 void ControlsWidget::setStatus( int status )
808 {
809     if( status == PLAYING_S ) /* Playing */
810     {
811         playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
812         playButton->setToolTip( qtr( "Pause" ) );
813     }
814     else
815     {
816         playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
817         playButton->setToolTip( qtr( "Play" ) );
818     }
819 }
820
821 /**
822  * TODO
823  * This functions toggle the fullscreen mode
824  * If there is no video, it should first activate Visualisations...
825  *  This has also to be fixed in enableVideo()
826  */
827 void ControlsWidget::fullscreen()
828 {
829     vout_thread_t *p_vout =
830         (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
831     if( p_vout)
832     {
833         var_SetBool( p_vout, "fullscreen", !var_GetBool( p_vout, "fullscreen" ) );
834         vlc_object_release( p_vout );
835     }
836 }
837
838 void ControlsWidget::extSettings()
839 {
840     THEDP->extendedDialog();
841 }
842
843 void ControlsWidget::slower()
844 {
845     THEMIM->getIM()->slower();
846 }
847
848 void ControlsWidget::faster()
849 {
850     THEMIM->getIM()->faster();
851 }
852
853 void ControlsWidget::enableInput( bool enable )
854 {
855     slowerButton->setEnabled( enable );
856     slider->setEnabled( enable );
857     fasterButton->setEnabled( enable );
858
859     /* Advanced Buttons too */
860     advControls->enableInput( enable );
861 }
862
863 void ControlsWidget::enableVideo( bool enable )
864 {
865     // TODO Later make the fullscreenButton toggle Visualisation and so on.
866     fullscreenButton->setEnabled( enable );
867
868     /* Advanced Buttons too */
869     advControls->enableVideo( enable );
870 }
871
872 void ControlsWidget::toggleAdvanced()
873 {
874     if( !VISIBLE( advControls ) )
875     {
876         advControls->show();
877         b_advancedVisible = true;
878     }
879     else
880     {
881         advControls->hide();
882         b_advancedVisible = false;
883     }
884     emit advancedControlsToggled( b_advancedVisible );
885 }
886
887
888 /**********************************************************************
889  * Fullscrenn control widget
890  **********************************************************************/
891 FullscreenControllerWidget::FullscreenControllerWidget( intf_thread_t *_p_i,
892         MainInterface *_p_mi, bool b_advControls, bool b_shiny )
893         : ControlsWidget( _p_i, _p_mi, b_advControls, b_shiny, true ),
894           i_mouse_last_x( -1 ), i_mouse_last_y( -1 ), b_mouse_over(false),
895           b_slow_hide_begin(false), i_slow_hide_timeout(1),
896           b_fullscreen( false ), i_hide_timeout( 1 )
897 {
898     setWindowFlags( Qt::ToolTip );
899
900     setFrameShape( QFrame::StyledPanel );
901     setFrameStyle( QFrame::Sunken );
902     setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
903
904     QGridLayout *fsLayout = new QGridLayout( this );
905     fsLayout->setLayoutMargins( 5, 1, 5, 1, 5 );
906
907     fsLayout->addWidget( slowerButton, 0, 0 );
908     slider->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum);
909     fsLayout->addWidget( slider, 0, 1, 1, 6 );
910     fsLayout->addWidget( fasterButton, 0, 7 );
911
912     fsLayout->addWidget( volMuteLabel, 1, 0);
913     fsLayout->addWidget( volumeSlider, 1, 1 );
914
915     fsLayout->addLayout( controlButLayout, 1, 2 );
916
917     fsLayout->addWidget( playButton, 1, 3 );
918
919     fsLayout->addWidget( discFrame, 1, 4 );
920
921     fsLayout->addWidget( telexFrame, 1, 5 );
922
923     fsLayout->addWidget( advControls, 1, 6, Qt::AlignVCenter );
924
925     fsLayout->addWidget( fullscreenButton, 1, 7 );
926
927     /* hiding timer */
928     p_hideTimer = new QTimer( this );
929     CONNECT( p_hideTimer, timeout(), this, hideFSC() );
930     p_hideTimer->setSingleShot( true );
931
932     /* slow hiding timer */
933 #if HAVE_TRANSPARENCY
934     p_slowHideTimer = new QTimer( this );
935     CONNECT( p_slowHideTimer, timeout(), this, slowHideFSC() );
936 #endif
937
938     adjustSize ();  /* need to get real width and height for moving */
939
940     /* center down */
941     QDesktopWidget * p_desktop = QApplication::desktop();
942
943     move( p_desktop->width() / 2 - width() / 2,
944           p_desktop->height() - height() );
945
946 #ifdef WIN32TRICK
947     setWindowOpacity( 0.0 );
948     fscHidden = true;
949     show();
950 #endif
951
952     vlc_mutex_init_recursive( &lock );
953 }
954
955 FullscreenControllerWidget::~FullscreenControllerWidget()
956 {
957     vlc_mutex_destroy( &lock );
958 }
959
960 /**
961  * Show fullscreen controller
962  */
963 void FullscreenControllerWidget::showFSC()
964 {
965 #ifdef WIN32TRICK
966     // after quiting and going to fs, we need to call show()
967     if( isHidden() )
968         show();
969
970     if( fscHidden )
971     {
972         fscHidden = false;
973         setWindowOpacity( 1.0 );
974     }
975 #else
976     show();
977 #endif
978
979 #if HAVE_TRANSPARENCY
980     setWindowOpacity( DEFAULT_OPACITY );
981 #endif
982 }
983
984 /**
985  * Hide fullscreen controller
986  * FIXME: under windows it have to be done by moving out of screen
987  *        because hide() doesnt work
988  */
989 void FullscreenControllerWidget::hideFSC()
990 {
991 #ifdef WIN32TRICK
992     fscHidden = true;
993     setWindowOpacity( 0.0 );    // simulate hidding
994 #else
995     hide();
996 #endif
997 }
998
999 /**
1000  * Plane to hide fullscreen controller
1001  */
1002 void FullscreenControllerWidget::planHideFSC()
1003 {
1004     vlc_mutex_lock( &lock );
1005     int i_timeout = i_hide_timeout;
1006     vlc_mutex_unlock( &lock );
1007
1008     p_hideTimer->start( i_timeout );
1009
1010 #if HAVE_TRANSPARENCY
1011     b_slow_hide_begin = true;
1012     i_slow_hide_timeout = i_timeout;
1013     p_slowHideTimer->start( i_slow_hide_timeout / 2 );
1014 #endif
1015 }
1016
1017 /**
1018  * Hidding fullscreen controller slowly
1019  * Linux: need composite manager
1020  * Windows: it is blinking, so it can be enabled by define TRASPARENCY
1021  */
1022 void FullscreenControllerWidget::slowHideFSC()
1023 {
1024 #if HAVE_TRANSPARENCY
1025     if( b_slow_hide_begin )
1026     {
1027         b_slow_hide_begin = false;
1028
1029         p_slowHideTimer->stop();
1030         /* the last part of time divided to 100 pieces */
1031         p_slowHideTimer->start( (int)( i_slow_hide_timeout / 2 / ( windowOpacity() * 100 ) ) );
1032
1033     }
1034     else
1035     {
1036 #ifdef WIN32TRICK
1037          if ( windowOpacity() > 0.0 && !fscHidden )
1038 #else
1039          if ( windowOpacity() > 0.0 )
1040 #endif
1041          {
1042              /* we should use 0.01 because of 100 pieces ^^^
1043                 but than it cannt be done in time */
1044              setWindowOpacity( windowOpacity() - 0.02 );
1045          }
1046
1047          if ( windowOpacity() <= 0.0 )
1048              p_slowHideTimer->stop();
1049     }
1050 #endif
1051 }
1052
1053 /**
1054  * event handling
1055  * events: show, hide, start timer for hidding
1056  */
1057 void FullscreenControllerWidget::customEvent( QEvent *event )
1058 {
1059     bool b_fs;
1060
1061     switch( event->type() )
1062     {
1063     case FullscreenControlShow_Type:
1064         vlc_mutex_lock( &lock );
1065         b_fs = b_fullscreen;
1066         vlc_mutex_unlock( &lock );
1067
1068         if( b_fs )  // FIXME I am not sure about that one
1069             showFSC();
1070         break;
1071     case FullscreenControlHide_Type:
1072         hideFSC();
1073         break;
1074     case FullscreenControlPlanHide_Type:
1075         if( !b_mouse_over ) // Only if the mouse is not over FSC
1076             planHideFSC();
1077         break;
1078     }
1079 }
1080
1081 /**
1082  * On mouse move
1083  * moving with FSC
1084  */
1085 void FullscreenControllerWidget::mouseMoveEvent( QMouseEvent *event )
1086 {
1087     if ( event->buttons() == Qt::LeftButton )
1088     {
1089         int i_moveX = event->globalX() - i_mouse_last_x;
1090         int i_moveY = event->globalY() - i_mouse_last_y;
1091
1092         move( x() + i_moveX, y() + i_moveY );
1093
1094         i_mouse_last_x = event->globalX();
1095         i_mouse_last_y = event->globalY();
1096     }
1097 }
1098
1099 /**
1100  * On mouse press
1101  * store position of cursor
1102  */
1103 void FullscreenControllerWidget::mousePressEvent( QMouseEvent *event )
1104 {
1105     i_mouse_last_x = event->globalX();
1106     i_mouse_last_y = event->globalY();
1107 }
1108
1109 /**
1110  * On mouse go above FSC
1111  */
1112 void FullscreenControllerWidget::enterEvent( QEvent *event )
1113 {
1114     b_mouse_over = true;
1115
1116     p_hideTimer->stop();
1117 #if HAVE_TRANSPARENCY
1118     p_slowHideTimer->stop();
1119 #endif
1120 }
1121
1122 /**
1123  * On mouse go out from FSC
1124  */
1125 void FullscreenControllerWidget::leaveEvent( QEvent *event )
1126 {
1127     planHideFSC();
1128
1129     b_mouse_over = false;
1130 }
1131
1132 /**
1133  * When you get pressed key, send it to video output
1134  * FIXME: clearing focus by clearFocus() to not getting
1135  * key press events didnt work
1136  */
1137 void FullscreenControllerWidget::keyPressEvent( QKeyEvent *event )
1138 {
1139     int i_vlck = qtEventToVLCKey( event );
1140     if( i_vlck > 0 )
1141     {
1142         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1143         event->accept();
1144     }
1145     else
1146         event->ignore();
1147 }
1148
1149 /* */
1150 static int FullscreenControllerWidgetFullscreenChanged( vlc_object_t *vlc_object, const char *variable,
1151                                                         vlc_value_t old_val, vlc_value_t new_val,
1152                                                         void *data )
1153 {
1154     vout_thread_t *p_vout = (vout_thread_t *) vlc_object;
1155     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1156
1157     p_fs->fullscreenChanged( p_vout, new_val.b_bool, var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1158
1159     return VLC_SUCCESS;
1160 }
1161 /* */
1162 static int FullscreenControllerWidgetMouseMoved( vlc_object_t *vlc_object, const char *variable,
1163                                                  vlc_value_t old_val, vlc_value_t new_val,
1164                                                  void *data )
1165 {
1166     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
1167
1168     /* Show event */
1169     IMEvent *eShow = new IMEvent( FullscreenControlShow_Type, 0 );
1170     QApplication::postEvent( p_fs, static_cast<QEvent *>(eShow) );
1171
1172     /* Plan hide event */
1173     IMEvent *eHide = new IMEvent( FullscreenControlPlanHide_Type, 0 );
1174     QApplication::postEvent( p_fs, static_cast<QEvent *>(eHide) );
1175
1176     return VLC_SUCCESS;
1177 }
1178
1179
1180 /**
1181  * It is called when video start
1182  */
1183 void FullscreenControllerWidget::attachVout( vout_thread_t *p_vout )
1184 {
1185     assert( p_vout );
1186
1187     vlc_mutex_lock( &lock );
1188     var_AddCallback( p_vout, "fullscreen", FullscreenControllerWidgetFullscreenChanged, this ); /* I miss a add and fire */
1189     fullscreenChanged( p_vout, var_GetBool( p_vout, "fullscreen" ), var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1190     vlc_mutex_unlock( &lock );
1191 }
1192 /**
1193  * It is called after turn off video.
1194  */
1195 void FullscreenControllerWidget::detachVout( vout_thread_t *p_vout )
1196 {
1197     assert( p_vout );
1198
1199     var_DelCallback( p_vout, "fullscreen", FullscreenControllerWidgetFullscreenChanged, this );
1200     vlc_mutex_lock( &lock );
1201     fullscreenChanged( p_vout, false, 0 );
1202     vlc_mutex_unlock( &lock );
1203 }
1204
1205 /**
1206  * Register and unregister callback for mouse moving
1207  */
1208 void FullscreenControllerWidget::fullscreenChanged( vout_thread_t *p_vout, bool b_fs, int i_timeout )
1209 {
1210     vlc_mutex_lock( &lock );
1211     if( b_fs && !b_fullscreen )
1212     {
1213         b_fullscreen = true;
1214         i_hide_timeout = i_timeout;
1215         var_AddCallback( p_vout, "mouse-moved", FullscreenControllerWidgetMouseMoved, this );
1216     }
1217     else if( !b_fs && b_fullscreen )
1218     {
1219         b_fullscreen = false;
1220         i_hide_timeout = i_timeout;
1221         var_DelCallback( p_vout, "mouse-moved", FullscreenControllerWidgetMouseMoved, this );
1222
1223         /* Force fs hidding */
1224         IMEvent *eHide = new IMEvent( FullscreenControlHide_Type, 0 );
1225         QApplication::postEvent( this, static_cast<QEvent *>(eHide) );
1226     }
1227     vlc_mutex_unlock( &lock );
1228 }
1229
1230 /**********************************************************************
1231  * Speed control widget
1232  **********************************************************************/
1233 SpeedControlWidget::SpeedControlWidget( intf_thread_t *_p_i ) :
1234                              QFrame( NULL ), p_intf( _p_i )
1235 {
1236     QSizePolicy sizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed );
1237     sizePolicy.setHorizontalStretch( 0 );
1238     sizePolicy.setVerticalStretch( 0 );
1239
1240     speedSlider = new QSlider;
1241     speedSlider->setSizePolicy( sizePolicy );
1242     speedSlider->setMaximumSize( QSize( 80, 200 ) );
1243     speedSlider->setOrientation( Qt::Vertical );
1244     speedSlider->setTickPosition( QSlider::TicksRight );
1245
1246     speedSlider->setRange( -24, 24 );
1247     speedSlider->setSingleStep( 1 );
1248     speedSlider->setPageStep( 1 );
1249     speedSlider->setTickInterval( 12 );
1250
1251     CONNECT( speedSlider, valueChanged( int ), this, updateRate( int ) );
1252
1253     QToolButton *normalSpeedButton = new QToolButton( this );
1254     normalSpeedButton->setMaximumSize( QSize( 26, 20 ) );
1255     normalSpeedButton->setAutoRaise( true );
1256     normalSpeedButton->setText( "1x" );
1257     normalSpeedButton->setToolTip( qtr( "Revert to normal play speed" ) );
1258
1259     CONNECT( normalSpeedButton, clicked(), this, resetRate() );
1260
1261     QVBoxLayout *speedControlLayout = new QVBoxLayout;
1262     speedControlLayout->setLayoutMargins( 4, 4, 4, 4, 4 );
1263     speedControlLayout->setSpacing( 4 );
1264     speedControlLayout->addWidget( speedSlider );
1265     speedControlLayout->addWidget( normalSpeedButton );
1266     setLayout( speedControlLayout );
1267 }
1268
1269 SpeedControlWidget::~SpeedControlWidget()
1270 {}
1271
1272 void SpeedControlWidget::setEnable( bool b_enable )
1273 {
1274     speedSlider->setEnabled( b_enable );
1275 }
1276
1277 void SpeedControlWidget::updateControls( int rate )
1278 {
1279     if( speedSlider->isSliderDown() )
1280     {
1281         //We don't want to change anything if the user is using the slider
1282         return;
1283     }
1284
1285     double value = 12 * log( (double)INPUT_RATE_DEFAULT / rate ) / log( 2 );
1286     int sliderValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
1287
1288     if( sliderValue < speedSlider->minimum() )
1289     {
1290         sliderValue = speedSlider->minimum();
1291     }
1292     else if( sliderValue > speedSlider->maximum() )
1293     {
1294         sliderValue = speedSlider->maximum();
1295     }
1296
1297     //Block signals to avoid feedback loop
1298     speedSlider->blockSignals( true );
1299     speedSlider->setValue( sliderValue );
1300     speedSlider->blockSignals( false );
1301 }
1302
1303 void SpeedControlWidget::updateRate( int sliderValue )
1304 {
1305     double speed = pow( 2, (double)sliderValue / 12 );
1306     int rate = INPUT_RATE_DEFAULT / speed;
1307
1308     THEMIM->getIM()->setRate(rate);
1309 }
1310
1311 void SpeedControlWidget::resetRate()
1312 {
1313     THEMIM->getIM()->setRate(INPUT_RATE_DEFAULT);
1314 }