123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- /*
-
- Cool Scrollbar Library Version 1.2
- Module: coolsblib.c
- Copyright (c) J Brown 2001
- This code is freeware, however, you may not publish
- this code elsewhere or charge any money for it. This code
- is supplied as-is. I make no guarantees about the suitability
- of this code - use at your own risk.
-
- It would be nice if you credited me, in the event
- that you use this code in a product.
- VERSION HISTORY:
- V1.2: TreeView problem fixed by Diego Tartara
- Small problem in thumbsize calculation also fixed (thanks Diego!)
- V1.1: Added support for Right-left windows
- Changed calling convention of APIs to WINAPI (__stdcall)
- Completely standalone (no need for c-runtime)
-
- V1.0: Apr 2001: Initial Version CoolSB_SetStyle
- */
- #include "stdafx.h"
- #include <windows.h>
- #include <commctrl.h>
- #include <tchar.h>
- #include "coolscroll.h"
- #include "userdefs.h"
- #include "coolsb_internal.h"
- static TCHAR szPropStr[] = _T("CoolSBSubclassPtr");
- LRESULT CALLBACK CoolSBWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
- SCROLLWND *GetScrollWndFromHwnd(HWND hwnd)
- {
- return (SCROLLWND *)GetProp(hwnd, szPropStr);
- }
- SCROLLBAR *GetScrollBarFromHwnd(HWND hwnd, UINT nBar)
- {
- SCROLLWND *sw = GetScrollWndFromHwnd(hwnd);
-
- if(!sw) return 0;
-
- if(nBar == SB_HORZ)
- return &sw->sbarHorz;
- else if(nBar == SB_VERT)
- return &sw->sbarVert;
- else
- return 0;
- }
- BOOL WINAPI CoolSB_IsCoolScrollEnabled(HWND hwnd)
- {
- if(GetScrollWndFromHwnd(hwnd))
- return TRUE;
- else
- return FALSE;
- }
- BOOL GetScrollRect(SCROLLWND *sw, UINT nBar, HWND hwnd, RECT *rect);
- //
- // Special support for USER32.DLL patching (using Detours library)
- // The only place we call a real scrollbar API is in InitializeCoolSB,
- // where we call EnableScrollbar.
- //
- // We HAVE to call the origial EnableScrollbar function,
- // so we need to be able to set a pointer to this func when using
- // using Detours (or any other LIB??)
- //
- static BOOL (WINAPI * pEnableScrollBar) (HWND, UINT, UINT) = 0;
- void WINAPI CoolSB_SetESBProc(void *proc)
- {
- // pEnableScrollBar = proc;
- }
- //
- //
- static void RedrawNonClient(HWND hwnd, BOOL fFrameChanged)
- {
- if(fFrameChanged == FALSE)
- {
- /*
- RECT rect;
- HRGN hrgn1, hrgn2;
-
- SCROLLWND *sw = GetScrollWndFromHwnd(hwnd);
-
- GetScrollRect(sw, SB_HORZ, hwnd, &rect);
- hrgn1 = CreateRectRgnIndirect(&rect);
-
- GetScrollRect(sw, SB_VERT, hwnd, &rect);
- hrgn2 = CreateRectRgnIndirect(&rect);
-
- CombineRgn(hrgn1, hrgn2, hrgn1, RGN_OR);
-
- SendMessage(hwnd, WM_NCPAINT, (WPARAM)hrgn1, 0);
-
- DeleteObject(hrgn1);
- DeleteObject(hrgn2);*/
- SendMessage(hwnd, WM_NCPAINT, (WPARAM)1, 0);
- }
- else
- {
- SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE
- | SWP_FRAMECHANGED | SWP_DRAWFRAME);
- }
- }
- //
- // return the default minimum size of a scrollbar thumb
- //
- int WINAPI CoolSB_GetDefaultMinThumbSize(void)
- {
- DWORD dwVersion = GetVersion();
- // set the minimum thumb size for a scrollbar. This
- // differs between NT4 and 2000, so need to check to see
- // which platform we are running under
- if(dwVersion < 0x80000000) // Windows NT/2000
- {
- if(LOBYTE(LOWORD(dwVersion)) >= 5)
- return MINTHUMBSIZE_2000;
- else
- return MINTHUMBSIZE_NT4;
- }
- else
- {
- return MINTHUMBSIZE_NT4;
- }
- }
- static SCROLLINFO *GetScrollInfoFromHwnd(HWND hwnd, int fnBar)
- {
- SCROLLBAR *sb = GetScrollBarFromHwnd(hwnd, fnBar);
- if(sb == 0)
- return FALSE;
- if(fnBar == SB_HORZ)
- {
- return &sb->scrollInfo;
- }
- else if(fnBar == SB_VERT)
- {
- return &sb->scrollInfo;
- }
- else
- return NULL;
- }
- //
- // Initialize the cool scrollbars for a window by subclassing it
- // and using the coolsb window procedure instead
- //
- BOOL WINAPI InitializeCoolSB(HWND hwnd)
- {
- SCROLLWND *sw;
- SCROLLINFO *si;
- RECT rect;
- DWORD dwCurStyle;
- if(pEnableScrollBar == 0)
- pEnableScrollBar = EnableScrollBar;
- GetClientRect(hwnd, &rect);
- //if we have already initialized Cool Scrollbars for this window,
- //then stop the user from doing it again
- if(GetScrollWndFromHwnd(hwnd) != 0)
- {
- return FALSE;
- }
- //allocate a private scrollbar structure which we
- //will use to keep track of the scrollbar data
- sw = (SCROLLWND *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SCROLLWND));
- si = &sw->sbarHorz.scrollInfo;
- si->cbSize = sizeof(SCROLLINFO);
- si->fMask = SIF_ALL;
- GetScrollInfo(hwnd, SB_HORZ, si);
- si = &sw->sbarVert.scrollInfo;
- si->cbSize = sizeof(SCROLLINFO);
- si->fMask = SIF_ALL;
- GetScrollInfo(hwnd, SB_VERT, si);
- //check to see if the window has left-aligned scrollbars
- if(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_LEFTSCROLLBAR)
- sw->fLeftScrollbar = TRUE;
- else
- sw->fLeftScrollbar = FALSE;
- dwCurStyle = GetWindowLong(hwnd, GWL_STYLE);
- SetProp(hwnd, szPropStr, (HANDLE)sw);
- //try to enable the scrollbar arrows - if the return value is
- //non-zero, then the scrollbars were previously disabled
- //fDisabled = pEnableScrollBar(hwnd, SB_HORZ, ESB_ENABLE_BOTH);
- //scrollbars will automatically get enabled, even if
- //they aren't to start with....sorry, but there isn't an
- //easy alternative.
- if(dwCurStyle & WS_HSCROLL)
- sw->sbarHorz.fScrollFlags = CSBS_VISIBLE;
- if(dwCurStyle & WS_VSCROLL)
- sw->sbarVert.fScrollFlags = CSBS_VISIBLE;
- //need to be able to distinguish between horizontal and vertical
- //scrollbars in some instances
- sw->sbarHorz.nBarType = SB_HORZ;
- sw->sbarVert.nBarType = SB_VERT;
- sw->sbarHorz.fFlatScrollbar = CSBS_NORMAL;
- sw->sbarVert.fFlatScrollbar = CSBS_NORMAL;
- //set the default arrow sizes for the scrollbars
- sw->sbarHorz.nArrowLength = SYSTEM_METRIC;
- sw->sbarHorz.nArrowWidth = SYSTEM_METRIC;
- sw->sbarVert.nArrowLength = SYSTEM_METRIC;
- sw->sbarVert.nArrowWidth = SYSTEM_METRIC;
- sw->bPreventStyleChange = FALSE;
-
- sw->oldproc = (WNDPROC)SetWindowLong(hwnd, GWL_WNDPROC, (LONG)CoolSBWndProc);
- CoolSB_SetMinThumbSize(hwnd, SB_BOTH, CoolSB_GetDefaultMinThumbSize());
-
- //send the window a frame changed message to update the scrollbars
- RedrawNonClient(hwnd, TRUE);
- return TRUE;
- }
- BOOL WINAPI CoolSB_EnableScrollBar (HWND hwnd, int wSBflags, UINT wArrows)
- {
- SCROLLBAR *sbar;
- UINT oldstate;
- BOOL bFailed = FALSE;
- if(!CoolSB_IsCoolScrollEnabled(hwnd))
- return EnableScrollBar(hwnd, wSBflags, wArrows);
- if((wSBflags == SB_HORZ || wSBflags == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_HORZ)))
- {
- oldstate = sbar->fScrollFlags;
-
- //clear any existing state, and OR in the disabled flags
- sbar->fScrollFlags = (sbar->fScrollFlags & ~ESB_DISABLE_BOTH) | wArrows;
- if(oldstate == sbar->fScrollFlags)
- bFailed = TRUE;
- }
- if((wSBflags == SB_VERT || wSBflags == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_VERT)))
- {
- oldstate = sbar->fScrollFlags;
-
- //clear any existing state, and OR in the disabled flags
- sbar->fScrollFlags = (sbar->fScrollFlags & ~ESB_DISABLE_BOTH) | wArrows;
- if(oldstate == sbar->fScrollFlags)
- bFailed = TRUE;
- }
- return !bFailed;
- }
- BOOL WINAPI CoolSB_GetScrollBarInfo(HWND hwnd)
- {
- // SCROLLBARINFO sbi; not defined in winuser.h
- return FALSE;
- }
- BOOL WINAPI CoolSB_GetScrollInfo (HWND hwnd, int fnBar, LPSCROLLINFO lpsi)
- {
- SCROLLINFO *mysi;
- BOOL copied = FALSE;
-
- if(!lpsi)
- return FALSE;
- if(!(mysi = GetScrollInfoFromHwnd(hwnd, fnBar)))
- {
- return GetScrollInfo(hwnd, fnBar, lpsi);
- }
-
- if(lpsi->fMask & SIF_PAGE)
- {
- lpsi->nPage = mysi->nPage;
- copied = TRUE;
- }
- if(lpsi->fMask & SIF_POS)
- {
- lpsi->nPos = mysi->nPos;
- copied = TRUE;
- }
- if(lpsi->fMask & SIF_TRACKPOS)
- {
- lpsi->nTrackPos = mysi->nTrackPos;
- copied = TRUE;
- }
- if(lpsi->fMask & SIF_RANGE)
- {
- lpsi->nMin = mysi->nMin;
- lpsi->nMax = mysi->nMax;
- copied = TRUE;
- }
- return copied;
- }
- int WINAPI CoolSB_GetScrollPos (HWND hwnd, int nBar)
- {
- SCROLLINFO *mysi;
-
- if(!(mysi = GetScrollInfoFromHwnd(hwnd, nBar)))
- return GetScrollPos(hwnd, nBar);
- return mysi->nPos;
- }
- BOOL WINAPI CoolSB_GetScrollRange (HWND hwnd, int nBar, LPINT lpMinPos, LPINT lpMaxPos)
- {
- SCROLLINFO *mysi;
-
- if(!lpMinPos || !lpMaxPos)
- return FALSE;
- if(!(mysi = GetScrollInfoFromHwnd(hwnd, nBar)))
- return GetScrollRange(hwnd, nBar, lpMinPos, lpMaxPos);
- *lpMinPos = mysi->nMin;
- *lpMaxPos = mysi->nMax;
- return TRUE;
- }
- int WINAPI CoolSB_SetScrollInfo (HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw)
- {
- SCROLLINFO *mysi;
- SCROLLBAR *sbar;
- BOOL fRecalcFrame = FALSE;
- if(!lpsi)
- return FALSE;
- if(!(mysi = GetScrollInfoFromHwnd(hwnd, fnBar)))
- return SetScrollInfo(hwnd, fnBar, lpsi, fRedraw);
- //if(CoolSB_IsThumbTracking(hwnd))
- // return mysi->nPos;
- if(lpsi->fMask & SIF_RANGE)
- {
- mysi->nMin = lpsi->nMin;
- mysi->nMax = lpsi->nMax;
- }
- //The nPage member must specify a value from 0 to nMax - nMin +1.
- if(lpsi->fMask & SIF_PAGE)
- {
- UINT t = (UINT)(mysi->nMax - mysi->nMin + 1);
- mysi->nPage = min(max(0, lpsi->nPage), t);
- }
- //The nPos member must specify a value between nMin and nMax - max(nPage - 1, 0).
- if(lpsi->fMask & SIF_POS)
- {
- mysi->nPos = max(lpsi->nPos, mysi->nMin);
- mysi->nPos = min((UINT)mysi->nPos, mysi->nMax - max(mysi->nPage - 1, 0));
- }
- sbar = GetScrollBarFromHwnd(hwnd, fnBar);
- if((lpsi->fMask & SIF_DISABLENOSCROLL) || (sbar->fScrollFlags & CSBS_THUMBALWAYS))
- {
- if(!sbar->fScrollVisible)
- {
- CoolSB_ShowScrollBar(hwnd, fnBar, TRUE);
- fRecalcFrame = TRUE;
- }
- }
- else
- {
- if( mysi->nPage > (UINT)mysi->nMax
- || mysi->nPage == (UINT)mysi->nMax && mysi->nMax == 0
- || mysi->nMax <= mysi->nMin)
- {
- if(sbar->fScrollVisible)
- {
- CoolSB_ShowScrollBar(hwnd, fnBar, FALSE);
- fRecalcFrame = TRUE;
- }
- }
- else
- {
- if(!sbar->fScrollVisible)
- {
- CoolSB_ShowScrollBar(hwnd, fnBar, TRUE);
- fRecalcFrame = TRUE;
- }
- }
- }
- if(fRedraw && !CoolSB_IsThumbTracking(hwnd))
- RedrawNonClient(hwnd, fRecalcFrame);
-
- return mysi->nPos;
- }
- int WINAPI CoolSB_SetScrollPos(HWND hwnd, int nBar, int nPos, BOOL fRedraw)
- {
- SCROLLINFO *mysi;
- int oldpos;
-
- if(!(mysi = GetScrollInfoFromHwnd(hwnd, nBar)))
- {
- return SetScrollPos(hwnd, nBar, nPos, fRedraw);
- }
- //this is what should happen, but real scrollbars don't work like this..
- //if(CoolSB_IsThumbTracking(hwnd))
- // return mysi->nPos;
- //validate and set the scollbar position
- oldpos = mysi->nPos;
- mysi->nPos = max(nPos, mysi->nMin);
- mysi->nPos = min((UINT)mysi->nPos, mysi->nMax - max(mysi->nPage - 1, 0));
- if(fRedraw && !CoolSB_IsThumbTracking(hwnd))
- RedrawNonClient(hwnd, FALSE);
- return oldpos;
- }
- int WINAPI CoolSB_SetScrollRange (HWND hwnd, int nBar, int nMinPos, int nMaxPos, BOOL fRedraw)
- {
- SCROLLINFO *mysi;
-
- if(!(mysi = GetScrollInfoFromHwnd(hwnd, nBar)))
- return SetScrollRange(hwnd, nBar, nMinPos, nMaxPos, fRedraw);
- if(CoolSB_IsThumbTracking(hwnd))
- return mysi->nPos;
- //hide the scrollbar if nMin == nMax
- //nMax-nMin must not be greater than MAXLONG
- mysi->nMin = nMinPos;
- mysi->nMax = nMaxPos;
-
- if(fRedraw)
- RedrawNonClient(hwnd, FALSE);
- return TRUE;
- }
- //
- // Show or hide the specified scrollbars
- //
- BOOL WINAPI CoolSB_ShowScrollBar (HWND hwnd, int wBar, BOOL fShow)
- {
- SCROLLBAR *sbar;
- BOOL bFailed = FALSE;
- DWORD dwStyle = GetWindowLong(hwnd, GWL_STYLE);
- if(!CoolSB_IsCoolScrollEnabled(hwnd))
- return ShowScrollBar(hwnd, wBar, fShow);
- if((wBar == SB_HORZ || wBar == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_HORZ)))
- {
- sbar->fScrollFlags = sbar->fScrollFlags & ~CSBS_VISIBLE;
- sbar->fScrollFlags |= (fShow == TRUE ? CSBS_VISIBLE : 0);
- //bFailed = TRUE;
- if(fShow) SetWindowLong(hwnd, GWL_STYLE, dwStyle | WS_HSCROLL);
- else SetWindowLong(hwnd, GWL_STYLE, dwStyle & ~WS_HSCROLL);
- }
- if((wBar == SB_VERT || wBar == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_VERT)))
- {
- sbar->fScrollFlags = sbar->fScrollFlags & ~CSBS_VISIBLE;
- sbar->fScrollFlags |= (fShow == TRUE ? CSBS_VISIBLE : 0);
- //bFailed = TRUE;
- if(fShow) SetWindowLong(hwnd, GWL_STYLE, dwStyle | WS_VSCROLL);
- else SetWindowLong(hwnd, GWL_STYLE, dwStyle & ~WS_VSCROLL);
- }
- if(bFailed)
- {
- return FALSE;
- }
- else
- {
- //DWORD style = GetWindowLong(hwnd, GWL_STYLE);
- //style |= WS_VSCROLL;
-
- //if(s
- //SetWindowLong(hwnd, GWL_STYLE, style);
- SetWindowPos(hwnd, 0, 0, 0, 0, 0,
- SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
- SWP_NOACTIVATE | SWP_FRAMECHANGED);
-
- return TRUE;
- }
- }
- //
- // Remove cool scrollbars from the specified window.
- //
- HRESULT WINAPI UninitializeCoolSB(HWND hwnd)
- {
- int i = 0;
- SCROLLWND *sw = GetScrollWndFromHwnd(hwnd);
- if(!sw) return E_FAIL;
- //restore the window procedure with the original one
- SetWindowLong(hwnd, GWL_WNDPROC, (LONG)sw->oldproc);
- RemoveProp(hwnd, szPropStr);
- //SetWindowLong(hwnd, GWL_USERDATA, 0);
- //finally, release the memory needed for the cool scrollbars
- HeapFree(GetProcessHeap(), 0, sw);
- //Force WM_NCCALCSIZE and WM_NCPAINT so the original scrollbars can kick in
- RedrawNonClient(hwnd, TRUE);
- return S_OK;
- }
- //
- // Set the size of the scrollbars
- //
- BOOL WINAPI CoolSB_SetSize (HWND hwnd, int wBar, int nLength, int nWidth)
- {
- SCROLLBAR *sbar;
-
- if(nLength == 0 || nWidth == 0)
- return FALSE;
- if(nLength < -8 || nWidth < -8)
- return FALSE;
- if(nLength > 256 || nWidth > 256)
- return FALSE;
- if(!GetScrollWndFromHwnd(hwnd))
- return FALSE;
- if((wBar == SB_HORZ || wBar == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_HORZ)))
- {
- sbar->nArrowLength = nLength;
- sbar->nArrowWidth = nWidth;
- }
- if((wBar == SB_VERT || wBar == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_VERT)))
- {
- sbar->nArrowLength = nLength;
- sbar->nArrowWidth = nWidth;
- }
- RedrawNonClient(hwnd, TRUE);
- return TRUE;
- }
- //
- // Alter the display mode of the scrollbars
- // wBar - SB_HORZ / SB_VERT / SB_BOTH
- // nStyle - CSBF_NORMAL / CSBF_FLAT / CSBF_HOTTRACKED
- //
- BOOL WINAPI CoolSB_SetStyle(HWND hwnd, int wBar, UINT nStyle)
- {
- SCROLLBAR *sbar;
- if(!GetScrollWndFromHwnd(hwnd))
- return FALSE;
- if((wBar == SB_HORZ || wBar == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_HORZ)))
- {
- sbar->fFlatScrollbar = nStyle;
- }
- if((wBar == SB_VERT || wBar == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_VERT)))
- {
- sbar->fFlatScrollbar = nStyle;
- }
- RedrawNonClient(hwnd, FALSE);
- return TRUE;
- }
- //
- // Set if the thumb is always visible, even if there is no data to
- // scroll. Setting this keeps the scrollbar enabled, but the thumb
- // covers the whole area
- //
- BOOL WINAPI CoolSB_SetThumbAlways(HWND hwnd, int wBar, BOOL fThumbAlways)
- {
- SCROLLBAR *sbar;
- if(!GetScrollWndFromHwnd(hwnd))
- return FALSE;
- if((wBar == SB_HORZ || wBar == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_HORZ)))
- {
- if(fThumbAlways)
- sbar->fScrollFlags |= CSBS_THUMBALWAYS;
- else
- sbar->fScrollFlags &= ~CSBS_THUMBALWAYS;
- }
- if((wBar == SB_VERT || wBar == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_VERT)))
- {
- if(fThumbAlways)
- sbar->fScrollFlags |= CSBS_THUMBALWAYS;
- else
- sbar->fScrollFlags &= ~CSBS_THUMBALWAYS;
- }
- RedrawNonClient(hwnd, FALSE);
- return TRUE;
- }
- //
- // Set the minimum size, in pixels, that the thumb box will shrink to.
- //
- BOOL WINAPI CoolSB_SetMinThumbSize(HWND hwnd, UINT wBar, UINT size)
- {
- SCROLLBAR *sbar;
- if(!GetScrollWndFromHwnd(hwnd))
- return FALSE;
- if(size == -1)
- size = CoolSB_GetDefaultMinThumbSize();
- if((wBar == SB_HORZ || wBar == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_HORZ)))
- {
- sbar->nMinThumbSize = size;
- }
- if((wBar == SB_VERT || wBar == SB_BOTH) &&
- (sbar = GetScrollBarFromHwnd(hwnd, SB_VERT)))
- {
- sbar->nMinThumbSize = size;
- }
- return TRUE;
- }
|