Пример #1
0
        public static IEnumerable <IEditor> AddImage(IEditor editor, Point location, Image img, bool useCommandExecutor, bool arrange)
        {
            var cmd = GetAddImageCommand(editor, location, img);

            if (cmd == null)
            {
                return(EmptyEditors);
            }

            if (useCommandExecutor && arrange)
            {
                editor.Site.CommandExecutor.BeginChain();
            }

            var existingEditorBounds = default(Rectangle[]);

            if (arrange)
            {
                existingEditorBounds = editor.Children.Select(e => e.Figure.Bounds).ToArray();
            }

            if (useCommandExecutor)
            {
                editor.Site.CommandExecutor.Execute(cmd);
            }
            else
            {
                cmd.Execute();
            }

            if (arrange)
            {
                var newLoc = RectUtil.GetPreferredLocation(
                    cmd.CreatedEditor.Figure.Bounds,
                    existingEditorBounds,
                    editor.Root.Figure.Right,
                    MemopadConsts.DefaultCaretPosition.X,
                    MemopadConsts.DefaultCaretPosition.Y
                    );
                var move = new ChangeBoundsCommand(
                    cmd.CreatedEditor,
                    (Size)newLoc - (Size)cmd.CreatedEditor.Figure.Location,
                    Size.Empty,
                    Directions.None,
                    new [] { cmd.CreatedEditor }
                    );
                if (useCommandExecutor)
                {
                    editor.Site.CommandExecutor.Execute(move);
                }
                else
                {
                    move.Execute();
                }
                if (useCommandExecutor)
                {
                    editor.Site.CommandExecutor.EndChain();
                }
            }

            return(new[] { cmd.CreatedEditor });
        }
Пример #2
0
        public static IEnumerable <IEditor> AddMetafile(IEditor editor, Point location, Metafile meta, bool useCommandExecutor, bool arrange)
        {
            using (editor.Figure.DirtManager.BeginDirty())
                using (var mem = new MemoryStream()) {
                    if (useCommandExecutor && arrange)
                    {
                        editor.Site.CommandExecutor.BeginChain();
                    }

                    var existingEditorBounds = default(Rectangle[]);
                    if (arrange)
                    {
                        existingEditorBounds = editor.Children.Select(e => e.Figure.Bounds).ToArray();
                    }

                    using (var bmp = new Bitmap(1, 1))
                        using (var bmpg = Graphics.FromImage(bmp)) {
                            var hdc = bmpg.GetHdc();
                            using (var mf = new Metafile(mem, hdc)) {
                                bmpg.ReleaseHdc(hdc);
                                using (var g = Graphics.FromImage(mf)) {
                                    g.DrawImage(meta, Point.Empty);
                                }
                            }
                        }

                    var imgFilePath = GetNewImageFilePath();
                    File.WriteAllBytes(imgFilePath, mem.GetBuffer());
                    var desc = new FileImageDescription(Path.GetFileName(imgFilePath));

                    var req = new CreateNodeRequest();
                    req.ModelFactory = new DelegatingModelFactory <MemoImage>(
                        () => {
                        var ret   = MemoFactory.CreateImage();
                        ret.Image = desc;
                        return(ret);
                    }
                        );
                    req.Bounds           = new Rectangle(location, meta.Size);
                    req.AdjustSizeToGrid = false;

                    var cmd = editor.GetCommand(req) as CreateNodeCommand;
                    if (useCommandExecutor)
                    {
                        editor.Site.CommandExecutor.Execute(cmd);
                    }
                    else
                    {
                        cmd.Execute();
                    }

                    meta.Dispose();


                    if (arrange)
                    {
                        var newLoc = RectUtil.GetPreferredLocation(
                            cmd.CreatedEditor.Figure.Bounds,
                            existingEditorBounds,
                            editor.Root.Figure.Right,
                            MemopadConsts.DefaultCaretPosition.X,
                            MemopadConsts.DefaultCaretPosition.Y
                            );
                        var move = new ChangeBoundsCommand(
                            cmd.CreatedEditor,
                            (Size)newLoc - (Size)cmd.CreatedEditor.Figure.Location,
                            Size.Empty,
                            Directions.None,
                            new [] { cmd.CreatedEditor }
                            );
                        if (useCommandExecutor)
                        {
                            editor.Site.CommandExecutor.Execute(move);
                        }
                        else
                        {
                            move.Execute();
                        }
                        if (useCommandExecutor)
                        {
                            editor.Site.CommandExecutor.EndChain();
                        }
                    }

                    return(new[] { cmd.CreatedEditor });
                }
        }
Пример #3
0
        public static IEnumerable <IEditor> AddFile(IEditor target, Point location, string filePath, bool embed, bool useCommandExecutor, bool arrange)
        {
            var isFile      = File.Exists(filePath);
            var isDirectory = Directory.Exists(filePath);

            if (isFile || isDirectory)
            {
                var fileName = Path.GetFileName(filePath);
                var path     = filePath;

                var embeddedId = "";

                if (!isFile && !isDirectory)
                {
                    MessageBox.Show("ファイルが見つかりませんでした。", "ファイルエラー");
                    return(EmptyEditors);
                }

                /// 埋め込む場合はEmbeddedFileRootにコピー
                if (embed && isFile)
                {
                    embeddedId = Guid.NewGuid().ToString();
                    path       = Path.Combine(embeddedId, fileName);
                    var fullPath = Path.Combine(MemopadConsts.EmbeddedFileRoot, path);
                    try {
                        PathUtil.EnsureDirectoryExists(Path.GetDirectoryName(fullPath));
                        File.Copy(filePath, fullPath);
                    } catch {
                        Logger.Warn("File copy failed. source=" + filePath + ",target=" + fullPath);
                        if (Directory.Exists(fullPath))
                        {
                            Directory.Delete(Path.GetDirectoryName(fullPath), true);
                        }
                        MessageBox.Show("ファイルのコピーに失敗しました。", "ファイルコピーエラー");
                    }
                }

                var bounds       = new Rectangle(location, new Size(1, 1));
                var modelFactory = new DelegatingModelFactory <MemoFile>(
                    () => {
                    var ret        = MemoFactory.CreateFile();
                    ret.Name       = fileName;
                    ret.Path       = path;
                    ret.IsEmbedded = embed && isFile;
                    ret.EmbeddedId = embeddedId;
                    return(ret);
                }
                    );

                if (useCommandExecutor && arrange)
                {
                    target.Site.CommandExecutor.BeginChain();
                }

                var existingEditorBounds = default(Rectangle[]);
                if (arrange)
                {
                    existingEditorBounds = target.Children.Select(e => e.Figure.Bounds).ToArray();
                }

                var req = new CreateNodeRequest();
                req.ModelFactory     = modelFactory;
                req.Bounds           = bounds;
                req.AdjustSizeToGrid = true;
                var cmd = target.GetCommand(req) as CreateNodeCommand;

                var sizeCmd = new DelegatingCommand(
                    () => {
                    var node = cmd.CreatedEditor.Figure as INode;
                    if (node != null)
                    {
                        node.AdjustSize();
                    }
                },
                    () => {
                    cmd.CreatedEditor.Figure.Bounds = bounds;
                }
                    );

                var chain = cmd.Chain(sizeCmd);

                if (useCommandExecutor)
                {
                    target.Site.CommandExecutor.Execute(chain);
                }
                else
                {
                    chain.Execute();
                }

                if (arrange)
                {
                    var newLoc = RectUtil.GetPreferredLocation(
                        cmd.CreatedEditor.Figure.Bounds,
                        existingEditorBounds,
                        target.Root.Figure.Right,
                        MemopadConsts.DefaultCaretPosition.X,
                        MemopadConsts.DefaultCaretPosition.Y
                        );
                    var move = new ChangeBoundsCommand(
                        cmd.CreatedEditor,
                        (Size)newLoc - (Size)cmd.CreatedEditor.Figure.Location,
                        Size.Empty,
                        Directions.None,
                        new [] { cmd.CreatedEditor }
                        );
                    if (useCommandExecutor)
                    {
                        target.Site.CommandExecutor.Execute(move);
                    }
                    else
                    {
                        move.Execute();
                    }
                    if (useCommandExecutor)
                    {
                        target.Site.CommandExecutor.EndChain();
                    }
                }

                return(new[] { cmd.CreatedEditor });
            }

            return(EmptyEditors);
        }