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

LiveListSizer.cpp

Go to the documentation of this file.
00001 //
00002 // LiveListSizer.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 "Settings.h"
00025 #include "LiveListSizer.h"
00026 
00027 #ifdef _DEBUG
00028 #undef THIS_FILE
00029 static char THIS_FILE[]=__FILE__;
00030 #define new DEBUG_NEW
00031 #endif
00032 
00033 
00035 // CLiveListSizer construction
00036 
00037 CLiveListSizer::CLiveListSizer(CListCtrl* pCtrl)
00038 {
00039         m_pCtrl         = NULL;
00040         m_nWidth        = 0;
00041         m_nColumns      = 0;
00042         m_pWidth        = NULL;
00043         m_pTake         = NULL;
00044         if ( pCtrl ) Attach( pCtrl );
00045 }
00046 
00047 CLiveListSizer::~CLiveListSizer()
00048 {
00049         Detach();
00050 }
00051 
00053 // CLiveListSizer attach and detach
00054 
00055 void CLiveListSizer::Attach(CListCtrl* pCtrl, BOOL bScale)
00056 {
00057         Detach();
00058         m_pCtrl = pCtrl;
00059         if ( pCtrl && bScale ) Resize( 0, TRUE );
00060 }
00061 
00062 void CLiveListSizer::Detach()
00063 {
00064         m_pCtrl         = NULL;
00065         m_nWidth        = 0;
00066         m_nColumns      = 0;
00067         if ( m_pWidth ) delete [] m_pWidth;
00068         m_pWidth = NULL;
00069         if ( m_pTake ) delete [] m_pTake;
00070         m_pTake = NULL;
00071 }
00072 
00074 // CLiveListSizer scaling resize
00075 
00076 BOOL CLiveListSizer::Resize(int nWidth, BOOL bScale)
00077 {
00078         if ( m_pCtrl == NULL ) return FALSE;
00079         if ( ! Settings.General.SizeLists ) return FALSE;
00080 
00081         if ( ! nWidth )
00082         {
00083                 CRect rc;
00084                 m_pCtrl->GetClientRect( &rc );
00085                 nWidth = rc.right;
00086         }
00087 
00088         nWidth -= GetSystemMetrics( SM_CXVSCROLL ) - 1;
00089 
00090         if ( nWidth < 64 ) return FALSE;
00091 
00092         LV_COLUMN pColumn;
00093         pColumn.mask = LVCF_WIDTH;
00094     int nColumn = 0;
00095         for ( ; m_pCtrl->GetColumn( nColumn, &pColumn ) ; nColumn++ );
00096 
00097         if ( nColumn != m_nColumns )
00098         {
00099                 if ( m_pWidth ) delete [] m_pWidth;
00100                 if ( m_pTake ) delete [] m_pTake;
00101 
00102                 m_pWidth        = new int[ m_nColumns = nColumn ];
00103                 m_pTake         = new float[ m_nColumns ];
00104         }
00105 
00106         if ( ! m_nWidth ) m_nWidth = nWidth;
00107 
00108         float nTotal = 0;
00109 
00110         for ( nColumn = 0 ; nColumn < m_nColumns ; nColumn++ )
00111         {
00112                 m_pCtrl->GetColumn( nColumn, &pColumn );
00113 
00114                 if ( pColumn.cx != m_pWidth[ nColumn ] )
00115                 {
00116                         m_pWidth[ nColumn ]     = pColumn.cx;
00117                         m_pTake[ nColumn ]      = (float)pColumn.cx / (float)m_nWidth;
00118                 }
00119 
00120                 nTotal += m_pTake[ nColumn ];
00121         }
00122 
00123         if ( nTotal > 1 || ( bScale && nTotal > 0 ) )
00124         {
00125                 for ( nColumn = 0 ; nColumn < m_nColumns ; nColumn++ )
00126                 {
00127                         m_pTake[ nColumn ] /= nTotal;
00128                 }
00129                 m_nWidth = 0;
00130         }
00131 
00132         if ( m_nWidth == nWidth ) return FALSE;
00133         m_nWidth = nWidth;
00134 
00135         for ( nColumn = 0 ; nColumn < m_nColumns ; nColumn++ )
00136         {
00137                 pColumn.cx = (int)( m_pTake[ nColumn ] * (float)m_nWidth );
00138                 m_pWidth[ nColumn ] = pColumn.cx;
00139                 m_pCtrl->SetColumn( nColumn, &pColumn );
00140         }
00141 
00142         return TRUE;
00143 }

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