gui: qt: use float for rate
[vlc.git] / modules / gui / qt / main_interface_win32.cpp
blob0f18da76adb9de184013a907472b615d0dfcd776
1 /*****************************************************************************
2 * main_interface_win32.cpp : Main interface
3 ****************************************************************************
4 * Copyright (C) 2006-2010 VideoLAN and AUTHORS
6 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
7 * Hugo Beauzée-Luyssen <hugo@beauzee.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #if HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include "main_interface_win32.hpp"
30 #include "input_manager.hpp"
31 #include "actions_manager.hpp"
32 #include "dialogs_provider.hpp"
33 #include "components/interface_widgets.hpp"
35 #include <QBitmap>
37 #include <assert.h>
39 #include <QWindow>
40 #include <qpa/qplatformnativeinterface.h>
42 #define WM_APPCOMMAND 0x0319
44 #define APPCOMMAND_VOLUME_MUTE 8
45 #define APPCOMMAND_VOLUME_DOWN 9
46 #define APPCOMMAND_VOLUME_UP 10
47 #define APPCOMMAND_MEDIA_NEXTTRACK 11
48 #define APPCOMMAND_MEDIA_PREVIOUSTRACK 12
49 #define APPCOMMAND_MEDIA_STOP 13
50 #define APPCOMMAND_MEDIA_PLAY_PAUSE 14
51 #define APPCOMMAND_LAUNCH_MEDIA_SELECT 16
52 #define APPCOMMAND_BASS_DOWN 19
53 #define APPCOMMAND_BASS_BOOST 20
54 #define APPCOMMAND_BASS_UP 21
55 #define APPCOMMAND_TREBLE_DOWN 22
56 #define APPCOMMAND_TREBLE_UP 23
57 #define APPCOMMAND_MICROPHONE_VOLUME_MUTE 24
58 #define APPCOMMAND_MICROPHONE_VOLUME_DOWN 25
59 #define APPCOMMAND_MICROPHONE_VOLUME_UP 26
60 #define APPCOMMAND_HELP 27
61 #define APPCOMMAND_OPEN 30
62 #define APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE 43
63 #define APPCOMMAND_MIC_ON_OFF_TOGGLE 44
64 #define APPCOMMAND_MEDIA_PLAY 46
65 #define APPCOMMAND_MEDIA_PAUSE 47
66 #define APPCOMMAND_MEDIA_RECORD 48
67 #define APPCOMMAND_MEDIA_FAST_FORWARD 49
68 #define APPCOMMAND_MEDIA_REWIND 50
69 #define APPCOMMAND_MEDIA_CHANNEL_UP 51
70 #define APPCOMMAND_MEDIA_CHANNEL_DOWN 52
72 #define FAPPCOMMAND_MOUSE 0x8000
73 #define FAPPCOMMAND_KEY 0
74 #define FAPPCOMMAND_OEM 0x1000
75 #define FAPPCOMMAND_MASK 0xF000
77 #define GET_APPCOMMAND_LPARAM(lParam) ((short)(HIWORD(lParam) & ~FAPPCOMMAND_MASK))
78 #define GET_DEVICE_LPARAM(lParam) ((WORD)(HIWORD(lParam) & FAPPCOMMAND_MASK))
79 #define GET_MOUSEORKEY_LPARAM GET_DEVICE_LPARAM
80 #define GET_FLAGS_LPARAM(lParam) (LOWORD(lParam))
81 #define GET_KEYSTATE_LPARAM(lParam) GET_FLAGS_LPARAM(lParam)
83 MainInterfaceWin32::MainInterfaceWin32( intf_thread_t *_p_intf )
84 : MainInterface( _p_intf )
85 , himl( NULL )
86 , p_taskbl( NULL )
88 /* Volume keys */
89 _p_intf->p_sys->disable_volume_keys = var_InheritBool( _p_intf, "qt-disable-volume-keys" );
90 taskbar_wmsg = RegisterWindowMessage(TEXT("TaskbarButtonCreated"));
91 if (taskbar_wmsg == 0)
92 msg_Warn( p_intf, "Failed to register TaskbarButtonCreated message" );
95 MainInterfaceWin32::~MainInterfaceWin32()
97 if( himl )
98 ImageList_Destroy( himl );
99 if(p_taskbl)
100 p_taskbl->Release();
101 CoUninitialize();
104 HWND MainInterfaceWin32::WinId( QWidget *w )
106 if( w && w->windowHandle() )
107 return static_cast<HWND>(QGuiApplication::platformNativeInterface()->
108 nativeResourceForWindow("handle", w->windowHandle()));
109 else
110 return 0;
113 Q_GUI_EXPORT HBITMAP qt_pixmapToWinHBITMAP(const QPixmap &p, int hbitmapFormat = 0);
115 enum HBitmapFormat
117 NoAlpha,
118 PremultipliedAlpha,
119 Alpha
122 void MainInterfaceWin32::createTaskBarButtons()
124 /*Here is the code for the taskbar thumb buttons
125 FIXME:We need pretty buttons in 16x16 px that are handled correctly by masks in Qt
127 p_taskbl = NULL;
128 himl = NULL;
130 HRESULT hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
131 if( FAILED(hr) )
132 return;
134 void *pv;
135 hr = CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
136 IID_ITaskbarList3, &pv);
137 if( FAILED(hr) )
139 CoUninitialize();
140 return;
143 p_taskbl = (ITaskbarList3 *)pv;
144 p_taskbl->HrInit();
146 int iconX = GetSystemMetrics(SM_CXSMICON);
147 int iconY = GetSystemMetrics(SM_CYSMICON);
148 himl = ImageList_Create( iconX /*cx*/, iconY /*cy*/, ILC_COLOR32 /*flags*/,
149 4 /*cInitial*/, 0 /*cGrow*/);
150 if( himl == NULL )
152 p_taskbl->Release();
153 p_taskbl = NULL;
154 CoUninitialize();
155 return;
158 QPixmap img = QPixmap(":/win7/prev.svg").scaled( iconX, iconY );
159 QPixmap img2 = QPixmap(":/win7/pause.svg").scaled( iconX, iconY );
160 QPixmap img3 = QPixmap(":/win7/play.svg").scaled( iconX, iconY );
161 QPixmap img4 = QPixmap(":/win7/next.svg").scaled( iconX, iconY );
162 QBitmap mask = img.createMaskFromColor(Qt::transparent);
163 QBitmap mask2 = img2.createMaskFromColor(Qt::transparent);
164 QBitmap mask3 = img3.createMaskFromColor(Qt::transparent);
165 QBitmap mask4 = img4.createMaskFromColor(Qt::transparent);
167 if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask)))
168 msg_Err( p_intf, "%s ImageList_Add failed", "First" );
169 if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img2, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask2)))
170 msg_Err( p_intf, "%s ImageList_Add failed", "Second" );
171 if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img3, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask3)))
172 msg_Err( p_intf, "%s ImageList_Add failed", "Third" );
173 if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img4, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask4)))
174 msg_Err( p_intf, "%s ImageList_Add failed", "Fourth" );
176 // Define an array of two buttons. These buttons provide images through an
177 // image list and also provide tooltips.
178 THUMBBUTTONMASK dwMask = THUMBBUTTONMASK(THB_BITMAP | THB_FLAGS);
179 THUMBBUTTON thbButtons[3];
181 thbButtons[0].dwMask = dwMask;
182 thbButtons[0].iId = 0;
183 thbButtons[0].iBitmap = 0;
184 thbButtons[0].dwFlags = THEPL->items.i_size > 1 ? THBF_ENABLED : THBF_HIDDEN;
186 thbButtons[1].dwMask = dwMask;
187 thbButtons[1].iId = 1;
188 thbButtons[1].iBitmap = 2;
189 thbButtons[1].dwFlags = THEPL->items.i_size > 0 ? THBF_ENABLED : THBF_HIDDEN;
191 thbButtons[2].dwMask = dwMask;
192 thbButtons[2].iId = 2;
193 thbButtons[2].iBitmap = 3;
194 thbButtons[2].dwFlags = THEPL->items.i_size > 1 ? THBF_ENABLED : THBF_HIDDEN;
196 hr = p_taskbl->ThumbBarSetImageList( WinId(this), himl );
197 if( FAILED(hr) )
198 msg_Err( p_intf, "%s failed with error %08lx", "ThumbBarSetImageList",
199 hr );
200 else
202 hr = p_taskbl->ThumbBarAddButtons( WinId(this), 3, thbButtons);
203 if( FAILED(hr) )
204 msg_Err( p_intf, "%s failed with error %08lx",
205 "ThumbBarAddButtons", hr );
207 CONNECT( THEMIM->getIM(), playingStatusChanged( int ),
208 this, changeThumbbarButtons( int ) );
209 CONNECT( THEMIM, playlistItemAppended( int, int ),
210 this, playlistItemAppended( int, int ) );
211 CONNECT( THEMIM, playlistItemRemoved( int ),
212 this, playlistItemRemoved( int ) );
213 if( THEMIM->getIM()->playingStatus() == PLAYING_S )
214 changeThumbbarButtons( THEMIM->getIM()->playingStatus() );
217 bool MainInterfaceWin32::nativeEvent(const QByteArray &, void *message, long *result)
219 return winEvent( static_cast<MSG*>( message ), result );
222 bool MainInterfaceWin32::winEvent ( MSG * msg, long * result )
224 if (msg->message == taskbar_wmsg)
226 //We received the taskbarbuttoncreated, now we can really create the buttons
227 createTaskBarButtons();
230 short cmd;
231 switch( msg->message )
233 case WM_COMMAND:
234 if (HIWORD(msg->wParam) == THBN_CLICKED)
236 switch(LOWORD(msg->wParam))
238 case 0:
239 THEMIM->prev();
240 break;
241 case 1:
242 THEMIM->togglePlayPause();
243 break;
244 case 2:
245 THEMIM->next();
246 break;
249 break;
250 case WM_APPCOMMAND:
251 cmd = GET_APPCOMMAND_LPARAM(msg->lParam);
253 if( p_intf->p_sys->disable_volume_keys &&
254 ( cmd == APPCOMMAND_VOLUME_DOWN ||
255 cmd == APPCOMMAND_VOLUME_UP ||
256 cmd == APPCOMMAND_VOLUME_MUTE ) )
258 break;
261 *result = TRUE;
263 switch(cmd)
265 case APPCOMMAND_MEDIA_PLAY_PAUSE:
266 THEMIM->togglePlayPause();
267 break;
268 case APPCOMMAND_MEDIA_PLAY:
269 THEMIM->play();
270 break;
271 case APPCOMMAND_MEDIA_PAUSE:
272 THEMIM->pause();
273 break;
274 case APPCOMMAND_MEDIA_CHANNEL_DOWN:
275 case APPCOMMAND_MEDIA_PREVIOUSTRACK:
276 THEMIM->prev();
277 break;
278 case APPCOMMAND_MEDIA_CHANNEL_UP:
279 case APPCOMMAND_MEDIA_NEXTTRACK:
280 THEMIM->next();
281 break;
282 case APPCOMMAND_MEDIA_STOP:
283 THEMIM->stop();
284 break;
285 case APPCOMMAND_MEDIA_RECORD:
286 THEAM->record();
287 break;
288 case APPCOMMAND_VOLUME_DOWN:
289 THEAM->AudioDown();
290 break;
291 case APPCOMMAND_VOLUME_UP:
292 THEAM->AudioUp();
293 break;
294 case APPCOMMAND_VOLUME_MUTE:
295 THEAM->toggleMuteAudio();
296 break;
297 case APPCOMMAND_MEDIA_FAST_FORWARD:
298 THEMIM->getIM()->faster();
299 break;
300 case APPCOMMAND_MEDIA_REWIND:
301 THEMIM->getIM()->slower();
302 break;
303 case APPCOMMAND_HELP:
304 THEDP->mediaInfoDialog();
305 break;
306 case APPCOMMAND_OPEN:
307 THEDP->simpleOpenDialog();
308 break;
309 default:
310 msg_Dbg( p_intf, "unknown APPCOMMAND = %d", cmd);
311 *result = FALSE;
312 break;
314 if (*result) return true;
315 break;
317 return false;
320 void MainInterfaceWin32::setVideoFullScreen( bool fs )
322 MainInterface::setVideoFullScreen( fs );
323 if( !fs )
324 changeThumbbarButtons( THEMIM->getIM()->playingStatus() );
327 void MainInterfaceWin32::toggleUpdateSystrayMenuWhenVisible()
329 /* check if any visible window is above vlc in the z-order,
330 * but ignore the ones always on top
331 * and the ones which can't be activated */
332 HWND winId;
333 QWindow *window = windowHandle();
334 winId = static_cast<HWND>(QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", window));
336 WINDOWINFO wi;
337 HWND hwnd;
338 wi.cbSize = sizeof( WINDOWINFO );
339 for( hwnd = GetNextWindow( winId, GW_HWNDPREV );
340 hwnd && ( !IsWindowVisible( hwnd ) || ( GetWindowInfo( hwnd, &wi ) &&
341 ( wi.dwExStyle&WS_EX_NOACTIVATE ) ) );
342 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) )
345 if( !hwnd || !GetWindowInfo( hwnd, &wi ) || (wi.dwExStyle&WS_EX_TOPMOST) )
346 hide();
347 else
348 activateWindow();
352 void MainInterfaceWin32::resizeEvent(QResizeEvent *event)
354 QWidget::resizeEvent(event);
357 * Detects if window placement is not in its normal position (ex: win7 aero snap)
358 * This function compares the normal position (non snapped) to the current position.
359 * The current position is translated from screen referential to workspace referential
360 * to workspace referential
362 b_isWindowTiled = false;
363 HWND winHwnd = WinId( this );
365 WINDOWPLACEMENT windowPlacement;
366 windowPlacement.length = sizeof( windowPlacement );
367 if ( GetWindowPlacement( winHwnd, &windowPlacement ) == 0 )
368 return;
370 if ( windowPlacement.showCmd != SW_SHOWNORMAL )
371 return;
373 HMONITOR monitor = MonitorFromWindow( winHwnd, MONITOR_DEFAULTTONEAREST );
375 MONITORINFO monitorInfo;
376 monitorInfo.cbSize = sizeof( monitorInfo );
377 if ( GetMonitorInfo( monitor, &monitorInfo ) == 0 )
378 return;
380 RECT windowRect;
381 if ( GetWindowRect( winHwnd, &windowRect ) == 0 )
382 return;
384 OffsetRect( &windowRect,
385 monitorInfo.rcMonitor.left - monitorInfo.rcWork.left ,
386 monitorInfo.rcMonitor.top - monitorInfo.rcWork.top );
388 b_isWindowTiled = ( EqualRect( &windowPlacement.rcNormalPosition, &windowRect ) == 0 );
391 void MainInterfaceWin32::reloadPrefs()
393 p_intf->p_sys->disable_volume_keys = var_InheritBool( p_intf, "qt-disable-volume-keys" );
394 MainInterface::reloadPrefs();
397 void MainInterfaceWin32::playlistItemAppended( int, int )
399 changeThumbbarButtons( THEMIM->getIM()->playingStatus() );
402 void MainInterfaceWin32::playlistItemRemoved( int )
404 changeThumbbarButtons( THEMIM->getIM()->playingStatus() );
407 void MainInterfaceWin32::changeThumbbarButtons( int i_status )
409 if( p_taskbl == NULL )
410 return;
412 // Define an array of three buttons. These buttons provide images through an
413 // image list and also provide tooltips.
414 THUMBBUTTONMASK dwMask = THUMBBUTTONMASK(THB_BITMAP | THB_FLAGS);
416 THUMBBUTTON thbButtons[3];
417 //prev
418 thbButtons[0].dwMask = dwMask;
419 thbButtons[0].iId = 0;
420 thbButtons[0].iBitmap = 0;
421 thbButtons[0].dwFlags = THEPL->items.i_size > 1 ? THBF_ENABLED : THBF_HIDDEN;
423 //play/pause
424 thbButtons[1].dwMask = dwMask;
425 thbButtons[1].iId = 1;
426 thbButtons[1].dwFlags = THBF_ENABLED;
428 //next
429 thbButtons[2].dwMask = dwMask;
430 thbButtons[2].iId = 2;
431 thbButtons[2].iBitmap = 3;
432 thbButtons[2].dwFlags = THEPL->items.i_size > 1 ? THBF_ENABLED : THBF_HIDDEN;
434 switch( i_status )
436 case OPENING_S:
437 case PLAYING_S:
439 thbButtons[1].iBitmap = 1;
440 break;
442 case END_S:
443 case PAUSE_S:
444 case ERROR_S:
446 thbButtons[1].iBitmap = 2;
447 break;
449 default:
450 return;
453 HRESULT hr = p_taskbl->ThumbBarUpdateButtons(WinId(this), 3, thbButtons);
455 if(S_OK != hr)
456 msg_Err( p_intf, "ThumbBarUpdateButtons failed with error %08lx", hr );
458 // If a video is playing, let the vout handle the thumbnail.
459 if( !videoWidget || !THEMIM->getIM()->hasVideo() )
461 hr = p_taskbl->SetThumbnailClip(WinId(this), NULL);
462 if(S_OK != hr)
463 msg_Err( p_intf, "SetThumbnailClip failed with error %08lx", hr );