File: text-edit.cpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (119 lines) | stat: -rw-r--r-- 3,732 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#if defined(Hiro_TextEdit)

namespace hiro {

auto pTextEdit::construct() -> void {
  hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE, L"EDIT", L"",
    WS_CHILD | WS_TABSTOP | WS_VSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | ES_WANTRETURN | (!state().wordWrap ? WS_HSCROLL | ES_AUTOHSCROLL : 0),
    0, 0, 0, 0, _parentHandle(), nullptr, GetModuleHandle(0), 0
  );
  pWidget::construct();
  setBackgroundColor(state().backgroundColor);
  setEditable(state().editable);
  setText(state().text);
  setTextCursor(state().textCursor);
}

auto pTextEdit::destruct() -> void {
  state().text = text();
  if(backgroundBrush) { DeleteObject(backgroundBrush); backgroundBrush = 0; }
  DestroyWindow(hwnd);
}

auto pTextEdit::setBackgroundColor(Color color) -> void {
  if(backgroundBrush) { DeleteObject(backgroundBrush); backgroundBrush = 0; }
  backgroundBrush = CreateSolidBrush(color ? CreateRGB(color) : GetSysColor(COLOR_WINDOW));
  InvalidateRect(hwnd, 0, true);
}

auto pTextEdit::setEditable(bool editable) -> void {
  SendMessage(hwnd, EM_SETREADONLY, editable == false, (LPARAM)0);
}

auto pTextEdit::setForegroundColor(Color color) -> void {
  InvalidateRect(hwnd, 0, true);
}

auto pTextEdit::setText(string text) -> void {
  auto lock = acquire();
  text.replace("\r", "");
  text.replace("\n", "\r\n");
  SetWindowText(hwnd, utf16_t(text));
}

auto pTextEdit::setTextCursor(TextCursor cursor) -> void {
  s32 end = GetWindowTextLength(hwnd);
  s32 offset = max(0, min(end, cursor.offset()));
  s32 length = max(0, min(end, cursor.offset() + cursor.length()));
  Edit_SetSel(hwnd, offset, length);
  Edit_ScrollCaret(hwnd);
}

auto pTextEdit::setWordWrap(bool wordWrap) -> void {
  //ES_AUTOHSCROLL cannot be changed after widget creation.
  //As a result, we must destroy and re-create widget to change this setting.
  reconstruct();
}

auto pTextEdit::text() const -> string {
  u32 length = GetWindowTextLength(hwnd);
  wchar_t buffer[length + 1];
  GetWindowText(hwnd, buffer, length + 1);
  buffer[length] = 0;
  string text = (const char*)utf8_t(buffer);
  text.replace("\r", "");
  return text;
}

auto pTextEdit::textCursor() const -> TextCursor {
  return state().textCursor;
}

//

auto pTextEdit::onChange() -> void {
  if(!locked()) self().doChange();
}

auto pTextEdit::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> maybe<LRESULT> {
  if(msg == WM_KEYDOWN) {
    if(wparam == 'A' && GetKeyState(VK_CONTROL) < 0) {
      //Ctrl+A = select all text
      //note: this is not a standard accelerator on Windows
      Edit_SetSel(hwnd, 0, ~0);
      return true;
    } else if(wparam == 'V' && GetKeyState(VK_CONTROL) < 0) {
      //Ctrl+V = paste text
      //note: this formats Unix (LF) and OS9 (CR) line-endings to Windows (CR+LF) line-endings
      //this is necessary as the EDIT control only supports Windows line-endings
      OpenClipboard(hwnd);
      if(auto handle = GetClipboardData(CF_UNICODETEXT)) {
        if(auto text = (wchar_t*)GlobalLock(handle)) {
          string data = (const char*)utf8_t(text);
          data.replace("\r\n", "\n");
          data.replace("\r", "\n");
          data.replace("\n", "\r\n");
          GlobalUnlock(handle);
          utf16_t output(data);
          if(auto resource = GlobalAlloc(GMEM_MOVEABLE, (wcslen(output) + 1) * sizeof(wchar_t))) {
            if(auto write = (wchar_t*)GlobalLock(resource)) {
              wcscpy(write, output);
              GlobalUnlock(write);
              if(SetClipboardData(CF_UNICODETEXT, resource) == nullptr) {
                GlobalFree(resource);
              }
            }
          }
        }
      }
      CloseClipboard();
    }
  }

  return pWidget::windowProc(hwnd, msg, wparam, lparam);
}

}

#endif