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