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