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>
23 LPHEADSCROLL
ScrollBarGetWindowAndStorage(HWND hwnd
, WND
**wndPtr
);
24 LPHEADSCROLL
ScrollBarGetStorageHeader(HWND hwnd
);
25 void StdDrawScrollBar(HWND hwnd
);
26 int CreateScrollBarStruct(HWND hwnd
);
29 void SCROLLBAR_CreateScrollBar(LPSTR className
, LPSTR scrollLabel
, HWND hwnd
)
31 WND
*wndPtr
= WIN_FindWndPtr(hwnd
);
32 WND
*parentPtr
= WIN_FindWndPtr(wndPtr
->hwndParent
);
36 #ifdef DEBUG_SCROLLBAR
37 printf("scroll: label = %s, x = %d, y = %d\n", scrollLabel
,
38 wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
);
39 printf(" width = %d, height = %d\n",
40 wndPtr
->rectClient
.right
- wndPtr
->rectClient
.left
,
41 wndPtr
->rectClient
.bottom
- wndPtr
->rectClient
.top
);
47 style
= wndPtr
->dwStyle
& 0x0000FFFF;
49 if ((style & SBS_NOTIFY) == SBS_NOTIFY)
51 sprintf(widgetName
, "%s%d", className
, wndPtr
->wIDmenu
);
52 wndPtr
->winWidget
= XtVaCreateManagedWidget(widgetName
,
55 XtNx
, wndPtr
->rectClient
.left
,
56 XtNy
, wndPtr
->rectClient
.top
,
57 XtNwidth
, wndPtr
->rectClient
.right
-
58 wndPtr
->rectClient
.left
,
59 XtNheight
, wndPtr
->rectClient
.bottom
-
60 wndPtr
->rectClient
.top
,
63 GlobalUnlock(wndPtr
->hwndParent
);
68 /***********************************************************************
69 * WIDGETS_ScrollBarWndProc
71 LONG
SCROLLBAR_ScrollBarWndProc( HWND hwnd
, WORD message
,
72 WORD wParam
, LONG lParam
)
83 CreateScrollBarStruct(hwnd
);
85 printf("ScrollBar Creation up=%X down=%X!\n", lphs
->hWndUp
, lphs
->hWndDown
);
89 lphs
= ScrollBarGetWindowAndStorage(hwnd
, &wndPtr
);
90 if (lphs
== 0) return 0;
92 printf("ScrollBar WM_DESTROY %lX !\n", lphs
);
94 DestroyWindow(lphs
->hWndUp
);
95 DestroyWindow(lphs
->hWndDown
);
97 *((LPHEADSCROLL
*)&wndPtr
->wExtra
[1]) = 0;
102 printf("ScrollBar WM_COMMAND wParam=%X lParam=%lX !\n", wParam
, lParam
);
104 lphs
= ScrollBarGetWindowAndStorage(hwnd
, &wndPtr
);
105 if (HIWORD(lParam
) != BN_CLICKED
) return 0;
106 if (LOWORD(lParam
) == lphs
->hWndUp
)
107 SendMessage(wndPtr
->hwndParent
, lphs
->Direction
,
108 SB_LINEUP
, MAKELONG(0, hwnd
));
109 if (LOWORD(lParam
) == lphs
->hWndDown
)
110 SendMessage(wndPtr
->hwndParent
, lphs
->Direction
,
111 SB_LINEDOWN
, MAKELONG(0, hwnd
));
118 lphs
= ScrollBarGetWindowAndStorage(hwnd
, &wndPtr
);
123 GetClientRect(hwnd
, &rect
);
124 if (lphs
->Direction
== WM_VSCROLL
) {
127 printf("WM_LBUTTONDOWN y=%d cur+right=%d %d\n",
128 y
, lphs
->CurPix
+ rect
.right
, lphs
->CurPix
+ (rect
.right
<< 1));
130 if (y
< (lphs
->CurPix
+ rect
.right
))
131 SendMessage(wndPtr
->hwndParent
, lphs
->Direction
,
132 SB_PAGEUP
, MAKELONG(0, hwnd
));
133 if (y
> (lphs
->CurPix
+ (rect
.right
<< 1)))
134 SendMessage(wndPtr
->hwndParent
, lphs
->Direction
,
135 SB_PAGEDOWN
, MAKELONG(0, hwnd
));
136 if ((y
> (lphs
->CurPix
+ rect
.right
)) &&
137 (y
< (lphs
->CurPix
+ (rect
.right
<< 1)))) {
138 lphs
->ThumbActive
= TRUE
;
140 printf("THUMB DOWN !\n");
147 printf("WM_LBUTTONDOWN x=%d Cur+bottom=%d %d\n",
148 x
, lphs
->CurPix
+ rect
.bottom
, lphs
->CurPix
+ (rect
.bottom
<< 1));
150 if (x
< (lphs
->CurPix
+ rect
.bottom
))
151 SendMessage(wndPtr
->hwndParent
, lphs
->Direction
,
152 SB_PAGEUP
, MAKELONG(0, hwnd
));
153 if (x
> (lphs
->CurPix
+ (rect
.bottom
<< 1)))
154 SendMessage(wndPtr
->hwndParent
, lphs
->Direction
,
155 SB_PAGEDOWN
, MAKELONG(0, hwnd
));
156 if ((x
> (lphs
->CurPix
+ rect
.bottom
)) &&
157 (x
< (lphs
->CurPix
+ (rect
.bottom
<< 1)))) {
158 lphs
->ThumbActive
= TRUE
;
160 printf("THUMB DOWN !\n");
166 lphs
= ScrollBarGetStorageHeader(hwnd
);
167 lphs
->ThumbActive
= FALSE
;
172 printf("ScrollBar WM_KEYDOWN wParam %X !\n", wParam
);
175 StdDrawScrollBar(hwnd
);
178 if ((wParam
& MK_LBUTTON
) != 0) {
179 lphs
= ScrollBarGetWindowAndStorage(hwnd
, &wndPtr
);
180 if (lphs
->ThumbActive
== 0) break;
181 GetClientRect(hwnd
, &rect
);
182 if (lphs
->Direction
== WM_VSCROLL
)
183 y
= HIWORD(lParam
) - rect
.right
- (rect
.right
>> 1);
185 y
= LOWORD(lParam
) - rect
.bottom
- (rect
.bottom
>> 1);
186 x
= (y
* (lphs
->MaxVal
- lphs
->MinVal
) /
187 lphs
->MaxPix
) + lphs
->MinVal
;
189 printf("WM_MOUSEMOVE val=%d pix=%d\n", x
, y
);
191 SendMessage(wndPtr
->hwndParent
, lphs
->Direction
,
192 SB_THUMBTRACK
, MAKELONG(x
, hwnd
));
196 return DefWindowProc( hwnd
, message
, wParam
, lParam
);
203 LPHEADSCROLL
ScrollBarGetWindowAndStorage(HWND hwnd
, WND
**wndPtr
)
207 *(wndPtr
) = Ptr
= WIN_FindWndPtr(hwnd
);
209 printf("Bad Window handle on ScrollBar !\n");
212 lphs
= *((LPHEADSCROLL
*)&Ptr
->wExtra
[1]);
217 LPHEADSCROLL
ScrollBarGetStorageHeader(HWND hwnd
)
221 wndPtr
= WIN_FindWndPtr(hwnd
);
223 printf("Bad Window handle on ScrollBar !\n");
226 lphs
= *((LPHEADSCROLL
*)&wndPtr
->wExtra
[1]);
231 void StdDrawScrollBar(HWND hwnd
)
240 hdc
= BeginPaint( hwnd
, &ps
);
241 if (!IsWindowVisible(hwnd
)) {
242 EndPaint( hwnd
, &ps
);
245 hBrush
= SendMessage(GetParent(hwnd
), WM_CTLCOLOR
, (WORD
)hdc
,
246 MAKELONG(hwnd
, CTLCOLOR_SCROLLBAR
));
247 if (hBrush
== (HBRUSH
)NULL
) hBrush
= GetStockObject(LTGRAY_BRUSH
);
248 lphs
= ScrollBarGetStorageHeader(hwnd
);
249 if (lphs
== NULL
) goto EndOfPaint
;
250 GetClientRect(hwnd
, &rect
);
251 w
= rect
.right
- rect
.left
;
252 h
= rect
.bottom
- rect
.top
;
253 if (lphs
->Direction
== WM_VSCROLL
) {
261 FillRect(hdc
, &rect
, hBrush
);
262 if (lphs
->Direction
== WM_VSCROLL
)
263 SetRect(&rect
, 0, lphs
->CurPix
+ w
, w
, lphs
->CurPix
+ (w
<< 1));
265 SetRect(&rect
, lphs
->CurPix
+ h
, 0, lphs
->CurPix
+ (h
<< 1), h
);
266 FrameRect(hdc
, &rect
, GetStockObject(BLACK_BRUSH
));
267 InflateRect(&rect
, -1, -1);
268 FillRect(hdc
, &rect
, GetStockObject(LTGRAY_BRUSH
));
269 DrawReliefRect(hdc
, rect
, 2, 0);
270 InflateRect(&rect
, -3, -3);
271 DrawReliefRect(hdc
, rect
, 1, 1);
272 if (!lphs
->ThumbActive
) {
273 InvalidateRect(lphs
->hWndUp
, NULL
, TRUE
);
274 UpdateWindow(lphs
->hWndUp
);
275 InvalidateRect(lphs
->hWndDown
, NULL
, TRUE
);
276 UpdateWindow(lphs
->hWndDown
);
279 EndPaint( hwnd
, &ps
);
284 int CreateScrollBarStruct(HWND hwnd
)
290 wndPtr
= WIN_FindWndPtr(hwnd
);
291 lphs
= (LPHEADSCROLL
)malloc(sizeof(HEADSCROLL
));
293 printf("Bad Memory Alloc on ScrollBar !\n");
298 printf("CreateScrollBarStruct %lX !\n", lphs
);
300 *((LPHEADSCROLL
*)&wndPtr
->wExtra
[1]) = lphs
;
301 lphs
->ThumbActive
= FALSE
;
306 width
= wndPtr
->rectClient
.right
- wndPtr
->rectClient
.left
;
307 height
= wndPtr
->rectClient
.bottom
- wndPtr
->rectClient
.top
;
310 lphs
->MaxPix
= height
- 3 * width
;
311 lphs
->Direction
= WM_VSCROLL
;
312 lphs
->hWndUp
= CreateWindow("BUTTON", "",
313 WS_CHILD
| WS_VISIBLE
| BS_PUSHBUTTON
,
314 0, 0, width
, width
, hwnd
, 1, wndPtr
->hInstance
, 0L);
315 lphs
->hWndDown
= CreateWindow("BUTTON", "",
316 WS_CHILD
| WS_VISIBLE
| BS_PUSHBUTTON
,
317 0, height
- width
, width
, width
, hwnd
, 2,
318 wndPtr
->hInstance
, 0L);
322 lphs
->MaxPix
= width
- 3 * height
;
323 lphs
->Direction
= WM_HSCROLL
;
324 lphs
->hWndUp
= CreateWindow("BUTTON", "",
325 WS_CHILD
| WS_VISIBLE
| BS_PUSHBUTTON
,
326 0, 0, height
, height
, hwnd
, 0, wndPtr
->hInstance
, 0L);
327 lphs
->hWndDown
= CreateWindow("BUTTON", "",
328 WS_CHILD
| WS_VISIBLE
| BS_PUSHBUTTON
,
329 width
- height
, 0, height
, height
, hwnd
, 0,
330 wndPtr
->hInstance
, 0L);
332 if (lphs
->MaxPix
< 1) lphs
->MaxPix
= 1;
338 int GetScrollPos(HWND hwnd
, int nBar
)
341 lphs
= ScrollBarGetStorageHeader(hwnd
);
342 if (lphs
== NULL
) return 0;
348 void GetScrollRange(HWND hwnd
, int nBar
, LPINT lpMin
, LPINT lpMax
)
351 lphs
= ScrollBarGetStorageHeader(hwnd
);
352 if (lphs
== NULL
) return;
353 *lpMin
= lphs
->MinVal
;
354 *lpMax
= lphs
->MaxVal
;
359 int SetScrollPos(HWND hwnd
, int nBar
, int nPos
, BOOL bRedraw
)
363 lphs
= ScrollBarGetStorageHeader(hwnd
);
364 if (lphs
== NULL
) return 0;
366 lphs
->CurVal
= (short)nPos
;
367 if (lphs
->MaxVal
!= lphs
->MinVal
)
368 lphs
->CurPix
= lphs
->MaxPix
* (abs((short)nPos
) - abs(lphs
->MinVal
)) /
369 (abs(lphs
->MaxVal
) - abs(lphs
->MinVal
));
370 if (lphs
->CurPix
> lphs
->MaxPix
) lphs
->CurPix
= lphs
->MaxPix
;
372 printf("SetScrollPos val=%d pixval=%d pixmax%d\n",
373 (short)nPos
, lphs
->CurPix
, lphs
->MaxPix
);
374 printf("SetScrollPos min=%d max=%d\n",
375 lphs
->MinVal
, lphs
->MaxVal
);
377 if ((bRedraw
) && (IsWindowVisible(hwnd
))) {
378 InvalidateRect(hwnd
, NULL
, TRUE
);
386 void SetScrollRange(HWND hwnd
, int nBar
, int MinPos
, int MaxPos
, BOOL bRedraw
)
389 lphs
= ScrollBarGetStorageHeader(hwnd
);
390 if (lphs
== NULL
) return;
391 lphs
->MinVal
= (short)MinPos
;
392 lphs
->MaxVal
= (short)MaxPos
;
393 if (lphs
->MaxVal
!= lphs
->MinVal
)
394 lphs
->CurPix
= abs(lphs
->MaxVal
) *
395 (abs(lphs
->CurVal
) - abs(lphs
->MinVal
)) /
396 (abs(lphs
->MaxVal
) - abs(lphs
->MinVal
));
397 if (lphs
->CurPix
> lphs
->MaxPix
) lphs
->CurPix
= lphs
->MaxPix
;
399 printf("SetScrollRange min=%d max=%d\n", lphs
->MinVal
, lphs
->MaxVal
);
401 if ((bRedraw
) && (IsWindowVisible(hwnd
))) {
402 InvalidateRect(hwnd
, NULL
, TRUE
);