]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Win32: get rid of calls to GetVersion (CoCreateInstance will fail anyway if the featu...
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Ilkka Ollakka <ileoo@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31
32 #include "main_interface.hpp"
33 #include "input_manager.hpp"
34 #include "actions_manager.hpp"
35 #include "extensions_manager.hpp" // killInstance
36
37 #include "util/customwidgets.hpp"
38 #include "util/qt_dirs.hpp"
39
40 #include "components/interface_widgets.hpp"
41 #include "components/controller.hpp"
42 #include "components/playlist/playlist.hpp"
43 #include "dialogs/external.hpp"
44 #include "dialogs/firstrun.hpp"
45
46 #include "menus.hpp"
47 #include "recents.hpp"
48
49 #include <QCloseEvent>
50 #include <QKeyEvent>
51
52 #include <QUrl>
53 #include <QSize>
54 #include <QDate>
55
56 #include <QMenu>
57 #include <QMenuBar>
58 #include <QStatusBar>
59 #include <QLabel>
60 #include <QGroupBox>
61 #include <QPushButton>
62 #include <QStackedWidget>
63
64 #ifdef WIN32
65  #include <vlc_windows_interfaces.h>
66  #include <QBitmap>
67 #endif
68
69 #include <assert.h>
70
71 #include <vlc_keys.h> /* Wheel event */
72 #include <vlc_vout_window.h>
73 #include <vlc_vout_display.h>
74
75 // #define DEBUG_INTF
76 /* Callback prototypes */
77 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
78                         vlc_value_t old_val, vlc_value_t new_val, void *param );
79 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
80                        vlc_value_t old_val, vlc_value_t new_val, void *param );
81
82 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
83 {
84     /* Variables initialisation */
85     // need_components_update = false;
86     bgWidget             = NULL;
87     videoWidget          = NULL;
88     playlistWidget       = NULL;
89 #ifndef HAVE_MAEMO
90     sysTray              = NULL;
91 #endif
92     fullscreenControls   = NULL;
93     cryptedLabel         = NULL;
94     controls             = NULL;
95     inputC               = NULL;
96
97     b_hideAfterCreation  = false;
98     playlistVisible      = false; // FIXME remove
99     input_name           = "";
100
101     i_bg_height          = 0;
102
103     /* Ask for Privacy */
104     FirstRun::CheckAndRun( this, p_intf );
105
106     /**
107      *  Configuration and settings
108      *  Pre-building of interface
109      **/
110     /* Main settings */
111     setFocusPolicy( Qt::StrongFocus );
112     setAcceptDrops( true );
113     setWindowRole( "vlc-main" );
114     setWindowIcon( QApplication::windowIcon() );
115     setWindowOpacity( var_InheritFloat( p_intf, "qt-opacity" ) );
116
117     /* Set The Video In emebedded Mode or not */
118     videoEmbeddedFlag = var_InheritBool( p_intf, "embedded-video" );
119
120     /* Does the interface resize to video size or the opposite */
121     b_keep_size = !var_InheritBool( p_intf, "qt-video-autoresize" );
122
123     /* Are we in the enhanced always-video mode or not ? */
124     i_visualmode = var_InheritInteger( p_intf, "qt-display-mode" );
125
126     /* Do we want anoying popups or not */
127     notificationEnabled = (bool)var_InheritBool( p_intf, "qt-notification" );
128
129     /* Set the other interface settings */
130     settings = getSettings();
131     settings->beginGroup( "MainWindow" );
132
133     /**
134      * Retrieve saved sizes for main window
135      *   mainBasedSize = based window size for normal mode
136      *                  (no video, no background)
137      *   mainVideoSize = window size with video (all modes)
138      **/
139     mainBasedSize = settings->value( "mainBasedSize", QSize( 350, 120 ) ).toSize();
140     mainVideoSize = settings->value( "mainVideoSize", QSize( 400, 300 ) ).toSize();
141
142
143     /**************
144      * Status Bar *
145      **************/
146     createStatusBar();
147
148     /**************************
149      *  UI and Widgets design
150      **************************/
151     setVLCWindowsTitle();
152     createMainWidget( settings );
153
154     /************
155      * Menu Bar *
156      ************/
157     QVLCMenu::createMenuBar( this, p_intf );
158     CONNECT( THEMIM->getIM(), voutListChanged( vout_thread_t **, int ),
159              this, destroyPopupMenu() );
160
161     /*********************************
162      * Create the Systray Management *
163      *********************************/
164     initSystray();
165
166     /********************
167      * Input Manager    *
168      ********************/
169     MainInputManager::getInstance( p_intf );
170
171 #ifdef WIN32
172     taskbar_wmsg = RegisterWindowMessage("TaskbarButtonCreated");
173 #endif
174
175     /************************************************************
176      * Connect the input manager to the GUI elements it manages *
177      ************************************************************/
178     /**
179      * Connects on nameChanged()
180      * Those connects are different because options can impeach them to trigger.
181      **/
182     /* Main Interface statusbar */
183     CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
184              this, setName( const QString& ) );
185     /* and systray */
186 #ifndef HAVE_MAEMO
187     if( sysTray )
188     {
189         CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
190                  this, updateSystrayTooltipName( const QString& ) );
191     }
192 #endif
193     /* and title of the Main Interface*/
194     if( var_InheritBool( p_intf, "qt-name-in-title" ) )
195     {
196         CONNECT( THEMIM->getIM(), nameChanged( const QString& ),
197                  this, setVLCWindowsTitle( const QString& ) );
198     }
199
200     /**
201      * CONNECTS on PLAY_STATUS
202      **/
203     /* Status on the systray */
204 #ifndef HAVE_MAEMO
205     if( sysTray )
206     {
207         CONNECT( THEMIM->getIM(), statusChanged( int ),
208                  this, updateSystrayTooltipStatus( int ) );
209     }
210 #endif
211
212     /* END CONNECTS ON IM */
213
214     /************
215      * Callbacks
216      ************/
217     var_AddCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
218
219     /* Register callback for the intf-popupmenu variable */
220     var_AddCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
221
222
223     /* VideoWidget connects for asynchronous calls */
224     connect( this, SIGNAL(askGetVideo(WId*,int*,int*,unsigned*,unsigned *)),
225              this, SLOT(getVideoSlot(WId*,int*,int*,unsigned*,unsigned*)),
226              Qt::BlockingQueuedConnection );
227     connect( this, SIGNAL(askReleaseVideo( void )),
228              this, SLOT(releaseVideoSlot( void )),
229              Qt::BlockingQueuedConnection );
230
231     if( videoWidget )
232     {
233         CONNECT( this, askVideoToResize( unsigned int, unsigned int ),
234                  videoWidget, SetSizing( unsigned int, unsigned int ) );
235         CONNECT( this, askVideoSetFullScreen( bool ),
236                  videoWidget, SetFullScreen( bool ) );
237         CONNECT( videoWidget, keyPressed( QKeyEvent * ),
238                  this, handleKeyPress( QKeyEvent * ) );
239     }
240
241     CONNECT( this, askUpdate(), this, doComponentsUpdate() );
242     CONNECT( THEDP, toolBarConfUpdated(), this, recreateToolbars() );
243
244         /* Enable the popup menu in the MI */
245     setContextMenuPolicy( Qt::CustomContextMenu );
246     CONNECT( this, customContextMenuRequested( const QPoint& ),
247              this, popupMenu( const QPoint& ) );
248
249     /** END of CONNECTS**/
250
251
252     /**** FINAL SIZING and placement of interface */
253     settings->beginGroup( "MainWindow" );
254     QVLCTools::restoreWidgetPosition( settings, this, QSize(380, 60) );
255
256     /* resize to previously saved main window size if appicable */
257     if( b_keep_size )
258     {
259        if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
260            i_visualmode == QT_MINIMAL_MODE )
261        {
262            resize( mainVideoSize );
263        }
264        else
265        {
266            resize( mainBasedSize );
267        }
268     }
269
270     msg_Dbg( p_intf, "%i", stackCentralOldState );
271     /* Playlist */
272     if( settings->value( "playlist-visible", 0 ).toInt() )
273         togglePlaylist();
274     settings->endGroup();
275
276     /* Final sizing and showing */
277     setVisible( !b_hideAfterCreation );
278     //setMinimumSize( QSize( 0, 0 ) );
279     //    setMinimumWidth( __MAX( controls->sizeHint().width(),
280     //                          menuBar()->sizeHint().width() ) );
281
282     debug();
283     /* And switch to minimal view if needed
284        Must be called after the show() */
285     if( i_visualmode == QT_MINIMAL_MODE )
286         toggleMinimalView( true );
287
288     /* Update the geometry : It is useful if you switch between
289        qt-display-modes */
290     updateGeometry();
291     resize( sizeHint() );
292 }
293
294 MainInterface::~MainInterface()
295 {
296     msg_Dbg( p_intf, "Destroying the main interface" );
297
298     /* Unsure we hide the videoWidget before destroying it */
299     if( stackCentralOldState == VIDEO_TAB )
300         showBg();
301
302     /* Save playlist state */
303     if( playlistWidget )
304     {
305         if( !isDocked() )
306             QVLCTools::saveWidgetPosition( p_intf, "Playlist", playlistWidget );
307
308         delete playlistWidget;
309     }
310
311 #ifdef WIN32
312     if( himl )
313         ImageList_Destroy( himl );
314     if(p_taskbl)
315         p_taskbl->vt->Release(p_taskbl);
316     CoUninitialize();
317 #endif
318
319     /* Be sure to kill the actionsManager... FIXME */
320     ActionsManager::killInstance();
321
322     /* Idem, FIXME */
323     ExtensionsManager::killInstance();
324
325     /* Delete the FSC controller */
326     delete fullscreenControls;
327
328     /* Save states */
329     settings->beginGroup( "MainWindow" );
330     settings->setValue( "pl-dock-status", (int)i_pl_dock );
331     settings->setValue( "playlist-visible", (int)playlistVisible );
332     settings->setValue( "adv-controls",
333                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
334
335     settings->setValue( "mainBasedSize", mainBasedSize );
336     settings->setValue( "mainVideoSize", mainVideoSize );
337
338     if( bgWidget )
339         settings->setValue( "backgroundSize", bgWidget->size() );
340
341     /* Save this size */
342     QVLCTools::saveWidgetPosition(settings, this);
343     settings->endGroup();
344
345     /* Unregister callbacks */
346     var_DelCallback( p_intf->p_libvlc, "intf-show", IntfShowCB, p_intf );
347     var_DelCallback( p_intf->p_libvlc, "intf-popupmenu", PopupMenuCB, p_intf );
348
349     p_intf->p_sys->p_mi = NULL;
350 }
351
352 /*****************************
353  *   Main UI handling        *
354  *****************************/
355 void MainInterface::recreateToolbars()
356 {
357     //msg_Dbg( p_intf, "Recreating the toolbars" );
358     settings->beginGroup( "MainWindow" );
359     delete controls;
360     delete inputC;
361
362     controls = new ControlsWidget( p_intf, false, this ); /* FIXME */
363     CONNECT( controls, advancedControlsToggled( bool ),
364              this, doComponentsUpdate() );
365     CONNECT( controls, sizeChanged(),
366              this, doComponentsUpdate() );
367
368     inputC = new InputControlsWidget( p_intf, this );
369
370     mainLayout->insertWidget( 2, inputC );
371     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
372                               controls );
373     settings->endGroup();
374 }
375
376 void MainInterface::createMainWidget( QSettings *settings )
377 {
378     /* Create the main Widget and the mainLayout */
379     QWidget *main = new QWidget;
380     setCentralWidget( main );
381     mainLayout = new QVBoxLayout( main );
382
383     /* Margins, spacing */
384     main->setContentsMargins( 0, 0, 0, 0 );
385     mainLayout->setSpacing( 0 ); mainLayout->setMargin( 0 );
386
387     /* */
388     stackCentralW = new QStackedWidget( main );
389
390     /* Bg Cone */
391     bgWidget = new BackgroundWidget( p_intf );
392     bgWidget->resize(
393             settings->value( "backgroundSize", QSize( 300, 200 ) ).toSize() );
394     bgWidget->updateGeometry();
395     stackCentralW->insertWidget( BACKG_TAB, bgWidget );
396
397
398     /* And video Outputs */
399     if( videoEmbeddedFlag )
400     {
401         videoWidget = new VideoWidget( p_intf );
402         stackCentralW->insertWidget( VIDEO_TAB, videoWidget );
403     }
404     mainLayout->insertWidget( 1, stackCentralW, 100 );
405
406
407     /* Create the CONTROLS Widget */
408     controls = new ControlsWidget( p_intf,
409                    settings->value( "adv-controls", false ).toBool(), this );
410     CONNECT( controls, advancedControlsToggled( bool ),
411              this, doComponentsUpdate() );
412     CONNECT( controls, sizeChanged(),
413              this, doComponentsUpdate() );
414     inputC = new InputControlsWidget( p_intf, this );
415
416     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
417         i_visualmode != QT_MINIMAL_MODE )
418     {
419         hideStackWidget();
420         stackCentralOldState = HIDDEN_TAB;
421     }
422     else
423     {
424         showTab( BACKG_TAB );
425         stackCentralOldState = BACKG_TAB;
426     }
427
428
429     //mainLayout->setRowStretch( 1, 10 );
430     mainLayout->insertWidget( 2, inputC );
431     mainLayout->insertWidget( settings->value( "ToolbarPos", 0 ).toInt() ? 0: 3,
432                               controls );
433
434     /* Visualisation */
435     /* Disabled for now, they SUCK */
436     #if 0
437     visualSelector = new VisualSelector( p_intf );
438     mainLayout->insertWidget( 0, visualSelector );
439     visualSelector->hide();
440     #endif
441
442     /* Finish the sizing */
443     main->updateGeometry();
444
445     getSettings()->endGroup();
446 #ifdef WIN32
447     if ( depth() > 8 )
448 #endif
449     /* Create the FULLSCREEN CONTROLS Widget */
450     if( var_InheritBool( p_intf, "qt-fs-controller" ) )
451     {
452         fullscreenControls = new FullscreenControllerWidget( p_intf, this );
453         CONNECT( fullscreenControls, keyPressed( QKeyEvent * ),
454                  this, handleKeyPress( QKeyEvent * ) );
455     }
456 }
457
458 inline void MainInterface::initSystray()
459 {
460 #ifndef HAVE_MAEMO
461     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
462     bool b_systrayWanted = var_InheritBool( p_intf, "qt-system-tray" );
463
464     if( var_InheritBool( p_intf, "qt-start-minimized") )
465     {
466         if( b_systrayAvailable )
467         {
468             b_systrayWanted = true;
469             b_hideAfterCreation = true;
470         }
471         else
472             msg_Err( p_intf, "cannot start minimized without system tray bar" );
473     }
474
475     if( b_systrayAvailable && b_systrayWanted )
476         createSystray();
477 #endif
478 }
479
480 inline void MainInterface::createStatusBar()
481 {
482     /****************
483      *  Status Bar  *
484      ****************/
485     /* Widgets Creation*/
486     QStatusBar *statusBarr = statusBar();
487
488     TimeLabel *timeLabel = new TimeLabel( p_intf );
489     nameLabel = new QLabel( this );
490     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
491                                       | Qt::TextSelectableByKeyboard );
492     SpeedLabel *speedLabel = new SpeedLabel( p_intf, "1.00x", this );
493
494     /* Styling those labels */
495     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
496     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
497     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
498
499     /* and adding those */
500     statusBarr->addWidget( nameLabel, 8 );
501     statusBarr->addPermanentWidget( speedLabel, 0 );
502     statusBarr->addPermanentWidget( timeLabel, 0 );
503
504     /* timeLabel behaviour:
505        - double clicking opens the goto time dialog
506        - right-clicking and clicking just toggle between remaining and
507          elapsed time.*/
508     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
509
510     CONNECT( THEMIM->getIM(), encryptionChanged( bool ),
511              this, showCryptedLabel( bool ) );
512 }
513
514 #ifdef WIN32
515 void MainInterface::createTaskBarButtons()
516 {
517     taskbar_wmsg = WM_NULL;
518     /*Here is the code for the taskbar thumb buttons
519     FIXME:We need pretty buttons in 16x16 px that are handled correctly by masks in Qt
520     FIXME:the play button's picture doesn't changed to pause when clicked
521     */
522
523     CoInitialize( 0 );
524
525     if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
526                 NULL, CLSCTX_INPROC_SERVER,
527                 &IID_ITaskbarList3,
528                 (void **)&p_taskbl) )
529     {
530         p_taskbl->vt->HrInit(p_taskbl);
531
532         if(himl = ImageList_Create( 15, //cx
533                         18, //cy
534                         ILC_COLOR,//flags
535                         4,//initial nb of images
536                         0//nb of images that can be added
537                         ))
538         {
539             QPixmap img   = QPixmap(":/toolbar/previous_b");
540             QPixmap img2  = QPixmap(":/toolbar/pause_b");
541             QPixmap img3  = QPixmap(":/toolbar/play_b");
542             QPixmap img4  = QPixmap(":/toolbar/next_b");
543             QBitmap mask  = img.createMaskFromColor(Qt::transparent);
544             QBitmap mask2 = img2.createMaskFromColor(Qt::transparent);
545             QBitmap mask3 = img3.createMaskFromColor(Qt::transparent);
546             QBitmap mask4 = img4.createMaskFromColor(Qt::transparent);
547
548             if(-1 == ImageList_Add(himl, img.toWinHBITMAP(QPixmap::PremultipliedAlpha),mask.toWinHBITMAP()))
549                 msg_Err( p_intf, "ImageList_Add failed" );
550             if(-1 == ImageList_Add(himl, img2.toWinHBITMAP(QPixmap::PremultipliedAlpha),mask2.toWinHBITMAP()))
551                 msg_Err( p_intf, "ImageList_Add failed" );
552             if(-1 == ImageList_Add(himl, img3.toWinHBITMAP(QPixmap::PremultipliedAlpha),mask3.toWinHBITMAP()))
553                 msg_Err( p_intf, "ImageList_Add failed" );
554             if(-1 == ImageList_Add(himl, img4.toWinHBITMAP(QPixmap::PremultipliedAlpha),mask4.toWinHBITMAP()))
555                 msg_Err( p_intf, "ImageList_Add failed" );
556         }
557
558         // Define an array of two buttons. These buttons provide images through an
559         // image list and also provide tooltips.
560         DWORD dwMask = THB_BITMAP | THB_FLAGS;
561
562         THUMBBUTTON thbButtons[3];
563         thbButtons[0].dwMask = dwMask;
564         thbButtons[0].iId = 0;
565         thbButtons[0].iBitmap = 0;
566         thbButtons[0].dwFlags = THBF_HIDDEN;
567
568         thbButtons[1].dwMask = dwMask;
569         thbButtons[1].iId = 1;
570         thbButtons[1].iBitmap = 2;
571         thbButtons[1].dwFlags = THBF_HIDDEN;
572
573         thbButtons[2].dwMask = dwMask;
574         thbButtons[2].iId = 2;
575         thbButtons[2].iBitmap = 3;
576         thbButtons[2].dwFlags = THBF_HIDDEN;
577
578         HRESULT hr = p_taskbl->vt->ThumbBarSetImageList(p_taskbl, winId(), himl );
579         if(S_OK != hr)
580             msg_Err( p_intf, "ThumbBarSetImageList failed with error %08x", hr );
581         else
582         {
583             hr = p_taskbl->vt->ThumbBarAddButtons(p_taskbl, winId(), 3, thbButtons);
584             if(S_OK != hr)
585                 msg_Err( p_intf, "ThumbBarAddButtons failed with error %08x", hr );
586         }
587         CONNECT( THEMIM->getIM(), statusChanged( int ), this, changeThumbbarButtons( int ) );
588     }
589     else
590     {
591         himl = NULL;
592         p_taskbl = NULL;
593     }
594
595 }
596
597 bool MainInterface::winEvent ( MSG * msg, long * result )
598 {
599     if (msg->message == taskbar_wmsg)
600     {
601         //We received the taskbarbuttoncreated, now we can really create th buttons
602         createTaskBarButtons();
603     }
604
605     short cmd;
606     switch( msg->message )
607     {
608         case WM_COMMAND:
609             if (HIWORD(msg->wParam) == THBN_CLICKED)
610             {
611                 switch(LOWORD(msg->wParam))
612                 {
613                     case 0:
614                         THEMIM->prev();
615                         break;
616                     case 1:
617                         THEMIM->togglePlayPause();
618                         break;
619                     case 2:
620                         THEMIM->next();
621                         break;
622                 }
623             }
624             break;
625         case WM_APPCOMMAND:
626             cmd = GET_APPCOMMAND_LPARAM(msg->lParam);
627             switch(cmd)
628             {
629                 case APPCOMMAND_MEDIA_PLAY_PAUSE:
630                     THEMIM->togglePlayPause();
631                     break;
632                 case APPCOMMAND_MEDIA_PLAY:
633                     THEMIM->play();
634                     break;
635                 case APPCOMMAND_MEDIA_PAUSE:
636                     THEMIM->pause();
637                     break;
638                 case APPCOMMAND_MEDIA_PREVIOUSTRACK:
639                     THEMIM->prev();
640                     break;
641                 case APPCOMMAND_MEDIA_NEXTTRACK:
642                     THEMIM->next();
643                     break;
644                 case APPCOMMAND_MEDIA_STOP:
645                     THEMIM->stop();
646                     break;
647                 case APPCOMMAND_VOLUME_DOWN:
648                     THEAM->AudioDown();
649                     break;
650                 case APPCOMMAND_VOLUME_UP:
651                     THEAM->AudioUp();
652                     break;
653                 case APPCOMMAND_VOLUME_MUTE:
654                     THEAM->toggleMuteAudio();
655                     break;
656                 default:
657                      msg_Dbg( p_intf, "unknown APPCOMMAND = %d", cmd);
658                      break;
659             }
660             break;
661     }
662     return false;
663 }
664 #endif
665
666 /**********************************************************************
667  * Handling of sizing of the components
668  **********************************************************************/
669
670 /* This function is probably wrong, but we don't have many many choices...
671    Since we can't know from the playlist Widget if we are inside a dock or not,
672    because the playlist Widget can be called by THEDP, as a separate windows for
673    the skins.
674    Maybe the other solution is to redefine the sizeHint() of the playlist and
675    ask _parent->isFloating()...
676    If you think this would be better, please FIXME it...
677 */
678
679 QSize MainInterface::sizeHint() const
680 {
681 #if 0
682     if( b_keep_size )
683     {
684         if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
685             i_visualmode == QT_MINIMAL_MODE )
686         {
687                 return mainVideoSize;
688         }
689         else
690         {
691             if( VISIBLE( bgWidget) ||
692                 ( videoIsActive && videoWidget->isVisible() )
693               )
694                 return mainVideoSize;
695             else
696                 return mainBasedSize;
697         }
698     }
699 #endif
700
701     int nwidth  = __MAX( controls->sizeHint().width(),
702                          menuBar()->sizeHint().width() );
703
704     int nheight = controls->isVisible() ?
705                   controls->size().height()
706                   + inputC->size().height()
707                   + menuBar()->size().height()
708                   + statusBar()->size().height()
709                   : 0 ;
710
711     if( stackCentralW->isVisible() )
712         nheight += stackCentralW->height();
713         nwidth  = __MAX( nwidth, stackCentralW->width() );
714
715 /*    if( VISIBLE( bgWidget ) )
716     {
717         msg_Warn( p_intf, "Hello here" );
718         if( i_bg_height )
719             nheight += i_bg_height;
720         else
721             nheight += bgWidget->size().height();
722         nwidth  = __MAX( nwidth, bgWidget->size().width() );
723     }
724     else if( videoIsActive && videoWidget->isVisible() )
725     {
726         msg_Warn( p_intf, "Hello there" );
727         nheight += videoWidget->sizeHint().height();
728         nwidth  = __MAX( nwidth, videoWidget->sizeHint().width() );
729     }*/
730 #if 0
731     if( !dockPL->isFloating() && dockPL->isVisible() && dockPL->widget()  )
732     {
733         nheight += dockPL->size().height();
734         nwidth = __MAX( nwidth, dockPL->size().width() );
735         msg_Warn( p_intf, "3 %i %i", nheight, nwidth );
736     }
737 #endif
738     return QSize( nwidth, nheight );
739 }
740
741
742 /* Video widget cannot do this synchronously as it runs in another thread */
743 /* Well, could it, actually ? Probably dangerous ... */
744
745 /* This function is called:
746    - toggling of minimal View
747    - through askUpdate() by Vout thread request video and resize video (zoom)
748    - Advanced buttons toggled
749  */
750 void MainInterface::doComponentsUpdate()
751 {
752     if( isFullScreen() || isMaximized() ) return;
753
754 //    msg_Warn( p_intf, "Updating the geometry" );
755     /* Here we resize to sizeHint() and not adjustsize because we want
756        the videoWidget to be exactly the correctSize */
757
758 #ifndef NDEBUG
759     debug();
760 #endif
761     /* This is WRONG, but I believe there is a Qt bug here */
762     setMinimumSize( 0, 0 );
763     resize( sizeHint() );
764
765     //adjustSize() ; /* This is not needed, but might help in the future */
766 }
767
768 void MainInterface::debug()
769 {
770 #ifndef NDEBUG
771     msg_Dbg( p_intf, "Stack Size: %i - %i", stackCentralW->size().height(), size().width() );
772     if( videoEmbeddedFlag )
773         msg_Dbg( p_intf, "Stack Size: %i - %i",
774                  stackCentralW->widget( VIDEO_TAB )->size().height(),
775                  stackCentralW->widget( VIDEO_TAB )->size().width() );
776     else
777         msg_Dbg( p_intf, "no embedded video" );
778
779     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
780     msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
781     //msg_Dbg( p_intf, "maximumsize: %i - %i", maximumSize().height(), maximumSize().width() );
782
783     msg_Dbg( p_intf, "Stack minimumsize: %i - %i", stackCentralW->minimumSize().height(), stackCentralW->minimumSize().width() );
784     msg_Dbg( p_intf, "Controls minimumsize: %i - %i", controls->minimumSize().height(), controls->minimumSize().width() );
785     msg_Dbg( p_intf, "Central minimumsize: %i - %i", centralWidget()->minimumSize().height(), centralWidget()->minimumSize().width() );
786     msg_Dbg( p_intf, "Menu minimumsize: %i - %i", menuBar()->minimumSize().height(), menuBar()->minimumSize().width() );
787     msg_Dbg( p_intf, "Input minimuSize: %i - %i", inputC->minimumSize().height(), inputC->minimumSize().width() );
788     msg_Dbg( p_intf, "Status minimumsize: %i - %i", statusBar()->minimumSize().height(), statusBar()->minimumSize().width() );
789     msg_Dbg( p_intf, "minimumsize: %i - %i", minimumSize().height(), minimumSize().width() );
790
791     /*if( videoWidget && videoWidget->isVisible() )
792     {
793         msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
794         msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
795     }*/
796 #endif
797 }
798
799 inline void MainInterface::showTab( int i_tab )
800 {
801 #ifdef DEBUG_INTF
802     msg_Err( p_intf, "showTab %i", i_tab );
803     msg_Warn( p_intf, "Old stackCentralOldState %i", stackCentralOldState );
804 #endif
805     stackCentralOldState = stackCentralW->isVisible() ? stackCentralW->currentIndex()
806                                           : HIDDEN_TAB;
807 #ifdef DEBUG_INTF
808     msg_Warn( p_intf, "State change %i %i",  stackCentralW->currentIndex(), i_tab );
809 #endif
810
811     if( i_visualmode == QT_NORMAL_MODE )
812     {
813         stackCentralW->setVisible( i_tab != HIDDEN_TAB );
814         doComponentsUpdate(); // resize the player
815     }
816     else
817         if( i_tab == HIDDEN_TAB ) i_tab == BACKG_TAB;
818
819     stackCentralW->setCurrentIndex( i_tab );
820
821 #ifdef DEBUG_INTF
822     msg_Warn( p_intf, "New stackCentralOldState %i", stackCentralOldState );
823 #endif
824 }
825
826 inline void MainInterface::restoreStackOldWidget()
827 {
828 #ifdef DEBUG_INTF
829     msg_Warn( p_intf, "Old stackCentralOldState %i", stackCentralOldState );
830 #endif
831     int temp = stackCentralW->isVisible() ? stackCentralW->currentIndex()
832                                           : HIDDEN_TAB;
833     stackCentralW->setCurrentIndex( stackCentralOldState );
834     if( i_visualmode == QT_NORMAL_MODE )
835     {
836         stackCentralW->setVisible( stackCentralOldState != HIDDEN_TAB );
837         doComponentsUpdate(); // resize the player
838     }
839
840     stackCentralOldState = temp;
841 #ifdef DEBUG_INTF
842     msg_Warn( p_intf, "Debug %i %i", temp, stackCentralW->currentIndex() );
843 #endif
844 }
845
846 void MainInterface::destroyPopupMenu()
847 {
848     QVLCMenu::PopupMenu( p_intf, false );
849 }
850
851 void MainInterface::toggleFSC()
852 {
853    if( !fullscreenControls ) return;
854
855    IMEvent *eShow = new IMEvent( FullscreenControlToggle_Type, 0 );
856    QApplication::postEvent( fullscreenControls, eShow );
857 }
858
859 void MainInterface::popupMenu( const QPoint &p )
860 {
861     /* Ow, that's ugly: don't show the popup menu if cursor over
862      * the main menu bar or the status bar */
863     if( !childAt( p ) || ( ( childAt( p ) != menuBar() )
864                         && ( childAt( p )->parentWidget() != statusBar() ) ) )
865         QVLCMenu::PopupMenu( p_intf, true );
866 }
867
868 /****************************************************************************
869  * Video Handling
870  ****************************************************************************/
871
872 /* This event is used to deal with the fullscreen and always on top
873    issue conflict (bug in wx) */
874 class SetVideoOnTopQtEvent : public QEvent
875 {
876 public:
877     SetVideoOnTopQtEvent( bool _onTop ) :
878       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
879     {}
880
881     bool OnTop() const { return onTop; }
882
883 private:
884     bool onTop;
885 };
886
887 /**
888  * NOTE:
889  * You must note change the state of this object or other Qt4 UI objects,
890  * from the video output thread - only from the Qt4 UI main loop thread.
891  * All window provider queries must be handled through signals or events.
892  * That's why we have all those emit statements...
893  */
894 WId MainInterface::getVideo( int *pi_x, int *pi_y,
895                              unsigned int *pi_width, unsigned int *pi_height )
896 {
897     if( !videoWidget )
898         return 0;
899
900     /* This is a blocking call signal. Results are returned through pointers.
901      * Beware of deadlocks! */
902     WId id;
903     emit askGetVideo( &id, pi_x, pi_y, pi_width, pi_height );
904     return id;
905 }
906
907 void MainInterface::getVideoSlot( WId *p_id, int *pi_x, int *pi_y,
908                                   unsigned *pi_width, unsigned *pi_height )
909 {
910     /* Request the videoWidget */
911     WId ret = videoWidget->request( pi_x, pi_y,
912                                     pi_width, pi_height, b_keep_size );
913     *p_id = ret;
914     if( ret ) /* The videoWidget is available */
915     {
916         /* ask videoWidget to show */
917         videoWidget->SetSizing( *pi_width, *pi_height );
918
919         /* Consider the video active now */
920         showVideo();
921
922         stackCentralW->resize( *pi_width, *pi_height );
923
924         emit askUpdate();
925     }
926 }
927
928
929
930 /* Asynchronous call from the WindowClose function */
931 void MainInterface::releaseVideo( void )
932 {
933     emit askReleaseVideo( );
934 }
935
936 /* Function that is CONNECTED to the previous emit */
937 void MainInterface::releaseVideoSlot( void )
938 {
939     videoWidget->release( );
940
941     restoreStackOldWidget();
942
943     /* Try to resize, except when you are in Fullscreen mode */
944 //    doComponentsUpdate();
945 }
946
947 /* Asynchronous call from WindowControl function */
948 int MainInterface::controlVideo( int i_query, va_list args )
949 {
950     /* Debug to check if VOUT_WINDOW_SET_SIZE is called, because this is broken now */
951     msg_Warn( p_intf, "Control Video: %i", i_query );
952     switch( i_query )
953     {
954     case VOUT_WINDOW_SET_SIZE:
955     {
956         unsigned int i_width  = va_arg( args, unsigned int );
957         unsigned int i_height = va_arg( args, unsigned int );
958         emit askVideoToResize( i_width, i_height );
959         emit askUpdate();
960         return VLC_EGENERIC;
961     }
962     case VOUT_WINDOW_SET_STATE:
963     {
964         unsigned i_arg = va_arg( args, unsigned );
965         unsigned on_top = i_arg & VOUT_WINDOW_STATE_ABOVE;
966         QApplication::postEvent( this, new SetVideoOnTopQtEvent( on_top ) );
967         return VLC_SUCCESS;
968     }
969     case VOUT_WINDOW_SET_FULLSCREEN:
970     {
971         bool b_fs = va_arg( args, int );
972         emit askVideoSetFullScreen( b_fs );
973         return VLC_SUCCESS;
974     }
975     default:
976         msg_Warn( p_intf, "unsupported control query" );
977         return VLC_EGENERIC;
978     }
979 }
980
981 /*****************************************************************************
982  * Playlist, Visualisation and Menus handling
983  *****************************************************************************/
984 /**
985  * Toggle the playlist widget or dialog
986  **/
987 void MainInterface::createPlaylist( bool b_show )
988 {
989     playlistWidget = new PlaylistWidget( p_intf, this );
990
991     i_pl_dock = PL_BOTTOM;
992     /* i_pl_dock = (pl_dock_e)getSettings()
993       ->value( "pl-dock-status", PL_UNDOCKED ).toInt(); */
994
995     if( i_pl_dock == PL_UNDOCKED )
996     {
997         playlistWidget->setWindowFlags( Qt::Window );
998
999         /* This will restore the geometry but will not work for position,
1000            because of parenting */
1001         QVLCTools::restoreWidgetPosition( p_intf, "Playlist",
1002                 playlistWidget, QSize( 600, 300 ) );
1003     }
1004     else
1005     {
1006 #ifdef DEBUG_INTF
1007         msg_Warn( p_intf, "Here %i", stackCentralW->currentIndex() );
1008 #endif
1009         stackCentralW->insertWidget( PLAYL_TAB, playlistWidget );
1010 #ifdef DEBUG_INTF
1011         msg_Warn( p_intf, "Here %i", stackCentralW->currentIndex() );
1012 #endif
1013     }
1014
1015     if( b_show )
1016     {
1017         playlistVisible = true;
1018         stackCentralW->show();
1019     }
1020 }
1021
1022 void MainInterface::togglePlaylist()
1023 {
1024 #ifdef DEBUG_INTF
1025     msg_Warn( p_intf, "Here toggling %i %i", stackCentralW->currentIndex(), stackCentralOldState );
1026 #endif
1027     if( !playlistWidget )
1028     {
1029         createPlaylist( true );
1030     }
1031 #ifdef DEBUG_INTF
1032     msg_Warn( p_intf, "Here toggling %i %i", stackCentralW->currentIndex(), stackCentralOldState );
1033
1034 #endif
1035     if( i_pl_dock != PL_UNDOCKED )
1036     {
1037         /* Playlist not visible */
1038         if( stackCentralW->currentIndex() != PLAYL_TAB )
1039         {
1040             showTab( PLAYL_TAB );
1041             stackCentralW->show();
1042         }
1043         else
1044         {
1045             restoreStackOldWidget();
1046         }
1047         playlistVisible = ( stackCentralW->currentIndex() == PLAYL_TAB );
1048         //doComponentsUpdate(); //resize( sizeHint() );
1049     }
1050 }
1051
1052 /* Function called from the menu to undock the playlist */
1053 void MainInterface::undockPlaylist()
1054 {
1055 //    dockPL->setFloating( true );
1056 //    adjustSize();
1057 }
1058
1059 void MainInterface::dockPlaylist( pl_dock_e i_pos )
1060 {
1061 }
1062
1063 void MainInterface::toggleMinimalView( bool b_switch )
1064 {
1065     if( i_visualmode != QT_ALWAYS_VIDEO_MODE &&
1066         i_visualmode != QT_MINIMAL_MODE )
1067     { /* NORMAL MODE then */
1068         stackCentralW->show();
1069         if( !videoWidget || stackCentralW->currentIndex() != VIDEO_TAB )
1070         {
1071             showBg();
1072         }
1073         else
1074         {
1075             /* If video is visible, then toggle the status of bgWidget */
1076             //bgWasVisible = !bgWasVisible;
1077             if( stackCentralOldState == BACKG_TAB )
1078                 stackCentralOldState = HIDDEN_TAB;
1079             else
1080                 stackCentralOldState = BACKG_TAB;
1081         }
1082     }
1083
1084     i_bg_height = stackCentralW->height();
1085
1086     menuBar()->setVisible( !b_switch );
1087     controls->setVisible( !b_switch );
1088     statusBar()->setVisible( !b_switch );
1089     inputC->setVisible( !b_switch );
1090
1091     doComponentsUpdate();
1092
1093     emit minimalViewToggled( b_switch );
1094 }
1095
1096 /* toggling advanced controls buttons */
1097 void MainInterface::toggleAdvanced()
1098 {
1099     controls->toggleAdvanced();
1100 //    if( fullscreenControls ) fullscreenControls->toggleAdvanced();
1101 }
1102
1103 /* Get the visibility status of the controls (hidden or not, advanced or not) */
1104 int MainInterface::getControlsVisibilityStatus()
1105 {
1106     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
1107                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
1108 }
1109
1110 #if 0
1111 void MainInterface::visual()
1112 {
1113     if( !VISIBLE( visualSelector) )
1114     {
1115         visualSelector->show();
1116         if( !THEMIM->getIM()->hasVideo() )
1117         {
1118             /* Show the background widget */
1119         }
1120         visualSelectorEnabled = true;
1121     }
1122     else
1123     {
1124         /* Stop any currently running visualization */
1125         visualSelector->hide();
1126         visualSelectorEnabled = false;
1127     }
1128     doComponentsUpdate();
1129 }
1130 #endif
1131
1132 /************************************************************************
1133  * Other stuff
1134  ************************************************************************/
1135 void MainInterface::setName( const QString& name )
1136 {
1137     input_name = name; /* store it for the QSystray use */
1138     /* Display it in the status bar, but also as a Tooltip in case it doesn't
1139        fit in the label */
1140     nameLabel->setText( " " + name + " " );
1141     nameLabel->setToolTip( " " + name +" " );
1142 }
1143
1144 /**
1145  * Give the decorations of the Main Window a correct Name.
1146  * If nothing is given, set it to VLC...
1147  **/
1148 void MainInterface::setVLCWindowsTitle( const QString& aTitle )
1149 {
1150     if( aTitle.isEmpty() )
1151     {
1152         setWindowTitle( qtr( "VLC media player" ) );
1153     }
1154     else
1155     {
1156         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
1157     }
1158 }
1159
1160 void MainInterface::showCryptedLabel( bool b_show )
1161 {
1162     if( cryptedLabel == NULL )
1163     {
1164         cryptedLabel = new QLabel;
1165         // The lock icon is not the right one for DRM protection/scrambled.
1166         //cryptedLabel->setPixmap( QPixmap( ":/lock" ) );
1167         cryptedLabel->setText( "DRM" );
1168         statusBar()->addWidget( cryptedLabel );
1169     }
1170
1171     cryptedLabel->setVisible( b_show );
1172 }
1173
1174 /*****************************************************************************
1175  * Systray Icon and Systray Menu
1176  *****************************************************************************/
1177 #ifndef HAVE_MAEMO
1178 /**
1179  * Create a SystemTray icon and a menu that would go with it.
1180  * Connects to a click handler on the icon.
1181  **/
1182 void MainInterface::createSystray()
1183 {
1184     QIcon iconVLC;
1185     if( QDate::currentDate().dayOfYear() >= 354 )
1186         iconVLC =  QIcon( ":/logo/vlc128-christmas.png" );
1187     else
1188         iconVLC =  QIcon( ":/logo/vlc128.png" );
1189     sysTray = new QSystemTrayIcon( iconVLC, this );
1190     sysTray->setToolTip( qtr( "VLC media player" ));
1191
1192     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
1193     systrayMenu->setIcon( iconVLC );
1194
1195     QVLCMenu::updateSystrayMenu( this, p_intf, true );
1196     sysTray->show();
1197
1198     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
1199             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
1200 }
1201
1202 /**
1203  * Updates the Systray Icon's menu and toggle the main interface
1204  */
1205 void MainInterface::toggleUpdateSystrayMenu()
1206 {
1207     /* If hidden, show it */
1208     if( isHidden() )
1209     {
1210         show();
1211         activateWindow();
1212     }
1213     else if( isMinimized() )
1214     {
1215         /* Minimized */
1216         showNormal();
1217         activateWindow();
1218     }
1219     else
1220     {
1221         /* Visible (possibly under other windows) */
1222 #ifdef WIN32
1223         /* check if any visible window is above vlc in the z-order,
1224          * but ignore the ones always on top
1225          * and the ones which can't be activated */
1226         WINDOWINFO wi;
1227         HWND hwnd;
1228         wi.cbSize = sizeof( WINDOWINFO );
1229         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
1230                 hwnd && ( !IsWindowVisible( hwnd ) ||
1231                     ( GetWindowInfo( hwnd, &wi ) &&
1232                       (wi.dwExStyle&WS_EX_NOACTIVATE) ) );
1233                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
1234             if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
1235                 (wi.dwExStyle&WS_EX_TOPMOST) )
1236             {
1237                 hide();
1238             }
1239             else
1240             {
1241                 activateWindow();
1242             }
1243 #else
1244         hide();
1245 #endif
1246     }
1247     QVLCMenu::updateSystrayMenu( this, p_intf );
1248 }
1249
1250 void MainInterface::handleSystrayClick(
1251                                     QSystemTrayIcon::ActivationReason reason )
1252 {
1253     switch( reason )
1254     {
1255         case QSystemTrayIcon::Trigger:
1256         case QSystemTrayIcon::DoubleClick:
1257             toggleUpdateSystrayMenu();
1258             break;
1259         case QSystemTrayIcon::MiddleClick:
1260             sysTray->showMessage( qtr( "VLC media player" ),
1261                     qtr( "Control menu for the player" ),
1262                     QSystemTrayIcon::Information, 3000 );
1263             break;
1264         default:
1265             break;
1266     }
1267 }
1268
1269 /**
1270  * Updates the name of the systray Icon tooltip.
1271  * Doesn't check if the systray exists, check before you call it.
1272  **/
1273 void MainInterface::updateSystrayTooltipName( const QString& name )
1274 {
1275     if( name.isEmpty() )
1276     {
1277         sysTray->setToolTip( qtr( "VLC media player" ) );
1278     }
1279     else
1280     {
1281         sysTray->setToolTip( name );
1282         if( notificationEnabled && ( isHidden() || isMinimized() ) )
1283         {
1284             sysTray->showMessage( qtr( "VLC media player" ), name,
1285                     QSystemTrayIcon::NoIcon, 3000 );
1286         }
1287     }
1288
1289     QVLCMenu::updateSystrayMenu( this, p_intf );
1290 }
1291
1292 /**
1293  * Updates the status of the systray Icon tooltip.
1294  * Doesn't check if the systray exists, check before you call it.
1295  **/
1296 void MainInterface::updateSystrayTooltipStatus( int i_status )
1297 {
1298     switch( i_status )
1299     {
1300         case  0:
1301         case  END_S:
1302             {
1303                 sysTray->setToolTip( qtr( "VLC media player" ) );
1304                 break;
1305             }
1306         case PLAYING_S:
1307             {
1308                 sysTray->setToolTip( input_name );
1309                 break;
1310             }
1311         case PAUSE_S:
1312             {
1313                 sysTray->setToolTip( input_name + " - "
1314                         + qtr( "Paused") );
1315                 break;
1316             }
1317     }
1318     QVLCMenu::updateSystrayMenu( this, p_intf );
1319 }
1320 #endif
1321
1322 /************************************************************************
1323  * D&D Events
1324  ************************************************************************/
1325 void MainInterface::dropEvent(QDropEvent *event)
1326 {
1327     dropEventPlay( event, true );
1328 }
1329
1330 void MainInterface::dropEventPlay( QDropEvent *event, bool b_play )
1331 {
1332     event->setDropAction( Qt::CopyAction );
1333     if( !event->possibleActions() & Qt::CopyAction )
1334         return;
1335
1336     const QMimeData *mimeData = event->mimeData();
1337
1338     /* D&D of a subtitles file, add it on the fly */
1339     if( mimeData->urls().size() == 1 && THEMIM->getIM()->hasInput() )
1340     {
1341         if( !input_AddSubtitle( THEMIM->getInput(),
1342                  qtu( toNativeSeparators( mimeData->urls()[0].toLocalFile() ) ),
1343                  true ) )
1344         {
1345             event->accept();
1346             return;
1347         }
1348     }
1349
1350     bool first = b_play;
1351     foreach( const QUrl &url, mimeData->urls() )
1352     {
1353         QString s = toNativeSeparators( url.toLocalFile() );
1354
1355         if( s.length() > 0 ) {
1356             char* psz_uri = make_URI( qtu(s) );
1357             playlist_Add( THEPL, psz_uri, NULL,
1358                           PLAYLIST_APPEND | (first ? PLAYLIST_GO: PLAYLIST_PREPARSE),
1359                           PLAYLIST_END, true, pl_Unlocked );
1360             free( psz_uri );
1361             first = false;
1362             RecentsMRL::getInstance( p_intf )->addRecent( s );
1363         }
1364     }
1365     event->accept();
1366 }
1367 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1368 {
1369      event->acceptProposedAction();
1370 }
1371 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1372 {
1373      event->acceptProposedAction();
1374 }
1375 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1376 {
1377      event->accept();
1378 }
1379
1380 /************************************************************************
1381  * Events stuff
1382  ************************************************************************/
1383 void MainInterface::customEvent( QEvent *event )
1384 {
1385 #if 0
1386     if( event->type() == PLDockEvent_Type )
1387     {
1388         PlaylistDialog::killInstance();
1389         playlistEmbeddedFlag = true;
1390         menuBar()->clear();
1391         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1392         togglePlaylist();
1393     }
1394 #endif
1395     /*else */
1396     if ( event->type() == (int)SetVideoOnTopEvent_Type )
1397     {
1398         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1399         if( p_event->OnTop() )
1400             setWindowFlags( windowFlags() | Qt::WindowStaysOnTopHint );
1401         else
1402             setWindowFlags( windowFlags() & ~Qt::WindowStaysOnTopHint );
1403         show(); /* necessary to apply window flags */
1404     }
1405 }
1406
1407 void MainInterface::keyPressEvent( QKeyEvent *e )
1408 {
1409     handleKeyPress( e );
1410 }
1411
1412 void MainInterface::handleKeyPress( QKeyEvent *e )
1413 {
1414     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() == Qt::Key_H )
1415           && !menuBar()->isVisible() )
1416     {
1417         toggleMinimalView( false );
1418         e->accept();
1419     }
1420
1421     int i_vlck = qtEventToVLCKey( e );
1422     if( i_vlck > 0 )
1423     {
1424         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1425         e->accept();
1426     }
1427     else
1428         e->ignore();
1429 }
1430
1431 void MainInterface::resizeEvent( QResizeEvent * event )
1432 {
1433 #if 0
1434     if( b_keep_size )
1435     {
1436         if( i_visualmode == QT_ALWAYS_VIDEO_MODE ||
1437             i_visualmode == QT_MINIMAL_MODE )
1438         {
1439                 mainVideoSize = size();
1440         }
1441         else
1442         {
1443             if( VISIBLE( bgWidget) ||
1444                 ( videoIsActive && videoWidget->isVisible() )
1445               )
1446                 mainVideoSize = size();
1447             else
1448                 mainBasedSize = size();
1449         }
1450     }
1451 #endif
1452     QVLCMW::resizeEvent( event );
1453     msg_Dbg( p_intf, "Resize Event, height: %i", size().height() );
1454 }
1455
1456 void MainInterface::wheelEvent( QWheelEvent *e )
1457 {
1458     int i_vlckey = qtWheelEventToVLCKey( e );
1459     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1460     e->accept();
1461 }
1462
1463 void MainInterface::closeEvent( QCloseEvent *e )
1464 {
1465     e->accept();
1466     hide();
1467     THEDP->quit();
1468 }
1469
1470 void MainInterface::toggleFullScreen( void )
1471 {
1472     if( isFullScreen() )
1473     {
1474         showNormal();
1475         emit askUpdate(); // Needed if video was launched after the F11
1476         emit fullscreenInterfaceToggled( false );
1477     }
1478     else
1479     {
1480         showFullScreen();
1481         emit fullscreenInterfaceToggled( true );
1482     }
1483
1484 }
1485
1486 //moc doesn't know about #ifdef, so we have to build this method for every platform
1487 void MainInterface::changeThumbbarButtons( int i_status)
1488 {
1489 #ifdef WIN32
1490     // Define an array of three buttons. These buttons provide images through an
1491     // image list and also provide tooltips.
1492     DWORD dwMask = THB_BITMAP | THB_FLAGS;
1493
1494     THUMBBUTTON thbButtons[3];
1495     //prev
1496     thbButtons[0].dwMask = dwMask;
1497     thbButtons[0].iId = 0;
1498     thbButtons[0].iBitmap = 0;
1499
1500     //play/pause
1501     thbButtons[1].dwMask = dwMask;
1502     thbButtons[1].iId = 1;
1503
1504     //next
1505     thbButtons[2].dwMask = dwMask;
1506     thbButtons[2].iId = 2;
1507     thbButtons[2].iBitmap = 3;
1508
1509     switch( i_status )
1510     {
1511         case PLAYING_S:
1512             {
1513                 thbButtons[0].dwFlags = THBF_ENABLED;
1514                 thbButtons[1].dwFlags = THBF_ENABLED;
1515                 thbButtons[2].dwFlags = THBF_ENABLED;
1516                 thbButtons[1].iBitmap = 1;
1517                 break;
1518             }
1519         case PAUSE_S:
1520             {
1521                 thbButtons[0].dwFlags = THBF_ENABLED;
1522                 thbButtons[1].dwFlags = THBF_ENABLED;
1523                 thbButtons[2].dwFlags = THBF_ENABLED;
1524                 thbButtons[1].iBitmap = 2;
1525                 break;
1526             }
1527         default:
1528             return;
1529     }
1530     HRESULT hr =  p_taskbl->vt->ThumbBarUpdateButtons(p_taskbl, this->winId(), 3, thbButtons);
1531     if(S_OK != hr)
1532         msg_Err( p_intf, "ThumbBarUpdateButtons failed with error %08x", hr );
1533 #else
1534     ;
1535 #endif
1536 }
1537
1538 /*****************************************************************************
1539  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1540  *  We don't show the menu directly here because we don't want the
1541  *  caller to block for a too long time.
1542  *****************************************************************************/
1543 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1544                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1545 {
1546     intf_thread_t *p_intf = (intf_thread_t *)param;
1547
1548     if( p_intf->pf_show_dialog )
1549     {
1550         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1551                                 new_val.b_bool, NULL );
1552     }
1553
1554     return VLC_SUCCESS;
1555 }
1556
1557 /*****************************************************************************
1558  * IntfShowCB: callback triggered by the intf-show libvlc variable.
1559  *****************************************************************************/
1560 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1561                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1562 {
1563     intf_thread_t *p_intf = (intf_thread_t *)param;
1564     p_intf->p_sys->p_mi->toggleFSC();
1565
1566     /* Show event */
1567      return VLC_SUCCESS;
1568 }