comctl32: rebar: Prevent unnecessary triggering of RBBIM_CHILDSIZE changes.
[wine] / dlls / comctl32 / rebar.c
1 /*
2  * Rebar control
3  *
4  * Copyright 1998, 1999 Eric Kohl
5  * Copyright 2007, 2008 Mikolaj Zalewski
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * NOTES
22  *
23  * This code was audited for completeness against the documented features
24  * of Comctl32.dll version 6.0 on Oct. 19, 2004, by Robert Shearman.
25  * 
26  * Unless otherwise noted, we believe this code to be complete, as per
27  * the specification mentioned above.
28  * If you discover missing features or bugs please note them below.
29  *
30  * TODO
31  *   Styles:
32  *   - RBS_DBLCLKTOGGLE
33  *   - RBS_FIXEDORDER
34  *   - RBS_REGISTERDROP
35  *   - RBS_TOOLTIPS
36  *   Messages:
37  *   - RB_BEGINDRAG
38  *   - RB_DRAGMOVE
39  *   - RB_ENDDRAG
40  *   - RB_GETBANDMARGINS
41  *   - RB_GETCOLORSCHEME
42  *   - RB_GETDROPTARGET
43  *   - RB_GETPALETTE
44  *   - RB_SETCOLORSCHEME
45  *   - RB_SETPALETTE
46  *   - RB_SETTOOLTIPS
47  *   - WM_CHARTOITEM
48  *   - WM_LBUTTONDBLCLK
49  *   - WM_MEASUREITEM
50  *   - WM_PALETTECHANGED
51  *   - WM_QUERYNEWPALETTE
52  *   - WM_RBUTTONDOWN
53  *   - WM_RBUTTONUP
54  *   - WM_SYSCOLORCHANGE
55  *   - WM_VKEYTOITEM
56  *   - WM_WININICHANGE
57  *   Notifications:
58  *   - NM_HCHITTEST
59  *   - NM_RELEASEDCAPTURE
60  *   - RBN_AUTOBREAK
61  *   - RBN_GETOBJECT
62  *   - RBN_MINMAX
63  *   Band styles:
64  *   - RBBS_FIXEDBMP
65  *   Native uses (on each draw!!) SM_CYBORDER (or SM_CXBORDER for CCS_VERT)
66  *   to set the size of the separator width (the value SEP_WIDTH_SIZE
67  *   in here). Should be fixed!!
68  */
69
70 /*
71  * Testing: set to 1 to make background brush *always* green
72  */
73 #define GLATESTING 0
74
75 /*
76  * 3. REBAR_MoveChildWindows should have a loop because more than
77  *    one pass (together with the RBN_CHILDSIZEs) is made on
78  *    at least RB_INSERTBAND
79  */
80
81 #include <stdarg.h>
82 #include <stdlib.h>
83 #include <string.h>
84
85 #include "windef.h"
86 #include "winbase.h"
87 #include "wingdi.h"
88 #include "wine/unicode.h"
89 #include "winuser.h"
90 #include "winnls.h"
91 #include "commctrl.h"
92 #include "comctl32.h"
93 #include "uxtheme.h"
94 #include "tmschema.h"
95 #include "wine/debug.h"
96
97 WINE_DEFAULT_DEBUG_CHANNEL(rebar);
98
99 typedef struct
100 {
101     UINT    fStyle;
102     UINT    fMask;
103     COLORREF  clrFore;
104     COLORREF  clrBack;
105     INT     iImage;
106     HWND    hwndChild;
107     UINT    cxMinChild;     /* valid if _CHILDSIZE */
108     UINT    cyMinChild;     /* valid if _CHILDSIZE */
109     UINT    cx;             /* valid if _SIZE */
110     HBITMAP hbmBack;
111     UINT    wID;
112     UINT    cyChild;        /* valid if _CHILDSIZE */
113     UINT    cyMaxChild;     /* valid if _CHILDSIZE */
114     UINT    cyIntegral;     /* valid if _CHILDSIZE */
115     UINT    cxIdeal;
116     LPARAM    lParam;
117     UINT    cxHeader;
118
119     INT     cxEffective;    /* current cx for band */
120     UINT    cyHeader;       /* the height of the header */
121     UINT    cxMinBand;      /* minimum cx for band */
122     UINT    cyMinBand;      /* minimum cy for band */
123
124     UINT    cyRowSoFar;     /* for RBS_VARHEIGHT - the height of the row if it would break on this band (set by _Layout) */
125     INT     iRow;           /* zero-based index of the row this band assigned to */
126     UINT    fStatus;        /* status flags, reset only by _Validate */
127     UINT    fDraw;          /* drawing flags, reset only by _Layout */
128     UINT    uCDret;         /* last return from NM_CUSTOMDRAW */
129     RECT    rcBand;         /* calculated band rectangle - coordinates swapped for CCS_VERT */
130     RECT    rcGripper;      /* calculated gripper rectangle */
131     RECT    rcCapImage;     /* calculated caption image rectangle */
132     RECT    rcCapText;      /* calculated caption text rectangle */
133     RECT    rcChild;        /* calculated child rectangle */
134     RECT    rcChevron;      /* calculated chevron rectangle */
135
136     LPWSTR    lpText;
137     HWND    hwndPrevParent;
138 } REBAR_BAND;
139
140 /* fStatus flags */
141 #define HAS_GRIPPER    0x00000001
142 #define HAS_IMAGE      0x00000002
143 #define HAS_TEXT       0x00000004
144
145 /* fDraw flags */
146 #define DRAW_GRIPPER    0x00000001
147 #define DRAW_IMAGE      0x00000002
148 #define DRAW_TEXT       0x00000004
149 #define DRAW_CHEVRONHOT 0x00000040
150 #define DRAW_CHEVRONPUSHED 0x00000080
151 #define NTF_INVALIDATE  0x01000000
152
153 typedef struct
154 {
155     COLORREF   clrBk;       /* background color */
156     COLORREF   clrText;     /* text color */
157     COLORREF   clrBtnText;  /* system color for BTNTEXT */
158     COLORREF   clrBtnFace;  /* system color for BTNFACE */
159     HIMAGELIST himl;        /* handle to imagelist */
160     UINT     uNumBands;   /* # of bands in rebar (first=0, last=uNumBands-1 */
161     UINT     uNumRows;    /* # of rows of bands (first=1, last=uNumRows */
162     HWND     hwndSelf;    /* handle of REBAR window itself */
163     HWND     hwndToolTip; /* handle to the tool tip control */
164     HWND     hwndNotify;  /* notification window (parent) */
165     HFONT    hDefaultFont;
166     HFONT    hFont;       /* handle to the rebar's font */
167     SIZE     imageSize;   /* image size (image list) */
168     DWORD    dwStyle;     /* window style */
169     DWORD    orgStyle;    /* original style (dwStyle may change) */
170     SIZE     calcSize;    /* calculated rebar size - coordinates swapped for CCS_VERT */
171     BOOL     bUnicode;    /* TRUE if parent wants notify in W format */
172     BOOL     DoRedraw;    /* TRUE to actually draw bands */
173     UINT     fStatus;     /* Status flags (see below)  */
174     HCURSOR  hcurArrow;   /* handle to the arrow cursor */
175     HCURSOR  hcurHorz;    /* handle to the EW cursor */
176     HCURSOR  hcurVert;    /* handle to the NS cursor */
177     HCURSOR  hcurDrag;    /* handle to the drag cursor */
178     INT      iVersion;    /* version number */
179     POINT    dragStart;   /* x,y of button down */
180     POINT    dragNow;     /* x,y of this MouseMove */
181     INT      iOldBand;    /* last band that had the mouse cursor over it */
182     INT      ihitoffset;  /* offset of hotspot from gripper.left */
183     INT      ichevronhotBand; /* last band that had a hot chevron */
184     INT      iGrabbedBand;/* band number of band whose gripper was grabbed */
185
186     REBAR_BAND *bands;      /* pointer to the array of rebar bands */
187 } REBAR_INFO;
188
189 /* fStatus flags */
190 #define BEGIN_DRAG_ISSUED   0x00000001
191 #define SELF_RESIZE         0x00000002
192 #define BAND_NEEDS_REDRAW   0x00000020
193
194 /* used by Windows to mark that the header size has been set by the user and shouldn't be changed */
195 #define RBBS_UNDOC_FIXEDHEADER 0x40000000
196
197 /* ----   REBAR layout constants. Mostly determined by        ---- */
198 /* ----   experiment on WIN 98.                               ---- */
199
200 /* Width (or height) of separators between bands (either horz. or  */
201 /* vert.). True only if RBS_BANDBORDERS is set                     */
202 #define SEP_WIDTH_SIZE  2
203 #define SEP_WIDTH       ((infoPtr->dwStyle & RBS_BANDBORDERS) ? SEP_WIDTH_SIZE : 0)
204
205 /* Blank (background color) space between Gripper (if present)     */
206 /* and next item (image, text, or window). Always present          */
207 #define REBAR_ALWAYS_SPACE  4
208
209 /* Blank (background color) space after Image (if present).        */
210 #define REBAR_POST_IMAGE  2
211
212 /* Blank (background color) space after Text (if present).         */
213 #define REBAR_POST_TEXT  4
214
215 /* Height of vertical gripper in a CCS_VERT rebar.                 */
216 #define GRIPPER_HEIGHT  16
217
218 /* Blank (background color) space before Gripper (if present).     */
219 #define REBAR_PRE_GRIPPER   2
220
221 /* Width (of normal vertical gripper) or height (of horz. gripper) */
222 /* if present.                                                     */
223 #define GRIPPER_WIDTH  3
224
225 /* Width of the chevron button if present */
226 #define CHEVRON_WIDTH  10
227
228 /* the gap between the child and the next band */
229 #define REBAR_POST_CHILD 4
230
231 /* Height of divider for Rebar if not disabled (CCS_NODIVIDER)     */
232 /* either top or bottom                                            */
233 #define REBAR_DIVIDER  2
234
235 /* height of a rebar without a child */
236 #define REBAR_NO_CHILD_HEIGHT 4
237
238 /* minimum vertical height of a normal bar                        */
239 /*   or minimum width of a CCS_VERT bar - from experiment on Win2k */
240 #define REBAR_MINSIZE  23
241
242 /* This is the increment that is used over the band height         */
243 #define REBARSPACE(a)     ((a->fStyle & RBBS_CHILDEDGE) ? 2*REBAR_DIVIDER : 0)
244
245 /* ----   End of REBAR layout constants.                      ---- */
246
247 #define RB_GETBANDINFO_OLD (WM_USER+5) /* obsoleted after IE3, but we have to support it anyway */
248
249 /*  The following define determines if a given band is hidden      */
250 #define HIDDENBAND(a)  (((a)->fStyle & RBBS_HIDDEN) ||   \
251                         ((infoPtr->dwStyle & CCS_VERT) &&         \
252                          ((a)->fStyle & RBBS_NOVERT)))
253
254 #define REBAR_GetInfoPtr(wndPtr) ((REBAR_INFO *)GetWindowLongPtrW (hwnd, 0))
255
256 static LRESULT REBAR_NotifyFormat(REBAR_INFO *infoPtr, LPARAM lParam);
257 static void REBAR_AutoSize(REBAR_INFO *infoPtr, BOOL needsLayout);
258
259 /* "constant values" retrieved when DLL was initialized    */
260 /* FIXME we do this when the classes are registered.       */
261 static UINT mindragx = 0;
262 static UINT mindragy = 0;
263
264 static const char * const band_stylename[] = {
265     "RBBS_BREAK",              /* 0001 */
266     "RBBS_FIXEDSIZE",          /* 0002 */
267     "RBBS_CHILDEDGE",          /* 0004 */
268     "RBBS_HIDDEN",             /* 0008 */
269     "RBBS_NOVERT",             /* 0010 */
270     "RBBS_FIXEDBMP",           /* 0020 */
271     "RBBS_VARIABLEHEIGHT",     /* 0040 */
272     "RBBS_GRIPPERALWAYS",      /* 0080 */
273     "RBBS_NOGRIPPER",          /* 0100 */
274     NULL };
275
276 static const char * const band_maskname[] = {
277     "RBBIM_STYLE",         /*    0x00000001 */
278     "RBBIM_COLORS",        /*    0x00000002 */
279     "RBBIM_TEXT",          /*    0x00000004 */
280     "RBBIM_IMAGE",         /*    0x00000008 */
281     "RBBIM_CHILD",         /*    0x00000010 */
282     "RBBIM_CHILDSIZE",     /*    0x00000020 */
283     "RBBIM_SIZE",          /*    0x00000040 */
284     "RBBIM_BACKGROUND",    /*    0x00000080 */
285     "RBBIM_ID",            /*    0x00000100 */
286     "RBBIM_IDEALSIZE",     /*    0x00000200 */
287     "RBBIM_LPARAM",        /*    0x00000400 */
288     "RBBIM_HEADERSIZE",    /*    0x00000800 */
289     NULL };
290
291
292 static CHAR line[200];
293
294 static const WCHAR themeClass[] = { 'R','e','b','a','r',0 };
295
296 static CHAR *
297 REBAR_FmtStyle( UINT style)
298 {
299     INT i = 0;
300
301     *line = 0;
302     while (band_stylename[i]) {
303         if (style & (1<<i)) {
304             if (*line != 0) strcat(line, " | ");
305             strcat(line, band_stylename[i]);
306         }
307         i++;
308     }
309     return line;
310 }
311
312
313 static CHAR *
314 REBAR_FmtMask( UINT mask)
315 {
316     INT i = 0;
317
318     *line = 0;
319     while (band_maskname[i]) {
320         if (mask & (1<<i)) {
321             if (*line != 0) strcat(line, " | ");
322             strcat(line, band_maskname[i]);
323         }
324         i++;
325     }
326     return line;
327 }
328
329
330 static VOID
331 REBAR_DumpBandInfo(const REBARBANDINFOW *pB)
332 {
333     if( !TRACE_ON(rebar) ) return;
334     TRACE("band info: ");
335     if (pB->fMask & RBBIM_ID)
336         TRACE("ID=%u, ", pB->wID);
337     TRACE("size=%u, child=%p", pB->cbSize, pB->hwndChild);
338     if (pB->fMask & RBBIM_COLORS)
339         TRACE(", clrF=0x%06x, clrB=0x%06x", pB->clrFore, pB->clrBack);
340     TRACE("\n");
341
342     TRACE("band info: mask=0x%08x (%s)\n", pB->fMask, REBAR_FmtMask(pB->fMask));
343     if (pB->fMask & RBBIM_STYLE)
344         TRACE("band info: style=0x%08x (%s)\n", pB->fStyle, REBAR_FmtStyle(pB->fStyle));
345     if (pB->fMask & (RBBIM_SIZE | RBBIM_IDEALSIZE | RBBIM_HEADERSIZE | RBBIM_LPARAM )) {
346         TRACE("band info:");
347         if (pB->fMask & RBBIM_SIZE)
348             TRACE(" cx=%u", pB->cx);
349         if (pB->fMask & RBBIM_IDEALSIZE)
350             TRACE(" xIdeal=%u", pB->cxIdeal);
351         if (pB->fMask & RBBIM_HEADERSIZE)
352             TRACE(" xHeader=%u", pB->cxHeader);
353         if (pB->fMask & RBBIM_LPARAM)
354             TRACE(" lParam=0x%08lx", pB->lParam);
355         TRACE("\n");
356     }
357     if (pB->fMask & RBBIM_CHILDSIZE)
358         TRACE("band info: xMin=%u, yMin=%u, yChild=%u, yMax=%u, yIntgl=%u\n",
359               pB->cxMinChild,
360               pB->cyMinChild, pB->cyChild, pB->cyMaxChild, pB->cyIntegral);
361 }
362
363 static VOID
364 REBAR_DumpBand (const REBAR_INFO *iP)
365 {
366     REBAR_BAND *pB;
367     UINT i;
368
369     if(! TRACE_ON(rebar) ) return;
370
371     TRACE("hwnd=%p: color=%08x/%08x, bands=%u, rows=%u, cSize=%d,%d\n",
372           iP->hwndSelf, iP->clrText, iP->clrBk, iP->uNumBands, iP->uNumRows,
373           iP->calcSize.cx, iP->calcSize.cy);
374     TRACE("hwnd=%p: flags=%08x, dragStart=%d,%d, dragNow=%d,%d, iGrabbedBand=%d\n",
375           iP->hwndSelf, iP->fStatus, iP->dragStart.x, iP->dragStart.y,
376           iP->dragNow.x, iP->dragNow.y,
377           iP->iGrabbedBand);
378     TRACE("hwnd=%p: style=%08x, notify in Unicode=%s, redraw=%s\n",
379           iP->hwndSelf, iP->dwStyle, (iP->bUnicode)?"TRUE":"FALSE",
380           (iP->DoRedraw)?"TRUE":"FALSE");
381     for (i = 0; i < iP->uNumBands; i++) {
382         pB = &iP->bands[i];
383         TRACE("band # %u:", i);
384         if (pB->fMask & RBBIM_ID)
385             TRACE(" ID=%u", pB->wID);
386         if (pB->fMask & RBBIM_CHILD)
387             TRACE(" child=%p", pB->hwndChild);
388         if (pB->fMask & RBBIM_COLORS)
389             TRACE(" clrF=0x%06x clrB=0x%06x", pB->clrFore, pB->clrBack);
390         TRACE("\n");
391         TRACE("band # %u: mask=0x%08x (%s)\n", i, pB->fMask, REBAR_FmtMask(pB->fMask));
392         if (pB->fMask & RBBIM_STYLE)
393             TRACE("band # %u: style=0x%08x (%s)\n",
394                   i, pB->fStyle, REBAR_FmtStyle(pB->fStyle));
395         TRACE("band # %u: xHeader=%u",
396               i, pB->cxHeader);
397         if (pB->fMask & (RBBIM_SIZE | RBBIM_IDEALSIZE | RBBIM_LPARAM )) {
398             if (pB->fMask & RBBIM_SIZE)
399                 TRACE(" cx=%u", pB->cx);
400             if (pB->fMask & RBBIM_IDEALSIZE)
401                 TRACE(" xIdeal=%u", pB->cxIdeal);
402             if (pB->fMask & RBBIM_LPARAM)
403                 TRACE(" lParam=0x%08lx", pB->lParam);
404         }
405         TRACE("\n");
406         if (RBBIM_CHILDSIZE)
407             TRACE("band # %u: xMin=%u, yMin=%u, yChild=%u, yMax=%u, yIntgl=%u\n",
408                   i, pB->cxMinChild, pB->cyMinChild, pB->cyChild, pB->cyMaxChild, pB->cyIntegral);
409         if (pB->fMask & RBBIM_TEXT)
410             TRACE("band # %u: text=%s\n",
411                   i, (pB->lpText) ? debugstr_w(pB->lpText) : "(null)");
412         TRACE("band # %u: cxMinBand=%u, cxEffective=%u, cyMinBand=%u\n",
413               i, pB->cxMinBand, pB->cxEffective, pB->cyMinBand);
414         TRACE("band # %u: fStatus=%08x, fDraw=%08x, Band=(%s), Grip=(%s)\n",
415               i, pB->fStatus, pB->fDraw, wine_dbgstr_rect(&pB->rcBand),
416               wine_dbgstr_rect(&pB->rcGripper));
417         TRACE("band # %u: Img=(%s), Txt=(%s), Child=(%s)\n",
418               i, wine_dbgstr_rect(&pB->rcCapImage),
419               wine_dbgstr_rect(&pB->rcCapText), wine_dbgstr_rect(&pB->rcChild));
420     }
421
422 }
423
424 /* dest can be equal to src */
425 static void translate_rect(const REBAR_INFO *infoPtr, RECT *dest, const RECT *src)
426 {
427     if (infoPtr->dwStyle & CCS_VERT) {
428         int tmp;
429         tmp = src->left;
430         dest->left = src->top;
431         dest->top = tmp;
432         
433         tmp = src->right;
434         dest->right = src->bottom;
435         dest->bottom = tmp;
436     } else {
437         *dest = *src;
438     }
439 }
440
441 static int get_rect_cx(const REBAR_INFO *infoPtr, const RECT *lpRect)
442 {
443     if (infoPtr->dwStyle & CCS_VERT)
444         return lpRect->bottom - lpRect->top;
445     return lpRect->right - lpRect->left;
446 }
447
448 static int get_rect_cy(const REBAR_INFO *infoPtr, const RECT *lpRect)
449 {
450     if (infoPtr->dwStyle & CCS_VERT)
451         return lpRect->right - lpRect->left;
452     return lpRect->bottom - lpRect->top;
453 }
454
455 static int round_child_height(REBAR_BAND *lpBand, int cyHeight)
456 {
457     int cy = 0;
458     if (lpBand->cyIntegral == 0)
459         return cyHeight;
460     cy = max(cyHeight - (int)lpBand->cyMinChild, 0);
461     cy = lpBand->cyMinChild + (cy/lpBand->cyIntegral) * lpBand->cyIntegral;
462     cy = min(cy, lpBand->cyMaxChild);
463     return cy;
464 }
465
466 static void update_min_band_height(const REBAR_INFO *infoPtr, REBAR_BAND *lpBand)
467 {
468     lpBand->cyMinBand = max(lpBand->cyHeader,
469         (lpBand->hwndChild ? lpBand->cyChild + REBARSPACE(lpBand) : REBAR_NO_CHILD_HEIGHT));
470 }
471
472 static void
473 REBAR_DrawChevron (HDC hdc, INT left, INT top, INT colorRef)
474 {
475     INT x, y;
476     HPEN hPen, hOldPen;
477
478     if (!(hPen = CreatePen( PS_SOLID, 1, GetSysColor( colorRef )))) return;
479     hOldPen = SelectObject ( hdc, hPen );
480     x = left + 2;
481     y = top;
482     MoveToEx (hdc, x, y, NULL);
483     LineTo (hdc, x+5, y++); x++;
484     MoveToEx (hdc, x, y, NULL);
485     LineTo (hdc, x+3, y++); x++;
486     MoveToEx (hdc, x, y, NULL);
487     LineTo (hdc, x+1, y++);
488     SelectObject( hdc, hOldPen );
489     DeleteObject( hPen );
490 }
491
492 static HWND
493 REBAR_GetNotifyParent (const REBAR_INFO *infoPtr)
494 {
495     HWND parent, owner;
496
497     parent = infoPtr->hwndNotify;
498     if (!parent) {
499         parent = GetParent (infoPtr->hwndSelf);
500         owner = GetWindow (infoPtr->hwndSelf, GW_OWNER);
501         if (owner) parent = owner;
502     }
503     return parent;
504 }
505
506
507 static INT
508 REBAR_Notify (NMHDR *nmhdr, const REBAR_INFO *infoPtr, UINT code)
509 {
510     HWND parent;
511
512     parent = REBAR_GetNotifyParent (infoPtr);
513     nmhdr->idFrom = GetDlgCtrlID (infoPtr->hwndSelf);
514     nmhdr->hwndFrom = infoPtr->hwndSelf;
515     nmhdr->code = code;
516
517     TRACE("window %p, code=%08x, via %s\n", parent, code, (infoPtr->bUnicode)?"Unicode":"ANSI");
518
519     return SendMessageW(parent, WM_NOTIFY, nmhdr->idFrom, (LPARAM)nmhdr);
520 }
521
522 static INT
523 REBAR_Notify_NMREBAR (const REBAR_INFO *infoPtr, UINT uBand, UINT code)
524 {
525     NMREBAR notify_rebar;
526     REBAR_BAND *lpBand;
527
528     notify_rebar.dwMask = 0;
529     if (uBand!=-1) {
530         lpBand = &infoPtr->bands[uBand];
531         if (lpBand->fMask & RBBIM_ID) {
532             notify_rebar.dwMask |= RBNM_ID;
533             notify_rebar.wID = lpBand->wID;
534         }
535         if (lpBand->fMask & RBBIM_LPARAM) {
536             notify_rebar.dwMask |= RBNM_LPARAM;
537             notify_rebar.lParam = lpBand->lParam;
538         }
539         if (lpBand->fMask & RBBIM_STYLE) {
540             notify_rebar.dwMask |= RBNM_STYLE;
541             notify_rebar.fStyle = lpBand->fStyle;
542         }
543     }
544     notify_rebar.uBand = uBand;
545     return REBAR_Notify ((NMHDR *)&notify_rebar, infoPtr, code);
546 }
547
548 static VOID
549 REBAR_DrawBand (HDC hdc, const REBAR_INFO *infoPtr, REBAR_BAND *lpBand)
550 {
551     HFONT hOldFont = 0;
552     INT oldBkMode = 0;
553     NMCUSTOMDRAW nmcd;
554     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
555     RECT rcBand;
556
557     translate_rect(infoPtr, &rcBand, &lpBand->rcBand);
558
559     if (lpBand->fDraw & DRAW_TEXT) {
560         hOldFont = SelectObject (hdc, infoPtr->hFont);
561         oldBkMode = SetBkMode (hdc, TRANSPARENT);
562     }
563
564     /* should test for CDRF_NOTIFYITEMDRAW here */
565     nmcd.dwDrawStage = CDDS_ITEMPREPAINT;
566     nmcd.hdc = hdc;
567     nmcd.rc = rcBand;
568     nmcd.rc.right = lpBand->rcCapText.right;
569     nmcd.rc.bottom = lpBand->rcCapText.bottom;
570     nmcd.dwItemSpec = lpBand->wID;
571     nmcd.uItemState = 0;
572     nmcd.lItemlParam = lpBand->lParam;
573     lpBand->uCDret = REBAR_Notify ((NMHDR *)&nmcd, infoPtr, NM_CUSTOMDRAW);
574     if (lpBand->uCDret == CDRF_SKIPDEFAULT) {
575         if (oldBkMode != TRANSPARENT)
576             SetBkMode (hdc, oldBkMode);
577         SelectObject (hdc, hOldFont);
578         return;
579     }
580
581     /* draw gripper */
582     if (lpBand->fDraw & DRAW_GRIPPER)
583     {
584         if (theme)
585         {
586             RECT rcGripper = lpBand->rcGripper;
587             int partId = (infoPtr->dwStyle & CCS_VERT) ? RP_GRIPPERVERT : RP_GRIPPER;
588             GetThemeBackgroundExtent (theme, hdc, partId, 0, &rcGripper, &rcGripper);
589             OffsetRect (&rcGripper, lpBand->rcGripper.left - rcGripper.left,
590                 lpBand->rcGripper.top - rcGripper.top);
591             DrawThemeBackground (theme, hdc, partId, 0, &rcGripper, NULL);
592         }
593         else
594             DrawEdge (hdc, &lpBand->rcGripper, BDR_RAISEDINNER, BF_RECT | BF_MIDDLE);
595     }
596
597     /* draw caption image */
598     if (lpBand->fDraw & DRAW_IMAGE) {
599         POINT pt;
600
601         /* center image */
602         pt.y = (lpBand->rcCapImage.bottom + lpBand->rcCapImage.top - infoPtr->imageSize.cy)/2;
603         pt.x = (lpBand->rcCapImage.right + lpBand->rcCapImage.left - infoPtr->imageSize.cx)/2;
604
605         ImageList_Draw (infoPtr->himl, lpBand->iImage, hdc,
606                         pt.x, pt.y,
607                         ILD_TRANSPARENT);
608     }
609
610     /* draw caption text */
611     if (lpBand->fDraw & DRAW_TEXT) {
612         /* need to handle CDRF_NEWFONT here */
613         INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
614         COLORREF oldcolor = CLR_NONE;
615         COLORREF new;
616         if (lpBand->clrFore != CLR_NONE) {
617             new = (lpBand->clrFore == CLR_DEFAULT) ? infoPtr->clrBtnText :
618                     lpBand->clrFore;
619             oldcolor = SetTextColor (hdc, new);
620         }
621         DrawTextW (hdc, lpBand->lpText, -1, &lpBand->rcCapText,
622                    DT_CENTER | DT_VCENTER | DT_SINGLELINE);
623         if (oldBkMode != TRANSPARENT)
624             SetBkMode (hdc, oldBkMode);
625         if (lpBand->clrFore != CLR_NONE)
626             SetTextColor (hdc, oldcolor);
627         SelectObject (hdc, hOldFont);
628     }
629
630     if (!IsRectEmpty(&lpBand->rcChevron))
631     {
632         if (theme)
633         {
634             int stateId; 
635             if (lpBand->fDraw & DRAW_CHEVRONPUSHED)
636                 stateId = CHEVS_PRESSED;
637             else if (lpBand->fDraw & DRAW_CHEVRONHOT)
638                 stateId = CHEVS_HOT;
639             else
640                 stateId = CHEVS_NORMAL;
641             DrawThemeBackground (theme, hdc, RP_CHEVRON, stateId, &lpBand->rcChevron, NULL);
642         }
643         else
644         {
645             if (lpBand->fDraw & DRAW_CHEVRONPUSHED)
646             {
647                 DrawEdge(hdc, &lpBand->rcChevron, BDR_SUNKENOUTER, BF_RECT | BF_MIDDLE);
648                 REBAR_DrawChevron(hdc, lpBand->rcChevron.left+1, lpBand->rcChevron.top + 11, COLOR_WINDOWFRAME);
649             }
650             else if (lpBand->fDraw & DRAW_CHEVRONHOT)
651             {
652                 DrawEdge(hdc, &lpBand->rcChevron, BDR_RAISEDINNER, BF_RECT | BF_MIDDLE);
653                 REBAR_DrawChevron(hdc, lpBand->rcChevron.left, lpBand->rcChevron.top + 10, COLOR_WINDOWFRAME);
654             }
655             else
656                 REBAR_DrawChevron(hdc, lpBand->rcChevron.left, lpBand->rcChevron.top + 10, COLOR_WINDOWFRAME);
657         }
658     }
659
660     if (lpBand->uCDret == (CDRF_NOTIFYPOSTPAINT | CDRF_NOTIFYITEMDRAW)) {
661         nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT;
662         nmcd.hdc = hdc;
663         nmcd.rc = rcBand;
664         nmcd.rc.right = lpBand->rcCapText.right;
665         nmcd.rc.bottom = lpBand->rcCapText.bottom;
666         nmcd.dwItemSpec = lpBand->wID;
667         nmcd.uItemState = 0;
668         nmcd.lItemlParam = lpBand->lParam;
669         lpBand->uCDret = REBAR_Notify ((NMHDR *)&nmcd, infoPtr, NM_CUSTOMDRAW);
670     }
671 }
672
673
674 static VOID
675 REBAR_Refresh (const REBAR_INFO *infoPtr, HDC hdc)
676 {
677     REBAR_BAND *lpBand;
678     UINT i;
679
680     if (!infoPtr->DoRedraw) return;
681
682     for (i = 0; i < infoPtr->uNumBands; i++) {
683         lpBand = &infoPtr->bands[i];
684
685         if (HIDDENBAND(lpBand)) continue;
686
687         /* now draw the band */
688         TRACE("[%p] drawing band %i, flags=%08x\n",
689               infoPtr->hwndSelf, i, lpBand->fDraw);
690         REBAR_DrawBand (hdc, infoPtr, lpBand);
691
692     }
693 }
694
695
696 static void
697 REBAR_CalcHorzBand (const REBAR_INFO *infoPtr, UINT rstart, UINT rend)
698      /* Function: this routine initializes all the rectangles in */
699      /*  each band in a row to fit in the adjusted rcBand rect.  */
700      /* *** Supports only Horizontal bars. ***                   */
701 {
702     REBAR_BAND *lpBand;
703     UINT i, xoff, yoff;
704     RECT work;
705
706     for(i=rstart; i<rend; i++){
707       lpBand = &infoPtr->bands[i];
708       if (HIDDENBAND(lpBand)) {
709           SetRect (&lpBand->rcChild,
710                    lpBand->rcBand.right, lpBand->rcBand.top,
711                    lpBand->rcBand.right, lpBand->rcBand.bottom);
712           continue;
713       }
714
715       /* set initial gripper rectangle */
716       SetRect (&lpBand->rcGripper, lpBand->rcBand.left, lpBand->rcBand.top,
717                lpBand->rcBand.left, lpBand->rcBand.bottom);
718
719       /* calculate gripper rectangle */
720       if ( lpBand->fStatus & HAS_GRIPPER) {
721           lpBand->fDraw |= DRAW_GRIPPER;
722           lpBand->rcGripper.left   += REBAR_PRE_GRIPPER;
723           lpBand->rcGripper.right  = lpBand->rcGripper.left + GRIPPER_WIDTH;
724           lpBand->rcGripper.top    += 2;
725           lpBand->rcGripper.bottom -= 2;
726
727           SetRect (&lpBand->rcCapImage,
728                    lpBand->rcGripper.right+REBAR_ALWAYS_SPACE, lpBand->rcBand.top,
729                    lpBand->rcGripper.right+REBAR_ALWAYS_SPACE, lpBand->rcBand.bottom);
730       }
731       else {  /* no gripper will be drawn */
732           xoff = 0;
733           if (lpBand->fStatus & (HAS_IMAGE | HAS_TEXT))
734               /* if no gripper but either image or text, then leave space */
735               xoff = REBAR_ALWAYS_SPACE;
736           SetRect (&lpBand->rcCapImage,
737                    lpBand->rcBand.left+xoff, lpBand->rcBand.top,
738                    lpBand->rcBand.left+xoff, lpBand->rcBand.bottom);
739       }
740
741       /* image is visible */
742       if (lpBand->fStatus & HAS_IMAGE) {
743           lpBand->fDraw |= DRAW_IMAGE;
744           lpBand->rcCapImage.right  += infoPtr->imageSize.cx;
745           lpBand->rcCapImage.bottom = lpBand->rcCapImage.top + infoPtr->imageSize.cy;
746
747           /* set initial caption text rectangle */
748           SetRect (&lpBand->rcCapText,
749                    lpBand->rcCapImage.right+REBAR_POST_IMAGE, lpBand->rcBand.top+1,
750                    lpBand->rcBand.left+lpBand->cxHeader, lpBand->rcBand.bottom-1);
751       }
752       else {
753           /* set initial caption text rectangle */
754           SetRect (&lpBand->rcCapText, lpBand->rcCapImage.right, lpBand->rcBand.top+1,
755                    lpBand->rcBand.left+lpBand->cxHeader, lpBand->rcBand.bottom-1);
756       }
757
758       /* text is visible */
759       if ((lpBand->fStatus & HAS_TEXT) && !(lpBand->fStyle & RBBS_HIDETITLE)) {
760           lpBand->fDraw |= DRAW_TEXT;
761           lpBand->rcCapText.right = max(lpBand->rcCapText.left,
762                                         lpBand->rcCapText.right-REBAR_POST_TEXT);
763       }
764
765       /* set initial child window rectangle if there is a child */
766       if (lpBand->hwndChild != NULL) {
767           int cyBand = lpBand->rcBand.bottom - lpBand->rcBand.top;
768           yoff = (cyBand - lpBand->cyChild) / 2;
769           SetRect (&lpBand->rcChild,
770                    lpBand->rcBand.left + lpBand->cxHeader, lpBand->rcBand.top + yoff,
771                    lpBand->rcBand.right - REBAR_POST_CHILD, lpBand->rcBand.top + yoff + lpBand->cyChild);
772           if ((lpBand->fStyle & RBBS_USECHEVRON) && (lpBand->rcChild.right - lpBand->rcChild.left < lpBand->cxIdeal))
773           {
774               lpBand->rcChild.right -= CHEVRON_WIDTH;
775               SetRect(&lpBand->rcChevron, lpBand->rcChild.right,
776                       lpBand->rcChild.top, lpBand->rcChild.right + CHEVRON_WIDTH,
777                       lpBand->rcChild.bottom);
778           }
779       }
780       else {
781           SetRect (&lpBand->rcChild,
782                    lpBand->rcBand.left+lpBand->cxHeader, lpBand->rcBand.top,
783                    lpBand->rcBand.right, lpBand->rcBand.bottom);
784       }
785
786       /* flag if notify required and invalidate rectangle */
787       if (lpBand->fDraw & NTF_INVALIDATE) {
788           TRACE("invalidating (%d,%d)-(%d,%d)\n",
789                 lpBand->rcBand.left,
790                 lpBand->rcBand.top,
791                 lpBand->rcBand.right + SEP_WIDTH,
792                 lpBand->rcBand.bottom + SEP_WIDTH);
793           lpBand->fDraw &= ~NTF_INVALIDATE;
794           work = lpBand->rcBand;
795           work.right += SEP_WIDTH;
796           work.bottom += SEP_WIDTH;
797           InvalidateRect(infoPtr->hwndSelf, &work, TRUE);
798           InvalidateRect(lpBand->hwndChild, NULL, TRUE);
799       }
800
801     }
802
803 }
804
805
806 static VOID
807 REBAR_CalcVertBand (const REBAR_INFO *infoPtr, UINT rstart, UINT rend)
808      /* Function: this routine initializes all the rectangles in */
809      /*  each band in a row to fit in the adjusted rcBand rect.  */
810      /* *** Supports only Vertical bars. ***                     */
811 {
812     REBAR_BAND *lpBand;
813     UINT i, xoff;
814     RECT work;
815
816     for(i=rstart; i<rend; i++){
817         RECT rcBand;
818         lpBand = &infoPtr->bands[i];
819         if (HIDDENBAND(lpBand)) continue;
820
821         translate_rect(infoPtr, &rcBand, &lpBand->rcBand);
822
823         /* set initial gripper rectangle */
824         SetRect (&lpBand->rcGripper, rcBand.left, rcBand.top, rcBand.right, rcBand.top);
825
826         /* calculate gripper rectangle */
827         if (lpBand->fStatus & HAS_GRIPPER) {
828             lpBand->fDraw |= DRAW_GRIPPER;
829
830             if (infoPtr->dwStyle & RBS_VERTICALGRIPPER) {
831                 /*  vertical gripper  */
832                 lpBand->rcGripper.left   += 3;
833                 lpBand->rcGripper.right  = lpBand->rcGripper.left + GRIPPER_WIDTH;
834                 lpBand->rcGripper.top    += REBAR_PRE_GRIPPER;
835                 lpBand->rcGripper.bottom = lpBand->rcGripper.top + GRIPPER_HEIGHT;
836
837                 /* initialize Caption image rectangle  */
838                 SetRect (&lpBand->rcCapImage, rcBand.left,
839                          lpBand->rcGripper.bottom + REBAR_ALWAYS_SPACE,
840                          rcBand.right,
841                          lpBand->rcGripper.bottom + REBAR_ALWAYS_SPACE);
842             }
843             else {
844                 /*  horizontal gripper  */
845                 lpBand->rcGripper.left   += 2;
846                 lpBand->rcGripper.right  -= 2;
847                 lpBand->rcGripper.top    += REBAR_PRE_GRIPPER;
848                 lpBand->rcGripper.bottom  = lpBand->rcGripper.top + GRIPPER_WIDTH;
849
850                 /* initialize Caption image rectangle  */
851                 SetRect (&lpBand->rcCapImage, rcBand.left,
852                          lpBand->rcGripper.bottom + REBAR_ALWAYS_SPACE,
853                          rcBand.right,
854                          lpBand->rcGripper.bottom + REBAR_ALWAYS_SPACE);
855             }
856         }
857         else {  /* no gripper will be drawn */
858             xoff = 0;
859             if (lpBand->fStatus & (HAS_IMAGE | HAS_TEXT))
860                 /* if no gripper but either image or text, then leave space */
861                 xoff = REBAR_ALWAYS_SPACE;
862             /* initialize Caption image rectangle  */
863             SetRect (&lpBand->rcCapImage,
864                       rcBand.left, rcBand.top+xoff,
865                       rcBand.right, rcBand.top+xoff);
866         }
867
868         /* image is visible */
869         if (lpBand->fStatus & HAS_IMAGE) {
870             lpBand->fDraw |= DRAW_IMAGE;
871
872             lpBand->rcCapImage.right  = lpBand->rcCapImage.left + infoPtr->imageSize.cx;
873             lpBand->rcCapImage.bottom += infoPtr->imageSize.cy;
874
875             /* set initial caption text rectangle */
876             SetRect (&lpBand->rcCapText,
877                      rcBand.left, lpBand->rcCapImage.bottom+REBAR_POST_IMAGE,
878                      rcBand.right, rcBand.top+lpBand->cxHeader);
879         }
880         else {
881             /* set initial caption text rectangle */
882             SetRect (&lpBand->rcCapText,
883                      rcBand.left, lpBand->rcCapImage.bottom,
884                      rcBand.right, rcBand.top+lpBand->cxHeader);
885         }
886
887         /* text is visible */
888         if ((lpBand->fStatus & HAS_TEXT) && !(lpBand->fStyle & RBBS_HIDETITLE)) {
889             lpBand->fDraw |= DRAW_TEXT;
890             lpBand->rcCapText.bottom = max(lpBand->rcCapText.top,
891                                            lpBand->rcCapText.bottom);
892         }
893
894         /* set initial child window rectangle if there is a child */
895         if (lpBand->hwndChild != NULL) {
896             int cxBand = rcBand.right - rcBand.left;
897             xoff = (cxBand - lpBand->cyChild) / 2;
898             SetRect (&lpBand->rcChild,
899                      rcBand.left + xoff,                   rcBand.top + lpBand->cxHeader,
900                      rcBand.left + xoff + lpBand->cyChild, rcBand.bottom - REBAR_POST_CHILD);
901         }
902         else {
903             SetRect (&lpBand->rcChild,
904                      rcBand.left, rcBand.top+lpBand->cxHeader,
905                      rcBand.right, rcBand.bottom);
906         }
907
908         if (lpBand->fDraw & NTF_INVALIDATE) {
909             TRACE("invalidating (%d,%d)-(%d,%d)\n",
910                   rcBand.left,
911                   rcBand.top,
912                   rcBand.right + SEP_WIDTH,
913                   rcBand.bottom + SEP_WIDTH);
914             lpBand->fDraw &= ~NTF_INVALIDATE;
915             work = rcBand;
916             work.bottom += SEP_WIDTH;
917             work.right += SEP_WIDTH;
918             InvalidateRect(infoPtr->hwndSelf, &work, TRUE);
919             InvalidateRect(lpBand->hwndChild, NULL, TRUE);
920         }
921
922     }
923 }
924
925
926 static VOID
927 REBAR_ForceResize (REBAR_INFO *infoPtr)
928      /* Function: This changes the size of the REBAR window to that */
929      /*  calculated by REBAR_Layout.                                */
930 {
931     INT x, y, width, height;
932     INT xedge = 0, yedge = 0;
933     RECT rcSelf;
934
935     TRACE("new size [%d x %d]\n", infoPtr->calcSize.cx, infoPtr->calcSize.cy);
936
937     if (infoPtr->dwStyle & CCS_NORESIZE)
938         return;
939
940     if (infoPtr->dwStyle & WS_BORDER)
941     {
942         xedge = GetSystemMetrics(SM_CXEDGE);
943         yedge = GetSystemMetrics(SM_CYEDGE);
944         /* swap for CCS_VERT? */
945     }
946
947     /* compute rebar window rect in parent client coordinates */
948     GetWindowRect(infoPtr->hwndSelf, &rcSelf);
949     MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->hwndSelf), (LPPOINT)&rcSelf, 2);
950     translate_rect(infoPtr, &rcSelf, &rcSelf);
951
952     height = infoPtr->calcSize.cy + 2*yedge;
953     if (!(infoPtr->dwStyle & CCS_NOPARENTALIGN)) {
954         RECT rcParent;
955
956         x = -xedge;
957         width = infoPtr->calcSize.cx + 2*xedge;
958         y = 0; /* quiet compiler warning */
959         switch ( infoPtr->dwStyle & CCS_LAYOUT_MASK) {
960             case 0:     /* shouldn't happen - see NCCreate */
961             case CCS_TOP:
962                 y = ((infoPtr->dwStyle & CCS_NODIVIDER) ? 0 : REBAR_DIVIDER) - yedge;
963                 break;
964             case CCS_NOMOVEY:
965                 y = rcSelf.top;
966                 break;
967             case CCS_BOTTOM:
968                 GetClientRect(GetParent(infoPtr->hwndSelf), &rcParent);
969                 translate_rect(infoPtr, &rcParent, &rcParent);
970                 y = rcParent.bottom - infoPtr->calcSize.cy - yedge;
971                 break;
972         }
973     }
974     else {
975         x = rcSelf.left;
976         /* As on Windows if the CCS_NODIVIDER is not present the control will move
977          * 2 pixel down after every layout */
978         y = rcSelf.top + ((infoPtr->dwStyle & CCS_NODIVIDER) ? 0 : REBAR_DIVIDER);
979         width = rcSelf.right - rcSelf.left;
980     }
981
982     TRACE("hwnd %p, style=%08x, setting at (%d,%d) for (%d,%d)\n",
983         infoPtr->hwndSelf, infoPtr->dwStyle, x, y, width, height);
984
985     /* Set flag to ignore next WM_SIZE message and resize the window */
986     infoPtr->fStatus |= SELF_RESIZE;
987     if ((infoPtr->dwStyle & CCS_VERT) == 0)
988         SetWindowPos(infoPtr->hwndSelf, 0, x, y, width, height, SWP_NOZORDER);
989     else
990         SetWindowPos(infoPtr->hwndSelf, 0, y, x, height, width, SWP_NOZORDER);
991     infoPtr->fStatus &= ~SELF_RESIZE;
992 }
993
994
995 static VOID
996 REBAR_MoveChildWindows (const REBAR_INFO *infoPtr, UINT start, UINT endplus)
997 {
998     static const WCHAR strComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
999     REBAR_BAND *lpBand;
1000     WCHAR szClassName[40];
1001     UINT i;
1002     NMREBARCHILDSIZE  rbcz;
1003     HDWP deferpos;
1004
1005     if (!(deferpos = BeginDeferWindowPos(infoPtr->uNumBands)))
1006         ERR("BeginDeferWindowPos returned NULL\n");
1007
1008     for (i = start; i < endplus; i++) {
1009         lpBand = &infoPtr->bands[i];
1010
1011         if (HIDDENBAND(lpBand)) continue;
1012         if (lpBand->hwndChild) {
1013             TRACE("hwndChild = %p\n", lpBand->hwndChild);
1014
1015             /* Always generate the RBN_CHILDSIZE even if child
1016                    did not change */
1017             rbcz.uBand = i;
1018             rbcz.wID = lpBand->wID;
1019             rbcz.rcChild = lpBand->rcChild;
1020             translate_rect(infoPtr, &rbcz.rcBand, &lpBand->rcBand);
1021             if (infoPtr->dwStyle & CCS_VERT)
1022                 rbcz.rcBand.top += lpBand->cxHeader;
1023             else
1024                 rbcz.rcBand.left += lpBand->cxHeader;
1025             REBAR_Notify ((NMHDR *)&rbcz, infoPtr, RBN_CHILDSIZE);
1026             if (!EqualRect (&lpBand->rcChild, &rbcz.rcChild)) {
1027                 TRACE("Child rect changed by NOTIFY for band %u\n", i);
1028                 TRACE("    from (%s)  to (%s)\n",
1029                       wine_dbgstr_rect(&lpBand->rcChild),
1030                       wine_dbgstr_rect(&rbcz.rcChild));
1031                 lpBand->rcChild = rbcz.rcChild;  /* *** ??? */
1032             }
1033
1034             /* native (IE4 in "Favorites" frame **1) does:
1035              *   SetRect (&rc, -1, -1, -1, -1)
1036              *   EqualRect (&rc,band->rc???)
1037              *   if ret==0
1038              *     CopyRect (band->rc????, &rc)
1039              *     set flag outside of loop
1040              */
1041
1042             GetClassNameW (lpBand->hwndChild, szClassName, sizeof(szClassName)/sizeof(szClassName[0]));
1043             if (!lstrcmpW (szClassName, strComboBox) ||
1044                 !lstrcmpW (szClassName, WC_COMBOBOXEXW)) {
1045                 INT nEditHeight, yPos;
1046                 RECT rc;
1047
1048                 /* special placement code for combo or comboex box */
1049
1050
1051                 /* get size of edit line */
1052                 GetWindowRect (lpBand->hwndChild, &rc);
1053                 nEditHeight = rc.bottom - rc.top;
1054                 yPos = (lpBand->rcChild.bottom + lpBand->rcChild.top - nEditHeight)/2;
1055
1056                 /* center combo box inside child area */
1057                 TRACE("moving child (Combo(Ex)) %p to (%d,%d) for (%d,%d)\n",
1058                       lpBand->hwndChild,
1059                       lpBand->rcChild.left, yPos,
1060                       lpBand->rcChild.right - lpBand->rcChild.left,
1061                       nEditHeight);
1062                 deferpos = DeferWindowPos (deferpos, lpBand->hwndChild, HWND_TOP,
1063                                            lpBand->rcChild.left,
1064                                            /*lpBand->rcChild.top*/ yPos,
1065                                            lpBand->rcChild.right - lpBand->rcChild.left,
1066                                            nEditHeight,
1067                                            SWP_NOZORDER);
1068                 if (!deferpos)
1069                     ERR("DeferWindowPos returned NULL\n");
1070             }
1071             else {
1072                 TRACE("moving child (Other) %p to (%d,%d) for (%d,%d)\n",
1073                       lpBand->hwndChild,
1074                       lpBand->rcChild.left, lpBand->rcChild.top,
1075                       lpBand->rcChild.right - lpBand->rcChild.left,
1076                       lpBand->rcChild.bottom - lpBand->rcChild.top);
1077                 deferpos = DeferWindowPos (deferpos, lpBand->hwndChild, HWND_TOP,
1078                                            lpBand->rcChild.left,
1079                                            lpBand->rcChild.top,
1080                                            lpBand->rcChild.right - lpBand->rcChild.left,
1081                                            lpBand->rcChild.bottom - lpBand->rcChild.top,
1082                                            SWP_NOZORDER);
1083                 if (!deferpos)
1084                     ERR("DeferWindowPos returned NULL\n");
1085             }
1086         }
1087     }
1088     if (!EndDeferWindowPos(deferpos))
1089         ERR("EndDeferWindowPos returned NULL\n");
1090
1091     if (infoPtr->DoRedraw)
1092         UpdateWindow (infoPtr->hwndSelf);
1093
1094     /* native (from **1 above) does:
1095      *      UpdateWindow(rebar)
1096      *      REBAR_ForceResize
1097      *      RBN_HEIGHTCHANGE if necessary
1098      *      if ret from any EqualRect was 0
1099      *         Goto "BeginDeferWindowPos"
1100      */
1101
1102 }
1103
1104 /* Returns the next visible band (the first visible band in [i+1; infoPtr->uNumBands) )
1105  * or infoPtr->uNumBands if none */
1106 static int next_visible(const REBAR_INFO *infoPtr, int i)
1107 {
1108     int n;
1109     for (n = i + 1; n < infoPtr->uNumBands; n++)
1110         if (!HIDDENBAND(&infoPtr->bands[n]))
1111             break;
1112     return n;
1113 }
1114
1115 /* Returns the previous visible band (the last visible band in [0; i) )
1116  * or -1 if none */
1117 static int prev_visible(const REBAR_INFO *infoPtr, int i)
1118 {
1119     int n;
1120     for (n = i - 1; n >= 0; n--)
1121         if (!HIDDENBAND(&infoPtr->bands[n]))
1122             break;
1123     return n;
1124 }
1125
1126 /* Returns the first visible band or infoPtr->uNumBands if none */
1127 static int first_visible(const REBAR_INFO *infoPtr)
1128 {
1129     return next_visible(infoPtr, -1); /* this works*/
1130 }
1131
1132 /* Returns the first visible band for the given row (or iBand if none) */
1133 static int get_row_begin_for_band(const REBAR_INFO *infoPtr, INT iBand)
1134 {
1135     int iLastBand = iBand;
1136     int iRow = infoPtr->bands[iBand].iRow;
1137     while ((iBand = prev_visible(infoPtr, iBand)) >= 0) {
1138         if (infoPtr->bands[iBand].iRow != iRow)
1139             break;
1140         else
1141             iLastBand = iBand;
1142     }
1143     return iLastBand;
1144 }
1145
1146 /* Returns the first visible band for the next row (or infoPtr->uNumBands if none) */
1147 static int get_row_end_for_band(const REBAR_INFO *infoPtr, INT iBand)
1148 {
1149     int iRow = infoPtr->bands[iBand].iRow;
1150     while ((iBand = next_visible(infoPtr, iBand)) < infoPtr->uNumBands)
1151         if (infoPtr->bands[iBand].iRow != iRow)
1152             break;
1153     return iBand;
1154 }
1155
1156 /* Compute the rcBand.{left,right} from the cxEffective bands widths computed earlier.
1157  * iBeginBand must be visible */
1158 static void REBAR_SetRowRectsX(const REBAR_INFO *infoPtr, INT iBeginBand, INT iEndBand)
1159 {
1160     int xPos = 0, i;
1161     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1162     {
1163         REBAR_BAND *lpBand = &infoPtr->bands[i];
1164
1165         lpBand = &infoPtr->bands[i];
1166         if (lpBand->rcBand.left != xPos || lpBand->rcBand.right != xPos + lpBand->cxEffective) {
1167             lpBand->fDraw |= NTF_INVALIDATE;
1168             TRACE("Setting rect %d to %d,%d\n", i, xPos, xPos + lpBand->cxEffective);
1169             lpBand->rcBand.left = xPos;
1170             lpBand->rcBand.right = xPos + lpBand->cxEffective;
1171         }
1172         xPos += lpBand->cxEffective + SEP_WIDTH;
1173     }
1174 }
1175
1176 /* The rationale of this function is probably as follows: if we have some space
1177  * to distribute we want to add it to a band on the right. However we don't want
1178  * to unminimize a minimized band so we search for a band that is big enough.
1179  * For some reason "big enough" is defined as bigger than the minimum size of the
1180  * first band in the row
1181  */
1182 static REBAR_BAND *REBAR_FindBandToGrow(const REBAR_INFO *infoPtr, INT iBeginBand, INT iEndBand)
1183 {
1184     INT cxMinFirstBand = 0, i;
1185
1186     cxMinFirstBand = infoPtr->bands[iBeginBand].cxMinBand;
1187
1188     for (i = prev_visible(infoPtr, iEndBand); i >= iBeginBand; i = prev_visible(infoPtr, i))
1189         if (infoPtr->bands[i].cxEffective > cxMinFirstBand && !(infoPtr->bands[i].fStyle&RBBS_FIXEDSIZE))
1190             break;
1191
1192     if (i < iBeginBand)
1193         for (i = prev_visible(infoPtr, iEndBand); i >= iBeginBand; i = prev_visible(infoPtr, i))
1194             if (infoPtr->bands[i].cxMinBand == cxMinFirstBand)
1195                 break;
1196
1197     TRACE("Extra space for row [%d..%d) should be added to band %d\n", iBeginBand, iEndBand, i);
1198     return &infoPtr->bands[i];
1199 }
1200
1201 /* Try to shrink the visible bands in [iBeginBand; iEndBand) by cxShrink, starting from the right */
1202 static int REBAR_ShrinkBandsRTL(const REBAR_INFO *infoPtr, INT iBeginBand, INT iEndBand, INT cxShrink, BOOL bEnforce)
1203 {
1204     REBAR_BAND *lpBand;
1205     INT width, i;
1206
1207     TRACE("Shrinking bands [%d..%d) by %d, right-to-left\n", iBeginBand, iEndBand, cxShrink);
1208     for (i = prev_visible(infoPtr, iEndBand); i >= iBeginBand; i = prev_visible(infoPtr, i))
1209     {
1210         lpBand = &infoPtr->bands[i];
1211
1212         width = max(lpBand->cxEffective - cxShrink, (int)lpBand->cxMinBand);
1213         cxShrink -= lpBand->cxEffective - width;
1214         lpBand->cxEffective = width;
1215         if (bEnforce && lpBand->cx > lpBand->cxEffective)
1216             lpBand->cx = lpBand->cxEffective;
1217         if (cxShrink == 0)
1218             break;
1219     }
1220     return cxShrink;
1221 }
1222
1223
1224 /* Try to shrink the visible bands in [iBeginBand; iEndBand) by cxShrink, starting from the left.
1225  * iBeginBand must be visible */
1226 static int REBAR_ShrinkBandsLTR(const REBAR_INFO *infoPtr, INT iBeginBand, INT iEndBand, INT cxShrink, BOOL bEnforce)
1227 {
1228     REBAR_BAND *lpBand;
1229     INT width, i;
1230
1231     TRACE("Shrinking bands [%d..%d) by %d, left-to-right\n", iBeginBand, iEndBand, cxShrink);
1232     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1233     {
1234         lpBand = &infoPtr->bands[i];
1235
1236         width = max(lpBand->cxEffective - cxShrink, (int)lpBand->cxMinBand);
1237         cxShrink -= lpBand->cxEffective - width;
1238         lpBand->cxEffective = width;
1239         if (bEnforce)
1240             lpBand->cx = lpBand->cxEffective;
1241         if (cxShrink == 0)
1242             break;
1243     }
1244     return cxShrink;
1245 }
1246
1247 /* Set the heights of the visible bands in [iBeginBand; iEndBand) to the max height. iBeginBand must be visible */
1248 static int REBAR_SetBandsHeight(const REBAR_INFO *infoPtr, INT iBeginBand, INT iEndBand, INT yStart)
1249 {
1250     REBAR_BAND *lpBand;
1251     int yMaxHeight = 0;
1252     int yPos = yStart;
1253     int row = infoPtr->bands[iBeginBand].iRow;
1254     int i;
1255     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1256     {
1257         lpBand = &infoPtr->bands[i];
1258         lpBand->cyRowSoFar = yMaxHeight;
1259         yMaxHeight = max(yMaxHeight, lpBand->cyMinBand);
1260     }
1261     TRACE("Bands [%d; %d) height: %d\n", iBeginBand, iEndBand, yMaxHeight);
1262
1263     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1264     {
1265         lpBand = &infoPtr->bands[i];
1266         /* we may be called for multiple rows if RBS_VARHEIGHT not set */
1267         if (lpBand->iRow != row) {
1268             yPos += yMaxHeight + SEP_WIDTH;
1269             row = lpBand->iRow;
1270         }
1271
1272         if (lpBand->rcBand.top != yPos || lpBand->rcBand.bottom != yPos + yMaxHeight) {
1273             lpBand->fDraw |= NTF_INVALIDATE;
1274             lpBand->rcBand.top = yPos;
1275             lpBand->rcBand.bottom = yPos + yMaxHeight;
1276             TRACE("Band %d: %s\n", i, wine_dbgstr_rect(&lpBand->rcBand));
1277         }
1278     }
1279     return yPos + yMaxHeight;
1280 }
1281
1282 /* Layout the row [iBeginBand; iEndBand). iBeginBand must be visible */
1283 static void REBAR_LayoutRow(const REBAR_INFO *infoPtr, int iBeginBand, int iEndBand, int cx, int *piRow, int *pyPos)
1284 {
1285     REBAR_BAND *lpBand;
1286     int i, extra;
1287     int width = 0;
1288
1289     TRACE("Adjusting row [%d;%d). Width: %d\n", iBeginBand, iEndBand, cx);
1290     for (i = iBeginBand; i < iEndBand; i++)
1291         infoPtr->bands[i].iRow = *piRow;
1292
1293     /* compute the extra space */
1294     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1295     {
1296         lpBand = &infoPtr->bands[i];
1297         if (i > iBeginBand)
1298             width += SEP_WIDTH;
1299         lpBand->cxEffective = max(lpBand->cxMinBand, lpBand->cx);
1300         width += lpBand->cxEffective;
1301     }
1302
1303     extra = cx - width;
1304     TRACE("Extra space: %d\n", extra);
1305     if (extra < 0) {
1306         int ret = REBAR_ShrinkBandsRTL(infoPtr, iBeginBand, iEndBand, -extra, FALSE);
1307         if (ret > 0 && next_visible(infoPtr, iBeginBand) != iEndBand)  /* one band may be longer than expected... */
1308             ERR("Error layouting row %d - couldn't shrink for %d pixels (%d total shrink)\n", *piRow, ret, -extra);
1309     } else
1310     if (extra > 0) {
1311         lpBand = REBAR_FindBandToGrow(infoPtr, iBeginBand, iEndBand);
1312         lpBand->cxEffective += extra;
1313     }
1314
1315     REBAR_SetRowRectsX(infoPtr, iBeginBand, iEndBand);
1316     if (infoPtr->dwStyle & RBS_VARHEIGHT)
1317     {
1318         if (*piRow > 0)
1319             *pyPos += SEP_WIDTH;
1320         *pyPos = REBAR_SetBandsHeight(infoPtr, iBeginBand, iEndBand, *pyPos);
1321     }
1322     (*piRow)++;
1323 }
1324
1325 static VOID
1326 REBAR_Layout(REBAR_INFO *infoPtr)
1327 {
1328     REBAR_BAND *lpBand;
1329     RECT rcAdj;
1330     SIZE oldSize;
1331     INT adjcx, i;
1332     INT rowstart;
1333     INT row = 0;
1334     INT xMin, yPos;
1335
1336     if (infoPtr->dwStyle & (CCS_NORESIZE | CCS_NOPARENTALIGN) || GetParent(infoPtr->hwndSelf) == NULL)
1337         GetClientRect(infoPtr->hwndSelf, &rcAdj);
1338     else
1339         GetClientRect(GetParent(infoPtr->hwndSelf), &rcAdj);
1340     TRACE("adjustment rect is (%s)\n", wine_dbgstr_rect(&rcAdj));
1341
1342     adjcx = get_rect_cx(infoPtr, &rcAdj);
1343
1344     if (infoPtr->uNumBands == 0) {
1345         TRACE("No bands - setting size to (0,%d), vert: %lx\n", adjcx, infoPtr->dwStyle & CCS_VERT);
1346         infoPtr->calcSize.cx = adjcx;
1347         /* the calcSize.cy won't change for a 0 band rebar */
1348         infoPtr->uNumRows = 0;
1349         REBAR_ForceResize(infoPtr);
1350         return;
1351     }
1352
1353     yPos = 0;
1354     xMin = 0;
1355     rowstart = first_visible(infoPtr);
1356     /* divide rows */
1357     for (i = rowstart; i < infoPtr->uNumBands; i = next_visible(infoPtr, i))
1358     {
1359         lpBand = &infoPtr->bands[i];
1360
1361         if (i > rowstart && (lpBand->fStyle & RBBS_BREAK || xMin + lpBand->cxMinBand > adjcx)) {
1362             TRACE("%s break on band %d\n", (lpBand->fStyle & RBBS_BREAK ? "Hard" : "Soft"), i - 1);
1363             REBAR_LayoutRow(infoPtr, rowstart, i, adjcx, &row, &yPos);
1364             rowstart = i;
1365             xMin = 0;
1366         }
1367         else
1368             xMin += SEP_WIDTH;
1369
1370         xMin += lpBand->cxMinBand;
1371     }
1372     REBAR_LayoutRow(infoPtr, rowstart, infoPtr->uNumBands, adjcx, &row, &yPos);
1373
1374     if (!(infoPtr->dwStyle & RBS_VARHEIGHT))
1375         yPos = REBAR_SetBandsHeight(infoPtr, first_visible(infoPtr), infoPtr->uNumBands, 0);
1376
1377     infoPtr->uNumRows = row;
1378
1379     if (infoPtr->dwStyle & CCS_VERT)
1380         REBAR_CalcVertBand(infoPtr, 0, infoPtr->uNumBands);
1381     else
1382         REBAR_CalcHorzBand(infoPtr, 0, infoPtr->uNumBands);
1383     /* now compute size of Rebar itself */
1384     oldSize = infoPtr->calcSize;
1385
1386     infoPtr->calcSize.cx = adjcx;
1387     infoPtr->calcSize.cy = yPos;
1388     TRACE("calcsize size=(%d, %d), origheight=(%d,%d)\n",
1389             infoPtr->calcSize.cx, infoPtr->calcSize.cy,
1390             oldSize.cx, oldSize.cy);
1391
1392     REBAR_DumpBand (infoPtr);
1393     REBAR_MoveChildWindows (infoPtr, 0, infoPtr->uNumBands);
1394     REBAR_ForceResize (infoPtr);
1395
1396     /* note: after a RBN_HEIGHTCHANGE native sends once again all the RBN_CHILDSIZE
1397      * and does another ForceResize */
1398     if (oldSize.cy != infoPtr->calcSize.cy)
1399     {
1400         NMHDR heightchange;
1401         REBAR_Notify(&heightchange, infoPtr, RBN_HEIGHTCHANGE);
1402         REBAR_AutoSize(infoPtr, FALSE);
1403     }
1404 }
1405
1406 /* iBeginBand must be visible */
1407 static int
1408 REBAR_SizeChildrenToHeight(const REBAR_INFO *infoPtr, int iBeginBand, int iEndBand, int extra, BOOL *fChanged)
1409 {
1410     int cyBandsOld;
1411     int cyBandsNew = 0;
1412     int i;
1413
1414     TRACE("[%d;%d) by %d\n", iBeginBand, iEndBand, extra);
1415
1416     cyBandsOld = infoPtr->bands[iBeginBand].rcBand.bottom - infoPtr->bands[iBeginBand].rcBand.top;
1417     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1418     {
1419         REBAR_BAND *lpBand = &infoPtr->bands[i];
1420         int cyMaxChild = cyBandsOld - REBARSPACE(lpBand) + extra;
1421         int cyChild = round_child_height(lpBand, cyMaxChild);
1422
1423         if (lpBand->hwndChild && cyChild != lpBand->cyChild && (lpBand->fStyle & RBBS_VARIABLEHEIGHT))
1424         {
1425             TRACE("Resizing %d: %d -> %d [%d]\n", i, lpBand->cyChild, cyChild, lpBand->cyMaxChild);
1426             *fChanged = TRUE;
1427             lpBand->cyChild = cyChild;
1428             lpBand->fDraw |= NTF_INVALIDATE;
1429             update_min_band_height(infoPtr, lpBand);
1430         }
1431         cyBandsNew = max(cyBandsNew, lpBand->cyMinBand);
1432     }
1433     return cyBandsNew - cyBandsOld;
1434 }
1435
1436 /* worker function for RB_SIZETORECT and RBS_AUTOSIZE */
1437 static VOID
1438 REBAR_SizeToHeight(REBAR_INFO *infoPtr, int height)
1439 {
1440     int extra = height - infoPtr->calcSize.cy;  /* may be negative */
1441     BOOL fChanged = FALSE;
1442     UINT uNumRows = infoPtr->uNumRows;
1443     int i;
1444
1445     if (uNumRows == 0)  /* avoid division by 0 */
1446         return;
1447
1448     /* That's not exactly what Windows does but should be similar */
1449
1450     /* Pass one: break-up/glue rows */
1451     if (extra > 0)
1452     {
1453         for (i = prev_visible(infoPtr, infoPtr->uNumBands); i > 0; i = prev_visible(infoPtr, i))
1454         {
1455             REBAR_BAND *lpBand = &infoPtr->bands[i];
1456             int height = lpBand->rcBand.bottom - lpBand->rcBand.top;
1457             int cyBreakExtra;  /* additional cy for the rebar after a RBBS_BREAK on this band */
1458
1459             if (infoPtr->dwStyle & RBS_VARHEIGHT)
1460                 cyBreakExtra = lpBand->cyRowSoFar; /* 'height' => 'lpBand->cyRowSoFar' + 'height'*/
1461             else
1462                 cyBreakExtra = height;             /* 'height' => 'height' + 'height'*/
1463             cyBreakExtra += SEP_WIDTH;
1464
1465             if (extra <= cyBreakExtra / 2)
1466                 break;
1467
1468             if (!(lpBand->fStyle & RBBS_BREAK))
1469             {
1470                 TRACE("Adding break on band %d - extra %d -> %d\n", i, extra, extra - cyBreakExtra);
1471                 lpBand->fStyle |= RBBS_BREAK;
1472                 lpBand->fDraw |= NTF_INVALIDATE;
1473                 fChanged = TRUE;
1474                 extra -= cyBreakExtra;
1475                 uNumRows++;
1476                 /* temporary change for _SizeControlsToHeight. The true values will be computed in _Layout */
1477                 if (infoPtr->dwStyle & RBS_VARHEIGHT)
1478                     lpBand->rcBand.bottom = lpBand->rcBand.top + lpBand->cyMinBand;
1479             }
1480         }
1481     }
1482     /* TODO: else if (extra < 0) { try to remove some RBBS_BREAKs } */
1483
1484     /* Pass two: increase/decrease control height */
1485     if (infoPtr->dwStyle & RBS_VARHEIGHT)
1486     {
1487         int i = first_visible(infoPtr);
1488         int iRow = 0;
1489         while (i < infoPtr->uNumBands)
1490         {
1491             REBAR_BAND *lpBand = &infoPtr->bands[i];
1492             int extraForRow = extra / (int)(uNumRows - iRow);
1493             int rowEnd;
1494
1495             /* we can't use get_row_end_for_band as we might have added RBBS_BREAK in the first phase */
1496             for (rowEnd = next_visible(infoPtr, i); rowEnd < infoPtr->uNumBands; rowEnd = next_visible(infoPtr, rowEnd))
1497                 if (infoPtr->bands[rowEnd].iRow != lpBand->iRow || (infoPtr->bands[rowEnd].fStyle & RBBS_BREAK))
1498                     break;
1499
1500             extra -= REBAR_SizeChildrenToHeight(infoPtr, i, rowEnd, extraForRow, &fChanged);
1501             TRACE("extra = %d\n", extra);
1502             i = rowEnd;
1503             iRow++;
1504         }
1505     }
1506     else
1507         extra -= REBAR_SizeChildrenToHeight(infoPtr, first_visible(infoPtr), infoPtr->uNumBands, extra / infoPtr->uNumRows, &fChanged);
1508
1509     if (fChanged)
1510         REBAR_Layout(infoPtr);
1511 }
1512
1513 static VOID
1514 REBAR_AutoSize(REBAR_INFO *infoPtr, BOOL needsLayout)
1515 {
1516     RECT rc, rcNew;
1517     NMRBAUTOSIZE autosize;
1518
1519     if (needsLayout)
1520         REBAR_Layout(infoPtr);
1521     GetClientRect(infoPtr->hwndSelf, &rc);
1522     REBAR_SizeToHeight(infoPtr, get_rect_cy(infoPtr, &rc));
1523     GetClientRect(infoPtr->hwndSelf, &rcNew);
1524
1525     GetClientRect(infoPtr->hwndSelf, &autosize.rcTarget);
1526     autosize.fChanged = (memcmp(&rc, &rcNew, sizeof(RECT)) == 0);
1527     autosize.rcTarget = rc;
1528     autosize.rcActual = rcNew;
1529     REBAR_Notify((NMHDR *)&autosize, infoPtr, RBN_AUTOSIZE);
1530 }
1531
1532 static VOID
1533 REBAR_ValidateBand (const REBAR_INFO *infoPtr, REBAR_BAND *lpBand)
1534      /* Function:  This routine evaluates the band specs supplied */
1535      /*  by the user and updates the following 5 fields in        */
1536      /*  the internal band structure: cxHeader, cyHeader, cxMinBand, cyMinBand, fStatus */
1537 {
1538     UINT header=0;
1539     UINT textheight=0, imageheight = 0;
1540     UINT i, nonfixed;
1541     REBAR_BAND *tBand;
1542
1543     lpBand->fStatus = 0;
1544     lpBand->cxMinBand = 0;
1545     lpBand->cyMinBand = 0;
1546
1547     /* Data coming in from users into the cx... and cy... fields   */
1548     /* may be bad, just garbage, because the user never clears     */
1549     /* the fields. RB_{SET|INSERT}BAND{A|W} just passes the data   */
1550     /* along if the fields exist in the input area. Here we must   */
1551     /* determine if the data is valid. I have no idea how MS does  */
1552     /* the validation, but it does because the RB_GETBANDINFO      */
1553     /* returns a 0 when I know the sample program passed in an     */
1554     /* address. Here I will use the algorithm that if the value    */
1555     /* is greater than 65535 then it is bad and replace it with    */
1556     /* a zero. Feel free to improve the algorithm.  -  GA 12/2000  */
1557     if (lpBand->cxMinChild > 65535) lpBand->cxMinChild = 0;
1558     if (lpBand->cyMinChild > 65535) lpBand->cyMinChild = 0;
1559     if (lpBand->cx         > 65535) lpBand->cx         = 0;
1560     if (lpBand->cyChild    > 65535) lpBand->cyChild    = 0;
1561     if (lpBand->cyIntegral > 65535) lpBand->cyIntegral = 0;
1562     if (lpBand->cxIdeal    > 65535) lpBand->cxIdeal    = 0;
1563     if (lpBand->cxHeader   > 65535) lpBand->cxHeader   = 0;
1564
1565     /* TODO : we could try return to the caller if a value changed so that */
1566     /*        a REBAR_Layout is needed. Till now the caller should call it */
1567     /*        it always (we should also check what native does)            */
1568
1569     /* Header is where the image, text and gripper exist  */
1570     /* in the band and precede the child window.          */
1571
1572     /* count number of non-FIXEDSIZE and non-Hidden bands */
1573     nonfixed = 0;
1574     for (i=0; i<infoPtr->uNumBands; i++){
1575         tBand = &infoPtr->bands[i];
1576         if (!HIDDENBAND(tBand) && !(tBand->fStyle & RBBS_FIXEDSIZE))
1577             nonfixed++;
1578     }
1579
1580     /* calculate gripper rectangle */
1581     if (  (!(lpBand->fStyle & RBBS_NOGRIPPER)) &&
1582           ( (lpBand->fStyle & RBBS_GRIPPERALWAYS) ||
1583             ( !(lpBand->fStyle & RBBS_FIXEDSIZE) && (nonfixed > 1)))
1584        ) {
1585         lpBand->fStatus |= HAS_GRIPPER;
1586         if (infoPtr->dwStyle & CCS_VERT)
1587             if (infoPtr->dwStyle & RBS_VERTICALGRIPPER)
1588                 header += (GRIPPER_HEIGHT + REBAR_PRE_GRIPPER);
1589             else
1590                 header += (GRIPPER_WIDTH + REBAR_PRE_GRIPPER);
1591         else
1592             header += (REBAR_PRE_GRIPPER + GRIPPER_WIDTH);
1593         /* Always have 4 pixels before anything else */
1594         header += REBAR_ALWAYS_SPACE;
1595     }
1596
1597     /* image is visible */
1598     if (lpBand->iImage != -1 && (infoPtr->himl)) {
1599         lpBand->fStatus |= HAS_IMAGE;
1600         if (infoPtr->dwStyle & CCS_VERT) {
1601            header += (infoPtr->imageSize.cy + REBAR_POST_IMAGE);
1602            imageheight = infoPtr->imageSize.cx + 4;
1603         }
1604         else {
1605            header += (infoPtr->imageSize.cx + REBAR_POST_IMAGE);
1606            imageheight = infoPtr->imageSize.cy + 4;
1607         }
1608     }
1609
1610     /* text is visible */
1611     if ((lpBand->fMask & RBBIM_TEXT) && (lpBand->lpText) &&
1612         !(lpBand->fStyle & RBBS_HIDETITLE)) {
1613         HDC hdc = GetDC (0);
1614         HFONT hOldFont = SelectObject (hdc, infoPtr->hFont);
1615         SIZE size;
1616
1617         lpBand->fStatus |= HAS_TEXT;
1618         GetTextExtentPoint32W (hdc, lpBand->lpText,
1619                                lstrlenW (lpBand->lpText), &size);
1620         header += ((infoPtr->dwStyle & CCS_VERT) ? (size.cy + REBAR_POST_TEXT) : (size.cx + REBAR_POST_TEXT));
1621         textheight = (infoPtr->dwStyle & CCS_VERT) ? 0 : size.cy;
1622
1623         SelectObject (hdc, hOldFont);
1624         ReleaseDC (0, hdc);
1625     }
1626
1627     /* if no gripper but either image or text, then leave space */
1628     if ((lpBand->fStatus & (HAS_IMAGE | HAS_TEXT)) &&
1629         !(lpBand->fStatus & HAS_GRIPPER)) {
1630         header += REBAR_ALWAYS_SPACE;
1631     }
1632
1633     /* check if user overrode the header value */
1634     if (!(lpBand->fStyle & RBBS_UNDOC_FIXEDHEADER))
1635         lpBand->cxHeader = header;
1636     lpBand->cyHeader = max(textheight, imageheight);
1637
1638     /* Now compute minimum size of child window */
1639     update_min_band_height(infoPtr, lpBand);       /* update lpBand->cyMinBand from cyHeader and cyChild*/
1640
1641     lpBand->cxMinBand = lpBand->cxMinChild + lpBand->cxHeader + REBAR_POST_CHILD;
1642     if (lpBand->fStyle & RBBS_USECHEVRON && lpBand->cxMinChild < lpBand->cxIdeal)
1643         lpBand->cxMinBand += CHEVRON_WIDTH;
1644 }
1645
1646 static UINT
1647 REBAR_CommonSetupBand(HWND hwnd, const REBARBANDINFOW *lprbbi, REBAR_BAND *lpBand)
1648      /* Function:  This routine copies the supplied values from   */
1649      /*  user input (lprbbi) to the internal band structure.      */
1650      /*  It returns the mask of what changed.   */
1651 {
1652     UINT uChanged = 0x0;
1653
1654     lpBand->fMask |= lprbbi->fMask;
1655
1656     if( (lprbbi->fMask & RBBIM_STYLE) &&
1657         (lpBand->fStyle != lprbbi->fStyle ) )
1658     {
1659         lpBand->fStyle = lprbbi->fStyle;
1660         uChanged |= RBBIM_STYLE;
1661     }
1662
1663     if( (lprbbi->fMask & RBBIM_COLORS) &&
1664        ( ( lpBand->clrFore != lprbbi->clrFore ) ||
1665          ( lpBand->clrBack != lprbbi->clrBack ) ) )
1666     {
1667         lpBand->clrFore = lprbbi->clrFore;
1668         lpBand->clrBack = lprbbi->clrBack;
1669         uChanged |= RBBIM_COLORS;
1670     }
1671
1672     if( (lprbbi->fMask & RBBIM_IMAGE) &&
1673        ( lpBand->iImage != lprbbi->iImage ) )
1674     {
1675         lpBand->iImage = lprbbi->iImage;
1676         uChanged |= RBBIM_IMAGE;
1677     }
1678
1679     if( (lprbbi->fMask & RBBIM_CHILD) &&
1680        (lprbbi->hwndChild != lpBand->hwndChild ) )
1681     {
1682         if (lprbbi->hwndChild) {
1683             lpBand->hwndChild = lprbbi->hwndChild;
1684             lpBand->hwndPrevParent =
1685                 SetParent (lpBand->hwndChild, hwnd);
1686             /* below in trace from WinRAR */
1687             ShowWindow(lpBand->hwndChild, SW_SHOWNOACTIVATE | SW_SHOWNORMAL);
1688             /* above in trace from WinRAR */
1689         }
1690         else {
1691             TRACE("child: %p  prev parent: %p\n",
1692                    lpBand->hwndChild, lpBand->hwndPrevParent);
1693             lpBand->hwndChild = 0;
1694             lpBand->hwndPrevParent = 0;
1695         }
1696         uChanged |= RBBIM_CHILD;
1697     }
1698
1699     if( (lprbbi->fMask & RBBIM_CHILDSIZE) &&
1700         ( (lpBand->cxMinChild != lprbbi->cxMinChild) ||
1701           (lpBand->cyMinChild != lprbbi->cyMinChild ) ||
1702           ( (lprbbi->cbSize >= sizeof (REBARBANDINFOA) && (lpBand->fStyle & RBBS_VARIABLEHEIGHT)) &&
1703             ( (lpBand->cyChild    != lprbbi->cyChild ) ||
1704               (lpBand->cyMaxChild != lprbbi->cyMaxChild ) ||
1705               (lpBand->cyIntegral != lprbbi->cyIntegral ) ) ) ||
1706           ( (lprbbi->cbSize < sizeof (REBARBANDINFOA)) &&
1707             ( (lpBand->cyChild || 
1708                lpBand->cyMaxChild || 
1709                lpBand->cyIntegral ) ) ) ) )
1710     {
1711         lpBand->cxMinChild = lprbbi->cxMinChild;
1712         lpBand->cyMinChild = lprbbi->cyMinChild;
1713         /* These fields where added in WIN32_IE == 0x400 and are set only for RBBS_VARIABLEHEIGHT bands */
1714         if (lprbbi->cbSize >= sizeof (REBARBANDINFOA) && (lpBand->fStyle & RBBS_VARIABLEHEIGHT)) {
1715             lpBand->cyMaxChild = lprbbi->cyMaxChild;
1716             lpBand->cyIntegral = lprbbi->cyIntegral;
1717
1718             lpBand->cyChild = round_child_height(lpBand, lprbbi->cyChild);  /* make (cyChild - cyMinChild) a multiple of cyIntergral */
1719         }
1720         else {
1721             lpBand->cyChild    = lpBand->cyMinChild;
1722             lpBand->cyMaxChild = 0x7fffffff;
1723             lpBand->cyIntegral = 0;
1724         }
1725         uChanged |= RBBIM_CHILDSIZE;
1726     }
1727
1728     if( (lprbbi->fMask & RBBIM_SIZE) &&
1729         (lpBand->cx != lprbbi->cx ) )
1730     {
1731         lpBand->cx = lprbbi->cx;
1732         uChanged |= RBBIM_SIZE;
1733     }
1734
1735     if( (lprbbi->fMask & RBBIM_BACKGROUND) &&
1736        ( lpBand->hbmBack != lprbbi->hbmBack ) )
1737     {
1738         lpBand->hbmBack = lprbbi->hbmBack;
1739         uChanged |= RBBIM_BACKGROUND;
1740     }
1741
1742     if( (lprbbi->fMask & RBBIM_ID) &&
1743         (lpBand->wID != lprbbi->wID ) )
1744     {
1745         lpBand->wID = lprbbi->wID;
1746         uChanged |= RBBIM_ID;
1747     }
1748
1749     /* check for additional data */
1750     if (lprbbi->cbSize >= sizeof (REBARBANDINFOA)) {
1751         if( (lprbbi->fMask & RBBIM_IDEALSIZE) &&
1752             ( lpBand->cxIdeal != lprbbi->cxIdeal ) )
1753         {
1754             lpBand->cxIdeal = lprbbi->cxIdeal;
1755             uChanged |= RBBIM_IDEALSIZE;
1756         }
1757
1758         if( (lprbbi->fMask & RBBIM_LPARAM) &&
1759             (lpBand->lParam != lprbbi->lParam ) )
1760         {
1761             lpBand->lParam = lprbbi->lParam;
1762             uChanged |= RBBIM_LPARAM;
1763         }
1764
1765         if( (lprbbi->fMask & RBBIM_HEADERSIZE) &&
1766             (lpBand->cxHeader != lprbbi->cxHeader ) )
1767         {
1768             lpBand->cxHeader = lprbbi->cxHeader;
1769             lpBand->fStyle |= RBBS_UNDOC_FIXEDHEADER;
1770             uChanged |= RBBIM_HEADERSIZE;
1771         }
1772     }
1773
1774     return uChanged;
1775 }
1776
1777 static LRESULT
1778 REBAR_InternalEraseBkGnd (const REBAR_INFO *infoPtr, WPARAM wParam, const RECT *clip)
1779      /* Function:  This erases the background rectangle by drawing  */
1780      /*  each band with its background color (or the default) and   */
1781      /*  draws each bands right separator if necessary. The row     */
1782      /*  separators are drawn on the first band of the next row.    */
1783 {
1784     REBAR_BAND *lpBand;
1785     UINT i;
1786     INT oldrow;
1787     HDC hdc = (HDC)wParam;
1788     RECT cr;
1789     COLORREF old = CLR_NONE, new;
1790     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
1791
1792     GetClientRect (infoPtr->hwndSelf, &cr);
1793
1794     oldrow = -1;
1795     for(i=0; i<infoPtr->uNumBands; i++) {
1796         RECT rcBand;
1797         lpBand = &infoPtr->bands[i];
1798         if (HIDDENBAND(lpBand)) continue;
1799         translate_rect(infoPtr, &rcBand, &lpBand->rcBand);
1800
1801         /* draw band separator between rows */
1802         if (lpBand->iRow != oldrow) {
1803             oldrow = lpBand->iRow;
1804             if (infoPtr->dwStyle & RBS_BANDBORDERS) {
1805                 RECT rcRowSep;
1806                 rcRowSep = rcBand;
1807                 if (infoPtr->dwStyle & CCS_VERT) {
1808                     rcRowSep.right += SEP_WIDTH_SIZE;
1809                     rcRowSep.bottom = infoPtr->calcSize.cx;
1810                     if (theme)
1811                         DrawThemeEdge (theme, hdc, RP_BAND, 0, &rcRowSep, EDGE_ETCHED, BF_RIGHT, NULL);
1812                     else
1813                         DrawEdge (hdc, &rcRowSep, EDGE_ETCHED, BF_RIGHT);
1814                 }
1815                 else {
1816                     rcRowSep.bottom += SEP_WIDTH_SIZE;
1817                     rcRowSep.right = infoPtr->calcSize.cx;
1818                     if (theme)
1819                         DrawThemeEdge (theme, hdc, RP_BAND, 0, &rcRowSep, EDGE_ETCHED, BF_BOTTOM, NULL);
1820                     else
1821                         DrawEdge (hdc, &rcRowSep, EDGE_ETCHED, BF_BOTTOM);
1822                 }
1823                 TRACE ("drawing band separator bottom (%s)\n",
1824                        wine_dbgstr_rect(&rcRowSep));
1825             }
1826         }
1827
1828         /* draw band separator between bands in a row */
1829         if (infoPtr->dwStyle & RBS_BANDBORDERS && lpBand->rcBand.left > 0) {
1830             RECT rcSep;
1831             rcSep = rcBand;
1832             if (infoPtr->dwStyle & CCS_VERT) {
1833                 rcSep.bottom = rcSep.top;
1834                 rcSep.top -= SEP_WIDTH_SIZE;
1835                 if (theme)
1836                     DrawThemeEdge (theme, hdc, RP_BAND, 0, &rcSep, EDGE_ETCHED, BF_BOTTOM, NULL);
1837                 else
1838                     DrawEdge (hdc, &rcSep, EDGE_ETCHED, BF_BOTTOM);
1839             }
1840             else {
1841                 rcSep.right = rcSep.left;
1842                 rcSep.left -= SEP_WIDTH_SIZE;
1843                 if (theme)
1844                     DrawThemeEdge (theme, hdc, RP_BAND, 0, &rcSep, EDGE_ETCHED, BF_RIGHT, NULL);
1845                 else
1846                     DrawEdge (hdc, &rcSep, EDGE_ETCHED, BF_RIGHT);
1847             }
1848             TRACE("drawing band separator right (%s)\n",
1849                   wine_dbgstr_rect(&rcSep));
1850         }
1851
1852         /* draw the actual background */
1853         if (lpBand->clrBack != CLR_NONE) {
1854             new = (lpBand->clrBack == CLR_DEFAULT) ? infoPtr->clrBtnFace :
1855                     lpBand->clrBack;
1856 #if GLATESTING
1857             /* testing only - make background green to see it */
1858             new = RGB(0,128,0);
1859 #endif
1860         }
1861         else {
1862             /* In the absence of documentation for Rebar vs. CLR_NONE,
1863              * we will use the default BtnFace color. Note documentation
1864              * exists for Listview and Imagelist.
1865              */
1866             new = infoPtr->clrBtnFace;
1867 #if GLATESTING
1868             /* testing only - make background green to see it */
1869             new = RGB(0,128,0);
1870 #endif
1871         }
1872
1873         if (theme)
1874         {
1875             /* When themed, the background color is ignored (but not a
1876              * background bitmap */
1877             DrawThemeBackground (theme, hdc, 0, 0, &cr, &rcBand);
1878         }
1879         else
1880         {
1881             old = SetBkColor (hdc, new);
1882             TRACE("%s background color=0x%06x, band (%d,%d)-(%d,%d), clip (%d,%d)-(%d,%d)\n",
1883                   (lpBand->clrBack == CLR_NONE) ? "none" :
1884                     ((lpBand->clrBack == CLR_DEFAULT) ? "dft" : ""),
1885                   GetBkColor(hdc),
1886                   rcBand.left,rcBand.top,
1887                   rcBand.right,rcBand.bottom,
1888                   clip->left, clip->top,
1889                   clip->right, clip->bottom);
1890             ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &rcBand, NULL, 0, 0);
1891             if (lpBand->clrBack != CLR_NONE)
1892                 SetBkColor (hdc, old);
1893         }
1894     }
1895     return TRUE;
1896 }
1897
1898 static void
1899 REBAR_InternalHitTest (const REBAR_INFO *infoPtr, const POINT *lpPt, UINT *pFlags, INT *pBand)
1900 {
1901     REBAR_BAND *lpBand;
1902     RECT rect;
1903     UINT  iCount;
1904
1905     GetClientRect (infoPtr->hwndSelf, &rect);
1906
1907     *pFlags = RBHT_NOWHERE;
1908     if (PtInRect (&rect, *lpPt))
1909     {
1910         if (infoPtr->uNumBands == 0) {
1911             *pFlags = RBHT_NOWHERE;
1912             if (pBand)
1913                 *pBand = -1;
1914             TRACE("NOWHERE\n");
1915             return;
1916         }
1917         else {
1918             /* somewhere inside */
1919             for (iCount = 0; iCount < infoPtr->uNumBands; iCount++) {
1920                 RECT rcBand;
1921                 lpBand = &infoPtr->bands[iCount];
1922                 translate_rect(infoPtr, &rcBand, &lpBand->rcBand);
1923                 if (HIDDENBAND(lpBand)) continue;
1924                 if (PtInRect (&rcBand, *lpPt)) {
1925                     if (pBand)
1926                         *pBand = iCount;
1927                     if (PtInRect (&lpBand->rcGripper, *lpPt)) {
1928                         *pFlags = RBHT_GRABBER;
1929                         TRACE("ON GRABBER %d\n", iCount);
1930                         return;
1931                     }
1932                     else if (PtInRect (&lpBand->rcCapImage, *lpPt)) {
1933                         *pFlags = RBHT_CAPTION;
1934                         TRACE("ON CAPTION %d\n", iCount);
1935                         return;
1936                     }
1937                     else if (PtInRect (&lpBand->rcCapText, *lpPt)) {
1938                         *pFlags = RBHT_CAPTION;
1939                         TRACE("ON CAPTION %d\n", iCount);
1940                         return;
1941                     }
1942                     else if (PtInRect (&lpBand->rcChild, *lpPt)) {
1943                         *pFlags = RBHT_CLIENT;
1944                         TRACE("ON CLIENT %d\n", iCount);
1945                         return;
1946                     }
1947                     else if (PtInRect (&lpBand->rcChevron, *lpPt)) {
1948                         *pFlags = RBHT_CHEVRON;
1949                         TRACE("ON CHEVRON %d\n", iCount);
1950                         return;
1951                     }
1952                     else {
1953                         *pFlags = RBHT_NOWHERE;
1954                         TRACE("NOWHERE %d\n", iCount);
1955                         return;
1956                     }
1957                 }
1958             }
1959
1960             *pFlags = RBHT_NOWHERE;
1961             if (pBand)
1962                 *pBand = -1;
1963
1964             TRACE("NOWHERE\n");
1965             return;
1966         }
1967     }
1968     else {
1969         *pFlags = RBHT_NOWHERE;
1970         if (pBand)
1971             *pBand = -1;
1972         TRACE("NOWHERE\n");
1973         return;
1974     }
1975 }
1976
1977 static void
1978 REBAR_HandleLRDrag (REBAR_INFO *infoPtr, const POINT *ptsmove)
1979      /* Function:  This will implement the functionality of a     */
1980      /*  Gripper drag within a row. It will not implement "out-   */
1981      /*  of-row" drags. (They are detected and handled in         */
1982      /*  REBAR_MouseMove.)                                        */
1983      /*  **** FIXME Switching order of bands in a row not   ****  */
1984      /*  ****       yet implemented.                        ****  */
1985 {
1986     REBAR_BAND *hitBand;
1987     INT iHitBand, iRowBegin, iRowEnd;
1988     INT movement, xBand;
1989
1990     /* on first significant mouse movement, issue notify */
1991     if (!(infoPtr->fStatus & BEGIN_DRAG_ISSUED)) {
1992         if (REBAR_Notify_NMREBAR (infoPtr, -1, RBN_BEGINDRAG)) {
1993             /* Notify returned TRUE - abort drag */
1994             infoPtr->dragStart.x = 0;
1995             infoPtr->dragStart.y = 0;
1996             infoPtr->dragNow = infoPtr->dragStart;
1997             infoPtr->iGrabbedBand = -1;
1998             ReleaseCapture ();
1999             return ;
2000         }
2001         infoPtr->fStatus |= BEGIN_DRAG_ISSUED;
2002     }
2003
2004     iHitBand = infoPtr->iGrabbedBand;
2005     iRowBegin = get_row_begin_for_band(infoPtr, iHitBand);
2006     iRowEnd = get_row_end_for_band(infoPtr, iHitBand);
2007     hitBand = &infoPtr->bands[iHitBand];
2008
2009     xBand = hitBand->rcBand.left;
2010     movement = (infoPtr->dwStyle&CCS_VERT ? ptsmove->y : ptsmove->x)
2011                     - (xBand + REBAR_PRE_GRIPPER - infoPtr->ihitoffset);
2012
2013     if (movement < 0) {
2014         int cxLeft = REBAR_ShrinkBandsRTL(infoPtr, iRowBegin, iHitBand, -movement, TRUE);
2015         hitBand->cxEffective += -movement - cxLeft;
2016         hitBand->cx = hitBand->cxEffective;
2017     } else if (movement > 0) {
2018         int cxLeft = REBAR_ShrinkBandsLTR(infoPtr, iHitBand, iRowEnd, movement, TRUE);
2019         REBAR_BAND *lpPrev = &infoPtr->bands[prev_visible(infoPtr, iHitBand)];
2020         lpPrev->cxEffective += movement - cxLeft;
2021         lpPrev->cx = lpPrev->cxEffective;
2022     }
2023
2024     REBAR_SetRowRectsX(infoPtr, iRowBegin, iRowEnd);
2025     if (infoPtr->dwStyle & CCS_VERT)
2026         REBAR_CalcVertBand(infoPtr, 0, infoPtr->uNumBands);
2027     else
2028         REBAR_CalcHorzBand(infoPtr, 0, infoPtr->uNumBands);
2029     REBAR_MoveChildWindows(infoPtr, iRowBegin, iRowEnd);
2030 }
2031
2032
2033
2034 /* << REBAR_BeginDrag >> */
2035
2036
2037 static LRESULT
2038 REBAR_DeleteBand (REBAR_INFO *infoPtr, WPARAM wParam)
2039 {
2040     UINT uBand = (UINT)wParam;
2041     REBAR_BAND *lpBand;
2042
2043     if (uBand >= infoPtr->uNumBands)
2044         return FALSE;
2045
2046     TRACE("deleting band %u!\n", uBand);
2047     lpBand = &infoPtr->bands[uBand];
2048     REBAR_Notify_NMREBAR (infoPtr, uBand, RBN_DELETINGBAND);
2049     /* TODO: a return of 1 should probably cancel the deletion */
2050
2051     if (lpBand->hwndChild)
2052         ShowWindow(lpBand->hwndChild, SW_HIDE);
2053     Free(lpBand->lpText);
2054
2055     infoPtr->uNumBands--;
2056     memmove(&infoPtr->bands[uBand], &infoPtr->bands[uBand+1],
2057         (infoPtr->uNumBands - uBand) * sizeof(REBAR_BAND));
2058     infoPtr->bands = ReAlloc(infoPtr->bands, infoPtr->uNumBands * sizeof(REBAR_BAND));
2059
2060     REBAR_Notify_NMREBAR (infoPtr, -1, RBN_DELETEDBAND);
2061
2062     /* if only 1 band left the re-validate to possible eliminate gripper */
2063     if (infoPtr->uNumBands == 1)
2064       REBAR_ValidateBand (infoPtr, &infoPtr->bands[0]);
2065
2066     REBAR_Layout(infoPtr);
2067
2068     return TRUE;
2069 }
2070
2071
2072 /* << REBAR_DragMove >> */
2073 /* << REBAR_EndDrag >> */
2074
2075
2076 static LRESULT
2077 REBAR_GetBandBorders (const REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2078 {
2079     LPRECT lpRect = (LPRECT)lParam;
2080     REBAR_BAND *lpBand;
2081
2082     if (!lParam)
2083         return 0;
2084     if ((UINT)wParam >= infoPtr->uNumBands)
2085         return 0;
2086
2087     lpBand = &infoPtr->bands[(UINT)wParam];
2088
2089     /* FIXME - the following values were determined by experimentation */
2090     /* with the REBAR Control Spy. I have guesses as to what the 4 and */
2091     /* 1 are, but I am not sure. There doesn't seem to be any actual   */
2092     /* difference in size of the control area with and without the     */
2093     /* style.  -  GA                                                   */
2094     if (infoPtr->dwStyle & RBS_BANDBORDERS) {
2095         if (infoPtr->dwStyle & CCS_VERT) {
2096             lpRect->left = 1;
2097             lpRect->top = lpBand->cxHeader + 4;
2098             lpRect->right = 1;
2099             lpRect->bottom = 0;
2100         }
2101         else {
2102             lpRect->left = lpBand->cxHeader + 4;
2103             lpRect->top = 1;
2104             lpRect->right = 0;
2105             lpRect->bottom = 1;
2106         }
2107     }
2108     else {
2109         lpRect->left = lpBand->cxHeader;
2110     }
2111     return 0;
2112 }
2113
2114
2115 static inline LRESULT
2116 REBAR_GetBandCount (const REBAR_INFO *infoPtr)
2117 {
2118     TRACE("band count %u!\n", infoPtr->uNumBands);
2119
2120     return infoPtr->uNumBands;
2121 }
2122
2123
2124 static LRESULT
2125 REBAR_GetBandInfoT(const REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam, BOOL bUnicode)
2126 {
2127     LPREBARBANDINFOW lprbbi = (LPREBARBANDINFOW)lParam;
2128     REBAR_BAND *lpBand;
2129
2130     if (lprbbi == NULL)
2131         return FALSE;
2132     if (lprbbi->cbSize < REBARBANDINFOA_V3_SIZE)
2133         return FALSE;
2134     if ((UINT)wParam >= infoPtr->uNumBands)
2135         return FALSE;
2136
2137     TRACE("index %u (bUnicode=%d)\n", (UINT)wParam, bUnicode);
2138
2139     /* copy band information */
2140     lpBand = &infoPtr->bands[(UINT)wParam];
2141
2142     if (lprbbi->fMask & RBBIM_STYLE)
2143         lprbbi->fStyle = lpBand->fStyle;
2144
2145     if (lprbbi->fMask & RBBIM_COLORS) {
2146         lprbbi->clrFore = lpBand->clrFore;
2147         lprbbi->clrBack = lpBand->clrBack;
2148         if (lprbbi->clrBack == CLR_DEFAULT)
2149             lprbbi->clrBack = infoPtr->clrBtnFace;
2150     }
2151
2152     if (lprbbi->fMask & RBBIM_TEXT) {
2153         if (bUnicode)
2154             Str_GetPtrW(lpBand->lpText, lprbbi->lpText, lprbbi->cch);
2155         else
2156             Str_GetPtrWtoA(lpBand->lpText, (LPSTR)lprbbi->lpText, lprbbi->cch);
2157     }
2158
2159     if (lprbbi->fMask & RBBIM_IMAGE)
2160         lprbbi->iImage = lpBand->iImage;
2161
2162     if (lprbbi->fMask & RBBIM_CHILD)
2163         lprbbi->hwndChild = lpBand->hwndChild;
2164
2165     if (lprbbi->fMask & RBBIM_CHILDSIZE) {
2166         lprbbi->cxMinChild = lpBand->cxMinChild;
2167         lprbbi->cyMinChild = lpBand->cyMinChild;
2168         /* to make tests pass we follow Windows behaviour and allow to read these fields only
2169          * for RBBS_VARIABLEHEIGHTS bands */
2170         if (lprbbi->cbSize >= sizeof (REBARBANDINFOA) && (lpBand->fStyle & RBBS_VARIABLEHEIGHT)) {
2171             lprbbi->cyChild    = lpBand->cyChild;
2172             lprbbi->cyMaxChild = lpBand->cyMaxChild;
2173             lprbbi->cyIntegral = lpBand->cyIntegral;
2174         }
2175     }
2176
2177     if (lprbbi->fMask & RBBIM_SIZE)
2178         lprbbi->cx = lpBand->cx;
2179
2180     if (lprbbi->fMask & RBBIM_BACKGROUND)
2181         lprbbi->hbmBack = lpBand->hbmBack;
2182
2183     if (lprbbi->fMask & RBBIM_ID)
2184         lprbbi->wID = lpBand->wID;
2185
2186     /* check for additional data */
2187     if (lprbbi->cbSize >= sizeof (REBARBANDINFOA)) {
2188         if (lprbbi->fMask & RBBIM_IDEALSIZE)
2189             lprbbi->cxIdeal = lpBand->cxIdeal;
2190
2191         if (lprbbi->fMask & RBBIM_LPARAM)
2192             lprbbi->lParam = lpBand->lParam;
2193
2194         if (lprbbi->fMask & RBBIM_HEADERSIZE)
2195             lprbbi->cxHeader = lpBand->cxHeader;
2196     }
2197
2198     REBAR_DumpBandInfo(lprbbi);
2199
2200     return TRUE;
2201 }
2202
2203
2204 static LRESULT
2205 REBAR_GetBarHeight (const REBAR_INFO *infoPtr)
2206 {
2207     INT nHeight;
2208
2209     nHeight = infoPtr->calcSize.cy;
2210
2211     TRACE("height = %d\n", nHeight);
2212
2213     return nHeight;
2214 }
2215
2216
2217 static LRESULT
2218 REBAR_GetBarInfo (const REBAR_INFO *infoPtr, LPARAM lParam)
2219 {
2220     LPREBARINFO lpInfo = (LPREBARINFO)lParam;
2221
2222     if (lpInfo == NULL)
2223         return FALSE;
2224
2225     if (lpInfo->cbSize < sizeof (REBARINFO))
2226         return FALSE;
2227
2228     TRACE("getting bar info!\n");
2229
2230     if (infoPtr->himl) {
2231         lpInfo->himl = infoPtr->himl;
2232         lpInfo->fMask |= RBIM_IMAGELIST;
2233     }
2234
2235     return TRUE;
2236 }
2237
2238
2239 static inline LRESULT
2240 REBAR_GetBkColor (const REBAR_INFO *infoPtr)
2241 {
2242     COLORREF clr = infoPtr->clrBk;
2243
2244     if (clr == CLR_DEFAULT)
2245       clr = infoPtr->clrBtnFace;
2246
2247     TRACE("background color 0x%06x!\n", clr);
2248
2249     return clr;
2250 }
2251
2252
2253 /* << REBAR_GetColorScheme >> */
2254 /* << REBAR_GetDropTarget >> */
2255
2256
2257 static LRESULT
2258 REBAR_GetPalette (const REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2259 {
2260     FIXME("empty stub!\n");
2261
2262     return 0;
2263 }
2264
2265
2266 static LRESULT
2267 REBAR_GetRect (const REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2268 {
2269     INT iBand = (INT)wParam;
2270     LPRECT lprc = (LPRECT)lParam;
2271     REBAR_BAND *lpBand;
2272
2273     if ((iBand < 0) || ((UINT)iBand >= infoPtr->uNumBands))
2274         return FALSE;
2275     if (!lprc)
2276         return FALSE;
2277
2278     lpBand = &infoPtr->bands[iBand];
2279     /* For CCS_VERT the coordinates will be swapped - like on Windows */
2280     CopyRect (lprc, &lpBand->rcBand);
2281
2282     TRACE("band %d, (%s)\n", iBand, wine_dbgstr_rect(lprc));
2283
2284     return TRUE;
2285 }
2286
2287
2288 static inline LRESULT
2289 REBAR_GetRowCount (const REBAR_INFO *infoPtr)
2290 {
2291     TRACE("%u\n", infoPtr->uNumRows);
2292
2293     return infoPtr->uNumRows;
2294 }
2295
2296
2297 static LRESULT
2298 REBAR_GetRowHeight (const REBAR_INFO *infoPtr, WPARAM wParam)
2299 {
2300     INT iRow = (INT)wParam;
2301     int j = 0, ret = 0;
2302     UINT i;
2303     REBAR_BAND *lpBand;
2304
2305     for (i=0; i<infoPtr->uNumBands; i++) {
2306         lpBand = &infoPtr->bands[i];
2307         if (HIDDENBAND(lpBand)) continue;
2308         if (lpBand->iRow != iRow) continue;
2309         j = lpBand->rcBand.bottom - lpBand->rcBand.top;
2310         if (j > ret) ret = j;
2311     }
2312
2313     TRACE("row %d, height %d\n", iRow, ret);
2314
2315     return ret;
2316 }
2317
2318
2319 static inline LRESULT
2320 REBAR_GetTextColor (const REBAR_INFO *infoPtr)
2321 {
2322     TRACE("text color 0x%06x!\n", infoPtr->clrText);
2323
2324     return infoPtr->clrText;
2325 }
2326
2327
2328 static inline LRESULT
2329 REBAR_GetToolTips (const REBAR_INFO *infoPtr)
2330 {
2331     return (LRESULT)infoPtr->hwndToolTip;
2332 }
2333
2334
2335 static inline LRESULT
2336 REBAR_GetUnicodeFormat (const REBAR_INFO *infoPtr)
2337 {
2338     TRACE("%s hwnd=%p\n",
2339           infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
2340
2341     return infoPtr->bUnicode;
2342 }
2343
2344
2345 static inline LRESULT
2346 REBAR_GetVersion (const REBAR_INFO *infoPtr)
2347 {
2348     TRACE("version %d\n", infoPtr->iVersion);
2349     return infoPtr->iVersion;
2350 }
2351
2352
2353 static LRESULT
2354 REBAR_HitTest (const REBAR_INFO *infoPtr, LPARAM lParam)
2355 {
2356     LPRBHITTESTINFO lprbht = (LPRBHITTESTINFO)lParam;
2357
2358     if (!lprbht)
2359         return -1;
2360
2361     REBAR_InternalHitTest (infoPtr, &lprbht->pt, &lprbht->flags, &lprbht->iBand);
2362
2363     return lprbht->iBand;
2364 }
2365
2366
2367 static LRESULT
2368 REBAR_IdToIndex (const REBAR_INFO *infoPtr, WPARAM wParam)
2369 {
2370     UINT i;
2371
2372     if (infoPtr == NULL)
2373         return -1;
2374
2375     if (infoPtr->uNumBands < 1)
2376         return -1;
2377
2378     for (i = 0; i < infoPtr->uNumBands; i++) {
2379         if (infoPtr->bands[i].wID == (UINT)wParam) {
2380             TRACE("id %u is band %u found!\n", (UINT)wParam, i);
2381             return i;
2382         }
2383     }
2384
2385     TRACE("id %u is not found\n", (UINT)wParam);
2386     return -1;
2387 }
2388
2389
2390 static LRESULT
2391 REBAR_InsertBandT(REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam, BOOL bUnicode)
2392 {
2393     LPREBARBANDINFOW lprbbi = (LPREBARBANDINFOW)lParam;
2394     UINT uIndex = (UINT)wParam;
2395     REBAR_BAND *lpBand;
2396
2397     if (infoPtr == NULL)
2398         return FALSE;
2399     if (lprbbi == NULL)
2400         return FALSE;
2401     if (lprbbi->cbSize < REBARBANDINFOA_V3_SIZE)
2402         return FALSE;
2403
2404     /* trace the index as signed to see the -1 */
2405     TRACE("insert band at %d (bUnicode=%d)!\n", (INT)uIndex, bUnicode);
2406     REBAR_DumpBandInfo(lprbbi);
2407
2408     infoPtr->bands = ReAlloc(infoPtr->bands, (infoPtr->uNumBands+1) * sizeof(REBAR_BAND));
2409     if (((INT)uIndex == -1) || (uIndex > infoPtr->uNumBands))
2410         uIndex = infoPtr->uNumBands;
2411     memmove(&infoPtr->bands[uIndex+1], &infoPtr->bands[uIndex],
2412         sizeof(REBAR_BAND) * (infoPtr->uNumBands - uIndex));
2413     infoPtr->uNumBands++;
2414
2415     TRACE("index %u!\n", uIndex);
2416
2417     /* initialize band (infoPtr->bands[uIndex])*/
2418     lpBand = &infoPtr->bands[uIndex];
2419     ZeroMemory(lpBand, sizeof(*lpBand));
2420     lpBand->clrFore = infoPtr->clrText;
2421     lpBand->clrBack = infoPtr->clrBk;
2422     lpBand->iImage = -1;
2423
2424     REBAR_CommonSetupBand(infoPtr->hwndSelf, lprbbi, lpBand);
2425
2426     /* Make sure the defaults for these are correct */
2427     if (lprbbi->cbSize < sizeof (REBARBANDINFOA) || !(lpBand->fStyle & RBBS_VARIABLEHEIGHT)) {
2428         lpBand->cyChild    = lpBand->cyMinChild;
2429         lpBand->cyMaxChild = 0x7fffffff;
2430         lpBand->cyIntegral = 0;
2431     }
2432
2433     if ((lprbbi->fMask & RBBIM_TEXT) && (lprbbi->lpText)) {
2434         if (bUnicode)
2435             Str_SetPtrW(&lpBand->lpText, lprbbi->lpText);
2436         else
2437             Str_SetPtrAtoW(&lpBand->lpText, (LPSTR)lprbbi->lpText);
2438     }
2439
2440     REBAR_ValidateBand (infoPtr, lpBand);
2441     /* On insert of second band, revalidate band 1 to possible add gripper */
2442     if (infoPtr->uNumBands == 2)
2443         REBAR_ValidateBand (infoPtr, &infoPtr->bands[0]);
2444
2445     REBAR_DumpBand (infoPtr);
2446
2447     REBAR_Layout(infoPtr);
2448     InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
2449
2450     return TRUE;
2451 }
2452
2453
2454 static LRESULT
2455 REBAR_MaximizeBand (const REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2456 {
2457     REBAR_BAND *lpBand;
2458     UINT uBand = (UINT) wParam;
2459     int iRowBegin, iRowEnd;
2460     int cxDesired, extra, extraOrig;
2461     int cxIdealBand;
2462
2463     /* Validate */
2464     if ((infoPtr->uNumBands == 0) ||
2465         ((INT)uBand < 0) || (uBand >= infoPtr->uNumBands)) {
2466         /* error !!! */
2467         ERR("Illegal MaximizeBand, requested=%d, current band count=%d\n",
2468               (INT)uBand, infoPtr->uNumBands);
2469         return FALSE;
2470     }
2471
2472     lpBand = &infoPtr->bands[uBand];
2473
2474     if (lpBand->fStyle & RBBS_HIDDEN)
2475     {
2476         /* Windows is buggy and creates a hole */
2477         WARN("Ignoring maximize request on a hidden band (%d)\n", uBand);
2478         return FALSE;
2479     }
2480
2481     cxIdealBand = lpBand->cxIdeal + lpBand->cxHeader + REBAR_POST_CHILD;
2482     if (lParam && (lpBand->cxEffective < cxIdealBand))
2483         cxDesired = cxIdealBand;
2484     else
2485         cxDesired = infoPtr->calcSize.cx;
2486
2487     iRowBegin = get_row_begin_for_band(infoPtr, uBand);
2488     iRowEnd   = get_row_end_for_band(infoPtr, uBand);
2489     extraOrig = extra = cxDesired - lpBand->cxEffective;
2490     if (extra > 0)
2491         extra = REBAR_ShrinkBandsRTL(infoPtr, iRowBegin, uBand, extra, TRUE);
2492     if (extra > 0)
2493         extra = REBAR_ShrinkBandsLTR(infoPtr, next_visible(infoPtr, uBand), iRowEnd, extra, TRUE);
2494     lpBand->cxEffective += extraOrig - extra;
2495     lpBand->cx = lpBand->cxEffective;
2496     TRACE("(%ld, %ld): Wanted size %d, obtained %d (shrink %d, %d)\n", wParam, lParam, cxDesired, lpBand->cx, extraOrig, extra);
2497     REBAR_SetRowRectsX(infoPtr, iRowBegin, iRowEnd);
2498
2499     if (infoPtr->dwStyle & CCS_VERT)
2500         REBAR_CalcVertBand(infoPtr, iRowBegin, iRowEnd);
2501     else
2502         REBAR_CalcHorzBand(infoPtr, iRowBegin, iRowEnd);
2503     REBAR_MoveChildWindows(infoPtr, iRowBegin, iRowEnd);
2504     return TRUE;
2505
2506 }
2507
2508
2509 static LRESULT
2510 REBAR_MinimizeBand (const REBAR_INFO *infoPtr, WPARAM wParam)
2511 {
2512     REBAR_BAND *lpBand;
2513     UINT uBand = (UINT) wParam;
2514     int iPrev, iRowBegin, iRowEnd;
2515
2516     /* A "minimize" band is equivalent to "dragging" the gripper
2517      * of than band to the right till the band is only the size
2518      * of the cxHeader.
2519      */
2520
2521     /* Validate */
2522     if ((infoPtr->uNumBands == 0) ||
2523         ((INT)uBand < 0) || (uBand >= infoPtr->uNumBands)) {
2524         /* error !!! */
2525         ERR("Illegal MinimizeBand, requested=%d, current band count=%d\n",
2526               (INT)uBand, infoPtr->uNumBands);
2527         return FALSE;
2528     }
2529
2530     /* compute amount of movement and validate */
2531     lpBand = &infoPtr->bands[uBand];
2532
2533     if (lpBand->fStyle & RBBS_HIDDEN)
2534     {
2535         /* Windows is buggy and creates a hole/overlap */
2536         WARN("Ignoring minimize request on a hidden band (%d)\n", uBand);
2537         return FALSE;
2538     }
2539
2540     iPrev = prev_visible(infoPtr, uBand);
2541     /* if first band in row */
2542     if (iPrev < 0 || infoPtr->bands[iPrev].iRow != lpBand->iRow) {
2543         int iNext = next_visible(infoPtr, uBand);
2544         if (iNext < infoPtr->uNumBands && infoPtr->bands[iNext].iRow == lpBand->iRow) {
2545             TRACE("(%ld): Minimizing the first band in row is by maximizing the second\n", wParam);
2546             REBAR_MaximizeBand(infoPtr, iNext, FALSE);
2547         }
2548         else
2549             TRACE("(%ld): Only one band in row - nothing to do\n", wParam);
2550         return TRUE;
2551     }
2552
2553     infoPtr->bands[iPrev].cxEffective += lpBand->cxEffective - lpBand->cxMinBand;
2554     infoPtr->bands[iPrev].cx = infoPtr->bands[iPrev].cxEffective;
2555     lpBand->cx = lpBand->cxEffective = lpBand->cxMinBand;
2556
2557     iRowBegin = get_row_begin_for_band(infoPtr, uBand);
2558     iRowEnd = get_row_end_for_band(infoPtr, uBand);
2559     REBAR_SetRowRectsX(infoPtr, iRowBegin, iRowEnd);
2560
2561     if (infoPtr->dwStyle & CCS_VERT)
2562         REBAR_CalcVertBand(infoPtr, iRowBegin, iRowEnd);
2563     else
2564         REBAR_CalcHorzBand(infoPtr, iRowBegin, iRowEnd);
2565     REBAR_MoveChildWindows(infoPtr, iRowBegin, iRowEnd);
2566     return FALSE;
2567 }
2568
2569
2570 static LRESULT
2571 REBAR_MoveBand (REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2572 {
2573     REBAR_BAND *oldBands = infoPtr->bands;
2574     REBAR_BAND holder;
2575     UINT uFrom = (UINT)wParam;
2576     UINT uTo = (UINT)lParam;
2577
2578     /* Validate */
2579     if ((infoPtr->uNumBands == 0) ||
2580         ((INT)uFrom < 0) || (uFrom >= infoPtr->uNumBands) ||
2581         ((INT)uTo < 0)   || (uTo >= infoPtr->uNumBands)) {
2582         /* error !!! */
2583         ERR("Illegal MoveBand, from=%d, to=%d, current band count=%d\n",
2584               (INT)uFrom, (INT)uTo, infoPtr->uNumBands);
2585         return FALSE;
2586     }
2587
2588     /* save one to be moved */
2589     holder = oldBands[uFrom];
2590
2591     /* close up rest of bands (pseudo delete) */
2592     if (uFrom < infoPtr->uNumBands - 1) {
2593         memcpy (&oldBands[uFrom], &oldBands[uFrom+1],
2594                 (infoPtr->uNumBands - uFrom - 1) * sizeof(REBAR_BAND));
2595     }
2596
2597     /* allocate new space and copy rest of bands into it */
2598     infoPtr->bands =
2599         (REBAR_BAND *)Alloc ((infoPtr->uNumBands)*sizeof(REBAR_BAND));
2600
2601     /* pre insert copy */
2602     if (uTo > 0) {
2603         memcpy (&infoPtr->bands[0], &oldBands[0],
2604                 uTo * sizeof(REBAR_BAND));
2605     }
2606
2607     /* set moved band */
2608     infoPtr->bands[uTo] = holder;
2609
2610     /* post copy */
2611     if (uTo < infoPtr->uNumBands - 1) {
2612         memcpy (&infoPtr->bands[uTo+1], &oldBands[uTo],
2613                 (infoPtr->uNumBands - uTo - 1) * sizeof(REBAR_BAND));
2614     }
2615
2616     Free (oldBands);
2617
2618     TRACE("moved band %d to index %d\n", uFrom, uTo);
2619     REBAR_DumpBand (infoPtr);
2620
2621     /* **************************************************** */
2622     /*                                                      */
2623     /* We do not do a REBAR_Layout here because the native  */
2624     /* control does not do that. The actual layout and      */
2625     /* repaint is done by the *next* real action, ex.:      */
2626     /* RB_INSERTBAND, RB_DELETEBAND, RB_SIZETORECT, etc.    */
2627     /*                                                      */
2628     /* **************************************************** */
2629
2630     return TRUE;
2631 }
2632
2633
2634 /* return TRUE if two strings are different */
2635 static BOOL
2636 REBAR_strdifW( LPCWSTR a, LPCWSTR b )
2637 {
2638     return ( (a && !b) || (b && !a) || (a && b && lstrcmpW(a, b) ) );
2639 }
2640
2641 static LRESULT
2642 REBAR_SetBandInfoT(REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam, BOOL bUnicode)
2643 {
2644     LPREBARBANDINFOW lprbbi = (LPREBARBANDINFOW)lParam;
2645     REBAR_BAND *lpBand;
2646     UINT uChanged;
2647
2648     if (lprbbi == NULL)
2649         return FALSE;
2650     if (lprbbi->cbSize < REBARBANDINFOA_V3_SIZE)
2651         return FALSE;
2652     if ((UINT)wParam >= infoPtr->uNumBands)
2653         return FALSE;
2654
2655     TRACE("index %u\n", (UINT)wParam);
2656     REBAR_DumpBandInfo (lprbbi);
2657
2658     /* set band information */
2659     lpBand = &infoPtr->bands[(UINT)wParam];
2660
2661     uChanged = REBAR_CommonSetupBand (infoPtr->hwndSelf, lprbbi, lpBand);
2662     if (lprbbi->fMask & RBBIM_TEXT) {
2663         LPWSTR wstr = NULL;
2664         if (bUnicode)
2665             Str_SetPtrW(&wstr, lprbbi->lpText);
2666         else
2667             Str_SetPtrAtoW(&wstr, (LPSTR)lprbbi->lpText);
2668
2669         if (REBAR_strdifW(wstr, lpBand->lpText)) {
2670             Free(lpBand->lpText);
2671             lpBand->lpText = wstr;
2672             uChanged |= RBBIM_TEXT;
2673         }
2674         else
2675             Free(wstr);
2676     }
2677
2678     REBAR_ValidateBand (infoPtr, lpBand);
2679
2680     REBAR_DumpBand (infoPtr);
2681
2682     if (uChanged & (RBBIM_CHILDSIZE | RBBIM_SIZE | RBBIM_STYLE | RBBIM_IMAGE)) {
2683           REBAR_Layout(infoPtr);
2684           InvalidateRect(infoPtr->hwndSelf, 0, 1);
2685     }
2686
2687     return TRUE;
2688 }
2689
2690
2691 static LRESULT
2692 REBAR_SetBarInfo (REBAR_INFO *infoPtr, LPARAM lParam)
2693 {
2694     LPREBARINFO lpInfo = (LPREBARINFO)lParam;
2695     REBAR_BAND *lpBand;
2696     UINT i;
2697
2698     if (lpInfo == NULL)
2699         return FALSE;
2700
2701     if (lpInfo->cbSize < sizeof (REBARINFO))
2702         return FALSE;
2703
2704     TRACE("setting bar info!\n");
2705
2706     if (lpInfo->fMask & RBIM_IMAGELIST) {
2707         infoPtr->himl = lpInfo->himl;
2708         if (infoPtr->himl) {
2709             INT cx, cy;
2710             ImageList_GetIconSize (infoPtr->himl, &cx, &cy);
2711             infoPtr->imageSize.cx = cx;
2712             infoPtr->imageSize.cy = cy;
2713         }
2714         else {
2715             infoPtr->imageSize.cx = 0;
2716             infoPtr->imageSize.cy = 0;
2717         }
2718         TRACE("new image cx=%d, cy=%d\n", infoPtr->imageSize.cx,
2719               infoPtr->imageSize.cy);
2720     }
2721
2722     /* revalidate all bands to reset flags for images in headers of bands */
2723     for (i=0; i<infoPtr->uNumBands; i++) {
2724         lpBand = &infoPtr->bands[i];
2725         REBAR_ValidateBand (infoPtr, lpBand);
2726     }
2727
2728     return TRUE;
2729 }
2730
2731
2732 static LRESULT
2733 REBAR_SetBkColor (REBAR_INFO *infoPtr, LPARAM lParam)
2734 {
2735     COLORREF clrTemp;
2736
2737     clrTemp = infoPtr->clrBk;
2738     infoPtr->clrBk = (COLORREF)lParam;
2739
2740     TRACE("background color 0x%06x!\n", infoPtr->clrBk);
2741
2742     return clrTemp;
2743 }
2744
2745
2746 /* << REBAR_SetColorScheme >> */
2747 /* << REBAR_SetPalette >> */
2748
2749
2750 static LRESULT
2751 REBAR_SetParent (REBAR_INFO *infoPtr, WPARAM wParam)
2752 {
2753     HWND hwndTemp = infoPtr->hwndNotify;
2754
2755     infoPtr->hwndNotify = (HWND)wParam;
2756
2757     return (LRESULT)hwndTemp;
2758 }
2759
2760
2761 static LRESULT
2762 REBAR_SetTextColor (REBAR_INFO *infoPtr, LPARAM lParam)
2763 {
2764     COLORREF clrTemp;
2765
2766     clrTemp = infoPtr->clrText;
2767     infoPtr->clrText = (COLORREF)lParam;
2768
2769     TRACE("text color 0x%06x!\n", infoPtr->clrText);
2770
2771     return clrTemp;
2772 }
2773
2774
2775 /* << REBAR_SetTooltips >> */
2776
2777
2778 static inline LRESULT
2779 REBAR_SetUnicodeFormat (REBAR_INFO *infoPtr, WPARAM wParam)
2780 {
2781     BOOL bTemp = infoPtr->bUnicode;
2782
2783     TRACE("to %s hwnd=%p, was %s\n",
2784           ((BOOL)wParam) ? "TRUE" : "FALSE", infoPtr->hwndSelf,
2785           (bTemp) ? "TRUE" : "FALSE");
2786
2787     infoPtr->bUnicode = (BOOL)wParam;
2788
2789    return bTemp;
2790 }
2791
2792
2793 static LRESULT
2794 REBAR_SetVersion (REBAR_INFO *infoPtr, INT iVersion)
2795 {
2796     INT iOldVersion = infoPtr->iVersion;
2797
2798     if (iVersion > COMCTL32_VERSION)
2799         return -1;
2800
2801     infoPtr->iVersion = iVersion;
2802
2803     TRACE("new version %d\n", iVersion);
2804
2805     return iOldVersion;
2806 }
2807
2808
2809 static LRESULT
2810 REBAR_ShowBand (REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2811 {
2812     REBAR_BAND *lpBand;
2813
2814     if (((INT)wParam < 0) || ((INT)wParam > infoPtr->uNumBands))
2815         return FALSE;
2816
2817     lpBand = &infoPtr->bands[(INT)wParam];
2818
2819     if ((BOOL)lParam) {
2820         TRACE("show band %d\n", (INT)wParam);
2821         lpBand->fStyle = lpBand->fStyle & ~RBBS_HIDDEN;
2822         if (IsWindow (lpBand->hwndChild))
2823             ShowWindow (lpBand->hwndChild, SW_SHOW);
2824     }
2825     else {
2826         TRACE("hide band %d\n", (INT)wParam);
2827         lpBand->fStyle = lpBand->fStyle | RBBS_HIDDEN;
2828         if (IsWindow (lpBand->hwndChild))
2829             ShowWindow (lpBand->hwndChild, SW_HIDE);
2830     }
2831
2832     REBAR_Layout(infoPtr);
2833     InvalidateRect(infoPtr->hwndSelf, 0, 1);
2834
2835     return TRUE;
2836 }
2837
2838
2839 static LRESULT
2840 REBAR_SizeToRect (REBAR_INFO *infoPtr, LPARAM lParam)
2841 {
2842     LPRECT lpRect = (LPRECT)lParam;
2843
2844     if (lpRect == NULL)
2845        return FALSE;
2846
2847     TRACE("[%s]\n", wine_dbgstr_rect(lpRect));
2848     REBAR_SizeToHeight(infoPtr, get_rect_cy(infoPtr, lpRect));
2849     return TRUE;
2850 }
2851
2852
2853
2854 static LRESULT
2855 REBAR_Create (REBAR_INFO *infoPtr, LPARAM lParam)
2856 {
2857     LPCREATESTRUCTW cs = (LPCREATESTRUCTW) lParam;
2858     RECT wnrc1, clrc1;
2859
2860     if (TRACE_ON(rebar)) {
2861         GetWindowRect(infoPtr->hwndSelf, &wnrc1);
2862         GetClientRect(infoPtr->hwndSelf, &clrc1);
2863         TRACE("window=(%s) client=(%s) cs=(%d,%d %dx%d)\n",
2864               wine_dbgstr_rect(&wnrc1), wine_dbgstr_rect(&clrc1),
2865               cs->x, cs->y, cs->cx, cs->cy);
2866     }
2867
2868     TRACE("created!\n");
2869
2870     if (OpenThemeData (infoPtr->hwndSelf, themeClass))
2871     {
2872         /* native seems to clear WS_BORDER when themed */
2873         infoPtr->dwStyle &= ~WS_BORDER;
2874     }
2875     
2876     return 0;
2877 }
2878
2879
2880 static LRESULT
2881 REBAR_Destroy (REBAR_INFO *infoPtr)
2882 {
2883     REBAR_BAND *lpBand;
2884     UINT i;
2885
2886
2887     /* free rebar bands */
2888     if ((infoPtr->uNumBands > 0) && infoPtr->bands) {
2889         /* clean up each band */
2890         for (i = 0; i < infoPtr->uNumBands; i++) {
2891             lpBand = &infoPtr->bands[i];
2892
2893             /* delete text strings */
2894             Free (lpBand->lpText);
2895             lpBand->lpText = NULL;
2896             /* destroy child window */
2897             DestroyWindow (lpBand->hwndChild);
2898         }
2899
2900         /* free band array */
2901         Free (infoPtr->bands);
2902         infoPtr->bands = NULL;
2903     }
2904
2905     DestroyCursor (infoPtr->hcurArrow);
2906     DestroyCursor (infoPtr->hcurHorz);
2907     DestroyCursor (infoPtr->hcurVert);
2908     DestroyCursor (infoPtr->hcurDrag);
2909     if(infoPtr->hDefaultFont) DeleteObject (infoPtr->hDefaultFont);
2910     SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
2911     
2912     CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2913
2914     /* free rebar info data */
2915     Free (infoPtr);
2916     TRACE("destroyed!\n");
2917     return 0;
2918 }
2919
2920
2921 static LRESULT
2922 REBAR_EraseBkGnd (const REBAR_INFO *infoPtr, WPARAM wParam)
2923 {
2924     RECT cliprect;
2925
2926     if (GetClipBox ( (HDC)wParam, &cliprect))
2927         return REBAR_InternalEraseBkGnd (infoPtr, wParam, &cliprect);
2928     return 0;
2929 }
2930
2931
2932 static LRESULT
2933 REBAR_GetFont (const REBAR_INFO *infoPtr)
2934 {
2935     return (LRESULT)infoPtr->hFont;
2936 }
2937
2938 static LRESULT
2939 REBAR_PushChevron(const REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
2940 {
2941     if ((UINT)wParam < infoPtr->uNumBands)
2942     {
2943         NMREBARCHEVRON nmrbc;
2944         REBAR_BAND *lpBand = &infoPtr->bands[wParam];
2945
2946         TRACE("Pressed chevron on band %ld\n", wParam);
2947
2948         /* redraw chevron in pushed state */
2949         lpBand->fDraw |= DRAW_CHEVRONPUSHED;
2950         RedrawWindow(infoPtr->hwndSelf, &lpBand->rcChevron,0,
2951           RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
2952
2953         /* notify app so it can display a popup menu or whatever */
2954         nmrbc.uBand = wParam;
2955         nmrbc.wID = lpBand->wID;
2956         nmrbc.lParam = lpBand->lParam;
2957         nmrbc.rc = lpBand->rcChevron;
2958         nmrbc.lParamNM = lParam;
2959         REBAR_Notify((NMHDR*)&nmrbc, infoPtr, RBN_CHEVRONPUSHED);
2960
2961         /* redraw chevron in previous state */
2962         lpBand->fDraw &= ~DRAW_CHEVRONPUSHED;
2963         InvalidateRect(infoPtr->hwndSelf, &lpBand->rcChevron, TRUE);
2964
2965         return TRUE;
2966     }
2967     return FALSE;
2968 }
2969
2970 static LRESULT
2971 REBAR_LButtonDown (REBAR_INFO *infoPtr, LPARAM lParam)
2972 {
2973     REBAR_BAND *lpBand;
2974     UINT htFlags;
2975     INT iHitBand;
2976     POINT ptMouseDown;
2977     ptMouseDown.x = (short)LOWORD(lParam);
2978     ptMouseDown.y = (short)HIWORD(lParam);
2979
2980     REBAR_InternalHitTest(infoPtr, &ptMouseDown, &htFlags, &iHitBand);
2981     lpBand = &infoPtr->bands[iHitBand];
2982
2983     if (htFlags == RBHT_CHEVRON)
2984     {
2985         REBAR_PushChevron(infoPtr, iHitBand, 0);
2986     }
2987     else if (htFlags == RBHT_GRABBER || htFlags == RBHT_CAPTION)
2988     {
2989         TRACE("Starting drag\n");
2990
2991         SetCapture (infoPtr->hwndSelf);
2992         infoPtr->iGrabbedBand = iHitBand;
2993
2994         /* save off the LOWORD and HIWORD of lParam as initial x,y */
2995         infoPtr->dragStart.x = (short)LOWORD(lParam);
2996         infoPtr->dragStart.y = (short)HIWORD(lParam);
2997         infoPtr->dragNow = infoPtr->dragStart;
2998         if (infoPtr->dwStyle & CCS_VERT)
2999             infoPtr->ihitoffset = infoPtr->dragStart.y - (lpBand->rcBand.left + REBAR_PRE_GRIPPER);
3000         else
3001             infoPtr->ihitoffset = infoPtr->dragStart.x - (lpBand->rcBand.left + REBAR_PRE_GRIPPER);
3002     }
3003     return 0;
3004 }
3005
3006 static LRESULT
3007 REBAR_LButtonUp (REBAR_INFO *infoPtr)
3008 {
3009     if (infoPtr->iGrabbedBand >= 0)
3010     {
3011         NMHDR layout;
3012         RECT rect;
3013
3014         infoPtr->dragStart.x = 0;
3015         infoPtr->dragStart.y = 0;
3016         infoPtr->dragNow = infoPtr->dragStart;
3017
3018         ReleaseCapture ();
3019
3020         if (infoPtr->fStatus & BEGIN_DRAG_ISSUED) {
3021             REBAR_Notify(&layout, infoPtr, RBN_LAYOUTCHANGED);
3022             REBAR_Notify_NMREBAR (infoPtr, infoPtr->iGrabbedBand, RBN_ENDDRAG);
3023             infoPtr->fStatus &= ~BEGIN_DRAG_ISSUED;
3024         }
3025
3026         infoPtr->iGrabbedBand = -1;
3027
3028         GetClientRect(infoPtr->hwndSelf, &rect);
3029         InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
3030     }
3031
3032     return 0;
3033 }
3034
3035 static LRESULT
3036 REBAR_MouseLeave (REBAR_INFO *infoPtr)
3037 {
3038     if (infoPtr->ichevronhotBand >= 0)
3039     {
3040         REBAR_BAND *lpChevronBand = &infoPtr->bands[infoPtr->ichevronhotBand];
3041         if (lpChevronBand->fDraw & DRAW_CHEVRONHOT)
3042         {
3043             lpChevronBand->fDraw &= ~DRAW_CHEVRONHOT;
3044             InvalidateRect(infoPtr->hwndSelf, &lpChevronBand->rcChevron, TRUE);
3045         }
3046     }
3047     infoPtr->iOldBand = -1;
3048     infoPtr->ichevronhotBand = -2;
3049
3050     return TRUE;
3051 }
3052
3053 static LRESULT
3054 REBAR_MouseMove (REBAR_INFO *infoPtr, LPARAM lParam)
3055 {
3056     REBAR_BAND *lpChevronBand;
3057     POINT ptMove;
3058
3059     ptMove.x = (short)LOWORD(lParam);
3060     ptMove.y = (short)HIWORD(lParam);
3061
3062     /* if we are currently dragging a band */
3063     if (infoPtr->iGrabbedBand >= 0)
3064     {
3065         REBAR_BAND *band1, *band2;
3066         int yPtMove = (infoPtr->dwStyle & CCS_VERT ? ptMove.x : ptMove.y);
3067
3068         if (GetCapture() != infoPtr->hwndSelf)
3069             ERR("We are dragging but haven't got capture?!?\n");
3070
3071         band1 = &infoPtr->bands[infoPtr->iGrabbedBand-1];
3072         band2 = &infoPtr->bands[infoPtr->iGrabbedBand];
3073
3074         /* if mouse did not move much, exit */
3075         if ((abs(ptMove.x - infoPtr->dragNow.x) <= mindragx) &&
3076             (abs(ptMove.y - infoPtr->dragNow.y) <= mindragy)) return 0;
3077
3078         /* Test for valid drag case - must not be first band in row */
3079         if ((yPtMove < band2->rcBand.top) ||
3080               (yPtMove > band2->rcBand.bottom) ||
3081               ((infoPtr->iGrabbedBand > 0) && (band1->iRow != band2->iRow))) {
3082             FIXME("Cannot drag to other rows yet!!\n");
3083         }
3084         else {
3085             REBAR_HandleLRDrag (infoPtr, &ptMove);
3086         }
3087     }
3088     else
3089     {
3090         INT iHitBand;
3091         UINT htFlags;
3092         TRACKMOUSEEVENT trackinfo;
3093
3094         REBAR_InternalHitTest(infoPtr, &ptMove, &htFlags, &iHitBand);
3095
3096         if (infoPtr->iOldBand >= 0 && infoPtr->iOldBand == infoPtr->ichevronhotBand)
3097         {
3098             lpChevronBand = &infoPtr->bands[infoPtr->ichevronhotBand];
3099             if (lpChevronBand->fDraw & DRAW_CHEVRONHOT)
3100             {
3101                 lpChevronBand->fDraw &= ~DRAW_CHEVRONHOT;
3102                 InvalidateRect(infoPtr->hwndSelf, &lpChevronBand->rcChevron, TRUE);
3103             }
3104             infoPtr->ichevronhotBand = -2;
3105         }
3106
3107         if (htFlags == RBHT_CHEVRON)
3108         {
3109             /* fill in the TRACKMOUSEEVENT struct */
3110             trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3111             trackinfo.dwFlags = TME_QUERY;
3112             trackinfo.hwndTrack = infoPtr->hwndSelf;
3113             trackinfo.dwHoverTime = 0;
3114
3115             /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
3116             _TrackMouseEvent(&trackinfo);
3117
3118             /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
3119             if(!(trackinfo.dwFlags & TME_LEAVE))
3120             {
3121                 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
3122
3123                 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
3124                 /* and can properly deactivate the hot chevron */
3125                 _TrackMouseEvent(&trackinfo);
3126             }
3127
3128             lpChevronBand = &infoPtr->bands[iHitBand];
3129             if (!(lpChevronBand->fDraw & DRAW_CHEVRONHOT))
3130             {
3131                 lpChevronBand->fDraw |= DRAW_CHEVRONHOT;
3132                 InvalidateRect(infoPtr->hwndSelf, &lpChevronBand->rcChevron, TRUE);
3133                 infoPtr->ichevronhotBand = iHitBand;
3134             }
3135         }
3136         infoPtr->iOldBand = iHitBand;
3137     }
3138
3139     return 0;
3140 }
3141
3142
3143 static inline LRESULT
3144 REBAR_NCCalcSize (const REBAR_INFO *infoPtr, LPARAM lParam)
3145 {
3146     HTHEME theme;
3147     RECT *rect = (RECT *)lParam;
3148
3149     if (infoPtr->dwStyle & WS_BORDER) {
3150         rect->left   = min(rect->left + GetSystemMetrics(SM_CXEDGE), rect->right);
3151         rect->right  = max(rect->right - GetSystemMetrics(SM_CXEDGE), rect->left);
3152         rect->top    = min(rect->top + GetSystemMetrics(SM_CYEDGE), rect->bottom);
3153         rect->bottom = max(rect->bottom - GetSystemMetrics(SM_CYEDGE), rect->top);
3154     }
3155     else if ((theme = GetWindowTheme (infoPtr->hwndSelf)))
3156     {
3157         /* FIXME: should use GetThemeInt */
3158         rect->top = min(rect->top + 1, rect->bottom);
3159     }
3160     TRACE("new client=(%s)\n", wine_dbgstr_rect(rect));
3161     return 0;
3162 }
3163
3164
3165 static LRESULT
3166 REBAR_NCCreate (HWND hwnd, LPARAM lParam)
3167 {
3168     LPCREATESTRUCTW cs = (LPCREATESTRUCTW) lParam;
3169     REBAR_INFO *infoPtr = REBAR_GetInfoPtr (hwnd);
3170     RECT wnrc1, clrc1;
3171     NONCLIENTMETRICSW ncm;
3172     HFONT tfont;
3173
3174     if (infoPtr != NULL) {
3175         ERR("Strange info structure pointer *not* NULL\n");
3176         return FALSE;
3177     }
3178
3179     if (TRACE_ON(rebar)) {
3180         GetWindowRect(hwnd, &wnrc1);
3181         GetClientRect(hwnd, &clrc1);
3182         TRACE("window=(%s) client=(%s) cs=(%d,%d %dx%d)\n",
3183               wine_dbgstr_rect(&wnrc1), wine_dbgstr_rect(&clrc1),
3184               cs->x, cs->y, cs->cx, cs->cy);
3185     }
3186
3187     /* allocate memory for info structure */
3188     infoPtr = (REBAR_INFO *)Alloc (sizeof(REBAR_INFO));
3189     SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
3190
3191     /* initialize info structure - initial values are 0 */
3192     infoPtr->clrBk = CLR_NONE;
3193     infoPtr->clrText = CLR_NONE;
3194     infoPtr->clrBtnText = GetSysColor (COLOR_BTNTEXT);
3195     infoPtr->clrBtnFace = GetSysColor (COLOR_BTNFACE);
3196     infoPtr->iOldBand = -1;
3197     infoPtr->ichevronhotBand = -2;
3198     infoPtr->iGrabbedBand = -1;
3199     infoPtr->hwndSelf = hwnd;
3200     infoPtr->DoRedraw = TRUE;
3201     infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
3202     infoPtr->hcurHorz  = LoadCursorW (0, (LPWSTR)IDC_SIZEWE);
3203     infoPtr->hcurVert  = LoadCursorW (0, (LPWSTR)IDC_SIZENS);
3204     infoPtr->hcurDrag  = LoadCursorW (0, (LPWSTR)IDC_SIZE);
3205     infoPtr->fStatus = 0;
3206     infoPtr->hFont = GetStockObject (SYSTEM_FONT);
3207
3208     /* issue WM_NOTIFYFORMAT to get unicode status of parent */
3209     REBAR_NotifyFormat(infoPtr, NF_REQUERY);
3210
3211     /* Stow away the original style */
3212     infoPtr->orgStyle = cs->style;
3213     /* add necessary styles to the requested styles */
3214     infoPtr->dwStyle = cs->style | WS_VISIBLE;
3215     if ((infoPtr->dwStyle & CCS_LAYOUT_MASK) == 0)
3216         infoPtr->dwStyle |= CCS_TOP;
3217     SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle);
3218
3219     /* get font handle for Caption Font */
3220     ncm.cbSize = sizeof(ncm);
3221     SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
3222     /* if the font is bold, set to normal */
3223     if (ncm.lfCaptionFont.lfWeight > FW_NORMAL) {
3224         ncm.lfCaptionFont.lfWeight = FW_NORMAL;
3225     }
3226     tfont = CreateFontIndirectW (&ncm.lfCaptionFont);
3227     if (tfont) {
3228         infoPtr->hFont = infoPtr->hDefaultFont = tfont;
3229     }
3230
3231 /* native does:
3232             GetSysColor (numerous);
3233             GetSysColorBrush (numerous) (see WM_SYSCOLORCHANGE);
3234            *GetStockObject (SYSTEM_FONT);
3235            *SetWindowLong (hwnd, 0, info ptr);
3236            *WM_NOTIFYFORMAT;
3237            *SetWindowLong (hwnd, GWL_STYLE, style+0x10000001);
3238                                     WS_VISIBLE = 0x10000000;
3239                                     CCS_TOP    = 0x00000001;
3240            *SystemParametersInfo (SPI_GETNONCLIENTMETRICS...);
3241            *CreateFontIndirect (lfCaptionFont from above);
3242             GetDC ();
3243             SelectObject (hdc, fontabove);
3244             GetTextMetrics (hdc, );    guessing is tmHeight
3245             SelectObject (hdc, oldfont);
3246             ReleaseDC ();
3247             GetWindowRect ();
3248             MapWindowPoints (0, parent, rectabove, 2);
3249             GetWindowRect ();
3250             GetClientRect ();
3251             ClientToScreen (clientrect);
3252             SetWindowPos (hwnd, 0, 0, 0, 0, 0, SWP_NOZORDER);
3253  */
3254     return TRUE;
3255 }
3256
3257
3258 static LRESULT
3259 REBAR_NCHitTest (const REBAR_INFO *infoPtr, LPARAM lParam)
3260 {
3261     NMMOUSE nmmouse;
3262     POINT clpt;
3263     INT i;
3264     UINT scrap;
3265     LRESULT ret = HTCLIENT;
3266
3267     /*
3268      * Differences from doc at MSDN (as observed with version 4.71 of
3269      *      comctl32.dll
3270      * 1. doc says nmmouse.pt is in screen coord, trace shows client coord.
3271      * 2. if band is not identified .dwItemSpec is 0xffffffff.
3272      * 3. native always seems to return HTCLIENT if notify return is 0.
3273      */
3274
3275     clpt.x = (short)LOWORD(lParam);
3276     clpt.y = (short)HIWORD(lParam);
3277     ScreenToClient (infoPtr->hwndSelf, &clpt);
3278     REBAR_InternalHitTest (infoPtr, &clpt, &scrap,
3279                            (INT *)&nmmouse.dwItemSpec);
3280     nmmouse.dwItemData = 0;
3281     nmmouse.pt = clpt;
3282     nmmouse.dwHitInfo = 0;
3283     if ((i = REBAR_Notify((NMHDR *) &nmmouse, infoPtr, NM_NCHITTEST))) {
3284         TRACE("notify changed return value from %ld to %d\n",
3285               ret, i);
3286         ret = (LRESULT) i;
3287     }
3288     TRACE("returning %ld, client point (%d,%d)\n", ret, clpt.x, clpt.y);
3289     return ret;
3290 }
3291
3292
3293 static LRESULT
3294 REBAR_NCPaint (const REBAR_INFO *infoPtr)
3295 {
3296     RECT rcWindow;
3297     HDC hdc;
3298     HTHEME theme;
3299
3300     if (infoPtr->dwStyle & WS_MINIMIZE)
3301         return 0; /* Nothing to do */
3302
3303     if (infoPtr->dwStyle & WS_BORDER) {
3304
3305         /* adjust rectangle and draw the necessary edge */
3306         if (!(hdc = GetDCEx( infoPtr->hwndSelf, 0, DCX_USESTYLE | DCX_WINDOW )))
3307             return 0;
3308         GetWindowRect (infoPtr->hwndSelf, &rcWindow);
3309         OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
3310         TRACE("rect (%s)\n", wine_dbgstr_rect(&rcWindow));
3311         DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_RECT);
3312         ReleaseDC( infoPtr->hwndSelf, hdc );
3313     }
3314     else if ((theme = GetWindowTheme (infoPtr->hwndSelf)))
3315     {
3316         /* adjust rectangle and draw the necessary edge */
3317         if (!(hdc = GetDCEx( infoPtr->hwndSelf, 0, DCX_USESTYLE | DCX_WINDOW )))
3318             return 0;
3319         GetWindowRect (infoPtr->hwndSelf, &rcWindow);
3320         OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
3321         TRACE("rect (%s)\n", wine_dbgstr_rect(&rcWindow));
3322         DrawThemeEdge (theme, hdc, 0, 0, &rcWindow, BDR_RAISEDINNER, BF_TOP, NULL);
3323         ReleaseDC( infoPtr->hwndSelf, hdc );
3324     }
3325
3326     return 0;
3327 }
3328
3329
3330 static LRESULT
3331 REBAR_NotifyFormat (REBAR_INFO *infoPtr, LPARAM lParam)
3332 {
3333     INT i;
3334
3335     if (lParam == NF_REQUERY) {
3336         i = SendMessageW(REBAR_GetNotifyParent (infoPtr),
3337                          WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
3338         if ((i != NFR_ANSI) && (i != NFR_UNICODE)) {
3339             ERR("wrong response to WM_NOTIFYFORMAT (%d), assuming ANSI\n", i);
3340             i = NFR_ANSI;
3341         }
3342         infoPtr->bUnicode = (i == NFR_UNICODE) ? 1 : 0;
3343         return (LRESULT)i;
3344     }
3345     return (LRESULT)((infoPtr->bUnicode) ? NFR_UNICODE : NFR_ANSI);
3346 }
3347
3348
3349 static LRESULT
3350 REBAR_Paint (const REBAR_INFO *infoPtr, WPARAM wParam)
3351 {
3352     HDC hdc = (HDC)wParam;
3353
3354     if (hdc) {
3355         TRACE("painting\n");
3356         REBAR_Refresh (infoPtr, hdc);
3357     } else {
3358         PAINTSTRUCT ps;
3359         hdc = BeginPaint (infoPtr->hwndSelf, &ps);
3360         TRACE("painting (%s)\n", wine_dbgstr_rect(&ps.rcPaint));
3361         if (ps.fErase) {
3362             /* Erase area of paint if requested */
3363             REBAR_InternalEraseBkGnd (infoPtr, wParam, &ps.rcPaint);
3364         }
3365         REBAR_Refresh (infoPtr, hdc);
3366         EndPaint (infoPtr->hwndSelf, &ps);
3367     }
3368
3369     return 0;
3370 }
3371
3372
3373 static LRESULT
3374 REBAR_SetCursor (const REBAR_INFO *infoPtr, LPARAM lParam)
3375 {
3376     POINT pt;
3377     UINT  flags;
3378
3379     TRACE("code=0x%X  id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
3380
3381     GetCursorPos (&pt);
3382     ScreenToClient (infoPtr->hwndSelf, &pt);
3383
3384     REBAR_InternalHitTest (infoPtr, &pt, &flags, NULL);
3385
3386     if (flags == RBHT_GRABBER) {
3387         if ((infoPtr->dwStyle & CCS_VERT) &&
3388             !(infoPtr->dwStyle & RBS_VERTICALGRIPPER))
3389             SetCursor (infoPtr->hcurVert);
3390         else
3391             SetCursor (infoPtr->hcurHorz);
3392     }
3393     else if (flags != RBHT_CLIENT)
3394         SetCursor (infoPtr->hcurArrow);
3395
3396     return 0;
3397 }
3398
3399
3400 static LRESULT
3401 REBAR_SetFont (REBAR_INFO *infoPtr, WPARAM wParam)
3402 {
3403     REBAR_BAND *lpBand;
3404     UINT i;
3405
3406     infoPtr->hFont = (HFONT)wParam;
3407
3408     /* revalidate all bands to change sizes of text in headers of bands */
3409     for (i=0; i<infoPtr->uNumBands; i++) {
3410         lpBand = &infoPtr->bands[i];
3411         REBAR_ValidateBand (infoPtr, lpBand);
3412     }
3413
3414     REBAR_Layout(infoPtr);
3415     return 0;
3416 }
3417
3418
3419 static inline LRESULT
3420 REBAR_SetRedraw (REBAR_INFO *infoPtr, WPARAM wParam)
3421      /*****************************************************
3422       *
3423       * Function;
3424       *  Handles the WM_SETREDRAW message.
3425       *
3426       * Documentation:
3427       *  According to testing V4.71 of COMCTL32 returns the
3428       *  *previous* status of the redraw flag (either 0 or -1)
3429       *  instead of the MSDN documented value of 0 if handled
3430       *
3431       *****************************************************/
3432 {
3433     BOOL oldredraw = infoPtr->DoRedraw;
3434
3435     TRACE("set to %s, fStatus=%08x\n",
3436           (wParam) ? "TRUE" : "FALSE", infoPtr->fStatus);
3437     infoPtr->DoRedraw = (BOOL) wParam;
3438     if (wParam) {
3439         if (infoPtr->fStatus & BAND_NEEDS_REDRAW) {
3440             REBAR_MoveChildWindows (infoPtr, 0, infoPtr->uNumBands);
3441             REBAR_ForceResize (infoPtr);
3442             InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
3443         }
3444         infoPtr->fStatus &= ~BAND_NEEDS_REDRAW;
3445     }
3446     return (oldredraw) ? -1 : 0;
3447 }
3448
3449
3450 static LRESULT
3451 REBAR_Size (REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3452 {
3453     TRACE("wParam=%lx, lParam=%lx\n", wParam, lParam);
3454
3455     /* avoid _Layout resize recursion (but it shouldn't be infinite and it seems Windows does recurse) */
3456     if (infoPtr->fStatus & SELF_RESIZE) {
3457         infoPtr->fStatus &= ~SELF_RESIZE;
3458         TRACE("SELF_RESIZE was set, reset, fStatus=%08x lparam=%08lx\n",
3459               infoPtr->fStatus, lParam);
3460         return 0;
3461     }
3462     
3463     if (infoPtr->dwStyle & RBS_AUTOSIZE)
3464         REBAR_AutoSize(infoPtr, TRUE);
3465     else
3466         REBAR_Layout(infoPtr);
3467
3468     return 0;
3469 }
3470
3471
3472 static LRESULT
3473 REBAR_StyleChanged (REBAR_INFO *infoPtr, LPARAM lParam)
3474 {
3475     STYLESTRUCT *ss = (STYLESTRUCT *)lParam;
3476
3477     TRACE("current style=%08x, styleOld=%08x, style being set to=%08x\n",
3478           infoPtr->dwStyle, ss->styleOld, ss->styleNew);
3479     infoPtr->orgStyle = infoPtr->dwStyle = ss->styleNew;
3480     if (GetWindowTheme (infoPtr->hwndSelf))
3481         infoPtr->dwStyle &= ~WS_BORDER;
3482     /* maybe it should be COMMON_STYLES like in toolbar */
3483     if ((ss->styleNew ^ ss->styleOld) & CCS_VERT)
3484         REBAR_Layout(infoPtr);
3485
3486     return FALSE;
3487 }
3488
3489 /* update theme after a WM_THEMECHANGED message */
3490 static LRESULT theme_changed (REBAR_INFO* infoPtr)
3491 {
3492     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
3493     CloseThemeData (theme);
3494     theme = OpenThemeData (infoPtr->hwndSelf, themeClass);
3495     /* WS_BORDER disappears when theming is enabled and reappears when
3496      * disabled... */
3497     infoPtr->dwStyle &= ~WS_BORDER;
3498     infoPtr->dwStyle |= theme ? 0 : (infoPtr->orgStyle & WS_BORDER);
3499     return 0;
3500 }
3501
3502 static LRESULT
3503 REBAR_WindowPosChanged (const REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3504 {
3505     LRESULT ret;
3506     RECT rc;
3507
3508     ret = DefWindowProcW(infoPtr->hwndSelf, WM_WINDOWPOSCHANGED,
3509                          wParam, lParam);
3510     GetWindowRect(infoPtr->hwndSelf, &rc);
3511     TRACE("hwnd %p new pos (%s)\n", infoPtr->hwndSelf, wine_dbgstr_rect(&rc));
3512     return ret;
3513 }
3514
3515
3516 static LRESULT WINAPI
3517 REBAR_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3518 {
3519     REBAR_INFO *infoPtr = REBAR_GetInfoPtr (hwnd);
3520
3521     TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
3522           hwnd, uMsg, wParam, lParam);
3523     if (!infoPtr && (uMsg != WM_NCCREATE))
3524         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
3525     switch (uMsg)
3526     {
3527 /*      case RB_BEGINDRAG: */
3528
3529         case RB_DELETEBAND:
3530             return REBAR_DeleteBand (infoPtr, wParam);
3531
3532 /*      case RB_DRAGMOVE: */
3533 /*      case RB_ENDDRAG: */
3534
3535         case RB_GETBANDBORDERS:
3536             return REBAR_GetBandBorders (infoPtr, wParam, lParam);
3537
3538         case RB_GETBANDCOUNT:
3539             return REBAR_GetBandCount (infoPtr);
3540
3541         case RB_GETBANDINFO_OLD:
3542         case RB_GETBANDINFOA:
3543             return REBAR_GetBandInfoT(infoPtr, wParam, lParam, FALSE);
3544
3545         case RB_GETBANDINFOW:
3546             return REBAR_GetBandInfoT(infoPtr, wParam, lParam, TRUE);
3547
3548         case RB_GETBARHEIGHT:
3549             return REBAR_GetBarHeight (infoPtr);
3550
3551         case RB_GETBARINFO:
3552             return REBAR_GetBarInfo (infoPtr, lParam);
3553
3554         case RB_GETBKCOLOR:
3555             return REBAR_GetBkColor (infoPtr);
3556
3557 /*      case RB_GETCOLORSCHEME: */
3558 /*      case RB_GETDROPTARGET: */
3559
3560         case RB_GETPALETTE:
3561             return REBAR_GetPalette (infoPtr, wParam, lParam);
3562
3563         case RB_GETRECT:
3564             return REBAR_GetRect (infoPtr, wParam, lParam);
3565
3566         case RB_GETROWCOUNT:
3567             return REBAR_GetRowCount (infoPtr);
3568
3569         case RB_GETROWHEIGHT:
3570             return REBAR_GetRowHeight (infoPtr, wParam);
3571
3572         case RB_GETTEXTCOLOR:
3573             return REBAR_GetTextColor (infoPtr);
3574
3575         case RB_GETTOOLTIPS:
3576             return REBAR_GetToolTips (infoPtr);
3577
3578         case RB_GETUNICODEFORMAT:
3579             return REBAR_GetUnicodeFormat (infoPtr);
3580
3581         case CCM_GETVERSION:
3582             return REBAR_GetVersion (infoPtr);
3583
3584         case RB_HITTEST:
3585             return REBAR_HitTest (infoPtr, lParam);
3586
3587         case RB_IDTOINDEX:
3588             return REBAR_IdToIndex (infoPtr, wParam);
3589
3590         case RB_INSERTBANDA:
3591             return REBAR_InsertBandT(infoPtr, wParam, lParam, FALSE);
3592
3593         case RB_INSERTBANDW:
3594             return REBAR_InsertBandT(infoPtr, wParam, lParam, TRUE);
3595
3596         case RB_MAXIMIZEBAND:
3597             return REBAR_MaximizeBand (infoPtr, wParam, lParam);
3598
3599         case RB_MINIMIZEBAND:
3600             return REBAR_MinimizeBand (infoPtr, wParam);
3601
3602         case RB_MOVEBAND:
3603             return REBAR_MoveBand (infoPtr, wParam, lParam);
3604
3605         case RB_PUSHCHEVRON:
3606             return REBAR_PushChevron (infoPtr, wParam, lParam);
3607
3608         case RB_SETBANDINFOA:
3609             return REBAR_SetBandInfoT(infoPtr, wParam, lParam, FALSE);
3610
3611         case RB_SETBANDINFOW:
3612             return REBAR_SetBandInfoT(infoPtr, wParam, lParam, TRUE);
3613
3614         case RB_SETBARINFO:
3615             return REBAR_SetBarInfo (infoPtr, lParam);
3616
3617         case RB_SETBKCOLOR:
3618             return REBAR_SetBkColor (infoPtr, lParam);
3619
3620 /*      case RB_SETCOLORSCHEME: */
3621 /*      case RB_SETPALETTE: */
3622 /*          return REBAR_GetPalette (infoPtr, wParam, lParam); */
3623
3624         case RB_SETPARENT:
3625             return REBAR_SetParent (infoPtr, wParam);
3626
3627         case RB_SETTEXTCOLOR:
3628             return REBAR_SetTextColor (infoPtr, lParam);
3629
3630 /*      case RB_SETTOOLTIPS: */
3631
3632         case RB_SETUNICODEFORMAT:
3633             return REBAR_SetUnicodeFormat (infoPtr, wParam);
3634
3635         case CCM_SETVERSION:
3636             return REBAR_SetVersion (infoPtr, (INT)wParam);
3637
3638         case RB_SHOWBAND:
3639             return REBAR_ShowBand (infoPtr, wParam, lParam);
3640
3641         case RB_SIZETORECT:
3642             return REBAR_SizeToRect (infoPtr, lParam);
3643
3644
3645 /*    Messages passed to parent */
3646         case WM_COMMAND:
3647         case WM_DRAWITEM:
3648         case WM_NOTIFY:
3649             return SendMessageW(REBAR_GetNotifyParent (infoPtr), uMsg, wParam, lParam);
3650
3651
3652 /*      case WM_CHARTOITEM:     supported according to ControlSpy */
3653
3654         case WM_CREATE:
3655             return REBAR_Create (infoPtr, lParam);
3656
3657         case WM_DESTROY:
3658             return REBAR_Destroy (infoPtr);
3659
3660         case WM_ERASEBKGND:
3661             return REBAR_EraseBkGnd (infoPtr, wParam);
3662
3663         case WM_GETFONT:
3664             return REBAR_GetFont (infoPtr);
3665
3666 /*      case WM_LBUTTONDBLCLK:  supported according to ControlSpy */
3667
3668         case WM_LBUTTONDOWN:
3669             return REBAR_LButtonDown (infoPtr, lParam);
3670
3671         case WM_LBUTTONUP:
3672             return REBAR_LButtonUp (infoPtr);
3673
3674 /*      case WM_MEASUREITEM:    supported according to ControlSpy */
3675
3676         case WM_MOUSEMOVE:
3677             return REBAR_MouseMove (infoPtr, lParam);
3678
3679         case WM_MOUSELEAVE:
3680             return REBAR_MouseLeave (infoPtr);
3681
3682         case WM_NCCALCSIZE:
3683             return REBAR_NCCalcSize (infoPtr, lParam);
3684
3685         case WM_NCCREATE:
3686             return REBAR_NCCreate (hwnd, lParam);
3687
3688         case WM_NCHITTEST:
3689             return REBAR_NCHitTest (infoPtr, lParam);
3690
3691         case WM_NCPAINT:
3692             return REBAR_NCPaint (infoPtr);
3693
3694         case WM_NOTIFYFORMAT:
3695             return REBAR_NotifyFormat (infoPtr, lParam);
3696
3697         case WM_PRINTCLIENT:
3698         case WM_PAINT:
3699             return REBAR_Paint (infoPtr, wParam);
3700
3701 /*      case WM_PALETTECHANGED: supported according to ControlSpy */
3702 /*      case WM_QUERYNEWPALETTE:supported according to ControlSpy */
3703 /*      case WM_RBUTTONDOWN:    supported according to ControlSpy */
3704 /*      case WM_RBUTTONUP:      supported according to ControlSpy */
3705
3706         case WM_SETCURSOR:
3707             return REBAR_SetCursor (infoPtr, lParam);
3708
3709         case WM_SETFONT:
3710             return REBAR_SetFont (infoPtr, wParam);
3711
3712         case WM_SETREDRAW:
3713             return REBAR_SetRedraw (infoPtr, wParam);
3714
3715         case WM_SIZE:
3716             return REBAR_Size (infoPtr, wParam, lParam);
3717
3718         case WM_STYLECHANGED:
3719             return REBAR_StyleChanged (infoPtr, lParam);
3720
3721         case WM_THEMECHANGED:
3722             return theme_changed (infoPtr);
3723
3724 /*      case WM_SYSCOLORCHANGE: supported according to ControlSpy */
3725 /*      "Applications that have brushes using the existing system colors
3726          should delete those brushes and recreate them using the new
3727          system colors."  per MSDN                                */
3728
3729 /*      case WM_VKEYTOITEM:     supported according to ControlSpy */
3730 /*      case WM_WININICHANGE: */
3731
3732         case WM_WINDOWPOSCHANGED:
3733             return REBAR_WindowPosChanged (infoPtr, wParam, lParam);
3734
3735         default:
3736             if ((uMsg >= WM_USER) && (uMsg < WM_APP))
3737                 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
3738                      uMsg, wParam, lParam);
3739             return DefWindowProcW (hwnd, uMsg, wParam, lParam);
3740     }
3741 }
3742
3743
3744 VOID
3745 REBAR_Register (void)
3746 {
3747     WNDCLASSW wndClass;
3748
3749     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
3750     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
3751     wndClass.lpfnWndProc   = REBAR_WindowProc;
3752     wndClass.cbClsExtra    = 0;
3753     wndClass.cbWndExtra    = sizeof(REBAR_INFO *);
3754     wndClass.hCursor       = 0;
3755     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
3756 #if GLATESTING
3757     wndClass.hbrBackground = CreateSolidBrush(RGB(0,128,0));
3758 #endif
3759     wndClass.lpszClassName = REBARCLASSNAMEW;
3760
3761     RegisterClassW (&wndClass);
3762
3763     mindragx = GetSystemMetrics (SM_CXDRAG);
3764     mindragy = GetSystemMetrics (SM_CYDRAG);
3765
3766 }
3767
3768
3769 VOID
3770 REBAR_Unregister (void)
3771 {
3772     UnregisterClassW (REBARCLASSNAMEW, NULL);
3773 }