]> git.saurik.com Git - wxWidgets.git/blame - src/msw/toolbar.cpp
don't use deprecate SetClippingRegion(wxRegion)
[wxWidgets.git] / src / msw / toolbar.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
936f6353 2// Name: src/msw/toolbar.cpp
8a0681f9 3// Purpose: wxToolBar
2bda0e17
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
1c383dba
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
1c383dba 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
ad9835c9
WS
27#if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && !defined(__SMARTPHONE__)
28
4e3e485b
WS
29#include "wx/toolbar.h"
30
2bda0e17 31#ifndef WX_PRECOMP
57bd4c60 32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
ad9835c9 33 #include "wx/dynarray.h"
8a0681f9 34 #include "wx/frame.h"
1c383dba
VZ
35 #include "wx/log.h"
36 #include "wx/intl.h"
4e15f6c5 37 #include "wx/settings.h"
381dd4bf 38 #include "wx/bitmap.h"
f6bcfd97 39 #include "wx/dcmemory.h"
f538710f 40 #include "wx/control.h"
670f9935 41 #include "wx/app.h" // for GetComCtl32Version
155ecd4c 42 #include "wx/image.h"
0f767c96 43 #include "wx/stattext.h"
2bda0e17
KB
44#endif
45
dfa53609 46#include "wx/sysopt.h"
888dde65 47#include "wx/dcclient.h"
2bda0e17 48
1c383dba 49#include "wx/msw/private.h"
025f7d77 50#include "wx/msw/dc.h"
2bda0e17 51
d679df70
JS
52#if wxUSE_UXTHEME
53#include "wx/msw/uxtheme.h"
54#endif
55
fb7e87ef
VZ
56// this define controls whether the code for button colours remapping (only
57// useful for 16 or 256 colour images) is active at all, it's always turned off
58// for CE where it doesn't compile (and is probably not needed anyhow) and may
59// also be turned off for other systems if you always use 24bpp images and so
60// never need it
61#ifndef __WXWINCE__
62 #define wxREMAP_BUTTON_COLOURS
63#endif // !__WXWINCE__
64
1c383dba
VZ
65// ----------------------------------------------------------------------------
66// constants
67// ----------------------------------------------------------------------------
68
69// these standard constants are not always defined in compilers headers
2bda0e17 70
6a23cbce 71// Styles
bb6290e3 72#ifndef TBSTYLE_FLAT
1c383dba
VZ
73 #define TBSTYLE_LIST 0x1000
74 #define TBSTYLE_FLAT 0x0800
dd177170
RL
75#endif
76
77#ifndef TBSTYLE_TRANSPARENT
1c383dba 78 #define TBSTYLE_TRANSPARENT 0x8000
bb6290e3 79#endif
bb6290e3 80
a3399e6c
VZ
81#ifndef TBSTYLE_TOOLTIPS
82 #define TBSTYLE_TOOLTIPS 0x0100
83#endif
84
6a23cbce
JS
85// Messages
86#ifndef TB_GETSTYLE
1c383dba 87 #define TB_SETSTYLE (WM_USER + 56)
8a0681f9
VZ
88 #define TB_GETSTYLE (WM_USER + 57)
89#endif
90
91#ifndef TB_HITTEST
92 #define TB_HITTEST (WM_USER + 69)
6a23cbce
JS
93#endif
94
308bb56f
VZ
95#ifndef TB_GETMAXSIZE
96 #define TB_GETMAXSIZE (WM_USER + 83)
97#endif
98
1c383dba 99// these values correspond to those used by comctl32.dll
81d66cf3
JS
100#define DEFAULTBITMAPX 16
101#define DEFAULTBITMAPY 15
81d66cf3 102
1c383dba
VZ
103// ----------------------------------------------------------------------------
104// wxWin macros
105// ----------------------------------------------------------------------------
106
2eb10e2a 107IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
2bda0e17 108
066f1b7a 109/*
bfbb0b4c
WS
110 TOOLBAR PROPERTIES
111 tool
112 bitmap
113 bitmap2
114 tooltip
115 longhelp
116 radio (bool)
117 toggle (bool)
118 separator
119 style ( wxNO_BORDER | wxTB_HORIZONTAL)
120 bitmapsize
121 margins
122 packing
123 separation
124
125 dontattachtoframe
066f1b7a
SC
126*/
127
8a0681f9
VZ
128BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
129 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
130 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged)
0090a153 131 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground)
2bda0e17 132END_EVENT_TABLE()
2bda0e17 133
8a0681f9
VZ
134// ----------------------------------------------------------------------------
135// private classes
136// ----------------------------------------------------------------------------
137
138class wxToolBarTool : public wxToolBarToolBase
139{
140public:
141 wxToolBarTool(wxToolBar *tbar,
142 int id,
a3399e6c
VZ
143 const wxString& label,
144 const wxBitmap& bmpNormal,
145 const wxBitmap& bmpDisabled,
146 wxItemKind kind,
8a0681f9 147 wxObject *clientData,
a3399e6c
VZ
148 const wxString& shortHelp,
149 const wxString& longHelp)
150 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
151 clientData, shortHelp, longHelp)
8a0681f9
VZ
152 {
153 m_nSepCount = 0;
cdb11cb9 154 m_staticText = 0;
8a0681f9
VZ
155 }
156
cdb11cb9
VZ
157 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
158 : wxToolBarToolBase(tbar, control, label)
8a0681f9 159 {
cdb11cb9
VZ
160 if ( IsControl() && !m_label.empty() )
161 {
162 // create a control to render the control's label
163 m_staticText = new wxStaticText
164 (
165 m_tbar,
166 wxID_ANY,
167 m_label,
168 wxDefaultPosition,
169 wxDefaultSize,
170 wxALIGN_CENTRE | wxST_NO_AUTORESIZE
171 );
172 }
173 else // no label
174 {
175 m_staticText = NULL;
176 }
177
8a0681f9
VZ
178 m_nSepCount = 1;
179 }
180
cdb11cb9
VZ
181 virtual ~wxToolBarTool()
182 {
183 delete m_staticText;
184 }
185
c631abda
VZ
186 virtual void SetLabel(const wxString& label)
187 {
188 if ( label == m_label )
189 return;
190
191 wxToolBarToolBase::SetLabel(label);
192
cdb11cb9
VZ
193 if ( m_staticText )
194 m_staticText->SetLabel(label);
195
c631abda
VZ
196 // we need to update the label shown in the toolbar because it has a
197 // pointer to the internal buffer of the old label
198 //
199 // TODO: use TB_SETBUTTONINFO
200 }
201
cdb11cb9
VZ
202 wxStaticText* GetStaticText()
203 {
204 wxASSERT_MSG( IsControl(),
205 _T("only makes sense for embedded control tools") );
206
207 return m_staticText;
208 }
209
8a0681f9
VZ
210 // set/get the number of separators which we use to cover the space used by
211 // a control in the toolbar
212 void SetSeparatorsCount(size_t count) { m_nSepCount = count; }
213 size_t GetSeparatorsCount() const { return m_nSepCount; }
214
215private:
216 size_t m_nSepCount;
cdb11cb9 217 wxStaticText *m_staticText;
2eb10e2a
VZ
218
219 DECLARE_NO_COPY_CLASS(wxToolBarTool)
8a0681f9
VZ
220};
221
3c30bbb9
VZ
222// ----------------------------------------------------------------------------
223// helper functions
224// ----------------------------------------------------------------------------
225
226// return the rectangle of the item at the given index
227//
228// returns an empty (0, 0, 0, 0) rectangle if fails so the caller may compare
229// r.right or r.bottom with 0 to check for this
230static RECT wxGetTBItemRect(HWND hwnd, int index)
231{
232 RECT r;
233
234 // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter
235 // only appeared in v4.70 of comctl32.dll
236 if ( !::SendMessage(hwnd, TB_GETITEMRECT, index, (LPARAM)&r) )
237 {
238 wxLogLastError(wxT("TB_GETITEMRECT"));
239
240 r.top =
241 r.left =
242 r.right =
243 r.bottom = 0;
244 }
245
246 return r;
247}
248
1c383dba
VZ
249// ============================================================================
250// implementation
251// ============================================================================
2bda0e17 252
1c383dba 253// ----------------------------------------------------------------------------
8a0681f9 254// wxToolBarTool
1c383dba
VZ
255// ----------------------------------------------------------------------------
256
8a0681f9 257wxToolBarToolBase *wxToolBar::CreateTool(int id,
a3399e6c
VZ
258 const wxString& label,
259 const wxBitmap& bmpNormal,
260 const wxBitmap& bmpDisabled,
261 wxItemKind kind,
8a0681f9 262 wxObject *clientData,
a3399e6c
VZ
263 const wxString& shortHelp,
264 const wxString& longHelp)
8a0681f9 265{
a3399e6c
VZ
266 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
267 clientData, shortHelp, longHelp);
8a0681f9
VZ
268}
269
cdb11cb9
VZ
270wxToolBarToolBase *
271wxToolBar::CreateTool(wxControl *control, const wxString& label)
8a0681f9 272{
cdb11cb9 273 return new wxToolBarTool(this, control, label);
8a0681f9
VZ
274}
275
276// ----------------------------------------------------------------------------
277// wxToolBar construction
278// ----------------------------------------------------------------------------
279
280void wxToolBar::Init()
2bda0e17 281{
1c383dba 282 m_hBitmap = 0;
8d0a7b56 283 m_disabledImgList = NULL;
8a0681f9
VZ
284
285 m_nButtons = 0;
286
1c383dba
VZ
287 m_defaultWidth = DEFAULTBITMAPX;
288 m_defaultHeight = DEFAULTBITMAPY;
37d0bdff 289
6ae2a4b7 290 m_pInTool = NULL;
2bda0e17
KB
291}
292
8a0681f9
VZ
293bool wxToolBar::Create(wxWindow *parent,
294 wxWindowID id,
295 const wxPoint& pos,
296 const wxSize& size,
297 long style,
298 const wxString& name)
2bda0e17 299{
1c383dba 300 // common initialisation
11b6a93b 301 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
bfbb0b4c 302 return false;
89b892a2 303
d408730c
VZ
304 FixupStyle();
305
24998710 306 // MSW-specific initialisation
278c927d 307 if ( !MSWCreateToolbar(pos, size) )
bfbb0b4c 308 return false;
b0766406 309
9a63feff
VZ
310 wxSetCCUnicodeFormat(GetHwnd());
311
f6becba5 312 // workaround for flat toolbar on Windows XP classic style: we have to set
a8cb1a49 313 // the style after creating the control; doing it at creation time doesn't work
d679df70 314#if wxUSE_UXTHEME
6d473712
JS
315 if ( style & wxTB_FLAT )
316 {
2588b9c4 317 LRESULT style = GetMSWToolbarStyle();
bfbb0b4c 318
f6becba5 319 if ( !(style & TBSTYLE_FLAT) )
f6becba5 320 ::SendMessage(GetHwnd(), TB_SETSTYLE, 0, style | TBSTYLE_FLAT);
6d473712 321 }
f6becba5 322#endif // wxUSE_UXTHEME
bfbb0b4c
WS
323
324 return true;
24998710 325}
2bda0e17 326
278c927d 327bool wxToolBar::MSWCreateToolbar(const wxPoint& pos, const wxSize& size)
24998710 328{
fda7962d 329 if ( !MSWCreateControl(TOOLBARCLASSNAME, wxEmptyString, pos, size) )
bfbb0b4c 330 return false;
2bda0e17 331
c8f1f088 332 // toolbar-specific post initialisation
1c383dba 333 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
bfbb0b4c 334
d6f2a891 335#ifdef TB_SETEXTENDEDSTYLE
a9a0ceca
VZ
336 if ( wxApp::GetComCtl32Version() >= 471 )
337 ::SendMessage(GetHwnd(), TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS);
d6f2a891 338#endif
a9a0ceca 339
bfbb0b4c 340 return true;
24998710 341}
c8f1f088 342
24998710
VZ
343void wxToolBar::Recreate()
344{
345 const HWND hwndOld = GetHwnd();
346 if ( !hwndOld )
347 {
348 // we haven't been created yet, no need to recreate
349 return;
350 }
2bda0e17 351
24998710
VZ
352 // get the position and size before unsubclassing the old toolbar
353 const wxPoint pos = GetPosition();
354 const wxSize size = GetSize();
bb6290e3 355
24998710 356 UnsubclassWin();
2bda0e17 357
278c927d 358 if ( !MSWCreateToolbar(pos, size) )
24998710
VZ
359 {
360 // what can we do?
361 wxFAIL_MSG( _T("recreating the toolbar failed") );
362
363 return;
364 }
365
366 // reparent all our children under the new toolbar
222ed1d6 367 for ( wxWindowList::compatibility_iterator node = m_children.GetFirst();
24998710
VZ
368 node;
369 node = node->GetNext() )
370 {
371 wxWindow *win = node->GetData();
372 if ( !win->IsTopLevel() )
373 ::SetParent(GetHwndOf(win), GetHwnd());
374 }
375
a8cb1a49
DS
376 // only destroy the old toolbar now --
377 // after all the children had been reparented
24998710
VZ
378 ::DestroyWindow(hwndOld);
379
380 // it is for the old bitmap control and can't be used with the new one
381 if ( m_hBitmap )
382 {
383 ::DeleteObject((HBITMAP) m_hBitmap);
384 m_hBitmap = 0;
385 }
386
8d0a7b56
VZ
387 if ( m_disabledImgList )
388 {
389 delete m_disabledImgList;
390 m_disabledImgList = NULL;
391 }
392
24998710 393 Realize();
2bda0e17
KB
394}
395
8a0681f9 396wxToolBar::~wxToolBar()
2bda0e17 397{
f6bcfd97
BP
398 // we must refresh the frame size when the toolbar is deleted but the frame
399 // is not - otherwise toolbar leaves a hole in the place it used to occupy
f6bcfd97 400 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
c2fd78b1 401 if ( frame && !frame->IsBeingDeleted() )
f6bcfd97 402 frame->SendSizeEvent();
f6bcfd97
BP
403
404 if ( m_hBitmap )
1c383dba 405 ::DeleteObject((HBITMAP) m_hBitmap);
8d0a7b56
VZ
406
407 delete m_disabledImgList;
1c383dba
VZ
408}
409
24998710
VZ
410wxSize wxToolBar::DoGetBestSize() const
411{
308bb56f
VZ
412 wxSize sizeBest;
413
414 SIZE size;
415 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE, 0, (LPARAM)&size) )
416 {
417 // maybe an old (< 0x400) Windows version? try to approximate the
418 // toolbar size ourselves
419 sizeBest = GetToolSize();
4f6b4292 420 sizeBest.y += 2 * ::GetSystemMetrics(SM_CYBORDER); // Add borders
308bb56f
VZ
421 sizeBest.x *= GetToolsCount();
422
423 // reverse horz and vertical components if necessary
7a976304 424 if ( IsVertical() )
308bb56f
VZ
425 {
426 int t = sizeBest.x;
427 sizeBest.x = sizeBest.y;
428 sizeBest.y = t;
429 }
430 }
3c30bbb9 431 else // TB_GETMAXSIZE succeeded
308bb56f 432 {
3c30bbb9
VZ
433 // but it could still return an incorrect result due to what appears to
434 // be a bug in old comctl32.dll versions which don't handle controls in
435 // the toolbar correctly, so work around it (see SF patch 1902358)
436 if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 )
437 {
438 // calculate the toolbar width in alternative way
439 const RECT rcFirst = wxGetTBItemRect(GetHwnd(), 0);
440 const RECT rcLast = wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1);
441
442 const int widthAlt = rcLast.right - rcFirst.left;
443 if ( widthAlt > size.cx )
444 size.cx = widthAlt;
445 }
446
308bb56f
VZ
447 sizeBest.x = size.cx;
448 sizeBest.y = size.cy;
449 }
24998710 450
3c30bbb9 451 if ( !IsVertical() )
c118d8b0
JS
452 {
453 // Without the extra height, DoGetBestSize can report a size that's
454 // smaller than the actual window, causing windows to overlap slightly
455 // in some circumstances, leading to missing borders (especially noticeable
456 // in AUI layouts).
457 if (!(GetWindowStyle() & wxTB_NODIVIDER))
458 sizeBest.y += 2;
459 sizeBest.y ++;
a14337d6 460 }
378c9833 461
31582e4e 462 CacheBestSize(sizeBest);
97071cdb 463
308bb56f 464 return sizeBest;
24998710
VZ
465}
466
467WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const
468{
469 // toolbars never have border, giving one to them results in broken
470 // appearance
471 WXDWORD msStyle = wxControl::MSWGetStyle
472 (
473 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
474 );
475
6a1b3ead
VZ
476 if ( !(style & wxTB_NO_TOOLTIPS) )
477 msStyle |= TBSTYLE_TOOLTIPS;
24998710 478
f9dae779 479 if ( style & (wxTB_FLAT | wxTB_HORZ_LAYOUT) )
24998710
VZ
480 {
481 // static as it doesn't change during the program lifetime
60decce5 482 static const int s_verComCtl = wxApp::GetComCtl32Version();
24998710
VZ
483
484 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
485 // style with 6.00 (part of Windows XP) leads to the toolbar with
486 // incorrect background colour - and not using it still results in the
487 // correct (flat) toolbar, so don't use it there
488 if ( s_verComCtl > 400 && s_verComCtl < 600 )
24998710 489 msStyle |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT;
f9dae779
VZ
490
491 if ( s_verComCtl >= 470 && style & wxTB_HORZ_LAYOUT )
f9dae779 492 msStyle |= TBSTYLE_LIST;
24998710
VZ
493 }
494
495 if ( style & wxTB_NODIVIDER )
496 msStyle |= CCS_NODIVIDER;
497
498 if ( style & wxTB_NOALIGN )
499 msStyle |= CCS_NOPARENTALIGN;
500
3bb63e5c
JS
501 if ( style & wxTB_VERTICAL )
502 msStyle |= CCS_VERT;
503
5b2acc3a
RR
504 if( style & wxTB_BOTTOM )
505 msStyle |= CCS_BOTTOM;
506
7a976304
VZ
507 if ( style & wxTB_RIGHT )
508 msStyle |= CCS_RIGHT;
509
24998710
VZ
510 return msStyle;
511}
512
bdc72a22 513// ----------------------------------------------------------------------------
8a0681f9 514// adding/removing tools
bdc72a22
VZ
515// ----------------------------------------------------------------------------
516
9ccffa51
VZ
517bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
518 wxToolBarToolBase * WXUNUSED(tool))
1c383dba 519{
8a0681f9
VZ
520 // nothing special to do here - we really create the toolbar buttons in
521 // Realize() later
9f884528 522 InvalidateBestSize();
bfbb0b4c 523 return true;
1c383dba
VZ
524}
525
8a0681f9 526bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
bdc72a22 527{
f6bcfd97
BP
528 // the main difficulty we have here is with the controls in the toolbars:
529 // as we (sometimes) use several separators to cover up the space used by
530 // them, the indices are not the same for us and the toolbar
531
532 // first determine the position of the first button to delete: it may be
533 // different from pos if we use several separators to cover the space used
534 // by a control
222ed1d6 535 wxToolBarToolsList::compatibility_iterator node;
f6bcfd97
BP
536 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
537 {
538 wxToolBarToolBase *tool2 = node->GetData();
539 if ( tool2 == tool )
540 {
541 // let node point to the next node in the list
542 node = node->GetNext();
543
544 break;
545 }
546
547 if ( tool2->IsControl() )
56bd6aac 548 pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1;
f6bcfd97
BP
549 }
550
551 // now determine the number of buttons to delete and the area taken by them
8a0681f9 552 size_t nButtonsToDelete = 1;
bdc72a22 553
8a0681f9 554 // get the size of the button we're going to delete
3c30bbb9 555 const RECT r = wxGetTBItemRect(GetHwnd(), pos);
bdc72a22 556
8a0681f9 557 int width = r.right - r.left;
bdc72a22 558
8a0681f9
VZ
559 if ( tool->IsControl() )
560 {
561 nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount();
8a0681f9
VZ
562 width *= nButtonsToDelete;
563 }
bdc72a22 564
f6bcfd97
BP
565 // do delete all buttons
566 m_nButtons -= nButtonsToDelete;
8a0681f9
VZ
567 while ( nButtonsToDelete-- > 0 )
568 {
569 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) )
570 {
f6bcfd97 571 wxLogLastError(wxT("TB_DELETEBUTTON"));
1c383dba 572
bfbb0b4c 573 return false;
8a0681f9
VZ
574 }
575 }
1c383dba 576
f6bcfd97
BP
577 // and finally reposition all the controls after this button (the toolbar
578 // takes care of all normal items)
579 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
8a0681f9 580 {
cdb11cb9 581 wxToolBarTool *tool2 = (wxToolBarTool*)node->GetData();
8a0681f9
VZ
582 if ( tool2->IsControl() )
583 {
345d60d3
VZ
584 wxControl * const control = tool2->GetControl();
585
8a0681f9 586 int x;
8a0681f9 587 control->GetPosition(&x, NULL);
bfbb0b4c 588 control->Move(x - width, wxDefaultCoord);
cdb11cb9 589
345d60d3
VZ
590 wxStaticText * const staticText = tool2->GetStaticText();
591 if ( staticText )
592 staticText->Move(x - width, wxDefaultCoord);
8a0681f9
VZ
593 }
594 }
1c383dba 595
9f884528 596 InvalidateBestSize();
97071cdb 597
bfbb0b4c 598 return true;
1c383dba
VZ
599}
600
8d0a7b56
VZ
601void wxToolBar::CreateDisabledImageList()
602{
2c622593
DS
603 if (m_disabledImgList != NULL)
604 {
605 delete m_disabledImgList;
606 m_disabledImgList = NULL;
607 }
608
8d0a7b56
VZ
609 // as we can't use disabled image list with older versions of comctl32.dll,
610 // don't even bother creating it
5625d0d4 611 if ( wxApp::GetComCtl32Version() >= 470 )
8d0a7b56
VZ
612 {
613 // search for the first disabled button img in the toolbar, if any
614 for ( wxToolBarToolsList::compatibility_iterator
615 node = m_tools.GetFirst(); node; node = node->GetNext() )
616 {
617 wxToolBarToolBase *tool = node->GetData();
618 wxBitmap bmpDisabled = tool->GetDisabledBitmap();
619 if ( bmpDisabled.Ok() )
620 {
621 m_disabledImgList = new wxImageList
622 (
623 m_defaultWidth,
624 m_defaultHeight,
625 bmpDisabled.GetMask() != NULL,
626 GetToolsCount()
627 );
2c622593 628 break;
8d0a7b56
VZ
629 }
630 }
631
632 // we don't have any disabled bitmaps
633 }
8d0a7b56
VZ
634}
635
8a0681f9 636bool wxToolBar::Realize()
1c383dba 637{
24998710 638 const size_t nTools = GetToolsCount();
8a0681f9 639 if ( nTools == 0 )
8a0681f9 640 // nothing to do
bfbb0b4c 641 return true;
1c383dba 642
fb7e87ef
VZ
643#ifdef wxREMAP_BUTTON_COLOURS
644 // don't change the values of these constants, they can be set from the
645 // user code via wxSystemOptions
646 enum
92661f97 647 {
fb7e87ef
VZ
648 Remap_None = -1,
649 Remap_Bg,
650 Remap_Buttons,
651 Remap_TransparentBg
652 };
653
654 // the user-specified option overrides anything, but if it wasn't set, only
655 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
656 // much worse after remapping
657 static const wxChar *remapOption = wxT("msw.remap");
658 const int remapValue = wxSystemOptions::HasOption(remapOption)
659 ? wxSystemOptions::GetOptionInt(remapOption)
660 : wxDisplayDepth() <= 8 ? Remap_Buttons
661 : Remap_None;
662
663#endif // wxREMAP_BUTTON_COLOURS
92661f97 664
2b5f62a0
VZ
665 // delete all old buttons, if any
666 for ( size_t pos = 0; pos < m_nButtons; pos++ )
667 {
668 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
669 {
670 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
671 }
672 }
673
8a0681f9
VZ
674 // First, add the bitmap: we use one bitmap for all toolbar buttons
675 // ----------------------------------------------------------------
2bda0e17 676
222ed1d6 677 wxToolBarToolsList::compatibility_iterator node;
24998710
VZ
678 int bitmapId = 0;
679
680 wxSize sizeBmp;
681 if ( HasFlag(wxTB_NOICONS) )
682 {
683 // no icons, don't leave space for them
684 sizeBmp.x =
685 sizeBmp.y = 0;
686 }
687 else // do show icons
688 {
689 // if we already have a bitmap, we'll replace the existing one --
690 // otherwise we'll install a new one
691 HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap;
692
693 sizeBmp.x = m_defaultWidth;
694 sizeBmp.y = m_defaultHeight;
89b892a2 695
4a10ea8b
MW
696 const wxCoord totalBitmapWidth = m_defaultWidth *
697 wx_truncate_cast(wxCoord, nTools),
24998710 698 totalBitmapHeight = m_defaultHeight;
2bda0e17 699
17cfea01 700 // Create a bitmap and copy all the tool bitmaps into it
24998710
VZ
701 wxMemoryDC dcAllButtons;
702 wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight);
703 dcAllButtons.SelectObject(bitmap);
bb86baea 704
fb7e87ef
VZ
705#ifdef wxREMAP_BUTTON_COLOURS
706 if ( remapValue != Remap_TransparentBg )
707#endif // wxREMAP_BUTTON_COLOURS
708 {
709 // VZ: why do we hardcode grey colour for CE?
710 dcAllButtons.SetBackground(wxBrush(
711#ifdef __WXWINCE__
712 wxColour(0xc0, 0xc0, 0xc0)
713#else // !__WXWINCE__
714 GetBackgroundColour()
715#endif // __WXWINCE__/!__WXWINCE__
716 ));
717 dcAllButtons.Clear();
718 }
24998710
VZ
719
720 m_hBitmap = bitmap.GetHBITMAP();
721 HBITMAP hBitmap = (HBITMAP)m_hBitmap;
2bda0e17 722
fb7e87ef
VZ
723#ifdef wxREMAP_BUTTON_COLOURS
724 if ( remapValue == Remap_Bg )
dfa53609 725 {
dfa53609 726 dcAllButtons.SelectObject(wxNullBitmap);
dfa53609
JS
727
728 // Even if we're not remapping the bitmap
729 // content, we still have to remap the background.
730 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
731 totalBitmapWidth, totalBitmapHeight);
732
dfa53609 733 dcAllButtons.SelectObject(bitmap);
dfa53609 734 }
fb7e87ef 735#endif // wxREMAP_BUTTON_COLOURS
dfa53609 736
24998710
VZ
737 // the button position
738 wxCoord x = 0;
2bda0e17 739
24998710
VZ
740 // the number of buttons (not separators)
741 int nButtons = 0;
1c383dba 742
8d0a7b56 743 CreateDisabledImageList();
4769a562 744 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1c383dba 745 {
24998710
VZ
746 wxToolBarToolBase *tool = node->GetData();
747 if ( tool->IsButton() )
1c383dba 748 {
24998710 749 const wxBitmap& bmp = tool->GetNormalBitmap();
ba700b5c
VZ
750
751 const int w = bmp.GetWidth();
752 const int h = bmp.GetHeight();
753
24998710
VZ
754 if ( bmp.Ok() )
755 {
ba700b5c
VZ
756 int xOffset = wxMax(0, (m_defaultWidth - w)/2);
757 int yOffset = wxMax(0, (m_defaultHeight - h)/2);
758
24998710 759 // notice the last parameter: do use mask
ba700b5c 760 dcAllButtons.DrawBitmap(bmp, x + xOffset, yOffset, true);
24998710
VZ
761 }
762 else
1c383dba 763 {
24998710 764 wxFAIL_MSG( _T("invalid tool button bitmap") );
1c383dba
VZ
765 }
766
8d0a7b56
VZ
767 // also deal with disabled bitmap if we want to use them
768 if ( m_disabledImgList )
769 {
770 wxBitmap bmpDisabled = tool->GetDisabledBitmap();
64c288fa 771#if wxUSE_IMAGE && wxUSE_WXDIB
8d0a7b56
VZ
772 if ( !bmpDisabled.Ok() )
773 {
8d0a7b56
VZ
774 // no disabled bitmap specified but we still need to
775 // fill the space in the image list with something, so
776 // we grey out the normal bitmap
9ac43913
VZ
777 wxImage
778 imgGreyed = bmp.ConvertToImage().ConvertToGreyscale();
ba700b5c 779
fb7e87ef
VZ
780#ifdef wxREMAP_BUTTON_COLOURS
781 if ( remapValue == Remap_Buttons )
ba700b5c 782 {
0f99ce3d
DS
783 // we need to have light grey background colour for
784 // MapBitmap() to work correctly
785 for ( int y = 0; y < h; y++ )
ba700b5c 786 {
0f99ce3d
DS
787 for ( int x = 0; x < w; x++ )
788 {
789 if ( imgGreyed.IsTransparent(x, y) )
790 imgGreyed.SetRGB(x, y,
791 wxLIGHT_GREY->Red(),
792 wxLIGHT_GREY->Green(),
793 wxLIGHT_GREY->Blue());
794 }
ba700b5c
VZ
795 }
796 }
fb7e87ef 797#endif // wxREMAP_BUTTON_COLOURS
8d0a7b56
VZ
798
799 bmpDisabled = wxBitmap(imgGreyed);
8d0a7b56 800 }
ba700b5c
VZ
801#endif // wxUSE_IMAGE
802
fb7e87ef
VZ
803#ifdef wxREMAP_BUTTON_COLOURS
804 if ( remapValue == Remap_Buttons )
0f99ce3d 805 MapBitmap(bmpDisabled.GetHBITMAP(), w, h);
fb7e87ef 806#endif // wxREMAP_BUTTON_COLOURS
8d0a7b56
VZ
807
808 m_disabledImgList->Add(bmpDisabled);
809 }
810
24998710
VZ
811 // still inc width and number of buttons because otherwise the
812 // subsequent buttons will all be shifted which is rather confusing
813 // (and like this you'd see immediately which bitmap was bad)
814 x += m_defaultWidth;
815 nButtons++;
1c383dba
VZ
816 }
817 }
8a0681f9 818
24998710 819 dcAllButtons.SelectObject(wxNullBitmap);
f6bcfd97 820
24998710
VZ
821 // don't delete this HBITMAP!
822 bitmap.SetHBITMAP(0);
2bda0e17 823
fb7e87ef
VZ
824#ifdef wxREMAP_BUTTON_COLOURS
825 if ( remapValue == Remap_Buttons )
dfa53609
JS
826 {
827 // Map to system colours
828 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
fb7e87ef 829 totalBitmapWidth, totalBitmapHeight);
dfa53609 830 }
fb7e87ef 831#endif // wxREMAP_BUTTON_COLOURS
1c383dba 832
bfbb0b4c 833 bool addBitmap = true;
9f83044f 834
24998710 835 if ( oldToolBarBitmap )
1c383dba 836 {
24998710 837#ifdef TB_REPLACEBITMAP
2a1f999f 838 if ( wxApp::GetComCtl32Version() >= 400 )
9f83044f 839 {
24998710
VZ
840 TBREPLACEBITMAP replaceBitmap;
841 replaceBitmap.hInstOld = NULL;
842 replaceBitmap.hInstNew = NULL;
dca0f651
VZ
843 replaceBitmap.nIDOld = (UINT_PTR)oldToolBarBitmap;
844 replaceBitmap.nIDNew = (UINT_PTR)hBitmap;
24998710
VZ
845 replaceBitmap.nButtons = nButtons;
846 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
847 0, (LPARAM) &replaceBitmap) )
848 {
849 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
850 }
9f83044f 851
24998710 852 ::DeleteObject(oldToolBarBitmap);
9f83044f 853
24998710 854 // already done
bfbb0b4c 855 addBitmap = false;
24998710
VZ
856 }
857 else
9f83044f 858#endif // TB_REPLACEBITMAP
24998710
VZ
859 {
860 // we can't replace the old bitmap, so we will add another one
861 // (awfully inefficient, but what else to do?) and shift the bitmap
862 // indices accordingly
bfbb0b4c 863 addBitmap = true;
1c383dba 864
24998710
VZ
865 bitmapId = m_nButtons;
866 }
9f83044f 867 }
1c383dba 868
24998710 869 if ( addBitmap ) // no old bitmap or we can't replace it
1c383dba 870 {
24998710
VZ
871 TBADDBITMAP addBitmap;
872 addBitmap.hInst = 0;
dca0f651 873 addBitmap.nID = (UINT_PTR)hBitmap;
24998710
VZ
874 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
875 (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 )
8a0681f9 876 {
24998710 877 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
8a0681f9 878 }
1c383dba 879 }
8d0a7b56 880
24bc71f4
VZ
881 // disable image lists are only supported in comctl32.dll 4.70+
882 if ( wxApp::GetComCtl32Version() >= 470 )
8d0a7b56 883 {
24bc71f4
VZ
884 HIMAGELIST hil = m_disabledImgList
885 ? GetHimagelistOf(m_disabledImgList)
886 : 0;
887
888 // notice that we set the image list even if don't have one right
889 // now as we could have it before and need to reset it in this case
8d0a7b56 890 HIMAGELIST oldImageList = (HIMAGELIST)
24bc71f4 891 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST, 0, (LPARAM)hil);
8d0a7b56
VZ
892
893 // delete previous image list if any
894 if ( oldImageList )
24bc71f4 895 ::DeleteObject(oldImageList);
8d0a7b56 896 }
2bda0e17 897 }
9f83044f 898
24998710
VZ
899 // don't call SetToolBitmapSize() as we don't want to change the values of
900 // m_defaultWidth/Height
901 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0,
902 MAKELONG(sizeBmp.x, sizeBmp.y)) )
051205e6 903 {
24998710 904 wxLogLastError(_T("TB_SETBITMAPSIZE"));
051205e6 905 }
2bda0e17 906
8a0681f9
VZ
907 // Next add the buttons and separators
908 // -----------------------------------
909
1c383dba 910 TBBUTTON *buttons = new TBBUTTON[nTools];
2bda0e17 911
8a0681f9 912 // this array will hold the indices of all controls in the toolbar
1c383dba
VZ
913 wxArrayInt controlIds;
914
bfbb0b4c 915 bool lastWasRadio = false;
1c383dba 916 int i = 0;
4769a562 917 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
2bda0e17 918 {
8a0681f9
VZ
919 wxToolBarToolBase *tool = node->GetData();
920
28603488
VZ
921 // don't add separators to the vertical toolbar with old comctl32.dll
922 // versions as they didn't handle this properly
7a976304 923 if ( IsVertical() && tool->IsSeparator() &&
2a1f999f 924 wxApp::GetComCtl32Version() <= 472 )
28603488
VZ
925 {
926 continue;
927 }
928
1c383dba
VZ
929 TBBUTTON& button = buttons[i];
930
931 wxZeroMemory(button);
932
bfbb0b4c 933 bool isRadio = false;
8a0681f9 934 switch ( tool->GetStyle() )
1c383dba
VZ
935 {
936 case wxTOOL_STYLE_CONTROL:
8a0681f9 937 button.idCommand = tool->GetId();
1c383dba
VZ
938 // fall through: create just a separator too
939
940 case wxTOOL_STYLE_SEPARATOR:
941 button.fsState = TBSTATE_ENABLED;
942 button.fsStyle = TBSTYLE_SEP;
943 break;
944
945 case wxTOOL_STYLE_BUTTON:
24998710
VZ
946 if ( !HasFlag(wxTB_NOICONS) )
947 button.iBitmap = bitmapId;
c631abda 948
24998710 949 if ( HasFlag(wxTB_TEXT) )
c631abda 950 {
24998710
VZ
951 const wxString& label = tool->GetLabel();
952 if ( !label.empty() )
dca0f651 953 button.iString = (INT_PTR)label.wx_str();
c631abda
VZ
954 }
955
8a0681f9 956 button.idCommand = tool->GetId();
1c383dba 957
8a0681f9 958 if ( tool->IsEnabled() )
1c383dba 959 button.fsState |= TBSTATE_ENABLED;
8a0681f9 960 if ( tool->IsToggled() )
1c383dba 961 button.fsState |= TBSTATE_CHECKED;
8a0681f9 962
c631abda
VZ
963 switch ( tool->GetKind() )
964 {
965 case wxITEM_RADIO:
966 button.fsStyle = TBSTYLE_CHECKGROUP;
967
968 if ( !lastWasRadio )
969 {
970 // the first item in the radio group is checked by
971 // default to be consistent with wxGTK and the menu
972 // radio items
973 button.fsState |= TBSTATE_CHECKED;
f3ba93c1 974
730d3366
RD
975 if (tool->Toggle(true))
976 {
977 DoToggleTool(tool, true);
978 }
979 }
0b62d387 980 else if ( tool->IsToggled() )
730d3366
RD
981 {
982 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
983 int prevIndex = i - 1;
984 while ( nodePrev )
985 {
986 TBBUTTON& prevButton = buttons[prevIndex];
987 wxToolBarToolBase *tool = nodePrev->GetData();
988 if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
989 break;
ed7fdb86 990
730d3366 991 if ( tool->Toggle(false) )
730d3366 992 DoToggleTool(tool, false);
a8cb1a49 993
a2dd2ea7 994 prevButton.fsState &= ~TBSTATE_CHECKED;
730d3366
RD
995 nodePrev = nodePrev->GetPrevious();
996 prevIndex--;
ed7fdb86 997 }
c631abda
VZ
998 }
999
bfbb0b4c 1000 isRadio = true;
c631abda
VZ
1001 break;
1002
1003 case wxITEM_CHECK:
1004 button.fsStyle = TBSTYLE_CHECK;
1005 break;
1006
97071cdb
DS
1007 case wxITEM_NORMAL:
1008 button.fsStyle = TBSTYLE_BUTTON;
1009 break;
1010
a9a0ceca
VZ
1011 case wxITEM_DROPDOWN:
1012 button.fsStyle = TBSTYLE_DROPDOWN;
1013 break;
1014
c631abda
VZ
1015 default:
1016 wxFAIL_MSG( _T("unexpected toolbar button kind") );
c631abda 1017 button.fsStyle = TBSTYLE_BUTTON;
97071cdb 1018 break;
c631abda 1019 }
1c383dba
VZ
1020
1021 bitmapId++;
1022 break;
1023 }
2bda0e17 1024
c631abda
VZ
1025 lastWasRadio = isRadio;
1026
1c383dba 1027 i++;
2bda0e17 1028 }
1c383dba 1029
a3399e6c 1030 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)buttons) )
2bda0e17 1031 {
f6bcfd97 1032 wxLogLastError(wxT("TB_ADDBUTTONS"));
2bda0e17 1033 }
89b892a2 1034
1c383dba 1035 delete [] buttons;
2bda0e17 1036
8a0681f9
VZ
1037 // Deal with the controls finally
1038 // ------------------------------
1039
1c383dba 1040 // adjust the controls size to fit nicely in the toolbar
34e5028f 1041 int y = 0;
8a0681f9
VZ
1042 size_t index = 0;
1043 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ )
1c383dba 1044 {
cdb11cb9 1045 wxToolBarTool *tool = (wxToolBarTool*)node->GetData();
1c383dba 1046
34e5028f
VZ
1047 // we calculate the running y coord for vertical toolbars so we need to
1048 // get the items size for all items but for the horizontal ones we
1049 // don't need to deal with the non controls
1050 bool isControl = tool->IsControl();
7a976304 1051 if ( !isControl && !IsVertical() )
34e5028f 1052 continue;
bdc72a22 1053
3c30bbb9 1054 const RECT r = wxGetTBItemRect(GetHwnd(), index);
34e5028f
VZ
1055 if ( !isControl )
1056 {
1057 // can only be control if isVertical
1058 y += r.bottom - r.top;
1059
1060 continue;
1061 }
1062
1063 wxControl *control = tool->GetControl();
cdb11cb9
VZ
1064 wxStaticText * const staticText = tool->GetStaticText();
1065
34e5028f 1066 wxSize size = control->GetSize();
cdb11cb9
VZ
1067 wxSize staticTextSize;
1068 if ( staticText )
1069 {
1070 staticTextSize = staticText->GetSize();
1071 staticTextSize.y += 3; // margin between control and its label
1072 }
34e5028f
VZ
1073
1074 // the position of the leftmost controls corner
bfbb0b4c 1075 int left = wxDefaultCoord;
34e5028f 1076
bdc72a22 1077 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
5b59df83 1078#ifdef TB_SETBUTTONINFO
34e5028f
VZ
1079 // available in headers, now check whether it is available now
1080 // (during run-time)
2a1f999f 1081 if ( wxApp::GetComCtl32Version() >= 471 )
34e5028f
VZ
1082 {
1083 // set the (underlying) separators width to be that of the
1084 // control
1085 TBBUTTONINFO tbbi;
1086 tbbi.cbSize = sizeof(tbbi);
1087 tbbi.dwMask = TBIF_SIZE;
5c519b6c 1088 tbbi.cx = (WORD)size.x;
bfbb0b4c
WS
1089 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO,
1090 tool->GetId(), (LPARAM)&tbbi) )
bdc72a22 1091 {
34e5028f
VZ
1092 // the id is probably invalid?
1093 wxLogLastError(wxT("TB_SETBUTTONINFO"));
bdc72a22 1094 }
34e5028f
VZ
1095 }
1096 else
1097#endif // comctl32.dll 4.71
1098 // TB_SETBUTTONINFO unavailable
1099 {
1100 // try adding several separators to fit the controls width
1101 int widthSep = r.right - r.left;
1102 left = r.left;
1103
1104 TBBUTTON tbb;
1105 wxZeroMemory(tbb);
1106 tbb.idCommand = 0;
1107 tbb.fsState = TBSTATE_ENABLED;
1108 tbb.fsStyle = TBSTYLE_SEP;
1109
1110 size_t nSeparators = size.x / widthSep;
1111 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
bdc72a22 1112 {
bfbb0b4c
WS
1113 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON,
1114 index, (LPARAM)&tbb) )
bdc72a22 1115 {
34e5028f 1116 wxLogLastError(wxT("TB_INSERTBUTTON"));
bdc72a22
VZ
1117 }
1118
34e5028f 1119 index++;
bdc72a22 1120 }
1c383dba 1121
34e5028f
VZ
1122 // remember the number of separators we used - we'd have to
1123 // delete all of them later
1124 ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators);
1125
1126 // adjust the controls width to exactly cover the separators
cdb11cb9
VZ
1127 size.x = (nSeparators + 1)*widthSep;
1128 control->SetSize(size.x, wxDefaultCoord);
34e5028f
VZ
1129 }
1130
cdb11cb9
VZ
1131 // position the control itself correctly vertically centering it on the
1132 // icon area of the toolbar
1133 int height = r.bottom - r.top - staticTextSize.y;
1134
1c383dba 1135 int diff = height - size.y;
cdb11cb9 1136 if ( diff < 0 || !HasFlag(wxTB_TEXT) )
1c383dba 1137 {
cdb11cb9
VZ
1138 // not enough room for the static text
1139 if ( staticText )
1140 staticText->Hide();
1141
1142 // recalculate height & diff without the staticText control
1143 height = r.bottom - r.top;
1144 diff = height - size.y;
1145 if ( diff < 0 )
1146 {
1147 // the control is too high, resize to fit
1148 control->SetSize(wxDefaultCoord, height - 2);
1c383dba 1149
cdb11cb9
VZ
1150 diff = 2;
1151 }
1152 }
1153 else // enough space for both the control and the label
1154 {
1155 if ( staticText )
1156 staticText->Show();
1c383dba 1157 }
2bda0e17 1158
34e5028f 1159 int top;
7a976304 1160 if ( IsVertical() )
34e5028f
VZ
1161 {
1162 left = 0;
1163 top = y;
1164
97071cdb 1165 y += height + 2 * GetMargins().y;
34e5028f
VZ
1166 }
1167 else // horizontal toolbar
1168 {
bfbb0b4c 1169 if ( left == wxDefaultCoord )
34e5028f
VZ
1170 left = r.left;
1171
1172 top = r.top;
1173 }
1174
1175 control->Move(left, top + (diff + 1) / 2);
cdb11cb9
VZ
1176 if ( staticText )
1177 {
1178 staticText->Move(left + (size.x - staticTextSize.x)/2,
1179 r.bottom - staticTextSize.y);
1180 }
1c383dba 1181 }
89b892a2 1182
8a0681f9
VZ
1183 // the max index is the "real" number of buttons - i.e. counting even the
1184 // separators which we added just for aligning the controls
1185 m_nButtons = index;
89b892a2 1186
7a976304 1187 if ( !IsVertical() )
8a0681f9
VZ
1188 {
1189 if ( m_maxRows == 0 )
8a0681f9
VZ
1190 // if not set yet, only one row
1191 SetRows(1);
8a0681f9
VZ
1192 }
1193 else if ( m_nButtons > 0 ) // vertical non empty toolbar
1194 {
e49426a2
RD
1195 // if not set yet, have one column
1196 m_maxRows = 1;
f4322df6 1197 SetRows(m_nButtons);
8a0681f9 1198 }
2bda0e17 1199
9f884528 1200 InvalidateBestSize();
2091f5e7 1201 UpdateSize();
c6dcd973 1202
bfbb0b4c 1203 return true;
2bda0e17
KB
1204}
1205
1c383dba
VZ
1206// ----------------------------------------------------------------------------
1207// message handlers
1208// ----------------------------------------------------------------------------
1209
0edeeb6d 1210bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_)
2bda0e17 1211{
0edeeb6d
VZ
1212 // cast to signed is important as we compare this id with (signed) ints in
1213 // FindById() and without the cast we'd get a positive int from a
1214 // "negative" (i.e. > 32767) WORD
1215 const int id = (signed short)id_;
1216
1217 wxToolBarToolBase *tool = FindById(id);
8a0681f9 1218 if ( !tool )
bfbb0b4c 1219 return false;
1c383dba 1220
1bba1946 1221 bool toggled = false; // just to suppress warnings
6bb7cee4 1222
8a0681f9 1223 if ( tool->CanBeToggled() )
1c383dba
VZ
1224 {
1225 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
6bb7cee4
VZ
1226 toggled = (state & TBSTATE_CHECKED) != 0;
1227
0356c259
VZ
1228 // ignore the event when a radio button is released, as this doesn't
1229 // seem to happen at all, and is handled otherwise
6bb7cee4 1230 if ( tool->GetKind() == wxITEM_RADIO && !toggled )
bfbb0b4c 1231 return true;
1c383dba 1232
6bb7cee4
VZ
1233 tool->Toggle(toggled);
1234 UnToggleRadioGroup(tool);
1235 }
8a0681f9 1236
6bb7cee4
VZ
1237 // OnLeftClick() can veto the button state change - for buttons which
1238 // may be toggled only, of couse
0edeeb6d 1239 if ( !OnLeftClick(id, toggled) && tool->CanBeToggled() )
f3ba93c1 1240 {
6bb7cee4
VZ
1241 // revert back
1242 tool->Toggle(!toggled);
8a0681f9 1243
0356c259 1244 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0));
1c383dba
VZ
1245 }
1246
bfbb0b4c 1247 return true;
2bda0e17
KB
1248}
1249
8a0681f9 1250bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
fd3f686c 1251 WXLPARAM lParam,
33ac7e6f 1252 WXLPARAM *WXUNUSED(result))
2bda0e17 1253{
a9a0ceca
VZ
1254 LPNMHDR hdr = (LPNMHDR)lParam;
1255 if ( hdr->code == TBN_DROPDOWN )
1256 {
1257 LPNMTOOLBAR tbhdr = (LPNMTOOLBAR)lParam;
1258
1259 wxCommandEvent evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED, tbhdr->iItem);
937013e0 1260 if ( HandleWindowEvent(evt) )
a9a0ceca
VZ
1261 {
1262 // Event got handled, don't display default popup menu
1263 return false;
1264 }
1265
1266 const wxToolBarToolBase * const tool = FindById(tbhdr->iItem);
1267 wxCHECK_MSG( tool, false, _T("drop down message for unknown tool") );
1268
1269 wxMenu * const menu = tool->GetDropdownMenu();
1270 if ( !menu )
1271 return false;
1272
1273 // Display popup menu below button
3c30bbb9
VZ
1274 const RECT r = wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr->iItem));
1275 if ( r.right )
a9a0ceca
VZ
1276 PopupMenu(menu, r.left, r.bottom);
1277
1278 return true;
1279 }
1280
1281
6a1b3ead
VZ
1282 if( !HasFlag(wxTB_NO_TOOLTIPS) )
1283 {
3bce6687 1284#if wxUSE_TOOLTIPS
6a1b3ead 1285 // First check if this applies to us
2bda0e17 1286
6a1b3ead
VZ
1287 // the tooltips control created by the toolbar is sometimes Unicode, even
1288 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1289 UINT code = hdr->code;
1290 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
1291 return false;
2bda0e17 1292
6a1b3ead
VZ
1293 HWND toolTipWnd = (HWND)::SendMessage(GetHwnd(), TB_GETTOOLTIPS, 0, 0);
1294 if ( toolTipWnd != hdr->hwndFrom )
1295 return false;
2bda0e17 1296
6a1b3ead
VZ
1297 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
1298 int id = (int)ttText->hdr.idFrom;
89b892a2 1299
6a1b3ead
VZ
1300 wxToolBarToolBase *tool = FindById(id);
1301 if ( tool )
1302 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
3bce6687 1303#else
6a1b3ead
VZ
1304 wxUnusedVar(lParam);
1305#endif
1306 }
abf912c5 1307
bfbb0b4c 1308 return false;
2bda0e17
KB
1309}
1310
1c383dba 1311// ----------------------------------------------------------------------------
8a0681f9 1312// toolbar geometry
1c383dba
VZ
1313// ----------------------------------------------------------------------------
1314
8a0681f9 1315void wxToolBar::SetToolBitmapSize(const wxSize& size)
2bda0e17 1316{
1c383dba
VZ
1317 wxToolBarBase::SetToolBitmapSize(size);
1318
1319 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
2bda0e17
KB
1320}
1321
8a0681f9 1322void wxToolBar::SetRows(int nRows)
2bda0e17 1323{
8a0681f9
VZ
1324 if ( nRows == m_maxRows )
1325 {
1326 // avoid resizing the frame uselessly
1327 return;
1328 }
1329
1330 // TRUE in wParam means to create at least as many rows, FALSE -
1331 // at most as many
1c383dba
VZ
1332 RECT rect;
1333 ::SendMessage(GetHwnd(), TB_SETROWS,
8a0681f9
VZ
1334 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
1335 (LPARAM) &rect);
1c383dba
VZ
1336
1337 m_maxRows = nRows;
8a0681f9
VZ
1338
1339 UpdateSize();
1340}
1341
1342// The button size is bigger than the bitmap size
1343wxSize wxToolBar::GetToolSize() const
1344{
1345 // TB_GETBUTTONSIZE is supported from version 4.70
5438a566 1346#if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
50165591
CE
1347 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1348 && !defined (__DIGITALMARS__)
2a1f999f 1349 if ( wxApp::GetComCtl32Version() >= 470 )
8a0681f9
VZ
1350 {
1351 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
1352
1353 return wxSize(LOWORD(dw), HIWORD(dw));
1354 }
1355 else
1356#endif // comctl32.dll 4.70+
1357 {
1358 // defaults
1359 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
1360 }
2bda0e17
KB
1361}
1362
82c9f85c
VZ
1363static
1364wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
1365 size_t index )
a8945eef 1366{
222ed1d6 1367 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
a8945eef 1368
489f6cf7 1369 for ( ; current ; current = current->GetNext() )
a8945eef 1370 {
82c9f85c 1371 if ( index == 0 )
a8945eef 1372 return current->GetData();
82c9f85c
VZ
1373
1374 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
1375 size_t separators = tool->GetSeparatorsCount();
1376
1377 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1378 // otherwise, skip as many items as the separator count, plus the
1379 // control itself
1380 index -= separators ? separators + 1 : 1;
a8945eef
MB
1381 }
1382
1383 return 0;
1384}
1385
8a0681f9 1386wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
2bda0e17 1387{
8a0681f9
VZ
1388 POINT pt;
1389 pt.x = x;
1390 pt.y = y;
1391 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
97071cdb 1392
37d0bdff
MB
1393 // MBN: when the point ( x, y ) is close to the toolbar border
1394 // TB_HITTEST returns m_nButtons ( not -1 )
1395 if ( index < 0 || (size_t)index >= m_nButtons )
8a0681f9
VZ
1396 // it's a separator or there is no tool at all there
1397 return (wxToolBarToolBase *)NULL;
1c383dba 1398
a84f66ef
VZ
1399 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1400 // we don't use the dummy separators hack
1401#ifdef TB_SETBUTTONINFO
2a1f999f 1402 if ( wxApp::GetComCtl32Version() >= 471 )
a8945eef
MB
1403 {
1404 return m_tools.Item((size_t)index)->GetData();
1405 }
1406 else
a84f66ef 1407#endif // TB_SETBUTTONINFO
a8945eef
MB
1408 {
1409 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1410 }
2bda0e17
KB
1411}
1412
8a0681f9 1413void wxToolBar::UpdateSize()
2bda0e17 1414{
48a102f9 1415 wxPoint pos = GetPosition();
93489fc8 1416 ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
48a102f9
JS
1417 if (pos != GetPosition())
1418 Move(pos);
93489fc8 1419
b93328d8
RD
1420 // In case Realize is called after the initial display (IOW the programmer
1421 // may have rebuilt the toolbar) give the frame the option of resizing the
1422 // toolbar to full width again, but only if the parent is a frame and the
1423 // toolbar is managed by the frame. Otherwise assume that some other
1424 // layout mechanism is controlling the toolbar size and leave it alone.
8a0681f9 1425 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
b93328d8
RD
1426 if ( frame && frame->GetToolBar() == this )
1427 {
f6bcfd97 1428 frame->SendSizeEvent();
b93328d8 1429 }
2bda0e17
KB
1430}
1431
c631abda
VZ
1432// ----------------------------------------------------------------------------
1433// toolbar styles
1434// ---------------------------------------------------------------------------
1435
2588b9c4
VZ
1436// get the TBSTYLE of the given toolbar window
1437long wxToolBar::GetMSWToolbarStyle() const
1438{
1439 return ::SendMessage(GetHwnd(), TB_GETSTYLE, 0, 0L);
1440}
1441
c631abda
VZ
1442void wxToolBar::SetWindowStyleFlag(long style)
1443{
24998710
VZ
1444 // the style bits whose changes force us to recreate the toolbar
1445 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
c631abda 1446
24998710 1447 const long styleOld = GetWindowStyle();
c631abda 1448
24998710 1449 wxToolBarBase::SetWindowStyleFlag(style);
c631abda 1450
24998710
VZ
1451 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1452 // also fatal as we'd then try to recreate the toolbar when it's just being
1453 // created
1454 if ( GetToolsCount() &&
1455 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1456 {
1457 // to remove the text labels, simply re-realizing the toolbar is enough
1458 // but I don't know of any way to add the text to an existing toolbar
1459 // other than by recreating it entirely
1460 Recreate();
c631abda 1461 }
c631abda
VZ
1462}
1463
1c383dba
VZ
1464// ----------------------------------------------------------------------------
1465// tool state
1466// ----------------------------------------------------------------------------
1467
8a0681f9 1468void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
2bda0e17 1469{
8a0681f9
VZ
1470 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1471 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
2bda0e17
KB
1472}
1473
8a0681f9 1474void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
2bda0e17 1475{
8a0681f9
VZ
1476 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1477 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
2bda0e17
KB
1478}
1479
33ac7e6f 1480void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
088a95f5 1481{
8a0681f9
VZ
1482 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1483 // without, so we really need to delete the button and recreate it here
1484 wxFAIL_MSG( _T("not implemented") );
2bda0e17
KB
1485}
1486
bbd321ff
RD
1487void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1488{
1489 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1490 if ( tool )
1491 {
1492 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1493
1494 tool->SetNormalBitmap(bitmap);
1495 Realize();
f4322df6 1496 }
bbd321ff
RD
1497}
1498
1499void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1500{
1501 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1502 if ( tool )
1503 {
1504 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1505
1506 tool->SetDisabledBitmap(bitmap);
1507 Realize();
f4322df6 1508 }
bbd321ff
RD
1509}
1510
1c383dba
VZ
1511// ----------------------------------------------------------------------------
1512// event handlers
1513// ----------------------------------------------------------------------------
2bda0e17
KB
1514
1515// Responds to colour changes, and passes event on to children.
8a0681f9 1516void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
2bda0e17 1517{
84c5b38d 1518 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
2bda0e17
KB
1519
1520 // Remap the buttons
8a0681f9 1521 Realize();
2bda0e17 1522
56bd6aac
VZ
1523 // Relayout the toolbar
1524 int nrows = m_maxRows;
1525 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1526 SetRows(nrows);
1527
2bda0e17
KB
1528 Refresh();
1529
56bd6aac
VZ
1530 // let the event propagate further
1531 event.Skip();
2bda0e17
KB
1532}
1533
8a0681f9 1534void wxToolBar::OnMouseEvent(wxMouseEvent& event)
e6460682 1535{
6ae2a4b7 1536 if ( event.Leaving() )
fe87983b 1537 {
6ae2a4b7
VZ
1538 if ( m_pInTool )
1539 {
1540 OnMouseEnter(wxID_ANY);
1541 m_pInTool = NULL;
1542 }
1543
fe87983b
MB
1544 event.Skip();
1545 return;
1546 }
1547
2334fef6 1548 if ( event.RightDown() )
e6460682 1549 {
2334fef6 1550 // find the tool under the mouse
8d7eaf91 1551 wxCoord x = 0, y = 0;
2334fef6
VZ
1552 event.GetPosition(&x, &y);
1553
1554 wxToolBarToolBase *tool = FindToolForPosition(x, y);
1555 OnRightClick(tool ? tool->GetId() : -1, x, y);
e6460682
JS
1556 }
1557 else
1558 {
42e69d6b 1559 event.Skip();
e6460682
JS
1560 }
1561}
1562
0090a153
JS
1563// This handler is required to allow the toolbar to be set to a non-default
1564// colour: for example, when it must blend in with a notebook page.
1565void wxToolBar::OnEraseBackground(wxEraseEvent& event)
1566{
848c37c3 1567 RECT rect = wxGetClientRect(GetHwnd());
888dde65
RR
1568
1569 wxDC *dc = event.GetDC();
1570 if (!dc) return;
1571 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc->GetImpl();
1572 HDC hdc = GetHdcOf(*impl);
0090a153 1573
99be3b7c
JS
1574 int majorVersion, minorVersion;
1575 wxGetOsVersion(& majorVersion, & minorVersion);
1576
2588b9c4
VZ
1577#if wxUSE_UXTHEME
1578 // we may need to draw themed colour so that we appear correctly on
1579 // e.g. notebook page under XP with themes but only do it if the parent
1580 // draws themed background itself
1581 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1582 {
1583 wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive();
1584 if ( theme )
1585 {
1586 HRESULT
1587 hr = theme->DrawThemeParentBackground(GetHwnd(), hdc, &rect);
1588 if ( hr == S_OK )
1589 return;
1590
1591 // it can also return S_FALSE which seems to simply say that it
1592 // didn't draw anything but no error really occurred
1593 if ( FAILED(hr) )
1594 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr);
1595 }
1596 }
99be3b7c
JS
1597
1598 // Only draw a rebar theme on Vista, since it doesn't jive so well with XP
921dd444 1599 if ( !UseBgCol() && majorVersion >= 6 )
99be3b7c
JS
1600 {
1601 wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive();
1602 if ( theme )
1603 {
1604 wxUxThemeHandle hTheme(this, L"REBAR");
1605
1606 RECT r;
1607 wxRect rect = GetClientRect();
1608 wxCopyRectToRECT(rect, r);
1609
1610 HRESULT hr = theme->DrawThemeBackground(hTheme, hdc, 0, 0, & r, NULL);
1611 if ( hr == S_OK )
1612 return;
1613
1614 // it can also return S_FALSE which seems to simply say that it
1615 // didn't draw anything but no error really occurred
1616 if ( FAILED(hr) )
1617 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr);
1618 }
1619 }
1620
2588b9c4
VZ
1621#endif // wxUSE_UXTHEME
1622
0fb0db5c
VZ
1623 // we need to always draw our background under XP, as otherwise it doesn't
1624 // appear correctly with some themes (e.g. Zune one)
1625 if ( majorVersion == 5 ||
1626 UseBgCol() || (GetMSWToolbarStyle() & TBSTYLE_TRANSPARENT) )
053889ad
VZ
1627 {
1628 // do draw our background
1629 //
1630 // notice that this 'dumb' implementation may cause flicker for some of
1631 // the controls in which case they should intercept wxEraseEvent and
1632 // process it themselves somehow
1633 AutoHBRUSH hBrush(wxColourToRGB(GetBackgroundColour()));
0090a153 1634
053889ad
VZ
1635 wxCHANGE_HDC_MAP_MODE(hdc, MM_TEXT);
1636 ::FillRect(hdc, &rect, hBrush);
1637 }
1638 else // we have no non default background colour
1639 {
2588b9c4
VZ
1640 // let the system do it for us
1641 event.Skip();
053889ad 1642 }
0090a153
JS
1643}
1644
2eb10e2a 1645bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
bdc72a22 1646{
fd6939a6
VZ
1647 // wait until we have some tools
1648 if ( !GetToolsCount() )
1649 return false;
1650
3bb63e5c 1651 // calculate our minor dimension ourselves - we're confusing the standard
7509fa8c 1652 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
3c30bbb9
VZ
1653 const RECT r = wxGetTBItemRect(GetHwnd(), 0);
1654 if ( !r.right )
1655 return false;
7509fa8c 1656
3c30bbb9
VZ
1657 int w, h;
1658
1659 if ( IsVertical() )
1660 {
1661 w = r.right - r.left;
1662 if ( m_maxRows )
7509fa8c 1663 {
3c30bbb9 1664 w *= (m_nButtons + m_maxRows - 1)/m_maxRows;
7509fa8c 1665 }
3c30bbb9
VZ
1666 h = HIWORD(lParam);
1667 }
1668 else
1669 {
1670 w = LOWORD(lParam);
1671 if (HasFlag( wxTB_FLAT ))
1672 h = r.bottom - r.top - 3;
7509fa8c 1673 else
3c30bbb9
VZ
1674 h = r.bottom - r.top;
1675 if ( m_maxRows )
7509fa8c 1676 {
3c30bbb9
VZ
1677 // FIXME: hardcoded separator line height...
1678 h += HasFlag(wxTB_NODIVIDER) ? 4 : 6;
1679 h *= m_maxRows;
7509fa8c 1680 }
3c30bbb9 1681 }
7509fa8c 1682
3c30bbb9
VZ
1683 if ( MAKELPARAM(w, h) != lParam )
1684 {
1685 // size really changed
1686 SetSize(w, h);
7509fa8c
VZ
1687 }
1688
3c30bbb9
VZ
1689 // message processed
1690 return true;
7509fa8c
VZ
1691}
1692
1693bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam)
1694{
97071cdb
DS
1695 // erase any dummy separators which were used
1696 // for aligning the controls if any here
7509fa8c 1697
97071cdb 1698 // first of all, are there any controls at all?
222ed1d6 1699 wxToolBarToolsList::compatibility_iterator node;
7509fa8c
VZ
1700 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1701 {
1702 if ( node->GetData()->IsControl() )
1703 break;
1704 }
1705
1706 if ( !node )
7509fa8c 1707 // no controls, nothing to erase
bfbb0b4c 1708 return false;
7509fa8c 1709
99be3b7c
JS
1710 wxSize clientSize = GetClientSize();
1711 int majorVersion, minorVersion;
1712 wxGetOsVersion(& majorVersion, & minorVersion);
1713
7509fa8c
VZ
1714 // prepare the DC on which we'll be drawing
1715 wxClientDC dc(this);
1716 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
1717 dc.SetPen(*wxTRANSPARENT_PEN);
1718
1719 RECT r;
bfbb0b4c 1720 if ( !::GetUpdateRect(GetHwnd(), &r, FALSE) )
7509fa8c 1721 // nothing to redraw anyhow
bfbb0b4c 1722 return false;
7509fa8c
VZ
1723
1724 wxRect rectUpdate;
1725 wxCopyRECTToRect(r, rectUpdate);
1726
1727 dc.SetClippingRegion(rectUpdate);
8a0681f9 1728
7509fa8c
VZ
1729 // draw the toolbar tools, separators &c normally
1730 wxControl::MSWWindowProc(WM_PAINT, wParam, lParam);
1731
1732 // for each control in the toolbar find all the separators intersecting it
1733 // and erase them
1734 //
1735 // NB: this is really the only way to do it as we don't know if a separator
1736 // corresponds to a control (i.e. is a dummy one) or a real one
1737 // otherwise
1738 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1739 {
cdb11cb9 1740 wxToolBarTool *tool = (wxToolBarTool*)node->GetData();
7509fa8c
VZ
1741 if ( tool->IsControl() )
1742 {
1743 // get the control rect in our client coords
1744 wxControl *control = tool->GetControl();
cdb11cb9 1745 wxStaticText *staticText = tool->GetStaticText();
7509fa8c 1746 wxRect rectCtrl = control->GetRect();
cdb11cb9
VZ
1747 wxRect rectStaticText(0,0,0,0);
1748 if ( staticText )
1749 {
1750 rectStaticText = staticText->GetRect();
1751 }
7509fa8c
VZ
1752
1753 // iterate over all buttons
1754 TBBUTTON tbb;
1755 int count = ::SendMessage(GetHwnd(), TB_BUTTONCOUNT, 0, 0);
1756 for ( int n = 0; n < count; n++ )
8a0681f9 1757 {
7509fa8c
VZ
1758 // is it a separator?
1759 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON,
1760 n, (LPARAM)&tbb) )
8a0681f9 1761 {
7509fa8c
VZ
1762 wxLogDebug(_T("TB_GETBUTTON failed?"));
1763
1764 continue;
8a0681f9 1765 }
7509fa8c
VZ
1766
1767 if ( tbb.fsStyle != TBSTYLE_SEP )
1768 continue;
1769
1770 // get the bounding rect of the separator
3c30bbb9
VZ
1771 RECT r = wxGetTBItemRect(GetHwnd(), n);
1772 if ( !r.right )
7509fa8c 1773 continue;
8a0681f9 1774
7509fa8c
VZ
1775 // does it intersect the control?
1776 wxRect rectItem;
1777 wxCopyRECTToRect(r, rectItem);
921dd444 1778 if ( rectCtrl.Intersects(rectItem) || (staticText && rectStaticText.Intersects(rectItem)))
cdb11cb9
VZ
1779 {
1780 // yes, do erase it!
99be3b7c
JS
1781
1782 bool haveRefreshed = false;
1783
1784#if wxUSE_UXTHEME
1785 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1786 {
1787 // Don't use DrawThemeBackground
1788 }
921dd444 1789 else if ( !UseBgCol() && majorVersion >= 6 )
99be3b7c
JS
1790 {
1791 wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive();
1792 if ( theme )
1793 {
1794 wxUxThemeHandle hTheme(this, L"REBAR");
1795
1796 RECT clipRect = r;
1797
1798 // Draw the whole background since the pattern may be position sensitive;
1799 // but clip it to the area of interest.
1800 r.left = 0;
1801 r.right = clientSize.x;
1802 r.top = 0;
1803 r.bottom = clientSize.y;
888dde65
RR
1804
1805 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
1806 HRESULT hr = theme->DrawThemeBackground(hTheme, GetHdcOf(*impl), 0, 0, & r, & clipRect);
99be3b7c
JS
1807 if ( hr == S_OK )
1808 haveRefreshed = true;
1809 }
1810 }
1811#endif
1812
1813 if (!haveRefreshed)
1814 dc.DrawRectangle(rectItem);
921dd444 1815 }
cdb11cb9 1816
921dd444
JS
1817 if ( rectCtrl.Intersects(rectItem) )
1818 {
1819 // Necessary in case we use a no-paint-on-size
1820 // style in the parent: the controls can disappear
1821 control->Refresh(false);
a14337d6 1822 }
921dd444
JS
1823
1824 if ( staticText && rectStaticText.Intersects(rectItem) )
1825 {
cdb11cb9
VZ
1826 // Necessary in case we use a no-paint-on-size
1827 // style in the parent: the controls can disappear
1828 staticText->Refresh(false);
1829 }
8a0681f9 1830 }
8a0681f9 1831 }
bdc72a22 1832 }
7509fa8c 1833
bfbb0b4c 1834 return true;
7509fa8c
VZ
1835}
1836
2eb10e2a 1837void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
7509fa8c
VZ
1838{
1839 wxCoord x = GET_X_LPARAM(lParam),
1840 y = GET_Y_LPARAM(lParam);
1841 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1842
6ae2a4b7
VZ
1843 // has the current tool changed?
1844 if ( tool != m_pInTool )
7509fa8c
VZ
1845 {
1846 m_pInTool = tool;
6ae2a4b7 1847 OnMouseEnter(tool ? tool->GetId() : wxID_ANY);
7509fa8c
VZ
1848 }
1849}
a8945eef 1850
c140b7e7 1851WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
7509fa8c
VZ
1852{
1853 switch ( nMsg )
1854 {
7509fa8c
VZ
1855 case WM_MOUSEMOVE:
1856 // we don't handle mouse moves, so always pass the message to
4012b958 1857 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
7509fa8c
VZ
1858 HandleMouseMove(wParam, lParam);
1859 break;
a8945eef 1860
4012b958
JS
1861 case WM_SIZE:
1862 if ( HandleSize(wParam, lParam) )
1863 return 0;
1864 break;
1865
1866#ifndef __WXWINCE__
7509fa8c
VZ
1867 case WM_PAINT:
1868 if ( HandlePaint(wParam, lParam) )
1869 return 0;
4012b958 1870#endif
97071cdb
DS
1871
1872 default:
1873 break;
a8945eef 1874 }
bdc72a22 1875
8a0681f9 1876 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
bdc72a22
VZ
1877}
1878
1c383dba
VZ
1879// ----------------------------------------------------------------------------
1880// private functions
1881// ----------------------------------------------------------------------------
1882
fb7e87ef
VZ
1883#ifdef wxREMAP_BUTTON_COLOURS
1884
566fb299 1885WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height)
2bda0e17 1886{
566fb299 1887 MemoryHDC hdcMem;
56bd6aac 1888
566fb299 1889 if ( !hdcMem )
5e68f8f3 1890 {
566fb299
VZ
1891 wxLogLastError(_T("CreateCompatibleDC"));
1892
1893 return bitmap;
5e68f8f3 1894 }
56bd6aac 1895
566fb299 1896 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
56bd6aac 1897
566fb299 1898 if ( !bmpInHDC )
5e68f8f3 1899 {
566fb299
VZ
1900 wxLogLastError(_T("SelectObject"));
1901
1902 return bitmap;
1903 }
56bd6aac 1904
90c1530a
VZ
1905 wxCOLORMAP *cmap = wxGetStdColourMap();
1906
684c68fd
VZ
1907 for ( int i = 0; i < width; i++ )
1908 {
1909 for ( int j = 0; j < height; j++ )
1910 {
1911 COLORREF pixel = ::GetPixel(hdcMem, i, j);
1912
90c1530a 1913 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
684c68fd 1914 {
90c1530a 1915 COLORREF col = cmap[k].from;
684c68fd
VZ
1916 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
1917 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
1918 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
1919 {
fb7e87ef
VZ
1920 if ( cmap[k].to != pixel )
1921 ::SetPixel(hdcMem, i, j, cmap[k].to);
684c68fd
VZ
1922 break;
1923 }
1924 }
1925 }
1926 }
3a3898f8
VZ
1927
1928 return bitmap;
566fb299 1929}
2bda0e17 1930
fb7e87ef
VZ
1931#endif // wxREMAP_BUTTON_COLOURS
1932
97071cdb 1933#endif // wxUSE_TOOLBAR