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