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
)
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 class wxToolBarTool
: public wxToolBarToolBase
138 wxToolBarTool(wxToolBar
*tbar
,
140 const wxString
& label
,
141 const wxBitmap
& bmpNormal
,
142 const wxBitmap
& bmpDisabled
,
144 wxObject
*clientData
,
145 const wxString
& shortHelp
,
146 const wxString
& longHelp
)
147 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
148 clientData
, shortHelp
, longHelp
)
154 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
155 : wxToolBarToolBase(tbar
, control
, label
)
157 if ( IsControl() && !m_label
.empty() )
159 // create a control to render the control's label
160 m_staticText
= new wxStaticText
167 wxALIGN_CENTRE
| wxST_NO_AUTORESIZE
178 virtual ~wxToolBarTool()
183 virtual void SetLabel(const wxString
& label
)
185 if ( label
== m_label
)
188 wxToolBarToolBase::SetLabel(label
);
191 m_staticText
->SetLabel(label
);
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
196 // TODO: use TB_SETBUTTONINFO
199 wxStaticText
* GetStaticText()
201 wxASSERT_MSG( IsControl(),
202 wxT("only makes sense for embedded control tools") );
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
; }
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
216 if ( m_id
== wxID_SEPARATOR
)
217 m_id
= wxWindow::NewControlId();
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
)
224 wxControl
* const control
= GetControl();
226 control
->Move(control
->GetPosition().x
+ offset
, wxDefaultCoord
);
230 m_staticText
->Move(m_staticText
->GetPosition().x
+ offset
,
237 wxStaticText
*m_staticText
;
239 wxDECLARE_NO_COPY_CLASS(wxToolBarTool
);
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 // return the rectangle of the item at the given index
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
)
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
) )
258 wxLogLastError(wxT("TB_GETITEMRECT"));
269 // ============================================================================
271 // ============================================================================
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
278 const wxString
& label
,
279 const wxBitmap
& bmpNormal
,
280 const wxBitmap
& bmpDisabled
,
282 wxObject
*clientData
,
283 const wxString
& shortHelp
,
284 const wxString
& longHelp
)
286 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
287 clientData
, shortHelp
, longHelp
);
291 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
293 return new wxToolBarTool(this, control
, label
);
296 // ----------------------------------------------------------------------------
297 // wxToolBar construction
298 // ----------------------------------------------------------------------------
300 void wxToolBar::Init()
303 m_disabledImgList
= NULL
;
306 m_totalFixedSize
= 0;
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()
314 m_defaultHeight
= 15;
319 bool wxToolBar::Create(wxWindow
*parent
,
324 const wxString
& name
)
326 // common initialisation
327 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
332 // MSW-specific initialisation
333 if ( !MSWCreateToolbar(pos
, size
) )
336 wxSetCCUnicodeFormat(GetHwnd());
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
341 if ( style
& wxTB_FLAT
)
343 LRESULT style
= GetMSWToolbarStyle();
345 if ( !(style
& TBSTYLE_FLAT
) )
346 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, style
| TBSTYLE_FLAT
);
348 #endif // wxUSE_UXTHEME
353 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
355 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
358 // toolbar-specific post initialisation
359 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
361 #ifdef TB_SETEXTENDEDSTYLE
362 if ( wxApp::GetComCtl32Version() >= 471 )
363 ::SendMessage(GetHwnd(), TB_SETEXTENDEDSTYLE
, 0, TBSTYLE_EX_DRAWDDARROWS
);
369 void wxToolBar::Recreate()
371 const HWND hwndOld
= GetHwnd();
374 // we haven't been created yet, no need to recreate
378 // get the position and size before unsubclassing the old toolbar
379 const wxPoint pos
= GetPosition();
380 const wxSize size
= GetSize();
384 if ( !MSWCreateToolbar(pos
, size
) )
387 wxFAIL_MSG( wxT("recreating the toolbar failed") );
392 // reparent all our children under the new toolbar
393 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
395 node
= node
->GetNext() )
397 wxWindow
*win
= node
->GetData();
398 if ( !win
->IsTopLevel() )
399 ::SetParent(GetHwndOf(win
), GetHwnd());
402 // only destroy the old toolbar now --
403 // after all the children had been reparented
404 ::DestroyWindow(hwndOld
);
406 // it is for the old bitmap control and can't be used with the new one
409 ::DeleteObject((HBITMAP
) m_hBitmap
);
413 if ( m_disabledImgList
)
415 delete m_disabledImgList
;
416 m_disabledImgList
= NULL
;
422 wxToolBar::~wxToolBar()
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();
429 ::DeleteObject((HBITMAP
) m_hBitmap
);
431 delete m_disabledImgList
;
434 wxSize
wxToolBar::DoGetBestSize() const
439 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
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();
447 // reverse horz and vertical components if necessary
451 sizeBest
.x
= sizeBest
.y
;
455 else // TB_GETMAXSIZE succeeded
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 )
462 // calculate the toolbar width in alternative way
463 const RECT rcFirst
= wxGetTBItemRect(GetHwnd(), 0);
464 const RECT rcLast
= wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1);
466 const int widthAlt
= rcLast
.right
- rcFirst
.left
;
467 if ( widthAlt
> size
.cx
)
471 sizeBest
.x
= size
.cx
;
472 sizeBest
.y
= size
.cy
;
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
481 if (!(GetWindowStyle() & wxTB_NODIVIDER
))
486 CacheBestSize(sizeBest
);
491 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
493 // toolbars never have border, giving one to them results in broken
495 WXDWORD msStyle
= wxControl::MSWGetStyle
497 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
500 if ( !(style
& wxTB_NO_TOOLTIPS
) )
501 msStyle
|= TBSTYLE_TOOLTIPS
;
503 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
505 // static as it doesn't change during the program lifetime
506 static const int s_verComCtl
= wxApp::GetComCtl32Version();
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
;
515 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
516 msStyle
|= TBSTYLE_LIST
;
519 if ( style
& wxTB_NODIVIDER
)
520 msStyle
|= CCS_NODIVIDER
;
522 if ( style
& wxTB_NOALIGN
)
523 msStyle
|= CCS_NOPARENTALIGN
;
525 if ( style
& wxTB_VERTICAL
)
528 if( style
& wxTB_BOTTOM
)
529 msStyle
|= CCS_BOTTOM
;
531 if ( style
& wxTB_RIGHT
)
532 msStyle
|= CCS_RIGHT
;
537 // ----------------------------------------------------------------------------
538 // adding/removing tools
539 // ----------------------------------------------------------------------------
541 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
542 wxToolBarToolBase
* WXUNUSED(tool
))
544 // nothing special to do here - we really create the toolbar buttons in
546 InvalidateBestSize();
550 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
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
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
559 wxToolBarToolsList::compatibility_iterator node
;
560 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
562 wxToolBarToolBase
*tool2
= node
->GetData();
565 // let node point to the next node in the list
566 node
= node
->GetNext();
571 if ( tool2
->IsControl() )
572 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
575 // now determine the number of buttons to delete and the area taken by them
576 size_t nButtonsToDelete
= 1;
578 // get the size of the button we're going to delete
579 const RECT r
= wxGetTBItemRect(GetHwnd(), pos
);
581 int width
= r
.right
- r
.left
;
583 if ( tool
->IsControl() )
585 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
586 width
*= nButtonsToDelete
;
589 // do delete all buttons
590 m_nButtons
-= nButtonsToDelete
;
591 while ( nButtonsToDelete
-- > 0 )
593 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
595 wxLogLastError(wxT("TB_DELETEBUTTON"));
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() )
605 wxToolBarTool
*tool2
= (wxToolBarTool
*)node
->GetData();
606 if ( tool2
->IsControl() )
608 tool2
->MoveBy(-width
);
612 InvalidateBestSize();
617 void wxToolBar::CreateDisabledImageList()
619 if (m_disabledImgList
!= NULL
)
621 delete m_disabledImgList
;
622 m_disabledImgList
= NULL
;
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 )
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() )
633 wxToolBarToolBase
*tool
= node
->GetData();
634 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
635 if ( bmpDisabled
.Ok() )
637 m_disabledImgList
= new wxImageList
641 bmpDisabled
.GetMask() != NULL
,
648 // we don't have any disabled bitmaps
652 bool wxToolBar::Realize()
654 if ( !wxToolBarBase::Realize() )
657 const size_t nTools
= GetToolsCount();
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
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
679 #endif // wxREMAP_BUTTON_COLOURS
681 // delete all old buttons, if any
682 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
684 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
686 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
690 // First, add the bitmap: we use one bitmap for all toolbar buttons
691 // ----------------------------------------------------------------
693 wxToolBarToolsList::compatibility_iterator node
;
696 if ( !HasFlag(wxTB_NOICONS
) )
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
;
702 const wxCoord totalBitmapWidth
= m_defaultWidth
*
703 wx_truncate_cast(wxCoord
, nTools
),
704 totalBitmapHeight
= m_defaultHeight
;
706 // Create a bitmap and copy all the tool bitmaps into it
707 wxMemoryDC dcAllButtons
;
708 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
709 dcAllButtons
.SelectObject(bitmap
);
711 #ifdef wxREMAP_BUTTON_COLOURS
712 if ( remapValue
!= Remap_TransparentBg
)
713 #endif // wxREMAP_BUTTON_COLOURS
715 // VZ: why do we hardcode grey colour for CE?
716 dcAllButtons
.SetBackground(wxBrush(
718 wxColour(0xc0, 0xc0, 0xc0)
719 #else // !__WXWINCE__
720 GetBackgroundColour()
721 #endif // __WXWINCE__/!__WXWINCE__
723 dcAllButtons
.Clear();
726 m_hBitmap
= bitmap
.GetHBITMAP();
727 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
729 #ifdef wxREMAP_BUTTON_COLOURS
730 if ( remapValue
== Remap_Bg
)
732 dcAllButtons
.SelectObject(wxNullBitmap
);
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
);
739 dcAllButtons
.SelectObject(bitmap
);
741 #endif // wxREMAP_BUTTON_COLOURS
743 // the button position
746 // the number of buttons (not separators)
749 CreateDisabledImageList();
750 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
752 wxToolBarToolBase
*tool
= node
->GetData();
753 if ( tool
->IsButton() )
755 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
757 const int w
= bmp
.GetWidth();
758 const int h
= bmp
.GetHeight();
762 int xOffset
= wxMax(0, (m_defaultWidth
- w
)/2);
763 int yOffset
= wxMax(0, (m_defaultHeight
- h
)/2);
765 // notice the last parameter: do use mask
766 dcAllButtons
.DrawBitmap(bmp
, x
+ xOffset
, yOffset
, true);
770 wxFAIL_MSG( wxT("invalid tool button bitmap") );
773 // also deal with disabled bitmap if we want to use them
774 if ( m_disabledImgList
)
776 wxBitmap bmpDisabled
= tool
->GetDisabledBitmap();
777 #if wxUSE_IMAGE && wxUSE_WXDIB
778 if ( !bmpDisabled
.Ok() )
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
784 imgGreyed
= bmp
.ConvertToImage().ConvertToGreyscale();
786 #ifdef wxREMAP_BUTTON_COLOURS
787 if ( remapValue
== Remap_Buttons
)
789 // we need to have light grey background colour for
790 // MapBitmap() to work correctly
791 for ( int y
= 0; y
< h
; y
++ )
793 for ( int x
= 0; x
< w
; x
++ )
795 if ( imgGreyed
.IsTransparent(x
, y
) )
796 imgGreyed
.SetRGB(x
, y
,
798 wxLIGHT_GREY
->Green(),
799 wxLIGHT_GREY
->Blue());
803 #endif // wxREMAP_BUTTON_COLOURS
805 bmpDisabled
= wxBitmap(imgGreyed
);
807 #endif // wxUSE_IMAGE
809 #ifdef wxREMAP_BUTTON_COLOURS
810 if ( remapValue
== Remap_Buttons
)
811 MapBitmap(bmpDisabled
.GetHBITMAP(), w
, h
);
812 #endif // wxREMAP_BUTTON_COLOURS
814 m_disabledImgList
->Add(bmpDisabled
);
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)
825 dcAllButtons
.SelectObject(wxNullBitmap
);
827 // don't delete this HBITMAP!
828 bitmap
.SetHBITMAP(0);
830 #ifdef wxREMAP_BUTTON_COLOURS
831 if ( remapValue
== Remap_Buttons
)
833 // Map to system colours
834 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
835 totalBitmapWidth
, totalBitmapHeight
);
837 #endif // wxREMAP_BUTTON_COLOURS
839 bool addBitmap
= true;
841 if ( oldToolBarBitmap
)
843 #ifdef TB_REPLACEBITMAP
844 if ( wxApp::GetComCtl32Version() >= 400 )
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
) )
855 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
858 ::DeleteObject(oldToolBarBitmap
);
864 #endif // TB_REPLACEBITMAP
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
871 bitmapId
= m_nButtons
;
875 if ( addBitmap
) // no old bitmap or we can't replace it
877 TBADDBITMAP addBitmap
;
879 addBitmap
.nID
= (UINT_PTR
)hBitmap
;
880 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
881 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
883 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
887 // disable image lists are only supported in comctl32.dll 4.70+
888 if ( wxApp::GetComCtl32Version() >= 470 )
890 HIMAGELIST hil
= m_disabledImgList
891 ? GetHimagelistOf(m_disabledImgList
)
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
);
899 // delete previous image list if any
901 ::DeleteObject(oldImageList
);
906 // Next add the buttons and separators
907 // -----------------------------------
909 wxScopedArray
<TBBUTTON
> buttons(new TBBUTTON
[nTools
]);
911 // this array will hold the indices of all controls in the toolbar
912 wxArrayInt controlIds
;
914 bool lastWasRadio
= false;
916 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
918 wxToolBarTool
*tool
= static_cast<wxToolBarTool
*>(node
->GetData());
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 )
928 TBBUTTON
& button
= buttons
[i
];
930 wxZeroMemory(button
);
932 bool isRadio
= false;
933 switch ( tool
->GetStyle() )
935 case wxTOOL_STYLE_CONTROL
:
936 case wxTOOL_STYLE_SEPARATOR
:
937 if ( tool
->IsStretchableSpace() )
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();
945 button
.idCommand
= tool
->GetId();
946 button
.fsState
= TBSTATE_ENABLED
;
947 button
.fsStyle
= TBSTYLE_SEP
;
950 case wxTOOL_STYLE_BUTTON
:
951 if ( !HasFlag(wxTB_NOICONS
) )
952 button
.iBitmap
= bitmapId
;
954 if ( HasFlag(wxTB_TEXT
) )
956 const wxString
& label
= tool
->GetLabel();
957 if ( !label
.empty() )
958 button
.iString
= (INT_PTR
)label
.wx_str();
961 button
.idCommand
= tool
->GetId();
963 if ( tool
->IsEnabled() )
964 button
.fsState
|= TBSTATE_ENABLED
;
965 if ( tool
->IsToggled() )
966 button
.fsState
|= TBSTATE_CHECKED
;
968 switch ( tool
->GetKind() )
971 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
975 // the first item in the radio group is checked by
976 // default to be consistent with wxGTK and the menu
978 button
.fsState
|= TBSTATE_CHECKED
;
980 if (tool
->Toggle(true))
982 DoToggleTool(tool
, true);
985 else if ( tool
->IsToggled() )
987 wxToolBarToolsList::compatibility_iterator nodePrev
= node
->GetPrevious();
988 int prevIndex
= i
- 1;
991 TBBUTTON
& prevButton
= buttons
[prevIndex
];
992 wxToolBarToolBase
*tool
= nodePrev
->GetData();
993 if ( !tool
->IsButton() || tool
->GetKind() != wxITEM_RADIO
)
996 if ( tool
->Toggle(false) )
997 DoToggleTool(tool
, false);
999 prevButton
.fsState
&= ~TBSTATE_CHECKED
;
1000 nodePrev
= nodePrev
->GetPrevious();
1009 button
.fsStyle
= TBSTYLE_CHECK
;
1013 button
.fsStyle
= TBSTYLE_BUTTON
;
1016 case wxITEM_DROPDOWN
:
1017 button
.fsStyle
= TBSTYLE_DROPDOWN
;
1021 wxFAIL_MSG( wxT("unexpected toolbar button kind") );
1022 button
.fsStyle
= TBSTYLE_BUTTON
;
1030 lastWasRadio
= isRadio
;
1035 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, i
, (LPARAM
)buttons
.get()) )
1037 wxLogLastError(wxT("TB_ADDBUTTONS"));
1041 // Adjust controls and stretchable spaces
1042 // --------------------------------------
1044 // adjust the controls size to fit nicely in the toolbar and compute its
1045 // total size while doing it
1046 m_totalFixedSize
= 0;
1048 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), toolIndex
++ )
1050 wxToolBarTool
* const tool
= (wxToolBarTool
*)node
->GetData();
1052 const RECT r
= wxGetTBItemRect(GetHwnd(), toolIndex
);
1054 if ( !tool
->IsControl() )
1057 m_totalFixedSize
+= r
.bottom
- r
.top
;
1059 m_totalFixedSize
+= r
.right
- r
.left
;
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)
1072 wxControl
* const control
= tool
->GetControl();
1073 wxStaticText
* const staticText
= tool
->GetStaticText();
1075 wxSize size
= control
->GetSize();
1076 wxSize staticTextSize
;
1079 staticTextSize
= staticText
->GetSize();
1080 staticTextSize
.y
+= 3; // margin between control and its label
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 )
1089 // set the (underlying) separators width to be that of the
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
) )
1098 // the id is probably invalid?
1099 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1103 #endif // comctl32.dll 4.71
1104 // TB_SETBUTTONINFO unavailable
1106 // try adding several separators to fit the controls width
1107 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 toolIndex
, (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 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
1164 control
->Move(r
.left
, r
.top
+ (diff
+ 1) / 2);
1167 staticText
->Move(r
.left
+ (size
.x
- staticTextSize
.x
)/2,
1168 r
.bottom
- staticTextSize
.y
);
1171 m_totalFixedSize
+= size
.x
;
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
;
1178 if ( !IsVertical() )
1180 if ( m_maxRows
== 0 )
1181 // if not set yet, only one row
1184 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
1186 // if not set yet, have one column
1188 SetRows(m_nButtons
);
1191 InvalidateBestSize();
1197 void wxToolBar::UpdateStretchableSpacersSize()
1199 // we can't resize the spacers if TB_SETBUTTONINFO is not supported
1200 if ( wxApp::GetComCtl32Version() < 471 )
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() )
1208 wxToolBarTool
* const tool
= (wxToolBarTool
*)node
->GetData();
1209 if ( tool
->IsStretchableSpace() )
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;
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
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
1234 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), toolIndex
++ )
1236 wxToolBarTool
* const tool
= (wxToolBarTool
*)node
->GetData();
1238 if ( tool
->IsControl() && offset
)
1240 tool
->MoveBy(offset
);
1245 if ( !tool
->IsStretchableSpace() )
1248 const RECT rcOld
= wxGetTBItemRect(GetHwnd(), toolIndex
);
1250 WinStruct
<TBBUTTONINFO
> tbbi
;
1251 tbbi
.dwMask
= TBIF_SIZE
;
1252 tbbi
.cx
= --numSpaces
? sizeSpacer
: sizeLastSpacer
;
1254 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
1255 tool
->GetId(), (LPARAM
)&tbbi
) )
1257 wxLogLastError(wxT("TB_SETBUTTONINFO"));
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
);
1268 // ----------------------------------------------------------------------------
1270 // ----------------------------------------------------------------------------
1272 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id_
)
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_
;
1279 wxToolBarToolBase
*tool
= FindById(id
);
1283 bool toggled
= false; // just to suppress warnings
1285 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
1287 if ( tool
->CanBeToggled() )
1289 toggled
= (state
& TBSTATE_CHECKED
) != 0;
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
)
1296 tool
->Toggle(toggled
);
1297 UnToggleRadioGroup(tool
);
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.
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));
1315 bool allowLeftClick
= OnLeftClick(id
, toggled
);
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
;
1322 state
&= ~TBSTATE_ENABLED
;
1323 if (tool
->IsToggled())
1324 state
|= TBSTATE_CHECKED
;
1326 state
&= ~TBSTATE_CHECKED
;
1327 ::SendMessage(GetHwnd(), TB_SETSTATE
, id
, MAKELONG(state
, 0));
1329 // OnLeftClick() can veto the button state change - for buttons which
1330 // may be toggled only, of couse
1331 if ( !allowLeftClick
&& tool
->CanBeToggled() )
1334 tool
->Toggle(!toggled
);
1336 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(!toggled
, 0));
1342 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
1344 WXLPARAM
*WXUNUSED(result
))
1346 LPNMHDR hdr
= (LPNMHDR
)lParam
;
1347 if ( hdr
->code
== TBN_DROPDOWN
)
1349 LPNMTOOLBAR tbhdr
= (LPNMTOOLBAR
)lParam
;
1351 wxCommandEvent
evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED
, tbhdr
->iItem
);
1352 if ( HandleWindowEvent(evt
) )
1354 // Event got handled, don't display default popup menu
1358 const wxToolBarToolBase
* const tool
= FindById(tbhdr
->iItem
);
1359 wxCHECK_MSG( tool
, false, wxT("drop down message for unknown tool") );
1361 wxMenu
* const menu
= tool
->GetDropdownMenu();
1365 // Display popup menu below button
1366 const RECT r
= wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr
->iItem
));
1368 PopupMenu(menu
, r
.left
, r
.bottom
);
1374 if( !HasFlag(wxTB_NO_TOOLTIPS
) )
1377 // First check if this applies to us
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
) )
1385 HWND toolTipWnd
= (HWND
)::SendMessage(GetHwnd(), TB_GETTOOLTIPS
, 0, 0);
1386 if ( toolTipWnd
!= hdr
->hwndFrom
)
1389 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1390 int id
= (int)ttText
->hdr
.idFrom
;
1392 wxToolBarToolBase
*tool
= FindById(id
);
1394 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1396 wxUnusedVar(lParam
);
1403 // ----------------------------------------------------------------------------
1405 // ----------------------------------------------------------------------------
1407 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1409 wxToolBarBase::SetToolBitmapSize(size
);
1411 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1414 void wxToolBar::SetRows(int nRows
)
1416 if ( nRows
== m_maxRows
)
1418 // avoid resizing the frame uselessly
1422 // TRUE in wParam means to create at least as many rows, FALSE -
1425 ::SendMessage(GetHwnd(), TB_SETROWS
,
1426 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1434 // The button size is bigger than the bitmap size
1435 wxSize
wxToolBar::GetToolSize() const
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 )
1443 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1445 return wxSize(LOWORD(dw
), HIWORD(dw
));
1448 #endif // comctl32.dll 4.70+
1451 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1456 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1459 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1461 for ( ; current
; current
= current
->GetNext() )
1464 return current
->GetData();
1466 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1467 size_t separators
= tool
->GetSeparatorsCount();
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
1472 index
-= separators
? separators
+ 1 : 1;
1478 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1483 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
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
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 )
1496 return m_tools
.Item((size_t)index
)->GetData();
1499 #endif // TB_SETBUTTONINFO
1501 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1505 void wxToolBar::UpdateSize()
1507 wxPoint pos
= GetPosition();
1508 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1509 if (pos
!= GetPosition())
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();
1520 // ----------------------------------------------------------------------------
1522 // ---------------------------------------------------------------------------
1524 // get the TBSTYLE of the given toolbar window
1525 long wxToolBar::GetMSWToolbarStyle() const
1527 return ::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L);
1530 void wxToolBar::SetWindowStyleFlag(long style
)
1532 // the style bits whose changes force us to recreate the toolbar
1533 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1535 const long styleOld
= GetWindowStyle();
1537 wxToolBarBase::SetWindowStyleFlag(style
);
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
1542 if ( GetToolsCount() &&
1543 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
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
1552 // ----------------------------------------------------------------------------
1554 // ----------------------------------------------------------------------------
1556 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1558 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1559 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1562 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1564 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1565 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1568 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
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") );
1575 void wxToolBar::SetToolNormalBitmap( int id
, const wxBitmap
& bitmap
)
1577 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1580 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1582 tool
->SetNormalBitmap(bitmap
);
1587 void wxToolBar::SetToolDisabledBitmap( int id
, const wxBitmap
& bitmap
)
1589 wxToolBarTool
* tool
= static_cast<wxToolBarTool
*>(FindById(id
));
1592 wxCHECK_RET( tool
->IsButton(), wxT("Can only set bitmap on button tools."));
1594 tool
->SetDisabledBitmap(bitmap
);
1599 // ----------------------------------------------------------------------------
1601 // ----------------------------------------------------------------------------
1603 // Responds to colour changes, and passes event on to children.
1604 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1606 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1608 // Remap the buttons
1611 // Relayout the toolbar
1612 int nrows
= m_maxRows
;
1613 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1618 // let the event propagate further
1622 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1624 if ( event
.Leaving() )
1628 OnMouseEnter(wxID_ANY
);
1636 if ( event
.RightDown() )
1638 // find the tool under the mouse
1639 wxCoord x
= 0, y
= 0;
1640 event
.GetPosition(&x
, &y
);
1642 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1643 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1651 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1653 // wait until we have some tools
1654 if ( !GetToolsCount() )
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);
1667 w
= r
.right
- r
.left
;
1670 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1677 if (HasFlag( wxTB_FLAT
))
1678 h
= r
.bottom
- r
.top
- 3;
1680 h
= r
.bottom
- r
.top
;
1683 // FIXME: hardcoded separator line height...
1684 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1689 if ( MAKELPARAM(w
, h
) != lParam
)
1691 // size really changed
1695 UpdateStretchableSpacersSize();
1697 // message processed
1703 void wxToolBar::MSWEraseRect(wxDC
& dc
, const wxRect
& rectItem
)
1705 dc
.DrawRectangle(rectItem
);
1708 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
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)
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() )
1717 wxToolBarToolBase
* const tool
= node
->GetData();
1718 if ( tool
->IsControl() || tool
->IsStretchableSpace() )
1724 // no controls, nothing to erase
1728 // prepare the DC on which we'll be drawing
1729 wxClientDC
dc(this);
1730 dc
.SetBrush(GetBackgroundColour());
1731 dc
.SetPen(*wxTRANSPARENT_PEN
);
1734 if ( !::GetUpdateRect(GetHwnd(), &rcUpdate
, FALSE
) )
1736 // nothing to redraw anyhow
1740 const wxRect rectUpdate
= wxRectFromRECT(rcUpdate
);
1741 dc
.SetClippingRegion(rectUpdate
);
1743 // draw the toolbar tools, separators &c normally
1744 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1746 // for each control in the toolbar find all the separators intersecting it
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
1753 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), toolIndex
++ )
1755 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
1756 if ( tool
->IsControl() )
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
;
1764 rectStaticText
= staticText
->GetRect();
1766 if ( !rectCtrl
.Intersects(rectUpdate
) &&
1767 (!staticText
|| !rectStaticText
.Intersects(rectUpdate
)) )
1770 // iterate over all buttons to find all separators intersecting
1773 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1774 for ( int n
= 0; n
< count
; n
++ )
1776 // is it a separator?
1777 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1780 wxLogDebug(wxT("TB_GETBUTTON failed?"));
1785 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1788 // get the bounding rect of the separator
1789 RECT r
= wxGetTBItemRect(GetHwnd(), n
);
1793 const wxRect rectItem
= wxRectFromRECT(r
);
1795 // does it intersect the update region at all?
1796 if ( !rectUpdate
.Intersects(rectItem
) )
1799 // does it intersect the control itself or its label?
1801 // if it does, refresh it so it's redrawn on top of the
1803 if ( rectCtrl
.Intersects(rectItem
) )
1804 control
->Refresh(false);
1805 else if ( staticText
&& rectStaticText
.Intersects(rectItem
) )
1806 staticText
->Refresh(false);
1810 MSWEraseRect(dc
, rectItem
);
1813 else if ( tool
->IsStretchableSpace() )
1816 rectItem
= wxRectFromRECT(wxGetTBItemRect(GetHwnd(), toolIndex
));
1818 if ( rectUpdate
.Intersects(rectItem
) )
1819 MSWEraseRect(dc
, rectItem
);
1825 #endif // __WXWINCE__
1827 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1829 wxCoord x
= GET_X_LPARAM(lParam
),
1830 y
= GET_Y_LPARAM(lParam
);
1831 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1833 // has the current tool changed?
1834 if ( tool
!= m_pInTool
)
1837 OnMouseEnter(tool
? tool
->GetId() : wxID_ANY
);
1841 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1846 // we don't handle mouse moves, so always pass the message to
1847 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
1848 HandleMouseMove(wParam
, lParam
);
1852 if ( HandleSize(wParam
, lParam
) )
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
) )
1865 #endif // __WXWINCE__
1868 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1871 // ----------------------------------------------------------------------------
1872 // private functions
1873 // ----------------------------------------------------------------------------
1875 #ifdef wxREMAP_BUTTON_COLOURS
1877 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1883 wxLogLastError(wxT("CreateCompatibleDC"));
1888 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1892 wxLogLastError(wxT("SelectObject"));
1897 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1899 for ( int i
= 0; i
< width
; i
++ )
1901 for ( int j
= 0; j
< height
; j
++ )
1903 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1905 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
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 )
1912 if ( cmap
[k
].to
!= pixel
)
1913 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1923 #endif // wxREMAP_BUTTON_COLOURS
1925 #endif // wxUSE_TOOLBAR