home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / leadtools / ocx32.lt / Ocrdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-13  |  7.5 KB  |  304 lines

  1. // OCRDoc.cpp : implementation of the COCRDEMODoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "OCRDEMO.h"
  6.  
  7. #include "OCRDoc.h"
  8. #include "CntrItem.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. extern COCRApp theApp;
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // COCRDEMODoc
  20.  
  21. IMPLEMENT_DYNCREATE(COCRDEMODoc, COleDocument)
  22.  
  23. BEGIN_MESSAGE_MAP(COCRDEMODoc, COleDocument)
  24.     //{{AFX_MSG_MAP(COCRDEMODoc)
  25.     ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
  26.     ON_COMMAND(ID_OCR_RECOGNIZE, OnOcrRecognize)
  27.     ON_COMMAND(ID_OCR_EDITDOCUMENT, OnOcrEditdocument)
  28.     ON_COMMAND(ID_OCR_CLEARDOCUMENT, OnOcrCleardocument)
  29.     //}}AFX_MSG_MAP
  30.     // Enable default OLE container implementation
  31.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, COleDocument::OnUpdatePasteMenu)
  32.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, COleDocument::OnUpdatePasteLinkMenu)
  33.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CONVERT, COleDocument::OnUpdateObjectVerbMenu)
  34.     ON_COMMAND(ID_OLE_EDIT_CONVERT, COleDocument::OnEditConvert)
  35.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, COleDocument::OnUpdateEditLinksMenu)
  36.     ON_COMMAND(ID_OLE_EDIT_LINKS, COleDocument::OnEditLinks)
  37.     ON_UPDATE_COMMAND_UI(ID_OLE_VERB_FIRST, COleDocument::OnUpdateObjectVerbMenu)
  38. END_MESSAGE_MAP()
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // COCRDEMODoc construction/destruction
  42. static const WCHAR BASED_CODE _szLicString[] =
  43.     L"LEADTOOLS OCX Copyright (c) 1998 LEAD Technologies, Inc.";
  44.  
  45. COCRDEMODoc::COCRDEMODoc()
  46. {
  47.    m_pOcr = NULL;
  48. }
  49.  
  50. void COCRDEMODoc::InitLEADDoc(void)
  51. {
  52.     CRect   rcClient(0,0,1,1);
  53.     BSTR lpLic = SysAllocString(_szLicString);
  54.     m_Lead1.Create("", 0, rcClient, theApp.m_pMainWnd, 0,NULL,FALSE,lpLic);
  55.     m_Lead1.ShowWindow(SW_HIDE);
  56.     UNLOCKSUPPORT(m_Lead1);
  57.     SysFreeString(lpLic);
  58. }
  59.  
  60. COCRDEMODoc::~COCRDEMODoc()
  61. {
  62.    if(m_pOcr)
  63.    {
  64.       m_pOcr->DestroyWindow();
  65.       delete m_pOcr;
  66.    }
  67.    m_Lead1.DestroyWindow();
  68. }
  69.  
  70. BOOL COCRDEMODoc::OnNewDocument()
  71. {
  72.     if (!COleDocument::OnNewDocument())
  73.         return FALSE;
  74.  
  75.     // TODO: add reinitialization code here
  76.     // (SDI documents will reuse this document)
  77.  
  78.     return TRUE;
  79. }
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // COCRDEMODoc serialization
  83.  
  84. void COCRDEMODoc::Serialize(CArchive& ar)
  85. {
  86.     if (ar.IsStoring())
  87.     {
  88.         // TODO: add storing code here
  89.     }
  90.     else
  91.     {
  92.         // TODO: add loading code here
  93.     }
  94. }
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // COCRDEMODoc diagnostics
  98.  
  99. #ifdef _DEBUG
  100. void COCRDEMODoc::AssertValid() const
  101. {
  102.     COleDocument::AssertValid();
  103. }
  104.  
  105. void COCRDEMODoc::Dump(CDumpContext& dc) const
  106. {
  107.     COleDocument::Dump(dc);
  108. }
  109. #endif //_DEBUG
  110.  
  111. /////////////////////////////////////////////////////////////////////////////
  112. // COCRDEMODoc commands
  113.  
  114. BOOL COCRDEMODoc::OnOpenDocument(LPCTSTR lpszPathName) 
  115. {
  116.     int nRet;
  117.  
  118.    if(lpszPathName)
  119.    {
  120.        nRet = m_Lead1.Load(lpszPathName, 0, theApp.m_nPage, 1);
  121.        if( nRet )
  122.        {
  123.            theApp.DisplayLEADError(nRet);
  124.            return FALSE;
  125.        }
  126.        else
  127.        theApp.m_fImage = TRUE;
  128.  
  129.        if ((m_pOcr == NULL) && (!nRet))
  130.        {
  131.           m_pOcr = new CLeadOcr;
  132.           m_pOcr->Create("", 0, CRect(0, 0, 50, 50), theApp.m_pMainWnd, 0, NULL, FALSE, NULL);
  133.           m_pOcr->UnlockSupport(L_SUPPORT_OCR, L_KEY_OCR);
  134.        }
  135.    }
  136.    return TRUE;
  137. }
  138.  
  139. #define FILETYPES "Ami Pro 2.0|*.ami|\
  140. Ami Pro 3.0|*.ami|\
  141. ASCII Smart|*.txt|\
  142. ASCII Standard|*.txt|\
  143. ASCII Standard (DOS)|*.txt|\
  144. ASCII Stripped|*.txt|\
  145. dBase IV v1.0|*.dbf|\
  146. DCA/RFT|*.dca|\
  147. DisplayWrite 5|*.dw5|\
  148. Excel for the Macintosh|*.xls|\
  149. Excel 3.0|*.xls|\
  150. Excel 4.0|*.xls|\
  151. Excel 5.0|*.xls|\
  152. Excel Office 97|*.xls|\
  153. FrameMaker|*.doc|\
  154. HTML (2.0 specification)|*.htm|\
  155. HTML (SoftQuad Editor)|*.htm|\
  156. HTML (Netscape additions)|*.htm|\
  157. Interleaf|*.doc|\
  158. Lotus 1-2-3|*.123|\
  159. Lotus Word Pro|*.lwp|\
  160. MultiMate Advantage II|*.mma|\
  161. Postscript|*.eps|\
  162. Professional Write 2.0|*.doc|\
  163. Professional Write 2.2|*.doc|\
  164. Quattro Pro|*.pro|\
  165. Rich Text Format|*.rtf|\
  166. Rich Text Format (Macintosh)|*.rtf|\
  167. Rich Text Format (Word 6.0)|*.rtf|\
  168. Windows Write|*.wri|\
  169. Word for Windows 2.X|*.doc|\
  170. Word for Windows 6.0|*.doc|\
  171. Word Office 97|*.doc|\
  172. WordPerfect 4.2 (DOS)|*.doc|\
  173. WordPerfect (Windows)|*.doc|\
  174. WordPerfect 6.0 (Windows)|*.doc|\
  175. WordPerfect 6.1 (Windows)|*.doc|\
  176. WordPerfect 7X (Windows)|*.doc|\
  177. WordStar 1.X (Windows)|*.doc|\
  178. Works|*.doc|\
  179. Xerox XDOC|*.xdc||"
  180.  
  181. void COCRDEMODoc::OnFileSaveAs()
  182. {
  183.    CFileDialog dlg(FALSE, NULL, NULL, OFN_LONGNAMES | OFN_HIDEREADONLY, NULL, NULL);
  184.   
  185.    char szFilter[] = FILETYPES;
  186.    LPTSTR pch;
  187.  
  188.    pch = szFilter;
  189.    while ((pch = _tcschr(pch, '|')) != NULL)
  190.    {
  191.       *pch++ = '\0';
  192.    }
  193.  
  194.    dlg.m_ofn.hInstance       = AfxGetInstanceHandle();
  195.    dlg.m_ofn.lpstrFilter     = szFilter;
  196.    dlg.m_ofn.nFilterIndex    = 0;
  197.  
  198.    if (dlg.DoModal() != IDOK)
  199.    {
  200.       return;
  201.    }
  202.       
  203.    CString strPathName;
  204.    POSITION pos = dlg.GetStartPosition();
  205.    if (pos == NULL)
  206.    {
  207.       return;
  208.    }
  209.  
  210.    strPathName = dlg.GetNextPathName(pos);
  211.  
  212.    m_nFormat = dlg.m_ofn.nFilterIndex + FILE_AMI_PRO_20 - 1;
  213.  
  214. //   DoSave(strPathName, TRUE);
  215.    OnSaveDocument(strPathName);
  216. }
  217.  
  218. BOOL COCRDEMODoc::OnSaveDocument(LPCTSTR lpszPathName) 
  219. {
  220. //    if (!CDocument::OnSaveDocument(lpszPathName))
  221. //        return FALSE;
  222.  
  223.    if (m_pOcr == NULL)
  224.       return FALSE;
  225.  
  226.    short nResult;
  227.  
  228.    BeginWaitCursor();
  229.    nResult = m_pOcr->SaveDoc((char FAR *)lpszPathName, m_nFormat);
  230.    EndWaitCursor();
  231.  
  232.    if (nResult)
  233.       theApp.DisplayLEADError(nResult);
  234.  
  235.    return TRUE;
  236. }
  237.  
  238. void COCRDEMODoc::OnOcrRecognize() 
  239. {
  240.    short         nResult;
  241.  
  242.    if (m_pOcr == NULL)
  243.       return;
  244.    
  245.    BeginWaitCursor();
  246.  
  247.    m_pOcr->SetBitmap((long)m_Lead1.GetBitmap());
  248.  
  249.    m_pOcr->SetPreprocessingMode(theApp.m_nPreprocessingMode);
  250.    m_pOcr->SetRecognitionMode(theApp.m_nRecognitionMode);
  251.     m_pOcr->SetVerifierMode(theApp.m_nVerifierMode);
  252.     m_pOcr->SetVerifierThreshold(theApp.m_nVerifierThreshold);
  253.     m_pOcr->SetMinPointSize(theApp.m_nMinPointSize);
  254.     m_pOcr->SetMaxPointSize(theApp.m_nMaxPointSize);
  255.     m_pOcr->SetLanguage(theApp.m_nLanguage);
  256.     m_pOcr->SetHeaderFooterProcess(theApp.m_bHeaderFooterProcess);
  257.     m_pOcr->SetSloppyManual(theApp.m_bSloppyManual);
  258.     m_pOcr->SetTextOrientLimit(theApp.m_nTextOrientLimit);
  259.    m_pOcr->SetDocumentName(theApp.m_szDocumentName);
  260.     m_pOcr->SetUnrecognized(theApp.m_szUnrecognized);
  261.     m_pOcr->SetQuestionable(theApp.m_szQuestionable);
  262.     m_pOcr->SetLeftSingleQuote(theApp.m_szLeftSingleQuote);
  263.     m_pOcr->SetRightSingleQuote(theApp.m_szRightSingleQuote);
  264.     m_pOcr->SetLeftDoubleQuote(theApp.m_szLeftDoubleQuote);
  265.     m_pOcr->SetRightDoubleQuote(theApp.m_szRightDoubleQuote);
  266.     m_pOcr->SetDecimal(theApp.m_szDecimal);
  267.     m_pOcr->SetThousands(theApp.m_szThousands);
  268.     m_pOcr->SetLexicon(theApp.m_szLexicon);
  269.     m_pOcr->SetTraining(theApp.m_szTraining);
  270.  
  271.    nResult = m_pOcr->RecognizeOCR(OCRFLAG_CALLBACK_AUTO);
  272.    EndWaitCursor();
  273.  
  274.    if (nResult)
  275.    {
  276.       theApp.m_fRecognize = FALSE;
  277.       theApp.DisplayLEADError(nResult);
  278.    }
  279.    else
  280.       theApp.m_fRecognize = TRUE;
  281. }
  282.  
  283. void COCRDEMODoc::OnOcrEditdocument() 
  284. {
  285.    short nResult;
  286.  
  287.    if (m_pOcr == NULL)
  288.       return;
  289.  
  290.    nResult = m_pOcr->EditDoc();
  291.    if (nResult)
  292.       theApp.DisplayLEADError(nResult);
  293. }
  294.  
  295. void COCRDEMODoc::OnOcrCleardocument() 
  296. {
  297.    if (m_pOcr == NULL)
  298.       return;
  299.  
  300.    m_pOcr->ClearDoc();
  301.    theApp.m_fRecognize = FALSE;
  302. }
  303.  
  304.