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