home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / leadtools / ocx32.lt / OCRVIEW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-02  |  9.1 KB  |  324 lines

  1. // OCRView.cpp : implementation of the COCRDEMOView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "OCRDEMO.h"
  6.  
  7. #include "OCRDoc.h"
  8. #include "CntrItem.h"
  9. #include "OCRView.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. extern COCRApp theApp;
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // COCRDEMOView
  21.  
  22. IMPLEMENT_DYNCREATE(COCRDEMOView, CFormView)
  23.  
  24. BEGIN_MESSAGE_MAP(COCRDEMOView, CFormView)
  25.     //{{AFX_MSG_MAP(COCRDEMOView)
  26.    ON_MESSAGE(WM_DOREALIZE, OnDoRealize)
  27.     ON_WM_SETFOCUS()
  28.     ON_WM_SIZE()
  29.     ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
  30.     ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
  31.     ON_COMMAND(ID_VIEW_FITIMAGETOWINDOW, OnViewFitimagetowindow)
  32.     ON_UPDATE_COMMAND_UI(ID_VIEW_FITIMAGETOWINDOW, OnUpdateViewFitimagetowindow)
  33.     ON_COMMAND(ID_VIEW_NORMAL, OnViewNormal)
  34.     ON_UPDATE_COMMAND_UI(ID_VIEW_NORMAL, OnUpdateViewNormal)
  35.     //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // COCRDEMOView construction/destruction
  40.  
  41. COCRDEMOView::COCRDEMOView()
  42.     : CFormView(COCRDEMOView::IDD)
  43. {
  44.     //{{AFX_DATA_INIT(COCRDEMOView)
  45.         // NOTE: the ClassWizard will add member initialization here
  46.     //}}AFX_DATA_INIT
  47.     m_pSelection = NULL;
  48.     // TODO: add construction code here
  49. }
  50.  
  51. COCRDEMOView::~COCRDEMOView()
  52. {
  53. }
  54.  
  55. void COCRDEMOView::DoDataExchange(CDataExchange* pDX)
  56. {
  57.     CFormView::DoDataExchange(pDX);
  58.     //{{AFX_DATA_MAP(COCRDEMOView)
  59.     DDX_Control(pDX, IDC_LEADCTRL1, m_Lead1);
  60.     //}}AFX_DATA_MAP
  61. }
  62.  
  63. BOOL COCRDEMOView::PreCreateWindow(CREATESTRUCT& cs)
  64. {
  65.     // TODO: Modify the Window class or styles here by modifying
  66.     //  the CREATESTRUCT cs
  67.  
  68.     return CFormView::PreCreateWindow(cs);
  69. }
  70.  
  71. void COCRDEMOView::OnInitialUpdate()
  72. {
  73.     CFormView::OnInitialUpdate();
  74.     // Disable the scroll bars in the parent window
  75.    SetScrollSizes(MM_TEXT,CSize(1,1));
  76.  
  77.     // Fit the LEAD control to the client area and enable autoscrolling.
  78.     CRect   rcClient;
  79.     GetClientRect(rcClient);
  80.     m_Lead1.MoveWindow(0, 0, rcClient.right, rcClient.bottom, FALSE); 
  81.     m_Lead1.SetAutoScroll(TRUE);
  82.    m_Lead1.SetPaintSizeMode(theApp.m_nPaintSize);
  83. }
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // OLE Client support and commands
  87.  
  88. BOOL COCRDEMOView::IsSelected(const CObject* pDocItem) const
  89. {
  90.     // The implementation below is adequate if your selection consists of
  91.     //  only COCRDEMOCntrItem objects.  To handle different selection
  92.     //  mechanisms, the implementation here should be replaced.
  93.  
  94.     // TODO: implement this function that tests for a selected OLE client item
  95.  
  96.     return pDocItem == m_pSelection;
  97. }
  98.  
  99. void COCRDEMOView::OnInsertObject()
  100. {
  101.     // Invoke the standard Insert Object dialog box to obtain information
  102.     //  for new COCRDEMOCntrItem object.
  103.     COleInsertDialog dlg;
  104.     if (dlg.DoModal() != IDOK)
  105.         return;
  106.  
  107.     BeginWaitCursor();
  108.  
  109.     COCRDEMOCntrItem* pItem = NULL;
  110.     TRY
  111.     {
  112.         // Create new item connected to this document.
  113.         COCRDEMODoc* pDoc = GetDocument();
  114.         ASSERT_VALID(pDoc);
  115.         pItem = new COCRDEMOCntrItem(pDoc);
  116.         ASSERT_VALID(pItem);
  117.  
  118.         // Initialize the item from the dialog data.
  119.         if (!dlg.CreateItem(pItem))
  120.             AfxThrowMemoryException();  // any exception will do
  121.         ASSERT_VALID(pItem);
  122.  
  123.         // If item created from class list (not from file) then launch
  124.         //  the server to edit the item.
  125.         if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
  126.             pItem->DoVerb(OLEIVERB_SHOW, this);
  127.  
  128.         ASSERT_VALID(pItem);
  129.  
  130.         // As an arbitrary user interface design, this sets the selection
  131.         //  to the last item inserted.
  132.  
  133.         // TODO: reimplement selection as appropriate for your application
  134.  
  135.         m_pSelection = pItem;   // set selection to last inserted item
  136.         pDoc->UpdateAllViews(NULL);
  137.     }
  138.     CATCH(CException, e)
  139.     {
  140.         if (pItem != NULL)
  141.         {
  142.             ASSERT_VALID(pItem);
  143.             pItem->Delete();
  144.         }
  145.         AfxMessageBox(IDP_FAILED_TO_CREATE);
  146.     }
  147.     END_CATCH
  148.  
  149.     EndWaitCursor();
  150. }
  151.  
  152. // The following command handler provides the standard keyboard
  153. //  user interface to cancel an in-place editing session.  Here,
  154. //  the container (not the server) causes the deactivation.
  155. void COCRDEMOView::OnCancelEditCntr()
  156. {
  157.     // Close any in-place active item on this view.
  158.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  159.     if (pActiveItem != NULL)
  160.     {
  161.         pActiveItem->Close();
  162.     }
  163.     ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  164. }
  165.  
  166. // Special handling of OnSetFocus and OnSize are required for a container
  167. //  when an object is being edited in-place.
  168. void COCRDEMOView::OnSetFocus(CWnd* pOldWnd)
  169. {
  170.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  171.     if (pActiveItem != NULL &&
  172.         pActiveItem->GetItemState() == COleClientItem::activeUIState)
  173.     {
  174.         // need to set focus to this item if it is in the same view
  175.         CWnd* pWnd = pActiveItem->GetInPlaceWindow();
  176.         if (pWnd != NULL)
  177.         {
  178.             pWnd->SetFocus();   // don't call the base class
  179.             return;
  180.         }
  181.     }
  182.  
  183.     CFormView::OnSetFocus(pOldWnd);
  184. }
  185.  
  186. void COCRDEMOView::OnSize(UINT nType, int cx, int cy)
  187. {
  188. //    CFormView::OnSize(nType, cx, cy);
  189. //    COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  190. //    if (pActiveItem != NULL)
  191. //        pActiveItem->SetItemRects();
  192.     CFormView::OnSize(nType, cx, cy);
  193.  
  194.     if( IsWindow(m_Lead1.m_hWnd) )
  195.         m_Lead1.MoveWindow(CRect( 0, 0, cx, cy));
  196. }
  197.  
  198. CLead* COCRDEMOView::GetDocLead() 
  199. {
  200.     COCRDEMODoc* pDoc = GetDocument();
  201.     return(&pDoc->m_Lead1);
  202. }
  203.  
  204. /////////////////////////////////////////////////////////////////////////////
  205. // COCRDEMOView diagnostics
  206.  
  207. #ifdef _DEBUG
  208. void COCRDEMOView::AssertValid() const
  209. {
  210.     CFormView::AssertValid();
  211. }
  212.  
  213. void COCRDEMOView::Dump(CDumpContext& dc) const
  214. {
  215.     CFormView::Dump(dc);
  216. }
  217.  
  218. COCRDEMODoc* COCRDEMOView::GetDocument() // non-debug version is inline
  219. {
  220.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COCRDEMODoc)));
  221.     return (COCRDEMODoc*)m_pDocument;
  222. }
  223. #endif //_DEBUG
  224.  
  225. /////////////////////////////////////////////////////////////////////////////
  226. // COCRDEMOView message handlers
  227.  
  228. void COCRDEMOView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
  229. {
  230.     CFormView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  231.     if (bActivate)
  232.     {
  233.         ASSERT(pActivateView == this);
  234.         OnDoRealize((WPARAM)theApp.m_pMainWnd->m_hWnd, (LPARAM) FALSE);   // same as SendMessage(WM_DOREALIZE);
  235.     }
  236. }
  237.  
  238. LRESULT COCRDEMOView::OnDoRealize(WPARAM wParam, LPARAM lParam)
  239. {
  240.     if( !IsWindow(m_Lead1.m_hWnd) || !m_Lead1.GetBitmap() )
  241.         return FALSE;
  242.     if(lParam)
  243.     {
  244.         return m_Lead1.SendMessage(WM_PALETTECHANGED, wParam);
  245.     }
  246.     else
  247.     {
  248.         UINT nColorsChanged = 0;
  249.         CDC* pdc;
  250.  
  251.         pdc = theApp.m_pMainWnd->GetDC();
  252.         HPALETTE hpal = (HPALETTE)m_Lead1.GetPalette((OLE_HANDLE) pdc->m_hDC);
  253.         if(hpal)
  254.         {
  255.             CPalette pal;
  256.  
  257.             pal.Attach(hpal);
  258.             CPalette* oldPalette = pdc->SelectPalette(&pal, (BOOL) lParam);
  259.             nColorsChanged = pdc->RealizePalette();
  260.             if (nColorsChanged > 0)
  261.             {
  262.                 m_Lead1.InvalidateRect(NULL, FALSE);
  263.             }
  264.             pdc->SelectPalette(oldPalette, TRUE);
  265.         }
  266.         theApp.m_pMainWnd->ReleaseDC(pdc);
  267.         return ((LRESULT) (BOOL) (nColorsChanged > 0));
  268.     }
  269. }
  270.  
  271. void COCRDEMOView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  272. {
  273.     // Note that this program has one instance of the Lead control for each 
  274.     // view, plus the one instance for the document, which is used as the master copy.
  275.     // The UpdateAllViews function calls this function once for each view; and 
  276.     // this function uses the master copy to update each view.
  277.  
  278.     ASSERT(pSender != this);
  279.     UNUSED(pSender);     // unused in release builds
  280.  
  281.     COCRDEMODoc* pDoc = GetDocument();
  282.  
  283.     // Avoid repainting until we are ready.
  284.     m_Lead1.SetAutoRepaint(FALSE);
  285.  
  286.     // Update this view's bitmap with the document bitmap.
  287.     m_Lead1.SetBackErase(TRUE);
  288.    if(IsWindow(pDoc->m_Lead1.m_hWnd))
  289.       m_Lead1.SetBitmap(GetDocLead()->GetBitmap());
  290.    m_Lead1.SetPaintPalette(PAINTPALETTE_AUTO);
  291.    m_Lead1.SetPaintDither(PAINTDITHER_DIFFUSION);
  292.    m_Lead1.SetBitonalScaling(BITONALSCALING_SCALETOGRAY);
  293.    
  294.    // Fit the LEAD control to the client area and enable autoscrolling.
  295.     CRect   rcClient;
  296.     GetClientRect(rcClient);
  297.     m_Lead1.MoveWindow(0, 0, rcClient.right, rcClient.bottom, FALSE); 
  298.     m_Lead1.SetAutoScroll(TRUE);
  299.     m_Lead1.SetAutoRepaint(TRUE);
  300. }
  301.  
  302.  
  303. void COCRDEMOView::OnViewNormal() 
  304. {
  305.    m_Lead1.SetPaintSizeMode(PAINTSIZEMODE_NORMAL);
  306.    theApp.m_nPaintSize = PAINTSIZEMODE_NORMAL;
  307. }
  308.  
  309. void COCRDEMOView::OnUpdateViewNormal(CCmdUI* pCmdUI) 
  310. {
  311.    pCmdUI->SetCheck(m_Lead1.GetPaintSizeMode() == PAINTSIZEMODE_NORMAL);
  312. }
  313.  
  314. void COCRDEMOView::OnViewFitimagetowindow() 
  315. {
  316.    m_Lead1.SetPaintSizeMode(PAINTSIZEMODE_FIT);
  317.    theApp.m_nPaintSize = PAINTSIZEMODE_FIT;
  318. }
  319.  
  320. void COCRDEMOView::OnUpdateViewFitimagetowindow(CCmdUI* pCmdUI) 
  321. {
  322.    pCmdUI->SetCheck(m_Lead1.GetPaintSizeMode() == PAINTSIZEMODE_FIT);
  323. }
  324.