//////////////////////////////////////////////////////////////////////////
        private bool InitEngine(string DefinitionFile, WmeCanvas Canvas)
        {
            try
            {
                DisposeNatives();

                string ProjectFile = GetProjectFile(DefinitionFile, ParentForm);
                if (ProjectFile != null)
                {
                    string LogFile = Path.Combine(Path.GetDirectoryName(ProjectFile), "WindowEdit.log");

                    Game = new WAdGame();
                    Game.ForceScripts = true;
                    Game.DoNotExpandStrings = true;
                    Game.EditorMode = true;
                    if (!Canvas.Create(Game, ProjectFile, LogFile))
                    {
                        DisposeNatives();
                        return false;
                    }
                    _Canvas = Canvas;
                    _Canvas.PaintContent += new WmeCanvas.PaintContentDelegate(OnPaintContent);
                    _Canvas.MouseMove += new MouseEventHandler(OnMouseMove);
                    _Canvas.MouseDown += new MouseEventHandler(OnMouseDown);
                    _Canvas.MouseUp += new MouseEventHandler(OnMouseUp);
                    _Canvas.KeyDown += new KeyEventHandler(OnKeyDown);

                    Application.Idle += new EventHandler(OnAppIdle);

                    return true;
                }
                return false;
            }
            catch(Exception e)
            {
                MessageBox.Show("Error initializing game engine:\n\n" + e.Message, Form.ActiveForm.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
        //////////////////////////////////////////////////////////////////////////
        private void DisposeNatives()
        {
            if(_Canvas != null)
            {
                _Canvas.PaintContent -= new WmeCanvas.PaintContentDelegate(OnPaintContent);
                _Canvas.MouseMove -= new MouseEventHandler(OnMouseMove);
                _Canvas.MouseDown -= new MouseEventHandler(OnMouseDown);
                _Canvas.MouseUp -= new MouseEventHandler(OnMouseUp);
                _Canvas.KeyDown -= new KeyEventHandler(OnKeyDown);

                _Canvas.Release();
                _Canvas.Invalidate();
                _Canvas = null;
            }

            if(LayoutTree != null)
            {
                LayoutTree.SelectionsChanged -= new EventHandler(OnSelectionChanged);
                LayoutTree.ItemDrag -= new ItemDragEventHandler(TreeItemDrag);
                LayoutTree.DragEnter -= new DragEventHandler(TreeDragEnter);
                LayoutTree.DragOver -= new DragEventHandler(TreeDragOver);
                LayoutTree.DragLeave -= new EventHandler(TreeDragLeave);
                LayoutTree.DragDrop -= new DragEventHandler(TreeDragDrop);
                LayoutTree.KeyUp -= new KeyEventHandler(TreeKeyUp);

                LayoutTree.Nodes.Clear();
                LayoutTree.SelectedNodes.Clear();
            }
            Application.Idle -= new EventHandler(OnAppIdle);

            if (_PropGrid != null)
            {
                _PropGrid.SelectedGridItemChanged -= new SelectedGridItemChangedEventHandler(OnPropertySelected);
                _PropGrid.SelectedObject = null;
            }

            RectResizer = null;

            if (Window != null)
            {
                Window.Dispose();
                Window = null;
            }

            if (InvBox != null)
            {
                InvBox.Dispose();
                InvBox = null;
            }

            if (RespBox != null)
            {
                RespBox.Dispose();
                RespBox = null;
            }

            if (Game != null)
            {
                Game.Dispose();
                Game = null;
            }
        }
 //////////////////////////////////////////////////////////////////////////
 public DocumentOpenResult OpenDocument(WmeCanvas Canvas)
 {
     return OpenDocument(Canvas, null);
 }
        //////////////////////////////////////////////////////////////////////////
        public DocumentOpenResult OpenDocument(WmeCanvas Canvas, string FileName)
        {
            if (FileName == null)
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "Windows and definition files (*.window; *.def)|*.window;*.def|Windows (*.window)|*.window|Defintion files (*.def)|*.def|All files (*.*)|*.*";
                dlg.RestoreDirectory = true;
                dlg.CheckFileExists = true;

                if (dlg.ShowDialog() != DialogResult.OK) return DocumentOpenResult.Cancel;
                else FileName = dlg.FileName;
            }

            DefinitionFile DefFile = new DefinitionFile();
            string FileType = "";

            if(DefFile.ParseFile(FileName) && DefFile.Children.Count > 0)
                FileType = DefFile.Children[0].Name.ToUpper();

            if(FileType != "WINDOW" && FileType != "INVENTORY_BOX" && FileType != "RESPONSE_BOX")
            {
                MessageBox.Show("Unsupported file type.", Form.ActiveForm.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return DocumentOpenResult.Cancel;
            }

            bool Ret = false;

            if (InitEngine(FileName, Canvas))
            {
                Game.AbsolutePathWarning = false;
                switch(FileType)
                {
                    case "WINDOW":
                        Window = new WUIWindow(Game);
                        Ret = Window.LoadFromFile(FileName);
                        if(Ret)
                        {
                            Game.Windows.Add(Window);
                            Game.FocusedWindow = Window;
                        }
                        break;

                    case "INVENTORY_BOX":
                        InvBox = new WAdInventoryBox(Game);
                        Ret = InvBox.LoadFromFile(FileName);
                        if(Ret && InvBox.Window != null)
                        {
                            Game.Windows.Add(InvBox.Window);
                            Game.FocusedWindow = InvBox.Window;
                        }
                        break;

                    case "RESPONSE_BOX":
                        RespBox = new WAdResponseBox(Game);
                        Ret = RespBox.LoadFromFile(FileName);
                        if (Ret && RespBox.Window != null)
                        {
                            Game.Windows.Add(RespBox.Window);
                            Game.FocusedWindow = RespBox.Window;
                        }
                        break;

                }
                Game.AbsolutePathWarning = true;
                UpdateScrollSize();

                this.FileName = FileName;
            }
            else Ret = false;

            if (!Ret) DisposeNatives();

            if (Ret) return DocumentOpenResult.Ok;
            else return DocumentOpenResult.Error;
        }
        //////////////////////////////////////////////////////////////////////////
        public bool NewDocument(WmeCanvas Canvas)
        {
            DocumentTypeForm dlg = new DocumentTypeForm();
            dlg.AppMgr = ParentForm.AppMgr;
            if (dlg.ShowDialog() != DialogResult.OK) return false;

            if (InitEngine(null, Canvas))
            {
                WUIWindow Win = new WUIWindow(Game);
                Win.Width = 200;
                Win.Height = 100;
                Game.Windows.Add(Win);
                Game.FocusedWindow = Win;

                switch(dlg.SelectedType)
                {
                    case "InvBox":
                        InvBox = new WAdInventoryBox(Game);
                        InvBox.Window = Win;
                        InvBox.Area = new Rectangle(0, 0, Win.Width, Win.Height);
                        break;
                    case "RespBox":
                        RespBox = new WAdResponseBox(Game);
                        RespBox.Window = Win;
                        RespBox.Area = new Rectangle(0, 0, Win.Width, Win.Height);
                        break;
                    default:
                        Window = Win;
                        break;
                }

                UpdateScrollSize();
                FileName = "";
                IsDirty = true;

                return true;
            }
            else return false;
        }