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