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