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

CtrlSchemaCombo.cpp

Go to the documentation of this file.
00001 //
00002 // CtrlSchemaCombo.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 "Schema.h"
00025 #include "SchemaCache.h"
00026 #include "ShellIcons.h"
00027 #include "CtrlSchemaCombo.h"
00028 #include "CoolInterface.h"
00029 #include "XML.h"
00030 
00031 #ifdef _DEBUG
00032 #define new DEBUG_NEW
00033 #undef THIS_FILE
00034 static char THIS_FILE[] = __FILE__;
00035 #endif
00036 
00037 BEGIN_MESSAGE_MAP(CSchemaCombo, CComboBox)
00038         //{{AFX_MSG_MAP(CSchemaCombo)
00039         ON_MESSAGE(WM_CTLCOLORLISTBOX, OnCtlColorListBox)
00040         ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropDown)
00041         //}}AFX_MSG_MAP
00042 END_MESSAGE_MAP()
00043 
00044 
00046 // CSchemaCombo construction
00047 
00048 CSchemaCombo::CSchemaCombo()
00049 {
00050         m_hListBox                      = 0;
00051         m_pWndProc                      = NULL;
00052 }
00053 
00054 CSchemaCombo::~CSchemaCombo()
00055 {
00056 }
00057 
00059 // CSchemaCombo operations
00060 
00061 BOOL CSchemaCombo::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID) 
00062 {
00063         dwStyle |= WS_CHILD|WS_VSCROLL|CBS_DROPDOWNLIST|CBS_OWNERDRAWVARIABLE|CBS_HASSTRINGS|CBS_SORT;
00064         return CComboBox::Create( dwStyle, rect, pParentWnd, nID );
00065 }
00066 
00067 void CSchemaCombo::SetEmptyString(UINT nID)
00068 {
00069         LoadString( m_sNoSchemaText, nID );
00070 }
00071 
00072 void CSchemaCombo::Load(LPCTSTR pszSelectURI, int nType, int nAvailability, BOOL bReset)
00073 {
00074         if ( ( GetStyle() & CBS_OWNERDRAWVARIABLE ) == 0 )
00075         {
00076                 ModifyStyle( 0, CBS_OWNERDRAWVARIABLE|CBS_HASSTRINGS );
00077         }
00078 
00079         SetExtendedUI();
00080         
00081         m_nType                 = nType;
00082         m_nAvailability = nAvailability;
00083         
00084         if ( bReset ) ResetContent();
00085         
00086         if ( bReset && m_sNoSchemaText.GetLength() )
00087         {
00088                 SetItemData( AddString( _T(" ") ), 0 );
00089                 SetCurSel( 0 );
00090         }
00091         
00092         for ( POSITION pos = SchemaCache.GetIterator() ; pos ; )
00093         {
00094                 CSchema* pSchema = SchemaCache.GetNext( pos );
00095                 
00096                 BOOL bSelected = pszSelectURI && pSchema->m_sURI.CompareNoCase( pszSelectURI ) == 0;
00097                 
00098                 if ( ! bReset )
00099                 {
00100                         int nIndex = FindParam( (DWORD)pSchema );
00101                         
00102                         if ( nIndex >= 0 )
00103                         {
00104                                 if ( bSelected ) SetCurSel( nIndex );
00105                                 continue;
00106                         }
00107                 }
00108                 
00109                 if ( ( bSelected || pSchema->m_nType == nType || nType == -1 ) &&
00110                          ( bSelected || pSchema->m_nAvailability <= nAvailability ) )
00111                 {
00112                         int nIndex = AddString( pSchema->m_sTitle );
00113                         SetItemData( nIndex, (LPARAM)pSchema );
00114                         
00115                         if ( bSelected ) SetCurSel( nIndex );
00116                 }
00117         }
00118         
00119         if ( bReset && nAvailability < CSchema::saMax )
00120         {
00121                 SetItemData( AddString( _T("ZZZ") ), 0 );
00122         }
00123 }
00124 
00125 void CSchemaCombo::Select(LPCTSTR pszURI)
00126 {
00127         for ( int nItem = 0 ; nItem < GetCount() ; nItem++ )
00128         {
00129                 CSchema* pSchema = (CSchema*)GetItemData( nItem );
00130                 
00131                 if ( pSchema != NULL && pSchema->CheckURI( pszURI ) )
00132                 {
00133                         SetCurSel( nItem );
00134                         return;
00135                 }
00136         }
00137         
00138         SetCurSel( 0 );
00139 }
00140 
00141 void CSchemaCombo::Select(CSchema* pSelect)
00142 {
00143         for ( int nItem = 0 ; nItem < GetCount() ; nItem++ )
00144         {
00145                 CSchema* pSchema = (CSchema*)GetItemData( nItem );
00146                 
00147                 if ( pSchema == pSelect )
00148                 {
00149                         SetCurSel( nItem );
00150                         return;
00151                 }
00152         }
00153         
00154         SetCurSel( 0 );
00155 }
00156 
00157 CSchema* CSchemaCombo::GetSelected() const
00158 {
00159         int nSel = GetCurSel();
00160         if ( nSel < 0 ) return NULL;
00161         return (CSchema*)GetItemData( nSel );
00162 }
00163 
00164 CString CSchemaCombo::GetSelectedURI() const
00165 {
00166         CString str;
00167         int nSel = GetCurSel();
00168         if ( nSel < 0 ) return str;
00169         if ( CSchema* pSchema = (CSchema*)GetItemData( nSel ) )
00170                 return pSchema->m_sURI;
00171         return str;
00172 }
00173 
00174 int CSchemaCombo::FindParam(DWORD nParam)
00175 {
00176         for ( int nItem = 0 ; nItem < GetCount() ; nItem++ )
00177         {
00178                 if ( GetItemData( nItem ) == nParam ) return nItem;
00179         }
00180         
00181         return -1;
00182 }
00183 
00185 // CSchemaCombo message handlers
00186 
00187 LRESULT CSchemaCombo::OnCtlColorListBox(WPARAM wParam, LPARAM lParam) 
00188 {
00189         if ( m_hListBox == 0 )
00190         {
00191                 if ( lParam != 0 && m_hWnd != (HWND)lParam )
00192                 {
00193                         m_hListBox = (HWND)lParam;
00194 
00195                         m_pWndProc = (WNDPROC)GetWindowLong( m_hListBox, GWL_WNDPROC );
00196                         SetWindowLong( m_hListBox, GWL_USERDATA, (LONG)this );
00197                         SetWindowLong( m_hListBox, GWL_WNDPROC, (LONG)ListWndProc );
00198 
00199                         ::InvalidateRect( m_hListBox, NULL, TRUE );
00200                 }
00201         }
00202         
00203         return DefWindowProc( WM_CTLCOLORLISTBOX, wParam, lParam );
00204 }
00205 
00206 void CSchemaCombo::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
00207 {
00208         lpMeasureItemStruct->itemWidth  = 1024;
00209         lpMeasureItemStruct->itemHeight = 18;
00210 }
00211 
00212 void CSchemaCombo::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
00213 {
00214         if ( lpDrawItemStruct->itemID == (UINT)-1 ) return;
00215         if ( ( lpDrawItemStruct->itemAction & ODA_SELECT ) == 0 &&
00216                  ( lpDrawItemStruct->itemAction & ODA_DRAWENTIRE ) == 0 ) return;
00217 
00218         CRect rcItem( &lpDrawItemStruct->rcItem );
00219         CPoint pt( rcItem.left + 1, rcItem.top + 1 );
00220         CDC dc;
00221         
00222         dc.Attach( lpDrawItemStruct->hDC );
00223         if ( theApp.m_bRTL ) theApp.m_pfnSetLayout( dc.m_hDC, LAYOUT_RTL );
00224         
00225         dc.SetTextColor( GetSysColor( ( lpDrawItemStruct->itemState & ODS_SELECTED )
00226                 ? COLOR_HIGHLIGHTTEXT : COLOR_MENUTEXT ) );
00227         
00228         CSchema* pSchema = (CSchema*)lpDrawItemStruct->itemData;
00229 
00230         if ( pSchema != NULL )
00231         {
00232                 /*dc.FillSolidRect( &rcItem,
00233                         GetSysColor( ( lpDrawItemStruct->itemState & ODS_SELECTED ) ? COLOR_HIGHLIGHT : COLOR_WINDOW ) );*/
00234                 if ( IsWindowEnabled() )
00235                 {
00236                         if ( lpDrawItemStruct->itemState & ODS_SELECTED ) 
00237                                 dc.FillSolidRect( &rcItem, GetSysColor( COLOR_HIGHLIGHT ) );
00238                         else 
00239                                 dc.FillSolidRect( &rcItem, GetSysColor( COLOR_WINDOW));
00240                 }
00241                 else 
00242                         dc.FillSolidRect( &rcItem, GetBkColor(lpDrawItemStruct->hDC) );
00243 
00244                 dc.SetBkMode( TRANSPARENT );
00245                 
00246                 ShellIcons.Draw( &dc, pSchema->m_nIcon16, 16, pt.x, pt.y, CLR_NONE, 
00247                         ( lpDrawItemStruct->itemState & ODS_SELECTED ) );
00248                 
00249                 rcItem.left += 20; rcItem.right -= 2;
00250                 
00251                 CFont* pOldFont = (CFont*)dc.SelectObject( &theApp.m_gdiFont );
00252                 CString strURI = pSchema->m_sURI;
00253                 
00254                 if ( dc.GetTextExtent( pSchema->m_sTitle + strURI ).cx > rcItem.Width() - 20
00255                          && strURI.GetLength() > 8 )
00256                 {
00257                         LPCTSTR pszLeft = _tcschr( (LPCTSTR)strURI + 7, '/' );
00258                         int nRight              = strURI.ReverseFind( '/' );
00259 
00260                         if ( pszLeft && nRight >= 0 )
00261                         {
00262                                 int nLeft = pszLeft - (LPCTSTR)strURI;
00263                                 strURI = strURI.Left( nLeft ) + _T("/\x2026") + strURI.Mid( nRight );
00264                         }
00265                 }
00266                 
00267                 if ( dc.GetTextExtent( pSchema->m_sTitle + strURI ).cx <= rcItem.Width() - 20 )
00268                 {
00269                         // COLORREF crBackup = dc.SetTextColor( GetSysColor( COLOR_GRAYTEXT ) );
00270                         dc.DrawText( strURI, &rcItem, DT_SINGLELINE|DT_RIGHT|DT_VCENTER|DT_NOPREFIX );
00271                         // dc.SetTextColor( crBackup );
00272                 }
00273                 
00274                 dc.SelectObject( &theApp.m_gdiFontBold );
00275                 dc.DrawText( pSchema->m_sTitle, &rcItem, DT_SINGLELINE|DT_LEFT|DT_VCENTER|DT_NOPREFIX );
00276                 dc.SelectObject( pOldFont );
00277         }
00278         else if ( lpDrawItemStruct->itemID == 0 )
00279         {
00280                 /*dc.FillSolidRect( &rcItem,
00281                         GetSysColor( ( lpDrawItemStruct->itemState & ODS_SELECTED ) ? COLOR_HIGHLIGHT : COLOR_WINDOW ) );*/
00282                 if ( IsWindowEnabled() )
00283                 {
00284                         if ( lpDrawItemStruct->itemState & ODS_SELECTED ) 
00285                                 dc.FillSolidRect( &rcItem, GetSysColor( COLOR_HIGHLIGHT ) );
00286                         else 
00287                                 dc.FillSolidRect( &rcItem, GetSysColor( COLOR_WINDOW));
00288                 }
00289                 else 
00290                         dc.FillSolidRect( &rcItem, GetBkColor(lpDrawItemStruct->hDC) );
00291                 dc.SetBkMode( TRANSPARENT );
00292                 
00293                 ShellIcons.Draw( &dc, SHI_SEARCH, 16, pt.x, pt.y, CLR_NONE, 
00294                         ( lpDrawItemStruct->itemState & ODS_SELECTED ) );
00295                 
00296                 rcItem.left += 20; rcItem.right -= 2;
00297 
00298                 CFont* pOldFont = (CFont*)dc.SelectObject( &theApp.m_gdiFontBold );
00299                 dc.DrawText( m_sNoSchemaText, &rcItem, DT_SINGLELINE|DT_LEFT|DT_VCENTER|DT_NOPREFIX );
00300                 dc.SelectObject( pOldFont );
00301         }
00302         else
00303         {
00304                 dc.Draw3dRect( &rcItem, GetSysColor( COLOR_WINDOW ), GetSysColor( COLOR_WINDOW ) );
00305                 rcItem.DeflateRect( 1, 1 );
00306                 
00307                 if ( lpDrawItemStruct->itemState & ODS_SELECTED )
00308                 {
00309                         dc.Draw3dRect( &rcItem, CoolInterface.m_crBorder, CoolInterface.m_crBorder );
00310                         rcItem.DeflateRect( 1, 1 );
00311                         dc.FillSolidRect( &rcItem, CoolInterface.m_crBackSel );
00312                 }
00313                 else
00314                 {
00315                         dc.FillSolidRect( &rcItem, GetSysColor( /* COLOR_BTNFACE */ COLOR_WINDOW ) );
00316                 }
00317                 
00318                 dc.SetBkMode( TRANSPARENT );
00319                 
00320                 pt = rcItem.CenterPoint();
00321                 pt.x -= 8;
00322                 pt.y -= 8;
00323                 
00324                 ShellIcons.Draw( &dc, SHI_CHEVRON, 16, pt.x, pt.y, CLR_NONE, FALSE );
00325         }
00326         
00327         dc.Detach();
00328 }
00329 
00330 void CSchemaCombo::OnDropDown() 
00331 {
00332         m_sPreDrop = GetSelectedURI();
00333 }
00334 
00336 // CSchemaCombo subclassed list box
00337 
00338 LRESULT PASCAL CSchemaCombo::ListWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
00339 {
00340         CSchemaCombo* pThis = (CSchemaCombo*)GetWindowLong( hWnd, GWL_USERDATA );
00341         
00342         if ( pThis->m_nAvailability < CSchema::saMax &&
00343                  ( nMsg == WM_LBUTTONDOWN || nMsg == WM_LBUTTONUP ) )
00344         {
00345                 //CPoint pt( LOWORD( lParam ), HIWORD( lParam ) );
00346                 CPoint pt( lParam );
00347                 CRect rcClient;
00348                 
00349                 ::GetClientRect( hWnd, &rcClient );
00350                 
00351                 if ( rcClient.PtInRect( pt ) )
00352                 {
00353                         int nItemHeight = ::SendMessage( hWnd, LB_GETITEMHEIGHT, 0, 0 );
00354                         int nTopIndex   = ::SendMessage( hWnd, LB_GETTOPINDEX, 0, 0 );
00355                         int nIndex              = nTopIndex + pt.y / nItemHeight;
00356                         
00357                         CRect rcItem;
00358                         ::SendMessage( hWnd, LB_GETITEMRECT, nIndex, (LONG)(VOID *)&rcItem );
00359 
00360                         if ( rcItem.PtInRect( pt ) )
00361                         {
00362                                 if ( pThis->OnClickItem( nIndex, nMsg == WM_LBUTTONDOWN ) )
00363                                 {
00364                                         if ( nMsg == WM_LBUTTONDOWN ) return 0;
00365                                         
00366                                         nIndex = pThis->GetCurSel();
00367                                         if ( nIndex >= 0 ) CallWindowProc( pThis->m_pWndProc, hWnd, LB_SETCURSEL, nIndex, 0 );
00368                                         
00369                                         ::GetWindowRect( hWnd, &rcClient );
00370                                         int nHeight = pThis->GetCount() * nItemHeight + 2;
00371                                         
00372                                         if ( rcClient.Height() < nHeight )
00373                                         {
00374                                                 rcClient.bottom = min( LONG(GetSystemMetrics( SM_CYSCREEN ) - 1),
00375                                                         LONG(rcClient.top + nHeight) );
00376                                                 
00377                                                 ::MoveWindow( hWnd, rcClient.left, rcClient.top,
00378                                                         rcClient.Width(), rcClient.Height(), TRUE );
00379                                         }
00380                                         
00381                                         return 0;
00382                                 }
00383                         }
00384                 }
00385         }
00386         else if ( pThis->m_nAvailability < CSchema::saMax && nMsg == WM_CHAR )
00387         {
00388                 if ( wParam == VK_RETURN || wParam == VK_SPACE )
00389                 {
00390                         int nIndex = CallWindowProc(    pThis->m_pWndProc, hWnd,
00391                                                                                         LB_GETCURSEL, 0, 0 );
00392                         
00393                         if ( pThis->OnClickItem( nIndex, FALSE ) ) return 0;
00394                 }
00395         }
00396 
00397         return CallWindowProc( pThis->m_pWndProc, hWnd, nMsg, wParam, lParam );
00398 }
00399 
00400 BOOL CSchemaCombo::OnClickItem(int nItem, BOOL bDown)
00401 {
00402         if ( nItem > 0 && GetItemData( nItem ) == 0 )
00403         {
00404                 if ( ! bDown )
00405                 {
00406                         DeleteString( nItem );
00407                         Load( m_sPreDrop, m_nType, CSchema::saMax, FALSE );
00408                         if ( GetCurSel() < 0 ) SetCurSel( 0 );
00409                 }
00410                 
00411                 return TRUE;
00412         }
00413         
00414         return FALSE;
00415 }

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