r632: Added Clipboard functions to image files
[ctsim.git] / src / views.cpp
index c9689bfab866a05e6b81b901f2d697b0198d2a07..41e4367fe44b675239a522f329577619f8674e76 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (c) 1983-2001 Kevin Rosenberg
 **
-**  $Id: views.cpp,v 1.133 2001/03/11 15:27:30 kevin Exp $
+**  $Id: views.cpp,v 1.134 2001/03/11 17:55:29 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
@@ -36,6 +36,7 @@
 
 #include "wx/image.h"
 #include "wx/progdlg.h"
+#include "wx/clipbrd.h"
 
 #include "ct.h"
 #include "ctsim.h"
@@ -204,6 +205,9 @@ IMPLEMENT_DYNAMIC_CLASS(ImageFileView, wxView)
 BEGIN_EVENT_TABLE(ImageFileView, wxView)
 EVT_MENU(IFMENU_FILE_EXPORT, ImageFileView::OnExport)
 EVT_MENU(IFMENU_FILE_PROPERTIES, ImageFileView::OnProperties)
+EVT_MENU(IFMENU_EDIT_COPY, ImageFileView::OnEditCopy)
+EVT_MENU(IFMENU_EDIT_CUT, ImageFileView::OnEditCut)
+EVT_MENU(IFMENU_EDIT_PASTE, ImageFileView::OnEditPaste)
 EVT_MENU(IFMENU_VIEW_SCALE_MINMAX, ImageFileView::OnScaleMinMax)
 EVT_MENU(IFMENU_VIEW_SCALE_AUTO, ImageFileView::OnScaleAuto)
 EVT_MENU(IFMENU_VIEW_SCALE_FULL, ImageFileView::OnScaleFull)
@@ -289,6 +293,7 @@ ImageFileView::OnProperties (wxCommandEvent& event)
     wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
     dialogMsg.ShowModal();
   }
+  GetDocument()->Activate();
 }
 
 void 
@@ -307,9 +312,10 @@ ImageFileView::OnScaleAuto (wxCommandEvent& event)
       m_dMinPixel = dMin;
       m_dMaxPixel = dMax;
       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
-      OnUpdate (this, NULL);
+      GetDocument()->UpdateAllViews (this);
     }
   }
+  GetDocument()->Activate();
 }
 
 void 
@@ -332,8 +338,9 @@ ImageFileView::OnScaleMinMax (wxCommandEvent& event)
     m_bMaxSpecified = true;
     m_dMinPixel = dialogMinMax.getMinimum();
     m_dMaxPixel = dialogMinMax.getMaximum();
-    OnUpdate (this, NULL);
+    GetDocument()->UpdateAllViews (this);
   }
+  GetDocument()->Activate();
 }
 
 void 
@@ -342,8 +349,9 @@ ImageFileView::OnScaleFull (wxCommandEvent& event)
   if (m_bMinSpecified || m_bMaxSpecified) {
     m_bMinSpecified = false;
     m_bMaxSpecified = false;
-    OnUpdate (this, NULL);
+    GetDocument()->UpdateAllViews (this);
   }
+  GetDocument()->Activate();
 }
 
 void
@@ -401,6 +409,7 @@ ImageFileView::OnCompare (wxCommandEvent& event)
         pDifferenceDoc->UpdateAllViews (this);
         pDifferenceDoc->getView()->OnUpdate (this, NULL);
         pDifferenceDoc->getView()->getFrame()->Show(true);
+        pDifferenceDoc->Activate();
       }
       wxMessageBox(os.str().c_str(), "Image Comparison");
     }
@@ -416,6 +425,7 @@ ImageFileView::OnInvertValues (wxCommandEvent& event)
   if (theApp->getAskDeleteNewDocs())
     GetDocument()->Modify (true);
   GetDocument()->UpdateAllViews (this);
+  GetDocument()->Activate();
 }
 
 void
@@ -427,6 +437,7 @@ ImageFileView::OnSquare (wxCommandEvent& event)
   if (theApp->getAskDeleteNewDocs())
     GetDocument()->Modify (true);
   GetDocument()->UpdateAllViews (this);
+  GetDocument()->Activate();
 }
 
 void
@@ -438,6 +449,7 @@ ImageFileView::OnSquareRoot (wxCommandEvent& event)
   if (theApp->getAskDeleteNewDocs())
     GetDocument()->Modify (true);
   GetDocument()->UpdateAllViews (this);
+  GetDocument()->Activate();
 }
 
 void
@@ -449,6 +461,7 @@ ImageFileView::OnLog (wxCommandEvent& event)
   if (theApp->getAskDeleteNewDocs())
     GetDocument()->Modify (true);
   GetDocument()->UpdateAllViews (this);
+  GetDocument()->Activate();
 }
 
 void
@@ -460,6 +473,7 @@ ImageFileView::OnExp (wxCommandEvent& event)
   if (theApp->getAskDeleteNewDocs())
     GetDocument()->Modify (true);
   GetDocument()->UpdateAllViews (this);
+  GetDocument()->Activate();
 }
 
 void
@@ -497,8 +511,8 @@ ImageFileView::OnAdd (wxCommandEvent& event)
       if (theApp->getAskDeleteNewDocs())
         pNewDoc->Modify (true);
       pNewDoc->UpdateAllViews (this);
-      pNewDoc->getView()->OnUpdate (this, NULL);
       pNewDoc->getView()->getFrame()->Show(true);
+      pNewDoc->Activate();
     }
   }
 }
@@ -538,8 +552,8 @@ ImageFileView::OnSubtract (wxCommandEvent& event)
       if (theApp->getAskDeleteNewDocs())
         pNewDoc->Modify (true);
       pNewDoc->UpdateAllViews (this);
-      pNewDoc->getView()->OnUpdate (this, NULL);
       pNewDoc->getView()->getFrame()->Show(true);
+      pNewDoc->Activate();
     }
   }
 }
@@ -579,8 +593,8 @@ ImageFileView::OnMultiply (wxCommandEvent& event)
       if (theApp->getAskDeleteNewDocs())
         pNewDoc->Modify (true);
       pNewDoc->UpdateAllViews (this);
-      pNewDoc->getView()->OnUpdate (this, NULL);
       pNewDoc->getView()->getFrame()->Show(true);
+      pNewDoc->Activate();
     }
   }
 }
@@ -620,8 +634,8 @@ ImageFileView::OnDivide (wxCommandEvent& event)
       if (theApp->getAskDeleteNewDocs())
         pNewDoc->Modify (true);
       pNewDoc->UpdateAllViews (this);
-      pNewDoc->getView()->OnUpdate (this, NULL);
       pNewDoc->getView()->getFrame()->Show(true);
+      pNewDoc->Activate();
     }
   }
 }
@@ -850,6 +864,11 @@ ImageFileView::CreateChildFrame(wxDocument *doc, wxView *view)
   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
   
+  wxMenu* edit_menu = new wxMenu;
+  edit_menu->Append(IFMENU_EDIT_COPY, "Copy\tCtrl-C");
+  edit_menu->Append(IFMENU_EDIT_CUT, "Cut\tCtrl-X");
+  edit_menu->Append(IFMENU_EDIT_PASTE, "Paste\tCtrl-V");
+
   wxMenu *view_menu = new wxMenu;
   view_menu->Append(IFMENU_VIEW_SCALE_MINMAX, "Display Scale S&et...\tCtrl-E");
   view_menu->Append(IFMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...\tCtrl-A");
@@ -918,6 +937,7 @@ ImageFileView::CreateChildFrame(wxDocument *doc, wxView *view)
   wxMenuBar *menu_bar = new wxMenuBar;
   
   menu_bar->Append(m_pFileMenu, "&File");
+  menu_bar->Append(edit_menu, "&Edit");
   menu_bar->Append(view_menu, "&View");
   menu_bar->Append(image_menu, "&Image");
   menu_bar->Append(filter_menu, "Fi&lter");
@@ -928,16 +948,19 @@ ImageFileView::CreateChildFrame(wxDocument *doc, wxView *view)
   
   subframe->Centre(wxBOTH);
   
-  wxAcceleratorEntry accelEntries[5];
+  wxAcceleratorEntry accelEntries[8];
   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('A'), IFMENU_VIEW_SCALE_AUTO);
   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('U'), IFMENU_VIEW_SCALE_FULL);
   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('E'), IFMENU_VIEW_SCALE_MINMAX);
   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('I'), IFMENU_FILE_PROPERTIES);
+  accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('C'), IFMENU_EDIT_COPY);
+  accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('X'), IFMENU_EDIT_CUT);
+  accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('V'), IFMENU_EDIT_PASTE);
 #if wxUSE_GLCANVAS
-  accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('3'), IFMENU_IMAGE_CONVERT3D);
-  wxAcceleratorTable accelTable (5, accelEntries);
+  accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('3'), IFMENU_IMAGE_CONVERT3D);
+  wxAcceleratorTable accelTable (8, accelEntries);
 #else
-  wxAcceleratorTable accelTable (4, accelEntries);
+  wxAcceleratorTable accelTable (7, accelEntries);
 #endif
   
   subframe->SetAcceleratorTable (accelTable);
@@ -1062,6 +1085,72 @@ ImageFileView::OnClose (bool deleteWindow)
   return true;
 }
 
+void
+ImageFileView::OnEditCopy (wxCommandEvent& event)
+{
+  wxBitmapDataObject *pBitmapObject = new wxBitmapDataObject;
+
+  pBitmapObject->SetBitmap (m_bitmap);
+
+  if (wxTheClipboard->Open()) {
+    wxTheClipboard->SetData (pBitmapObject);
+    wxTheClipboard->Close();
+  }
+}
+
+void
+ImageFileView::OnEditCut (wxCommandEvent& event)
+{
+  OnEditCopy (event);
+  ImageFile& rIF = GetDocument()->getImageFile();
+  int nx = rIF.nx();
+  int ny = rIF.ny();
+  ImageFile* pIF = new ImageFile (nx, ny);
+  pIF->arrayDataClear();
+  GetDocument()->setImageFile (pIF); // deletes old IF
+  OnUpdate(this, NULL);
+  GetDocument()->UpdateAllViews();
+  if (theApp->getAskDeleteNewDocs())
+    GetDocument()->Modify (true);
+}
+
+void
+ImageFileView::OnEditPaste (wxCommandEvent& event)
+{
+  ImageFile& rIF = GetDocument()->getImageFile();
+
+  if (wxTheClipboard->Open()) {
+    wxBitmap bitmap;
+    if (wxTheClipboard->IsSupported (wxDF_BITMAP)) {
+      wxBitmapDataObject bitmapObject;
+      wxTheClipboard->GetData (bitmapObject);
+      bitmap = bitmapObject.GetBitmap ();
+    }
+    wxTheClipboard->Close();
+
+    int nx = rIF.nx();
+    int ny = rIF.ny();
+    if (bitmap.Ok() == true && bitmap.GetWidth() == nx && bitmap.GetHeight() == ny) {
+      wxImage image (bitmap);
+      unsigned char* pixels = image.GetData();
+      ImageFileArray v = rIF.getArray();
+      for (int ix = 0; ix < rIF.nx(); ix++) {
+        for (int iy = 0; iy < rIF.ny(); iy++) {
+          unsigned int iBase = 3 * (iy * nx + ix);
+          double dR = pixels[iBase] / 255.;
+          double dG = pixels[iBase+1] / 255.;
+          double dB = pixels[iBase+2] / 255.;
+          v[ix][ny - 1 - iy] = ImageFile::colorToGrayscale (dR, dG, dB);
+        }
+      }
+      OnUpdate(this, NULL);
+      GetDocument()->UpdateAllViews();
+      if (theApp->getAskDeleteNewDocs())
+        GetDocument()->Modify(true);
+    }
+  }
+}
+
 void
 ImageFileView::OnExport (wxCommandEvent& event)
 {
@@ -2340,6 +2429,7 @@ ProjectionFileView::OnConvertPolar (wxCommandEvent& event)
   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
     m_iDefaultPolarInterpolation, -1);
   if (dialogPolar.ShowModal() == wxID_OK) {
+    wxProgressDialog dlgProgress (wxString("Convert Polar"), wxString("Conversion Progress"), 1, getFrameForChild(), wxPD_APP_MODAL);
     wxString strInterpolation (dialogPolar.getInterpolationName());
     m_iDefaultPolarNX = dialogPolar.getXSize();
     m_iDefaultPolarNY = dialogPolar.getYSize();
@@ -2381,6 +2471,7 @@ ProjectionFileView::OnConvertFFTPolar (wxCommandEvent& event)
   DialogGetConvertPolarParameters dialogPolar (getFrameForChild(), "Convert to FFT Polar", m_iDefaultPolarNX, m_iDefaultPolarNY,
     m_iDefaultPolarInterpolation, m_iDefaultPolarZeropad);
   if (dialogPolar.ShowModal() == wxID_OK) {
+    wxProgressDialog dlgProgress (wxString("Convert FFT Polar"), wxString("Conversion Progress"), 1, getFrameForChild(), wxPD_APP_MODAL);
     wxString strInterpolation (dialogPolar.getInterpolationName());
     m_iDefaultPolarNX = dialogPolar.getXSize();
     m_iDefaultPolarNY = dialogPolar.getYSize();