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