예제 #1
0
 protected Exception NewParseException(string message)
 {
     return(new StyleUserError(App.Translation.Calculator.CouldNotParseExpression + ".\n\n"
                               + "*{0}:* {1}\n".Fmt(EggsML.Escape(App.Translation.Calculator.ErrLabel_Error), EggsML.Escape(message))
                               + "*{0}:* {1}<Red>=\"<\"{2}\">\"={3}".Fmt(
                                   EggsML.Escape(App.Translation.Calculator.ErrLabel_Expression),
                                   EggsML.Escape(Input.Substring(0, Pos)),
                                   EggsML.Escape(App.Translation.Calculator.Err_LocationMarker),
                                   EggsML.Escape(Input.Substring(Pos)))
                               + Description,
                               formatted: true));
 }
예제 #2
0
        /// <summary>Constructor.</summary>
        public LabelEx()
        {
            _parsed        = EggsML.Parse("");
            DoubleBuffered = true;
            SetStyle(ControlStyles.FixedHeight, false);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            // Set default properties
            TabStop         = false;
            AutoSize        = true;
            LinkColor       = SystemColors.HotTrack;
            LinkActiveColor = Color.Red;
        }
예제 #3
0
        /// <summary>Override; see base.</summary>
        protected override void OnTextChanged(EventArgs e)
        {
            // If a link has focus, trigger the LinkLostFocus event.
            // OnPaint will trigger the LinkGotFocus event as appropriate
            _keyboardFocusOnLink = null;

            _cachedPreferredSizes.Clear();
            _cachedRendering = null;
            base.OnTextChanged(e);
            _mnemonic = '\0';
            TabStop   = false;
            var origText = base.Text;

            try
            {
                _parsed    = EggsML.Parse(origText);
                ParseError = null;

                // We know there are no mnemonics or links in the exception message, so only do this if there was no parse error
                extractMnemonicEtc(_parsed);
            }
            catch (EggsMLParseException epe)
            {
                ParseError = epe;
                var msg = "";
                int ind = 0;
                if (epe.FirstIndex != null)
                {
                    ind  = epe.FirstIndex.Value;
                    msg += EggsML.Escape(origText.Substring(0, ind));
                    msg += "<Red>={0}=".Fmt(EggsML.Escape(origText.Substring(ind, 1)));
                    ind++;
                }
                msg += EggsML.Escape(origText.Substring(ind, epe.Index - ind));
                ind  = epe.Index;
                if (epe.Length > 0)
                {
                    msg += "<Red>={0}=".Fmt(EggsML.Escape(origText.Substring(ind, epe.Length)));
                    ind += epe.Length;
                }
                msg    += "<Red>= ← (" + EggsML.Escape(epe.Message) + ")=";
                msg    += EggsML.Escape(origText.Substring(ind));
                _parsed = EggsML.Parse(msg);
            }
            autosize();
            Invalidate();
        }
예제 #4
0
        /// <summary>
        ///     Creates a message form and fills in the controls using the current settings. Does not verify whether the
        ///     settings are valid, so must only be used internally and only after the settings have been verified.</summary>
        /// <returns>
        ///     The index of the button pressed.</returns>
        private int showAsIs()
        {
            using (var form = new DlgMessageForm())
            {
                if (Image != null)
                {
                    form.img.Image   = Image;
                    form.img.Visible = true;
                }

                form.Text = Caption;

                form.Font = SystemFonts.MessageBoxFont;
                if (Font != null)
                {
                    form.Message.Font = Font;
                }

                form.Message.MaximumSize = new Size(Math.Max(600, Screen.PrimaryScreen.WorkingArea.Width / 2), Screen.PrimaryScreen.WorkingArea.Height * 3 / 4);
                form.Message.Text        = Format == DlgMessageFormat.PlainText ? EggsML.Escape(Message) : Message;

                // --- Buttons - captions, visibility, accept/cancel

                Button[] Btn = new Button[4];
                Btn[0] = form.Btn0;
                Btn[1] = form.Btn1;
                Btn[2] = form.Btn2;
                Btn[3] = form.Btn3;

                form.AcceptButton = null;
                form.CancelButton = null;

                for (int i = Buttons.Length - 1; i >= 0; i--)
                {
                    Btn[i].Visible = true;
                    Btn[i].Text    = Buttons[i];
                    Btn[i].SendToBack(); // otherwise table layout ordering is messed up
                }

                form.AcceptButton = Btn[AcceptButton];
                form.CancelButton = Btn[CancelButton];

                form.Shown += (dummy1, dummy2) => Btn[AcceptButton].Focus(); // otherwise the AcceptButton has no effect

                // --- Ding

                if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX)
                {
                    switch (Type)
                    {
                    case DlgType.Info:
                        WinAPI.MessageBeep(WinAPI.MessageBeepType.Information);
                        break;

                    case DlgType.Question:
                        WinAPI.MessageBeep(WinAPI.MessageBeepType.Question);
                        break;

                    case DlgType.Warning:
                        WinAPI.MessageBeep(WinAPI.MessageBeepType.Warning);
                        break;

                    case DlgType.Error:
                        WinAPI.MessageBeep(WinAPI.MessageBeepType.Error);
                        break;
                    }
                }

                // --- Show

                form.ShowInTaskbar = ShowInTaskbar;
                var result = form.ShowDialog();

                // --- Return button index

                switch (result)
                {
                case DialogResult.OK:
                    return(0);

                case DialogResult.Cancel:
                    return(1);

                case DialogResult.Yes:
                    return(2);

                case DialogResult.No:
                    return(3);

                default:
                    // Should be unable to get here
                    throw new Exception("Internal exception in Util: unreachable code");
                }
            }
        }
예제 #5
0
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            var calculator = new SizeCalculator(renderTask, layer);
            Func <string, string> describe = property =>
                                             "*{0}:* {1}\n".Fmt(EggsML.Escape(App.Translation.Calculator.ErrLabel_Layer), EggsML.Escape((string.IsNullOrEmpty(Layer.Name) ? "" : (Layer.Name + " – ")) + Layer.TypeName)) +
                                             "*{0}:* {1}\n".Fmt(EggsML.Escape(App.Translation.Calculator.ErrLabel_Effect), EggsML.Escape((string.IsNullOrEmpty(Name) ? "" : (Name + " – ")) + TypeName)) +
                                             "*{0}:* {1}".Fmt(EggsML.Escape(App.Translation.Calculator.ErrLabel_Property), EggsML.Escape(property));

            double ParsedWidth  = Math.Max(0, calculator.Parse(Width, describe(WidthTr(App.Translation).DisplayName))),
                   ParsedHeight = Math.Max(0, calculator.Parse(Height, describe(HeightTr(App.Translation).DisplayName))),
                   ParsedX      = calculator.Parse(X, describe(XTr(App.Translation).DisplayName)),
                   ParsedY      = calculator.Parse(Y, describe(YTr(App.Translation).DisplayName));

            Tank tank   = renderTask.Tank;
            var  pixels = PixelRect.FromMixed(0, 0, layer.Width, layer.Height);

            if (ShowPixelBorders || PositionByPixels || (SizeByPixels && SizeMode2 != SizeMode2.NoChange && SizeMode2 != SizeMode2.ByPercentage))
            {
                pixels = layer.PreciseSize(PixelAlphaThreshold);
            }
            bool emptyPixels = pixels.Width <= 0 || pixels.Height <= 0;

            if (emptyPixels)
            {
                pixels = PixelRect.FromMixed(0, 0, layer.Width, layer.Height);
            }

            double scaleWidth, scaleHeight;
            int    sourceWidth  = SizeByPixels ? pixels.Width : layer.Width;
            int    sourceHeight = SizeByPixels ? pixels.Height : layer.Height;

            switch (SizeMode2)
            {
            case SizeMode2.NoChange:
                scaleWidth = scaleHeight = 1;
                break;

            case SizeMode2.ByPercentage:
                scaleWidth = scaleHeight = Percentage / 100.0;
                break;

            case SizeMode2.BySizeWidthOnly:
                scaleWidth = scaleHeight = ParsedWidth / (double)sourceWidth;
                break;

            case SizeMode2.BySizeHeightOnly:
                scaleWidth = scaleHeight = ParsedHeight / (double)sourceHeight;
                break;

            case SizeMode2.BySizeFit:
                scaleWidth = scaleHeight = Math.Min(ParsedWidth / (double)sourceWidth, ParsedHeight / (double)sourceHeight);
                break;

            case SizeMode2.BySizeStretch:
                scaleWidth  = ParsedWidth / (double)sourceWidth;
                scaleHeight = ParsedHeight / (double)sourceHeight;
                break;

            default:
                throw new Exception("7924688");
            }

            if (GrowShrinkMode == GrowShrinkMode.GrowOnly)
            {
                scaleWidth  = Math.Max(1.0, scaleWidth);
                scaleHeight = Math.Max(1.0, scaleHeight);
            }
            else if (GrowShrinkMode == GrowShrinkMode.ShrinkOnly)
            {
                scaleWidth  = Math.Min(1.0, scaleWidth);
                scaleHeight = Math.Min(1.0, scaleHeight);
            }

            var anchor       = (AnchorRaw)Anchor;
            int anchorWidth  = (int)Math.Ceiling((PositionByPixels ? pixels.Width : layer.Width) * scaleWidth);
            int anchorHeight = (int)Math.Ceiling((PositionByPixels ? pixels.Height : layer.Height) * scaleHeight);
            // Location of the top left corner of the anchored rectangle
            int tgtX = (int)ParsedX - (anchor.HasFlag(AnchorRaw.Right) ? anchorWidth - 1 : anchor.HasFlag(AnchorRaw.Center) ? (anchorWidth - 1) / 2 : 0);
            int tgtY = (int)ParsedY - (anchor.HasFlag(AnchorRaw.Bottom) ? anchorHeight - 1 : anchor.HasFlag(AnchorRaw.Mid) ? (anchorHeight - 1) / 2 : 0);
            // Location of the top left corner of the whole scaled layer image
            double x       = tgtX - (PositionByPixels ? pixels.Left * scaleWidth : 0);
            double y       = tgtY - (PositionByPixels ? pixels.Top * scaleHeight : 0);
            int    offsetX = (PositionByPixels ? pixels.Left : 0);
            int    offsetY = (PositionByPixels ? pixels.Top : 0);

            if (ShowLayerBorders || ShowPixelBorders)
            {
                using (var image = layer.ToMagickImage())
                {
                    image.Settings.StrokeWidth = 1;
                    if (ShowLayerBorders)
                    {
                        image.Settings.FillColor   = ImageMagick.MagickColors.Transparent;
                        image.Settings.StrokeColor = new ImageMagick.MagickColor("aqua");
                        image.Draw(new ImageMagick.DrawableRectangle(0, 0, layer.Width - 1, layer.Height - 1));
                    }
                    if (ShowPixelBorders && !emptyPixels)
                    {
                        image.Settings.FillColor   = ImageMagick.MagickColors.Transparent;
                        image.Settings.StrokeColor = new ImageMagick.MagickColor("red");
                        image.Draw(new ImageMagick.DrawableRectangle(pixels.Left, pixels.Top, pixels.Right, pixels.Bottom));
                    }
                    layer.CopyPixelsFrom(image.ToBitmapSource());
                }
            }
            BitmapResampler.Filter filter;
            switch (Filter)
            {
            case Filter.Auto: filter = null; break;

            case Filter.Mitchell: filter = new BitmapResampler.MitchellFilter(); break;

            case Filter.Bicubic: filter = new BitmapResampler.CatmullRomFilter(); break;

            case Filter.Lanczos: filter = new BitmapResampler.LanczosFilter(); break;

            case Filter.Sinc256: filter = new BitmapResampler.LanczosFilter(8); break;

            case Filter.Sinc1024: filter = new BitmapResampler.LanczosFilter(16); break;

            default: throw new Exception("SizePosEffect.Filter 4107");
            }
            layer = BitmapResampler.SizePos(layer, scaleWidth, scaleHeight, offsetX, offsetY, tgtX, tgtY, Math.Max(layer.Width, Layer.ParentStyle.IconWidth), Math.Max(layer.Height, Layer.ParentStyle.IconHeight), filter);
            if (ShowAnchor)
            {
                using (var image = layer.ToMagickImage())
                {
                    image.Settings.StrokeWidth = 1;
                    image.Settings.StrokeColor = new ImageMagick.MagickColor(255, 255, 0, 120);
                    image.Draw(new ImageMagick.DrawableLine((int)ParsedX - 1, (int)ParsedY, (int)ParsedX + 1, (int)ParsedY));
                    image.Draw(new ImageMagick.DrawableLine((int)ParsedX, (int)ParsedY - 1, (int)ParsedX, (int)ParsedY + 1));
                    layer.CopyPixelsFrom(image.ToBitmapSource());
                }
            }
            return(layer);
        }
예제 #6
0
        private Size doPaintOrMeasure(Graphics g, EggsNode node, Font initialFont, Color initialForeColor, int constrainingWidth,
                                      List <renderingInfo> renderings = null, List <locationInfo> locations = null)
        {
            var  glyphOverhang = TextRenderer.MeasureText(g, "Wg", initialFont, _dummySize) - TextRenderer.MeasureText(g, "Wg", initialFont, _dummySize, TextFormatFlags.NoPadding);
            int  x = glyphOverhang.Width / 2, y = glyphOverhang.Height / 2;
            int  wrapWidth         = WordWrap ? Math.Max(1, constrainingWidth - glyphOverhang.Width) : int.MaxValue;
            int  hangingIndent     = _hangingIndent * (_hangingIndentUnit == IndentUnit.Spaces ? measure(initialFont, " ", g).Width : 1);
            bool atBeginningOfLine = false;

            // STEP 1: Run the word-wrapping as if TextAlign were TopLeft

            int actualWidth = EggsML.WordWrap(node, new renderState(initialFont, initialForeColor), wrapWidth,
                                              (state, text) => measure(state.Font, text, g).Width,
                                              (state, text, width) =>
            {
                if (state.Mnemonic && !string.IsNullOrWhiteSpace(text))
                {
                    state.ActiveLocations.OfType <linkLocationInfo>().FirstOrDefault().NullOr(link => { link.Mnemonic = char.ToLowerInvariant(text.Trim()[0]); return(link); });
                }

                if (renderings != null && !string.IsNullOrEmpty(text))
                {
                    renderingInfo info;
                    if (!atBeginningOfLine && renderings.Count > 0 && (info = renderings[renderings.Count - 1]).State == state)
                    {
                        info.Text     += text;
                        var rect       = info.Rectangle;
                        rect.Width    += width;
                        info.Rectangle = rect;
                    }
                    else
                    {
                        info = new renderingInfo(text, new Rectangle(x, y, width, measure(state.Font, " ", g).Height), state);
                        renderings.Add(info);
                    }
                    foreach (var location in state.ActiveLocations)
                    {
                        if (location.Rectangles.Count == 0 || location.Rectangles[location.Rectangles.Count - 1].Y != info.Rectangle.Y)
                        {
                            location.Rectangles.Add(info.Rectangle);
                        }
                        else
                        {
                            var rect    = location.Rectangles[location.Rectangles.Count - 1];
                            rect.Width += width;
                            location.Rectangles[location.Rectangles.Count - 1] = rect;
                        }
                    }
                }
                atBeginningOfLine = false;
                x += width;
            },
                                              (state, newParagraph, indent) =>
            {
                atBeginningOfLine = true;
                var sh            = measure(state.Font, " ", g).Height;
                y += sh;
                if (newParagraph && _paragraphSpacing > 0)
                {
                    y += (int)(_paragraphSpacing * sh);
                }
                var newIndent = state.BlockIndent + indent;
                if (!newParagraph)
                {
                    newIndent += hangingIndent;
                }
                x = newIndent + glyphOverhang.Width / 2;
                return(newIndent);
            },
                                              (state, tag, parameter) =>
            {
                var font = state.Font;
                switch (tag)
                {
                // ITALICS
                case '/': return(state.ChangeFont(new Font(font, font.Style | FontStyle.Italic)), 0);