home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Power Pack / Visual_Basic4_Power_Pack.bin / vb4files / ilib_vb / mfcdoc.cp_ / mfcdoc.cp
Encoding:
Text File  |  1996-11-20  |  6.6 KB  |  303 lines

  1. // mfcdoc.cpp : implementation of the CMfcDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "imview.h"
  6. #include <limits.h>
  7.  
  8. #include "mfcdoc.h" 
  9. #include "sky16v3b.h" 
  10. #include "encdlg.h"
  11. #include "decdlg.h"
  12.  
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char BASED_CODE THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CDibDoc
  20.  
  21. IMPLEMENT_DYNCREATE(CMfcDoc, CDocument)
  22.  
  23. BEGIN_MESSAGE_MAP(CMfcDoc, CDocument)
  24.     //{{AFX_MSG_MAP(CMfcDoc)
  25.     //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CMfcDoc construction/destruction
  30.  
  31. CMfcDoc::CMfcDoc()
  32. {
  33.     m_ddb = NULL;
  34.     m_pal = NULL;
  35.     m_sizeDoc = CSize(1,1);     // dummy value to make CScrollView happy
  36. }
  37.  
  38. CMfcDoc::~CMfcDoc()
  39. {
  40.     if (m_ddb != NULL)
  41.     {
  42.         delete m_ddb;
  43.     }
  44.     if (m_pal != NULL)
  45.     {
  46.         delete m_pal;
  47.     }
  48. }
  49.  
  50. void CMfcDoc::InitBitmapData()
  51. {       
  52.     BITMAP bm;
  53.     if (m_pal != NULL)
  54.     {
  55.         delete m_pal;
  56.         m_pal = NULL;
  57.     }
  58.     if (m_ddb != NULL)
  59.     {          
  60.         delete m_ddb;
  61.         return;
  62.     }
  63.     // Create copy of palette
  64.     m_pal = new CPalette;
  65.     if (m_pal == NULL)
  66.     {
  67.         // we must be really low on memory
  68.         m_ddb = NULL;
  69.         return;
  70.     }
  71.     m_pal->Attach(hpal);
  72.     m_ddb = new CBitmap;
  73.     if (m_ddb == NULL)
  74.     {
  75.         // we must be really low on memory  
  76.         delete m_pal;
  77.         m_pal = NULL;
  78.         return;
  79.     }
  80.     m_ddb->Attach(hddb);
  81.     // Set up document size 
  82.     GetObject(m_ddb->m_hObject, sizeof(BITMAP), (LPSTR)&bm);
  83.     
  84.     m_sizeDoc = CSize(bm.bmWidth, bm.bmHeight); 
  85. }
  86.  
  87. int CMfcDoc::DecodeFile(const char* filename)
  88. {                                               
  89.   int res = 0, colors, dither;
  90.   char * ptr;
  91.   int type;   
  92.   CDecDlg dlg;
  93.   dlg.m_colors = 1;
  94.   dlg.m_dither = 1;
  95.   if (dlg.DoModal() == IDOK)
  96.    {             
  97.     BeginWaitCursor();
  98.     if (dlg.m_colors == 0)
  99.       colors = 24;
  100.     else if (dlg.m_colors == 1)
  101.       colors = 8;
  102.     else if (dlg.m_colors == 2)
  103.       colors = 4;              
  104.     else if (dlg.m_colors == 3)
  105.       colors = 0;
  106.     else
  107.       colors = 1;
  108.     dither = dlg.m_dither;   
  109.     type = -1;
  110.     ptr = strstr(filename, ".BMP");
  111.     if (ptr != NULL)
  112.        type = 1;
  113.     ptr = strstr(filename, ".GIF");
  114.     if (ptr != NULL)
  115.        type = 2;
  116.     ptr = strstr(filename, ".PCX");
  117.     if (ptr != NULL)
  118.        type = 3;
  119.     ptr = strstr(filename, ".JPG");
  120.     if (ptr != NULL)
  121.        type = 4;
  122.     ptr = strstr(filename, ".PNG");
  123.     if (ptr != NULL)
  124.        type = 5;     
  125.     ptr = strstr(filename, ".TIF");
  126.     if (ptr != NULL)
  127.        type = 6;
  128.  
  129.     switch(type)
  130.       {
  131.        case 1:
  132.           res = readbmpfile(filename, colors, dither,(UINT *)&hddb,
  133.                                 (UINT *)&hpal, NULL, 1);
  134.         break;
  135.        case 2:
  136.         res = readgiffile(filename, colors, dither,(UINT *)&hddb,
  137.                                 (UINT *)&hpal, NULL, 1);
  138.         break;
  139.        case 3:
  140.         res = readpcxfile(filename, colors, dither,(UINT *)&hddb,
  141.                                 (UINT *)&hpal, NULL, 1);
  142.         break;
  143.        case 4:
  144.         res = readjpgfile(filename, colors, 1, dither,(UINT *)&hddb,
  145.                                 (UINT *)&hpal, NULL, 1);
  146.         break;
  147.        case 5:
  148.         res = readpngfile(filename, colors, dither,(UINT *)&hddb,
  149.                                 (UINT *)&hpal, NULL, 1);
  150.         break;  
  151.        case 6:
  152.         res = readtiffile(filename, colors, dither,(UINT *)&hddb,
  153.                                 (UINT *)&hpal, NULL, 1);
  154.         break;
  155.        default:
  156.          res = 0;   
  157.          break;
  158.       }
  159.      EndWaitCursor();
  160.    }
  161.  
  162.   return res;
  163. }
  164.  
  165. BOOL CMfcDoc::OnOpenDocument(const char* pszPathName)
  166. {
  167.     DeleteContents();
  168.    // open here
  169.     if (DecodeFile(pszPathName) != 1)
  170.      {
  171.         MessageBox(NULL, "Couldn't load Image", NULL,
  172.                          MB_ICONINFORMATION | MB_OK); 
  173.         return FALSE;
  174.      }
  175.  
  176.      InitBitmapData();
  177.  
  178.     if (m_ddb == NULL)
  179.     {
  180.         // may not be valid format
  181.         MessageBox(NULL, "Couldn't load Image", NULL,
  182.                          MB_ICONINFORMATION | MB_OK);
  183.         return FALSE;
  184.     }
  185.     SetPathName(pszPathName);
  186.     SetModifiedFlag(FALSE);     // start off with unmodified
  187.     return TRUE;
  188. }
  189.  
  190.  
  191. BOOL CMfcDoc::OnSaveDocument(const char* pszPathName)
  192. {
  193.     BOOL bSuccess = FALSE;
  194.     SetModifiedFlag(FALSE);     // back to unmodified
  195.     if (EncodeFile(pszPathName) == 1)
  196.       bSuccess = TRUE;
  197.     else
  198.       bSuccess = FALSE;
  199.     if (!bSuccess)
  200.     {
  201.         MessageBox(NULL, "Couldn't save Image", NULL,
  202.                          MB_ICONINFORMATION | MB_OK);
  203.     }
  204.  
  205.     return bSuccess;
  206. }
  207.  
  208.  
  209. int CMfcDoc::EncodeFile(const char* filename)
  210. {                                               
  211.   int res = 0, colors, quality, smooth;
  212.   char * ptr;
  213.   int type;   
  214.   CEncDlg dlg;
  215.   dlg.m_colors = 1;      
  216.   dlg.m_quality = 70;
  217.   if (dlg.DoModal() == IDOK)
  218.    {             
  219.     BeginWaitCursor();
  220.     if (dlg.m_colors == 0)
  221.       colors = 24;
  222.     else if (dlg.m_colors == 1)
  223.       colors = 8;
  224.     else if (dlg.m_colors == 2)
  225.       colors = 4;              
  226.     else if (dlg.m_colors == 3)
  227.       colors = 0;
  228.     else
  229.       colors = 1;
  230.     quality = dlg.m_quality;   
  231.     smooth = 0;
  232.     type = -1;
  233.     ptr = strstr(filename, ".BMP");
  234.     if (ptr != NULL)
  235.        type = 1;
  236.     ptr = strstr(filename, ".GIF");
  237.     if (ptr != NULL)
  238.        type = 2;
  239.     ptr = strstr(filename, ".PCX");
  240.     if (ptr != NULL)
  241.        type = 3;
  242.     ptr = strstr(filename, ".JPG");
  243.     if (ptr != NULL)
  244.        type = 4;
  245.     ptr = strstr(filename, ".PNG");
  246.     if (ptr != NULL)
  247.        type = 5;
  248.     ptr = strstr(filename, ".TIF");
  249.     if (ptr != NULL)
  250.        type = 6;
  251.  
  252.     switch(type)
  253.       {
  254.        case 1:
  255.           res = writebmpfile(filename, colors,(UINT)m_ddb->m_hObject,
  256.                                 (UINT)m_pal->m_hObject, NULL, 1);
  257.         break;
  258.        case 2:
  259.         res = writegiffile(filename, colors,(UINT)m_ddb->m_hObject,
  260.                                 (UINT)m_pal->m_hObject, NULL, 1);
  261.         break;
  262.        case 3:
  263.         res = writepcxfile(filename, colors,(UINT)m_ddb->m_hObject,
  264.                                 (UINT)m_pal->m_hObject, NULL, 1);
  265.         break;
  266.        case 4:
  267.         res = writejpgfile(filename, quality, smooth, colors, (UINT)m_ddb->m_hObject,
  268.                                 (UINT)m_pal->m_hObject, NULL, 1);
  269.         break;
  270.        case 5:
  271.         res = writepngfile(filename, colors, 1,(UINT)m_ddb->m_hObject,
  272.                                 (UINT)m_pal->m_hObject, NULL, 1);
  273.         break;
  274.        case 6:
  275.         res = writetiffile(filename, NO_COMPRESSION, 5, colors,(UINT)m_ddb->m_hObject,
  276.                                 (UINT)m_pal->m_hObject, NULL, 1);
  277.        default:
  278.         res = 0;
  279.         break;
  280.       }
  281.      EndWaitCursor();
  282.   }         
  283.   return res;
  284. }
  285. /////////////////////////////////////////////////////////////////////////////
  286. // CMfcDoc diagnostics
  287.  
  288. #ifdef _DEBUG
  289. void CMfcDoc::AssertValid() const
  290. {
  291.     CDocument::AssertValid();
  292. }
  293.  
  294. void CMfcDoc::Dump(CDumpContext& dc) const
  295. {
  296.     CDocument::Dump(dc);
  297. }
  298.  
  299. #endif //_DEBUG
  300.  
  301. /////////////////////////////////////////////////////////////////////////////
  302. // CMfcDoc commands
  303.