home *** CD-ROM | disk | FTP | other *** search
- // $$VAL:BaseDocCpp$$ : implementation of the $$VAL:BaseDocClass$$ class
- //
-
- #include "stdafx.h"
- #include "$$VAL:ResInclude$$"
-
- #include "$$VAL:BaseDocHeader$$"
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // $$VAL:BaseDocClass$$
-
- IMPLEMENT_DYNCREATE($$VAL:BaseDocClass$$, CDocument)
-
- BEGIN_MESSAGE_MAP($$VAL:BaseDocClass$$, CDocument)
- //{{AFX_MSG_MAP($$VAL:BaseDocClass$$)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // $$VAL:BaseDocClass$$ construction/destruction
-
- $$VAL:BaseDocClass$$::$$VAL:BaseDocClass$$()
- {
- }
-
- $$VAL:BaseDocClass$$::~$$VAL:BaseDocClass$$()
- {
- }
-
- BOOL $$VAL:BaseDocClass$$::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
-
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
-
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // $$VAL:BaseDocClass$$ serialization
-
- void $$VAL:BaseDocClass$$::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- }
- else
- {
- // TODO: add loading code here
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // $$VAL:BaseDocClass$$ diagnostics
-
- #ifdef _DEBUG
- void $$VAL:BaseDocClass$$::AssertValid() const
- {
- CDocument::AssertValid();
- }
-
- void $$VAL:BaseDocClass$$::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // $$VAL:BaseDocClass$$ commands
-
- // NOTE: To change the standard colors, change these values:
- static RGBQUAD rgbStdColors[] =
- {
- { 0, 0, 0, 0 },
- { 0, 0,128, 0 },
- { 0,128, 0, 0 },
- { 0,128,128, 0 },
- { 128, 0, 0, 0 },
- { 128, 0,128, 0 },
- { 128,128, 0, 0 },
- { 192,192,192, 0 },
- { 192,220,192, 0 },
- { 240,202,166, 0 },
- { 238,238,238, 0 },
- { 221,221,221, 0 },
- { 204,204,204, 0 },
- { 187,187,187, 0 },
- { 170,170,170, 0 },
- { 153,153,153, 0 },
- { 136,136,136, 0 },
- { 119,119,119, 0 },
- { 102,102,102, 0 },
- { 85, 85, 85, 0 },
- { 68, 68, 68, 0 },
- { 51, 51, 51, 0 },
- { 34, 34, 34, 0 },
- { 17, 17, 17, 0 },
- { 204,255,255, 0 },
- { 153,255,255, 0 },
- { 102,255,255, 0 },
- { 51,255,255, 0 },
- { 255,204,255, 0 },
- { 204,204,255, 0 },
- { 153,204,255, 0 },
- { 102,204,255, 0 },
- { 51,204,255, 0 },
- { 0,204,255, 0 },
- { 255,153,255, 0 },
- { 204,153,255, 0 },
- { 153,153,255, 0 },
- { 102,153,255, 0 },
- { 51,153,255, 0 },
- { 0,153,255, 0 },
- { 255,102,255, 0 },
- { 204,102,255, 0 },
- { 153,102,255, 0 },
- { 102,102,255, 0 },
- { 51,102,255, 0 },
- { 0,102,255, 0 },
- { 255, 51,255, 0 },
- { 204, 51,255, 0 },
- { 153, 51,255, 0 },
- { 102, 51,255, 0 },
- { 51, 51,255, 0 },
- { 0, 51,255, 0 },
- { 204, 0,255, 0 },
- { 153, 0,255, 0 },
- { 102, 0,255, 0 },
- { 51, 0,255, 0 },
- { 255,255,204, 0 },
- { 204,255,204, 0 },
- { 153,255,204, 0 },
- { 102,255,204, 0 },
- { 51,255,204, 0 },
- { 0,255,204, 0 },
- { 255,204,204, 0 },
- { 153,204,204, 0 },
- { 102,204,204, 0 },
- { 51,204,204, 0 },
- { 0,204,204, 0 },
- { 255,153,204, 0 },
- { 204,153,204, 0 },
- { 153,153,204, 0 },
- { 102,153,204, 0 },
- { 51,153,204, 0 },
- { 0,153,204, 0 },
- { 255,102,204, 0 },
- { 204,102,204, 0 },
- { 153,102,204, 0 },
- { 102,102,204, 0 },
- { 51,102,204, 0 },
- { 0,102,204, 0 },
- { 255, 51,204, 0 },
- { 204, 51,204, 0 },
- { 153, 51,204, 0 },
- { 102, 51,204, 0 },
- { 51, 51,204, 0 },
- { 0, 51,204, 0 },
- { 255, 0,204, 0 },
- { 204, 0,204, 0 },
- { 153, 0,204, 0 },
- { 102, 0,204, 0 },
- { 51, 0,204, 0 },
- { 255,255,153, 0 },
- { 204,255,153, 0 },
- { 153,255,153, 0 },
- { 102,255,153, 0 },
- { 51,255,153, 0 },
- { 0,255,153, 0 },
- { 255,204,153, 0 },
- { 204,204,153, 0 },
- { 153,204,153, 0 },
- { 102,204,153, 0 },
- { 51,204,153, 0 },
- { 0,204,153, 0 },
- { 255,153,153, 0 },
- { 204,153,153, 0 },
- { 102,153,153, 0 },
- { 51,153,153, 0 },
- { 0,153,153, 0 },
- { 255,102,153, 0 },
- { 204,102,153, 0 },
- { 153,102,153, 0 },
- { 102,102,153, 0 },
- { 51,102,153, 0 },
- { 0,102,153, 0 },
- { 255, 51,153, 0 },
- { 204, 51,153, 0 },
- { 153, 51,153, 0 },
- { 102, 51,153, 0 },
- { 51, 51,153, 0 },
- { 0, 51,153, 0 },
- { 255, 0,153, 0 },
- { 204, 0,153, 0 },
- { 153, 0,153, 0 },
- { 102, 0,153, 0 },
- { 51, 0,153, 0 },
- { 255,255,102, 0 },
- { 204,255,102, 0 },
- { 153,255,102, 0 },
- { 102,255,102, 0 },
- { 51,255,102, 0 },
- { 0,255,102, 0 },
- { 255,204,102, 0 },
- { 204,204,102, 0 },
- { 153,204,102, 0 },
- { 102,204,102, 0 },
- { 51,204,102, 0 },
- { 0,204,102, 0 },
- { 255,153,102, 0 },
- { 204,153,102, 0 },
- { 153,153,102, 0 },
- { 102,153,102, 0 },
- { 51,153,102, 0 },
- { 0,153,102, 0 },
- { 255,102,102, 0 },
- { 204,102,102, 0 },
- { 153,102,102, 0 },
- { 51,102,102, 0 },
- { 0,102,102, 0 },
- { 255, 51,102, 0 },
- { 204, 51,102, 0 },
- { 153, 51,102, 0 },
- { 102, 51,102, 0 },
- { 51, 51,102, 0 },
- { 0, 51,102, 0 },
- { 255, 0,102, 0 },
- { 204, 0,102, 0 },
- { 153, 0,102, 0 },
- { 102, 0,102, 0 },
- { 51, 0,102, 0 },
- { 255,255, 51, 0 },
- { 204,255, 51, 0 },
- { 153,255, 51, 0 },
- { 102,255, 51, 0 },
- { 51,255, 51, 0 },
- { 0,255, 51, 0 },
- { 255,204, 51, 0 },
- { 204,204, 51, 0 },
- { 153,204, 51, 0 },
- { 102,204, 51, 0 },
- { 51,204, 51, 0 },
- { 0,204, 51, 0 },
- { 255,153, 51, 0 },
- { 204,153, 51, 0 },
- { 153,153, 51, 0 },
- { 102,153, 51, 0 },
- { 51,153, 51, 0 },
- { 0,153, 51, 0 },
- { 255,102, 51, 0 },
- { 204,102, 51, 0 },
- { 153,102, 51, 0 },
- { 102,102, 51, 0 },
- { 51,102, 51, 0 },
- { 0,102, 51, 0 },
- { 255, 51, 51, 0 },
- { 204, 51, 51, 0 },
- { 153, 51, 51, 0 },
- { 102, 51, 51, 0 },
- { 0, 51, 51, 0 },
- { 255, 0, 51, 0 },
- { 204, 0, 51, 0 },
- { 153, 0, 51, 0 },
- { 102, 0, 51, 0 },
- { 51, 0, 51, 0 },
- { 204,255, 0, 0 },
- { 153,255, 0, 0 },
- { 102,255, 0, 0 },
- { 51,255, 0, 0 },
- { 255,204, 0, 0 },
- { 204,204, 0, 0 },
- { 153,204, 0, 0 },
- { 102,204, 0, 0 },
- { 51,204, 0, 0 },
- { 255,153, 0, 0 },
- { 204,153, 0, 0 },
- { 153,153, 0, 0 },
- { 102,153, 0, 0 },
- { 0, 0,238, 0 },
- { 0, 0,221, 0 },
- { 0, 0,204, 0 },
- { 0, 0,187, 0 },
- { 0, 0,170, 0 },
- { 0, 0,153, 0 },
- { 0, 0,136, 0 },
- { 0, 0,119, 0 },
- { 0, 0,102, 0 },
- { 0, 0, 85, 0 },
- { 0, 0, 68, 0 },
- { 0, 0, 51, 0 },
- { 0, 0, 34, 0 },
- { 0, 0, 17, 0 },
- { 0,238, 0, 0 },
- { 0,221, 0, 0 },
- { 0,204, 0, 0 },
- { 0,187, 0, 0 },
- { 0,170, 0, 0 },
- { 0,153, 0, 0 },
- { 0,136, 0, 0 },
- { 0,119, 0, 0 },
- { 0,102, 0, 0 },
- { 0, 85, 0, 0 },
- { 0, 68, 0, 0 },
- { 0, 51, 0, 0 },
- { 0, 34, 0, 0 },
- { 0, 17, 0, 0 },
- { 238, 0, 0, 0 },
- { 221, 0, 0, 0 },
- { 204, 0, 0, 0 },
- { 187, 0, 0, 0 },
- { 170, 0, 0, 0 },
- { 153, 0, 0, 0 },
- { 136, 0, 0, 0 },
- { 119, 0, 0, 0 },
- { 102, 0, 0, 0 },
- { 85, 0, 0, 0 },
- { 68, 0, 0, 0 },
- { 51, 0, 0, 0 },
- { 34, 0, 0, 0 },
- { 240,251,255, 0 },
- { 164,160,160, 0 },
- { 128,128,128, 0 },
- { 0, 0,255, 0 },
- { 0,255, 0, 0 },
- { 0,255,255, 0 },
- { 255, 0, 0, 0 },
- { 255, 0,255, 0 },
- { 255,255, 0, 0 },
- { 255,255,255, 0 }
- };
-
- BOOL $$VAL:BaseDocClass$$::NormalizeColors(LPBITMAPINFO lpbiDest, LPBITMAPINFO lpbiSrc)
- {
- // If we have too few colors, put our entries in the middle, and use colors
- // from the standard palette to fill in the beginning and end of the palette.
- // If we have too many colors, use the first (cColorsWanted / 2) and the last
- // (cColorsWanted / 2) colors from the palette.
-
- UINT cColorsWanted = 1 << lpbiDest->bmiHeader.biBitCount;
- ASSERT(cColorsWanted == 2 || cColorsWanted == 16 || cColorsWanted == 256);
- UINT cHaveColors = 1 << lpbiSrc->bmiHeader.biBitCount;
- if (cColorsWanted < cHaveColors)
- {
- // we know cColorsWanted will be even, so this is ok
- ASSERT(cColorsWanted % 2 == 0);
-
- // copy the first half
- memcpy(&lpbiDest->bmiColors[0],
- &lpbiSrc->bmiColors[0],
- (cColorsWanted / 2) * sizeof(RGBQUAD));
-
- // copy the second half
- memcpy(&lpbiDest->bmiColors[cColorsWanted / 2],
- &lpbiSrc->bmiColors[cHaveColors - (cColorsWanted / 2)],
- (cColorsWanted / 2) * sizeof(RGBQUAD));
- }
- else
- {
- // cHaveColors might not be even...
- UINT cNeedColors = cColorsWanted - cHaveColors;
- UINT cFirstColors = cNeedColors / 2;
- UINT cLastColors = cNeedColors - cFirstColors;
-
- // insert cFirstColors standard colors into our palette
- memcpy(&lpbiDest->bmiColors[0],
- &rgbStdColors[0],
- cFirstColors * sizeof(RGBQUAD));
-
- // set the middle colors equal to the old colors
- memcpy(&lpbiDest->bmiColors[cFirstColors],
- &lpbiSrc->bmiColors[0],
- cHaveColors * sizeof(RGBQUAD));
-
- // and set the last cLastColors to standard colors
- memcpy(&lpbiDest->bmiColors[cColorsWanted - cLastColors],
- &rgbStdColors[256 - cLastColors],
- cLastColors * sizeof(RGBQUAD));
- }
-
- return TRUE;
- }
-
- #define WIDTHBYTES(bits) ((((bits) + 31) / 32) * 4)
- BOOL $$VAL:BaseDocClass$$::OnOpenDocument(LPCTSTR lpszPathName)
- {
- // Read the DIB in, and create a DIBSECTION.
- SECURITY_ATTRIBUTES sa;
- sa.nLength = sizeof(SECURITY_ATTRIBUTES);
- sa.lpSecurityDescriptor = NULL;
- sa.bInheritHandle = FALSE;
-
- // Open the file
- HANDLE hFile = CreateFile(lpszPathName,
- GENERIC_READ, FILE_SHARE_READ, &sa, OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == NULL)
- {
- DoMessageBox(CString(_T("OnOpenDocument: Cannot open file")));
- return FALSE;
- }
-
- // Create a mapping
- HANDLE hMap = CreateFileMapping(hFile, &sa, PAGE_READONLY,
- 0, 0, NULL);
- if (hMap == NULL)
- {
- VERIFY(CloseHandle(hFile));
- DoMessageBox(CString(_T("OnOpenDocument: Cannot create file mapping")));
- return FALSE;
- }
-
- // Map a view of the file
- LPVOID lpFile = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
- if (lpFile == NULL)
- {
- VERIFY(CloseHandle(hMap));
- VERIFY(CloseHandle(hFile));
- DoMessageBox(CString(_T("OnOpenDocument: Cannot map view of file")));
- return FALSE;
- }
-
- // Convert the Bitmap file into a DIB Section
- // BLOCK: Verify the file header
- LPVOID lpBits;
- {
- LPBITMAPFILEHEADER lpfh = (LPBITMAPFILEHEADER) lpFile;
- if (lpfh->bfType != ((WORD) 'B' | ('M' << 8)))
- {
- VERIFY(UnmapViewOfFile(lpFile));
- VERIFY(CloseHandle(hMap));
- VERIFY(CloseHandle(hFile));
- DoMessageBox(CString(_T("OnOpenDocument: Unrecognized file type")));
- return FALSE;
- }
-
- lpBits = (LPBYTE) lpFile + lpfh->bfOffBits;
- }
-
- LPBITMAPINFO lpbi = (LPBITMAPINFO) ((LPBITMAPFILEHEADER)lpFile + 1);
- // BLOCK: This code only supports 2, 16, and 256 color bitmaps
- {
- if (lpbi->bmiHeader.biBitCount != 1 &&
- lpbi->bmiHeader.biBitCount != 4 &&
- lpbi->bmiHeader.biBitCount != 8)
- {
- VERIFY( UnmapViewOfFile( lpFile ) );
- VERIFY( CloseHandle( hMap ) );
- VERIFY( CloseHandle( hFile ) );
- DoMessageBox(CString(_T("OnOpenDocument: Unsupported number of colors")));
- return FALSE;
- }
- }
-
- // Get the size of the bits
- DWORD dwBits = lpbi->bmiHeader.biHeight *
- WIDTHBYTES(lpbi->bmiHeader.biWidth * lpbi->bmiHeader.biBitCount);
- if (lpbi->bmiHeader.biSizeImage != 0)
- dwBits = lpbi->bmiHeader.biSizeImage;
-
- // Get the size of the color table
- UINT cColors = 1 << lpbi->bmiHeader.biBitCount;
- if (lpbi->bmiHeader.biClrUsed != 0)
- cColors = lpbi->bmiHeader.biClrUsed;
-
- // BLOCK: Create a DIB section
- LPVOID lpDest;
- HBITMAP hDib;
- LPBITMAPINFO lpbiNew;
- {
- lpbiNew = (LPBITMAPINFO) new
- BYTE[sizeof(BITMAPINFOHEADER) + (sizeof(RGBQUAD) * 256)];
-
- // NOTE: If you want to change the memory format of the DIB,
- // change this code:
- lpbiNew->bmiHeader = lpbi->bmiHeader;
- lpbiNew->bmiHeader.biBitCount = 8;
- lpbiNew->bmiHeader.biCompression = BI_RGB;
- lpbiNew->bmiHeader.biSizeImage = 0;
- NormalizeColors( lpbiNew, lpbi );
-
- hDib = CreateDIBSection( NULL, lpbiNew, DIB_RGB_COLORS, &lpDest, NULL, 0);
- if (hDib == NULL)
- {
- delete [] (BYTE *)lpbiNew;
- VERIFY( UnmapViewOfFile( lpFile ) );
- VERIFY( CloseHandle( hMap ) );
- VERIFY( CloseHandle( hFile ) );
- DoMessageBox(CString(_T("OnOpenDocument: Cannot create DIB format")));
- return FALSE;
- }
- ASSERT( lpDest != NULL );
- }
-
- // BLOCK: Set the DIB sections bits from the DIB we are loading
- {
- CWindowDC dc(NULL);
- VERIFY( SetDIBits( dc.m_hDC, hDib,
- 0, lpbi->bmiHeader.biHeight,
- lpBits, lpbi, DIB_RGB_COLORS ) == lpbi->bmiHeader.biHeight );
- }
-
- // Attach the new bitmap to our document, and inform our views of the change
- DeleteContents();
- m_Bitmap.Attach( hDib );
- UpdateAllViews( NULL );
-
- // Create a palette
- CWindowDC dc(NULL);
- if (dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
- {
- LPLOGPALETTE lpLogPal;
- lpLogPal = (LPLOGPALETTE) new
- BYTE[sizeof(LOGPALETTE) + ((255) * sizeof(PALETTEENTRY))];
-
- lpLogPal->palVersion = 0x0300;
- lpLogPal->palNumEntries = 256;
-
- for (UINT i = 0; i < cColors; i++)
- {
- lpLogPal->palPalEntry[i].peRed = lpbiNew->bmiColors[i].rgbRed;
- lpLogPal->palPalEntry[i].peGreen = lpbiNew->bmiColors[i].rgbGreen;
- lpLogPal->palPalEntry[i].peBlue = lpbiNew->bmiColors[i].rgbBlue;
- lpLogPal->palPalEntry[i].peFlags = 0;
- }
-
- VERIFY( m_Palette.CreatePalette( lpLogPal ) );
- delete [] (BYTE *)lpLogPal;
-
- // BLOCK: Reset the active palette
- {
- CWnd* pActiveWnd = CWnd::GetActiveWindow();
- if (pActiveWnd != NULL)
- pActiveWnd->SendMessage(WM_QUERYNEWPALETTE);
- }
- }
-
- // Put stuff back
- delete [] (BYTE *)lpbiNew;
- VERIFY( UnmapViewOfFile( lpFile ) );
- VERIFY( CloseHandle( hMap ) );
- VERIFY( CloseHandle( hFile ) );
-
- return TRUE;
- }
-
- void $$VAL:BaseDocClass$$::DoMessageBox(CString str)
- {
- CWnd* pActiveWnd = CWnd::GetActiveWindow();
- if (pActiveWnd != NULL)
- pActiveWnd->MessageBox(str);
- }
-
-
- BOOL $$VAL:BaseDocClass$$::OnSaveDocument(LPCTSTR lpszPathName)
- {
- // this class doesn't implement saving anything
- ASSERT(FALSE);
- return FALSE;
- }
-
- CSize $$VAL:BaseDocClass$$::GetSize() const
- {
- if (m_Bitmap.m_hObject == NULL)
- return CSize(0,0);
-
- BITMAP bm;
- m_Bitmap.GetObject( sizeof(BITMAP), &bm );
- return CSize(bm.bmWidth, bm.bmHeight);
- }
-
- CPalette* $$VAL:BaseDocClass$$::GetPalette()
- {
- if (m_Palette.m_hObject == NULL)
- return NULL;
-
- return &m_Palette;
- }
-
-
- void $$VAL:BaseDocClass$$::DeleteContents()
- {
- if (m_Bitmap.m_hObject != NULL)
- m_Bitmap.DeleteObject();
- if (m_Palette.m_hObject != NULL)
- m_Palette.DeleteObject();
-
- CDocument::DeleteContents();
- }
-