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; @@ -9,8 +9,9 @@ using System;
using System.IO;
using System.Drawing;
using System.Drawing.Text;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using ICSharpCode.SharpDevelop.Internal.ExternalTool;
using ICSharpCode.Core;
@ -34,31 +35,27 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -34,31 +35,27 @@ namespace ICSharpCode.SharpDevelop.Gui
public Font CurrentFont {
get {
int fontSize = 10;
try {
fontSize = Math.Max(6, Int32.Parse(ControlDictionary["fontSizeComboBox"].Text));
} 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);
if (helper == null)
return null;
return helper.GetSelectedFont();
}
set {
int index = 0;
for (int i = 0; i < ((ComboBox)ControlDictionary["fontListComboBox"]).Items.Count; ++i) {
FontDescriptor descriptor = (FontDescriptor)((ComboBox)ControlDictionary["fontListComboBox"]).Items[i];
if (descriptor.Name == value.Name) {
index = i;
if (helper == null) {
helper = new FontSelectionPanelHelper((ComboBox)ControlDictionary["fontSizeComboBox"], (ComboBox)ControlDictionary["fontListComboBox"], value);
helper.StartThread();
((ComboBox)ControlDictionary["fontListComboBox"]).MeasureItem += helper.MeasureComboBoxItem;
((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["fontListComboBox"]).SelectedIndex = index;
UpdateFontPreviewLabel(this, EventArgs.Empty);
}
}
@ -66,38 +63,18 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -66,38 +63,18 @@ namespace ICSharpCode.SharpDevelop.Gui
{
SetupFromXmlStream(this.GetType().Assembly.GetManifestResourceStream("Resources.FontSelectionPanel.xfrm"));
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
for (int i = 6; i <= 24; ++i) {
((ComboBox)ControlDictionary["fontSizeComboBox"]).Items.Add(i);
}
((ComboBox)ControlDictionary["fontSizeComboBox"]).TextChanged += new EventHandler(UpdateFontPreviewLabel);
foreach (FontFamily fontFamily in installedFontCollection.Families) {
if (fontFamily.IsStyleAvailable(FontStyle.Regular) && fontFamily.IsStyleAvailable(FontStyle.Bold) && fontFamily.IsStyleAvailable(FontStyle.Italic)) {
((ComboBox)ControlDictionary["fontListComboBox"]).Items.Add(new FontDescriptor(fontFamily));
}
}
((ComboBox)ControlDictionary["fontSizeComboBox"]).Enabled = false;
((ComboBox)ControlDictionary["fontListComboBox"]).Enabled = false;
((ComboBox)ControlDictionary["fontListComboBox"]).TextChanged += 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)
{
@ -110,25 +87,107 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -110,25 +87,107 @@ namespace ICSharpCode.SharpDevelop.Gui
}
}
static StringFormat drawStringFormat = new StringFormat(StringFormatFlags.NoWrap);
static Font boldComboBoxFont;
void UpdateFontPreviewLabel(object sender, EventArgs e)
{
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;
e.DrawBackground();
if (e.Index >= 0) {
FontDescriptor fontDescriptor = (FontDescriptor)comboBox.Items[e.Index];
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;
}
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);
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,
fontDescriptor.IsMonospaced ? boldComboBoxFont : comboBox.Font,
drawItemBrush,
@ -138,50 +197,57 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -138,50 +197,57 @@ namespace ICSharpCode.SharpDevelop.Gui
e.DrawFocusRectangle();
}
class FontDescriptor
public Font GetSelectedFont()
{
FontFamily fontFamily;
bool isMonospaced = false;
bool initializedMonospace = false;
public string Name {
get {
return fontFamily.Name;
}
}
if (!fontListComboBox.Enabled)
return null;
int fontSize = 10;
try {
fontSize = Math.Max(6, Int32.Parse(fontSizeComboBox.Text));
} catch (Exception) {}
public bool IsMonospaced {
get {
if (!initializedMonospace) {
isMonospaced = GetIsMonospaced(fontFamily);
}
return isMonospaced;
}
}
FontDescriptor fontDescriptor = (FontDescriptor)fontListComboBox.Items[fontListComboBox.SelectedIndex];
bool GetIsMonospaced(FontFamily fontFamily)
{
using (Bitmap newBitmap = new Bitmap(1, 1)) {
using (Graphics g = Graphics.FromImage(newBitmap)) {
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 = (int)g.MeasureString("i.", f).Width;
int w2 = (int)g.MeasureString("mw", f).Width;
return w1 == w2;
}
}
}
return new Font(fontDescriptor.Name,
fontSize);
}
public void UpdateFontPreviewLabel(Control fontPreviewLabel)
{
Font currentFont = GetSelectedFont();
fontPreviewLabel.Visible = currentFont != null;
if (currentFont != null) {
fontPreviewLabel.Font = currentFont;
}
}
public class FontDescriptor
{
FontFamily fontFamily;
internal string Name;
internal bool IsMonospaced;
public FontDescriptor(FontFamily fontFamily)
{
this.fontFamily = fontFamily;
this.Name = fontFamily.Name;
}
}
void UpdateFontPreviewLabel(object sender, EventArgs e)
{
ControlDictionary["fontPreviewLabel"].Font = CurrentFont;
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;
}
}
}
}
}

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

@ -31,36 +31,8 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.OptionPanels @@ -31,36 +31,8 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.OptionPanels
{
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;
FontSelectionPanelHelper helper;
public override void LoadPanelContents()
{
@ -92,134 +64,33 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.OptionPanels @@ -92,134 +64,33 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.OptionPanels
for (int i = 6; i <= 24; ++i) {
fontSizeComboBox.Items.Add(i);
}
fontSizeComboBox.TextChanged += new EventHandler(UpdateFontPreviewLabel);
fontSizeComboBox.Enabled = false;
fontListComboBox.Enabled = false;
fontListComboBox.TextChanged += 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,
e.Bounds.Y,
e.Bounds.Width,
e.Bounds.Height);
Font currentFont = FontSelectionPanel.ParseFont(((Properties)CustomizationObject).Get("DefaultFont", ResourceService.DefaultMonospacedFont.ToString()).ToString());
helper = new FontSelectionPanelHelper(fontSizeComboBox, fontListComboBox, currentFont);
Brush drawItemBrush = SystemBrushes.WindowText;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
drawItemBrush = SystemBrushes.HighlightText;
}
fontListComboBox.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(helper.MeasureComboBoxItem);
fontListComboBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(helper.ComboBoxDrawItem);
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,
fontDescriptor.IsMonospaced ? boldComboBoxFont : comboBox.Font,
drawItemBrush,
drawingRect,
drawStringFormat);
}
e.DrawFocusRectangle();
UpdateFontPreviewLabel(null, null);
helper.StartThread();
}
Font CurrentFont {
get {
if (!fontListComboBox.Enabled)
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);
return helper.GetSelectedFont();
}
}
void UpdateFontPreviewLabel(object sender, EventArgs e)
{
Font currentFont = CurrentFont;
ControlDictionary["fontPreviewLabel"].Visible = currentFont != null;
if (currentFont != null) {
ControlDictionary["fontPreviewLabel"].Font = currentFont;
}
helper.UpdateFontPreviewLabel(ControlDictionary["fontPreviewLabel"]);
}
public override bool StorePanelContents()

Loading…
Cancel
Save