radio-button.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #if defined(Hiro_RadioButton)
  2. namespace hiro {
  3. auto pRadioButton::construct() -> void {
  4. hwnd = CreateWindow(L"BUTTON", L"",
  5. WS_CHILD | WS_TABSTOP | BS_CHECKBOX | BS_PUSHLIKE,
  6. 0, 0, 0, 0, _parentHandle(), nullptr, GetModuleHandle(0), 0);
  7. pWidget::construct();
  8. setGroup(state().group);
  9. _setState();
  10. }
  11. auto pRadioButton::destruct() -> void {
  12. DestroyWindow(hwnd);
  13. }
  14. auto pRadioButton::minimumSize() const -> Size {
  15. Size icon = {(int)state().icon.width(), (int)state().icon.height()};
  16. Size text = state().text ? pFont::size(self().font(true), state().text) : Size{};
  17. Size size;
  18. if(state().orientation == Orientation::Horizontal) {
  19. size.setWidth(icon.width() + (icon && text ? 5 : 0) + text.width());
  20. size.setHeight(max(icon.height(), text.height()));
  21. }
  22. if(state().orientation == Orientation::Vertical) {
  23. size.setWidth(max(icon.width(), text.width()));
  24. size.setHeight(icon.height() + (icon && text ? 5 : 0) + text.height());
  25. }
  26. size.setHeight(max(size.height(), pFont::size(self().font(true), " ").height()));
  27. return {size.width() + (state().bordered && text ? 20 : 10), size.height() + 10};
  28. }
  29. auto pRadioButton::setBordered(bool bordered) -> void {
  30. _setState();
  31. }
  32. auto pRadioButton::setChecked() -> void {
  33. if(auto& group = state().group) {
  34. for(auto& weak : group->state.objects) {
  35. if(auto object = weak.acquire()) {
  36. if(auto radioButton = dynamic_cast<mRadioButton*>(object.data())) {
  37. if(auto self = radioButton->self()) {
  38. SendMessage(self->hwnd, BM_SETCHECK, (WPARAM)(&self->reference == &reference), 0);
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. auto pRadioButton::setEnabled(bool enabled) -> void {
  46. pWidget::setEnabled(enabled);
  47. _setState();
  48. }
  49. auto pRadioButton::setFont(const Font& font) -> void {
  50. pWidget::setFont(font);
  51. _setState();
  52. }
  53. auto pRadioButton::setGroup(sGroup group) -> void {
  54. bool first = true;
  55. if(auto& group = state().group) {
  56. for(auto& weak : group->state.objects) {
  57. if(auto object = weak.acquire()) {
  58. if(auto radioButton = dynamic_cast<mRadioButton*>(object.data())) {
  59. if(auto self = radioButton->self()) {
  60. SendMessage(self->hwnd, BM_SETCHECK, (WPARAM)(radioButton->state.checked = first), 0);
  61. first = false;
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. auto pRadioButton::setIcon(const image& icon) -> void {
  69. _setState();
  70. }
  71. auto pRadioButton::setOrientation(Orientation orientation) -> void {
  72. _setState();
  73. }
  74. auto pRadioButton::setText(const string& text) -> void {
  75. _setState();
  76. }
  77. auto pRadioButton::setVisible(bool visible) -> void {
  78. pWidget::setVisible(visible);
  79. _setState();
  80. }
  81. //
  82. auto pRadioButton::onActivate() -> void {
  83. if(state().checked) return;
  84. self().setChecked();
  85. self().doActivate();
  86. }
  87. auto pRadioButton::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> maybe<LRESULT> {
  88. if(msg == WM_PAINT) {
  89. PAINTSTRUCT ps;
  90. BeginPaint(hwnd, &ps);
  91. auto buttonState = Button_GetState(hwnd);
  92. Button_CustomDraw(hwnd, ps,
  93. state().bordered, state().checked, self().enabled(true), buttonState,
  94. self().font(true), state().icon, state().orientation, state().text
  95. );
  96. EndPaint(hwnd, &ps);
  97. return false;
  98. }
  99. return pWidget::windowProc(hwnd, msg, wparam, lparam);
  100. }
  101. //
  102. auto pRadioButton::_setState() -> void {
  103. InvalidateRect(hwnd, 0, false);
  104. }
  105. }
  106. #endif