home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // VistaDoc.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include "VistaDoc.h"
-
- IMPLEMENT_DYNCREATE (CVistaDoc, CDocument)
-
- BOOL CVistaDoc::OnOpenDocument (LPCTSTR lpszPathName)
- {
- if (!CDocument::OnOpenDocument (lpszPathName)) {
- POSITION pos = GetFirstViewPosition ();
- CView* pView = GetNextView (pos);
- pView->OnInitialUpdate ();
- return FALSE;
- }
-
- // Return now if this device doesn't support palettes
- CClientDC dc (AfxGetMainWnd ());
- if ((dc.GetDeviceCaps (RASTERCAPS) & RC_PALETTE) == 0)
- return TRUE;
-
- // Create a palette to go with the DIB section
- if ((HBITMAP) m_bitmap != NULL) {
- DIBSECTION ds;
- m_bitmap.GetObject (sizeof (DIBSECTION), &ds);
-
- int nColors;
- if (ds.dsBmih.biClrUsed != 0)
- nColors = ds.dsBmih.biClrUsed;
- else
- nColors = 1 << ds.dsBmih.biBitCount;
-
- // Create a halftone palette if the DIB section contains more
- // than 256 colors
- if (nColors > 256)
- m_palette.CreateHalftonePalette (&dc);
-
- // Create a custom palette from the DIB section's color table
- // if the number of colors is 256 or less
- else {
- RGBQUAD* pRGB = new RGBQUAD[nColors];
-
- CDC memDC;
- memDC.CreateCompatibleDC (&dc);
- CBitmap* pOldBitmap = memDC.SelectObject (&m_bitmap);
- ::GetDIBColorTable ((HDC) memDC, 0, nColors, pRGB);
- memDC.SelectObject (pOldBitmap);
-
- UINT nSize = sizeof (LOGPALETTE) +
- (sizeof (PALETTEENTRY) * (nColors - 1));
- LOGPALETTE* pLP = (LOGPALETTE*) new BYTE[nSize];
-
- pLP->palVersion = 0x300;
- pLP->palNumEntries = nColors;
-
- for (int i=0; i<nColors; i++) {
- pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
- pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
- pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
- pLP->palPalEntry[i].peFlags = 0;
- }
-
- m_palette.CreatePalette (pLP);
- delete[] pLP;
- delete[] pRGB;
- }
- }
- return TRUE;
- }
-
- void CVistaDoc::DeleteContents ()
- {
- if ((HBITMAP) m_bitmap != NULL)
- m_bitmap.DeleteObject ();
-
- if ((HPALETTE) m_palette != NULL)
- m_palette.DeleteObject ();
-
- CDocument::DeleteContents();
- }
-
- void CVistaDoc::Serialize (CArchive& ar)
- {
- if (ar.IsLoading ()) {
- CString strFileName = ar.GetFile ()->GetFilePath ();
-
- HBITMAP hBitmap = (HBITMAP) ::LoadImage (AfxGetInstanceHandle (),
- (LPCTSTR) strFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE |
- LR_CREATEDIBSECTION);
-
- if (hBitmap == NULL)
- AfxThrowArchiveException (CArchiveException::badIndex);
-
- m_bitmap.Attach (hBitmap);
- }
- }
-
- CBitmap* CVistaDoc::GetBitmap ()
- {
- return ((HBITMAP) m_bitmap == NULL) ? NULL : &m_bitmap;
- }
-
- CPalette* CVistaDoc::GetPalette ()
- {
- return ((HPALETTE) m_palette == NULL) ? NULL : &m_palette;
- }
-