comctl32/progress: Implement PBM_GETBKCOLOR.
[wine] / dlls / comctl32 / trackbar.c
1 /*
2  * Trackbar control
3  *
4  * Copyright 1998, 1999 Eric Kohl
5  * Copyright 1998, 1999 Alex Priem
6  * Copyright 2002 Dimitrie O. Paun
7  *
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.
12  *
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.
17  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  * NOTE
23  * 
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.
26  * 
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.
30  * 
31  */
32
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
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 "uxtheme.h"
45 #include "tmschema.h"
46 #include "wine/debug.h"
47
48 #include "comctl32.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(trackbar);
51
52 typedef struct
53 {
54     HWND hwndSelf;
55     LONG lRangeMin;
56     LONG lRangeMax;
57     LONG lLineSize;
58     LONG lPageSize;
59     LONG lSelMin;
60     LONG lSelMax;
61     LONG lPos;
62     UINT uThumbLen;
63     UINT uNumTics;
64     UINT uTicFreq;
65     HWND hwndNotify;
66     HWND hwndToolTip;
67     HWND hwndBuddyLA;
68     HWND hwndBuddyRB;
69     INT  fLocation;
70     INT  flags;
71     BOOL bUnicode;
72     BOOL bFocussed;
73     RECT rcChannel;
74     RECT rcSelection;
75     RECT rcThumb;
76     LPLONG tics;
77 } TRACKBAR_INFO;
78
79 #define TB_REFRESH_TIMER        1
80 #define TB_REFRESH_DELAY        500
81
82 #define TOOLTIP_OFFSET          2     /* distance from ctrl edge to tooltip */
83
84 #define TB_DEFAULTPAGESIZE      20
85
86 /* Used by TRACKBAR_Refresh to find out which parts of the control
87    need to be recalculated */
88
89 #define TB_THUMBPOSCHANGED      1
90 #define TB_THUMBSIZECHANGED     2
91 #define TB_THUMBCHANGED         (TB_THUMBPOSCHANGED | TB_THUMBSIZECHANGED)
92 #define TB_SELECTIONCHANGED     4
93 #define TB_DRAG_MODE            8     /* we're dragging the slider */
94 #define TB_AUTO_PAGE_LEFT       16
95 #define TB_AUTO_PAGE_RIGHT      32
96 #define TB_AUTO_PAGE            (TB_AUTO_PAGE_LEFT | TB_AUTO_PAGE_RIGHT)
97 #define TB_THUMB_HOT            64    /* mouse hovers above thumb */
98
99 /* helper defines for TRACKBAR_DrawTic */
100 #define TIC_EDGE                0x20
101 #define TIC_SELECTIONMARKMAX    0x80
102 #define TIC_SELECTIONMARKMIN    0x100
103 #define TIC_SELECTIONMARK       (TIC_SELECTIONMARKMAX | TIC_SELECTIONMARKMIN)
104
105 static const WCHAR themeClass[] = { 'T','r','a','c','k','b','a','r',0 };
106
107 static inline int 
108 notify_customdraw (const TRACKBAR_INFO *infoPtr, NMCUSTOMDRAW *pnmcd, int stage)
109 {
110     pnmcd->dwDrawStage = stage;
111     return SendMessageW (infoPtr->hwndNotify, WM_NOTIFY, 
112                          pnmcd->hdr.idFrom, (LPARAM)pnmcd);
113 }
114
115 static LRESULT notify_hdr (const TRACKBAR_INFO *infoPtr, INT code, LPNMHDR pnmh)
116 {
117     LRESULT result;
118     
119     TRACE("(code=%d)\n", code);
120
121     pnmh->hwndFrom = infoPtr->hwndSelf;
122     pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
123     pnmh->code = code;
124     result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
125
126     TRACE("  <= %ld\n", result);
127
128     return result;
129 }
130
131 static inline int notify (const TRACKBAR_INFO *infoPtr, INT code)
132 {
133     NMHDR nmh;
134     return notify_hdr(infoPtr, code, &nmh);
135 }
136
137 static BOOL
138 notify_with_scroll (const TRACKBAR_INFO *infoPtr, UINT code)
139 {
140     BOOL bVert = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_VERT;
141
142     TRACE("%x\n", code);
143
144     return (BOOL) SendMessageW (infoPtr->hwndNotify,
145                                 bVert ? WM_VSCROLL : WM_HSCROLL,
146                                 (WPARAM)code, (LPARAM)infoPtr->hwndSelf);
147 }
148     
149 static void TRACKBAR_RecalculateTics (TRACKBAR_INFO *infoPtr)
150 {
151     int tic;
152     unsigned nrTics, i;
153
154     if (infoPtr->uTicFreq && infoPtr->lRangeMax >= infoPtr->lRangeMin)
155         nrTics=(infoPtr->lRangeMax - infoPtr->lRangeMin)/infoPtr->uTicFreq;
156     else {
157         Free (infoPtr->tics);
158         infoPtr->tics = NULL;
159         infoPtr->uNumTics = 0;
160         return;
161     }
162
163     if (nrTics != infoPtr->uNumTics) {
164         infoPtr->tics=ReAlloc (infoPtr->tics,
165                                         (nrTics+1)*sizeof (DWORD));
166         if (!infoPtr->tics) {
167             infoPtr->uNumTics = 0;
168             notify(infoPtr, NM_OUTOFMEMORY);
169             return;
170         }
171         infoPtr->uNumTics = nrTics;
172     }
173
174     tic = infoPtr->lRangeMin + infoPtr->uTicFreq;
175     for (i = 0; i < nrTics; i++, tic += infoPtr->uTicFreq)
176         infoPtr->tics[i] = tic;
177 }
178
179 /* converts from physical (mouse) position to logical position
180    (in range of trackbar) */
181
182 static inline LONG
183 TRACKBAR_ConvertPlaceToPosition (const TRACKBAR_INFO *infoPtr, int place, int vertical)
184 {
185     double range, width, pos, offsetthumb;
186
187     range = infoPtr->lRangeMax - infoPtr->lRangeMin;
188     if (vertical) {
189         offsetthumb = (infoPtr->rcThumb.bottom - infoPtr->rcThumb.top)/2;
190         width = infoPtr->rcChannel.bottom - infoPtr->rcChannel.top - (offsetthumb * 2) - 1;
191         pos = (range*(place - infoPtr->rcChannel.top - offsetthumb)) / width;
192     } else {
193         offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
194         width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - (offsetthumb * 2) - 1;
195         pos = (range*(place - infoPtr->rcChannel.left - offsetthumb)) / width;
196     }
197     pos += infoPtr->lRangeMin;
198     if (pos > infoPtr->lRangeMax)
199         pos = infoPtr->lRangeMax;
200     else if (pos < infoPtr->lRangeMin)
201         pos = infoPtr->lRangeMin;
202
203     TRACE("%.2f\n", pos);
204     return (LONG)(pos + 0.5);
205 }
206
207
208 /* return: 0> prev, 0 none, >0 next */
209 static LONG
210 TRACKBAR_GetAutoPageDirection (const TRACKBAR_INFO *infoPtr, POINT clickPoint)
211 {
212     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
213     RECT pageRect;
214
215     if (dwStyle & TBS_VERT) {
216         pageRect.top = infoPtr->rcChannel.top;
217         pageRect.bottom = infoPtr->rcChannel.bottom;
218         pageRect.left = infoPtr->rcThumb.left;
219         pageRect.right = infoPtr->rcThumb.right;
220     } else {
221         pageRect.top = infoPtr->rcThumb.top;
222         pageRect.bottom = infoPtr->rcThumb.bottom;
223         pageRect.left = infoPtr->rcChannel.left;
224         pageRect.right = infoPtr->rcChannel.right;
225     }
226
227
228     if (PtInRect(&pageRect, clickPoint))
229     {
230         int clickPlace = (dwStyle & TBS_VERT) ? clickPoint.y : clickPoint.x;
231
232         LONG clickPos = TRACKBAR_ConvertPlaceToPosition(infoPtr, clickPlace,
233                                                         dwStyle & TBS_VERT);
234         return clickPos - infoPtr->lPos;
235     }
236
237     return 0;
238 }
239
240 static inline void
241 TRACKBAR_PageDown (TRACKBAR_INFO *infoPtr)
242 {
243     if (infoPtr->lPos == infoPtr->lRangeMax) return;
244
245     infoPtr->lPos += infoPtr->lPageSize;
246     if (infoPtr->lPos > infoPtr->lRangeMax)
247         infoPtr->lPos = infoPtr->lRangeMax;
248     notify_with_scroll (infoPtr, TB_PAGEDOWN);
249 }
250
251
252 static inline void
253 TRACKBAR_PageUp (TRACKBAR_INFO *infoPtr)
254 {
255     if (infoPtr->lPos == infoPtr->lRangeMin) return;
256
257     infoPtr->lPos -= infoPtr->lPageSize;
258     if (infoPtr->lPos < infoPtr->lRangeMin)
259         infoPtr->lPos = infoPtr->lRangeMin;
260     notify_with_scroll (infoPtr, TB_PAGEUP);
261 }
262
263 static inline void TRACKBAR_LineUp(TRACKBAR_INFO *infoPtr)
264 {
265     if (infoPtr->lPos == infoPtr->lRangeMin) return;
266     infoPtr->lPos -= infoPtr->lLineSize;
267     if (infoPtr->lPos < infoPtr->lRangeMin)
268         infoPtr->lPos = infoPtr->lRangeMin;
269     notify_with_scroll (infoPtr, TB_LINEUP);
270 }
271
272 static inline void TRACKBAR_LineDown(TRACKBAR_INFO *infoPtr)
273 {
274     if (infoPtr->lPos == infoPtr->lRangeMax) return;
275     infoPtr->lPos += infoPtr->lLineSize;
276     if (infoPtr->lPos > infoPtr->lRangeMax)
277         infoPtr->lPos = infoPtr->lRangeMax;
278     notify_with_scroll (infoPtr, TB_LINEDOWN);
279 }
280
281 static void
282 TRACKBAR_CalcChannel (TRACKBAR_INFO *infoPtr)
283 {
284     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
285     INT cyChannel, offsetthumb, offsetedge;
286     RECT lpRect, *channel = & infoPtr->rcChannel;
287
288     GetClientRect (infoPtr->hwndSelf, &lpRect);
289
290     offsetthumb = infoPtr->uThumbLen / 4;
291     offsetedge  = offsetthumb + 3;
292     cyChannel   = (dwStyle & TBS_ENABLESELRANGE) ? offsetthumb*3 : 4;
293     if (dwStyle & TBS_VERT) {
294         channel->top    = lpRect.top + offsetedge;
295         channel->bottom = lpRect.bottom - offsetedge;
296         if (dwStyle & TBS_ENABLESELRANGE)
297             channel->left = lpRect.left + ((infoPtr->uThumbLen - cyChannel + 2) / 2);
298         else
299             channel->left = lpRect.left + (infoPtr->uThumbLen / 2) - 1;
300         if (dwStyle & TBS_BOTH) {
301             if (dwStyle & TBS_NOTICKS)
302                 channel->left += 1;
303             else
304                 channel->left += 9;
305         }
306         else if (dwStyle & TBS_TOP) {
307             if (dwStyle & TBS_NOTICKS)
308                 channel->left += 2;
309             else
310                 channel->left += 10;
311         }
312         channel->right = channel->left + cyChannel;
313     } else {
314         channel->left = lpRect.left + offsetedge;
315         channel->right = lpRect.right - offsetedge;
316         if (dwStyle & TBS_ENABLESELRANGE)
317             channel->top = lpRect.top + ((infoPtr->uThumbLen - cyChannel + 2) / 2);
318         else
319             channel->top = lpRect.top + (infoPtr->uThumbLen / 2) - 1;
320         if (dwStyle & TBS_BOTH) {
321             if (dwStyle & TBS_NOTICKS)
322                 channel->top += 1;
323             else
324                 channel->top += 9;
325         }
326         else if (dwStyle & TBS_TOP) {
327             if (dwStyle & TBS_NOTICKS)
328                 channel->top += 2;
329             else
330                 channel->top += 10;
331         }
332         channel->bottom   = channel->top + cyChannel;
333     }
334 }
335
336 static void
337 TRACKBAR_CalcThumb (const TRACKBAR_INFO *infoPtr, LONG lPos, RECT *thumb)
338 {
339     int range, width, height, thumbwidth;
340     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
341     RECT lpRect;
342
343     range = infoPtr->lRangeMax - infoPtr->lRangeMin;
344     thumbwidth = (infoPtr->uThumbLen / 2) | 1;
345
346     if (!range) range = 1;
347
348     GetClientRect(infoPtr->hwndSelf, &lpRect);
349     if (dwStyle & TBS_VERT)
350     {
351         height = infoPtr->rcChannel.bottom - infoPtr->rcChannel.top - thumbwidth;
352
353         if ((dwStyle & (TBS_BOTH | TBS_LEFT)) && !(dwStyle & TBS_NOTICKS))
354             thumb->left = 10;
355         else
356             thumb->left = 2;
357         thumb->right = thumb->left + infoPtr->uThumbLen;
358         thumb->top = infoPtr->rcChannel.top +
359                      (height*(lPos - infoPtr->lRangeMin))/range;
360         thumb->bottom = thumb->top + thumbwidth;
361     }
362     else
363     {
364         width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - thumbwidth;
365
366         thumb->left = infoPtr->rcChannel.left +
367                       (width*(lPos - infoPtr->lRangeMin))/range;
368         thumb->right = thumb->left + thumbwidth;
369         if ((dwStyle & (TBS_BOTH | TBS_TOP)) && !(dwStyle & TBS_NOTICKS))
370             thumb->top = 10;
371         else
372             thumb->top = 2;
373         thumb->bottom = thumb->top + infoPtr->uThumbLen;
374     }
375 }
376
377 static inline void
378 TRACKBAR_UpdateThumb (TRACKBAR_INFO *infoPtr)
379 {
380     TRACKBAR_CalcThumb(infoPtr, infoPtr->lPos, &infoPtr->rcThumb);
381 }
382
383 static inline void
384 TRACKBAR_InvalidateAll (const TRACKBAR_INFO *infoPtr)
385 {
386     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
387 }
388
389 static void
390 TRACKBAR_InvalidateThumb (const TRACKBAR_INFO *infoPtr, LONG thumbPos)
391 {
392     RECT rcThumb;
393
394     TRACKBAR_CalcThumb(infoPtr, thumbPos, &rcThumb);
395     InflateRect(&rcThumb, 1, 1);
396     InvalidateRect(infoPtr->hwndSelf, &rcThumb, FALSE);
397 }
398
399 static inline void
400 TRACKBAR_InvalidateThumbMove (const TRACKBAR_INFO *infoPtr, LONG oldPos, LONG newPos)
401 {
402     TRACKBAR_InvalidateThumb (infoPtr, oldPos);
403     if (newPos != oldPos)
404         TRACKBAR_InvalidateThumb (infoPtr, newPos);
405 }
406
407 static inline BOOL
408 TRACKBAR_HasSelection (const TRACKBAR_INFO *infoPtr)
409 {
410     return infoPtr->lSelMin != infoPtr->lSelMax;
411 }
412
413 static void
414 TRACKBAR_CalcSelection (TRACKBAR_INFO *infoPtr)
415 {
416     RECT *selection = &infoPtr->rcSelection;
417     int range = infoPtr->lRangeMax - infoPtr->lRangeMin;
418     int offsetthumb, height, width;
419
420     if (range <= 0) {
421         SetRectEmpty (selection);
422     } else {
423         if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_VERT) {
424             offsetthumb = (infoPtr->rcThumb.bottom - infoPtr->rcThumb.top)/2;
425             height = infoPtr->rcChannel.bottom - infoPtr->rcChannel.top - offsetthumb*2;
426             selection->top    = infoPtr->rcChannel.top + offsetthumb +
427                 (height*infoPtr->lSelMin)/range;
428             selection->bottom = infoPtr->rcChannel.top + offsetthumb +
429                 (height*infoPtr->lSelMax)/range;
430             selection->left   = infoPtr->rcChannel.left + 3;
431             selection->right  = infoPtr->rcChannel.right - 3;
432         } else {
433             offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
434             width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - offsetthumb*2;
435             selection->left   = infoPtr->rcChannel.left + offsetthumb +
436                 (width*infoPtr->lSelMin)/range;
437             selection->right  = infoPtr->rcChannel.left + offsetthumb +
438                 (width*infoPtr->lSelMax)/range;
439             selection->top    = infoPtr->rcChannel.top + 3;
440             selection->bottom = infoPtr->rcChannel.bottom - 3;
441         }
442     }
443
444     TRACE("selection[%s]\n", wine_dbgstr_rect(selection));
445 }
446
447 static BOOL
448 TRACKBAR_AutoPage (TRACKBAR_INFO *infoPtr, POINT clickPoint)
449 {
450     LONG dir = TRACKBAR_GetAutoPageDirection(infoPtr, clickPoint);
451     LONG prevPos = infoPtr->lPos;
452
453     TRACE("x=%d, y=%d, dir=%d\n", clickPoint.x, clickPoint.y, dir);
454
455     if (dir > 0 && (infoPtr->flags & TB_AUTO_PAGE_RIGHT))
456         TRACKBAR_PageDown(infoPtr);
457     else if (dir < 0 && (infoPtr->flags & TB_AUTO_PAGE_LEFT))
458         TRACKBAR_PageUp(infoPtr);
459     else return FALSE;
460
461     infoPtr->flags |= TB_THUMBPOSCHANGED;
462     TRACKBAR_InvalidateThumbMove (infoPtr, prevPos, infoPtr->lPos);
463
464     return TRUE;
465 }
466
467 /* Trackbar drawing code. I like my spaghetti done milanese.  */
468
469 static void
470 TRACKBAR_DrawChannel (const TRACKBAR_INFO *infoPtr, HDC hdc, DWORD dwStyle)
471 {
472     RECT rcChannel = infoPtr->rcChannel;
473     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
474
475     if (theme)
476     {
477         DrawThemeBackground (theme, hdc, 
478             (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_VERT) ? 
479                 TKP_TRACKVERT : TKP_TRACK, TKS_NORMAL, &rcChannel, 0);
480     }
481     else
482     {
483         DrawEdge (hdc, &rcChannel, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
484         if (dwStyle & TBS_ENABLESELRANGE) {              /* fill the channel */
485             FillRect (hdc, &rcChannel, GetStockObject(WHITE_BRUSH));
486             if (TRACKBAR_HasSelection(infoPtr))
487                 FillRect (hdc, &infoPtr->rcSelection, GetSysColorBrush(COLOR_HIGHLIGHT));
488         }
489     }
490 }
491
492 static void
493 TRACKBAR_DrawOneTic (const TRACKBAR_INFO *infoPtr, HDC hdc, LONG ticPos, int flags)
494 {
495     int x, y, ox, oy, range, side, indent = 0, len = 3;
496     int offsetthumb;
497     RECT rcTics;
498
499     if (flags & TBS_VERT) {
500         offsetthumb = (infoPtr->rcThumb.bottom - infoPtr->rcThumb.top)/2;
501         rcTics.left = infoPtr->rcThumb.left - 2;
502         rcTics.right = infoPtr->rcThumb.right + 2;
503         rcTics.top    = infoPtr->rcChannel.top + offsetthumb + 1;
504         rcTics.bottom = infoPtr->rcChannel.bottom - offsetthumb;
505     } else {
506         offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
507         rcTics.left   = infoPtr->rcChannel.left + offsetthumb + 1;
508         rcTics.right  = infoPtr->rcChannel.right - offsetthumb;
509         rcTics.top = infoPtr->rcThumb.top - 2;
510         rcTics.bottom = infoPtr->rcThumb.bottom + 2;
511     }
512
513     if (flags & (TBS_TOP | TBS_LEFT)) {
514         x = rcTics.left;
515         y = rcTics.top;
516         side = -1;
517     } else {
518         x = rcTics.right;
519         y = rcTics.bottom;
520         side = 1;
521     }
522
523     range = infoPtr->lRangeMax - infoPtr->lRangeMin;
524     if (range <= 0)
525       range = 1; /* to avoid division by zero */
526
527     if (flags & TIC_SELECTIONMARK) {
528         indent = (flags & TIC_SELECTIONMARKMIN) ? -1 : 1;
529     } else if (flags & TIC_EDGE) {
530         len++;
531     }
532
533     if (flags & TBS_VERT) {
534         int height = rcTics.bottom - rcTics.top;
535         y = rcTics.top + (height*(ticPos - infoPtr->lRangeMin))/range;
536     } else {
537         int width = rcTics.right - rcTics.left;
538         x = rcTics.left + (width*(ticPos - infoPtr->lRangeMin))/range;
539     }
540
541     ox = x;
542     oy = y;
543     MoveToEx(hdc, x, y, 0);
544     if (flags & TBS_VERT) x += len * side;
545     else y += len * side;
546     LineTo(hdc, x, y);
547             
548     if (flags & TIC_SELECTIONMARK) {
549         if (flags & TBS_VERT) {
550             x -= side;
551         } else {
552             y -= side;
553         }
554         MoveToEx(hdc, x, y, 0);
555         if (flags & TBS_VERT) {
556             y += 2 * indent;
557         } else {
558             x += 2 * indent;
559         }
560         
561         LineTo(hdc, x, y);
562         LineTo(hdc, ox, oy);
563     }
564 }
565
566
567 static inline void
568 TRACKBAR_DrawTic (const TRACKBAR_INFO *infoPtr, HDC hdc, LONG ticPos, int flags)
569 {
570     if ((flags & (TBS_LEFT | TBS_TOP)) || (flags & TBS_BOTH))
571         TRACKBAR_DrawOneTic (infoPtr, hdc, ticPos, flags | TBS_LEFT);
572
573     if (!(flags & (TBS_LEFT | TBS_TOP)) || (flags & TBS_BOTH))
574         TRACKBAR_DrawOneTic (infoPtr, hdc, ticPos, flags & ~TBS_LEFT);
575 }
576
577 static void
578 TRACKBAR_DrawTics (const TRACKBAR_INFO *infoPtr, HDC hdc, DWORD dwStyle)
579 {
580     unsigned int i;
581     int ticFlags = dwStyle & 0x0f;
582     LOGPEN ticPen = { PS_SOLID, {1, 0}, GetSysColor (COLOR_3DDKSHADOW) };
583     HPEN hOldPen, hTicPen;
584     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
585     
586     if (theme)
587     {
588         int part = (dwStyle & TBS_VERT) ? TKP_TICSVERT : TKP_TICS;
589         GetThemeColor (theme, part, TSS_NORMAL, TMT_COLOR, &ticPen.lopnColor);
590     }
591     /* create the pen to draw the tics with */
592     hTicPen = CreatePenIndirect(&ticPen);
593     hOldPen = hTicPen ? SelectObject(hdc, hTicPen) : 0;
594
595     /* actually draw the tics */
596     for (i=0; i<infoPtr->uNumTics; i++)
597         TRACKBAR_DrawTic (infoPtr, hdc, infoPtr->tics[i], ticFlags);
598
599     TRACKBAR_DrawTic (infoPtr, hdc, infoPtr->lRangeMin, ticFlags | TIC_EDGE);
600     TRACKBAR_DrawTic (infoPtr, hdc, infoPtr->lRangeMax, ticFlags | TIC_EDGE);
601
602     if ((dwStyle & TBS_ENABLESELRANGE) && TRACKBAR_HasSelection(infoPtr)) {
603         TRACKBAR_DrawTic (infoPtr, hdc, infoPtr->lSelMin,
604                           ticFlags | TIC_SELECTIONMARKMIN);
605         TRACKBAR_DrawTic (infoPtr, hdc, infoPtr->lSelMax,
606                           ticFlags | TIC_SELECTIONMARKMAX);
607     }
608     
609     /* clean up the pen, if we created one */
610     if (hTicPen) {
611         SelectObject(hdc, hOldPen);
612         DeleteObject(hTicPen);
613     }
614 }
615
616 static void
617 TRACKBAR_DrawThumb (const TRACKBAR_INFO *infoPtr, HDC hdc, DWORD dwStyle)
618 {
619     HBRUSH oldbr;
620     HPEN  oldpen;
621     RECT thumb = infoPtr->rcThumb;
622     int BlackUntil = 3;
623     int PointCount = 6;
624     POINT points[6];
625     int fillClr;
626     int PointDepth;
627     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
628     
629     if (theme)
630     {
631         int partId;
632         int stateId;
633         if (dwStyle & TBS_BOTH)
634             partId = (dwStyle & TBS_VERT) ? TKP_THUMBVERT : TKP_THUMB;
635         else if (dwStyle & TBS_LEFT)
636             partId = (dwStyle & TBS_VERT) ? TKP_THUMBLEFT : TKP_THUMBTOP;
637         else
638             partId = (dwStyle & TBS_VERT) ? TKP_THUMBRIGHT : TKP_THUMBBOTTOM;
639             
640         if (dwStyle & WS_DISABLED)
641             stateId = TUS_DISABLED;
642         else if (infoPtr->flags & TB_DRAG_MODE)
643             stateId = TUS_PRESSED;
644         else if (infoPtr->flags & TB_THUMB_HOT)
645             stateId = TUS_HOT;
646         else
647             stateId = TUS_NORMAL;
648         
649         DrawThemeBackground (theme, hdc, partId, stateId, &thumb, 0);
650         
651         return;
652     }
653
654     fillClr = infoPtr->flags & TB_DRAG_MODE ? COLOR_BTNHILIGHT : COLOR_BTNFACE;
655     oldbr = SelectObject (hdc, GetSysColorBrush(fillClr));
656     SetPolyFillMode (hdc, WINDING);
657
658     if (dwStyle & TBS_BOTH)
659     {
660        points[0].x=thumb.right;
661        points[0].y=thumb.top;
662        points[1].x=thumb.right;
663        points[1].y=thumb.bottom;
664        points[2].x=thumb.left;
665        points[2].y=thumb.bottom;
666        points[3].x=thumb.left;
667        points[3].y=thumb.top;
668        points[4].x=points[0].x;
669        points[4].y=points[0].y;
670        PointCount = 5;
671        BlackUntil = 3;
672     }
673     else
674     {
675         if (dwStyle & TBS_VERT)
676         {
677           PointDepth = (thumb.bottom - thumb.top) / 2;
678           if (dwStyle & TBS_LEFT)
679           {
680             points[0].x=thumb.right;
681             points[0].y=thumb.top;
682             points[1].x=thumb.right;
683             points[1].y=thumb.bottom;
684             points[2].x=thumb.left + PointDepth;
685             points[2].y=thumb.bottom;
686             points[3].x=thumb.left;
687             points[3].y=(thumb.bottom - thumb.top) / 2 + thumb.top + 1;
688             points[4].x=thumb.left + PointDepth;
689             points[4].y=thumb.top;
690             points[5].x=points[0].x;
691             points[5].y=points[0].y;
692             BlackUntil = 4;
693           }
694           else
695           {
696             points[0].x=thumb.right;
697             points[0].y=(thumb.bottom - thumb.top) / 2 + thumb.top + 1;
698             points[1].x=thumb.right - PointDepth;
699             points[1].y=thumb.bottom;
700             points[2].x=thumb.left;
701             points[2].y=thumb.bottom;
702             points[3].x=thumb.left;
703             points[3].y=thumb.top;
704             points[4].x=thumb.right - PointDepth;
705             points[4].y=thumb.top;
706             points[5].x=points[0].x;
707             points[5].y=points[0].y;
708           }
709         }
710         else
711         {
712           PointDepth = (thumb.right - thumb.left) / 2;
713           if (dwStyle & TBS_TOP)
714           {
715             points[0].x=(thumb.right - thumb.left) / 2 + thumb.left + 1;
716             points[0].y=thumb.top;
717             points[1].x=thumb.right;
718             points[1].y=thumb.top + PointDepth;
719             points[2].x=thumb.right;
720             points[2].y=thumb.bottom;
721             points[3].x=thumb.left;
722             points[3].y=thumb.bottom;
723             points[4].x=thumb.left;
724             points[4].y=thumb.top + PointDepth;
725             points[5].x=points[0].x;
726             points[5].y=points[0].y;
727             BlackUntil = 4;
728           }
729           else
730           {
731             points[0].x=thumb.right;
732             points[0].y=thumb.top;
733             points[1].x=thumb.right;
734             points[1].y=thumb.bottom - PointDepth;
735             points[2].x=(thumb.right - thumb.left) / 2 + thumb.left + 1;
736             points[2].y=thumb.bottom;
737             points[3].x=thumb.left;
738             points[3].y=thumb.bottom - PointDepth;
739             points[4].x=thumb.left;
740             points[4].y=thumb.top;
741             points[5].x=points[0].x;
742             points[5].y=points[0].y;
743           }
744         }
745
746     }
747
748     /* Draw the thumb now */
749     Polygon (hdc, points, PointCount);
750     oldpen = SelectObject(hdc, GetStockObject(BLACK_PEN));
751     Polyline(hdc,points, BlackUntil);
752     SelectObject(hdc, GetStockObject(WHITE_PEN));
753     Polyline(hdc, &points[BlackUntil-1], PointCount+1-BlackUntil);
754     SelectObject(hdc, oldpen);
755     SelectObject(hdc, oldbr);
756 }
757
758
759 static inline void
760 TRACKBAR_ActivateToolTip (const TRACKBAR_INFO *infoPtr, BOOL fShow)
761 {
762     TTTOOLINFOW ti;
763
764     if (!infoPtr->hwndToolTip) return;
765
766     ZeroMemory(&ti, sizeof(ti));
767     ti.cbSize = sizeof(ti);
768     ti.hwnd   = infoPtr->hwndSelf;
769
770     SendMessageW (infoPtr->hwndToolTip, TTM_TRACKACTIVATE, fShow, (LPARAM)&ti);
771 }
772
773
774 static void
775 TRACKBAR_UpdateToolTip (const TRACKBAR_INFO *infoPtr)
776 {
777     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
778     WCHAR buf[80];
779     static const WCHAR fmt[] = { '%', 'l', 'd', 0 };
780     TTTOOLINFOW ti;
781     POINT pt;
782     RECT rcClient;
783     LRESULT size;
784
785     if (!infoPtr->hwndToolTip) return;
786
787     ZeroMemory(&ti, sizeof(ti));
788     ti.cbSize = sizeof(ti);
789     ti.hwnd   = infoPtr->hwndSelf;
790     ti.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
791
792     wsprintfW (buf, fmt, infoPtr->lPos);
793     ti.lpszText = buf;
794     SendMessageW (infoPtr->hwndToolTip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
795
796     GetClientRect (infoPtr->hwndSelf, &rcClient);
797     size = SendMessageW (infoPtr->hwndToolTip, TTM_GETBUBBLESIZE, 0, (LPARAM)&ti);
798     if (dwStyle & TBS_VERT) {
799         if (infoPtr->fLocation == TBTS_LEFT)
800             pt.x = 0 - LOWORD(size) - TOOLTIP_OFFSET;
801         else
802             pt.x = rcClient.right + TOOLTIP_OFFSET;
803         pt.y = (infoPtr->rcThumb.top + infoPtr->rcThumb.bottom - HIWORD(size))/2;
804     } else {
805         if (infoPtr->fLocation == TBTS_TOP)
806             pt.y = 0 - HIWORD(size) - TOOLTIP_OFFSET;
807         else
808             pt.y = rcClient.bottom + TOOLTIP_OFFSET;
809         pt.x = (infoPtr->rcThumb.left + infoPtr->rcThumb.right - LOWORD(size))/2;
810     }
811     ClientToScreen(infoPtr->hwndSelf, &pt);
812
813     SendMessageW (infoPtr->hwndToolTip, TTM_TRACKPOSITION,
814                   0, MAKELPARAM(pt.x, pt.y));
815 }
816
817
818 static void
819 TRACKBAR_Refresh (TRACKBAR_INFO *infoPtr, HDC hdcDst)
820 {
821     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
822     RECT rcClient;
823     HDC hdc;
824     HBITMAP hOldBmp = 0, hOffScreenBmp = 0;
825     NMCUSTOMDRAW nmcd;
826     int gcdrf, icdrf;
827
828     if (infoPtr->flags & TB_THUMBCHANGED) {
829         TRACKBAR_UpdateThumb (infoPtr);
830         if (infoPtr->flags & TB_THUMBSIZECHANGED)
831             TRACKBAR_CalcChannel (infoPtr);
832     }
833     if (infoPtr->flags & TB_SELECTIONCHANGED)
834         TRACKBAR_CalcSelection (infoPtr);
835
836     if (infoPtr->flags & TB_DRAG_MODE)
837         TRACKBAR_UpdateToolTip (infoPtr);
838
839     infoPtr->flags &= ~ (TB_THUMBCHANGED | TB_SELECTIONCHANGED);
840
841     GetClientRect (infoPtr->hwndSelf, &rcClient);
842     
843     /* try to render offscreen, if we fail, carrry onscreen */
844     hdc = CreateCompatibleDC(hdcDst);
845     if (hdc) {
846         hOffScreenBmp = CreateCompatibleBitmap(hdcDst, rcClient.right, rcClient.bottom);
847         if (hOffScreenBmp) {
848             hOldBmp = SelectObject(hdc, hOffScreenBmp);
849         } else {
850             DeleteObject(hdc);
851             hdc = hdcDst;
852         }
853     } else {
854         hdc = hdcDst;
855     }
856
857     ZeroMemory(&nmcd, sizeof(nmcd));
858     nmcd.hdr.hwndFrom = infoPtr->hwndSelf;
859     nmcd.hdr.idFrom = GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_ID);
860     nmcd.hdr.code = NM_CUSTOMDRAW;
861     nmcd.hdc = hdc;
862
863     /* start the paint cycle */
864     nmcd.rc = rcClient;
865     gcdrf = notify_customdraw(infoPtr, &nmcd, CDDS_PREPAINT);
866     if (gcdrf & CDRF_SKIPDEFAULT) goto cleanup;
867     
868     /* Erase background */
869     if (gcdrf == CDRF_DODEFAULT ||
870         notify_customdraw(infoPtr, &nmcd, CDDS_PREERASE) != CDRF_SKIPDEFAULT) {
871         if (GetWindowTheme (infoPtr->hwndSelf)) {
872             DrawThemeParentBackground (infoPtr->hwndSelf, hdc, 0);
873         }
874         else
875             FillRect (hdc, &rcClient, GetSysColorBrush(COLOR_BTNFACE));
876         if (gcdrf != CDRF_DODEFAULT)
877             notify_customdraw(infoPtr, &nmcd, CDDS_POSTERASE);
878     }
879     
880     /* draw channel */
881     if (gcdrf & CDRF_NOTIFYITEMDRAW) {
882         nmcd.dwItemSpec = TBCD_CHANNEL;
883         nmcd.uItemState = CDIS_DEFAULT;
884         nmcd.rc = infoPtr->rcChannel;
885         icdrf = notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPREPAINT);
886     } else icdrf = CDRF_DODEFAULT;
887     if ( !(icdrf & CDRF_SKIPDEFAULT) ) {
888         TRACKBAR_DrawChannel (infoPtr, hdc, dwStyle);
889         if (icdrf & CDRF_NOTIFYPOSTPAINT)
890             notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPOSTPAINT);
891     }
892
893
894     /* draw tics */
895     if (!(dwStyle & TBS_NOTICKS)) {
896         if (gcdrf & CDRF_NOTIFYITEMDRAW) {
897             nmcd.dwItemSpec = TBCD_TICS;
898             nmcd.uItemState = CDIS_DEFAULT;
899             nmcd.rc = rcClient;
900             icdrf = notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPREPAINT);
901         } else icdrf = CDRF_DODEFAULT;
902         if ( !(icdrf & CDRF_SKIPDEFAULT) ) {
903             TRACKBAR_DrawTics (infoPtr, hdc, dwStyle);
904             if (icdrf & CDRF_NOTIFYPOSTPAINT)
905                 notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPOSTPAINT);
906         }
907     }
908     
909     /* draw thumb */
910     if (!(dwStyle & TBS_NOTHUMB)) {
911         if (gcdrf & CDRF_NOTIFYITEMDRAW) {
912             nmcd.dwItemSpec = TBCD_THUMB;
913             nmcd.uItemState = infoPtr->flags & TB_DRAG_MODE ? CDIS_HOT : CDIS_DEFAULT;
914             nmcd.rc = infoPtr->rcThumb;
915             icdrf = notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPREPAINT);
916         } else icdrf = CDRF_DODEFAULT;
917         if ( !(icdrf & CDRF_SKIPDEFAULT) ) {
918             TRACKBAR_DrawThumb(infoPtr, hdc, dwStyle);
919             if (icdrf & CDRF_NOTIFYPOSTPAINT)
920                 notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPOSTPAINT);
921         }
922     }
923
924     /* draw focus rectangle */
925     if (infoPtr->bFocussed) {
926         DrawFocusRect(hdc, &rcClient);
927     }
928
929     /* finish up the painting */
930     if (gcdrf & CDRF_NOTIFYPOSTPAINT)
931         notify_customdraw(infoPtr, &nmcd, CDDS_POSTPAINT);
932     
933 cleanup:
934     /* cleanup, if we rendered offscreen */
935     if (hdc != hdcDst) {
936         BitBlt(hdcDst, 0, 0, rcClient.right, rcClient.bottom, hdc, 0, 0, SRCCOPY);
937         SelectObject(hdc, hOldBmp);
938         DeleteObject(hOffScreenBmp);
939         DeleteObject(hdc);
940     }
941 }
942
943
944 static void
945 TRACKBAR_AlignBuddies (const TRACKBAR_INFO *infoPtr)
946 {
947     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
948     HWND hwndParent = GetParent (infoPtr->hwndSelf);
949     RECT rcSelf, rcBuddy;
950     INT x, y;
951
952     GetWindowRect (infoPtr->hwndSelf, &rcSelf);
953     MapWindowPoints (HWND_DESKTOP, hwndParent, (LPPOINT)&rcSelf, 2);
954
955     /* align buddy left or above */
956     if (infoPtr->hwndBuddyLA) {
957         GetWindowRect (infoPtr->hwndBuddyLA, &rcBuddy);
958         MapWindowPoints (HWND_DESKTOP, hwndParent, (LPPOINT)&rcBuddy, 2);
959
960         if (dwStyle & TBS_VERT) {
961             x = (infoPtr->rcChannel.right + infoPtr->rcChannel.left) / 2 -
962                 (rcBuddy.right - rcBuddy.left) / 2 + rcSelf.left;
963             y = rcSelf.top - (rcBuddy.bottom - rcBuddy.top);
964         }
965         else {
966             x = rcSelf.left - (rcBuddy.right - rcBuddy.left);
967             y = (infoPtr->rcChannel.bottom + infoPtr->rcChannel.top) / 2 -
968                 (rcBuddy.bottom - rcBuddy.top) / 2 + rcSelf.top;
969         }
970
971         SetWindowPos (infoPtr->hwndBuddyLA, 0, x, y, 0, 0,
972                       SWP_NOZORDER | SWP_NOSIZE);
973     }
974
975
976     /* align buddy right or below */
977     if (infoPtr->hwndBuddyRB) {
978         GetWindowRect (infoPtr->hwndBuddyRB, &rcBuddy);
979         MapWindowPoints (HWND_DESKTOP, hwndParent, (LPPOINT)&rcBuddy, 2);
980
981         if (dwStyle & TBS_VERT) {
982             x = (infoPtr->rcChannel.right + infoPtr->rcChannel.left) / 2 -
983                 (rcBuddy.right - rcBuddy.left) / 2 + rcSelf.left;
984             y = rcSelf.bottom;
985         }
986         else {
987             x = rcSelf.right;
988             y = (infoPtr->rcChannel.bottom + infoPtr->rcChannel.top) / 2 -
989                 (rcBuddy.bottom - rcBuddy.top) / 2 + rcSelf.top;
990         }
991         SetWindowPos (infoPtr->hwndBuddyRB, 0, x, y, 0, 0,
992                       SWP_NOZORDER | SWP_NOSIZE);
993     }
994 }
995
996
997 static LRESULT
998 TRACKBAR_ClearSel (TRACKBAR_INFO *infoPtr, BOOL fRedraw)
999 {
1000     infoPtr->lSelMin = 0;
1001     infoPtr->lSelMax = 0;
1002     infoPtr->flags |= TB_SELECTIONCHANGED;
1003
1004     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1005
1006     return 0;
1007 }
1008
1009
1010 static LRESULT
1011 TRACKBAR_ClearTics (TRACKBAR_INFO *infoPtr, BOOL fRedraw)
1012 {
1013     if (infoPtr->tics) {
1014         Free (infoPtr->tics);
1015         infoPtr->tics = NULL;
1016         infoPtr->uNumTics = 0;
1017     }
1018
1019     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1020
1021     return 0;
1022 }
1023
1024
1025 static inline LRESULT
1026 TRACKBAR_GetChannelRect (const TRACKBAR_INFO *infoPtr, LPRECT lprc)
1027 {
1028     if (lprc == NULL) return 0;
1029
1030     lprc->left   = infoPtr->rcChannel.left;
1031     lprc->right  = infoPtr->rcChannel.right;
1032     lprc->bottom = infoPtr->rcChannel.bottom;
1033     lprc->top    = infoPtr->rcChannel.top;
1034
1035     return 0;
1036 }
1037
1038
1039 static inline LONG
1040 TRACKBAR_GetNumTics (const TRACKBAR_INFO *infoPtr)
1041 {
1042     if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_NOTICKS)
1043         return 0;
1044
1045     if(infoPtr->uNumTics == 0)
1046         return 2;
1047     else
1048         return infoPtr->uNumTics + 1;
1049 }
1050
1051
1052 static int comp_tics (const void *ap, const void *bp)
1053 {
1054     const DWORD a = *(const DWORD *)ap;
1055     const DWORD b = *(const DWORD *)bp;
1056
1057     TRACE("(a=%d, b=%d)\n", a, b);
1058     if (a < b) return -1;
1059     if (a > b) return 1;
1060     return 0;
1061 }
1062
1063
1064 static inline LONG
1065 TRACKBAR_GetTic (const TRACKBAR_INFO *infoPtr, INT iTic)
1066 {
1067     if ((iTic < 0) || (iTic >= infoPtr->uNumTics) || !infoPtr->tics)
1068         return -1;
1069
1070     qsort(infoPtr->tics, infoPtr->uNumTics, sizeof(DWORD), comp_tics);
1071     return infoPtr->tics[iTic];
1072 }
1073
1074
1075 static inline LONG
1076 TRACKBAR_GetTicPos (const TRACKBAR_INFO *infoPtr, INT iTic)
1077 {
1078     LONG range, width, pos, tic;
1079     int offsetthumb;
1080
1081     if ((iTic < 0) || (iTic >= infoPtr->uNumTics) || !infoPtr->tics)
1082         return -1;
1083
1084     tic   = TRACKBAR_GetTic (infoPtr, iTic);
1085     range = infoPtr->lRangeMax - infoPtr->lRangeMin;
1086     if (range <= 0) range = 1;
1087     offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
1088     width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - offsetthumb*2;
1089     pos   = infoPtr->rcChannel.left + offsetthumb + (width * tic) / range;
1090
1091     return pos;
1092 }
1093
1094
1095 static HWND
1096 TRACKBAR_SetBuddy (TRACKBAR_INFO *infoPtr, BOOL fLocation, HWND hwndBuddy)
1097 {
1098     HWND hwndTemp;
1099
1100     if (fLocation) {
1101         /* buddy is left or above */
1102         hwndTemp = infoPtr->hwndBuddyLA;
1103         infoPtr->hwndBuddyLA = hwndBuddy;
1104     }
1105     else {
1106         /* buddy is right or below */
1107         hwndTemp = infoPtr->hwndBuddyRB;
1108         infoPtr->hwndBuddyRB = hwndBuddy;
1109     }
1110
1111     TRACKBAR_AlignBuddies (infoPtr);
1112
1113     return hwndTemp;
1114 }
1115
1116
1117 static inline LONG
1118 TRACKBAR_SetLineSize (TRACKBAR_INFO *infoPtr, LONG lLineSize)
1119 {
1120     LONG lTemp = infoPtr->lLineSize;
1121
1122     infoPtr->lLineSize = lLineSize;
1123
1124     return lTemp;
1125 }
1126
1127
1128 static inline LONG
1129 TRACKBAR_SetPageSize (TRACKBAR_INFO *infoPtr, LONG lPageSize)
1130 {
1131     LONG lTemp = infoPtr->lPageSize;
1132
1133     if (lPageSize != -1)
1134         infoPtr->lPageSize = lPageSize;
1135     else
1136         infoPtr->lPageSize = TB_DEFAULTPAGESIZE;
1137
1138     return lTemp;
1139 }
1140
1141
1142 static inline LRESULT
1143 TRACKBAR_SetPos (TRACKBAR_INFO *infoPtr, BOOL fPosition, LONG lPosition)
1144 {
1145     LONG oldPos = infoPtr->lPos;
1146     infoPtr->lPos = lPosition;
1147
1148     if (infoPtr->lPos < infoPtr->lRangeMin)
1149         infoPtr->lPos = infoPtr->lRangeMin;
1150
1151     if (infoPtr->lPos > infoPtr->lRangeMax)
1152         infoPtr->lPos = infoPtr->lRangeMax;
1153     infoPtr->flags |= TB_THUMBPOSCHANGED;
1154
1155     if (fPosition && oldPos != lPosition) TRACKBAR_InvalidateThumbMove(infoPtr, oldPos, lPosition);
1156
1157     return 0;
1158 }
1159
1160
1161 static inline LRESULT
1162 TRACKBAR_SetRange (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lRange)
1163 {
1164     infoPtr->lRangeMin = (SHORT)LOWORD(lRange);
1165     infoPtr->lRangeMax = (SHORT)HIWORD(lRange);
1166
1167     if (infoPtr->lPos < infoPtr->lRangeMin) {
1168         infoPtr->lPos = infoPtr->lRangeMin;
1169         infoPtr->flags |= TB_THUMBPOSCHANGED;
1170     }
1171
1172     if (infoPtr->lPos > infoPtr->lRangeMax) {
1173         infoPtr->lPos = infoPtr->lRangeMax;
1174         infoPtr->flags |= TB_THUMBPOSCHANGED;
1175     }
1176
1177     infoPtr->lPageSize = (infoPtr->lRangeMax - infoPtr->lRangeMin) / 5;
1178     if (infoPtr->lPageSize == 0) infoPtr->lPageSize = 1;
1179
1180     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1181
1182     return 0;
1183 }
1184
1185
1186 static inline LRESULT
1187 TRACKBAR_SetRangeMax (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lMax)
1188 {
1189     infoPtr->lRangeMax = lMax;
1190     if (infoPtr->lPos > infoPtr->lRangeMax) {
1191         infoPtr->lPos = infoPtr->lRangeMax;
1192         infoPtr->flags |= TB_THUMBPOSCHANGED;
1193     }
1194
1195     infoPtr->lPageSize = (infoPtr->lRangeMax - infoPtr->lRangeMin) / 5;
1196     if (infoPtr->lPageSize == 0) infoPtr->lPageSize = 1;
1197
1198     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1199
1200     return 0;
1201 }
1202
1203
1204 static inline LRESULT
1205 TRACKBAR_SetRangeMin (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lMin)
1206 {
1207     infoPtr->lRangeMin = lMin;
1208     if (infoPtr->lPos < infoPtr->lRangeMin) {
1209         infoPtr->lPos = infoPtr->lRangeMin;
1210         infoPtr->flags |= TB_THUMBPOSCHANGED;
1211     }
1212
1213     infoPtr->lPageSize = (infoPtr->lRangeMax - infoPtr->lRangeMin) / 5;
1214     if (infoPtr->lPageSize == 0) infoPtr->lPageSize = 1;
1215
1216     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1217
1218     return 0;
1219 }
1220
1221
1222 static inline LRESULT
1223 TRACKBAR_SetSel (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lSel)
1224 {
1225     if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_ENABLESELRANGE)){
1226         infoPtr->lSelMin = 0;
1227         infoPtr->lSelMax = 0;
1228         return 0;
1229     }
1230
1231     infoPtr->lSelMin = (SHORT)LOWORD(lSel);
1232     infoPtr->lSelMax = (SHORT)HIWORD(lSel);
1233     infoPtr->flags |= TB_SELECTIONCHANGED;
1234
1235     if (infoPtr->lSelMin < infoPtr->lRangeMin)
1236         infoPtr->lSelMin = infoPtr->lRangeMin;
1237     if (infoPtr->lSelMax > infoPtr->lRangeMax)
1238         infoPtr->lSelMax = infoPtr->lRangeMax;
1239
1240     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1241
1242     return 0;
1243 }
1244
1245
1246 static inline LRESULT
1247 TRACKBAR_SetSelEnd (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lEnd)
1248 {
1249     if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_ENABLESELRANGE)){
1250         infoPtr->lSelMax = 0;
1251         return 0;
1252     }
1253
1254     infoPtr->lSelMax = lEnd;
1255     infoPtr->flags |= TB_SELECTIONCHANGED;
1256
1257     if (infoPtr->lSelMax > infoPtr->lRangeMax)
1258         infoPtr->lSelMax = infoPtr->lRangeMax;
1259
1260     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1261
1262     return 0;
1263 }
1264
1265
1266 static inline LRESULT
1267 TRACKBAR_SetSelStart (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lStart)
1268 {
1269     if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_ENABLESELRANGE)){
1270         infoPtr->lSelMin = 0;
1271         return 0;
1272     }
1273
1274     infoPtr->lSelMin = lStart;
1275     infoPtr->flags  |=TB_SELECTIONCHANGED;
1276
1277     if (infoPtr->lSelMin < infoPtr->lRangeMin)
1278         infoPtr->lSelMin = infoPtr->lRangeMin;
1279
1280     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1281
1282     return 0;
1283 }
1284
1285
1286 static inline LRESULT
1287 TRACKBAR_SetThumbLength (TRACKBAR_INFO *infoPtr, UINT iLength)
1288 {
1289     if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_FIXEDLENGTH) {
1290         infoPtr->uThumbLen = iLength;
1291         infoPtr->flags |= TB_THUMBSIZECHANGED;
1292         InvalidateRect (infoPtr->hwndSelf, &infoPtr->rcThumb, FALSE);
1293     }
1294
1295     return 0;
1296 }
1297
1298
1299 static inline LRESULT
1300 TRACKBAR_SetTic (TRACKBAR_INFO *infoPtr, LONG lPos)
1301 {
1302     if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_AUTOTICKS)
1303         return FALSE;
1304
1305     if ((lPos < infoPtr->lRangeMin) || (lPos> infoPtr->lRangeMax))
1306         return FALSE;
1307
1308     TRACE("lPos=%d\n", lPos);
1309
1310     infoPtr->uNumTics++;
1311     infoPtr->tics=ReAlloc( infoPtr->tics,
1312                                     (infoPtr->uNumTics)*sizeof (DWORD));
1313     if (!infoPtr->tics) {
1314         infoPtr->uNumTics = 0;
1315         notify(infoPtr, NM_OUTOFMEMORY);
1316         return FALSE;
1317     }
1318     infoPtr->tics[infoPtr->uNumTics-1] = lPos;
1319
1320     TRACKBAR_InvalidateAll(infoPtr);
1321
1322     return TRUE;
1323 }
1324
1325
1326 static inline LRESULT
1327 TRACKBAR_SetTicFreq (TRACKBAR_INFO *infoPtr, WORD wFreq)
1328 {
1329     if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_AUTOTICKS) {
1330         infoPtr->uTicFreq = wFreq;
1331         TRACKBAR_RecalculateTics (infoPtr);
1332         TRACKBAR_InvalidateAll(infoPtr);
1333     }
1334
1335     return 0;
1336 }
1337
1338
1339 static inline INT
1340 TRACKBAR_SetTipSide (TRACKBAR_INFO *infoPtr, INT fLocation)
1341 {
1342     INT fTemp = infoPtr->fLocation;
1343
1344     infoPtr->fLocation = fLocation;
1345
1346     return fTemp;
1347 }
1348
1349
1350 static inline LRESULT
1351 TRACKBAR_SetToolTips (TRACKBAR_INFO *infoPtr, HWND hwndTT)
1352 {
1353     infoPtr->hwndToolTip = hwndTT;
1354
1355     return 0;
1356 }
1357
1358
1359 static inline BOOL
1360 TRACKBAR_SetUnicodeFormat (TRACKBAR_INFO *infoPtr, BOOL fUnicode)
1361 {
1362     BOOL bTemp = infoPtr->bUnicode;
1363
1364     infoPtr->bUnicode = fUnicode;
1365
1366     return bTemp;
1367 }
1368
1369
1370 static LRESULT
1371 TRACKBAR_InitializeThumb (TRACKBAR_INFO *infoPtr)
1372 {
1373     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
1374     RECT rect;
1375     int clientWidth, clientMetric;
1376
1377     /* initial thumb length */
1378     clientMetric = (dwStyle & TBS_ENABLESELRANGE) ? 23 : 21;
1379     GetClientRect(infoPtr->hwndSelf,&rect);
1380     if (dwStyle & TBS_VERT) {
1381         clientWidth = rect.right - rect.left;
1382     } else {
1383         clientWidth = rect.bottom - rect.top;
1384     }
1385     if (clientWidth >= clientMetric)
1386         infoPtr->uThumbLen = clientMetric;
1387     else
1388         infoPtr->uThumbLen = clientWidth > 9 ? clientWidth - 6 : 4;
1389
1390     TRACKBAR_CalcChannel (infoPtr);
1391     TRACKBAR_UpdateThumb (infoPtr);
1392     infoPtr->flags &= ~TB_SELECTIONCHANGED;
1393
1394     return 0;
1395 }
1396
1397
1398 static LRESULT
1399 TRACKBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1400 {
1401     TRACKBAR_INFO *infoPtr;
1402     DWORD dwStyle;
1403
1404     infoPtr = Alloc (sizeof(TRACKBAR_INFO));
1405     if (!infoPtr) return -1;
1406     SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1407
1408     /* set default values */
1409     infoPtr->hwndSelf  = hwnd;
1410     infoPtr->lRangeMin = 0;
1411     infoPtr->lRangeMax = 100;
1412     infoPtr->lLineSize = 1;
1413     infoPtr->lPageSize = TB_DEFAULTPAGESIZE;
1414     infoPtr->lSelMin   = 0;
1415     infoPtr->lSelMax   = 0;
1416     infoPtr->lPos      = 0;
1417     infoPtr->fLocation = -1;
1418     infoPtr->uNumTics  = 0;    /* start and end tic are not included in count*/
1419     infoPtr->uTicFreq  = 1;
1420     infoPtr->tics      = NULL;
1421     infoPtr->hwndNotify= lpcs->hwndParent;
1422
1423     TRACKBAR_InitializeThumb (infoPtr);
1424
1425     dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1426
1427     /* Create tooltip control */
1428     if (dwStyle & TBS_TOOLTIPS) {
1429
1430         infoPtr->hwndToolTip =
1431             CreateWindowExW (0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
1432                              CW_USEDEFAULT, CW_USEDEFAULT,
1433                              CW_USEDEFAULT, CW_USEDEFAULT,
1434                              hwnd, 0, 0, 0);
1435
1436         if (infoPtr->hwndToolTip) {
1437             TTTOOLINFOW ti;         
1438             ZeroMemory (&ti, sizeof(ti));
1439             ti.cbSize   = sizeof(ti);
1440             ti.uFlags   = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
1441             ti.hwnd     = hwnd;
1442
1443             SendMessageW (infoPtr->hwndToolTip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
1444          }
1445     }
1446     
1447     OpenThemeData (hwnd, themeClass);
1448
1449     return 0;
1450 }
1451
1452
1453 static LRESULT
1454 TRACKBAR_Destroy (TRACKBAR_INFO *infoPtr)
1455 {
1456     /* delete tooltip control */
1457     if (infoPtr->hwndToolTip)
1458         DestroyWindow (infoPtr->hwndToolTip);
1459
1460     Free (infoPtr->tics);
1461     infoPtr->tics = NULL;
1462
1463     SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1464     CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
1465     Free (infoPtr);
1466
1467     return 0;
1468 }
1469
1470
1471 static LRESULT
1472 TRACKBAR_KillFocus (TRACKBAR_INFO *infoPtr)
1473 {
1474     TRACE("\n");
1475     infoPtr->bFocussed = FALSE;
1476     TRACKBAR_InvalidateAll(infoPtr);
1477
1478     return 0;
1479 }
1480
1481 static LRESULT
1482 TRACKBAR_LButtonDown (TRACKBAR_INFO *infoPtr, INT x, INT y)
1483 {
1484     POINT clickPoint;
1485
1486     clickPoint.x = x;
1487     clickPoint.y = y;
1488
1489     SetFocus(infoPtr->hwndSelf);
1490
1491     if (PtInRect(&infoPtr->rcThumb, clickPoint)) {
1492         infoPtr->flags |= TB_DRAG_MODE;
1493         SetCapture (infoPtr->hwndSelf);
1494         TRACKBAR_UpdateToolTip (infoPtr);
1495         TRACKBAR_ActivateToolTip (infoPtr, TRUE);
1496         TRACKBAR_InvalidateThumb(infoPtr, infoPtr->lPos);
1497     } else {
1498         LONG dir = TRACKBAR_GetAutoPageDirection(infoPtr, clickPoint);
1499         if (dir == 0) return 0;
1500         infoPtr->flags |= (dir < 0) ? TB_AUTO_PAGE_LEFT : TB_AUTO_PAGE_RIGHT;
1501         TRACKBAR_AutoPage (infoPtr, clickPoint);
1502         SetCapture (infoPtr->hwndSelf);
1503         SetTimer(infoPtr->hwndSelf, TB_REFRESH_TIMER, TB_REFRESH_DELAY, 0);
1504     }
1505
1506     return 0;
1507 }
1508
1509
1510 static LRESULT
1511 TRACKBAR_LButtonUp (TRACKBAR_INFO *infoPtr)
1512 {
1513     if (infoPtr->flags & TB_DRAG_MODE) {
1514         notify_with_scroll (infoPtr, TB_THUMBPOSITION | (infoPtr->lPos<<16));
1515         notify_with_scroll (infoPtr, TB_ENDTRACK);
1516         infoPtr->flags &= ~TB_DRAG_MODE;
1517         ReleaseCapture ();
1518         notify(infoPtr, NM_RELEASEDCAPTURE);
1519         TRACKBAR_ActivateToolTip(infoPtr, FALSE);
1520         TRACKBAR_InvalidateThumb(infoPtr, infoPtr->lPos);
1521     }
1522     if (infoPtr->flags & TB_AUTO_PAGE) {
1523         KillTimer (infoPtr->hwndSelf, TB_REFRESH_TIMER);
1524         infoPtr->flags &= ~TB_AUTO_PAGE;
1525         notify_with_scroll (infoPtr, TB_ENDTRACK);
1526         ReleaseCapture ();
1527         notify(infoPtr, NM_RELEASEDCAPTURE);
1528     }
1529
1530     return 0;
1531 }
1532
1533
1534 static LRESULT
1535 TRACKBAR_CaptureChanged (const TRACKBAR_INFO *infoPtr)
1536 {
1537     notify_with_scroll (infoPtr, TB_ENDTRACK);
1538     return 0;
1539 }
1540
1541
1542 static LRESULT
1543 TRACKBAR_Paint (TRACKBAR_INFO *infoPtr, HDC hdc)
1544 {
1545     if (hdc) {
1546         TRACKBAR_Refresh(infoPtr, hdc);
1547     } else {
1548         PAINTSTRUCT ps;
1549         hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1550         TRACKBAR_Refresh (infoPtr, hdc);
1551         EndPaint (infoPtr->hwndSelf, &ps);
1552     }
1553
1554     return 0;
1555 }
1556
1557
1558 static LRESULT
1559 TRACKBAR_SetFocus (TRACKBAR_INFO *infoPtr)
1560 {
1561     TRACE("\n");
1562     infoPtr->bFocussed = TRUE;
1563     TRACKBAR_InvalidateAll(infoPtr);
1564
1565     return 0;
1566 }
1567
1568
1569 static LRESULT
1570 TRACKBAR_Size (TRACKBAR_INFO *infoPtr)
1571 {
1572     TRACKBAR_InitializeThumb (infoPtr);
1573     TRACKBAR_AlignBuddies (infoPtr);
1574
1575     return 0;
1576 }
1577
1578
1579 static LRESULT
1580 TRACKBAR_Timer (TRACKBAR_INFO *infoPtr)
1581 {
1582     if (infoPtr->flags & TB_AUTO_PAGE) {
1583         POINT pt;
1584         if (GetCursorPos(&pt))
1585             if (ScreenToClient(infoPtr->hwndSelf, &pt))
1586                 TRACKBAR_AutoPage(infoPtr, pt);
1587     }
1588     return 0;
1589 }
1590
1591
1592 /* update theme after a WM_THEMECHANGED message */
1593 static LRESULT theme_changed (const TRACKBAR_INFO* infoPtr)
1594 {
1595     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
1596     CloseThemeData (theme);
1597     OpenThemeData (infoPtr->hwndSelf, themeClass);
1598     return 0;
1599 }
1600
1601
1602 static LRESULT
1603 TRACKBAR_MouseMove (TRACKBAR_INFO *infoPtr, INT x, INT y)
1604 {
1605     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
1606     INT clickPlace = (dwStyle & TBS_VERT) ? y : x;
1607     LONG dragPos, oldPos = infoPtr->lPos;
1608
1609     TRACE("(x=%d. y=%d)\n", x, y);
1610
1611     if (infoPtr->flags & TB_AUTO_PAGE) {
1612         POINT pt;
1613         pt.x = x;
1614         pt.y = y;
1615         TRACKBAR_AutoPage (infoPtr, pt);
1616         return TRUE;
1617     }
1618
1619     if (!(infoPtr->flags & TB_DRAG_MODE)) 
1620     {
1621         if (GetWindowTheme (infoPtr->hwndSelf))
1622         {
1623             DWORD oldFlags = infoPtr->flags;
1624             POINT pt;
1625             pt.x = x;
1626             pt.y = y;
1627             if (PtInRect (&infoPtr->rcThumb, pt))
1628             {
1629                 TRACKMOUSEEVENT tme;
1630                 tme.cbSize = sizeof( tme );
1631                 tme.dwFlags = TME_LEAVE;
1632                 tme.hwndTrack = infoPtr->hwndSelf;
1633                 TrackMouseEvent( &tme );
1634                 infoPtr->flags |= TB_THUMB_HOT;
1635             }
1636             else
1637             {
1638                 TRACKMOUSEEVENT tme;
1639                 tme.cbSize = sizeof( tme );
1640                 tme.dwFlags = TME_CANCEL;
1641                 tme.hwndTrack = infoPtr->hwndSelf;
1642                 TrackMouseEvent( &tme );
1643                 infoPtr->flags &= ~TB_THUMB_HOT; 
1644             }
1645             if (oldFlags != infoPtr->flags) InvalidateRect (infoPtr->hwndSelf, &infoPtr->rcThumb, FALSE);
1646         }
1647         return TRUE;
1648     }
1649
1650     dragPos = TRACKBAR_ConvertPlaceToPosition (infoPtr, clickPlace,
1651                                                dwStyle & TBS_VERT);
1652     if (dragPos == oldPos) return TRUE;
1653
1654     infoPtr->lPos = dragPos;
1655
1656     infoPtr->flags |= TB_THUMBPOSCHANGED;
1657     notify_with_scroll (infoPtr, TB_THUMBTRACK | (infoPtr->lPos<<16));
1658
1659
1660     TRACKBAR_InvalidateThumbMove(infoPtr, oldPos, dragPos);
1661     UpdateWindow (infoPtr->hwndSelf);
1662
1663     return TRUE;
1664 }
1665
1666 static BOOL
1667 TRACKBAR_KeyDown (TRACKBAR_INFO *infoPtr, INT nVirtKey)
1668 {
1669     DWORD style = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
1670     BOOL downIsLeft = style & TBS_DOWNISLEFT;
1671     BOOL vert = style & TBS_VERT;
1672     LONG pos = infoPtr->lPos;
1673
1674     TRACE("%x\n", nVirtKey);
1675
1676     switch (nVirtKey) {
1677     case VK_UP:
1678         if (!vert && downIsLeft) TRACKBAR_LineDown(infoPtr);
1679         else TRACKBAR_LineUp(infoPtr);
1680         break;
1681     case VK_LEFT:
1682         if (vert && downIsLeft) TRACKBAR_LineDown(infoPtr);
1683         else TRACKBAR_LineUp(infoPtr);
1684         break;
1685     case VK_DOWN:
1686         if (!vert && downIsLeft) TRACKBAR_LineUp(infoPtr);
1687         else TRACKBAR_LineDown(infoPtr);
1688         break;
1689     case VK_RIGHT:
1690         if (vert && downIsLeft) TRACKBAR_LineUp(infoPtr);
1691         else TRACKBAR_LineDown(infoPtr);
1692         break;
1693     case VK_NEXT:
1694         if (!vert && downIsLeft) TRACKBAR_PageUp(infoPtr);
1695         else TRACKBAR_PageDown(infoPtr);
1696         break;
1697     case VK_PRIOR:
1698         if (!vert && downIsLeft) TRACKBAR_PageDown(infoPtr);
1699         else TRACKBAR_PageUp(infoPtr);
1700         break;
1701     case VK_HOME:
1702         if (infoPtr->lPos == infoPtr->lRangeMin) return FALSE;
1703         infoPtr->lPos = infoPtr->lRangeMin;
1704         notify_with_scroll (infoPtr, TB_TOP);
1705         break;
1706     case VK_END:
1707         if (infoPtr->lPos == infoPtr->lRangeMax) return FALSE;
1708         infoPtr->lPos = infoPtr->lRangeMax;
1709         notify_with_scroll (infoPtr, TB_BOTTOM);
1710         break;
1711     }
1712
1713     if (pos != infoPtr->lPos) {
1714         infoPtr->flags |=TB_THUMBPOSCHANGED;
1715         TRACKBAR_InvalidateThumbMove (infoPtr, pos, infoPtr->lPos);
1716     }
1717
1718     return TRUE;
1719 }
1720
1721
1722 static inline BOOL
1723 TRACKBAR_KeyUp (const TRACKBAR_INFO *infoPtr, INT nVirtKey)
1724 {
1725     switch (nVirtKey) {
1726     case VK_LEFT:
1727     case VK_UP:
1728     case VK_RIGHT:
1729     case VK_DOWN:
1730     case VK_NEXT:
1731     case VK_PRIOR:
1732     case VK_HOME:
1733     case VK_END:
1734         notify_with_scroll (infoPtr, TB_ENDTRACK);
1735     }
1736     return TRUE;
1737 }
1738
1739
1740 static LRESULT WINAPI
1741 TRACKBAR_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1742 {
1743     TRACKBAR_INFO *infoPtr = (TRACKBAR_INFO *)GetWindowLongPtrW (hwnd, 0);
1744
1745     TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1746
1747     if (!infoPtr && (uMsg != WM_CREATE))
1748         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1749
1750     switch (uMsg)
1751     {
1752     case TBM_CLEARSEL:
1753         return TRACKBAR_ClearSel (infoPtr, (BOOL)wParam);
1754
1755     case TBM_CLEARTICS:
1756         return TRACKBAR_ClearTics (infoPtr, (BOOL)wParam);
1757
1758     case TBM_GETBUDDY:
1759         return (LRESULT)(wParam ? infoPtr->hwndBuddyLA : infoPtr->hwndBuddyRB);
1760
1761     case TBM_GETCHANNELRECT:
1762         return TRACKBAR_GetChannelRect (infoPtr, (LPRECT)lParam);
1763
1764     case TBM_GETLINESIZE:
1765         return infoPtr->lLineSize;
1766
1767     case TBM_GETNUMTICS:
1768         return TRACKBAR_GetNumTics (infoPtr);
1769
1770     case TBM_GETPAGESIZE:
1771         return infoPtr->lPageSize;
1772
1773     case TBM_GETPOS:
1774         return infoPtr->lPos;
1775
1776     case TBM_GETPTICS:
1777         return (LRESULT)infoPtr->tics;
1778
1779     case TBM_GETRANGEMAX:
1780         return infoPtr->lRangeMax;
1781
1782     case TBM_GETRANGEMIN:
1783         return infoPtr->lRangeMin;
1784
1785     case TBM_GETSELEND:
1786         return infoPtr->lSelMax;
1787
1788     case TBM_GETSELSTART:
1789         return infoPtr->lSelMin;
1790
1791     case TBM_GETTHUMBLENGTH:
1792         return infoPtr->uThumbLen;
1793
1794     case TBM_GETTHUMBRECT:
1795         return CopyRect((LPRECT)lParam, &infoPtr->rcThumb);
1796
1797     case TBM_GETTIC:
1798         return TRACKBAR_GetTic (infoPtr, (INT)wParam);
1799
1800     case TBM_GETTICPOS:
1801         return TRACKBAR_GetTicPos (infoPtr, (INT)wParam);
1802
1803     case TBM_GETTOOLTIPS:
1804         return (LRESULT)infoPtr->hwndToolTip;
1805
1806     case TBM_GETUNICODEFORMAT:
1807         return infoPtr->bUnicode;
1808
1809     case TBM_SETBUDDY:
1810         return (LRESULT) TRACKBAR_SetBuddy(infoPtr, (BOOL)wParam, (HWND)lParam);
1811
1812     case TBM_SETLINESIZE:
1813         return TRACKBAR_SetLineSize (infoPtr, (LONG)lParam);
1814
1815     case TBM_SETPAGESIZE:
1816         return TRACKBAR_SetPageSize (infoPtr, (LONG)lParam);
1817
1818     case TBM_SETPOS:
1819         return TRACKBAR_SetPos (infoPtr, (BOOL)wParam, (LONG)lParam);
1820
1821     case TBM_SETRANGE:
1822         return TRACKBAR_SetRange (infoPtr, (BOOL)wParam, (LONG)lParam);
1823
1824     case TBM_SETRANGEMAX:
1825         return TRACKBAR_SetRangeMax (infoPtr, (BOOL)wParam, (LONG)lParam);
1826
1827     case TBM_SETRANGEMIN:
1828         return TRACKBAR_SetRangeMin (infoPtr, (BOOL)wParam, (LONG)lParam);
1829
1830     case TBM_SETSEL:
1831         return TRACKBAR_SetSel (infoPtr, (BOOL)wParam, (LONG)lParam);
1832
1833     case TBM_SETSELEND:
1834         return TRACKBAR_SetSelEnd (infoPtr, (BOOL)wParam, (LONG)lParam);
1835
1836     case TBM_SETSELSTART:
1837         return TRACKBAR_SetSelStart (infoPtr, (BOOL)wParam, (LONG)lParam);
1838
1839     case TBM_SETTHUMBLENGTH:
1840         return TRACKBAR_SetThumbLength (infoPtr, (UINT)wParam);
1841
1842     case TBM_SETTIC:
1843         return TRACKBAR_SetTic (infoPtr, (LONG)lParam);
1844
1845     case TBM_SETTICFREQ:
1846         return TRACKBAR_SetTicFreq (infoPtr, (WORD)wParam);
1847
1848     case TBM_SETTIPSIDE:
1849         return TRACKBAR_SetTipSide (infoPtr, (INT)wParam);
1850
1851     case TBM_SETTOOLTIPS:
1852         return TRACKBAR_SetToolTips (infoPtr, (HWND)wParam);
1853
1854     case TBM_SETUNICODEFORMAT:
1855         return TRACKBAR_SetUnicodeFormat (infoPtr, (BOOL)wParam);
1856
1857
1858     case WM_CAPTURECHANGED:
1859         return TRACKBAR_CaptureChanged (infoPtr);
1860
1861     case WM_CREATE:
1862         return TRACKBAR_Create (hwnd, (LPCREATESTRUCTW)lParam);
1863
1864     case WM_DESTROY:
1865         return TRACKBAR_Destroy (infoPtr);
1866
1867 /*      case WM_ENABLE: */
1868
1869     case WM_ERASEBKGND:
1870         return 0;
1871
1872     case WM_GETDLGCODE:
1873         return DLGC_WANTARROWS;
1874
1875     case WM_KEYDOWN:
1876         return TRACKBAR_KeyDown (infoPtr, (INT)wParam);
1877
1878     case WM_KEYUP:
1879         return TRACKBAR_KeyUp (infoPtr, (INT)wParam);
1880
1881     case WM_KILLFOCUS:
1882         return TRACKBAR_KillFocus (infoPtr);
1883
1884     case WM_LBUTTONDOWN:
1885         return TRACKBAR_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1886
1887     case WM_LBUTTONUP:
1888         return TRACKBAR_LButtonUp (infoPtr);
1889
1890     case WM_MOUSELEAVE:
1891         infoPtr->flags &= ~TB_THUMB_HOT; 
1892         InvalidateRect (infoPtr->hwndSelf, &infoPtr->rcThumb, FALSE);
1893         return 0;
1894     
1895     case WM_MOUSEMOVE:
1896         return TRACKBAR_MouseMove (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1897
1898     case WM_PRINTCLIENT:
1899     case WM_PAINT:
1900         return TRACKBAR_Paint (infoPtr, (HDC)wParam);
1901
1902     case WM_SETFOCUS:
1903         return TRACKBAR_SetFocus (infoPtr);
1904
1905     case WM_SIZE:
1906         return TRACKBAR_Size (infoPtr);
1907
1908     case WM_THEMECHANGED:
1909         return theme_changed (infoPtr);
1910
1911     case WM_TIMER:
1912         return TRACKBAR_Timer (infoPtr);
1913
1914     case WM_WININICHANGE:
1915         return TRACKBAR_InitializeThumb (infoPtr);
1916
1917     default:
1918         if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1919             ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
1920         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1921     }
1922 }
1923
1924
1925 void TRACKBAR_Register (void)
1926 {
1927     WNDCLASSW wndClass;
1928
1929     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1930     wndClass.style         = CS_GLOBALCLASS;
1931     wndClass.lpfnWndProc   = TRACKBAR_WindowProc;
1932     wndClass.cbClsExtra    = 0;
1933     wndClass.cbWndExtra    = sizeof(TRACKBAR_INFO *);
1934     wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1935     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1936     wndClass.lpszClassName = TRACKBAR_CLASSW;
1937
1938     RegisterClassW (&wndClass);
1939 }
1940
1941
1942 void TRACKBAR_Unregister (void)
1943 {
1944     UnregisterClassW (TRACKBAR_CLASSW, NULL);
1945 }