Skip to content

Commit

Permalink
+ Added reset style button in syntax style option page
Browse files Browse the repository at this point in the history
+ Shows property declaration in quick info
  • Loading branch information
wmjordan committed Feb 1, 2018
1 parent 4d6df3d commit f280c2c
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 158 deletions.
11 changes: 9 additions & 2 deletions Codist/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,17 @@ internal Color BackColor {
set { _backColor = value; }
}

public abstract string Category { get; }
internal StyleBase Clone() {
return (StyleBase)MemberwiseClone();
}
public abstract string Category { get; }
internal void Reset() {
Bold = Italic = OverLine = Underline = StrikeThrough = null;
FontSize = 0;
BackgroundEffect = BrushEffect.Solid;
Font = null;
_foreColor = _backColor = default(Color);
}
}

[DebuggerDisplay("{StyleID} {ForegroundColor} {FontSize}")]
Expand Down Expand Up @@ -371,7 +378,7 @@ public override string Category {
if (f == null) {
return _Category = String.Empty;
}
var c = f.GetCustomAttribute<System.ComponentModel.CategoryAttribute>(false);
var c = f.GetCustomAttribute<CategoryAttribute>(false);
if (c == null) {
return _Category = String.Empty;
}
Expand Down
14 changes: 14 additions & 0 deletions Codist/Options/SyntaxStyleOptionPage.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

192 changes: 102 additions & 90 deletions Codist/Options/SyntaxStyleOptionPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ namespace Codist.Options
[Browsable(false)]
public partial class SyntaxStyleOptionPage : UserControl
{
readonly Func<IEnumerable<StyleBase>> _defaultStyleLoader;
readonly ConfigPage _service;
readonly Func<IEnumerable<StyleBase>> _styleLoader;
readonly Func<IEnumerable<StyleBase>> _defaultStyleLoader;
StyleBase _activeStyle;
bool _uiLock;
bool _loaded;

bool _uiLock;
public SyntaxStyleOptionPage() {
InitializeComponent();
_BackgroundEffectBox.Items.AddRange(new[] { "Solid", "Paint bottom", "Paint top", "Paint right", "Paint left" });
Expand Down Expand Up @@ -71,6 +70,92 @@ protected override void OnLoad(EventArgs e) {
_loaded = true;
}

static void RenderPreview(Bitmap bmp, FontInfo fs, StyleBase style) {
var fontSize = (float)(fs.wPointSize + style.FontSize);
if (fontSize < 2) {
return;
}
using (var g = Graphics.FromImage(bmp))
using (var f = new Font(String.IsNullOrEmpty(style.Font) ? fs.bstrFaceName : style.Font, fontSize, ConfigPage.GetFontStyle(style)))
using (var b = style.ForeColor.A == 0 ? (Brush)Brushes.Black.Clone() : new SolidBrush(style.ForeColor.ToGdiColor())) {
const string t = "Preview 01ioIOlLWM";
var m = g.MeasureString(t, f, bmp.Size);
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.CompositingQuality = CompositingQuality.HighQuality;
using (var bb = ConfigPage.GetPreviewBrush(style.BackgroundEffect, style.BackColor, ref m)) {
g.FillRectangle(bb, new Rectangle(0, 0, (int)m.Width, (int)m.Height));
}
g.DrawString(t, f, b, new RectangleF(PointF.Empty, bmp.PhysicalDimension));
}
}

static bool? ToBool(CheckState state) {
switch (state) {
case CheckState.Unchecked: return false;
case CheckState.Checked: return true;
default: return null;
}
}

static CheckState ToCheckState(bool? value) {
return value.HasValue == false
? CheckState.Indeterminate
: value.Value
? CheckState.Checked
: CheckState.Unchecked;
}

static Color ToColor(System.Windows.Media.Color color) {
return Color.FromArgb(255, color.R, color.G, color.B);
}

void _SyntaxListBox_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
if (e.ItemIndex == -1) {
return;
}
var i = e.Item.Tag as StyleBase;
if (i == null) {
return;
}
_uiLock = true;
_activeStyle = i;
UpdateUIControls(i);

UpdatePreview();
_uiLock = false;
}

void UpdateUIControls(StyleBase style) {
_BoldBox.CheckState = ToCheckState(style.Bold);
_ItalicBox.CheckState = ToCheckState(style.Italic);
_StrikeBox.CheckState = ToCheckState(style.StrikeThrough);
_UnderlineBox.CheckState = ToCheckState(style.Underline);
_BackgroundEffectBox.SelectedIndex = (int)style.BackgroundEffect;

_FontBox.Text = style.Font;
_FontSizeBox.Value = style.FontSize > 100 ? 100m : style.FontSize < -10 ? -10m : (decimal)style.FontSize;
_ForeColorTransBox.Value = style.ForeColor.A;
_BackColorTransBox.Value = style.BackColor.A;
_ForeColorButton.SelectedColor = ToColor(style.ForeColor);
_BackColorButton.SelectedColor = ToColor(style.BackColor);
}

private ListViewItem GetListItemForStyle(string category, ListViewItem vi) {
vi.Text = category;
vi.IndentCount = 1;
switch (category) {
case Constants.SyntaxCategory.Comment: vi.BackColor = Color.LightGreen; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.CompilerMarked: vi.BackColor = Color.LightGray; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.Declaration: vi.BackColor = Color.LightCyan; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.Keyword: vi.BackColor = Color.LightBlue; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.Preprocessor: vi.BackColor = Color.Gray; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.Member: vi.BackColor = Color.LightCoral; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.TypeDefinition: vi.BackColor = Color.LightYellow; vi.ForeColor = Color.Black; break;
}
return vi;
}

void LoadStyleList() {
_uiLock = true;
_SyntaxListBox.Items.Clear();
Expand All @@ -96,22 +181,6 @@ void LoadStyleList() {
}
_uiLock = false;
}

private ListViewItem GetListItemForStyle(string category, ListViewItem vi) {
vi.Text = category;
vi.IndentCount = 1;
switch (category) {
case Constants.SyntaxCategory.Comment: vi.BackColor = Color.LightGreen; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.CompilerMarked: vi.BackColor = Color.LightGray; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.Declaration: vi.BackColor = Color.LightCyan; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.Keyword: vi.BackColor = Color.LightBlue; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.Preprocessor: vi.BackColor = Color.Gray; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.Member: vi.BackColor = Color.LightCoral; vi.ForeColor = Color.Black; break;
case Constants.SyntaxCategory.TypeDefinition: vi.BackColor = Color.LightYellow; vi.ForeColor = Color.Black; break;
}
return vi;
}

void MarkChanged(object sender, EventArgs args) {
if (_uiLock || _activeStyle == null) {
return;
Expand All @@ -120,14 +189,16 @@ void MarkChanged(object sender, EventArgs args) {
Config.Instance.FireConfigChangedEvent();
}

private void SetForeColor(object sender, EventArgs args) {
if (_uiLock || _activeStyle == null) {
void ResetButton_Click(object sender, EventArgs e) {
if (_activeStyle == null) {
return;
}
if (sender == _ForeColorButton && _ForeColorTransBox.Value == 0) {
_ForeColorTransBox.Value = 255;
}
_activeStyle.ForeColor = _ForeColorButton.SelectedColor.ChangeTrasparency((byte)_ForeColorTransBox.Value).ToWpfColor();
_uiLock = true;
_activeStyle.Reset();
UpdateUIControls(_activeStyle);
UpdatePreview();
Config.Instance.FireConfigChangedEvent();
_uiLock = false;
}

private void SetBackColor(object sender, EventArgs args) {
Expand All @@ -140,37 +211,15 @@ private void SetBackColor(object sender, EventArgs args) {
_activeStyle.BackColor = _BackColorButton.SelectedColor.ChangeTrasparency((byte)_BackColorTransBox.Value).ToWpfColor();
}

void _SyntaxListBox_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
if (e.ItemIndex == -1) {
private void SetForeColor(object sender, EventArgs args) {
if (_uiLock || _activeStyle == null) {
return;
}
var i = e.Item.Tag as StyleBase;
if (i == null) {
return;
if (sender == _ForeColorButton && _ForeColorTransBox.Value == 0) {
_ForeColorTransBox.Value = 255;
}
_uiLock = true;
_activeStyle = i;
_BoldBox.CheckState = ToCheckState(i.Bold);
_ItalicBox.CheckState = ToCheckState(i.Italic);
_StrikeBox.CheckState = ToCheckState(i.StrikeThrough);
_UnderlineBox.CheckState = ToCheckState(i.Underline);
_BackgroundEffectBox.SelectedIndex = (int)i.BackgroundEffect;

_FontBox.Text = i.Font;
_FontSizeBox.Value = i.FontSize > 100 ? 100m : i.FontSize < -10 ? -10m : (decimal)i.FontSize;
_ForeColorTransBox.Value = i.ForeColor.A;
_BackColorTransBox.Value = i.BackColor.A;
_ForeColorButton.SelectedColor = ToColor(i.ForeColor);
_BackColorButton.SelectedColor = ToColor(i.BackColor);

UpdatePreview();
_uiLock = false;
}

static Color ToColor(System.Windows.Media.Color color) {
return Color.FromArgb(255, color.R, color.G, color.B);
_activeStyle.ForeColor = _ForeColorButton.SelectedColor.ChangeTrasparency((byte)_ForeColorTransBox.Value).ToWpfColor();
}

void UpdatePreview() {
if (_activeStyle == null) {
return;
Expand All @@ -181,47 +230,10 @@ void UpdatePreview() {
RenderPreview(bmp, fs, style);
_PreviewBox.Image = bmp;
}

static void RenderPreview(Bitmap bmp, FontInfo fs, StyleBase style) {
var fontSize = (float)(fs.wPointSize + style.FontSize);
if (fontSize < 2) {
return;
}
using (var g = Graphics.FromImage(bmp))
using (var f = new Font(String.IsNullOrEmpty(style.Font) ? fs.bstrFaceName : style.Font, fontSize, ConfigPage.GetFontStyle(style)))
using (var b = style.ForeColor.A == 0 ? (Brush)Brushes.Black.Clone() : new SolidBrush(style.ForeColor.ToGdiColor())) {
const string t = "Preview 01ioIOlLWM";
var m = g.MeasureString(t, f, bmp.Size);
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.CompositingQuality = CompositingQuality.HighQuality;
using (var bb = ConfigPage.GetPreviewBrush(style.BackgroundEffect, style.BackColor, ref m)) {
g.FillRectangle(bb, new Rectangle(0, 0, (int)m.Width, (int)m.Height));
}
g.DrawString(t, f, b, new RectangleF(PointF.Empty, bmp.PhysicalDimension));
}
}

static CheckState ToCheckState(bool? value) {
return value.HasValue == false
? CheckState.Indeterminate
: value.Value
? CheckState.Checked
: CheckState.Unchecked;
}
static bool? ToBool(CheckState state) {
switch (state) {
case CheckState.Unchecked: return false;
case CheckState.Checked: return true;
default: return null;
}
}

struct FontFamilyItem
{
internal readonly string Name;
internal readonly FontFamily FontFamily;

internal readonly string Name;
public FontFamilyItem(FontFamily fontFamily) {
Name = fontFamily.GetName(0);
FontFamily = fontFamily;
Expand Down
2 changes: 1 addition & 1 deletion Codist/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.4.0.0")]
[assembly: AssemblyFileVersion("2.4.1.401")]
[assembly: AssemblyFileVersion("2.4.1.406")]
Loading

0 comments on commit f280c2c

Please sign in to comment.