Click here to Skip to main content
15,881,600 members
Articles / Desktop Programming / Windows Forms

Storm - the world's best IDE framework for .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (82 votes)
4 Feb 2010LGPL311 min read 273.7K   6.5K   340  
Create fast, flexible, and extensible IDE applications easily with Storm - it takes nearly no code at all!
//
//    ___ _____ ___  ___ __  __ 
//   / __|_   _/ _ \| _ \  \/  |
//   \__ \ | || (_) |   / |\/| |
//   |___/ |_| \___/|_|_\_|  |_|
// 
//   Storm.TextEditor.dll
//   ��������������������
//     Storm.TabControl.dll was created under the LGPL 
//     license. Some of the code was created from scratch, 
//     some was not. Code not created from scratch was 
//     based on the DotNetFireball framework and evolved 
//     from that. 
//     
//     What I mostly did in this library was that I 
//     cleaned up the code, structured it, documentated 
//     it and added new features. 
//     
//     Although it might not sound like it, it was very
//     hard and took a long (pretty much a shitload)
//     time. Why? Well, this was* some of the crappiest,
//     most unstructured, undocumentated, ugly code I've
//     ever seen. It would actually have taken me less 
//     time to create it from scratch, but I figured that
//     out too late. Figuring out what the code actually
//     did and then documentating it was (mostly) a 
//     day-long process. Well, I hope you enjoy some of
//     my hard work. /rant
//     
//     Of course some/most of it is made from scratch by me.
//     *Yes, was. It isn't now. :) Some of the naming 
//     conventions might still seem a little bit off though.
//
//   What is Storm?
//   ��������������
//     Storm is a set of dynamic link libraries used in 
//     Theodor "Vestras" Storm Kristensen's application 
//     "Moonlite".
//     
//
//   Thanks:
//   �������
//     - The DotNetFireball team for creating and publishing 
//       DotNetFireball which I based some of my code on.
//
//
//   Copyright (c) Theodor "Vestras" Storm Kristensen 2009
//   �����������������������������������������������������
//


namespace Storm.TextEditor.Controls.Core
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Diagnostics.Design;
    using System.Diagnostics.SymbolStore;
    using System.IO;
    using System.Reflection;
    using System.Resources;
    using System.Text;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    using Storm.TextEditor;
    using Storm.TextEditor.Controls;
    using Storm.TextEditor.Controls.Core;
    using Storm.TextEditor.Controls.Core.Globalization;
    using Storm.TextEditor.Controls.Core.Timers;
    using Storm.TextEditor.Controls.IntelliMouse;
    using Storm.TextEditor.Document;
    using Storm.TextEditor.Document.Exporters;
    using Storm.TextEditor.Forms;
    using Storm.TextEditor.Interacting;
    using Storm.TextEditor.Painting;
    using Storm.TextEditor.Parsing;
    using Storm.TextEditor.Parsing.Base;
    using Storm.TextEditor.Parsing.Classes;
    using Storm.TextEditor.Parsing.Language;
    using Storm.TextEditor.Preset;
    using Storm.TextEditor.Preset.Painting;
    using Storm.TextEditor.Preset.TextDraw;
    using Storm.TextEditor.Printing;
    using Storm.TextEditor.Utilities;
    using Storm.TextEditor.Win32;

    #region FontList class
    public class FontList : UITypeEditor
    {
        #region Members
        // Basic members.
        private bool             handleLostfocus = false;
        private IWindowsFormsEditorService edSvc = null;
        private ListBox              FontListbox = null;
        #endregion

        #region Methods
        private void LB_DrawItem(object sender, DrawItemEventArgs e)
        {
            bool selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;

            if (e.Index == -1)
                return;


            object li = FontListbox.Items[e.Index];
            string text = li.ToString();
            Brush bg;
            Brush fg;

            if (selected)
            {
                bg = SystemBrushes.Highlight;
                fg = SystemBrushes.HighlightText;
            }
            else
            {
                bg = SystemBrushes.Window;
                fg = SystemBrushes.WindowText;
            }
			
            if (selected)
            {
                int ofs = 37;
                e.Graphics.FillRectangle(SystemBrushes.Window, new Rectangle
                    (ofs, e.Bounds.Top, e.Bounds.Width - ofs, FontListbox.ItemHeight));

                e.Graphics.FillRectangle(SystemBrushes.Highlight, new Rectangle
                    (ofs + 1, e.Bounds.Top + 1, e.Bounds.Width - ofs - 2, 
                    FontListbox.ItemHeight - 2));

                System.Windows.Forms.ControlPaint.DrawFocusRectangle(e.Graphics, 
                    new Rectangle(ofs, e.Bounds.Top, e.Bounds.Width - ofs, 
                    FontListbox.ItemHeight));
            }
            else
                e.Graphics.FillRectangle(SystemBrushes.Window, 0, e.Bounds.Top, 
                    e.Bounds.Width, FontListbox.ItemHeight);


            e.Graphics.DrawString(text, e.Font, fg, 38, e.Bounds.Top + 4);
            e.Graphics.SetClip(new Rectangle(1, e.Bounds.Top + 2, 34, 
                               FontListbox.ItemHeight - 4));
            e.Graphics.FillRectangle(SystemBrushes.Highlight, 
                                     new Rectangle(1, e.Bounds.Top + 2, 34, 
                                     FontListbox.ItemHeight - 4));

            IntPtr hdc = e.Graphics.GetHdc();
            GDIFont gf = new GDIFont(text, 9);

            int a = 0;
            IntPtr res = NativeGdi32Api.SelectObject(hdc, gf._hFont);

            NativeGdi32Api.SetTextColor(hdc, ColorTranslator.ToWin32
                                        (SystemColors.Window));

            NativeGdi32Api.SetBkMode(hdc, 0);
            NativeUser32Api.TabbedTextOut(hdc, 3, e.Bounds.Top + 5, 
                                          "abc", 3, 0, ref a, 0);

            NativeGdi32Api.SelectObject(hdc, res);

            gf.Dispose();
            e.Graphics.ReleaseHdc(hdc);
            e.Graphics.DrawRectangle(Pens.Black, 
                new Rectangle(1, e.Bounds.Top + 2, 34, 
                FontListbox.ItemHeight - 4));

            e.Graphics.ResetClip();

        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context != null
                && context.Instance != null
                && provider != null)
            {
                edSvc = (IWindowsFormsEditorService)provider.GetService
                    (typeof(IWindowsFormsEditorService));
                if (edSvc != null)
                {
                    FontListbox = new ListBox();
                    FontListbox.DrawMode = DrawMode.OwnerDrawFixed;
                    FontListbox.BorderStyle = BorderStyle.None;
                    FontListbox.Sorted = true;
                    FontListbox.MouseDown += new MouseEventHandler(OnMouseDown);
                    FontListbox.DoubleClick += new EventHandler(ValueChanged);
                    FontListbox.DrawItem += new DrawItemEventHandler(LB_DrawItem);
                    FontListbox.ItemHeight = 20;
                    FontListbox.MouseMove += new MouseEventHandler(OnMouseMoved);
                    FontListbox.Height = 200;
                    FontListbox.Width = 180;

                    ICollection fonts = new FontEnum().EnumFonts();
                    foreach (string font in fonts)
                        FontListbox.Items.Add(font);

                    edSvc.DropDownControl(FontListbox);
                    if (FontListbox.SelectedItem != null)
                        return FontListbox.SelectedItem.ToString();
                }
            }

            return value;
        }


        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        { return UITypeEditorEditStyle.DropDown; }

        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (!handleLostfocus && FontListbox.ClientRectangle.Contains(FontListbox.PointToClient(new Point(e.X, e.Y))))
            {
                FontListbox.LostFocus += new EventHandler(ValueChanged);
                handleLostfocus = true;
            }
        }

        private void OnMouseMoved(object sender, MouseEventArgs e)
        {
        }

        private void ValueChanged(object sender, EventArgs e)
        {
            if (edSvc != null)
                edSvc.CloseDropDown();
        }

        public override void PaintValue(PaintValueEventArgs e)
        {
            string text = e.Value.ToString();
            Bitmap bp = new Bitmap(e.Bounds.Width, e.Bounds.Height);
            Graphics g = Graphics.FromImage(bp);
            Brush bg;
            Brush fg;

            bg = SystemBrushes.Window;
            fg = SystemBrushes.WindowText;

            g.FillRectangle(SystemBrushes.Highlight, e.Bounds);

            IntPtr hdc = g.GetHdc();
            GDIFont gf = new GDIFont(text, 9);
            int a = 0;

            IntPtr res = NativeGdi32Api.SelectObject(hdc, gf._hFont);
            NativeGdi32Api.SetTextColor(hdc, ColorTranslator.ToWin32
                                        (SystemColors.Window));

            NativeGdi32Api.SetBkMode(hdc, 0);
            NativeUser32Api.TabbedTextOut(hdc, 1, 1, "abc", 3, 0, ref a, 0);
            NativeGdi32Api.SelectObject(hdc, res);

            gf.Dispose();
            g.ReleaseHdc(hdc);
            e.Graphics.DrawImage(bp, e.Bounds.Left, e.Bounds.Top);
        }

        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        { return true; }
        #endregion
    }
    #endregion

    public class FontEnum
    {
        #region Members
        private Hashtable Fonts = null;
        #endregion

        #region Methods
        public ICollection EnumFonts()
        {
            Bitmap bmp = new Bitmap(10, 10);
            Graphics g = Graphics.FromImage(bmp);

            IntPtr hDC = g.GetHdc();
            Fonts = new Hashtable();
            LogFont lf = new LogFont();
            lf.lfCharSet = 1;
            FONTENUMPROC callback = new FONTENUMPROC(CallbackFunc);
            NativeGdi32Api.EnumFontFamiliesEx(hDC, lf, callback, 0, 0);

            g.ReleaseHdc(hDC);
            g.Dispose();
            bmp.Dispose();
            return Fonts.Keys;
        }

        private int CallbackFunc(ENUMLOGFONTEX f, int a, int b, int LParam)
        {
            Fonts[f.elfLogFont.lfFaceName] = "abc";
            return 1;
        }
        #endregion

        /// <summary>
        /// Initializes the FontEnum.
        /// </summary>
        public FontEnum()
        {
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)



Comments and Discussions