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.statusbar;
10 
11 import std.utf : toUTFz;
12 import dguihub.core.controls.subclassedcontrol;
13 
14 final class StatusPart {
15    private StatusBar _owner;
16    private string _text;
17    private int _width;
18 
19    package this(StatusBar sb, string txt, int w) {
20       this._owner = sb;
21       this._text = txt;
22       this._width = w;
23    }
24 
25    @property public string text() {
26       return this._text;
27    }
28 
29    @property public void text(string s) {
30       this._text = s;
31 
32       if (this._owner && this._owner.created) {
33          this._owner.sendMessage(SB_SETTEXTW, MAKEWPARAM(this.index, 0),
34                cast(LPARAM)toUTFz!(wchar*)(s));
35       }
36    }
37 
38    @property public int width() {
39       return this._width;
40    }
41 
42    @property public int index() {
43       foreach (int i, StatusPart sp; this._owner.parts) {
44          if (sp is this) {
45             return i;
46          }
47       }
48 
49       return -1;
50    }
51 
52    @property public StatusBar statusBar() {
53       return this._owner;
54    }
55 }
56 
57 class StatusBar : SubclassedControl {
58    private Collection!(StatusPart) _parts;
59    private bool _partsVisible = false;
60 
61    public StatusPart addPart(string s, int w) {
62       if (!this._parts) {
63          this._parts = new Collection!(StatusPart)();
64       }
65 
66       StatusPart sp = new StatusPart(this, s, w);
67       this._parts.add(sp);
68 
69       if (this.created) {
70          StatusBar.insertPart(sp);
71       }
72 
73       return sp;
74    }
75 
76    public StatusPart addPart(int w) {
77       return this.addPart(null, w);
78    }
79 
80    /*
81 	public void removePanel(int idx)
82 	{
83 
84 	}
85 	*/
86 
87    @property public bool partsVisible() {
88       return this._partsVisible;
89    }
90 
91    @property public void partsVisible(bool b) {
92       this._partsVisible = b;
93 
94       if (this.created) {
95          this.setStyle(SBARS_SIZEGRIP, b);
96       }
97    }
98 
99    @property public StatusPart[] parts() {
100       if (this._parts) {
101          return this._parts.get();
102       }
103 
104       return null;
105    }
106 
107    private static void insertPart(StatusPart stp) {
108       StatusBar owner = stp.statusBar;
109       StatusPart[] sparts = owner.parts;
110       uint[] parts = new uint[sparts.length];
111 
112       foreach (int i, StatusPart sp; sparts) {
113          if (!i) {
114             parts[i] = sp.width;
115          } else {
116             parts[i] = parts[i - 1] + sp.width;
117          }
118       }
119 
120       owner.sendMessage(SB_SETPARTS, sparts.length, cast(LPARAM)parts.ptr);
121 
122       foreach (int i, StatusPart sp; sparts) {
123          owner.sendMessage(SB_SETTEXTW, MAKEWPARAM(i, 0), cast(LPARAM)toUTFz!(wchar*)(sp.text));
124       }
125    }
126 
127    protected override void createControlParams(ref CreateControlParams ccp) {
128       this._dock = DockStyle.bottom; //Force dock
129 
130       ccp.superclassName = WC_STATUSBAR;
131       ccp.className = WC_DSTATUSBAR;
132 
133       if (this._partsVisible) {
134          this.setStyle(SBARS_SIZEGRIP, true);
135       }
136 
137       super.createControlParams(ccp);
138    }
139 
140    protected override void onHandleCreated(EventArgs e) {
141       if (this._parts) {
142          foreach (StatusPart sp; this._parts) {
143             StatusBar.insertPart(sp);
144          }
145       }
146 
147       super.onHandleCreated(e);
148    }
149 }