#pragma once


namespace Bildbetrachter {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
   using namespace System::Drawing::Imaging;

	/// <summary>
	/// Zusammenfassung für Form1
	///
	/// Warnung: Wenn Sie den Namen dieser Klasse ändern, müssen Sie auch
	///          die Ressourcendateiname-Eigenschaft für das Tool zur Kompilierung verwalteter Ressourcen ändern,
	///          das allen RESX-Dateien zugewiesen ist, von denen diese Klasse abhängt.
	///          Anderenfalls können die Designer nicht korrekt mit den lokalisierten Ressourcen
	///          arbeiten, die diesem Formular zugewiesen sind.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
   private:
      OpenFileDialog^ openFileDialog1;
      SaveFileDialog^ saveFileDialog1;

	public:
		Form1(void)
		{
			InitializeComponent();

			ResizeRedraw = true;
         
			hScrollBar1->Parent = pictureBox1;
			vScrollBar1->Parent = pictureBox1;
			hScrollBar1->Visible = false;
			vScrollBar1->Visible = false;

			openFileDialog1 = gcnew OpenFileDialog();
			openFileDialog1->InitialDirectory = ".";
			openFileDialog1->Filter = "Windows Bitmap (*.bmp) | *.bmp"
									+ "| JPEG (*.jpg) | *.jpg"
									+ "| PNG (*.png) | *.png"
									+ "| Grafikdatei (*.bmp;*.jpg;*.png) "
													+ "| *.bmp;*.jpg;*.png";
			openFileDialog1->FilterIndex = 5;
			openFileDialog1->Multiselect = false;
			openFileDialog1->RestoreDirectory = true;
         
			saveFileDialog1 = gcnew SaveFileDialog();
			saveFileDialog1->InitialDirectory = ".";
			saveFileDialog1->Filter = "Windows Bitmap (*.bmp) | *.bmp"
									+ "| JPEG (*.jpg) | *.jpg"
									+ "| PNG (*.png) | *.png";
			saveFileDialog1->FilterIndex = 1;
			saveFileDialog1->RestoreDirectory = true;


			// Zwischenablage-Unterstützung
			ausschneidenToolStripMenuItem->Enabled = false;
			kopierenToolStripMenuItem->Enabled = false;

			Timer ^clipboardTimer = gcnew Timer();
			clipboardTimer->Interval = 200;
			clipboardTimer->Tick += gcnew EventHandler(this, &Form1::timer1_Tick);
			clipboardTimer->Start();
      }

	protected:
		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
   private: System::Windows::Forms::MenuStrip^  menuStrip1;
   private: System::Windows::Forms::ToolStripMenuItem^  dateiToolStripMenuItem;
   private: System::Windows::Forms::ToolStripMenuItem^  oeffnenToolStripMenuItem;
   private: System::Windows::Forms::ToolStripMenuItem^  speichernToolStripMenuItem;
   private: System::Windows::Forms::ToolStripSeparator^  toolStripMenuItem1;
   private: System::Windows::Forms::ToolStripMenuItem^  beendenToolStripMenuItem;
   private: System::Windows::Forms::PictureBox^  pictureBox1;
   private: System::Windows::Forms::HScrollBar^  hScrollBar1;
   private: System::Windows::Forms::VScrollBar^  vScrollBar1;
   private: System::Windows::Forms::ToolStripMenuItem^  bearbeitenToolStripMenuItem;
   private: System::Windows::Forms::ToolStripMenuItem^  ausschneidenToolStripMenuItem;
   private: System::Windows::Forms::ToolStripMenuItem^  kopierenToolStripMenuItem;
   private: System::Windows::Forms::ToolStripMenuItem^  einfuegenToolStripMenuItem;


	private:
		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Erforderliche Methode für die Designerunterstützung.
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
		/// </summary>
		void InitializeComponent(void)
		{
         this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
         this->dateiToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->oeffnenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->speichernToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->toolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripSeparator());
         this->beendenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->bearbeitenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->ausschneidenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->kopierenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->einfuegenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
         this->hScrollBar1 = (gcnew System::Windows::Forms::HScrollBar());
         this->vScrollBar1 = (gcnew System::Windows::Forms::VScrollBar());
         this->menuStrip1->SuspendLayout();
         (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
         this->SuspendLayout();
         // 
         // menuStrip1
         // 
         this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->dateiToolStripMenuItem, 
         this->bearbeitenToolStripMenuItem});
         this->menuStrip1->Location = System::Drawing::Point(0, 0);
         this->menuStrip1->Name = L"menuStrip1";
         this->menuStrip1->Size = System::Drawing::Size(442, 24);
         this->menuStrip1->TabIndex = 0;
         this->menuStrip1->Text = L"menuStrip1";
         // 
         // dateiToolStripMenuItem
         // 
         this->dateiToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(4) {this->oeffnenToolStripMenuItem, 
         this->speichernToolStripMenuItem, this->toolStripMenuItem1, this->beendenToolStripMenuItem});
         this->dateiToolStripMenuItem->Name = L"dateiToolStripMenuItem";
         this->dateiToolStripMenuItem->Size = System::Drawing::Size(46, 20);
         this->dateiToolStripMenuItem->Text = L"&Datei";
         // 
         // oeffnenToolStripMenuItem
         // 
         this->oeffnenToolStripMenuItem->Name = L"oeffnenToolStripMenuItem";
         this->oeffnenToolStripMenuItem->Size = System::Drawing::Size(126, 22);
         this->oeffnenToolStripMenuItem->Text = L"Ö&ffnen";
         this->oeffnenToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::oeffnenToolStripMenuItem_Click);
         // 
         // speichernToolStripMenuItem
         // 
         this->speichernToolStripMenuItem->Name = L"speichernToolStripMenuItem";
         this->speichernToolStripMenuItem->Size = System::Drawing::Size(126, 22);
         this->speichernToolStripMenuItem->Text = L"&Speichern";
         this->speichernToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::speichernToolStripMenuItem_Click);
         // 
         // toolStripMenuItem1
         // 
         this->toolStripMenuItem1->Name = L"toolStripMenuItem1";
         this->toolStripMenuItem1->Size = System::Drawing::Size(123, 6);
         // 
         // beendenToolStripMenuItem
         // 
         this->beendenToolStripMenuItem->Name = L"beendenToolStripMenuItem";
         this->beendenToolStripMenuItem->Size = System::Drawing::Size(126, 22);
         this->beendenToolStripMenuItem->Text = L"&Beenden";
         this->beendenToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::beendenToolStripMenuItem_Click);
         // 
         // bearbeitenToolStripMenuItem
         // 
         this->bearbeitenToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->ausschneidenToolStripMenuItem, 
            this->kopierenToolStripMenuItem, this->einfuegenToolStripMenuItem});
         this->bearbeitenToolStripMenuItem->Name = L"bearbeitenToolStripMenuItem";
         this->bearbeitenToolStripMenuItem->Size = System::Drawing::Size(75, 20);
         this->bearbeitenToolStripMenuItem->Text = L"&Bearbeiten";
         // 
         // ausschneidenToolStripMenuItem
         // 
         this->ausschneidenToolStripMenuItem->Name = L"ausschneidenToolStripMenuItem";
         this->ausschneidenToolStripMenuItem->ShortcutKeys = static_cast<System::Windows::Forms::Keys>((System::Windows::Forms::Keys::Control | System::Windows::Forms::Keys::X));
         this->ausschneidenToolStripMenuItem->Size = System::Drawing::Size(191, 22);
         this->ausschneidenToolStripMenuItem->Text = L"&Ausschneiden";
         this->ausschneidenToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::ausschneidenToolStripMenuItem_Click);
         // 
         // kopierenToolStripMenuItem
         // 
         this->kopierenToolStripMenuItem->Name = L"kopierenToolStripMenuItem";
         this->kopierenToolStripMenuItem->ShortcutKeys = static_cast<System::Windows::Forms::Keys>((System::Windows::Forms::Keys::Control | System::Windows::Forms::Keys::C));
         this->kopierenToolStripMenuItem->Size = System::Drawing::Size(191, 22);
         this->kopierenToolStripMenuItem->Text = L"&Kopieren";
         this->kopierenToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::kopierenToolStripMenuItem_Click);
         // 
         // einfuegenToolStripMenuItem
         // 
         this->einfuegenToolStripMenuItem->Name = L"einfuegenToolStripMenuItem";
         this->einfuegenToolStripMenuItem->ShortcutKeys = static_cast<System::Windows::Forms::Keys>((System::Windows::Forms::Keys::Control | System::Windows::Forms::Keys::V));
         this->einfuegenToolStripMenuItem->Size = System::Drawing::Size(191, 22);
         this->einfuegenToolStripMenuItem->Text = L"&Einfuegen";
         this->einfuegenToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::einfuegenToolStripMenuItem_Click);
         // 
         // pictureBox1
         // 
         this->pictureBox1->Dock = System::Windows::Forms::DockStyle::Fill;
         this->pictureBox1->Location = System::Drawing::Point(0, 24);
         this->pictureBox1->Name = L"pictureBox1";
         this->pictureBox1->Size = System::Drawing::Size(442, 225);
         this->pictureBox1->TabIndex = 2;
         this->pictureBox1->TabStop = false;
         this->pictureBox1->Resize += gcnew System::EventHandler(this, &Form1::pictureBox1_Resize);
         this->pictureBox1->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::pictureBox1_Paint);
         // 
         // hScrollBar1
         // 
         this->hScrollBar1->Dock = System::Windows::Forms::DockStyle::Bottom;
         this->hScrollBar1->Location = System::Drawing::Point(0, 232);
         this->hScrollBar1->Name = L"hScrollBar1";
         this->hScrollBar1->Size = System::Drawing::Size(442, 17);
         this->hScrollBar1->TabIndex = 3;
         this->hScrollBar1->Scroll += gcnew System::Windows::Forms::ScrollEventHandler(this, &Form1::hScrollBar1_Scroll);
         // 
         // vScrollBar1
         // 
         this->vScrollBar1->Dock = System::Windows::Forms::DockStyle::Right;
         this->vScrollBar1->Location = System::Drawing::Point(425, 24);
         this->vScrollBar1->Name = L"vScrollBar1";
         this->vScrollBar1->Size = System::Drawing::Size(17, 208);
         this->vScrollBar1->TabIndex = 4;
         this->vScrollBar1->Scroll += gcnew System::Windows::Forms::ScrollEventHandler(this, &Form1::vScrollBar1_Scroll);
         // 
         // Form1
         // 
         this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
         this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
         this->ClientSize = System::Drawing::Size(442, 249);
         this->Controls->Add(this->pictureBox1);
         this->Controls->Add(this->menuStrip1);
         this->MainMenuStrip = this->menuStrip1;
         this->Name = L"Form1";
         this->Text = L"Bildbetrachter";
         this->menuStrip1->ResumeLayout(false);
         this->menuStrip1->PerformLayout();
         (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
         this->ResumeLayout(false);
         this->PerformLayout();

      }
#pragma endregion
   private: 

#pragma region Dateibehandlung und Bildlaufleisten

      void oeffnenToolStripMenuItem_Click(Object^  sender, EventArgs^  e) 
      {
         if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
         {
             // Bild laden
             Image^ img = Image::FromFile(openFileDialog1->FileName);
             
             // Rendering des Bildes anstoßen
             pictureBox1->Image = img;
                 
             // Bildlaufleisten einstellen
             setScrollBars();   

             // Zwischenablage-Befehle
            ausschneidenToolStripMenuItem->Enabled = true;
            kopierenToolStripMenuItem->Enabled = true;
         }
      }
      void speichernToolStripMenuItem_Click(Object^  sender, EventArgs^  e) 
      {
            if (pictureBox1->Image == nullptr)
                return;
               
            Image^ img = pictureBox1->Image;

            // voreingestellten Dateityp an Bildformat anpassen
            if (img->RawFormat->Equals(ImageFormat::Bmp))
                saveFileDialog1->FilterIndex = 1;    
            else if (img->RawFormat->Equals(ImageFormat::Jpeg))
                saveFileDialog1->FilterIndex = 2;
            else 
                saveFileDialog1->FilterIndex = 3;    
                
                
            if (saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                if (saveFileDialog1->FileName == openFileDialog1->FileName)
                {
                   MessageBox::Show("Zurückschreiben in die alte Datei wird nicht unterstützt");
                   return;
                }
               
                switch (saveFileDialog1->FilterIndex)
                {
                case 1 : img->Save(saveFileDialog1->FileName, ImageFormat::Bmp);    
                         break;
                case 2 : img->Save(saveFileDialog1->FileName, ImageFormat::Jpeg);    
                         break;
                case 3 : img->Save(saveFileDialog1->FileName, ImageFormat::Png);    
                         break;
                }         
            }            
      }
      void beendenToolStripMenuItem_Click(Object^  sender, EventArgs^  e) 
      {
         this->Close();
      }

      void pictureBox1_Paint(Object^  sender, PaintEventArgs^  e) 
      {
         Graphics^ gc = e->Graphics;

         gc->Clear(SystemColors::ControlLight);
            
         if (pictureBox1->Image != nullptr)
         {
             // Korrektur für möglicherweise eingeblendete Bildlaufleisten
             int corrW = 0;
             int corrH = 0;
             
             if (vScrollBar1->Visible)
                 corrW = vScrollBar1->Width;
             if (hScrollBar1->Visible)
                 corrH = hScrollBar1->Height;
                 
             gc->DrawImage(pictureBox1->Image, 
                           Rectangle(0, 0, 
                                     pictureBox1->Right - corrW, 
                                     pictureBox1->Bottom - corrH), 
                           Rectangle(hScrollBar1->Value, vScrollBar1->Value, 
                                     pictureBox1->Right - corrW,
                                     pictureBox1->Bottom - corrH),
                                     GraphicsUnit::Pixel);
         }
      }
  
      void setScrollBars()
      {
         if (pictureBox1->Image == nullptr)
            return;
         
         // Bildlaufleisten einblenden, wenn die Bildbreite (-höhe) größer als die PictureBox ist
         hScrollBar1->Visible = pictureBox1->Image->Width  > pictureBox1->Width;
         vScrollBar1->Visible = pictureBox1->Image->Height > pictureBox1->Height;
         
         // nochmals prüfen und sichtbare Bildlaufleisten mit berücksichtigen
         if (vScrollBar1->Visible)
             hScrollBar1->Visible = pictureBox1->Image->Width  > (pictureBox1->Width - vScrollBar1->Width);
         if (hScrollBar1->Visible)
             vScrollBar1->Visible = pictureBox1->Image->Height > (pictureBox1->Height - hScrollBar1->Height);

         // horizontale Bildlaufleiste konfigurieren
         hScrollBar1->Value = 0;
         hScrollBar1->Minimum = 0;
         hScrollBar1->Maximum = pictureBox1->Image->Width - 1;
         hScrollBar1->SmallChange = 1;
         // LargeChange auf Anzeigebreite setzen
         if (hScrollBar1->Visible)
         {
             if (vScrollBar1->Visible)
                 hScrollBar1->LargeChange = pictureBox1->Width - vScrollBar1->Width;
             else
                 hScrollBar1->LargeChange = pictureBox1->Width;
         }            
         
         // vertikale Bildlaufleiste konfigurieren
         vScrollBar1->Value = 0;
         vScrollBar1->Minimum = 0;
         vScrollBar1->Maximum = pictureBox1->Image->Height - 1;
         vScrollBar1->SmallChange = 1;         
         // LargeChange auf Anzeigehöhe setzen
         if (vScrollBar1->Visible)
         {
             if (hScrollBar1->Visible)
                 vScrollBar1->LargeChange = pictureBox1->Height - hScrollBar1->Height;
             else
                 vScrollBar1->LargeChange = pictureBox1->Height;
         }
      }
      void vScrollBar1_Scroll(Object^  sender, ScrollEventArgs ^e)
      {
         pictureBox1->Refresh();
      }
      void hScrollBar1_Scroll(Object^  sender, ScrollEventArgs ^e)
      {
         pictureBox1->Refresh();
      }
      void pictureBox1_Resize(Object^  sender, EventArgs^  e)
      {
         // Nicht reagieren, wenn kein Bild geladen oder das Fenster minimiert wird
         if (pictureBox1->Image != nullptr && WindowState != FormWindowState::Minimized)
         {
             setScrollBars();
             pictureBox1->Refresh();
         }
      }
#pragma endregion

#pragma region Zwischenablage-Unterstützung

	  // Menü_Befehle
      void ausschneidenToolStripMenuItem_Click(Object^  sender, EventArgs^  e) 
      {
         if (pictureBox1->Image != nullptr)
         {
            Clipboard::SetImage(pictureBox1->Image);

			pictureBox1->Image = nullptr;

            /*
               Bitmap^ bitmap = dynamic_cast<Bitmap^>(pictureBox1->Image);
               if (bitmap != nullptr)
               {
                  for(int x = 0; x < bitmap->Width; x++)
                     for (int y = 0; y < bitmap->Height; y++)
                        bitmap->SetPixel(x, y, Color::White);
               }
            */
            pictureBox1->Refresh();
         }
      }
      void kopierenToolStripMenuItem_Click(Object^  sender, EventArgs^  e) 
      {
         if (pictureBox1->Image != nullptr)
         {
            Clipboard::SetImage(pictureBox1->Image);
         }
      }
      void einfuegenToolStripMenuItem_Click(Object^  sender, EventArgs^  e) 
      {
         if (Clipboard::ContainsImage() == true)
         {
            pictureBox1->Image = Clipboard::GetImage();
            setScrollBars();
            pictureBox1->Refresh();
         }
      }

      // Zwischenablage-Aktivierung
      void timer1_Tick(Object^  sender, EventArgs^  e) 
      {
          if (pictureBox1->Image != nullptr)
          {
              ausschneidenToolStripMenuItem->Enabled = true;
              kopierenToolStripMenuItem->Enabled = true;
          }
          else
          {
              ausschneidenToolStripMenuItem->Enabled = false;
              kopierenToolStripMenuItem->Enabled = false;
          }
          
          if (Clipboard::ContainsImage() == true)
          {
              einfuegenToolStripMenuItem->Enabled = true;
          }
          else
          {
              einfuegenToolStripMenuItem->Enabled = false;
          }
      }

#pragma endregion



};
}

