File: label.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 (102 lines) | stat: -rw-r--r-- 3,240 bytes parent folder | download | duplicates (2)
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
#if defined(Hiro_Label)

namespace hiro {

//warning: WS_CLIPSIBLINGS flag will prevent Label widgets from rendering inside of Frame widgets
auto pLabel::construct() -> void {
  hwnd = CreateWindow(L"hiroWidget", L"", WS_CHILD, 0, 0, 0, 0, _parentHandle(), nullptr, GetModuleHandle(0), 0);
  pWidget::construct();
  setText(state().text);
}

auto pLabel::destruct() -> void {
  DestroyWindow(hwnd);
}

auto pLabel::minimumSize() const -> Size {
  auto size = pFont::size(self().font(true), state().text ? state().text : " ");
  return {size.width(), size.height()};
}

auto pLabel::setAlignment(Alignment alignment) -> void {
  InvalidateRect(hwnd, 0, false);
}

auto pLabel::setBackgroundColor(Color color) -> void {
  InvalidateRect(hwnd, 0, false);
}

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

auto pLabel::setText(const string& text) -> void {
  InvalidateRect(hwnd, 0, false);
}

auto pLabel::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> maybe<LRESULT> {
  if(msg == WM_GETDLGCODE) {
    return DLGC_STATIC | DLGC_WANTCHARS;
  }

  if(msg == WM_ERASEBKGND || msg == WM_PAINT) {
    PAINTSTRUCT ps;
    BeginPaint(hwnd, &ps);
    RECT rc;
    GetClientRect(hwnd, &rc);

    auto hdcMemory = CreateCompatibleDC(ps.hdc);
    auto hbmMemory = CreateCompatibleBitmap(ps.hdc, rc.right - rc.left, rc.bottom - rc.top);
    SelectObject(hdcMemory, hbmMemory);

    if(auto color = state().backgroundColor) {
      auto brush = CreateSolidBrush(CreateRGB(color));
      FillRect(hdcMemory, &rc, brush);
      DeleteObject(brush);
    } else if(self().parentTabFrame(true)) {
      DrawThemeParentBackground(hwnd, hdcMemory, &rc);
    } else if(auto window = self().parentWindow(true)) {
      if(auto color = window->backgroundColor()) {
        auto brush = CreateSolidBrush(CreateRGB(color));
        FillRect(hdcMemory, &rc, brush);
        DeleteObject(brush);
      } else {
        DrawThemeParentBackground(hwnd, hdcMemory, &rc);
      }
    }

    utf16_t text(state().text);
    SetBkMode(hdcMemory, TRANSPARENT);
    SelectObject(hdcMemory, hfont);
    DrawText(hdcMemory, text, -1, &rc, DT_CALCRECT | DT_END_ELLIPSIS);
    u32 height = rc.bottom;

    GetClientRect(hwnd, &rc);
    rc.top = (rc.bottom - height) / 2;
    rc.bottom = rc.top + height;
    u32 horizontalAlignment = DT_CENTER;
    if(state().alignment.horizontal() < 0.333) horizontalAlignment = DT_LEFT;
    if(state().alignment.horizontal() > 0.666) horizontalAlignment = DT_RIGHT;
    u32 verticalAlignment = DT_VCENTER;
    if(state().alignment.vertical() < 0.333) verticalAlignment = DT_TOP;
    if(state().alignment.vertical() > 0.666) verticalAlignment = DT_BOTTOM;
    if(auto color = state().foregroundColor) {
      SetTextColor(hdcMemory, CreateRGB(color));
    }
    DrawText(hdcMemory, text, -1, &rc, DT_END_ELLIPSIS | horizontalAlignment | verticalAlignment);

    GetClientRect(hwnd, &rc);
    BitBlt(ps.hdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, hdcMemory, 0, 0, SRCCOPY);
    DeleteObject(hbmMemory);
    DeleteObject(hdcMemory);
    EndPaint(hwnd, &ps);

    return msg == WM_ERASEBKGND;
  }

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

}

#endif