coolscroll.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123
  1. /*
  2. Cool Scrollbar Library Version 1.2
  3. Module: coolscroll.c
  4. Copyright (c) J Brown 2001
  5. This code is freeware, however, you may not publish
  6. this code elsewhere or charge any money for it. This code
  7. is supplied as-is. I make no guarantees about the suitability
  8. of this code - use at your own risk.
  9. It would be nice if you credited me, in the event
  10. that you use this code in a product.
  11. VERSION HISTORY:
  12. V1.2: TreeView problem fixed by Diego Tartara
  13. Small problem in thumbsize calculation also fixed (thanks Diego!)
  14. V1.1: Added support for Right-left windows
  15. Changed calling convention of APIs to WINAPI (__stdcall)
  16. Completely standalone (no need for c-runtime)
  17. Now supports ALL windows with appropriate USER32.DLL patching
  18. (you provide!!)
  19. V1.0: Apr 2001: Initial Version
  20. IMPORTANT:
  21. This whole library is based around code for a horizontal scrollbar.
  22. All "vertical" scrollbar drawing / mouse interaction uses the
  23. horizontal scrollbar functions, but uses a trick to convert the vertical
  24. scrollbar coordinates into horizontal equivelants. When I started this project,
  25. I quickly realised that the code for horz/vert bars was IDENTICAL, apart
  26. from the fact that horizontal code uses left/right coords, and vertical code
  27. uses top/bottom coords. On entry to a "vertical" drawing function, for example,
  28. the coordinates are "rotated" before the horizontal function is called, and
  29. then rotated back once the function has completed. When something needs to
  30. be drawn, the coords are converted back again before drawing.
  31. This trick greatly reduces the amount of code required, and makes
  32. maintanence much simpler. This way, only one function is needed to draw
  33. a scrollbar, but this can be used for both horizontal and vertical bars
  34. with careful thought.
  35. */
  36. #include "stdafx.h"
  37. #define WIN32_LEAN_AND_MEAN
  38. #include <windows.h>
  39. #include <commctrl.h>
  40. #include <tchar.h>
  41. #include "coolscroll.h"
  42. #include "userdefs.h"
  43. #include "coolsb_internal.h"
  44. extern HBRUSH g_frameedgebrush; //´°¿Ú±ß¿òÑÕÉ«»­Ë¢
  45. //define some values if the new version of common controls CoolSB_SetStyle
  46. //is not available. GetParent
  47. #ifndef NM_CUSTOMDRAW
  48. #define NM_CUSTOMDRAW (NM_FIRST-12)
  49. #define CDRF_DODEFAULT 0x0000
  50. #define CDRF_SKIPDEFAULT 0x0004
  51. #define CDDS_PREPAINT 0x0001
  52. #define CDDS_POSTPAINT 0x0002
  53. #endif
  54. //
  55. // Special thumb-tracking variables
  56. //
  57. //
  58. static UINT uCurrentScrollbar = COOLSB_NONE; //SB_HORZ / SB_VERT
  59. static UINT uCurrentScrollPortion = HTSCROLL_NONE;
  60. static UINT uCurrentButton = 0;
  61. static RECT rcThumbBounds; //area that the scroll thumb can travel in
  62. static int nThumbSize; //(pixels)
  63. static int nThumbPos; //(pixels)
  64. static int nThumbMouseOffset; //(pixels)
  65. static int nLastPos = -1; //(scrollbar units)
  66. static int nThumbPos0; //(pixels) initial thumb position
  67. //
  68. // Temporary state used to auto-generate timer messages
  69. //
  70. static UINT uMouseOverId = 0;
  71. static UINT uMouseOverScrollbar = COOLSB_NONE;
  72. static UINT uHitTestPortion = HTSCROLL_NONE;
  73. static UINT uLastHitTestPortion = HTSCROLL_NONE;
  74. static RECT MouseOverRect;
  75. static UINT uScrollTimerMsg = 0;
  76. static UINT uScrollTimerPortion = HTSCROLL_NONE;
  77. static UINT uScrollTimerId = 0;
  78. static HWND hwndCurCoolSB = 0;
  79. int nVScrollMax, nVScrollPos, nVScrollPage;
  80. int nHScrollMax, nHScrollPos, nHScrollPage;
  81. int nVMaxLines = 20;
  82. int nHMaxLines = 80;
  83. int xChar = 10;
  84. int yChar = 10;
  85. void SetupScrollbars(HWND hwnd)
  86. {
  87. SCROLLINFO si;
  88. RECT rect;
  89. GetClientRect(hwnd, &rect);
  90. // VERT
  91. nVScrollPage = min(nVMaxLines + 1, (rect.bottom - rect.top) / yChar);
  92. nVScrollMax = max(0, nVMaxLines);
  93. nVScrollPos = min(nVScrollPos, nVScrollMax - nVScrollPage + 1);
  94. si.cbSize = sizeof(si);
  95. si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
  96. si.nMin = 0;
  97. si.nMax = nVScrollMax;
  98. si.nPos = nVScrollPos;
  99. si.nPage = min(nVScrollPage, nVScrollMax + 1);
  100. CoolSB_SetScrollInfo (hwnd, SB_VERT, &si, TRUE);
  101. // HORZ
  102. nHScrollPage = min(nHMaxLines + 1, (rect.right - rect.left) / xChar);
  103. nHScrollMax = max(0, nHMaxLines);
  104. nHScrollPos = min(nHScrollPos, nHScrollMax - nHScrollPage + 1);
  105. si.cbSize = sizeof(si);
  106. si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
  107. si.nMin = 0;
  108. si.nMax = nHScrollMax;
  109. si.nPos = nHScrollPos;
  110. si.nPage = min(nHScrollPage, nHScrollMax + 1);
  111. CoolSB_SetScrollInfo (hwnd, SB_HORZ, &si, TRUE);
  112. }
  113. //
  114. // Provide this so there are NO dependencies on CRT
  115. //
  116. static void CoolSB_ZeroMemory(void *ptr, DWORD bytes)
  117. {
  118. BYTE *bptr = (BYTE *)ptr;
  119. while(bytes--) *bptr++ = 0;
  120. }
  121. BOOL WINAPI CoolSB_IsThumbTracking(HWND hwnd)
  122. {
  123. SCROLLWND *sw;
  124. if((sw = GetScrollWndFromHwnd(hwnd)) == NULL)
  125. return FALSE;
  126. else
  127. return sw->fThumbTracking;
  128. }
  129. //
  130. // swap the rectangle's x coords with its y coords
  131. //
  132. static void __stdcall RotateRect(RECT *rect)
  133. {
  134. int temp;
  135. temp = rect->left;
  136. rect->left = rect->top;
  137. rect->top = temp;
  138. temp = rect->right;
  139. rect->right = rect->bottom;
  140. rect->bottom = temp;
  141. }
  142. //
  143. // swap the coords if the scrollbar is a SB_VERT
  144. //
  145. static void __stdcall RotateRect0(SCROLLBAR *sb, RECT *rect)
  146. {
  147. if(sb->nBarType == SB_VERT)
  148. RotateRect(rect);
  149. }
  150. //
  151. // Calculate if the SCROLLINFO members produce
  152. // an enabled or disabled scrollbar
  153. //
  154. static BOOL IsScrollInfoActive(SCROLLINFO *si)
  155. {
  156. if((si->nPage > (UINT)si->nMax
  157. || si->nMax <= si->nMin || si->nMax == 0))
  158. return FALSE;
  159. else
  160. return TRUE;
  161. }
  162. //
  163. // Return if the specified scrollbar is enabled or not
  164. //
  165. static BOOL IsScrollbarActive(SCROLLBAR *sb)
  166. {
  167. SCROLLINFO *si = &sb->scrollInfo;
  168. if(((sb->fScrollFlags & ESB_DISABLE_BOTH) == ESB_DISABLE_BOTH) ||
  169. !(sb->fScrollFlags & CSBS_THUMBALWAYS) && !IsScrollInfoActive(si))
  170. return FALSE;
  171. else
  172. return TRUE;
  173. }
  174. //
  175. // Draw a standard scrollbar arrow
  176. //
  177. static int DrawScrollArrow(SCROLLBAR *sbar, HDC hdc, RECT *rect, UINT arrow, BOOL fMouseDown, BOOL fMouseOver)
  178. {
  179. UINT ret;
  180. UINT flags = arrow;
  181. //HACKY bit so this routine can be called by vertical and horizontal code
  182. if(sbar->nBarType == SB_VERT)
  183. {
  184. if(flags & DFCS_SCROLLLEFT) flags = flags & ~DFCS_SCROLLLEFT | DFCS_SCROLLUP;
  185. if(flags & DFCS_SCROLLRIGHT) flags = flags & ~DFCS_SCROLLRIGHT | DFCS_SCROLLDOWN;
  186. }
  187. if(fMouseDown) flags |= (DFCS_FLAT | DFCS_PUSHED);
  188. #ifdef FLAT_SCROLLBARS
  189. if(sbar->fFlatScrollbar != CSBS_NORMAL)
  190. {
  191. HDC hdcmem1, hdcmem2;
  192. HBITMAP hbm1, oldbm1;
  193. HBITMAP hbm2, oldbm2;
  194. RECT rc;
  195. int width, height;
  196. rc = *rect;
  197. width = rc.right-rc.left;
  198. height = rc.bottom-rc.top;
  199. SetRect(&rc, 0, 0, width, height);
  200. //MONOCHROME bitmap to convert the arrow to black/white mask
  201. hdcmem1 = CreateCompatibleDC(hdc);
  202. hbm1 = CreateBitmap(width, height, 1, 1, NULL);
  203. UnrealizeObject(hbm1);
  204. oldbm1 = (HBITMAP)SelectObject(hdcmem1, hbm1);
  205. //NORMAL bitmap to draw the arrow into
  206. hdcmem2 = CreateCompatibleDC(hdc);
  207. hbm2 = CreateCompatibleBitmap(hdc, width, height);
  208. UnrealizeObject(hbm2);
  209. oldbm2 = (HBITMAP)SelectObject(hdcmem2, hbm2);
  210. flags = flags & ~DFCS_PUSHED | DFCS_FLAT; //just in case
  211. DrawFrameControl(hdcmem2, &rc, DFC_SCROLL, flags);
  212. #ifndef HOT_TRACKING
  213. if(fMouseDown)
  214. {
  215. //uncomment these to make the cool scrollbars
  216. //look like the common controls flat scrollbars
  217. //fMouseDown = FALSE;
  218. //fMouseOver = TRUE;
  219. }
  220. #endif
  221. //draw a flat monochrome version of a scrollbar arrow (dark)
  222. if(fMouseDown)
  223. {
  224. SetBkColor(hdcmem2, GetSysColor(COLOR_BTNTEXT));
  225. BitBlt(hdcmem1, 0, 0, width, height, hdcmem2, 0, 0, SRCCOPY);
  226. SetBkColor(hdc, 0x00ffffff);
  227. SetTextColor(hdc, GetSysColor(COLOR_3DDKSHADOW));
  228. BitBlt(hdc, rect->left, rect->top, width, height, hdcmem1, 0, 0, SRCCOPY);
  229. }
  230. //draw a flat monochrome version of a scrollbar arrow (grey)
  231. else if(fMouseOver)
  232. {
  233. SetBkColor(hdcmem2, GetSysColor(COLOR_BTNTEXT));
  234. FillRect(hdcmem1, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH));
  235. BitBlt(hdcmem1, 0, 0, width, height, hdcmem2, 0, 0, SRCINVERT);
  236. SetBkColor(hdc, GetSysColor(COLOR_3DSHADOW));
  237. SetTextColor(hdc, 0x00ffffff);
  238. BitBlt(hdc, rect->left, rect->top, width, height, hdcmem1, 0, 0, SRCCOPY);
  239. }
  240. //draw the arrow normally
  241. else
  242. {
  243. BitBlt(hdc, rect->left, rect->top, width, height, hdcmem2, 0, 0, SRCCOPY);
  244. }
  245. SelectObject(hdcmem1, oldbm1);
  246. SelectObject(hdcmem2, oldbm2);
  247. DeleteObject(hbm1);
  248. DeleteObject(hbm2);
  249. DeleteDC(hdcmem1);
  250. DeleteDC(hdcmem2);
  251. ret = 0;
  252. }
  253. else
  254. #endif
  255. ret = DrawFrameControl(hdc, rect, DFC_SCROLL, flags);
  256. return ret;
  257. }
  258. //
  259. // Return the size in pixels for the specified scrollbar metric,
  260. // for the specified scrollbar SM_CXVERTSB
  261. //
  262. static int GetScrollMetric(SCROLLBAR *sbar, int metric)
  263. {
  264. if(sbar->nBarType == SB_HORZ)
  265. {
  266. if(metric == SM_CXHORZSB)
  267. {
  268. if(sbar->nArrowLength < 0)
  269. return -sbar->nArrowLength * GetSystemMetrics(SM_CXHSCROLL);
  270. else
  271. return sbar->nArrowLength;
  272. }
  273. else
  274. {
  275. if(sbar->nArrowWidth < 0)
  276. return -sbar->nArrowWidth * GetSystemMetrics(SM_CYHSCROLL);
  277. else
  278. return sbar->nArrowWidth;
  279. }
  280. }
  281. else if(sbar->nBarType == SB_VERT)
  282. {
  283. if(metric == SM_CYVERTSB)
  284. {
  285. if(sbar->nArrowLength < 0)
  286. return -sbar->nArrowLength * GetSystemMetrics(SM_CYVSCROLL);
  287. else
  288. return sbar->nArrowLength;
  289. }
  290. else
  291. {
  292. if(sbar->nArrowWidth < 0)
  293. return -sbar->nArrowWidth * GetSystemMetrics(SM_CXVSCROLL);
  294. else
  295. return sbar->nArrowWidth;
  296. }
  297. }
  298. return 0;
  299. }
  300. //
  301. //
  302. //
  303. static COLORREF GetSBForeColor(void)
  304. {
  305. COLORREF c1 = GetSysColor(COLOR_3DHILIGHT);
  306. COLORREF c2 = GetSysColor(COLOR_WINDOW);
  307. if(c1 != 0xffffff && c1 == c2)
  308. {
  309. return GetSysColor(COLOR_BTNFACE);
  310. }
  311. else
  312. {
  313. return GetSysColor(COLOR_3DHILIGHT);
  314. }
  315. }
  316. static COLORREF GetSBBackColor(void)
  317. {
  318. return GetSysColor(COLOR_SCROLLBAR);
  319. }
  320. //
  321. // Paint a checkered rectangle, with each alternate
  322. // pixel being assigned a different colour
  323. //
  324. static void DrawCheckedRect(HDC hdc, RECT *rect, COLORREF fg, COLORREF bg)
  325. {
  326. static WORD wCheckPat[8] =
  327. {
  328. 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555
  329. };
  330. HBITMAP hbmp;
  331. HBRUSH hbr, hbrold;
  332. COLORREF fgold, bgold;
  333. hbmp = CreateBitmap(8, 8, 1, 1, wCheckPat);
  334. hbr = CreatePatternBrush(hbmp);
  335. UnrealizeObject(hbr);
  336. SetBrushOrgEx(hdc, rect->left, rect->top, 0);
  337. hbrold = (HBRUSH)SelectObject(hdc, hbr);
  338. fgold = SetTextColor(hdc, fg);
  339. bgold = SetBkColor(hdc, bg);
  340. PatBlt(hdc, rect->left, rect->top,
  341. rect->right - rect->left,
  342. rect->bottom - rect->top,
  343. PATCOPY);
  344. SetBkColor(hdc, bgold);
  345. SetTextColor(hdc, fgold);
  346. SelectObject(hdc, hbrold);
  347. DeleteObject(hbr);
  348. DeleteObject(hbmp);
  349. }
  350. //
  351. // Fill the specifed rectangle using a solid colour
  352. //
  353. static void PaintRect(HDC hdc, RECT *rect, COLORREF color)
  354. {
  355. COLORREF oldcol = SetBkColor(hdc, color);
  356. ExtTextOut(hdc, 0, 0, ETO_OPAQUE, rect, _T(""), 0, 0);
  357. SetBkColor(hdc, oldcol);
  358. }
  359. //
  360. // Draw a simple blank scrollbar push-button. Can be used
  361. // to draw a push button, or the scrollbar thumb
  362. // drawflag - could set to BF_FLAT to make flat scrollbars
  363. //
  364. void DrawBlankButton(HDC hdc, const RECT *rect, UINT drawflag)
  365. {
  366. RECT rc = *rect;
  367. #ifndef FLAT_SCROLLBARS
  368. drawflag &= ~BF_FLAT;
  369. #endif
  370. DrawEdge(hdc, &rc, EDGE_RAISED, BF_RECT | drawflag | BF_ADJUST);
  371. FillRect(hdc, &rc, GetSysColorBrush(COLOR_BTNFACE));
  372. }
  373. //
  374. // Send a WM_VSCROLL or WM_HSCROLL message
  375. //
  376. static void SendScrollMessage(HWND hwnd, UINT scrMsg, UINT scrId, UINT pos)
  377. {
  378. SendMessage(hwnd, scrMsg, MAKEWPARAM(scrId, pos), 0);
  379. }
  380. //
  381. // Calculate the screen coordinates of the area taken by
  382. // the horizontal scrollbar. Take into account the size
  383. // of the window borders
  384. //
  385. static BOOL GetHScrollRect(SCROLLWND *sw, HWND hwnd, RECT *rect)
  386. {
  387. GetWindowRect(hwnd, rect);
  388. if(sw->fLeftScrollbar)
  389. {
  390. rect->left += sw->cxLeftEdge + (sw->sbarVert.fScrollVisible ?
  391. GetScrollMetric(&sw->sbarVert, SM_CXVERTSB) : 0);
  392. rect->right -= sw->cxRightEdge;
  393. }
  394. else
  395. {
  396. rect->left += sw->cxLeftEdge; //left window edge
  397. rect->right -= sw->cxRightEdge + //right window edge
  398. (sw->sbarVert.fScrollVisible ?
  399. GetScrollMetric(&sw->sbarVert, SM_CXVERTSB) : 0);
  400. }
  401. rect->bottom -= sw->cyBottomEdge; //bottom window edge
  402. rect->top = rect->bottom -
  403. (sw->sbarHorz.fScrollVisible ?
  404. GetScrollMetric(&sw->sbarHorz, SM_CYHORZSB) : 0);
  405. return TRUE;
  406. }
  407. //
  408. // Calculate the screen coordinates of the area taken by the
  409. // vertical scrollbar
  410. //
  411. static BOOL GetVScrollRect(SCROLLWND *sw, HWND hwnd, RECT *rect)
  412. {
  413. GetWindowRect(hwnd, rect);
  414. rect->top += sw->cyTopEdge; //top window edge
  415. rect->bottom -= sw->cyBottomEdge +
  416. (sw->sbarHorz.fScrollVisible ? //bottom window edge
  417. GetScrollMetric(&sw->sbarHorz, SM_CYHORZSB) : 0);
  418. if(sw->fLeftScrollbar)
  419. {
  420. rect->left += sw->cxLeftEdge;
  421. rect->right = rect->left + (sw->sbarVert.fScrollVisible ?
  422. GetScrollMetric(&sw->sbarVert, SM_CXVERTSB) : 0);
  423. }
  424. else
  425. {
  426. rect->right -= sw->cxRightEdge;
  427. // rect->left = rect->right - 18;
  428. rect->left = rect->right - (sw->sbarVert.fScrollVisible ?GetScrollMetric(&sw->sbarVert, SM_CXVERTSB) : 0);
  429. }
  430. return TRUE;
  431. }
  432. // Depending on what type of scrollbar nBar refers to, call the
  433. // appropriate Get?ScrollRect function
  434. //
  435. BOOL GetScrollRect(SCROLLWND *sw, UINT nBar, HWND hwnd, RECT *rect)
  436. {
  437. if(nBar == SB_HORZ)
  438. return GetHScrollRect(sw, hwnd, rect);
  439. else if(nBar == SB_VERT)
  440. return GetVScrollRect(sw, hwnd, rect);
  441. else
  442. return FALSE;
  443. }
  444. //
  445. // Work out the scrollbar width/height for either type of scrollbar (SB_HORZ/SB_VERT)
  446. // rect - coords of the scrollbar.
  447. // store results into *thumbsize and *thumbpos
  448. //
  449. static int CalcThumbSize(SCROLLBAR *sbar, const RECT *rect, int *pthumbsize, int *pthumbpos)
  450. {
  451. SCROLLINFO *si;
  452. int scrollsize; //total size of the scrollbar including arrow buttons
  453. int workingsize; //working area (where the thumb can slide)
  454. int siMaxMin;
  455. int butsize;
  456. int startcoord;
  457. int thumbpos = 0, thumbsize = 0;
  458. int adjust=0;
  459. static int count=0;
  460. //work out the width (for a horizontal) or the height (for a vertical)
  461. //of a standard scrollbar button
  462. butsize = GetScrollMetric(sbar, SM_SCROLL_LENGTH);
  463. if(1) //sbar->nBarType == SB_HORZ)
  464. {
  465. scrollsize = rect->right - rect->left;
  466. startcoord = rect->left;
  467. }
  468. /*else if(sbar->nBarType == SB_VERT)
  469. {
  470. scrollsize = rect->bottom - rect->top;
  471. startcoord = rect->top;
  472. }
  473. else
  474. {
  475. return 0;
  476. }*/
  477. si = &sbar->scrollInfo;
  478. siMaxMin = si->nMax - si->nMin + 1;
  479. workingsize = scrollsize - butsize * 2;
  480. //
  481. // Work out the scrollbar thumb SIZE
  482. //
  483. if(si->nPage == 0)
  484. {
  485. thumbsize = butsize;
  486. }
  487. else if(siMaxMin > 0)
  488. {
  489. thumbsize = MulDiv(si->nPage, workingsize, siMaxMin);
  490. if(thumbsize < sbar->nMinThumbSize)
  491. thumbsize = sbar->nMinThumbSize;
  492. }
  493. //
  494. // Work out the scrollbar thumb position
  495. //
  496. if(siMaxMin > 0)
  497. {
  498. int pagesize = max(1, si->nPage);
  499. thumbpos = MulDiv(si->nPos - si->nMin, workingsize-thumbsize, siMaxMin - pagesize);
  500. if(thumbpos < 0)
  501. thumbpos = 0;
  502. if(thumbpos >= workingsize-thumbsize)
  503. thumbpos = workingsize-thumbsize;
  504. }
  505. thumbpos += startcoord + butsize;
  506. *pthumbpos = thumbpos;
  507. *pthumbsize = thumbsize;
  508. return 1;
  509. }
  510. //
  511. // return a hit-test value for whatever part of the scrollbar x,y is located in
  512. // rect, x, y: SCREEN coordinates
  513. // the rectangle must not include space for any inserted buttons
  514. // (i.e, JUST the scrollbar area)
  515. //
  516. static UINT GetHorzScrollPortion(SCROLLBAR *sbar, HWND hwnd, const RECT *rect, int x, int y)
  517. {
  518. int thumbwidth, thumbpos;
  519. int butwidth = GetScrollMetric(sbar, SM_SCROLL_LENGTH);
  520. int scrollwidth = rect->right-rect->left;
  521. int workingwidth = scrollwidth - butwidth*2;
  522. if(y < rect->top || y >= rect->bottom)
  523. return HTSCROLL_NONE;
  524. CalcThumbSize(sbar, rect, &thumbwidth, &thumbpos);
  525. //if we have had to scale the buttons to fit in the rect,
  526. //then adjust the button width accordingly
  527. if(scrollwidth <= butwidth * 2)
  528. {
  529. butwidth = scrollwidth / 2;
  530. }
  531. //check for left button click
  532. if(x >= rect->left && x < rect->left + butwidth)
  533. {
  534. return HTSCROLL_LEFT;
  535. }
  536. //check for right button click
  537. else if(x >= rect->right-butwidth && x < rect->right)
  538. {
  539. return HTSCROLL_RIGHT;
  540. }
  541. //if the thumb is too big to fit (i.e. it isn't visible)
  542. //then return a NULL scrollbar area
  543. if(thumbwidth >= workingwidth)
  544. return HTSCROLL_NONE;
  545. //check for point in the thumbbar
  546. if(x >= thumbpos && x < thumbpos+thumbwidth)
  547. {
  548. return HTSCROLL_THUMB;
  549. }
  550. //check for left margin
  551. else if(x >= rect->left+butwidth && x < thumbpos)
  552. {
  553. return HTSCROLL_PAGELEFT;
  554. }
  555. else if(x >= thumbpos+thumbwidth && x < rect->right-butwidth)
  556. {
  557. return HTSCROLL_PAGERIGHT;
  558. }
  559. return HTSCROLL_NONE;
  560. }
  561. //
  562. // For vertical scrollbars, rotate all coordinates by -90 degrees
  563. // so that we can use the horizontal version of this function
  564. //
  565. static UINT GetVertScrollPortion(SCROLLBAR *sb, HWND hwnd, RECT *rect, int x, int y)
  566. {
  567. UINT r;
  568. RotateRect(rect);
  569. r = GetHorzScrollPortion(sb, hwnd, rect, y, x);
  570. RotateRect(rect);
  571. return r;
  572. }
  573. //
  574. // CUSTOM DRAW support
  575. //
  576. static LRESULT PostCustomPrePostPaint0(HWND hwnd, HDC hdc, SCROLLBAR *sb, UINT dwStage)
  577. {
  578. NMCSBCUSTOMDRAW nmcd;
  579. CoolSB_ZeroMemory(&nmcd, sizeof nmcd);
  580. nmcd.hdr.hwndFrom = hwnd;
  581. nmcd.hdr.idFrom = GetWindowLong(hwnd, GWL_ID);
  582. nmcd.hdr.code = NM_COOLSB_CUSTOMDRAW;
  583. nmcd.nBar = sb->nBarType;
  584. nmcd.dwDrawStage = dwStage;
  585. nmcd.hdc = hdc;
  586. hwnd = GetParent(hwnd);
  587. return SendMessage(hwnd, WM_NOTIFY, 0, (LPARAM)&nmcd);
  588. }
  589. static LRESULT PostCustomDrawNotify0(HWND hwnd, HDC hdc, UINT nBar, RECT *prect, UINT nItem, BOOL fMouseDown, BOOL fMouseOver, BOOL fInactive)
  590. {
  591. NMCSBCUSTOMDRAW nmcd;
  592. //fill in the standard header
  593. nmcd.hdr.hwndFrom = hwnd;
  594. nmcd.hdr.idFrom = GetWindowLong(hwnd, GWL_ID);
  595. nmcd.hdr.code = NM_COOLSB_CUSTOMDRAW;
  596. nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
  597. nmcd.nBar = nBar;
  598. nmcd.rect = *prect;
  599. nmcd.uItem = nItem;
  600. nmcd.hdc = hdc;
  601. if(fMouseDown)
  602. nmcd.uState = CDIS_SELECTED;
  603. else if(fMouseOver)
  604. nmcd.uState = CDIS_HOT;
  605. else if(fInactive)
  606. nmcd.uState = CDIS_DISABLED;
  607. else
  608. nmcd.uState = CDIS_DEFAULT;
  609. hwnd = GetParent(hwnd);
  610. return SendMessage(hwnd, WM_NOTIFY, nmcd.hdr.idFrom, (LPARAM)&nmcd);
  611. }
  612. // Depending on if we are supporting custom draw, either define
  613. // a macro to the function name, or to nothing at all. If custom draw
  614. // is turned off, then we can save ALOT of code space by binning all
  615. // calls to the custom draw support.
  616. //
  617. #define PostCustomDrawNotify PostCustomDrawNotify0
  618. #define PostCustomPrePostPaint PostCustomPrePostPaint0
  619. static LRESULT PostMouseNotify0(HWND hwnd, UINT msg, UINT nBar, RECT *prect, UINT nCmdId, POINT pt)
  620. {
  621. #ifdef NOTIFY_MOUSE
  622. NMCOOLBUTMSG nmcb;
  623. //fill in the standard header
  624. nmcb.hdr.hwndFrom = hwnd;
  625. nmcb.hdr.idFrom = GetWindowLong(hwnd, GWL_ID);
  626. nmcb.hdr.code = NM_CLICK;
  627. nmcb.nBar = nBar;
  628. nmcb.uCmdId = nCmdId;
  629. nmcb.uState = 0;
  630. nmcb.rect = *prect;
  631. nmcb.pt = pt;
  632. hwnd = GetParent(hwnd);
  633. return SendMessage(hwnd, WM_NOTIFY, nmcb.hdr.idFrom, (LPARAM)&nmcb);
  634. #else
  635. return 0;
  636. #endif
  637. }
  638. #ifdef NOTIFY_MOUSE
  639. #define PostMouseNotify PostMouseNotify0
  640. #else
  641. #define PostMouseNotify 1 ? (void)0 : PostMouseNotify0
  642. #endif
  643. //
  644. // Draw a complete HORIZONTAL scrollbar in the given rectangle
  645. // Don't draw any inserted buttons in this procedure
  646. //
  647. // uDrawFlags - hittest code, to say if to draw the
  648. // specified portion in an active state or not.
  649. //
  650. //
  651. static LRESULT NCDrawHScrollbar(SCROLLBAR *sb, HWND hwnd, HDC hdc, const RECT *rect, UINT uDrawFlags)
  652. {
  653. SCROLLINFO *si;
  654. RECT ctrl, thumb;
  655. RECT sbm;
  656. int butwidth = GetScrollMetric(sb, SM_SCROLL_LENGTH);
  657. int scrollwidth = rect->right-rect->left;
  658. int workingwidth = scrollwidth - butwidth*2;
  659. int thumbwidth = 0, thumbpos = 0;
  660. int siMaxMin;
  661. BOOL fCustomDraw = 0;
  662. BOOL fMouseDownL = 0, fMouseOverL = 0, fBarHot = 0;
  663. BOOL fMouseDownR = 0, fMouseOverR = 0;
  664. COLORREF crCheck1 = GetSBForeColor();
  665. COLORREF crCheck2 = GetSBBackColor();
  666. COLORREF crInverse1 = InvertCOLORREF(crCheck1);
  667. COLORREF crInverse2 = InvertCOLORREF(crCheck2);
  668. UINT uDFCFlat = sb->fFlatScrollbar ? DFCS_FLAT : 0;
  669. UINT uDEFlat = sb->fFlatScrollbar ? BF_FLAT : 0;
  670. //drawing flags to modify the appearance of the scrollbar buttons
  671. UINT uLeftButFlags = DFCS_SCROLLLEFT;
  672. UINT uRightButFlags = DFCS_SCROLLRIGHT;
  673. if(scrollwidth <= 0)
  674. return 0;
  675. si = &sb->scrollInfo;
  676. siMaxMin = si->nMax - si->nMin;
  677. if(hwnd != hwndCurCoolSB)
  678. uDrawFlags = HTSCROLL_NONE;
  679. //
  680. // work out the thumb size and position
  681. //
  682. CalcThumbSize(sb, rect, &thumbwidth, &thumbpos);
  683. if(sb->fScrollFlags & ESB_DISABLE_LEFT) uLeftButFlags |= DFCS_INACTIVE;
  684. if(sb->fScrollFlags & ESB_DISABLE_RIGHT) uRightButFlags |= DFCS_INACTIVE;
  685. //if we need to grey the arrows because there is no data to scroll
  686. if(!IsScrollInfoActive(si) && !(sb->fScrollFlags & CSBS_THUMBALWAYS))
  687. {
  688. uLeftButFlags |= DFCS_INACTIVE;
  689. uRightButFlags |= DFCS_INACTIVE;
  690. }
  691. if(hwnd == hwndCurCoolSB)
  692. {
  693. #ifdef FLAT_SCROLLBARS
  694. BOOL ldis = !(uLeftButFlags & DFCS_INACTIVE);
  695. BOOL rdis = !(uRightButFlags & DFCS_INACTIVE);
  696. fBarHot = (sb->nBarType == (int)uMouseOverScrollbar && sb->fFlatScrollbar == CSBS_HOTTRACKED);
  697. fMouseOverL = uHitTestPortion == HTSCROLL_LEFT && fBarHot && ldis;
  698. fMouseOverR = uHitTestPortion == HTSCROLL_RIGHT && fBarHot && rdis;
  699. #endif
  700. fMouseDownL = (uDrawFlags == HTSCROLL_LEFT);
  701. fMouseDownR = (uDrawFlags == HTSCROLL_RIGHT);
  702. }
  703. #ifdef CUSTOM_DRAW
  704. fCustomDraw = ((PostCustomPrePostPaint(hwnd, hdc, sb, CDDS_PREPAINT)) == CDRF_SKIPDEFAULT);
  705. #endif
  706. //
  707. // Draw the scrollbar now
  708. //
  709. if(scrollwidth > butwidth*2)
  710. {
  711. //LEFT ARROW
  712. SetRect(&ctrl, rect->left, rect->top, rect->left + butwidth, rect->bottom);
  713. RotateRect0(sb, &ctrl);
  714. if(fCustomDraw)
  715. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_LINELEFT, fMouseDownL, fMouseOverL, uLeftButFlags & DFCS_INACTIVE);
  716. else
  717. DrawScrollArrow(sb, hdc, &ctrl, uLeftButFlags, fMouseDownL, fMouseOverL);
  718. RotateRect0(sb, &ctrl);
  719. //MIDDLE PORTION
  720. //if we can fit the thumbbar in, then draw it
  721. if(thumbwidth > 0 && thumbwidth <= workingwidth
  722. && IsScrollInfoActive(si) && ((sb->fScrollFlags & ESB_DISABLE_BOTH) != ESB_DISABLE_BOTH))
  723. {
  724. //Draw the scrollbar margin above the thumb
  725. SetRect(&sbm, rect->left + butwidth, rect->top, thumbpos, rect->bottom);
  726. RotateRect0(sb, &sbm);
  727. if(fCustomDraw)
  728. {
  729. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &sbm, SB_PAGELEFT, uDrawFlags == HTSCROLL_PAGELEFT, FALSE, FALSE);
  730. }
  731. else
  732. {
  733. if(uDrawFlags == HTSCROLL_PAGELEFT)
  734. DrawCheckedRect(hdc, &sbm, crInverse1, crInverse2);
  735. else
  736. DrawCheckedRect(hdc, &sbm, crCheck1, crCheck2);
  737. }
  738. RotateRect0(sb, &sbm);
  739. //Draw the margin below the thumb
  740. sbm.left = thumbpos+thumbwidth;
  741. sbm.right = rect->right - butwidth;
  742. RotateRect0(sb, &sbm);
  743. if(fCustomDraw)
  744. {
  745. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &sbm, SB_PAGERIGHT, uDrawFlags == HTSCROLL_PAGERIGHT, 0, 0);
  746. }
  747. else
  748. {
  749. if(uDrawFlags == HTSCROLL_PAGERIGHT)
  750. DrawCheckedRect(hdc, &sbm, crInverse1, crInverse2);
  751. else
  752. DrawCheckedRect(hdc, &sbm, crCheck1, crCheck2);
  753. }
  754. RotateRect0(sb, &sbm);
  755. //Draw the THUMB finally
  756. SetRect(&thumb, thumbpos, rect->top, thumbpos+thumbwidth, rect->bottom);
  757. RotateRect0(sb, &thumb);
  758. if(fCustomDraw)
  759. {
  760. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &thumb, SB_THUMBTRACK, uDrawFlags==HTSCROLL_THUMB, uHitTestPortion == HTSCROLL_THUMB && fBarHot, FALSE);
  761. }
  762. else
  763. {
  764. #ifdef FLAT_SCROLLBARS
  765. if(hwnd == hwndCurCoolSB && sb->fFlatScrollbar && (uDrawFlags == HTSCROLL_THUMB ||
  766. (uHitTestPortion == HTSCROLL_THUMB && fBarHot)))
  767. {
  768. PaintRect(hdc, &thumb, GetSysColor(COLOR_3DSHADOW));
  769. }
  770. else
  771. #endif
  772. {
  773. DrawBlankButton(hdc, &thumb, uDEFlat);
  774. }
  775. }
  776. RotateRect0(sb, &thumb);
  777. }
  778. //otherwise, just leave that whole area blank
  779. else
  780. {
  781. OffsetRect(&ctrl, butwidth, 0);
  782. ctrl.right = rect->right - butwidth;
  783. //if we always show the thumb covering the whole scrollbar,
  784. //then draw it that way
  785. if(!IsScrollInfoActive(si) && (sb->fScrollFlags & CSBS_THUMBALWAYS)
  786. && ctrl.right - ctrl.left > sb->nMinThumbSize)
  787. {
  788. //leave a 1-pixel gap between the thumb + right button
  789. ctrl.right --;
  790. RotateRect0(sb, &ctrl);
  791. if(fCustomDraw)
  792. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_THUMBTRACK, fMouseDownL, FALSE, FALSE);
  793. else
  794. {
  795. #ifdef FLAT_SCROLLBARS
  796. if(sb->fFlatScrollbar == CSBS_HOTTRACKED && uDrawFlags == HTSCROLL_THUMB)
  797. PaintRect(hdc, &ctrl, GetSysColor(COLOR_3DSHADOW));
  798. else
  799. #endif
  800. DrawBlankButton(hdc, &ctrl, uDEFlat);
  801. }
  802. RotateRect0(sb, &ctrl);
  803. //draw the single-line gap
  804. ctrl.left = ctrl.right;
  805. ctrl.right += 1;
  806. RotateRect0(sb, &ctrl);
  807. if(fCustomDraw)
  808. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_PAGERIGHT, 0, 0, 0);
  809. else
  810. PaintRect(hdc, &ctrl, GetSysColor(COLOR_SCROLLBAR));
  811. RotateRect0(sb, &ctrl);
  812. }
  813. //otherwise, paint a blank if the thumb doesn't fit in
  814. else
  815. {
  816. RotateRect0(sb, &ctrl);
  817. if(fCustomDraw)
  818. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_PAGERIGHT, 0, 0, 0);
  819. else
  820. DrawCheckedRect(hdc, &ctrl, crCheck1, crCheck2);
  821. RotateRect0(sb, &ctrl);
  822. }
  823. }
  824. //RIGHT ARROW
  825. SetRect(&ctrl, rect->right - butwidth, rect->top, rect->right, rect->bottom);
  826. RotateRect0(sb, &ctrl);
  827. if(fCustomDraw)
  828. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_LINERIGHT, fMouseDownR, fMouseOverR, uRightButFlags & DFCS_INACTIVE);
  829. else
  830. DrawScrollArrow(sb, hdc, &ctrl, uRightButFlags, fMouseDownR, fMouseOverR);
  831. RotateRect0(sb, &ctrl);
  832. }
  833. //not enough room for the scrollbar, so just draw the buttons (scaled in size to fit)
  834. else
  835. {
  836. butwidth = scrollwidth / 2;
  837. //LEFT ARROW
  838. SetRect(&ctrl, rect->left, rect->top, rect->left + butwidth, rect->bottom);
  839. RotateRect0(sb, &ctrl);
  840. if(fCustomDraw)
  841. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_LINELEFT, fMouseDownL, fMouseOverL, uLeftButFlags & DFCS_INACTIVE);
  842. else
  843. DrawScrollArrow(sb, hdc, &ctrl, uLeftButFlags, fMouseDownL, fMouseOverL);
  844. RotateRect0(sb, &ctrl);
  845. //RIGHT ARROW
  846. OffsetRect(&ctrl, scrollwidth - butwidth, 0);
  847. RotateRect0(sb, &ctrl);
  848. if(fCustomDraw)
  849. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_LINERIGHT, fMouseDownR, fMouseOverR, uRightButFlags & DFCS_INACTIVE);
  850. else
  851. DrawScrollArrow(sb, hdc, &ctrl, uRightButFlags, fMouseDownR, fMouseOverR);
  852. RotateRect0(sb, &ctrl);
  853. //if there is a gap between the buttons, fill it with a solid color
  854. //if(butwidth & 0x0001)
  855. if(ctrl.left != rect->left + butwidth)
  856. {
  857. ctrl.left --;
  858. ctrl.right -= butwidth;
  859. RotateRect0(sb, &ctrl);
  860. if(fCustomDraw)
  861. PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_PAGERIGHT, 0, 0, 0);
  862. else
  863. DrawCheckedRect(hdc, &ctrl, crCheck1, crCheck2);
  864. RotateRect0(sb, &ctrl);
  865. }
  866. }
  867. #ifdef CUSTOM_DRAW
  868. PostCustomPrePostPaint(hwnd, hdc, sb, CDDS_POSTPAINT);
  869. #endif
  870. return fCustomDraw;
  871. }
  872. //
  873. // Draw a vertical scrollbar using the horizontal draw routine, but
  874. // with the coordinates adjusted accordingly
  875. //
  876. static LRESULT NCDrawVScrollbar(SCROLLBAR *sb, HWND hwnd, HDC hdc, const RECT *rect, UINT uDrawFlags)
  877. {
  878. LRESULT ret;
  879. RECT rc;
  880. rc = *rect;
  881. RotateRect(&rc);
  882. ret = NCDrawHScrollbar(sb, hwnd, hdc, &rc, uDrawFlags);
  883. RotateRect(&rc);
  884. return ret;
  885. }
  886. //
  887. // Generic wrapper function for the scrollbar drawing
  888. //
  889. static LRESULT NCDrawScrollbar(SCROLLBAR *sb, HWND hwnd, HDC hdc, const RECT *rect, UINT uDrawFlags)
  890. {
  891. if(sb->nBarType == SB_HORZ)
  892. return NCDrawHScrollbar(sb, hwnd, hdc, rect, uDrawFlags);
  893. else
  894. return NCDrawVScrollbar(sb, hwnd, hdc, rect, uDrawFlags);
  895. }
  896. //
  897. // Define these two for proper processing of NCPAINT
  898. // NOT needed if we don't bother to mask the scrollbars we draw
  899. // to prevent the old window procedure from accidently drawing over them
  900. //
  901. HDC CoolSB_GetDC(HWND hwnd, WPARAM wParam)
  902. {
  903. // I just can't figure out GetDCEx, so I'll just use this:
  904. return GetWindowDC(hwnd);
  905. /*
  906. RECT rc;
  907. DWORD flags = 0x10000;
  908. HRGN hrgn = (HRGN)wParam;
  909. if(hrgn == (HRGN)1)
  910. {
  911. GetWindowRect(hwnd, &rc);
  912. OffsetRect(&rc, -rc.left, -rc.top);
  913. hrgn = CreateRectRgnIndirect(&rc);
  914. }
  915. if(GetWindowLong(hwnd, GWL_STYLE) & WS_CLIPCHILDREN)
  916. flags |= DCX_CLIPCHILDREN;
  917. if(GetWindowLong(hwnd, GWL_STYLE) & WS_CLIPSIBLINGS)
  918. flags |= DCX_CLIPSIBLINGS;
  919. return GetDCEx(hwnd, hrgn, flags | DCX_CACHE|DCX_NORESETATTRS|DCX_WINDOW | DCX_INTERSECTUPDATE);
  920. */
  921. //return GetDCEx(hwnd, NULL, flags | DCX_WINDOW| DCX_NORESETATTRS);
  922. }
  923. static void NCPaint(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
  924. {
  925. SCROLLBAR *sb;
  926. HDC hdc;
  927. HRGN hrgn;
  928. RECT winrect, rect;
  929. BOOL fUpdateAll = ((LONG)wParam == 1);
  930. BOOL fCustomDraw = FALSE;
  931. GetWindowRect(hwnd, &winrect);
  932. int aaa=GetSystemMetrics(SM_CXVSCROLL);
  933. //if entire region needs painting, then make a region to cover the entire window
  934. if(fUpdateAll)
  935. hrgn = (HRGN)wParam;
  936. else
  937. hrgn = (HRGN)wParam;
  938. //hdc = GetWindowDC(hwnd);
  939. hdc = CoolSB_GetDC(hwnd, wParam);
  940. ///////////////////////////////////////////////////
  941. CRect rcItem;
  942. GetWindowRect(hwnd, &rcItem);
  943. rcItem.right -=rcItem.left ;
  944. rcItem.bottom -=rcItem.top ;
  945. rcItem.left =rcItem.top =0;
  946. ::FrameRect(hdc, rcItem, g_frameedgebrush);
  947. InflateRect(&rcItem, -1, -1);
  948. FrameRect(hdc, &rcItem, (HBRUSH) ::GetStockObject (WHITE_BRUSH));
  949. ////////////////////////////////////////////////////////////////////////////
  950. //
  951. // Only draw the horizontal scrollbar if the window is tall enough
  952. //
  953. sb = &sw->sbarHorz;
  954. if(sb->fScrollVisible)
  955. {
  956. int hbarwidth = 0, leftright = 0;
  957. //get the screen coordinates of the whole horizontal scrollbar area
  958. GetHScrollRect(sw, hwnd, &rect);
  959. //make the coordinates relative to the window for drawing
  960. OffsetRect(&rect, -winrect.left, -winrect.top);
  961. if(uCurrentScrollbar == SB_HORZ)
  962. fCustomDraw |= NCDrawHScrollbar(sb, hwnd, hdc, &rect, uScrollTimerPortion);
  963. else
  964. fCustomDraw |= NCDrawHScrollbar(sb, hwnd, hdc, &rect, HTSCROLL_NONE);
  965. }
  966. //
  967. // Only draw the vertical scrollbar if the window is wide enough to accomodate it
  968. //
  969. sb = &sw->sbarVert;
  970. if(sb->fScrollVisible)
  971. {
  972. int vbarheight = 0, updown = 0;
  973. //get the screen cooridinates of the whole horizontal scrollbar area
  974. GetVScrollRect(sw, hwnd, &rect);
  975. //make the coordinates relative to the window for drawing
  976. OffsetRect(&rect, -winrect.left, -winrect.top);
  977. if(uCurrentScrollbar == SB_VERT)
  978. fCustomDraw |= NCDrawVScrollbar(sb, hwnd, hdc, &rect, uScrollTimerPortion);
  979. else
  980. fCustomDraw |= NCDrawVScrollbar(sb, hwnd, hdc, &rect, HTSCROLL_NONE);
  981. }
  982. // DRAW THE DEAD AREA
  983. // only do this if the horizontal and vertical bars are visible
  984. if(sw->sbarHorz.fScrollVisible && sw->sbarVert.fScrollVisible)
  985. {
  986. GetWindowRect(hwnd, &rect);
  987. OffsetRect(&rect, -winrect.left, -winrect.top);
  988. rect.bottom -= sw->cyBottomEdge;
  989. rect.top = rect.bottom - GetScrollMetric(&sw->sbarHorz, SM_CYHORZSB);
  990. if(sw->fLeftScrollbar)
  991. {
  992. rect.left += sw->cxLeftEdge;
  993. rect.right = rect.left + GetScrollMetric(&sw->sbarVert, SM_CXVERTSB);
  994. }
  995. else
  996. {
  997. rect.right -= sw->cxRightEdge;
  998. rect.left = rect.right - GetScrollMetric(&sw->sbarVert, SM_CXVERTSB);
  999. }
  1000. PostCustomDrawNotify(hwnd, hdc, SB_BOTH, &rect, 32, 0, 0, 0);
  1001. }
  1002. ReleaseDC(hwnd, hdc);
  1003. }
  1004. //
  1005. // Need to detect if we have clicked in the scrollbar region or not
  1006. //
  1007. static LRESULT NCHitTest(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
  1008. {
  1009. RECT hrect;
  1010. RECT vrect;
  1011. POINT pt;
  1012. pt.x = LOWORD(lParam);
  1013. pt.y = HIWORD(lParam);
  1014. //work out exactly where the Horizontal and Vertical scrollbars are
  1015. GetHScrollRect(sw, hwnd, &hrect);
  1016. GetVScrollRect(sw, hwnd, &vrect);
  1017. //Clicked in the horizontal scrollbar area
  1018. if(sw->sbarHorz.fScrollVisible && PtInRect(&hrect, pt))
  1019. {
  1020. return HTHSCROLL;
  1021. }
  1022. //Clicked in the vertical scrollbar area
  1023. else if(sw->sbarVert.fScrollVisible && PtInRect(&vrect, pt))
  1024. {
  1025. return HTVSCROLL;
  1026. }
  1027. //clicked somewhere else
  1028. else
  1029. {
  1030. return CallWindowProc(sw->oldproc, hwnd, WM_NCHITTEST, wParam, lParam);
  1031. }
  1032. }
  1033. //
  1034. // Return a HT* value indicating what part of the scrollbar was clicked
  1035. // Rectangle is not adjusted
  1036. //
  1037. static UINT GetHorzPortion(SCROLLBAR *sb, HWND hwnd, RECT *rect, int x, int y)
  1038. {
  1039. RECT rc = *rect;
  1040. if(y < rc.top || y >= rc.bottom) return HTSCROLL_NONE;
  1041. //Now we have the rectangle for the scrollbar itself, so work out
  1042. //what part we clicked on.
  1043. return GetHorzScrollPortion(sb, hwnd, &rc, x, y);
  1044. }
  1045. //
  1046. // Just call the horizontal version, with adjusted coordinates
  1047. //
  1048. static UINT GetVertPortion(SCROLLBAR *sb, HWND hwnd, RECT *rect, int x, int y)
  1049. {
  1050. UINT ret;
  1051. RotateRect(rect);
  1052. ret = GetHorzPortion(sb, hwnd, rect, y, x);
  1053. RotateRect(rect);
  1054. return ret;
  1055. }
  1056. //
  1057. // Wrapper function for GetHorzPortion and GetVertPortion
  1058. //
  1059. static UINT GetPortion(SCROLLBAR *sb, HWND hwnd, RECT *rect, int x, int y)
  1060. {
  1061. if(sb->nBarType == SB_HORZ)
  1062. return GetHorzPortion(sb, hwnd, rect, x, y);
  1063. else if(sb->nBarType == SB_VERT)
  1064. return GetVertPortion(sb, hwnd, rect, x, y);
  1065. else
  1066. return HTSCROLL_NONE;
  1067. }
  1068. //
  1069. // Input: rectangle of the total scrollbar area
  1070. // Output: adjusted to take the inserted buttons into account
  1071. //
  1072. static void GetRealHorzScrollRect(SCROLLBAR *sb, RECT *rect)
  1073. {
  1074. if(sb->fButVisibleBefore) rect->left += sb->nButSizeBefore;
  1075. if(sb->fButVisibleAfter) rect->right -= sb->nButSizeAfter;
  1076. }
  1077. //
  1078. // Input: rectangle of the total scrollbar area
  1079. // Output: adjusted to take the inserted buttons into account
  1080. //
  1081. static void GetRealVertScrollRect(SCROLLBAR *sb, RECT *rect)
  1082. {
  1083. if(sb->fButVisibleBefore) rect->top += sb->nButSizeBefore;
  1084. if(sb->fButVisibleAfter) rect->bottom -= sb->nButSizeAfter;
  1085. }
  1086. //
  1087. // Decide which type of scrollbar we have before calling
  1088. // the real function to do the job
  1089. //
  1090. static void GetRealScrollRect(SCROLLBAR *sb, RECT *rect)
  1091. {
  1092. if(sb->nBarType == SB_HORZ)
  1093. {
  1094. GetRealHorzScrollRect(sb, rect);
  1095. }
  1096. else if(sb->nBarType == SB_VERT)
  1097. {
  1098. GetRealVertScrollRect(sb, rect);
  1099. }
  1100. }
  1101. //
  1102. // Left button click in the non-client area
  1103. //
  1104. static LRESULT NCLButtonDown(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
  1105. {
  1106. RECT rect, winrect;
  1107. HDC hdc;
  1108. SCROLLBAR *sb;
  1109. SCROLLBUT *sbut = 0;
  1110. POINT pt;
  1111. pt.x = LOWORD(lParam);
  1112. pt.y = HIWORD(lParam);
  1113. hwndCurCoolSB = hwnd;
  1114. /////////////////////////////////////////////////////////////////////////////////
  1115. //
  1116. // HORIZONTAL SCROLLBAR PROCESSING
  1117. //
  1118. if(wParam == HTHSCROLL)
  1119. {
  1120. uScrollTimerMsg = WM_HSCROLL;
  1121. uCurrentScrollbar = SB_HORZ;
  1122. sb = &sw->sbarHorz;
  1123. //get the total area of the normal Horz scrollbar area
  1124. GetHScrollRect(sw, hwnd, &rect);
  1125. uCurrentScrollPortion = GetHorzPortion(sb, hwnd, &rect, LOWORD(lParam), HIWORD(lParam));
  1126. }
  1127. //
  1128. // VERTICAL SCROLLBAR PROCESSING
  1129. //
  1130. else if(wParam == HTVSCROLL)
  1131. {
  1132. uScrollTimerMsg = WM_VSCROLL;
  1133. uCurrentScrollbar = SB_VERT;
  1134. sb = &sw->sbarVert;
  1135. //get the total area of the normal Horz scrollbar area
  1136. GetVScrollRect(sw, hwnd, &rect);
  1137. uCurrentScrollPortion = GetVertPortion(sb, hwnd, &rect, LOWORD(lParam), HIWORD(lParam));
  1138. }
  1139. //
  1140. // NORMAL PROCESSING
  1141. //
  1142. else
  1143. {
  1144. uCurrentScrollPortion = HTSCROLL_NONE;
  1145. return CallWindowProc(sw->oldproc, hwnd, WM_NCLBUTTONDOWN, wParam, lParam);
  1146. }
  1147. //
  1148. // we can now share the same code for vertical
  1149. // and horizontal scrollbars
  1150. //
  1151. switch(uCurrentScrollPortion)
  1152. {
  1153. case HTSCROLL_THUMB:
  1154. //if the scrollbar is disabled, then do no further processing
  1155. if(!IsScrollbarActive(sb))
  1156. return 0;
  1157. GetRealScrollRect(sb, &rect);
  1158. RotateRect0(sb, &rect);
  1159. CalcThumbSize(sb, &rect, &nThumbSize, &nThumbPos);
  1160. RotateRect0(sb, &rect);
  1161. //remember the bounding rectangle of the scrollbar work area
  1162. rcThumbBounds = rect;
  1163. sw->fThumbTracking = TRUE;
  1164. sb->scrollInfo.nTrackPos = sb->scrollInfo.nPos;
  1165. if(wParam == HTVSCROLL)
  1166. nThumbMouseOffset = pt.y - nThumbPos;
  1167. else
  1168. nThumbMouseOffset = pt.x - nThumbPos;
  1169. nLastPos = -sb->scrollInfo.nPos;
  1170. nThumbPos0 = nThumbPos;
  1171. //if(sb->fFlatScrollbar)
  1172. //{
  1173. GetWindowRect(hwnd, &winrect);
  1174. OffsetRect(&rect, -winrect.left, -winrect.top);
  1175. hdc = GetWindowDC(hwnd);
  1176. NCDrawScrollbar(sb, hwnd, hdc, &rect, HTSCROLL_THUMB);
  1177. ReleaseDC(hwnd, hdc);
  1178. //}
  1179. break;
  1180. //Any part of the scrollbar
  1181. case HTSCROLL_LEFT:
  1182. if(sb->fScrollFlags & ESB_DISABLE_LEFT) return 0;
  1183. else goto target1;
  1184. case HTSCROLL_RIGHT:
  1185. if(sb->fScrollFlags & ESB_DISABLE_RIGHT) return 0;
  1186. else goto target1;
  1187. goto target1;
  1188. case HTSCROLL_PAGELEFT: case HTSCROLL_PAGERIGHT:
  1189. target1:
  1190. //if the scrollbar is disabled, then do no further processing
  1191. if(!IsScrollbarActive(sb))
  1192. break;
  1193. //ajust the horizontal rectangle to NOT include
  1194. //any inserted buttons
  1195. GetRealScrollRect(sb, &rect);
  1196. SendScrollMessage(hwnd, uScrollTimerMsg, uCurrentScrollPortion, 0);
  1197. // Check what area the mouse is now over :
  1198. // If the scroll thumb has moved under the mouse in response to
  1199. // a call to SetScrollPos etc, then we don't hilight the scrollbar margin
  1200. if(uCurrentScrollbar == SB_HORZ)
  1201. uScrollTimerPortion = GetHorzScrollPortion(sb, hwnd, &rect, pt.x, pt.y);
  1202. else
  1203. uScrollTimerPortion = GetVertScrollPortion(sb, hwnd, &rect, pt.x, pt.y);
  1204. GetWindowRect(hwnd, &winrect);
  1205. OffsetRect(&rect, -winrect.left, -winrect.top);
  1206. hdc = GetWindowDC(hwnd);
  1207. #ifndef HOT_TRACKING
  1208. //if we aren't hot-tracking, then don't highlight
  1209. //the scrollbar thumb unless we click on it
  1210. if(uScrollTimerPortion == HTSCROLL_THUMB)
  1211. uScrollTimerPortion = HTSCROLL_NONE;
  1212. #endif
  1213. NCDrawScrollbar(sb, hwnd, hdc, &rect, uScrollTimerPortion);
  1214. ReleaseDC(hwnd, hdc);
  1215. //Post the scroll message!!!!
  1216. uScrollTimerPortion = uCurrentScrollPortion;
  1217. //set a timer going on the first click.
  1218. //if this one expires, then we can start off a more regular timer
  1219. //to generate the auto-scroll behaviour
  1220. uScrollTimerId = SetTimer(hwnd, COOLSB_TIMERID1, COOLSB_TIMERINTERVAL1, 0);
  1221. break;
  1222. default:
  1223. return CallWindowProc(sw->oldproc, hwnd, WM_NCLBUTTONDOWN, wParam, lParam);
  1224. //return 0;
  1225. }
  1226. SetCapture(hwnd);
  1227. return 0;
  1228. }
  1229. //
  1230. // Left button released
  1231. //
  1232. static LRESULT LButtonUp(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
  1233. {
  1234. RECT rect;
  1235. //UINT thisportion;
  1236. HDC hdc;
  1237. POINT pt;
  1238. RECT winrect;
  1239. UINT buttonIdx = 0;
  1240. //current scrollportion is the button that we clicked down on
  1241. if(uCurrentScrollPortion != HTSCROLL_NONE)
  1242. {
  1243. SCROLLBAR *sb = &sw->sbarHorz;
  1244. lParam = GetMessagePos();
  1245. ReleaseCapture();
  1246. GetWindowRect(hwnd, &winrect);
  1247. pt.x = LOWORD(lParam);
  1248. pt.y = HIWORD(lParam);
  1249. //emulate the mouse input on a scrollbar here...
  1250. if(uCurrentScrollbar == SB_HORZ)
  1251. {
  1252. //get the total area of the normal Horz scrollbar area
  1253. sb = &sw->sbarHorz;
  1254. GetHScrollRect(sw, hwnd, &rect);
  1255. }
  1256. else if(uCurrentScrollbar == SB_VERT)
  1257. {
  1258. //get the total area of the normal Horz scrollbar area
  1259. sb = &sw->sbarVert;
  1260. GetVScrollRect(sw, hwnd, &rect);
  1261. }
  1262. //we need to do different things depending on if the
  1263. //user is activating the scrollbar itself, or one of
  1264. //the inserted buttons
  1265. switch(uCurrentScrollPortion)
  1266. {
  1267. //The scrollbar is active
  1268. case HTSCROLL_LEFT: case HTSCROLL_RIGHT:
  1269. case HTSCROLL_PAGELEFT: case HTSCROLL_PAGERIGHT:
  1270. case HTSCROLL_NONE:
  1271. KillTimer(hwnd, uScrollTimerId);
  1272. case HTSCROLL_THUMB:
  1273. //In case we were thumb tracking, make sure we stop NOW
  1274. if(sw->fThumbTracking == TRUE)
  1275. {
  1276. SendScrollMessage(hwnd, uScrollTimerMsg, SB_THUMBPOSITION, nLastPos);
  1277. sw->fThumbTracking = FALSE;
  1278. }
  1279. //send the SB_ENDSCROLL message now that scrolling has finished
  1280. SendScrollMessage(hwnd, uScrollTimerMsg, SB_ENDSCROLL, 0);
  1281. //adjust the total scroll area to become where the scrollbar
  1282. //really is (take into account the inserted buttons)
  1283. GetRealScrollRect(sb, &rect);
  1284. OffsetRect(&rect, -winrect.left, -winrect.top);
  1285. hdc = GetWindowDC(hwnd);
  1286. //draw whichever scrollbar sb is
  1287. NCDrawScrollbar(sb, hwnd, hdc, &rect, HTSCROLL_NORMAL);
  1288. ReleaseDC(hwnd, hdc);
  1289. break;
  1290. }
  1291. //reset our state to default
  1292. uCurrentScrollPortion = HTSCROLL_NONE;
  1293. uScrollTimerPortion = HTSCROLL_NONE;
  1294. uScrollTimerId = 0;
  1295. uScrollTimerMsg = 0;
  1296. uCurrentScrollbar = COOLSB_NONE;
  1297. return 0;
  1298. }
  1299. else
  1300. {
  1301. /*
  1302. // Can't remember why I did this!
  1303. if(GetCapture() == hwnd)
  1304. {
  1305. ReleaseCapture();
  1306. }*/
  1307. }
  1308. return CallWindowProc(sw->oldproc, hwnd, WM_LBUTTONUP, wParam, lParam);
  1309. }
  1310. //
  1311. // This function is called whenever the mouse is moved and
  1312. // we are dragging the scrollbar thumb about.
  1313. //
  1314. static LRESULT ThumbTrackHorz(SCROLLBAR *sbar, HWND hwnd, int x, int y)
  1315. {
  1316. POINT pt;
  1317. RECT rc, winrect, rc2;
  1318. COLORREF crCheck1 = GetSBForeColor();
  1319. COLORREF crCheck2 = GetSBBackColor();
  1320. HDC hdc;
  1321. int thumbpos = nThumbPos;
  1322. int pos;
  1323. int siMaxMin = 0;
  1324. UINT flatflag = sbar->fFlatScrollbar ? BF_FLAT : 0;
  1325. BOOL fCustomDraw = FALSE;
  1326. SCROLLINFO *si;
  1327. si = &sbar->scrollInfo;
  1328. pt.x = x;
  1329. pt.y = y;
  1330. //draw the thumb at whatever position
  1331. rc = rcThumbBounds;
  1332. SetRect(&rc2, rc.left - THUMBTRACK_SNAPDIST*2, rc.top - THUMBTRACK_SNAPDIST,
  1333. rc.right + THUMBTRACK_SNAPDIST*2, rc.bottom + THUMBTRACK_SNAPDIST);
  1334. rc.left += GetScrollMetric(sbar, SM_CXHORZSB);
  1335. rc.right -= GetScrollMetric(sbar, SM_CXHORZSB);
  1336. //if the mouse is not in a suitable distance of the scrollbar,
  1337. //then "snap" the thumb back to its initial position
  1338. #ifdef SNAP_THUMB_BACK
  1339. if(!PtInRect(&rc2, pt))
  1340. {
  1341. thumbpos = nThumbPos0;
  1342. }
  1343. //otherwise, move the thumb to where the mouse is
  1344. else
  1345. #endif //SNAP_THUMB_BACK
  1346. {
  1347. //keep the thumb within the scrollbar limits
  1348. thumbpos = pt.x - nThumbMouseOffset;
  1349. if(thumbpos < rc.left) thumbpos = rc.left;
  1350. if(thumbpos > rc.right - nThumbSize) thumbpos = rc.right - nThumbSize;
  1351. }
  1352. GetWindowRect(hwnd, &winrect);
  1353. if(sbar->nBarType == SB_VERT)
  1354. RotateRect(&winrect);
  1355. hdc = GetWindowDC(hwnd);
  1356. #ifdef CUSTOM_DRAW
  1357. fCustomDraw = PostCustomPrePostPaint(hwnd, hdc, sbar, CDDS_PREPAINT) == CDRF_SKIPDEFAULT;
  1358. #endif
  1359. OffsetRect(&rc, -winrect.left, -winrect.top);
  1360. thumbpos -= winrect.left;
  1361. //draw the margin before the thumb
  1362. SetRect(&rc2, rc.left, rc.top, thumbpos, rc.bottom);
  1363. RotateRect0(sbar, &rc2);
  1364. if(fCustomDraw)
  1365. PostCustomDrawNotify(hwnd, hdc, sbar->nBarType, &rc2, SB_PAGELEFT, 0, 0, 0);
  1366. else
  1367. DrawCheckedRect(hdc, &rc2, crCheck1, crCheck2);
  1368. RotateRect0(sbar, &rc2);
  1369. //draw the margin after the thumb
  1370. SetRect(&rc2, thumbpos+nThumbSize, rc.top, rc.right, rc.bottom);
  1371. RotateRect0(sbar, &rc2);
  1372. if(fCustomDraw)
  1373. PostCustomDrawNotify(hwnd, hdc, sbar->nBarType, &rc2, SB_PAGERIGHT, 0, 0, 0);
  1374. else
  1375. DrawCheckedRect(hdc, &rc2, crCheck1, crCheck2);
  1376. RotateRect0(sbar, &rc2);
  1377. //finally draw the thumb itelf. This is how it looks on win2000, anyway
  1378. SetRect(&rc2, thumbpos, rc.top, thumbpos+nThumbSize, rc.bottom);
  1379. RotateRect0(sbar, &rc2);
  1380. if(fCustomDraw)
  1381. PostCustomDrawNotify(hwnd, hdc, sbar->nBarType, &rc2, SB_THUMBTRACK, TRUE, TRUE, FALSE);
  1382. else
  1383. {
  1384. #ifdef FLAT_SCROLLBARS
  1385. if(sbar->fFlatScrollbar)
  1386. PaintRect(hdc, &rc2, GetSysColor(COLOR_3DSHADOW));
  1387. else
  1388. #endif
  1389. {
  1390. DrawBlankButton(hdc, &rc2, flatflag);
  1391. }
  1392. }
  1393. RotateRect0(sbar, &rc2);
  1394. ReleaseDC(hwnd, hdc);
  1395. //post a SB_TRACKPOS message!!!
  1396. siMaxMin = si->nMax - si->nMin;
  1397. if(siMaxMin > 0)
  1398. pos = MulDiv(thumbpos-rc.left, siMaxMin-si->nPage + 1, rc.right-rc.left-nThumbSize);
  1399. else
  1400. pos = thumbpos - rc.left;
  1401. if(pos != nLastPos)
  1402. {
  1403. si->nTrackPos = pos;
  1404. SendScrollMessage(hwnd, uScrollTimerMsg, SB_THUMBTRACK, pos);
  1405. }
  1406. nLastPos = pos;
  1407. #ifdef CUSTOM_DRAW
  1408. PostCustomPrePostPaint(hwnd, hdc, sbar, CDDS_POSTPAINT);
  1409. #endif
  1410. return 0;
  1411. }
  1412. //
  1413. // remember to rotate the thumb bounds rectangle!!
  1414. //
  1415. static LRESULT ThumbTrackVert(SCROLLBAR *sb, HWND hwnd, int x, int y)
  1416. {
  1417. //sw->swapcoords = TRUE;
  1418. RotateRect(&rcThumbBounds);
  1419. ThumbTrackHorz(sb, hwnd, y, x);
  1420. RotateRect(&rcThumbBounds);
  1421. //sw->swapcoords = FALSE;
  1422. return 0;
  1423. }
  1424. //
  1425. // Called when we have set the capture from the NCLButtonDown(...)
  1426. //
  1427. static LRESULT MouseMove(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
  1428. {
  1429. RECT rect;
  1430. UINT thisportion;
  1431. HDC hdc;
  1432. static UINT lastportion = 0;
  1433. static UINT lastbutton = 0;
  1434. POINT pt;
  1435. RECT winrect;
  1436. UINT buttonIdx = 0;
  1437. if(sw->fThumbTracking == TRUE)
  1438. {
  1439. int x, y;
  1440. lParam = GetMessagePos();
  1441. x = LOWORD(lParam);
  1442. y = HIWORD(lParam);
  1443. if(uCurrentScrollbar == SB_HORZ)
  1444. return ThumbTrackHorz(&sw->sbarHorz, hwnd, x,y);
  1445. else if(uCurrentScrollbar == SB_VERT)
  1446. return ThumbTrackVert(&sw->sbarVert, hwnd, x,y);
  1447. }
  1448. if(uCurrentScrollPortion == HTSCROLL_NONE)
  1449. {
  1450. return CallWindowProc(sw->oldproc, hwnd, WM_MOUSEMOVE, wParam, lParam);
  1451. }
  1452. else
  1453. {
  1454. LPARAM nlParam;
  1455. SCROLLBAR *sb = &sw->sbarHorz;
  1456. SCROLLBUT *sbut = 0;
  1457. nlParam = GetMessagePos();
  1458. GetWindowRect(hwnd, &winrect);
  1459. pt.x = LOWORD(nlParam);
  1460. pt.y = HIWORD(nlParam);
  1461. //emulate the mouse input on a scrollbar here...
  1462. if(uCurrentScrollbar == SB_HORZ)
  1463. {
  1464. sb = &sw->sbarHorz;
  1465. }
  1466. else if(uCurrentScrollbar == SB_VERT)
  1467. {
  1468. sb = &sw->sbarVert;
  1469. }
  1470. //get the total area of the normal scrollbar area
  1471. GetScrollRect(sw, sb->nBarType, hwnd, &rect);
  1472. //see if we clicked in the inserted buttons / normal scrollbar
  1473. //thisportion = GetPortion(sb, hwnd, &rect, LOWORD(lParam), HIWORD(lParam));
  1474. thisportion = GetPortion(sb, hwnd, &rect, pt.x, pt.y);
  1475. //we need to do different things depending on if the
  1476. //user is activating the scrollbar itself, or one of
  1477. //the inserted buttons
  1478. switch(uCurrentScrollPortion)
  1479. {
  1480. //The scrollbar is active
  1481. case HTSCROLL_LEFT: case HTSCROLL_RIGHT:case HTSCROLL_THUMB:
  1482. case HTSCROLL_PAGELEFT: case HTSCROLL_PAGERIGHT:
  1483. case HTSCROLL_NONE:
  1484. //adjust the total scroll area to become where the scrollbar
  1485. //really is (take into account the inserted buttons)
  1486. GetRealScrollRect(sb, &rect);
  1487. OffsetRect(&rect, -winrect.left, -winrect.top);
  1488. hdc = GetWindowDC(hwnd);
  1489. if(thisportion != uCurrentScrollPortion)
  1490. {
  1491. uScrollTimerPortion = HTSCROLL_NONE;
  1492. if(lastportion != thisportion)
  1493. NCDrawScrollbar(sb, hwnd, hdc, &rect, HTSCROLL_NORMAL);
  1494. }
  1495. //otherwise, draw the button in its depressed / clicked state
  1496. else
  1497. {
  1498. uScrollTimerPortion = uCurrentScrollPortion;
  1499. if(lastportion != thisportion)
  1500. NCDrawScrollbar(sb, hwnd, hdc, &rect, thisportion);
  1501. }
  1502. ReleaseDC(hwnd, hdc);
  1503. break;
  1504. }
  1505. lastportion = thisportion;
  1506. lastbutton = buttonIdx;
  1507. //must return zero here, because we might get cursor anomilies
  1508. //CallWindowProc(sw->oldproc, hwnd, WM_MOUSEMOVE, wParam, lParam);
  1509. return 0;
  1510. }
  1511. }
  1512. //
  1513. // We must allocate from in the non-client area for our scrollbars
  1514. // Call the default window procedure first, to get the borders (if any)
  1515. // allocated some space, then allocate the space for the scrollbars
  1516. // if they fit
  1517. //
  1518. static LRESULT NCCalcSize(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
  1519. {
  1520. NCCALCSIZE_PARAMS *nccsp;
  1521. RECT *rect;
  1522. RECT oldrect;
  1523. BOOL fCalcValidRects = (wParam == TRUE);
  1524. SCROLLBAR *sb;
  1525. UINT ret;
  1526. DWORD dwStyle;
  1527. //Regardless of the value of fCalcValidRects, the first rectangle
  1528. //in the array specified by the rgrc structure member of the
  1529. //NCCALCSIZE_PARAMS structure contains the coordinates of the window,
  1530. //so we can use the exact same code to modify this rectangle, when
  1531. //wParam is TRUE and when it is FALSE.
  1532. nccsp = (NCCALCSIZE_PARAMS *)lParam;
  1533. rect = &nccsp->rgrc[0];
  1534. oldrect = *rect;
  1535. dwStyle = GetWindowLong(hwnd, GWL_STYLE);
  1536. // TURN OFF SCROLL-STYLES.
  1537. if ( dwStyle & (WS_VSCROLL|WS_HSCROLL) )
  1538. {
  1539. sw->bPreventStyleChange = TRUE;
  1540. SetWindowLong(hwnd, GWL_STYLE, dwStyle & ~(WS_VSCROLL|WS_HSCROLL));
  1541. }
  1542. //call the default procedure to get the borders allocated
  1543. ret = CallWindowProc(sw->oldproc, hwnd, WM_NCCALCSIZE, wParam, lParam);
  1544. // RESTORE PREVIOUS STYLES (if present at all)
  1545. if ( dwStyle & (WS_VSCROLL|WS_HSCROLL) )
  1546. {
  1547. SetWindowLong(hwnd, GWL_STYLE, dwStyle);
  1548. sw->bPreventStyleChange = FALSE;
  1549. }
  1550. // calculate what the size of each window border is,
  1551. sw->cxLeftEdge = rect->left - oldrect.left;
  1552. sw->cxRightEdge = oldrect.right - rect->right;
  1553. sw->cyTopEdge = rect->top - oldrect.top;
  1554. sw->cyBottomEdge = oldrect.bottom - rect->bottom;
  1555. sb = &sw->sbarHorz;
  1556. //if there is room, allocate some space for the horizontal scrollbar
  1557. //NOTE: Change the ">" to a ">=" to make the horz bar totally fill the
  1558. //window before disappearing
  1559. if((sb->fScrollFlags & CSBS_VISIBLE) &&
  1560. #ifdef COOLSB_FILLWINDOW
  1561. rect->bottom - rect->top >= GetScrollMetric(sb, SM_CYHORZSB))
  1562. #else
  1563. rect->bottom - rect->top > GetScrollMetric(sb, SM_CYHORZSB))
  1564. #endif
  1565. {
  1566. rect->bottom -= GetScrollMetric(sb, SM_CYHORZSB);
  1567. sb->fScrollVisible = TRUE;
  1568. }
  1569. else
  1570. sb->fScrollVisible = FALSE;
  1571. sb = &sw->sbarVert;
  1572. //if there is room, allocate some space for the vertical scrollbar
  1573. if((sb->fScrollFlags & CSBS_VISIBLE) &&
  1574. rect->right - rect->left >= GetScrollMetric(sb, SM_CXVERTSB))
  1575. {
  1576. if(sw->fLeftScrollbar)
  1577. rect->left += GetScrollMetric(sb, SM_CXVERTSB);
  1578. else
  1579. rect->right -= GetScrollMetric(sb, SM_CXVERTSB);
  1580. sb->fScrollVisible = TRUE;
  1581. }
  1582. else
  1583. sb->fScrollVisible = FALSE;
  1584. //don't return a value unless we actually modify the other rectangles
  1585. //in the NCCALCSIZE_PARAMS structure. In this case, we return 0
  1586. //no matter what the value of fCalcValidRects is
  1587. return ret;//FALSE;
  1588. }
  1589. //
  1590. // Timer routine to generate scrollbar messages
  1591. //
  1592. static LRESULT CoolSB_Timer(SCROLLWND *swnd, HWND hwnd, WPARAM wTimerId, LPARAM lParam)
  1593. {
  1594. //let all timer messages go past if we don't have a timer installed ourselves
  1595. if(uScrollTimerId == 0 && uMouseOverId == 0)
  1596. {
  1597. return CallWindowProc(swnd->oldproc, hwnd, WM_TIMER, wTimerId, lParam);
  1598. }
  1599. #ifdef HOT_TRACKING
  1600. //mouse-over timer
  1601. if(wTimerId == COOLSB_TIMERID3)
  1602. {
  1603. POINT pt;
  1604. RECT rect, winrect;
  1605. HDC hdc;
  1606. SCROLLBAR *sbar;
  1607. if(swnd->fThumbTracking)
  1608. return 0;
  1609. //if the mouse moves outside the current scrollbar,
  1610. //then kill the timer..
  1611. GetCursorPos(&pt);
  1612. if(!PtInRect(&MouseOverRect, pt))
  1613. {
  1614. KillTimer(hwnd, uMouseOverId);
  1615. uMouseOverId = 0;
  1616. uMouseOverScrollbar = COOLSB_NONE;
  1617. uLastHitTestPortion = HTSCROLL_NONE;
  1618. uHitTestPortion = HTSCROLL_NONE;
  1619. NCPaint(swnd, hwnd, 1, 0);
  1620. }
  1621. else
  1622. {
  1623. if(uMouseOverScrollbar == SB_HORZ)
  1624. {
  1625. sbar = &swnd->sbarHorz;
  1626. uHitTestPortion = GetHorzPortion(sbar, hwnd, &MouseOverRect, pt.x, pt.y);
  1627. }
  1628. else
  1629. {
  1630. sbar = &swnd->sbarVert;
  1631. uHitTestPortion = GetVertPortion(sbar, hwnd, &MouseOverRect, pt.x, pt.y);
  1632. }
  1633. if(uLastHitTestPortion != uHitTestPortion)
  1634. {
  1635. rect = MouseOverRect;
  1636. GetRealScrollRect(sbar, &rect);
  1637. GetWindowRect(hwnd, &winrect);
  1638. OffsetRect(&rect, -winrect.left, -winrect.top);
  1639. hdc = GetWindowDC(hwnd);
  1640. NCDrawScrollbar(sbar, hwnd, hdc, &rect, HTSCROLL_NONE);
  1641. ReleaseDC(hwnd, hdc);
  1642. }
  1643. uLastHitTestPortion = uHitTestPortion;
  1644. }
  1645. return 0;
  1646. }
  1647. #endif // HOT_TRACKING
  1648. //if the first timer goes off, then we can start a more
  1649. //regular timer interval to auto-generate scroll messages
  1650. //this gives a slight pause between first pressing the scroll arrow, and the
  1651. //actual scroll starting
  1652. if(wTimerId == COOLSB_TIMERID1)
  1653. {
  1654. KillTimer(hwnd, uScrollTimerId);
  1655. uScrollTimerId = SetTimer(hwnd, COOLSB_TIMERID2, COOLSB_TIMERINTERVAL2, 0);
  1656. return 0;
  1657. }
  1658. //send the scrollbar message repeatedly
  1659. else if(wTimerId == COOLSB_TIMERID2)
  1660. {
  1661. //need to process a spoof WM_MOUSEMOVE, so that
  1662. //we know where the mouse is each time the scroll timer goes off.
  1663. //This is so we can stop sending scroll messages if the thumb moves
  1664. //under the mouse.
  1665. POINT pt;
  1666. GetCursorPos(&pt);
  1667. ScreenToClient(hwnd, &pt);
  1668. MouseMove(swnd, hwnd, MK_LBUTTON, MAKELPARAM(pt.x, pt.y));
  1669. if(uScrollTimerPortion != HTSCROLL_NONE)
  1670. SendScrollMessage(hwnd, uScrollTimerMsg, uScrollTimerPortion, 0);
  1671. return 0;
  1672. }
  1673. else
  1674. {
  1675. return CallWindowProc(swnd->oldproc, hwnd, WM_TIMER, wTimerId, lParam);
  1676. }
  1677. }
  1678. //
  1679. // We must intercept any calls to SetWindowLong, to check if
  1680. // left-scrollbars are taking effect or not
  1681. //
  1682. static LRESULT CoolSB_StyleChange(SCROLLWND *swnd, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  1683. {
  1684. STYLESTRUCT *ss = (STYLESTRUCT *)lParam;
  1685. if(wParam == GWL_EXSTYLE)
  1686. {
  1687. if(ss->styleNew & WS_EX_LEFTSCROLLBAR)
  1688. swnd->fLeftScrollbar = TRUE;
  1689. else
  1690. swnd->fLeftScrollbar = FALSE;
  1691. }
  1692. return CallWindowProc(swnd->oldproc, hwnd, msg, wParam, lParam);
  1693. }
  1694. //
  1695. // CoolScrollbar subclass procedure.
  1696. // Handle all messages needed to mimick normal windows scrollbars
  1697. //
  1698. LRESULT CALLBACK CoolSBWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  1699. {
  1700. WNDPROC oldproc;
  1701. SCROLLWND *swnd = GetScrollWndFromHwnd(hwnd);
  1702. static int count;
  1703. switch(message)
  1704. {
  1705. case WM_NCDESTROY:
  1706. //this should NEVER be called, because the user
  1707. //should have called Uninitialize() themselves.
  1708. //However, if the user tries to call Uninitialize()..
  1709. //after this window is destroyed, this window's entry in the lookup
  1710. //table will not be there, and the call will fail
  1711. oldproc = swnd->oldproc;
  1712. UninitializeCoolSB(hwnd);
  1713. //we must call the original window procedure, otherwise it
  1714. //will never get the WM_NCDESTROY message, and it wouldn't
  1715. //be able to clean up etc.
  1716. return CallWindowProc(oldproc, hwnd, message, wParam, lParam);
  1717. case WM_NCCALCSIZE:
  1718. return NCCalcSize(swnd, hwnd, wParam, lParam);
  1719. case WM_NCPAINT:
  1720. NCPaint(swnd, hwnd, wParam, lParam);
  1721. return 1;
  1722. case WM_NCHITTEST:
  1723. return NCHitTest(swnd, hwnd, wParam, lParam);
  1724. case WM_NCLBUTTONDBLCLK:
  1725. //TRACE("WM_NCLBUTTONDBLCLK %d\n", count++);
  1726. if(wParam == HTHSCROLL || wParam == HTVSCROLL)
  1727. return NCLButtonDown(swnd, hwnd, wParam, lParam);
  1728. else
  1729. break;
  1730. case WM_NCLBUTTONDOWN:
  1731. return NCLButtonDown(swnd, hwnd, wParam, lParam);
  1732. case WM_LBUTTONUP:
  1733. return LButtonUp(swnd, hwnd, wParam, lParam);
  1734. //Mouse moves are received when we set the mouse capture,
  1735. //even when the mouse moves over the non-client area
  1736. case WM_MOUSEMOVE:
  1737. //TRACE("WM_MOUSEMOVE %d\n", count++);
  1738. return MouseMove(swnd, hwnd, wParam, lParam);
  1739. case WM_TIMER:
  1740. return CoolSB_Timer(swnd, hwnd, wParam, lParam);
  1741. //case WM_STYLECHANGING:
  1742. // return CoolSB_StyleChange(swnd, hwnd, WM_STYLECHANGING, wParam, lParam);
  1743. case WM_STYLECHANGED:
  1744. if(swnd->bPreventStyleChange)
  1745. {
  1746. // the NCPAINT handler has told us to eat this message!
  1747. return 0;
  1748. }
  1749. else
  1750. {
  1751. if (message == WM_STYLECHANGED)
  1752. return CoolSB_StyleChange(swnd, hwnd, WM_STYLECHANGED, wParam, lParam);
  1753. else
  1754. break;
  1755. }
  1756. default:
  1757. break;
  1758. }
  1759. return CallWindowProc(swnd->oldproc, hwnd, message, wParam, lParam);
  1760. }