Blame mono/Diagram/MainForm.cs

8cb222
/*
8cb222
    ......... 2015 Ivan Mahonin
8cb222
8cb222
    This program is free software: you can redistribute it and/or modify
8cb222
    it under the terms of the GNU General Public License as published by
8cb222
    the Free Software Foundation, either version 3 of the License, or
8cb222
    (at your option) any later version.
8cb222
8cb222
    This program is distributed in the hope that it will be useful,
8cb222
    but WITHOUT ANY WARRANTY; without even the implied warranty of
8cb222
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8cb222
    GNU General Public License for more details.
8cb222
8cb222
    You should have received a copy of the GNU General Public License
8cb222
    along with this program.  If not, see <http: licenses="" www.gnu.org="">.</http:>
8cb222
*/
8cb222
8cb222
using System;
8cb222
using System.Windows.Forms;
8cb222
using System.Collections.Generic;
8cb222
using System.Linq;
8cb222
using System.Drawing;
8cb222
8cb222
namespace Diagram {
8cb222
    public class MainForm: Form {
8cb222
        Button bSave;
8cb222
        SaveFileDialog sfdSave;
8cb222
8cb222
        ActiveDiagram diagram;
8cb222
        ActiveBlock mouseBlock;
8cb222
        PointF mouseBlockOffset;
8cb222
    
8cb222
        public MainForm() {
8cb222
            diagram = Test.buildTestActiveDiagram(DiaSynfig.build());
8cb222
8cb222
            Width = 800;
8cb222
            Height = 600;
8cb222
        
8cb222
            bSave = new Button();
8cb222
            bSave.Left = 20;
8cb222
            bSave.Top = 20;
8cb222
            bSave.Text = "save";
8cb222
            bSave.Click += bTestClicked;
8cb222
            Controls.Add(bSave);
8cb222
8cb222
            sfdSave = new SaveFileDialog();
8cb222
            sfdSave.OverwritePrompt = true;
8cb222
8cb222
            Paint += onPaint;
8cb222
            MouseDown += onMouseDown;
8cb222
            MouseUp += onMouseUp;
8cb222
            MouseMove += onMouseMove;
8cb222
            FormClosed += onClose;
8cb222
8cb222
            try { diagram.loadPositions("positions.ini"); } catch (Exception) { }
8cb222
            Invalidate();
8cb222
        }
8cb222
8cb222
        void onClose(object sender, EventArgs e) {
8cb222
            try { diagram.savePositions("positions.ini"); }
8cb222
            catch (Exception ex) { MessageBox.Show(ex.Message); }
8cb222
            diagram.savePositions("positions.ini");
8cb222
        }
8cb222
8cb222
        void bTestClicked(object sender, EventArgs e) {
8cb222
            onClose(null, null);
8cb222
            if (sfdSave.ShowDialog() == DialogResult.OK) {
8cb222
                Bitmap b = new Bitmap(
8cb222
                    (int)Math.Ceiling(diagram.bounds.Width),
8cb222
                    (int)Math.Ceiling(diagram.bounds.Height) );
8cb222
                Graphics g = Graphics.FromImage(b);
8cb222
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
8cb222
                g.FillRectangle(Brushes.White, new RectangleF(0f, 0f, b.Width, b.Height));
8cb222
                g.TranslateTransform(-diagram.bounds.Left, -diagram.bounds.Top);
8cb222
                diagram.draw(g);
8cb222
                g.Flush();
8cb222
                b.Save(sfdSave.FileName);
8cb222
            }
8cb222
        }
8cb222
8cb222
        void onMouseDown(Object sender, MouseEventArgs e) {
8cb222
            if (e.Button == MouseButtons.Left) {
8cb222
                mouseBlock = null;
8cb222
8cb222
                foreach(KeyValuePair<string, activeblock=""> pair in diagram.blocks) {</string,>
8cb222
                    ActiveBlock b = pair.Value;
8cb222
                    if ( e.X >= b.position.X
8cb222
                      && e.Y >= b.position.Y
8cb222
                      && e.X <= b.position.X + b.size.Width
8cb222
                      && e.Y <= b.position.Y + b.size.Height )
8cb222
                    {
8cb222
                        mouseBlock = b;
8cb222
                        mouseBlockOffset = new PointF(
8cb222
                            e.X - mouseBlock.position.X,
8cb222
                            e.Y - mouseBlock.position.Y );
8cb222
                        //break;
8cb222
                    }
8cb222
                }
8cb222
            }
8cb222
        }
8cb222
8cb222
        void onMouseUp(Object sender, MouseEventArgs e) {
8cb222
            if (e.Button == MouseButtons.Left) {
8cb222
                mouseBlock = null;
8cb222
            }
8cb222
        }
8cb222
8cb222
        void onMouseMove(Object sender, MouseEventArgs e) {
8cb222
            if (mouseBlock != null) {
8cb222
                mouseBlock.position = new PointF(
8cb222
                    e.X - mouseBlockOffset.X,
8cb222
                    e.Y - mouseBlockOffset.Y );
8cb222
                diagram.placeLinks();
8cb222
                Invalidate();
8cb222
            }
8cb222
        }
8cb222
8cb222
        public void onPaint(Object sender, PaintEventArgs e) {
8cb222
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
8cb222
            diagram.draw(e.Graphics);
8cb222
        }
8cb222
    }
8cb222
}
8cb222