crypt32: Use skip to avoid failures where support is missing.
[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 /* Used by TRACKBAR_Refresh to find out which parts of the control
85    need to be recalculated */
86
87 #define TB_THUMBPOSCHANGED      1
88 #define TB_THUMBSIZECHANGED     2
89 #define TB_THUMBCHANGED         (TB_THUMBPOSCHANGED | TB_THUMBSIZECHANGED)
90 #define TB_SELECTIONCHANGED     4
91 #define TB_DRAG_MODE            8     /* we're dragging the slider */
92 #define TB_AUTO_PAGE_LEFT       16
93 #define TB_AUTO_PAGE_RIGHT      32
94 #define TB_AUTO_PAGE            (TB_AUTO_PAGE_LEFT | TB_AUTO_PAGE_RIGHT)
95 #define TB_THUMB_HOT            64    /* mouse hovers above thumb */
96
97 /* helper defines for TRACKBAR_DrawTic */
98 #define TIC_EDGE                0x20
99 #define TIC_SELECTIONMARKMAX    0x80
100 #define TIC_SELECTIONMARKMIN    0x100
101 #define TIC_SELECTIONMARK       (TIC_SELECTIONMARKMAX | TIC_SELECTIONMARKMIN)
102
103 static const WCHAR themeClass[] = { 'T','r','a','c','k','b','a','r',0 };
104
105 static inline int 
106 notify_customdraw (const TRACKBAR_INFO *infoPtr, NMCUSTOMDRAW *pnmcd, int stage)
107 {
108     pnmcd->dwDrawStage = stage;
109     return SendMessageW (infoPtr->hwndNotify, WM_NOTIFY, 
110                          pnmcd->hdr.idFrom, (LPARAM)pnmcd);
111 }
112
113 static LRESULT notify_hdr (const TRACKBAR_INFO *infoPtr, INT code, LPNMHDR pnmh)
114 {
115     LRESULT result;
116     
117     TRACE("(code=%d)\n", code);
118
119     pnmh->hwndFrom = infoPtr->hwndSelf;
120     pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
121     pnmh->code = code;
122     result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
123                           (WPARAM)pnmh->idFrom, (LPARAM)pnmh);
124
125     TRACE("  <= %ld\n", result);
126
127     return result;
128 }
129
130 static inline int notify (const TRACKBAR_INFO *infoPtr, INT code)
131 {
132     NMHDR nmh;
133     return notify_hdr(infoPtr, code, &nmh);
134 }
135
136 static BOOL
137 notify_with_scroll (const TRACKBAR_INFO *infoPtr, UINT code)
138 {
139     BOOL bVert = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_VERT;
140
141     TRACE("%x\n", code);
142
143     return (BOOL) SendMessageW (infoPtr->hwndNotify,
144                                 bVert ? WM_VSCROLL : WM_HSCROLL,
145                                 (WPARAM)code, (LPARAM)infoPtr->hwndSelf);
146 }
147     
148 static void TRACKBAR_RecalculateTics (TRACKBAR_INFO *infoPtr)
149 {
150     int i, tic, nrTics;
151
152     if (infoPtr->uTicFreq && infoPtr->lRangeMax >= infoPtr->lRangeMin)
153         nrTics=(infoPtr->lRangeMax - infoPtr->lRangeMin)/infoPtr->uTicFreq;
154     else {
155         nrTics = 0;
156         Free (infoPtr->tics);
157         infoPtr->tics = NULL;
158         infoPtr->uNumTics = 0;
159         return;
160     }
161
162     if (nrTics != infoPtr->uNumTics) {
163         infoPtr->tics=ReAlloc (infoPtr->tics,
164                                         (nrTics+1)*sizeof (DWORD));
165         if (!infoPtr->tics) {
166             infoPtr->uNumTics = 0;
167             notify(infoPtr, NM_OUTOFMEMORY);
168             return;
169         }
170         infoPtr->uNumTics = nrTics;
171     }
172
173     tic = infoPtr->lRangeMin + infoPtr->uTicFreq;
174     for (i = 0; i < nrTics; i++, tic += infoPtr->uTicFreq)
175         infoPtr->tics[i] = tic;
176 }
177
178 /* converts from physical (mouse) position to logical position
179    (in range of trackbar) */
180
181 static inline LONG
182 TRACKBAR_ConvertPlaceToPosition (const TRACKBAR_INFO *infoPtr, int place, int vertical)
183 {
184     double range, width, pos, offsetthumb;
185
186     range = infoPtr->lRangeMax - infoPtr->lRangeMin;
187     if (vertical) {
188         offsetthumb = (infoPtr->rcThumb.bottom - infoPtr->rcThumb.top)/2;
189         width = infoPtr->rcChannel.bottom - infoPtr->rcChannel.top - (offsetthumb * 2) - 1;
190         pos = (range*(place - infoPtr->rcChannel.top - offsetthumb)) / width;
191     } else {
192         offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
193         width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - (offsetthumb * 2) - 1;
194         pos = (range*(place - infoPtr->rcChannel.left - offsetthumb)) / width;
195     }
196     pos += infoPtr->lRangeMin;
197     if (pos > infoPtr->lRangeMax)
198         pos = infoPtr->lRangeMax;
199     else if (pos < infoPtr->lRangeMin)
200         pos = infoPtr->lRangeMin;
201
202     TRACE("%.2f\n", pos);
203     return (LONG)(pos + 0.5);
204 }
205
206
207 /* return: 0> prev, 0 none, >0 next */
208 static LONG
209 TRACKBAR_GetAutoPageDirection (const TRACKBAR_INFO *infoPtr, POINT clickPoint)
210 {
211     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
212     RECT pageRect;
213
214     if (dwStyle & TBS_VERT) {
215         pageRect.top = infoPtr->rcChannel.top;
216         pageRect.bottom = infoPtr->rcChannel.bottom;
217         pageRect.left = infoPtr->rcThumb.left;
218         pageRect.right = infoPtr->rcThumb.right;
219     } else {
220         pageRect.top = infoPtr->rcThumb.top;
221         pageRect.bottom = infoPtr->rcThumb.bottom;
222         pageRect.left = infoPtr->rcChannel.left;
223         pageRect.right = infoPtr->rcChannel.right;
224     }
225
226
227     if (PtInRect(&pageRect, clickPoint))
228     {
229         int clickPlace = (dwStyle & TBS_VERT) ? clickPoint.y : clickPoint.x;
230
231         LONG clickPos = TRACKBAR_ConvertPlaceToPosition(infoPtr, clickPlace,
232                                                         dwStyle & TBS_VERT);
233         return clickPos - infoPtr->lPos;
234     }
235
236     return 0;
237 }
238
239 static inline void
240 TRACKBAR_PageDown (TRACKBAR_INFO *infoPtr)
241 {
242     if (infoPtr->lPos == infoPtr->lRangeMax) return;
243
244     infoPtr->lPos += infoPtr->lPageSize;
245     if (infoPtr->lPos > infoPtr->lRangeMax)
246         infoPtr->lPos = infoPtr->lRangeMax;
247     notify_with_scroll (infoPtr, TB_PAGEDOWN);
248 }
249
250
251 static inline void
252 TRACKBAR_PageUp (TRACKBAR_INFO *infoPtr)
253 {
254     if (infoPtr->lPos == infoPtr->lRangeMin) return;
255
256     infoPtr->lPos -= infoPtr->lPageSize;
257     if (infoPtr->lPos < infoPtr->lRangeMin)
258         infoPtr->lPos = infoPtr->lRangeMin;
259     notify_with_scroll (infoPtr, TB_PAGEUP);
260 }
261
262 static inline void TRACKBAR_LineUp(TRACKBAR_INFO *infoPtr)
263 {
264     if (infoPtr->lPos == infoPtr->lRangeMin) return;
265     infoPtr->lPos -= infoPtr->lLineSize;
266     if (infoPtr->lPos < infoPtr->lRangeMin)
267         infoPtr->lPos = infoPtr->lRangeMin;
268     notify_with_scroll (infoPtr, TB_LINEUP);
269 }
270
271 static inline void TRACKBAR_LineDown(TRACKBAR_INFO *infoPtr)
272 {
273     if (infoPtr->lPos == infoPtr->lRangeMax) return;
274     infoPtr->lPos += infoPtr->lLineSize;
275     if (infoPtr->lPos > infoPtr->lRangeMax)
276         infoPtr->lPos = infoPtr->lRangeMax;
277     notify_with_scroll (infoPtr, TB_LINEDOWN);
278 }
279
280 static void
281 TRACKBAR_CalcChannel (TRACKBAR_INFO *infoPtr)
282 {
283     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
284     INT cyChannel, offsetthumb, offsetedge;
285     RECT lpRect, *channel = & infoPtr->rcChannel;
286
287     GetClientRect (infoPtr->hwndSelf, &lpRect);
288
289     offsetthumb = infoPtr->uThumbLen / 4;
290     offsetedge  = offsetthumb + 3;
291     cyChannel   = (dwStyle & TBS_ENABLESELRANGE) ? offsetthumb*3 : 4;
292     if (dwStyle & TBS_VERT) {
293         channel->top    = lpRect.top + offsetedge;
294         channel->bottom = lpRect.bottom - offsetedge;
295         if (dwStyle & TBS_ENABLESELRANGE)
296             channel->left = lpRect.left + ((infoPtr->uThumbLen - cyChannel + 2) / 2);
297         else
298             channel->left = lpRect.left + (infoPtr->uThumbLen / 2) - 1;
299         if (dwStyle & TBS_BOTH) {
300             if (dwStyle & TBS_NOTICKS)
301                 channel->left += 1;
302             else
303                 channel->left += 9;
304         }
305         else if (dwStyle & TBS_TOP) {
306             if (dwStyle & TBS_NOTICKS)
307                 channel->left += 2;
308             else
309                 channel->left += 10;
310         }
311         channel->right = channel->left + cyChannel;
312     } else {
313         channel->left = lpRect.left + offsetedge;
314         channel->right = lpRect.right - offsetedge;
315         if (dwStyle & TBS_ENABLESELRANGE)
316             channel->top = lpRect.top + ((infoPtr->uThumbLen - cyChannel + 2) / 2);
317         else
318             channel->top = lpRect.top + (infoPtr->uThumbLen / 2) - 1;
319         if (dwStyle & TBS_BOTH) {
320             if (dwStyle & TBS_NOTICKS)
321                 channel->top += 1;
322             else
323                 channel->top += 9;
324         }
325         else if (dwStyle & TBS_TOP) {
326             if (dwStyle & TBS_NOTICKS)
327                 channel->top += 2;
328             else
329                 channel->top += 10;
330         }
331         channel->bottom   = channel->top + cyChannel;
332     }
333 }
334
335 static void
336 TRACKBAR_CalcThumb (const TRACKBAR_INFO *infoPtr, LONG lPos, RECT *thumb)
337 {
338     int range, width, height, thumbwidth;
339     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
340     RECT lpRect;
341
342     range = infoPtr->lRangeMax - infoPtr->lRangeMin;
343     thumbwidth = (infoPtr->uThumbLen / 2) | 1;
344
345     if (!range) range = 1;
346
347     GetClientRect(infoPtr->hwndSelf, &lpRect);
348     if (dwStyle & TBS_VERT)
349     {
350         height = infoPtr->rcChannel.bottom - infoPtr->rcChannel.top - thumbwidth;
351
352         if ((dwStyle & (TBS_BOTH | TBS_LEFT)) && !(dwStyle & TBS_NOTICKS))
353             thumb->left = 10;
354         else
355             thumb->left = 2;
356         thumb->right = thumb->left + infoPtr->uThumbLen;
357         thumb->top = infoPtr->rcChannel.top +
358                      (height*(lPos - infoPtr->lRangeMin))/range;
359         thumb->bottom = thumb->top + thumbwidth;
360     }
361     else
362     {
363         width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - thumbwidth;
364
365         thumb->left = infoPtr->rcChannel.left +
366                       (width*(lPos - infoPtr->lRangeMin))/range;
367         thumb->right = thumb->left + thumbwidth;
368         if ((dwStyle & (TBS_BOTH | TBS_TOP)) && !(dwStyle & TBS_NOTICKS))
369             thumb->top = 10;
370         else
371             thumb->top = 2;
372         thumb->bottom = thumb->top + infoPtr->uThumbLen;
373     }
374 }
375
376 static inline void
377 TRACKBAR_UpdateThumb (TRACKBAR_INFO *infoPtr)
378 {
379     TRACKBAR_CalcThumb(infoPtr, infoPtr->lPos, &infoPtr->rcThumb);
380 }
381
382 static inline void
383 TRACKBAR_InvalidateAll (const TRACKBAR_INFO *infoPtr)
384 {
385     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
386 }
387
388 static void
389 TRACKBAR_InvalidateThumb (const TRACKBAR_INFO *infoPtr, LONG thumbPos)
390 {
391     RECT rcThumb;
392
393     TRACKBAR_CalcThumb(infoPtr, thumbPos, &rcThumb);
394     InflateRect(&rcThumb, 1, 1);
395     InvalidateRect(infoPtr->hwndSelf, &rcThumb, FALSE);
396 }
397
398 static inline void
399 TRACKBAR_InvalidateThumbMove (const TRACKBAR_INFO *infoPtr, LONG oldPos, LONG newPos)
400 {
401     TRACKBAR_InvalidateThumb (infoPtr, oldPos);
402     if (newPos != oldPos)
403         TRACKBAR_InvalidateThumb (infoPtr, newPos);
404 }
405
406 static inline BOOL
407 TRACKBAR_HasSelection (const TRACKBAR_INFO *infoPtr)
408 {
409     return infoPtr->lSelMin != infoPtr->lSelMax;
410 }
411
412 static void
413 TRACKBAR_CalcSelection (TRACKBAR_INFO *infoPtr)
414 {
415     RECT *selection = &infoPtr->rcSelection;
416     int range = infoPtr->lRangeMax - infoPtr->lRangeMin;
417     int offsetthumb, height, width;
418
419     if (range <= 0) {
420         SetRectEmpty (selection);
421     } else {
422         if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_VERT) {
423             offsetthumb = (infoPtr->rcThumb.bottom - infoPtr->rcThumb.top)/2;
424             height = infoPtr->rcChannel.bottom - infoPtr->rcChannel.top - offsetthumb*2;
425             selection->top    = infoPtr->rcChannel.top + offsetthumb +
426                 (height*infoPtr->lSelMin)/range;
427             selection->bottom = infoPtr->rcChannel.top + offsetthumb +
428                 (height*infoPtr->lSelMax)/range;
429             selection->left   = infoPtr->rcChannel.left + 3;
430             selection->right  = infoPtr->rcChannel.right - 3;
431         } else {
432             offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
433             width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - offsetthumb*2;
434             selection->left   = infoPtr->rcChannel.left + offsetthumb +
435                 (width*infoPtr->lSelMin)/range;
436             selection->right  = infoPtr->rcChannel.left + offsetthumb +
437                 (width*infoPtr->lSelMax)/range;
438             selection->top    = infoPtr->rcChannel.top + 3;
439             selection->bottom = infoPtr->rcChannel.bottom - 3;
440         }
441     }
442
443     TRACE("selection[left=%d, top=%d, right=%d, bottom=%d]\n",
444            selection->left, selection->top, selection->right, selection->bottom);
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, (LPARAM)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 backbround */
869     if (gcdrf == CDRF_DODEFAULT ||
870         notify_customdraw(infoPtr, &nmcd, CDDS_PREERASE) != CDRF_SKIPDEFAULT) {
871         if (GetWindowTheme (infoPtr->hwndSelf))
872             DrawThemeParentBackground (infoPtr->hwndSelf, hdc, &rcClient);
873         else
874             FillRect (hdc, &rcClient, GetSysColorBrush(COLOR_BTNFACE));
875         if (gcdrf != CDRF_DODEFAULT)
876             notify_customdraw(infoPtr, &nmcd, CDDS_POSTERASE);
877     }
878     
879     /* draw channel */
880     if (gcdrf & CDRF_NOTIFYITEMDRAW) {
881         nmcd.dwItemSpec = TBCD_CHANNEL;
882         nmcd.uItemState = CDIS_DEFAULT;
883         nmcd.rc = infoPtr->rcChannel;
884         icdrf = notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPREPAINT);
885     } else icdrf = CDRF_DODEFAULT;
886     if ( !(icdrf & CDRF_SKIPDEFAULT) ) {
887         TRACKBAR_DrawChannel (infoPtr, hdc, dwStyle);
888         if (icdrf & CDRF_NOTIFYPOSTPAINT)
889             notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPOSTPAINT);
890     }
891
892
893     /* draw tics */
894     if (!(dwStyle & TBS_NOTICKS)) {
895         if (gcdrf & CDRF_NOTIFYITEMDRAW) {
896             nmcd.dwItemSpec = TBCD_TICS;
897             nmcd.uItemState = CDIS_DEFAULT;
898             nmcd.rc = rcClient;
899             icdrf = notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPREPAINT);
900         } else icdrf = CDRF_DODEFAULT;
901         if ( !(icdrf & CDRF_SKIPDEFAULT) ) {
902             TRACKBAR_DrawTics (infoPtr, hdc, dwStyle);
903             if (icdrf & CDRF_NOTIFYPOSTPAINT)
904                 notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPOSTPAINT);
905         }
906     }
907     
908     /* draw thumb */
909     if (!(dwStyle & TBS_NOTHUMB)) {
910         if (gcdrf & CDRF_NOTIFYITEMDRAW) {
911             nmcd.dwItemSpec = TBCD_THUMB;
912             nmcd.uItemState = infoPtr->flags & TB_DRAG_MODE ? CDIS_HOT : CDIS_DEFAULT;
913             nmcd.rc = infoPtr->rcThumb;
914             icdrf = notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPREPAINT);
915         } else icdrf = CDRF_DODEFAULT;
916         if ( !(icdrf & CDRF_SKIPDEFAULT) ) {
917             TRACKBAR_DrawThumb(infoPtr, hdc, dwStyle);
918             if (icdrf & CDRF_NOTIFYPOSTPAINT)
919                 notify_customdraw(infoPtr, &nmcd, CDDS_ITEMPOSTPAINT);
920         }
921     }
922
923     /* draw focus rectangle */
924     if (infoPtr->bFocussed) {
925         DrawFocusRect(hdc, &rcClient);
926     }
927
928     /* finish up the painting */
929     if (gcdrf & CDRF_NOTIFYPOSTPAINT)
930         notify_customdraw(infoPtr, &nmcd, CDDS_POSTPAINT);
931     
932 cleanup:
933     /* cleanup, if we rendered offscreen */
934     if (hdc != hdcDst) {
935         BitBlt(hdcDst, 0, 0, rcClient.right, rcClient.bottom, hdc, 0, 0, SRCCOPY);
936         SelectObject(hdc, hOldBmp);
937         DeleteObject(hOffScreenBmp);
938         DeleteObject(hdc);
939     }
940 }
941
942
943 static void
944 TRACKBAR_AlignBuddies (const TRACKBAR_INFO *infoPtr)
945 {
946     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
947     HWND hwndParent = GetParent (infoPtr->hwndSelf);
948     RECT rcSelf, rcBuddy;
949     INT x, y;
950
951     GetWindowRect (infoPtr->hwndSelf, &rcSelf);
952     MapWindowPoints (HWND_DESKTOP, hwndParent, (LPPOINT)&rcSelf, 2);
953
954     /* align buddy left or above */
955     if (infoPtr->hwndBuddyLA) {
956         GetWindowRect (infoPtr->hwndBuddyLA, &rcBuddy);
957         MapWindowPoints (HWND_DESKTOP, hwndParent, (LPPOINT)&rcBuddy, 2);
958
959         if (dwStyle & TBS_VERT) {
960             x = (infoPtr->rcChannel.right + infoPtr->rcChannel.left) / 2 -
961                 (rcBuddy.right - rcBuddy.left) / 2 + rcSelf.left;
962             y = rcSelf.top - (rcBuddy.bottom - rcBuddy.top);
963         }
964         else {
965             x = rcSelf.left - (rcBuddy.right - rcBuddy.left);
966             y = (infoPtr->rcChannel.bottom + infoPtr->rcChannel.top) / 2 -
967                 (rcBuddy.bottom - rcBuddy.top) / 2 + rcSelf.top;
968         }
969
970         SetWindowPos (infoPtr->hwndBuddyLA, 0, x, y, 0, 0,
971                       SWP_NOZORDER | SWP_NOSIZE);
972     }
973
974
975     /* align buddy right or below */
976     if (infoPtr->hwndBuddyRB) {
977         GetWindowRect (infoPtr->hwndBuddyRB, &rcBuddy);
978         MapWindowPoints (HWND_DESKTOP, hwndParent, (LPPOINT)&rcBuddy, 2);
979
980         if (dwStyle & TBS_VERT) {
981             x = (infoPtr->rcChannel.right + infoPtr->rcChannel.left) / 2 -
982                 (rcBuddy.right - rcBuddy.left) / 2 + rcSelf.left;
983             y = rcSelf.bottom;
984         }
985         else {
986             x = rcSelf.right;
987             y = (infoPtr->rcChannel.bottom + infoPtr->rcChannel.top) / 2 -
988                 (rcBuddy.bottom - rcBuddy.top) / 2 + rcSelf.top;
989         }
990         SetWindowPos (infoPtr->hwndBuddyRB, 0, x, y, 0, 0,
991                       SWP_NOZORDER | SWP_NOSIZE);
992     }
993 }
994
995
996 static LRESULT
997 TRACKBAR_ClearSel (TRACKBAR_INFO *infoPtr, BOOL fRedraw)
998 {
999     infoPtr->lSelMin = 0;
1000     infoPtr->lSelMax = 0;
1001     infoPtr->flags |= TB_SELECTIONCHANGED;
1002
1003     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1004
1005     return 0;
1006 }
1007
1008
1009 static LRESULT
1010 TRACKBAR_ClearTics (TRACKBAR_INFO *infoPtr, BOOL fRedraw)
1011 {
1012     if (infoPtr->tics) {
1013         Free (infoPtr->tics);
1014         infoPtr->tics = NULL;
1015         infoPtr->uNumTics = 0;
1016     }
1017
1018     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1019
1020     return 0;
1021 }
1022
1023
1024 static inline LRESULT
1025 TRACKBAR_GetChannelRect (const TRACKBAR_INFO *infoPtr, LPRECT lprc)
1026 {
1027     if (lprc == NULL) return 0;
1028
1029     lprc->left   = infoPtr->rcChannel.left;
1030     lprc->right  = infoPtr->rcChannel.right;
1031     lprc->bottom = infoPtr->rcChannel.bottom;
1032     lprc->top    = infoPtr->rcChannel.top;
1033
1034     return 0;
1035 }
1036
1037
1038 static inline LONG
1039 TRACKBAR_GetNumTics (const TRACKBAR_INFO *infoPtr)
1040 {
1041     if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_NOTICKS)
1042         return 0;
1043
1044     if(infoPtr->uNumTics == 0)
1045         return 2;
1046     else
1047         return infoPtr->uNumTics + 1;
1048 }
1049
1050
1051 static int comp_tics (const void *ap, const void *bp)
1052 {
1053     const DWORD a = *(const DWORD *)ap;
1054     const DWORD b = *(const DWORD *)bp;
1055
1056     TRACE("(a=%d, b=%d)\n", a, b);
1057     if (a < b) return -1;
1058     if (a > b) return 1;
1059     return 0;
1060 }
1061
1062
1063 static inline LONG
1064 TRACKBAR_GetTic (const TRACKBAR_INFO *infoPtr, INT iTic)
1065 {
1066     if ((iTic < 0) || (iTic >= infoPtr->uNumTics) || !infoPtr->tics)
1067         return -1;
1068
1069     qsort(infoPtr->tics, infoPtr->uNumTics, sizeof(DWORD), comp_tics);
1070     return infoPtr->tics[iTic];
1071 }
1072
1073
1074 static inline LONG
1075 TRACKBAR_GetTicPos (const TRACKBAR_INFO *infoPtr, INT iTic)
1076 {
1077     LONG range, width, pos, tic;
1078     int offsetthumb;
1079
1080     if ((iTic < 0) || (iTic >= infoPtr->uNumTics) || !infoPtr->tics)
1081         return -1;
1082
1083     tic   = TRACKBAR_GetTic (infoPtr, iTic);
1084     range = infoPtr->lRangeMax - infoPtr->lRangeMin;
1085     if (range <= 0) range = 1;
1086     offsetthumb = (infoPtr->rcThumb.right - infoPtr->rcThumb.left)/2;
1087     width = infoPtr->rcChannel.right - infoPtr->rcChannel.left - offsetthumb*2;
1088     pos   = infoPtr->rcChannel.left + offsetthumb + (width * tic) / range;
1089
1090     return pos;
1091 }
1092
1093
1094 static HWND
1095 TRACKBAR_SetBuddy (TRACKBAR_INFO *infoPtr, BOOL fLocation, HWND hwndBuddy)
1096 {
1097     HWND hwndTemp;
1098
1099     if (fLocation) {
1100         /* buddy is left or above */
1101         hwndTemp = infoPtr->hwndBuddyLA;
1102         infoPtr->hwndBuddyLA = hwndBuddy;
1103     }
1104     else {
1105         /* buddy is right or below */
1106         hwndTemp = infoPtr->hwndBuddyRB;
1107         infoPtr->hwndBuddyRB = hwndBuddy;
1108     }
1109
1110     TRACKBAR_AlignBuddies (infoPtr);
1111
1112     return hwndTemp;
1113 }
1114
1115
1116 static inline LONG
1117 TRACKBAR_SetLineSize (TRACKBAR_INFO *infoPtr, LONG lLineSize)
1118 {
1119     LONG lTemp = infoPtr->lLineSize;
1120
1121     infoPtr->lLineSize = lLineSize;
1122
1123     return lTemp;
1124 }
1125
1126
1127 static inline LONG
1128 TRACKBAR_SetPageSize (TRACKBAR_INFO *infoPtr, LONG lPageSize)
1129 {
1130     LONG lTemp = infoPtr->lPageSize;
1131
1132     infoPtr->lPageSize = lPageSize;
1133
1134     return lTemp;
1135 }
1136
1137
1138 static inline LRESULT
1139 TRACKBAR_SetPos (TRACKBAR_INFO *infoPtr, BOOL fPosition, LONG lPosition)
1140 {
1141     LONG oldPos = infoPtr->lPos;
1142     infoPtr->lPos = lPosition;
1143
1144     if (infoPtr->lPos < infoPtr->lRangeMin)
1145         infoPtr->lPos = infoPtr->lRangeMin;
1146
1147     if (infoPtr->lPos > infoPtr->lRangeMax)
1148         infoPtr->lPos = infoPtr->lRangeMax;
1149     infoPtr->flags |= TB_THUMBPOSCHANGED;
1150
1151     if (fPosition) TRACKBAR_InvalidateThumbMove(infoPtr, oldPos, lPosition);
1152
1153     return 0;
1154 }
1155
1156
1157 static inline LRESULT
1158 TRACKBAR_SetRange (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lRange)
1159 {
1160     infoPtr->lRangeMin = (SHORT)LOWORD(lRange);
1161     infoPtr->lRangeMax = (SHORT)HIWORD(lRange);
1162
1163     if (infoPtr->lPos < infoPtr->lRangeMin) {
1164         infoPtr->lPos = infoPtr->lRangeMin;
1165         infoPtr->flags |= TB_THUMBPOSCHANGED;
1166     }
1167
1168     if (infoPtr->lPos > infoPtr->lRangeMax) {
1169         infoPtr->lPos = infoPtr->lRangeMax;
1170         infoPtr->flags |= TB_THUMBPOSCHANGED;
1171     }
1172
1173     infoPtr->lPageSize = (infoPtr->lRangeMax - infoPtr->lRangeMin) / 5;
1174     if (infoPtr->lPageSize == 0) infoPtr->lPageSize = 1;
1175
1176     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1177
1178     return 0;
1179 }
1180
1181
1182 static inline LRESULT
1183 TRACKBAR_SetRangeMax (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lMax)
1184 {
1185     infoPtr->lRangeMax = lMax;
1186     if (infoPtr->lPos > infoPtr->lRangeMax) {
1187         infoPtr->lPos = infoPtr->lRangeMax;
1188         infoPtr->flags |= TB_THUMBPOSCHANGED;
1189     }
1190
1191     infoPtr->lPageSize = (infoPtr->lRangeMax - infoPtr->lRangeMin) / 5;
1192     if (infoPtr->lPageSize == 0) infoPtr->lPageSize = 1;
1193
1194     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1195
1196     return 0;
1197 }
1198
1199
1200 static inline LRESULT
1201 TRACKBAR_SetRangeMin (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lMin)
1202 {
1203     infoPtr->lRangeMin = lMin;
1204     if (infoPtr->lPos < infoPtr->lRangeMin) {
1205         infoPtr->lPos = infoPtr->lRangeMin;
1206         infoPtr->flags |= TB_THUMBPOSCHANGED;
1207     }
1208
1209     infoPtr->lPageSize = (infoPtr->lRangeMax - infoPtr->lRangeMin) / 5;
1210     if (infoPtr->lPageSize == 0) infoPtr->lPageSize = 1;
1211
1212     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1213
1214     return 0;
1215 }
1216
1217
1218 static inline LRESULT
1219 TRACKBAR_SetSel (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lSel)
1220 {
1221     if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_ENABLESELRANGE)){
1222         infoPtr->lSelMin = 0;
1223         infoPtr->lSelMax = 0;
1224         return 0;
1225     }
1226
1227     infoPtr->lSelMin = (SHORT)LOWORD(lSel);
1228     infoPtr->lSelMax = (SHORT)HIWORD(lSel);
1229     infoPtr->flags |= TB_SELECTIONCHANGED;
1230
1231     if (infoPtr->lSelMin < infoPtr->lRangeMin)
1232         infoPtr->lSelMin = infoPtr->lRangeMin;
1233     if (infoPtr->lSelMax > infoPtr->lRangeMax)
1234         infoPtr->lSelMax = infoPtr->lRangeMax;
1235
1236     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1237
1238     return 0;
1239 }
1240
1241
1242 static inline LRESULT
1243 TRACKBAR_SetSelEnd (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lEnd)
1244 {
1245     if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_ENABLESELRANGE)){
1246         infoPtr->lSelMax = 0;
1247         return 0;
1248     }
1249
1250     infoPtr->lSelMax = lEnd;
1251     infoPtr->flags |= TB_SELECTIONCHANGED;
1252
1253     if (infoPtr->lSelMax > infoPtr->lRangeMax)
1254         infoPtr->lSelMax = infoPtr->lRangeMax;
1255
1256     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1257
1258     return 0;
1259 }
1260
1261
1262 static inline LRESULT
1263 TRACKBAR_SetSelStart (TRACKBAR_INFO *infoPtr, BOOL fRedraw, LONG lStart)
1264 {
1265     if (!(GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_ENABLESELRANGE)){
1266         infoPtr->lSelMin = 0;
1267         return 0;
1268     }
1269
1270     infoPtr->lSelMin = lStart;
1271     infoPtr->flags  |=TB_SELECTIONCHANGED;
1272
1273     if (infoPtr->lSelMin < infoPtr->lRangeMin)
1274         infoPtr->lSelMin = infoPtr->lRangeMin;
1275
1276     if (fRedraw) TRACKBAR_InvalidateAll(infoPtr);
1277
1278     return 0;
1279 }
1280
1281
1282 static inline LRESULT
1283 TRACKBAR_SetThumbLength (TRACKBAR_INFO *infoPtr, UINT iLength)
1284 {
1285     if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_FIXEDLENGTH) {
1286         infoPtr->uThumbLen = iLength;
1287         infoPtr->flags |= TB_THUMBSIZECHANGED;
1288         InvalidateRect (infoPtr->hwndSelf, &infoPtr->rcThumb, FALSE);
1289     }
1290
1291     return 0;
1292 }
1293
1294
1295 static inline LRESULT
1296 TRACKBAR_SetTic (TRACKBAR_INFO *infoPtr, LONG lPos)
1297 {
1298     if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_AUTOTICKS)
1299         return FALSE;
1300
1301     if ((lPos < infoPtr->lRangeMin) || (lPos> infoPtr->lRangeMax))
1302         return FALSE;
1303
1304     TRACE("lPos=%d\n", lPos);
1305
1306     infoPtr->uNumTics++;
1307     infoPtr->tics=ReAlloc( infoPtr->tics,
1308                                     (infoPtr->uNumTics)*sizeof (DWORD));
1309     if (!infoPtr->tics) {
1310         infoPtr->uNumTics = 0;
1311         notify(infoPtr, NM_OUTOFMEMORY);
1312         return FALSE;
1313     }
1314     infoPtr->tics[infoPtr->uNumTics-1] = lPos;
1315
1316     TRACKBAR_InvalidateAll(infoPtr);
1317
1318     return TRUE;
1319 }
1320
1321
1322 static inline LRESULT
1323 TRACKBAR_SetTicFreq (TRACKBAR_INFO *infoPtr, WORD wFreq)
1324 {
1325     if (GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE) & TBS_AUTOTICKS) {
1326         infoPtr->uTicFreq = wFreq;
1327         TRACKBAR_RecalculateTics (infoPtr);
1328         TRACKBAR_InvalidateAll(infoPtr);
1329     }
1330
1331     return 0;
1332 }
1333
1334
1335 static inline INT
1336 TRACKBAR_SetTipSide (TRACKBAR_INFO *infoPtr, INT fLocation)
1337 {
1338     INT fTemp = infoPtr->fLocation;
1339
1340     infoPtr->fLocation = fLocation;
1341
1342     return fTemp;
1343 }
1344
1345
1346 static inline LRESULT
1347 TRACKBAR_SetToolTips (TRACKBAR_INFO *infoPtr, HWND hwndTT)
1348 {
1349     infoPtr->hwndToolTip = hwndTT;
1350
1351     return 0;
1352 }
1353
1354
1355 static inline BOOL
1356 TRACKBAR_SetUnicodeFormat (TRACKBAR_INFO *infoPtr, BOOL fUnicode)
1357 {
1358     BOOL bTemp = infoPtr->bUnicode;
1359
1360     infoPtr->bUnicode = fUnicode;
1361
1362     return bTemp;
1363 }
1364
1365
1366 static LRESULT
1367 TRACKBAR_InitializeThumb (TRACKBAR_INFO *infoPtr)
1368 {
1369     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
1370     RECT rect;
1371     int clientWidth, clientMetric;
1372
1373     /* initial thumb length */
1374     clientMetric = (dwStyle & TBS_ENABLESELRANGE) ? 23 : 21;
1375     GetClientRect(infoPtr->hwndSelf,&rect);
1376     if (dwStyle & TBS_VERT) {
1377         clientWidth = rect.right - rect.left;
1378     } else {
1379         clientWidth = rect.bottom - rect.top;
1380     }
1381     if (clientWidth >= clientMetric)
1382         infoPtr->uThumbLen = clientMetric;
1383     else
1384         infoPtr->uThumbLen = clientWidth > 9 ? clientWidth - 6 : 4;
1385
1386     TRACKBAR_CalcChannel (infoPtr);
1387     TRACKBAR_UpdateThumb (infoPtr);
1388     infoPtr->flags &= ~TB_SELECTIONCHANGED;
1389
1390     return 0;
1391 }
1392
1393
1394 static LRESULT
1395 TRACKBAR_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1396 {
1397     TRACKBAR_INFO *infoPtr;
1398     DWORD dwStyle;
1399
1400     infoPtr = (TRACKBAR_INFO *)Alloc (sizeof(TRACKBAR_INFO));
1401     if (!infoPtr) return -1;
1402     SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1403
1404     /* set default values */
1405     infoPtr->hwndSelf  = hwnd;
1406     infoPtr->lRangeMin = 0;
1407     infoPtr->lRangeMax = 100;
1408     infoPtr->lLineSize = 1;
1409     infoPtr->lPageSize = 20;
1410     infoPtr->lSelMin   = 0;
1411     infoPtr->lSelMax   = 0;
1412     infoPtr->lPos      = 0;
1413     infoPtr->fLocation = -1;
1414     infoPtr->uNumTics  = 0;    /* start and end tic are not included in count*/
1415     infoPtr->uTicFreq  = 1;
1416     infoPtr->tics      = NULL;
1417     infoPtr->hwndNotify= lpcs->hwndParent;
1418
1419     TRACKBAR_InitializeThumb (infoPtr);
1420
1421     dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
1422
1423     /* Create tooltip control */
1424     if (dwStyle & TBS_TOOLTIPS) {
1425
1426         infoPtr->hwndToolTip =
1427             CreateWindowExW (0, TOOLTIPS_CLASSW, NULL, WS_POPUP,
1428                              CW_USEDEFAULT, CW_USEDEFAULT,
1429                              CW_USEDEFAULT, CW_USEDEFAULT,
1430                              hwnd, 0, 0, 0);
1431
1432         if (infoPtr->hwndToolTip) {
1433             TTTOOLINFOW ti;         
1434             ZeroMemory (&ti, sizeof(ti));
1435             ti.cbSize   = sizeof(ti);
1436             ti.uFlags   = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
1437             ti.hwnd     = hwnd;
1438
1439             SendMessageW (infoPtr->hwndToolTip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
1440          }
1441     }
1442     
1443     OpenThemeData (hwnd, themeClass);
1444
1445     return 0;
1446 }
1447
1448
1449 static LRESULT
1450 TRACKBAR_Destroy (TRACKBAR_INFO *infoPtr)
1451 {
1452     /* delete tooltip control */
1453     if (infoPtr->hwndToolTip)
1454         DestroyWindow (infoPtr->hwndToolTip);
1455
1456     Free (infoPtr);
1457     SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
1458     CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
1459     return 0;
1460 }
1461
1462
1463 static LRESULT
1464 TRACKBAR_KillFocus (TRACKBAR_INFO *infoPtr, HWND hwndGetFocus)
1465 {
1466     TRACE("\n");
1467     infoPtr->bFocussed = FALSE;
1468     TRACKBAR_InvalidateAll(infoPtr);
1469
1470     return 0;
1471 }
1472
1473 static LRESULT
1474 TRACKBAR_LButtonDown (TRACKBAR_INFO *infoPtr, DWORD fwKeys, INT x, INT y)
1475 {
1476     POINT clickPoint;
1477
1478     clickPoint.x = x;
1479     clickPoint.y = y;
1480
1481     SetFocus(infoPtr->hwndSelf);
1482
1483     if (PtInRect(&infoPtr->rcThumb, clickPoint)) {
1484         infoPtr->flags |= TB_DRAG_MODE;
1485         SetCapture (infoPtr->hwndSelf);
1486         TRACKBAR_UpdateToolTip (infoPtr);
1487         TRACKBAR_ActivateToolTip (infoPtr, TRUE);
1488         TRACKBAR_InvalidateThumb(infoPtr, infoPtr->lPos);
1489     } else {
1490         LONG dir = TRACKBAR_GetAutoPageDirection(infoPtr, clickPoint);
1491         if (dir == 0) return 0;
1492         infoPtr->flags |= (dir < 0) ? TB_AUTO_PAGE_LEFT : TB_AUTO_PAGE_RIGHT;
1493         TRACKBAR_AutoPage (infoPtr, clickPoint);
1494         SetCapture (infoPtr->hwndSelf);
1495         SetTimer(infoPtr->hwndSelf, TB_REFRESH_TIMER, TB_REFRESH_DELAY, 0);
1496     }
1497
1498     return 0;
1499 }
1500
1501
1502 static LRESULT
1503 TRACKBAR_LButtonUp (TRACKBAR_INFO *infoPtr, DWORD fwKeys, INT x, INT y)
1504 {
1505     if (infoPtr->flags & TB_DRAG_MODE) {
1506         notify_with_scroll (infoPtr, TB_THUMBPOSITION | (infoPtr->lPos<<16));
1507         notify_with_scroll (infoPtr, TB_ENDTRACK);
1508         infoPtr->flags &= ~TB_DRAG_MODE;
1509         ReleaseCapture ();
1510         notify(infoPtr, NM_RELEASEDCAPTURE);
1511         TRACKBAR_ActivateToolTip(infoPtr, FALSE);
1512         TRACKBAR_InvalidateThumb(infoPtr, infoPtr->lPos);
1513     }
1514     if (infoPtr->flags & TB_AUTO_PAGE) {
1515         KillTimer (infoPtr->hwndSelf, TB_REFRESH_TIMER);
1516         infoPtr->flags &= ~TB_AUTO_PAGE;
1517         notify_with_scroll (infoPtr, TB_ENDTRACK);
1518         ReleaseCapture ();
1519         notify(infoPtr, NM_RELEASEDCAPTURE);
1520     }
1521
1522     return 0;
1523 }
1524
1525
1526 static LRESULT
1527 TRACKBAR_CaptureChanged (const TRACKBAR_INFO *infoPtr)
1528 {
1529     notify_with_scroll (infoPtr, TB_ENDTRACK);
1530     return 0;
1531 }
1532
1533
1534 static LRESULT
1535 TRACKBAR_Paint (TRACKBAR_INFO *infoPtr, HDC hdc)
1536 {
1537     if (hdc) {
1538         TRACKBAR_Refresh(infoPtr, hdc);
1539     } else {
1540         PAINTSTRUCT ps;
1541         hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1542         TRACKBAR_Refresh (infoPtr, hdc);
1543         EndPaint (infoPtr->hwndSelf, &ps);
1544     }
1545
1546     return 0;
1547 }
1548
1549
1550 static LRESULT
1551 TRACKBAR_SetFocus (TRACKBAR_INFO *infoPtr, HWND hwndLoseFocus)
1552 {
1553     TRACE("\n");
1554     infoPtr->bFocussed = TRUE;
1555     TRACKBAR_InvalidateAll(infoPtr);
1556
1557     return 0;
1558 }
1559
1560
1561 static LRESULT
1562 TRACKBAR_Size (TRACKBAR_INFO *infoPtr, DWORD fwSizeType, INT nWidth, INT nHeight)
1563 {
1564     TRACKBAR_InitializeThumb (infoPtr);
1565     TRACKBAR_AlignBuddies (infoPtr);
1566
1567     return 0;
1568 }
1569
1570
1571 static LRESULT
1572 TRACKBAR_Timer (TRACKBAR_INFO *infoPtr, INT wTimerID, const TIMERPROC *tmrpc)
1573 {
1574     if (infoPtr->flags & TB_AUTO_PAGE) {
1575         POINT pt;
1576         if (GetCursorPos(&pt))
1577             if (ScreenToClient(infoPtr->hwndSelf, &pt))
1578                 TRACKBAR_AutoPage(infoPtr, pt);
1579     }
1580     return 0;
1581 }
1582
1583
1584 /* update theme after a WM_THEMECHANGED message */
1585 static LRESULT theme_changed (const TRACKBAR_INFO* infoPtr)
1586 {
1587     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
1588     CloseThemeData (theme);
1589     theme = OpenThemeData (infoPtr->hwndSelf, themeClass);
1590     return 0;
1591 }
1592
1593
1594 static LRESULT
1595 TRACKBAR_MouseMove (TRACKBAR_INFO *infoPtr, DWORD fwKeys, INT x, INT y)
1596 {
1597     DWORD dwStyle = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
1598     INT clickPlace = (dwStyle & TBS_VERT) ? y : x;
1599     LONG dragPos, oldPos = infoPtr->lPos;
1600
1601     TRACE("(x=%d. y=%d)\n", x, y);
1602
1603     if (infoPtr->flags & TB_AUTO_PAGE) {
1604         POINT pt;
1605         pt.x = x;
1606         pt.y = y;
1607         TRACKBAR_AutoPage (infoPtr, pt);
1608         return TRUE;
1609     }
1610
1611     if (!(infoPtr->flags & TB_DRAG_MODE)) 
1612     {
1613         if (GetWindowTheme (infoPtr->hwndSelf))
1614         {
1615             DWORD oldFlags = infoPtr->flags;
1616             POINT pt;
1617             pt.x = x;
1618             pt.y = y;
1619             if (PtInRect (&infoPtr->rcThumb, pt))
1620             {
1621                 TRACKMOUSEEVENT tme;
1622                 tme.cbSize = sizeof( tme );
1623                 tme.dwFlags = TME_LEAVE;
1624                 tme.hwndTrack = infoPtr->hwndSelf;
1625                 TrackMouseEvent( &tme );
1626                 infoPtr->flags |= TB_THUMB_HOT;
1627             }
1628             else
1629             {
1630                 TRACKMOUSEEVENT tme;
1631                 tme.cbSize = sizeof( tme );
1632                 tme.dwFlags = TME_CANCEL;
1633                 tme.hwndTrack = infoPtr->hwndSelf;
1634                 TrackMouseEvent( &tme );
1635                 infoPtr->flags &= ~TB_THUMB_HOT; 
1636             }
1637             if (oldFlags != infoPtr->flags) InvalidateRect (infoPtr->hwndSelf, &infoPtr->rcThumb, FALSE);
1638         }
1639         return TRUE;
1640     }
1641
1642     dragPos = TRACKBAR_ConvertPlaceToPosition (infoPtr, clickPlace,
1643                                                dwStyle & TBS_VERT);
1644     if (dragPos == oldPos) return TRUE;
1645
1646     infoPtr->lPos = dragPos;
1647
1648     infoPtr->flags |= TB_THUMBPOSCHANGED;
1649     notify_with_scroll (infoPtr, TB_THUMBTRACK | (infoPtr->lPos<<16));
1650
1651
1652     TRACKBAR_InvalidateThumbMove(infoPtr, oldPos, dragPos);
1653     UpdateWindow (infoPtr->hwndSelf);
1654
1655     return TRUE;
1656 }
1657
1658 static BOOL
1659 TRACKBAR_KeyDown (TRACKBAR_INFO *infoPtr, INT nVirtKey, DWORD lKeyData)
1660 {
1661     DWORD style = GetWindowLongW (infoPtr->hwndSelf, GWL_STYLE);
1662     BOOL downIsLeft = style & TBS_DOWNISLEFT;
1663     BOOL vert = style & TBS_VERT;
1664     LONG pos = infoPtr->lPos;
1665
1666     TRACE("%x\n", nVirtKey);
1667
1668     switch (nVirtKey) {
1669     case VK_UP:
1670         if (!vert && downIsLeft) TRACKBAR_LineDown(infoPtr);
1671         else TRACKBAR_LineUp(infoPtr);
1672         break;
1673     case VK_LEFT:
1674         if (vert && downIsLeft) TRACKBAR_LineDown(infoPtr);
1675         else TRACKBAR_LineUp(infoPtr);
1676         break;
1677     case VK_DOWN:
1678         if (!vert && downIsLeft) TRACKBAR_LineUp(infoPtr);
1679         else TRACKBAR_LineDown(infoPtr);
1680         break;
1681     case VK_RIGHT:
1682         if (vert && downIsLeft) TRACKBAR_LineUp(infoPtr);
1683         else TRACKBAR_LineDown(infoPtr);
1684         break;
1685     case VK_NEXT:
1686         if (!vert && downIsLeft) TRACKBAR_PageUp(infoPtr);
1687         else TRACKBAR_PageDown(infoPtr);
1688         break;
1689     case VK_PRIOR:
1690         if (!vert && downIsLeft) TRACKBAR_PageDown(infoPtr);
1691         else TRACKBAR_PageUp(infoPtr);
1692         break;
1693     case VK_HOME:
1694         if (infoPtr->lPos == infoPtr->lRangeMin) return FALSE;
1695         infoPtr->lPos = infoPtr->lRangeMin;
1696         notify_with_scroll (infoPtr, TB_TOP);
1697         break;
1698     case VK_END:
1699         if (infoPtr->lPos == infoPtr->lRangeMax) return FALSE;
1700         infoPtr->lPos = infoPtr->lRangeMax;
1701         notify_with_scroll (infoPtr, TB_BOTTOM);
1702         break;
1703     }
1704
1705     if (pos != infoPtr->lPos) {
1706         infoPtr->flags |=TB_THUMBPOSCHANGED;
1707         TRACKBAR_InvalidateThumbMove (infoPtr, pos, infoPtr->lPos);
1708     }
1709
1710     return TRUE;
1711 }
1712
1713
1714 static inline BOOL
1715 TRACKBAR_KeyUp (const TRACKBAR_INFO *infoPtr, INT nVirtKey, DWORD lKeyData)
1716 {
1717     switch (nVirtKey) {
1718     case VK_LEFT:
1719     case VK_UP:
1720     case VK_RIGHT:
1721     case VK_DOWN:
1722     case VK_NEXT:
1723     case VK_PRIOR:
1724     case VK_HOME:
1725     case VK_END:
1726         notify_with_scroll (infoPtr, TB_ENDTRACK);
1727     }
1728     return TRUE;
1729 }
1730
1731
1732 static LRESULT WINAPI
1733 TRACKBAR_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1734 {
1735     TRACKBAR_INFO *infoPtr = (TRACKBAR_INFO *)GetWindowLongPtrW (hwnd, 0);
1736
1737     TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1738
1739     if (!infoPtr && (uMsg != WM_CREATE))
1740         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1741
1742     switch (uMsg)
1743     {
1744     case TBM_CLEARSEL:
1745         return TRACKBAR_ClearSel (infoPtr, (BOOL)wParam);
1746
1747     case TBM_CLEARTICS:
1748         return TRACKBAR_ClearTics (infoPtr, (BOOL)wParam);
1749
1750     case TBM_GETBUDDY:
1751         return (LRESULT)(wParam ? infoPtr->hwndBuddyLA : infoPtr->hwndBuddyRB);
1752
1753     case TBM_GETCHANNELRECT:
1754         return TRACKBAR_GetChannelRect (infoPtr, (LPRECT)lParam);
1755
1756     case TBM_GETLINESIZE:
1757         return infoPtr->lLineSize;
1758
1759     case TBM_GETNUMTICS:
1760         return TRACKBAR_GetNumTics (infoPtr);
1761
1762     case TBM_GETPAGESIZE:
1763         return infoPtr->lPageSize;
1764
1765     case TBM_GETPOS:
1766         return infoPtr->lPos;
1767
1768     case TBM_GETPTICS:
1769         return (LRESULT)infoPtr->tics;
1770
1771     case TBM_GETRANGEMAX:
1772         return infoPtr->lRangeMax;
1773
1774     case TBM_GETRANGEMIN:
1775         return infoPtr->lRangeMin;
1776
1777     case TBM_GETSELEND:
1778         return infoPtr->lSelMax;
1779
1780     case TBM_GETSELSTART:
1781         return infoPtr->lSelMin;
1782
1783     case TBM_GETTHUMBLENGTH:
1784         return infoPtr->uThumbLen;
1785
1786     case TBM_GETTHUMBRECT:
1787         return CopyRect((LPRECT)lParam, &infoPtr->rcThumb);
1788
1789     case TBM_GETTIC:
1790         return TRACKBAR_GetTic (infoPtr, (INT)wParam);
1791
1792     case TBM_GETTICPOS:
1793         return TRACKBAR_GetTicPos (infoPtr, (INT)wParam);
1794
1795     case TBM_GETTOOLTIPS:
1796         return (LRESULT)infoPtr->hwndToolTip;
1797
1798     case TBM_GETUNICODEFORMAT:
1799         return infoPtr->bUnicode;
1800
1801     case TBM_SETBUDDY:
1802         return (LRESULT) TRACKBAR_SetBuddy(infoPtr, (BOOL)wParam, (HWND)lParam);
1803
1804     case TBM_SETLINESIZE:
1805         return TRACKBAR_SetLineSize (infoPtr, (LONG)lParam);
1806
1807     case TBM_SETPAGESIZE:
1808         return TRACKBAR_SetPageSize (infoPtr, (LONG)lParam);
1809
1810     case TBM_SETPOS:
1811         return TRACKBAR_SetPos (infoPtr, (BOOL)wParam, (LONG)lParam);
1812
1813     case TBM_SETRANGE:
1814         return TRACKBAR_SetRange (infoPtr, (BOOL)wParam, (LONG)lParam);
1815
1816     case TBM_SETRANGEMAX:
1817         return TRACKBAR_SetRangeMax (infoPtr, (BOOL)wParam, (LONG)lParam);
1818
1819     case TBM_SETRANGEMIN:
1820         return TRACKBAR_SetRangeMin (infoPtr, (BOOL)wParam, (LONG)lParam);
1821
1822     case TBM_SETSEL:
1823         return TRACKBAR_SetSel (infoPtr, (BOOL)wParam, (LONG)lParam);
1824
1825     case TBM_SETSELEND:
1826         return TRACKBAR_SetSelEnd (infoPtr, (BOOL)wParam, (LONG)lParam);
1827
1828     case TBM_SETSELSTART:
1829         return TRACKBAR_SetSelStart (infoPtr, (BOOL)wParam, (LONG)lParam);
1830
1831     case TBM_SETTHUMBLENGTH:
1832         return TRACKBAR_SetThumbLength (infoPtr, (UINT)wParam);
1833
1834     case TBM_SETTIC:
1835         return TRACKBAR_SetTic (infoPtr, (LONG)lParam);
1836
1837     case TBM_SETTICFREQ:
1838         return TRACKBAR_SetTicFreq (infoPtr, (WORD)wParam);
1839
1840     case TBM_SETTIPSIDE:
1841         return TRACKBAR_SetTipSide (infoPtr, (INT)wParam);
1842
1843     case TBM_SETTOOLTIPS:
1844         return TRACKBAR_SetToolTips (infoPtr, (HWND)wParam);
1845
1846     case TBM_SETUNICODEFORMAT:
1847         return TRACKBAR_SetUnicodeFormat (infoPtr, (BOOL)wParam);
1848
1849
1850     case WM_CAPTURECHANGED:
1851         return TRACKBAR_CaptureChanged (infoPtr);
1852
1853     case WM_CREATE:
1854         return TRACKBAR_Create (hwnd, (LPCREATESTRUCTW)lParam);
1855
1856     case WM_DESTROY:
1857         return TRACKBAR_Destroy (infoPtr);
1858
1859 /*      case WM_ENABLE: */
1860
1861     case WM_ERASEBKGND:
1862         return 0;
1863
1864     case WM_GETDLGCODE:
1865         return DLGC_WANTARROWS;
1866
1867     case WM_KEYDOWN:
1868         return TRACKBAR_KeyDown (infoPtr, (INT)wParam, (DWORD)lParam);
1869
1870     case WM_KEYUP:
1871         return TRACKBAR_KeyUp (infoPtr, (INT)wParam, (DWORD)lParam);
1872
1873     case WM_KILLFOCUS:
1874         return TRACKBAR_KillFocus (infoPtr, (HWND)wParam);
1875
1876     case WM_LBUTTONDOWN:
1877         return TRACKBAR_LButtonDown (infoPtr, wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1878
1879     case WM_LBUTTONUP:
1880         return TRACKBAR_LButtonUp (infoPtr, wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1881
1882     case WM_MOUSELEAVE:
1883         infoPtr->flags &= ~TB_THUMB_HOT; 
1884         InvalidateRect (infoPtr->hwndSelf, &infoPtr->rcThumb, FALSE);
1885         return 0;
1886     
1887     case WM_MOUSEMOVE:
1888         return TRACKBAR_MouseMove (infoPtr, wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1889
1890     case WM_PRINTCLIENT:
1891     case WM_PAINT:
1892         return TRACKBAR_Paint (infoPtr, (HDC)wParam);
1893
1894     case WM_SETFOCUS:
1895         return TRACKBAR_SetFocus (infoPtr, (HWND)wParam);
1896
1897     case WM_SIZE:
1898         return TRACKBAR_Size (infoPtr, wParam, LOWORD(lParam), HIWORD(lParam));
1899
1900     case WM_THEMECHANGED:
1901         return theme_changed (infoPtr);
1902
1903     case WM_TIMER:
1904         return TRACKBAR_Timer (infoPtr, (INT)wParam, (TIMERPROC *)lParam);
1905
1906     case WM_WININICHANGE:
1907         return TRACKBAR_InitializeThumb (infoPtr);
1908
1909     default:
1910         if ((uMsg >= WM_USER) && (uMsg < WM_APP))
1911             ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
1912         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1913     }
1914 }
1915
1916
1917 void TRACKBAR_Register (void)
1918 {
1919     WNDCLASSW wndClass;
1920
1921     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1922     wndClass.style         = CS_GLOBALCLASS;
1923     wndClass.lpfnWndProc   = TRACKBAR_WindowProc;
1924     wndClass.cbClsExtra    = 0;
1925     wndClass.cbWndExtra    = sizeof(TRACKBAR_INFO *);
1926     wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1927     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1928     wndClass.lpszClassName = TRACKBAR_CLASSW;
1929
1930     RegisterClassW (&wndClass);
1931 }
1932
1933
1934 void TRACKBAR_Unregister (void)
1935 {
1936     UnregisterClassW (TRACKBAR_CLASSW, NULL);
1937 }