jacobslusser / ScintillaNET

A Windows Forms control, wrapper, and bindings for the Scintilla text editor.
MIT License
967 stars 245 forks source link

Punch list of API requests for the new ScintillaNET #121

Open countryrobot opened 9 years ago

countryrobot commented 9 years ago

I just completed upgrading my application from Scintilla 2.6 to 3.5.1. Thank you very much for finally solving the Unicode mapping problem. Well worth the work to upgrade!

In order to migrate, I had to implement several workarounds to compensate for missing features in the redesigned API. It would be much better if the ScintillaNET could provide official API functions to avoid the need for these workarounds. Thanks!

// Workaround for missing Style.CopyTo()
public static void CopyStyle(Style target, Style source)
{
    target.BackColor = source.BackColor;
    target.Bold = source.Bold;
    target.Case = source.Case;
    target.FillLine = source.FillLine;
    target.Font = source.Font;
    target.ForeColor = source.ForeColor;
    target.Hotspot = source.Hotspot;
    target.Italic = source.Italic;
    target.Size = source.Size;
    target.SizeF = source.SizeF;
    target.Underline = source.Underline;
    target.Visible = source.Visible;
    target.Weight = source.Weight;
}

// Workaround for missing Indicator.Reset()
public static void ResetIndicator(Indicator indicator)
{
    indicator.Alpha = 30;
    indicator.ForeColor = GetDefaultIndicatorColor(indicator.Index);
    indicator.Under = false;
    indicator.OutlineAlpha = 50;
    indicator.Style = GetDefaultIndicatorStyle(indicator.Index);

}

private static Color GetDefaultIndicatorColor(int index)
{
    if (index == 0)
    {
        return Color.FromArgb(0, 0x7f, 0);
    }
    if (index == 1)
    {
        return Color.FromArgb(0, 0, 0xff);
    }
    if (index == 2)
    {
        return Color.FromArgb(0xff, 0, 0);
    }
    return Color.FromArgb(0, 0, 0);
}

private static IndicatorStyle GetDefaultIndicatorStyle(int index)
{
    if (index == 0)
    {
        return IndicatorStyle.Squiggle;
    }
    if (index == 1)
    {
        return IndicatorStyle.TT;
    }
    return IndicatorStyle.Plain;
}

// Workaround for missing Scintilla.HotspotStyle.ActiveForeColor
public static void SetHotspotActiveFore(Scintilla scintilla, bool useHotspotForeColor, Color color)
{
    const int SCI_SETHOTSPOTACTIVEFORE = 2410;
    scintilla.DirectMessage(SCI_SETHOTSPOTACTIVEFORE,
        new IntPtr(useHotspotForeColor ? 1 : 0),
        new IntPtr(ColorTranslator.ToWin32(color)));

}

// Workaround for missing Scintilla.HotspotStyle.ActiveBackColor
public static void SetHotspotActiveBack(Scintilla scintilla, bool useHotspotBackColor, Color color)
{
    const int SCI_SETHOTSPOTACTIVEBACK = 2411;
    scintilla.DirectMessage(SCI_SETHOTSPOTACTIVEBACK,
        new IntPtr(useHotspotBackColor ? 1 : 0),
        new IntPtr(ColorTranslator.ToWin32(color)));
}

// Workaround for missing Scintilla.Clipboard.CanCopy and Scintilla.Clipboard.CanCut
public static bool CanCopy(Scintilla scintilla)
{
    return scintilla.SelectionStart != scintilla.SelectionEnd;
}

// Workaround for missing Scintilla.SupressControlCharacters
private void ctlScintilla_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar < 32)
    {
        // Prevent control characters from getting inserted into the text buffer
        e.Handled = true;
        return;
    }
}

// Feature request:  An option to prevent CTRL+WHEEL from zooming accidentally.
// This is apparently a frequently encountered annoyance, e.g. see here:
// https://groups.google.com/forum/#!topic/scintilla-interest/YC_uQttrFmQ
// https://visualstudiogallery.msdn.microsoft.com/d088791c-150a-4834-8f28-462696a82bb8
//
// However, if you don't think it merits its own API, perhaps you could provide a more
// convenient way to intercept WM_MOUSEWHEEL without having to subclass
// the Scintilla control?
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg == Win32.WM_MOUSEWHEEL)
    {
        const int MK_CONTROL = 0x0008;
        int fwKeys = m.WParam.ToInt32();
        if ((fwKeys & MK_CONTROL) != 0)
        {
            // Ignore CTRL+WHEEL
            return;
        }
    }

    base.WndProc(ref m);
}
jacobslusser commented 9 years ago

Cool. I love helpful suggestions. I'll see about getting these in.

ElektroStudios commented 7 years ago

"Workaround for missing Scintilla.SupressControlCharacters". Many thanks for that, really this should be a built-in feature... any professional text editor writes by default the ugly ascii codes whhen pressing control chars... that has no sense to do it unless for debug purposes I think.

I have read about 10 questions in the issues section, and other 20 in stackoverflow until I get to the solution in this thread... thanks again. This feature of writing the ascii codes just makes our life dificult, really this should be deactivated by default and implement a workaround to handle it (the workaround gave here is just perfect)...

Thanks for read.

Miki101 commented 5 years ago

I use this to block out control characters. private void ctlScintilla_KeyPress(object sender, KeyPressEventArgs e) { if (char.IsControl(e.KeyChar) == true) e.Handled = true; } Old habits die hard. :)