1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
27 #include "win32_factory.hpp"
28 #include "win32_graphics.hpp"
29 #include "win32_timer.hpp"
30 #include "win32_window.hpp"
31 #include "win32_tooltip.hpp"
32 #include "win32_popup.hpp"
33 #include "win32_loop.hpp"
34 #include "../src/theme.hpp"
35 #include "../src/window_manager.hpp"
36 #include "commands/cmd_dialogs.hpp"
38 // Custom message for the notifications of the system tray
39 #define MY_WSTRAYACTION (WM_APP + 1)
42 LRESULT CALLBACK Win32Proc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
44 // Get pointer to thread info: should only work with the parent window
45 intf_thread_t *p_intf = (intf_thread_t *)GetWindowLongPtr( hwnd,
48 // If doesn't exist, treat windows message normally
49 if( p_intf == NULL || p_intf->p_sys->p_osFactory == NULL )
51 return DefWindowProc( hwnd, uMsg, wParam, lParam );
54 // Here we know we are getting a message for the parent window, since it is
55 // the only one to store p_intf...
56 // Yes, it is a kludge :)
58 //Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( p_intf );
59 //msg_Err( p_intf, "Parent window %p %p %u %i\n", pFactory->m_hParentWindow, hwnd, uMsg, wParam );
60 // If Window is parent window
61 // XXX: this test isn't needed, see the kludge above...
62 // if( hwnd == pFactory->m_hParentWindow )
64 if( uMsg == WM_SYSCOMMAND )
66 // If closing parent window
67 if( wParam == SC_CLOSE )
69 Win32Loop *pLoop = (Win32Loop*)Win32Loop::instance( p_intf );
75 msg_Err( p_intf, "WM_SYSCOMMAND %i", wParam );
77 // if( (Event *)wParam != NULL )
78 // ( (Event *)wParam )->SendEvent();
81 // Handle systray notifications
82 else if( uMsg == MY_WSTRAYACTION )
84 if( (UINT)lParam == WM_LBUTTONDOWN )
86 p_intf->p_sys->p_theme->getWindowManager().raiseAll();
88 else if( (UINT)lParam == WM_RBUTTONDOWN )
90 CmdDlgShowPopupMenu aCmdPopup( p_intf );
93 else if( (UINT)lParam == WM_LBUTTONDBLCLK )
95 ShowWindow( hwnd, SW_RESTORE );
100 // If hwnd does not match any window or message not processed
101 return DefWindowProc( hwnd, uMsg, wParam, lParam );
105 Win32Factory::Win32Factory( intf_thread_t *pIntf ):
106 OSFactory( pIntf ), TransparentBlt( NULL ), AlphaBlend( NULL ),
107 SetLayeredWindowAttributes( NULL ), m_hParentWindow( NULL ),
114 bool Win32Factory::init()
116 // Get instance handle
117 m_hInst = GetModuleHandle( NULL );
118 if( m_hInst == NULL )
120 msg_Err( getIntf(), "Cannot get module handle" );
123 // Create window class
124 WNDCLASS skinWindowClass;
125 skinWindowClass.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
126 skinWindowClass.lpfnWndProc = (WNDPROC) Win32Proc;
127 skinWindowClass.lpszClassName = _T("SkinWindowClass");
128 skinWindowClass.lpszMenuName = NULL;
129 skinWindowClass.cbClsExtra = 0;
130 skinWindowClass.cbWndExtra = 0;
131 skinWindowClass.hbrBackground = NULL;
132 skinWindowClass.hCursor = LoadCursor( NULL , IDC_ARROW );
133 skinWindowClass.hIcon = LoadIcon( m_hInst, _T("VLC_ICON") );
134 skinWindowClass.hInstance = m_hInst;
136 // Register class and check it
137 if( !RegisterClass( &skinWindowClass ) )
141 // Check why it failed. If it's because the class already exists
142 // then fine, otherwise return with an error.
143 if( !GetClassInfo( m_hInst, _T("SkinWindowClass"), &wndclass ) )
145 msg_Err( getIntf(), "cannot register window class" );
151 m_hParentWindow = CreateWindowEx( WS_EX_APPWINDOW, _T("SkinWindowClass"),
152 _T("VLC media player"), WS_SYSMENU|WS_POPUP,
153 -200, -200, 0, 0, 0, 0, m_hInst, 0 );
154 if( m_hParentWindow == NULL )
156 msg_Err( getIntf(), "cannot create parent window" );
160 // Store with it a pointer to the interface thread
161 SetWindowLongPtr( m_hParentWindow, GWLP_USERDATA, (LONG_PTR)getIntf() );
163 // Initialize the systray icon
164 m_trayIcon.cbSize = sizeof( NOTIFYICONDATA );
165 m_trayIcon.hWnd = m_hParentWindow;
167 m_trayIcon.uFlags = NIF_ICON|NIF_TIP|NIF_MESSAGE;
168 m_trayIcon.uCallbackMessage = MY_WSTRAYACTION;
169 m_trayIcon.hIcon = LoadIcon( m_hInst, _T("VLC_ICON") );
170 strcpy( m_trayIcon.szTip, "VLC media player" );
172 // Show the systray icon if needed
173 if( config_GetInt( getIntf(), "skins2-systray" ) )
175 Shell_NotifyIcon( NIM_ADD, &m_trayIcon );
178 // We do it this way otherwise CreateWindowEx will fail
179 // if WS_EX_LAYERED is not supported
180 SetWindowLongPtr( m_hParentWindow, GWL_EXSTYLE,
181 GetWindowLong( m_hParentWindow, GWL_EXSTYLE ) |
184 ShowWindow( m_hParentWindow, SW_SHOW );
186 // Initialize the OLE library (for drag & drop)
187 OleInitialize( NULL );
189 // We dynamically load msimg32.dll to get a pointer to TransparentBlt()
190 m_hMsimg32 = LoadLibrary( _T("msimg32.dll") );
193 (BOOL (WINAPI*)(HDC, int, int, int, int,
194 HDC, int, int, int, int, unsigned int))
195 GetProcAddress( m_hMsimg32, _T("TransparentBlt") ) ) )
197 TransparentBlt = NULL;
198 msg_Dbg( getIntf(), "couldn't find TransparentBlt(), "
199 "falling back to BitBlt()" );
203 (BOOL (WINAPI*)( HDC, int, int, int, int, HDC, int, int,
204 int, int, BLENDFUNCTION ))
205 GetProcAddress( m_hMsimg32, _T("AlphaBlend") ) ) )
208 msg_Dbg( getIntf(), "couldn't find AlphaBlend()" );
211 // Idem for user32.dll and SetLayeredWindowAttributes()
212 m_hUser32 = LoadLibrary( _T("user32.dll") );
214 !( SetLayeredWindowAttributes =
215 (BOOL (WINAPI *)(HWND, COLORREF, BYTE, DWORD))
216 GetProcAddress( m_hUser32, _T("SetLayeredWindowAttributes") ) ) )
218 SetLayeredWindowAttributes = NULL;
219 msg_Dbg( getIntf(), "couldn't find SetLayeredWindowAttributes()" );
222 // Initialize the resource path
223 m_resourcePath.push_back( (string)getIntf()->p_vlc->psz_homedir +
224 "\\" + CONFIG_DIR + "\\skins" );
225 m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
227 m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
229 m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
231 m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
239 Win32Factory::~Win32Factory()
241 // Uninitialize the OLE library
244 // Remove the systray icon
245 Shell_NotifyIcon( NIM_DELETE, &m_trayIcon );
247 if( m_hParentWindow ) DestroyWindow( m_hParentWindow );
249 // Unload msimg32.dll and user32.dll
251 FreeLibrary( m_hMsimg32 );
253 FreeLibrary( m_hUser32 );
257 OSGraphics *Win32Factory::createOSGraphics( int width, int height )
259 return new Win32Graphics( getIntf(), width, height );
263 OSLoop *Win32Factory::getOSLoop()
265 return Win32Loop::instance( getIntf() );
269 void Win32Factory::destroyOSLoop()
271 Win32Loop::destroy( getIntf() );
274 void Win32Factory::minimize()
276 /* Make sure no tooltip is visible first */
277 getIntf()->p_sys->p_theme->getWindowManager().hideTooltip();
279 ShowWindow( m_hParentWindow, SW_MINIMIZE );
282 OSTimer *Win32Factory::createOSTimer( CmdGeneric &rCmd )
284 return new Win32Timer( getIntf(), rCmd, m_hParentWindow );
288 OSWindow *Win32Factory::createOSWindow( GenericWindow &rWindow, bool dragDrop,
289 bool playOnDrop, OSWindow *pParent )
291 return new Win32Window( getIntf(), rWindow, m_hInst, m_hParentWindow,
292 dragDrop, playOnDrop, (Win32Window*)pParent );
296 OSTooltip *Win32Factory::createOSTooltip()
298 return new Win32Tooltip( getIntf(), m_hInst, m_hParentWindow );
302 OSPopup *Win32Factory::createOSPopup()
304 // XXX FIXME: this way of getting the handle really sucks!
305 // In fact, the clean way would be to have in Builder::addPopup() a call
306 // to pPopup->associateToWindow() (to be written)... but the problem is
307 // that there is no way to access the OS-dependent window handle from a
308 // GenericWindow (we cannot even access the OSWindow).
309 if( m_windowMap.begin() == m_windowMap.end() )
311 msg_Err( getIntf(), "no window has been created before the popup!" );
315 return new Win32Popup( getIntf(), m_windowMap.begin()->first );
319 int Win32Factory::getScreenWidth() const
321 return GetSystemMetrics(SM_CXSCREEN);
326 int Win32Factory::getScreenHeight() const
328 return GetSystemMetrics(SM_CYSCREEN);
332 Rect Win32Factory::getWorkArea() const
335 SystemParametersInfo( SPI_GETWORKAREA, 0, &r, 0 );
336 // Fill a Rect object
337 Rect rect( r.left, r.top, r.right, r.bottom );
342 void Win32Factory::getMousePos( int &rXPos, int &rYPos ) const
345 GetCursorPos( &mousePos );
351 void Win32Factory::changeCursor( CursorType_t type ) const
376 HCURSOR hCurs = LoadCursor( NULL, id );
381 void Win32Factory::rmDir( const string &rPath )
383 WIN32_FIND_DATA find;
385 string findFiles = rPath + "\\*";
386 HANDLE handle = FindFirstFile( findFiles.c_str(), &find );
388 while( handle != INVALID_HANDLE_VALUE )
390 // If file is neither "." nor ".."
391 if( strcmp( find.cFileName, "." ) && strcmp( find.cFileName, ".." ) )
394 file = rPath + "\\" + (string)find.cFileName;
396 // If file is a directory, delete it recursively
397 if( find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
401 // Else, it is a file so simply delete it
404 DeleteFile( file.c_str() );
408 // If no more file in directory, exit while
409 if( !FindNextFile( handle, &find ) )
413 // Now directory is empty so can be removed
415 RemoveDirectory( rPath.c_str() );