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

CtrlTaskPanel.cpp

Go to the documentation of this file.
00001 //
00002 // CtrlTaskPanel.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 #include "CtrlTaskPanel.h"
00026 
00027 #ifdef _DEBUG
00028 #define new DEBUG_NEW
00029 #undef THIS_FILE
00030 static char THIS_FILE[] = __FILE__;
00031 #endif
00032 
00033 IMPLEMENT_DYNAMIC(CTaskPanel, CWnd)
00034 
00035 BEGIN_MESSAGE_MAP(CTaskPanel, CWnd)
00036         //{{AFX_MSG_MAP(CTaskPanel)
00037         ON_WM_SIZE()
00038         ON_WM_PAINT()
00039         ON_WM_ERASEBKGND()
00040         ON_WM_CREATE()
00041         //}}AFX_MSG_MAP
00042 END_MESSAGE_MAP()
00043 
00044 IMPLEMENT_DYNAMIC(CTaskBox, CWnd)
00045 
00046 BEGIN_MESSAGE_MAP(CTaskBox, CWnd)
00047         //{{AFX_MSG_MAP(CTaskBox)
00048         ON_WM_NCCALCSIZE()
00049         ON_WM_NCPAINT()
00050         ON_WM_NCHITTEST()
00051         ON_WM_NCACTIVATE()
00052         ON_WM_PAINT()
00053         ON_WM_SYSCOMMAND()
00054         ON_WM_SETCURSOR()
00055         ON_WM_NCLBUTTONUP()
00056         ON_WM_TIMER()
00057         ON_WM_NCLBUTTONDOWN()
00058         //}}AFX_MSG_MAP
00059 END_MESSAGE_MAP()
00060 
00061 #define CAPTION_HEIGHT  25
00062 
00063 
00065 // CTaskPanel construction
00066 
00067 CTaskPanel::CTaskPanel()
00068 {
00069         m_pStretch      = NULL;
00070         m_nMargin       = 12;
00071         m_nCurve        = 2;
00072         m_bLayout       = FALSE;
00073 }
00074 
00075 CTaskPanel::~CTaskPanel()
00076 {
00077 }
00078 
00080 // CTaskPanel create
00081 
00082 BOOL CTaskPanel::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID) 
00083 {
00084         dwStyle |= WS_CHILD|WS_CLIPCHILDREN;
00085         return CWnd::Create( NULL, NULL, dwStyle, rect, pParentWnd, nID, NULL );
00086 }
00087 
00089 // CTaskPanel box list management
00090 
00091 CTaskBox* CTaskPanel::AddBox(CTaskBox* pBox, POSITION posBefore)
00092 {
00093         ASSERT ( pBox != NULL );
00094         if ( posBefore )
00095         {
00096                 m_pBoxes.InsertBefore( posBefore, pBox );
00097         }
00098         else
00099         {
00100                 m_pBoxes.AddTail( pBox );
00101         }
00102 
00103         OnChanged();
00104         
00105         return pBox;
00106 }
00107 
00108 POSITION CTaskPanel::GetBoxIterator() const
00109 {
00110         return m_pBoxes.GetHeadPosition();
00111 }
00112 
00113 CTaskBox* CTaskPanel::GetNextBox(POSITION& pos) const
00114 {
00115         return (CTaskBox*)m_pBoxes.GetNext( pos );
00116 }
00117 
00118 int CTaskPanel::GetBoxCount() const
00119 {
00120         return m_pBoxes.GetCount();
00121 }
00122 
00123 void CTaskPanel::RemoveBox(CTaskBox* pBox)
00124 {
00125         POSITION pos = m_pBoxes.Find( pBox );
00126 
00127         if ( pos )
00128         {
00129                 m_pBoxes.RemoveAt( pos );
00130                 if ( m_pStretch == pBox ) m_pStretch = NULL;
00131                 OnChanged();
00132         }
00133 }
00134 
00135 void CTaskPanel::ClearBoxes(BOOL bDelete)
00136 {
00137         if ( bDelete )
00138         {
00139                 for ( POSITION pos = GetBoxIterator() ; pos ; ) delete GetNextBox( pos );
00140         }
00141         
00142         m_pBoxes.RemoveAll();
00143         m_pStretch = NULL;
00144         
00145         OnChanged();
00146 }
00147 
00148 void CTaskPanel::SetStretchBox(CTaskBox* pBox)
00149 {
00150         m_pStretch = pBox;
00151 }
00152 
00153 void CTaskPanel::SetMargin(int nMargin, int nCurve)
00154 {
00155         m_nMargin       = nMargin;
00156         m_nCurve        = nCurve;
00157 }
00158 
00159 void CTaskPanel::SetWatermark(HBITMAP hBitmap)
00160 {
00161         if ( m_bmWatermark.m_hObject != NULL ) m_bmWatermark.DeleteObject();
00162         if ( hBitmap != NULL ) m_bmWatermark.Attach( hBitmap );
00163 }
00164 
00165 void CTaskPanel::SetFooter(HBITMAP hBitmap, BOOL bDefault)
00166 {
00167         if ( m_bmFooter.m_hObject != NULL ) m_bmFooter.DeleteObject();
00168         
00169         if ( hBitmap != NULL)
00170                 m_bmFooter.Attach( hBitmap );
00171         else if ( bDefault && CoolInterface.m_crTaskPanelBack == RGB( 122, 161, 230 ) )
00172                 m_bmFooter.LoadBitmap( IDB_TASKPANEL_FOOTER );
00173 }
00174 
00176 // CTaskPanel message handlers
00177 
00178 int CTaskPanel::OnCreate(LPCREATESTRUCT lpCreateStruct) 
00179 {
00180         if ( CWnd::OnCreate( lpCreateStruct ) == -1 ) return -1;
00181         SetOwner( GetParent() );
00182         return 0;
00183 }
00184 
00185 void CTaskPanel::OnSize(UINT nType, int cx, int cy) 
00186 {
00187         CWnd::OnSize( nType, cx, cy );
00188         
00189         if ( m_pStretch != NULL && m_pStretch->GetOuterHeight() )
00190         {
00191                 m_bLayout = TRUE;
00192                 Invalidate();
00193         }
00194 }
00195 
00196 BOOL CTaskPanel::OnEraseBkgnd(CDC* pDC) 
00197 {
00198         return TRUE;
00199 }
00200 
00201 void CTaskPanel::OnPaint() 
00202 {
00203         CPaintDC dc( this );
00204         CRect rc;
00205         
00206         GetClientRect( &rc );
00207         
00208         if ( m_bLayout ) Layout( rc );
00209         
00210         if ( m_bmFooter.m_hObject != NULL )
00211         {
00212                 BITMAP pInfo;
00213                 m_bmFooter.GetBitmap( &pInfo );
00214                 
00215                 CRect rcFooter( &rc );
00216                 rc.bottom = rcFooter.top = rcFooter.bottom - pInfo.bmHeight;
00217                 
00218                 CoolInterface.DrawWatermark( &dc, &rcFooter, &m_bmFooter );
00219         }
00220         
00221         if ( ! CoolInterface.DrawWatermark( &dc, &rc, &m_bmWatermark ) )
00222         {
00223                 dc.FillSolidRect( &rc, CoolInterface.m_crTaskPanelBack );
00224         }
00225 }
00226 
00227 void CTaskPanel::OnChanged()
00228 {
00229         m_bLayout = TRUE;
00230         if ( m_hWnd ) Invalidate();
00231 }
00232 
00233 void CTaskPanel::Layout(CRect& rcClient)
00234 {
00235         CRect rcBox( &rcClient );
00236         
00237         rcBox.DeflateRect( m_nMargin, m_nMargin );
00238         
00239         int nStretch = rcBox.Height();
00240         
00241         if ( m_pStretch && m_pStretch->GetOuterHeight() )
00242         {
00243                 for ( POSITION pos = GetBoxIterator() ; pos ; )
00244                 {
00245                         CTaskBox* pBox = GetNextBox( pos );
00246                         
00247                         if ( pBox->m_bVisible && pBox->m_nHeight && pBox != m_pStretch )
00248                         {
00249                                 nStretch -= pBox->GetOuterHeight() + m_nMargin;
00250                         }
00251                 }
00252         }
00253 
00254         // Prevent stretch boxes from having negative height
00255         nStretch = max( nStretch, CAPTION_HEIGHT * 2 );
00256         
00257         for ( POSITION pos = GetBoxIterator() ; pos ; )
00258         {
00259                 CTaskBox* pBox = GetNextBox( pos );
00260                 
00261                 int nHeight = pBox->GetOuterHeight();
00262                 
00263                 if ( nHeight )
00264                 {
00265                         if ( pBox == m_pStretch && pBox->m_bOpen ) nHeight = nStretch;
00266                         
00267                         rcBox.bottom = rcBox.top + nHeight;
00268                         
00269                         pBox->SetWindowPos( NULL, rcBox.left, rcBox.top, rcBox.Width(), rcBox.Height(),
00270                                 SWP_SHOWWINDOW|SWP_NOZORDER );
00271                         
00272                         rcBox.OffsetRect( 0, nHeight + m_nMargin );
00273                 }
00274                 else if ( pBox->IsWindowVisible() )
00275                 {
00276                         pBox->ShowWindow( SW_HIDE );
00277                 }
00278         }
00279         
00280         m_bLayout = FALSE;
00281 }
00282 
00283 
00285 // CTaskBox construction
00286 
00287 CTaskBox::CTaskBox()
00288 {
00289         m_pPanel                = NULL;
00290         m_nHeight               = 0;
00291         m_bVisible              = TRUE;
00292         m_bOpen                 = TRUE;
00293         m_bHover                = FALSE;
00294         m_bPrimary              = FALSE;
00295         m_hIcon                 = NULL;
00296         m_bIconDel              = FALSE;
00297         m_bCaptionCurve = TRUE;
00298 }
00299 
00300 CTaskBox::~CTaskBox()
00301 {
00302         if ( m_bIconDel ) DestroyIcon( m_hIcon );
00303 }
00304 
00306 // CTaskBox operations
00307 
00308 BOOL CTaskBox::Create(CTaskPanel* pPanel, int nHeight, LPCTSTR pszCaption, UINT nIDIcon)
00309 {
00310         CRect rect( 0, 0, 0, 0 );
00311         
00312         m_pPanel        = pPanel;
00313         m_nHeight       = nHeight;
00314         
00315         if ( pPanel->m_hWnd )
00316         {
00317                 pPanel->GetClientRect( &rect );
00318                 rect.DeflateRect( pPanel->m_nMargin, 0 );
00319                 rect.bottom = 0;
00320         }
00321         
00322         if ( ! CWnd::Create( NULL, pszCaption, WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
00323                 rect, pPanel, 100, NULL ) ) return FALSE;
00324         
00325         if ( nIDIcon )
00326         {
00327                 HICON hIcon = (HICON)LoadImage( AfxGetResourceHandle(),
00328                         MAKEINTRESOURCE( nIDIcon ), IMAGE_ICON, 16, 16, 0 );
00329                 m_hIcon = theApp.m_bRTL ? CreateMirroredIcon( hIcon ) : hIcon;
00330                 CWnd::SetIcon( m_hIcon, FALSE );
00331         }
00332         
00333         CString strKey;
00334         strKey.Format( _T("%s.Open"), (LPCTSTR)CString( GetRuntimeClass()->m_lpszClassName ) );
00335         m_bOpen = theApp.GetProfileInt( _T("Interface"), strKey, TRUE );
00336         
00337         return TRUE;
00338 }
00339 
00340 CTaskPanel* CTaskBox::GetPanel() const
00341 {
00342         ASSERT( m_pPanel != NULL );
00343         ASSERT_KINDOF( CTaskPanel, m_pPanel );
00344         return m_pPanel;
00345 }
00346 
00347 void CTaskBox::SetCaption(LPCTSTR pszCaption)
00348 {
00349         CString strOld;
00350         GetWindowText( strOld );
00351         
00352         if ( strOld != pszCaption )
00353         {
00354                 SetWindowText( pszCaption );
00355                 InvalidateNonclient();
00356         }
00357 }
00358 
00359 void CTaskBox::SetIcon(HICON hIcon, BOOL bIconDel)
00360 {
00361         if ( m_bIconDel ) DestroyIcon( m_hIcon );
00362 
00363         m_hIcon         = hIcon;
00364         m_bIconDel      = bIconDel;
00365 
00366         CWnd::SetIcon( hIcon, FALSE );
00367         InvalidateNonclient();
00368 }
00369 
00370 void CTaskBox::SetSize(int nHeight)
00371 {
00372         if ( m_nHeight == nHeight ) return;
00373         m_nHeight = nHeight;
00374         if ( m_pPanel ) m_pPanel->OnChanged();
00375 }
00376 
00377 void CTaskBox::SetPrimary(BOOL bPrimary)
00378 {
00379         if ( m_bPrimary == bPrimary ) return;
00380         m_bPrimary = bPrimary;
00381         if ( m_pPanel ) m_pPanel->OnChanged();
00382 }
00383 
00384 void CTaskBox::SetWatermark(HBITMAP hBitmap)
00385 {
00386         if ( m_bmWatermark.m_hObject != NULL ) m_bmWatermark.DeleteObject();
00387         if ( hBitmap != NULL ) m_bmWatermark.Attach( hBitmap );
00388 }
00389 
00390 void CTaskBox::SetCaptionmark(HBITMAP hBitmap, BOOL bDefault)
00391 {
00392         if ( m_bmCaptionmark.m_hObject != NULL ) m_bmCaptionmark.DeleteObject();
00393         
00394         if ( hBitmap != NULL )
00395                 m_bmCaptionmark.Attach( hBitmap );
00396         else if ( bDefault && m_bPrimary && CoolInterface.m_crTaskBoxPrimaryBack == RGB( 30, 87, 199 ) )
00397                 m_bmCaptionmark.LoadBitmap( IDB_TASKBOX_CAPTION );
00398         
00399         m_bCaptionCurve = TRUE;
00400         
00401         if ( m_bmCaptionmark.m_hObject != NULL )
00402         {
00403                 BITMAP pInfo;
00404                 m_bmCaptionmark.GetBitmap( &pInfo );
00405                 m_bCaptionCurve = pInfo.bmWidth < 176;
00406         }
00407 }
00408 
00409 void CTaskBox::Expand(BOOL bOpen)
00410 {
00411         if ( m_bOpen == bOpen ) return;
00412         m_bOpen = bOpen;
00413         
00414         if ( m_pPanel != NULL )
00415         {
00416                 m_pPanel->OnChanged();
00417                 OnExpanded( m_bOpen );
00418         }
00419         
00420         CString strKey;
00421         strKey.Format( _T("%s.Open"), (LPCTSTR)CString( GetRuntimeClass()->m_lpszClassName ) );
00422         theApp.WriteProfileInt( _T("Interface"), strKey, m_bOpen );
00423         
00424         InvalidateNonclient();
00425 }
00426 
00427 void CTaskBox::OnExpanded(BOOL bOpen)
00428 {
00429 }
00430 
00431 int CTaskBox::GetOuterHeight() const
00432 {
00433         if ( ! m_bVisible || ! m_nHeight ) return 0;
00434         return CAPTION_HEIGHT + ( m_bOpen ? m_nHeight + 1 : 0 );
00435 }
00436 
00438 // CTaskBox message handlers
00439 
00440 void CTaskBox::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp) 
00441 {
00442         NCCALCSIZE_PARAMS* pSize = (NCCALCSIZE_PARAMS*)lpncsp;
00443 
00444         pSize->rgrc[0].left ++;
00445         pSize->rgrc[0].top += CAPTION_HEIGHT;
00446         pSize->rgrc[0].right --;
00447         pSize->rgrc[0].bottom --;
00448 }
00449 
00450 UINT CTaskBox::OnNcHitTest(CPoint point) 
00451 {
00452         CRect rc;
00453         GetWindowRect( &rc );
00454 
00455         if ( rc.PtInRect( point ) )
00456         {
00457                 if ( point.y < rc.top + CAPTION_HEIGHT ) return HTCAPTION;
00458                 return HTCLIENT;
00459         }
00460         
00461         return HTNOWHERE;
00462 }
00463 
00464 BOOL CTaskBox::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
00465 {
00466         if ( nHitTest == HTCAPTION )
00467         {
00468                 if ( ! m_bHover )
00469                 {
00470                         m_bHover = TRUE;
00471                         PaintBorders();
00472                         SetTimer( 1, 50, NULL );
00473                 }
00474                 
00475                 SetCursor( theApp.LoadCursor( IDC_HAND ) );
00476                 return TRUE;
00477         }
00478         
00479         return CWnd::OnSetCursor(pWnd, nHitTest, message);
00480 }
00481 
00482 void CTaskBox::OnNcLButtonDown(UINT nHitTest, CPoint point) 
00483 {
00484 }
00485 
00486 void CTaskBox::OnNcLButtonUp(UINT nHitTest, CPoint point) 
00487 {
00488         if ( nHitTest == HTCAPTION ) Expand( ! m_bOpen );       
00489 }
00490 
00491 void CTaskBox::OnTimer(UINT nIDEvent) 
00492 {
00493         CPoint point;
00494         GetCursorPos( &point );
00495 
00496         if ( OnNcHitTest( point ) != HTCAPTION )
00497         {
00498                 KillTimer( 1 );
00499                 m_bHover = FALSE;
00500                 PaintBorders();
00501         }
00502 }
00503 
00504 BOOL CTaskBox::OnNcActivate(BOOL bActive) 
00505 {
00506         PaintBorders();
00507         return TRUE;
00508 }
00509 
00510 void CTaskBox::OnNcPaint() 
00511 {
00512         PaintBorders();
00513 }
00514 
00515 void CTaskBox::PaintBorders()
00516 {
00517         CWindowDC dc( this );
00518         CString strCaption;
00519         CRect rc, rcc;
00520         
00521         GetWindowRect( &rc );
00522         rc.OffsetRect( -rc.left, -rc.top );
00523         
00524         if ( m_pPanel->m_nCurve != 0 && m_bCaptionCurve )
00525         {
00526                 dc.SetPixel( 0, 0, CoolInterface.m_crTaskPanelBack );
00527                 dc.SetPixel( 1, 0, CoolInterface.m_crTaskPanelBack );
00528                 dc.SetPixel( 0, 1, CoolInterface.m_crTaskPanelBack );
00529                 dc.SetPixel( rc.right - 1, 0, CoolInterface.m_crTaskPanelBack );
00530                 dc.SetPixel( rc.right - 2, 0, CoolInterface.m_crTaskPanelBack );
00531                 dc.SetPixel( rc.right - 1, 1, CoolInterface.m_crTaskPanelBack );
00532                 
00533                 dc.ExcludeClipRect( 0, 0, 2, 1 );
00534                 dc.ExcludeClipRect( 0, 1, 1, 2 );
00535                 dc.ExcludeClipRect( rc.right - 2, 0, rc.right, 1 );
00536                 dc.ExcludeClipRect( rc.right - 1, 1, rc.right, 2 );
00537         }
00538         
00539         rcc.SetRect( 0, 0, rc.right, CAPTION_HEIGHT );
00540         
00541         CDC* pBuffer = CoolInterface.GetBuffer( dc, rcc.Size() );
00542         
00543         if ( m_bmCaptionmark.m_hObject != NULL )
00544         {
00545                 CoolInterface.DrawWatermark( pBuffer, &rcc, &m_bmCaptionmark );
00546         }
00547         else
00548         {
00549                 pBuffer->FillSolidRect( &rcc, m_bPrimary ?
00550                         CoolInterface.m_crTaskBoxPrimaryBack : CoolInterface.m_crTaskBoxCaptionBack );
00551         }
00552         
00553         CPoint ptIcon( 6, rcc.Height() / 2 - 7 );
00554         
00555         DrawIconEx( pBuffer->GetSafeHdc(), ptIcon.x, ptIcon.y, GetIcon( FALSE ),
00556                 16, 16, 0, NULL, DI_NORMAL );
00557         
00558         GetWindowText( strCaption );
00559         
00560         CFont* pOldFont = (CFont*)pBuffer->SelectObject( &theApp.m_gdiFontBold );
00561         CSize sz                = pBuffer->GetTextExtent( strCaption );
00562         
00563         pBuffer->SetBkMode( TRANSPARENT );
00564         pBuffer->SetTextColor( m_bHover ? CoolInterface.m_crTaskBoxCaptionHover :
00565                 ( m_bPrimary ? CoolInterface.m_crTaskBoxPrimaryText : CoolInterface.m_crTaskBoxCaptionText ) );
00566         
00567         pBuffer->ExtTextOut( ptIcon.x * 2 + 16 + 1, rcc.Height() / 2 - sz.cy / 2,
00568                 ETO_CLIPPED, &rcc, strCaption, NULL );
00569         
00570         pBuffer->SelectObject( pOldFont );
00571         
00572         dc.BitBlt( rc.left, rc.top, rcc.Width(), rcc.Height(), pBuffer, 0, 0, SRCCOPY );
00573         
00574         dc.ExcludeClipRect( &rcc );
00575         
00576         if ( m_bOpen )
00577         {
00578                 rc.top = rcc.bottom - 1;
00579                 dc.Draw3dRect( &rc, CoolInterface.m_crTaskBoxCaptionBack,
00580                         CoolInterface.m_crTaskBoxCaptionBack );
00581         }
00582 }
00583 
00584 void CTaskBox::InvalidateNonclient()
00585 {
00586         if ( m_hWnd )
00587         {
00588                 CRect rc;
00589                 GetWindowRect( &rc );
00590                 rc.bottom = rc.top + CAPTION_HEIGHT;
00591                 ScreenToClient( &rc );
00592                 RedrawWindow( &rc, NULL, RDW_FRAME|RDW_INVALIDATE );
00593         }
00594 }
00595 
00596 void CTaskBox::OnPaint() 
00597 {
00598         CPaintDC dc( this );
00599         CRect rc;
00600         
00601         GetClientRect( &rc );   
00602 
00603         if ( ! CoolInterface.DrawWatermark( &dc, &rc, &m_bmWatermark ) )
00604         {
00605                 dc.FillSolidRect( &rc, CoolInterface.m_crTaskBoxClient );
00606         }
00607 }
00608 
00609 void CTaskBox::OnSysCommand(UINT nID, LPARAM lParam) 
00610 {
00611 }
00612 

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