#ifndef __MODELVIEWFORM_H__
#define __MODELVIEWFORM_H__

#include "workbench/wb_model_diagram_form.h"
#include "GrtTemplates.h"

#ifdef _MSC_VER
#include  <vcclr.h> // .net interop helpers
using namespace MySQL::Grt;
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Windows::Forms;
#endif


namespace MySQL {
  namespace Workbench {

    public ref class ModelViewForm : public ::MySQL::Grt::UIForm
    {
    public:

      ModelViewForm(::wb::ModelDiagramForm *inn)
        : ::MySQL::Grt::UIForm(inn)
      { 
        
      }

      ::wb::ModelDiagramForm* get_unmanaged_object()
      {
        return (::wb::ModelDiagramForm*)inner;
      }
      

      void OnMouseMove(MouseEventArgs^ e, int X, int Y, Keys keystate, MouseButtons buttons)
      {
        get_unmanaged_object()->handle_mouse_move(X, Y, 
          ::MySQL::GUI::Mdc::BaseWindowsCanvasView::getEventState(keystate, buttons));
      }

      void OnMouseDown(MouseEventArgs^ e, int X, int Y, Keys keystate, MouseButtons buttons)
      {
        mdc::MouseButton butt= mdc::ButtonLeft;
        switch(e->Button)
        {
        case MouseButtons::Left: butt= mdc::ButtonLeft; break;
        case MouseButtons::Middle: butt= mdc::ButtonMiddle; break;
        case MouseButtons::Right: butt= mdc::ButtonRight; break;
        }
        get_unmanaged_object()->handle_mouse_button(butt, true, X, Y, 
          ::MySQL::GUI::Mdc::BaseWindowsCanvasView::getEventState(keystate, buttons));
      }

      void OnMouseUp(MouseEventArgs^ e, int X, int Y, Keys keystate, MouseButtons buttons)
      {
        mdc::MouseButton butt= mdc::ButtonLeft;
        switch(e->Button)
        {
        case MouseButtons::Left: butt= mdc::ButtonLeft; break;
        case MouseButtons::Middle: butt= mdc::ButtonMiddle; break;
        case MouseButtons::Right: butt= mdc::ButtonRight; break;
        }
        get_unmanaged_object()->handle_mouse_button(butt, false, X, Y,
          ::MySQL::GUI::Mdc::BaseWindowsCanvasView::getEventState(keystate, buttons));
      }

      void OnKeyDown(KeyEventArgs^ e, Keys keystate)
      {
        get_unmanaged_object()->handle_key(::MySQL::GUI::Mdc::BaseWindowsCanvasView::getKeyInfo(e), true, 
          ::MySQL::GUI::Mdc::BaseWindowsCanvasView::getEventState(keystate, MouseButtons::None));
      }

      void OnKeyUp(KeyEventArgs^ e, Keys keystate)
      {
        get_unmanaged_object()->handle_key(::MySQL::GUI::Mdc::BaseWindowsCanvasView::getKeyInfo(e), false, 
          ::MySQL::GUI::Mdc::BaseWindowsCanvasView::getEventState(keystate, MouseButtons::None));
      }


      String^ get_tool_cursor()
      { return CppStringToNative(get_unmanaged_object()->get_cursor()); }
      
      bool accepts_drop(int x, int y, String^ type, List<GrtValue^>^ objects)
      {
        std::list<GrtObjectRef> cobjects;

        List<GrtValue^>::Enumerator^ e= objects->GetEnumerator();
        while (e->MoveNext())
        {
          if (e->Current->is_object_instance_of("GrtObject"))
            cobjects.push_back(GrtObjectRef::cast_from(e->Current->get_unmanaged_object()));
          else
            return false;
        }

        return get_unmanaged_object()->
          accepts_drop(x, y, NativeToCppString(type), cobjects);
      }

      bool accepts_drop(int x, int y, String^ type, String^ text)
      {
        return get_unmanaged_object()->
          accepts_drop(x, y, NativeToCppString(type), NativeToCppString(text));
      }

      bool perform_drop(int x, int y, String^ type, List<GrtValue^>^ objects)
      {
        std::list<GrtObjectRef> cobjects;

        List<GrtValue^>::Enumerator^ e= objects->GetEnumerator();
        while (e->MoveNext())
        {
          if (e->Current->is_object_instance_of("GrtObject"))
            cobjects.push_back(GrtObjectRef::cast_from(e->Current->get_unmanaged_object()));
          else
            return false;
        }

        return get_unmanaged_object()->
          perform_drop(x, y, NativeToCppString(type), cobjects);
      }

      bool perform_drop(int x, int y, String^ type, String^ text)
      {
        return get_unmanaged_object()->
          perform_drop(x, y, NativeToCppString(type), NativeToCppString(text));
      }


      void set_closed(bool flag)
      {
        if (get_unmanaged_object())
          get_unmanaged_object()->set_closed(flag);
      }

      bool is_closed()
      {
        if (get_unmanaged_object())
          return get_unmanaged_object()->is_closed();
        return false;
      }

    };

  } // namespace Workbench
} // namespace MySQL

#endif // __MODELVIEWFORM_H__
