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 dguihub.label;
10
11 import std..string;
12 import dguihub.core.controls.control;
13
14 enum LabelDrawMode : ubyte {
15 normal = 0,
16 ownerDraw = 1,
17 }
18
19 class Label : Control {
20 private LabelDrawMode _drawMode = LabelDrawMode.normal;
21 private TextAlignment _textAlign = TextAlignment.middle | TextAlignment.left;
22
23 alias @property Control.text text;
24 private bool _multiLine = false;
25
26 @property public override void text(string s) {
27 super.text = s;
28
29 this._multiLine = false;
30
31 foreach (char ch; s) {
32 if (ch == '\n' || ch == '\r') {
33 this._multiLine = true;
34 break;
35 }
36 }
37
38 if (this.created) {
39 this.invalidate();
40 }
41 }
42
43 @property public final LabelDrawMode drawMode() {
44 return this._drawMode;
45 }
46
47 @property public final void drawMode(LabelDrawMode ldm) {
48 this._drawMode = ldm;
49 }
50
51 @property public final TextAlignment alignment() {
52 return this._textAlign;
53 }
54
55 @property public final void alignment(TextAlignment ta) {
56 this._textAlign = ta;
57
58 if (this.created) {
59 this.invalidate();
60 }
61 }
62
63 protected override void createControlParams(ref CreateControlParams ccp) {
64 ccp.className = WC_DLABEL;
65 ccp.classStyle = ClassStyles.hRedraw | ClassStyles.vRedraw;
66
67 super.createControlParams(ccp);
68 }
69
70 protected override void onPaint(PaintEventArgs e) {
71 super.onPaint(e);
72
73 if (this._drawMode is LabelDrawMode.normal) {
74 Canvas c = e.canvas;
75 Rect r = Rect(nullPoint, this.clientSize);
76
77 scope TextFormat tf = new TextFormat(this._multiLine
78 ? TextFormatFlags.wordBreak : TextFormatFlags.singleLine);
79 tf.alignment = this._textAlign;
80
81 scope SolidBrush sb = new SolidBrush(this.backColor);
82 c.fillRectangle(sb, r);
83 c.drawText(this.text, r, this.foreColor, this.font, tf);
84 }
85 }
86 }