]> git.sesse.net Git - vlc/blob - modules/gui/skins2/win32/win32_factory.cpp
Add --data-path option. Access the src share directory now works from build tree.
[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 "../src/generic_window.hpp"
37 #include "../commands/cmd_dialogs.hpp"
38 #include "../commands/cmd_minimize.hpp"
39
40 // Custom message for the notifications of the system tray
41 #define MY_WM_TRAYACTION (WM_APP + 1)
42
43
44 LRESULT CALLBACK Win32Proc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
45 {
46     // Get pointer to thread info: should only work with the parent window
47     intf_thread_t *p_intf = (intf_thread_t *)GetWindowLongPtr( hwnd,
48         GWLP_USERDATA );
49
50     // If doesn't exist, treat windows message normally
51     if( p_intf == NULL || p_intf->p_sys->p_osFactory == NULL )
52     {
53         return DefWindowProc( hwnd, uMsg, wParam, lParam );
54     }
55
56     // Here we know we are getting a message for the parent window, since it is
57     // the only one to store p_intf...
58     // Yes, it is a kludge :)
59
60 //Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( p_intf );
61 //msg_Err( p_intf, "Parent window %p %p %u %i\n", pFactory->m_hParentWindow, hwnd, uMsg, wParam );
62     // If Window is parent window
63     // XXX: this test isn't needed, see the kludge above...
64 //    if( hwnd == pFactory->m_hParentWindow )
65     {
66         if( uMsg == WM_SYSCOMMAND )
67         {
68             // If closing parent window
69             if( wParam == SC_CLOSE )
70             {
71                 Win32Loop *pLoop = (Win32Loop*)Win32Loop::instance( p_intf );
72                 pLoop->exit();
73                 return 0;
74             }
75             else
76             {
77                 msg_Dbg( p_intf, "WM_SYSCOMMAND %i", wParam );
78             }
79         }
80         // Handle systray notifications
81         else if( uMsg == MY_WM_TRAYACTION )
82         {
83             if( (UINT)lParam == WM_LBUTTONDOWN )
84             {
85                 p_intf->p_sys->p_theme->getWindowManager().raiseAll();
86             }
87             else if( (UINT)lParam == WM_RBUTTONDOWN )
88             {
89                 CmdDlgShowPopupMenu aCmdPopup( p_intf );
90                 aCmdPopup.execute();
91             }
92             else if( (UINT)lParam == WM_LBUTTONDBLCLK )
93             {
94                 CmdRestore aCmdRestore( p_intf );
95                 aCmdRestore.execute();
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_TOOLWINDOW, _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     // We do it this way otherwise CreateWindowEx will fail
164     // if WS_EX_LAYERED is not supported
165     SetWindowLongPtr( m_hParentWindow, GWL_EXSTYLE,
166                       GetWindowLong( m_hParentWindow, GWL_EXSTYLE ) |
167                       WS_EX_LAYERED );
168
169     ShowWindow( m_hParentWindow, SW_SHOW );
170
171     // Initialize the systray icon
172     m_trayIcon.cbSize = sizeof( NOTIFYICONDATA );
173     m_trayIcon.hWnd = m_hParentWindow;
174     m_trayIcon.uID = 42;
175     m_trayIcon.uFlags = NIF_ICON|NIF_TIP|NIF_MESSAGE;
176     m_trayIcon.uCallbackMessage = MY_WM_TRAYACTION;
177     m_trayIcon.hIcon = LoadIcon( m_hInst, _T("VLC_ICON") );
178     strcpy( m_trayIcon.szTip, "VLC media player" );
179
180     // Show the systray icon if needed
181     if( config_GetInt( getIntf(), "skins2-systray" ) )
182     {
183         addInTray();
184     }
185
186     // Show the task in the task bar if needed
187     if( config_GetInt( getIntf(), "skins2-taskbar" ) )
188     {
189         addInTaskBar();
190     }
191
192     // Initialize the OLE library (for drag & drop)
193     OleInitialize( NULL );
194
195     // We dynamically load msimg32.dll to get a pointer to TransparentBlt()
196     m_hMsimg32 = LoadLibrary( _T("msimg32.dll") );
197     if( !m_hMsimg32 ||
198         !( TransparentBlt =
199             (BOOL (WINAPI*)(HDC, int, int, int, int,
200                             HDC, int, int, int, int, unsigned int))
201             GetProcAddress( m_hMsimg32, _T("TransparentBlt") ) ) )
202     {
203         TransparentBlt = NULL;
204         msg_Dbg( getIntf(), "couldn't find TransparentBlt(), "
205                  "falling back to BitBlt()" );
206     }
207     if( !m_hMsimg32 ||
208         !( AlphaBlend =
209             (BOOL (WINAPI*)( HDC, int, int, int, int, HDC, int, int,
210                               int, int, BLENDFUNCTION ))
211             GetProcAddress( m_hMsimg32, _T("AlphaBlend") ) ) )
212     {
213         AlphaBlend = NULL;
214         msg_Dbg( getIntf(), "couldn't find AlphaBlend()" );
215     }
216
217     // Idem for user32.dll and SetLayeredWindowAttributes()
218     m_hUser32 = LoadLibrary( _T("user32.dll") );
219     if( !m_hUser32 ||
220         !( SetLayeredWindowAttributes =
221             (BOOL (WINAPI *)(HWND, COLORREF, BYTE, DWORD))
222             GetProcAddress( m_hUser32, _T("SetLayeredWindowAttributes") ) ) )
223     {
224         SetLayeredWindowAttributes = NULL;
225         msg_Dbg( getIntf(), "couldn't find SetLayeredWindowAttributes()" );
226     }
227
228     // Initialize the resource path
229     char *datadir = config_GetUserDir( VLC_DATA_DIR );
230     m_resourcePath.push_back( (string)datadir + "\\skins" );
231     free( datadir );
232     datadir = config_GetDataDir( getIntf() );
233     m_resourcePath.push_back( (string)datadir + "\\skins" );
234     m_resourcePath.push_back( (string)datadir + "\\skins2" );
235     m_resourcePath.push_back( (string)datadir + "\\share\\skins" );
236     m_resourcePath.push_back( (string)datadir + "\\share\\skins2" );
237     free( datadir );
238
239     // All went well
240     return true;
241 }
242
243
244 Win32Factory::~Win32Factory()
245 {
246     // Uninitialize the OLE library
247     OleUninitialize();
248
249     // Remove the systray icon
250     removeFromTray();
251
252     if( m_hParentWindow ) DestroyWindow( m_hParentWindow );
253
254     // Unload msimg32.dll and user32.dll
255     if( m_hMsimg32 )
256         FreeLibrary( m_hMsimg32 );
257     if( m_hUser32 )
258         FreeLibrary( m_hUser32 );
259 }
260
261
262 OSGraphics *Win32Factory::createOSGraphics( int width, int height )
263 {
264     return new Win32Graphics( getIntf(), width, height );
265 }
266
267
268 OSLoop *Win32Factory::getOSLoop()
269 {
270     return Win32Loop::instance( getIntf() );
271 }
272
273
274 void Win32Factory::destroyOSLoop()
275 {
276     Win32Loop::destroy( getIntf() );
277 }
278
279 void Win32Factory::minimize()
280 {
281     /* Make sure no tooltip is visible first */
282     getIntf()->p_sys->p_theme->getWindowManager().hideTooltip();
283
284     ShowWindow( m_hParentWindow, SW_MINIMIZE );
285 }
286
287 void Win32Factory::restore()
288 {
289     ShowWindow( m_hParentWindow, SW_RESTORE );
290 }
291
292 void Win32Factory::addInTray()
293 {
294     Shell_NotifyIcon( NIM_ADD, &m_trayIcon );
295 }
296
297 void Win32Factory::removeFromTray()
298 {
299     Shell_NotifyIcon( NIM_DELETE, &m_trayIcon );
300 }
301
302 void Win32Factory::addInTaskBar()
303 {
304     ShowWindow( m_hParentWindow, SW_HIDE );
305     SetWindowLongPtr( m_hParentWindow, GWL_EXSTYLE,
306                       WS_EX_LAYERED|WS_EX_APPWINDOW );
307     ShowWindow( m_hParentWindow, SW_SHOW );
308 }
309
310 void Win32Factory::removeFromTaskBar()
311 {
312     ShowWindow( m_hParentWindow, SW_HIDE );
313     SetWindowLongPtr( m_hParentWindow, GWL_EXSTYLE,
314                       WS_EX_LAYERED|WS_EX_TOOLWINDOW );
315     ShowWindow( m_hParentWindow, SW_SHOW );
316 }
317
318 OSTimer *Win32Factory::createOSTimer( CmdGeneric &rCmd )
319 {
320     return new Win32Timer( getIntf(), rCmd, m_hParentWindow );
321 }
322
323
324 OSWindow *Win32Factory::createOSWindow( GenericWindow &rWindow, bool dragDrop,
325                                         bool playOnDrop, OSWindow *pParent,
326                                         GenericWindow::WindowType_t type )
327 {
328     return new Win32Window( getIntf(), rWindow, m_hInst, m_hParentWindow,
329                             dragDrop, playOnDrop, (Win32Window*)pParent );
330 }
331
332
333 OSTooltip *Win32Factory::createOSTooltip()
334 {
335     return new Win32Tooltip( getIntf(), m_hInst, m_hParentWindow );
336 }
337
338
339 OSPopup *Win32Factory::createOSPopup()
340 {
341     // XXX FIXME: this way of getting the handle really sucks!
342     // In fact, the clean way would be to have in Builder::addPopup() a call
343     // to pPopup->associateToWindow() (to be written)... but the problem is
344     // that there is no way to access the OS-dependent window handle from a
345     // GenericWindow (we cannot even access the OSWindow).
346     if( m_windowMap.begin() == m_windowMap.end() )
347     {
348         msg_Err( getIntf(), "no window has been created before the popup!" );
349         return NULL;
350     }
351
352     return new Win32Popup( getIntf(), m_windowMap.begin()->first );
353 }
354
355
356 int Win32Factory::getScreenWidth() const
357 {
358     return GetSystemMetrics(SM_CXSCREEN);
359
360 }
361
362
363 int Win32Factory::getScreenHeight() const
364 {
365     return GetSystemMetrics(SM_CYSCREEN);
366 }
367
368
369 SkinsRect Win32Factory::getWorkArea() const
370 {
371     RECT r;
372     SystemParametersInfo( SPI_GETWORKAREA, 0, &r, 0 );
373     // Fill a Rect object
374     return  SkinsRect( r.left, r.top, r.right, r.bottom );
375 }
376
377
378 void Win32Factory::getMousePos( int &rXPos, int &rYPos ) const
379 {
380     POINT mousePos;
381     GetCursorPos( &mousePos );
382     rXPos = mousePos.x;
383     rYPos = mousePos.y;
384 }
385
386
387 void Win32Factory::changeCursor( CursorType_t type ) const
388 {
389     LPCTSTR id;
390     switch( type )
391     {
392     default:
393     case kDefaultArrow: id = IDC_ARROW;    break;
394     case kResizeNWSE:   id = IDC_SIZENWSE; break;
395     case kResizeNS:     id = IDC_SIZENS;   break;
396     case kResizeWE:     id = IDC_SIZEWE;   break;
397     case kResizeNESW:   id = IDC_SIZENESW; break;
398     }
399
400     HCURSOR hCurs = LoadCursor( NULL, id );
401     SetCursor( hCurs );
402 }
403
404
405 void Win32Factory::rmDir( const string &rPath )
406 {
407     WIN32_FIND_DATA find;
408     string file;
409     string findFiles = rPath + "\\*";
410     HANDLE handle    = FindFirstFile( findFiles.c_str(), &find );
411
412     while( handle != INVALID_HANDLE_VALUE )
413     {
414         // If file is neither "." nor ".."
415         if( strcmp( find.cFileName, "." ) && strcmp( find.cFileName, ".." ) )
416         {
417             // Set file name
418             file = rPath + "\\" + (string)find.cFileName;
419
420             // If file is a directory, delete it recursively
421             if( find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
422             {
423                 rmDir( file );
424             }
425             // Else, it is a file so simply delete it
426             else
427             {
428                 DeleteFile( file.c_str() );
429             }
430         }
431
432         // If no more file in directory, exit while
433         if( !FindNextFile( handle, &find ) )
434             break;
435     }
436
437     // Now directory is empty so can be removed
438     FindClose( handle );
439     RemoveDirectory( rPath.c_str() );
440 }
441
442 #endif