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