qt: move the main interface to its own folder
[vlc.git] / modules / gui / qt / maininterface / main_interface_win32.cpp
blobaad0c7e33e4ea30daf433045e2340bba40b99ad1
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 "player/player_controller.hpp"
31 #include "dialogs/dialogs_provider.hpp"
32 #include "components/interface_widgets.hpp"
34 #include <QBitmap>
36 #include <assert.h>
38 #include <QWindow>
39 #include <qpa/qplatformnativeinterface.h>
41 #define WM_APPCOMMAND 0x0319
43 #define APPCOMMAND_VOLUME_MUTE 8
44 #define APPCOMMAND_VOLUME_DOWN 9
45 #define APPCOMMAND_VOLUME_UP 10
46 #define APPCOMMAND_MEDIA_NEXTTRACK 11
47 #define APPCOMMAND_MEDIA_PREVIOUSTRACK 12
48 #define APPCOMMAND_MEDIA_STOP 13
49 #define APPCOMMAND_MEDIA_PLAY_PAUSE 14
50 #define APPCOMMAND_LAUNCH_MEDIA_SELECT 16
51 #define APPCOMMAND_BASS_DOWN 19
52 #define APPCOMMAND_BASS_BOOST 20
53 #define APPCOMMAND_BASS_UP 21
54 #define APPCOMMAND_TREBLE_DOWN 22
55 #define APPCOMMAND_TREBLE_UP 23
56 #define APPCOMMAND_MICROPHONE_VOLUME_MUTE 24
57 #define APPCOMMAND_MICROPHONE_VOLUME_DOWN 25
58 #define APPCOMMAND_MICROPHONE_VOLUME_UP 26
59 #define APPCOMMAND_HELP 27
60 #define APPCOMMAND_OPEN 30
61 #define APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE 43
62 #define APPCOMMAND_MIC_ON_OFF_TOGGLE 44
63 #define APPCOMMAND_MEDIA_PLAY 46
64 #define APPCOMMAND_MEDIA_PAUSE 47
65 #define APPCOMMAND_MEDIA_RECORD 48
66 #define APPCOMMAND_MEDIA_FAST_FORWARD 49
67 #define APPCOMMAND_MEDIA_REWIND 50
68 #define APPCOMMAND_MEDIA_CHANNEL_UP 51
69 #define APPCOMMAND_MEDIA_CHANNEL_DOWN 52
71 #define FAPPCOMMAND_MOUSE 0x8000
72 #define FAPPCOMMAND_KEY 0
73 #define FAPPCOMMAND_OEM 0x1000
74 #define FAPPCOMMAND_MASK 0xF000
76 #define GET_APPCOMMAND_LPARAM(lParam) ((short)(HIWORD(lParam) & ~FAPPCOMMAND_MASK))
77 #define GET_DEVICE_LPARAM(lParam) ((WORD)(HIWORD(lParam) & FAPPCOMMAND_MASK))
78 #define GET_MOUSEORKEY_LPARAM GET_DEVICE_LPARAM
79 #define GET_FLAGS_LPARAM(lParam) (LOWORD(lParam))
80 #define GET_KEYSTATE_LPARAM(lParam) GET_FLAGS_LPARAM(lParam)
82 MainInterfaceWin32::MainInterfaceWin32( intf_thread_t *_p_intf )
83 : MainInterface( _p_intf )
84 , himl( NULL )
85 , p_taskbl( NULL )
87 /* Volume keys */
88 _p_intf->p_sys->disable_volume_keys = var_InheritBool( _p_intf, "qt-disable-volume-keys" );
89 taskbar_wmsg = RegisterWindowMessage(TEXT("TaskbarButtonCreated"));
90 if (taskbar_wmsg == 0)
91 msg_Warn( p_intf, "Failed to register TaskbarButtonCreated message" );
94 MainInterfaceWin32::~MainInterfaceWin32()
96 if( himl )
97 ImageList_Destroy( himl );
98 if(p_taskbl)
99 p_taskbl->Release();
100 CoUninitialize();
103 HWND MainInterfaceWin32::WinId( QWidget *w )
105 if( w && w->windowHandle() )
106 return static_cast<HWND>(QGuiApplication::platformNativeInterface()->
107 nativeResourceForWindow("handle", w->windowHandle()));
108 else
109 return 0;
112 Q_GUI_EXPORT HBITMAP qt_pixmapToWinHBITMAP(const QPixmap &p, int hbitmapFormat = 0);
114 enum HBitmapFormat
116 NoAlpha,
117 PremultipliedAlpha,
118 Alpha
121 void MainInterfaceWin32::createTaskBarButtons()
123 /*Here is the code for the taskbar thumb buttons
124 FIXME:We need pretty buttons in 16x16 px that are handled correctly by masks in Qt
126 p_taskbl = NULL;
127 himl = NULL;
129 HRESULT hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
130 if( FAILED(hr) )
131 return;
133 void *pv;
134 hr = CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
135 IID_ITaskbarList3, &pv);
136 if( FAILED(hr) )
138 CoUninitialize();
139 return;
142 p_taskbl = (ITaskbarList3 *)pv;
143 p_taskbl->HrInit();
145 int iconX = GetSystemMetrics(SM_CXSMICON);
146 int iconY = GetSystemMetrics(SM_CYSMICON);
147 himl = ImageList_Create( iconX /*cx*/, iconY /*cy*/, ILC_COLOR32 /*flags*/,
148 4 /*cInitial*/, 0 /*cGrow*/);
149 if( himl == NULL )
151 p_taskbl->Release();
152 p_taskbl = NULL;
153 CoUninitialize();
154 return;
157 QPixmap img = QPixmap(":/win7/prev.svg").scaled( iconX, iconY );
158 QPixmap img2 = QPixmap(":/win7/pause.svg").scaled( iconX, iconY );
159 QPixmap img3 = QPixmap(":/win7/play.svg").scaled( iconX, iconY );
160 QPixmap img4 = QPixmap(":/win7/next.svg").scaled( iconX, iconY );
161 QBitmap mask = img.createMaskFromColor(Qt::transparent);
162 QBitmap mask2 = img2.createMaskFromColor(Qt::transparent);
163 QBitmap mask3 = img3.createMaskFromColor(Qt::transparent);
164 QBitmap mask4 = img4.createMaskFromColor(Qt::transparent);
166 if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask)))
167 msg_Err( p_intf, "%s ImageList_Add failed", "First" );
168 if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img2, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask2)))
169 msg_Err( p_intf, "%s ImageList_Add failed", "Second" );
170 if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img3, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask3)))
171 msg_Err( p_intf, "%s ImageList_Add failed", "Third" );
172 if( -1 == ImageList_Add(himl, qt_pixmapToWinHBITMAP(img4, PremultipliedAlpha), qt_pixmapToWinHBITMAP(mask4)))
173 msg_Err( p_intf, "%s ImageList_Add failed", "Fourth" );
175 // Define an array of two buttons. These buttons provide images through an
176 // image list and also provide tooltips.
177 THUMBBUTTONMASK dwMask = THUMBBUTTONMASK(THB_BITMAP | THB_FLAGS);
178 THUMBBUTTON thbButtons[3];
180 thbButtons[0].dwMask = dwMask;
181 thbButtons[0].iId = 0;
182 thbButtons[0].iBitmap = 0;
183 thbButtons[0].dwFlags = THEMPL->count() > 1 ? THBF_ENABLED : THBF_HIDDEN;
185 thbButtons[1].dwMask = dwMask;
186 thbButtons[1].iId = 1;
187 thbButtons[1].iBitmap = 2;
188 thbButtons[1].dwFlags = THEMPL->count() > 0 ? THBF_ENABLED : THBF_HIDDEN;
190 thbButtons[2].dwMask = dwMask;
191 thbButtons[2].iId = 2;
192 thbButtons[2].iBitmap = 3;
193 thbButtons[2].dwFlags = THEMPL->count() > 1 ? THBF_ENABLED : THBF_HIDDEN;
195 hr = p_taskbl->ThumbBarSetImageList( WinId(this), himl );
196 if( FAILED(hr) )
197 msg_Err( p_intf, "%s failed with error %08lx", "ThumbBarSetImageList",
198 hr );
199 else
201 hr = p_taskbl->ThumbBarAddButtons( WinId(this), 3, thbButtons);
202 if( FAILED(hr) )
203 msg_Err( p_intf, "%s failed with error %08lx",
204 "ThumbBarAddButtons", hr );
206 connect( THEMIM, &PlayerController::playingStateChanged,
207 this, &MainInterfaceWin32::changeThumbbarButtons);
208 connect( THEMPL, &vlc::playlist::PlaylistControllerModel::countChanged,
209 this, &MainInterfaceWin32::playlistItemCountChanged );
210 if( THEMIM->getPlayingState() == PlayerController::PLAYING_STATE_PLAYING )
211 changeThumbbarButtons( THEMIM->getPlayingState() );
214 bool MainInterfaceWin32::nativeEvent(const QByteArray &, void *message, long *result)
216 return winEvent( static_cast<MSG*>( message ), result );
219 bool MainInterfaceWin32::winEvent ( MSG * msg, long * result )
221 if (msg->message == taskbar_wmsg)
223 //We received the taskbarbuttoncreated, now we can really create the buttons
224 createTaskBarButtons();
227 short cmd;
228 switch( msg->message )
230 case WM_COMMAND:
231 if (HIWORD(msg->wParam) == THBN_CLICKED)
233 switch(LOWORD(msg->wParam))
235 case 0:
236 THEMPL->prev();
237 break;
238 case 1:
239 THEMPL->togglePlayPause();
240 break;
241 case 2:
242 THEMPL->next();
243 break;
246 break;
247 case WM_APPCOMMAND:
248 cmd = GET_APPCOMMAND_LPARAM(msg->lParam);
250 if( p_intf->p_sys->disable_volume_keys &&
251 ( cmd == APPCOMMAND_VOLUME_DOWN ||
252 cmd == APPCOMMAND_VOLUME_UP ||
253 cmd == APPCOMMAND_VOLUME_MUTE ) )
255 break;
258 *result = TRUE;
260 switch(cmd)
262 case APPCOMMAND_MEDIA_PLAY_PAUSE:
263 THEMPL->togglePlayPause();
264 break;
265 case APPCOMMAND_MEDIA_PLAY:
266 THEMPL->play();
267 break;
268 case APPCOMMAND_MEDIA_PAUSE:
269 THEMPL->pause();
270 break;
271 case APPCOMMAND_MEDIA_CHANNEL_DOWN:
272 case APPCOMMAND_MEDIA_PREVIOUSTRACK:
273 THEMPL->prev();
274 break;
275 case APPCOMMAND_MEDIA_CHANNEL_UP:
276 case APPCOMMAND_MEDIA_NEXTTRACK:
277 THEMPL->next();
278 break;
279 case APPCOMMAND_MEDIA_STOP:
280 THEMPL->stop();
281 break;
282 case APPCOMMAND_MEDIA_RECORD:
283 THEMIM->toggleRecord();
284 break;
285 case APPCOMMAND_VOLUME_DOWN:
286 THEMIM->setVolumeDown();
287 break;
288 case APPCOMMAND_VOLUME_UP:
289 THEMIM->setVolumeUp();
290 break;
291 case APPCOMMAND_VOLUME_MUTE:
292 THEMIM->toggleMuted();
293 break;
294 case APPCOMMAND_MEDIA_FAST_FORWARD:
295 THEMIM->faster();
296 break;
297 case APPCOMMAND_MEDIA_REWIND:
298 THEMIM->slower();
299 break;
300 case APPCOMMAND_HELP:
301 THEDP->mediaInfoDialog();
302 break;
303 case APPCOMMAND_OPEN:
304 THEDP->simpleOpenDialog();
305 break;
306 default:
307 msg_Dbg( p_intf, "unknown APPCOMMAND = %d", cmd);
308 *result = FALSE;
309 break;
311 if (*result) return true;
312 break;
314 return false;
317 void MainInterfaceWin32::setVideoFullScreen( bool fs )
319 MainInterface::setVideoFullScreen( fs );
320 if( !fs )
321 changeThumbbarButtons( THEMIM->getPlayingState() );
324 void MainInterfaceWin32::toggleUpdateSystrayMenuWhenVisible()
326 /* check if any visible window is above vlc in the z-order,
327 * but ignore the ones always on top
328 * and the ones which can't be activated */
329 HWND winId;
330 QWindow *window = windowHandle();
331 winId = static_cast<HWND>(QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", window));
333 WINDOWINFO wi;
334 HWND hwnd;
335 wi.cbSize = sizeof( WINDOWINFO );
336 for( hwnd = GetNextWindow( winId, GW_HWNDPREV );
337 hwnd && ( !IsWindowVisible( hwnd ) || ( GetWindowInfo( hwnd, &wi ) &&
338 ( wi.dwExStyle&WS_EX_NOACTIVATE ) ) );
339 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) )
342 if( !hwnd || !GetWindowInfo( hwnd, &wi ) || (wi.dwExStyle&WS_EX_TOPMOST) )
343 hide();
344 else
345 activateWindow();
349 void MainInterfaceWin32::resizeEvent(QResizeEvent *event)
351 QWidget::resizeEvent(event);
354 * Detects if window placement is not in its normal position (ex: win7 aero snap)
355 * This function compares the normal position (non snapped) to the current position.
356 * The current position is translated from screen referential to workspace referential
357 * to workspace referential
359 b_isWindowTiled = false;
360 HWND winHwnd = WinId( this );
362 WINDOWPLACEMENT windowPlacement;
363 windowPlacement.length = sizeof( windowPlacement );
364 if ( GetWindowPlacement( winHwnd, &windowPlacement ) == 0 )
365 return;
367 if ( windowPlacement.showCmd != SW_SHOWNORMAL )
368 return;
370 HMONITOR monitor = MonitorFromWindow( winHwnd, MONITOR_DEFAULTTONEAREST );
372 MONITORINFO monitorInfo;
373 monitorInfo.cbSize = sizeof( monitorInfo );
374 if ( GetMonitorInfo( monitor, &monitorInfo ) == 0 )
375 return;
377 RECT windowRect;
378 if ( GetWindowRect( winHwnd, &windowRect ) == 0 )
379 return;
381 OffsetRect( &windowRect,
382 monitorInfo.rcMonitor.left - monitorInfo.rcWork.left ,
383 monitorInfo.rcMonitor.top - monitorInfo.rcWork.top );
385 b_isWindowTiled = ( EqualRect( &windowPlacement.rcNormalPosition, &windowRect ) == 0 );
388 void MainInterfaceWin32::reloadPrefs()
390 p_intf->p_sys->disable_volume_keys = var_InheritBool( p_intf, "qt-disable-volume-keys" );
391 MainInterface::reloadPrefs();
394 void MainInterfaceWin32::playlistItemCountChanged( size_t )
396 changeThumbbarButtons( THEMIM->getPlayingState() );
399 void MainInterfaceWin32::changeThumbbarButtons( PlayerController::PlayingState i_status )
401 if( p_taskbl == NULL )
402 return;
404 // Define an array of three buttons. These buttons provide images through an
405 // image list and also provide tooltips.
406 THUMBBUTTONMASK dwMask = THUMBBUTTONMASK(THB_BITMAP | THB_FLAGS);
408 THUMBBUTTON thbButtons[3];
409 //prev
410 thbButtons[0].dwMask = dwMask;
411 thbButtons[0].iId = 0;
412 thbButtons[0].iBitmap = 0;
413 thbButtons[0].dwFlags = THEMPL->count() > 1 ? THBF_ENABLED : THBF_HIDDEN;
415 //play/pause
416 thbButtons[1].dwMask = dwMask;
417 thbButtons[1].iId = 1;
418 thbButtons[1].dwFlags = THBF_ENABLED;
420 //next
421 thbButtons[2].dwMask = dwMask;
422 thbButtons[2].iId = 2;
423 thbButtons[2].iBitmap = 3;
424 thbButtons[2].dwFlags = THEMPL->count() > 1 ? THBF_ENABLED : THBF_HIDDEN;
426 switch( i_status )
428 case PlayerController::PLAYING_STATE_PLAYING:
430 thbButtons[1].iBitmap = 1;
431 break;
433 case PlayerController::PLAYING_STATE_STARTED:
434 case PlayerController::PLAYING_STATE_PAUSED:
435 case PlayerController::PLAYING_STATE_STOPPING:
436 case PlayerController::PLAYING_STATE_STOPPED:
438 thbButtons[1].iBitmap = 2;
439 break;
441 default:
442 return;
445 HRESULT hr = p_taskbl->ThumbBarUpdateButtons(WinId(this), 3, thbButtons);
447 if(S_OK != hr)
448 msg_Err( p_intf, "ThumbBarUpdateButtons failed with error %08lx", hr );
450 // If a video is playing, let the vout handle the thumbnail.
451 //if( !videoWidget || !THEMIM->hasVideoOutput() )
453 // hr = p_taskbl->SetThumbnailClip(WinId(this), NULL);
454 // if(S_OK != hr)
455 // msg_Err( p_intf, "SetThumbnailClip failed with error %08lx", hr );