1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tbar95.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "tbar95.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/dynarray.h"
36 #include "wx/settings.h"
37 #include "wx/bitmap.h"
38 #include "wx/dcmemory.h"
39 #include "wx/control.h"
42 #if wxUSE_TOOLBAR && defined(__WIN95__) && wxUSE_TOOLBAR_NATIVE
44 #include "wx/toolbar.h"
46 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
50 #include "wx/msw/private.h"
52 // include <commctrl.h> "properly"
53 #include "wx/msw/wrapcctl.h"
55 #include "wx/app.h" // for GetComCtl32Version
57 // ----------------------------------------------------------------------------
58 // conditional compilation
59 // ----------------------------------------------------------------------------
61 // wxWindows previously always considered that toolbar buttons have light grey
62 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
63 // doesn't work with XPMs which then appear to have black background. To make
64 // this work, we must respect the bitmap masks - which we do now. This should
65 // be ok in any case, but to restore 100% compatible with the old version
66 // behaviour, you can set this to 0.
67 #define USE_BITMAP_MASKS 1
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // these standard constants are not always defined in compilers headers
77 #define TBSTYLE_LIST 0x1000
78 #define TBSTYLE_FLAT 0x0800
81 #ifndef TBSTYLE_TRANSPARENT
82 #define TBSTYLE_TRANSPARENT 0x8000
85 #ifndef TBSTYLE_TOOLTIPS
86 #define TBSTYLE_TOOLTIPS 0x0100
91 #define TB_SETSTYLE (WM_USER + 56)
92 #define TB_GETSTYLE (WM_USER + 57)
96 #define TB_HITTEST (WM_USER + 69)
99 // these values correspond to those used by comctl32.dll
100 #define DEFAULTBITMAPX 16
101 #define DEFAULTBITMAPY 15
102 #define DEFAULTBUTTONX 24
103 #define DEFAULTBUTTONY 24
104 #define DEFAULTBARHEIGHT 27
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
112 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
113 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
114 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 class wxToolBarTool
: public wxToolBarToolBase
124 wxToolBarTool(wxToolBar
*tbar
,
126 const wxString
& label
,
127 const wxBitmap
& bmpNormal
,
128 const wxBitmap
& bmpDisabled
,
130 wxObject
*clientData
,
131 const wxString
& shortHelp
,
132 const wxString
& longHelp
)
133 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
134 clientData
, shortHelp
, longHelp
)
139 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
140 : wxToolBarToolBase(tbar
, control
)
145 virtual void SetLabel(const wxString
& label
)
147 if ( label
== m_label
)
150 wxToolBarToolBase::SetLabel(label
);
152 // we need to update the label shown in the toolbar because it has a
153 // pointer to the internal buffer of the old label
155 // TODO: use TB_SETBUTTONINFO
158 // set/get the number of separators which we use to cover the space used by
159 // a control in the toolbar
160 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
161 size_t GetSeparatorsCount() const { return m_nSepCount
; }
166 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
170 // ============================================================================
172 // ============================================================================
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
179 const wxString
& label
,
180 const wxBitmap
& bmpNormal
,
181 const wxBitmap
& bmpDisabled
,
183 wxObject
*clientData
,
184 const wxString
& shortHelp
,
185 const wxString
& longHelp
)
187 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
188 clientData
, shortHelp
, longHelp
);
191 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
193 return new wxToolBarTool(this, control
);
196 // ----------------------------------------------------------------------------
197 // wxToolBar construction
198 // ----------------------------------------------------------------------------
200 void wxToolBar::Init()
206 m_defaultWidth
= DEFAULTBITMAPX
;
207 m_defaultHeight
= DEFAULTBITMAPY
;
212 bool wxToolBar::Create(wxWindow
*parent
,
217 const wxString
& name
)
219 // common initialisation
220 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
223 // MSW-specific initialisation
224 if ( !MSWCreateToolbar(pos
, size
) )
227 // set up the colors and fonts
228 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
229 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
234 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
236 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
239 // toolbar-specific post initialisation
240 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
245 void wxToolBar::Recreate()
247 const HWND hwndOld
= GetHwnd();
250 // we haven't been created yet, no need to recreate
254 // get the position and size before unsubclassing the old toolbar
255 const wxPoint pos
= GetPosition();
256 const wxSize size
= GetSize();
260 if ( !MSWCreateToolbar(pos
, size
) )
263 wxFAIL_MSG( _T("recreating the toolbar failed") );
268 // reparent all our children under the new toolbar
269 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
271 node
= node
->GetNext() )
273 wxWindow
*win
= node
->GetData();
274 if ( !win
->IsTopLevel() )
275 ::SetParent(GetHwndOf(win
), GetHwnd());
278 // only destroy the old toolbar now -- after all the children had been
280 ::DestroyWindow(hwndOld
);
282 // it is for the old bitmap control and can't be used with the new one
285 ::DeleteObject((HBITMAP
) m_hBitmap
);
293 wxToolBar::~wxToolBar()
295 // we must refresh the frame size when the toolbar is deleted but the frame
296 // is not - otherwise toolbar leaves a hole in the place it used to occupy
297 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
298 if ( frame
&& !frame
->IsBeingDeleted() )
300 frame
->SendSizeEvent();
305 ::DeleteObject((HBITMAP
) m_hBitmap
);
309 wxSize
wxToolBar::DoGetBestSize() const
311 wxSize sizeBest
= GetToolSize();
312 sizeBest
.x
*= GetToolsCount();
314 // reverse horz and vertical components if necessary
315 return HasFlag(wxTB_VERTICAL
) ? wxSize(sizeBest
.y
, sizeBest
.x
) : sizeBest
;
318 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
320 // toolbars never have border, giving one to them results in broken
322 WXDWORD msStyle
= wxControl::MSWGetStyle
324 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
327 // always include this one, it never hurts and setting it later only if we
328 // do have tooltips wouldn't work
329 msStyle
|= TBSTYLE_TOOLTIPS
;
331 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
333 // static as it doesn't change during the program lifetime
334 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
336 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
337 // style with 6.00 (part of Windows XP) leads to the toolbar with
338 // incorrect background colour - and not using it still results in the
339 // correct (flat) toolbar, so don't use it there
340 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
342 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
345 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
347 msStyle
|= TBSTYLE_LIST
;
351 if ( style
& wxTB_NODIVIDER
)
352 msStyle
|= CCS_NODIVIDER
;
354 if ( style
& wxTB_NOALIGN
)
355 msStyle
|= CCS_NOPARENTALIGN
;
357 if ( style
& wxTB_VERTICAL
)
363 // ----------------------------------------------------------------------------
364 // adding/removing tools
365 // ----------------------------------------------------------------------------
367 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
369 // nothing special to do here - we really create the toolbar buttons in
376 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
378 // the main difficulty we have here is with the controls in the toolbars:
379 // as we (sometimes) use several separators to cover up the space used by
380 // them, the indices are not the same for us and the toolbar
382 // first determine the position of the first button to delete: it may be
383 // different from pos if we use several separators to cover the space used
385 wxToolBarToolsList::compatibility_iterator node
;
386 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
388 wxToolBarToolBase
*tool2
= node
->GetData();
391 // let node point to the next node in the list
392 node
= node
->GetNext();
397 if ( tool2
->IsControl() )
399 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
403 // now determine the number of buttons to delete and the area taken by them
404 size_t nButtonsToDelete
= 1;
406 // get the size of the button we're going to delete
408 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
410 wxLogLastError(_T("TB_GETITEMRECT"));
413 int width
= r
.right
- r
.left
;
415 if ( tool
->IsControl() )
417 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
419 width
*= nButtonsToDelete
;
422 // do delete all buttons
423 m_nButtons
-= nButtonsToDelete
;
424 while ( nButtonsToDelete
-- > 0 )
426 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
428 wxLogLastError(wxT("TB_DELETEBUTTON"));
436 // and finally reposition all the controls after this button (the toolbar
437 // takes care of all normal items)
438 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
440 wxToolBarToolBase
*tool2
= node
->GetData();
441 if ( tool2
->IsControl() )
444 wxControl
*control
= tool2
->GetControl();
445 control
->GetPosition(&x
, NULL
);
446 control
->Move(x
- width
, -1);
453 bool wxToolBar::Realize()
455 const size_t nTools
= GetToolsCount();
462 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
464 // delete all old buttons, if any
465 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
467 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
469 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
473 // First, add the bitmap: we use one bitmap for all toolbar buttons
474 // ----------------------------------------------------------------
476 wxToolBarToolsList::compatibility_iterator node
;
480 if ( HasFlag(wxTB_NOICONS
) )
482 // no icons, don't leave space for them
486 else // do show icons
488 // if we already have a bitmap, we'll replace the existing one --
489 // otherwise we'll install a new one
490 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
492 sizeBmp
.x
= m_defaultWidth
;
493 sizeBmp
.y
= m_defaultHeight
;
495 const wxCoord totalBitmapWidth
= m_defaultWidth
* nTools
,
496 totalBitmapHeight
= m_defaultHeight
;
498 // Create a bitmap and copy all the tool bitmaps to it
500 wxMemoryDC dcAllButtons
;
501 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
502 dcAllButtons
.SelectObject(bitmap
);
503 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
504 dcAllButtons
.Clear();
506 m_hBitmap
= bitmap
.GetHBITMAP();
507 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
508 #else // !USE_BITMAP_MASKS
509 HBITMAP hBitmap
= ::CreateCompatibleBitmap(ScreenHDC(),
514 wxLogLastError(_T("CreateCompatibleBitmap"));
519 m_hBitmap
= (WXHBITMAP
)hBitmap
;
522 SelectInHDC
hdcSelector(memoryDC
, hBitmap
);
525 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
527 // the button position
530 // the number of buttons (not separators)
533 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
535 wxToolBarToolBase
*tool
= node
->GetData();
536 if ( tool
->IsButton() )
538 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
542 // notice the last parameter: do use mask
543 dcAllButtons
.DrawBitmap(bmp
, x
, 0, TRUE
);
544 #else // !USE_BITMAP_MASKS
545 SelectInHDC
hdcSelector2(memoryDC2
, GetHbitmapOf(bmp
));
546 if ( !BitBlt(memoryDC
,
547 x
, 0, m_defaultWidth
, m_defaultHeight
,
551 wxLogLastError(wxT("BitBlt"));
553 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
557 wxFAIL_MSG( _T("invalid tool button bitmap") );
560 // still inc width and number of buttons because otherwise the
561 // subsequent buttons will all be shifted which is rather confusing
562 // (and like this you'd see immediately which bitmap was bad)
569 dcAllButtons
.SelectObject(wxNullBitmap
);
571 // don't delete this HBITMAP!
572 bitmap
.SetHBITMAP(0);
573 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
575 // Map to system colours
576 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
577 totalBitmapWidth
, totalBitmapHeight
);
579 bool addBitmap
= TRUE
;
581 if ( oldToolBarBitmap
)
583 #ifdef TB_REPLACEBITMAP
584 if ( wxTheApp
->GetComCtl32Version() >= 400 )
586 TBREPLACEBITMAP replaceBitmap
;
587 replaceBitmap
.hInstOld
= NULL
;
588 replaceBitmap
.hInstNew
= NULL
;
589 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
590 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
591 replaceBitmap
.nButtons
= nButtons
;
592 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
593 0, (LPARAM
) &replaceBitmap
) )
595 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
598 ::DeleteObject(oldToolBarBitmap
);
604 #endif // TB_REPLACEBITMAP
606 // we can't replace the old bitmap, so we will add another one
607 // (awfully inefficient, but what else to do?) and shift the bitmap
608 // indices accordingly
611 bitmapId
= m_nButtons
;
615 if ( addBitmap
) // no old bitmap or we can't replace it
617 TBADDBITMAP addBitmap
;
619 addBitmap
.nID
= (UINT
) hBitmap
;
620 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
621 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
623 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
628 // don't call SetToolBitmapSize() as we don't want to change the values of
629 // m_defaultWidth/Height
630 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
631 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
633 wxLogLastError(_T("TB_SETBITMAPSIZE"));
636 // Next add the buttons and separators
637 // -----------------------------------
639 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
641 // this array will hold the indices of all controls in the toolbar
642 wxArrayInt controlIds
;
644 bool lastWasRadio
= FALSE
;
646 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
648 wxToolBarToolBase
*tool
= node
->GetData();
650 // don't add separators to the vertical toolbar with old comctl32.dll
651 // versions as they didn't handle this properly
652 if ( isVertical
&& tool
->IsSeparator() &&
653 wxTheApp
->GetComCtl32Version() <= 472 )
659 TBBUTTON
& button
= buttons
[i
];
661 wxZeroMemory(button
);
663 bool isRadio
= FALSE
;
664 switch ( tool
->GetStyle() )
666 case wxTOOL_STYLE_CONTROL
:
667 button
.idCommand
= tool
->GetId();
668 // fall through: create just a separator too
670 case wxTOOL_STYLE_SEPARATOR
:
671 button
.fsState
= TBSTATE_ENABLED
;
672 button
.fsStyle
= TBSTYLE_SEP
;
675 case wxTOOL_STYLE_BUTTON
:
676 if ( !HasFlag(wxTB_NOICONS
) )
677 button
.iBitmap
= bitmapId
;
679 if ( HasFlag(wxTB_TEXT
) )
681 const wxString
& label
= tool
->GetLabel();
682 if ( !label
.empty() )
684 button
.iString
= (int)label
.c_str();
688 button
.idCommand
= tool
->GetId();
690 if ( tool
->IsEnabled() )
691 button
.fsState
|= TBSTATE_ENABLED
;
692 if ( tool
->IsToggled() )
693 button
.fsState
|= TBSTATE_CHECKED
;
695 switch ( tool
->GetKind() )
698 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
702 // the first item in the radio group is checked by
703 // default to be consistent with wxGTK and the menu
705 button
.fsState
|= TBSTATE_CHECKED
;
714 button
.fsStyle
= TBSTYLE_CHECK
;
718 wxFAIL_MSG( _T("unexpected toolbar button kind") );
722 button
.fsStyle
= TBSTYLE_BUTTON
;
729 lastWasRadio
= isRadio
;
734 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
736 wxLogLastError(wxT("TB_ADDBUTTONS"));
741 // Deal with the controls finally
742 // ------------------------------
744 // adjust the controls size to fit nicely in the toolbar
747 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
749 wxToolBarToolBase
*tool
= node
->GetData();
751 // we calculate the running y coord for vertical toolbars so we need to
752 // get the items size for all items but for the horizontal ones we
753 // don't need to deal with the non controls
754 bool isControl
= tool
->IsControl();
755 if ( !isControl
&& !isVertical
)
758 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
759 // latter only appeared in v4.70 of comctl32.dll
761 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
762 index
, (LPARAM
)(LPRECT
)&r
) )
764 wxLogLastError(wxT("TB_GETITEMRECT"));
769 // can only be control if isVertical
770 y
+= r
.bottom
- r
.top
;
775 wxControl
*control
= tool
->GetControl();
777 wxSize size
= control
->GetSize();
779 // the position of the leftmost controls corner
782 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
783 #ifdef TB_SETBUTTONINFO
784 // available in headers, now check whether it is available now
786 if ( wxTheApp
->GetComCtl32Version() >= 471 )
788 // set the (underlying) separators width to be that of the
791 tbbi
.cbSize
= sizeof(tbbi
);
792 tbbi
.dwMask
= TBIF_SIZE
;
794 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
795 tool
->GetId(), (LPARAM
)&tbbi
) )
797 // the id is probably invalid?
798 wxLogLastError(wxT("TB_SETBUTTONINFO"));
802 #endif // comctl32.dll 4.71
803 // TB_SETBUTTONINFO unavailable
805 // try adding several separators to fit the controls width
806 int widthSep
= r
.right
- r
.left
;
812 tbb
.fsState
= TBSTATE_ENABLED
;
813 tbb
.fsStyle
= TBSTYLE_SEP
;
815 size_t nSeparators
= size
.x
/ widthSep
;
816 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
818 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
819 index
, (LPARAM
)&tbb
) )
821 wxLogLastError(wxT("TB_INSERTBUTTON"));
827 // remember the number of separators we used - we'd have to
828 // delete all of them later
829 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
831 // adjust the controls width to exactly cover the separators
832 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
835 // position the control itself correctly vertically
836 int height
= r
.bottom
- r
.top
;
837 int diff
= height
- size
.y
;
840 // the control is too high, resize to fit
841 control
->SetSize(-1, height
- 2);
852 y
+= height
+ 2*GetMargins().y
;
854 else // horizontal toolbar
862 control
->Move(left
, top
+ (diff
+ 1) / 2);
865 // the max index is the "real" number of buttons - i.e. counting even the
866 // separators which we added just for aligning the controls
871 if ( m_maxRows
== 0 )
873 // if not set yet, only one row
877 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
879 if ( m_maxRows
== 0 )
881 // if not set yet, have one column
889 // ----------------------------------------------------------------------------
891 // ----------------------------------------------------------------------------
893 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
895 wxToolBarToolBase
*tool
= FindById((int)id
);
899 if ( tool
->CanBeToggled() )
901 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
902 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
905 bool toggled
= tool
->IsToggled();
907 // avoid sending the event when a radio button is released, this is not
909 if ( !tool
->CanBeToggled() || tool
->GetKind() != wxITEM_RADIO
|| toggled
)
911 // OnLeftClick() can veto the button state change - for buttons which
912 // may be toggled only, of couse
913 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
917 tool
->SetToggle(toggled
);
919 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
926 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
928 WXLPARAM
*WXUNUSED(result
))
931 // First check if this applies to us
932 NMHDR
*hdr
= (NMHDR
*)lParam
;
934 // the tooltips control created by the toolbar is sometimes Unicode, even
935 // in an ANSI application - this seems to be a bug in comctl32.dll v5
936 UINT code
= hdr
->code
;
937 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
940 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
941 if ( toolTipWnd
!= hdr
->hwndFrom
)
944 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
945 int id
= (int)ttText
->hdr
.idFrom
;
947 wxToolBarToolBase
*tool
= FindById(id
);
951 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
957 // ----------------------------------------------------------------------------
959 // ----------------------------------------------------------------------------
961 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
963 wxToolBarBase::SetToolBitmapSize(size
);
965 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
968 void wxToolBar::SetRows(int nRows
)
970 if ( nRows
== m_maxRows
)
972 // avoid resizing the frame uselessly
976 // TRUE in wParam means to create at least as many rows, FALSE -
979 ::SendMessage(GetHwnd(), TB_SETROWS
,
980 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
988 // The button size is bigger than the bitmap size
989 wxSize
wxToolBar::GetToolSize() const
991 // TB_GETBUTTONSIZE is supported from version 4.70
992 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
993 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
994 if ( wxTheApp
->GetComCtl32Version() >= 470 )
996 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
998 return wxSize(LOWORD(dw
), HIWORD(dw
));
1001 #endif // comctl32.dll 4.70+
1004 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1009 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1012 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1014 for ( ; current
!= 0; current
= current
->GetNext() )
1017 return current
->GetData();
1019 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1020 size_t separators
= tool
->GetSeparatorsCount();
1022 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1023 // otherwise, skip as many items as the separator count, plus the
1025 index
-= separators
? separators
+ 1 : 1;
1031 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1036 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1037 // MBN: when the point ( x, y ) is close to the toolbar border
1038 // TB_HITTEST returns m_nButtons ( not -1 )
1039 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1041 // it's a separator or there is no tool at all there
1042 return (wxToolBarToolBase
*)NULL
;
1045 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1046 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1047 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1049 return m_tools
.Item((size_t)index
)->GetData();
1054 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1058 void wxToolBar::UpdateSize()
1060 // the toolbar size changed
1061 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1063 // we must also refresh the frame after the toolbar size (possibly) changed
1064 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1067 frame
->SendSizeEvent();
1071 // ----------------------------------------------------------------------------
1073 // ---------------------------------------------------------------------------
1075 void wxToolBar::SetWindowStyleFlag(long style
)
1077 // the style bits whose changes force us to recreate the toolbar
1078 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1080 const long styleOld
= GetWindowStyle();
1082 wxToolBarBase::SetWindowStyleFlag(style
);
1084 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1085 // also fatal as we'd then try to recreate the toolbar when it's just being
1087 if ( GetToolsCount() &&
1088 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1090 // to remove the text labels, simply re-realizing the toolbar is enough
1091 // but I don't know of any way to add the text to an existing toolbar
1092 // other than by recreating it entirely
1097 // ----------------------------------------------------------------------------
1099 // ----------------------------------------------------------------------------
1101 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1103 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1104 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1107 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1109 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1110 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1113 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1115 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1116 // without, so we really need to delete the button and recreate it here
1117 wxFAIL_MSG( _T("not implemented") );
1120 // ----------------------------------------------------------------------------
1122 // ----------------------------------------------------------------------------
1124 // Responds to colour changes, and passes event on to children.
1125 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1127 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1129 // Remap the buttons
1132 // Relayout the toolbar
1133 int nrows
= m_maxRows
;
1134 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1139 // let the event propagate further
1143 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1145 if (event
.Leaving() && m_pInTool
)
1152 if (event
.RightDown())
1154 // For now, we don't have an id. Later we could
1155 // try finding the tool.
1156 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1164 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1166 // calculate our minor dimension ourselves - we're confusing the standard
1167 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1169 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1173 if ( GetWindowStyle() & wxTB_VERTICAL
)
1175 w
= r
.right
- r
.left
;
1178 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1185 if (HasFlag( wxTB_FLAT
))
1186 h
= r
.bottom
- r
.top
- 3;
1188 h
= r
.bottom
- r
.top
;
1191 // FIXME: hardcoded separator line height...
1192 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1197 if ( MAKELPARAM(w
, h
) != lParam
)
1199 // size really changed
1203 // message processed
1210 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1212 // erase any dummy separators which we used for aligning the controls if
1215 // first of all, do we have any controls at all?
1216 wxToolBarToolsList::compatibility_iterator node
;
1217 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1219 if ( node
->GetData()->IsControl() )
1225 // no controls, nothing to erase
1229 // prepare the DC on which we'll be drawing
1230 wxClientDC
dc(this);
1231 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1232 dc
.SetPen(*wxTRANSPARENT_PEN
);
1235 if ( !GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1237 // nothing to redraw anyhow
1242 wxCopyRECTToRect(r
, rectUpdate
);
1244 dc
.SetClippingRegion(rectUpdate
);
1246 // draw the toolbar tools, separators &c normally
1247 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1249 // for each control in the toolbar find all the separators intersecting it
1252 // NB: this is really the only way to do it as we don't know if a separator
1253 // corresponds to a control (i.e. is a dummy one) or a real one
1255 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1257 wxToolBarToolBase
*tool
= node
->GetData();
1258 if ( tool
->IsControl() )
1260 // get the control rect in our client coords
1261 wxControl
*control
= tool
->GetControl();
1262 wxRect rectCtrl
= control
->GetRect();
1264 // iterate over all buttons
1266 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1267 for ( int n
= 0; n
< count
; n
++ )
1269 // is it a separator?
1270 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1273 wxLogDebug(_T("TB_GETBUTTON failed?"));
1278 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1281 // get the bounding rect of the separator
1283 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1286 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1291 // does it intersect the control?
1293 wxCopyRECTToRect(r
, rectItem
);
1294 if ( rectCtrl
.Intersects(rectItem
) )
1296 // yes, do erase it!
1297 dc
.DrawRectangle(rectItem
);
1299 // Necessary in case we use a no-paint-on-size
1300 // style in the parent: the controls can disappear
1301 control
->Refresh(FALSE
);
1310 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1312 wxCoord x
= GET_X_LPARAM(lParam
),
1313 y
= GET_Y_LPARAM(lParam
);
1314 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1316 // cursor left current tool
1317 if( tool
!= m_pInTool
&& !tool
)
1323 // cursor entered a tool
1324 if( tool
!= m_pInTool
&& tool
)
1327 OnMouseEnter( tool
->GetId() );
1331 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1336 if ( HandleSize(wParam
, lParam
) )
1341 // we don't handle mouse moves, so always pass the message to
1342 // wxControl::MSWWindowProc
1343 HandleMouseMove(wParam
, lParam
);
1347 if ( HandlePaint(wParam
, lParam
) )
1351 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1354 // ----------------------------------------------------------------------------
1355 // private functions
1356 // ----------------------------------------------------------------------------
1358 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1364 wxLogLastError(_T("CreateCompatibleDC"));
1369 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1373 wxLogLastError(_T("SelectObject"));
1378 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1380 for ( int i
= 0; i
< width
; i
++ )
1382 for ( int j
= 0; j
< height
; j
++ )
1384 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1386 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1388 COLORREF col
= cmap
[k
].from
;
1389 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1390 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1391 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1393 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1402 // VZ: I leave here my attempts to map the bitmap to the system colours
1403 // faster by using BitBlt() even though it's broken currently - but
1404 // maybe someone else can finish it? It should be faster than iterating
1405 // over all pixels...
1407 MemoryHDC hdcMask
, hdcDst
;
1408 if ( !hdcMask
|| !hdcDst
)
1410 wxLogLastError(_T("CreateCompatibleDC"));
1415 // create the target bitmap
1416 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1419 wxLogLastError(_T("CreateCompatibleBitmap"));
1424 // create the monochrome mask bitmap
1425 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1428 wxLogLastError(_T("CreateBitmap(mono)"));
1430 ::DeleteObject(hbmpDst
);
1435 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1436 bmpInMask(hdcMask
, hbmpMask
);
1439 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1441 // create the mask for this colour
1442 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1443 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1445 // replace this colour with the target one in the dst bitmap
1446 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1447 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1449 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1452 MAKEROP4(PATCOPY
, SRCCOPY
));
1454 (void)::SelectObject(hdcDst
, hbrOld
);
1455 ::DeleteObject(hbr
);
1458 ::DeleteObject((HBITMAP
)bitmap
);
1460 return (WXHBITMAP
)hbmpDst
;
1464 #endif // wxUSE_TOOLBAR && Win95