1 /** DGui project file.
2 
3 Copyright: Trogu Antonio Davide 2011-2013
4 
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 
7 Authors: Trogu Antonio Davide
8 */
9 module dgui.label;
10 
11 import std..string;
12 import dgui.core.controls.control;
13 
14 enum LabelDrawMode: ubyte
15 {
16 	normal = 0,
17 	ownerDraw = 1,
18 }
19 
20 class Label: Control
21 {
22 	private LabelDrawMode _drawMode = LabelDrawMode.normal;
23 	private TextAlignment _textAlign = TextAlignment.middle | TextAlignment.left;
24 
25 	alias @property Control.text text;
26 	private bool _multiLine = false;
27 
28 	@property public override void text(string s)
29 	{
30 		super.text = s;
31 
32 		this._multiLine = false;
33 
34 		foreach(char ch; s)
35 		{
36 			if(ch == '\n' || ch == '\r')
37 			{
38 				this._multiLine = true;
39 				break;
40 			}
41 		}
42 
43 		if(this.created)
44 		{
45 			this.invalidate();
46 		}
47 	}
48 
49 	@property public final LabelDrawMode drawMode()
50 	{
51 		return this._drawMode;
52 	}
53 
54 	@property public final void drawMode(LabelDrawMode ldm)
55 	{
56 		this._drawMode = ldm;
57 	}
58 
59 	@property public final TextAlignment alignment()
60 	{
61 		return this._textAlign;
62 	}
63 
64 	@property public final void alignment(TextAlignment ta)
65 	{
66 		this._textAlign = ta;
67 
68 		if(this.created)
69 		{
70 			this.invalidate();
71 		}
72 	}
73 
74 	protected override void createControlParams(ref CreateControlParams ccp)
75 	{
76 		ccp.className = WC_DLABEL;
77 		ccp.classStyle = ClassStyles.hRedraw | ClassStyles.vRedraw;
78 
79 		super.createControlParams(ccp);
80 	}
81 
82 	protected override void onPaint(PaintEventArgs e)
83 	{
84 		super.onPaint(e);
85 
86 		if(this._drawMode is LabelDrawMode.normal)
87 		{
88 			Canvas c = e.canvas;
89 			Rect r = Rect(nullPoint, this.clientSize);
90 
91 			scope TextFormat tf = new TextFormat(this._multiLine ? TextFormatFlags.wordBreak : TextFormatFlags.singleLine);
92 			tf.alignment = this._textAlign;
93 
94 			scope SolidBrush sb = new SolidBrush(this.backColor);
95 			c.fillRectangle(sb, r);
96 			c.drawText(this.text, r, this.foreColor, this.font, tf);
97 		}
98 	}
99 }