Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

win32_factory.cpp

00001 /*****************************************************************************
00002  * win32_factory.cpp
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: win32_factory.cpp 12207 2005-08-15 15:54:32Z asmax $
00006  *
00007  * Authors: Cyril Deguet     <[email protected]>
00008  *          Olivier Teulière <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 #ifdef WIN32_SKINS
00026 
00027 #include "win32_factory.hpp"
00028 #include "win32_graphics.hpp"
00029 #include "win32_timer.hpp"
00030 #include "win32_window.hpp"
00031 #include "win32_tooltip.hpp"
00032 #include "win32_loop.hpp"
00033 #include "../src/theme.hpp"
00034 
00035 
00036 LRESULT CALLBACK Win32Proc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
00037 {
00038     // Get pointer to thread info: should only work with the parent window
00039     intf_thread_t *p_intf = (intf_thread_t *)GetWindowLongPtr( hwnd,
00040         GWLP_USERDATA );
00041 
00042     // If doesn't exist, treat windows message normally
00043     if( p_intf == NULL || p_intf->p_sys->p_osFactory == NULL )
00044     {
00045         return DefWindowProc( hwnd, uMsg, wParam, lParam );
00046     }
00047 
00048     // Here we know we are getting a message for the parent window, since it is
00049     // the only one to store p_intf...
00050     // Yes, it is a kludge :)
00051 
00052 //Win32Factory *pFactory = (Win32Factory*)Win32Factory::instance( p_intf );
00053 //msg_Err( p_intf, "Parent window %p %p %u %i\n", pFactory->m_hParentWindow, hwnd, uMsg, wParam );
00054     // If Window is parent window
00055     // XXX: this test isn't needed, see the kludge above...
00056 //    if( hwnd == pFactory->m_hParentWindow )
00057     {
00058         if( uMsg == WM_SYSCOMMAND )
00059         {
00060             // If closing parent window
00061             if( wParam == SC_CLOSE )
00062             {
00063                 Win32Loop *pLoop = (Win32Loop*)Win32Loop::instance( p_intf );
00064                 pLoop->exit();
00065                 return 0;
00066             }
00067             else
00068             {
00069                 msg_Err( p_intf, "WM_SYSCOMMAND %i", wParam );
00070             }
00071 //            if( (Event *)wParam != NULL )
00072 //                ( (Event *)wParam )->SendEvent();
00073 //            return 0;
00074         }
00075     }
00076 
00077     // If hwnd does not match any window or message not processed
00078     return DefWindowProc( hwnd, uMsg, wParam, lParam );
00079 }
00080 
00081 
00082 Win32Factory::Win32Factory( intf_thread_t *pIntf ):
00083     OSFactory( pIntf ), TransparentBlt( NULL ), AlphaBlend( NULL ),
00084     SetLayeredWindowAttributes( NULL ), m_hParentWindow( NULL ),
00085     m_dirSep( "\\" )
00086 {
00087     // see init()
00088 }
00089 
00090 
00091 bool Win32Factory::init()
00092 {
00093     // Get instance handle
00094     m_hInst = GetModuleHandle( NULL );
00095     if( m_hInst == NULL )
00096     {
00097         msg_Err( getIntf(), "Cannot get module handle" );
00098     }
00099 
00100     // Create window class
00101     WNDCLASS skinWindowClass;
00102     skinWindowClass.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
00103     skinWindowClass.lpfnWndProc = (WNDPROC) Win32Proc;
00104     skinWindowClass.lpszClassName = _T("SkinWindowClass");
00105     skinWindowClass.lpszMenuName = NULL;
00106     skinWindowClass.cbClsExtra = 0;
00107     skinWindowClass.cbWndExtra = 0;
00108     skinWindowClass.hbrBackground = NULL;
00109     skinWindowClass.hCursor = LoadCursor( NULL , IDC_ARROW );
00110     skinWindowClass.hIcon = LoadIcon( m_hInst, _T("VLC_ICON") );
00111     skinWindowClass.hInstance = m_hInst;
00112 
00113     // Register class and check it
00114     if( !RegisterClass( &skinWindowClass ) )
00115     {
00116         WNDCLASS wndclass;
00117 
00118         // Check why it failed. If it's because the class already exists
00119         // then fine, otherwise return with an error.
00120         if( !GetClassInfo( m_hInst, _T("SkinWindowClass"), &wndclass ) )
00121         {
00122             msg_Err( getIntf(), "Cannot register window class" );
00123             return false;
00124         }
00125     }
00126 
00127     // Create Window
00128     m_hParentWindow = CreateWindowEx( WS_EX_APPWINDOW, _T("SkinWindowClass"),
00129         _T("VLC media player"), WS_SYSMENU|WS_POPUP,
00130         -200, -200, 0, 0, 0, 0, m_hInst, 0 );
00131     if( m_hParentWindow == NULL )
00132     {
00133         msg_Err( getIntf(), "Cannot create parent window" );
00134         return false;
00135     }
00136 
00137     // We do it this way otherwise CreateWindowEx will fail
00138     // if WS_EX_LAYERED is not supported
00139     SetWindowLongPtr( m_hParentWindow, GWL_EXSTYLE,
00140                       GetWindowLong( m_hParentWindow, GWL_EXSTYLE ) |
00141                       WS_EX_LAYERED );
00142 
00143     // Store with it a pointer to the interface thread
00144     SetWindowLongPtr( m_hParentWindow, GWLP_USERDATA, (LONG_PTR)getIntf() );
00145     ShowWindow( m_hParentWindow, SW_SHOW );
00146 
00147     // Initialize the OLE library (for drag & drop)
00148     OleInitialize( NULL );
00149 
00150     // We dynamically load msimg32.dll to get a pointer to TransparentBlt()
00151     m_hMsimg32 = LoadLibrary( _T("msimg32.dll") );
00152     if( !m_hMsimg32 ||
00153         !( TransparentBlt =
00154             (BOOL (WINAPI*)(HDC, int, int, int, int,
00155                             HDC, int, int, int, int, unsigned int))
00156             GetProcAddress( m_hMsimg32, _T("TransparentBlt") ) ) )
00157     {
00158         TransparentBlt = NULL;
00159         msg_Dbg( getIntf(), "Couldn't find TransparentBlt(), "
00160                  "falling back to BitBlt()" );
00161     }
00162     if( !m_hMsimg32 ||
00163         !( AlphaBlend =
00164             (BOOL (WINAPI*)( HDC, int, int, int, int, HDC, int, int,
00165                               int, int, BLENDFUNCTION ))
00166             GetProcAddress( m_hMsimg32, _T("AlphaBlend") ) ) )
00167     {
00168         AlphaBlend = NULL;
00169         msg_Dbg( getIntf(), "Couldn't find AlphaBlend()" );
00170     }
00171 
00172     // Idem for user32.dll and SetLayeredWindowAttributes()
00173     m_hUser32 = LoadLibrary( _T("user32.dll") );
00174     if( !m_hUser32 ||
00175         !( SetLayeredWindowAttributes =
00176             (BOOL (WINAPI *)(HWND, COLORREF, BYTE, DWORD))
00177             GetProcAddress( m_hUser32, _T("SetLayeredWindowAttributes") ) ) )
00178     {
00179         SetLayeredWindowAttributes = NULL;
00180         msg_Dbg( getIntf(), "Couldn't find SetLayeredWindowAttributes()" );
00181     }
00182 
00183     // Initialize the resource path
00184     m_resourcePath.push_back( (string)getIntf()->p_vlc->psz_homedir +
00185                                "\\" + CONFIG_DIR + "\\skins" );
00186     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
00187                               "\\skins" );
00188     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
00189                               "\\skins2" );
00190     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
00191                               "\\share\\skins" );
00192     m_resourcePath.push_back( (string)getIntf()->p_libvlc->psz_vlcpath +
00193                               "\\share\\skins2" );
00194 
00195     // All went well
00196     return true;
00197 }
00198 
00199 
00200 Win32Factory::~Win32Factory()
00201 {
00202     // Uninitialize the OLE library
00203     OleUninitialize();
00204 
00205     if( m_hParentWindow ) DestroyWindow( m_hParentWindow );
00206 
00207     // Unload msimg32.dll and user32.dll
00208     if( m_hMsimg32 )
00209         FreeLibrary( m_hMsimg32 );
00210     if( m_hUser32 )
00211         FreeLibrary( m_hUser32 );
00212 }
00213 
00214 
00215 OSGraphics *Win32Factory::createOSGraphics( int width, int height )
00216 {
00217     return new Win32Graphics( getIntf(), width, height );
00218 }
00219 
00220 
00221 OSLoop *Win32Factory::getOSLoop()
00222 {
00223     return Win32Loop::instance( getIntf() );
00224 }
00225 
00226 
00227 void Win32Factory::destroyOSLoop()
00228 {
00229     Win32Loop::destroy( getIntf() );
00230 }
00231 
00232 void Win32Factory::minimize()
00233 {
00234     /* Make sure no tooltip is visible first */
00235     getIntf()->p_sys->p_theme->getWindowManager().hideTooltip();
00236 
00237     ShowWindow( m_hParentWindow, SW_MINIMIZE );
00238 }
00239 
00240 OSTimer *Win32Factory::createOSTimer( CmdGeneric &rCmd )
00241 {
00242     return new Win32Timer( getIntf(), rCmd, m_hParentWindow );
00243 }
00244 
00245 
00246 OSWindow *Win32Factory::createOSWindow( GenericWindow &rWindow, bool dragDrop,
00247                                         bool playOnDrop, OSWindow *pParent )
00248 {
00249     return new Win32Window( getIntf(), rWindow, m_hInst, m_hParentWindow,
00250                             dragDrop, playOnDrop, (Win32Window*)pParent );
00251 }
00252 
00253 
00254 OSTooltip *Win32Factory::createOSTooltip()
00255 {
00256     return new Win32Tooltip( getIntf(), m_hInst, m_hParentWindow );
00257 }
00258 
00259 
00260 int Win32Factory::getScreenWidth() const
00261 {
00262     return GetSystemMetrics(SM_CXSCREEN);
00263 
00264 }
00265 
00266 
00267 int Win32Factory::getScreenHeight() const
00268 {
00269     return GetSystemMetrics(SM_CYSCREEN);
00270 }
00271 
00272 
00273 Rect Win32Factory::getWorkArea() const
00274 {
00275     RECT r;
00276     SystemParametersInfo( SPI_GETWORKAREA, 0, &r, 0 );
00277     // Fill a Rect object
00278     Rect rect( r.left, r.top, r.right, r.bottom );
00279     return rect;
00280 }
00281 
00282 
00283 void Win32Factory::getMousePos( int &rXPos, int &rYPos ) const
00284 {
00285     POINT mousePos;
00286     GetCursorPos( &mousePos );
00287     rXPos = mousePos.x;
00288     rYPos = mousePos.y;
00289 }
00290 
00291 
00292 void Win32Factory::changeCursor( CursorType_t type ) const
00293 {
00294     LPCTSTR id;
00295     switch( type )
00296     {
00297         case kDefaultArrow:
00298             id = IDC_ARROW;
00299             break;
00300         case kResizeNWSE:
00301             id = IDC_SIZENWSE;
00302             break;
00303         case kResizeNS:
00304             id = IDC_SIZENS;
00305             break;
00306         case kResizeWE:
00307             id = IDC_SIZEWE;
00308             break;
00309         case kResizeNESW:
00310             id = IDC_SIZENESW;
00311             break;
00312         default:
00313             id = IDC_ARROW;
00314             break;
00315     }
00316 
00317     HCURSOR hCurs = LoadCursor( NULL, id );
00318     SetCursor( hCurs );
00319 }
00320 
00321 
00322 void Win32Factory::rmDir( const string &rPath )
00323 {
00324     WIN32_FIND_DATA find;
00325     string file;
00326     string findFiles = rPath + "\\*";
00327     HANDLE handle    = FindFirstFile( findFiles.c_str(), &find );
00328 
00329     while( handle != INVALID_HANDLE_VALUE )
00330     {
00331         // If file is neither "." nor ".."
00332         if( strcmp( find.cFileName, "." ) && strcmp( find.cFileName, ".." ) )
00333         {
00334             // Set file name
00335             file = rPath + "\\" + (string)find.cFileName;
00336 
00337             // If file is a directory, delete it recursively
00338             if( find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
00339             {
00340                 rmDir( file );
00341             }
00342             // Else, it is a file so simply delete it
00343             else
00344             {
00345                 DeleteFile( file.c_str() );
00346             }
00347         }
00348 
00349         // If no more file in directory, exit while
00350         if( !FindNextFile( handle, &find ) )
00351             break;
00352     }
00353 
00354     // Now directory is empty so can be removed
00355     FindClose( handle );
00356     RemoveDirectory( rPath.c_str() );
00357 }
00358 
00359 #endif

Generated on Tue Dec 20 10:14:44 2005 for vlc-0.8.4a by  doxygen 1.4.2