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