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