1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/toolbar.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && !defined(__SMARTPHONE__)
29 #include "wx/toolbar.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/dynarray.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
43 #include "wx/stattext.h"
46 #include "wx/artprov.h"
47 #include "wx/sysopt.h"
48 #include "wx/dcclient.h"
49 #include "wx/scopedarray.h"
51 #include "wx/msw/private.h"
52 #include "wx/msw/dc.h"
55 #include "wx/msw/uxtheme.h"
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
64 #define wxREMAP_BUTTON_COLOURS
65 #endif // !__WXWINCE__
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // these standard constants are not always defined in compilers headers
75 #define TBSTYLE_LIST 0x1000
76 #define TBSTYLE_FLAT 0x0800
79 #ifndef TBSTYLE_TRANSPARENT
80 #define TBSTYLE_TRANSPARENT 0x8000
83 #ifndef TBSTYLE_TOOLTIPS
84 #define TBSTYLE_TOOLTIPS 0x0100
89 #define TB_SETSTYLE (WM_USER + 56)
90 #define TB_GETSTYLE (WM_USER + 57)
94 #define TB_HITTEST (WM_USER + 69)
98 #define TB_GETMAXSIZE (WM_USER + 83)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
117 style ( wxNO_BORDER | wxTB_HORIZONTAL)
126 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
127 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
128 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
129 EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground
)
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 class wxToolBarTool
: public wxToolBarToolBase
139 wxToolBarTool(wxToolBar
*tbar
,
141 const wxString
& label
,
142 const wxBitmap
& bmpNormal
,
143 const wxBitmap
& bmpDisabled
,
145 wxObject
*clientData
,
146 const wxString
& shortHelp
,
147 const wxString
& longHelp
)
148 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
149 clientData
, shortHelp
, longHelp
)
155 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
156 : wxToolBarToolBase(tbar
, control
, label
)
158 if ( IsControl() && !m_label
.empty() )
160 // create a control to render the control's label
161 m_staticText
= new wxStaticText
168 wxALIGN_CENTRE
| wxST_NO_AUTORESIZE
179 virtual ~wxToolBarTool()
184 virtual void SetLabel(const wxString
& label
)
186 if ( label
== m_label
)
189 wxToolBarToolBase::SetLabel(label
);
192 m_staticText
->SetLabel(label
);
194 // we need to update the label shown in the toolbar because it has a
195 // pointer to the internal buffer of the old label
197 // TODO: use TB_SETBUTTONINFO
200 wxStaticText
* GetStaticText()
202 wxASSERT_MSG( IsControl(),
203 _T("only makes sense for embedded control tools") );
208 // set/get the number of separators which we use to cover the space used by
209 // a control in the toolbar
210 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
211 size_t GetSeparatorsCount() const { return m_nSepCount
; }
215 wxStaticText
*m_staticText
;
217 wxDECLARE_NO_COPY_CLASS(wxToolBarTool
);
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
224 // return the rectangle of the item at the given index
226 // returns an empty (0, 0, 0, 0) rectangle if fails so the caller may compare
227 // r.right or r.bottom with 0 to check for this
228 static RECT
wxGetTBItemRect(HWND hwnd
, int index
)
232 // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter
233 // only appeared in v4.70 of comctl32.dll
234 if ( !::SendMessage(hwnd
, TB_GETITEMRECT
, index
, (LPARAM
)&r
) )
236 wxLogLastError(wxT("TB_GETITEMRECT"));
247 // ============================================================================
249 // ============================================================================
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
256 const wxString
& label
,
257 const wxBitmap
& bmpNormal
,
258 const wxBitmap
& bmpDisabled
,
260 wxObject
*clientData
,
261 const wxString
& shortHelp
,
262 const wxString
& longHelp
)
264 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
265 clientData
, shortHelp
, longHelp
);
269 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
271 return new wxToolBarTool(this, control
, label
);
274 // ----------------------------------------------------------------------------
275 // wxToolBar construction
276 // ----------------------------------------------------------------------------
278 void wxToolBar::Init()
281 m_disabledImgList
= NULL
;
285 // even though modern Windows applications typically use 24*24 (or even
286 // 32*32) size for their bitmaps, the native control itself still uses the
287 // old 16*15 default size (see TB_SETBITMAPSIZE documentation in MSDN), so
288 // default to it so that we don't call SetToolBitmapSize() unnecessarily in
289 // AdjustToolBitmapSize()
291 m_defaultHeight
= 15;
296 bool wxToolBar::Create(wxWindow
*parent
,
301 const wxString
& name
)
303 // common initialisation
304 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
309 // MSW-specific initialisation
310 if ( !MSWCreateToolbar(pos
, size
) )
313 wxSetCCUnicodeFormat(GetHwnd());
315 // workaround for flat toolbar on Windows XP classic style: we have to set
316 // the style after creating the control; doing it at creation time doesn't work
318 if ( style
& wxTB_FLAT
)
320 LRESULT style
= GetMSWToolbarStyle();
322 if ( !(style
& TBSTYLE_FLAT
) )
323 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, style
| TBSTYLE_FLAT
);
325 #endif // wxUSE_UXTHEME
330 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
332 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
335 // toolbar-specific post initialisation
336 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
338 #ifdef TB_SETEXTENDEDSTYLE
339 if ( wxApp::GetComCtl32Version() >= 471 )
340 ::SendMessage(GetHwnd(), TB_SETEXTENDEDSTYLE
, 0, TBSTYLE_EX_DRAWDDARROWS
);
346 void wxToolBar::Recreate()
348 const HWND hwndOld
= GetHwnd();
351 // we haven't been created yet, no need to recreate
355 // get the position and size before unsubclassing the old toolbar
356 const wxPoint pos
= GetPosition();
357 const wxSize size
= GetSize();
361 if ( !MSWCreateToolbar(pos
, size
) )
364 wxFAIL_MSG( _T("recreating the toolbar failed") );
369 // reparent all our children under the new toolbar
370 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
372 node
= node
->GetNext() )
374 wxWindow
*win
= node
->GetData();
375 if ( !win
->IsTopLevel() )
376 ::SetParent(GetHwndOf(win
), GetHwnd());
379 // only destroy the old toolbar now --
380 // after all the children had been reparented
381 ::DestroyWindow(hwndOld
);
383 // it is for the old bitmap control and can't be used with the new one
386 ::DeleteObject((HBITMAP
) m_hBitmap
);
390 if ( m_disabledImgList
)
392 delete m_disabledImgList
;
393 m_disabledImgList
= NULL
;
399 wxToolBar::~wxToolBar()
401 // we must refresh the frame size when the toolbar is deleted but the frame
402 // is not - otherwise toolbar leaves a hole in the place it used to occupy
403 SendSizeEventToParent();
406 ::DeleteObject((HBITMAP
) m_hBitmap
);
408 delete m_disabledImgList
;
411 wxSize
wxToolBar::DoGetBestSize() const
416 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
418 // maybe an old (< 0x400) Windows version? try to approximate the
419 // toolbar size ourselves
420 sizeBest
= GetToolSize();
421 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
422 sizeBest
.x
*= GetToolsCount();
424 // reverse horz and vertical components if necessary
428 sizeBest
.x
= sizeBest
.y
;
432 else // TB_GETMAXSIZE succeeded
434 // but it could still return an incorrect result due to what appears to
435 // be a bug in old comctl32.dll versions which don't handle controls in
436 // the toolbar correctly, so work around it (see SF patch 1902358)
437 if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 )
439 // calculate the toolbar width in alternative way
440 const RECT rcFirst
= wxGetTBItemRect(GetHwnd(), 0);
441 const RECT rcLast
= wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1);
443 const int widthAlt
= rcLast
.right
- rcFirst
.left
;
444 if ( widthAlt
> size
.cx
)
448 sizeBest
.x
= size
.cx
;
449 sizeBest
.y
= size
.cy
;
454 // Without the extra height, DoGetBestSize can report a size that's
455 // smaller than the actual window, causing windows to overlap slightly
456 // in some circumstances, leading to missing borders (especially noticeable
458 if (!(GetWindowStyle() & wxTB_NODIVIDER
))
463 CacheBestSize(sizeBest
);
468 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
470 // toolbars never have border, giving one to them results in broken
472 WXDWORD msStyle
= wxControl::MSWGetStyle
474 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
477 if ( !(style
& wxTB_NO_TOOLTIPS
) )
478 msStyle
|= TBSTYLE_TOOLTIPS
;
480 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
482 // static as it doesn't change during the program lifetime
483 static const int s_verComCtl
= wxApp::GetComCtl32Version();
485 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
486 // style with 6.00 (part of Windows XP) leads to the toolbar with
487 // incorrect background colour - and not using it still results in the
488 // correct (flat) toolbar, so don't use it there
489 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
490 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
492 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
493 msStyle
|= TBSTYLE_LIST
;
496 if ( style
& wxTB_NODIVIDER
)
497 msStyle
|= CCS_NODIVIDER
;
499 if ( style
& wxTB_NOALIGN
)
500 msStyle
|= CCS_NOPARENTALIGN
;
502 if ( style
& wxTB_VERTICAL
)
505 if( style
& wxTB_BOTTOM
)
506 msStyle
|= CCS_BOTTOM
;
508 if ( style
& wxTB_RIGHT
)
509 msStyle
|= CCS_RIGHT
;
514 // ----------------------------------------------------------------------------
515 // adding/removing tools
516 // ----------------------------------------------------------------------------
518 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
519 wxToolBarToolBase
* WXUNUSED(tool
))
521 // nothing special to do here - we really create the toolbar buttons in
523 InvalidateBestSize();
527 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
529 // the main difficulty we have here is with the controls in the toolbars:
530 // as we (sometimes) use several separators to cover up the space used by
531 // them, the indices are not the same for us and the toolbar
533 // first determine the position of the first button to delete: it may be
534 // different from pos if we use several separators to cover the space used
536 wxToolBarToolsList::compatibility_iterator node
;
537 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
539 wxToolBarToolBase
*tool2
= node
->GetData();
542 // let node point to the next node in the list
543 node
= node
->GetNext();
548 if ( tool2
->IsControl() )
549 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
552 // now determine the number of buttons to delete and the area taken by them
553 size_t nButtonsToDelete
= 1;
555 // get the size of the button we're going to delete
556 const RECT r
= wxGetTBItemRect(GetHwnd(), pos
);
558 int width
= r
.right
- r
.left
;
560 if ( tool
->IsControl() )
562 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
563 width
*= nButtonsToDelete
;
566 // do delete all buttons
567 m_nButtons
-= nButtonsToDelete
;
568 while ( nButtonsToDelete
-- > 0 )
570 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
572 wxLogLastError(wxT("TB_DELETEBUTTON"));
578 // and finally reposition all the controls after this button (the toolbar
579 // takes care of all normal items)
580 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
582 wxToolBarTool
*tool2
= (wxToolBarTool
*)node
->GetData();
583 if ( tool2
->IsControl() )
585 wxControl
* const control
= tool2
->GetControl();
588 control
->GetPosition(&x
, NULL
);
589 control
->Move(x
- width
, wxDefaultCoord
);
591 wxStaticText
* const staticText
= tool2
->GetStaticText();
593 staticText
->Move(x
- width
, wxDefaultCoord
);
597 InvalidateBestSize();
602 void wxToolBar::CreateDisabledImageList()
604 if (m_disabledImgList
!= NULL
)
606 delete m_disabledImgList
;
607 m_disabledImgList
= NULL
;
610 // as we can't use disabled image list with older versions of comctl32.dll,
611 // don't even bother creating it
612 if ( wxApp::GetComCtl32Version() >= 470 )
614 // search for the first disabled button img in the toolbar, if any
615 for ( wxToolBarToolsList::compatibility_iterator
616 node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
618 wxToolBarToolBase
*tool
= node
->GetData();
619 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
620 if ( bmpDisabled
.Ok() )
622 m_disabledImgList
= new wxImageList
626 bmpDisabled
.GetMask() != NULL
,
633 // we don't have any disabled bitmaps
637 void wxToolBar::AdjustToolBitmapSize()
639 const wxSize
sizeOrig(m_defaultWidth
, m_defaultHeight
);
641 wxSize
sizeActual(sizeOrig
);
643 for ( wxToolBarToolsList::const_iterator i
= m_tools
.begin();
647 const wxBitmap
& bmp
= (*i
)->GetNormalBitmap();
648 sizeActual
.IncTo(bmp
.GetSize());
651 if ( sizeActual
!= sizeOrig
)
652 SetToolBitmapSize(sizeActual
);
655 bool wxToolBar::Realize()
657 const size_t nTools
= GetToolsCount();
662 // make sure tool size is larger enough for all all bitmaps to fit in
663 // (this is consistent with what other ports do):
664 AdjustToolBitmapSize();
666 #ifdef wxREMAP_BUTTON_COLOURS
667 // don't change the values of these constants, they can be set from the
668 // user code via wxSystemOptions
677 // the user-specified option overrides anything, but if it wasn't set, only
678 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
679 // much worse after remapping
680 static const wxChar
*remapOption
= wxT("msw.remap");
681 const int remapValue
= wxSystemOptions::HasOption(remapOption
)
682 ? wxSystemOptions::GetOptionInt(remapOption
)
683 : wxDisplayDepth() <= 8 ? Remap_Buttons
686 #endif // wxREMAP_BUTTON_COLOURS
688 // delete all old buttons, if any
689 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
691 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
693 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
697 // First, add the bitmap: we use one bitmap for all toolbar buttons
698 // ----------------------------------------------------------------
700 wxToolBarToolsList::compatibility_iterator node
;
703 if ( !HasFlag(wxTB_NOICONS
) )
705 // if we already have a bitmap, we'll replace the existing one --
706 // otherwise we'll install a new one
707 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
709 const wxCoord totalBitmapWidth
= m_defaultWidth
*
710 wx_truncate_cast(wxCoord
, nTools
),
711 totalBitmapHeight
= m_defaultHeight
;
713 // Create a bitmap and copy all the tool bitmaps into it
714 wxMemoryDC dcAllButtons
;
715 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
716 dcAllButtons
.SelectObject(bitmap
);
718 #ifdef wxREMAP_BUTTON_COLOURS
719 if ( remapValue
!= Remap_TransparentBg
)
720 #endif // wxREMAP_BUTTON_COLOURS
722 // VZ: why do we hardcode grey colour for CE?
723 dcAllButtons
.SetBackground(wxBrush(
725 wxColour(0xc0, 0xc0, 0xc0)
726 #else // !__WXWINCE__
727 GetBackgroundColour()
728 #endif // __WXWINCE__/!__WXWINCE__
730 dcAllButtons
.Clear();
733 m_hBitmap
= bitmap
.GetHBITMAP();
734 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
736 #ifdef wxREMAP_BUTTON_COLOURS
737 if ( remapValue
== Remap_Bg
)
739 dcAllButtons
.SelectObject(wxNullBitmap
);
741 // Even if we're not remapping the bitmap
742 // content, we still have to remap the background.
743 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
744 totalBitmapWidth
, totalBitmapHeight
);
746 dcAllButtons
.SelectObject(bitmap
);
748 #endif // wxREMAP_BUTTON_COLOURS
750 // the button position
753 // the number of buttons (not separators)
756 CreateDisabledImageList();
757 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
759 wxToolBarToolBase
*tool
= node
->GetData();
760 if ( tool
->IsButton() )
762 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
764 const int w
= bmp
.GetWidth();
765 const int h
= bmp
.GetHeight();
769 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
770 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
772 // notice the last parameter: do use mask
773 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
777 wxFAIL_MSG( _T("invalid tool button bitmap") );
780 // also deal with disabled bitmap if we want to use them
781 if ( m_disabledImgList
)
783 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
784 #if wxUSE_IMAGE && wxUSE_WXDIB
785 if ( !bmpDisabled
.Ok() )
787 // no disabled bitmap specified but we still need to
788 // fill the space in the image list with something, so
789 // we grey out the normal bitmap
791 imgGreyed
= bmp
.ConvertToImage().ConvertToGreyscale();
793 #ifdef wxREMAP_BUTTON_COLOURS
794 if ( remapValue
== Remap_Buttons
)
796 // we need to have light grey background colour for
797 // MapBitmap() to work correctly
798 for ( int y
= 0; y
< h
; y
++ )
800 for ( int x
= 0; x
< w
; x
++ )
802 if ( imgGreyed
.IsTransparent(x
, y
) )
803 imgGreyed
.SetRGB(x
, y
,
805 wxLIGHT_GREY
->Green(),
806 wxLIGHT_GREY
->Blue());
810 #endif // wxREMAP_BUTTON_COLOURS
812 bmpDisabled
= wxBitmap(imgGreyed
);
814 #endif // wxUSE_IMAGE
816 #ifdef wxREMAP_BUTTON_COLOURS
817 if ( remapValue
== Remap_Buttons
)
818 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
819 #endif // wxREMAP_BUTTON_COLOURS
821 m_disabledImgList
->Add(bmpDisabled
);
824 // still inc width and number of buttons because otherwise the
825 // subsequent buttons will all be shifted which is rather confusing
826 // (and like this you'd see immediately which bitmap was bad)
832 dcAllButtons
.SelectObject(wxNullBitmap
);
834 // don't delete this HBITMAP!
835 bitmap
.SetHBITMAP(0);
837 #ifdef wxREMAP_BUTTON_COLOURS
838 if ( remapValue
== Remap_Buttons
)
840 // Map to system colours
841 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
842 totalBitmapWidth
, totalBitmapHeight
);
844 #endif // wxREMAP_BUTTON_COLOURS
846 bool addBitmap
= true;
848 if ( oldToolBarBitmap
)
850 #ifdef TB_REPLACEBITMAP
851 if ( wxApp::GetComCtl32Version() >= 400 )
853 TBREPLACEBITMAP replaceBitmap
;
854 replaceBitmap
.hInstOld
= NULL
;
855 replaceBitmap
.hInstNew
= NULL
;
856 replaceBitmap
.nIDOld
= (UINT_PTR
)oldToolBarBitmap
;
857 replaceBitmap
.nIDNew
= (UINT_PTR
)hBitmap
;
858 replaceBitmap
.nButtons
= nButtons
;
859 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
860 0, (LPARAM
) &replaceBitmap
) )
862 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
865 ::DeleteObject(oldToolBarBitmap
);
871 #endif // TB_REPLACEBITMAP
873 // we can't replace the old bitmap, so we will add another one
874 // (awfully inefficient, but what else to do?) and shift the bitmap
875 // indices accordingly
878 bitmapId
= m_nButtons
;
882 if ( addBitmap
) // no old bitmap or we can't replace it
884 TBADDBITMAP addBitmap
;
886 addBitmap
.nID
= (UINT_PTR
)hBitmap
;
887 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
888 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
890 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
894 // disable image lists are only supported in comctl32.dll 4.70+
895 if ( wxApp::GetComCtl32Version() >= 470 )
897 HIMAGELIST hil
= m_disabledImgList
898 ? GetHimagelistOf(m_disabledImgList
)
901 // notice that we set the image list even if don't have one right
902 // now as we could have it before and need to reset it in this case
903 HIMAGELIST oldImageList
= (HIMAGELIST
)
904 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST
, 0, (LPARAM
)hil
);
906 // delete previous image list if any
908 ::DeleteObject(oldImageList
);
913 // Next add the buttons and separators
914 // -----------------------------------
916 wxScopedArray
<TBBUTTON
> buttons(new TBBUTTON
[nTools
]);
918 // this array will hold the indices of all controls in the toolbar
919 wxArrayInt controlIds
;
921 bool lastWasRadio
= false;
923 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
925 wxToolBarToolBase
*tool
= node
->GetData();
927 // don't add separators to the vertical toolbar with old comctl32.dll
928 // versions as they didn't handle this properly
929 if ( IsVertical() && tool
->IsSeparator() &&
930 wxApp::GetComCtl32Version() <= 472 )
935 TBBUTTON
& button
= buttons
[i
];
937 wxZeroMemory(button
);
939 bool isRadio
= false;
940 switch ( tool
->GetStyle() )
942 case wxTOOL_STYLE_CONTROL
:
943 button
.idCommand
= tool
->GetId();
944 // fall through: create just a separator too
946 case wxTOOL_STYLE_SEPARATOR
:
947 button
.fsState
= TBSTATE_ENABLED
;
948 button
.fsStyle
= TBSTYLE_SEP
;
951 case wxTOOL_STYLE_BUTTON
:
952 if ( !HasFlag(wxTB_NOICONS
) )
953 button
.iBitmap
= bitmapId
;
955 if ( HasFlag(wxTB_TEXT
) )
957 const wxString
& label
= tool
->GetLabel();
958 if ( !label
.empty() )
959 button
.iString
= (INT_PTR
)label
.wx_str();
962 button
.idCommand
= tool
->GetId();
964 if ( tool
->IsEnabled() )
965 button
.fsState
|= TBSTATE_ENABLED
;
966 if ( tool
->IsToggled() )
967 button
.fsState
|= TBSTATE_CHECKED
;
969 switch ( tool
->GetKind() )
972 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
976 // the first item in the radio group is checked by
977 // default to be consistent with wxGTK and the menu
979 button
.fsState
|= TBSTATE_CHECKED
;
981 if (tool
->Toggle(true))
983 DoToggleTool(tool
, true);
986 else if ( tool
->IsToggled() )
988 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
989 int prevIndex
= i
- 1;
992 TBBUTTON
& prevButton
= buttons
[prevIndex
];
993 wxToolBarToolBase
*tool
= nodePrev
->GetData();
994 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
997 if ( tool
->Toggle(false) )
998 DoToggleTool(tool
, false);
1000 prevButton
.fsState
&= ~TBSTATE_CHECKED
;
1001 nodePrev
= nodePrev
->GetPrevious();
1010 button
.fsStyle
= TBSTYLE_CHECK
;
1014 button
.fsStyle
= TBSTYLE_BUTTON
;
1017 case wxITEM_DROPDOWN
:
1018 button
.fsStyle
= TBSTYLE_DROPDOWN
;
1022 wxFAIL_MSG( _T("unexpected toolbar button kind") );
1023 button
.fsStyle
= TBSTYLE_BUTTON
;
1031 lastWasRadio
= isRadio
;
1036 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, i
, (LPARAM
)buttons
.get()) )
1038 wxLogLastError(wxT("TB_ADDBUTTONS"));
1042 // Deal with the controls finally
1043 // ------------------------------
1045 // adjust the controls size to fit nicely in the toolbar
1048 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
1050 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1052 // we calculate the running y coord for vertical toolbars so we need to
1053 // get the items size for all items but for the horizontal ones we
1054 // don't need to deal with the non controls
1055 bool isControl
= tool
->IsControl();
1056 if ( !isControl
&& !IsVertical() )
1059 const RECT r
= wxGetTBItemRect(GetHwnd(), index
);
1062 // can only be control if isVertical
1063 y
+= r
.bottom
- r
.top
;
1068 wxControl
*control
= tool
->GetControl();
1069 wxStaticText
* const staticText
= tool
->GetStaticText();
1071 wxSize size
= control
->GetSize();
1072 wxSize staticTextSize
;
1075 staticTextSize
= staticText
->GetSize();
1076 staticTextSize
.y
+= 3; // margin between control and its label
1079 // the position of the leftmost controls corner
1080 int left
= wxDefaultCoord
;
1082 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
1083 #ifdef TB_SETBUTTONINFO
1084 // available in headers, now check whether it is available now
1085 // (during run-time)
1086 if ( wxApp::GetComCtl32Version() >= 471 )
1088 // set the (underlying) separators width to be that of the
1091 tbbi
.cbSize
= sizeof(tbbi
);
1092 tbbi
.dwMask
= TBIF_SIZE
;
1093 tbbi
.cx
= (WORD
)size
.x
;
1094 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
1095 tool
->GetId(), (LPARAM
)&tbbi
) )
1097 // the id is probably invalid?
1098 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1102 #endif // comctl32.dll 4.71
1103 // TB_SETBUTTONINFO unavailable
1105 // try adding several separators to fit the controls width
1106 int widthSep
= r
.right
- r
.left
;
1112 tbb
.fsState
= TBSTATE_ENABLED
;
1113 tbb
.fsStyle
= TBSTYLE_SEP
;
1115 size_t nSeparators
= size
.x
/ widthSep
;
1116 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
1118 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
1119 index
, (LPARAM
)&tbb
) )
1121 wxLogLastError(wxT("TB_INSERTBUTTON"));
1127 // remember the number of separators we used - we'd have to
1128 // delete all of them later
1129 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
1131 // adjust the controls width to exactly cover the separators
1132 size
.x
= (nSeparators
+ 1)*widthSep
;
1133 control
->SetSize(size
.x
, wxDefaultCoord
);
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
;
1140 int diff
= height
- size
.y
;
1141 if ( diff
< 0 || !HasFlag(wxTB_TEXT
) )
1143 // not enough room for the static text
1147 // recalculate height & diff without the staticText control
1148 height
= r
.bottom
- r
.top
;
1149 diff
= height
- size
.y
;
1152 // the control is too high, resize to fit
1153 control
->SetSize(wxDefaultCoord
, height
- 2);
1158 else // enough space for both the control and the label
1170 y
+= height
+ 2 * GetMargins().y
;
1172 else // horizontal toolbar
1174 if ( left
== wxDefaultCoord
)
1180 control
->Move(left
, top
+ (diff
+ 1) / 2);
1183 staticText
->Move(left
+ (size
.x
- staticTextSize
.x
)/2,
1184 r
.bottom
- staticTextSize
.y
);
1188 // the max index is the "real" number of buttons - i.e. counting even the
1189 // separators which we added just for aligning the controls
1192 if ( !IsVertical() )
1194 if ( m_maxRows
== 0 )
1195 // if not set yet, only one row
1198 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1200 // if not set yet, have one column
1202 SetRows(m_nButtons
);
1205 InvalidateBestSize();
1211 // ----------------------------------------------------------------------------
1213 // ----------------------------------------------------------------------------
1215 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id_
)
1217 // cast to signed is important as we compare this id with (signed) ints in
1218 // FindById() and without the cast we'd get a positive int from a
1219 // "negative" (i.e. > 32767) WORD
1220 const int id
= (signed short)id_
;
1222 wxToolBarToolBase
*tool
= FindById(id
);
1226 bool toggled
= false; // just to suppress warnings
1228 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1230 if ( tool
->CanBeToggled() )
1232 toggled
= (state
& TBSTATE_CHECKED
) != 0;
1234 // ignore the event when a radio button is released, as this doesn't
1235 // seem to happen at all, and is handled otherwise
1236 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
1239 tool
->Toggle(toggled
);
1240 UnToggleRadioGroup(tool
);
1243 // Without the two lines of code below, if the toolbar was repainted during
1244 // OnLeftClick(), then it could end up without the tool bitmap temporarily
1245 // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html).
1246 // The Update() call bellow ensures that this won't happen, by repainting
1247 // invalidated areas of the toolbar immediately.
1249 // To complicate matters, the tool would be drawn in depressed state (this
1250 // code is called when mouse button is released, not pressed). That's not
1251 // ideal, having the tool pressed for the duration of OnLeftClick()
1252 // provides the user with useful visual clue that the app is busy reacting
1253 // to the event. So we manually put the tool into pressed state, handle the
1254 // event and then finally restore tool's original state.
1255 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
| TBSTATE_PRESSED
, 0));
1258 bool allowLeftClick
= OnLeftClick(id
, toggled
);
1260 // Restore the unpressed state. Enabled/toggled state might have been
1261 // changed since so take care of it.
1262 if (tool
->IsEnabled())
1263 state
|= TBSTATE_ENABLED
;
1265 state
&= ~TBSTATE_ENABLED
;
1266 if (tool
->IsToggled())
1267 state
|= TBSTATE_CHECKED
;
1269 state
&= ~TBSTATE_CHECKED
;
1270 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
, 0));
1272 // OnLeftClick() can veto the button state change - for buttons which
1273 // may be toggled only, of couse
1274 if ( !allowLeftClick
&& tool
->CanBeToggled() )
1277 tool
->Toggle(!toggled
);
1279 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1285 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1287 WXLPARAM
*WXUNUSED(result
))
1289 LPNMHDR hdr
= (LPNMHDR
)lParam
;
1290 if ( hdr
->code
== TBN_DROPDOWN
)
1292 LPNMTOOLBAR tbhdr
= (LPNMTOOLBAR
)lParam
;
1294 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tbhdr
->iItem
);
1295 if ( HandleWindowEvent(evt
) )
1297 // Event got handled, don't display default popup menu
1301 const wxToolBarToolBase
* const tool
= FindById(tbhdr
->iItem
);
1302 wxCHECK_MSG( tool
, false, _T("drop down message for unknown tool") );
1304 wxMenu
* const menu
= tool
->GetDropdownMenu();
1308 // Display popup menu below button
1309 const RECT r
= wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr
->iItem
));
1311 PopupMenu(menu
, r
.left
, r
.bottom
);
1317 if( !HasFlag(wxTB_NO_TOOLTIPS
) )
1320 // First check if this applies to us
1322 // the tooltips control created by the toolbar is sometimes Unicode, even
1323 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1324 UINT code
= hdr
->code
;
1325 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1328 HWND toolTipWnd
= (HWND
)::SendMessage(GetHwnd(), TB_GETTOOLTIPS
, 0, 0);
1329 if ( toolTipWnd
!= hdr
->hwndFrom
)
1332 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1333 int id
= (int)ttText
->hdr
.idFrom
;
1335 wxToolBarToolBase
*tool
= FindById(id
);
1337 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1339 wxUnusedVar(lParam
);
1346 // ----------------------------------------------------------------------------
1348 // ----------------------------------------------------------------------------
1350 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1352 wxToolBarBase::SetToolBitmapSize(size
);
1354 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1357 void wxToolBar::SetRows(int nRows
)
1359 if ( nRows
== m_maxRows
)
1361 // avoid resizing the frame uselessly
1365 // TRUE in wParam means to create at least as many rows, FALSE -
1368 ::SendMessage(GetHwnd(), TB_SETROWS
,
1369 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1377 // The button size is bigger than the bitmap size
1378 wxSize
wxToolBar::GetToolSize() const
1380 // TB_GETBUTTONSIZE is supported from version 4.70
1381 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1382 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1383 && !defined (__DIGITALMARS__)
1384 if ( wxApp::GetComCtl32Version() >= 470 )
1386 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1388 return wxSize(LOWORD(dw
), HIWORD(dw
));
1391 #endif // comctl32.dll 4.70+
1394 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1399 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1402 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1404 for ( ; current
; current
= current
->GetNext() )
1407 return current
->GetData();
1409 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1410 size_t separators
= tool
->GetSeparatorsCount();
1412 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1413 // otherwise, skip as many items as the separator count, plus the
1415 index
-= separators
? separators
+ 1 : 1;
1421 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1426 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1428 // MBN: when the point ( x, y ) is close to the toolbar border
1429 // TB_HITTEST returns m_nButtons ( not -1 )
1430 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1431 // it's a separator or there is no tool at all there
1434 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1435 // we don't use the dummy separators hack
1436 #ifdef TB_SETBUTTONINFO
1437 if ( wxApp::GetComCtl32Version() >= 471 )
1439 return m_tools
.Item((size_t)index
)->GetData();
1442 #endif // TB_SETBUTTONINFO
1444 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1448 void wxToolBar::UpdateSize()
1450 wxPoint pos
= GetPosition();
1451 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1452 if (pos
!= GetPosition())
1455 // In case Realize is called after the initial display (IOW the programmer
1456 // may have rebuilt the toolbar) give the frame the option of resizing the
1457 // toolbar to full width again, but only if the parent is a frame and the
1458 // toolbar is managed by the frame. Otherwise assume that some other
1459 // layout mechanism is controlling the toolbar size and leave it alone.
1460 SendSizeEventToParent();
1463 // ----------------------------------------------------------------------------
1465 // ---------------------------------------------------------------------------
1467 // get the TBSTYLE of the given toolbar window
1468 long wxToolBar::GetMSWToolbarStyle() const
1470 return ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
1473 void wxToolBar::SetWindowStyleFlag(long style
)
1475 // the style bits whose changes force us to recreate the toolbar
1476 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1478 const long styleOld
= GetWindowStyle();
1480 wxToolBarBase::SetWindowStyleFlag(style
);
1482 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1483 // also fatal as we'd then try to recreate the toolbar when it's just being
1485 if ( GetToolsCount() &&
1486 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1488 // to remove the text labels, simply re-realizing the toolbar is enough
1489 // but I don't know of any way to add the text to an existing toolbar
1490 // other than by recreating it entirely
1495 // ----------------------------------------------------------------------------
1497 // ----------------------------------------------------------------------------
1499 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1501 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1502 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1505 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1507 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1508 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1511 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1513 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1514 // without, so we really need to delete the button and recreate it here
1515 wxFAIL_MSG( _T("not implemented") );
1518 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1520 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1523 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1525 tool
->SetNormalBitmap(bitmap
);
1530 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1532 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1535 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1537 tool
->SetDisabledBitmap(bitmap
);
1542 // ----------------------------------------------------------------------------
1544 // ----------------------------------------------------------------------------
1546 // Responds to colour changes, and passes event on to children.
1547 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1549 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1551 // Remap the buttons
1554 // Relayout the toolbar
1555 int nrows
= m_maxRows
;
1556 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1561 // let the event propagate further
1565 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1567 if ( event
.Leaving() )
1571 OnMouseEnter(wxID_ANY
);
1579 if ( event
.RightDown() )
1581 // find the tool under the mouse
1582 wxCoord x
= 0, y
= 0;
1583 event
.GetPosition(&x
, &y
);
1585 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1586 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1594 // This handler is required to allow the toolbar to be set to a non-default
1595 // colour: for example, when it must blend in with a notebook page.
1596 void wxToolBar::OnEraseBackground(wxEraseEvent
& event
)
1598 RECT rect
= wxGetClientRect(GetHwnd());
1600 wxDC
*dc
= event
.GetDC();
1602 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
->GetImpl();
1603 HDC hdc
= GetHdcOf(*impl
);
1605 int majorVersion
, minorVersion
;
1606 wxGetOsVersion(& majorVersion
, & minorVersion
);
1609 // we may need to draw themed colour so that we appear correctly on
1610 // e.g. notebook page under XP with themes but only do it if the parent
1611 // draws themed background itself
1612 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1614 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1618 hr
= theme
->DrawThemeParentBackground(GetHwnd(), hdc
, &rect
);
1622 // it can also return S_FALSE which seems to simply say that it
1623 // didn't draw anything but no error really occurred
1625 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1629 // Only draw a rebar theme on Vista, since it doesn't jive so well with XP
1630 if ( !UseBgCol() && majorVersion
>= 6 )
1632 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1635 wxUxThemeHandle
hTheme(this, L
"REBAR");
1638 wxRect rect
= GetClientRect();
1639 wxCopyRectToRECT(rect
, r
);
1641 HRESULT hr
= theme
->DrawThemeBackground(hTheme
, hdc
, 0, 0, & r
, NULL
);
1645 // it can also return S_FALSE which seems to simply say that it
1646 // didn't draw anything but no error really occurred
1648 wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr
);
1652 #endif // wxUSE_UXTHEME
1654 // we need to always draw our background under XP, as otherwise it doesn't
1655 // appear correctly with some themes (e.g. Zune one)
1656 if ( majorVersion
== 5 ||
1657 UseBgCol() || (GetMSWToolbarStyle() & TBSTYLE_TRANSPARENT
) )
1659 // do draw our background
1661 // notice that this 'dumb' implementation may cause flicker for some of
1662 // the controls in which case they should intercept wxEraseEvent and
1663 // process it themselves somehow
1664 AutoHBRUSH
hBrush(wxColourToRGB(GetBackgroundColour()));
1666 wxCHANGE_HDC_MAP_MODE(hdc
, MM_TEXT
);
1667 ::FillRect(hdc
, &rect
, hBrush
);
1669 else // we have no non default background colour
1671 // let the system do it for us
1676 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1678 // wait until we have some tools
1679 if ( !GetToolsCount() )
1682 // calculate our minor dimension ourselves - we're confusing the standard
1683 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1684 const RECT r
= wxGetTBItemRect(GetHwnd(), 0);
1692 w
= r
.right
- r
.left
;
1695 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1702 if (HasFlag( wxTB_FLAT
))
1703 h
= r
.bottom
- r
.top
- 3;
1705 h
= r
.bottom
- r
.top
;
1708 // FIXME: hardcoded separator line height...
1709 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1714 if ( MAKELPARAM(w
, h
) != lParam
)
1716 // size really changed
1720 // message processed
1725 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1727 // erase any dummy separators which were used
1728 // for aligning the controls if any here
1730 // first of all, are there any controls at all?
1731 wxToolBarToolsList::compatibility_iterator node
;
1732 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1734 if ( node
->GetData()->IsControl() )
1739 // no controls, nothing to erase
1742 wxSize clientSize
= GetClientSize();
1743 int majorVersion
, minorVersion
;
1744 wxGetOsVersion(& majorVersion
, & minorVersion
);
1746 // prepare the DC on which we'll be drawing
1747 wxClientDC
dc(this);
1748 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1749 dc
.SetPen(*wxTRANSPARENT_PEN
);
1752 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1753 // nothing to redraw anyhow
1757 wxCopyRECTToRect(r
, rectUpdate
);
1759 dc
.SetClippingRegion(rectUpdate
);
1761 // draw the toolbar tools, separators &c normally
1762 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1764 // for each control in the toolbar find all the separators intersecting it
1767 // NB: this is really the only way to do it as we don't know if a separator
1768 // corresponds to a control (i.e. is a dummy one) or a real one
1770 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1772 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1773 if ( tool
->IsControl() )
1775 // get the control rect in our client coords
1776 wxControl
*control
= tool
->GetControl();
1777 wxStaticText
*staticText
= tool
->GetStaticText();
1778 wxRect rectCtrl
= control
->GetRect();
1779 wxRect
rectStaticText(0,0,0,0);
1782 rectStaticText
= staticText
->GetRect();
1785 // iterate over all buttons
1787 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1788 for ( int n
= 0; n
< count
; n
++ )
1790 // is it a separator?
1791 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1794 wxLogDebug(_T("TB_GETBUTTON failed?"));
1799 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1802 // get the bounding rect of the separator
1803 RECT r
= wxGetTBItemRect(GetHwnd(), n
);
1807 // does it intersect the control?
1809 wxCopyRECTToRect(r
, rectItem
);
1810 if ( rectCtrl
.Intersects(rectItem
) || (staticText
&& rectStaticText
.Intersects(rectItem
)))
1812 // yes, do erase it!
1814 bool haveRefreshed
= false;
1817 if ( !UseBgCol() && !GetParent()->UseBgCol() )
1819 // Don't use DrawThemeBackground
1821 else if ( !UseBgCol() && majorVersion
>= 6 )
1823 wxUxThemeEngine
*theme
= wxUxThemeEngine::GetIfActive();
1826 wxUxThemeHandle
hTheme(this, L
"REBAR");
1830 // Draw the whole background since the pattern may be position sensitive;
1831 // but clip it to the area of interest.
1833 r
.right
= clientSize
.x
;
1835 r
.bottom
= clientSize
.y
;
1837 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
1838 HRESULT hr
= theme
->DrawThemeBackground(hTheme
, GetHdcOf(*impl
), 0, 0, & r
, & clipRect
);
1840 haveRefreshed
= true;
1843 #endif // wxUSE_UXTHEME
1846 dc
.DrawRectangle(rectItem
);
1849 if ( rectCtrl
.Intersects(rectItem
) )
1851 // Necessary in case we use a no-paint-on-size
1852 // style in the parent: the controls can disappear
1853 control
->Refresh(false);
1856 if ( staticText
&& rectStaticText
.Intersects(rectItem
) )
1858 // Necessary in case we use a no-paint-on-size
1859 // style in the parent: the controls can disappear
1860 staticText
->Refresh(false);
1868 #endif // __WXWINCE__
1870 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1872 wxCoord x
= GET_X_LPARAM(lParam
),
1873 y
= GET_Y_LPARAM(lParam
);
1874 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1876 // has the current tool changed?
1877 if ( tool
!= m_pInTool
)
1880 OnMouseEnter(tool
? tool
->GetId() : wxID_ANY
);
1884 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1889 // we don't handle mouse moves, so always pass the message to
1890 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1891 HandleMouseMove(wParam
, lParam
);
1895 if ( HandleSize(wParam
, lParam
) )
1901 // refreshing the controls in the toolbar inside a composite window
1902 // results in an endless stream of WM_PAINT messages -- and seems
1903 // to be unnecessary anyhow as everything works just fine without
1904 // any special workarounds in this case
1905 if ( !IsDoubleBuffered() && HandlePaint(wParam
, lParam
) )
1908 #endif // __WXWINCE__
1911 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1914 // ----------------------------------------------------------------------------
1915 // private functions
1916 // ----------------------------------------------------------------------------
1918 #ifdef wxREMAP_BUTTON_COLOURS
1920 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1926 wxLogLastError(_T("CreateCompatibleDC"));
1931 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1935 wxLogLastError(_T("SelectObject"));
1940 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1942 for ( int i
= 0; i
< width
; i
++ )
1944 for ( int j
= 0; j
< height
; j
++ )
1946 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1948 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1950 COLORREF col
= cmap
[k
].from
;
1951 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1952 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1953 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1955 if ( cmap
[k
].to
!= pixel
)
1956 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1966 #endif // wxREMAP_BUTTON_COLOURS
1968 #endif // wxUSE_TOOLBAR