home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / BCBViewer / Main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-09  |  11.4 KB  |  376 lines

  1. //
  2. // Copyright (c) 1998 Colosseum Builders, Inc.
  3. // All rights reserved.
  4. //
  5. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  6. // with regards to this software. It is provided as is.
  7. //
  8. // Permission to use, redistribute, and copy this file is granted
  9. // without a fee so long as as the following conditions are adhered to:
  10. //
  11. // o The user assumes all risk for using this software. The authors of this
  12. //   software shall be liable for no damages of any kind.
  13. //
  14. // o If the source code is distributed then this copyright notice must
  15. //   remain unaltered and any modification must be noted.
  16. //
  17. // o If this code is shipped in binary format the accompanying documentation
  18. //   should state that "this software is based, in part, on the work of
  19. //   Colosseum Builders, Inc."
  20. //
  21.  
  22. //
  23. //  Title:  Sample Image Viewer/Format Conversion Application
  24. //
  25. //  Author:  John M. Miano miano@colosseumbuilders.com
  26. //
  27. //---------------------------------------------------------------------
  28. #include <vcl.h>
  29. #include <shellapi.h>
  30. #include <clipbrd.hpp>
  31. #include <fstream>
  32. #pragma hdrstop
  33.  
  34. using namespace std ;
  35.  
  36. #include "Main.h"
  37. #include "About.h"
  38. #include "JPEGConfig.h"
  39. #include "gamma.h"
  40. #include "grexcept.h"
  41. #include "PngConfig.h"
  42. //---------------------------------------------------------------------
  43. #pragma resource "*.dfm"
  44. TMainForm *MainForm;
  45.  
  46. static void ProgressFunction (BitmapImageCoder &coder,
  47.                               void *data,
  48.                               unsigned int currentpass,
  49.                               unsigned int passcount,
  50.                               unsigned int progress,
  51.                               bool &cancel)
  52. {
  53.   MainForm->ProgressBar->Position = (100 * (currentpass - 1) + progress)
  54.                                / passcount ;
  55.   Application->ProcessMessages () ;
  56.   return ;
  57. }
  58.  
  59. //---------------------------------------------------------------------
  60. __fastcall TMainForm::TMainForm(TComponent *Owner)
  61.     : TForm(Owner)
  62. {
  63.   show_progressive = false ;
  64.   return ;
  65. }
  66. //---------------------------------------------------------------------
  67. void __fastcall TMainForm::FormCreate(TObject *Sender)
  68. {
  69.     Application->OnHint = ShowHint;
  70.     Screen->OnActiveFormChange = UpdateMenuItems;
  71.  
  72.   // Allow the user to drop files.
  73.   DragAcceptFiles (Handle, true) ;
  74.  
  75.   // Here we remove the shading along the bottom and right from the
  76.   // progress bar.
  77.   LONG style = GetWindowLong (ProgressBar->Handle, GWL_EXSTYLE) ;
  78.   style &= ~WS_EX_STATICEDGE ;
  79.   SetWindowLong (ProgressBar->Handle, GWL_EXSTYLE, style) ;
  80.  
  81.   return ;
  82. }
  83. //---------------------------------------------------------------------
  84. void __fastcall TMainForm::ShowHint(TObject *Sender)
  85. {
  86.     StatusBar->SimpleText = Application->Hint;
  87. }
  88. //---------------------------------------------------------------------
  89. void __fastcall TMainForm::CreateMDIChild(String Name)
  90. {
  91.   TMDIChild *Child;
  92.  
  93.   //--- create a new MDI child window ----
  94.   Child = new TMDIChild(Application);
  95.   try
  96.   {
  97.     Child->ReadImage (Name) ;
  98.   }
  99.   catch (...)
  100.   {
  101.     delete Child ;
  102.     throw ;
  103.   }
  104.   if (Child->GetImageType () == UnknownImage)
  105.   {
  106.     delete Child ;
  107.     throw Exception ("Unknown or Invalid Image Format") ;
  108.   }
  109.   return ;
  110. }
  111. //---------------------------------------------------------------------
  112. void __fastcall TMainForm::FileNewItemClick(TObject *Sender)
  113. {
  114.     CreateMDIChild("NONAME" + IntToStr(MDIChildCount + 1));
  115. }
  116. //---------------------------------------------------------------------
  117. void __fastcall TMainForm::FileOpenItemClick(TObject *Sender)
  118. {
  119.     if (OpenDialog->Execute())
  120.         CreateMDIChild(OpenDialog->FileName);
  121. }
  122. //--------------------------------------------------------------------- 
  123. void __fastcall TMainForm::FileCloseItemClick(TObject *Sender)
  124. {
  125.     if (ActiveMDIChild)
  126.         ActiveMDIChild->Close();
  127. }
  128. //--------------------------------------------------------------------- 
  129. void __fastcall TMainForm::FileSaveItemClick(TObject *Sender)
  130. {
  131.     //---- save current file (ActiveMDIChild points to the window) ----
  132. }
  133. //---------------------------------------------------------------------
  134. void __fastcall TMainForm::FileSaveAsItemClick(TObject *Sender)
  135. {
  136.   TMDIChild *child = dynamic_cast<TMDIChild *>(ActiveMDIChild) ;
  137.   if (child == NULL)
  138.     return ;
  139.  
  140.     //---- save current file under new name ----
  141.   String filename = child->Caption ;
  142.   String ext = ExtractFileExt (filename) ;
  143.   filename = filename.SubString (1, filename.Length () - ext.Length ()) ;
  144.   SaveDialog->FileName = filename ;
  145.   if (!SaveDialog->Execute ())
  146.     return ;
  147.  
  148.   filename = SaveDialog->FileName ;
  149.   ext = ExtractFileExt (filename) ;
  150.   if (ext == "")
  151.   {
  152.     switch (SaveDialog->FilterIndex)
  153.     {
  154.     case 1: filename += ".JPG" ; break ;
  155.     case 2: filename += ".PNG" ; break ;
  156.     case 3: filename += ".BMP" ; break ;
  157.     }
  158.   }
  159.   ofstream strm (filename.c_str (), ios::binary|ios::trunc) ;
  160.   if (! strm)
  161.     throw Exception (String ("Can't open file ") + SaveDialog->FileName) ;
  162.  
  163.   MainForm->ShowProgressBar () ;
  164.   Cursor = crHourGlass ;
  165.   BitmapImageEncoder *encoder ;
  166.   switch (SaveDialog->FilterIndex)
  167.   {
  168.   case 1:
  169.     encoder = &jpeg_encoder ;
  170.     break ;
  171.   case 2:
  172.     encoder = &png_encoder ;
  173.     break ;
  174.   case 3:
  175.     encoder = &bmp_encoder ;
  176.     break ;
  177.   }
  178.   encoder->SetProgressFunction (ProgressFunction, NULL) ;
  179.   try
  180.   {
  181.     encoder->WriteImage (strm, child->GetImage ()) ;
  182.   }
  183.   catch (EGraphicsException &ee)
  184.   {
  185.     ProgressBar->Visible = false ;
  186.     Cursor = crDefault ;
  187.     throw Exception (ee.what ()) ;
  188.   }
  189.   ProgressBar->Visible = false ;
  190.   Cursor = crDefault ;
  191.   child->Caption = filename ;
  192.   return ;
  193. }
  194. //---------------------------------------------------------------------
  195. void __fastcall TMainForm::FileExitItemClick(TObject *Sender)
  196. {
  197.     Close();
  198. }
  199. //--------------------------------------------------------------------- 
  200. void __fastcall TMainForm::CutItemClick(TObject *Sender)
  201. {
  202.     //---- cut selection to clipboard ----
  203. }
  204. //---------------------------------------------------------------------
  205. void __fastcall TMainForm::CopyItemClick(TObject *Sender)
  206. {
  207.     //---- copy selection to clipboard ----
  208.   TMDIChild *child = dynamic_cast<TMDIChild *>(ActiveMDIChild) ;
  209.   if (child == NULL)
  210.     return ;
  211.  
  212.   child->CopyToClipboard () ;
  213.   return ;
  214. }
  215. //---------------------------------------------------------------------
  216. void __fastcall TMainForm::PasteItemClick(TObject *Sender)
  217. {
  218.     //---- paste from clipboard ----
  219.   TMDIChild *Child;
  220.  
  221.   //--- create a new MDI child window ----
  222.   Child = new TMDIChild(Application);
  223.   try
  224.   {
  225.     Child->CopyFromClipboard () ;
  226.   }
  227.   catch (...)
  228.   {
  229.     delete Child ;
  230.     throw ;
  231.   }
  232.   if (Child->GetImageType () == UnknownImage)
  233.   {
  234.     delete Child ;
  235.     throw Exception ("Unknown or Invalid Image Format") ;
  236.   }
  237.   return ;
  238. }
  239. //---------------------------------------------------------------------
  240. void __fastcall TMainForm::WindowCascadeItemClick(TObject *Sender)
  241. {
  242.     Cascade();
  243. }
  244. //--------------------------------------------------------------------- 
  245. void __fastcall TMainForm::WindowTileItemClick(TObject *Sender)
  246. {
  247.     Tile();
  248. }
  249. //---------------------------------------------------------------------
  250. void __fastcall TMainForm::WindowArrangeItemClick(TObject *Sender)
  251. {
  252.     ArrangeIcons();
  253. }
  254. //---------------------------------------------------------------------
  255. void __fastcall TMainForm::WindowMinimizeItemClick(TObject *Sender)
  256. {
  257.     int i;
  258.  
  259.     //---- Must be done backwards through the MDIChildren array ----
  260.     for (i=MDIChildCount-1; i >= 0; i--)
  261.         MDIChildren[i]->WindowState = wsMinimized;
  262. }
  263. //---------------------------------------------------------------------
  264. void __fastcall TMainForm::UpdateMenuItems(TObject *Sender)
  265. {
  266.     FileCloseItem->Enabled = MDIChildCount > 0;
  267.     FileSaveAsItem->Enabled = MDIChildCount > 0;
  268.     CopyItem->Enabled = MDIChildCount > 0;
  269.     PasteItem->Enabled = Clipboard ()->HasFormat (CF_DIB) ;
  270.     SaveBtn->Enabled = MDIChildCount > 0;
  271.     CopyBtn->Enabled = MDIChildCount > 0;
  272.     PasteBtn->Enabled = Clipboard ()->HasFormat (CF_DIB) ;
  273.     WindowCascadeItem->Enabled = MDIChildCount > 0;
  274.     WindowTileItem->Enabled = MDIChildCount > 0;
  275.     WindowArrangeItem->Enabled = MDIChildCount > 0;
  276.     WindowMinimizeItem->Enabled = MDIChildCount > 0;
  277.   GammaItem->Enabled = MDIChildCount > 0 ;
  278.   GrayscaleItem->Enabled = MDIChildCount > 0 ;
  279.   return ;
  280. }
  281. //---------------------------------------------------------------------
  282. void __fastcall TMainForm::FormDestroy(TObject *Sender)
  283. {
  284.     Screen->OnActiveFormChange = NULL;
  285. }
  286. //---------------------------------------------------------------------------
  287. void __fastcall TMainForm::HelpAboutItemClick(TObject *Sender)
  288. {
  289.     AboutBox->ShowModal();
  290. }
  291. //---------------------------------------------------------------------------
  292. void __fastcall TMainForm::WMDropFiles (TMessage &msg)
  293. {
  294.   HANDLE hdrop = (HANDLE) msg.WParam ;
  295.   char buffer [FILENAME_MAX] ;
  296.   UINT count = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0) ;
  297.   for (unsigned int ii = 0 ; ii < count ; ++ ii)
  298.   {
  299.     DragQueryFile (hdrop, ii, buffer, FILENAME_MAX) ;
  300.     CreateMDIChild (buffer) ;
  301.   }
  302.   DragFinish (hdrop) ;
  303.   return ;
  304. }
  305.  
  306. void __fastcall TMainForm::Options1Click(TObject *Sender)
  307. {
  308.   ShowProgressive1->Checked = ShowProgressive ;
  309.   return ;
  310. }
  311. //---------------------------------------------------------------------------
  312. void __fastcall TMainForm::ShowProgressive1Click(TObject *Sender)
  313. {
  314.   show_progressive = ! show_progressive ;
  315.   ShowProgressive1->Checked = show_progressive ;
  316.   return ;
  317. }
  318. //---------------------------------------------------------------------------
  319. void __fastcall TMainForm::GammaItemClick(TObject *Sender)
  320. {
  321.   TMDIChild *child = dynamic_cast<TMDIChild *>(ActiveMDIChild) ;
  322.   if (child == NULL)
  323.     return ;
  324.   GammaDlg->GammaValue = child->GammaValue ;
  325.   GammaDlg->ShowModal () ;
  326.   if (GammaDlg->ModalResult == mrOk)
  327.     child->GammaValue = GammaDlg->GammaValue ;
  328.   return ;
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TMainForm::GrayscaleItemClick(TObject *Sender)
  332. {
  333.   TMDIChild *child = dynamic_cast<TMDIChild *>(ActiveMDIChild) ;
  334.   if (child == NULL)
  335.     return ;
  336.   child->Grayscale() ;
  337.   return ;
  338. }
  339. //---------------------------------------------------------------------------
  340.  
  341. void __fastcall TMainForm::JPEGOptionsItemClick(TObject *Sender)
  342. {
  343.   try
  344.   {
  345.     JPEGOptions->ShowModal (jpeg_encoder) ;
  346.   }
  347.   catch (EGraphicsException &ee)
  348.   {
  349.     Application->MessageBox ((char *)ee.what (), (char*) "Image Viewer", 0) ;
  350.   }
  351.   return ;
  352. }
  353. //---------------------------------------------------------------------------
  354. void TMainForm::ShowProgressBar ()
  355. {
  356.   // We use this function to dislay the progress bar.
  357.   ProgressBar->Parent = MainForm->StatusBar ;
  358.   ProgressBar->Left = MainForm->StatusBar->ClientRect.Left ;
  359.   ProgressBar->Top = MainForm->StatusBar->ClientRect.Top ;
  360.   ProgressBar->Width = MainForm->StatusBar->ClientRect.Right
  361.                      - MainForm->StatusBar->ClientRect.Left ;
  362.   ProgressBar->Height = MainForm->StatusBar->ClientRect.Bottom
  363.                       - MainForm->StatusBar->ClientRect.Top ;
  364.   ProgressBar->Visible = true ;
  365.   return ;
  366. }
  367.  
  368. void __fastcall TMainForm::PNGOptions1Click(TObject *Sender)
  369. {
  370.   PngConfiguration->ShowModal (png_encoder) ;
  371.   return ;
  372. }
  373. //---------------------------------------------------------------------------
  374.  
  375.  
  376.