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