OSDN Git Service

34be64d99cae83eda5b08ebcc990e2d07d44fb49
[radegast/radegast.git] / Radegast / Core / Types / LineNumberPanel.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2013, Radegast Development Team
4 // All rights reserved.
5 // 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 
9 //     * Redistributions of source code must retain the above copyright notice,
10 //       this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above copyright
12 //       notice, this list of conditions and the following disclaimer in the
13 //       documentation and/or other materials provided with the distribution.
14 //     * Neither the name of the application "Radegast", nor the names of its
15 //       contributors may be used to endorse or promote products derived from
16 //       this software without specific prior written permission.
17 // 
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // $Id$
30 //
31 using System;
32 using System.Drawing;
33 using System.Windows.Forms;
34 using System.ComponentModel;
35
36 namespace Radegast
37 {
38     /// <summary>
39     /// Used in conjuction with ritch text box to display lone numbers
40     /// </summary>
41     [Browsable(true),
42         Category("Containers"),
43         Description("Used in conjuction with ritch text box to display lone numbers")]
44     public partial class LineNumberPanel : Control
45     {
46         private RRichTextBox rtb;
47         private SolidBrush brush;
48
49         /// <summary>
50         /// Associated ritch text box control
51         /// </summary>
52         [Category("Behavior")]
53         public RRichTextBox RTB
54         {
55             get
56             {
57                 if (rtb == null)
58                 {
59                     return new RRichTextBox();
60                 }
61                 else
62                 {
63                     return rtb;
64                 }
65             }
66
67             set
68             {
69                 if (rtb != null)
70                 {
71                     rtb.VScroll -= new EventHandler(rtb_InvalidateNumbers);
72                     rtb.TextChanged -= new EventHandler(rtb_InvalidateNumbers);
73                     rtb.Resize -= new EventHandler(rtb_InvalidateNumbers);
74                 }
75                 rtb = value;
76                 rtb.VScroll += new EventHandler(rtb_InvalidateNumbers);
77                 rtb.TextChanged += new EventHandler(rtb_InvalidateNumbers);
78                 rtb.Resize += new EventHandler(rtb_InvalidateNumbers);
79             }
80         }
81
82         void rtb_InvalidateNumbers(object sender, EventArgs e)
83         {
84             Invalidate();
85         }
86
87         public LineNumberPanel()
88             : base()
89         {
90             SetStyle(
91                 ControlStyles.DoubleBuffer |
92                 ControlStyles.UserPaint |
93                 ControlStyles.AllPaintingInWmPaint,
94                 true
95             );
96
97             UpdateStyles();
98             brush = new SolidBrush(ForeColor);
99         }
100
101         protected override void OnPaint(PaintEventArgs e)
102         {
103             if (rtb == null)
104             {
105                 return;
106             }
107
108             float font_height;
109             Graphics g = e.Graphics;
110             brush.Color = ForeColor;
111             if (rtb.Lines.Length < 3)
112             {
113                 font_height = rtb.Font.Height + 1.5f;
114             }
115             else
116             {
117                 font_height = rtb.GetPositionFromCharIndex(rtb.GetFirstCharIndexFromLine(2)).Y - rtb.GetPositionFromCharIndex(rtb.GetFirstCharIndexFromLine(1)).Y;
118             }
119
120             Point pos = new Point(0, (int)(g.VisibleClipBounds.Y + font_height / 3));
121             int first_index, first_line, first_line_y;
122             first_index = rtb.GetCharIndexFromPosition(pos);
123             first_line = rtb.GetLineFromCharIndex(first_index);
124             first_line_y = 1 + rtb.GetPositionFromCharIndex(first_index).Y;
125
126             int i = first_line;
127             int x = 0;
128             Single y = 0;
129             int total_lines = rtb.GetLineFromCharIndex(Int32.MaxValue) + 1;
130             StringFormat align = new StringFormat(StringFormatFlags.NoWrap);
131             align.Alignment = StringAlignment.Far;
132             align.LineAlignment = StringAlignment.Center;
133             int maxWidth = 0;
134
135             while (y < g.VisibleClipBounds.Y + g.VisibleClipBounds.Height)
136             {
137                 SizeF tSize = g.MeasureString(i.ToString(), rtb.Font);
138
139                 if (maxWidth < (int)tSize.Width)
140                 {
141                     maxWidth = (int)tSize.Width;
142                 }
143
144                 y = first_line_y - 1 + font_height * (i - first_line - 1);
145                 x = Width - (int)tSize.Width - 5;
146
147                 if (i <= total_lines)
148                 {
149                     g.DrawString((i).ToString(), rtb.Font, brush, x, y);
150                 }
151                 i++;
152             }
153
154             maxWidth++;
155
156             if (Width != (maxWidth + 13))
157             {
158                 Width = maxWidth + 13;
159                 Invalidate();
160             }
161    
162         }
163
164         protected override void OnPaintBackground(PaintEventArgs pevent)
165         {
166             pevent.Graphics.Clear(BackColor);
167         }
168     }
169 }