/* * BitmapFontPanel.cs - A panel that displays text with a bitmap font. * * Copyright (C) 2004 - 2011 Andreas Scherrer * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; namespace Level9 { /// /// A panel that displays text with a bitmap font. /// public class BitmapFontPanel : Panel { // -- Constants ------------------------------------------------------ /// /// The width of the borders around a title bar or status bar. /// const int BAR_XY_SPACING = 2; /// /// The width of the upper and lower borders around a text area. /// const int TEXT_Y_SPACING = 2; /// /// The width of the left and right borders around a text area. /// const int TEXT_X_SPACING = 2; // -- Attributes ----------------------------------------------------- /// /// The bitmap font. /// BitmapFont bitmapFont; /// /// The spacing between lines, in pixels. /// int lineSpacing = 2; /// /// A list of paragraphs associated with this control. /// List paragraphs; /// /// The title bar text associated with this control. /// string titleBarText; /// /// The input text color. /// Color inputColor; /// /// The title bar foreground color. /// Color titleBarForeColor; /// /// The title bar background color. /// Color titleBarBackColor; /// /// A bitmap that contains the rendered title bar text. /// Bitmap titleBitmap = null; /// /// A bitmap that contains the rendered text. /// Bitmap textBitmap = null; /// /// A renderer that draws the title bar text with a bitmap font. /// BitmapFontRenderer titleRenderer = new BitmapFontRenderer(); /// /// A renderer that draws the text with a bitmap font. /// BitmapFontRenderer fontRenderer = new BitmapFontRenderer(); // -- Constructor ---------------------------------------------------- /// /// Initializes a new instance of the BitmapFontPanel class. /// public BitmapFontPanel() { this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.ResizeRedraw, true); } // -- Methods -------------------------------------------------------- /// /// Handles the Paint event. /// /// /// a PaintEventArgs that contains the event data /// protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (bitmapFont == null) { return; } Graphics g = e.Graphics; g.InterpolationMode = InterpolationMode.NearestNeighbor; Rectangle clientRect = this.ClientRectangle; int clientWidth = this.ClientSize.Width; int clientHeight = this.ClientSize.Height; int fontHeight = bitmapFont.Height; int fontWidth = bitmapFont.Width; int barHeight = fontHeight + (BAR_XY_SPACING * 2); SolidBrush titleBarBgBrush = new SolidBrush(titleBarBackColor); Rectangle rect; int screenY = 0; // // Title bar // rect = new Rectangle(0, 0, clientWidth, barHeight); if (rect.IntersectsWith(clientRect)) { g.FillRectangle(titleBarBgBrush, rect); if (titleBarText != null && titleBarText.Length > 0) { titleBitmap = titleRenderer.RenderText(titleBarText, bitmapFont, titleBarBackColor, titleBarForeColor); if (titleBitmap != null) { g.DrawImageUnscaled(titleBitmap, BAR_XY_SPACING, BAR_XY_SPACING); } } } screenY += barHeight + TEXT_Y_SPACING; // // Text // int textWidth = clientWidth - TEXT_X_SPACING * 2; int textHeight = clientHeight - screenY - TEXT_Y_SPACING; rect = new Rectangle(TEXT_X_SPACING, screenY, textWidth, textHeight); if (base.Text != null && base.Text.Length > 0 && rect.IntersectsWith(clientRect)) { int lines = textHeight / (fontHeight + lineSpacing); int columns = textWidth / fontWidth; List outputLines = TextUtils.SplitParagraphs( paragraphs, lines, columns, false); textBitmap = fontRenderer.RenderText(outputLines, bitmapFont, lineSpacing, 0, this.BackColor, this.ForeColor, inputColor); if (textBitmap != null) { g.DrawImageUnscaled(textBitmap, TEXT_X_SPACING, screenY); } } } /// /// Releases the unmanaged resources used by the Control and its child /// controls and optionally releases the managed resources. /// /// /// true to release both managed and unmanaged resources; false to /// release only unmanaged resources. /// protected override void Dispose(bool disposing) { base.Dispose(disposing); if (titleBitmap != null) { titleBitmap.Dispose(); titleBitmap = null; } if (textBitmap != null) { textBitmap.Dispose(); textBitmap = null; } } // -- Accessors ------------------------------------------------------ /// /// Gets or sets the input text color. /// public Color InputColor { get { return inputColor; } set { inputColor = value; this.Invalidate(); } } /// /// Gets or sets the title bar foreground color. /// public Color TitleBarForeColor { get { return titleBarForeColor; } set { titleBarForeColor = value; this.Invalidate(); } } /// /// Gets or sets the title bar background color. /// public Color TitleBarBackColor { get { return titleBarBackColor; } set { titleBarBackColor = value; this.Invalidate(); } } /// /// Gets or sets the bitmap font. /// public BitmapFont BitmapFont { get { return bitmapFont; } set { bitmapFont = value; this.Invalidate(); } } /// /// Gets or sets the spacing between lines, in pixels. /// public int LineSpacing { get { return lineSpacing; } set { lineSpacing = value; this.Invalidate(); } } /// /// Gets or sets the list of paragraphs associated with this control. /// public List Paragraphs { get { return paragraphs; } set { paragraphs = value; if (value != null) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < paragraphs.Count; i++) { builder.Append(paragraphs[i].Text + "\r\n"); } base.Text = builder.ToString(); } else { base.Text = null; } this.Invalidate(); } } /// /// Gets or sets the text associated with this control. /// public override string Text { get { return base.Text; } set { if (value != null) { paragraphs = new List(); string[] temp = value.Split(new char[] {'\r', '\n'}); for (int i = 0; i < temp.Length; i++) { paragraphs.Add(new AttributedText(temp[i])); } } else { paragraphs = null; } base.Text = value; this.Invalidate(); } } /// /// Gets or sets the title bar text associated with this control. /// public string TitleBarText { get { return titleBarText; } set { titleBarText = value; this.Invalidate(); } } } }