Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Windows Forms apps #169

Open
pavledev opened this issue Oct 7, 2020 · 21 comments
Open

Support for Windows Forms apps #169

pavledev opened this issue Oct 7, 2020 · 21 comments
Labels
Status/1. Ready This issue has been confirmed and is ready to be worked on. Type/Enhancement

Comments

@pavledev
Copy link

pavledev commented Oct 7, 2020

Is your feature request related to a problem? Please describe.
Can you please add support for Windows Forms Apps?

@pavledev pavledev added Status/0. New This issue is new and is awaiting confirmation from the maintainers. Type/Enhancement labels Oct 7, 2020
@pavledev pavledev changed the title Support for Windows Forms apps? Support for Windows Forms apps Oct 7, 2020
@Happypig375
Copy link
Collaborator

@FoggyFinder Probably using WpfMath would be more suitable for WinForms/WPF?

@Happypig375 Happypig375 added Status/1. Ready This issue has been confirmed and is ready to be worked on. and removed Status/0. New This issue is new and is awaiting confirmation from the maintainers. labels Oct 7, 2020
@FoggyFinder
Copy link
Collaborator

Yep

@pavledev Btw, why? I believe WinForms is outdated.

@Happypig375
Copy link
Collaborator

Merge into ForNeVeR/xaml-math#281?

@charlesroddie
Copy link
Collaborator

Windows forms apps are already supported. CSharpMath supports everything that SkiaSharp supports: https://github.com/mono/SkiaSharp

@Happypig375
Copy link
Collaborator

The control isn't there though. Additional code to wire up CSharpMath.SkiaSharp and Windows Forms is needed for even the most basic of usages.

@pavledev
Copy link
Author

pavledev commented Oct 7, 2020

@Happypig375 WpfMath only supports WPF.
@FoggyFinder Because WinForms are easier for designing

@Happypig375
Copy link
Collaborator

@pavledev As written in ForNeVeR/xaml-math#281 (comment), there is a Windows Forms control that can wrap WPF controls.

@pavledev
Copy link
Author

pavledev commented Oct 7, 2020

@Happypig375 Can you please help me to wrap it?

@charlesroddie
Copy link
Collaborator

charlesroddie commented Oct 7, 2020

Additional code to wire up

The three lines of code in https://github.com/verybadcat/CSharpMath#2-csharpmathskiasharp?

@Happypig375
Copy link
Collaborator

@pavledev
Copy link
Author

pavledev commented Oct 7, 2020

@Happypig375 Which additional code is required to use CSharpMath.SkiaSharp in WinForm?

@charlesroddie
Copy link
Collaborator

charlesroddie commented Oct 7, 2020

@pavledev
To use SkiaSharp in WinForms: Get https://www.nuget.org/packages/SkiaSharp.Views/ , create an SKControl and add to its paint event, or inherit from it and override its paint event.
To draw CSharpMath things on it: get a MathPainter or TextPainter and Draw with it on the SKCanvas.

@pavledev
Copy link
Author

pavledev commented Oct 7, 2020

var painter = new CSharpMath.SkiaSharp.MathPainter();
painter.LaTeX = @"\frac\sqrt23";
painter.Draw(skControl1);

@charlesroddie I get error: argument 1: cannot convert from 'SkiaSharp.Views.Desktop.SKControl' to 'SkiaSharp.SKCanvas'

@charlesroddie
Copy link
Collaborator

I'm not at my computer but in the paint event eventArgs you should get access to the canvas. Maybe e.Canvas or e.Surface.Canvas or something similar.

@pavledev
Copy link
Author

pavledev commented Oct 8, 2020

private void skControl1_PaintSurface(object sender, SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgs e)
{
var painter = new CSharpMath.SkiaSharp.MathPainter();
painter.LaTeX = @"\frac\sqrt23";
painter.Draw(e.Surface.Canvas);
}

@charlesroddie It's working now. Thank you.

@pavledev
Copy link
Author

pavledev commented Oct 9, 2020

@charlesroddie I can also add formula setting LaTeX property to MathPainter object and by calling skControl1.Invalidate(); from button click event right?

@charlesroddie
Copy link
Collaborator

Yes an extremely simple control would be

type MathView() =
    inherit SKControl()
    let mathPainter = MathPainter()
    member this.LaTeX
        with set tex =
            mathPainter.LaTeX <- tex
            this.InvalidateSurface()
    override _.OnPaintSurface(e) =
        e.Surface.Canvas.Clear()
        mathPainter.Draw(e.Surface.Canvas)

@pavledev
Copy link
Author

@charlesroddie How I can add scrollbar to SKControl?

@pavledev
Copy link
Author

@Happypig375 Can you please help me to add them to SKControl?

@FoggyFinder
Copy link
Collaborator

@pavledev Consider asking WinForms - related Qs in a some kind of Q&A community. Your Q isn't related to this issue. Moreover it's not even related to CSharpMath itself.

Here is a quick and dirty sample:

using SkiaSharp.Views.Desktop;
using System.Windows.Forms;

namespace CSharpMathSample
{
    public partial class Form1 : Form
    {
        private readonly Panel panel;
        private readonly SKControl skControl;
        private readonly string sample = @"Here are some text.
This text is made to be long enough to have the TextPainter of CSharpMath add a line break to this text automatically.
To demonstrate the capabilities of the TextPainter,
here are some math content:
First, a fraction in inline mode: $\frac34$
Next, a summation in inline mode: $\sum_{i=0}^3i^i$
Then, a summation in display mode: $$\sum_{i=0}^3i^i$$
After that, an integral in display mode: $$\int^6_{-56}x\ dx$$
Finally, an escaped dollar sign \$ that represents the start/end of math mode when it is unescaped.
Colors can be achieved via \backslash color{color}{content}, or \backslash \textit{color}{content},
where \textit{color} stands for one of the LaTeX standard colors.
\red{Colored text in text mode are able to automatically break up when spaces are inside the colored text, which the equivalent in math mode cannot do.}
\textbf{Styled} \texttt{text} can be achieved via the LaTeX styling commands.
The SkiaSharp version of this is located at CSharpMath.SkiaSharp.TextPainter;
and the Xamarin.Forms version of this is located at CSharpMath.Forms.TextView.
Was added in 0.1.0-pre4; working in 0.1.0-pre5; fully tested in 0.1.0-pre6. \[\frac{Display}{maths} \sqrt\text\mathtt{\ at\ the\ end}^\mathbf{are\ now\ incuded\ in\ Measure!} \]";
        public Form1()
        {
            InitializeComponent();
            panel = new Panel()
            {
                AutoScroll = true,
                Dock = DockStyle.Fill
            };
            skControl = new SKControl()
            {
                Width = Width,
                Height = Height
            };
            
            panel.Controls.Add(skControl);
            this.Controls.Add(panel);
            skControl.PaintSurface += SkControl_PaintSurface;
        }

        private void SkControl_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            e.Surface.Canvas.Clear();
            var painter = new CSharpMath.SkiaSharp.TextPainter
            {
                LaTeX = sample
            };
            painter.Draw(e.Surface.Canvas);
            var r = painter.Measure(Width);
            skControl.Width = (int)r.Width;
            skControl.Height = (int)r.Height;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status/1. Ready This issue has been confirmed and is ready to be worked on. Type/Enhancement
Projects
None yet
Development

No branches or pull requests

4 participants