]> git.sesse.net Git - vlc/blob - modules/gui/skins2/win32/win32_factory.cpp
* modules/gui/skins2/win32/win32_factory.cpp: implement minimize() on win32 (I need...
[vlc] / modules / gui / skins2 / win32 / win32_factory.cpp
1 /*****************************************************************************
2  * win32_factory.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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_loop.hpp"
33 #include "../src/theme.hpp"
34
35
36 LRESULT CALLBACK Win32Proc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
37 {
38     // Get pointer to thread info: should only work with the parent window
39     intf_thread_t *p_intf = (intf_thread_t *)GetWindowLongPtr( hwnd,
40         GWLP_USERDATA );
41
42     // If doesn't exist, treat windows message normally
43     if( p_intf == NULL || p_intf->p_sys->p_osFactory == NULL )
44     {
45         return DefWindowProc( hwnd, uMsg, wParam, lParam );
46     }
47
48     // Here we know we are getting a message for the parent window, since it is
49     // the only one to store p_intf...
50     // Yes, it is a kludge :)
51
52 //Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( p_intf );
53 //msg_Err( p_intf, "Parent window %p %p %u %i\n", pFactory->m_hParentWindow, hwnd, uMsg, wParam );
54     // If Window is parent window
55     // XXX: this test isn't needed, see the kludge above...
56 //    if( hwnd == pFactory->m_hParentWindow )
57     {
58         if( uMsg == WM_SYSCOMMAND )
59         {
60             // If closing parent window
61             if( wParam == SC_CLOSE )
62             {
63                 Win32Loop *pLoop = (Win32Loop*)Win32Loop::instance( p_intf );
64                 pLoop->exit();
65                 return 0;
66             }
67             else
68             {
69                 msg_Err( p_intf, "WM_SYSCOMMAND %i", wParam );
70             }
71 //            if( (Event *)wParam != NULL )
72 //                ( (Event *)wParam )->SendEvent();
73 //            return 0;
74         }
75     }
76
77     // If hwnd does not match any window or message not processed
78     return DefWindowProc( hwnd, uMsg, wParam, lParam );
79 }
80
81
82 Win32Factory::Win32Factory( intf_thread_t *pIntf ):
83     OSFactory( pIntf ), TransparentBlt( NULL ), AlphaBlend( NULL ),
84     SetLayeredWindowAttributes( NULL ), m_hParentWindow( NULL ),
85     m_dirSep( "\\" )
86 {
87     // see init()
88 }
89
90
91 bool Win32Factory::init()
92 {
93     // Get instance handle
94     m_hInst = GetModuleHandle( NULL );
95     if( m_hInst == NULL )
96     {
97         msg_Err( getIntf(), "Cannot get module handle" );
98     }
99
100     // Create window class
101     WNDCLASS skinWindowClass;
102     skinWindowClass.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
103     skinWindowClass.lpfnWndProc = (WNDPROC) Win32Proc;
104     skinWindowClass.lpszClassName = "SkinWindowClass";
105     skinWindowClass.lpszMenuName = NULL;
106     skinWindowClass.cbClsExtra = 0;
107     skinWindowClass.cbWndExtra = 0;
108     skinWindowClass.hbrBackground = HBRUSH (COLOR_WINDOW);
109     skinWindowClass.hCursor = LoadCursor( NULL , IDC_ARROW );
110     skinWindowClass.hIcon = LoadIcon( m_hInst, "VLC_ICON" );
111     skinWindowClass.hInstance = m_hInst;
112
113     // Register class and check it
114     if( !RegisterClass( &skinWindowClass ) )
115     {
116         WNDCLASS wndclass;
117
118         // Check why it failed. If it's because the class already exists
119         // then fine, otherwise return with an error.
120         if( !GetClassInfo( m_hInst, "SkinWindowClass", &wndclass ) )
121         {
122             msg_Err( getIntf(), "Cannot register window class" );
123             return false;
124         }
125     }
126
127     // Create Window
128     m_hParentWindow = CreateWindowEx( WS_EX_APPWINDOW, "SkinWindowClass",
129         "VLC media player", WS_SYSMENU|WS_POPUP, -200, -200, 0, 0, 0, 0, m_hInst, 0 );
130     if( m_hParentWindow == NULL )
131     {
132         msg_Err( getIntf(), "Cannot create parent window" );
133         return false;
134     }
135
136     // We do it this way otherwise CreateWindowEx will fail
137     // if WS_EX_LAYERED is not supported
138     SetWindowLongPtr( m_hParentWindow, GWL_EXSTYLE,
139                       GetWindowLong( m_hParentWindow, GWL_EXSTYLE )
140                       | WS_EX_LAYERED );
141
142     // Store with it a pointer to the interface thread
143     SetWindowLongPtr( m_hParentWindow, GWLP_USERDATA, (LONG_PTR)getIntf() );
144     ShowWindow( m_hParentWindow, SW_SHOW );
145
146     // Initialize the OLE library (for drag & drop)
147     OleInitialize( NULL );
148
149     // We dynamically load msimg32.dll to get a pointer to TransparentBlt()
150     m_hMsimg32 = LoadLibrary( "msimg32.dll" );
151     if( !m_hMsimg32 ||
152         !( TransparentBlt =
153             (BOOL (WINAPI*)(HDC, int, int, int, int,
154                             HDC, int, int, int, int, unsigned int))
155             GetProcAddress( m_hMsimg32, "TransparentBlt" ) ) )
156     {
157         TransparentBlt = NULL;
158         msg_Dbg( getIntf(), "Couldn't find TransparentBlt(), "
159                  "falling back to BitBlt()" );
160     }
161     if( !m_hMsimg32 ||
162         !( AlphaBlend =
163             (BOOL (WINAPI*)( HDC, int, int, int, int, HDC, int, int,
164                               int, int, BLENDFUNCTION ))
165             GetProcAddress( m_hMsimg32, "AlphaBlend" ) ) )
166     {
167         AlphaBlend = NULL;
168         msg_Dbg( getIntf(), "Couldn't find AlphaBlend()" );
169     }
170
171     // Idem for user32.dll and SetLayeredWindowAttributes()
172     m_hUser32 = LoadLibrary( "user32.dll" );
173     if( !m_hUser32 ||
174         !( SetLayeredWindowAttributes =
175             (BOOL (WINAPI *)(HWND, COLORREF, BYTE, DWORD))
176             GetProcAddress( m_hUser32, "SetLayeredWindowAttributes" ) ) )
177     {
178         SetLayeredWindowAttributes = NULL;
179         msg_Dbg( getIntf(), "Couldn't find SetLayeredWindowAttributes()" );
180     }
181
182     // Initialize the resource path
183     m_resourcePath.push_back( (string)getIntf()->p_vlc->psz_homedir +
184                                "\\" + CONFIG_DIR + "\\skins2" );
185     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
186                               "\\skins2" );
187     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
188                               "\\share\\skins2" );
189
190     // All went well
191     return true;
192 }
193
194
195 Win32Factory::~Win32Factory()
196 {
197     // Uninitialize the OLE library
198     OleUninitialize();
199
200     if( m_hParentWindow ) DestroyWindow( m_hParentWindow );
201
202     // Unload msimg32.dll and user32.dll
203     if( m_hMsimg32 )
204         FreeLibrary( m_hMsimg32 );
205     if( m_hUser32 )
206         FreeLibrary( m_hUser32 );
207 }
208
209
210 OSGraphics *Win32Factory::createOSGraphics( int width, int height )
211 {
212     return new Win32Graphics( getIntf(), width, height );
213 }
214
215
216 OSLoop *Win32Factory::getOSLoop()
217 {
218     return Win32Loop::instance( getIntf() );
219 }
220
221
222 void Win32Factory::destroyOSLoop()
223 {
224     Win32Loop::destroy( getIntf() );
225 }
226
227 void Win32Factory::minimize()
228 {
229     /* Make sure no tooltip is visible first */
230     getIntf()->p_sys->p_theme->getWindowManager().hideTooltip();
231
232     ShowWindow( m_hParentWindow, SW_MINIMIZE );
233 }
234
235 OSTimer *Win32Factory::createOSTimer( const Callback &rCallback )
236 {
237     return new Win32Timer( getIntf(), rCallback, m_hParentWindow );
238 }
239
240
241 OSWindow *Win32Factory::createOSWindow( GenericWindow &rWindow, bool dragDrop,
242                                         bool playOnDrop, OSWindow *pParent )
243 {
244     return new Win32Window( getIntf(), rWindow, m_hInst, m_hParentWindow,
245                             dragDrop, playOnDrop, (Win32Window*)pParent );
246 }
247
248
249 OSTooltip *Win32Factory::createOSTooltip()
250 {
251     return new Win32Tooltip( getIntf(), m_hInst, m_hParentWindow );
252 }
253
254
255 int Win32Factory::getScreenWidth() const
256 {
257     return GetSystemMetrics(SM_CXSCREEN);
258
259 }
260
261
262 int Win32Factory::getScreenHeight() const
263 {
264     return GetSystemMetrics(SM_CYSCREEN);
265 }
266
267
268 Rect Win32Factory::getWorkArea() const
269 {
270     RECT r;
271     SystemParametersInfo( SPI_GETWORKAREA, 0, &r, 0 );
272     // Fill a Rect object
273     Rect rect( r.left, r.top, r.right, r.bottom );
274     return rect;
275 }
276
277
278 void Win32Factory::getMousePos( int &rXPos, int &rYPos ) const
279 {
280     POINT mousePos;
281     GetCursorPos( &mousePos );
282     rXPos = mousePos.x;
283     rYPos = mousePos.y;
284 }
285
286
287 void Win32Factory::changeCursor( CursorType_t type ) const
288 {
289     LPCTSTR id;
290     switch( type )
291     {
292         case kDefaultArrow:
293             id = IDC_ARROW;
294             break;
295         case kResizeNWSE:
296             id = IDC_SIZENWSE;
297             break;
298         case kResizeNS:
299             id = IDC_SIZENS;
300             break;
301         case kResizeWE:
302             id = IDC_SIZEWE;
303             break;
304         case kResizeNESW:
305             id = IDC_SIZENESW;
306             break;
307         default:
308             id = IDC_ARROW;
309             break;
310     }
311
312     HCURSOR hCurs = LoadCursor( NULL, id );
313     SetCursor( hCurs );
314 }
315
316
317 void Win32Factory::rmDir( const string &rPath )
318 {
319     WIN32_FIND_DATA find;
320     string file;
321     string findFiles = rPath + "\\*";
322     HANDLE handle    = FindFirstFile( findFiles.c_str(), &find );
323
324     while( handle != INVALID_HANDLE_VALUE )
325     {
326         // If file is neither "." nor ".."
327         if( strcmp( find.cFileName, "." ) && strcmp( find.cFileName, ".." ) )
328         {
329             // Set file name
330             file = rPath + "\\" + (string)find.cFileName;
331
332             // If file is a directory, delete it recursively
333             if( find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
334             {
335                 rmDir( file );
336             }
337             // Else, it is a file so simply delete it
338             else
339             {
340                 DeleteFile( file.c_str() );
341             }
342         }
343
344         // If no more file in directory, exit while
345         if( !FindNextFile( handle, &find ) )
346             break;
347     }
348
349     // Now directory is empty so can be removed
350     FindClose( handle );
351     RemoveDirectory( rPath.c_str() );
352 }
353
354
355 #endif