public static void Open(MainWindow window, Model model)
        {
            bool goAhead = true;

            if (model != null && model.IsDirty && !CommandDoer.PromptSave(window, model))
            {
                goAhead = false;
            }

            if (goAhead)
            {
                System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
                ofd.InitialDirectory = CommandDoer.MapsDirectory;
                System.Windows.Forms.DialogResult result = ofd.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    string    filename  = ofd.FileName;
                    MapParser mapParser = new MapParser(filename, window.TileTemplateLookup);
                    window.ActiveModel         = mapParser.Parse();
                    window.ActiveModel.IsDirty = false;
                    window.InvalidateDrawing();
                    window.UpdateTitle();
                }
            }
        }
        public static bool SaveAs(MainWindow window, Model model)
        {
            if (model == null)
            {
                return(false);
            }

            System.Windows.Forms.SaveFileDialog sfd    = new System.Windows.Forms.SaveFileDialog();
            System.Windows.Forms.DialogResult   result = sfd.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                return(false);
            }

            string filename = sfd.FileName;

            if (!filename.ToLowerInvariant().EndsWith(".map"))
            {
                System.Windows.MessageBox.Show("File name must end with .map");
                return(false);
            }

            model.Path = filename;
            return(CommandDoer.Save(window, model));
        }
 // returns false if next action should be canceled.
 private static bool PromptSave(MainWindow window, Model model)
 {
     System.Windows.MessageBoxResult result = System.Windows.MessageBox.Show("Document has unsaved changes. Do you wish to save?", "SKWIRLZ!", System.Windows.MessageBoxButton.YesNoCancel);
     if (result == System.Windows.MessageBoxResult.Cancel)
     {
         return(false);                                                              // hit Cancel? stop action
     }
     if (result == System.Windows.MessageBoxResult.No)
     {
         return(true);                                   // hit No? continue with action
     }
     return(CommandDoer.Save(window, model));            // hit yes but then hit cancel? stop action
 }
        public MainWindow()
        {
            MainWindow.Instance = this;             // >:[

            InitializeComponent();

            this.menu_file_exit.Click   += (sender, e) => { CommandDoer.Exit(this, this.ActiveModel); };
            this.menu_file_new.Click    += (sender, e) => { CommandDoer.New(this, this.ActiveModel); };
            this.menu_file_open.Click   += (sender, e) => { CommandDoer.Open(this, this.ActiveModel); };
            this.menu_file_save.Click   += (sender, e) => { CommandDoer.Save(this, this.ActiveModel); };
            this.menu_file_saveas.Click += (sender, e) => { CommandDoer.SaveAs(this, this.ActiveModel); };
            this.menu_edit_resize.Click += (sender, e) =>
            {
                if (this.ActiveModel != null)
                {
                    new ResizeDialog(this.ActiveModel).ShowDialog();
                }
            };
            this.menu_edit_maptype.Click += (sender, e) =>
            {
                if (this.ActiveModel != null)
                {
                    new MapTypeDialog(this.ActiveModel).ShowDialog();
                }
            };

            this.SizeChanged += (sender, e) =>
            {
                this.artboardBitmap = null;
                this.RedrawTheWholeDamnThing();
            };

            this.layerToggle.Click += (sender, e) => { this.LayerToggle(); };
            this.gridToggle.Click  += (sender, e) => { this.GridToggle(); };

            this.clickCatcher.MouseDown += (sender, e) => { this.HandleMouseClick(e.GetPosition(this.clickCatcher), true, e.ChangedButton == MouseButton.Left); };
            this.clickCatcher.MouseUp   += (sender, e) => { this.HandleMouseClick(e.GetPosition(this.clickCatcher), false, e.ChangedButton == MouseButton.Left); };
            this.clickCatcher.MouseMove += (sender, e) => { this.HandleMouseMove(e.GetPosition(this.clickCatcher)); };

            this.KeyDown += (sender, e) => { this.HandleKey(e.Key, true); };
            this.KeyUp   += (sender, e) => { this.HandleKey(e.Key, false); };

            this.Loaded += (sender, e) => { this.Initialize(); };
            this.LayerToggle();
            this.UpdateTitle();
        }
        private void HandleKey(Key key, bool down)
        {
            bool shiftPressed = Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift);
            bool ctrlPressed  = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);

            if (down && !shiftPressed && !ctrlPressed)
            {
                if (key == Key.G)
                {
                    this.GridToggle();
                }
                else if (key == Key.L)
                {
                    this.LayerToggle();
                }
            }

            if (ctrlPressed)
            {
                switch (key)
                {
                case Key.N:
                    CommandDoer.New(this, this.ActiveModel);
                    break;

                case Key.O:
                    CommandDoer.Open(this, this.ActiveModel);
                    break;

                case Key.S:
                    if (shiftPressed)
                    {
                        CommandDoer.SaveAs(this, this.ActiveModel);
                    }
                    else
                    {
                        CommandDoer.Save(this, this.ActiveModel);
                    }
                    break;

                default:
                    break;
                }
            }
        }
 public static void Exit(MainWindow window, Model model)
 {
     if (model.IsDirty)
     {
         if (CommandDoer.PromptSave(window, model))
         {
             window.Close();
         }
         else
         {
             // do nothing.
         }
     }
     else
     {
         window.Close();
     }
 }
        // returns false if the user hit cancel or the document was not saved.
        public static bool Save(MainWindow window, Model model)
        {
            if (model == null)
            {
                return(false);
            }

            if (model.Path == null)
            {
                return(CommandDoer.SaveAs(window, model));
            }
            else
            {
                if (model.IsDirty)
                {
                    string fileContents = model.Serialize();
                    System.IO.File.WriteAllText(model.Path, fileContents);
                    model.IsDirty = false;
                    window.UpdateTitle();
                }
            }
            return(false);
        }