1
0

toolbarProgress.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "main.h"
  2. #include "./toolbarProgress.h"
  3. #include "./toolbar.h"
  4. #include "./graphics.h"
  5. #include "./resource.h"
  6. #include "../Plugins/General/gen_ml/ml_ipc_0313.h"
  7. #include "./ifc_imageloader.h"
  8. #include <strsafe.h>
  9. #define PROGRESS_FRAMECOUNT 9
  10. static ATOM TOOLBARPROGRESS_PROP = 0;
  11. #define TIMER_PROGRESSSTEP_ID 111
  12. #define TIMER_PROGRESSSTEP_INTERVAL 120
  13. ToolbarProgress::ToolbarProgress(LPCSTR pszName, UINT nStyle, LPCWSTR pszText, LPCWSTR pszDescription)
  14. : ToolbarItem(pszName, nStyle, ICON_NONE, pszText, pszDescription),
  15. bitmap(NULL), frame(0), hTimer(NULL)
  16. {
  17. }
  18. ToolbarProgress::~ToolbarProgress()
  19. {
  20. if (NULL != hTimer)
  21. Animate(hTimer, FALSE);
  22. if (NULL != bitmap)
  23. DeleteObject(bitmap);
  24. }
  25. static void CALLBACK ToolbarProgress_TimerElapsed(HWND hwnd, UINT uMsg, UINT_PTR eventId, DWORD dwTime)
  26. {
  27. ToolbarProgress *item = (ToolbarProgress*)GetProp(hwnd, MAKEINTATOM(TOOLBARPROGRESS_PROP));
  28. if (NULL == item || 0 != ((ToolbarItem::stateDisabled | ToolbarItem::stateHidden) & item->GetStyle()))
  29. {
  30. if (NULL != item)
  31. item->Animate(hwnd, FALSE);
  32. else
  33. KillTimer(hwnd, TIMER_PROGRESSSTEP_ID);
  34. return;
  35. }
  36. item->Animate(hwnd, TRUE);
  37. }
  38. ToolbarItem* CALLBACK ToolbarProgress::CreateInstance(ToolbarItem::Template *item)
  39. {
  40. if (NULL == item)
  41. return NULL;
  42. return new ToolbarProgress( (NULL != item->name) ? item->name : TOOLCLS_PROGRESS,
  43. item->style | styleNoChevron,
  44. item->text,
  45. item->description);
  46. }
  47. void ToolbarProgress::SetStyle(HWND hToolbar, UINT newStyle, UINT styleMask)
  48. {
  49. __super::SetStyle(hToolbar, newStyle, styleMask);
  50. if (0 == ((stateDisabled | stateHidden) & style))
  51. {
  52. Animate(hToolbar, TRUE);
  53. }
  54. }
  55. BOOL ToolbarProgress::Animate(HWND hToolbar, BOOL fAnimate)
  56. {
  57. if (FALSE == fAnimate)
  58. {
  59. if (NULL != hTimer)
  60. {
  61. RemoveProp(hTimer, MAKEINTATOM(TOOLBARPROGRESS_PROP));
  62. KillTimer(hTimer, TIMER_PROGRESSSTEP_ID);
  63. hTimer = NULL;
  64. }
  65. if (0 != frame)
  66. {
  67. frame = 0;
  68. InvalidateRect(hToolbar, &rect, FALSE);
  69. }
  70. }
  71. else
  72. {
  73. if (NULL == hTimer)
  74. {
  75. if (0 == TOOLBARPROGRESS_PROP)
  76. {
  77. TOOLBARPROGRESS_PROP = GlobalAddAtom(L"omToolbarProgressProp");
  78. if (0 == TOOLBARPROGRESS_PROP) return FALSE;
  79. }
  80. if (!SetProp(hToolbar, MAKEINTATOM(TOOLBARPROGRESS_PROP), (HANDLE)this))
  81. return FALSE;
  82. if (!SetTimer(hToolbar, TIMER_PROGRESSSTEP_ID, TIMER_PROGRESSSTEP_INTERVAL,
  83. ToolbarProgress_TimerElapsed))
  84. {
  85. RemoveProp(hToolbar, MAKEINTATOM(TOOLBARPROGRESS_PROP));
  86. return FALSE;
  87. }
  88. hTimer = hToolbar;
  89. }
  90. if (++frame >= PROGRESS_FRAMECOUNT)
  91. {
  92. frame = 1;
  93. }
  94. InvalidateRect(hToolbar, &rect, FALSE);
  95. }
  96. return TRUE;
  97. }
  98. void ToolbarProgress::UpdateSkin(HWND hToolbar)
  99. {
  100. if (NULL != bitmap)
  101. {
  102. DeleteObject(bitmap);
  103. bitmap = NULL;
  104. }
  105. ifc_omimageloader *loader;
  106. if (SUCCEEDED(Plugin_QueryImageLoader(Plugin_GetInstance(), MAKEINTRESOURCE(IDR_TOOLBARPROGRESS_IMAGE), TRUE, &loader)))
  107. {
  108. BITMAPINFOHEADER headerInfo;
  109. BYTE *pixelData;
  110. if (SUCCEEDED(loader->LoadBitmapEx(&bitmap, &headerInfo, (void**)&pixelData)))
  111. {
  112. if (headerInfo.biHeight < 0) headerInfo.biHeight = -headerInfo.biHeight;
  113. Image_Colorize(pixelData, headerInfo.biWidth, headerInfo.biHeight, headerInfo.biBitCount,
  114. Toolbar_GetBkColor(hToolbar), Toolbar_GetFgColor(hToolbar), TRUE);
  115. }
  116. loader->Release();
  117. }
  118. }
  119. BOOL ToolbarProgress::AdjustRect(HWND hToolbar, RECT *proposedRect)
  120. {
  121. BITMAP bm;
  122. if (NULL == bitmap || sizeof(BITMAP) != GetObject(bitmap, sizeof(BITMAP), &bm))
  123. return FALSE;
  124. if (bm.bmHeight < 0) bm.bmHeight = -bm.bmHeight;
  125. bm.bmHeight /= PROGRESS_FRAMECOUNT;
  126. proposedRect->right = proposedRect->left + bm.bmWidth;
  127. proposedRect->top += (((proposedRect->bottom - proposedRect->top) - bm.bmHeight)/2);
  128. proposedRect->bottom = proposedRect->top + bm.bmHeight;
  129. return TRUE;
  130. }
  131. BOOL ToolbarProgress::Paint(HWND hToolbar, HDC hdc, const RECT *paintRect, UINT state)
  132. {
  133. BITMAP bm;
  134. if (NULL == bitmap || sizeof(BITMAP) != GetObject(bitmap, sizeof(BITMAP), &bm))
  135. return FALSE;
  136. if (bm.bmHeight < 0) bm.bmHeight = -bm.bmHeight;
  137. bm.bmHeight /= PROGRESS_FRAMECOUNT;
  138. RECT blitRect;
  139. if (!IntersectRect(&blitRect, paintRect))
  140. return TRUE;
  141. BOOL success = FALSE;
  142. HDC hdcSrc = CreateCompatibleDC(hdc);
  143. if (NULL != hdcSrc)
  144. {
  145. HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcSrc, bitmap);
  146. success = BitBlt(hdc, blitRect.left, blitRect.top,
  147. blitRect.right - blitRect.left, blitRect.bottom - blitRect.top,
  148. hdcSrc,
  149. blitRect.left - rect.left,
  150. bm.bmHeight * frame + (blitRect.top - rect.top),
  151. SRCCOPY);
  152. SelectObject(hdcSrc, hbmpOld);
  153. DeleteDC(hdcSrc);
  154. }
  155. return success;
  156. }