1
0

ml_rating.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "./ml_rating.h"
  2. #include <commctrl.h>
  3. #define IMAGE_PARTSCOUNT 4
  4. BOOL MLRatingI_Draw(HDC hdc, INT maxValue, INT value, INT trackingVal, HMLIMGLST hmlil, INT index, RECT *prc, UINT fStyle)
  5. {
  6. INT ilIndex;
  7. if (!hdc || !hmlil || !prc) return FALSE;
  8. if (RDS_OPAQUE_I & fStyle) ExtTextOutW(hdc, 0, 0, ETO_OPAQUE, prc, L"", 0, 0);
  9. ilIndex = MLImageListI_GetRealIndex(hmlil, index, GetBkColor(hdc), GetTextColor(hdc));
  10. if (-1 != ilIndex)
  11. {
  12. INT val, count;
  13. static IMAGELISTDRAWPARAMS ildp = { 56/*sizeof(IMAGELISTDRAWPARAMS)*/, 0, };
  14. ildp.hdcDst = hdc;
  15. ildp.himl = MLImageListI_GetRealList(hmlil);
  16. ildp.i = ilIndex;
  17. ildp.x = prc->left;
  18. ildp.y = prc->top;
  19. ildp.rgbBk = CLR_DEFAULT;
  20. ildp.rgbFg = CLR_DEFAULT;
  21. ildp.fStyle = ILD_NORMAL;
  22. ildp.dwRop = SRCCOPY;
  23. MLImageListI_GetImageSize(hmlil, &ildp.cx, &ildp.cy);
  24. val = (value < 0) ? 0 : value;
  25. count = ((RDS_SHOWEMPTY_I & fStyle) ? maxValue : (((RDS_HOT_I & fStyle) && trackingVal > val) ? trackingVal : val));
  26. if (count < 0) count = 0;
  27. ildp.cx = ildp.cx/IMAGE_PARTSCOUNT;
  28. ildp.xBitmap = ((RDS_HOT_I & fStyle) ? (ildp.cx*2) : (RDS_INACTIVE_HOT_I & fStyle) ? ildp.cx*2 : 0) + ildp.cx;
  29. if(RDS_RIGHT_I & fStyle) ildp.x = prc->right - maxValue*ildp.cx;
  30. if(RDS_HCENTER_I & fStyle) ildp.x = prc->left + (prc->right - prc->left - maxValue*ildp.cx)/2;
  31. if(RDS_BOTTOM_I & fStyle) ildp.y = prc->bottom - ildp.cy;
  32. if(RDS_VCENTER_I & fStyle) ildp.y = prc->top + (prc->bottom - prc->top - ildp.cy)/2;
  33. if (ildp.y < prc->top)
  34. {
  35. ildp.yBitmap = prc->top - ildp.y;
  36. ildp.y = prc->top;
  37. }
  38. else ildp.yBitmap = 0;
  39. if (ildp.cy > (prc->bottom - ildp.y)) ildp.cy = prc->bottom - ildp.y;
  40. for (INT i = 0; ildp.x < prc->right && i < count; i++, ildp.x += ildp.cx)
  41. {
  42. if (RDS_HOT_I & fStyle)
  43. {
  44. if (i == trackingVal) ildp.xBitmap -= ildp.cx * ((val > trackingVal) ? 2 : 1);
  45. else if (i == val && val > trackingVal) ildp.xBitmap += ildp.cx;
  46. }
  47. else
  48. {
  49. if (i == val) ildp.xBitmap -= ildp.cx;
  50. }
  51. if (ildp.x < (prc->left - ildp.cx)) continue;
  52. if (prc->right < (ildp.x + ildp.cx)) ildp.cx = prc->right - ildp.x;
  53. ImageList_DrawIndirect(&ildp);
  54. }
  55. }
  56. return TRUE;
  57. }
  58. LONG MLRatingI_HitTest(POINT pt, INT maxValue, HMLIMGLST hmlil, RECT *prc, UINT fStyle)
  59. {
  60. INT imageCX, imageCY, imageX, imageY;
  61. WORD index, flags;
  62. if (!hmlil || !prc) return MAKELONG(0,0);
  63. if (!PtInRect(prc, pt)) return MAKELONG(0, RHT_NOWHERE_I);
  64. MLImageListI_GetImageSize(hmlil, &imageCX, &imageCY);
  65. imageCX = imageCX/IMAGE_PARTSCOUNT;
  66. imageX = prc->left;
  67. if(RDS_RIGHT_I & fStyle) imageX = prc->right - maxValue*imageCX;
  68. if(RDS_HCENTER_I & fStyle) imageX = prc->left + (prc->right - prc->left - maxValue*imageCX)/2;
  69. imageY = prc->top;
  70. if(RDS_BOTTOM_I & fStyle) imageY = prc->bottom - imageCY;
  71. if(RDS_VCENTER_I & fStyle) imageY = prc->top + (prc->bottom - prc->top - imageCY)/2;
  72. if (imageY < prc->top) imageY = prc->top;
  73. if (imageCY > (prc->bottom - imageY)) imageCY = prc->bottom - imageY;
  74. flags = 0;
  75. index = 0;
  76. if (pt.x < imageX) flags |= RHT_TOLEFT_I;
  77. else if (pt.x > (imageX + imageCX*maxValue)) flags |= RHT_TORIGHT_I;
  78. else
  79. {
  80. flags |= RHT_ONVALUE_I;
  81. if (pt.y < imageY) flags |= RHT_ONVALUEABOVE_I;
  82. else if (pt.y > (imageY + imageCY)) flags |= RHT_ONVALUEBELOW_I;
  83. index = (WORD)(pt.x - imageX)/imageCX + 1;
  84. if (index > maxValue) index = maxValue;
  85. }
  86. return MAKELONG(index, flags);
  87. }
  88. BOOL MLRatingI_CalcMinRect(INT maxValue, HMLIMGLST hmlil, RECT *prc)
  89. {
  90. INT imageCX, imageCY;
  91. if (!hmlil || !prc || !MLImageListI_GetImageSize(hmlil, &imageCX, &imageCY)) return FALSE;
  92. SetRect(prc, 0, 0, maxValue*imageCX/IMAGE_PARTSCOUNT, imageCY);
  93. return TRUE;
  94. }