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

  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "ChildWin.h"
  6. #include "ProgDiag.h"
  7. #include "Main.h"
  8. #include "SaveDlg.h"
  9. //---------------------------------------------------------------------
  10. #pragma link "LTOCX10N"
  11. #pragma resource "*.dfm"
  12. //--------------------------------------------------------------------- 
  13. __fastcall TMDIChild::TMDIChild(TComponent *Owner)
  14.     : TForm(Owner)
  15. {
  16. }
  17. //--------------------------------------------------------------------- 
  18. void __fastcall TMDIChild::FormClose(TObject *Sender, TCloseAction &Action)
  19. {
  20.     Lead1->Bitmap = 0;
  21.     Action = caFree;
  22. }
  23. //---------------------------------------------------------------------
  24.  
  25. //---------------------------------------------------------------------
  26. void TMDIChild::InitStatusProcess(String Title)
  27. {
  28.   ProgressDialog->Caption = Title;
  29.   if(Lead1->EnableProgressEvent)
  30.     ProgressDialog->Show();
  31. }
  32. //---------------------------------------------------------------------------
  33.  
  34. void TMDIChild::ResetStatusProcess()
  35. {
  36.   ProgressDialog->Hide();
  37. }
  38.  
  39. void __fastcall TMDIChild::Lead1ProgressStatus(TObject *Sender, short iPercent)
  40. {
  41.   ProgressDialog->ProgressBar1->Position=iPercent;
  42.   Application->ProcessMessages();
  43. }
  44. //---------------------------------------------------------------------------
  45.  
  46. void TMDIChild::SaveImage()
  47. {
  48.    int nRet,nVal;
  49.    int nFormat,nBits,nQFactor;
  50.  
  51.    MainForm->SaveDialog1->Filter = szSaveFilter;
  52.    if(MainForm->SaveDialog1->Execute())
  53.     {
  54.       SaveOptions->ShowModal();
  55.       if(SaveOptions->ModalResult == mrOk)
  56.        {
  57.          switch(SaveOptions->SaveBits->ItemIndex)
  58.           {
  59.             case 0:
  60.               nBits=1;
  61.               break;
  62.             case 1:
  63.               nBits=2;
  64.               break;
  65.             case 2:
  66.               nBits=3;
  67.               break;
  68.             case 3:
  69.               nBits=4;
  70.               break;
  71.             case 4:
  72.               nBits=5;
  73.               break;
  74.             case 5:
  75.               nBits=6;
  76.               break;
  77.             case 6:
  78.               nBits=7;
  79.               break;
  80.             case 7:
  81.               nBits=8;
  82.               break;
  83.             case 8:
  84.               nBits=16;
  85.               break;
  86.             case 9:
  87.               nBits=24;
  88.               break;
  89.             case 10:
  90.               nBits=32;
  91.               break;
  92.           }
  93.           switch(MainForm->SaveDialog1->FilterIndex)
  94.            {
  95.              case 1:
  96.                if(nBits==1)
  97.                  nFormat=FILE_LEAD1BIT;
  98.                else
  99.                  nFormat=FILE_LEAD;
  100.                break;
  101.              case 2: nFormat = FILE_JFIF; break;
  102.              case 3: nFormat = FILE_CALS; break;
  103.              case 4: nFormat = FILE_PNG; break;
  104.              case 5: nFormat = FILE_EPS; break;
  105.              case 6: nFormat = FILE_IMG; break;
  106.              case 7: nFormat = FILE_GIF; break;
  107.              case 8: nFormat = FILE_RAWICA_G3_1D; break;
  108.              case 9: nFormat = FILE_PCT; break;
  109.              case 10: nFormat = FILE_MAC; break;
  110.              case 11: nFormat = FILE_MSP; break;
  111.              case 12: nFormat = FILE_PCX; break;
  112.              case 13: nFormat = FILE_PSD; break;
  113.              case 14: nFormat = FILE_RAS; break;
  114.              case 15: nFormat = FILE_TGA; break;
  115.              case 16: nFormat = FILE_TIF; break;
  116.              case 17: nFormat = FILE_CCITT; break;
  117.              case 18: nFormat = FILE_JTIF; break;
  118.              case 19: nFormat = FILE_TIFLZW; break;
  119.              case 20: nFormat = FILE_BMP; break;
  120.              case 21: nFormat = FILE_FPX; break;
  121.              case 22: nFormat = FILE_BMP_RLE; break;
  122.              case 23: nFormat = FILE_TIF_CMYK; break;
  123.              case 24: nFormat = FILE_TIFLZW_CMYK; break;
  124.              case 25: nFormat = FILE_TIF_PACKBITS; break;
  125.              case 26: nFormat = FILE_TIF_PACKBITS_CMYK; break;
  126.              case 27: nFormat = FILE_DICOM_GRAY; break;
  127.              case 28: nFormat = FILE_DICOM_COLOR; break;
  128.              case 29: nFormat = FILE_WIN_ICO; break;
  129.              case 30: nFormat = FILE_WIN_CUR; break;
  130.              case 31: nFormat = FILE_TIF_YCC; break;
  131.              case 32: nFormat = FILE_TIFLZW_YCC; break;
  132.              case 33: nFormat = FILE_TIF_PACKBITS_YCC; break;
  133.              case 34: nFormat = FILE_EXIF; break;
  134.              case 35: nFormat = FILE_EXIF_YCC; break;
  135.              case 36: nFormat = FILE_EXIF_JPEG; break;
  136.              case 37: nFormat = FILE_AWD; break;
  137.            }
  138.           nVal = SaveOptions->SaveQFactor->Text.ToInt();
  139.           if(nVal==0)
  140.             Application->MessageBox("Error Saving Image","Error",MB_ICONINFORMATION);
  141.           else
  142.            {
  143.               nQFactor=nVal;
  144.               Cursor=crHourGlass;
  145.               InitStatusProcess("Saving File: " + MainForm->SaveDialog1->FileName);
  146.               nRet=Lead1->Save(MainForm->SaveDialog1->FileName,(short)nFormat,
  147.                                (short)nBits,(short)nQFactor,false);
  148.               ResetStatusProcess();
  149.               Cursor=crDefault;
  150.            }
  151.           if(nRet<0)
  152.             Application->MessageBox("Error Saving Image","Error",MB_ICONINFORMATION);
  153.        }
  154.     }
  155. }
  156. void __fastcall TMDIChild::Lead1MouseDown(TObject *Sender, TMouseButton Button,
  157.     TShiftState Shift, int X, int Y)
  158. {
  159.   if(Button == mbLeft)
  160.    {
  161.       if(Lead1->HasRgn && (Lead1->IsPtInRgn((X-Lead1->DstLeft) * Lead1->SrcWidth /
  162.          Lead1->DstWidth,(Y-Lead1->DstTop)*Lead1->SrcHeight / Lead1->DstHeight)))
  163.        {
  164.           Lead1->RgnMarkingMode=RGNMARK_NONE;
  165.           Lead1->RgnFrameType=RGNFRAME_NONE;
  166.           xStart = X;
  167.           yStart = Y;
  168.           Lead1->FloaterVisible = false;
  169.           Lead1->Floater = Lead1->Bitmap;
  170.           Lead1->SetFloaterDstRect(Lead1->RgnLeft * Lead1->DstWidth / Lead1->SrcWidth +
  171.                                     Lead1->DstLeft,Lead1->RgnTop * Lead1->DstHeight /
  172.                                   Lead1->SrcHeight + Lead1->DstTop, Lead1->FloaterWidth *
  173.                                   Lead1->DstWidth / Lead1->SrcWidth, Lead1->FloaterHeight *
  174.                                   Lead1->DstHeight / Lead1->SrcHeight);
  175.           if(Lead2->Bitmap!=0)
  176.            {
  177.               Lead1->Combine(Lead1->RgnLeft,Lead1->RgnTop,Lead1->FloaterWidth,
  178.                             Lead1->FloaterHeight,Lead2->Bitmap,0.0,0.0,CB_DST_0 | CB_OP_OR);
  179.                             Lead1->RepaintRect(Lead1->FloaterDstLeft,Lead1->FloaterDstTop,
  180.                                               Lead1->FloaterDstWidth,Lead1->FloaterDstHeight,
  181.                                               false);
  182.            }
  183.           Lead1->FloaterVisible=true;
  184.           MovingFloater=true;
  185.        }
  186.       else if(RegionTool!=RGNMARK_NONE)
  187.        {
  188.          Lead1->RgnFrameType=RGNFRAME_NONE;
  189.          Lead1->RgnMarkingMode=(short)RegionTool;
  190.        }
  191.       Lead2->Bitmap=0;
  192.    }
  193. }
  194. //---------------------------------------------------------------------------
  195. void __fastcall TMDIChild::Lead1MouseMove(TObject *Sender, TShiftState Shift,
  196.     int X, int Y)
  197. {
  198.    if(MovingFloater)
  199.     {
  200.        Lead1->SetFloaterDstRect(Lead1->FloaterDstLeft + X - xStart,
  201.                                Lead1->FloaterDstTop + Y - yStart,
  202.                                Lead1->FloaterDstWidth,Lead1->FloaterDstHeight);
  203.        xStart=X;
  204.        yStart=Y;
  205.     }
  206. }
  207. //---------------------------------------------------------------------------
  208. void __fastcall TMDIChild::FormCreate(TObject *Sender)
  209. {
  210.   MovingFloater=false;
  211.   Lead2->Visible=false;
  212.   Lead3->Visible=false;
  213. }
  214. //---------------------------------------------------------------------------
  215. void __fastcall TMDIChild::Lead1MouseUp(TObject *Sender, TMouseButton Button,
  216.     TShiftState Shift, int X, int Y)
  217. {
  218.   int hRegion;
  219.  
  220.   if(Button!=mbLeft)
  221.     return;
  222.   if(Lead1->RgnMarkingMode!=RGNMARK_NONE)
  223.    {
  224.      Lead1->RgnMarkingMode=RGNMARK_NONE;
  225.      if(Lead1->HasRgn)
  226.       {
  227.          Lead1->RgnFrameType=RGNFRAME_ANIMATED;
  228.          MainForm->CancelRegion1->Enabled=true;
  229.       }
  230.      else
  231.        Lead1->RgnFrameType=RGNFRAME_NONE;
  232.    }
  233.   else if(MovingFloater)
  234.    {
  235.      MovingFloater=false;
  236.      Lead1->RgnFrameType=RGNFRAME_NONE;
  237.      hRegion=Lead1->GetFloaterHandle();
  238.      Lead1->SetRgnHandle(hRegion,(Lead1->FloaterDstLeft - Lead1->DstLeft) * Lead1->SrcWidth /
  239.                         Lead1->DstWidth,(Lead1->FloaterDstTop - Lead1->DstTop) * Lead1->SrcHeight /
  240.                         Lead1->DstHeight,L_RGN_SET);
  241.      Lead2->CreateBitmap(Lead1->FloaterWidth,Lead1->FloaterHeight,Lead1->BitmapBits);
  242.      Lead2->Combine(0.0,0.0,Lead1->FloaterWidth,Lead1->FloaterHeight,Lead1->Bitmap,
  243.                    Lead1->RgnLeft,Lead1->RgnTop,CB_DST_0 | CB_OP_OR);
  244.      Lead1->Combine(Lead1->RgnLeft,Lead1->RgnTop,Lead1->FloaterWidth,Lead1->FloaterHeight,
  245.                     Lead1->Floater,0.0,0.0,CB_DST_0 | CB_OP_OR);
  246.      Lead1->Floater=0;
  247.      Lead1->FloaterVisible=false;
  248.      Lead1->RepaintRect(Lead1->RgnLeft * Lead1->DstWidth / Lead1->SrcWidth - Lead1->DstLeft,
  249.                        Lead1->RgnTop * Lead1->DstHeight / Lead1->SrcHeight + Lead1->DstTop,
  250.                        Lead1->RgnWidth * Lead1->DstWidth / Lead1->SrcWidth,
  251.                        Lead1->RgnHeight * Lead1->DstHeight / Lead1->SrcHeight,
  252.                        false);
  253.      Lead1->RgnFrameType = RGNFRAME_ANIMATED;
  254.      Lead1->DeleteRgnHandle(hRegion);
  255.    }
  256. }
  257. //---------------------------------------------------------------------------
  258.  
  259. void TMDIChild::SaveBackground()
  260. {
  261.    Lead2->CreateBitmap(Lead1->RgnWidth,Lead1->RgnHeight,Lead1->BitmapBits);
  262.    Lead2->Combine(0.0,0.0,Lead1->RgnWidth,Lead1->RgnHeight,Lead1->Bitmap,Lead1->RgnLeft,
  263.                  Lead1->RgnTop,CB_DST_0 | CB_OP_OR);
  264. }
  265.  
  266. void TMDIChild::RestoreBackground()
  267. {
  268.    if(Lead2->Bitmap!=0)
  269.     {
  270.        Lead1->Combine(Lead1->RgnLeft,Lead1->RgnTop,Lead1->RgnWidth,Lead1->RgnHeight,
  271.                       Lead2->Bitmap,0.0,0.0,CB_DST_0 | CB_OP_OR);
  272.     }
  273. }
  274.  
  275. void TMDIChild::CancelRegion()
  276. {
  277.   RestoreBackground();
  278.   Lead1->RgnFrameType=RGNFRAME_NONE;
  279.   Lead1->FreeRgn();
  280.   Lead1->Floater=0;
  281.   MainForm->CancelRegion1->Enabled=false;
  282. }
  283.  
  284. void TMDIChild::ZoomImage(int amount,bool up)
  285. {
  286.    int zoom;
  287.  
  288.    if(up)
  289.      zoom=100 * amount;
  290.    else
  291.      zoom = 100 / amount;
  292.    Lead1->PaintZoomFactor = zoom;
  293. }
  294.  
  295. void TMDIChild::ProcessRegion(String what)
  296. {
  297.   int xCenter,yCenter;
  298.   int hRegion;
  299.  
  300.   Lead1->FloaterVisible=false;
  301.   Lead1->Floater=Lead1->Bitmap;
  302.   RestoreBackground();
  303.   Lead3->Bitmap=Lead1->Floater;
  304.   Lead1->Floater=0;
  305.   Lead3->EnableProgressEvent=Lead1->EnableProgressEvent;
  306.   if(what=="Flip")
  307.     Lead3->Flip();
  308.   else if(what=="Reverse")
  309.     Lead3->Reverse();
  310.   else if(what=="Rotate")
  311.     Lead3->Rotate(rValue*100,true,clBlack);
  312.   else if(what=="Shear")
  313.     Lead3->Shear(rValue*100,true,clBlack);
  314.   else if(what=="Resize")
  315.     Lead3->Size(rValue,sValue,RESIZE_NORMAL);
  316.   else if(what=="Resample");
  317.     Lead3->Size(rValue,sValue,RESIZE_RESAMPLE);
  318.   Lead1->RgnFrameType=RGNFRAME_NONE;
  319.   hRegion=Lead3->GetRgnHandle();
  320.   xCenter=Lead1->RgnLeft + Lead1->RgnWidth / 2;
  321.   yCenter=Lead1->RgnTop + Lead1->RgnHeight / 2;
  322.   Lead1->SetRgnHandle(hRegion,xCenter-Lead3->BitmapWidth/2,yCenter-Lead3->BitmapWidth/2,
  323.                       L_RGN_SET);
  324.   Lead1->DeleteRgnHandle(hRegion);
  325.   SaveBackground();
  326.   Lead1->Combine(xCenter-Lead3->BitmapWidth/2,yCenter-Lead3->BitmapHeight/2,
  327.                  Lead3->BitmapWidth,Lead3->BitmapHeight,Lead3->Bitmap,0.0,0.0,
  328.                  CB_DST_0 | CB_OP_OR);
  329.   Lead1->RgnFrameType=RGNFRAME_ANIMATED;
  330.   Lead1->RepaintRect(Lead1->RgnLeft*Lead1->DstWidth/Lead1->SrcWidth+Lead1->DstLeft,
  331.                      Lead1->RgnTop*Lead1->DstWidth/Lead1->SrcWidth+Lead1->DstTop,
  332.                      Lead1->RgnWidth*Lead1->DstWidth/Lead1->SrcWidth,
  333.                      Lead1->RgnHeight*Lead1->DstWidth/Lead1->SrcWidth,
  334.                      false);
  335.   Lead3->Bitmap=0;
  336. }
  337.