using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySQL.Workbench;
using MySQL.GUI.Mdc;
using MySQL.Utilities;

namespace MySQL.GUI.Workbench
{
  public partial class DiagramOptionsForm : Form
  {
    WbContext wbContext;
    DiagramOptionsBE optionsBE= null;
    MySQL.GUI.Mdc.WindowsGDICanvasView canvas;


    public DiagramOptionsForm()
    {
      InitializeComponent();
    }

    public DiagramOptionsForm(WbContext wbContext)
    {
      InitializeComponent();

      contentPanel.CustomBackground = true;

      this.wbContext = wbContext;

      canvas = new MySQL.GUI.Mdc.WindowsGDICanvasView(contentPanel.Handle, contentPanel.Width, contentPanel.Height, true);

      canvas.set_on_needs_repaint(canvasNeedsRepaint);

      canvas.initialize();

      optionsBE = new DiagramOptionsBE(canvas, wbContext, DiagramSizeChanged);

      optionsBE.update_size();

      widthUpDown.Value = optionsBE.get_xpages();
      heightUpDown.Value = optionsBE.get_ypages();
    }


    void DiagramSizeChanged()
    {
      widthUpDown.Value = optionsBE.get_xpages();
      heightUpDown.Value = optionsBE.get_ypages();
    }

    public void UpdateImage()
    {
      if (!Visible) 
        return;

      if (canvas != null)
      {
        canvas.repaint();
      }
    }

    protected void canvasNeedsRepaint(int x, int y, int w, int h)
    {
      contentPanel.Invalidate(new System.Drawing.Rectangle(x,y,w,h));
    }

    private void contentPanel_Paint(object sender, PaintEventArgs e)
    {
      //if (canvas != null)
        // hack
        //canvas.OnSizeChanged(contentPanel.Width, contentPanel.Height);

      UpdateImage();
    }

    private void contentPanel_SizeChanged(object sender, EventArgs e)
    {
      if (canvas != null)
        canvas.OnSizeChanged(contentPanel.Width, contentPanel.Height);
      //UpdateImage();
    }


    void contentPanel_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      if (canvas != null)
        canvas.OnMouseMove(e, ModifierKeys, MouseButtons);
    }

    void contentPanel_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      if (canvas != null)
        canvas.OnMouseUp(e, ModifierKeys, MouseButtons);
    }

    void contentPanel_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      if (canvas != null)
        canvas.OnMouseDown(e, ModifierKeys, MouseButtons);
    }

    private void heightUpDown_ValueChanged(object sender, EventArgs e)
    {
      optionsBE.set_ypages((int)heightUpDown.Value);
    }

    private void widthUpDown_ValueChanged(object sender, EventArgs e)
    {
      optionsBE.set_xpages((int)widthUpDown.Value);
    }

    private void button2_Click(object sender, EventArgs e)
    {
      optionsBE.commit();
    }

  }
}