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

WindowManager.cpp

Go to the documentation of this file.
00001 //
00002 // WindowManager.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 "WindowManager.h"
00026 #include "Skin.h"
00027 #include "CtrlWndTabBar.h"
00028 
00029 #include "WndHome.h"
00030 #include "WndSystem.h"
00031 #include "WndNeighbours.h"
00032 #include "WndDownloads.h"
00033 #include "WndUploads.h"
00034 
00035 #include "WndTraffic.h"
00036 #include "WndSearch.h"
00037 
00038 
00039 #ifdef _DEBUG
00040 #undef THIS_FILE
00041 static char THIS_FILE[]=__FILE__;
00042 #define new DEBUG_NEW
00043 #endif
00044 
00045 BEGIN_MESSAGE_MAP(CWindowManager, CWnd)
00046         //{{AFX_MSG_MAP(CWindowManager)
00047         ON_WM_SIZE()
00048         ON_WM_ERASEBKGND()
00049         ON_WM_PAINT()
00050         //}}AFX_MSG_MAP
00051 END_MESSAGE_MAP()
00052 
00053 
00055 // CWindowManager construction
00056 
00057 CWindowManager::CWindowManager(CMDIFrameWnd* pParent)
00058 {
00059         m_bIgnoreActivate       = FALSE;
00060         m_bClosing                      = FALSE;
00061 
00062         if ( pParent ) SetOwner( pParent );
00063 }
00064 
00065 CWindowManager::~CWindowManager()
00066 {
00067 }
00068 
00070 // CWindowManager owner
00071 
00072 void CWindowManager::SetOwner(CMDIFrameWnd* pParent)
00073 {
00074         m_pParent = pParent;
00075         SubclassWindow( m_pParent->m_hWndMDIClient );
00076 }
00077 
00079 // CWindowManager add and remove
00080 
00081 void CWindowManager::Add(CChildWnd* pChild)
00082 {
00083         CSingleLock pLock( &theApp.m_pSection, TRUE );
00084         if ( m_pWindows.Find( pChild ) == NULL ) m_pWindows.AddTail( pChild );
00085 }
00086 
00087 void CWindowManager::Remove(CChildWnd* pChild)
00088 {
00089         CSingleLock pLock( &theApp.m_pSection, TRUE );
00090         POSITION pos = m_pWindows.Find( pChild );
00091         if ( pos ) m_pWindows.RemoveAt( pos );
00092 }
00093 
00095 // CWindowManager get active
00096 
00097 CChildWnd* CWindowManager::GetActive() const
00098 {
00099         if ( m_hWnd == NULL ) return NULL;
00100 
00101         CWnd* pActive = m_pParent->MDIGetActive();
00102 
00103         if ( pActive && pActive->IsKindOf( RUNTIME_CLASS(CChildWnd) ) )
00104                 return (CChildWnd*)pActive;
00105         else
00106                 return NULL;
00107 }
00108 
00110 // CWindowManager iteration
00111 
00112 POSITION CWindowManager::GetIterator() const
00113 {
00114         return m_pWindows.GetHeadPosition();
00115 }
00116 
00117 CChildWnd* CWindowManager::GetNext(POSITION& pos) const
00118 {
00119         return (CChildWnd*)m_pWindows.GetNext( pos );
00120 }
00121 
00122 BOOL CWindowManager::Check(CChildWnd* pChild) const
00123 {
00124         return m_pWindows.Find( pChild ) != NULL;
00125 }
00126 
00128 // CWindowManager find
00129 
00130 CChildWnd* CWindowManager::Find(CRuntimeClass* pClass, CChildWnd* pAfter, CChildWnd* pExcept)
00131 {
00132         CSingleLock pLock( &theApp.m_pSection, TRUE );
00133         BOOL bFound = ( pAfter == NULL );
00134 
00135         for ( POSITION pos = GetIterator() ; pos ; )
00136         {
00137                 CChildWnd* pChild = GetNext( pos );
00138 
00139                 if ( pChild == pExcept ) continue;
00140                 else if ( bFound && ( !pClass || pChild->IsKindOf( pClass ) ) ) return pChild;
00141                 else if ( pChild == pAfter ) bFound = TRUE;
00142         }
00143 
00144         return NULL;
00145 }
00146 
00148 // CWindowManager open / toggle
00149 
00150 CChildWnd* CWindowManager::Open(CRuntimeClass* pClass, BOOL bToggle, BOOL bFocus)
00151 {
00152         CSingleLock pLock( &theApp.m_pSection, TRUE );
00153 
00154         CChildWnd* pChild = Find( pClass );
00155 
00156         if ( pChild && pChild->IsIconic() )
00157         {
00158                 pChild->ShowWindow( SW_SHOWNORMAL );
00159                 bToggle = FALSE;
00160         }
00161 
00162         if ( pChild && pChild->m_bTabMode ) bToggle = FALSE;
00163 
00164         if ( pChild && bToggle && GetActive() == pChild )
00165         {
00166                 pChild->DestroyWindow();
00167                 return NULL;
00168         }
00169 
00170         pLock.Unlock();
00171 
00172         if ( ! pChild ) pChild = (CChildWnd*)pClass->CreateObject();
00173 
00174         if ( bFocus ) pChild->BringWindowToTop();
00175 
00176         return pChild;
00177 }
00178 
00180 // CWindowManager find by point
00181 
00182 CChildWnd* CWindowManager::FindFromPoint(const CPoint& point) const
00183 {
00184         CPoint pt( point );
00185 
00186         ScreenToClient( &pt );
00187         CChildWnd* pWnd = (CChildWnd*)ChildWindowFromPoint( pt );
00188 
00189         return ( pWnd != NULL && pWnd->IsKindOf( RUNTIME_CLASS(CChildWnd) ) ) ? pWnd : NULL;
00190 }
00191 
00193 // CWindowManager close
00194 
00195 void CWindowManager::Close()
00196 {
00197         CSingleLock pLock( &theApp.m_pSection, TRUE );
00198         CPtrList pClose;
00199 
00200         for ( POSITION pos = GetIterator() ; pos ; )
00201         {
00202                 pClose.AddTail( GetNext( pos ) );
00203         }
00204 
00205         for ( POSITION pos = pClose.GetHeadPosition() ; pos ; )
00206         {
00207                 CChildWnd* pChild = (CChildWnd*)pClose.GetNext( pos );
00208                 pChild->DestroyWindow();
00209         }
00210 }
00211 
00213 // CWindowManager automatic resize
00214 
00215 void CWindowManager::AutoResize()
00216 {
00217         CChildWnd* pChild;
00218         CRect rcSize;
00219 
00220         GetClientRect( &rcSize );
00221         if ( rcSize.right < 64 || rcSize.bottom < 64 ) return;
00222 
00223         for ( pChild = (CChildWnd*)GetWindow( GW_CHILD ) ; pChild ; pChild = (CChildWnd*)pChild->GetNextWindow() )
00224         {
00225                 if ( ! pChild->IsKindOf( RUNTIME_CLASS(CChildWnd) ) ) continue;
00226 
00227                 CRect rcChild;
00228                 pChild->GetWindowRect( &rcChild );
00229                 ScreenToClient( &rcChild );
00230 
00231                 if ( rcChild.right == m_rcSize.right || rcChild.bottom == m_rcSize.bottom )
00232                 {
00233                         if ( rcChild.right == m_rcSize.right ) rcChild.right = rcSize.right;
00234                         if ( rcChild.bottom == m_rcSize.bottom ) rcChild.bottom = rcSize.bottom;
00235 
00236                         pChild->MoveWindow( &rcChild );
00237                 }
00238         }
00239 
00240         m_rcSize = rcSize;
00241 
00242         if ( Settings.General.GUIMode != GUI_WINDOWED )
00243         {
00244                 for ( pChild = (CChildWnd*)GetWindow( GW_CHILD ) ; pChild ; pChild = (CChildWnd*)pChild->GetNextWindow() )
00245                 {
00246                         if ( ! pChild->IsKindOf( RUNTIME_CLASS(CChildWnd) ) ) continue;
00247 
00248                         if ( pChild->m_bGroupMode && pChild->m_pGroupParent )
00249                         {
00250                                 CRect rcChild( &rcSize );
00251                                 rcChild.bottom = (int)( pChild->m_pGroupParent->m_nGroupSize * rcChild.bottom );
00252                                 pChild->m_pGroupParent->MoveWindow( &rcChild );
00253                                 rcChild.top = rcChild.bottom;
00254                                 rcChild.bottom = rcSize.bottom;
00255                                 pChild->MoveWindow( &rcChild );
00256                         }
00257                         else if ( pChild->m_bPanelMode && ! pChild->m_bGroupMode )
00258                         {
00259                                 CRect rcChild;
00260                                 pChild->GetWindowRect( &rcChild );
00261                                 ScreenToClient( &rcChild );
00262                                 if ( rcChild != rcSize ) pChild->MoveWindow( &rcSize );
00263                         }
00264                 }
00265         }
00266 }
00267 
00269 // CWindowManager customized cascade
00270 
00271 void CWindowManager::Cascade(BOOL bActiveOnly)
00272 {
00273         CSingleLock pLock( &theApp.m_pSection, TRUE );
00274 
00275         CChildWnd* pActive = bActiveOnly ? GetActive() : NULL;
00276 
00277         for ( POSITION pos = GetIterator() ; pos ; )
00278         {
00279                 CChildWnd* pChild = GetNext( pos );
00280 
00281                 if ( ! pChild->m_bPanelMode && pChild->IsWindowVisible() &&
00282                          ! pChild->IsIconic() && ( pChild == pActive || ! pActive ) )
00283                 {
00284                         CRect rcClient;
00285                         pChild->ShowWindow( SW_RESTORE );
00286                         pChild->GetParent()->GetClientRect( &rcClient );
00287                         pChild->MoveWindow( &rcClient );
00288                 }
00289         }
00290 }
00291 
00293 // CWindowManager activate grouped windows
00294 
00295 void CWindowManager::ActivateGrouped(CChildWnd* pExcept)
00296 {
00297         CSingleLock pLock( &theApp.m_pSection, TRUE );
00298 
00299         if ( m_bIgnoreActivate ) return;
00300         m_bIgnoreActivate = TRUE;
00301 
00302         CChildWnd* pParent = pExcept->m_pGroupParent ? pExcept->m_pGroupParent : pExcept;
00303 
00304         for ( POSITION pos = GetIterator() ; pos ; )
00305         {
00306                 CChildWnd* pChild = GetNext( pos );
00307 
00308                 if ( pChild != pExcept )
00309                 {
00310                         if ( pChild->m_bGroupMode && ( pChild == pParent || pChild->m_pGroupParent == pParent ) )
00311                         {
00312                                 pChild->ShowWindow( SW_SHOWNORMAL );
00313                                 pChild->MDIActivate();
00314                         }
00315                 }
00316         }
00317 
00318         pExcept->MDIActivate();
00319         m_bIgnoreActivate = FALSE;
00320 }
00321 
00323 // CWindowManager GUI mode
00324 
00325 void CWindowManager::SetGUIMode(int nMode, BOOL bSaveState)
00326 {
00327         ModifyStyle( WS_VISIBLE, 0 );
00328 
00329         if ( bSaveState )
00330         {
00331                 SaveSearchWindows();
00332                 if ( Settings.General.GUIMode == GUI_WINDOWED )
00333                         SaveWindowStates();
00334         }
00335 
00336         Close();
00337 
00338         Settings.General.GUIMode = nMode;
00339         theApp.WriteProfileInt( _T("Settings"), _T("GUIMode"), nMode );
00340 
00341         if ( Settings.General.GUIMode != GUI_WINDOWED )
00342         {
00343                 CreateTabbedWindows();
00344         }
00345         else
00346         {
00347                 LoadWindowStates();
00348         }
00349 
00350         LoadSearchWindows();
00351 
00352         AutoResize();
00353         ShowWindow( SW_SHOW );
00354 }
00355 
00357 // CWindowManager create tabbed windows
00358 
00359 void CWindowManager::CreateTabbedWindows()
00360 {
00361         CDownloadsWnd* pDownloads = new CDownloadsWnd();
00362 
00363         if ( Settings.General.GUIMode != GUI_BASIC )
00364         {
00365                 CUploadsWnd* pUploads = new CUploadsWnd();
00366                 pUploads->m_pGroupParent = pDownloads;
00367 
00368                 CNeighboursWnd* pNeighbours = new CNeighboursWnd();
00369                 CSystemWnd* pSystem = new CSystemWnd();
00370                 pSystem->m_pGroupParent = pNeighbours;
00371         }
00372 
00373         CHomeWnd* pHome = new CHomeWnd();
00374 }
00375 
00377 // CWindowManager load complex window states
00378 
00379 void CWindowManager::LoadWindowStates()
00380 {
00381         if ( Settings.General.GUIMode != GUI_WINDOWED ) return;
00382 
00383         CString strWindows = theApp.GetProfileString( _T("Windows"), _T("State"),
00384                 _T("CSystemWnd|CNeighboursWnd") );
00385 
00386         for ( strWindows += '|' ; strWindows.GetLength() ; )
00387         {
00388                 CString strClass = strWindows.SpanExcluding( _T("| ,.\t") );
00389                 strWindows = strWindows.Mid( strClass.GetLength() + 1 );
00390 
00391                 if ( strClass.Find( _T("TG#") ) == 0 )
00392                 {
00393                         DWORD nUnique;
00394 
00395                         if ( _stscanf( (LPCTSTR)strClass + 3, _T("%lu"), &nUnique ) == 1 )
00396                         {
00397                                 new CTrafficWnd( nUnique );
00398                         }
00399                 }
00400                 else if ( strClass.GetLength() )
00401                 {
00402                         CRuntimeClass* pClass = AfxClassForName( strClass );
00403 
00404                         if ( pClass && pClass->IsDerivedFrom( RUNTIME_CLASS(CChildWnd) ) )
00405                         {
00406                                 Open( pClass );
00407                         }
00408                 }
00409         }
00410 }
00411 
00413 // CWindowManager save complex window states
00414 
00415 void CWindowManager::SaveWindowStates()
00416 {
00417         if ( Settings.General.GUIMode != GUI_WINDOWED ) return;
00418 
00419         CString strWindows;
00420 
00421         for ( POSITION pos = GetIterator() ; pos ; )
00422         {
00423                 CChildWnd* pChild = GetNext( pos );
00424 
00425                 if ( strWindows.GetLength() ) strWindows += '|';
00426 
00427                 if ( pChild->IsKindOf( RUNTIME_CLASS(CTrafficWnd) ) )
00428                 {
00429                         CTrafficWnd* pTraffic = (CTrafficWnd*)pChild;
00430                         CString strItem;
00431                         strItem.Format( _T("TG#%.4x"), pTraffic->m_nUnique );
00432                         strWindows += strItem;
00433                 }
00434                 else
00435                 {
00436                         strWindows += pChild->GetRuntimeClass()->m_lpszClassName;
00437                 }
00438         }
00439 
00440         theApp.WriteProfileString( _T("Windows"), _T("State"), strWindows );
00441 }
00442 
00444 // CWindowManager search load and save
00445 
00446 BOOL CWindowManager::LoadSearchWindows()
00447 {
00448         CString strFile = Settings.General.UserPath + _T("\\Data\\Searches.dat");
00449         CFile pFile;
00450 
00451         if ( ! pFile.Open( strFile, CFile::modeRead ) ) return FALSE;
00452 
00453         CArchive ar( &pFile, CArchive::load );
00454         CWaitCursor pCursor;
00455         BOOL bSuccess = TRUE;
00456 
00457         try
00458         {
00459                 while ( ar.ReadCount() == 1 )
00460                 {
00461                         CSearchWnd* pWnd = new CSearchWnd();
00462                         pWnd->Serialize( ar );
00463                 }
00464         }
00465         catch ( CException* pException )
00466         {
00467                 pException->Delete();
00468                 bSuccess = FALSE;
00469         }
00470 
00471         if ( Settings.General.GUIMode != GUI_WINDOWED ) Open( RUNTIME_CLASS(CHomeWnd) );
00472 
00473         return bSuccess;
00474 }
00475 
00476 void CWindowManager::SaveSearchWindows()
00477 {
00478         CString strFile = Settings.General.UserPath + _T("\\Data\\Searches.dat");
00479         CFile pFile;
00480 
00481         if ( ! pFile.Open( strFile, CFile::modeWrite|CFile::modeCreate ) ) return;
00482 
00483         CArchive ar( &pFile, CArchive::store );
00484         int nCount = 0;
00485 
00486         for ( POSITION pos = GetIterator() ; pos ; )
00487         {
00488                 CSearchWnd* pWnd = (CSearchWnd*)GetNext( pos );
00489 
00490                 if ( pWnd->IsKindOf( RUNTIME_CLASS(CSearchWnd) ) && pWnd->GetLastSearch() )
00491                 {
00492                         ar.WriteCount( 1 );
00493                         pWnd->Serialize( ar );
00494                         nCount++;
00495                 }
00496         }
00497 
00498         ar.WriteCount( 0 );
00499         ar.Close();
00500         pFile.Close();
00501 
00502         if ( ! nCount ) DeleteFile( strFile );
00503 }
00504 
00506 // CWindowManager new blank search window
00507 
00508 void CWindowManager::OpenNewSearchWindow()
00509 {
00510         for ( POSITION pos = GetIterator() ; pos ; )
00511         {
00512                 CSearchWnd* pChild = (CSearchWnd*)GetNext( pos );
00513 
00514                 if ( pChild->IsKindOf( RUNTIME_CLASS(CSearchWnd) ) )
00515                 {
00516                         if ( pChild->GetLastSearch() == NULL )
00517                         {
00518                                 if ( pChild->IsIconic() ) pChild->ShowWindow( SW_SHOWNORMAL );
00519                                 pChild->BringWindowToTop();
00520                                 return;
00521                         }
00522                 }
00523         }
00524 
00525         new CSearchWnd();
00526 }
00527 
00529 // CWindowManager skin change
00530 
00531 void CWindowManager::PostSkinChange()
00532 {
00533         for ( POSITION pos = GetIterator() ; pos ; )
00534         {
00535                 CChildWnd* pChildWnd = GetNext( pos );
00536                 pChildWnd->OnSkinChange();
00537 /*
00538                 CWndTabBar* pTabBar = (CWndTabBar*)pChildWnd;
00539                 CMenu* pMenu = &pTabBar->m_mnuChild;
00540                 //CMenu* pTabMenu = Skin.GetMenu( _T("Child") );
00541                 //CMenu pTabMenu = pTabBar->m_mnuChild;
00542 
00543                 MENUITEMINFO pInfo;
00544                 ZeroMemory( &pInfo, sizeof( pInfo ) );
00545                 pInfo.cbSize = sizeof( MENUITEMINFO );
00546                 pInfo.fMask = MIIM_DATA;
00547                 pMenu->DestroyMenu();
00548 */
00549         }
00550 }
00551 
00552 void CWindowManager::PostSkinRemove()
00553 {
00554         for ( POSITION pos = GetIterator() ; pos ; )
00555         {
00556                 GetNext( pos )->m_pSkin = NULL;
00557         }
00558 }
00559 
00561 // CWindowManager message handlers
00562 
00563 void CWindowManager::OnSize(UINT nType, int cx, int cy)
00564 {
00565         if ( nType != 1982 ) CWnd::OnSize( nType, cx, cy );
00566         AutoResize();
00567 }
00568 
00569 BOOL CWindowManager::OnEraseBkgnd(CDC* pDC)
00570 {
00571         CRect rc;
00572         GetClientRect( &rc );
00573         pDC->FillSolidRect( &rc, ( Settings.General.GUIMode != GUI_WINDOWED && ! m_bClosing ) ?
00574                 RGB( 0xBE, 0, 0 ) : GetSysColor( COLOR_APPWORKSPACE ) );
00575         return TRUE;
00576 }
00577 
00578 void CWindowManager::OnPaint()
00579 {
00580         CPaintDC dc( this );
00581 }
00582 

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