00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "RichDocument.h"
00026 #include "RichElement.h"
00027 #include "RichFragment.h"
00028
00029 #include "CoolInterface.h"
00030 #include "ImageServices.h"
00031
00032 #ifdef _DEBUG
00033 #undef THIS_FILE
00034 static char THIS_FILE[]=__FILE__;
00035 #define new DEBUG_NEW
00036 #endif
00037
00038
00040
00041
00042 CRichElement::CRichElement(int nType, LPCTSTR pszText, LPCTSTR pszLink, DWORD nFlags, int nGroup)
00043 {
00044 m_pDocument = NULL;
00045 m_nType = nType;
00046 m_nFlags = nFlags;
00047 m_nGroup = nGroup;
00048 m_hImage = NULL;
00049
00050 if ( m_nType == retHeading )
00051 {
00052 m_nType = retText;
00053 m_nFlags |= retfHeading;
00054 }
00055
00056 if ( pszText != NULL )
00057 {
00058 if ( ( m_nType == retBitmap || m_nType == retIcon ) && HIWORD(pszText) == 0 )
00059 {
00060 m_sText.Format( _T("%lu"), (DWORD)pszText );
00061 }
00062 else
00063 {
00064 m_sText = pszText;
00065 }
00066 }
00067
00068 if ( pszLink != NULL ) m_sLink = pszLink;
00069 }
00070
00071 CRichElement::~CRichElement()
00072 {
00073 if ( m_hImage != NULL )
00074 {
00075 if ( m_nType == retBitmap )
00076 {
00077 DeleteObject( (HBITMAP)m_hImage );
00078 m_hImage = NULL;
00079 }
00080 else if ( m_nType == retIcon )
00081 {
00082 DestroyIcon( (HICON)m_hImage );
00083 m_hImage = NULL;
00084 }
00085 }
00086 }
00087
00089
00090
00091 void CRichElement::Show(BOOL bShow)
00092 {
00093 if ( bShow == ( ( m_nFlags & retfHidden ) > 0 ) )
00094 {
00095 m_nFlags |= retfHidden;
00096 if ( bShow ) m_nFlags &= ~retfHidden;
00097 m_pDocument->m_nCookie++;
00098 }
00099 }
00100
00101 void CRichElement::SetText(LPCTSTR pszText)
00102 {
00103 if ( m_sText != pszText )
00104 {
00105 m_sText = pszText;
00106 m_pDocument->m_nCookie++;
00107 }
00108 }
00109
00110 void CRichElement::SetFlags(DWORD nFlags, DWORD nMask)
00111 {
00112 DWORD nNew = ( m_nFlags & ~nMask ) | ( nFlags & nMask );
00113
00114 if ( nNew != m_nFlags )
00115 {
00116 m_nFlags = nNew;
00117 m_pDocument->m_nCookie++;
00118 }
00119 }
00120
00122
00123
00124 void CRichElement::Delete()
00125 {
00126 if ( m_pDocument ) m_pDocument->Remove( this );
00127 delete this;
00128 }
00129
00131
00132
00133 void CRichElement::PrePaint(CDC* pDC, BOOL bHover)
00134 {
00135 if ( m_pDocument->m_fntNormal.m_hObject == NULL ) m_pDocument->CreateFonts();
00136
00137 CFont* pFont = &m_pDocument->m_fntNormal;
00138
00139 switch ( m_nType )
00140 {
00141 case retText:
00142 if ( m_nFlags & retfColour )
00143 pDC->SetTextColor( m_cColour );
00144 else
00145 pDC->SetTextColor( m_pDocument->m_crText );
00146 pFont = &m_pDocument->m_fntNormal;
00147 break;
00148 case retLink:
00149 if ( m_nFlags & retfColour )
00150 pDC->SetTextColor( m_cColour );
00151 else
00152 pDC->SetTextColor( bHover ? m_pDocument->m_crHover : m_pDocument->m_crLink );
00153 pFont = &m_pDocument->m_fntUnder;
00154 break;
00155 case retBitmap:
00156 PrePaintBitmap( pDC );
00157 pFont = NULL;
00158 break;
00159 case retIcon:
00160 PrePaintIcon( pDC );
00161 pFont = NULL;
00162 break;
00163 case retEmoticon:
00164 _stscanf( m_sText, _T("%i"), &m_hImage );
00165 pFont = NULL;
00166 break;
00167 case retCmdIcon:
00168 if ( UINT nID = CoolInterface.NameToID( m_sText ) )
00169 {
00170 m_hImage = CoolInterface.ImageForID( nID );
00171 }
00172 break;
00173 default:
00174 pFont = NULL;
00175 break;
00176 }
00177
00178 if ( m_nFlags & retfBold )
00179 {
00180 if ( m_nFlags & retfUnderline ) pFont = &m_pDocument->m_fntBoldUnder;
00181 else pFont = &m_pDocument->m_fntBold;
00182 }
00183 else if ( m_nFlags & retfItalic )
00184 {
00185 pFont = &m_pDocument->m_fntItalic;
00186 }
00187 else if ( m_nFlags & retfUnderline )
00188 {
00189 pFont = &m_pDocument->m_fntUnder;
00190 }
00191 else if ( m_nFlags & retfHeading )
00192 {
00193 pFont = &m_pDocument->m_fntHeading;
00194 pDC->SetTextColor( m_pDocument->m_crHeading );
00195 }
00196
00197 if ( pFont ) pDC->SelectObject( pFont );
00198 }
00199
00200 void CRichElement::PrePaintBitmap(CDC* pDC)
00201 {
00202 if ( m_hImage != NULL ) return;
00203
00204 if ( _tcsnicmp( m_sText, _T("res:"), 4 ) == 0 )
00205 {
00206 LPCTSTR pszDot = _tcschr( m_sText, '.' );
00207 CBitmap pBitmap;
00208 UINT nID;
00209
00210 if ( pszDot == NULL || _stscanf( (LPCTSTR)m_sText + 4, _T("%lu"), &nID ) != 1 ) return;
00211
00212 if ( CImageServices::LoadBitmap( &pBitmap, nID, pszDot + 1 ) )
00213 {
00214 m_hImage = (DWORD)pBitmap.Detach();
00215 }
00216 }
00217 else
00218 {
00219 CString strFile = Settings.General.Path + '\\' + m_sText;
00220 HBITMAP hBitmap = (HBITMAP)LoadImage( AfxGetResourceHandle(),
00221 strFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
00222 m_hImage = theApp.m_bRTL ? (DWORD)CreateMirroredBitmap( hBitmap ) : (DWORD)hBitmap;
00223 }
00224 }
00225
00226 void CRichElement::PrePaintIcon(CDC* pDC)
00227 {
00228 if ( m_hImage != NULL || m_sText.IsEmpty() ) return;
00229
00230 UINT nID, nWidth = 16, nHeight = 16;
00231 _stscanf( m_sText, _T("%lu.%i.%i"), &nID, &nWidth, &nHeight );
00232
00233 HICON hIcon = CoolInterface.ExtractIcon( nID );
00234 m_hImage = theApp.m_bRTL ? (DWORD)CreateMirroredIcon( hIcon ) : (DWORD)hIcon;
00235
00236 if ( m_hImage == NULL )
00237 {
00238 hIcon = (HICON)LoadImage( AfxGetResourceHandle(),
00239 MAKEINTRESOURCE( nID ), IMAGE_ICON, nWidth, nHeight, 0 );
00240 m_hImage = theApp.m_bRTL ? (DWORD)CreateMirroredIcon( hIcon ) : (DWORD)hIcon;
00241 }
00242 }
00243
00245
00246
00247 CSize CRichElement::GetSize()
00248 {
00249 CSize sz( 0, 0 );
00250
00251 if ( m_nType == retGap )
00252 {
00253 _stscanf( m_sText, _T("%lu"), &sz.cx );
00254 }
00255 else if ( m_nType == retBitmap && m_hImage != NULL )
00256 {
00257 BITMAP pInfo;
00258 GetObject( (HBITMAP)m_hImage, sizeof(pInfo), &pInfo );
00259
00260 sz.cx = pInfo.bmWidth;
00261 sz.cy = pInfo.bmHeight;
00262 }
00263 else if ( m_nType == retIcon )
00264 {
00265 sz.cx = sz.cy = 16;
00266 UINT nID;
00267 _stscanf( m_sText, _T("%lu.%i.%i"), &nID, &sz.cx, &sz.cy );
00268 }
00269 else if ( m_nType == retEmoticon || m_nType == retCmdIcon )
00270 {
00271 sz.cx = sz.cy = 16;
00272 }
00273 else if ( m_nType == retAnchor )
00274 {
00275 sz.cx = sz.cy = 16;
00276 _stscanf( m_sText, _T("%i.%i"), &sz.cx, &sz.cy );
00277 }
00278
00279 return sz;
00280 }