File: ScrollableLabel.cs

package info (click to toggle)
quickroute-gps 2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,576 kB
  • sloc: cs: 74,488; makefile: 72; sh: 43
file content (161 lines) | stat: -rw-r--r-- 3,705 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;


/* MinHeight
 * MaxHeight
 * 
 * 
*/

namespace QuickRoute.Controls
{
  public partial class ScrollableLabel : UserControl
  {
    private SizeF textSize;
    private int minHeight = 16;
    private int maxHeight = 32;
    private bool scrollbarVisible;
    private bool resizingNow;

    public ScrollableLabel()
    {
      InitializeComponent();
    }

    public override string Text
    {
      get
      {
        return base.Text;
      }
      set
      {
        base.Text = value;
        MeasureText();
        DrawText();
      }
    }

    public override bool AutoSize
    {
      get
      {
        return base.AutoSize;
      }
      set
      {
        base.AutoSize = value;
      }
    }

    public int MinHeight
    {
      get { return minHeight; }
      set { minHeight = value; }
    }

    public int MaxHeight
    {
      get { return maxHeight; }
      set { maxHeight = value; }
    }

    private void DrawText()
    {
      Graphics g = CreateGraphics();
      g.Clear(BackColor);
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
      g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
      RectangleF rect = new RectangleF(
        (float)Padding.Left,
        (float)(Padding.Top - (scrollbarVisible ? scrollbar.Value : 0)),
        textSize.Width,
        textSize.Height);
      Brush b = new SolidBrush(ForeColor);
      g.DrawString(Text, Font, b, rect);
      b.Dispose();
      g.Dispose();
    }

    private void MeasureText()
    {
      Graphics g = CreateGraphics();
      // measure the string
      // without scrollbar
      SizeF characterSize = g.MeasureString("A", Font);
      SizeF textSizeWithoutScrollbar = g.MeasureString(Text, Font, Width - Padding.Horizontal);
      // with scrollbar
      SizeF textSizeWithScrollbar = g.MeasureString(Text, Font, Width - Padding.Horizontal - scrollbar.Width);

      // do we need to use a scrollbar?
      scrollbarVisible = (int)textSizeWithoutScrollbar.Height > maxHeight;
      if (scrollbarVisible)
      {
        textSize = textSizeWithScrollbar;
      }
      else
      {
        textSize = textSizeWithoutScrollbar;
      }

      int proposedHeight = Padding.Vertical + (int)Math.Ceiling(textSize.Height);
      proposedHeight = Math.Min(Math.Max(proposedHeight, minHeight), maxHeight);
      resizingNow = true;
      Height = proposedHeight;
      resizingNow = false;

      if (scrollbarVisible)
      {
        scrollbar.Maximum = (int)textSizeWithScrollbar.Height;
        scrollbar.LargeChange = maxHeight;
        scrollbar.SmallChange = (int)characterSize.Height;
      }
      
      g.Dispose();

      scrollbar.Visible = scrollbarVisible;
      scrollbar.Left = Width - scrollbar.Width;
      scrollbar.Height = Height;
    }

    private void ScrollableLabel_Paint(object sender, PaintEventArgs e)
    {
      DrawText();
    }

    private void ScrollableLabel_Resize(object sender, EventArgs e)
    {
      if (!resizingNow)
      {
        MeasureText();
        DrawText();
      }
    }

    private void scrollbar_Scroll(object sender, ScrollEventArgs e)
    {
      DrawText();
    }

    private void scrollbar_ValueChanged(object sender, EventArgs e)
    {
      DrawText();
    }

    private void ScrollableLabel_LocationChanged(object sender, EventArgs e)
    {
      DrawText();
    }




  }

}