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