Debian Codegraph @

phoenix/windows/widget/text-edit.cpp

raw
1 namespace phoenix {
2
3 void pTextEdit::setCursorPosition(unsigned position) {
4 if(position == ~0) position >>= 1; //Edit_SetSel takes signed type
5 Edit_SetSel(hwnd, position, position);
6 Edit_ScrollCaret(hwnd);
7 }
8
9 void pTextEdit::setEditable(bool editable) {
10 SendMessage(hwnd, EM_SETREADONLY, editable == false, (LPARAM)0);
11 }
12
13 void pTextEdit::setText(string text) {
14 locked = true;
15 string output = text;
16 output.replace("\r", "");
17 output.replace("\n", "\r\n");
18 SetWindowText(hwnd, utf16_t(output));
19 locked = false;
20 }
21
22 void pTextEdit::setWordWrap(bool wordWrap) {
23 //ES_AUTOHSCROLL cannot be changed after widget creation.
24 //As a result, we must destroy and re-create widget to change this setting.
25 orphan();
26 }
27
28 string pTextEdit::text() {
29 unsigned length = GetWindowTextLength(hwnd);
30 wchar_t buffer[length + 1];
31 GetWindowText(hwnd, buffer, length + 1);
32 buffer[length] = 0;
33 string text = (const char*)utf8_t(buffer);
34 text.replace("\r", "");
35 return text;
36 }
37
38 void pTextEdit::constructor() {
39 hwnd = CreateWindowEx(
40 WS_EX_CLIENTEDGE, L"EDIT", L"",
41 WS_CHILD | WS_TABSTOP | WS_VSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | ES_WANTRETURN | (textEdit.state.wordWrap == false ? WS_HSCROLL | ES_AUTOHSCROLL : 0),
42 0, 0, 0, 0, parentHwnd, (HMENU)id, GetModuleHandle(0), 0
43 );
44 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&textEdit);
45 setDefaultFont();
46 setCursorPosition(textEdit.state.cursorPosition);
47 setEditable(textEdit.state.editable);
48 setText(textEdit.state.text);
49 synchronize();
50 }
51
52 void pTextEdit::destructor() {
53 textEdit.state.text = text();
54 DestroyWindow(hwnd);
55 }
56
57 void pTextEdit::orphan() {
58 destructor();
59 constructor();
60 }
61
62 void pTextEdit::onChange() {
63 if(locked) return;
64 if(textEdit.onChange) textEdit.onChange();
65 }
66
67 }