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