line-edit.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #if defined(Hiro_LineEdit)
  2. namespace hiro {
  3. auto pLineEdit::construct() -> void {
  4. hwnd = CreateWindowEx(
  5. WS_EX_CLIENTEDGE, L"EDIT", L"",
  6. WS_CHILD | WS_TABSTOP | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  7. 0, 0, 0, 0, _parentHandle(), nullptr, GetModuleHandle(0), 0
  8. );
  9. pWidget::construct();
  10. setBackgroundColor(state().backgroundColor);
  11. setEditable(state().editable);
  12. setText(state().text);
  13. }
  14. auto pLineEdit::destruct() -> void {
  15. if(backgroundBrush) { DeleteObject(backgroundBrush); backgroundBrush = 0; }
  16. DestroyWindow(hwnd);
  17. }
  18. auto pLineEdit::minimumSize() const -> Size {
  19. auto size = pFont::size(hfont, state().text ? state().text : " ");
  20. return {size.width() + 12, size.height() + 10};
  21. }
  22. auto pLineEdit::setBackgroundColor(Color color) -> void {
  23. if(backgroundBrush) { DeleteObject(backgroundBrush); backgroundBrush = 0; }
  24. backgroundBrush = CreateSolidBrush(color ? CreateRGB(color) : GetSysColor(COLOR_WINDOW));
  25. InvalidateRect(hwnd, 0, true);
  26. }
  27. auto pLineEdit::setEditable(bool editable) -> void {
  28. SendMessage(hwnd, EM_SETREADONLY, editable == false, 0);
  29. }
  30. auto pLineEdit::setForegroundColor(Color color) -> void {
  31. InvalidateRect(hwnd, 0, true);
  32. }
  33. auto pLineEdit::setText(const string& text) -> void {
  34. auto lock = acquire();
  35. SetWindowText(hwnd, utf16_t(text));
  36. }
  37. //
  38. auto pLineEdit::onChange() -> void {
  39. state().text = _text();
  40. if(!locked()) self().doChange();
  41. }
  42. auto pLineEdit::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> maybe<LRESULT> {
  43. if(msg == WM_KEYDOWN && wparam == VK_RETURN) {
  44. self().doActivate();
  45. }
  46. return pWidget::windowProc(hwnd, msg, wparam, lparam);
  47. }
  48. //
  49. auto pLineEdit::_text() -> string {
  50. unsigned length = GetWindowTextLength(hwnd);
  51. wchar_t text[length + 1];
  52. GetWindowText(hwnd, text, length + 1);
  53. text[length] = 0;
  54. return (const char*)utf8_t(text);
  55. }
  56. }
  57. #endif