Browse Source

Remove duplicated code in FontSelectionPanel and GeneralTextEditorPanel.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1950 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 19 years ago
parent
commit
e45e18552f
  1. 248
      src/Main/Base/Project/Src/Gui/Components/FontSelectionPanel.cs
  2. 149
      src/Main/Base/Project/Src/TextEditor/Gui/OptionPanels/GeneralTextEditorPanel.cs

248
src/Main/Base/Project/Src/Gui/Components/FontSelectionPanel.cs

@ -9,8 +9,9 @@ using System;
using System.IO; using System.IO;
using System.Drawing; using System.Drawing;
using System.Drawing.Text; using System.Drawing.Text;
using System.Collections; using System.Collections.Generic;
using System.Windows.Forms; using System.Windows.Forms;
using System.Threading;
using ICSharpCode.SharpDevelop.Internal.ExternalTool; using ICSharpCode.SharpDevelop.Internal.ExternalTool;
using ICSharpCode.Core; using ICSharpCode.Core;
@ -34,31 +35,27 @@ namespace ICSharpCode.SharpDevelop.Gui
public Font CurrentFont { public Font CurrentFont {
get { get {
int fontSize = 10; if (helper == null)
try { return null;
fontSize = Math.Max(6, Int32.Parse(ControlDictionary["fontSizeComboBox"].Text)); return helper.GetSelectedFont();
} catch (Exception) {}
int index = ((ComboBox)ControlDictionary["fontListComboBox"]).SelectedIndex;
if (index < 0) {
return Font;
}
FontDescriptor fontDescriptor = (FontDescriptor)((ComboBox)ControlDictionary["fontListComboBox"]).Items[index];
return new Font(fontDescriptor.Name,
fontSize);
} }
set { set {
int index = 0; if (helper == null) {
for (int i = 0; i < ((ComboBox)ControlDictionary["fontListComboBox"]).Items.Count; ++i) { helper = new FontSelectionPanelHelper((ComboBox)ControlDictionary["fontSizeComboBox"], (ComboBox)ControlDictionary["fontListComboBox"], value);
FontDescriptor descriptor = (FontDescriptor)((ComboBox)ControlDictionary["fontListComboBox"]).Items[i]; helper.StartThread();
if (descriptor.Name == value.Name) { ((ComboBox)ControlDictionary["fontListComboBox"]).MeasureItem += helper.MeasureComboBoxItem;
index = i; ((ComboBox)ControlDictionary["fontListComboBox"]).DrawItem += helper.ComboBoxDrawItem;
} else {
int index = 0;
for (int i = 0; i < ((ComboBox)ControlDictionary["fontListComboBox"]).Items.Count; ++i) {
FontSelectionPanelHelper.FontDescriptor descriptor = (FontSelectionPanelHelper.FontDescriptor)((ComboBox)ControlDictionary["fontListComboBox"]).Items[i];
if (descriptor.Name == value.Name) {
index = i;
}
} }
((ComboBox)ControlDictionary["fontListComboBox"]).SelectedIndex = index;
} }
((ComboBox)ControlDictionary["fontSizeComboBox"]).Text = value.Size.ToString(); ((ComboBox)ControlDictionary["fontSizeComboBox"]).Text = value.Size.ToString();
((ComboBox)ControlDictionary["fontListComboBox"]).SelectedIndex = index;
UpdateFontPreviewLabel(this, EventArgs.Empty);
} }
} }
@ -66,38 +63,18 @@ namespace ICSharpCode.SharpDevelop.Gui
{ {
SetupFromXmlStream(this.GetType().Assembly.GetManifestResourceStream("Resources.FontSelectionPanel.xfrm")); SetupFromXmlStream(this.GetType().Assembly.GetManifestResourceStream("Resources.FontSelectionPanel.xfrm"));
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
for (int i = 6; i <= 24; ++i) { for (int i = 6; i <= 24; ++i) {
((ComboBox)ControlDictionary["fontSizeComboBox"]).Items.Add(i); ((ComboBox)ControlDictionary["fontSizeComboBox"]).Items.Add(i);
} }
((ComboBox)ControlDictionary["fontSizeComboBox"]).TextChanged += new EventHandler(UpdateFontPreviewLabel); ((ComboBox)ControlDictionary["fontSizeComboBox"]).TextChanged += new EventHandler(UpdateFontPreviewLabel);
foreach (FontFamily fontFamily in installedFontCollection.Families) { ((ComboBox)ControlDictionary["fontSizeComboBox"]).Enabled = false;
if (fontFamily.IsStyleAvailable(FontStyle.Regular) && fontFamily.IsStyleAvailable(FontStyle.Bold) && fontFamily.IsStyleAvailable(FontStyle.Italic)) { ((ComboBox)ControlDictionary["fontListComboBox"]).Enabled = false;
((ComboBox)ControlDictionary["fontListComboBox"]).Items.Add(new FontDescriptor(fontFamily));
}
}
((ComboBox)ControlDictionary["fontListComboBox"]).TextChanged += new EventHandler(UpdateFontPreviewLabel); ((ComboBox)ControlDictionary["fontListComboBox"]).TextChanged += new EventHandler(UpdateFontPreviewLabel);
((ComboBox)ControlDictionary["fontListComboBox"]).SelectedIndexChanged += new EventHandler(UpdateFontPreviewLabel); ((ComboBox)ControlDictionary["fontListComboBox"]).SelectedIndexChanged += new EventHandler(UpdateFontPreviewLabel);
((ComboBox)ControlDictionary["fontListComboBox"]).MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.MeasureComboBoxItem);
((ComboBox)ControlDictionary["fontListComboBox"]).DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ComboBoxDrawItem);
boldComboBoxFont = new Font(ControlDictionary["fontListComboBox"].Font, FontStyle.Bold);
}
void MeasureComboBoxItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (e.Index >= 0) {
FontDescriptor fontDescriptor = (FontDescriptor)comboBox.Items[e.Index];
SizeF size = e.Graphics.MeasureString(fontDescriptor.Name, comboBox.Font);
e.ItemWidth = (int)size.Width;
e.ItemHeight = (int)comboBox.Font.Height;
}
} }
FontSelectionPanelHelper helper;
public static Font ParseFont(string font) public static Font ParseFont(string font)
{ {
@ -110,25 +87,107 @@ namespace ICSharpCode.SharpDevelop.Gui
} }
} }
static StringFormat drawStringFormat = new StringFormat(StringFormatFlags.NoWrap); void UpdateFontPreviewLabel(object sender, EventArgs e)
static Font boldComboBoxFont; {
helper.UpdateFontPreviewLabel(ControlDictionary["fontPreviewLabel"]);
}
}
class FontSelectionPanelHelper
{
ComboBox fontSizeComboBox, fontListComboBox;
Font defaultFont;
public FontSelectionPanelHelper(ComboBox fontSizeComboBox, ComboBox fontListComboBox, Font defaultFont)
{
this.fontSizeComboBox = fontSizeComboBox;
this.fontListComboBox = fontListComboBox;
this.defaultFont = defaultFont;
boldComboBoxFont = new Font(fontListComboBox.Font, FontStyle.Bold);
}
public void StartThread()
{
Thread thread = new Thread(DetectMonospacedThread);
thread.IsBackground = true;
thread.Start();
}
void ComboBoxDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) void DetectMonospacedThread()
{
Thread.Sleep(0); // first allow UI thread to do some work
DebugTimer.Start();
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
Font currentFont = defaultFont;
List<FontDescriptor> fonts = new List<FontDescriptor>();
int index = 0;
foreach (FontFamily fontFamily in installedFontCollection.Families) {
if (fontFamily.IsStyleAvailable(FontStyle.Regular) && fontFamily.IsStyleAvailable(FontStyle.Bold) && fontFamily.IsStyleAvailable(FontStyle.Italic)) {
if (fontFamily.Name == currentFont.Name) {
index = fonts.Count;
}
fonts.Add(new FontDescriptor(fontFamily));
}
}
DebugTimer.Stop("Getting installed fonts");
WorkbenchSingleton.SafeThreadAsyncCall(
delegate {
fontListComboBox.Items.AddRange(fonts.ToArray());
fontSizeComboBox.Enabled = true;
fontListComboBox.Enabled = true;
fontListComboBox.SelectedIndex = index;
fontSizeComboBox.Text = currentFont.Size.ToString();
});
DebugTimer.Start();
using (Bitmap newBitmap = new Bitmap(1, 1)) {
using (Graphics g = Graphics.FromImage(newBitmap)) {
foreach (FontDescriptor fd in fonts) {
fd.DetectMonospaced(g);
}
}
}
DebugTimer.Stop("Detect Monospaced");
fontListComboBox.Invalidate();
}
internal void MeasureComboBoxItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{ {
ComboBox comboBox = (ComboBox)sender; ComboBox comboBox = (ComboBox)sender;
e.DrawBackground();
if (e.Index >= 0) { if (e.Index >= 0) {
FontDescriptor fontDescriptor = (FontDescriptor)comboBox.Items[e.Index]; FontDescriptor fontDescriptor = (FontDescriptor)comboBox.Items[e.Index];
Rectangle drawingRect = new Rectangle(e.Bounds.X, SizeF size = e.Graphics.MeasureString(fontDescriptor.Name, comboBox.Font);
e.Bounds.Y, e.ItemWidth = (int)size.Width;
e.Bounds.Width, e.ItemHeight = (int)comboBox.Font.Height;
e.Bounds.Height); }
}
Brush drawItemBrush = SystemBrushes.WindowText;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { static StringFormat drawStringFormat = new StringFormat(StringFormatFlags.NoWrap);
drawItemBrush = SystemBrushes.HighlightText; Font boldComboBoxFont;
}
internal void ComboBoxDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
e.DrawBackground();
Rectangle drawingRect = new Rectangle(e.Bounds.X,
e.Bounds.Y,
e.Bounds.Width,
e.Bounds.Height);
Brush drawItemBrush = SystemBrushes.WindowText;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
drawItemBrush = SystemBrushes.HighlightText;
}
if (comboBox.Enabled == false) {
e.Graphics.DrawString(ResourceService.GetString("ICSharpCode.SharpDevelop.Gui.Pads.ClassScout.LoadingNode"),
comboBox.Font,
drawItemBrush,
drawingRect,
drawStringFormat);
} else if (e.Index >= 0) {
FontDescriptor fontDescriptor = (FontDescriptor)comboBox.Items[e.Index];
e.Graphics.DrawString(fontDescriptor.Name, e.Graphics.DrawString(fontDescriptor.Name,
fontDescriptor.IsMonospaced ? boldComboBoxFont : comboBox.Font, fontDescriptor.IsMonospaced ? boldComboBoxFont : comboBox.Font,
drawItemBrush, drawItemBrush,
@ -138,50 +197,57 @@ namespace ICSharpCode.SharpDevelop.Gui
e.DrawFocusRectangle(); e.DrawFocusRectangle();
} }
class FontDescriptor public Font GetSelectedFont()
{ {
FontFamily fontFamily; if (!fontListComboBox.Enabled)
bool isMonospaced = false; return null;
bool initializedMonospace = false; int fontSize = 10;
public string Name { try {
get { fontSize = Math.Max(6, Int32.Parse(fontSizeComboBox.Text));
return fontFamily.Name; } catch (Exception) {}
}
}
public bool IsMonospaced { FontDescriptor fontDescriptor = (FontDescriptor)fontListComboBox.Items[fontListComboBox.SelectedIndex];
get {
if (!initializedMonospace) {
isMonospaced = GetIsMonospaced(fontFamily);
}
return isMonospaced;
}
}
bool GetIsMonospaced(FontFamily fontFamily) return new Font(fontDescriptor.Name,
{ fontSize);
using (Bitmap newBitmap = new Bitmap(1, 1)) { }
using (Graphics g = Graphics.FromImage(newBitmap)) {
using (Font f = new Font(fontFamily, 10)) { public void UpdateFontPreviewLabel(Control fontPreviewLabel)
// determine if the length of i == m because I see no other way of {
// getting if a font is monospaced or not. Font currentFont = GetSelectedFont();
int w1 = (int)g.MeasureString("i.", f).Width; fontPreviewLabel.Visible = currentFont != null;
int w2 = (int)g.MeasureString("mw", f).Width; if (currentFont != null) {
return w1 == w2; fontPreviewLabel.Font = currentFont;
}
}
}
} }
}
public class FontDescriptor
{
FontFamily fontFamily;
internal string Name;
internal bool IsMonospaced;
public FontDescriptor(FontFamily fontFamily) public FontDescriptor(FontFamily fontFamily)
{ {
this.fontFamily = fontFamily; this.fontFamily = fontFamily;
this.Name = fontFamily.Name;
} }
}
void UpdateFontPreviewLabel(object sender, EventArgs e) internal void DetectMonospaced(Graphics g)
{ {
ControlDictionary["fontPreviewLabel"].Font = CurrentFont; this.IsMonospaced = DetectMonospaced(g, fontFamily);
}
static bool DetectMonospaced(Graphics g, FontFamily fontFamily)
{
using (Font f = new Font(fontFamily, 10)) {
// determine if the length of i == m because I see no other way of
// getting if a font is monospaced or not.
int w1 = TextRenderer.MeasureText("i.", f).Width;
int w2 = TextRenderer.MeasureText("mw", f).Width;
return w1 == w2;
}
}
} }
} }
} }

149
src/Main/Base/Project/Src/TextEditor/Gui/OptionPanels/GeneralTextEditorPanel.cs

@ -31,36 +31,8 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.OptionPanels
{ {
int encoding = Encoding.UTF8.CodePage; int encoding = Encoding.UTF8.CodePage;
class FontDescriptor
{
FontFamily fontFamily;
internal string Name;
internal bool IsMonospaced;
public FontDescriptor(FontFamily fontFamily)
{
this.fontFamily = fontFamily;
this.Name = fontFamily.Name;
}
internal void DetectMonospaced(Graphics g)
{
this.IsMonospaced = DetectMonospaced(g, fontFamily);
}
static bool DetectMonospaced(Graphics g, FontFamily fontFamily)
{
using (Font f = new Font(fontFamily, 10)) {
// determine if the length of i == m because I see no other way of
// getting if a font is monospaced or not.
int w1 = TextRenderer.MeasureText("i.", f).Width;
int w2 = TextRenderer.MeasureText("mw", f).Width;
return w1 == w2;
}
}
}
ComboBox fontListComboBox, fontSizeComboBox; ComboBox fontListComboBox, fontSizeComboBox;
FontSelectionPanelHelper helper;
public override void LoadPanelContents() public override void LoadPanelContents()
{ {
@ -92,134 +64,33 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.OptionPanels
for (int i = 6; i <= 24; ++i) { for (int i = 6; i <= 24; ++i) {
fontSizeComboBox.Items.Add(i); fontSizeComboBox.Items.Add(i);
} }
fontSizeComboBox.TextChanged += new EventHandler(UpdateFontPreviewLabel); fontSizeComboBox.TextChanged += new EventHandler(UpdateFontPreviewLabel);
fontSizeComboBox.Enabled = false; fontSizeComboBox.Enabled = false;
fontListComboBox.Enabled = false; fontListComboBox.Enabled = false;
fontListComboBox.TextChanged += new EventHandler(UpdateFontPreviewLabel); fontListComboBox.TextChanged += new EventHandler(UpdateFontPreviewLabel);
fontListComboBox.SelectedIndexChanged += new EventHandler(UpdateFontPreviewLabel); fontListComboBox.SelectedIndexChanged += new EventHandler(UpdateFontPreviewLabel);
fontListComboBox.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.MeasureComboBoxItem);
fontListComboBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ComboBoxDrawItem);
boldComboBoxFont = new Font(ControlDictionary["fontListComboBox"].Font, FontStyle.Bold);
// GeneralTextEditorPanel.selectedFont = ParseFont(ControlDictionary["fontNameDisplayTextBox"].Text);
//
// ControlDictionary["browseButton"].Click += new EventHandler(SelectFontEvent);
UpdateFontPreviewLabel(null, null);
Thread thread = new Thread(DetectMonospacedThread);
thread.IsBackground = true;
thread.Start();
}
void DetectMonospacedThread()
{
Thread.Sleep(0); // first allow UI thread to do some work
DebugTimer.Start();
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
Font currentFont = ParseFont(((Properties)CustomizationObject).Get("DefaultFont", ResourceService.DefaultMonospacedFont.ToString()).ToString());
List<FontDescriptor> fonts = new List<FontDescriptor>();
int index = 0;
foreach (FontFamily fontFamily in installedFontCollection.Families) {
if (fontFamily.IsStyleAvailable(FontStyle.Regular) && fontFamily.IsStyleAvailable(FontStyle.Bold) && fontFamily.IsStyleAvailable(FontStyle.Italic)) {
if (fontFamily.Name == currentFont.Name) {
index = fonts.Count;
}
fonts.Add(new FontDescriptor(fontFamily));
}
}
DebugTimer.Stop("Getting installed fonts");
WorkbenchSingleton.SafeThreadAsyncCall(
delegate {
fontListComboBox.Items.AddRange(fonts.ToArray());
fontListComboBox.SelectedIndex = index;
fontListComboBox.Enabled = true;
fontSizeComboBox.Text = currentFont.Size.ToString();
fontSizeComboBox.Enabled = true;
});
DebugTimer.Start();
using (Bitmap newBitmap = new Bitmap(1, 1)) {
using (Graphics g = Graphics.FromImage(newBitmap)) {
foreach (FontDescriptor fd in fonts) {
fd.DetectMonospaced(g);
}
}
}
DebugTimer.Stop("Detect Monospaced");
fontListComboBox.Invalidate();
}
void MeasureComboBoxItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (e.Index >= 0) {
FontDescriptor fontDescriptor = (FontDescriptor)comboBox.Items[e.Index];
SizeF size = e.Graphics.MeasureString(fontDescriptor.Name, comboBox.Font);
e.ItemWidth = (int)size.Width;
e.ItemHeight = (int)comboBox.Font.Height;
}
}
static StringFormat drawStringFormat = new StringFormat(StringFormatFlags.NoWrap);
static Font boldComboBoxFont;
void ComboBoxDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
e.DrawBackground();
Rectangle drawingRect = new Rectangle(e.Bounds.X, Font currentFont = FontSelectionPanel.ParseFont(((Properties)CustomizationObject).Get("DefaultFont", ResourceService.DefaultMonospacedFont.ToString()).ToString());
e.Bounds.Y, helper = new FontSelectionPanelHelper(fontSizeComboBox, fontListComboBox, currentFont);
e.Bounds.Width,
e.Bounds.Height);
Brush drawItemBrush = SystemBrushes.WindowText; fontListComboBox.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(helper.MeasureComboBoxItem);
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { fontListComboBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(helper.ComboBoxDrawItem);
drawItemBrush = SystemBrushes.HighlightText;
}
if (comboBox.Enabled == false) { UpdateFontPreviewLabel(null, null);
e.Graphics.DrawString(ResourceService.GetString("ICSharpCode.SharpDevelop.Gui.Pads.ClassScout.LoadingNode"), helper.StartThread();
comboBox.Font,
drawItemBrush,
drawingRect,
drawStringFormat);
} else if (e.Index >= 0) {
FontDescriptor fontDescriptor = (FontDescriptor)comboBox.Items[e.Index];
e.Graphics.DrawString(fontDescriptor.Name,
fontDescriptor.IsMonospaced ? boldComboBoxFont : comboBox.Font,
drawItemBrush,
drawingRect,
drawStringFormat);
}
e.DrawFocusRectangle();
} }
Font CurrentFont { Font CurrentFont {
get { get {
if (!fontListComboBox.Enabled) return helper.GetSelectedFont();
return null;
int fontSize = 10;
try {
fontSize = Math.Max(6, Int32.Parse(ControlDictionary["fontSizeComboBox"].Text));
} catch (Exception) {}
FontDescriptor fontDescriptor = (FontDescriptor)fontListComboBox.Items[fontListComboBox.SelectedIndex];
return new Font(fontDescriptor.Name,
fontSize);
} }
} }
void UpdateFontPreviewLabel(object sender, EventArgs e) void UpdateFontPreviewLabel(object sender, EventArgs e)
{ {
Font currentFont = CurrentFont; helper.UpdateFontPreviewLabel(ControlDictionary["fontPreviewLabel"]);
ControlDictionary["fontPreviewLabel"].Visible = currentFont != null;
if (currentFont != null) {
ControlDictionary["fontPreviewLabel"].Font = currentFont;
}
} }
public override bool StorePanelContents() public override bool StorePanelContents()

Loading…
Cancel
Save