user32: Reimplement 16-bit clipboard functions on top of the 32-bit ones.
[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 "tmschema.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, yoff;
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 != NULL) {
774           int cyBand = lpBand->rcBand.bottom - lpBand->rcBand.top;
775           yoff = (cyBand - lpBand->cyChild) / 2;
776           SetRect (&lpBand->rcChild,
777                    lpBand->rcBand.left + lpBand->cxHeader, lpBand->rcBand.top + yoff,
778                    lpBand->rcBand.right - REBAR_POST_CHILD, lpBand->rcBand.top + yoff + lpBand->cyChild);
779           if ((lpBand->fStyle & RBBS_USECHEVRON) && (lpBand->rcChild.right - lpBand->rcChild.left < lpBand->cxIdeal))
780           {
781               lpBand->rcChild.right -= CHEVRON_WIDTH;
782               SetRect(&lpBand->rcChevron, lpBand->rcChild.right,
783                       lpBand->rcChild.top, lpBand->rcChild.right + CHEVRON_WIDTH,
784                       lpBand->rcChild.bottom);
785           }
786       }
787       else {
788           SetRect (&lpBand->rcChild,
789                    lpBand->rcBand.left+lpBand->cxHeader, lpBand->rcBand.top,
790                    lpBand->rcBand.right, lpBand->rcBand.bottom);
791       }
792
793       /* flag if notify required and invalidate rectangle */
794       if (lpBand->fDraw & NTF_INVALIDATE) {
795           TRACE("invalidating (%d,%d)-(%d,%d)\n",
796                 lpBand->rcBand.left,
797                 lpBand->rcBand.top,
798                 lpBand->rcBand.right + SEP_WIDTH,
799                 lpBand->rcBand.bottom + SEP_WIDTH);
800           lpBand->fDraw &= ~NTF_INVALIDATE;
801           work = lpBand->rcBand;
802           work.right += SEP_WIDTH;
803           work.bottom += SEP_WIDTH;
804           InvalidateRect(infoPtr->hwndSelf, &work, TRUE);
805           InvalidateRect(lpBand->hwndChild, NULL, TRUE);
806       }
807
808     }
809
810 }
811
812
813 static VOID
814 REBAR_CalcVertBand (const REBAR_INFO *infoPtr, UINT rstart, UINT rend)
815      /* Function: this routine initializes all the rectangles in */
816      /*  each band in a row to fit in the adjusted rcBand rect.  */
817      /* *** Supports only Vertical bars. ***                     */
818 {
819     REBAR_BAND *lpBand;
820     UINT i, xoff;
821     RECT work;
822
823     for(i=rstart; i<rend; i++){
824         RECT rcBand;
825         lpBand = REBAR_GetBand(infoPtr, i);
826         if (HIDDENBAND(lpBand)) continue;
827
828         translate_rect(infoPtr, &rcBand, &lpBand->rcBand);
829
830         /* set initial gripper rectangle */
831         SetRect (&lpBand->rcGripper, rcBand.left, rcBand.top, rcBand.right, rcBand.top);
832
833         /* calculate gripper rectangle */
834         if (lpBand->fStatus & HAS_GRIPPER) {
835             lpBand->fDraw |= DRAW_GRIPPER;
836
837             if (infoPtr->dwStyle & RBS_VERTICALGRIPPER) {
838                 /*  vertical gripper  */
839                 lpBand->rcGripper.left   += 3;
840                 lpBand->rcGripper.right  = lpBand->rcGripper.left + GRIPPER_WIDTH;
841                 lpBand->rcGripper.top    += REBAR_PRE_GRIPPER;
842                 lpBand->rcGripper.bottom = lpBand->rcGripper.top + GRIPPER_HEIGHT;
843
844                 /* initialize Caption image rectangle  */
845                 SetRect (&lpBand->rcCapImage, rcBand.left,
846                          lpBand->rcGripper.bottom + REBAR_ALWAYS_SPACE,
847                          rcBand.right,
848                          lpBand->rcGripper.bottom + REBAR_ALWAYS_SPACE);
849             }
850             else {
851                 /*  horizontal gripper  */
852                 lpBand->rcGripper.left   += 2;
853                 lpBand->rcGripper.right  -= 2;
854                 lpBand->rcGripper.top    += REBAR_PRE_GRIPPER;
855                 lpBand->rcGripper.bottom  = lpBand->rcGripper.top + GRIPPER_WIDTH;
856
857                 /* initialize Caption image rectangle  */
858                 SetRect (&lpBand->rcCapImage, rcBand.left,
859                          lpBand->rcGripper.bottom + REBAR_ALWAYS_SPACE,
860                          rcBand.right,
861                          lpBand->rcGripper.bottom + REBAR_ALWAYS_SPACE);
862             }
863         }
864         else {  /* no gripper will be drawn */
865             xoff = 0;
866             if (lpBand->fStatus & (HAS_IMAGE | HAS_TEXT))
867                 /* if no gripper but either image or text, then leave space */
868                 xoff = REBAR_ALWAYS_SPACE;
869             /* initialize Caption image rectangle  */
870             SetRect (&lpBand->rcCapImage,
871                       rcBand.left, rcBand.top+xoff,
872                       rcBand.right, rcBand.top+xoff);
873         }
874
875         /* image is visible */
876         if (lpBand->fStatus & HAS_IMAGE) {
877             lpBand->fDraw |= DRAW_IMAGE;
878
879             lpBand->rcCapImage.right  = lpBand->rcCapImage.left + infoPtr->imageSize.cx;
880             lpBand->rcCapImage.bottom += infoPtr->imageSize.cy;
881
882             /* set initial caption text rectangle */
883             SetRect (&lpBand->rcCapText,
884                      rcBand.left, lpBand->rcCapImage.bottom+REBAR_POST_IMAGE,
885                      rcBand.right, rcBand.top+lpBand->cxHeader);
886         }
887         else {
888             /* set initial caption text rectangle */
889             SetRect (&lpBand->rcCapText,
890                      rcBand.left, lpBand->rcCapImage.bottom,
891                      rcBand.right, rcBand.top+lpBand->cxHeader);
892         }
893
894         /* text is visible */
895         if ((lpBand->fStatus & HAS_TEXT) && !(lpBand->fStyle & RBBS_HIDETITLE)) {
896             lpBand->fDraw |= DRAW_TEXT;
897             lpBand->rcCapText.bottom = max(lpBand->rcCapText.top,
898                                            lpBand->rcCapText.bottom);
899         }
900
901         /* set initial child window rectangle if there is a child */
902         if (lpBand->hwndChild) {
903             int cxBand = rcBand.right - rcBand.left;
904             xoff = (cxBand - lpBand->cyChild) / 2;
905             SetRect (&lpBand->rcChild,
906                      rcBand.left + xoff,                   rcBand.top + lpBand->cxHeader,
907                      rcBand.left + xoff + lpBand->cyChild, rcBand.bottom - REBAR_POST_CHILD);
908         }
909         else {
910             SetRect (&lpBand->rcChild,
911                      rcBand.left, rcBand.top+lpBand->cxHeader,
912                      rcBand.right, rcBand.bottom);
913         }
914
915         if (lpBand->fDraw & NTF_INVALIDATE) {
916             TRACE("invalidating (%d,%d)-(%d,%d)\n",
917                   rcBand.left,
918                   rcBand.top,
919                   rcBand.right + SEP_WIDTH,
920                   rcBand.bottom + SEP_WIDTH);
921             lpBand->fDraw &= ~NTF_INVALIDATE;
922             work = rcBand;
923             work.bottom += SEP_WIDTH;
924             work.right += SEP_WIDTH;
925             InvalidateRect(infoPtr->hwndSelf, &work, TRUE);
926             InvalidateRect(lpBand->hwndChild, NULL, TRUE);
927         }
928
929     }
930 }
931
932
933 static VOID
934 REBAR_ForceResize (REBAR_INFO *infoPtr)
935      /* Function: This changes the size of the REBAR window to that */
936      /*  calculated by REBAR_Layout.                                */
937 {
938     INT x, y, width, height;
939     INT xedge = 0, yedge = 0;
940     RECT rcSelf;
941
942     TRACE("new size [%d x %d]\n", infoPtr->calcSize.cx, infoPtr->calcSize.cy);
943
944     if (infoPtr->dwStyle & CCS_NORESIZE)
945         return;
946
947     if (infoPtr->dwStyle & WS_BORDER)
948     {
949         xedge = GetSystemMetrics(SM_CXEDGE);
950         yedge = GetSystemMetrics(SM_CYEDGE);
951         /* swap for CCS_VERT? */
952     }
953
954     /* compute rebar window rect in parent client coordinates */
955     GetWindowRect(infoPtr->hwndSelf, &rcSelf);
956     MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->hwndSelf), (LPPOINT)&rcSelf, 2);
957     translate_rect(infoPtr, &rcSelf, &rcSelf);
958
959     height = infoPtr->calcSize.cy + 2*yedge;
960     if (!(infoPtr->dwStyle & CCS_NOPARENTALIGN)) {
961         RECT rcParent;
962
963         x = -xedge;
964         width = infoPtr->calcSize.cx + 2*xedge;
965         y = 0; /* quiet compiler warning */
966         switch ( infoPtr->dwStyle & CCS_LAYOUT_MASK) {
967             case 0:     /* shouldn't happen - see NCCreate */
968             case CCS_TOP:
969                 y = ((infoPtr->dwStyle & CCS_NODIVIDER) ? 0 : REBAR_DIVIDER) - yedge;
970                 break;
971             case CCS_NOMOVEY:
972                 y = rcSelf.top;
973                 break;
974             case CCS_BOTTOM:
975                 GetClientRect(GetParent(infoPtr->hwndSelf), &rcParent);
976                 translate_rect(infoPtr, &rcParent, &rcParent);
977                 y = rcParent.bottom - infoPtr->calcSize.cy - yedge;
978                 break;
979         }
980     }
981     else {
982         x = rcSelf.left;
983         /* As on Windows if the CCS_NODIVIDER is not present the control will move
984          * 2 pixel down after every layout */
985         y = rcSelf.top + ((infoPtr->dwStyle & CCS_NODIVIDER) ? 0 : REBAR_DIVIDER);
986         width = rcSelf.right - rcSelf.left;
987     }
988
989     TRACE("hwnd %p, style=%08x, setting at (%d,%d) for (%d,%d)\n",
990         infoPtr->hwndSelf, infoPtr->dwStyle, x, y, width, height);
991
992     /* Set flag to ignore next WM_SIZE message and resize the window */
993     infoPtr->fStatus |= SELF_RESIZE;
994     if ((infoPtr->dwStyle & CCS_VERT) == 0)
995         SetWindowPos(infoPtr->hwndSelf, 0, x, y, width, height, SWP_NOZORDER);
996     else
997         SetWindowPos(infoPtr->hwndSelf, 0, y, x, height, width, SWP_NOZORDER);
998     infoPtr->fStatus &= ~SELF_RESIZE;
999 }
1000
1001
1002 static VOID
1003 REBAR_MoveChildWindows (const REBAR_INFO *infoPtr, UINT start, UINT endplus)
1004 {
1005     static const WCHAR strComboBox[] = { 'C','o','m','b','o','B','o','x',0 };
1006     REBAR_BAND *lpBand;
1007     WCHAR szClassName[40];
1008     UINT i;
1009     NMREBARCHILDSIZE  rbcz;
1010     HDWP deferpos;
1011
1012     if (!(deferpos = BeginDeferWindowPos(infoPtr->uNumBands)))
1013         ERR("BeginDeferWindowPos returned NULL\n");
1014
1015     for (i = start; i < endplus; i++) {
1016         lpBand = REBAR_GetBand(infoPtr, i);
1017
1018         if (HIDDENBAND(lpBand)) continue;
1019         if (lpBand->hwndChild) {
1020             TRACE("hwndChild = %p\n", lpBand->hwndChild);
1021
1022             /* Always generate the RBN_CHILDSIZE even if child
1023                    did not change */
1024             rbcz.uBand = i;
1025             rbcz.wID = lpBand->wID;
1026             rbcz.rcChild = lpBand->rcChild;
1027             translate_rect(infoPtr, &rbcz.rcBand, &lpBand->rcBand);
1028             if (infoPtr->dwStyle & CCS_VERT)
1029                 rbcz.rcBand.top += lpBand->cxHeader;
1030             else
1031                 rbcz.rcBand.left += lpBand->cxHeader;
1032             REBAR_Notify ((NMHDR *)&rbcz, infoPtr, RBN_CHILDSIZE);
1033             if (!EqualRect (&lpBand->rcChild, &rbcz.rcChild)) {
1034                 TRACE("Child rect changed by NOTIFY for band %u\n", i);
1035                 TRACE("    from (%s)  to (%s)\n",
1036                       wine_dbgstr_rect(&lpBand->rcChild),
1037                       wine_dbgstr_rect(&rbcz.rcChild));
1038                 lpBand->rcChild = rbcz.rcChild;  /* *** ??? */
1039             }
1040
1041             /* native (IE4 in "Favorites" frame **1) does:
1042              *   SetRect (&rc, -1, -1, -1, -1)
1043              *   EqualRect (&rc,band->rc???)
1044              *   if ret==0
1045              *     CopyRect (band->rc????, &rc)
1046              *     set flag outside of loop
1047              */
1048
1049             GetClassNameW (lpBand->hwndChild, szClassName, sizeof(szClassName)/sizeof(szClassName[0]));
1050             if (!lstrcmpW (szClassName, strComboBox) ||
1051                 !lstrcmpW (szClassName, WC_COMBOBOXEXW)) {
1052                 INT nEditHeight, yPos;
1053                 RECT rc;
1054
1055                 /* special placement code for combo or comboex box */
1056
1057
1058                 /* get size of edit line */
1059                 GetWindowRect (lpBand->hwndChild, &rc);
1060                 nEditHeight = rc.bottom - rc.top;
1061                 yPos = (lpBand->rcChild.bottom + lpBand->rcChild.top - nEditHeight)/2;
1062
1063                 /* center combo box inside child area */
1064                 TRACE("moving child (Combo(Ex)) %p to (%d,%d) for (%d,%d)\n",
1065                       lpBand->hwndChild,
1066                       lpBand->rcChild.left, yPos,
1067                       lpBand->rcChild.right - lpBand->rcChild.left,
1068                       nEditHeight);
1069                 deferpos = DeferWindowPos (deferpos, lpBand->hwndChild, HWND_TOP,
1070                                            lpBand->rcChild.left,
1071                                            /*lpBand->rcChild.top*/ yPos,
1072                                            lpBand->rcChild.right - lpBand->rcChild.left,
1073                                            nEditHeight,
1074                                            SWP_NOZORDER);
1075                 if (!deferpos)
1076                     ERR("DeferWindowPos returned NULL\n");
1077             }
1078             else {
1079                 TRACE("moving child (Other) %p to (%d,%d) for (%d,%d)\n",
1080                       lpBand->hwndChild,
1081                       lpBand->rcChild.left, lpBand->rcChild.top,
1082                       lpBand->rcChild.right - lpBand->rcChild.left,
1083                       lpBand->rcChild.bottom - lpBand->rcChild.top);
1084                 deferpos = DeferWindowPos (deferpos, lpBand->hwndChild, HWND_TOP,
1085                                            lpBand->rcChild.left,
1086                                            lpBand->rcChild.top,
1087                                            lpBand->rcChild.right - lpBand->rcChild.left,
1088                                            lpBand->rcChild.bottom - lpBand->rcChild.top,
1089                                            SWP_NOZORDER);
1090                 if (!deferpos)
1091                     ERR("DeferWindowPos returned NULL\n");
1092             }
1093         }
1094     }
1095     if (!EndDeferWindowPos(deferpos))
1096         ERR("EndDeferWindowPos returned NULL\n");
1097
1098     if (infoPtr->DoRedraw)
1099         UpdateWindow (infoPtr->hwndSelf);
1100
1101     /* native (from **1 above) does:
1102      *      UpdateWindow(rebar)
1103      *      REBAR_ForceResize
1104      *      RBN_HEIGHTCHANGE if necessary
1105      *      if ret from any EqualRect was 0
1106      *         Goto "BeginDeferWindowPos"
1107      */
1108
1109 }
1110
1111 /* Returns the next visible band (the first visible band in [i+1; infoPtr->uNumBands) )
1112  * or infoPtr->uNumBands if none */
1113 static int next_visible(const REBAR_INFO *infoPtr, int i)
1114 {
1115     int n;
1116     for (n = i + 1; n < infoPtr->uNumBands; n++)
1117         if (!HIDDENBAND(REBAR_GetBand(infoPtr, n)))
1118             break;
1119     return n;
1120 }
1121
1122 /* Returns the previous visible band (the last visible band in [0; i) )
1123  * or -1 if none */
1124 static int prev_visible(const REBAR_INFO *infoPtr, int i)
1125 {
1126     int n;
1127     for (n = i - 1; n >= 0; n--)
1128         if (!HIDDENBAND(REBAR_GetBand(infoPtr, n)))
1129             break;
1130     return n;
1131 }
1132
1133 /* Returns the first visible band or infoPtr->uNumBands if none */
1134 static int first_visible(const REBAR_INFO *infoPtr)
1135 {
1136     return next_visible(infoPtr, -1); /* this works*/
1137 }
1138
1139 /* Returns the first visible band for the given row (or iBand if none) */
1140 static int get_row_begin_for_band(const REBAR_INFO *infoPtr, INT iBand)
1141 {
1142     int iLastBand = iBand;
1143     int iRow = REBAR_GetBand(infoPtr, iBand)->iRow;
1144     while ((iBand = prev_visible(infoPtr, iBand)) >= 0) {
1145         if (REBAR_GetBand(infoPtr, iBand)->iRow != iRow)
1146             break;
1147         else
1148             iLastBand = iBand;
1149     }
1150     return iLastBand;
1151 }
1152
1153 /* Returns the first visible band for the next row (or infoPtr->uNumBands if none) */
1154 static int get_row_end_for_band(const REBAR_INFO *infoPtr, INT iBand)
1155 {
1156     int iRow = REBAR_GetBand(infoPtr, iBand)->iRow;
1157     while ((iBand = next_visible(infoPtr, iBand)) < infoPtr->uNumBands)
1158         if (REBAR_GetBand(infoPtr, iBand)->iRow != iRow)
1159             break;
1160     return iBand;
1161 }
1162
1163 /* Compute the rcBand.{left,right} from the cxEffective bands widths computed earlier.
1164  * iBeginBand must be visible */
1165 static void REBAR_SetRowRectsX(const REBAR_INFO *infoPtr, INT iBeginBand, INT iEndBand)
1166 {
1167     int xPos = 0, i;
1168     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1169     {
1170         REBAR_BAND *lpBand = REBAR_GetBand(infoPtr, i);
1171
1172         lpBand = REBAR_GetBand(infoPtr, i);
1173         if (lpBand->rcBand.left != xPos || lpBand->rcBand.right != xPos + lpBand->cxEffective) {
1174             lpBand->fDraw |= NTF_INVALIDATE;
1175             TRACE("Setting rect %d to %d,%d\n", i, xPos, xPos + lpBand->cxEffective);
1176             lpBand->rcBand.left = xPos;
1177             lpBand->rcBand.right = xPos + lpBand->cxEffective;
1178         }
1179         xPos += lpBand->cxEffective + SEP_WIDTH;
1180     }
1181 }
1182
1183 /* The rationale of this function is probably as follows: if we have some space
1184  * to distribute we want to add it to a band on the right. However we don't want
1185  * to unminimize a minimized band so we search for a band that is big enough.
1186  * For some reason "big enough" is defined as bigger than the minimum size of the
1187  * first band in the row
1188  */
1189 static REBAR_BAND *REBAR_FindBandToGrow(const REBAR_INFO *infoPtr, INT iBeginBand, INT iEndBand)
1190 {
1191     INT cxMinFirstBand = 0, i;
1192
1193     cxMinFirstBand = REBAR_GetBand(infoPtr, iBeginBand)->cxMinBand;
1194
1195     for (i = prev_visible(infoPtr, iEndBand); i >= iBeginBand; i = prev_visible(infoPtr, i))
1196         if (REBAR_GetBand(infoPtr, i)->cxEffective > cxMinFirstBand &&
1197           !(REBAR_GetBand(infoPtr, i)->fStyle & RBBS_FIXEDSIZE))
1198             break;
1199
1200     if (i < iBeginBand)
1201         for (i = prev_visible(infoPtr, iEndBand); i >= iBeginBand; i = prev_visible(infoPtr, i))
1202             if (REBAR_GetBand(infoPtr, i)->cxMinBand == cxMinFirstBand)
1203                 break;
1204
1205     TRACE("Extra space for row [%d..%d) should be added to band %d\n", iBeginBand, iEndBand, i);
1206     return REBAR_GetBand(infoPtr, i);
1207 }
1208
1209 /* Try to shrink the visible bands in [iBeginBand; iEndBand) by cxShrink, starting from the right */
1210 static int REBAR_ShrinkBandsRTL(const REBAR_INFO *infoPtr, INT iBeginBand, INT iEndBand, INT cxShrink, BOOL bEnforce)
1211 {
1212     REBAR_BAND *lpBand;
1213     INT width, i;
1214
1215     TRACE("Shrinking bands [%d..%d) by %d, right-to-left\n", iBeginBand, iEndBand, cxShrink);
1216     for (i = prev_visible(infoPtr, iEndBand); i >= iBeginBand; i = prev_visible(infoPtr, i))
1217     {
1218         lpBand = REBAR_GetBand(infoPtr, i);
1219
1220         width = max(lpBand->cxEffective - cxShrink, (int)lpBand->cxMinBand);
1221         cxShrink -= lpBand->cxEffective - width;
1222         lpBand->cxEffective = width;
1223         if (bEnforce && lpBand->cx > lpBand->cxEffective)
1224             lpBand->cx = lpBand->cxEffective;
1225         if (cxShrink == 0)
1226             break;
1227     }
1228     return cxShrink;
1229 }
1230
1231
1232 /* Try to shrink the visible bands in [iBeginBand; iEndBand) by cxShrink, starting from the left.
1233  * iBeginBand must be visible */
1234 static int REBAR_ShrinkBandsLTR(const REBAR_INFO *infoPtr, INT iBeginBand, INT iEndBand, INT cxShrink, BOOL bEnforce)
1235 {
1236     REBAR_BAND *lpBand;
1237     INT width, i;
1238
1239     TRACE("Shrinking bands [%d..%d) by %d, left-to-right\n", iBeginBand, iEndBand, cxShrink);
1240     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1241     {
1242         lpBand = REBAR_GetBand(infoPtr, i);
1243
1244         width = max(lpBand->cxEffective - cxShrink, (int)lpBand->cxMinBand);
1245         cxShrink -= lpBand->cxEffective - width;
1246         lpBand->cxEffective = width;
1247         if (bEnforce)
1248             lpBand->cx = lpBand->cxEffective;
1249         if (cxShrink == 0)
1250             break;
1251     }
1252     return cxShrink;
1253 }
1254
1255 /* Set the heights of the visible bands in [iBeginBand; iEndBand) to the max height. iBeginBand must be visible */
1256 static int REBAR_SetBandsHeight(const REBAR_INFO *infoPtr, INT iBeginBand, INT iEndBand, INT yStart)
1257 {
1258     REBAR_BAND *lpBand;
1259     int yMaxHeight = 0;
1260     int yPos = yStart;
1261     int row = REBAR_GetBand(infoPtr, iBeginBand)->iRow;
1262     int i;
1263     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1264     {
1265         lpBand = REBAR_GetBand(infoPtr, i);
1266         lpBand->cyRowSoFar = yMaxHeight;
1267         yMaxHeight = max(yMaxHeight, lpBand->cyMinBand);
1268     }
1269     TRACE("Bands [%d; %d) height: %d\n", iBeginBand, iEndBand, yMaxHeight);
1270
1271     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1272     {
1273         lpBand = REBAR_GetBand(infoPtr, i);
1274         /* we may be called for multiple rows if RBS_VARHEIGHT not set */
1275         if (lpBand->iRow != row) {
1276             yPos += yMaxHeight + SEP_WIDTH;
1277             row = lpBand->iRow;
1278         }
1279
1280         if (lpBand->rcBand.top != yPos || lpBand->rcBand.bottom != yPos + yMaxHeight) {
1281             lpBand->fDraw |= NTF_INVALIDATE;
1282             lpBand->rcBand.top = yPos;
1283             lpBand->rcBand.bottom = yPos + yMaxHeight;
1284             TRACE("Band %d: %s\n", i, wine_dbgstr_rect(&lpBand->rcBand));
1285         }
1286     }
1287     return yPos + yMaxHeight;
1288 }
1289
1290 /* Layout the row [iBeginBand; iEndBand). iBeginBand must be visible */
1291 static void REBAR_LayoutRow(const REBAR_INFO *infoPtr, int iBeginBand, int iEndBand, int cx, int *piRow, int *pyPos)
1292 {
1293     REBAR_BAND *lpBand;
1294     int i, extra;
1295     int width = 0;
1296
1297     TRACE("Adjusting row [%d;%d). Width: %d\n", iBeginBand, iEndBand, cx);
1298     for (i = iBeginBand; i < iEndBand; i++)
1299         REBAR_GetBand(infoPtr, i)->iRow = *piRow;
1300
1301     /* compute the extra space */
1302     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1303     {
1304         lpBand = REBAR_GetBand(infoPtr, i);
1305         if (i > iBeginBand)
1306             width += SEP_WIDTH;
1307         lpBand->cxEffective = max(lpBand->cxMinBand, lpBand->cx);
1308         width += lpBand->cxEffective;
1309     }
1310
1311     extra = cx - width;
1312     TRACE("Extra space: %d\n", extra);
1313     if (extra < 0) {
1314         int ret = REBAR_ShrinkBandsRTL(infoPtr, iBeginBand, iEndBand, -extra, FALSE);
1315         if (ret > 0 && next_visible(infoPtr, iBeginBand) != iEndBand)  /* one band may be longer than expected... */
1316             ERR("Error layouting row %d - couldn't shrink for %d pixels (%d total shrink)\n", *piRow, ret, -extra);
1317     } else
1318     if (extra > 0) {
1319         lpBand = REBAR_FindBandToGrow(infoPtr, iBeginBand, iEndBand);
1320         lpBand->cxEffective += extra;
1321     }
1322
1323     REBAR_SetRowRectsX(infoPtr, iBeginBand, iEndBand);
1324     if (infoPtr->dwStyle & RBS_VARHEIGHT)
1325     {
1326         if (*piRow > 0)
1327             *pyPos += SEP_WIDTH;
1328         *pyPos = REBAR_SetBandsHeight(infoPtr, iBeginBand, iEndBand, *pyPos);
1329     }
1330     (*piRow)++;
1331 }
1332
1333 static VOID
1334 REBAR_Layout(REBAR_INFO *infoPtr)
1335 {
1336     REBAR_BAND *lpBand;
1337     RECT rcAdj;
1338     SIZE oldSize;
1339     INT adjcx, i;
1340     INT rowstart;
1341     INT row = 0;
1342     INT xMin, yPos;
1343
1344     if (infoPtr->dwStyle & (CCS_NORESIZE | CCS_NOPARENTALIGN) || GetParent(infoPtr->hwndSelf) == NULL)
1345         GetClientRect(infoPtr->hwndSelf, &rcAdj);
1346     else
1347         GetClientRect(GetParent(infoPtr->hwndSelf), &rcAdj);
1348     TRACE("adjustment rect is (%s)\n", wine_dbgstr_rect(&rcAdj));
1349
1350     adjcx = get_rect_cx(infoPtr, &rcAdj);
1351
1352     if (infoPtr->uNumBands == 0) {
1353         TRACE("No bands - setting size to (0,%d), vert: %lx\n", adjcx, infoPtr->dwStyle & CCS_VERT);
1354         infoPtr->calcSize.cx = adjcx;
1355         /* the calcSize.cy won't change for a 0 band rebar */
1356         infoPtr->uNumRows = 0;
1357         REBAR_ForceResize(infoPtr);
1358         return;
1359     }
1360
1361     yPos = 0;
1362     xMin = 0;
1363     rowstart = first_visible(infoPtr);
1364     /* divide rows */
1365     for (i = rowstart; i < infoPtr->uNumBands; i = next_visible(infoPtr, i))
1366     {
1367         lpBand = REBAR_GetBand(infoPtr, i);
1368
1369         if (i > rowstart && (lpBand->fStyle & RBBS_BREAK || xMin + lpBand->cxMinBand > adjcx)) {
1370             TRACE("%s break on band %d\n", (lpBand->fStyle & RBBS_BREAK ? "Hard" : "Soft"), i - 1);
1371             REBAR_LayoutRow(infoPtr, rowstart, i, adjcx, &row, &yPos);
1372             rowstart = i;
1373             xMin = 0;
1374         }
1375         else
1376             xMin += SEP_WIDTH;
1377
1378         xMin += lpBand->cxMinBand;
1379     }
1380     REBAR_LayoutRow(infoPtr, rowstart, infoPtr->uNumBands, adjcx, &row, &yPos);
1381
1382     if (!(infoPtr->dwStyle & RBS_VARHEIGHT))
1383         yPos = REBAR_SetBandsHeight(infoPtr, first_visible(infoPtr), infoPtr->uNumBands, 0);
1384
1385     infoPtr->uNumRows = row;
1386
1387     if (infoPtr->dwStyle & CCS_VERT)
1388         REBAR_CalcVertBand(infoPtr, 0, infoPtr->uNumBands);
1389     else
1390         REBAR_CalcHorzBand(infoPtr, 0, infoPtr->uNumBands);
1391     /* now compute size of Rebar itself */
1392     oldSize = infoPtr->calcSize;
1393
1394     infoPtr->calcSize.cx = adjcx;
1395     infoPtr->calcSize.cy = yPos;
1396     TRACE("calcsize size=(%d, %d), origheight=(%d,%d)\n",
1397             infoPtr->calcSize.cx, infoPtr->calcSize.cy,
1398             oldSize.cx, oldSize.cy);
1399
1400     REBAR_DumpBand (infoPtr);
1401     REBAR_MoveChildWindows (infoPtr, 0, infoPtr->uNumBands);
1402     REBAR_ForceResize (infoPtr);
1403
1404     /* note: after a RBN_HEIGHTCHANGE native sends once again all the RBN_CHILDSIZE
1405      * and does another ForceResize */
1406     if (oldSize.cy != infoPtr->calcSize.cy)
1407     {
1408         NMHDR heightchange;
1409         REBAR_Notify(&heightchange, infoPtr, RBN_HEIGHTCHANGE);
1410         REBAR_AutoSize(infoPtr, FALSE);
1411     }
1412 }
1413
1414 /* iBeginBand must be visible */
1415 static int
1416 REBAR_SizeChildrenToHeight(const REBAR_INFO *infoPtr, int iBeginBand, int iEndBand, int extra, BOOL *fChanged)
1417 {
1418     int cyBandsOld;
1419     int cyBandsNew = 0;
1420     int i;
1421
1422     TRACE("[%d;%d) by %d\n", iBeginBand, iEndBand, extra);
1423
1424     cyBandsOld = REBAR_GetBand(infoPtr, iBeginBand)->rcBand.bottom -
1425                  REBAR_GetBand(infoPtr, iBeginBand)->rcBand.top;
1426     for (i = iBeginBand; i < iEndBand; i = next_visible(infoPtr, i))
1427     {
1428         REBAR_BAND *lpBand = REBAR_GetBand(infoPtr, i);
1429         int cyMaxChild = cyBandsOld - REBARSPACE(lpBand) + extra;
1430         int cyChild = round_child_height(lpBand, cyMaxChild);
1431
1432         if (lpBand->hwndChild && cyChild != lpBand->cyChild && (lpBand->fStyle & RBBS_VARIABLEHEIGHT))
1433         {
1434             TRACE("Resizing %d: %d -> %d [%d]\n", i, lpBand->cyChild, cyChild, lpBand->cyMaxChild);
1435             *fChanged = TRUE;
1436             lpBand->cyChild = cyChild;
1437             lpBand->fDraw |= NTF_INVALIDATE;
1438             update_min_band_height(infoPtr, lpBand);
1439         }
1440         cyBandsNew = max(cyBandsNew, lpBand->cyMinBand);
1441     }
1442     return cyBandsNew - cyBandsOld;
1443 }
1444
1445 /* worker function for RB_SIZETORECT and RBS_AUTOSIZE */
1446 static VOID
1447 REBAR_SizeToHeight(REBAR_INFO *infoPtr, int height)
1448 {
1449     int extra = height - infoPtr->calcSize.cy;  /* may be negative */
1450     BOOL fChanged = FALSE;
1451     UINT uNumRows = infoPtr->uNumRows;
1452     int i;
1453
1454     if (uNumRows == 0)  /* avoid division by 0 */
1455         return;
1456
1457     /* That's not exactly what Windows does but should be similar */
1458
1459     /* Pass one: break-up/glue rows */
1460     if (extra > 0)
1461     {
1462         for (i = prev_visible(infoPtr, infoPtr->uNumBands); i > 0; i = prev_visible(infoPtr, i))
1463         {
1464             REBAR_BAND *lpBand = REBAR_GetBand(infoPtr, i);
1465             int height = lpBand->rcBand.bottom - lpBand->rcBand.top;
1466             int cyBreakExtra;  /* additional cy for the rebar after a RBBS_BREAK on this band */
1467
1468             if (infoPtr->dwStyle & RBS_VARHEIGHT)
1469                 cyBreakExtra = lpBand->cyRowSoFar; /* 'height' => 'lpBand->cyRowSoFar' + 'height'*/
1470             else
1471                 cyBreakExtra = height;             /* 'height' => 'height' + 'height'*/
1472             cyBreakExtra += SEP_WIDTH;
1473
1474             if (extra <= cyBreakExtra / 2)
1475                 break;
1476
1477             if (!(lpBand->fStyle & RBBS_BREAK))
1478             {
1479                 TRACE("Adding break on band %d - extra %d -> %d\n", i, extra, extra - cyBreakExtra);
1480                 lpBand->fStyle |= RBBS_BREAK;
1481                 lpBand->fDraw |= NTF_INVALIDATE;
1482                 fChanged = TRUE;
1483                 extra -= cyBreakExtra;
1484                 uNumRows++;
1485                 /* temporary change for _SizeControlsToHeight. The true values will be computed in _Layout */
1486                 if (infoPtr->dwStyle & RBS_VARHEIGHT)
1487                     lpBand->rcBand.bottom = lpBand->rcBand.top + lpBand->cyMinBand;
1488             }
1489         }
1490     }
1491     /* TODO: else if (extra < 0) { try to remove some RBBS_BREAKs } */
1492
1493     /* Pass two: increase/decrease control height */
1494     if (infoPtr->dwStyle & RBS_VARHEIGHT)
1495     {
1496         int i = first_visible(infoPtr);
1497         int iRow = 0;
1498         while (i < infoPtr->uNumBands)
1499         {
1500             REBAR_BAND *lpBand = REBAR_GetBand(infoPtr, i);
1501             int extraForRow = extra / (int)(uNumRows - iRow);
1502             int rowEnd;
1503
1504             /* we can't use get_row_end_for_band as we might have added RBBS_BREAK in the first phase */
1505             for (rowEnd = next_visible(infoPtr, i); rowEnd < infoPtr->uNumBands; rowEnd = next_visible(infoPtr, rowEnd))
1506                 if (REBAR_GetBand(infoPtr, rowEnd)->iRow != lpBand->iRow ||
1507                     REBAR_GetBand(infoPtr, rowEnd)->fStyle & RBBS_BREAK)
1508                     break;
1509
1510             extra -= REBAR_SizeChildrenToHeight(infoPtr, i, rowEnd, extraForRow, &fChanged);
1511             TRACE("extra = %d\n", extra);
1512             i = rowEnd;
1513             iRow++;
1514         }
1515     }
1516     else
1517         extra -= REBAR_SizeChildrenToHeight(infoPtr, first_visible(infoPtr), infoPtr->uNumBands, extra / infoPtr->uNumRows, &fChanged);
1518
1519     if (fChanged)
1520         REBAR_Layout(infoPtr);
1521 }
1522
1523 static VOID
1524 REBAR_AutoSize(REBAR_INFO *infoPtr, BOOL needsLayout)
1525 {
1526     RECT rc, rcNew;
1527     NMRBAUTOSIZE autosize;
1528
1529     if (needsLayout)
1530         REBAR_Layout(infoPtr);
1531     GetClientRect(infoPtr->hwndSelf, &rc);
1532     REBAR_SizeToHeight(infoPtr, get_rect_cy(infoPtr, &rc));
1533     GetClientRect(infoPtr->hwndSelf, &rcNew);
1534
1535     GetClientRect(infoPtr->hwndSelf, &autosize.rcTarget);
1536     autosize.fChanged = (memcmp(&rc, &rcNew, sizeof(RECT)) == 0);
1537     autosize.rcTarget = rc;
1538     autosize.rcActual = rcNew;
1539     REBAR_Notify((NMHDR *)&autosize, infoPtr, RBN_AUTOSIZE);
1540 }
1541
1542 static VOID
1543 REBAR_ValidateBand (const REBAR_INFO *infoPtr, REBAR_BAND *lpBand)
1544      /* Function:  This routine evaluates the band specs supplied */
1545      /*  by the user and updates the following 5 fields in        */
1546      /*  the internal band structure: cxHeader, cyHeader, cxMinBand, cyMinBand, fStatus */
1547 {
1548     UINT header=0;
1549     UINT textheight=0, imageheight = 0;
1550     UINT i, nonfixed;
1551     REBAR_BAND *tBand;
1552
1553     lpBand->fStatus = 0;
1554     lpBand->cxMinBand = 0;
1555     lpBand->cyMinBand = 0;
1556
1557     /* Data coming in from users into the cx... and cy... fields   */
1558     /* may be bad, just garbage, because the user never clears     */
1559     /* the fields. RB_{SET|INSERT}BAND{A|W} just passes the data   */
1560     /* along if the fields exist in the input area. Here we must   */
1561     /* determine if the data is valid. I have no idea how MS does  */
1562     /* the validation, but it does because the RB_GETBANDINFO      */
1563     /* returns a 0 when I know the sample program passed in an     */
1564     /* address. Here I will use the algorithm that if the value    */
1565     /* is greater than 65535 then it is bad and replace it with    */
1566     /* a zero. Feel free to improve the algorithm.  -  GA 12/2000  */
1567     if (lpBand->cxMinChild > 65535) lpBand->cxMinChild = 0;
1568     if (lpBand->cyMinChild > 65535) lpBand->cyMinChild = 0;
1569     if (lpBand->cx         > 65535) lpBand->cx         = 0;
1570     if (lpBand->cyChild    > 65535) lpBand->cyChild    = 0;
1571     if (lpBand->cyIntegral > 65535) lpBand->cyIntegral = 0;
1572     if (lpBand->cxIdeal    > 65535) lpBand->cxIdeal    = 0;
1573     if (lpBand->cxHeader   > 65535) lpBand->cxHeader   = 0;
1574
1575     /* TODO : we could try return to the caller if a value changed so that */
1576     /*        a REBAR_Layout is needed. Till now the caller should call it */
1577     /*        it always (we should also check what native does)            */
1578
1579     /* Header is where the image, text and gripper exist  */
1580     /* in the band and precede the child window.          */
1581
1582     /* count number of non-FIXEDSIZE and non-Hidden bands */
1583     nonfixed = 0;
1584     for (i=0; i<infoPtr->uNumBands; i++){
1585         tBand = REBAR_GetBand(infoPtr, i);
1586         if (!HIDDENBAND(tBand) && !(tBand->fStyle & RBBS_FIXEDSIZE))
1587             nonfixed++;
1588     }
1589
1590     /* calculate gripper rectangle */
1591     if (  (!(lpBand->fStyle & RBBS_NOGRIPPER)) &&
1592           ( (lpBand->fStyle & RBBS_GRIPPERALWAYS) ||
1593             ( !(lpBand->fStyle & RBBS_FIXEDSIZE) && (nonfixed > 1)))
1594        ) {
1595         lpBand->fStatus |= HAS_GRIPPER;
1596         if (infoPtr->dwStyle & CCS_VERT)
1597             if (infoPtr->dwStyle & RBS_VERTICALGRIPPER)
1598                 header += (GRIPPER_HEIGHT + REBAR_PRE_GRIPPER);
1599             else
1600                 header += (GRIPPER_WIDTH + REBAR_PRE_GRIPPER);
1601         else
1602             header += (REBAR_PRE_GRIPPER + GRIPPER_WIDTH);
1603         /* Always have 4 pixels before anything else */
1604         header += REBAR_ALWAYS_SPACE;
1605     }
1606
1607     /* image is visible */
1608     if (lpBand->iImage != -1 && (infoPtr->himl)) {
1609         lpBand->fStatus |= HAS_IMAGE;
1610         if (infoPtr->dwStyle & CCS_VERT) {
1611            header += (infoPtr->imageSize.cy + REBAR_POST_IMAGE);
1612            imageheight = infoPtr->imageSize.cx + 4;
1613         }
1614         else {
1615            header += (infoPtr->imageSize.cx + REBAR_POST_IMAGE);
1616            imageheight = infoPtr->imageSize.cy + 4;
1617         }
1618     }
1619
1620     /* text is visible */
1621     if ((lpBand->fMask & RBBIM_TEXT) && (lpBand->lpText) &&
1622         !(lpBand->fStyle & RBBS_HIDETITLE)) {
1623         HDC hdc = GetDC (0);
1624         HFONT hOldFont = SelectObject (hdc, infoPtr->hFont);
1625         SIZE size;
1626
1627         lpBand->fStatus |= HAS_TEXT;
1628         GetTextExtentPoint32W (hdc, lpBand->lpText,
1629                                lstrlenW (lpBand->lpText), &size);
1630         header += ((infoPtr->dwStyle & CCS_VERT) ? (size.cy + REBAR_POST_TEXT) : (size.cx + REBAR_POST_TEXT));
1631         textheight = (infoPtr->dwStyle & CCS_VERT) ? 0 : size.cy;
1632
1633         SelectObject (hdc, hOldFont);
1634         ReleaseDC (0, hdc);
1635     }
1636
1637     /* if no gripper but either image or text, then leave space */
1638     if ((lpBand->fStatus & (HAS_IMAGE | HAS_TEXT)) &&
1639         !(lpBand->fStatus & HAS_GRIPPER)) {
1640         header += REBAR_ALWAYS_SPACE;
1641     }
1642
1643     /* check if user overrode the header value */
1644     if (!(lpBand->fStyle & RBBS_UNDOC_FIXEDHEADER))
1645         lpBand->cxHeader = header;
1646     lpBand->cyHeader = max(textheight, imageheight);
1647
1648     /* Now compute minimum size of child window */
1649     update_min_band_height(infoPtr, lpBand);       /* update lpBand->cyMinBand from cyHeader and cyChild*/
1650
1651     lpBand->cxMinBand = lpBand->cxMinChild + lpBand->cxHeader + REBAR_POST_CHILD;
1652     if (lpBand->fStyle & RBBS_USECHEVRON && lpBand->cxMinChild < lpBand->cxIdeal)
1653         lpBand->cxMinBand += CHEVRON_WIDTH;
1654 }
1655
1656 static UINT
1657 REBAR_CommonSetupBand(HWND hwnd, const REBARBANDINFOW *lprbbi, REBAR_BAND *lpBand)
1658      /* Function:  This routine copies the supplied values from   */
1659      /*  user input (lprbbi) to the internal band structure.      */
1660      /*  It returns the mask of what changed.   */
1661 {
1662     UINT uChanged = 0x0;
1663
1664     lpBand->fMask |= lprbbi->fMask;
1665
1666     if( (lprbbi->fMask & RBBIM_STYLE) &&
1667         (lpBand->fStyle != lprbbi->fStyle ) )
1668     {
1669         lpBand->fStyle = lprbbi->fStyle;
1670         uChanged |= RBBIM_STYLE;
1671     }
1672
1673     if( (lprbbi->fMask & RBBIM_COLORS) &&
1674        ( ( lpBand->clrFore != lprbbi->clrFore ) ||
1675          ( lpBand->clrBack != lprbbi->clrBack ) ) )
1676     {
1677         lpBand->clrFore = lprbbi->clrFore;
1678         lpBand->clrBack = lprbbi->clrBack;
1679         uChanged |= RBBIM_COLORS;
1680     }
1681
1682     if( (lprbbi->fMask & RBBIM_IMAGE) &&
1683        ( lpBand->iImage != lprbbi->iImage ) )
1684     {
1685         lpBand->iImage = lprbbi->iImage;
1686         uChanged |= RBBIM_IMAGE;
1687     }
1688
1689     if( (lprbbi->fMask & RBBIM_CHILD) &&
1690        (lprbbi->hwndChild != lpBand->hwndChild ) )
1691     {
1692         if (lprbbi->hwndChild) {
1693             lpBand->hwndChild = lprbbi->hwndChild;
1694             lpBand->hwndPrevParent =
1695                 SetParent (lpBand->hwndChild, hwnd);
1696             /* below in trace from WinRAR */
1697             ShowWindow(lpBand->hwndChild, SW_SHOWNOACTIVATE | SW_SHOWNORMAL);
1698             /* above in trace from WinRAR */
1699         }
1700         else {
1701             TRACE("child: %p  prev parent: %p\n",
1702                    lpBand->hwndChild, lpBand->hwndPrevParent);
1703             lpBand->hwndChild = 0;
1704             lpBand->hwndPrevParent = 0;
1705         }
1706         uChanged |= RBBIM_CHILD;
1707     }
1708
1709     if( (lprbbi->fMask & RBBIM_CHILDSIZE) &&
1710         ( (lpBand->cxMinChild != lprbbi->cxMinChild) ||
1711           (lpBand->cyMinChild != lprbbi->cyMinChild ) ||
1712           ( (lprbbi->cbSize >= REBARBANDINFOA_V6_SIZE && (lpBand->fStyle & RBBS_VARIABLEHEIGHT)) &&
1713             ( (lpBand->cyChild    != lprbbi->cyChild ) ||
1714               (lpBand->cyMaxChild != lprbbi->cyMaxChild ) ||
1715               (lpBand->cyIntegral != lprbbi->cyIntegral ) ) ) ||
1716           ( (lprbbi->cbSize < REBARBANDINFOA_V6_SIZE) &&
1717             ( (lpBand->cyChild || 
1718                lpBand->cyMaxChild || 
1719                lpBand->cyIntegral ) ) ) ) )
1720     {
1721         lpBand->cxMinChild = lprbbi->cxMinChild;
1722         lpBand->cyMinChild = lprbbi->cyMinChild;
1723         /* These fields where added in WIN32_IE == 0x400 and are set only for RBBS_VARIABLEHEIGHT bands */
1724         if (lprbbi->cbSize >= REBARBANDINFOA_V6_SIZE && (lpBand->fStyle & RBBS_VARIABLEHEIGHT)) {
1725             lpBand->cyMaxChild = lprbbi->cyMaxChild;
1726             lpBand->cyIntegral = lprbbi->cyIntegral;
1727
1728             lpBand->cyChild = round_child_height(lpBand, lprbbi->cyChild);  /* make (cyChild - cyMinChild) a multiple of cyIntergral */
1729         }
1730         else {
1731             lpBand->cyChild    = lpBand->cyMinChild;
1732             lpBand->cyMaxChild = 0x7fffffff;
1733             lpBand->cyIntegral = 0;
1734         }
1735         uChanged |= RBBIM_CHILDSIZE;
1736     }
1737
1738     if( (lprbbi->fMask & RBBIM_SIZE) &&
1739         (lpBand->cx != lprbbi->cx ) )
1740     {
1741         lpBand->cx = lprbbi->cx;
1742         uChanged |= RBBIM_SIZE;
1743     }
1744
1745     if( (lprbbi->fMask & RBBIM_BACKGROUND) &&
1746        ( lpBand->hbmBack != lprbbi->hbmBack ) )
1747     {
1748         lpBand->hbmBack = lprbbi->hbmBack;
1749         uChanged |= RBBIM_BACKGROUND;
1750     }
1751
1752     if( (lprbbi->fMask & RBBIM_ID) &&
1753         (lpBand->wID != lprbbi->wID ) )
1754     {
1755         lpBand->wID = lprbbi->wID;
1756         uChanged |= RBBIM_ID;
1757     }
1758
1759     /* check for additional data */
1760     if (lprbbi->cbSize >= REBARBANDINFOA_V6_SIZE) {
1761         if( (lprbbi->fMask & RBBIM_IDEALSIZE) &&
1762             ( lpBand->cxIdeal != lprbbi->cxIdeal ) )
1763         {
1764             lpBand->cxIdeal = lprbbi->cxIdeal;
1765             uChanged |= RBBIM_IDEALSIZE;
1766         }
1767
1768         if( (lprbbi->fMask & RBBIM_LPARAM) &&
1769             (lpBand->lParam != lprbbi->lParam ) )
1770         {
1771             lpBand->lParam = lprbbi->lParam;
1772             uChanged |= RBBIM_LPARAM;
1773         }
1774
1775         if( (lprbbi->fMask & RBBIM_HEADERSIZE) &&
1776             (lpBand->cxHeader != lprbbi->cxHeader ) )
1777         {
1778             lpBand->cxHeader = lprbbi->cxHeader;
1779             lpBand->fStyle |= RBBS_UNDOC_FIXEDHEADER;
1780             uChanged |= RBBIM_HEADERSIZE;
1781         }
1782     }
1783
1784     return uChanged;
1785 }
1786
1787 static LRESULT
1788 REBAR_InternalEraseBkGnd (const REBAR_INFO *infoPtr, HDC hdc, const RECT *clip)
1789      /* Function:  This erases the background rectangle by drawing  */
1790      /*  each band with its background color (or the default) and   */
1791      /*  draws each bands right separator if necessary. The row     */
1792      /*  separators are drawn on the first band of the next row.    */
1793 {
1794     REBAR_BAND *lpBand;
1795     UINT i;
1796     INT oldrow;
1797     RECT cr;
1798     COLORREF old = CLR_NONE, new;
1799     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
1800
1801     GetClientRect (infoPtr->hwndSelf, &cr);
1802
1803     oldrow = -1;
1804     for(i=0; i<infoPtr->uNumBands; i++) {
1805         RECT rcBand;
1806         lpBand = REBAR_GetBand(infoPtr, i);
1807         if (HIDDENBAND(lpBand)) continue;
1808         translate_rect(infoPtr, &rcBand, &lpBand->rcBand);
1809
1810         /* draw band separator between rows */
1811         if (lpBand->iRow != oldrow) {
1812             oldrow = lpBand->iRow;
1813             if (infoPtr->dwStyle & RBS_BANDBORDERS) {
1814                 RECT rcRowSep;
1815                 rcRowSep = rcBand;
1816                 if (infoPtr->dwStyle & CCS_VERT) {
1817                     rcRowSep.right += SEP_WIDTH_SIZE;
1818                     rcRowSep.bottom = infoPtr->calcSize.cx;
1819                     if (theme)
1820                         DrawThemeEdge (theme, hdc, RP_BAND, 0, &rcRowSep, EDGE_ETCHED, BF_RIGHT, NULL);
1821                     else
1822                         DrawEdge (hdc, &rcRowSep, EDGE_ETCHED, BF_RIGHT);
1823                 }
1824                 else {
1825                     rcRowSep.bottom += SEP_WIDTH_SIZE;
1826                     rcRowSep.right = infoPtr->calcSize.cx;
1827                     if (theme)
1828                         DrawThemeEdge (theme, hdc, RP_BAND, 0, &rcRowSep, EDGE_ETCHED, BF_BOTTOM, NULL);
1829                     else
1830                         DrawEdge (hdc, &rcRowSep, EDGE_ETCHED, BF_BOTTOM);
1831                 }
1832                 TRACE ("drawing band separator bottom (%s)\n",
1833                        wine_dbgstr_rect(&rcRowSep));
1834             }
1835         }
1836
1837         /* draw band separator between bands in a row */
1838         if (infoPtr->dwStyle & RBS_BANDBORDERS && lpBand->rcBand.left > 0) {
1839             RECT rcSep;
1840             rcSep = rcBand;
1841             if (infoPtr->dwStyle & CCS_VERT) {
1842                 rcSep.bottom = rcSep.top;
1843                 rcSep.top -= SEP_WIDTH_SIZE;
1844                 if (theme)
1845                     DrawThemeEdge (theme, hdc, RP_BAND, 0, &rcSep, EDGE_ETCHED, BF_BOTTOM, NULL);
1846                 else
1847                     DrawEdge (hdc, &rcSep, EDGE_ETCHED, BF_BOTTOM);
1848             }
1849             else {
1850                 rcSep.right = rcSep.left;
1851                 rcSep.left -= SEP_WIDTH_SIZE;
1852                 if (theme)
1853                     DrawThemeEdge (theme, hdc, RP_BAND, 0, &rcSep, EDGE_ETCHED, BF_RIGHT, NULL);
1854                 else
1855                     DrawEdge (hdc, &rcSep, EDGE_ETCHED, BF_RIGHT);
1856             }
1857             TRACE("drawing band separator right (%s)\n",
1858                   wine_dbgstr_rect(&rcSep));
1859         }
1860
1861         /* draw the actual background */
1862         if (lpBand->clrBack != CLR_NONE) {
1863             new = (lpBand->clrBack == CLR_DEFAULT) ? infoPtr->clrBtnFace :
1864                     lpBand->clrBack;
1865 #if GLATESTING
1866             /* testing only - make background green to see it */
1867             new = RGB(0,128,0);
1868 #endif
1869         }
1870         else {
1871             /* In the absence of documentation for Rebar vs. CLR_NONE,
1872              * we will use the default BtnFace color. Note documentation
1873              * exists for Listview and Imagelist.
1874              */
1875             new = infoPtr->clrBtnFace;
1876 #if GLATESTING
1877             /* testing only - make background green to see it */
1878             new = RGB(0,128,0);
1879 #endif
1880         }
1881
1882         if (theme)
1883         {
1884             /* When themed, the background color is ignored (but not a
1885              * background bitmap */
1886             DrawThemeBackground (theme, hdc, 0, 0, &cr, &rcBand);
1887         }
1888         else
1889         {
1890             old = SetBkColor (hdc, new);
1891             TRACE("%s background color=0x%06x, band (%d,%d)-(%d,%d), clip (%d,%d)-(%d,%d)\n",
1892                   (lpBand->clrBack == CLR_NONE) ? "none" :
1893                     ((lpBand->clrBack == CLR_DEFAULT) ? "dft" : ""),
1894                   GetBkColor(hdc),
1895                   rcBand.left,rcBand.top,
1896                   rcBand.right,rcBand.bottom,
1897                   clip->left, clip->top,
1898                   clip->right, clip->bottom);
1899             ExtTextOutW (hdc, 0, 0, ETO_OPAQUE, &rcBand, NULL, 0, 0);
1900             if (lpBand->clrBack != CLR_NONE)
1901                 SetBkColor (hdc, old);
1902         }
1903     }
1904     return TRUE;
1905 }
1906
1907 static void
1908 REBAR_InternalHitTest (const REBAR_INFO *infoPtr, const POINT *lpPt, UINT *pFlags, INT *pBand)
1909 {
1910     REBAR_BAND *lpBand;
1911     RECT rect;
1912     UINT  iCount;
1913
1914     GetClientRect (infoPtr->hwndSelf, &rect);
1915
1916     *pFlags = RBHT_NOWHERE;
1917     if (PtInRect (&rect, *lpPt))
1918     {
1919         if (infoPtr->uNumBands == 0) {
1920             *pFlags = RBHT_NOWHERE;
1921             if (pBand)
1922                 *pBand = -1;
1923             TRACE("NOWHERE\n");
1924             return;
1925         }
1926         else {
1927             /* somewhere inside */
1928             for (iCount = 0; iCount < infoPtr->uNumBands; iCount++) {
1929                 RECT rcBand;
1930                 lpBand = REBAR_GetBand(infoPtr, iCount);
1931                 translate_rect(infoPtr, &rcBand, &lpBand->rcBand);
1932                 if (HIDDENBAND(lpBand)) continue;
1933                 if (PtInRect (&rcBand, *lpPt)) {
1934                     if (pBand)
1935                         *pBand = iCount;
1936                     if (PtInRect (&lpBand->rcGripper, *lpPt)) {
1937                         *pFlags = RBHT_GRABBER;
1938                         TRACE("ON GRABBER %d\n", iCount);
1939                         return;
1940                     }
1941                     else if (PtInRect (&lpBand->rcCapImage, *lpPt)) {
1942                         *pFlags = RBHT_CAPTION;
1943                         TRACE("ON CAPTION %d\n", iCount);
1944                         return;
1945                     }
1946                     else if (PtInRect (&lpBand->rcCapText, *lpPt)) {
1947                         *pFlags = RBHT_CAPTION;
1948                         TRACE("ON CAPTION %d\n", iCount);
1949                         return;
1950                     }
1951                     else if (PtInRect (&lpBand->rcChild, *lpPt)) {
1952                         *pFlags = RBHT_CLIENT;
1953                         TRACE("ON CLIENT %d\n", iCount);
1954                         return;
1955                     }
1956                     else if (PtInRect (&lpBand->rcChevron, *lpPt)) {
1957                         *pFlags = RBHT_CHEVRON;
1958                         TRACE("ON CHEVRON %d\n", iCount);
1959                         return;
1960                     }
1961                     else {
1962                         *pFlags = RBHT_NOWHERE;
1963                         TRACE("NOWHERE %d\n", iCount);
1964                         return;
1965                     }
1966                 }
1967             }
1968
1969             *pFlags = RBHT_NOWHERE;
1970             if (pBand)
1971                 *pBand = -1;
1972
1973             TRACE("NOWHERE\n");
1974             return;
1975         }
1976     }
1977     else {
1978         *pFlags = RBHT_NOWHERE;
1979         if (pBand)
1980             *pBand = -1;
1981         TRACE("NOWHERE\n");
1982         return;
1983     }
1984 }
1985
1986 static void
1987 REBAR_HandleLRDrag (REBAR_INFO *infoPtr, const POINT *ptsmove)
1988      /* Function:  This will implement the functionality of a     */
1989      /*  Gripper drag within a row. It will not implement "out-   */
1990      /*  of-row" drags. (They are detected and handled in         */
1991      /*  REBAR_MouseMove.)                                        */
1992      /*  **** FIXME Switching order of bands in a row not   ****  */
1993      /*  ****       yet implemented.                        ****  */
1994 {
1995     REBAR_BAND *hitBand;
1996     INT iHitBand, iRowBegin, iRowEnd;
1997     INT movement, xBand;
1998
1999     /* on first significant mouse movement, issue notify */
2000     if (!(infoPtr->fStatus & BEGIN_DRAG_ISSUED)) {
2001         if (REBAR_Notify_NMREBAR (infoPtr, -1, RBN_BEGINDRAG)) {
2002             /* Notify returned TRUE - abort drag */
2003             infoPtr->dragStart.x = 0;
2004             infoPtr->dragStart.y = 0;
2005             infoPtr->dragNow = infoPtr->dragStart;
2006             infoPtr->iGrabbedBand = -1;
2007             ReleaseCapture ();
2008             return ;
2009         }
2010         infoPtr->fStatus |= BEGIN_DRAG_ISSUED;
2011     }
2012
2013     iHitBand = infoPtr->iGrabbedBand;
2014     iRowBegin = get_row_begin_for_band(infoPtr, iHitBand);
2015     iRowEnd = get_row_end_for_band(infoPtr, iHitBand);
2016     hitBand = REBAR_GetBand(infoPtr, iHitBand);
2017
2018     xBand = hitBand->rcBand.left;
2019     movement = (infoPtr->dwStyle&CCS_VERT ? ptsmove->y : ptsmove->x)
2020                     - (xBand + REBAR_PRE_GRIPPER - infoPtr->ihitoffset);
2021
2022     if (movement < 0) {
2023         INT cxLeft = REBAR_ShrinkBandsRTL(infoPtr, iRowBegin, iHitBand, -movement, TRUE);
2024         hitBand->cxEffective += -movement - cxLeft;
2025         hitBand->cx = hitBand->cxEffective;
2026     } else if (movement > 0) {
2027         INT prev;
2028
2029         if ((prev = prev_visible(infoPtr, iHitBand)) >= 0) {
2030             INT cxLeft = REBAR_ShrinkBandsLTR(infoPtr, iHitBand, iRowEnd, movement, TRUE);
2031             REBAR_BAND *lpPrev = REBAR_GetBand(infoPtr, prev_visible(infoPtr, iHitBand));
2032             lpPrev->cxEffective += movement - cxLeft;
2033             lpPrev->cx = lpPrev->cxEffective;
2034         }
2035     }
2036
2037     REBAR_SetRowRectsX(infoPtr, iRowBegin, iRowEnd);
2038     if (infoPtr->dwStyle & CCS_VERT)
2039         REBAR_CalcVertBand(infoPtr, 0, infoPtr->uNumBands);
2040     else
2041         REBAR_CalcHorzBand(infoPtr, 0, infoPtr->uNumBands);
2042     REBAR_MoveChildWindows(infoPtr, iRowBegin, iRowEnd);
2043 }
2044
2045
2046
2047 /* << REBAR_BeginDrag >> */
2048
2049
2050 static LRESULT
2051 REBAR_DeleteBand (REBAR_INFO *infoPtr, WPARAM wParam)
2052 {
2053     UINT uBand = (UINT)wParam;
2054     REBAR_BAND *lpBand;
2055
2056     if (uBand >= infoPtr->uNumBands)
2057         return FALSE;
2058
2059     TRACE("deleting band %u!\n", uBand);
2060     lpBand = REBAR_GetBand(infoPtr, uBand);
2061     REBAR_Notify_NMREBAR (infoPtr, uBand, RBN_DELETINGBAND);
2062     /* TODO: a return of 1 should probably cancel the deletion */
2063
2064     if (lpBand->hwndChild)
2065         ShowWindow(lpBand->hwndChild, SW_HIDE);
2066     Free(lpBand->lpText);
2067     Free(lpBand);
2068
2069     infoPtr->uNumBands--;
2070     DPA_DeletePtr(infoPtr->bands, uBand);
2071
2072     REBAR_Notify_NMREBAR (infoPtr, -1, RBN_DELETEDBAND);
2073
2074     /* if only 1 band left the re-validate to possible eliminate gripper */
2075     if (infoPtr->uNumBands == 1)
2076       REBAR_ValidateBand (infoPtr, REBAR_GetBand(infoPtr, 0));
2077
2078     REBAR_Layout(infoPtr);
2079
2080     return TRUE;
2081 }
2082
2083
2084 /* << REBAR_DragMove >> */
2085 /* << REBAR_EndDrag >> */
2086
2087
2088 static LRESULT
2089 REBAR_GetBandBorders (const REBAR_INFO *infoPtr, UINT uBand, RECT *lpRect)
2090 {
2091     REBAR_BAND *lpBand;
2092
2093     if (!lpRect)
2094         return 0;
2095     if (uBand >= infoPtr->uNumBands)
2096         return 0;
2097
2098     lpBand = REBAR_GetBand(infoPtr, uBand);
2099
2100     /* FIXME - the following values were determined by experimentation */
2101     /* with the REBAR Control Spy. I have guesses as to what the 4 and */
2102     /* 1 are, but I am not sure. There doesn't seem to be any actual   */
2103     /* difference in size of the control area with and without the     */
2104     /* style.  -  GA                                                   */
2105     if (infoPtr->dwStyle & RBS_BANDBORDERS) {
2106         if (infoPtr->dwStyle & CCS_VERT) {
2107             lpRect->left = 1;
2108             lpRect->top = lpBand->cxHeader + 4;
2109             lpRect->right = 1;
2110             lpRect->bottom = 0;
2111         }
2112         else {
2113             lpRect->left = lpBand->cxHeader + 4;
2114             lpRect->top = 1;
2115             lpRect->right = 0;
2116             lpRect->bottom = 1;
2117         }
2118     }
2119     else {
2120         lpRect->left = lpBand->cxHeader;
2121     }
2122     return 0;
2123 }
2124
2125
2126 static inline LRESULT
2127 REBAR_GetBandCount (const REBAR_INFO *infoPtr)
2128 {
2129     TRACE("band count %u!\n", infoPtr->uNumBands);
2130
2131     return infoPtr->uNumBands;
2132 }
2133
2134
2135 static LRESULT
2136 REBAR_GetBandInfoT(const REBAR_INFO *infoPtr, UINT uIndex, LPREBARBANDINFOW lprbbi, BOOL bUnicode)
2137 {
2138     REBAR_BAND *lpBand;
2139
2140     if (!lprbbi || lprbbi->cbSize < REBARBANDINFOA_V3_SIZE)
2141         return FALSE;
2142
2143     if (uIndex >= infoPtr->uNumBands)
2144         return FALSE;
2145
2146     TRACE("index %u (bUnicode=%d)\n", uIndex, bUnicode);
2147
2148     /* copy band information */
2149     lpBand = REBAR_GetBand(infoPtr, uIndex);
2150
2151     if (lprbbi->fMask & RBBIM_STYLE)
2152         lprbbi->fStyle = lpBand->fStyle;
2153
2154     if (lprbbi->fMask & RBBIM_COLORS) {
2155         lprbbi->clrFore = lpBand->clrFore;
2156         lprbbi->clrBack = lpBand->clrBack;
2157         if (lprbbi->clrBack == CLR_DEFAULT)
2158             lprbbi->clrBack = infoPtr->clrBtnFace;
2159     }
2160
2161     if (lprbbi->fMask & RBBIM_TEXT) {
2162         if (bUnicode)
2163             Str_GetPtrW(lpBand->lpText, lprbbi->lpText, lprbbi->cch);
2164         else
2165             Str_GetPtrWtoA(lpBand->lpText, (LPSTR)lprbbi->lpText, lprbbi->cch);
2166     }
2167
2168     if (lprbbi->fMask & RBBIM_IMAGE)
2169         lprbbi->iImage = lpBand->iImage;
2170
2171     if (lprbbi->fMask & RBBIM_CHILD)
2172         lprbbi->hwndChild = lpBand->hwndChild;
2173
2174     if (lprbbi->fMask & RBBIM_CHILDSIZE) {
2175         lprbbi->cxMinChild = lpBand->cxMinChild;
2176         lprbbi->cyMinChild = lpBand->cyMinChild;
2177         /* to make tests pass we follow Windows behaviour and allow to read these fields only
2178          * for RBBS_VARIABLEHEIGHTS bands */
2179         if (lprbbi->cbSize >= REBARBANDINFOW_V6_SIZE && (lpBand->fStyle & RBBS_VARIABLEHEIGHT)) {
2180             lprbbi->cyChild    = lpBand->cyChild;
2181             lprbbi->cyMaxChild = lpBand->cyMaxChild;
2182             lprbbi->cyIntegral = lpBand->cyIntegral;
2183         }
2184     }
2185
2186     if (lprbbi->fMask & RBBIM_SIZE)
2187         lprbbi->cx = lpBand->cx;
2188
2189     if (lprbbi->fMask & RBBIM_BACKGROUND)
2190         lprbbi->hbmBack = lpBand->hbmBack;
2191
2192     if (lprbbi->fMask & RBBIM_ID)
2193         lprbbi->wID = lpBand->wID;
2194
2195     /* check for additional data */
2196     if (lprbbi->cbSize >= REBARBANDINFOW_V6_SIZE) {
2197         if (lprbbi->fMask & RBBIM_IDEALSIZE)
2198             lprbbi->cxIdeal = lpBand->cxIdeal;
2199
2200         if (lprbbi->fMask & RBBIM_LPARAM)
2201             lprbbi->lParam = lpBand->lParam;
2202
2203         if (lprbbi->fMask & RBBIM_HEADERSIZE)
2204             lprbbi->cxHeader = lpBand->cxHeader;
2205     }
2206
2207     REBAR_DumpBandInfo(lprbbi);
2208
2209     return TRUE;
2210 }
2211
2212
2213 static LRESULT
2214 REBAR_GetBarHeight (const REBAR_INFO *infoPtr)
2215 {
2216     INT nHeight;
2217
2218     nHeight = infoPtr->calcSize.cy;
2219
2220     TRACE("height = %d\n", nHeight);
2221
2222     return nHeight;
2223 }
2224
2225
2226 static LRESULT
2227 REBAR_GetBarInfo (const REBAR_INFO *infoPtr, LPREBARINFO lpInfo)
2228 {
2229     if (!lpInfo || lpInfo->cbSize < sizeof (REBARINFO))
2230         return FALSE;
2231
2232     TRACE("getting bar info!\n");
2233
2234     if (infoPtr->himl) {
2235         lpInfo->himl = infoPtr->himl;
2236         lpInfo->fMask |= RBIM_IMAGELIST;
2237     }
2238
2239     return TRUE;
2240 }
2241
2242
2243 static inline LRESULT
2244 REBAR_GetBkColor (const REBAR_INFO *infoPtr)
2245 {
2246     COLORREF clr = infoPtr->clrBk;
2247
2248     if (clr == CLR_DEFAULT)
2249       clr = infoPtr->clrBtnFace;
2250
2251     TRACE("background color 0x%06x!\n", clr);
2252
2253     return clr;
2254 }
2255
2256
2257 /* << REBAR_GetColorScheme >> */
2258 /* << REBAR_GetDropTarget >> */
2259
2260
2261 static LRESULT
2262 REBAR_GetPalette (const REBAR_INFO *infoPtr)
2263 {
2264     FIXME("empty stub!\n");
2265
2266     return 0;
2267 }
2268
2269
2270 static LRESULT
2271 REBAR_GetRect (const REBAR_INFO *infoPtr, INT iBand, RECT *lprc)
2272 {
2273     REBAR_BAND *lpBand;
2274
2275     if (iBand < 0 || iBand >= infoPtr->uNumBands)
2276         return FALSE;
2277     if (!lprc)
2278         return FALSE;
2279
2280     lpBand = REBAR_GetBand(infoPtr, iBand);
2281     /* For CCS_VERT the coordinates will be swapped - like on Windows */
2282     CopyRect (lprc, &lpBand->rcBand);
2283
2284     TRACE("band %d, (%s)\n", iBand, wine_dbgstr_rect(lprc));
2285
2286     return TRUE;
2287 }
2288
2289
2290 static inline LRESULT
2291 REBAR_GetRowCount (const REBAR_INFO *infoPtr)
2292 {
2293     TRACE("%u\n", infoPtr->uNumRows);
2294
2295     return infoPtr->uNumRows;
2296 }
2297
2298
2299 static LRESULT
2300 REBAR_GetRowHeight (const REBAR_INFO *infoPtr, INT iRow)
2301 {
2302     int j = 0, ret = 0;
2303     UINT i;
2304     REBAR_BAND *lpBand;
2305
2306     for (i=0; i<infoPtr->uNumBands; i++) {
2307         lpBand = REBAR_GetBand(infoPtr, i);
2308         if (HIDDENBAND(lpBand)) continue;
2309         if (lpBand->iRow != iRow) continue;
2310         j = lpBand->rcBand.bottom - lpBand->rcBand.top;
2311         if (j > ret) ret = j;
2312     }
2313
2314     TRACE("row %d, height %d\n", iRow, ret);
2315
2316     return ret;
2317 }
2318
2319
2320 static inline LRESULT
2321 REBAR_GetTextColor (const REBAR_INFO *infoPtr)
2322 {
2323     TRACE("text color 0x%06x!\n", infoPtr->clrText);
2324
2325     return infoPtr->clrText;
2326 }
2327
2328
2329 static inline LRESULT
2330 REBAR_GetToolTips (const REBAR_INFO *infoPtr)
2331 {
2332     return (LRESULT)infoPtr->hwndToolTip;
2333 }
2334
2335
2336 static inline LRESULT
2337 REBAR_GetUnicodeFormat (const REBAR_INFO *infoPtr)
2338 {
2339     TRACE("%s hwnd=%p\n",
2340           infoPtr->bUnicode ? "TRUE" : "FALSE", infoPtr->hwndSelf);
2341
2342     return infoPtr->bUnicode;
2343 }
2344
2345
2346 static inline LRESULT
2347 REBAR_GetVersion (const REBAR_INFO *infoPtr)
2348 {
2349     TRACE("version %d\n", infoPtr->iVersion);
2350     return infoPtr->iVersion;
2351 }
2352
2353
2354 static LRESULT
2355 REBAR_HitTest (const REBAR_INFO *infoPtr, LPRBHITTESTINFO lprbht)
2356 {
2357     if (!lprbht)
2358         return -1;
2359
2360     REBAR_InternalHitTest (infoPtr, &lprbht->pt, &lprbht->flags, &lprbht->iBand);
2361
2362     return lprbht->iBand;
2363 }
2364
2365
2366 static LRESULT
2367 REBAR_IdToIndex (const REBAR_INFO *infoPtr, UINT uId)
2368 {
2369     UINT i;
2370
2371     if (infoPtr->uNumBands < 1)
2372         return -1;
2373
2374     for (i = 0; i < infoPtr->uNumBands; i++) {
2375         if (REBAR_GetBand(infoPtr, i)->wID == uId) {
2376             TRACE("id %u is band %u found!\n", uId, i);
2377             return i;
2378         }
2379     }
2380
2381     TRACE("id %u is not found\n", uId);
2382     return -1;
2383 }
2384
2385
2386 static LRESULT
2387 REBAR_InsertBandT(REBAR_INFO *infoPtr, INT iIndex, LPREBARBANDINFOW lprbbi, BOOL bUnicode)
2388 {
2389     REBAR_BAND *lpBand;
2390
2391     if (!lprbbi || lprbbi->cbSize < REBARBANDINFOA_V3_SIZE)
2392         return FALSE;
2393
2394     /* trace the index as signed to see the -1 */
2395     TRACE("insert band at %d (bUnicode=%d)!\n", iIndex, bUnicode);
2396     REBAR_DumpBandInfo(lprbbi);
2397
2398     if (!(lpBand = Alloc(sizeof(REBAR_BAND)))) return FALSE;
2399     if ((iIndex == -1) || (iIndex > infoPtr->uNumBands))
2400         iIndex = infoPtr->uNumBands;
2401     if (DPA_InsertPtr(infoPtr->bands, iIndex, lpBand) == -1)
2402     {
2403         Free(lpBand);
2404         return FALSE;
2405     }
2406     infoPtr->uNumBands++;
2407
2408     TRACE("index %d!\n", iIndex);
2409
2410     /* initialize band */
2411     memset(lpBand, 0, sizeof(lpBand));
2412     lpBand->clrFore = infoPtr->clrText == CLR_NONE ? infoPtr->clrBtnText :
2413                                                      infoPtr->clrText;
2414     lpBand->clrBack = infoPtr->clrBk == CLR_NONE ? infoPtr->clrBtnFace :
2415                                                    infoPtr->clrBk;
2416     lpBand->iImage = -1;
2417
2418     REBAR_CommonSetupBand(infoPtr->hwndSelf, lprbbi, lpBand);
2419
2420     /* Make sure the defaults for these are correct */
2421     if (lprbbi->cbSize < REBARBANDINFOA_V6_SIZE || !(lpBand->fStyle & RBBS_VARIABLEHEIGHT)) {
2422         lpBand->cyChild    = lpBand->cyMinChild;
2423         lpBand->cyMaxChild = 0x7fffffff;
2424         lpBand->cyIntegral = 0;
2425     }
2426
2427     if ((lprbbi->fMask & RBBIM_TEXT) && (lprbbi->lpText)) {
2428         if (bUnicode)
2429             Str_SetPtrW(&lpBand->lpText, lprbbi->lpText);
2430         else
2431             Str_SetPtrAtoW(&lpBand->lpText, (LPSTR)lprbbi->lpText);
2432     }
2433
2434     REBAR_ValidateBand (infoPtr, lpBand);
2435     /* On insert of second band, revalidate band 1 to possible add gripper */
2436     if (infoPtr->uNumBands == 2)
2437         REBAR_ValidateBand (infoPtr, REBAR_GetBand(infoPtr, 0));
2438
2439     REBAR_DumpBand (infoPtr);
2440
2441     REBAR_Layout(infoPtr);
2442     InvalidateRect(infoPtr->hwndSelf, 0, TRUE);
2443
2444     return TRUE;
2445 }
2446
2447
2448 static LRESULT
2449 REBAR_MaximizeBand (const REBAR_INFO *infoPtr, INT iBand, LPARAM lParam)
2450 {
2451     REBAR_BAND *lpBand;
2452     int iRowBegin, iRowEnd;
2453     int cxDesired, extra, extraOrig;
2454     int cxIdealBand;
2455
2456     /* Validate */
2457     if (infoPtr->uNumBands == 0 || iBand < 0 || iBand >= infoPtr->uNumBands) {
2458         /* error !!! */
2459         ERR("Illegal MaximizeBand, requested=%d, current band count=%d\n",
2460               iBand, infoPtr->uNumBands);
2461         return FALSE;
2462     }
2463
2464     lpBand = REBAR_GetBand(infoPtr, iBand);
2465
2466     if (lpBand->fStyle & RBBS_HIDDEN)
2467     {
2468         /* Windows is buggy and creates a hole */
2469         WARN("Ignoring maximize request on a hidden band (%d)\n", iBand);
2470         return FALSE;
2471     }
2472
2473     cxIdealBand = lpBand->cxIdeal + lpBand->cxHeader + REBAR_POST_CHILD;
2474     if (lParam && (lpBand->cxEffective < cxIdealBand))
2475         cxDesired = cxIdealBand;
2476     else
2477         cxDesired = infoPtr->calcSize.cx;
2478
2479     iRowBegin = get_row_begin_for_band(infoPtr, iBand);
2480     iRowEnd   = get_row_end_for_band(infoPtr, iBand);
2481     extraOrig = extra = cxDesired - lpBand->cxEffective;
2482     if (extra > 0)
2483         extra = REBAR_ShrinkBandsRTL(infoPtr, iRowBegin, iBand, extra, TRUE);
2484     if (extra > 0)
2485         extra = REBAR_ShrinkBandsLTR(infoPtr, next_visible(infoPtr, iBand), iRowEnd, extra, TRUE);
2486     lpBand->cxEffective += extraOrig - extra;
2487     lpBand->cx = lpBand->cxEffective;
2488     TRACE("(%d, %ld): Wanted size %d, obtained %d (shrink %d, %d)\n", iBand, lParam, cxDesired, lpBand->cx, extraOrig, extra);
2489     REBAR_SetRowRectsX(infoPtr, iRowBegin, iRowEnd);
2490
2491     if (infoPtr->dwStyle & CCS_VERT)
2492         REBAR_CalcVertBand(infoPtr, iRowBegin, iRowEnd);
2493     else
2494         REBAR_CalcHorzBand(infoPtr, iRowBegin, iRowEnd);
2495     REBAR_MoveChildWindows(infoPtr, iRowBegin, iRowEnd);
2496     return TRUE;
2497
2498 }
2499
2500
2501 static LRESULT
2502 REBAR_MinimizeBand (const REBAR_INFO *infoPtr, INT iBand)
2503 {
2504     REBAR_BAND *lpBand;
2505     int iPrev, iRowBegin, iRowEnd;
2506
2507     /* A "minimize" band is equivalent to "dragging" the gripper
2508      * of than band to the right till the band is only the size
2509      * of the cxHeader.
2510      */
2511
2512     /* Validate */
2513     if (infoPtr->uNumBands == 0 || iBand < 0 || iBand >= infoPtr->uNumBands) {
2514         /* error !!! */
2515         ERR("Illegal MinimizeBand, requested=%d, current band count=%d\n",
2516               iBand, infoPtr->uNumBands);
2517         return FALSE;
2518     }
2519
2520     /* compute amount of movement and validate */
2521     lpBand = REBAR_GetBand(infoPtr, iBand);
2522
2523     if (lpBand->fStyle & RBBS_HIDDEN)
2524     {
2525         /* Windows is buggy and creates a hole/overlap */
2526         WARN("Ignoring minimize request on a hidden band (%d)\n", iBand);
2527         return FALSE;
2528     }
2529
2530     iPrev = prev_visible(infoPtr, iBand);
2531     /* if first band in row */
2532     if (iPrev < 0 || REBAR_GetBand(infoPtr, iPrev)->iRow != lpBand->iRow) {
2533         int iNext = next_visible(infoPtr, iBand);
2534         if (iNext < infoPtr->uNumBands && REBAR_GetBand(infoPtr, iNext)->iRow == lpBand->iRow) {
2535             TRACE("(%d): Minimizing the first band in row is by maximizing the second\n", iBand);
2536             REBAR_MaximizeBand(infoPtr, iNext, FALSE);
2537         }
2538         else
2539             TRACE("(%d): Only one band in row - nothing to do\n", iBand);
2540         return TRUE;
2541     }
2542
2543     REBAR_GetBand(infoPtr, iPrev)->cxEffective += lpBand->cxEffective - lpBand->cxMinBand;
2544     REBAR_GetBand(infoPtr, iPrev)->cx = REBAR_GetBand(infoPtr, iPrev)->cxEffective;
2545     lpBand->cx = lpBand->cxEffective = lpBand->cxMinBand;
2546
2547     iRowBegin = get_row_begin_for_band(infoPtr, iBand);
2548     iRowEnd = get_row_end_for_band(infoPtr, iBand);
2549     REBAR_SetRowRectsX(infoPtr, iRowBegin, iRowEnd);
2550
2551     if (infoPtr->dwStyle & CCS_VERT)
2552         REBAR_CalcVertBand(infoPtr, iRowBegin, iRowEnd);
2553     else
2554         REBAR_CalcHorzBand(infoPtr, iRowBegin, iRowEnd);
2555     REBAR_MoveChildWindows(infoPtr, iRowBegin, iRowEnd);
2556     return FALSE;
2557 }
2558
2559
2560 static LRESULT
2561 REBAR_MoveBand (REBAR_INFO *infoPtr, INT iFrom, INT iTo)
2562 {
2563     REBAR_BAND *lpBand;
2564
2565     /* Validate */
2566     if ((infoPtr->uNumBands == 0) ||
2567         (iFrom < 0) || iFrom >= infoPtr->uNumBands ||
2568         (iTo < 0)   || iTo >= infoPtr->uNumBands) {
2569         /* error !!! */
2570         ERR("Illegal MoveBand, from=%d, to=%d, current band count=%d\n",
2571               iFrom, iTo, infoPtr->uNumBands);
2572         return FALSE;
2573     }
2574
2575     lpBand = REBAR_GetBand(infoPtr, iFrom);
2576     DPA_DeletePtr(infoPtr->bands, iFrom);
2577     DPA_InsertPtr(infoPtr->bands, iTo, lpBand);
2578
2579     TRACE("moved band %d to index %d\n", iFrom, iTo);
2580     REBAR_DumpBand (infoPtr);
2581
2582     /* **************************************************** */
2583     /*                                                      */
2584     /* We do not do a REBAR_Layout here because the native  */
2585     /* control does not do that. The actual layout and      */
2586     /* repaint is done by the *next* real action, ex.:      */
2587     /* RB_INSERTBAND, RB_DELETEBAND, RB_SIZETORECT, etc.    */
2588     /*                                                      */
2589     /* **************************************************** */
2590
2591     return TRUE;
2592 }
2593
2594
2595 /* return TRUE if two strings are different */
2596 static BOOL
2597 REBAR_strdifW( LPCWSTR a, LPCWSTR b )
2598 {
2599     return ( (a && !b) || (b && !a) || (a && b && lstrcmpW(a, b) ) );
2600 }
2601
2602 static LRESULT
2603 REBAR_SetBandInfoT(REBAR_INFO *infoPtr, INT iBand, LPREBARBANDINFOW lprbbi, BOOL bUnicode)
2604 {
2605     REBAR_BAND *lpBand;
2606     UINT uChanged;
2607
2608     if (!lprbbi || lprbbi->cbSize < REBARBANDINFOA_V3_SIZE)
2609         return FALSE;
2610
2611     if (iBand >= infoPtr->uNumBands)
2612         return FALSE;
2613
2614     TRACE("index %d\n", iBand);
2615     REBAR_DumpBandInfo (lprbbi);
2616
2617     /* set band information */
2618     lpBand = REBAR_GetBand(infoPtr, iBand);
2619
2620     uChanged = REBAR_CommonSetupBand (infoPtr->hwndSelf, lprbbi, lpBand);
2621     if (lprbbi->fMask & RBBIM_TEXT) {
2622         LPWSTR wstr = NULL;
2623         if (bUnicode)
2624             Str_SetPtrW(&wstr, lprbbi->lpText);
2625         else
2626             Str_SetPtrAtoW(&wstr, (LPSTR)lprbbi->lpText);
2627
2628         if (REBAR_strdifW(wstr, lpBand->lpText)) {
2629             Free(lpBand->lpText);
2630             lpBand->lpText = wstr;
2631             uChanged |= RBBIM_TEXT;
2632         }
2633         else
2634             Free(wstr);
2635     }
2636
2637     REBAR_ValidateBand (infoPtr, lpBand);
2638
2639     REBAR_DumpBand (infoPtr);
2640
2641     if (uChanged & (RBBIM_CHILDSIZE | RBBIM_SIZE | RBBIM_STYLE | RBBIM_IMAGE)) {
2642           REBAR_Layout(infoPtr);
2643           InvalidateRect(infoPtr->hwndSelf, 0, 1);
2644     }
2645
2646     return TRUE;
2647 }
2648
2649
2650 static LRESULT
2651 REBAR_SetBarInfo (REBAR_INFO *infoPtr, LPREBARINFO lpInfo)
2652 {
2653     REBAR_BAND *lpBand;
2654     UINT i;
2655
2656     if (!lpInfo || lpInfo->cbSize < sizeof (REBARINFO))
2657         return FALSE;
2658
2659     TRACE("setting bar info!\n");
2660
2661     if (lpInfo->fMask & RBIM_IMAGELIST) {
2662         infoPtr->himl = lpInfo->himl;
2663         if (infoPtr->himl) {
2664             INT cx, cy;
2665             ImageList_GetIconSize (infoPtr->himl, &cx, &cy);
2666             infoPtr->imageSize.cx = cx;
2667             infoPtr->imageSize.cy = cy;
2668         }
2669         else {
2670             infoPtr->imageSize.cx = 0;
2671             infoPtr->imageSize.cy = 0;
2672         }
2673         TRACE("new image cx=%d, cy=%d\n", infoPtr->imageSize.cx,
2674               infoPtr->imageSize.cy);
2675     }
2676
2677     /* revalidate all bands to reset flags for images in headers of bands */
2678     for (i=0; i<infoPtr->uNumBands; i++) {
2679         lpBand = REBAR_GetBand(infoPtr, i);
2680         REBAR_ValidateBand (infoPtr, lpBand);
2681     }
2682
2683     return TRUE;
2684 }
2685
2686
2687 static LRESULT
2688 REBAR_SetBkColor (REBAR_INFO *infoPtr, COLORREF clr)
2689 {
2690     COLORREF clrTemp;
2691
2692     clrTemp = infoPtr->clrBk;
2693     infoPtr->clrBk = clr;
2694
2695     TRACE("background color 0x%06x!\n", infoPtr->clrBk);
2696
2697     return clrTemp;
2698 }
2699
2700
2701 /* << REBAR_SetColorScheme >> */
2702 /* << REBAR_SetPalette >> */
2703
2704
2705 static LRESULT
2706 REBAR_SetParent (REBAR_INFO *infoPtr, HWND parent)
2707 {
2708     HWND hwndTemp = infoPtr->hwndNotify;
2709
2710     infoPtr->hwndNotify = parent;
2711
2712     return (LRESULT)hwndTemp;
2713 }
2714
2715
2716 static LRESULT
2717 REBAR_SetTextColor (REBAR_INFO *infoPtr, COLORREF clr)
2718 {
2719     COLORREF clrTemp;
2720
2721     clrTemp = infoPtr->clrText;
2722     infoPtr->clrText = clr;
2723
2724     TRACE("text color 0x%06x!\n", infoPtr->clrText);
2725
2726     return clrTemp;
2727 }
2728
2729
2730 /* << REBAR_SetTooltips >> */
2731
2732
2733 static inline LRESULT
2734 REBAR_SetUnicodeFormat (REBAR_INFO *infoPtr, BOOL unicode)
2735 {
2736     BOOL bTemp = infoPtr->bUnicode;
2737
2738     TRACE("to %s hwnd=%p, was %s\n",
2739            unicode ? "TRUE" : "FALSE", infoPtr->hwndSelf,
2740           (bTemp) ? "TRUE" : "FALSE");
2741
2742     infoPtr->bUnicode = unicode;
2743
2744    return bTemp;
2745 }
2746
2747
2748 static LRESULT
2749 REBAR_SetVersion (REBAR_INFO *infoPtr, INT iVersion)
2750 {
2751     INT iOldVersion = infoPtr->iVersion;
2752
2753     if (iVersion > COMCTL32_VERSION)
2754         return -1;
2755
2756     infoPtr->iVersion = iVersion;
2757
2758     TRACE("new version %d\n", iVersion);
2759
2760     return iOldVersion;
2761 }
2762
2763
2764 static LRESULT
2765 REBAR_ShowBand (REBAR_INFO *infoPtr, INT iBand, BOOL show)
2766 {
2767     REBAR_BAND *lpBand;
2768
2769     if (iBand < 0 || iBand > infoPtr->uNumBands)
2770         return FALSE;
2771
2772     lpBand = REBAR_GetBand(infoPtr, iBand);
2773
2774     if (show) {
2775         TRACE("show band %d\n", iBand);
2776         lpBand->fStyle = lpBand->fStyle & ~RBBS_HIDDEN;
2777         if (IsWindow (lpBand->hwndChild))
2778             ShowWindow (lpBand->hwndChild, SW_SHOW);
2779     }
2780     else {
2781         TRACE("hide band %d\n", iBand);
2782         lpBand->fStyle = lpBand->fStyle | RBBS_HIDDEN;
2783         if (IsWindow (lpBand->hwndChild))
2784             ShowWindow (lpBand->hwndChild, SW_HIDE);
2785     }
2786
2787     REBAR_Layout(infoPtr);
2788     InvalidateRect(infoPtr->hwndSelf, 0, 1);
2789
2790     return TRUE;
2791 }
2792
2793
2794 static LRESULT
2795 REBAR_SizeToRect (REBAR_INFO *infoPtr, const RECT *lpRect)
2796 {
2797     if (!lpRect) return FALSE;
2798
2799     TRACE("[%s]\n", wine_dbgstr_rect(lpRect));
2800     REBAR_SizeToHeight(infoPtr, get_rect_cy(infoPtr, lpRect));
2801     return TRUE;
2802 }
2803
2804
2805
2806 static LRESULT
2807 REBAR_Create (REBAR_INFO *infoPtr, LPCREATESTRUCTW cs)
2808 {
2809     RECT wnrc1, clrc1;
2810
2811     if (TRACE_ON(rebar)) {
2812         GetWindowRect(infoPtr->hwndSelf, &wnrc1);
2813         GetClientRect(infoPtr->hwndSelf, &clrc1);
2814         TRACE("window=(%s) client=(%s) cs=(%d,%d %dx%d)\n",
2815               wine_dbgstr_rect(&wnrc1), wine_dbgstr_rect(&clrc1),
2816               cs->x, cs->y, cs->cx, cs->cy);
2817     }
2818
2819     TRACE("created!\n");
2820
2821     if (OpenThemeData (infoPtr->hwndSelf, themeClass))
2822     {
2823         /* native seems to clear WS_BORDER when themed */
2824         infoPtr->dwStyle &= ~WS_BORDER;
2825     }
2826
2827     return 0;
2828 }
2829
2830
2831 static LRESULT
2832 REBAR_Destroy (REBAR_INFO *infoPtr)
2833 {
2834     REBAR_BAND *lpBand;
2835     UINT i;
2836
2837     /* clean up each band */
2838     for (i = 0; i < infoPtr->uNumBands; i++) {
2839         lpBand = REBAR_GetBand(infoPtr, i);
2840
2841         /* delete text strings */
2842         Free (lpBand->lpText);
2843         lpBand->lpText = NULL;
2844         /* destroy child window */
2845         DestroyWindow (lpBand->hwndChild);
2846         Free (lpBand);
2847     }
2848
2849     /* free band array */
2850     DPA_Destroy (infoPtr->bands);
2851     infoPtr->bands = NULL;
2852
2853     DestroyCursor (infoPtr->hcurArrow);
2854     DestroyCursor (infoPtr->hcurHorz);
2855     DestroyCursor (infoPtr->hcurVert);
2856     DestroyCursor (infoPtr->hcurDrag);
2857     if (infoPtr->hDefaultFont) DeleteObject (infoPtr->hDefaultFont);
2858     SetWindowLongPtrW (infoPtr->hwndSelf, 0, 0);
2859     
2860     CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2861
2862     /* free rebar info data */
2863     Free (infoPtr);
2864     TRACE("destroyed!\n");
2865     return 0;
2866 }
2867
2868
2869 static LRESULT
2870 REBAR_EraseBkGnd (const REBAR_INFO *infoPtr, HDC hdc)
2871 {
2872     RECT cliprect;
2873
2874     if (GetClipBox ( hdc, &cliprect))
2875         return REBAR_InternalEraseBkGnd (infoPtr, hdc, &cliprect);
2876     return 0;
2877 }
2878
2879
2880 static LRESULT
2881 REBAR_GetFont (const REBAR_INFO *infoPtr)
2882 {
2883     return (LRESULT)infoPtr->hFont;
2884 }
2885
2886 static LRESULT
2887 REBAR_PushChevron(const REBAR_INFO *infoPtr, UINT uBand, LPARAM lParam)
2888 {
2889     if (uBand < infoPtr->uNumBands)
2890     {
2891         NMREBARCHEVRON nmrbc;
2892         REBAR_BAND *lpBand = REBAR_GetBand(infoPtr, uBand);
2893
2894         TRACE("Pressed chevron on band %u\n", uBand);
2895
2896         /* redraw chevron in pushed state */
2897         lpBand->fDraw |= DRAW_CHEVRONPUSHED;
2898         RedrawWindow(infoPtr->hwndSelf, &lpBand->rcChevron,0,
2899           RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW);
2900
2901         /* notify app so it can display a popup menu or whatever */
2902         nmrbc.uBand = uBand;
2903         nmrbc.wID = lpBand->wID;
2904         nmrbc.lParam = lpBand->lParam;
2905         nmrbc.rc = lpBand->rcChevron;
2906         nmrbc.lParamNM = lParam;
2907         REBAR_Notify((NMHDR*)&nmrbc, infoPtr, RBN_CHEVRONPUSHED);
2908
2909         /* redraw chevron in previous state */
2910         lpBand->fDraw &= ~DRAW_CHEVRONPUSHED;
2911         InvalidateRect(infoPtr->hwndSelf, &lpBand->rcChevron, TRUE);
2912
2913         return TRUE;
2914     }
2915     return FALSE;
2916 }
2917
2918 static LRESULT
2919 REBAR_LButtonDown (REBAR_INFO *infoPtr, LPARAM lParam)
2920 {
2921     REBAR_BAND *lpBand;
2922     UINT htFlags;
2923     INT iHitBand;
2924     POINT ptMouseDown;
2925     ptMouseDown.x = (short)LOWORD(lParam);
2926     ptMouseDown.y = (short)HIWORD(lParam);
2927
2928     REBAR_InternalHitTest(infoPtr, &ptMouseDown, &htFlags, &iHitBand);
2929     lpBand = REBAR_GetBand(infoPtr, iHitBand);
2930
2931     if (htFlags == RBHT_CHEVRON)
2932     {
2933         REBAR_PushChevron(infoPtr, iHitBand, 0);
2934     }
2935     else if (htFlags == RBHT_GRABBER || htFlags == RBHT_CAPTION)
2936     {
2937         TRACE("Starting drag\n");
2938
2939         SetCapture (infoPtr->hwndSelf);
2940         infoPtr->iGrabbedBand = iHitBand;
2941
2942         /* save off the LOWORD and HIWORD of lParam as initial x,y */
2943         infoPtr->dragStart.x = (short)LOWORD(lParam);
2944         infoPtr->dragStart.y = (short)HIWORD(lParam);
2945         infoPtr->dragNow = infoPtr->dragStart;
2946         if (infoPtr->dwStyle & CCS_VERT)
2947             infoPtr->ihitoffset = infoPtr->dragStart.y - (lpBand->rcBand.left + REBAR_PRE_GRIPPER);
2948         else
2949             infoPtr->ihitoffset = infoPtr->dragStart.x - (lpBand->rcBand.left + REBAR_PRE_GRIPPER);
2950     }
2951     return 0;
2952 }
2953
2954 static LRESULT
2955 REBAR_LButtonUp (REBAR_INFO *infoPtr)
2956 {
2957     if (infoPtr->iGrabbedBand >= 0)
2958     {
2959         NMHDR layout;
2960         RECT rect;
2961
2962         infoPtr->dragStart.x = 0;
2963         infoPtr->dragStart.y = 0;
2964         infoPtr->dragNow = infoPtr->dragStart;
2965
2966         ReleaseCapture ();
2967
2968         if (infoPtr->fStatus & BEGIN_DRAG_ISSUED) {
2969             REBAR_Notify(&layout, infoPtr, RBN_LAYOUTCHANGED);
2970             REBAR_Notify_NMREBAR (infoPtr, infoPtr->iGrabbedBand, RBN_ENDDRAG);
2971             infoPtr->fStatus &= ~BEGIN_DRAG_ISSUED;
2972         }
2973
2974         infoPtr->iGrabbedBand = -1;
2975
2976         GetClientRect(infoPtr->hwndSelf, &rect);
2977         InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2978     }
2979
2980     return 0;
2981 }
2982
2983 static LRESULT
2984 REBAR_MouseLeave (REBAR_INFO *infoPtr)
2985 {
2986     if (infoPtr->ichevronhotBand >= 0)
2987     {
2988         REBAR_BAND *lpChevronBand = REBAR_GetBand(infoPtr, infoPtr->ichevronhotBand);
2989         if (lpChevronBand->fDraw & DRAW_CHEVRONHOT)
2990         {
2991             lpChevronBand->fDraw &= ~DRAW_CHEVRONHOT;
2992             InvalidateRect(infoPtr->hwndSelf, &lpChevronBand->rcChevron, TRUE);
2993         }
2994     }
2995     infoPtr->iOldBand = -1;
2996     infoPtr->ichevronhotBand = -2;
2997
2998     return TRUE;
2999 }
3000
3001 static LRESULT
3002 REBAR_MouseMove (REBAR_INFO *infoPtr, LPARAM lParam)
3003 {
3004     REBAR_BAND *lpChevronBand;
3005     POINT ptMove;
3006
3007     ptMove.x = (short)LOWORD(lParam);
3008     ptMove.y = (short)HIWORD(lParam);
3009
3010     /* if we are currently dragging a band */
3011     if (infoPtr->iGrabbedBand >= 0)
3012     {
3013         REBAR_BAND *band1 = NULL, *band2;
3014         int yPtMove = (infoPtr->dwStyle & CCS_VERT ? ptMove.x : ptMove.y);
3015
3016         if (GetCapture() != infoPtr->hwndSelf)
3017             ERR("We are dragging but haven't got capture?!?\n");
3018
3019         if (infoPtr->iGrabbedBand > 0)
3020             band1 = REBAR_GetBand(infoPtr, infoPtr->iGrabbedBand - 1);
3021         band2 = REBAR_GetBand(infoPtr, infoPtr->iGrabbedBand);
3022
3023         /* if mouse did not move much, exit */
3024         if ((abs(ptMove.x - infoPtr->dragNow.x) <= mindragx) &&
3025             (abs(ptMove.y - infoPtr->dragNow.y) <= mindragy)) return 0;
3026
3027         /* Test for valid drag case - must not be first band in row */
3028         if ((yPtMove < band2->rcBand.top) ||
3029               (yPtMove > band2->rcBand.bottom) ||
3030               ((infoPtr->iGrabbedBand > 0) && (band1->iRow != band2->iRow))) {
3031             FIXME("Cannot drag to other rows yet!!\n");
3032         }
3033         else {
3034             REBAR_HandleLRDrag (infoPtr, &ptMove);
3035         }
3036     }
3037     else
3038     {
3039         INT iHitBand;
3040         UINT htFlags;
3041         TRACKMOUSEEVENT trackinfo;
3042
3043         REBAR_InternalHitTest(infoPtr, &ptMove, &htFlags, &iHitBand);
3044
3045         if (infoPtr->iOldBand >= 0 && infoPtr->iOldBand == infoPtr->ichevronhotBand)
3046         {
3047             lpChevronBand = REBAR_GetBand(infoPtr, infoPtr->ichevronhotBand);
3048             if (lpChevronBand->fDraw & DRAW_CHEVRONHOT)
3049             {
3050                 lpChevronBand->fDraw &= ~DRAW_CHEVRONHOT;
3051                 InvalidateRect(infoPtr->hwndSelf, &lpChevronBand->rcChevron, TRUE);
3052             }
3053             infoPtr->ichevronhotBand = -2;
3054         }
3055
3056         if (htFlags == RBHT_CHEVRON)
3057         {
3058             /* fill in the TRACKMOUSEEVENT struct */
3059             trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3060             trackinfo.dwFlags = TME_QUERY;
3061             trackinfo.hwndTrack = infoPtr->hwndSelf;
3062             trackinfo.dwHoverTime = 0;
3063
3064             /* call _TrackMouseEvent to see if we are currently tracking for this hwnd */
3065             _TrackMouseEvent(&trackinfo);
3066
3067             /* Make sure tracking is enabled so we receive a WM_MOUSELEAVE message */
3068             if(!(trackinfo.dwFlags & TME_LEAVE))
3069             {
3070                 trackinfo.dwFlags = TME_LEAVE; /* notify upon leaving */
3071
3072                 /* call TRACKMOUSEEVENT so we receive a WM_MOUSELEAVE message */
3073                 /* and can properly deactivate the hot chevron */
3074                 _TrackMouseEvent(&trackinfo);
3075             }
3076
3077             lpChevronBand = REBAR_GetBand(infoPtr, iHitBand);
3078             if (!(lpChevronBand->fDraw & DRAW_CHEVRONHOT))
3079             {
3080                 lpChevronBand->fDraw |= DRAW_CHEVRONHOT;
3081                 InvalidateRect(infoPtr->hwndSelf, &lpChevronBand->rcChevron, TRUE);
3082                 infoPtr->ichevronhotBand = iHitBand;
3083             }
3084         }
3085         infoPtr->iOldBand = iHitBand;
3086     }
3087
3088     return 0;
3089 }
3090
3091
3092 static inline LRESULT
3093 REBAR_NCCalcSize (const REBAR_INFO *infoPtr, RECT *rect)
3094 {
3095     HTHEME theme;
3096
3097     if (infoPtr->dwStyle & WS_BORDER) {
3098         rect->left   = min(rect->left + GetSystemMetrics(SM_CXEDGE), rect->right);
3099         rect->right  = max(rect->right - GetSystemMetrics(SM_CXEDGE), rect->left);
3100         rect->top    = min(rect->top + GetSystemMetrics(SM_CYEDGE), rect->bottom);
3101         rect->bottom = max(rect->bottom - GetSystemMetrics(SM_CYEDGE), rect->top);
3102     }
3103     else if ((theme = GetWindowTheme (infoPtr->hwndSelf)))
3104     {
3105         /* FIXME: should use GetThemeInt */
3106         rect->top = min(rect->top + 1, rect->bottom);
3107     }
3108     TRACE("new client=(%s)\n", wine_dbgstr_rect(rect));
3109     return 0;
3110 }
3111
3112
3113 static LRESULT
3114 REBAR_NCCreate (HWND hwnd, LPCREATESTRUCTW cs)
3115 {
3116     REBAR_INFO *infoPtr = REBAR_GetInfoPtr (hwnd);
3117     RECT wnrc1, clrc1;
3118     NONCLIENTMETRICSW ncm;
3119     HFONT tfont;
3120
3121     if (infoPtr) {
3122         ERR("Strange info structure pointer *not* NULL\n");
3123         return FALSE;
3124     }
3125
3126     if (TRACE_ON(rebar)) {
3127         GetWindowRect(hwnd, &wnrc1);
3128         GetClientRect(hwnd, &clrc1);
3129         TRACE("window=(%s) client=(%s) cs=(%d,%d %dx%d)\n",
3130               wine_dbgstr_rect(&wnrc1), wine_dbgstr_rect(&clrc1),
3131               cs->x, cs->y, cs->cx, cs->cy);
3132     }
3133
3134     /* allocate memory for info structure */
3135     infoPtr = Alloc (sizeof(REBAR_INFO));
3136     SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
3137
3138     /* initialize info structure - initial values are 0 */
3139     infoPtr->clrBk = CLR_NONE;
3140     infoPtr->clrText = CLR_NONE;
3141     infoPtr->clrBtnText = comctl32_color.clrBtnText;
3142     infoPtr->clrBtnFace = comctl32_color.clrBtnFace;
3143     infoPtr->iOldBand = -1;
3144     infoPtr->ichevronhotBand = -2;
3145     infoPtr->iGrabbedBand = -1;
3146     infoPtr->hwndSelf = hwnd;
3147     infoPtr->DoRedraw = TRUE;
3148     infoPtr->hcurArrow = LoadCursorW (0, (LPWSTR)IDC_ARROW);
3149     infoPtr->hcurHorz  = LoadCursorW (0, (LPWSTR)IDC_SIZEWE);
3150     infoPtr->hcurVert  = LoadCursorW (0, (LPWSTR)IDC_SIZENS);
3151     infoPtr->hcurDrag  = LoadCursorW (0, (LPWSTR)IDC_SIZE);
3152     infoPtr->fStatus = 0;
3153     infoPtr->hFont = GetStockObject (SYSTEM_FONT);
3154     infoPtr->bands = DPA_Create(8);
3155
3156     /* issue WM_NOTIFYFORMAT to get unicode status of parent */
3157     REBAR_NotifyFormat(infoPtr, NF_REQUERY);
3158
3159     /* Stow away the original style */
3160     infoPtr->orgStyle = cs->style;
3161     /* add necessary styles to the requested styles */
3162     infoPtr->dwStyle = cs->style | WS_VISIBLE;
3163     if ((infoPtr->dwStyle & CCS_LAYOUT_MASK) == 0)
3164         infoPtr->dwStyle |= CCS_TOP;
3165     SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle);
3166
3167     /* get font handle for Caption Font */
3168     ncm.cbSize = sizeof(ncm);
3169     SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
3170     /* if the font is bold, set to normal */
3171     if (ncm.lfCaptionFont.lfWeight > FW_NORMAL) {
3172         ncm.lfCaptionFont.lfWeight = FW_NORMAL;
3173     }
3174     tfont = CreateFontIndirectW (&ncm.lfCaptionFont);
3175     if (tfont) {
3176         infoPtr->hFont = infoPtr->hDefaultFont = tfont;
3177     }
3178
3179 /* native does:
3180             GetSysColor (numerous);
3181             GetSysColorBrush (numerous) (see WM_SYSCOLORCHANGE);
3182            *GetStockObject (SYSTEM_FONT);
3183            *SetWindowLong (hwnd, 0, info ptr);
3184            *WM_NOTIFYFORMAT;
3185            *SetWindowLong (hwnd, GWL_STYLE, style+0x10000001);
3186                                     WS_VISIBLE = 0x10000000;
3187                                     CCS_TOP    = 0x00000001;
3188            *SystemParametersInfo (SPI_GETNONCLIENTMETRICS...);
3189            *CreateFontIndirect (lfCaptionFont from above);
3190             GetDC ();
3191             SelectObject (hdc, fontabove);
3192             GetTextMetrics (hdc, );    guessing is tmHeight
3193             SelectObject (hdc, oldfont);
3194             ReleaseDC ();
3195             GetWindowRect ();
3196             MapWindowPoints (0, parent, rectabove, 2);
3197             GetWindowRect ();
3198             GetClientRect ();
3199             ClientToScreen (clientrect);
3200             SetWindowPos (hwnd, 0, 0, 0, 0, 0, SWP_NOZORDER);
3201  */
3202     return TRUE;
3203 }
3204
3205
3206 static LRESULT
3207 REBAR_NCHitTest (const REBAR_INFO *infoPtr, LPARAM lParam)
3208 {
3209     NMMOUSE nmmouse;
3210     POINT clpt;
3211     INT i;
3212     UINT scrap;
3213     LRESULT ret = HTCLIENT;
3214
3215     /*
3216      * Differences from doc at MSDN (as observed with version 4.71 of
3217      *      comctl32.dll
3218      * 1. doc says nmmouse.pt is in screen coord, trace shows client coord.
3219      * 2. if band is not identified .dwItemSpec is 0xffffffff.
3220      * 3. native always seems to return HTCLIENT if notify return is 0.
3221      */
3222
3223     clpt.x = (short)LOWORD(lParam);
3224     clpt.y = (short)HIWORD(lParam);
3225     ScreenToClient (infoPtr->hwndSelf, &clpt);
3226     REBAR_InternalHitTest (infoPtr, &clpt, &scrap,
3227                            (INT *)&nmmouse.dwItemSpec);
3228     nmmouse.dwItemData = 0;
3229     nmmouse.pt = clpt;
3230     nmmouse.dwHitInfo = 0;
3231     if ((i = REBAR_Notify((NMHDR *) &nmmouse, infoPtr, NM_NCHITTEST))) {
3232         TRACE("notify changed return value from %ld to %d\n",
3233               ret, i);
3234         ret = (LRESULT) i;
3235     }
3236     TRACE("returning %ld, client point (%d,%d)\n", ret, clpt.x, clpt.y);
3237     return ret;
3238 }
3239
3240
3241 static LRESULT
3242 REBAR_NCPaint (const REBAR_INFO *infoPtr)
3243 {
3244     RECT rcWindow;
3245     HDC hdc;
3246     HTHEME theme;
3247
3248     if (infoPtr->dwStyle & WS_MINIMIZE)
3249         return 0; /* Nothing to do */
3250
3251     if (infoPtr->dwStyle & WS_BORDER) {
3252
3253         /* adjust rectangle and draw the necessary edge */
3254         if (!(hdc = GetDCEx( infoPtr->hwndSelf, 0, DCX_USESTYLE | DCX_WINDOW )))
3255             return 0;
3256         GetWindowRect (infoPtr->hwndSelf, &rcWindow);
3257         OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
3258         TRACE("rect (%s)\n", wine_dbgstr_rect(&rcWindow));
3259         DrawEdge (hdc, &rcWindow, EDGE_ETCHED, BF_RECT);
3260         ReleaseDC( infoPtr->hwndSelf, hdc );
3261     }
3262     else if ((theme = GetWindowTheme (infoPtr->hwndSelf)))
3263     {
3264         /* adjust rectangle and draw the necessary edge */
3265         if (!(hdc = GetDCEx( infoPtr->hwndSelf, 0, DCX_USESTYLE | DCX_WINDOW )))
3266             return 0;
3267         GetWindowRect (infoPtr->hwndSelf, &rcWindow);
3268         OffsetRect (&rcWindow, -rcWindow.left, -rcWindow.top);
3269         TRACE("rect (%s)\n", wine_dbgstr_rect(&rcWindow));
3270         DrawThemeEdge (theme, hdc, 0, 0, &rcWindow, BDR_RAISEDINNER, BF_TOP, NULL);
3271         ReleaseDC( infoPtr->hwndSelf, hdc );
3272     }
3273
3274     return 0;
3275 }
3276
3277
3278 static LRESULT
3279 REBAR_NotifyFormat (REBAR_INFO *infoPtr, LPARAM cmd)
3280 {
3281     INT i;
3282
3283     if (cmd == NF_REQUERY) {
3284         i = SendMessageW(REBAR_GetNotifyParent (infoPtr),
3285                          WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
3286         if ((i != NFR_ANSI) && (i != NFR_UNICODE)) {
3287             ERR("wrong response to WM_NOTIFYFORMAT (%d), assuming ANSI\n", i);
3288             i = NFR_ANSI;
3289         }
3290         infoPtr->bUnicode = (i == NFR_UNICODE) ? 1 : 0;
3291         return (LRESULT)i;
3292     }
3293     return (LRESULT)((infoPtr->bUnicode) ? NFR_UNICODE : NFR_ANSI);
3294 }
3295
3296
3297 static LRESULT
3298 REBAR_Paint (const REBAR_INFO *infoPtr, HDC hdc)
3299 {
3300     if (hdc) {
3301         TRACE("painting\n");
3302         REBAR_Refresh (infoPtr, hdc);
3303     } else {
3304         PAINTSTRUCT ps;
3305         hdc = BeginPaint (infoPtr->hwndSelf, &ps);
3306         TRACE("painting (%s)\n", wine_dbgstr_rect(&ps.rcPaint));
3307         if (ps.fErase) {
3308             /* Erase area of paint if requested */
3309             REBAR_InternalEraseBkGnd (infoPtr, hdc, &ps.rcPaint);
3310         }
3311         REBAR_Refresh (infoPtr, hdc);
3312         EndPaint (infoPtr->hwndSelf, &ps);
3313     }
3314
3315     return 0;
3316 }
3317
3318
3319 static LRESULT
3320 REBAR_SetCursor (const REBAR_INFO *infoPtr, LPARAM lParam)
3321 {
3322     POINT pt;
3323     UINT  flags;
3324
3325     TRACE("code=0x%X  id=0x%X\n", LOWORD(lParam), HIWORD(lParam));
3326
3327     GetCursorPos (&pt);
3328     ScreenToClient (infoPtr->hwndSelf, &pt);
3329
3330     REBAR_InternalHitTest (infoPtr, &pt, &flags, NULL);
3331
3332     if (flags == RBHT_GRABBER) {
3333         if ((infoPtr->dwStyle & CCS_VERT) &&
3334             !(infoPtr->dwStyle & RBS_VERTICALGRIPPER))
3335             SetCursor (infoPtr->hcurVert);
3336         else
3337             SetCursor (infoPtr->hcurHorz);
3338     }
3339     else if (flags != RBHT_CLIENT)
3340         SetCursor (infoPtr->hcurArrow);
3341
3342     return 0;
3343 }
3344
3345
3346 static LRESULT
3347 REBAR_SetFont (REBAR_INFO *infoPtr, HFONT font)
3348 {
3349     REBAR_BAND *lpBand;
3350     UINT i;
3351
3352     infoPtr->hFont = font;
3353
3354     /* revalidate all bands to change sizes of text in headers of bands */
3355     for (i=0; i<infoPtr->uNumBands; i++) {
3356         lpBand = REBAR_GetBand(infoPtr, i);
3357         REBAR_ValidateBand (infoPtr, lpBand);
3358     }
3359
3360     REBAR_Layout(infoPtr);
3361     return 0;
3362 }
3363
3364
3365 /*****************************************************
3366  *
3367  *  Handles the WM_SETREDRAW message.
3368  *
3369  * Documentation:
3370  *  According to testing V4.71 of COMCTL32 returns the
3371  *  *previous* status of the redraw flag (either 0 or -1)
3372  *  instead of the MSDN documented value of 0 if handled
3373  *
3374  *****************************************************/
3375 static inline LRESULT
3376 REBAR_SetRedraw (REBAR_INFO *infoPtr, BOOL redraw)
3377 {
3378     BOOL oldredraw = infoPtr->DoRedraw;
3379
3380     TRACE("set to %s, fStatus=%08x\n",
3381           (redraw) ? "TRUE" : "FALSE", infoPtr->fStatus);
3382     infoPtr->DoRedraw = redraw;
3383     if (redraw) {
3384         if (infoPtr->fStatus & BAND_NEEDS_REDRAW) {
3385             REBAR_MoveChildWindows (infoPtr, 0, infoPtr->uNumBands);
3386             REBAR_ForceResize (infoPtr);
3387             InvalidateRect (infoPtr->hwndSelf, 0, TRUE);
3388         }
3389         infoPtr->fStatus &= ~BAND_NEEDS_REDRAW;
3390     }
3391     return (oldredraw) ? -1 : 0;
3392 }
3393
3394
3395 static LRESULT
3396 REBAR_Size (REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3397 {
3398     TRACE("wParam=%lx, lParam=%lx\n", wParam, lParam);
3399
3400     /* avoid _Layout resize recursion (but it shouldn't be infinite and it seems Windows does recurse) */
3401     if (infoPtr->fStatus & SELF_RESIZE) {
3402         infoPtr->fStatus &= ~SELF_RESIZE;
3403         TRACE("SELF_RESIZE was set, reset, fStatus=%08x lparam=%08lx\n",
3404               infoPtr->fStatus, lParam);
3405         return 0;
3406     }
3407     
3408     if (infoPtr->dwStyle & RBS_AUTOSIZE)
3409         REBAR_AutoSize(infoPtr, TRUE);
3410     else
3411         REBAR_Layout(infoPtr);
3412
3413     return 0;
3414 }
3415
3416
3417 static LRESULT
3418 REBAR_StyleChanged (REBAR_INFO *infoPtr, INT nType, const STYLESTRUCT *lpStyle)
3419 {
3420     TRACE("current style=%08x, styleOld=%08x, style being set to=%08x\n",
3421           infoPtr->dwStyle, lpStyle->styleOld, lpStyle->styleNew);
3422     if (nType == GWL_STYLE)
3423     {
3424         infoPtr->orgStyle = infoPtr->dwStyle = lpStyle->styleNew;
3425         if (GetWindowTheme (infoPtr->hwndSelf))
3426             infoPtr->dwStyle &= ~WS_BORDER;
3427         /* maybe it should be COMMON_STYLES like in toolbar */
3428         if ((lpStyle->styleNew ^ lpStyle->styleOld) & CCS_VERT)
3429             REBAR_Layout(infoPtr);
3430     }
3431     return FALSE;
3432 }
3433
3434 /* update theme after a WM_THEMECHANGED message */
3435 static LRESULT theme_changed (REBAR_INFO* infoPtr)
3436 {
3437     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
3438     CloseThemeData (theme);
3439     theme = OpenThemeData (infoPtr->hwndSelf, themeClass);
3440     /* WS_BORDER disappears when theming is enabled and reappears when
3441      * disabled... */
3442     infoPtr->dwStyle &= ~WS_BORDER;
3443     infoPtr->dwStyle |= theme ? 0 : (infoPtr->orgStyle & WS_BORDER);
3444     return 0;
3445 }
3446
3447 static LRESULT
3448 REBAR_WindowPosChanged (const REBAR_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
3449 {
3450     LRESULT ret;
3451     RECT rc;
3452
3453     ret = DefWindowProcW(infoPtr->hwndSelf, WM_WINDOWPOSCHANGED,
3454                          wParam, lParam);
3455     GetWindowRect(infoPtr->hwndSelf, &rc);
3456     TRACE("hwnd %p new pos (%s)\n", infoPtr->hwndSelf, wine_dbgstr_rect(&rc));
3457     return ret;
3458 }
3459
3460
3461 static LRESULT WINAPI
3462 REBAR_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3463 {
3464     REBAR_INFO *infoPtr = REBAR_GetInfoPtr (hwnd);
3465
3466     TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n",
3467           hwnd, uMsg, wParam, lParam);
3468     if (!infoPtr && (uMsg != WM_NCCREATE))
3469         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
3470     switch (uMsg)
3471     {
3472 /*      case RB_BEGINDRAG: */
3473
3474         case RB_DELETEBAND:
3475             return REBAR_DeleteBand (infoPtr, wParam);
3476
3477 /*      case RB_DRAGMOVE: */
3478 /*      case RB_ENDDRAG: */
3479
3480         case RB_GETBANDBORDERS:
3481             return REBAR_GetBandBorders (infoPtr, wParam, (LPRECT)lParam);
3482
3483         case RB_GETBANDCOUNT:
3484             return REBAR_GetBandCount (infoPtr);
3485
3486         case RB_GETBANDINFO_OLD:
3487         case RB_GETBANDINFOA:
3488         case RB_GETBANDINFOW:
3489             return REBAR_GetBandInfoT(infoPtr, wParam, (LPREBARBANDINFOW)lParam,
3490                                                         uMsg == RB_GETBANDINFOW);
3491         case RB_GETBARHEIGHT:
3492             return REBAR_GetBarHeight (infoPtr);
3493
3494         case RB_GETBARINFO:
3495             return REBAR_GetBarInfo (infoPtr, (LPREBARINFO)lParam);
3496
3497         case RB_GETBKCOLOR:
3498             return REBAR_GetBkColor (infoPtr);
3499
3500 /*      case RB_GETCOLORSCHEME: */
3501 /*      case RB_GETDROPTARGET: */
3502
3503         case RB_GETPALETTE:
3504             return REBAR_GetPalette (infoPtr);
3505
3506         case RB_GETRECT:
3507             return REBAR_GetRect (infoPtr, wParam, (LPRECT)lParam);
3508
3509         case RB_GETROWCOUNT:
3510             return REBAR_GetRowCount (infoPtr);
3511
3512         case RB_GETROWHEIGHT:
3513             return REBAR_GetRowHeight (infoPtr, wParam);
3514
3515         case RB_GETTEXTCOLOR:
3516             return REBAR_GetTextColor (infoPtr);
3517
3518         case RB_GETTOOLTIPS:
3519             return REBAR_GetToolTips (infoPtr);
3520
3521         case RB_GETUNICODEFORMAT:
3522             return REBAR_GetUnicodeFormat (infoPtr);
3523
3524         case CCM_GETVERSION:
3525             return REBAR_GetVersion (infoPtr);
3526
3527         case RB_HITTEST:
3528             return REBAR_HitTest (infoPtr, (LPRBHITTESTINFO)lParam);
3529
3530         case RB_IDTOINDEX:
3531             return REBAR_IdToIndex (infoPtr, wParam);
3532
3533         case RB_INSERTBANDA:
3534         case RB_INSERTBANDW:
3535             return REBAR_InsertBandT(infoPtr, wParam, (LPREBARBANDINFOW)lParam,
3536                                                        uMsg == RB_INSERTBANDW);
3537         case RB_MAXIMIZEBAND:
3538             return REBAR_MaximizeBand (infoPtr, wParam, lParam);
3539
3540         case RB_MINIMIZEBAND:
3541             return REBAR_MinimizeBand (infoPtr, wParam);
3542
3543         case RB_MOVEBAND:
3544             return REBAR_MoveBand (infoPtr, wParam, lParam);
3545
3546         case RB_PUSHCHEVRON:
3547             return REBAR_PushChevron (infoPtr, wParam, lParam);
3548
3549         case RB_SETBANDINFOA:
3550         case RB_SETBANDINFOW:
3551             return REBAR_SetBandInfoT(infoPtr, wParam, (LPREBARBANDINFOW)lParam,
3552                                                         uMsg == RB_SETBANDINFOW);
3553         case RB_SETBARINFO:
3554             return REBAR_SetBarInfo (infoPtr, (LPREBARINFO)lParam);
3555
3556         case RB_SETBKCOLOR:
3557             return REBAR_SetBkColor (infoPtr, lParam);
3558
3559 /*      case RB_SETCOLORSCHEME: */
3560 /*      case RB_SETPALETTE: */
3561 /*          return REBAR_GetPalette (infoPtr, wParam, lParam); */
3562
3563         case RB_SETPARENT:
3564             return REBAR_SetParent (infoPtr, (HWND)wParam);
3565
3566         case RB_SETTEXTCOLOR:
3567             return REBAR_SetTextColor (infoPtr, lParam);
3568
3569 /*      case RB_SETTOOLTIPS: */
3570
3571         case RB_SETUNICODEFORMAT:
3572             return REBAR_SetUnicodeFormat (infoPtr, wParam);
3573
3574         case CCM_SETVERSION:
3575             return REBAR_SetVersion (infoPtr, (INT)wParam);
3576
3577         case RB_SHOWBAND:
3578             return REBAR_ShowBand (infoPtr, wParam, lParam);
3579
3580         case RB_SIZETORECT:
3581             return REBAR_SizeToRect (infoPtr, (LPCRECT)lParam);
3582
3583
3584 /*    Messages passed to parent */
3585         case WM_COMMAND:
3586         case WM_DRAWITEM:
3587         case WM_NOTIFY:
3588             return SendMessageW(REBAR_GetNotifyParent (infoPtr), uMsg, wParam, lParam);
3589
3590
3591 /*      case WM_CHARTOITEM:     supported according to ControlSpy */
3592
3593         case WM_CREATE:
3594             return REBAR_Create (infoPtr, (LPCREATESTRUCTW)lParam);
3595
3596         case WM_DESTROY:
3597             return REBAR_Destroy (infoPtr);
3598
3599         case WM_ERASEBKGND:
3600             return REBAR_EraseBkGnd (infoPtr, (HDC)wParam);
3601
3602         case WM_GETFONT:
3603             return REBAR_GetFont (infoPtr);
3604
3605 /*      case WM_LBUTTONDBLCLK:  supported according to ControlSpy */
3606
3607         case WM_LBUTTONDOWN:
3608             return REBAR_LButtonDown (infoPtr, lParam);
3609
3610         case WM_LBUTTONUP:
3611             return REBAR_LButtonUp (infoPtr);
3612
3613 /*      case WM_MEASUREITEM:    supported according to ControlSpy */
3614
3615         case WM_MOUSEMOVE:
3616             return REBAR_MouseMove (infoPtr, lParam);
3617
3618         case WM_MOUSELEAVE:
3619             return REBAR_MouseLeave (infoPtr);
3620
3621         case WM_NCCALCSIZE:
3622             return REBAR_NCCalcSize (infoPtr, (RECT*)lParam);
3623
3624         case WM_NCCREATE:
3625             return REBAR_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
3626
3627         case WM_NCHITTEST:
3628             return REBAR_NCHitTest (infoPtr, lParam);
3629
3630         case WM_NCPAINT:
3631             return REBAR_NCPaint (infoPtr);
3632
3633         case WM_NOTIFYFORMAT:
3634             return REBAR_NotifyFormat (infoPtr, lParam);
3635
3636         case WM_PRINTCLIENT:
3637         case WM_PAINT:
3638             return REBAR_Paint (infoPtr, (HDC)wParam);
3639
3640 /*      case WM_PALETTECHANGED: supported according to ControlSpy */
3641 /*      case WM_QUERYNEWPALETTE:supported according to ControlSpy */
3642 /*      case WM_RBUTTONDOWN:    supported according to ControlSpy */
3643 /*      case WM_RBUTTONUP:      supported according to ControlSpy */
3644
3645         case WM_SETCURSOR:
3646             return REBAR_SetCursor (infoPtr, lParam);
3647
3648         case WM_SETFONT:
3649             return REBAR_SetFont (infoPtr, (HFONT)wParam);
3650
3651         case WM_SETREDRAW:
3652             return REBAR_SetRedraw (infoPtr, wParam);
3653
3654         case WM_SIZE:
3655             return REBAR_Size (infoPtr, wParam, lParam);
3656
3657         case WM_STYLECHANGED:
3658             return REBAR_StyleChanged (infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3659
3660         case WM_THEMECHANGED:
3661             return theme_changed (infoPtr);
3662
3663         case WM_SYSCOLORCHANGE:
3664             COMCTL32_RefreshSysColors();
3665             return 0;
3666
3667 /*      case WM_VKEYTOITEM:     supported according to ControlSpy */
3668 /*      case WM_WININICHANGE: */
3669
3670         case WM_WINDOWPOSCHANGED:
3671             return REBAR_WindowPosChanged (infoPtr, wParam, lParam);
3672
3673         default:
3674             if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
3675                 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
3676                      uMsg, wParam, lParam);
3677             return DefWindowProcW (hwnd, uMsg, wParam, lParam);
3678     }
3679 }
3680
3681
3682 VOID
3683 REBAR_Register (void)
3684 {
3685     WNDCLASSW wndClass;
3686
3687     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
3688     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
3689     wndClass.lpfnWndProc   = REBAR_WindowProc;
3690     wndClass.cbClsExtra    = 0;
3691     wndClass.cbWndExtra    = sizeof(REBAR_INFO *);
3692     wndClass.hCursor       = 0;
3693     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
3694 #if GLATESTING
3695     wndClass.hbrBackground = CreateSolidBrush(RGB(0,128,0));
3696 #endif
3697     wndClass.lpszClassName = REBARCLASSNAMEW;
3698
3699     RegisterClassW (&wndClass);
3700
3701     mindragx = GetSystemMetrics (SM_CXDRAG);
3702     mindragy = GetSystemMetrics (SM_CYDRAG);
3703
3704 }
3705
3706
3707 VOID
3708 REBAR_Unregister (void)
3709 {
3710     UnregisterClassW (REBARCLASSNAMEW, NULL);
3711 }