]> git.sesse.net Git - vlc/blob - modules/gui/skins2/win32/win32_factory.cpp
* skins2: new skins2-systray option on Windows to activate an icon in the system...
[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
38 // Custom message for the notifications of the system tray
39 #define MY_WSTRAYACTION (WM_APP + 1)
40
41
42 LRESULT CALLBACK Win32Proc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
43 {
44     // Get pointer to thread info: should only work with the parent window
45     intf_thread_t *p_intf = (intf_thread_t *)GetWindowLongPtr( hwnd,
46         GWLP_USERDATA );
47
48     // If doesn't exist, treat windows message normally
49     if( p_intf == NULL || p_intf->p_sys->p_osFactory == NULL )
50     {
51         return DefWindowProc( hwnd, uMsg, wParam, lParam );
52     }
53
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 :)
57
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 )
63     {
64         if( uMsg == WM_SYSCOMMAND )
65         {
66             // If closing parent window
67             if( wParam == SC_CLOSE )
68             {
69                 Win32Loop *pLoop = (Win32Loop*)Win32Loop::instance( p_intf );
70                 pLoop->exit();
71                 return 0;
72             }
73             else
74             {
75                 msg_Err( p_intf, "WM_SYSCOMMAND %i", wParam );
76             }
77 //            if( (Event *)wParam != NULL )
78 //                ( (Event *)wParam )->SendEvent();
79 //            return 0;
80         }
81         // Handle systray notifications
82         else if( uMsg == MY_WSTRAYACTION )
83         {
84             if( (UINT)lParam == WM_LBUTTONDOWN )
85             {
86                 p_intf->p_sys->p_theme->getWindowManager().raiseAll();
87             }
88             else if( (UINT)lParam == WM_RBUTTONDOWN )
89             {
90                 CmdDlgShowPopupMenu aCmdPopup( p_intf );
91                 aCmdPopup.execute();
92             }
93             else if( (UINT)lParam == WM_LBUTTONDBLCLK )
94             {
95                 ShowWindow( hwnd, SW_RESTORE );
96             }
97         }
98     }
99
100     // If hwnd does not match any window or message not processed
101     return DefWindowProc( hwnd, uMsg, wParam, lParam );
102 }
103
104
105 Win32Factory::Win32Factory( intf_thread_t *pIntf ):
106     OSFactory( pIntf ), TransparentBlt( NULL ), AlphaBlend( NULL ),
107     SetLayeredWindowAttributes( NULL ), m_hParentWindow( NULL ),
108     m_dirSep( "\\" )
109 {
110     // see init()
111 }
112
113
114 bool Win32Factory::init()
115 {
116     // Get instance handle
117     m_hInst = GetModuleHandle( NULL );
118     if( m_hInst == NULL )
119     {
120         msg_Err( getIntf(), "Cannot get module handle" );
121     }
122
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;
135
136     // Register class and check it
137     if( !RegisterClass( &skinWindowClass ) )
138     {
139         WNDCLASS wndclass;
140
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 ) )
144         {
145             msg_Err( getIntf(), "cannot register window class" );
146             return false;
147         }
148     }
149
150     // Create Window
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 )
155     {
156         msg_Err( getIntf(), "cannot create parent window" );
157         return false;
158     }
159
160     // Store with it a pointer to the interface thread
161     SetWindowLongPtr( m_hParentWindow, GWLP_USERDATA, (LONG_PTR)getIntf() );
162
163     // Initialize the systray icon
164     m_trayIcon.cbSize = sizeof( NOTIFYICONDATA );
165     m_trayIcon.hWnd = m_hParentWindow;
166     m_trayIcon.uID = 42;
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" );
171
172     // Show the systray icon if needed
173     if( config_GetInt( getIntf(), "skins2-systray" ) )
174     {
175         Shell_NotifyIcon( NIM_ADD, &m_trayIcon );
176     }
177
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 ) |
182                       WS_EX_LAYERED );
183
184     ShowWindow( m_hParentWindow, SW_SHOW );
185
186     // Initialize the OLE library (for drag & drop)
187     OleInitialize( NULL );
188
189     // We dynamically load msimg32.dll to get a pointer to TransparentBlt()
190     m_hMsimg32 = LoadLibrary( _T("msimg32.dll") );
191     if( !m_hMsimg32 ||
192         !( TransparentBlt =
193             (BOOL (WINAPI*)(HDC, int, int, int, int,
194                             HDC, int, int, int, int, unsigned int))
195             GetProcAddress( m_hMsimg32, _T("TransparentBlt") ) ) )
196     {
197         TransparentBlt = NULL;
198         msg_Dbg( getIntf(), "couldn't find TransparentBlt(), "
199                  "falling back to BitBlt()" );
200     }
201     if( !m_hMsimg32 ||
202         !( AlphaBlend =
203             (BOOL (WINAPI*)( HDC, int, int, int, int, HDC, int, int,
204                               int, int, BLENDFUNCTION ))
205             GetProcAddress( m_hMsimg32, _T("AlphaBlend") ) ) )
206     {
207         AlphaBlend = NULL;
208         msg_Dbg( getIntf(), "couldn't find AlphaBlend()" );
209     }
210
211     // Idem for user32.dll and SetLayeredWindowAttributes()
212     m_hUser32 = LoadLibrary( _T("user32.dll") );
213     if( !m_hUser32 ||
214         !( SetLayeredWindowAttributes =
215             (BOOL (WINAPI *)(HWND, COLORREF, BYTE, DWORD))
216             GetProcAddress( m_hUser32, _T("SetLayeredWindowAttributes") ) ) )
217     {
218         SetLayeredWindowAttributes = NULL;
219         msg_Dbg( getIntf(), "couldn't find SetLayeredWindowAttributes()" );
220     }
221
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 +
226                               "\\skins" );
227     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
228                               "\\skins2" );
229     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
230                               "\\share\\skins" );
231     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
232                               "\\share\\skins2" );
233
234     // All went well
235     return true;
236 }
237
238
239 Win32Factory::~Win32Factory()
240 {
241     // Uninitialize the OLE library
242     OleUninitialize();
243
244     // Remove the systray icon
245     Shell_NotifyIcon( NIM_DELETE, &m_trayIcon );
246
247     if( m_hParentWindow ) DestroyWindow( m_hParentWindow );
248
249     // Unload msimg32.dll and user32.dll
250     if( m_hMsimg32 )
251         FreeLibrary( m_hMsimg32 );
252     if( m_hUser32 )
253         FreeLibrary( m_hUser32 );
254 }
255
256
257 OSGraphics *Win32Factory::createOSGraphics( int width, int height )
258 {
259     return new Win32Graphics( getIntf(), width, height );
260 }
261
262
263 OSLoop *Win32Factory::getOSLoop()
264 {
265     return Win32Loop::instance( getIntf() );
266 }
267
268
269 void Win32Factory::destroyOSLoop()
270 {
271     Win32Loop::destroy( getIntf() );
272 }
273
274 void Win32Factory::minimize()
275 {
276     /* Make sure no tooltip is visible first */
277     getIntf()->p_sys->p_theme->getWindowManager().hideTooltip();
278
279     ShowWindow( m_hParentWindow, SW_MINIMIZE );
280 }
281
282 OSTimer *Win32Factory::createOSTimer( CmdGeneric &rCmd )
283 {
284     return new Win32Timer( getIntf(), rCmd, m_hParentWindow );
285 }
286
287
288 OSWindow *Win32Factory::createOSWindow( GenericWindow &rWindow, bool dragDrop,
289                                         bool playOnDrop, OSWindow *pParent )
290 {
291     return new Win32Window( getIntf(), rWindow, m_hInst, m_hParentWindow,
292                             dragDrop, playOnDrop, (Win32Window*)pParent );
293 }
294
295
296 OSTooltip *Win32Factory::createOSTooltip()
297 {
298     return new Win32Tooltip( getIntf(), m_hInst, m_hParentWindow );
299 }
300
301
302 OSPopup *Win32Factory::createOSPopup()
303 {
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() )
310     {
311         msg_Err( getIntf(), "no window has been created before the popup!" );
312         return NULL;
313     }
314
315     return new Win32Popup( getIntf(), m_windowMap.begin()->first );
316 }
317
318
319 int Win32Factory::getScreenWidth() const
320 {
321     return GetSystemMetrics(SM_CXSCREEN);
322
323 }
324
325
326 int Win32Factory::getScreenHeight() const
327 {
328     return GetSystemMetrics(SM_CYSCREEN);
329 }
330
331
332 Rect Win32Factory::getWorkArea() const
333 {
334     RECT r;
335     SystemParametersInfo( SPI_GETWORKAREA, 0, &r, 0 );
336     // Fill a Rect object
337     Rect rect( r.left, r.top, r.right, r.bottom );
338     return rect;
339 }
340
341
342 void Win32Factory::getMousePos( int &rXPos, int &rYPos ) const
343 {
344     POINT mousePos;
345     GetCursorPos( &mousePos );
346     rXPos = mousePos.x;
347     rYPos = mousePos.y;
348 }
349
350
351 void Win32Factory::changeCursor( CursorType_t type ) const
352 {
353     LPCTSTR id;
354     switch( type )
355     {
356         case kDefaultArrow:
357             id = IDC_ARROW;
358             break;
359         case kResizeNWSE:
360             id = IDC_SIZENWSE;
361             break;
362         case kResizeNS:
363             id = IDC_SIZENS;
364             break;
365         case kResizeWE:
366             id = IDC_SIZEWE;
367             break;
368         case kResizeNESW:
369             id = IDC_SIZENESW;
370             break;
371         default:
372             id = IDC_ARROW;
373             break;
374     }
375
376     HCURSOR hCurs = LoadCursor( NULL, id );
377     SetCursor( hCurs );
378 }
379
380
381 void Win32Factory::rmDir( const string &rPath )
382 {
383     WIN32_FIND_DATA find;
384     string file;
385     string findFiles = rPath + "\\*";
386     HANDLE handle    = FindFirstFile( findFiles.c_str(), &find );
387
388     while( handle != INVALID_HANDLE_VALUE )
389     {
390         // If file is neither "." nor ".."
391         if( strcmp( find.cFileName, "." ) && strcmp( find.cFileName, ".." ) )
392         {
393             // Set file name
394             file = rPath + "\\" + (string)find.cFileName;
395
396             // If file is a directory, delete it recursively
397             if( find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
398             {
399                 rmDir( file );
400             }
401             // Else, it is a file so simply delete it
402             else
403             {
404                 DeleteFile( file.c_str() );
405             }
406         }
407
408         // If no more file in directory, exit while
409         if( !FindNextFile( handle, &find ) )
410             break;
411     }
412
413     // Now directory is empty so can be removed
414     FindClose( handle );
415     RemoveDirectory( rPath.c_str() );
416 }
417
418 #endif