Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Controls/ILBorderFitLabel.cs @ 9407

Last change on this file since 9407 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 8.4 KB
RevLine 
[9102]1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections.Generic;
42using System.Text;
43using System.Windows.Forms;
44using System.Drawing;
45
46namespace ILNumerics.Drawing {
47    /// <summary>
48    /// Label with flexible configurable orientation of it's text
49    /// </summary>
50    public class ILBorderFitLabel : ILMovingDockPanel {
51        private SizeF m_textSize = SizeF.Empty;
52        private ContextMenu m_menu;
53        private ILTextBoxAutoHide m_textBox;
54
55        private void autoSize() {
56            // sets the height or width according to orientation & font
57            if (m_orientation == TextOrientation.Vertical) {
58                Width = Font.Height + Padding.Left + Padding.Right;
59            } else {
60                Height = Font.Height + Padding.Top + Padding.Bottom;
61            }
62        }
63
64        private string m_text;
65        public override string Text {
66            get {
67                return m_text;
68            }
69            set {
70                m_text = value;
71                Invalidate();
72            }
73        }
74
75        /// <summary>
76        /// The labels text
77        /// </summary>
78        public string Caption {
79            get { return m_text; }
80            set {
81                m_text = value;
82                Invalidate();
83            }
84        }
85        private VerticalAlignment m_valign;
86        /// <summary>
87        /// vertical alignment for the text
88        /// </summary>
89        public VerticalAlignment VerticalAlign {
90            get {
91                return m_valign;
92            }
93            set {
94                m_valign = value;
95                Invalidate();
96           }
97        }
98        private HorizontalAlignment m_halign = HorizontalAlignment.Center;
99        /// <summary>
100        /// horizontal alignment for the text
101        /// </summary>
102        public HorizontalAlignment HorizontalAlign {
103            get {
104                return m_halign;
105            }
106            set {
107                m_halign = value;
108                Invalidate();
109            }
110        }
111
112        protected override void OnPaint(PaintEventArgs e) {
113            base.OnPaint(e);
114            PointF pointF = new PointF();
115            StringFormat sf;
116            if (m_orientation == TextOrientation.Vertical)
117                sf = new StringFormat(StringFormatFlags.DirectionVertical);
118            else
119                sf = new StringFormat();
120            m_textSize = e.Graphics.MeasureString(m_text,Font,pointF,sf);
121            #region compute horizontal position
122            switch (m_halign) {
123                case HorizontalAlignment.Center:
124                    pointF.X = (ClientSize.Width - m_textSize.Width - Padding.Horizontal) / 2 - 1;
125                    break;
126                case HorizontalAlignment.Left:
127                    pointF.X = Padding.Left;
128                    break;
129                case HorizontalAlignment.Right:
130                    pointF.X = ClientSize.Width - Padding.Right - m_textSize.Width - 1;
131                    break;
132                default:
133                    break;
134            }
135            if (m_orientation == TextOrientation.Vertical)
136                pointF.X += Font.Height;
137            #endregion
138            #region compute vertical position
139            switch (m_valign) {
140                case VerticalAlignment.Middle:
141                    pointF.Y = (ClientSize.Height - m_textSize.Height - Padding.Top - Padding.Bottom) / 2;
142                    break;
143                case VerticalAlignment.Upper:
144                    pointF.Y = Padding.Top;
145                    break;
146                case VerticalAlignment.Lower:
147                    pointF.Y = ClientSize.Height - Padding.Bottom - m_textSize.Height;
148                    break;
149                default:
150                    break;
151            }
152            #endregion
153            SolidBrush solidBrush = new SolidBrush(ForeColor);
154            e.Graphics.DrawString(m_text, this.Font, solidBrush, pointF, sf);
155        }
156        protected override void OnResize(EventArgs eventargs) {
157            base.OnResize(eventargs);
158            Invalidate();
159        }
160        public ILBorderFitLabel() {
161            m_text = "Title";
162            m_orientation = TextOrientation.Horizontal;
163            m_halign = HorizontalAlignment.Center;
164            m_valign = VerticalAlignment.Middle;
165            Height = Font.Height + Padding.Top + Padding.Bottom;
166            Width = 150;
167            this.Padding = new Padding(2);
168            this.ForeColor = Color.White;
169            m_menu = new ContextMenu();
170            MenuItem mi = new MenuItem("Edit",new EventHandler(Edit_Click));
171            m_menu.MenuItems.Add(mi);
172            mi.DefaultItem = true;
173            mi.Shortcut = Shortcut.CtrlE;
174            mi.ShowShortcut = true;
175           
176        }
177        protected void Edit_Click(object sender, EventArgs e) {
178            EditShow();
179        }
180        protected override void OnMouseClick(MouseEventArgs e) {
181            base.OnMouseClick(e);
182            if (e.Button == MouseButtons.Right)  {
183                m_menu.Show(this,e.Location);
184            }
185        }
186        private void EditShow() {
187            if (m_textBox == null) {
188                m_textBox = new ILTextBoxAutoHide();
189                if (Parent != null)
190                    Parent.Controls.Add(m_textBox);
191                m_textBox.Committing += new EventHandler(m_textBox_Committing);
192            }
193            m_textBox.Text = m_text;
194            if (m_orientation == TextOrientation.Horizontal) {
195                m_textBox.Size = Size;
196                m_textBox.Left = Location.X;
197                m_textBox.Top = Top + (Height - m_textBox.Height) / 2;
198            } else {
199                if (Parent != null) {
200                    m_textBox.Top = Parent.Padding.Top;
201                    m_textBox.Left = Parent.Padding.Left;
202                    m_textBox.Width = Parent.Width - Parent.Padding.Horizontal;
203                    m_textBox.Height = Parent.Height - Parent.Padding.Vertical;
204                }
205            }
206            m_textBox.Font = Font;
207            m_textBox.Show();
208            m_textBox.BringToFront();
209            m_textBox.Focus();
210            m_textBox.SelectAll();
211        }
212
213        void m_textBox_Committing(object sender, EventArgs e) {
214            m_text = m_textBox.Text;
215            Invalidate();
216        }
217    }
218}
Note: See TracBrowser for help on using the repository browser.