SBdetours.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include "stdafx.h"
  2. #include "SBdetours.h"
  3. #include "detours.h"
  4. #include <tchar.h>
  5. #include "windows.h"
  6. #pragma warning(disable:4100) // Trampolines don't use formal parameters.
  7. static TCHAR szPropStr[] = _T("SkinScrollBarPtr");
  8. SCROLLWND *GetScrollWndFromHwnd(HWND hwnd)
  9. {
  10. return (SCROLLWND *)GetProp(hwnd, szPropStr);
  11. }
  12. SCROLLBAR *GetScrollBarFromHwnd(HWND hwnd, UINT nBar)
  13. {
  14. SCROLLWND *sw = GetScrollWndFromHwnd(hwnd);
  15. if(!sw) return 0;
  16. if(nBar == SB_HORZ)
  17. return &sw->sbarHorz;
  18. else if(nBar == SB_VERT)
  19. return &sw->sbarVert;
  20. else
  21. return 0;
  22. }
  23. BOOL WINAPI CoolSB_IsCoolScrollEnabled(HWND hwnd)
  24. {
  25. if(GetScrollWndFromHwnd(hwnd))
  26. return TRUE;
  27. else
  28. return FALSE;
  29. }
  30. SCROLLINFO *GetScrollInfoFromHwnd(HWND hwnd, int fnBar)
  31. {
  32. SCROLLBAR *sb = GetScrollBarFromHwnd(hwnd, fnBar);
  33. if(sb == 0)
  34. return FALSE;
  35. if(fnBar == SB_HORZ)
  36. {
  37. return &sb->scrollInfo;
  38. }
  39. else if(fnBar == SB_VERT)
  40. {
  41. return &sb->scrollInfo;
  42. }
  43. else
  44. return NULL;
  45. }
  46. BOOL WINAPI CoolSB_EnableScrollBar (HWND hwnd, int wSBflags, UINT wArrows)
  47. {
  48. SCROLLBAR *sbar;
  49. UINT oldstate;
  50. BOOL bFailed = FALSE;
  51. if(!CoolSB_IsCoolScrollEnabled(hwnd))
  52. return EnableScrollBar(hwnd, wSBflags, wArrows);
  53. if((wSBflags == SB_HORZ || wSBflags == SB_BOTH) &&
  54. (sbar = GetScrollBarFromHwnd(hwnd, SB_HORZ)))
  55. {
  56. oldstate = sbar->fScrollFlags;
  57. //clear any existing state, and OR in the disabled flags
  58. sbar->fScrollFlags = (sbar->fScrollFlags & ~ESB_DISABLE_BOTH) | wArrows;
  59. if(oldstate == sbar->fScrollFlags)
  60. bFailed = TRUE;
  61. }
  62. if((wSBflags == SB_VERT || wSBflags == SB_BOTH) &&
  63. (sbar = GetScrollBarFromHwnd(hwnd, SB_VERT)))
  64. {
  65. oldstate = sbar->fScrollFlags;
  66. //clear any existing state, and OR in the disabled flags
  67. sbar->fScrollFlags = (sbar->fScrollFlags & ~ESB_DISABLE_BOTH) | wArrows;
  68. if(oldstate == sbar->fScrollFlags)
  69. bFailed = TRUE;
  70. }
  71. return !bFailed;
  72. }
  73. BOOL WINAPI CoolSB_GetScrollBarInfo(HWND hwnd)
  74. {
  75. // SCROLLBARINFO sbi; not defined in winuser.h
  76. return FALSE;
  77. }
  78. BOOL WINAPI CoolSB_GetScrollInfo (HWND hwnd, int fnBar, LPSCROLLINFO lpsi)
  79. {
  80. SCROLLINFO *mysi;
  81. BOOL copied = FALSE;
  82. if(!lpsi)
  83. return FALSE;
  84. if(!(mysi = GetScrollInfoFromHwnd(hwnd, fnBar)))
  85. {
  86. return GetScrollInfo(hwnd, fnBar, lpsi);
  87. }
  88. if(lpsi->fMask & SIF_PAGE)
  89. {
  90. lpsi->nPage = mysi->nPage;
  91. copied = TRUE;
  92. }
  93. if(lpsi->fMask & SIF_POS)
  94. {
  95. lpsi->nPos = mysi->nPos;
  96. copied = TRUE;
  97. }
  98. if(lpsi->fMask & SIF_TRACKPOS)
  99. {
  100. lpsi->nTrackPos = mysi->nTrackPos;
  101. copied = TRUE;
  102. }
  103. if(lpsi->fMask & SIF_RANGE)
  104. {
  105. lpsi->nMin = mysi->nMin;
  106. lpsi->nMax = mysi->nMax;
  107. copied = TRUE;
  108. }
  109. return copied;
  110. }
  111. int WINAPI CoolSB_GetScrollPos (HWND hwnd, int nBar)
  112. {
  113. SCROLLINFO *mysi;
  114. if(!(mysi = GetScrollInfoFromHwnd(hwnd, nBar)))
  115. return GetScrollPos(hwnd, nBar);
  116. return mysi->nPos;
  117. }
  118. BOOL WINAPI CoolSB_GetScrollRange (HWND hwnd, int nBar, LPINT lpMinPos, LPINT lpMaxPos)
  119. {
  120. SCROLLINFO *mysi;
  121. if(!lpMinPos || !lpMaxPos)
  122. return FALSE;
  123. if(!(mysi = GetScrollInfoFromHwnd(hwnd, nBar)))
  124. return GetScrollRange(hwnd, nBar, lpMinPos, lpMaxPos);
  125. *lpMinPos = mysi->nMin;
  126. *lpMaxPos = mysi->nMax;
  127. return TRUE;
  128. }
  129. int WINAPI CoolSB_SetScrollInfo (HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw)
  130. {
  131. SCROLLINFO *mysi;
  132. SCROLLBAR *sbar;
  133. BOOL fRecalcFrame = FALSE;
  134. if(!lpsi)
  135. return FALSE;
  136. if(!(mysi = GetScrollInfoFromHwnd(hwnd, fnBar)))
  137. return SetScrollInfo(hwnd, fnBar, lpsi, fRedraw);
  138. //if(CoolSB_IsThumbTracking(hwnd))
  139. // return mysi->nPos;
  140. if(lpsi->fMask & SIF_RANGE)
  141. {
  142. mysi->nMin = lpsi->nMin;
  143. mysi->nMax = lpsi->nMax;
  144. }
  145. //The nPage member must specify a value from 0 to nMax - nMin +1.
  146. if(lpsi->fMask & SIF_PAGE)
  147. {
  148. UINT t = (UINT)(mysi->nMax - mysi->nMin + 1);
  149. mysi->nPage = min(max(0, lpsi->nPage), t);
  150. }
  151. //The nPos member must specify a value between nMin and nMax - max(nPage - 1, 0).
  152. if(lpsi->fMask & SIF_POS)
  153. {
  154. mysi->nPos = max(lpsi->nPos, mysi->nMin);
  155. mysi->nPos = min((UINT)mysi->nPos, mysi->nMax - max(mysi->nPage - 1, 0));
  156. }
  157. sbar = GetScrollBarFromHwnd(hwnd, fnBar);
  158. if((lpsi->fMask & SIF_DISABLENOSCROLL) )//|| (sbar->fScrollFlags & CSBS_THUMBALWAYS))
  159. {
  160. if(!sbar->fScrollVisible)
  161. {
  162. // CoolSB_ShowScrollBar(hwnd, fnBar, TRUE);
  163. fRecalcFrame = TRUE;
  164. }
  165. }
  166. else
  167. {
  168. if( mysi->nPage > (UINT)mysi->nMax
  169. || mysi->nPage == (UINT)mysi->nMax && mysi->nMax == 0
  170. || mysi->nMax <= mysi->nMin)
  171. {
  172. if(sbar->fScrollVisible)
  173. {
  174. // CoolSB_ShowScrollBar(hwnd, fnBar, FALSE);
  175. fRecalcFrame = TRUE;
  176. }
  177. }
  178. else
  179. {
  180. if(!sbar->fScrollVisible)
  181. {
  182. // CoolSB_ShowScrollBar(hwnd, fnBar, TRUE);
  183. fRecalcFrame = TRUE;
  184. }
  185. }
  186. }
  187. // if(fRedraw && !CoolSB_IsThumbTracking(hwnd))
  188. // RedrawNonClient(hwnd, fRecalcFrame);
  189. if(fRedraw)
  190. SendMessage(hwnd,WM_NCPAINT,0,0);
  191. return mysi->nPos;
  192. }
  193. int WINAPI CoolSB_SetScrollPos(HWND hwnd, int nBar, int nPos, BOOL fRedraw)
  194. {
  195. SCROLLINFO *mysi;
  196. int oldpos;
  197. if(!(mysi = GetScrollInfoFromHwnd(hwnd, nBar)))
  198. {
  199. return SetScrollPos(hwnd, nBar, nPos, fRedraw);
  200. }
  201. //this is what should happen, but real scrollbars don't work like this..
  202. //if(CoolSB_IsThumbTracking(hwnd))
  203. // return mysi->nPos;
  204. //validate and set the scollbar position
  205. oldpos = mysi->nPos;
  206. mysi->nPos = max(nPos, mysi->nMin);
  207. mysi->nPos = min((UINT)mysi->nPos, mysi->nMax - max(mysi->nPage - 1, 0));
  208. // if(fRedraw && !CoolSB_IsThumbTracking(hwnd))
  209. // RedrawNonClient(hwnd, FALSE);
  210. if(fRedraw)
  211. SendMessage(hwnd,WM_NCPAINT,0,0);
  212. return oldpos;
  213. }
  214. int WINAPI CoolSB_SetScrollRange (HWND hwnd, int nBar, int nMinPos, int nMaxPos, BOOL fRedraw)
  215. {
  216. SCROLLINFO *mysi;
  217. if(!(mysi = GetScrollInfoFromHwnd(hwnd, nBar)))
  218. return SetScrollRange(hwnd, nBar, nMinPos, nMaxPos, fRedraw);
  219. // if(CoolSB_IsThumbTracking(hwnd))
  220. // return mysi->nPos;
  221. //hide the scrollbar if nMin == nMax
  222. //nMax-nMin must not be greater than MAXLONG
  223. mysi->nMin = nMinPos;
  224. mysi->nMax = nMaxPos;
  225. if(fRedraw)
  226. SendMessage(hwnd,WM_NCPAINT,0,0);
  227. return TRUE;
  228. }
  229. //
  230. // Show or hide the specified scrollbars
  231. //
  232. BOOL WINAPI CoolSB_ShowScrollBar (HWND hwnd, int wBar, BOOL fShow)
  233. {
  234. SCROLLBAR *sbar;
  235. BOOL bFailed = FALSE;
  236. DWORD dwStyle = GetWindowLong(hwnd, GWL_STYLE);
  237. if(!CoolSB_IsCoolScrollEnabled(hwnd))
  238. return ShowScrollBar(hwnd, wBar, fShow);
  239. if((wBar == SB_HORZ || wBar == SB_BOTH) &&
  240. (sbar = GetScrollBarFromHwnd(hwnd, SB_HORZ)))
  241. {
  242. sbar->fScrollFlags = sbar->fScrollFlags & ~CSBS_VISIBLE;
  243. sbar->fScrollFlags |= (fShow == TRUE ? CSBS_VISIBLE : 0);
  244. //bFailed = TRUE;
  245. if(fShow) SetWindowLong(hwnd, GWL_STYLE, dwStyle | WS_HSCROLL);
  246. else SetWindowLong(hwnd, GWL_STYLE, dwStyle & ~WS_HSCROLL);
  247. }
  248. if((wBar == SB_VERT || wBar == SB_BOTH) &&
  249. (sbar = GetScrollBarFromHwnd(hwnd, SB_VERT)))
  250. {
  251. sbar->fScrollFlags = sbar->fScrollFlags & ~CSBS_VISIBLE;
  252. sbar->fScrollFlags |= (fShow == TRUE ? CSBS_VISIBLE : 0);
  253. //bFailed = TRUE;
  254. if(fShow) SetWindowLong(hwnd, GWL_STYLE, dwStyle | WS_VSCROLL);
  255. else SetWindowLong(hwnd, GWL_STYLE, dwStyle & ~WS_VSCROLL);
  256. }
  257. if(bFailed)
  258. {
  259. return FALSE;
  260. }
  261. else
  262. {
  263. //DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  264. //style |= WS_VSCROLL;
  265. //if(s
  266. //SetWindowLong(hwnd, GWL_STYLE, style);
  267. SetWindowPos(hwnd, 0, 0, 0, 0, 0,
  268. SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
  269. SWP_NOACTIVATE | SWP_FRAMECHANGED);
  270. return TRUE;
  271. }
  272. }
  273. #pragma warning(disable:4100) // Trampolines don't use formal parameters.
  274. DETOUR_TRAMPOLINE(BOOL WINAPI Detour_EnableScrollBar(HWND hwnd, int wSBflags, UINT wArrows), EnableScrollBar);
  275. DETOUR_TRAMPOLINE(BOOL WINAPI Detour_GetScrollInfo (HWND hwnd, int fnBar, LPSCROLLINFO lpsi), GetScrollInfo);
  276. DETOUR_TRAMPOLINE(int WINAPI Detour_GetScrollPos (HWND hwnd, int nBar), GetScrollPos);
  277. DETOUR_TRAMPOLINE(BOOL WINAPI Detour_GetScrollRange (HWND hwnd, int nBar, LPINT lpMinPos, LPINT lpMaxPos), GetScrollRange);
  278. DETOUR_TRAMPOLINE(int WINAPI Detour_SetScrollInfo (HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw), SetScrollInfo);
  279. DETOUR_TRAMPOLINE(int WINAPI Detour_SetScrollPos (HWND hwnd, int nBar, int nPos, BOOL fRedraw), SetScrollPos);
  280. DETOUR_TRAMPOLINE(int WINAPI Detour_SetScrollRange (HWND hwnd, int nBar, int nMinPos, int nMaxPos, BOOL fRedraw), SetScrollRange);
  281. DETOUR_TRAMPOLINE(BOOL WINAPI Detour_ShowScrollBar (HWND hwnd, int wBar, BOOL fShow), ShowScrollBar);
  282. static BOOL WINAPI Tramp_EnableScrollBar(HWND hwnd, int wSBflags, UINT wArrows)
  283. {
  284. if(CoolSB_IsCoolScrollEnabled(hwnd))
  285. return CoolSB_EnableScrollBar(hwnd, wSBflags, wArrows);
  286. else
  287. return Detour_EnableScrollBar(hwnd, wSBflags, wArrows);
  288. }
  289. static BOOL WINAPI Tramp_GetScrollInfo(HWND hwnd, int fnBar, LPSCROLLINFO lpsi)
  290. {
  291. if(CoolSB_IsCoolScrollEnabled(hwnd))
  292. return CoolSB_GetScrollInfo(hwnd, fnBar, lpsi);
  293. else
  294. return Detour_GetScrollInfo(hwnd, fnBar, lpsi);
  295. }
  296. static int WINAPI Tramp_GetScrollPos(HWND hwnd, int nBar)
  297. {
  298. if(CoolSB_IsCoolScrollEnabled(hwnd))
  299. return CoolSB_GetScrollPos(hwnd, nBar);
  300. else
  301. return Detour_GetScrollPos(hwnd, nBar);
  302. }
  303. static BOOL WINAPI Tramp_GetScrollRange(HWND hwnd, int nBar, LPINT lpMinPos, LPINT lpMaxPos)
  304. {
  305. if(CoolSB_IsCoolScrollEnabled(hwnd))
  306. return CoolSB_GetScrollRange(hwnd, nBar, lpMinPos, lpMaxPos);
  307. else
  308. return Detour_GetScrollRange(hwnd, nBar, lpMinPos, lpMaxPos);
  309. }
  310. static int WINAPI Tramp_SetScrollInfo(HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw)
  311. {
  312. if(CoolSB_IsCoolScrollEnabled(hwnd))
  313. return CoolSB_SetScrollInfo(hwnd, fnBar, lpsi, fRedraw);
  314. else
  315. return Detour_SetScrollInfo(hwnd, fnBar, lpsi, fRedraw);
  316. }
  317. static int WINAPI Tramp_SetScrollPos(HWND hwnd, int nBar, int nPos, BOOL fRedraw)
  318. {
  319. if(CoolSB_IsCoolScrollEnabled(hwnd))
  320. return CoolSB_SetScrollPos(hwnd, nBar, nPos, fRedraw);
  321. else
  322. return Detour_SetScrollPos(hwnd, nBar, nPos, fRedraw);
  323. }
  324. static int WINAPI Tramp_SetScrollRange(HWND hwnd, int nBar, int nMinPos, int nMaxPos, BOOL fRedraw)
  325. {
  326. if(CoolSB_IsCoolScrollEnabled(hwnd))
  327. return CoolSB_SetScrollRange(hwnd, nBar, nMinPos, nMaxPos, fRedraw);
  328. else
  329. return Detour_SetScrollRange(hwnd, nBar, nMinPos, nMaxPos, fRedraw);
  330. }
  331. static BOOL WINAPI Tramp_ShowScrollBar (HWND hwnd, int wBar, BOOL fShow)
  332. {
  333. if(CoolSB_IsCoolScrollEnabled(hwnd))
  334. return CoolSB_ShowScrollBar(hwnd, wBar, fShow);
  335. else
  336. return Detour_ShowScrollBar(hwnd, wBar, fShow);
  337. }
  338. BOOL WINAPI CoolSB_InitializeApp(void)
  339. {
  340. DWORD dwVersion = GetVersion();
  341. // Only available under Windows NT, 2000 and XP
  342. if(dwVersion < 0x80000000)
  343. {
  344. DetourFunctionWithTrampoline((PBYTE)Detour_EnableScrollBar, (PBYTE)Tramp_EnableScrollBar);
  345. DetourFunctionWithTrampoline((PBYTE)Detour_GetScrollInfo, (PBYTE)Tramp_GetScrollInfo);
  346. DetourFunctionWithTrampoline((PBYTE)Detour_GetScrollPos, (PBYTE)Tramp_GetScrollPos);
  347. DetourFunctionWithTrampoline((PBYTE)Detour_GetScrollRange, (PBYTE)Tramp_GetScrollRange);
  348. DetourFunctionWithTrampoline((PBYTE)Detour_SetScrollInfo, (PBYTE)Tramp_SetScrollInfo);
  349. DetourFunctionWithTrampoline((PBYTE)Detour_SetScrollPos, (PBYTE)Tramp_SetScrollPos);
  350. DetourFunctionWithTrampoline((PBYTE)Detour_SetScrollRange, (PBYTE)Tramp_SetScrollRange);
  351. DetourFunctionWithTrampoline((PBYTE)Detour_ShowScrollBar, (PBYTE)Tramp_ShowScrollBar);
  352. // don't actually use this feature within coolsb yet, but we might need it
  353. CoolSB_SetESBProc(Detour_EnableScrollBar);
  354. return TRUE;
  355. }
  356. else
  357. {
  358. return FALSE;
  359. }
  360. }
  361. BOOL WINAPI CoolSB_UninitializeApp(void)
  362. {
  363. DWORD dwVersion = GetVersion();
  364. // Only available under Windows NT, 2000 and XP
  365. if(dwVersion < 0x80000000)
  366. {
  367. DetourRemove((PBYTE)Detour_EnableScrollBar, (PBYTE)Tramp_EnableScrollBar);
  368. DetourRemove((PBYTE)Detour_GetScrollInfo, (PBYTE)Tramp_GetScrollInfo);
  369. DetourRemove((PBYTE)Detour_GetScrollPos, (PBYTE)Tramp_GetScrollPos);
  370. DetourRemove((PBYTE)Detour_GetScrollRange, (PBYTE)Tramp_GetScrollRange);
  371. DetourRemove((PBYTE)Detour_SetScrollInfo, (PBYTE)Tramp_SetScrollInfo);
  372. DetourRemove((PBYTE)Detour_SetScrollPos, (PBYTE)Tramp_SetScrollPos);
  373. DetourRemove((PBYTE)Detour_SetScrollRange, (PBYTE)Tramp_SetScrollRange);
  374. DetourRemove((PBYTE)Detour_ShowScrollBar, (PBYTE)Tramp_ShowScrollBar);
  375. // don't actually use this feature within coolsb yet, but we might need it
  376. CoolSB_SetESBProc(EnableScrollBar);
  377. return TRUE;
  378. }
  379. else
  380. {
  381. return FALSE;
  382. }
  383. }