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