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