2 * Interface code to SCROLLBAR widget
4 * Copyright Martin Ayotte, 1993
12 static char Copyright[] = "Copyright Martin Ayotte, 1993";
14 #include <X11/Intrinsic.h>
15 #include <X11/StringDefs.h>
20 #include <sys/types.h>
28 HBITMAP hUpArrowD = 0;
29 HBITMAP hDnArrowD = 0;
30 HBITMAP hLfArrowD = 0;
31 HBITMAP hRgArrowD = 0;
33 LPHEADSCROLL ScrollBarGetWindowAndStorage(HWND hwnd, WND **wndPtr);
34 LPHEADSCROLL ScrollBarGetStorageHeader(HWND hwnd);
35 void StdDrawScrollBar(HWND hwnd);
36 int CreateScrollBarStruct(HWND hwnd);
39 /***********************************************************************
40 * WIDGETS_ScrollBarWndProc
42 LONG ScrollBarWndProc( HWND hwnd, WORD message, WORD wParam, LONG lParam )
49 LPDRAWITEMSTRUCT lpdis;
57 CreateScrollBarStruct(hwnd);
59 printf("ScrollBar Creation up=%X down=%X!\n", lphs->hWndUp, lphs->hWndDown);
61 if (hUpArrow == (HBITMAP)NULL)
62 hUpArrow = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_UPARROWI));
63 if (hDnArrow == (HBITMAP)NULL)
64 hDnArrow = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_DNARROWI));
65 if (hLfArrow == (HBITMAP)NULL)
66 hLfArrow = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_LFARROWI));
67 if (hRgArrow == (HBITMAP)NULL)
68 hRgArrow = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_RGARROWI));
69 if (hUpArrowD == (HBITMAP)NULL)
70 hUpArrowD = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_UPARROWD));
71 if (hDnArrowD == (HBITMAP)NULL)
72 hDnArrowD = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_DNARROWD));
73 if (hLfArrowD == (HBITMAP)NULL)
74 hLfArrowD = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_LFARROWD));
75 if (hRgArrowD == (HBITMAP)NULL)
76 hRgArrowD = LoadBitmap((HINSTANCE)NULL, MAKEINTRESOURCE(OBM_RGARROWD));
79 lphs = ScrollBarGetWindowAndStorage(hwnd, &wndPtr);
80 if (lphs == 0) return 0;
82 printf("ScrollBar WM_DESTROY %lX !\n", lphs);
84 DestroyWindow(lphs->hWndUp);
85 DestroyWindow(lphs->hWndDown);
87 *((LPHEADSCROLL *)&wndPtr->wExtra[1]) = 0;
92 printf("ScrollBar WM_COMMAND wParam=%X lParam=%lX !\n", wParam, lParam);
94 lphs = ScrollBarGetWindowAndStorage(hwnd, &wndPtr);
95 if (HIWORD(lParam) != BN_CLICKED) return 0;
96 if (LOWORD(lParam) == lphs->hWndUp)
97 SendMessage(wndPtr->hwndParent, lphs->Direction,
98 SB_LINEUP, MAKELONG(0, hwnd));
99 if (LOWORD(lParam) == lphs->hWndDown)
100 SendMessage(wndPtr->hwndParent, lphs->Direction,
101 SB_LINEDOWN, MAKELONG(0, hwnd));
108 lphs = ScrollBarGetWindowAndStorage(hwnd, &wndPtr);
113 GetClientRect(hwnd, &rect);
114 if (lphs->Direction == WM_VSCROLL) {
117 printf("WM_LBUTTONDOWN y=%d cur+right=%d %d\n",
118 y, lphs->CurPix + rect.right, lphs->CurPix + (rect.right << 1));
120 if (y < (lphs->CurPix + rect.right))
121 SendMessage(wndPtr->hwndParent, lphs->Direction,
122 SB_PAGEUP, MAKELONG(0, hwnd));
123 if (y > (lphs->CurPix + (rect.right << 1)))
124 SendMessage(wndPtr->hwndParent, lphs->Direction,
125 SB_PAGEDOWN, MAKELONG(0, hwnd));
126 if ((y > (lphs->CurPix + rect.right)) &&
127 (y < (lphs->CurPix + (rect.right << 1)))) {
128 lphs->ThumbActive = TRUE;
130 printf("THUMB DOWN !\n");
137 printf("WM_LBUTTONDOWN x=%d Cur+bottom=%d %d\n",
138 x, lphs->CurPix + rect.bottom, lphs->CurPix + (rect.bottom << 1));
140 if (x < (lphs->CurPix + rect.bottom))
141 SendMessage(wndPtr->hwndParent, lphs->Direction,
142 SB_PAGEUP, MAKELONG(0, hwnd));
143 if (x > (lphs->CurPix + (rect.bottom << 1)))
144 SendMessage(wndPtr->hwndParent, lphs->Direction,
145 SB_PAGEDOWN, MAKELONG(0, hwnd));
146 if ((x > (lphs->CurPix + rect.bottom)) &&
147 (x < (lphs->CurPix + (rect.bottom << 1)))) {
148 lphs->ThumbActive = TRUE;
150 printf("THUMB DOWN !\n");
156 lphs = ScrollBarGetStorageHeader(hwnd);
157 lphs->ThumbActive = FALSE;
162 if ((wParam & MK_LBUTTON) != 0) {
163 lphs = ScrollBarGetWindowAndStorage(hwnd, &wndPtr);
164 if (lphs->ThumbActive == 0) break;
165 GetClientRect(hwnd, &rect);
166 if (lphs->Direction == WM_VSCROLL)
167 y = HIWORD(lParam) - rect.right - (rect.right >> 1);
169 y = LOWORD(lParam) - rect.bottom - (rect.bottom >> 1);
170 x = (y * (lphs->MaxVal - lphs->MinVal) /
171 lphs->MaxPix) + lphs->MinVal;
173 printf("Scroll WM_MOUSEMOVE val=%d pix=%d\n", x, y);
175 SendMessage(wndPtr->hwndParent, lphs->Direction,
176 SB_THUMBTRACK, MAKELONG(x, hwnd));
182 lphs = ScrollBarGetWindowAndStorage(hwnd, &wndPtr);
183 return(SendMessage(wndPtr->hwndParent, message, wParam, lParam));
186 lphs = ScrollBarGetWindowAndStorage(hwnd, &wndPtr);
187 width = LOWORD(lParam);
188 height = HIWORD(lParam);
189 if (lphs->Direction == WM_VSCROLL) {
190 MoveWindow(lphs->hWndUp, 0, 0, width, width, TRUE);
191 MoveWindow(lphs->hWndDown, 0, height - width, width, width, TRUE);
194 MoveWindow(lphs->hWndUp, 0, 0, height, height, TRUE);
195 MoveWindow(lphs->hWndDown, width - height, 0, height, height, TRUE);
200 printf("Scroll WM_DRAWITEM w=%04X l=%08X\n", wParam, lParam);
202 lpdis = (LPDRAWITEMSTRUCT)lParam;
203 if (lpdis->CtlType == ODT_BUTTON && lpdis->itemAction == ODA_DRAWENTIRE) {
204 hMemDC = CreateCompatibleDC(lpdis->hDC);
205 if (lpdis->CtlID == 1) {
206 GetObject(hUpArrow, sizeof(BITMAP), (LPSTR)&bm);
207 SelectObject(hMemDC, hUpArrow);
208 /* BitBlt(lpdis->hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY); */
209 StretchBlt(lpdis->hDC, 0, 0, lpdis->rcItem.right, lpdis->rcItem.right,
210 hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
212 if (lpdis->CtlID == 2) {
213 GetObject(hDnArrow, sizeof(BITMAP), (LPSTR)&bm);
214 SelectObject(hMemDC, hDnArrow);
215 BitBlt(lpdis->hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
217 if (lpdis->CtlID == 3) {
218 GetObject(hLfArrow, sizeof(BITMAP), (LPSTR)&bm);
219 SelectObject(hMemDC, hLfArrow);
220 BitBlt(lpdis->hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
222 if (lpdis->CtlID == 4) {
223 GetObject(hRgArrow, sizeof(BITMAP), (LPSTR)&bm);
224 SelectObject(hMemDC, hRgArrow);
225 BitBlt(lpdis->hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
229 if (lpdis->CtlType == ODT_BUTTON && lpdis->itemAction == ODA_SELECT) {
230 hMemDC = CreateCompatibleDC(lpdis->hDC);
231 if (lpdis->CtlID == 1) {
232 GetObject(hUpArrowD, sizeof(BITMAP), (LPSTR)&bm);
233 SelectObject(hMemDC, hUpArrowD);
234 BitBlt(lpdis->hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
236 if (lpdis->CtlID == 2) {
237 GetObject(hDnArrowD, sizeof(BITMAP), (LPSTR)&bm);
238 SelectObject(hMemDC, hDnArrowD);
239 BitBlt(lpdis->hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
241 if (lpdis->CtlID == 3) {
242 GetObject(hLfArrowD, sizeof(BITMAP), (LPSTR)&bm);
243 SelectObject(hMemDC, hLfArrowD);
244 BitBlt(lpdis->hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
246 if (lpdis->CtlID == 4) {
247 GetObject(hRgArrowD, sizeof(BITMAP), (LPSTR)&bm);
248 SelectObject(hMemDC, hRgArrowD);
249 BitBlt(lpdis->hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
255 StdDrawScrollBar(hwnd);
258 return DefWindowProc( hwnd, message, wParam, lParam );
265 LPHEADSCROLL ScrollBarGetWindowAndStorage(HWND hwnd, WND **wndPtr)
269 *(wndPtr) = Ptr = WIN_FindWndPtr(hwnd);
271 printf("Bad Window handle on ScrollBar !\n");
274 lphs = *((LPHEADSCROLL *)&Ptr->wExtra[1]);
279 LPHEADSCROLL ScrollBarGetStorageHeader(HWND hwnd)
283 wndPtr = WIN_FindWndPtr(hwnd);
285 printf("Bad Window handle on ScrollBar !\n");
288 lphs = *((LPHEADSCROLL *)&wndPtr->wExtra[1]);
293 void StdDrawScrollBar(HWND hwnd)
302 hdc = BeginPaint( hwnd, &ps );
303 if (!IsWindowVisible(hwnd)) {
304 EndPaint( hwnd, &ps );
307 hBrush = SendMessage(GetParent(hwnd), WM_CTLCOLOR, (WORD)hdc,
308 MAKELONG(hwnd, CTLCOLOR_SCROLLBAR));
309 if (hBrush == (HBRUSH)NULL) hBrush = GetStockObject(LTGRAY_BRUSH);
310 lphs = ScrollBarGetStorageHeader(hwnd);
311 if (lphs == NULL) goto EndOfPaint;
312 GetClientRect(hwnd, &rect);
313 w = rect.right - rect.left;
314 h = rect.bottom - rect.top;
315 if (lphs->Direction == WM_VSCROLL) {
323 FillRect(hdc, &rect, hBrush);
324 if (lphs->Direction == WM_VSCROLL)
325 SetRect(&rect, 0, lphs->CurPix + w, w, lphs->CurPix + (w << 1));
327 SetRect(&rect, lphs->CurPix + h, 0, lphs->CurPix + (h << 1), h);
328 FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
329 InflateRect(&rect, -1, -1);
330 FillRect(hdc, &rect, GetStockObject(LTGRAY_BRUSH));
331 DrawReliefRect(hdc, rect, 2, 0);
332 InflateRect(&rect, -3, -3);
333 DrawReliefRect(hdc, rect, 1, 1);
334 if (!lphs->ThumbActive) {
335 InvalidateRect(lphs->hWndUp, NULL, TRUE);
336 UpdateWindow(lphs->hWndUp);
337 InvalidateRect(lphs->hWndDown, NULL, TRUE);
338 UpdateWindow(lphs->hWndDown);
341 EndPaint( hwnd, &ps );
346 int CreateScrollBarStruct(HWND hwnd)
352 wndPtr = WIN_FindWndPtr(hwnd);
353 lphs = (LPHEADSCROLL)malloc(sizeof(HEADSCROLL));
355 printf("Bad Memory Alloc on ScrollBar !\n");
360 printf("CreateScrollBarStruct %lX !\n", lphs);
362 *((LPHEADSCROLL *)&wndPtr->wExtra[1]) = lphs;
363 lphs->ThumbActive = FALSE;
368 width = wndPtr->rectClient.right - wndPtr->rectClient.left;
369 height = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
372 lphs->MaxPix = height - 3 * width;
373 lphs->Direction = WM_VSCROLL;
374 lphs->hWndUp = CreateWindow("BUTTON", "",
375 WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
376 0, 0, width, width, hwnd, 1, wndPtr->hInstance, 0L);
377 lphs->hWndDown = CreateWindow("BUTTON", "",
378 WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
379 0, height - width, width, width, hwnd, 2,
380 wndPtr->hInstance, 0L);
384 lphs->MaxPix = width - 3 * height;
385 lphs->Direction = WM_HSCROLL;
386 lphs->hWndUp = CreateWindow("BUTTON", "",
387 WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
388 0, 0, height, height, hwnd, 3, wndPtr->hInstance, 0L);
389 lphs->hWndDown = CreateWindow("BUTTON", "",
390 WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
391 width - height, 0, height, height, hwnd, 4,
392 wndPtr->hInstance, 0L);
394 if (lphs->MaxPix < 1) lphs->MaxPix = 1;
395 if (wndPtr->hCursor == (HCURSOR)NULL)
396 wndPtr->hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
401 /*************************************************************************
402 * GetScrollWindowHandle
404 HWND GetScrollWindowHandle(HWND hWnd, int nBar)
407 if (nBar != SB_CTL) {
408 wndPtr = WIN_FindWndPtr(hWnd);
409 if (nBar == SB_VERT) return wndPtr->hWndVScroll;
410 if (nBar == SB_HORZ) return wndPtr->hWndHScroll;
417 /*************************************************************************
418 * SetScrollPos [USER.62]
420 int SetScrollPos(HWND hwnd, int nBar, int nPos, BOOL bRedraw)
424 hwnd = GetScrollWindowHandle(hwnd, nBar);
425 lphs = ScrollBarGetStorageHeader(hwnd);
426 if (lphs == NULL) return 0;
428 lphs->CurVal = (short)nPos;
429 if (lphs->MaxVal != lphs->MinVal)
430 lphs->CurPix = lphs->MaxPix * (abs((short)nPos) - abs(lphs->MinVal)) /
431 (abs(lphs->MaxVal) - abs(lphs->MinVal));
432 if (lphs->CurPix > lphs->MaxPix) lphs->CurPix = lphs->MaxPix;
434 printf("SetScrollPos val=%d pixval=%d pixmax%d\n",
435 (short)nPos, lphs->CurPix, lphs->MaxPix);
436 printf("SetScrollPos min=%d max=%d\n",
437 lphs->MinVal, lphs->MaxVal);
439 if ((bRedraw) && (IsWindowVisible(hwnd))) {
440 InvalidateRect(hwnd, NULL, TRUE);
448 /*************************************************************************
449 * GetScrollPos [USER.63]
451 int GetScrollPos(HWND hwnd, int nBar)
454 hwnd = GetScrollWindowHandle(hwnd, nBar);
455 lphs = ScrollBarGetStorageHeader(hwnd);
456 if (lphs == NULL) return 0;
462 /*************************************************************************
463 * SetScrollRange [USER.64]
465 void SetScrollRange(HWND hwnd, int nBar, int MinPos, int MaxPos, BOOL bRedraw)
468 hwnd = GetScrollWindowHandle(hwnd, nBar);
469 lphs = ScrollBarGetStorageHeader(hwnd);
470 if (lphs == NULL) return;
471 lphs->MinVal = (short)MinPos;
472 lphs->MaxVal = (short)MaxPos;
473 if (lphs->MaxVal != lphs->MinVal)
474 lphs->CurPix = abs(lphs->MaxVal) *
475 (abs(lphs->CurVal) - abs(lphs->MinVal)) /
476 (abs(lphs->MaxVal) - abs(lphs->MinVal));
477 if (lphs->CurPix > lphs->MaxPix) lphs->CurPix = lphs->MaxPix;
479 printf("SetScrollRange min=%d max=%d\n", lphs->MinVal, lphs->MaxVal);
481 if ((bRedraw) && (IsWindowVisible(hwnd))) {
482 InvalidateRect(hwnd, NULL, TRUE);
489 /*************************************************************************
490 * GetScrollRange [USER.65]
492 void GetScrollRange(HWND hwnd, int nBar, LPINT lpMin, LPINT lpMax)
495 hwnd = GetScrollWindowHandle(hwnd, nBar);
496 lphs = ScrollBarGetStorageHeader(hwnd);
497 if (lphs == NULL) return;
498 *lpMin = lphs->MinVal;
499 *lpMax = lphs->MaxVal;
504 /*************************************************************************
505 * ShowScrollBar [USER.267]
507 void ShowScrollBar(HWND hWnd, WORD wBar, BOOL bFlag)
511 printf("ShowScrollBar hWnd=%04X wBar=%d bFlag=%d\n", hWnd, wBar, bFlag);
513 if (wBar == SB_CTL) {
515 ShowWindow(hWnd, SW_SHOW);
517 ShowWindow(hWnd, SW_HIDE);
520 wndPtr = WIN_FindWndPtr(hWnd);
521 if ((wBar == SB_VERT) || (wBar == SB_BOTH)) {
523 ShowWindow(wndPtr->hWndVScroll, SW_SHOW);
525 ShowWindow(wndPtr->hWndVScroll, SW_HIDE);
527 if ((wBar == SB_HORZ) || (wBar == SB_BOTH)) {
529 ShowWindow(wndPtr->hWndHScroll, SW_SHOW);
531 ShowWindow(wndPtr->hWndHScroll, SW_HIDE);