Esempio n. 1
0
CDocument* CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,
	BOOL bMakeVisible)
	// if lpszPathName == NULL => create new file of this type
{
	CDocument* pDocument = NULL;
	CFrameWnd* pFrame = NULL;
	BOOL bCreated = FALSE;      // => doc and frame created
	BOOL bWasModified = FALSE;

	if (m_pOnlyDoc != NULL)
	{
		// already have a document - reinit it
		pDocument = m_pOnlyDoc;
		if (!pDocument->SaveModified())
			return NULL;        // leave the original one

		pFrame = (CFrameWnd*)AfxGetMainWnd();
		ASSERT(pFrame != NULL);
		ASSERT(pFrame->IsKindOf(RUNTIME_CLASS(CFrameWnd)));
		ASSERT_VALID(pFrame);
	}
	else
	{
		// create a new document
		pDocument = CreateNewDocument();
		ASSERT(pFrame == NULL);     // will be created below
		bCreated = TRUE;
	}

	if (pDocument == NULL)
	{
		AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
		return NULL;
	}
	ASSERT(pDocument == m_pOnlyDoc);

	if (pFrame == NULL)
	{
		ASSERT(bCreated);

		// create frame - set as main document frame
		BOOL bAutoDelete = pDocument->m_bAutoDelete;
		pDocument->m_bAutoDelete = FALSE;
					// don't destroy if something goes wrong
		pFrame = CreateNewFrame(pDocument, NULL);
		pDocument->m_bAutoDelete = bAutoDelete;
		if (pFrame == NULL)
		{
			AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
			delete pDocument;       // explicit delete on error
			return NULL;
		}
	}

	if (lpszPathName == NULL)
	{
		// create a new document
		SetDefaultTitle(pDocument);

		// avoid creating temporary compound file when starting up invisible
		if (!bMakeVisible)
			pDocument->m_bEmbedded = TRUE;

		if (!pDocument->OnNewDocument())
		{
			// user has been alerted to what failed in OnNewDocument
			TRACE0("CDocument::OnNewDocument returned FALSE.\n");
			if (bCreated)
				pFrame->DestroyWindow();    // will destroy document
			return NULL;
		}
	}
	else
	{
		BeginWaitCursor();

		// open an existing document
		bWasModified = pDocument->IsModified();
		pDocument->SetModifiedFlag(FALSE);  // not dirty for open

		if (!pDocument->OnOpenDocument(lpszPathName))
		{
			// user has been alerted to what failed in OnOpenDocument
			TRACE0("CDocument::OnOpenDocument returned FALSE.\n");
			if (bCreated)
			{
				pFrame->DestroyWindow();    // will destroy document
			}
			else if (!pDocument->IsModified())
			{
				// original document is untouched
				pDocument->SetModifiedFlag(bWasModified);
			}
			else
			{
				// we corrupted the original document
				SetDefaultTitle(pDocument);

				if (!pDocument->OnNewDocument())
				{
					TRACE0("Error: OnNewDocument failed after trying to open a document - trying to continue.\n");
					// assume we can continue
				}
			}
			EndWaitCursor();
			return NULL;        // open failed
		}
#ifdef _MAC
		// if the document is dirty, we must have opened a stationery pad - don't
		// change the pathname because we want to treat the document as untitled
		if (!pDocument->IsModified())
#endif
			pDocument->SetPathName(lpszPathName);

		EndWaitCursor();
	}

	if (bCreated && AfxGetMainWnd() == NULL)
	{
		// set as main frame (InitialUpdateFrame will show the window)
		AfxGetThread()->m_pMainWnd = pFrame;
	}
	InitialUpdateFrame(pFrame, pDocument, bMakeVisible);

	return pDocument;
}
Esempio n. 2
0
BOOL CExtMdiWindowsListDlg::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
{
	if( nCode == CN_COMMAND )
	{
		bool bNeedToUpdateButtons = false;
		int nCount = m_ListWindows.GetCount();
		HWND hWnd = (HWND)m_ListWindows.GetItemData( 
			m_ListWindows.GetCurSel() 
			);
		WINDOWPLACEMENT	wp;

		if(hWnd != (HWND)LB_ERR)
		{
			switch(nID) 
			{
			case IDC_EXT_TILEVERT:
				if( nCount != LB_ERR && nCount > 0 )
				{
					for(int nItem = nCount-1; nItem >= 0; nItem-- )	
					{
						HWND hWnd=(HWND)m_ListWindows.GetItemData(nItem);
						if( m_ListWindows.GetSel(nItem) > 0 )
							::ShowWindow(hWnd, SW_RESTORE);
						else
							::ShowWindow(hWnd, SW_MINIMIZE);
					} // for(int nItem = nCount-1; nItem >= 0; nItem-- )	
					::SendMessage(m_hWndMDIClient, WM_MDITILE, MDITILE_VERTICAL, 0);	
				} // if( nCount != LB_ERR && nCount > 0 )
				bNeedToUpdateButtons = true;
				break;
				
			case IDC_EXT_TILEHORZ:
				if( nCount != LB_ERR && nCount > 0 )
				{
					for(int nItem = nCount-1; nItem >= 0; nItem-- )	
					{
						HWND hWnd=(HWND)m_ListWindows.GetItemData(nItem);
						if( m_ListWindows.GetSel(nItem) > 0 )
							::ShowWindow(hWnd, SW_RESTORE);
						else
							::ShowWindow(hWnd, SW_MINIMIZE);
					} // for(int nItem = nCount-1; nItem >= 0; nItem-- )	
					::SendMessage(m_hWndMDIClient, WM_MDITILE, MDITILE_HORIZONTAL, 0);	
				} // if( nCount != LB_ERR && nCount > 0 )
				bNeedToUpdateButtons = true;
				break;
				
			case IDC_EXT_SAVE:
				if( nCount != LB_ERR && nCount > 0 )
				{
					for( int nItem = 0; nItem < nCount; nItem++ ) 
					{
						if( m_ListWindows.GetSel(nItem) > 0 ) 
						{
							HWND hWnd = (HWND)m_ListWindows.GetItemData(nItem);
							if( hWnd != NULL ) 
							{
								CMDIChildWnd* pMDIChildWnd = (CMDIChildWnd*)CWnd::FromHandle(hWnd);
								if (!pMDIChildWnd)
									continue;
								CDocument *pDocument = pMDIChildWnd->GetActiveDocument();
								if(pDocument != NULL)
									pDocument->SaveModified();
							} // if( hWnd != NULL )
						} // if( m_ListWindows.GetSel(nItem) > 0 ) 
					} // for( int nItem = 0; nItem < nCount; nItem++ ) 
				} // if( nCount != LB_ERR && nCount > 0 )
				_FillMDIWindowList();
				_SelectMDIActive();
				bNeedToUpdateButtons = true;
				break;
				
			case IDC_EXT_MINIMIZE:
				if( nCount != LB_ERR && nCount > 0 )
				{
					for(int nItem = nCount-1; nItem >= 0; nItem-- )	
					{
						HWND hWnd=(HWND)m_ListWindows.GetItemData(nItem);
						if( m_ListWindows.GetSel(nItem) > 0 )
							::ShowWindow(hWnd, SW_MINIMIZE);
					} // for(int nItem = nCount-1; nItem >= 0; nItem-- )
					::SendMessage(m_hWndMDIClient, WM_MDICASCADE, 0, 0);	
				} // if( nCount != LB_ERR && nCount > 0 )
				bNeedToUpdateButtons = true;
				break;
				
			case IDC_EXT_ACTIVATE:
				::GetWindowPlacement(hWnd, &wp);
				if( wp.showCmd == SW_SHOWMINIMIZED )
					::ShowWindow( hWnd, SW_RESTORE );
				::SendMessage( m_hWndMDIClient, WM_MDIACTIVATE, (WPARAM)hWnd, 0 );
				CExtResizableDialog::OnOK();
				break;
				
			case IDC_EXT_CLOSEWINDOWS:
				if( nCount != LB_ERR && nCount > 0 )
				{
					for( int nItem = 0; nItem < nCount; nItem++ ) 
					{
						if( m_ListWindows.GetSel(nItem) > 0 ) 
						{
							HWND hWnd = (HWND)m_ListWindows.GetItemData(nItem);
							if( hWnd != NULL )
								::SendMessage(hWnd, WM_CLOSE, 0,0);
						} // if( m_ListWindows.GetSel(nItem) > 0 ) 
					} // for( int nItem = 0; nItem < nCount; nItem++ ) 
				} // if( nCount != LB_ERR && nCount > 0 )
				_FillMDIWindowList();
				_SelectMDIActive();
				bNeedToUpdateButtons = true;
				break;
				
			case IDC_EXT_CASCADE:
				if( nCount != LB_ERR && nCount > 0 )
				{
					for(int nItem = nCount-1; nItem >= 0; nItem-- )	
					{
						HWND hWnd=(HWND)m_ListWindows.GetItemData(nItem);
						if( m_ListWindows.GetSel(nItem) > 0 )
							::ShowWindow(hWnd, SW_RESTORE);
						else
							::ShowWindow(hWnd, SW_MINIMIZE);
					} // for(int nItem = nCount-1; nItem >= 0; nItem-- )	
					::SendMessage(m_hWndMDIClient, WM_MDICASCADE, 0, 0);	
				} // if( nCount != LB_ERR && nCount > 0 )
				bNeedToUpdateButtons = true;
				break;
			}
			if( bNeedToUpdateButtons )
				OnUpdateButtons();
		} // if(hWnd != (HWND)LB_ERR)
	} // if( nCode == CN_COMMAND )
	return CExtResizableDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}