1
0

loginGui.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "./loginGui.h"
  2. #include "./common.h"
  3. #include "./imageLoader.h"
  4. #include "../resource.h"
  5. #include "../api.h"
  6. #include <strsafe.h>
  7. static size_t threadStorage = TLS_OUT_OF_INDEXES;
  8. LoginGuiObject::LoginGuiObject()
  9. : ref(1), bitmapIcons(NULL), fontTitle(NULL), fontEditor(NULL), fontText(NULL)
  10. {
  11. }
  12. LoginGuiObject::~LoginGuiObject()
  13. {
  14. Reset();
  15. }
  16. HRESULT LoginGuiObject::InitializeThread()
  17. {
  18. if (TLS_OUT_OF_INDEXES == threadStorage)
  19. {
  20. if (NULL == WASABI_API_APP)
  21. return E_UNEXPECTED;
  22. threadStorage = WASABI_API_APP->AllocateThreadStorage();
  23. if (TLS_OUT_OF_INDEXES == threadStorage)
  24. return E_UNEXPECTED;
  25. }
  26. LoginGuiObject *cache = (LoginGuiObject*)WASABI_API_APP->GetThreadStorage(threadStorage);
  27. if (NULL != cache)
  28. {
  29. cache->AddRef();
  30. return S_FALSE;
  31. }
  32. if (NULL == cache)
  33. {
  34. cache = new LoginGuiObject();
  35. if (NULL == cache) return E_OUTOFMEMORY;
  36. WASABI_API_APP->SetThreadStorage(threadStorage, cache);
  37. }
  38. return S_OK;
  39. }
  40. HRESULT LoginGuiObject::UninitializeThread()
  41. {
  42. if (TLS_OUT_OF_INDEXES == threadStorage)
  43. return E_FAIL;
  44. if (NULL == WASABI_API_APP)
  45. return E_UNEXPECTED;
  46. LoginGuiObject *cache = (LoginGuiObject*)WASABI_API_APP->GetThreadStorage(threadStorage);
  47. if (NULL != cache && 0 == cache->Release())
  48. WASABI_API_APP->SetThreadStorage(threadStorage, NULL);
  49. return S_OK;
  50. }
  51. HRESULT LoginGuiObject::QueryInstance(LoginGuiObject **instance)
  52. {
  53. if (NULL == instance)
  54. return E_POINTER;
  55. if (TLS_OUT_OF_INDEXES == threadStorage)
  56. return E_UNEXPECTED;
  57. *instance = (LoginGuiObject*)WASABI_API_APP->GetThreadStorage(threadStorage);
  58. if (NULL == *instance) return E_FAIL;
  59. (*instance)->AddRef();
  60. return S_OK;
  61. }
  62. ULONG LoginGuiObject::AddRef()
  63. {
  64. return InterlockedIncrement((LONG*)&ref);
  65. }
  66. ULONG LoginGuiObject::Release()
  67. {
  68. if (0 == ref)
  69. return ref;
  70. LONG r = InterlockedDecrement((LONG*)&ref);
  71. if (0 == r)
  72. delete(this);
  73. return r;
  74. }
  75. HRESULT LoginGuiObject::Reset()
  76. {
  77. if (NULL != bitmapIcons)
  78. {
  79. DeleteObject(bitmapIcons);
  80. bitmapIcons = NULL;
  81. }
  82. if (NULL != fontTitle)
  83. {
  84. DeleteObject(fontTitle);
  85. fontTitle = NULL;
  86. }
  87. if (NULL != fontEditor)
  88. {
  89. DeleteObject(fontEditor);
  90. fontEditor = NULL;
  91. }
  92. if (NULL != fontText)
  93. {
  94. DeleteObject(fontText);
  95. fontText = NULL;
  96. }
  97. return S_OK;
  98. }
  99. HRESULT LoginGuiObject::GetIconDimensions(INT *pWidth, INT *pHeight)
  100. {
  101. if (NULL == bitmapIcons)
  102. {
  103. INT width, height;
  104. bitmapIcons = ImageLoader_LoadBitmap(WASABI_API_ORIG_HINST, MAKEINTRESOURCE(IDR_NOTIFIERICONS_IMAGE),
  105. TRUE, &width, &height);
  106. if (NULL == bitmapIcons)
  107. return E_FAIL;
  108. if (height < 0) height = -height;
  109. if (NULL != pWidth) *pWidth = width;
  110. if (NULL != pHeight) *pHeight = width;
  111. return S_OK;
  112. }
  113. BITMAP bm;
  114. if (sizeof(bm) != GetObject(bitmapIcons, sizeof(bm), &bm))
  115. return E_FAIL;
  116. if (NULL != pWidth) *pWidth = bm.bmWidth;
  117. if (NULL != pHeight) *pHeight = bm.bmWidth;
  118. return S_OK;
  119. }
  120. HBITMAP LoginGuiObject::GetIcon(INT iconId, RECT *prcIcon)
  121. {
  122. if (NULL == prcIcon || iconId < 0)
  123. return NULL;
  124. INT width, height;
  125. if (NULL != bitmapIcons)
  126. {
  127. BITMAP bm;
  128. if (sizeof(bm) != GetObject(bitmapIcons, sizeof(bm), &bm))
  129. bitmapIcons = NULL;
  130. else
  131. {
  132. width = bm.bmWidth;
  133. height = bm.bmHeight;
  134. }
  135. }
  136. if (NULL == bitmapIcons)
  137. {
  138. bitmapIcons = ImageLoader_LoadBitmap(WASABI_API_ORIG_HINST, MAKEINTRESOURCE(IDR_NOTIFIERICONS_IMAGE),
  139. TRUE, &width, &height);
  140. if (NULL == bitmapIcons)
  141. return NULL;
  142. }
  143. if (height < 0) height = -height;
  144. if (width * (iconId + 1) > height)
  145. return NULL;
  146. prcIcon->left = 0;
  147. prcIcon->right = width;
  148. prcIcon->top = width * iconId;
  149. prcIcon->bottom = prcIcon->top + width;
  150. return bitmapIcons;
  151. }
  152. static HFONT LoginGuiObject_DuplicateFont(HFONT fontBase, INT heightDeltaPt)
  153. {
  154. if (NULL == fontBase) return NULL;
  155. LOGFONT lf;
  156. if (sizeof(lf) != GetObject(fontBase, sizeof(lf), &lf))
  157. return NULL;
  158. if (0 != heightDeltaPt)
  159. {
  160. HDC hdc = GetDCEx(NULL, NULL, DCX_WINDOW | DCX_CACHE | DCX_NORESETATTRS);
  161. HDC hdcTmp = NULL;
  162. if (NULL != hdc)
  163. {
  164. hdcTmp = CreateCompatibleDC(hdc);
  165. ReleaseDC(NULL, hdc);
  166. }
  167. if (NULL == hdcTmp)
  168. return NULL;
  169. LONG pixelsY = GetDeviceCaps (hdcTmp, LOGPIXELSY);
  170. HFONT fontOrig = (HFONT)SelectObject(hdcTmp, fontBase);
  171. TEXTMETRIC tm;
  172. if (FALSE != GetTextMetrics(hdcTmp, &tm))
  173. {
  174. INT basePt = MulDiv(tm.tmHeight - tm.tmInternalLeading, 72, pixelsY);
  175. lf.lfHeight = -MulDiv((basePt + heightDeltaPt), pixelsY, 72);
  176. }
  177. SelectObject(hdcTmp, fontOrig);
  178. DeleteDC(hdcTmp);
  179. }
  180. return CreateFontIndirect(&lf);
  181. }
  182. HFONT LoginGuiObject::GetTitleFont()
  183. {
  184. if (NULL == fontTitle)
  185. {
  186. HFONT fontBase = GetTextFont();
  187. if (NULL != fontBase)
  188. {
  189. fontTitle = LoginGuiObject_DuplicateFont(fontBase, 3);
  190. }
  191. }
  192. return fontTitle;
  193. }
  194. HFONT LoginGuiObject::GetEditorFont()
  195. {
  196. if (NULL == fontEditor)
  197. {
  198. HFONT fontBase = GetTextFont();
  199. if (NULL != fontBase)
  200. {
  201. fontEditor = LoginGuiObject_DuplicateFont(fontBase, 0);
  202. }
  203. }
  204. return fontEditor;
  205. }
  206. HFONT LoginGuiObject::GetTextFont()
  207. {
  208. if (NULL == fontText)
  209. {
  210. LOGFONT lf;
  211. if (FALSE != SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0))
  212. {
  213. lf.lfQuality = LoginBox_GetSysFontQuality();
  214. fontText = CreateFontIndirect(&lf);
  215. }
  216. }
  217. return fontText;
  218. }