#pragma once


namespace TexteditorDemo {

	using namespace System;
	using namespace System::Windows::Forms;
	using namespace System::IO;                      // Öffnen und Speichern von Dateien
	using namespace System::Text;                    // Auswahl des Zeichensatzes
	using namespace System::Drawing;                 // Drucken
	using namespace System::Drawing::Printing;       // Drucken

	/// <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:
        String^ dateiname;                   // speichert den Dateinamen
        OpenFileDialog^ openFileDialog1;     // Öffnen-Dialog
        SaveFileDialog^ saveFileDialog1;     // Öffnen-Dialog
        PrintDocument^ printDocument1;       // Druckdokument

	public:
		Form1(void)
		{
			InitializeComponent();

         dateiname = nullptr;

         // Öffnen-Dialog erzeugen und für ersten Aufruf konfigurieren
         openFileDialog1 = gcnew OpenFileDialog();
         openFileDialog1->InitialDirectory = ".";
         openFileDialog1->Filter = "Quelltextdateien (*.cpp)|*.cpp|"
                                 + "Headerateien (*.h)|*.h|"
                                 + "Textdateien (*.txt)|*.txt";
         openFileDialog1->FilterIndex = 1;
         openFileDialog1->Multiselect = false;

         // Speichern-Dialog erzeugen und für ersten Aufruf konfigurieren
         saveFileDialog1 = gcnew SaveFileDialog();
         saveFileDialog1->InitialDirectory = ".";
         saveFileDialog1->Filter = "Quelltextdateien (*.cpp)|*.cpp|"
                                 + "Headerateien (*.h)|*.h|"
                                 + "Textdateien (*.txt)|*.txt";
         saveFileDialog1->FilterIndex = 1;

         // Druckunterstützung
         printDocument1 = gcnew PrintDocument();
         printDocument1->PrintPage += gcnew PrintPageEventHandler(this, 
                                                                  &Form1::printDocument1_PrintPage);
      }

	protected:
		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

    private: System::Windows::Forms::MenuStrip^  menue;
    private: System::Windows::Forms::ToolStripMenuItem^  miDatei;


    private: System::Windows::Forms::ToolStripMenuItem^  miDateiNeu;
    private: System::Windows::Forms::ToolStripMenuItem^  miDateiOeffnen;
    private: System::Windows::Forms::ToolStripMenuItem^  miDateiSpeichern;
    private: System::Windows::Forms::ToolStripSeparator^  toolStripMenuItem3;
    private: System::Windows::Forms::ToolStripMenuItem^  miDateiDrucken;
    private: System::Windows::Forms::ToolStripSeparator^  toolStripMenuItem1;
    private: System::Windows::Forms::ToolStripMenuItem^  miDateiBeenden;
    private: System::Windows::Forms::ToolStripMenuItem^  miBearbeiten;


    private: System::Windows::Forms::ToolStripMenuItem^  miBearbeitenAusschneiden;
    private: System::Windows::Forms::ToolStripMenuItem^  miBearbeitenKopieren;
    private: System::Windows::Forms::ToolStripMenuItem^  miBearbeitenEinfuegen;


    private: System::Windows::Forms::ContextMenuStrip^  contextmenue;
    private: System::Windows::Forms::ToolStripMenuItem^  ausschneidenToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  kopierenToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  einfuegenToolStripMenuItem;
    private: System::Windows::Forms::TextBox^  textBox1;
    private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>


#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->components = (gcnew System::ComponentModel::Container());
         this->menue = (gcnew System::Windows::Forms::MenuStrip());
         this->miDatei = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->miDateiNeu = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->miDateiOeffnen = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->miDateiSpeichern = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->toolStripMenuItem3 = (gcnew System::Windows::Forms::ToolStripSeparator());
         this->miDateiDrucken = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->toolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripSeparator());
         this->miDateiBeenden = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->miBearbeiten = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->miBearbeitenAusschneiden = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->miBearbeitenKopieren = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->miBearbeitenEinfuegen = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->contextmenue = (gcnew System::Windows::Forms::ContextMenuStrip(this->components));
         this->ausschneidenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->kopierenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->einfuegenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->textBox1 = (gcnew System::Windows::Forms::TextBox());
         this->menue->SuspendLayout();
         this->contextmenue->SuspendLayout();
         this->SuspendLayout();
         // 
         // menue
         // 
         this->menue->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->miDatei, this->miBearbeiten});
         this->menue->Location = System::Drawing::Point(0, 0);
         this->menue->Name = L"menue";
         this->menue->Size = System::Drawing::Size(532, 24);
         this->menue->TabIndex = 1;
         this->menue->Text = L"menue";
         // 
         // miDatei
         // 
         this->miDatei->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(7) {this->miDateiNeu, this->miDateiOeffnen, 
            this->miDateiSpeichern, this->toolStripMenuItem3, this->miDateiDrucken, this->toolStripMenuItem1, this->miDateiBeenden});
         this->miDatei->Name = L"miDatei";
         this->miDatei->Size = System::Drawing::Size(46, 20);
         this->miDatei->Text = L"&Datei";
         // 
         // miDateiNeu
         // 
         this->miDateiNeu->Name = L"miDateiNeu";
         this->miDateiNeu->ShortcutKeys = static_cast<System::Windows::Forms::Keys>((System::Windows::Forms::Keys::Control | System::Windows::Forms::Keys::N));
         this->miDateiNeu->Size = System::Drawing::Size(168, 22);
         this->miDateiNeu->Text = L"&Neu";
         this->miDateiNeu->Click += gcnew System::EventHandler(this, &Form1::miDateiNeu_Click);
         // 
         // miDateiOeffnen
         // 
         this->miDateiOeffnen->Name = L"miDateiOeffnen";
         this->miDateiOeffnen->ShortcutKeys = static_cast<System::Windows::Forms::Keys>((System::Windows::Forms::Keys::Control | System::Windows::Forms::Keys::O));
         this->miDateiOeffnen->Size = System::Drawing::Size(168, 22);
         this->miDateiOeffnen->Text = L"Ö&ffnen";
         this->miDateiOeffnen->Click += gcnew System::EventHandler(this, &Form1::miDateiOeffnen_Click);
         // 
         // miDateiSpeichern
         // 
         this->miDateiSpeichern->Name = L"miDateiSpeichern";
         this->miDateiSpeichern->ShortcutKeys = static_cast<System::Windows::Forms::Keys>((System::Windows::Forms::Keys::Control | System::Windows::Forms::Keys::S));
         this->miDateiSpeichern->Size = System::Drawing::Size(168, 22);
         this->miDateiSpeichern->Text = L"&Speichern";
         this->miDateiSpeichern->Click += gcnew System::EventHandler(this, &Form1::miDateiSpeichern_Click);
         // 
         // toolStripMenuItem3
         // 
         this->toolStripMenuItem3->Name = L"toolStripMenuItem3";
         this->toolStripMenuItem3->Size = System::Drawing::Size(165, 6);
         // 
         // miDateiDrucken
         // 
         this->miDateiDrucken->Name = L"miDateiDrucken";
         this->miDateiDrucken->ShortcutKeys = static_cast<System::Windows::Forms::Keys>((System::Windows::Forms::Keys::Control | System::Windows::Forms::Keys::P));
         this->miDateiDrucken->Size = System::Drawing::Size(168, 22);
         this->miDateiDrucken->Text = L"&Drucken";
         this->miDateiDrucken->Click += gcnew System::EventHandler(this, &Form1::miDateiDrucken_Click);
         // 
         // toolStripMenuItem1
         // 
         this->toolStripMenuItem1->Name = L"toolStripMenuItem1";
         this->toolStripMenuItem1->Size = System::Drawing::Size(165, 6);
         // 
         // miDateiBeenden
         // 
         this->miDateiBeenden->Name = L"miDateiBeenden";
         this->miDateiBeenden->Size = System::Drawing::Size(168, 22);
         this->miDateiBeenden->Text = L"&Beenden";
         this->miDateiBeenden->Click += gcnew System::EventHandler(this, &Form1::miDateiBeenden_Click);
         // 
         // miBearbeiten
         // 
         this->miBearbeiten->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->miBearbeitenAusschneiden, this->miBearbeitenKopieren, 
            this->miBearbeitenEinfuegen});
         this->miBearbeiten->Name = L"miBearbeiten";
         this->miBearbeiten->Size = System::Drawing::Size(75, 20);
         this->miBearbeiten->Text = L"&Bearbeiten";
         // 
         // miBearbeitenAusschneiden
         // 
         this->miBearbeitenAusschneiden->Name = L"miBearbeitenAusschneiden";
         this->miBearbeitenAusschneiden->ShortcutKeys = static_cast<System::Windows::Forms::Keys>((System::Windows::Forms::Keys::Control | System::Windows::Forms::Keys::X));
         this->miBearbeitenAusschneiden->Size = System::Drawing::Size(191, 22);
         this->miBearbeitenAusschneiden->Text = L"Auss&chneiden";
         this->miBearbeitenAusschneiden->Click += gcnew System::EventHandler(this, &Form1::miBearbeitenAusschneiden_Click);
         // 
         // miBearbeitenKopieren
         // 
         this->miBearbeitenKopieren->Name = L"miBearbeitenKopieren";
         this->miBearbeitenKopieren->ShortcutKeys = static_cast<System::Windows::Forms::Keys>((System::Windows::Forms::Keys::Control | System::Windows::Forms::Keys::C));
         this->miBearbeitenKopieren->Size = System::Drawing::Size(191, 22);
         this->miBearbeitenKopieren->Text = L"&Kopieren";
         this->miBearbeitenKopieren->Click += gcnew System::EventHandler(this, &Form1::miBearbeitenKopieren_Click);
         // 
         // miBearbeitenEinfuegen
         // 
         this->miBearbeitenEinfuegen->Name = L"miBearbeitenEinfuegen";
         this->miBearbeitenEinfuegen->ShortcutKeys = static_cast<System::Windows::Forms::Keys>((System::Windows::Forms::Keys::Control | System::Windows::Forms::Keys::V));
         this->miBearbeitenEinfuegen->Size = System::Drawing::Size(191, 22);
         this->miBearbeitenEinfuegen->Text = L"&Einfügen";
         this->miBearbeitenEinfuegen->Click += gcnew System::EventHandler(this, &Form1::miBearbeitenEinfuegen_Click);
         // 
         // contextmenue
         // 
         this->contextmenue->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->ausschneidenToolStripMenuItem, 
            this->kopierenToolStripMenuItem, this->einfuegenToolStripMenuItem});
         this->contextmenue->Name = L"contextmenue";
         this->contextmenue->Size = System::Drawing::Size(192, 70);
         // 
         // 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::contextmenu_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::contextmenu_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"Einfügen";
         this->einfuegenToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::contextmenu_Click);
         // 
         // textBox1
         // 
         this->textBox1->ContextMenuStrip = this->contextmenue;
         this->textBox1->Dock = System::Windows::Forms::DockStyle::Fill;
         this->textBox1->Location = System::Drawing::Point(0, 24);
         this->textBox1->Multiline = true;
         this->textBox1->Name = L"textBox1";
         this->textBox1->Size = System::Drawing::Size(532, 271);
         this->textBox1->TabIndex = 2;
         // 
         // Form1
         // 
         this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
         this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
         this->ClientSize = System::Drawing::Size(532, 295);
         this->ContextMenuStrip = this->contextmenue;
         this->Controls->Add(this->textBox1);
         this->Controls->Add(this->menue);
         this->Name = L"Form1";
         this->Text = L"Menü-Demo";
         this->menue->ResumeLayout(false);
         this->menue->PerformLayout();
         this->contextmenue->ResumeLayout(false);
         this->ResumeLayout(false);
         this->PerformLayout();

      }
#pragma endregion
private: 
        void miDateiNeu_Click(System::Object^  sender, System::EventArgs^  e) 
        {
            // Inhalt des Textfelds löschen
            textBox1->Text = "";

            // kein Dateiname
            dateiname = nullptr;

            // Fenstertitel aktualisieren
            this->Text = "Texteditor: Unbenannt.txt";
        }

        void miDateiOeffnen_Click(System::Object^  sender, System::EventArgs^  e) 
        {
            if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
               try
               {
                 StreamReader^ sr = gcnew StreamReader(openFileDialog1->FileName, 
                                                       Encoding::GetEncoding("windows-1252"), false);
                 textBox1->Text = sr->ReadToEnd();
                 sr->Close();
              
                 // Fenstertitel aktualisieren und Dateiname speichern
                 this->dateiname = openFileDialog1->FileName;
                 this->Text = "Texteditor: " + dateiname->Substring(dateiname->LastIndexOf('\\')+1);
               }
               catch (IOException^)
               {
                  MessageBox::Show("Datei konnte nicht geöffnet werden.", "Fehler");
               }
            }

        }

        void miDateiSpeichern_Click(System::Object^  sender, System::EventArgs^  e) 
        {
             if (dateiname == nullptr)
             {
                 if (saveFileDialog1->ShowDialog() 
                               == System::Windows::Forms::DialogResult::OK)
                     dateiname = saveFileDialog1->FileName;                
                 else
                     return;
             }
             
             try
             {
                 StreamWriter^ sw = gcnew StreamWriter(dateiname, false, 
                                    Encoding::GetEncoding("windows-1252"));

                 sw->Write(textBox1->Text);
                 sw->Close();
                 
                 // Fenstertitel aktualisieren und Dateiname speichern
                 this->dateiname = dateiname;
                 this->Text = "Texteditor: " + dateiname->Substring(dateiname->LastIndexOf('\\')+1);
             }
             catch (IOException^)
             {
                 MessageBox::Show("Datei konnte nicht gespeichert werden.", "Fehler");
             }
        }

        void miDateiDrucken_Click(System::Object^  sender, System::EventArgs^  e) 
         {
            if (textBox1->Text != nullptr)
            {
                if (dateiname == nullptr)
                   printDocument1->DocumentName = "Unbenannt.txt";
                else
                   printDocument1->DocumentName = 
                      dateiname->Substring(dateiname->LastIndexOf('\\')+1);

                // Standardddrucker verwenden
                printDocument1->PrinterSettings->PrinterName = nullptr;

                // Seitenränder
                printDocument1->DefaultPageSettings->Margins 
                                          = gcnew Margins((int)(2/0.0254), 
                                                          (int)(1/0.0254), 
                                                          (int)(1/0.0254), 
                                                          (int)(2/0.0254));
                // Drucken anstoßen
                printDocument1->Print();
            }
         }
         void printDocument1_PrintPage(Object^  sender, PrintPageEventArgs^  e) 
         {
             e->Graphics->DrawString(textBox1->Text, 
                                     this->Font, Brushes::Black, 
                                     e->MarginBounds);
        }

        void miDateiBeenden_Click(System::Object^  sender, System::EventArgs^  e) 
        {
            this->Close();
        }


        void miBearbeitenAusschneiden_Click(System::Object^  sender, System::EventArgs^  e) 
        {
            textBox1->Cut();
        }
        void miBearbeitenKopieren_Click(System::Object^  sender, System::EventArgs^  e) 
        {
            textBox1->Copy();
        }
        void miBearbeitenEinfuegen_Click(System::Object^  sender, System::EventArgs^  e) 
        {
            textBox1->Paste();
        }

        
        void contextmenu_Click(System::Object^  sender, System::EventArgs^  e) 
        {
            ToolStripMenuItem^ mi = dynamic_cast<ToolStripMenuItem^>(sender);

            if (mi->Text == "Ausschneiden")
                textBox1->Cut();
            else if (mi->Text == "Kopieren")
                textBox1->Copy();
            else if (mi->Text == "Einfügen")
                textBox1->Paste();
        }
};
}

