Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / comctl32 / trackbar.c
blob2972c840842d7c7cea15bf1ef983653590d25df1
1 /*
2 * Trackbar control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1998, 1999 Alex Priem
6 * Copyright 2002 Dimitrie O. Paun
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * NOTE
24 * This code was audited for completeness against the documented features
25 * of Comctl32.dll version 6.0 on Sep. 12, 2002, by Dimitrie O. Paun.
27 * Unless otherwise noted, we believe this code to be complete, as per
28 * the specification mentioned above.
29 * If you discover missing features, or bugs, please note them below.
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wingdi.h"
41 #include "winuser.h"
42 #include "winnls.h"
43 #include "commctrl.h"
44 #include "wine/debug.h"
46 #include "comctl32.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(trackbar);
50 typedef struct
52 HWND hwndSelf;
53 LONG lRangeMin;
54 LONG lRangeMax;
55 LONG lLineSize;
56 LONG lPageSize;
57 LONG lSelMin;
58 LONG lSelMax;
59 LONG lPos;
60 UINT uThumbLen;
61 UINT uNumTics;
62 UINT uTicFreq;
63 HWND hwndNotify;
64 HWND hwndToolTip;
65 HWND hwndBuddyLA;
66 HWND hwndBuddyRB;
67 INT fLocation;
68 INT flags;
69 BOOL bUnicode;
70 BOOL bFocussed;
71 RECT rcChannel;
72 RECT rcSelection;
73 RECT rcThumb;
74 LPLONG tics;
75 } TRACKBAR_INFO;
77 #define TB_REFRESH_TIMER 1
78 #define TB_REFRESH_DELAY 500
80 #define TOOLTIP_OFFSET 2 /* distance from ctrl edge to tooltip */
82 /* Used by TRACKBAR_Refresh to find out which parts of the control
83 need to be recalculated */
85 #define TB_THUMBPOSCHANGED 1
86 #define TB_THUMBSIZECHANGED 2
87 #define TB_THUMBCHANGED (TB_THUMBPOSCHANGED | TB_THUMBSIZECHANGED)
88 #define TB_SELECTIONCHANGED 4
89 #define TB_DRAG_MODE 8 /* we're dragging the slider */
90 #define TB_AUTO_PAGE_LEFT 16
91 #define TB_AUTO_PAGE_RIGHT 32
92 #define TB_AUTO_PAGE (TB_AUTO_PAGE_LEFT | TB_AUTO_PAGE_RIGHT)
94 /* helper defines for TRACKBAR_DrawTic */
95 #define TIC_EDGE 0x20
96 #define TIC_SELECTIONMARKMAX 0x80
97 #define TIC_SELECTIONMARKMIN 0x100
98 #define TIC_SELECTIONMARK (TIC_SELECTIONMARKMAX | TIC_SELECTIONMARKMIN)
100 static inline int
101 notify_customdraw(TRACKBAR_INFO *infoPtr, NMCUSTOMDRAW *pnmcd, int stage)
103 pnmcd->dwDrawStage = stage;
104 return SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
105 pnmcd->hdr.idFrom, (LPARAM)pnmcd);
108 static LRESULT notify_hdr(TRACKBAR_INFO *infoPtr, INT code, LPNMHDR pnmh)
110 LRESULT result;
112 TRACE("(code=%d)\n", code);
114 pnmh->hwndFrom = infoPtr->hwndSelf;
115 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
116 pnmh->code = code;
117 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
118 (WPARAM)pnmh->idFrom, (LPARAM)pnmh);
120 TRACE(" <= %ld\n", result);
122 return result;
125 static inline int notify(TRACKBAR_INFO *infoPtr, INT code)
127 NMHDR nmh;
128 return notify_hdr(infoPtr, code, &nmh);
131 static BOOL
132 notify_with_scroll (TRACKBAR_INFO *infoPtr, UINT code)
134 BOOL bVert = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_VERT;
136 TRACE("%x\n", code);
138 return (BOOL) SendMessageW (infoPtr->hwndNotify,
139 bVert ? WM_VSCROLL : WM_HSCROLL,
140 (WPARAM)code, (LPARAM)infoPtr->hwndSelf);
143 static void TRACKBAR_RecalculateTics (TRACKBAR_INFO *infoPtr)
145 int i, tic, nrTics;
147 if (infoPtr->uTicFreq && infoPtr->lRangeMax >= infoPtr->lRangeMin)
148 nrTics=(infoPtr->lRangeMax - infoPtr->lRangeMin)/infoPtr->uTicFreq;
149 else {
150 nrTics = 0;
151 Free (infoPtr->tics);
152 infoPtr->tics = NULL;
153 infoPtr->uNumTics = 0;
154 return;
157 if (nrTics != infoPtr->uNumTics) {
158 infoPtr->tics=ReAlloc (infoPtr->tics,
159 (nrTics+1)*sizeof (DWORD));
160 if (!infoPtr->tics) {
161 infoPtr->uNumTics = 0;
162 notify(infoPtr, NM_OUTOFMEMORY);
163 return;
165 infoPtr->uNumTics = nrTics;
168 tic = infoPtr->lRangeMin + infoPtr->uTicFreq;
169 for (i = 0; i < nrTics; i++, tic += infoPtr->uTicFreq)
170 infoPtr->tics[i] = tic;
173 /* converts from physical (mouse) position to logical position
174 (in range of trackbar) */
176 static inline LONG
177 TRACKBAR_ConvertPlaceToPosition (TRACKBAR_INFO *infoPtr, int place,
178 int vertical)
180 double range, width, pos, offsetthumb;
182 range = infoPtr->lRangeMax - infoPtr->lRangeMin;
183 if (vertical) {
184 offsetthumb = (infoPtr->rcThumb.bottom - infoPtr->rcThumb.top)/2;
185 width = infoPtr->rcChannel.bottom - infoPtr->rcChannel.top - (offsetthumb * 2) - 1;
186 pos = (range*(place - infoPtr->rcChannel.top - offsetthumb)) / width;
187 } else {
188 offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
189 width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - (offsetthumb * 2) - 1;
190 pos = (range*(place - infoPtr->rcChannel.left - offsetthumb)) / width;
192 pos += infoPtr->lRangeMin;
193 if (pos > infoPtr->lRangeMax)
194 pos = infoPtr->lRangeMax;
195 else if (pos < infoPtr->lRangeMin)
196 pos = infoPtr->lRangeMin;
198 TRACE("%.2f\n", pos);
199 return (LONG)(pos + 0.5);
203 /* return: 0> prev, 0 none, >0 next */
204 static LONG
205 TRACKBAR_GetAutoPageDirection (TRACKBAR_INFO *infoPtr, POINT clickPoint)
207 DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
208 RECT pageRect;
210 if (dwStyle & TBS_VERT) {
211 pageRect.top = infoPtr->rcChannel.top;
212 pageRect.bottom = infoPtr->rcChannel.bottom;
213 pageRect.left = infoPtr->rcThumb.left;
214 pageRect.right = infoPtr->rcThumb.right;
215 } else {
216 pageRect.top = infoPtr->rcThumb.top;
217 pageRect.bottom = infoPtr->rcThumb.bottom;
218 pageRect.left = infoPtr->rcChannel.left;
219 pageRect.right = infoPtr->rcChannel.right;
223 if (PtInRect(&pageRect, clickPoint))
225 int clickPlace = (dwStyle & TBS_VERT) ? clickPoint.y : clickPoint.x;
227 LONG clickPos = TRACKBAR_ConvertPlaceToPosition(infoPtr, clickPlace,
228 dwStyle & TBS_VERT);
229 return clickPos - infoPtr->lPos;
232 return 0;
235 static void inline
236 TRACKBAR_PageDown (TRACKBAR_INFO *infoPtr)
238 if (infoPtr->lPos == infoPtr->lRangeMax) return;
240 infoPtr->lPos += infoPtr->lPageSize;
241 if (infoPtr->lPos > infoPtr->lRangeMax)
242 infoPtr->lPos = infoPtr->lRangeMax;
243 notify_with_scroll (infoPtr, TB_PAGEDOWN);
247 static void inline
248 TRACKBAR_PageUp (TRACKBAR_INFO *infoPtr)
250 if (infoPtr->lPos == infoPtr->lRangeMin) return;
252 infoPtr->lPos -= infoPtr->lPageSize;
253 if (infoPtr->lPos < infoPtr->lRangeMin)
254 infoPtr->lPos = infoPtr->lRangeMin;
255 notify_with_scroll (infoPtr, TB_PAGEUP);
258 static void inline TRACKBAR_LineUp(TRACKBAR_INFO *infoPtr)
260 if (infoPtr->lPos == infoPtr->lRangeMin) return;
261 infoPtr->lPos -= infoPtr->lLineSize;
262 if (infoPtr->lPos < infoPtr->lRangeMin)
263 infoPtr->lPos = infoPtr->lRangeMin;
264 notify_with_scroll (infoPtr, TB_LINEUP);
267 static void inline TRACKBAR_LineDown(TRACKBAR_INFO *infoPtr)
269 if (infoPtr->lPos == infoPtr->lRangeMax) return;
270 infoPtr->lPos += infoPtr->lLineSize;
271 if (infoPtr->lPos > infoPtr->lRangeMax)
272 infoPtr->lPos = infoPtr->lRangeMax;
273 notify_with_scroll (infoPtr, TB_LINEDOWN);
276 static void
277 TRACKBAR_CalcChannel (TRACKBAR_INFO *infoPtr)
279 DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
280 INT cyChannel, offsetthumb, offsetedge;
281 RECT lpRect, *channel = & infoPtr->rcChannel;
283 GetClientRect (infoPtr->hwndSelf, &lpRect);
285 offsetthumb = infoPtr->uThumbLen / 4;
286 offsetedge = offsetthumb + 3;
287 cyChannel = (dwStyle & TBS_ENABLESELRANGE) ? offsetthumb*3 : 4;
288 if (dwStyle & TBS_VERT) {
289 channel->top = lpRect.top + offsetedge;
290 channel->bottom = lpRect.bottom - offsetedge;
291 if (dwStyle & TBS_ENABLESELRANGE)
292 channel->left = lpRect.left + ((infoPtr->uThumbLen - cyChannel + 2) / 2);
293 else
294 channel->left = lpRect.left + (infoPtr->uThumbLen / 2) - 1;
295 if (dwStyle & TBS_BOTH) {
296 if (dwStyle & TBS_NOTICKS)
297 channel->left += 1;
298 else
299 channel->left += 9;
301 else if (dwStyle & TBS_TOP) {
302 if (dwStyle & TBS_NOTICKS)
303 channel->left += 2;
304 else
305 channel->left += 10;
307 channel->right = channel->left + cyChannel;
308 } else {
309 channel->left = lpRect.left + offsetedge;
310 channel->right = lpRect.right - offsetedge;
311 if (dwStyle & TBS_ENABLESELRANGE)
312 channel->top = lpRect.top + ((infoPtr->uThumbLen - cyChannel + 2) / 2);
313 else
314 channel->top = lpRect.top + (infoPtr->uThumbLen / 2) - 1;
315 if (dwStyle & TBS_BOTH) {
316 if (dwStyle & TBS_NOTICKS)
317 channel->top += 1;
318 else
319 channel->top += 9;
321 else if (dwStyle & TBS_TOP) {
322 if (dwStyle & TBS_NOTICKS)
323 channel->top += 2;
324 else
325 channel->top += 10;
327 channel->bottom = channel->top + cyChannel;
331 static void
332 TRACKBAR_CalcThumb (TRACKBAR_INFO *infoPtr, LONG lPos, RECT *thumb)
334 int range, width, height, thumbwidth;
335 DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
336 RECT lpRect;
338 range = infoPtr->lRangeMax - infoPtr->lRangeMin;
339 thumbwidth = (infoPtr->uThumbLen / 2) | 1;
341 if (!range) range = 1;
343 GetClientRect(infoPtr->hwndSelf, &lpRect);
344 if (dwStyle & TBS_VERT)
346 height = infoPtr->rcChannel.bottom - infoPtr->rcChannel.top - thumbwidth;
348 if ((dwStyle & (TBS_BOTH | TBS_LEFT)) && !(dwStyle & TBS_NOTICKS))
349 thumb->left = 10;
350 else
351 thumb->left = 2;
352 thumb->right = thumb->left + infoPtr->uThumbLen;
353 thumb->top = infoPtr->rcChannel.top +
354 (height*(lPos - infoPtr->lRangeMin))/range;
355 thumb->bottom = thumb->top + thumbwidth;
357 else
359 width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - thumbwidth;
361 thumb->left = infoPtr->rcChannel.left +
362 (width*(lPos - infoPtr->lRangeMin))/range;
363 thumb->right = thumb->left + thumbwidth;
364 if ((dwStyle & (TBS_BOTH | TBS_TOP)) && !(dwStyle & TBS_NOTICKS))
365 thumb->top = 10;
366 else
367 thumb->top = 2;
368 thumb->bottom = thumb->top + infoPtr->uThumbLen;
372 inline static void
373 TRACKBAR_UpdateThumb (TRACKBAR_INFO *infoPtr)
375 TRACKBAR_CalcThumb(infoPtr, infoPtr->lPos, &infoPtr->rcThumb);
378 static inline void
379 TRACKBAR_InvalidateAll(TRACKBAR_INFO * infoPtr)
381 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
384 static void
385 TRACKBAR_InvalidateThumb (TRACKBAR_INFO *infoPtr, LONG thumbPos)
387 RECT rcThumb;
389 TRACKBAR_CalcThumb(infoPtr, thumbPos, &rcThumb);
390 InflateRect(&rcThumb, 1, 1);
391 InvalidateRect(infoPtr->hwndSelf, &rcThumb, FALSE);
394 static inline void
395 TRACKBAR_InvalidateThumbMove (TRACKBAR_INFO *infoPtr, LONG oldPos, LONG newPos)
397 TRACKBAR_InvalidateThumb (infoPtr, oldPos);
398 if (newPos != oldPos)
399 TRACKBAR_InvalidateThumb (infoPtr, newPos);
402 static BOOL inline
403 TRACKBAR_HasSelection (TRACKBAR_INFO *infoPtr)
405 return infoPtr->lSelMin != infoPtr->lSelMax;
408 static void
409 TRACKBAR_CalcSelection (TRACKBAR_INFO *infoPtr)
411 RECT *selection = &infoPtr->rcSelection;
412 int range = infoPtr->lRangeMax - infoPtr->lRangeMin;
413 int offsetthumb, height, width;
415 if (range <= 0) {
416 SetRectEmpty (selection);
417 } else {
418 if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_VERT) {
419 offsetthumb = (infoPtr->rcThumb.bottom - infoPtr->rcThumb.top)/2;
420 height = infoPtr->rcChannel.bottom - infoPtr->rcChannel.top - offsetthumb*2;
421 selection->top = infoPtr->rcChannel.top + offsetthumb +
422 (height*infoPtr->lSelMin)/range;
423 selection->bottom = infoPtr->rcChannel.top + offsetthumb +
424 (height*infoPtr->lSelMax)/range;
425 selection->left = infoPtr->rcChannel.left + 3;
426 selection->right = infoPtr->rcChannel.right - 3;
427 } else {
428 offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
429 width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - offsetthumb*2;
430 selection->left = infoPtr->rcChannel.left + offsetthumb +
431 (width*infoPtr->lSelMin)/range;
432 selection->right = infoPtr->rcChannel.left + offsetthumb +
433 (width*infoPtr->lSelMax)/range;
434 selection->top = infoPtr->rcChannel.top + 3;
435 selection->bottom = infoPtr->rcChannel.bottom - 3;
439 TRACE("selection[left=%ld, top=%ld, right=%ld, bottom=%ld]\n",
440 selection->left, selection->top, selection->right, selection->bottom);
443 static BOOL
444 TRACKBAR_AutoPage (TRACKBAR_INFO *infoPtr, POINT clickPoint)
446 LONG dir = TRACKBAR_GetAutoPageDirection(infoPtr, clickPoint);
447 LONG prevPos = infoPtr->lPos;
449 TRACE("x=%ld, y=%ld, dir=%ld\n", clickPoint.x, clickPoint.y, dir);
451 if (dir > 0 && (infoPtr->flags & TB_AUTO_PAGE_RIGHT))
452 TRACKBAR_PageDown(infoPtr);
453 else if (dir < 0 && (infoPtr->flags & TB_AUTO_PAGE_LEFT))
454 TRACKBAR_PageUp(infoPtr);
455 else return FALSE;
457 infoPtr->flags |= TB_THUMBPOSCHANGED;
458 TRACKBAR_InvalidateThumbMove (infoPtr, prevPos, infoPtr->lPos);
460 return TRUE;
463 /* Trackbar drawing code. I like my spaghetti done milanese. */
465 static void
466 TRACKBAR_DrawChannel (TRACKBAR_INFO *infoPtr, HDC hdc, DWORD dwStyle)
468 RECT rcChannel = infoPtr->rcChannel;
470 DrawEdge (hdc, &rcChannel, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
471 if (dwStyle & TBS_ENABLESELRANGE) { /* fill the channel */
472 FillRect (hdc, &rcChannel, GetStockObject(WHITE_BRUSH));
473 if (TRACKBAR_HasSelection(infoPtr))
474 FillRect (hdc, &infoPtr->rcSelection, GetSysColorBrush(COLOR_HIGHLIGHT));
478 static void
479 TRACKBAR_DrawOneTic (TRACKBAR_INFO *infoPtr, HDC hdc, LONG ticPos, int flags)
481 int x, y, ox, oy, range, side, indent = 0, len = 3;
482 int offsetthumb;
483 RECT rcTics;
485 if (flags & TBS_VERT) {
486 offsetthumb = (infoPtr->rcThumb.bottom - infoPtr->rcThumb.top)/2;
487 rcTics.left = infoPtr->rcThumb.left - 2;
488 rcTics.right = infoPtr->rcThumb.right + 2;
489 rcTics.top = infoPtr->rcChannel.top + offsetthumb + 1;
490 rcTics.bottom = infoPtr->rcChannel.bottom - offsetthumb;
491 } else {
492 offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
493 rcTics.left = infoPtr->rcChannel.left + offsetthumb + 1;
494 rcTics.right = infoPtr->rcChannel.right - offsetthumb;
495 rcTics.top = infoPtr->rcThumb.top - 2;
496 rcTics.bottom = infoPtr->rcThumb.bottom + 2;
499 if (flags & (TBS_TOP | TBS_LEFT)) {
500 x = rcTics.left;
501 y = rcTics.top;
502 side = -1;
503 } else {
504 x = rcTics.right;
505 y = rcTics.bottom;
506 side = 1;
509 range = infoPtr->lRangeMax - infoPtr->lRangeMin;
510 if (range <= 0)
511 range = 1; /* to avoid division by zero */
513 if (flags & TIC_SELECTIONMARK) {
514 indent = (flags & TIC_SELECTIONMARKMIN) ? -1 : 1;
515 } else if (flags & TIC_EDGE) {
516 len++;
519 if (flags & TBS_VERT) {
520 int height = rcTics.bottom - rcTics.top;
521 y = rcTics.top + (height*(ticPos - infoPtr->lRangeMin))/range;
522 } else {
523 int width = rcTics.right - rcTics.left;
524 x = rcTics.left + (width*(ticPos - infoPtr->lRangeMin))/range;
527 ox = x;
528 oy = y;
529 MoveToEx(hdc, x, y, 0);
530 if (flags & TBS_VERT) x += len * side;
531 else y += len * side;
532 LineTo(hdc, x, y);
534 if (flags & TIC_SELECTIONMARK) {
535 if (flags & TBS_VERT) {
536 x -= side;
537 } else {
538 y -= side;
540 MoveToEx(hdc, x, y, 0);
541 if (flags & TBS_VERT) {
542 y += 2 * indent;
543 } else {
544 x += 2 * indent;
547 LineTo(hdc, x, y);
548 LineTo(hdc, ox, oy);
553 static inline void
554 TRACKBAR_DrawTic (TRACKBAR_INFO *infoPtr, HDC hdc, LONG ticPos, int flags)
556 if ((flags & (TBS_LEFT | TBS_TOP)) || (flags & TBS_BOTH))
557 TRACKBAR_DrawOneTic (infoPtr, hdc, ticPos, flags | TBS_LEFT);
559 if (!(flags & (TBS_LEFT | TBS_TOP)) || (flags & TBS_BOTH))
560 TRACKBAR_DrawOneTic (infoPtr, hdc, ticPos, flags & ~TBS_LEFT);
563 static void
564 TRACKBAR_DrawTics (TRACKBAR_INFO *infoPtr, HDC hdc, DWORD dwStyle)
566 unsigned int i;
567 int ticFlags = dwStyle & 0x0f;
568 LOGPEN ticPen = { PS_SOLID, {1, 0}, GetSysColor (COLOR_3DDKSHADOW) };
569 HPEN hOldPen, hTicPen;
571 /* create the pen to draw the tics with */
572 hTicPen = CreatePenIndirect(&ticPen);
573 hOldPen = hTicPen ? SelectObject(hdc, hTicPen) : 0;
575 /* actually draw the tics */
576 for (i=0; i<infoPtr->uNumTics; i++)
577 TRACKBAR_DrawTic (infoPtr, hdc, infoPtr->tics[i], ticFlags);
579 TRACKBAR_DrawTic (infoPtr, hdc, infoPtr->lRangeMin, ticFlags | TIC_EDGE);
580 TRACKBAR_DrawTic (infoPtr, hdc, infoPtr->lRangeMax, ticFlags | TIC_EDGE);
582 if ((dwStyle & TBS_ENABLESELRANGE) && TRACKBAR_HasSelection(infoPtr)) {
583 TRACKBAR_DrawTic (infoPtr, hdc, infoPtr->lSelMin,
584 ticFlags | TIC_SELECTIONMARKMIN);
585 TRACKBAR_DrawTic (infoPtr, hdc, infoPtr->lSelMax,
586 ticFlags | TIC_SELECTIONMARKMAX);
589 /* clean up the pen, if we created one */
590 if (hTicPen) {
591 SelectObject(hdc, hOldPen);
592 DeleteObject(hTicPen);
596 static void
597 TRACKBAR_DrawThumb(TRACKBAR_INFO *infoPtr, HDC hdc, DWORD dwStyle)
599 HBRUSH oldbr;
600 HPEN oldpen;
601 RECT thumb = infoPtr->rcThumb;
602 int BlackUntil = 3;
603 int PointCount = 6;
604 POINT points[6];
605 int fillClr;
606 int PointDepth;
608 fillClr = infoPtr->flags & TB_DRAG_MODE ? COLOR_BTNHILIGHT : COLOR_BTNFACE;
609 oldbr = SelectObject (hdc, GetSysColorBrush(fillClr));
610 SetPolyFillMode (hdc, WINDING);
612 if (dwStyle & TBS_BOTH)
614 points[0].x=thumb.right;
615 points[0].y=thumb.top;
616 points[1].x=thumb.right;
617 points[1].y=thumb.bottom;
618 points[2].x=thumb.left;
619 points[2].y=thumb.bottom;
620 points[3].x=thumb.left;
621 points[3].y=thumb.top;
622 points[4].x=points[0].x;
623 points[4].y=points[0].y;
624 PointCount = 5;
625 BlackUntil = 3;
627 else
629 if (dwStyle & TBS_VERT)
631 PointDepth = (thumb.bottom - thumb.top) / 2;
632 if (dwStyle & TBS_LEFT)
634 points[0].x=thumb.right;
635 points[0].y=thumb.top;
636 points[1].x=thumb.right;
637 points[1].y=thumb.bottom;
638 points[2].x=thumb.left + PointDepth;
639 points[2].y=thumb.bottom;
640 points[3].x=thumb.left;
641 points[3].y=(thumb.bottom - thumb.top) / 2 + thumb.top + 1;
642 points[4].x=thumb.left + PointDepth;
643 points[4].y=thumb.top;
644 points[5].x=points[0].x;
645 points[5].y=points[0].y;
646 BlackUntil = 4;
648 else
650 points[0].x=thumb.right;
651 points[0].y=(thumb.bottom - thumb.top) / 2 + thumb.top + 1;
652 points[1].x=thumb.right - PointDepth;
653 points[1].y=thumb.bottom;
654 points[2].x=thumb.left;
655 points[2].y=thumb.bottom;
656 points[3].x=thumb.left;
657 points[3].y=thumb.top;
658 points[4].x=thumb.right - PointDepth;
659 points[4].y=thumb.top;
660 points[5].x=points[0].x;
661 points[5].y=points[0].y;
664 else
666 PointDepth = (thumb.right - thumb.left) / 2;
667 if (dwStyle & TBS_TOP)
669 points[0].x=(thumb.right - thumb.left) / 2 + thumb.left + 1;
670 points[0].y=thumb.top;
671 points[1].x=thumb.right;
672 points[1].y=thumb.top + PointDepth;
673 points[2].x=thumb.right;
674 points[2].y=thumb.bottom;
675 points[3].x=thumb.left;
676 points[3].y=thumb.bottom;
677 points[4].x=thumb.left;
678 points[4].y=thumb.top + PointDepth;
679 points[5].x=points[0].x;
680 points[5].y=points[0].y;
681 BlackUntil = 4;
683 else
685 points[0].x=thumb.right;
686 points[0].y=thumb.top;
687 points[1].x=thumb.right;
688 points[1].y=thumb.bottom - PointDepth;
689 points[2].x=(thumb.right - thumb.left) / 2 + thumb.left + 1;
690 points[2].y=thumb.bottom;
691 points[3].x=thumb.left;
692 points[3].y=thumb.bottom - PointDepth;
693 points[4].x=thumb.left;
694 points[4].y=thumb.top;
695 points[5].x=points[0].x;
696 points[5].y=points[0].y;
702 /* Draw the thumb now */
703 Polygon (hdc, points, PointCount);
704 oldpen = SelectObject(hdc, GetStockObject(BLACK_PEN));
705 Polyline(hdc,points, BlackUntil);
706 SelectObject(hdc, GetStockObject(WHITE_PEN));
707 Polyline(hdc, &points[BlackUntil-1], PointCount+1-BlackUntil);
708 SelectObject(hdc, oldpen);
709 SelectObject(hdc, oldbr);
713 static void inline
714 TRACKBAR_ActivateToolTip (TRACKBAR_INFO *infoPtr, BOOL fShow)
716 TTTOOLINFOW ti;
718 if (!infoPtr->hwndToolTip) return;
720 ZeroMemory(&ti, sizeof(ti));
721 ti.cbSize = sizeof(ti);
722 ti.hwnd = infoPtr->hwndSelf;
724 SendMessageW (infoPtr->hwndToolTip, TTM_TRACKACTIVATE, fShow, (LPARAM)&ti);
728 static void
729 TRACKBAR_UpdateToolTip (TRACKBAR_INFO *infoPtr)
731 DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
732 WCHAR buf[80];
733 static const WCHAR fmt[] = { '%', 'l', 'd', 0 };
734 TTTOOLINFOW ti;
735 POINT pt;
736 RECT rcClient;
737 LRESULT size;
739 if (!infoPtr->hwndToolTip) return;
741 ZeroMemory(&ti, sizeof(ti));
742 ti.cbSize = sizeof(ti);
743 ti.hwnd = infoPtr->hwndSelf;
744 ti.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
746 wsprintfW (buf, fmt, infoPtr->lPos);
747 ti.lpszText = buf;
748 SendMessageW (infoPtr->hwndToolTip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
750 GetClientRect (infoPtr->hwndSelf, &rcClient);
751 size = SendMessageW (infoPtr->hwndToolTip, TTM_GETBUBBLESIZE, 0, (LPARAM)&ti);
752 if (dwStyle & TBS_VERT) {
753 if (infoPtr->fLocation == TBTS_LEFT)
754 pt.x = 0 - LOWORD(size) - TOOLTIP_OFFSET;
755 else
756 pt.x = rcClient.right + TOOLTIP_OFFSET;
757 pt.y = (infoPtr->rcThumb.top + infoPtr->rcThumb.bottom - HIWORD(size))/2;
758 } else {
759 if (infoPtr->fLocation == TBTS_TOP)
760 pt.y = 0 - HIWORD(size) - TOOLTIP_OFFSET;
761 else
762 pt.y = rcClient.bottom + TOOLTIP_OFFSET;
763 pt.x = (infoPtr->rcThumb.left + infoPtr->rcThumb.right - LOWORD(size))/2;
765 ClientToScreen(infoPtr->hwndSelf, &pt);
767 SendMessageW (infoPtr->hwndToolTip, TTM_TRACKPOSITION,
768 0, (LPARAM)MAKELPARAM(pt.x, pt.y));
772 static void
773 TRACKBAR_Refresh (TRACKBAR_INFO *infoPtr, HDC hdcDst)
775 DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
776 RECT rcClient;
777 HDC hdc;
778 HBITMAP hOldBmp = 0, hOffScreenBmp = 0;
779 NMCUSTOMDRAW nmcd;
780 int gcdrf, icdrf;
782 if (infoPtr->flags & TB_THUMBCHANGED) {
783 TRACKBAR_UpdateThumb (infoPtr);
784 if (infoPtr->flags & TB_THUMBSIZECHANGED)
785 TRACKBAR_CalcChannel (infoPtr);
787 if (infoPtr->flags & TB_SELECTIONCHANGED)
788 TRACKBAR_CalcSelection (infoPtr);
790 if (infoPtr->flags & TB_DRAG_MODE)
791 TRACKBAR_UpdateToolTip (infoPtr);
793 infoPtr->flags &= ~ (TB_THUMBCHANGED | TB_SELECTIONCHANGED);
795 GetClientRect (infoPtr->hwndSelf, &rcClient);
797 /* try to render offscreen, if we fail, carrry onscreen */
798 hdc = CreateCompatibleDC(hdcDst);
799 if (hdc) {
800 hOffScreenBmp = CreateCompatibleBitmap(hdcDst, rcClient.right, rcClient.bottom);
801 if (hOffScreenBmp) {
802 hOldBmp = SelectObject(hdc, hOffScreenBmp);
803 } else {
804 DeleteObject(hdc);
805 hdc = hdcDst;
807 } else {
808 hdc = hdcDst;
811 ZeroMemory(&nmcd, sizeof(nmcd));
812 nmcd.hdr.hwndFrom = infoPtr->hwndSelf;
813 nmcd.hdr.idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
814 nmcd.hdr.code = NM_CUSTOMDRAW;
815 nmcd.hdc = hdc;
817 /* start the paint cycle */
818 nmcd.rc = rcClient;
819 gcdrf = notify_customdraw(infoPtr, &nmcd, CDDS_PREPAINT);
820 if (gcdrf & CDRF_SKIPDEFAULT) goto cleanup;
822 /* Erase backbround */
823 if (gcdrf == CDRF_DODEFAULT ||
824 notify_customdraw(infoPtr, &nmcd, CDDS_PREERASE) != CDRF_SKIPDEFAULT) {
825 FillRect (hdc, &rcClient, GetSysColorBrush(COLOR_BTNFACE));
826 if (gcdrf != CDRF_DODEFAULT)
827 notify_customdraw(infoPtr, &nmcd, CDDS_POSTERASE);
830 /* draw channel */
831 if (gcdrf & CDRF_NOTIFYITEMDRAW) {
832 nmcd.dwItemSpec = TBCD_CHANNEL;
833 nmcd.uItemState = CDIS_DEFAULT;
834 nmcd.rc = infoPtr->rcChannel;
835 icdrf = notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPREPAINT);
836 } else icdrf = CDRF_DODEFAULT;
837 if ( !(icdrf & CDRF_SKIPDEFAULT) ) {
838 TRACKBAR_DrawChannel (infoPtr, hdc, dwStyle);
839 if (icdrf & CDRF_NOTIFYPOSTPAINT)
840 notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPOSTPAINT);
844 /* draw tics */
845 if (!(dwStyle & TBS_NOTICKS)) {
846 if (gcdrf & CDRF_NOTIFYITEMDRAW) {
847 nmcd.dwItemSpec = TBCD_TICS;
848 nmcd.uItemState = CDIS_DEFAULT;
849 nmcd.rc = rcClient;
850 icdrf = notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPREPAINT);
851 } else icdrf = CDRF_DODEFAULT;
852 if ( !(icdrf & CDRF_SKIPDEFAULT) ) {
853 TRACKBAR_DrawTics (infoPtr, hdc, dwStyle);
854 if (icdrf & CDRF_NOTIFYPOSTPAINT)
855 notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPOSTPAINT);
859 /* draw thumb */
860 if (!(dwStyle & TBS_NOTHUMB)) {
861 if (gcdrf & CDRF_NOTIFYITEMDRAW) {
862 nmcd.dwItemSpec = TBCD_THUMB;
863 nmcd.uItemState = infoPtr->flags & TB_DRAG_MODE ? CDIS_HOT : CDIS_DEFAULT;
864 nmcd.rc = infoPtr->rcThumb;
865 icdrf = notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPREPAINT);
866 } else icdrf = CDRF_DODEFAULT;
867 if ( !(icdrf & CDRF_SKIPDEFAULT) ) {
868 TRACKBAR_DrawThumb(infoPtr, hdc, dwStyle);
869 if (icdrf & CDRF_NOTIFYPOSTPAINT)
870 notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPOSTPAINT);
874 /* draw focus rectangle */
875 if (infoPtr->bFocussed) {
876 DrawFocusRect(hdc, &rcClient);
879 /* finish up the painting */
880 if (gcdrf & CDRF_NOTIFYPOSTPAINT)
881 notify_customdraw(infoPtr, &nmcd, CDDS_POSTPAINT);
883 cleanup:
884 /* cleanup, if we rendered offscreen */
885 if (hdc != hdcDst) {
886 BitBlt(hdcDst, 0, 0, rcClient.right, rcClient.bottom, hdc, 0, 0, SRCCOPY);
887 SelectObject(hdc, hOldBmp);
888 DeleteObject(hOffScreenBmp);
889 DeleteObject(hdc);
894 static void
895 TRACKBAR_AlignBuddies (TRACKBAR_INFO *infoPtr)
897 DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
898 HWND hwndParent = GetParent (infoPtr->hwndSelf);
899 RECT rcSelf, rcBuddy;
900 INT x, y;
902 GetWindowRect (infoPtr->hwndSelf, &rcSelf);
903 MapWindowPoints (HWND_DESKTOP, hwndParent, (LPPOINT)&rcSelf, 2);
905 /* align buddy left or above */
906 if (infoPtr->hwndBuddyLA) {
907 GetWindowRect (infoPtr->hwndBuddyLA, &rcBuddy);
908 MapWindowPoints (HWND_DESKTOP, hwndParent, (LPPOINT)&rcBuddy, 2);
910 if (dwStyle & TBS_VERT) {
911 x = (infoPtr->rcChannel.right + infoPtr->rcChannel.left) / 2 -
912 (rcBuddy.right - rcBuddy.left) / 2 + rcSelf.left;
913 y = rcSelf.top - (rcBuddy.bottom - rcBuddy.top);
915 else {
916 x = rcSelf.left - (rcBuddy.right - rcBuddy.left);
917 y = (infoPtr->rcChannel.bottom + infoPtr->rcChannel.top) / 2 -
918 (rcBuddy.bottom - rcBuddy.top) / 2 + rcSelf.top;
921 SetWindowPos (infoPtr->hwndBuddyLA, 0, x, y, 0, 0,
922 SWP_NOZORDER | SWP_NOSIZE);
926 /* align buddy right or below */
927 if (infoPtr->hwndBuddyRB) {
928 GetWindowRect (infoPtr->hwndBuddyRB, &rcBuddy);
929 MapWindowPoints (HWND_DESKTOP, hwndParent, (LPPOINT)&rcBuddy, 2);
931 if (dwStyle & TBS_VERT) {
932 x = (infoPtr->rcChannel.right + infoPtr->rcChannel.left) / 2 -
933 (rcBuddy.right - rcBuddy.left) / 2 + rcSelf.left;
934 y = rcSelf.bottom;
936 else {
937 x = rcSelf.right;
938 y = (infoPtr->rcChannel.bottom + infoPtr->rcChannel.top) / 2 -
939 (rcBuddy.bottom - rcBuddy.top) / 2 + rcSelf.top;
941 SetWindowPos (infoPtr->hwndBuddyRB, 0, x, y, 0, 0,
942 SWP_NOZORDER | SWP_NOSIZE);
947 static LRESULT
948 TRACKBAR_ClearSel (TRACKBAR_INFO *infoPtr, BOOL fRedraw)
950 infoPtr->lSelMin = 0;
951 infoPtr->lSelMax = 0;
952 infoPtr->flags |= TB_SELECTIONCHANGED;
954 if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
956 return 0;
960 static LRESULT
961 TRACKBAR_ClearTics (TRACKBAR_INFO *infoPtr, BOOL fRedraw)
963 if (infoPtr->tics) {
964 Free (infoPtr->tics);
965 infoPtr->tics = NULL;
966 infoPtr->uNumTics = 0;
969 if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
971 return 0;
975 static LRESULT inline
976 TRACKBAR_GetChannelRect (TRACKBAR_INFO *infoPtr, LPRECT lprc)
978 if (lprc == NULL) return 0;
980 lprc->left = infoPtr->rcChannel.left;
981 lprc->right = infoPtr->rcChannel.right;
982 lprc->bottom = infoPtr->rcChannel.bottom;
983 lprc->top = infoPtr->rcChannel.top;
985 return 0;
989 static LONG inline
990 TRACKBAR_GetNumTics (TRACKBAR_INFO *infoPtr)
992 if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_NOTICKS)
993 return 0;
995 return infoPtr->uNumTics + 2;
999 static int comp_tics(const void *ap, const void *bp)
1001 DWORD a = *((DWORD *)ap);
1002 DWORD b = *((DWORD *)bp);
1004 TRACE("(a=%ld, b=%ld)\n", a, b);
1005 if (a < b) return -1;
1006 if (a > b) return 1;
1007 return 0;
1011 static LONG inline
1012 TRACKBAR_GetTic (TRACKBAR_INFO *infoPtr, INT iTic)
1014 if ((iTic < 0) || (iTic >= infoPtr->uNumTics) || !infoPtr->tics)
1015 return -1;
1017 qsort(infoPtr->tics, infoPtr->uNumTics, sizeof(DWORD), comp_tics);
1018 return infoPtr->tics[iTic];
1022 static LONG inline
1023 TRACKBAR_GetTicPos (TRACKBAR_INFO *infoPtr, INT iTic)
1025 LONG range, width, pos, tic;
1026 int offsetthumb;
1028 if ((iTic < 0) || (iTic >= infoPtr->uNumTics) || !infoPtr->tics)
1029 return -1;
1031 tic = TRACKBAR_GetTic (infoPtr, iTic);
1032 range = infoPtr->lRangeMax - infoPtr->lRangeMin;
1033 if (range <= 0) range = 1;
1034 offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
1035 width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - offsetthumb*2;
1036 pos = infoPtr->rcChannel.left + offsetthumb + (width * tic) / range;
1038 return pos;
1042 static HWND
1043 TRACKBAR_SetBuddy (TRACKBAR_INFO *infoPtr, BOOL fLocation, HWND hwndBuddy)
1045 HWND hwndTemp;
1047 if (fLocation) {
1048 /* buddy is left or above */
1049 hwndTemp = infoPtr->hwndBuddyLA;
1050 infoPtr->hwndBuddyLA = hwndBuddy;
1052 else {
1053 /* buddy is right or below */
1054 hwndTemp = infoPtr->hwndBuddyRB;
1055 infoPtr->hwndBuddyRB = hwndBuddy;
1058 TRACKBAR_AlignBuddies (infoPtr);
1060 return hwndTemp;
1064 static LONG inline
1065 TRACKBAR_SetLineSize (TRACKBAR_INFO *infoPtr, LONG lLineSize)
1067 LONG lTemp = infoPtr->lLineSize;
1069 infoPtr->lLineSize = lLineSize;
1071 return lTemp;
1075 static LONG inline
1076 TRACKBAR_SetPageSize (TRACKBAR_INFO *infoPtr, LONG lPageSize)
1078 LONG lTemp = infoPtr->lPageSize;
1080 infoPtr->lPageSize = lPageSize;
1082 return lTemp;
1086 static LRESULT inline
1087 TRACKBAR_SetPos (TRACKBAR_INFO *infoPtr, BOOL fPosition, LONG lPosition)
1089 LONG oldPos = infoPtr->lPos;
1090 infoPtr->lPos = lPosition;
1092 if (infoPtr->lPos < infoPtr->lRangeMin)
1093 infoPtr->lPos = infoPtr->lRangeMin;
1095 if (infoPtr->lPos > infoPtr->lRangeMax)
1096 infoPtr->lPos = infoPtr->lRangeMax;
1097 infoPtr->flags |= TB_THUMBPOSCHANGED;
1099 if (fPosition) TRACKBAR_InvalidateThumbMove(infoPtr, oldPos, lPosition);
1101 return 0;
1105 static LRESULT inline
1106 TRACKBAR_SetRange (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lRange)
1108 infoPtr->lRangeMin = (SHORT)LOWORD(lRange);
1109 infoPtr->lRangeMax = (SHORT)HIWORD(lRange);
1111 if (infoPtr->lPos < infoPtr->lRangeMin) {
1112 infoPtr->lPos = infoPtr->lRangeMin;
1113 infoPtr->flags |= TB_THUMBPOSCHANGED;
1116 if (infoPtr->lPos > infoPtr->lRangeMax) {
1117 infoPtr->lPos = infoPtr->lRangeMax;
1118 infoPtr->flags |= TB_THUMBPOSCHANGED;
1121 infoPtr->lPageSize = (infoPtr->lRangeMax - infoPtr->lRangeMin) / 5;
1122 if (infoPtr->lPageSize == 0) infoPtr->lPageSize = 1;
1124 if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1126 return 0;
1130 static LRESULT inline
1131 TRACKBAR_SetRangeMax (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lMax)
1133 infoPtr->lRangeMax = lMax;
1134 if (infoPtr->lPos > infoPtr->lRangeMax) {
1135 infoPtr->lPos = infoPtr->lRangeMax;
1136 infoPtr->flags |= TB_THUMBPOSCHANGED;
1139 infoPtr->lPageSize = (infoPtr->lRangeMax - infoPtr->lRangeMin) / 5;
1140 if (infoPtr->lPageSize == 0) infoPtr->lPageSize = 1;
1142 if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1144 return 0;
1148 static LRESULT inline
1149 TRACKBAR_SetRangeMin (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lMin)
1151 infoPtr->lRangeMin = lMin;
1152 if (infoPtr->lPos < infoPtr->lRangeMin) {
1153 infoPtr->lPos = infoPtr->lRangeMin;
1154 infoPtr->flags |= TB_THUMBPOSCHANGED;
1157 infoPtr->lPageSize = (infoPtr->lRangeMax - infoPtr->lRangeMin) / 5;
1158 if (infoPtr->lPageSize == 0) infoPtr->lPageSize = 1;
1160 if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1162 return 0;
1166 static LRESULT inline
1167 TRACKBAR_SetSel (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lSel)
1169 if (!GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_ENABLESELRANGE)
1170 return 0;
1172 infoPtr->lSelMin = (SHORT)LOWORD(lSel);
1173 infoPtr->lSelMax = (SHORT)HIWORD(lSel);
1174 infoPtr->flags |= TB_SELECTIONCHANGED;
1176 if (infoPtr->lSelMin < infoPtr->lRangeMin)
1177 infoPtr->lSelMin = infoPtr->lRangeMin;
1178 if (infoPtr->lSelMax > infoPtr->lRangeMax)
1179 infoPtr->lSelMax = infoPtr->lRangeMax;
1181 if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1183 return 0;
1187 static LRESULT inline
1188 TRACKBAR_SetSelEnd (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lEnd)
1190 if (!GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_ENABLESELRANGE)
1191 return 0;
1193 infoPtr->lSelMax = lEnd;
1194 infoPtr->flags |= TB_SELECTIONCHANGED;
1196 if (infoPtr->lSelMax > infoPtr->lRangeMax)
1197 infoPtr->lSelMax = infoPtr->lRangeMax;
1199 if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1201 return 0;
1205 static LRESULT inline
1206 TRACKBAR_SetSelStart (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lStart)
1208 if (!GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_ENABLESELRANGE)
1209 return 0;
1211 infoPtr->lSelMin = lStart;
1212 infoPtr->flags |=TB_SELECTIONCHANGED;
1214 if (infoPtr->lSelMin < infoPtr->lRangeMin)
1215 infoPtr->lSelMin = infoPtr->lRangeMin;
1217 if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1219 return 0;
1223 static LRESULT inline
1224 TRACKBAR_SetThumbLength (TRACKBAR_INFO *infoPtr, UINT iLength)
1226 if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_FIXEDLENGTH) {
1227 infoPtr->uThumbLen = iLength;
1228 infoPtr->flags |= TB_THUMBSIZECHANGED;
1229 InvalidateRect (infoPtr->hwndSelf, &infoPtr->rcThumb, FALSE);
1232 return 0;
1236 static LRESULT inline
1237 TRACKBAR_SetTic (TRACKBAR_INFO *infoPtr, LONG lPos)
1239 if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_AUTOTICKS)
1240 return FALSE;
1242 if ((lPos < infoPtr->lRangeMin) || (lPos> infoPtr->lRangeMax))
1243 return FALSE;
1245 TRACE("lPos=%ld\n", lPos);
1247 infoPtr->uNumTics++;
1248 infoPtr->tics=ReAlloc( infoPtr->tics,
1249 (infoPtr->uNumTics)*sizeof (DWORD));
1250 if (!infoPtr->tics) {
1251 infoPtr->uNumTics = 0;
1252 notify(infoPtr, NM_OUTOFMEMORY);
1253 return FALSE;
1255 infoPtr->tics[infoPtr->uNumTics-1] = lPos;
1257 TRACKBAR_InvalidateAll(infoPtr);
1259 return TRUE;
1263 static LRESULT inline
1264 TRACKBAR_SetTicFreq (TRACKBAR_INFO *infoPtr, WORD wFreq)
1266 if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_AUTOTICKS) {
1267 infoPtr->uTicFreq = wFreq;
1268 TRACKBAR_RecalculateTics (infoPtr);
1269 TRACKBAR_InvalidateAll(infoPtr);
1272 return 0;
1276 static INT inline
1277 TRACKBAR_SetTipSide (TRACKBAR_INFO *infoPtr, INT fLocation)
1279 INT fTemp = infoPtr->fLocation;
1281 infoPtr->fLocation = fLocation;
1283 return fTemp;
1287 static LRESULT inline
1288 TRACKBAR_SetToolTips (TRACKBAR_INFO *infoPtr, HWND hwndTT)
1290 infoPtr->hwndToolTip = hwndTT;
1292 return 0;
1296 static BOOL inline
1297 TRACKBAR_SetUnicodeFormat (TRACKBAR_INFO *infoPtr, BOOL fUnicode)
1299 BOOL bTemp = infoPtr->bUnicode;
1301 infoPtr->bUnicode = fUnicode;
1303 return bTemp;
1307 static LRESULT
1308 TRACKBAR_InitializeThumb (TRACKBAR_INFO *infoPtr)
1310 DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
1311 RECT rect;
1312 int clientWidth, clientMetric;
1314 /* initial thumb length */
1315 clientMetric = (dwStyle & TBS_ENABLESELRANGE) ? 23 : 21;
1316 GetClientRect(infoPtr->hwndSelf,&rect);
1317 if (dwStyle & TBS_VERT) {
1318 clientWidth = rect.right - rect.left;
1319 } else {
1320 clientWidth = rect.bottom - rect.top;
1322 if (clientWidth >= clientMetric)
1323 infoPtr->uThumbLen = clientMetric;
1324 else
1325 infoPtr->uThumbLen = clientWidth > 9 ? clientWidth - 6 : 4;
1327 TRACKBAR_CalcChannel (infoPtr);
1328 TRACKBAR_UpdateThumb (infoPtr);
1329 infoPtr->flags &= ~TB_SELECTIONCHANGED;
1331 return 0;
1335 static LRESULT
1336 TRACKBAR_Create (HWND hwnd, LPCREATESTRUCTW lpcs)
1338 TRACKBAR_INFO *infoPtr;
1339 DWORD dwStyle;
1341 infoPtr = (TRACKBAR_INFO *)Alloc (sizeof(TRACKBAR_INFO));
1342 if (!infoPtr) return -1;
1343 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1345 /* set default values */
1346 infoPtr->hwndSelf = hwnd;
1347 infoPtr->lRangeMin = 0;
1348 infoPtr->lRangeMax = 100;
1349 infoPtr->lLineSize = 1;
1350 infoPtr->lPageSize = 20;
1351 infoPtr->lSelMin = 0;
1352 infoPtr->lSelMax = 0;
1353 infoPtr->lPos = 0;
1354 infoPtr->fLocation = -1;
1355 infoPtr->uNumTics = 0; /* start and end tic are not included in count*/
1356 infoPtr->uTicFreq = 1;
1357 infoPtr->tics = NULL;
1358 infoPtr->hwndNotify= lpcs->hwndParent;
1360 TRACKBAR_InitializeThumb (infoPtr);
1362 dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1364 /* Create tooltip control */
1365 if (dwStyle & TBS_TOOLTIPS) {
1367 infoPtr->hwndToolTip =
1368 CreateWindowExW (0, TOOLTIPS_CLASSW, NULL, 0,
1369 CW_USEDEFAULT, CW_USEDEFAULT,
1370 CW_USEDEFAULT, CW_USEDEFAULT,
1371 hwnd, 0, 0, 0);
1373 if (infoPtr->hwndToolTip) {
1374 TTTOOLINFOW ti;
1375 ZeroMemory (&ti, sizeof(ti));
1376 ti.cbSize = sizeof(ti);
1377 ti.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
1378 ti.hwnd = hwnd;
1380 SendMessageW (infoPtr->hwndToolTip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
1384 return 0;
1388 static LRESULT
1389 TRACKBAR_Destroy (TRACKBAR_INFO *infoPtr)
1391 /* delete tooltip control */
1392 if (infoPtr->hwndToolTip)
1393 DestroyWindow (infoPtr->hwndToolTip);
1395 Free (infoPtr);
1396 SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1397 return 0;
1401 static LRESULT
1402 TRACKBAR_KillFocus (TRACKBAR_INFO *infoPtr, HWND hwndGetFocus)
1404 TRACE("\n");
1405 infoPtr->bFocussed = FALSE;
1406 TRACKBAR_InvalidateAll(infoPtr);
1408 return 0;
1411 static LRESULT
1412 TRACKBAR_LButtonDown (TRACKBAR_INFO *infoPtr, DWORD fwKeys, INT x, INT y)
1414 POINT clickPoint;
1416 clickPoint.x = x;
1417 clickPoint.y = y;
1419 SetFocus(infoPtr->hwndSelf);
1421 if (PtInRect(&infoPtr->rcThumb, clickPoint)) {
1422 infoPtr->flags |= TB_DRAG_MODE;
1423 SetCapture (infoPtr->hwndSelf);
1424 TRACKBAR_UpdateToolTip (infoPtr);
1425 TRACKBAR_ActivateToolTip (infoPtr, TRUE);
1426 TRACKBAR_InvalidateThumb(infoPtr, infoPtr->lPos);
1427 } else {
1428 LONG dir = TRACKBAR_GetAutoPageDirection(infoPtr, clickPoint);
1429 if (dir == 0) return 0;
1430 infoPtr->flags |= (dir < 0) ? TB_AUTO_PAGE_LEFT : TB_AUTO_PAGE_RIGHT;
1431 TRACKBAR_AutoPage (infoPtr, clickPoint);
1432 SetCapture (infoPtr->hwndSelf);
1433 SetTimer(infoPtr->hwndSelf, TB_REFRESH_TIMER, TB_REFRESH_DELAY, 0);
1436 return 0;
1440 static LRESULT
1441 TRACKBAR_LButtonUp (TRACKBAR_INFO *infoPtr, DWORD fwKeys, INT x, INT y)
1443 if (infoPtr->flags & TB_DRAG_MODE) {
1444 notify_with_scroll (infoPtr, TB_THUMBPOSITION | (infoPtr->lPos<<16));
1445 notify_with_scroll (infoPtr, TB_ENDTRACK);
1446 infoPtr->flags &= ~TB_DRAG_MODE;
1447 ReleaseCapture ();
1448 notify(infoPtr, NM_RELEASEDCAPTURE);
1449 TRACKBAR_ActivateToolTip(infoPtr, FALSE);
1450 TRACKBAR_InvalidateThumb(infoPtr, infoPtr->lPos);
1452 if (infoPtr->flags & TB_AUTO_PAGE) {
1453 KillTimer (infoPtr->hwndSelf, TB_REFRESH_TIMER);
1454 infoPtr->flags &= ~TB_AUTO_PAGE;
1455 notify_with_scroll (infoPtr, TB_ENDTRACK);
1456 ReleaseCapture ();
1457 notify(infoPtr, NM_RELEASEDCAPTURE);
1460 return 0;
1464 static LRESULT
1465 TRACKBAR_CaptureChanged (TRACKBAR_INFO *infoPtr)
1467 notify_with_scroll (infoPtr, TB_ENDTRACK);
1468 return 0;
1472 static LRESULT
1473 TRACKBAR_Paint (TRACKBAR_INFO *infoPtr, HDC hdc)
1475 if (hdc) {
1476 TRACKBAR_Refresh(infoPtr, hdc);
1477 } else {
1478 PAINTSTRUCT ps;
1479 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1480 TRACKBAR_Refresh (infoPtr, hdc);
1481 EndPaint (infoPtr->hwndSelf, &ps);
1484 return 0;
1488 static LRESULT
1489 TRACKBAR_SetFocus (TRACKBAR_INFO *infoPtr, HWND hwndLoseFocus)
1491 TRACE("\n");
1492 infoPtr->bFocussed = TRUE;
1493 TRACKBAR_InvalidateAll(infoPtr);
1495 return 0;
1499 static LRESULT
1500 TRACKBAR_Size (TRACKBAR_INFO *infoPtr, DWORD fwSizeType, INT nWidth, INT nHeight)
1502 TRACKBAR_InitializeThumb (infoPtr);
1503 TRACKBAR_AlignBuddies (infoPtr);
1505 return 0;
1509 static LRESULT
1510 TRACKBAR_Timer (TRACKBAR_INFO *infoPtr, INT wTimerID, TIMERPROC *tmrpc)
1512 if (infoPtr->flags & TB_AUTO_PAGE) {
1513 POINT pt;
1514 if (GetCursorPos(&pt))
1515 if (ScreenToClient(infoPtr->hwndSelf, &pt))
1516 TRACKBAR_AutoPage(infoPtr, pt);
1518 return 0;
1522 static LRESULT
1523 TRACKBAR_MouseMove (TRACKBAR_INFO *infoPtr, DWORD fwKeys, INT x, INT y)
1525 DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
1526 INT clickPlace = (dwStyle & TBS_VERT) ? y : x;
1527 LONG dragPos, oldPos = infoPtr->lPos;
1529 TRACE("(x=%d. y=%d)\n", x, y);
1531 if (infoPtr->flags & TB_AUTO_PAGE) {
1532 POINT pt;
1533 pt.x = x;
1534 pt.y = y;
1535 TRACKBAR_AutoPage (infoPtr, pt);
1536 return TRUE;
1539 if (!(infoPtr->flags & TB_DRAG_MODE)) return TRUE;
1541 dragPos = TRACKBAR_ConvertPlaceToPosition (infoPtr, clickPlace,
1542 dwStyle & TBS_VERT);
1543 if (dragPos == oldPos) return TRUE;
1545 infoPtr->lPos = dragPos;
1547 infoPtr->flags |= TB_THUMBPOSCHANGED;
1548 notify_with_scroll (infoPtr, TB_THUMBTRACK | (infoPtr->lPos<<16));
1551 TRACKBAR_InvalidateThumbMove(infoPtr, oldPos, dragPos);
1552 UpdateWindow (infoPtr->hwndSelf);
1554 return TRUE;
1557 static BOOL
1558 TRACKBAR_KeyDown (TRACKBAR_INFO *infoPtr, INT nVirtKey, DWORD lKeyData)
1560 DWORD style = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
1561 BOOL downIsLeft = style & TBS_DOWNISLEFT;
1562 BOOL vert = style & TBS_VERT;
1563 LONG pos = infoPtr->lPos;
1565 TRACE("%x\n", nVirtKey);
1567 switch (nVirtKey) {
1568 case VK_UP:
1569 if (!vert && downIsLeft) TRACKBAR_LineDown(infoPtr);
1570 else TRACKBAR_LineUp(infoPtr);
1571 break;
1572 case VK_LEFT:
1573 if (vert && downIsLeft) TRACKBAR_LineDown(infoPtr);
1574 else TRACKBAR_LineUp(infoPtr);
1575 break;
1576 case VK_DOWN:
1577 if (!vert && downIsLeft) TRACKBAR_LineUp(infoPtr);
1578 else TRACKBAR_LineDown(infoPtr);
1579 break;
1580 case VK_RIGHT:
1581 if (vert && downIsLeft) TRACKBAR_LineUp(infoPtr);
1582 else TRACKBAR_LineDown(infoPtr);
1583 break;
1584 case VK_NEXT:
1585 if (!vert && downIsLeft) TRACKBAR_PageUp(infoPtr);
1586 else TRACKBAR_PageDown(infoPtr);
1587 break;
1588 case VK_PRIOR:
1589 if (!vert && downIsLeft) TRACKBAR_PageDown(infoPtr);
1590 else TRACKBAR_PageUp(infoPtr);
1591 break;
1592 case VK_HOME:
1593 if (infoPtr->lPos == infoPtr->lRangeMin) return FALSE;
1594 infoPtr->lPos = infoPtr->lRangeMin;
1595 notify_with_scroll (infoPtr, TB_TOP);
1596 break;
1597 case VK_END:
1598 if (infoPtr->lPos == infoPtr->lRangeMax) return FALSE;
1599 infoPtr->lPos = infoPtr->lRangeMax;
1600 notify_with_scroll (infoPtr, TB_BOTTOM);
1601 break;
1604 if (pos != infoPtr->lPos) {
1605 infoPtr->flags |=TB_THUMBPOSCHANGED;
1606 TRACKBAR_InvalidateThumbMove (infoPtr, pos, infoPtr->lPos);
1609 return TRUE;
1613 static BOOL inline
1614 TRACKBAR_KeyUp (TRACKBAR_INFO *infoPtr, INT nVirtKey, DWORD lKeyData)
1616 switch (nVirtKey) {
1617 case VK_LEFT:
1618 case VK_UP:
1619 case VK_RIGHT:
1620 case VK_DOWN:
1621 case VK_NEXT:
1622 case VK_PRIOR:
1623 case VK_HOME:
1624 case VK_END:
1625 notify_with_scroll (infoPtr, TB_ENDTRACK);
1627 return TRUE;
1631 static LRESULT WINAPI
1632 TRACKBAR_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1634 TRACKBAR_INFO *infoPtr = (TRACKBAR_INFO *)GetWindowLongPtrW (hwnd, 0);
1636 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1638 if (!infoPtr && (uMsg != WM_CREATE))
1639 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1641 switch (uMsg)
1643 case TBM_CLEARSEL:
1644 return TRACKBAR_ClearSel (infoPtr, (BOOL)wParam);
1646 case TBM_CLEARTICS:
1647 return TRACKBAR_ClearTics (infoPtr, (BOOL)wParam);
1649 case TBM_GETBUDDY:
1650 return (LRESULT)(wParam ? infoPtr->hwndBuddyLA : infoPtr->hwndBuddyRB);
1652 case TBM_GETCHANNELRECT:
1653 return TRACKBAR_GetChannelRect (infoPtr, (LPRECT)lParam);
1655 case TBM_GETLINESIZE:
1656 return infoPtr->lLineSize;
1658 case TBM_GETNUMTICS:
1659 return TRACKBAR_GetNumTics (infoPtr);
1661 case TBM_GETPAGESIZE:
1662 return infoPtr->lPageSize;
1664 case TBM_GETPOS:
1665 return infoPtr->lPos;
1667 case TBM_GETPTICS:
1668 return (LRESULT)infoPtr->tics;
1670 case TBM_GETRANGEMAX:
1671 return infoPtr->lRangeMax;
1673 case TBM_GETRANGEMIN:
1674 return infoPtr->lRangeMin;
1676 case TBM_GETSELEND:
1677 return infoPtr->lSelMax;
1679 case TBM_GETSELSTART:
1680 return infoPtr->lSelMin;
1682 case TBM_GETTHUMBLENGTH:
1683 return infoPtr->uThumbLen;
1685 case TBM_GETTHUMBRECT:
1686 return CopyRect((LPRECT)lParam, &infoPtr->rcThumb);
1688 case TBM_GETTIC:
1689 return TRACKBAR_GetTic (infoPtr, (INT)wParam);
1691 case TBM_GETTICPOS:
1692 return TRACKBAR_GetTicPos (infoPtr, (INT)wParam);
1694 case TBM_GETTOOLTIPS:
1695 return (LRESULT)infoPtr->hwndToolTip;
1697 case TBM_GETUNICODEFORMAT:
1698 return infoPtr->bUnicode;
1700 case TBM_SETBUDDY:
1701 return (LRESULT) TRACKBAR_SetBuddy(infoPtr, (BOOL)wParam, (HWND)lParam);
1703 case TBM_SETLINESIZE:
1704 return TRACKBAR_SetLineSize (infoPtr, (LONG)lParam);
1706 case TBM_SETPAGESIZE:
1707 return TRACKBAR_SetPageSize (infoPtr, (LONG)lParam);
1709 case TBM_SETPOS:
1710 return TRACKBAR_SetPos (infoPtr, (BOOL)wParam, (LONG)lParam);
1712 case TBM_SETRANGE:
1713 return TRACKBAR_SetRange (infoPtr, (BOOL)wParam, (LONG)lParam);
1715 case TBM_SETRANGEMAX:
1716 return TRACKBAR_SetRangeMax (infoPtr, (BOOL)wParam, (LONG)lParam);
1718 case TBM_SETRANGEMIN:
1719 return TRACKBAR_SetRangeMin (infoPtr, (BOOL)wParam, (LONG)lParam);
1721 case TBM_SETSEL:
1722 return TRACKBAR_SetSel (infoPtr, (BOOL)wParam, (LONG)lParam);
1724 case TBM_SETSELEND:
1725 return TRACKBAR_SetSelEnd (infoPtr, (BOOL)wParam, (LONG)lParam);
1727 case TBM_SETSELSTART:
1728 return TRACKBAR_SetSelStart (infoPtr, (BOOL)wParam, (LONG)lParam);
1730 case TBM_SETTHUMBLENGTH:
1731 return TRACKBAR_SetThumbLength (infoPtr, (UINT)wParam);
1733 case TBM_SETTIC:
1734 return TRACKBAR_SetTic (infoPtr, (LONG)lParam);
1736 case TBM_SETTICFREQ:
1737 return TRACKBAR_SetTicFreq (infoPtr, (WORD)wParam);
1739 case TBM_SETTIPSIDE:
1740 return TRACKBAR_SetTipSide (infoPtr, (INT)wParam);
1742 case TBM_SETTOOLTIPS:
1743 return TRACKBAR_SetToolTips (infoPtr, (HWND)wParam);
1745 case TBM_SETUNICODEFORMAT:
1746 return TRACKBAR_SetUnicodeFormat (infoPtr, (BOOL)wParam);
1749 case WM_CAPTURECHANGED:
1750 return TRACKBAR_CaptureChanged (infoPtr);
1752 case WM_CREATE:
1753 return TRACKBAR_Create (hwnd, (LPCREATESTRUCTW)lParam);
1755 case WM_DESTROY:
1756 return TRACKBAR_Destroy (infoPtr);
1758 /* case WM_ENABLE: */
1760 case WM_ERASEBKGND:
1761 return 0;
1763 case WM_GETDLGCODE:
1764 return DLGC_WANTARROWS;
1766 case WM_KEYDOWN:
1767 return TRACKBAR_KeyDown (infoPtr, (INT)wParam, (DWORD)lParam);
1769 case WM_KEYUP:
1770 return TRACKBAR_KeyUp (infoPtr, (INT)wParam, (DWORD)lParam);
1772 case WM_KILLFOCUS:
1773 return TRACKBAR_KillFocus (infoPtr, (HWND)wParam);
1775 case WM_LBUTTONDOWN:
1776 return TRACKBAR_LButtonDown (infoPtr, wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1778 case WM_LBUTTONUP:
1779 return TRACKBAR_LButtonUp (infoPtr, wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1781 case WM_MOUSEMOVE:
1782 return TRACKBAR_MouseMove (infoPtr, wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1784 case WM_PAINT:
1785 return TRACKBAR_Paint (infoPtr, (HDC)wParam);
1787 case WM_SETFOCUS:
1788 return TRACKBAR_SetFocus (infoPtr, (HWND)wParam);
1790 case WM_SIZE:
1791 return TRACKBAR_Size (infoPtr, wParam, LOWORD(lParam), HIWORD(lParam));
1793 case WM_TIMER:
1794 return TRACKBAR_Timer (infoPtr, (INT)wParam, (TIMERPROC *)lParam);
1796 case WM_WININICHANGE:
1797 return TRACKBAR_InitializeThumb (infoPtr);
1799 default:
1800 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
1801 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
1802 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1807 void TRACKBAR_Register (void)
1809 WNDCLASSW wndClass;
1811 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1812 wndClass.style = CS_GLOBALCLASS;
1813 wndClass.lpfnWndProc = TRACKBAR_WindowProc;
1814 wndClass.cbClsExtra = 0;
1815 wndClass.cbWndExtra = sizeof(TRACKBAR_INFO *);
1816 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1817 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1818 wndClass.lpszClassName = TRACKBAR_CLASSW;
1820 RegisterClassW (&wndClass);
1824 void TRACKBAR_Unregister (void)
1826 UnregisterClassW (TRACKBAR_CLASSW, NULL);