home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / leadtools / ocx32.lt / OCDBVIEW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-18  |  13.8 KB  |  462 lines

  1. // ltcdbView.cpp : implementation of the CLtcdbView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "ltcdb.h"
  6.  
  7. #include "ltcdbSet.h"
  8. #include "ltcdbDoc.h"
  9. #include "ocdbView.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CLtcdbView
  19.  
  20. IMPLEMENT_DYNCREATE(CLtcdbView, CDaoRecordView)
  21.  
  22. BEGIN_MESSAGE_MAP(CLtcdbView, CDaoRecordView)
  23.     //{{AFX_MSG_MAP(CLtcdbView)
  24.     ON_WM_DESTROY()
  25.     ON_BN_CLICKED(IDC_ADDNEW, OnAddnew)
  26.     ON_BN_CLICKED(IDC_FLIP, OnFlip)
  27.     ON_BN_CLICKED(IDC_DELETE, OnDelete)
  28.     ON_BN_CLICKED(IDC_FINDFIRST, OnFindfirst)
  29.     ON_BN_CLICKED(IDC_FINDNEXT, OnFindnext)
  30.     ON_EN_SETFOCUS(IDC_SEARCHTEXT, OnSetfocusSearchtext)
  31.     //}}AFX_MSG_MAP
  32.     ON_WM_QUERYNEWPALETTE()
  33.     ON_WM_PALETTECHANGED()
  34. END_MESSAGE_MAP()
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CLtcdbView construction/destruction
  38.  
  39. CLtcdbView::CLtcdbView()
  40.     : CDaoRecordView(CLtcdbView::IDD)
  41. {
  42.     //{{AFX_DATA_INIT(CLtcdbView)
  43.     m_pSet = NULL;
  44.     //}}AFX_DATA_INIT
  45.     // TODO: add construction code here
  46.  
  47. }
  48.  
  49. CLtcdbView::~CLtcdbView()
  50. {
  51. }
  52.  
  53. void CLtcdbView::DoDataExchange(CDataExchange* pDX)
  54. {
  55.     CDaoRecordView::DoDataExchange(pDX);
  56.     //{{AFX_DATA_MAP(CLtcdbView)
  57.     DDX_FieldText(pDX, IDC_Cwho, m_pSet->m_who, m_pSet);
  58.     DDX_Control(pDX, IDC_LEADCTRL1, m_Lead1);
  59.     //}}AFX_DATA_MAP
  60.     if ((NormalMove == TRUE) && (NewPosition != m_pSet->GetAbsolutePosition()))
  61.     {
  62.         OldPosition = NewPosition;
  63.         NewPosition = m_pSet->GetAbsolutePosition();
  64.         m_Lead1.dbMove(NewPosition - OldPosition);
  65.     }
  66. }
  67.  
  68. BOOL CLtcdbView::PreCreateWindow(CREATESTRUCT& cs)
  69. {
  70.     // TODO: Modify the Window class or styles here by modifying
  71.     //  the CREATESTRUCT cs
  72.  
  73.     return CDaoRecordView::PreCreateWindow(cs);
  74. }
  75.  
  76. void CLtcdbView::OnInitialUpdate()
  77. {
  78.     m_pSet = &GetDocument()->m_ltcdbSet;
  79.     CDaoRecordView::OnInitialUpdate();
  80.     // Turn off automatic scrolling and repainting
  81.    UNLOCKSUPPORT(m_Lead1); // Unlock support for the LZW files and express capabilities
  82.     m_Lead1.SetAutoScroll(FALSE);
  83.     m_Lead1.SetAutoRepaint(FALSE);
  84.  
  85.  
  86.    // IMPORTANT NOTE:
  87.    // To keep things synchronized, edit the LTCDBSET.CPP and make sure the path 
  88.    // in CLtcdbSet::GetDefaultDBName() points to the real place of the
  89.    // database file!.
  90.  
  91.  
  92.  
  93.     // Open the database.
  94.     // Use the same SELECT statement that is used for the data control.
  95.     // Note that the image field must be listed first in the SELECT statement.
  96.     int nRet = m_Lead1.dbOpen("ODBC;DSN=LEADACCESS", "SELECT photo FROM people ORDER BY who", "photo", DB_OPENOPTIONS_NONE);
  97.     // Set the LEAD control's database properties
  98.     m_Lead1.SetDbLoadBits(24);
  99.     m_Lead1.SetDbLockingMode(DB_LOCKINGMODE_OPTIMISTIC);
  100.  
  101.    m_Lead1.SetAnnTool(ANNTOOL_RECT);
  102.    m_Lead1.SetAnnUserMode(ANNUSERMODE_DESIGN);
  103.  
  104.     // Enable use of the dbMove method in the data control's Reposition event
  105.     NormalMove = TRUE;
  106.     NewPosition = 0;
  107. }
  108.  
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CLtcdbView diagnostics
  111.  
  112. #ifdef _DEBUG
  113. void CLtcdbView::AssertValid() const
  114. {
  115.     CDaoRecordView::AssertValid();
  116. }
  117.  
  118. void CLtcdbView::Dump(CDumpContext& dc) const
  119. {
  120.     CDaoRecordView::Dump(dc);
  121. }
  122.  
  123. CLtcdbDoc* CLtcdbView::GetDocument() // non-debug version is inline
  124. {
  125.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLtcdbDoc)));
  126.     return (CLtcdbDoc*)m_pDocument;
  127. }
  128. #endif //_DEBUG
  129.  
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CLtcdbView database support
  132. CDaoRecordset* CLtcdbView::OnGetRecordset()
  133. {
  134.     return m_pSet;
  135. }
  136.  
  137.  
  138. /////////////////////////////////////////////////////////////////////////////
  139. // CLtcdbView message handlers
  140.  
  141. BEGIN_EVENTSINK_MAP(CLtcdbView, CDaoRecordView)
  142.     //{{AFX_EVENTSINK_MAP(CLtcdbView)
  143.     ON_EVENT(CLtcdbView, IDC_LEADCTRL1, 6 /* Change */, OnChangeLeadctrl1, VTS_NONE)
  144.     //}}AFX_EVENTSINK_MAP
  145. END_EVENTSINK_MAP()
  146.  
  147. void CLtcdbView::OnChangeLeadctrl1() 
  148. {
  149.     // Avoid sizing images that are not loaded.
  150.     if (m_Lead1.GetBitmap() == 0)
  151.     {
  152.         m_Lead1.ForceRepaint(); // So we can see when images are missing.
  153.         return;
  154.     }
  155.     // Calculate the display rectangle to fit the image inside the control
  156.     float HeightAllowed = m_Lead1.GetScaleHeight();
  157.     float WidthAllowed = m_Lead1.GetScaleWidth();
  158.     float XProportion = m_Lead1.GetBitmapWidth();
  159.     float YProportion = m_Lead1.GetBitmapHeight();
  160.     float DisplayTop;
  161.     float DisplayHeight;
  162.     float DisplayWidth;
  163.     float DisplayLeft;
  164.  
  165.     // See if using the allowed height makes the image too wide.
  166.     if ((HeightAllowed * XProportion / YProportion) <= WidthAllowed)
  167.     {   // Use the allowed height.
  168.         DisplayTop = (float)0;
  169.         DisplayHeight = HeightAllowed;
  170.         DisplayWidth = DisplayHeight * XProportion / YProportion;
  171.         DisplayLeft = (WidthAllowed - DisplayWidth) / 2;
  172.     }    
  173.     else
  174.     {   // Use the allowed width.
  175.         DisplayLeft = (float)0;
  176.         DisplayWidth = WidthAllowed;
  177.         DisplayHeight = DisplayWidth * YProportion / XProportion;
  178.         DisplayTop = (HeightAllowed - DisplayHeight) / 2;
  179.     }
  180.  
  181.     // Set the image display size
  182.     m_Lead1.SetDstRect(DisplayLeft, DisplayTop, DisplayWidth, DisplayHeight);
  183.     m_Lead1.SetDstClipRect(DisplayLeft, DisplayTop, DisplayWidth, DisplayHeight);
  184.     m_Lead1.ForceRepaint();
  185.     
  186. }
  187.  
  188. void CLtcdbView::OnDestroy() 
  189. {
  190.     CDaoRecordView::OnDestroy();
  191.     
  192.     // TODO: Add your message handler code here
  193.     m_Lead1.dbClose();
  194.     
  195. }
  196.  
  197. void CLtcdbView::OnAddnew() 
  198. {
  199.     // Make sure we can add records to the database.
  200.     if (!m_pSet->CanUpdate())
  201.     {
  202.         MessageBox("You cannot update this database.", "ERROR");
  203.         return; 
  204.     }
  205.     // Get the name of the file to load.
  206.     // This filter list is complete, except for GIF, which requires an LZW license.
  207.     CString ImageFilter = "Graphics|*.cmp; *.jpg; *.jff; *.jtf; *.bmp; *.tif; *.tga; *.pcx; *.cal; *.mac; *.mac; *.img; *.msp; *.wpg; *.wpg; *.ras; *.pct; *.pcd; *.eps; *.wmf||";
  208.     CFileDialog  OpenFile(TRUE, NULL, NULL,0,ImageFilter);
  209.     if (OpenFile.DoModal() != IDOK)
  210.         return;
  211.     CWaitCursor wait;  // Display an hourglass 
  212.     CString MyFile = OpenFile.GetPathName();
  213.  
  214.     // Disable the normal synchronization code and hide the LEAD control
  215.     // while we manipulate the recordsets.
  216.     NormalMove = FALSE; 
  217.     m_Lead1.ShowWindow(SW_HIDE);
  218.     // Add a record to the DAO recordset.
  219.     m_pSet->AddNew();
  220.     SetDlgItemText(IDC_Cwho, MyFile);
  221.     UpdateData();
  222.  
  223.     try
  224.     {
  225.         m_pSet->Update();
  226.     }
  227.     catch( CDaoException* e )
  228.     {
  229.        AfxMessageBox( 
  230.           e->m_pErrorInfo->m_strDescription, 
  231.           MB_ICONEXCLAMATION );
  232.         m_pSet->CancelUpdate();
  233.          UpdateData(FALSE);
  234.         m_Lead1.ShowWindow(SW_NORMAL);
  235.         NormalMove = TRUE; 
  236.        return;
  237.     }
  238.  
  239.  
  240.     // Requery the DAO recordset and wait for the change to take effect.
  241.     m_pSet->Requery();
  242.     Sleep(5000);
  243.     
  244.     // Requery the LEAD recordset and make sure the recordsets match.
  245.     m_Lead1.dbRequery();
  246.     m_pSet->MoveFirst();
  247.     NewPosition = 0;
  248.     m_pSet->MoveLast();
  249.  
  250.     if (NewPosition != m_pSet->GetAbsolutePosition())
  251.     {
  252.         OldPosition = NewPosition;
  253.         NewPosition = m_pSet->GetAbsolutePosition();
  254.         m_Lead1.dbMove(NewPosition - OldPosition);
  255.     }
  256.     if(m_Lead1.GetDbCurrentRecord() != m_pSet->GetAbsolutePosition())
  257.     {
  258.         MessageBox("Synchronization error!\nDelete the record and exit", "ERROR", MB_OK);
  259.         m_pSet->FindFirst(CString("who = '") + MyFile + CString("'"));
  260.         return;
  261.     }
  262.     // Find the record we just added and synchronize the recordsets.
  263.     m_pSet->FindFirst(CString("who = '") + MyFile + CString("'"));
  264.     if (NewPosition != m_pSet->GetAbsolutePosition())
  265.     {
  266.         OldPosition = NewPosition;
  267.         NewPosition = m_pSet->GetAbsolutePosition();
  268.         m_Lead1.dbMove(NewPosition - OldPosition);
  269.     }
  270.  
  271.     // Use return values to trap errors.
  272.     BOOL ErrHand = m_Lead1.GetEnableMethodErrors(); // Save the current setting.
  273.     m_Lead1.SetEnableMethodErrors(FALSE);
  274.     // Redisplay the LEAD control, load the image, update the record,
  275.     // and restore normal recordset synchronization.
  276.     m_Lead1.ShowWindow(SW_NORMAL);
  277.     m_Lead1.dbEdit();
  278.     int nRet = m_Lead1.Load(MyFile, 0, 0, 1);
  279.     if (nRet != 0)
  280.     {
  281.         char  buffer[50];
  282.         sprintf(buffer, "LEAD Error %d loading image.", nRet);
  283.         MessageBox(buffer, "ERROR", MB_OK);
  284.     }
  285.  
  286.     // Update the recordset using the appropriate format
  287.     if (m_Lead1.GetBitmapBits() == 1)
  288.          nRet = m_Lead1.dbUpdate(FILE_LEAD1BIT, 1, 0);
  289.     else if  (m_Lead1.GetBitmapBits() == 4)
  290.          nRet = m_Lead1.dbUpdate(FILE_PCX, 4, 0);
  291.     else if (m_Lead1.GetIsGrayscale() == GRAY_NO)
  292.          nRet = m_Lead1.dbUpdate(FILE_LEAD, 24, QFACTOR_QMS);
  293.     else // save as grayscale
  294.          nRet = m_Lead1.dbUpdate(FILE_LEAD, 8, QFACTOR_QMS);
  295.     if (nRet != 0)
  296.     {
  297.         char  buffer[50];
  298.         sprintf(buffer, "LEAD Error %d saving image to database", nRet);
  299.         MessageBox(buffer, "ERROR", MB_OK);
  300.     }
  301.     // Restore previous error handling.
  302.     m_Lead1.SetEnableMethodErrors(ErrHand);
  303.     NormalMove = TRUE;
  304.     
  305. }
  306.  
  307. void CLtcdbView::OnFlip() 
  308. {
  309.     // Make sure we can update records in the database.
  310.     if (m_Lead1.GetDbCanUpdate() == FALSE)
  311.     {
  312.         MessageBox("You cannot update this database.", "ERROR");
  313.         return; 
  314.     }
  315.     // Make sure the current record contains an image.
  316.     if (m_Lead1.GetDbIsEOF() || m_Lead1.GetDbIsBOF() || m_Lead1.GetBitmap() == 0)
  317.     {
  318.         MessageBox("No current image", "NOTICE", MB_OK);
  319.         return;
  320.     }
  321.     // Flip the image.
  322.     m_Lead1.dbEdit();
  323.     m_Lead1.Flip();
  324.  
  325.     // Use return values to trap errors.
  326.     BOOL ErrHand = m_Lead1.GetEnableMethodErrors(); // Save the current setting.
  327.     m_Lead1.SetEnableMethodErrors(FALSE);
  328.     // Update the recordset using the appropriate format
  329.     int nRet;
  330.     if (m_Lead1.GetBitmapBits() == 1)
  331.          nRet = m_Lead1.dbUpdate(FILE_LEAD1BIT, 1, 0);
  332.     else if  (m_Lead1.GetBitmapBits() == 4)
  333.          nRet = m_Lead1.dbUpdate(FILE_PCX, 4, 0);
  334.     else if (m_Lead1.GetIsGrayscale() == GRAY_NO)
  335.          nRet = m_Lead1.dbUpdate(FILE_LEAD, 24, QFACTOR_QMS);
  336.     else // save as grayscale
  337.          nRet = m_Lead1.dbUpdate(FILE_LEAD, 8, QFACTOR_QMS);
  338.     if (nRet != 0)
  339.     {
  340.         char  buffer[50];
  341.         sprintf(buffer, "LEAD Error %d saving image to database", nRet);
  342.         MessageBox(buffer, "ERROR", MB_OK);
  343.     }
  344.     // Restore previous error handling.
  345.     m_Lead1.SetEnableMethodErrors(ErrHand);
  346.     
  347. }
  348.  
  349. void CLtcdbView::OnDelete() 
  350. {
  351.     // Make sure we can delete records from the database.
  352.     if (!m_pSet->CanUpdate())
  353.     {
  354.         MessageBox("You cannot update this database.", "ERROR");
  355.         return; 
  356.     }
  357.     // Make sure the database is not empty.
  358.     if (m_pSet->IsEOF() && m_pSet->IsBOF())
  359.     {
  360.         MessageBox("The database is empty", "NOTICE", MB_OK);
  361.         return;
  362.     }
  363.     CWaitCursor wait;  // Display an hourglass 
  364.     // Disable the normal record synchronization and hide the LEAD control.
  365.     NormalMove = FALSE;
  366.     m_Lead1.ShowWindow(SW_HIDE);
  367.  
  368.     // Save the current position.
  369.     long RecMarker = m_pSet->GetAbsolutePosition();
  370.     // Delete the current record and wait for the change to take effect.
  371.     m_pSet->Delete();
  372.     Sleep(5000);
  373.     // Requery the recordsets, move to the last record,
  374.     // and initialize the NewPosition global variable.
  375.     m_pSet->Requery();
  376.     m_Lead1.dbRequery();
  377.     m_pSet->MoveLast();
  378.     NewPosition = m_pSet->GetAbsolutePosition();
  379.     m_Lead1.dbMove(NewPosition);
  380.     // Make sure the LEAD control is on the last record.
  381.     m_Lead1.dbMoveNext();
  382.     if(m_Lead1.GetDbIsEOF())
  383.     {
  384.         m_Lead1.dbMovePrev();
  385.     }
  386.     else
  387.     {
  388.         int nRet = MessageBox("Synchronization error!\nRestart the application", "ERROR", MB_OK);
  389.         return;
  390.     }
  391.  
  392.     // Return to the old record position, if possible.
  393.     NormalMove = TRUE;
  394.     m_Lead1.ShowWindow(SW_NORMAL);
  395.     long RelativePosition = RecMarker - NewPosition;
  396.     if (RelativePosition < 0)
  397.     {
  398.         m_pSet->Move(RelativePosition);
  399.     }
  400.     // Update the edit box with data from the current record.
  401.     UpdateData(FALSE);
  402.     
  403. }
  404.  
  405. BOOL CLtcdbView::OnQueryNewPalette()
  406. {
  407.     if(!IsWindow(m_Lead1.m_hWnd))
  408.             return FALSE;
  409.     return m_Lead1.SendMessage(WM_QUERYNEWPALETTE);
  410. }
  411.  
  412. void CLtcdbView::OnPaletteChanged(CWnd* pFocusWnd)
  413. {
  414.     if(pFocusWnd->m_hWnd == m_hWnd || !IsWindow(m_Lead1.m_hWnd))
  415.             return;
  416.     m_Lead1.SendMessage(WM_PALETTECHANGED, (WPARAM) pFocusWnd->m_hWnd);
  417. }
  418. void CLtcdbView::OnFindfirst() 
  419. {
  420.     CString SearchString;
  421.     GetDlgItemText(IDC_SEARCHTEXT,SearchString);
  422.     if (m_pSet->FindFirst(CString("who Like '*") + SearchString + CString("*'")))
  423.     {
  424.         // Generate a data exchange event to update the controls
  425.         UpdateData(FALSE);
  426.         // Switch the default button to Find Next
  427.         SendDlgItemMessage(IDC_FINDFIRST, BM_SETSTYLE, BS_PUSHBUTTON, (LPARAM)TRUE);
  428.         SendMessage(DM_SETDEFID, IDC_FINDNEXT, 0L);
  429.         SendDlgItemMessage(IDC_FINDNEXT, BM_SETSTYLE, BS_DEFPUSHBUTTON, (LPARAM)TRUE);
  430.     }
  431.     else
  432.     {
  433.         MessageBox("Not found", "Notice");
  434.     }
  435. }
  436.  
  437. void CLtcdbView::OnFindnext() 
  438. {
  439.     CString SearchString;
  440.     GetDlgItemText(IDC_SEARCHTEXT,SearchString);
  441.     if (m_pSet->FindNext(CString("who Like '*") + SearchString + CString("*'")))
  442.     {
  443.         // Generate a data exchange event to update the controls
  444.         UpdateData(FALSE);
  445.     }
  446.     else
  447.     {
  448.         MessageBox("Not found", "Notice");
  449.     }
  450.     
  451. }
  452.  
  453.  
  454. void CLtcdbView::OnSetfocusSearchtext() 
  455. {
  456.     // Set the Find First button as the default button
  457.     SendMessage(DM_SETDEFID, IDC_FINDFIRST, 0L);
  458.     SendDlgItemMessage(IDC_FINDFIRST, BM_SETSTYLE, BS_DEFPUSHBUTTON, (LPARAM)TRUE);
  459. }
  460.  
  461.  
  462.