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