1
2
3
4
5
6 using System;
7
8 namespace System.Windows
9 {
10 public enum TextBoxStyle : long
11 {
12 ES_AUTOHSCROLL = 0x0080,
13 ES_AUTOVSCROLL = 0x0040,
14 ES_CENTER = 0x0001,
15 ES_LEFT = 0x0000,
16 ES_LOWERCASE = 0x0010,
17 ES_MULTILINE = 0x0004,
18 ES_NOHIDESEL = 0x0100,
19 ES_NUMBER = 0x2000,
20 ES_OEMCONVERT = 0x0400,
21 ES_PASSWORD = 0x0020,
22 ES_READONLY = 0x0800,
23 ES_RIGHT = 0x0002,
24 ES_UPPERCASE = 0x0008,
25 ES_WANTRETURN = 0x1000
26 }
27
28 public nothrow ControlCreateParams& TextBoxControlCreateParams(ControlCreateParams& controlCreateParams, TextBoxStyle textBoxStyle)
29 {
30 return controlCreateParams.SetWindowClassName("EDIT").SetWindowClassBackgroundColor(SystemColor.COLOR_WINDOW).SetBackgroundColor(Color.White()).
31 SetWindowStyle(cast<WindowStyle>(DefaultChildWindowStyle() | WindowStyle.WS_TABSTOP | textBoxStyle));
32 }
33
34 public nothrow ControlCreateParams& TextBoxControlCreateParams(ControlCreateParams& controlCreateParams)
35 {
36 return TextBoxControlCreateParams(controlCreateParams, cast<TextBoxStyle>(TextBoxStyle.ES_LEFT | TextBoxStyle.ES_AUTOHSCROLL));
37 }
38
39 public class TextBoxCreateParams
40 {
41 public nothrow TextBoxCreateParams(ControlCreateParams& controlCreateParams_) : controlCreateParams(controlCreateParams_)
42 {
43 }
44 public nothrow TextBoxCreateParams& Defaults()
45 {
46 return *this;
47 }
48 public ControlCreateParams& controlCreateParams;
49 }
50
51 public class TextBox : Control
52 {
53 private enum Flags : sbyte
54 {
55 none = 0, selectAll = 1 << 0, multiline = 1 << 1
56 }
57
58 public TextBox(TextBoxStyle textBoxStyle, const Color& backgroundColor, const string& text, const Point& location, const Size& size,
59 Dock dock, Anchors anchors) :
60 base("EDIT", DefaultWindowClassStyle(), cast<WindowStyle>(DefaultChildWindowStyle() | WindowStyle.WS_TABSTOP | textBoxStyle),
61 DefaultExtendedWindowStyle(), backgroundColor, text, location, size, dock, anchors)
62 {
63 if ((textBoxStyle & TextBoxStyle.ES_MULTILINE) != 0)
64 {
65 SetMultiline();
66 }
67 }
68 public TextBox(const string& text, const Point& location, const Size& size, Dock dock, Anchors anchors) :
69 this(cast<TextBoxStyle>(TextBoxStyle.ES_LEFT | TextBoxStyle.ES_AUTOHSCROLL), Color.White(), text, location, size, dock, anchors)
70 {
71 }
72 public TextBox(const string& text) : this(text, Point(), Size(), Dock.none, Anchors.none)
73 {
74 }
75 public TextBox(TextBoxCreateParams& createParams) : base(createParams.controlCreateParams)
76 {
77 if ((cast<TextBoxStyle>(cast<long>(createParams.controlCreateParams.windowStyle)) & TextBoxStyle.ES_MULTILINE) != 0)
78 {
79 SetMultiline();
80 }
81 }
82 public inline nothrow bool IsMultiline() const
83 {
84 return (flags & Flags.multiline) != Flags.none;
85 }
86 private inline nothrow void SetMultiline()
87 {
88 flags = cast<Flags>(flags | Flags.multiline);
89 }
90 private inline nothrow void ResetMultiline()
91 {
92 flags = cast<Flags>(flags & ~Flags.multiline);
93 }
94 public void SelectAll()
95 {
96 if (Handle() != null)
97 {
98 WinSendMessage(Handle(), EM_SETSEL, 0u, -1);
99 }
100 else
101 {
102 SetSelectAllFlag();
103 }
104 }
105 public void Clear()
106 {
107 SetText(string());
108 }
109 protected override void OnCreated()
110 {
111 base->OnCreated();
112 Graphics graphics = Graphics.FromWindowHandle(Handle());
113 const FontHandle& fontHandle = GetFontHandle(graphics);
114 if (!fontHandle.IsNull())
115 {
116 SendSetFontMessage(fontHandle);
117 }
118 if (SelectAllFlagSet())
119 {
120 ResetSelectAllFlag();
121 SelectAll();
122 }
123 }
124 protected override void OnGotFocus()
125 {
126 base->OnGotFocus();
127 SelectAll();
128 }
129 private inline nothrow bool SelectAllFlagSet() const
130 {
131 return (flags & Flags.selectAll) != Flags.none;
132 }
133 private inline nothrow void SetSelectAllFlag()
134 {
135 flags = cast<Flags>(flags | Flags.selectAll);
136 }
137 private inline nothrow void ResetSelectAllFlag()
138 {
139 flags = cast<Flags>(flags & ~Flags.selectAll);
140 }
141 private Flags flags;
142 }
143 }