Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

CoolInterface.cpp

Go to the documentation of this file.
00001 //
00002 // CoolInterface.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "CoolInterface.h"
00025 
00026 #ifdef _DEBUG
00027 #undef THIS_FILE
00028 static char THIS_FILE[]=__FILE__;
00029 #define new DEBUG_NEW
00030 #endif
00031 
00032 CCoolInterface CoolInterface;
00033 
00034 
00036 // CCoolInterface construction
00037 
00038 CCoolInterface::CCoolInterface()
00039 {
00040         CreateFonts();
00041         CalculateColours();
00042         
00043         m_czBuffer = CSize( 0, 0 );
00044 }
00045 
00046 CCoolInterface::~CCoolInterface()
00047 {
00048         Clear();
00049 }
00050 
00052 // CCoolInterface clear
00053 
00054 void CCoolInterface::Clear()
00055 {
00056         m_pNameMap.RemoveAll();
00057         m_pImageMap.RemoveAll();
00058         
00059         if ( m_pImages.m_hImageList )
00060         {
00061                 m_pImages.DeleteImageList();
00062         }
00063         
00064         if ( m_bmBuffer.m_hObject != NULL )
00065         {
00066                 m_dcBuffer.SelectObject( CGdiObject::FromHandle( m_bmOldBuffer ) );
00067                 m_dcBuffer.DeleteDC();
00068                 m_bmBuffer.DeleteObject();
00069         }
00070         
00071         m_czBuffer = CSize( 0, 0 );
00072 }
00073 
00075 // CCoolInterface command management
00076 
00077 void CCoolInterface::NameCommand(UINT nID, LPCTSTR pszName)
00078 {
00079         m_pNameMap.SetAt( pszName, (LPVOID)nID );
00080 }
00081 
00082 UINT CCoolInterface::NameToID(LPCTSTR pszName)
00083 {
00084         UINT nID = 0;
00085         if ( m_pNameMap.Lookup( pszName, (void*&)nID ) ) return nID;
00086         if ( _stscanf( pszName, _T("%u"), &nID ) == 1 ) return nID;
00087         return 0;
00088 }
00089 
00091 // CCoolInterface image management
00092 
00093 int CCoolInterface::ImageForID(UINT nID)
00094 {
00095         int nImage;
00096         if ( m_pImageMap.Lookup( (LPVOID)nID, (void*&)nImage ) ) return nImage;
00097         return -1;
00098 }
00099 
00100 void CCoolInterface::AddIcon(UINT nID, HICON hIcon)
00101 {
00102         ConfirmImageList();
00103         int nImage = m_pImages.Add( hIcon );
00104         m_pImageMap.SetAt( (LPVOID)nID, (LPVOID)nImage );
00105 }
00106 
00107 void CCoolInterface::CopyIcon(UINT nFromID, UINT nToID)
00108 {
00109         int nImage;
00110         if ( m_pImageMap.Lookup( (LPVOID)nFromID, (void*&)nImage ) )
00111                 m_pImageMap.SetAt( (LPVOID)nToID, (LPVOID)nImage );
00112 }
00113 
00114 HICON CCoolInterface::ExtractIcon(UINT nID)
00115 {
00116         int nImage = ImageForID( nID );
00117         if ( nImage >= 0 ) return m_pImages.ExtractIcon( nImage );
00118         return NULL;
00119 }
00120 
00121 BOOL CCoolInterface::AddImagesFromToolbar(UINT nIDToolBar, COLORREF crBack)
00122 {
00123         ConfirmImageList();
00124         
00125         CBitmap pBmp;
00126         if ( ! pBmp.LoadBitmap( nIDToolBar ) ) return FALSE;
00127         int nBase = m_pImages.Add( &pBmp, crBack );
00128         pBmp.DeleteObject();
00129         
00130         if ( nBase < 0 ) return FALSE;
00131         
00132         HRSRC hRsrc = FindResource( AfxGetInstanceHandle(), MAKEINTRESOURCE(nIDToolBar), RT_TOOLBAR );
00133         if ( hRsrc == NULL ) return FALSE;
00134         
00135         HGLOBAL hGlobal = LoadResource( AfxGetInstanceHandle(), hRsrc );
00136         if ( hGlobal == NULL ) return FALSE;
00137         
00138         TOOLBAR_RES* pData = (TOOLBAR_RES*)LockResource( hGlobal );
00139         
00140         if ( pData == NULL )
00141         {
00142                 FreeResource( hGlobal );
00143                 return FALSE;
00144         }
00145         
00146         for ( WORD nItem = 0 ; nItem < pData->wItemCount ; nItem++ )
00147         {
00148                 if ( pData->items()[ nItem ] != ID_SEPARATOR )
00149                 {
00150                         m_pImageMap.SetAt( (LPVOID)(DWORD)pData->items()[ nItem ],
00151                                 (LPVOID)nBase );
00152                         nBase++;
00153                 }
00154         }
00155         
00156         UnlockResource( hGlobal );
00157         FreeResource( hGlobal );
00158         
00159         return TRUE;
00160 }
00161 
00162 BOOL CCoolInterface::ConfirmImageList()
00163 {
00164         if ( m_pImages.m_hImageList != NULL ) return TRUE;
00165         
00166         if ( m_pImages.Create( 16, 16, ILC_COLOR16|ILC_MASK, 16, 4 ) ) return TRUE;
00167         else return m_pImages.Create( 16, 16, ILC_COLOR24|ILC_MASK, 16, 4 );
00168 }
00169 
00171 // CCoolInterface double buffer
00172 
00173 CDC* CCoolInterface::GetBuffer(CDC& dcScreen, CSize& szItem)
00174 {
00175         if ( szItem.cx <= m_czBuffer.cx && szItem.cy <= m_czBuffer.cy )
00176         {
00177                 m_dcBuffer.SelectClipRgn( NULL );
00178                 return &m_dcBuffer;
00179         }
00180         
00181         if ( m_bmBuffer.m_hObject )
00182         {
00183                 m_dcBuffer.SelectObject( CGdiObject::FromHandle( m_bmOldBuffer ) );
00184                 m_bmBuffer.DeleteObject();
00185         }
00186         
00187         m_czBuffer.cx = max( m_czBuffer.cx, szItem.cx );
00188         m_czBuffer.cy = max( m_czBuffer.cy, szItem.cy );
00189         
00190         if ( m_dcBuffer.m_hDC == NULL ) m_dcBuffer.CreateCompatibleDC( &dcScreen );
00191         m_bmBuffer.CreateCompatibleBitmap( &dcScreen, m_czBuffer.cx, m_czBuffer.cy );
00192         m_bmOldBuffer = (HBITMAP)m_dcBuffer.SelectObject( &m_bmBuffer )->GetSafeHandle();
00193         
00194         return &m_dcBuffer;
00195 }
00196 
00198 // CCoolInterface watermarking
00199 
00200 BOOL CCoolInterface::DrawWatermark(CDC* pDC, CRect* pRect, CBitmap* pMark, int nOffX, int nOffY)
00201 {
00202         BITMAP pWatermark;
00203         CBitmap* pOldMark;
00204         CDC dcMark;
00205         
00206         if ( pDC == NULL || pRect == NULL || pMark == NULL || pMark->m_hObject == NULL )
00207                 return FALSE;
00208         
00209         dcMark.CreateCompatibleDC( pDC );
00210         if ( theApp.m_bRTL ) theApp.m_pfnSetLayout( dcMark.m_hDC, LAYOUT_BITMAPORIENTATIONPRESERVED );
00211         pOldMark = (CBitmap*)dcMark.SelectObject( pMark );
00212         pMark->GetBitmap( &pWatermark );
00213         
00214         for ( int nY = pRect->top - nOffY ; nY < pRect->bottom ; nY += pWatermark.bmHeight )
00215         {
00216                 if ( nY + pWatermark.bmHeight < pRect->top ) continue;
00217                 
00218                 for ( int nX = pRect->left - nOffX ; nX < pRect->right ; nX += pWatermark.bmWidth )
00219                 {
00220                         if ( nX + pWatermark.bmWidth < pRect->left ) continue;
00221                         
00222                         pDC->BitBlt( nX, nY, pWatermark.bmWidth, pWatermark.bmHeight, &dcMark, 0, 0, SRCCOPY );
00223                 }
00224         }
00225         
00226         dcMark.SelectObject( pOldMark );
00227         dcMark.DeleteDC();
00228         
00229         return TRUE;
00230 }
00231 
00233 // CCoolInterface fonts
00234 
00235 void CCoolInterface::CreateFonts(LPCTSTR pszFace, int nSize)
00236 {
00237         if ( ! pszFace ) pszFace = theApp.m_sDefaultFont;
00238         if ( ! nSize ) nSize = theApp.m_nDefaultFontSize;
00239         
00240         if ( m_fntNormal.m_hObject ) m_fntNormal.DeleteObject();
00241         if ( m_fntBold.m_hObject ) m_fntBold.DeleteObject();
00242         if ( m_fntUnder.m_hObject ) m_fntUnder.DeleteObject();
00243         if ( m_fntCaption.m_hObject ) m_fntCaption.DeleteObject();
00244         if ( m_fntItalic.m_hObject ) m_fntItalic.DeleteObject();
00245         if ( m_fntBoldItalic.m_hObject ) m_fntBoldItalic.DeleteObject();
00246         
00247         m_fntNormal.CreateFont( -nSize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
00248                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
00249                 DEFAULT_PITCH|FF_DONTCARE, pszFace );
00250         
00251         m_fntBold.CreateFont( -nSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
00252                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
00253                 DEFAULT_PITCH|FF_DONTCARE, pszFace );
00254         
00255         m_fntUnder.CreateFont( -nSize, 0, 0, 0, FW_NORMAL, FALSE, TRUE, FALSE,
00256                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
00257                 DEFAULT_PITCH|FF_DONTCARE, pszFace );
00258         
00259         m_fntCaption.CreateFont( -nSize - 2, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
00260                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
00261                 DEFAULT_PITCH|FF_DONTCARE, pszFace );
00262 
00263         m_fntItalic.CreateFont( -nSize, 0, 0, 0, FW_NORMAL, TRUE, FALSE, FALSE,
00264                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
00265                 DEFAULT_PITCH|FF_DONTCARE, pszFace );
00266 
00267         m_fntBoldItalic.CreateFont( -nSize, 0, 0, 0, FW_BOLD, TRUE, FALSE, FALSE,
00268                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
00269                 DEFAULT_PITCH|FF_DONTCARE, pszFace );
00270 }
00271 
00273 // CCoolInterface colours
00274 
00275 void CCoolInterface::CalculateColours(BOOL bCustom)
00276 {
00277         if ( ! ( m_bCustom = bCustom ) )
00278         {
00279                 m_crWindow              = GetSysColor( COLOR_WINDOW );
00280                 m_crMidtone             = GetSysColor( COLOR_BTNFACE );
00281                 m_crHighlight   = GetSysColor( COLOR_HIGHLIGHT );
00282                 m_crText                = GetSysColor( COLOR_WINDOWTEXT );
00283                 m_crHiText              = GetSysColor( COLOR_HIGHLIGHTTEXT );
00284         }
00285         
00286         m_crBackNormal                  = CalculateColour( m_crMidtone, m_crWindow, 215 );
00287         m_crBackSel                             = CalculateColour( m_crHighlight, m_crWindow, 178 );
00288         m_crBackCheck                   = CalculateColour( m_crHighlight, m_crWindow, 200 );
00289         m_crBackCheckSel                = CalculateColour( m_crHighlight, m_crWindow, 127 );
00290         m_crMargin                              = CalculateColour( m_crMidtone, m_crWindow, 39 );
00291         m_crShadow                              = CalculateColour( m_crHighlight, GetSysColor( COLOR_3DSHADOW ), 200 );
00292         m_crBorder                              = m_crHighlight;
00293         m_crCmdText                             = GetSysColor( COLOR_MENUTEXT );
00294         m_crCmdTextSel                  = GetSysColor( COLOR_MENUTEXT );
00295         m_crDisabled                    = GetSysColor( COLOR_GRAYTEXT );
00296         
00297         m_crTipBack                             = GetSysColor( COLOR_INFOBK );
00298         m_crTipText                             = GetSysColor( COLOR_INFOTEXT );
00299         m_crTipBorder                   = CalculateColour( m_crTipBack, (COLORREF)0, 100 );
00300         m_crTipWarnings                 = RGB( 64, 64, 64 ); // Colour of warning messages (Grey)
00301         
00302         m_crTaskPanelBack               = RGB( 122, 161, 230 );
00303         m_crTaskBoxCaptionBack  = RGB( 250, 250, 255 );
00304         m_crTaskBoxPrimaryBack  = RGB( 30, 87, 199 );
00305         m_crTaskBoxCaptionText  = RGB( 34, 93, 217 );
00306         m_crTaskBoxPrimaryText  = RGB( 255, 255, 255 );
00307         m_crTaskBoxCaptionHover = RGB( 84, 143, 255 );
00308         m_crTaskBoxClient               = RGB( 214, 223, 247 );
00309 }
00310 
00311 void CCoolInterface::OnSysColourChange()
00312 {
00313         if ( ! m_bCustom ) CalculateColours();
00314 }
00315 
00316 COLORREF CCoolInterface::CalculateColour(COLORREF crFore, COLORREF crBack, int nAlpha)
00317 {
00318         int nRed        = GetRValue( crFore ) * ( 255 - nAlpha ) / 255 + GetRValue( crBack ) * nAlpha / 255;
00319         int nGreen      = GetGValue( crFore ) * ( 255 - nAlpha ) / 255 + GetGValue( crBack ) * nAlpha / 255;
00320         int nBlue       = GetBValue( crFore ) * ( 255 - nAlpha ) / 255 + GetBValue( crBack ) * nAlpha / 255;
00321 
00322         return RGB( nRed, nGreen, nBlue );
00323 }
00324 
00325 COLORREF CCoolInterface::GetDialogBkColor()
00326 {
00327         return CalculateColour( GetSysColor( COLOR_BTNFACE ), GetSysColor( COLOR_WINDOW ), 200 );
00328 }
00329 
00331 // CCoolInterface windows version check
00332 
00333 BOOL CCoolInterface::IsNewWindows()
00334 {
00335         OSVERSIONINFO pVersion;
00336         pVersion.dwOSVersionInfoSize = sizeof(pVersion);
00337         GetVersionEx( &pVersion );
00338 
00339         return pVersion.dwMajorVersion > 5 || ( pVersion.dwMajorVersion == 5 && pVersion.dwMinorVersion >= 1 );
00340 }
00341 
00343 // CCoolInterface windows XP+ themes
00344 
00345 BOOL CCoolInterface::EnableTheme(CWnd* pWnd, BOOL bEnable)
00346 {
00347         HINSTANCE hTheme = LoadLibrary( _T("UxTheme.dll") );
00348         if ( ! hTheme ) return FALSE;
00349 
00350         HRESULT (WINAPI *pfnSetWindowTheme)(HWND, LPCWSTR, LPCWSTR);
00351 
00352         (FARPROC&)pfnSetWindowTheme = GetProcAddress( hTheme, "SetWindowTheme" );
00353 
00354         BOOL bResult = FALSE;
00355 
00356         if ( pfnSetWindowTheme )
00357         {
00358                 if ( bEnable )
00359                 {
00360                         bResult = SUCCEEDED( (*pfnSetWindowTheme)( pWnd->GetSafeHwnd(), NULL, NULL ) );
00361                 }
00362                 else
00363                 {
00364                         bResult = SUCCEEDED( (*pfnSetWindowTheme)( pWnd->GetSafeHwnd(), L" ", L" " ) );
00365                 }
00366         }
00367 
00368         FreeLibrary( hTheme );
00369 
00370         return bResult;
00371 }
00372 

Generated on Thu Dec 15 10:39:34 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2