]> git.saurik.com Git - wxWidgets.git/blame - src/msw/toolbar.cpp
Generate key events in generic wxDataViewCtrl implementation.
[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();
621 if ( bmpDisabled.Ok() )
622 {
623 m_disabledImgList = new wxImageList
624 (
625 m_defaultWidth,
626 m_defaultHeight,
627 bmpDisabled.GetMask() != NULL,
628 GetToolsCount()
629 );
2c622593 630 break;
8d0a7b56
VZ
631 }
632 }
633
634 // we don't have any disabled bitmaps
635 }
8d0a7b56
VZ
636}
637
8a0681f9 638bool wxToolBar::Realize()
1c383dba 639{
bb2212e6
VZ
640 if ( !wxToolBarBase::Realize() )
641 return false;
1c383dba 642
bb2212e6 643 const size_t nTools = GetToolsCount();
26dd7154 644
fb7e87ef
VZ
645#ifdef wxREMAP_BUTTON_COLOURS
646 // don't change the values of these constants, they can be set from the
647 // user code via wxSystemOptions
648 enum
92661f97 649 {
fb7e87ef
VZ
650 Remap_None = -1,
651 Remap_Bg,
652 Remap_Buttons,
653 Remap_TransparentBg
654 };
655
656 // the user-specified option overrides anything, but if it wasn't set, only
657 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
658 // much worse after remapping
659 static const wxChar *remapOption = wxT("msw.remap");
660 const int remapValue = wxSystemOptions::HasOption(remapOption)
661 ? wxSystemOptions::GetOptionInt(remapOption)
662 : wxDisplayDepth() <= 8 ? Remap_Buttons
663 : Remap_None;
664
665#endif // wxREMAP_BUTTON_COLOURS
92661f97 666
2b5f62a0
VZ
667 // delete all old buttons, if any
668 for ( size_t pos = 0; pos < m_nButtons; pos++ )
669 {
670 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
671 {
672 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
673 }
674 }
675
8a0681f9
VZ
676 // First, add the bitmap: we use one bitmap for all toolbar buttons
677 // ----------------------------------------------------------------
2bda0e17 678
222ed1d6 679 wxToolBarToolsList::compatibility_iterator node;
24998710
VZ
680 int bitmapId = 0;
681
0580ddb2 682 if ( !HasFlag(wxTB_NOICONS) )
24998710
VZ
683 {
684 // if we already have a bitmap, we'll replace the existing one --
685 // otherwise we'll install a new one
686 HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap;
687
4a10ea8b
MW
688 const wxCoord totalBitmapWidth = m_defaultWidth *
689 wx_truncate_cast(wxCoord, nTools),
24998710 690 totalBitmapHeight = m_defaultHeight;
2bda0e17 691
17cfea01 692 // Create a bitmap and copy all the tool bitmaps into it
24998710
VZ
693 wxMemoryDC dcAllButtons;
694 wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight);
695 dcAllButtons.SelectObject(bitmap);
bb86baea 696
fb7e87ef
VZ
697#ifdef wxREMAP_BUTTON_COLOURS
698 if ( remapValue != Remap_TransparentBg )
699#endif // wxREMAP_BUTTON_COLOURS
700 {
701 // VZ: why do we hardcode grey colour for CE?
702 dcAllButtons.SetBackground(wxBrush(
703#ifdef __WXWINCE__
704 wxColour(0xc0, 0xc0, 0xc0)
705#else // !__WXWINCE__
706 GetBackgroundColour()
707#endif // __WXWINCE__/!__WXWINCE__
708 ));
709 dcAllButtons.Clear();
710 }
24998710
VZ
711
712 m_hBitmap = bitmap.GetHBITMAP();
713 HBITMAP hBitmap = (HBITMAP)m_hBitmap;
2bda0e17 714
fb7e87ef
VZ
715#ifdef wxREMAP_BUTTON_COLOURS
716 if ( remapValue == Remap_Bg )
dfa53609 717 {
dfa53609 718 dcAllButtons.SelectObject(wxNullBitmap);
dfa53609
JS
719
720 // Even if we're not remapping the bitmap
721 // content, we still have to remap the background.
722 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
723 totalBitmapWidth, totalBitmapHeight);
724
dfa53609 725 dcAllButtons.SelectObject(bitmap);
dfa53609 726 }
fb7e87ef 727#endif // wxREMAP_BUTTON_COLOURS
dfa53609 728
24998710
VZ
729 // the button position
730 wxCoord x = 0;
2bda0e17 731
24998710
VZ
732 // the number of buttons (not separators)
733 int nButtons = 0;
1c383dba 734
8d0a7b56 735 CreateDisabledImageList();
4769a562 736 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1c383dba 737 {
24998710
VZ
738 wxToolBarToolBase *tool = node->GetData();
739 if ( tool->IsButton() )
1c383dba 740 {
24998710 741 const wxBitmap& bmp = tool->GetNormalBitmap();
ba700b5c
VZ
742
743 const int w = bmp.GetWidth();
744 const int h = bmp.GetHeight();
745
24998710
VZ
746 if ( bmp.Ok() )
747 {
ba700b5c
VZ
748 int xOffset = wxMax(0, (m_defaultWidth - w)/2);
749 int yOffset = wxMax(0, (m_defaultHeight - h)/2);
750
24998710 751 // notice the last parameter: do use mask
ba700b5c 752 dcAllButtons.DrawBitmap(bmp, x + xOffset, yOffset, true);
24998710
VZ
753 }
754 else
1c383dba 755 {
9a83f860 756 wxFAIL_MSG( wxT("invalid tool button bitmap") );
1c383dba
VZ
757 }
758
8d0a7b56
VZ
759 // also deal with disabled bitmap if we want to use them
760 if ( m_disabledImgList )
761 {
762 wxBitmap bmpDisabled = tool->GetDisabledBitmap();
64c288fa 763#if wxUSE_IMAGE && wxUSE_WXDIB
8d0a7b56
VZ
764 if ( !bmpDisabled.Ok() )
765 {
8d0a7b56
VZ
766 // no disabled bitmap specified but we still need to
767 // fill the space in the image list with something, so
768 // we grey out the normal bitmap
9ac43913
VZ
769 wxImage
770 imgGreyed = bmp.ConvertToImage().ConvertToGreyscale();
ba700b5c 771
fb7e87ef
VZ
772#ifdef wxREMAP_BUTTON_COLOURS
773 if ( remapValue == Remap_Buttons )
ba700b5c 774 {
0f99ce3d
DS
775 // we need to have light grey background colour for
776 // MapBitmap() to work correctly
777 for ( int y = 0; y < h; y++ )
ba700b5c 778 {
0f99ce3d
DS
779 for ( int x = 0; x < w; x++ )
780 {
781 if ( imgGreyed.IsTransparent(x, y) )
782 imgGreyed.SetRGB(x, y,
783 wxLIGHT_GREY->Red(),
784 wxLIGHT_GREY->Green(),
785 wxLIGHT_GREY->Blue());
786 }
ba700b5c
VZ
787 }
788 }
fb7e87ef 789#endif // wxREMAP_BUTTON_COLOURS
8d0a7b56
VZ
790
791 bmpDisabled = wxBitmap(imgGreyed);
8d0a7b56 792 }
ba700b5c
VZ
793#endif // wxUSE_IMAGE
794
fb7e87ef
VZ
795#ifdef wxREMAP_BUTTON_COLOURS
796 if ( remapValue == Remap_Buttons )
0f99ce3d 797 MapBitmap(bmpDisabled.GetHBITMAP(), w, h);
fb7e87ef 798#endif // wxREMAP_BUTTON_COLOURS
8d0a7b56
VZ
799
800 m_disabledImgList->Add(bmpDisabled);
801 }
802
24998710
VZ
803 // still inc width and number of buttons because otherwise the
804 // subsequent buttons will all be shifted which is rather confusing
805 // (and like this you'd see immediately which bitmap was bad)
806 x += m_defaultWidth;
807 nButtons++;
1c383dba
VZ
808 }
809 }
8a0681f9 810
24998710 811 dcAllButtons.SelectObject(wxNullBitmap);
f6bcfd97 812
24998710
VZ
813 // don't delete this HBITMAP!
814 bitmap.SetHBITMAP(0);
2bda0e17 815
fb7e87ef
VZ
816#ifdef wxREMAP_BUTTON_COLOURS
817 if ( remapValue == Remap_Buttons )
dfa53609
JS
818 {
819 // Map to system colours
820 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
fb7e87ef 821 totalBitmapWidth, totalBitmapHeight);
dfa53609 822 }
fb7e87ef 823#endif // wxREMAP_BUTTON_COLOURS
1c383dba 824
bfbb0b4c 825 bool addBitmap = true;
9f83044f 826
24998710 827 if ( oldToolBarBitmap )
1c383dba 828 {
24998710 829#ifdef TB_REPLACEBITMAP
2a1f999f 830 if ( wxApp::GetComCtl32Version() >= 400 )
9f83044f 831 {
24998710
VZ
832 TBREPLACEBITMAP replaceBitmap;
833 replaceBitmap.hInstOld = NULL;
834 replaceBitmap.hInstNew = NULL;
dca0f651
VZ
835 replaceBitmap.nIDOld = (UINT_PTR)oldToolBarBitmap;
836 replaceBitmap.nIDNew = (UINT_PTR)hBitmap;
24998710
VZ
837 replaceBitmap.nButtons = nButtons;
838 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
839 0, (LPARAM) &replaceBitmap) )
840 {
841 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
842 }
9f83044f 843
24998710 844 ::DeleteObject(oldToolBarBitmap);
9f83044f 845
24998710 846 // already done
bfbb0b4c 847 addBitmap = false;
24998710
VZ
848 }
849 else
9f83044f 850#endif // TB_REPLACEBITMAP
24998710
VZ
851 {
852 // we can't replace the old bitmap, so we will add another one
853 // (awfully inefficient, but what else to do?) and shift the bitmap
854 // indices accordingly
bfbb0b4c 855 addBitmap = true;
1c383dba 856
24998710
VZ
857 bitmapId = m_nButtons;
858 }
9f83044f 859 }
1c383dba 860
24998710 861 if ( addBitmap ) // no old bitmap or we can't replace it
1c383dba 862 {
24998710
VZ
863 TBADDBITMAP addBitmap;
864 addBitmap.hInst = 0;
dca0f651 865 addBitmap.nID = (UINT_PTR)hBitmap;
24998710
VZ
866 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
867 (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 )
8a0681f9 868 {
24998710 869 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
8a0681f9 870 }
1c383dba 871 }
8d0a7b56 872
24bc71f4
VZ
873 // disable image lists are only supported in comctl32.dll 4.70+
874 if ( wxApp::GetComCtl32Version() >= 470 )
8d0a7b56 875 {
24bc71f4
VZ
876 HIMAGELIST hil = m_disabledImgList
877 ? GetHimagelistOf(m_disabledImgList)
878 : 0;
879
880 // notice that we set the image list even if don't have one right
881 // now as we could have it before and need to reset it in this case
8d0a7b56 882 HIMAGELIST oldImageList = (HIMAGELIST)
24bc71f4 883 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST, 0, (LPARAM)hil);
8d0a7b56
VZ
884
885 // delete previous image list if any
886 if ( oldImageList )
24bc71f4 887 ::DeleteObject(oldImageList);
8d0a7b56 888 }
2bda0e17 889 }
9f83044f 890
2bda0e17 891
8a0681f9
VZ
892 // Next add the buttons and separators
893 // -----------------------------------
894
0580ddb2 895 wxScopedArray<TBBUTTON> buttons(new TBBUTTON[nTools]);
2bda0e17 896
8a0681f9 897 // this array will hold the indices of all controls in the toolbar
1c383dba
VZ
898 wxArrayInt controlIds;
899
bfbb0b4c 900 bool lastWasRadio = false;
1c383dba 901 int i = 0;
4769a562 902 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
2bda0e17 903 {
cc260109 904 wxToolBarTool *tool = static_cast<wxToolBarTool *>(node->GetData());
8a0681f9 905
28603488
VZ
906 // don't add separators to the vertical toolbar with old comctl32.dll
907 // versions as they didn't handle this properly
7a976304 908 if ( IsVertical() && tool->IsSeparator() &&
2a1f999f 909 wxApp::GetComCtl32Version() <= 472 )
28603488
VZ
910 {
911 continue;
912 }
913
1c383dba
VZ
914 TBBUTTON& button = buttons[i];
915
916 wxZeroMemory(button);
917
bfbb0b4c 918 bool isRadio = false;
8a0681f9 919 switch ( tool->GetStyle() )
1c383dba
VZ
920 {
921 case wxTOOL_STYLE_CONTROL:
1c383dba 922 case wxTOOL_STYLE_SEPARATOR:
cc260109
VZ
923 if ( tool->IsStretchableSpace() )
924 {
925 // we're going to modify the size of this button later and
926 // so we need a valid id for it and not wxID_SEPARATOR
927 // which is used by spacers by default
928 tool->AllocSpacerId();
8c118ca4
VZ
929
930 // also set the number of separators so that the logic in
931 // HandlePaint() works correctly
932 tool->SetSeparatorsCount(1);
cc260109
VZ
933 }
934
935 button.idCommand = tool->GetId();
1c383dba
VZ
936 button.fsState = TBSTATE_ENABLED;
937 button.fsStyle = TBSTYLE_SEP;
938 break;
939
940 case wxTOOL_STYLE_BUTTON:
24998710
VZ
941 if ( !HasFlag(wxTB_NOICONS) )
942 button.iBitmap = bitmapId;
c631abda 943
24998710 944 if ( HasFlag(wxTB_TEXT) )
c631abda 945 {
24998710
VZ
946 const wxString& label = tool->GetLabel();
947 if ( !label.empty() )
dca0f651 948 button.iString = (INT_PTR)label.wx_str();
c631abda
VZ
949 }
950
8a0681f9 951 button.idCommand = tool->GetId();
1c383dba 952
8a0681f9 953 if ( tool->IsEnabled() )
1c383dba 954 button.fsState |= TBSTATE_ENABLED;
8a0681f9 955 if ( tool->IsToggled() )
1c383dba 956 button.fsState |= TBSTATE_CHECKED;
8a0681f9 957
c631abda
VZ
958 switch ( tool->GetKind() )
959 {
960 case wxITEM_RADIO:
961 button.fsStyle = TBSTYLE_CHECKGROUP;
962
963 if ( !lastWasRadio )
964 {
965 // the first item in the radio group is checked by
966 // default to be consistent with wxGTK and the menu
967 // radio items
968 button.fsState |= TBSTATE_CHECKED;
f3ba93c1 969
730d3366
RD
970 if (tool->Toggle(true))
971 {
972 DoToggleTool(tool, true);
973 }
974 }
0b62d387 975 else if ( tool->IsToggled() )
730d3366
RD
976 {
977 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
978 int prevIndex = i - 1;
979 while ( nodePrev )
980 {
981 TBBUTTON& prevButton = buttons[prevIndex];
982 wxToolBarToolBase *tool = nodePrev->GetData();
983 if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
984 break;
ed7fdb86 985
730d3366 986 if ( tool->Toggle(false) )
730d3366 987 DoToggleTool(tool, false);
a8cb1a49 988
a2dd2ea7 989 prevButton.fsState &= ~TBSTATE_CHECKED;
730d3366
RD
990 nodePrev = nodePrev->GetPrevious();
991 prevIndex--;
ed7fdb86 992 }
c631abda
VZ
993 }
994
bfbb0b4c 995 isRadio = true;
c631abda
VZ
996 break;
997
998 case wxITEM_CHECK:
999 button.fsStyle = TBSTYLE_CHECK;
1000 break;
1001
97071cdb
DS
1002 case wxITEM_NORMAL:
1003 button.fsStyle = TBSTYLE_BUTTON;
1004 break;
1005
a9a0ceca
VZ
1006 case wxITEM_DROPDOWN:
1007 button.fsStyle = TBSTYLE_DROPDOWN;
1008 break;
1009
c631abda 1010 default:
9a83f860 1011 wxFAIL_MSG( wxT("unexpected toolbar button kind") );
c631abda 1012 button.fsStyle = TBSTYLE_BUTTON;
97071cdb 1013 break;
c631abda 1014 }
1c383dba
VZ
1015
1016 bitmapId++;
1017 break;
1018 }
2bda0e17 1019
c631abda
VZ
1020 lastWasRadio = isRadio;
1021
1c383dba 1022 i++;
2bda0e17 1023 }
1c383dba 1024
0580ddb2 1025 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, i, (LPARAM)buttons.get()) )
2bda0e17 1026 {
f6bcfd97 1027 wxLogLastError(wxT("TB_ADDBUTTONS"));
2bda0e17 1028 }
89b892a2 1029
2bda0e17 1030
cc260109
VZ
1031 // Adjust controls and stretchable spaces
1032 // --------------------------------------
8a0681f9 1033
cc260109
VZ
1034 // adjust the controls size to fit nicely in the toolbar and compute its
1035 // total size while doing it
1036 m_totalFixedSize = 0;
1037 int toolIndex = 0;
1038 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), toolIndex++ )
1c383dba 1039 {
cc260109 1040 wxToolBarTool * const tool = (wxToolBarTool*)node->GetData();
1c383dba 1041
cc260109 1042 const RECT r = wxGetTBItemRect(GetHwnd(), toolIndex);
bdc72a22 1043
cc260109 1044 if ( !tool->IsControl() )
34e5028f 1045 {
cc260109
VZ
1046 if ( IsVertical() )
1047 m_totalFixedSize += r.bottom - r.top;
1048 else
1049 m_totalFixedSize += r.right - r.left;
1050
1051 continue;
1052 }
34e5028f 1053
cc260109
VZ
1054 if ( IsVertical() )
1055 {
1056 // don't embed controls in the vertical toolbar, this doesn't look
1057 // good and wxGTK doesn't do it neither (and the code below can't
1058 // deal with this case)
34e5028f
VZ
1059 continue;
1060 }
1061
cc260109 1062 wxControl * const control = tool->GetControl();
cdb11cb9
VZ
1063 wxStaticText * const staticText = tool->GetStaticText();
1064
34e5028f 1065 wxSize size = control->GetSize();
cdb11cb9
VZ
1066 wxSize staticTextSize;
1067 if ( staticText )
1068 {
1069 staticTextSize = staticText->GetSize();
1070 staticTextSize.y += 3; // margin between control and its label
1071 }
34e5028f 1072
bdc72a22 1073 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
5b59df83 1074#ifdef TB_SETBUTTONINFO
34e5028f
VZ
1075 // available in headers, now check whether it is available now
1076 // (during run-time)
2a1f999f 1077 if ( wxApp::GetComCtl32Version() >= 471 )
34e5028f
VZ
1078 {
1079 // set the (underlying) separators width to be that of the
1080 // control
1081 TBBUTTONINFO tbbi;
1082 tbbi.cbSize = sizeof(tbbi);
1083 tbbi.dwMask = TBIF_SIZE;
5c519b6c 1084 tbbi.cx = (WORD)size.x;
bfbb0b4c
WS
1085 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO,
1086 tool->GetId(), (LPARAM)&tbbi) )
bdc72a22 1087 {
34e5028f
VZ
1088 // the id is probably invalid?
1089 wxLogLastError(wxT("TB_SETBUTTONINFO"));
bdc72a22 1090 }
34e5028f
VZ
1091 }
1092 else
1093#endif // comctl32.dll 4.71
1094 // TB_SETBUTTONINFO unavailable
1095 {
1096 // try adding several separators to fit the controls width
1097 int widthSep = r.right - r.left;
34e5028f
VZ
1098
1099 TBBUTTON tbb;
1100 wxZeroMemory(tbb);
1101 tbb.idCommand = 0;
1102 tbb.fsState = TBSTATE_ENABLED;
1103 tbb.fsStyle = TBSTYLE_SEP;
1104
1105 size_t nSeparators = size.x / widthSep;
1106 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
bdc72a22 1107 {
bfbb0b4c 1108 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON,
cc260109 1109 toolIndex, (LPARAM)&tbb) )
bdc72a22 1110 {
34e5028f 1111 wxLogLastError(wxT("TB_INSERTBUTTON"));
bdc72a22
VZ
1112 }
1113
cc260109 1114 toolIndex++;
bdc72a22 1115 }
1c383dba 1116
34e5028f
VZ
1117 // remember the number of separators we used - we'd have to
1118 // delete all of them later
cc260109 1119 tool->SetSeparatorsCount(nSeparators);
34e5028f
VZ
1120
1121 // adjust the controls width to exactly cover the separators
cdb11cb9
VZ
1122 size.x = (nSeparators + 1)*widthSep;
1123 control->SetSize(size.x, wxDefaultCoord);
34e5028f
VZ
1124 }
1125
cdb11cb9
VZ
1126 // position the control itself correctly vertically centering it on the
1127 // icon area of the toolbar
1128 int height = r.bottom - r.top - staticTextSize.y;
1129
1c383dba 1130 int diff = height - size.y;
cdb11cb9 1131 if ( diff < 0 || !HasFlag(wxTB_TEXT) )
1c383dba 1132 {
cdb11cb9
VZ
1133 // not enough room for the static text
1134 if ( staticText )
1135 staticText->Hide();
1136
1137 // recalculate height & diff without the staticText control
1138 height = r.bottom - r.top;
1139 diff = height - size.y;
1140 if ( diff < 0 )
1141 {
1142 // the control is too high, resize to fit
1143 control->SetSize(wxDefaultCoord, height - 2);
1c383dba 1144
cdb11cb9
VZ
1145 diff = 2;
1146 }
1147 }
1148 else // enough space for both the control and the label
1149 {
1150 if ( staticText )
1151 staticText->Show();
1c383dba 1152 }
2bda0e17 1153
cc260109 1154 control->Move(r.left, r.top + (diff + 1) / 2);
cdb11cb9
VZ
1155 if ( staticText )
1156 {
cc260109 1157 staticText->Move(r.left + (size.x - staticTextSize.x)/2,
cdb11cb9
VZ
1158 r.bottom - staticTextSize.y);
1159 }
cc260109
VZ
1160
1161 m_totalFixedSize += size.x;
1c383dba 1162 }
89b892a2 1163
8a0681f9
VZ
1164 // the max index is the "real" number of buttons - i.e. counting even the
1165 // separators which we added just for aligning the controls
cc260109 1166 m_nButtons = toolIndex;
89b892a2 1167
7a976304 1168 if ( !IsVertical() )
8a0681f9
VZ
1169 {
1170 if ( m_maxRows == 0 )
8a0681f9
VZ
1171 // if not set yet, only one row
1172 SetRows(1);
8a0681f9
VZ
1173 }
1174 else if ( m_nButtons > 0 ) // vertical non empty toolbar
1175 {
e49426a2
RD
1176 // if not set yet, have one column
1177 m_maxRows = 1;
f4322df6 1178 SetRows(m_nButtons);
8a0681f9 1179 }
2bda0e17 1180
9f884528 1181 InvalidateBestSize();
2091f5e7 1182 UpdateSize();
c6dcd973 1183
bfbb0b4c 1184 return true;
2bda0e17
KB
1185}
1186
cc260109
VZ
1187void wxToolBar::UpdateStretchableSpacersSize()
1188{
8c118ca4
VZ
1189#ifdef TB_SETBUTTONINFO
1190 // we can't resize the spacers if TB_SETBUTTONINFO is not supported (we
1191 // could try to do it with multiple separators as for the controls but this
1192 // is too painful and it just doesn't seem to be worth doing for the
1193 // ancient systems)
cc260109
VZ
1194 if ( wxApp::GetComCtl32Version() < 471 )
1195 return;
1196
1197 // check if we have any stretchable spacers in the first place
1198 unsigned numSpaces = 0;
1199 wxToolBarToolsList::compatibility_iterator node;
1200 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1201 {
1202 wxToolBarTool * const tool = (wxToolBarTool*)node->GetData();
1203 if ( tool->IsStretchableSpace() )
1204 numSpaces++;
1205 }
1206
1207 if ( !numSpaces )
1208 return;
1209
1210 // we do, adjust their size: either distribute the extra size among them or
1211 // reduce their size if there is not enough place for all tools
1212 const int totalSize = IsVertical() ? GetClientSize().y : GetClientSize().x;
1213 const int extraSize = totalSize - m_totalFixedSize;
187839af 1214 const int sizeSpacer = extraSize > 0 ? extraSize / numSpaces : 1;
cc260109
VZ
1215
1216 // the last spacer should consume all remaining space if we have too much
1217 // of it (which can be greater than sizeSpacer because of the rounding)
1218 const int sizeLastSpacer = extraSize > 0
1219 ? extraSize - (numSpaces - 1)*sizeSpacer
187839af 1220 : 1;
cc260109
VZ
1221
1222 // cumulated offset by which we need to move all the following controls to
1223 // the right: while the toolbar takes care of the normal items, we must
1224 // move the controls manually ourselves to ensure they remain at the
1225 // correct place
1226 int offset = 0;
1227 int toolIndex = 0;
1228 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), toolIndex++ )
1229 {
1230 wxToolBarTool * const tool = (wxToolBarTool*)node->GetData();
1231
1232 if ( tool->IsControl() && offset )
1233 {
1234 tool->MoveBy(offset);
1235
1236 continue;
1237 }
1238
1239 if ( !tool->IsStretchableSpace() )
1240 continue;
1241
1242 const RECT rcOld = wxGetTBItemRect(GetHwnd(), toolIndex);
1243
1244 WinStruct<TBBUTTONINFO> tbbi;
1245 tbbi.dwMask = TBIF_SIZE;
1246 tbbi.cx = --numSpaces ? sizeSpacer : sizeLastSpacer;
1247
1248 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO,
1249 tool->GetId(), (LPARAM)&tbbi) )
1250 {
1251 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1252 }
1253 else
1254 {
1255 // we successfully resized this one, move all the controls after it
1256 // by the corresponding amount (may be positive or negative)
1257 offset += tbbi.cx - (rcOld.right - rcOld.left);
1258 }
1259 }
8c118ca4 1260#endif // TB_SETBUTTONINFO
cc260109
VZ
1261}
1262
1c383dba
VZ
1263// ----------------------------------------------------------------------------
1264// message handlers
1265// ----------------------------------------------------------------------------
1266
0edeeb6d 1267bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_)
2bda0e17 1268{
0edeeb6d
VZ
1269 // cast to signed is important as we compare this id with (signed) ints in
1270 // FindById() and without the cast we'd get a positive int from a
1271 // "negative" (i.e. > 32767) WORD
1272 const int id = (signed short)id_;
1273
1274 wxToolBarToolBase *tool = FindById(id);
8a0681f9 1275 if ( !tool )
bfbb0b4c 1276 return false;
1c383dba 1277
1bba1946 1278 bool toggled = false; // just to suppress warnings
6bb7cee4 1279
ed7701bd
VS
1280 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
1281
8a0681f9 1282 if ( tool->CanBeToggled() )
1c383dba 1283 {
6bb7cee4
VZ
1284 toggled = (state & TBSTATE_CHECKED) != 0;
1285
0356c259
VZ
1286 // ignore the event when a radio button is released, as this doesn't
1287 // seem to happen at all, and is handled otherwise
6bb7cee4 1288 if ( tool->GetKind() == wxITEM_RADIO && !toggled )
bfbb0b4c 1289 return true;
1c383dba 1290
6bb7cee4
VZ
1291 tool->Toggle(toggled);
1292 UnToggleRadioGroup(tool);
1293 }
8a0681f9 1294
ed7701bd
VS
1295 // Without the two lines of code below, if the toolbar was repainted during
1296 // OnLeftClick(), then it could end up without the tool bitmap temporarily
1297 // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html).
1298 // The Update() call bellow ensures that this won't happen, by repainting
1299 // invalidated areas of the toolbar immediately.
1300 //
1301 // To complicate matters, the tool would be drawn in depressed state (this
1302 // code is called when mouse button is released, not pressed). That's not
1303 // ideal, having the tool pressed for the duration of OnLeftClick()
1304 // provides the user with useful visual clue that the app is busy reacting
1305 // to the event. So we manually put the tool into pressed state, handle the
1306 // event and then finally restore tool's original state.
1307 ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state | TBSTATE_PRESSED, 0));
1308 Update();
1309
1310 bool allowLeftClick = OnLeftClick(id, toggled);
1311
5f4250ed
VS
1312 // Restore the unpressed state. Enabled/toggled state might have been
1313 // changed since so take care of it.
1314 if (tool->IsEnabled())
1315 state |= TBSTATE_ENABLED;
1316 else
1317 state &= ~TBSTATE_ENABLED;
1318 if (tool->IsToggled())
1319 state |= TBSTATE_CHECKED;
1320 else
1321 state &= ~TBSTATE_CHECKED;
ed7701bd
VS
1322 ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state, 0));
1323
6bb7cee4
VZ
1324 // OnLeftClick() can veto the button state change - for buttons which
1325 // may be toggled only, of couse
ed7701bd 1326 if ( !allowLeftClick && tool->CanBeToggled() )
f3ba93c1 1327 {
6bb7cee4
VZ
1328 // revert back
1329 tool->Toggle(!toggled);
8a0681f9 1330
0356c259 1331 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0));
1c383dba
VZ
1332 }
1333
bfbb0b4c 1334 return true;
2bda0e17
KB
1335}
1336
8a0681f9 1337bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
fd3f686c 1338 WXLPARAM lParam,
33ac7e6f 1339 WXLPARAM *WXUNUSED(result))
2bda0e17 1340{
a9a0ceca
VZ
1341 LPNMHDR hdr = (LPNMHDR)lParam;
1342 if ( hdr->code == TBN_DROPDOWN )
1343 {
1344 LPNMTOOLBAR tbhdr = (LPNMTOOLBAR)lParam;
1345
1346 wxCommandEvent evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED, tbhdr->iItem);
937013e0 1347 if ( HandleWindowEvent(evt) )
a9a0ceca
VZ
1348 {
1349 // Event got handled, don't display default popup menu
1350 return false;
1351 }
1352
1353 const wxToolBarToolBase * const tool = FindById(tbhdr->iItem);
9a83f860 1354 wxCHECK_MSG( tool, false, wxT("drop down message for unknown tool") );
a9a0ceca
VZ
1355
1356 wxMenu * const menu = tool->GetDropdownMenu();
1357 if ( !menu )
1358 return false;
1359
1360 // Display popup menu below button
3c30bbb9
VZ
1361 const RECT r = wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr->iItem));
1362 if ( r.right )
a9a0ceca
VZ
1363 PopupMenu(menu, r.left, r.bottom);
1364
1365 return true;
1366 }
1367
1368
6a1b3ead
VZ
1369 if( !HasFlag(wxTB_NO_TOOLTIPS) )
1370 {
3bce6687 1371#if wxUSE_TOOLTIPS
6a1b3ead 1372 // First check if this applies to us
2bda0e17 1373
6a1b3ead
VZ
1374 // the tooltips control created by the toolbar is sometimes Unicode, even
1375 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1376 UINT code = hdr->code;
1377 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
1378 return false;
2bda0e17 1379
6a1b3ead
VZ
1380 HWND toolTipWnd = (HWND)::SendMessage(GetHwnd(), TB_GETTOOLTIPS, 0, 0);
1381 if ( toolTipWnd != hdr->hwndFrom )
1382 return false;
2bda0e17 1383
6a1b3ead
VZ
1384 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
1385 int id = (int)ttText->hdr.idFrom;
89b892a2 1386
6a1b3ead
VZ
1387 wxToolBarToolBase *tool = FindById(id);
1388 if ( tool )
1389 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
3bce6687 1390#else
6a1b3ead
VZ
1391 wxUnusedVar(lParam);
1392#endif
1393 }
abf912c5 1394
bfbb0b4c 1395 return false;
2bda0e17
KB
1396}
1397
1c383dba 1398// ----------------------------------------------------------------------------
8a0681f9 1399// toolbar geometry
1c383dba
VZ
1400// ----------------------------------------------------------------------------
1401
8a0681f9 1402void wxToolBar::SetToolBitmapSize(const wxSize& size)
2bda0e17 1403{
1c383dba
VZ
1404 wxToolBarBase::SetToolBitmapSize(size);
1405
040d3c2e 1406 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
2bda0e17
KB
1407}
1408
8a0681f9 1409void wxToolBar::SetRows(int nRows)
2bda0e17 1410{
8a0681f9
VZ
1411 if ( nRows == m_maxRows )
1412 {
1413 // avoid resizing the frame uselessly
1414 return;
1415 }
1416
1417 // TRUE in wParam means to create at least as many rows, FALSE -
1418 // at most as many
1c383dba
VZ
1419 RECT rect;
1420 ::SendMessage(GetHwnd(), TB_SETROWS,
8a0681f9
VZ
1421 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
1422 (LPARAM) &rect);
1c383dba
VZ
1423
1424 m_maxRows = nRows;
8a0681f9
VZ
1425
1426 UpdateSize();
1427}
1428
1429// The button size is bigger than the bitmap size
1430wxSize wxToolBar::GetToolSize() const
1431{
1432 // TB_GETBUTTONSIZE is supported from version 4.70
5438a566 1433#if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
50165591
CE
1434 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1435 && !defined (__DIGITALMARS__)
2a1f999f 1436 if ( wxApp::GetComCtl32Version() >= 470 )
8a0681f9
VZ
1437 {
1438 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
1439
1440 return wxSize(LOWORD(dw), HIWORD(dw));
1441 }
1442 else
1443#endif // comctl32.dll 4.70+
1444 {
1445 // defaults
1446 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
1447 }
2bda0e17
KB
1448}
1449
82c9f85c
VZ
1450static
1451wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
1452 size_t index )
a8945eef 1453{
222ed1d6 1454 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
a8945eef 1455
489f6cf7 1456 for ( ; current ; current = current->GetNext() )
a8945eef 1457 {
82c9f85c 1458 if ( index == 0 )
a8945eef 1459 return current->GetData();
82c9f85c
VZ
1460
1461 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
1462 size_t separators = tool->GetSeparatorsCount();
1463
1464 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1465 // otherwise, skip as many items as the separator count, plus the
1466 // control itself
1467 index -= separators ? separators + 1 : 1;
a8945eef
MB
1468 }
1469
1470 return 0;
1471}
1472
8a0681f9 1473wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
2bda0e17 1474{
8a0681f9
VZ
1475 POINT pt;
1476 pt.x = x;
1477 pt.y = y;
1478 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
97071cdb 1479
37d0bdff
MB
1480 // MBN: when the point ( x, y ) is close to the toolbar border
1481 // TB_HITTEST returns m_nButtons ( not -1 )
1482 if ( index < 0 || (size_t)index >= m_nButtons )
8a0681f9 1483 // it's a separator or there is no tool at all there
d3b9f782 1484 return NULL;
1c383dba 1485
a84f66ef
VZ
1486 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1487 // we don't use the dummy separators hack
1488#ifdef TB_SETBUTTONINFO
2a1f999f 1489 if ( wxApp::GetComCtl32Version() >= 471 )
a8945eef
MB
1490 {
1491 return m_tools.Item((size_t)index)->GetData();
1492 }
1493 else
a84f66ef 1494#endif // TB_SETBUTTONINFO
a8945eef
MB
1495 {
1496 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1497 }
2bda0e17
KB
1498}
1499
8a0681f9 1500void wxToolBar::UpdateSize()
2bda0e17 1501{
48a102f9 1502 wxPoint pos = GetPosition();
93489fc8 1503 ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
48a102f9
JS
1504 if (pos != GetPosition())
1505 Move(pos);
93489fc8 1506
b93328d8
RD
1507 // In case Realize is called after the initial display (IOW the programmer
1508 // may have rebuilt the toolbar) give the frame the option of resizing the
1509 // toolbar to full width again, but only if the parent is a frame and the
1510 // toolbar is managed by the frame. Otherwise assume that some other
1511 // layout mechanism is controlling the toolbar size and leave it alone.
0dba08dd 1512 SendSizeEventToParent();
2bda0e17
KB
1513}
1514
c631abda
VZ
1515// ----------------------------------------------------------------------------
1516// toolbar styles
1517// ---------------------------------------------------------------------------
1518
2588b9c4
VZ
1519// get the TBSTYLE of the given toolbar window
1520long wxToolBar::GetMSWToolbarStyle() const
1521{
1522 return ::SendMessage(GetHwnd(), TB_GETSTYLE, 0, 0L);
1523}
1524
c631abda
VZ
1525void wxToolBar::SetWindowStyleFlag(long style)
1526{
24998710
VZ
1527 // the style bits whose changes force us to recreate the toolbar
1528 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
c631abda 1529
24998710 1530 const long styleOld = GetWindowStyle();
c631abda 1531
24998710 1532 wxToolBarBase::SetWindowStyleFlag(style);
c631abda 1533
24998710
VZ
1534 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1535 // also fatal as we'd then try to recreate the toolbar when it's just being
1536 // created
1537 if ( GetToolsCount() &&
1538 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1539 {
1540 // to remove the text labels, simply re-realizing the toolbar is enough
1541 // but I don't know of any way to add the text to an existing toolbar
1542 // other than by recreating it entirely
1543 Recreate();
c631abda 1544 }
c631abda
VZ
1545}
1546
1c383dba
VZ
1547// ----------------------------------------------------------------------------
1548// tool state
1549// ----------------------------------------------------------------------------
1550
8a0681f9 1551void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
2bda0e17 1552{
8a0681f9
VZ
1553 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1554 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
2bda0e17
KB
1555}
1556
8a0681f9 1557void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
2bda0e17 1558{
8a0681f9
VZ
1559 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1560 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
2bda0e17
KB
1561}
1562
33ac7e6f 1563void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
088a95f5 1564{
8a0681f9
VZ
1565 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1566 // without, so we really need to delete the button and recreate it here
9a83f860 1567 wxFAIL_MSG( wxT("not implemented") );
2bda0e17
KB
1568}
1569
bbd321ff
RD
1570void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1571{
5c33522f 1572 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
bbd321ff
RD
1573 if ( tool )
1574 {
1575 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1576
1577 tool->SetNormalBitmap(bitmap);
1578 Realize();
f4322df6 1579 }
bbd321ff
RD
1580}
1581
1582void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1583{
5c33522f 1584 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
bbd321ff
RD
1585 if ( tool )
1586 {
1587 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1588
1589 tool->SetDisabledBitmap(bitmap);
1590 Realize();
f4322df6 1591 }
bbd321ff
RD
1592}
1593
1c383dba
VZ
1594// ----------------------------------------------------------------------------
1595// event handlers
1596// ----------------------------------------------------------------------------
2bda0e17
KB
1597
1598// Responds to colour changes, and passes event on to children.
8a0681f9 1599void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
2bda0e17 1600{
84c5b38d 1601 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
2bda0e17
KB
1602
1603 // Remap the buttons
8a0681f9 1604 Realize();
2bda0e17 1605
56bd6aac
VZ
1606 // Relayout the toolbar
1607 int nrows = m_maxRows;
1608 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1609 SetRows(nrows);
1610
2bda0e17
KB
1611 Refresh();
1612
56bd6aac
VZ
1613 // let the event propagate further
1614 event.Skip();
2bda0e17
KB
1615}
1616
8a0681f9 1617void wxToolBar::OnMouseEvent(wxMouseEvent& event)
e6460682 1618{
6ae2a4b7 1619 if ( event.Leaving() )
fe87983b 1620 {
6ae2a4b7
VZ
1621 if ( m_pInTool )
1622 {
1623 OnMouseEnter(wxID_ANY);
1624 m_pInTool = NULL;
1625 }
1626
fe87983b
MB
1627 event.Skip();
1628 return;
1629 }
1630
2334fef6 1631 if ( event.RightDown() )
e6460682 1632 {
2334fef6 1633 // find the tool under the mouse
8d7eaf91 1634 wxCoord x = 0, y = 0;
2334fef6
VZ
1635 event.GetPosition(&x, &y);
1636
1637 wxToolBarToolBase *tool = FindToolForPosition(x, y);
1638 OnRightClick(tool ? tool->GetId() : -1, x, y);
e6460682
JS
1639 }
1640 else
1641 {
42e69d6b 1642 event.Skip();
e6460682
JS
1643 }
1644}
1645
2eb10e2a 1646bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
bdc72a22 1647{
fd6939a6
VZ
1648 // wait until we have some tools
1649 if ( !GetToolsCount() )
1650 return false;
1651
3bb63e5c 1652 // calculate our minor dimension ourselves - we're confusing the standard
7509fa8c 1653 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
3c30bbb9
VZ
1654 const RECT r = wxGetTBItemRect(GetHwnd(), 0);
1655 if ( !r.right )
1656 return false;
7509fa8c 1657
3c30bbb9
VZ
1658 int w, h;
1659
1660 if ( IsVertical() )
1661 {
1662 w = r.right - r.left;
1663 if ( m_maxRows )
7509fa8c 1664 {
3c30bbb9 1665 w *= (m_nButtons + m_maxRows - 1)/m_maxRows;
7509fa8c 1666 }
3c30bbb9
VZ
1667 h = HIWORD(lParam);
1668 }
1669 else
1670 {
1671 w = LOWORD(lParam);
1672 if (HasFlag( wxTB_FLAT ))
1673 h = r.bottom - r.top - 3;
7509fa8c 1674 else
3c30bbb9
VZ
1675 h = r.bottom - r.top;
1676 if ( m_maxRows )
7509fa8c 1677 {
3c30bbb9
VZ
1678 // FIXME: hardcoded separator line height...
1679 h += HasFlag(wxTB_NODIVIDER) ? 4 : 6;
1680 h *= m_maxRows;
7509fa8c 1681 }
3c30bbb9 1682 }
7509fa8c 1683
3c30bbb9
VZ
1684 if ( MAKELPARAM(w, h) != lParam )
1685 {
1686 // size really changed
1687 SetSize(w, h);
7509fa8c
VZ
1688 }
1689
cc260109
VZ
1690 UpdateStretchableSpacersSize();
1691
3c30bbb9
VZ
1692 // message processed
1693 return true;
7509fa8c
VZ
1694}
1695
bec9bf3e 1696#ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK
cc260109 1697
bec9bf3e 1698bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam)
7509fa8c 1699{
bec9bf3e
VZ
1700 // we must prevent the dummy separators corresponding to controls or
1701 // stretchable spaces from being seen: we used to do it by painting over
1702 // them but this, unsurprisingly, resulted in a lot of flicker so now we
1703 // prevent the toolbar from painting them at all
1704
1705 // compute the region containing all dummy separators which we don't want
1706 // to be seen
1707 wxRegion rgnDummySeps;
1708 const wxRect rectTotal = GetClientRect();
cc260109 1709 int toolIndex = 0;
8c118ca4
VZ
1710 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1711 node;
1712 node = node->GetNext() )
7509fa8c 1713 {
8c118ca4
VZ
1714 wxToolBarTool * const
1715 tool = static_cast<wxToolBarTool *>(node->GetData());
cc260109 1716
8c118ca4
VZ
1717 if ( tool->IsControl() || tool->IsStretchableSpace() )
1718 {
1719 const size_t numSeps = tool->GetSeparatorsCount();
1720 for ( size_t n = 0; n < numSeps; n++, toolIndex++ )
8a0681f9 1721 {
bec9bf3e
VZ
1722 // for some reason TB_GETITEMRECT returns a rectangle 1 pixel
1723 // shorter than the full window size (at least under Windows 7)
1724 // but we need to erase the full height below
1725 RECT rcItem = wxGetTBItemRect(GetHwnd(), toolIndex);
1726 rcItem.top = 0;
1727 rcItem.bottom = rectTotal.height;
1728
1729 rgnDummySeps.Union(wxRectFromRECT(rcItem));
8a0681f9 1730 }
8a0681f9 1731 }
8c118ca4 1732 else
cc260109 1733 {
8c118ca4
VZ
1734 // normal tools never correspond to more than one native button
1735 toolIndex++;
cc260109 1736 }
bdc72a22 1737 }
7509fa8c 1738
f3d0bc43 1739 if ( rgnDummySeps.IsOk() )
bec9bf3e 1740 {
f3d0bc43
VZ
1741 // exclude the area occupied by the controls and stretchable spaces
1742 // from the update region to prevent the toolbar from drawing
1743 // separators in it
1744 if ( !::ValidateRgn(GetHwnd(), GetHrgnOf(rgnDummySeps)) )
1745 {
1746 wxLogLastError(wxT("ValidateRgn()"));
1747 }
bec9bf3e
VZ
1748 }
1749
1750 // still let the native control draw everything else normally but set up a
1751 // hook to be able to process the next WM_ERASEBKGND sent to our parent
1752 // because toolbar will ask it to erase its background from its WM_PAINT
1753 // handler (when using TBSTYLE_TRANSPARENT which we do always use)
1754 //
1755 // installing hook is not completely trivial as all kinds of strange
1756 // situations are possible: sometimes we can be called recursively from
1757 // inside the native toolbar WM_PAINT handler so the hook might already be
1758 // installed and sometimes the native toolbar might not send WM_ERASEBKGND
1759 // to the parent at all for whatever reason, so deal with all these cases
1760 wxWindow * const parent = GetParent();
1761 const bool hadHook = parent->MSWHasEraseBgHook();
1762 if ( !hadHook )
1763 GetParent()->MSWSetEraseBgHook(this);
1764
1765 MSWDefWindowProc(WM_PAINT, wParam, lParam);
1766
1767 if ( !hadHook )
1768 GetParent()->MSWSetEraseBgHook(NULL);
1769
1770
f3d0bc43
VZ
1771 if ( rgnDummySeps.IsOk() )
1772 {
1773 // erase the dummy separators region ourselves now as nobody painted
1774 // over them
1775 WindowHDC hdc(GetHwnd());
1776 ::SelectClipRgn(hdc, GetHrgnOf(rgnDummySeps));
1777 MSWDoEraseBackground(hdc);
1778 }
bec9bf3e
VZ
1779
1780 return true;
1781}
1782
1783WXHBRUSH wxToolBar::MSWGetToolbarBgBrush()
1784{
1785 // we conservatively use a solid brush here but we could also use a themed
1786 // brush by using DrawThemeBackground() to create a bitmap brush (it'd need
1787 // to be invalidated whenever the toolbar is resized and, also, correctly
1788 // aligned using SetBrushOrgEx() before each use -- there is code for doing
1789 // this in wxNotebook already so it'd need to be refactored into wxWindow)
1790 //
1791 // however inasmuch as there is a default background for the toolbar at all
1792 // (and this is not a trivial question as different applications use very
1793 // different colours), it seems to be a solid one and using REBAR
1794 // background brush as we used to do before doesn't look good at all under
1795 // Windows 7 (and probably Vista too), so for now we just keep it simple
1796 wxColour const
1797 colBg = m_hasBgCol ? GetBackgroundColour()
1798 : wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1799 wxBrush * const
1800 brush = wxTheBrushList->FindOrCreateBrush(colBg);
1801
fe42ab1d 1802 return brush ? static_cast<WXHBRUSH>(brush->GetResourceHandle()) : 0;
bec9bf3e
VZ
1803}
1804
1805WXHBRUSH wxToolBar::MSWGetBgBrushForChild(WXHDC hDC, wxWindowMSW *child)
1806{
1807 WXHBRUSH hbr = wxToolBarBase::MSWGetBgBrushForChild(hDC, child);
1808 if ( hbr )
1809 return hbr;
1810
1811 // the base class version only returns a brush for erasing children
1812 // background if we have a non-default background colour but as the toolbar
1813 // doesn't erase its own background by default, we need to always do it for
1814 // (semi-)transparent children
1815 if ( child->GetParent() == this && child->HasTransparentBackground() )
1816 return MSWGetToolbarBgBrush();
1817
1818 return 0;
1819}
1820
1821void wxToolBar::MSWDoEraseBackground(WXHDC hDC)
1822{
1823 wxFillRect(GetHwnd(), (HDC)hDC, (HBRUSH)MSWGetToolbarBgBrush());
1824}
1825
1826bool wxToolBar::MSWEraseBgHook(WXHDC hDC)
1827{
1828 // toolbar WM_PAINT handler offsets the DC origin before sending
1829 // WM_ERASEBKGND to the parent but as we handle it in the toolbar itself,
1830 // we need to reset it back
1831 HDC hdc = (HDC)hDC;
1832 POINT ptOldOrg;
1833 if ( !::SetWindowOrgEx(hdc, 0, 0, &ptOldOrg) )
1834 {
1835 wxLogLastError(wxT("SetWindowOrgEx(tbar-bg-hdc)"));
1836 return false;
1837 }
1838
1839 MSWDoEraseBackground(hDC);
1840
1841 ::SetWindowOrgEx(hdc, ptOldOrg.x, ptOldOrg.y, NULL);
1842
1843 return true;
7509fa8c 1844}
bec9bf3e
VZ
1845
1846#endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK
7509fa8c 1847
2eb10e2a 1848void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
7509fa8c
VZ
1849{
1850 wxCoord x = GET_X_LPARAM(lParam),
1851 y = GET_Y_LPARAM(lParam);
1852 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1853
6ae2a4b7
VZ
1854 // has the current tool changed?
1855 if ( tool != m_pInTool )
7509fa8c
VZ
1856 {
1857 m_pInTool = tool;
6ae2a4b7 1858 OnMouseEnter(tool ? tool->GetId() : wxID_ANY);
7509fa8c
VZ
1859 }
1860}
a8945eef 1861
c140b7e7 1862WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
7509fa8c
VZ
1863{
1864 switch ( nMsg )
1865 {
7509fa8c
VZ
1866 case WM_MOUSEMOVE:
1867 // we don't handle mouse moves, so always pass the message to
4012b958 1868 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
7509fa8c
VZ
1869 HandleMouseMove(wParam, lParam);
1870 break;
a8945eef 1871
4012b958
JS
1872 case WM_SIZE:
1873 if ( HandleSize(wParam, lParam) )
1874 return 0;
1875 break;
1876
bec9bf3e 1877#ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK
7509fa8c 1878 case WM_PAINT:
1c2b921a
VZ
1879 // refreshing the controls in the toolbar inside a composite window
1880 // results in an endless stream of WM_PAINT messages -- and seems
1881 // to be unnecessary anyhow as everything works just fine without
1882 // any special workarounds in this case
1883 if ( !IsDoubleBuffered() && HandlePaint(wParam, lParam) )
7509fa8c 1884 return 0;
97071cdb 1885 break;
bec9bf3e 1886#endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK
a8945eef 1887 }
bdc72a22 1888
8a0681f9 1889 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
bdc72a22
VZ
1890}
1891
1c383dba
VZ
1892// ----------------------------------------------------------------------------
1893// private functions
1894// ----------------------------------------------------------------------------
1895
fb7e87ef
VZ
1896#ifdef wxREMAP_BUTTON_COLOURS
1897
566fb299 1898WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height)
2bda0e17 1899{
566fb299 1900 MemoryHDC hdcMem;
56bd6aac 1901
566fb299 1902 if ( !hdcMem )
5e68f8f3 1903 {
9a83f860 1904 wxLogLastError(wxT("CreateCompatibleDC"));
566fb299
VZ
1905
1906 return bitmap;
5e68f8f3 1907 }
56bd6aac 1908
566fb299 1909 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
56bd6aac 1910
566fb299 1911 if ( !bmpInHDC )
5e68f8f3 1912 {
9a83f860 1913 wxLogLastError(wxT("SelectObject"));
566fb299
VZ
1914
1915 return bitmap;
1916 }
56bd6aac 1917
90c1530a
VZ
1918 wxCOLORMAP *cmap = wxGetStdColourMap();
1919
684c68fd
VZ
1920 for ( int i = 0; i < width; i++ )
1921 {
1922 for ( int j = 0; j < height; j++ )
1923 {
1924 COLORREF pixel = ::GetPixel(hdcMem, i, j);
1925
90c1530a 1926 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
684c68fd 1927 {
90c1530a 1928 COLORREF col = cmap[k].from;
684c68fd
VZ
1929 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
1930 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
1931 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
1932 {
fb7e87ef
VZ
1933 if ( cmap[k].to != pixel )
1934 ::SetPixel(hdcMem, i, j, cmap[k].to);
684c68fd
VZ
1935 break;
1936 }
1937 }
1938 }
1939 }
3a3898f8
VZ
1940
1941 return bitmap;
566fb299 1942}
2bda0e17 1943
fb7e87ef
VZ
1944#endif // wxREMAP_BUTTON_COLOURS
1945
97071cdb 1946#endif // wxUSE_TOOLBAR