Mercurial > projects > dynamin
annotate dynamin/gui/text_box.d @ 8:b621b528823d
whenXX methods have to come before the event if switched to template mixins.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Wed, 15 Jul 2009 14:04:55 -0500 |
parents | aa4efef0f0b1 |
children | ccc108b25a0a |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2007-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.gui.text_box; | |
27 | |
28 import dynamin.all_painting; | |
29 import dynamin.all_core; | |
30 import dynamin.all_gui; | |
31 | |
32 /** | |
33 * A control that allows text to be entered and edited. | |
34 * | |
35 * The appearance of a text box with Windows Classic: | |
36 * | |
37 * $(IMAGE ../web/example_text_box.png) | |
38 */ | |
39 class TextBox : Scrollable { | |
40 protected: | |
41 string _text; | |
42 int _caret = 0; | |
43 override Size contentBestSize() { | |
44 return Size(0, 0); | |
45 } | |
46 override void whenContentPainting(PaintingEventArgs e) { | |
47 with(e.graphics) { | |
48 source = Color.White; | |
49 paint(); | |
50 source = Color.Black; | |
51 drawText(_text, 1, 1); | |
52 if(focused) { | |
53 Size ex = getTextExtents(_text[0.._caret]); | |
54 moveTo(1+ex.width+0.5, 1); | |
55 lineTo(1+ex.width+0.5, 14); | |
56 stroke(); | |
57 } | |
58 } | |
59 } | |
60 override void whenContentMouseDown(MouseEventArgs e) { | |
61 focus(); | |
62 repaint(); | |
63 } | |
64 override void whenKeyDown(KeyEventArgs e) { | |
65 switch(e.key) { | |
66 case Key.Backspace: | |
67 if(_caret > 0) | |
68 _text = _text[0.._caret-1] ~ _text[_caret..$]; | |
69 if(_caret > 0) | |
70 _caret--; | |
71 break; | |
72 case Key.Delete: | |
73 if(_text.length > _caret) | |
74 _text = _text[0.._caret] ~ _text[_caret+1..$]; | |
75 break; | |
76 case Key.Right: | |
77 if(_caret < _text.length) | |
78 _caret++; | |
79 break; | |
80 case Key.Left: | |
81 if(_caret > 0) | |
82 _caret--; | |
83 break; | |
84 case Key.Home: | |
85 _caret = 0; | |
86 break; | |
87 case Key.End: | |
88 _caret = _text.length; | |
89 break; | |
90 default: | |
91 return; | |
92 } | |
93 repaint(); | |
94 } | |
95 override void whenKeyTyped(KeyTypedEventArgs e) { | |
96 _text = format("{}{}{}", _text[0.._caret], e.character, _text[_caret..$]); | |
97 _caret++; | |
98 repaint(); | |
99 } | |
100 override void whenMouseDown(MouseEventArgs e) { | |
101 focus(); | |
102 } | |
103 public: | |
104 override Size bestSize() { | |
105 // TODO: columns and rows | |
106 return Size(100, 20); | |
107 } | |
108 override int baseline() { return 14; } | |
109 | |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
110 /// Override this method in a subclass to handle the SelectionChanged event. |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
111 protected void whenSelectionChanged(EventArgs e) { } |
0 | 112 /// This event occurs after the selection has changed. |
113 Event!() SelectionChanged; | |
114 | |
115 this() { | |
116 SelectionChanged = new Event!()(&whenSelectionChanged); | |
117 | |
118 super(); | |
119 _focusable = true; | |
120 content.cursor = Cursor.Text; | |
121 | |
122 // TODO: change if Multiline is added | |
123 visibleScrollBars = VisibleScrollBars.None; | |
124 elasticX = true; | |
125 elasticY = false; | |
126 } | |
127 } | |
128 |