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