]> git.sesse.net Git - vlc/blob - modules/gui/skins2/win32/win32_factory.cpp
* skins2: new skins2-taskbar configuration option, to allow removing VLC from the...
[vlc] / modules / gui / skins2 / win32 / win32_factory.cpp
1 /*****************************************************************************
2  * win32_factory.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef WIN32_SKINS
26
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"
37 #include "../commands/cmd_minimize.hpp"
38
39 // Custom message for the notifications of the system tray
40 #define MY_WSTRAYACTION (WM_APP + 1)
41
42
43 LRESULT CALLBACK Win32Proc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
44 {
45     // Get pointer to thread info: should only work with the parent window
46     intf_thread_t *p_intf = (intf_thread_t *)GetWindowLongPtr( hwnd,
47         GWLP_USERDATA );
48
49     // If doesn't exist, treat windows message normally
50     if( p_intf == NULL || p_intf->p_sys->p_osFactory == NULL )
51     {
52         return DefWindowProc( hwnd, uMsg, wParam, lParam );
53     }
54
55     // Here we know we are getting a message for the parent window, since it is
56     // the only one to store p_intf...
57     // Yes, it is a kludge :)
58
59 //Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( p_intf );
60 //msg_Err( p_intf, "Parent window %p %p %u %i\n", pFactory->m_hParentWindow, hwnd, uMsg, wParam );
61     // If Window is parent window
62     // XXX: this test isn't needed, see the kludge above...
63 //    if( hwnd == pFactory->m_hParentWindow )
64     {
65         if( uMsg == WM_SYSCOMMAND )
66         {
67             // If closing parent window
68             if( wParam == SC_CLOSE )
69             {
70                 Win32Loop *pLoop = (Win32Loop*)Win32Loop::instance( p_intf );
71                 pLoop->exit();
72                 return 0;
73             }
74             else
75             {
76                 msg_Err( p_intf, "WM_SYSCOMMAND %i", wParam );
77             }
78 //            if( (Event *)wParam != NULL )
79 //                ( (Event *)wParam )->SendEvent();
80 //            return 0;
81         }
82         // Handle systray notifications
83         else if( uMsg == MY_WSTRAYACTION )
84         {
85             if( (UINT)lParam == WM_LBUTTONDOWN )
86             {
87                 p_intf->p_sys->p_theme->getWindowManager().raiseAll();
88             }
89             else if( (UINT)lParam == WM_RBUTTONDOWN )
90             {
91                 CmdDlgShowPopupMenu aCmdPopup( p_intf );
92                 aCmdPopup.execute();
93             }
94             else if( (UINT)lParam == WM_LBUTTONDBLCLK )
95             {
96                 CmdRestore aCmdRestore( p_intf );
97                 aCmdRestore.execute();
98             }
99         }
100     }
101
102     // If hwnd does not match any window or message not processed
103     return DefWindowProc( hwnd, uMsg, wParam, lParam );
104 }
105
106
107 Win32Factory::Win32Factory( intf_thread_t *pIntf ):
108     OSFactory( pIntf ), TransparentBlt( NULL ), AlphaBlend( NULL ),
109     SetLayeredWindowAttributes( NULL ), m_hParentWindow( NULL ),
110     m_dirSep( "\\" )
111 {
112     // see init()
113 }
114
115
116 bool Win32Factory::init()
117 {
118     // Get instance handle
119     m_hInst = GetModuleHandle( NULL );
120     if( m_hInst == NULL )
121     {
122         msg_Err( getIntf(), "Cannot get module handle" );
123     }
124
125     // Create window class
126     WNDCLASS skinWindowClass;
127     skinWindowClass.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
128     skinWindowClass.lpfnWndProc = (WNDPROC) Win32Proc;
129     skinWindowClass.lpszClassName = _T("SkinWindowClass");
130     skinWindowClass.lpszMenuName = NULL;
131     skinWindowClass.cbClsExtra = 0;
132     skinWindowClass.cbWndExtra = 0;
133     skinWindowClass.hbrBackground = NULL;
134     skinWindowClass.hCursor = LoadCursor( NULL , IDC_ARROW );
135     skinWindowClass.hIcon = LoadIcon( m_hInst, _T("VLC_ICON") );
136     skinWindowClass.hInstance = m_hInst;
137
138     // Register class and check it
139     if( !RegisterClass( &skinWindowClass ) )
140     {
141         WNDCLASS wndclass;
142
143         // Check why it failed. If it's because the class already exists
144         // then fine, otherwise return with an error.
145         if( !GetClassInfo( m_hInst, _T("SkinWindowClass"), &wndclass ) )
146         {
147             msg_Err( getIntf(), "cannot register window class" );
148             return false;
149         }
150     }
151
152     // Create Window
153     m_hParentWindow = CreateWindowEx( WS_EX_TOOLWINDOW, _T("SkinWindowClass"),
154         _T("VLC media player"), WS_SYSMENU|WS_POPUP,
155         -200, -200, 0, 0, 0, 0, m_hInst, 0 );
156     if( m_hParentWindow == NULL )
157     {
158         msg_Err( getIntf(), "cannot create parent window" );
159         return false;
160     }
161
162     // Store with it a pointer to the interface thread
163     SetWindowLongPtr( m_hParentWindow, GWLP_USERDATA, (LONG_PTR)getIntf() );
164
165     // We do it this way otherwise CreateWindowEx will fail
166     // if WS_EX_LAYERED is not supported
167     SetWindowLongPtr( m_hParentWindow, GWL_EXSTYLE,
168                       GetWindowLong( m_hParentWindow, GWL_EXSTYLE ) |
169                       WS_EX_LAYERED );
170
171     ShowWindow( m_hParentWindow, SW_SHOW );
172
173     // Initialize the systray icon
174     m_trayIcon.cbSize = sizeof( NOTIFYICONDATA );
175     m_trayIcon.hWnd = m_hParentWindow;
176     m_trayIcon.uID = 42;
177     m_trayIcon.uFlags = NIF_ICON|NIF_TIP|NIF_MESSAGE;
178     m_trayIcon.uCallbackMessage = MY_WSTRAYACTION;
179     m_trayIcon.hIcon = LoadIcon( m_hInst, _T("VLC_ICON") );
180     strcpy( m_trayIcon.szTip, "VLC media player" );
181
182     // Show the systray icon if needed
183     if( config_GetInt( getIntf(), "skins2-systray" ) )
184     {
185         addInTray();
186     }
187
188     // Show the task in the task bar if needed
189     if( config_GetInt( getIntf(), "skins2-taskbar" ) )
190     {
191         addInTaskBar();
192     }
193
194     // Initialize the OLE library (for drag & drop)
195     OleInitialize( NULL );
196
197     // We dynamically load msimg32.dll to get a pointer to TransparentBlt()
198     m_hMsimg32 = LoadLibrary( _T("msimg32.dll") );
199     if( !m_hMsimg32 ||
200         !( TransparentBlt =
201             (BOOL (WINAPI*)(HDC, int, int, int, int,
202                             HDC, int, int, int, int, unsigned int))
203             GetProcAddress( m_hMsimg32, _T("TransparentBlt") ) ) )
204     {
205         TransparentBlt = NULL;
206         msg_Dbg( getIntf(), "couldn't find TransparentBlt(), "
207                  "falling back to BitBlt()" );
208     }
209     if( !m_hMsimg32 ||
210         !( AlphaBlend =
211             (BOOL (WINAPI*)( HDC, int, int, int, int, HDC, int, int,
212                               int, int, BLENDFUNCTION ))
213             GetProcAddress( m_hMsimg32, _T("AlphaBlend") ) ) )
214     {
215         AlphaBlend = NULL;
216         msg_Dbg( getIntf(), "couldn't find AlphaBlend()" );
217     }
218
219     // Idem for user32.dll and SetLayeredWindowAttributes()
220     m_hUser32 = LoadLibrary( _T("user32.dll") );
221     if( !m_hUser32 ||
222         !( SetLayeredWindowAttributes =
223             (BOOL (WINAPI *)(HWND, COLORREF, BYTE, DWORD))
224             GetProcAddress( m_hUser32, _T("SetLayeredWindowAttributes") ) ) )
225     {
226         SetLayeredWindowAttributes = NULL;
227         msg_Dbg( getIntf(), "couldn't find SetLayeredWindowAttributes()" );
228     }
229
230     // Initialize the resource path
231     m_resourcePath.push_back( (string)getIntf()->p_vlc->psz_homedir +
232                                "\\" + CONFIG_DIR + "\\skins" );
233     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
234                               "\\skins" );
235     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
236                               "\\skins2" );
237     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
238                               "\\share\\skins" );
239     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
240                               "\\share\\skins2" );
241
242     // All went well
243     return true;
244 }
245
246
247 Win32Factory::~Win32Factory()
248 {
249     // Uninitialize the OLE library
250     OleUninitialize();
251
252     // Remove the systray icon
253     removeFromTray();
254
255     if( m_hParentWindow ) DestroyWindow( m_hParentWindow );
256
257     // Unload msimg32.dll and user32.dll
258     if( m_hMsimg32 )
259         FreeLibrary( m_hMsimg32 );
260     if( m_hUser32 )
261         FreeLibrary( m_hUser32 );
262 }
263
264
265 OSGraphics *Win32Factory::createOSGraphics( int width, int height )
266 {
267     return new Win32Graphics( getIntf(), width, height );
268 }
269
270
271 OSLoop *Win32Factory::getOSLoop()
272 {
273     return Win32Loop::instance( getIntf() );
274 }
275
276
277 void Win32Factory::destroyOSLoop()
278 {
279     Win32Loop::destroy( getIntf() );
280 }
281
282 void Win32Factory::minimize()
283 {
284     /* Make sure no tooltip is visible first */
285     getIntf()->p_sys->p_theme->getWindowManager().hideTooltip();
286
287     ShowWindow( m_hParentWindow, SW_MINIMIZE );
288 }
289
290 void Win32Factory::restore()
291 {
292     ShowWindow( m_hParentWindow, SW_RESTORE );
293 }
294
295 void Win32Factory::addInTray()
296 {
297     Shell_NotifyIcon( NIM_ADD, &m_trayIcon );
298 }
299
300 void Win32Factory::removeFromTray()
301 {
302     Shell_NotifyIcon( NIM_DELETE, &m_trayIcon );
303 }
304
305 void Win32Factory::addInTaskBar()
306 {
307     ShowWindow( m_hParentWindow, SW_HIDE );
308     SetWindowLongPtr( m_hParentWindow, GWL_EXSTYLE,
309                       WS_EX_LAYERED|WS_EX_APPWINDOW );
310     ShowWindow( m_hParentWindow, SW_SHOW );
311 }
312
313 void Win32Factory::removeFromTaskBar()
314 {
315     ShowWindow( m_hParentWindow, SW_HIDE );
316     SetWindowLongPtr( m_hParentWindow, GWL_EXSTYLE,
317                       WS_EX_LAYERED|WS_EX_TOOLWINDOW );
318     ShowWindow( m_hParentWindow, SW_SHOW );
319 }
320
321 OSTimer *Win32Factory::createOSTimer( CmdGeneric &rCmd )
322 {
323     return new Win32Timer( getIntf(), rCmd, m_hParentWindow );
324 }
325
326
327 OSWindow *Win32Factory::createOSWindow( GenericWindow &rWindow, bool dragDrop,
328                                         bool playOnDrop, OSWindow *pParent )
329 {
330     return new Win32Window( getIntf(), rWindow, m_hInst, m_hParentWindow,
331                             dragDrop, playOnDrop, (Win32Window*)pParent );
332 }
333
334
335 OSTooltip *Win32Factory::createOSTooltip()
336 {
337     return new Win32Tooltip( getIntf(), m_hInst, m_hParentWindow );
338 }
339
340
341 OSPopup *Win32Factory::createOSPopup()
342 {
343     // XXX FIXME: this way of getting the handle really sucks!
344     // In fact, the clean way would be to have in Builder::addPopup() a call
345     // to pPopup->associateToWindow() (to be written)... but the problem is
346     // that there is no way to access the OS-dependent window handle from a
347     // GenericWindow (we cannot even access the OSWindow).
348     if( m_windowMap.begin() == m_windowMap.end() )
349     {
350         msg_Err( getIntf(), "no window has been created before the popup!" );
351         return NULL;
352     }
353
354     return new Win32Popup( getIntf(), m_windowMap.begin()->first );
355 }
356
357
358 int Win32Factory::getScreenWidth() const
359 {
360     return GetSystemMetrics(SM_CXSCREEN);
361
362 }
363
364
365 int Win32Factory::getScreenHeight() const
366 {
367     return GetSystemMetrics(SM_CYSCREEN);
368 }
369
370
371 Rect Win32Factory::getWorkArea() const
372 {
373     RECT r;
374     SystemParametersInfo( SPI_GETWORKAREA, 0, &r, 0 );
375     // Fill a Rect object
376     Rect rect( r.left, r.top, r.right, r.bottom );
377     return rect;
378 }
379
380
381 void Win32Factory::getMousePos( int &rXPos, int &rYPos ) const
382 {
383     POINT mousePos;
384     GetCursorPos( &mousePos );
385     rXPos = mousePos.x;
386     rYPos = mousePos.y;
387 }
388
389
390 void Win32Factory::changeCursor( CursorType_t type ) const
391 {
392     LPCTSTR id;
393     switch( type )
394     {
395         case kDefaultArrow:
396             id = IDC_ARROW;
397             break;
398         case kResizeNWSE:
399             id = IDC_SIZENWSE;
400             break;
401         case kResizeNS:
402             id = IDC_SIZENS;
403             break;
404         case kResizeWE:
405             id = IDC_SIZEWE;
406             break;
407         case kResizeNESW:
408             id = IDC_SIZENESW;
409             break;
410         default:
411             id = IDC_ARROW;
412             break;
413     }
414
415     HCURSOR hCurs = LoadCursor( NULL, id );
416     SetCursor( hCurs );
417 }
418
419
420 void Win32Factory::rmDir( const string &rPath )
421 {
422     WIN32_FIND_DATA find;
423     string file;
424     string findFiles = rPath + "\\*";
425     HANDLE handle    = FindFirstFile( findFiles.c_str(), &find );
426
427     while( handle != INVALID_HANDLE_VALUE )
428     {
429         // If file is neither "." nor ".."
430         if( strcmp( find.cFileName, "." ) && strcmp( find.cFileName, ".." ) )
431         {
432             // Set file name
433             file = rPath + "\\" + (string)find.cFileName;
434
435             // If file is a directory, delete it recursively
436             if( find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
437             {
438                 rmDir( file );
439             }
440             // Else, it is a file so simply delete it
441             else
442             {
443                 DeleteFile( file.c_str() );
444             }
445         }
446
447         // If no more file in directory, exit while
448         if( !FindNextFile( handle, &find ) )
449             break;
450     }
451
452     // Now directory is empty so can be removed
453     FindClose( handle );
454     RemoveDirectory( rPath.c_str() );
455 }
456
457 #endif