1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tbar95.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
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"
41 #if wxUSE_TOOLBAR && defined(__WIN95__) && wxUSE_TOOLBAR_NATIVE
43 #include "wx/toolbar.h"
45 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
49 #include "wx/msw/private.h"
53 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
56 #include "wx/msw/gnuwin32/extra.h"
61 #include "wx/msw/dib.h"
62 #include "wx/app.h" // for GetComCtl32Version
64 #if defined(__MWERKS__) && defined(__WXMSW__)
65 // including <windef.h> for max definition doesn't seem
66 // to work using CodeWarrior 6 Windows. So we define it
67 // here. (Otherwise we get a undefined identifier 'max'
68 // later on in this file.) (Added by dimitri@shortcut.nl)
70 # define max(a,b) (((a) > (b)) ? (a) : (b))
75 // ----------------------------------------------------------------------------
76 // conditional compilation
77 // ----------------------------------------------------------------------------
79 // wxWindows previously always considered that toolbar buttons have light grey
80 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
81 // doesn't work with XPMs which then appear to have black background. To make
82 // this work, we must respect the bitmap masks - which we do now. This should
83 // be ok in any case, but to restore 100% compatible with the old version
84 // behaviour, you can set this to 0.
85 #define USE_BITMAP_MASKS 1
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // these standard constants are not always defined in compilers headers
95 #define TBSTYLE_LIST 0x1000
96 #define TBSTYLE_FLAT 0x0800
97 #define TBSTYLE_TRANSPARENT 0x8000
99 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
103 #define TB_SETSTYLE (WM_USER + 56)
104 #define TB_GETSTYLE (WM_USER + 57)
108 #define TB_HITTEST (WM_USER + 69)
111 // these values correspond to those used by comctl32.dll
112 #define DEFAULTBITMAPX 16
113 #define DEFAULTBITMAPY 15
114 #define DEFAULTBUTTONX 24
115 #define DEFAULTBUTTONY 24
116 #define DEFAULTBARHEIGHT 27
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
124 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
125 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
126 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 class wxToolBarTool
: public wxToolBarToolBase
136 wxToolBarTool(wxToolBar
*tbar
,
138 const wxBitmap
& bitmap1
,
139 const wxBitmap
& bitmap2
,
141 wxObject
*clientData
,
142 const wxString
& shortHelpString
,
143 const wxString
& longHelpString
)
144 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
145 clientData
, shortHelpString
, longHelpString
)
150 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
151 : wxToolBarToolBase(tbar
, control
)
156 // set/get the number of separators which we use to cover the space used by
157 // a control in the toolbar
158 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
159 size_t GetSeparatorsCount() const { return m_nSepCount
; }
166 // ============================================================================
168 // ============================================================================
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
175 const wxBitmap
& bitmap1
,
176 const wxBitmap
& bitmap2
,
178 wxObject
*clientData
,
179 const wxString
& shortHelpString
,
180 const wxString
& longHelpString
)
182 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
183 clientData
, shortHelpString
, longHelpString
);
186 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
188 return new wxToolBarTool(this, control
);
191 // ----------------------------------------------------------------------------
192 // wxToolBar construction
193 // ----------------------------------------------------------------------------
195 void wxToolBar::Init()
201 m_defaultWidth
= DEFAULTBITMAPX
;
202 m_defaultHeight
= DEFAULTBITMAPY
;
207 bool wxToolBar::Create(wxWindow
*parent
,
212 const wxString
& name
)
214 // toolbars never have border, giving one to them results in broken
216 style
&= ~wxBORDER_MASK
;
217 style
|= wxBORDER_NONE
;
219 // common initialisation
220 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
224 DWORD msflags
= 0; // WS_VISIBLE | WS_CHILD always included
226 if ( style
& wxCLIP_SIBLINGS
)
227 msflags
|= WS_CLIPSIBLINGS
;
229 #ifdef TBSTYLE_TOOLTIPS
230 msflags
|= TBSTYLE_TOOLTIPS
;
233 if (style
& wxTB_FLAT
)
235 if (wxTheApp
->GetComCtl32Version() > 400)
236 msflags
|= TBSTYLE_FLAT
;
239 // MSW-specific initialisation
240 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME
, msflags
) )
243 // toolbar-specific post initialisation
244 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
246 // set up the colors and fonts
247 wxRGBToColour(m_backgroundColour
, GetSysColor(COLOR_BTNFACE
));
248 m_foregroundColour
= *wxBLACK
;
250 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
261 height
= m_defaultHeight
;
267 SetSize(x
, y
, width
, height
);
272 wxToolBar::~wxToolBar()
274 // we must refresh the frame size when the toolbar is deleted but the frame
275 // is not - otherwise toolbar leaves a hole in the place it used to occupy
276 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
277 if ( frame
&& !frame
->IsBeingDeleted() )
279 frame
->SendSizeEvent();
284 ::DeleteObject((HBITMAP
) m_hBitmap
);
288 // ----------------------------------------------------------------------------
289 // adding/removing tools
290 // ----------------------------------------------------------------------------
292 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
293 wxToolBarToolBase
*tool
)
295 // nothing special to do here - we really create the toolbar buttons in
302 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
304 // the main difficulty we have here is with the controls in the toolbars:
305 // as we (sometimes) use several separators to cover up the space used by
306 // them, the indices are not the same for us and the toolbar
308 // first determine the position of the first button to delete: it may be
309 // different from pos if we use several separators to cover the space used
311 wxToolBarToolsList::Node
*node
;
312 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
314 wxToolBarToolBase
*tool2
= node
->GetData();
317 // let node point to the next node in the list
318 node
= node
->GetNext();
323 if ( tool2
->IsControl() )
325 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
329 // now determine the number of buttons to delete and the area taken by them
330 size_t nButtonsToDelete
= 1;
332 // get the size of the button we're going to delete
334 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
336 wxLogLastError(_T("TB_GETITEMRECT"));
339 int width
= r
.right
- r
.left
;
341 if ( tool
->IsControl() )
343 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
345 width
*= nButtonsToDelete
;
348 // do delete all buttons
349 m_nButtons
-= nButtonsToDelete
;
350 while ( nButtonsToDelete
-- > 0 )
352 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
354 wxLogLastError(wxT("TB_DELETEBUTTON"));
362 // and finally reposition all the controls after this button (the toolbar
363 // takes care of all normal items)
364 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
366 wxToolBarToolBase
*tool2
= node
->GetData();
367 if ( tool2
->IsControl() )
370 wxControl
*control
= tool2
->GetControl();
371 control
->GetPosition(&x
, NULL
);
372 control
->Move(x
- width
, -1);
379 bool wxToolBar::Realize()
381 size_t nTools
= GetToolsCount();
388 bool isVertical
= (GetWindowStyle() & wxTB_VERTICAL
) != 0;
390 // First, add the bitmap: we use one bitmap for all toolbar buttons
391 // ----------------------------------------------------------------
393 // if we already have a bitmap, we'll replace the existing one - otherwise
394 // we'll install a new one
395 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
397 int totalBitmapWidth
= (int)(m_defaultWidth
* nTools
);
398 int totalBitmapHeight
= (int)m_defaultHeight
;
400 // Create a bitmap and copy all the tool bitmaps to it
402 wxMemoryDC dcAllButtons
;
403 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
404 dcAllButtons
.SelectObject(bitmap
);
405 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
406 dcAllButtons
.Clear();
408 m_hBitmap
= bitmap
.GetHBITMAP();
409 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
410 #else // !USE_BITMAP_MASKS
411 HBITMAP hBitmap
= ::CreateCompatibleBitmap(ScreenHDC(),
416 wxLogLastError(_T("CreateCompatibleBitmap"));
421 m_hBitmap
= (WXHBITMAP
)hBitmap
;
423 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
424 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, hBitmap
);
426 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
427 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
429 // the button position
432 // the number of buttons (not separators)
435 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
438 wxToolBarToolBase
*tool
= node
->GetData();
439 if ( tool
->IsButton() )
441 const wxBitmap
& bmp
= tool
->GetBitmap1();
445 // notice the last parameter: do use mask
446 dcAllButtons
.DrawBitmap(tool
->GetBitmap1(), x
, 0, TRUE
);
447 #else // !USE_BITMAP_MASKS
448 HBITMAP hbmp
= GetHbitmapOf(bmp
);
449 HBITMAP oldBitmap2
= (HBITMAP
)::SelectObject(memoryDC2
, hbmp
);
450 if ( !BitBlt(memoryDC
, x
, 0, m_defaultWidth
, m_defaultHeight
,
451 memoryDC2
, 0, 0, SRCCOPY
) )
453 wxLogLastError(wxT("BitBlt"));
456 ::SelectObject(memoryDC2
, oldBitmap2
);
457 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
461 wxFAIL_MSG( _T("invalid tool button bitmap") );
464 // still inc width and number of buttons because otherwise the
465 // subsequent buttons will all be shifted which is rather confusing
466 // (and like this you'd see immediately which bitmap was bad)
471 node
= node
->GetNext();
475 dcAllButtons
.SelectObject(wxNullBitmap
);
477 // don't delete this HBITMAP!
478 bitmap
.SetHBITMAP(0);
479 #else // !USE_BITMAP_MASKS
480 ::SelectObject(memoryDC
, oldBitmap
);
481 ::DeleteDC(memoryDC
);
482 ::DeleteDC(memoryDC2
);
483 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
485 // Map to system colours
486 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
487 totalBitmapWidth
, totalBitmapHeight
);
491 bool addBitmap
= TRUE
;
493 if ( oldToolBarBitmap
)
495 #ifdef TB_REPLACEBITMAP
496 if ( wxTheApp
->GetComCtl32Version() >= 400 )
498 TBREPLACEBITMAP replaceBitmap
;
499 replaceBitmap
.hInstOld
= NULL
;
500 replaceBitmap
.hInstNew
= NULL
;
501 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
502 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
503 replaceBitmap
.nButtons
= nButtons
;
504 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
505 0, (LPARAM
) &replaceBitmap
) )
507 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
510 ::DeleteObject(oldToolBarBitmap
);
516 #endif // TB_REPLACEBITMAP
518 // we can't replace the old bitmap, so we will add another one
519 // (awfully inefficient, but what else to do?) and shift the bitmap
520 // indices accordingly
523 bitmapId
= m_nButtons
;
526 // Now delete all the buttons
527 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
529 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
531 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
537 if ( addBitmap
) // no old bitmap or we can't replace it
539 TBADDBITMAP addBitmap
;
541 addBitmap
.nID
= (UINT
) hBitmap
;
542 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
543 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
545 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
549 // Next add the buttons and separators
550 // -----------------------------------
552 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
554 // this array will hold the indices of all controls in the toolbar
555 wxArrayInt controlIds
;
558 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
560 wxToolBarToolBase
*tool
= node
->GetData();
562 // don't add separators to the vertical toolbar - looks ugly
563 if ( isVertical
&& tool
->IsSeparator() )
566 TBBUTTON
& button
= buttons
[i
];
568 wxZeroMemory(button
);
570 switch ( tool
->GetStyle() )
572 case wxTOOL_STYLE_CONTROL
:
573 button
.idCommand
= tool
->GetId();
574 // fall through: create just a separator too
576 case wxTOOL_STYLE_SEPARATOR
:
577 button
.fsState
= TBSTATE_ENABLED
;
578 button
.fsStyle
= TBSTYLE_SEP
;
581 case wxTOOL_STYLE_BUTTON
:
582 button
.iBitmap
= bitmapId
;
583 button
.idCommand
= tool
->GetId();
585 if ( tool
->IsEnabled() )
586 button
.fsState
|= TBSTATE_ENABLED
;
587 if ( tool
->IsToggled() )
588 button
.fsState
|= TBSTATE_CHECKED
;
590 button
.fsStyle
= tool
->CanBeToggled() ? TBSTYLE_CHECK
600 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
,
601 (WPARAM
)i
, (LPARAM
)buttons
) )
603 wxLogLastError(wxT("TB_ADDBUTTONS"));
608 // Deal with the controls finally
609 // ------------------------------
611 // adjust the controls size to fit nicely in the toolbar
614 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
616 wxToolBarToolBase
*tool
= node
->GetData();
618 // we calculate the running y coord for vertical toolbars so we need to
619 // get the items size for all items but for the horizontal ones we
620 // don't need to deal with the non controls
621 bool isControl
= tool
->IsControl();
622 if ( !isControl
&& !isVertical
)
625 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
626 // latter only appeared in v4.70 of comctl32.dll
628 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
629 index
, (LPARAM
)(LPRECT
)&r
) )
631 wxLogLastError(wxT("TB_GETITEMRECT"));
636 // can only be control if isVertical
637 y
+= r
.bottom
- r
.top
;
642 wxControl
*control
= tool
->GetControl();
644 wxSize size
= control
->GetSize();
646 // the position of the leftmost controls corner
649 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
650 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
651 // available in headers, now check whether it is available now
653 if ( wxTheApp
->GetComCtl32Version() >= 471 )
655 // set the (underlying) separators width to be that of the
658 tbbi
.cbSize
= sizeof(tbbi
);
659 tbbi
.dwMask
= TBIF_SIZE
;
661 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
662 tool
->GetId(), (LPARAM
)&tbbi
) )
664 // the id is probably invalid?
665 wxLogLastError(wxT("TB_SETBUTTONINFO"));
669 #endif // comctl32.dll 4.71
670 // TB_SETBUTTONINFO unavailable
672 // try adding several separators to fit the controls width
673 int widthSep
= r
.right
- r
.left
;
679 tbb
.fsState
= TBSTATE_ENABLED
;
680 tbb
.fsStyle
= TBSTYLE_SEP
;
682 size_t nSeparators
= size
.x
/ widthSep
;
683 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
685 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
686 index
, (LPARAM
)&tbb
) )
688 wxLogLastError(wxT("TB_INSERTBUTTON"));
694 // remember the number of separators we used - we'd have to
695 // delete all of them later
696 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
698 // adjust the controls width to exactly cover the separators
699 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
702 // position the control itself correctly vertically
703 int height
= r
.bottom
- r
.top
;
704 int diff
= height
- size
.y
;
707 // the control is too high, resize to fit
708 control
->SetSize(-1, height
- 2);
719 y
+= height
+ 2*GetMargins().y
;
721 else // horizontal toolbar
729 control
->Move(left
, top
+ (diff
+ 1) / 2);
732 // the max index is the "real" number of buttons - i.e. counting even the
733 // separators which we added just for aligning the controls
738 if ( m_maxRows
== 0 )
740 // if not set yet, only one row
744 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
746 if ( m_maxRows
== 0 )
748 // if not set yet, have one column
756 // ----------------------------------------------------------------------------
758 // ----------------------------------------------------------------------------
760 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
762 wxToolBarToolBase
*tool
= FindById((int)id
);
766 if ( tool
->CanBeToggled() )
768 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
769 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
772 bool toggled
= tool
->IsToggled();
774 // OnLeftClick() can veto the button state change - for buttons which may
775 // be toggled only, of couse
776 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
780 tool
->SetToggle(toggled
);
782 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
788 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
790 WXLPARAM
*WXUNUSED(result
))
792 // First check if this applies to us
793 NMHDR
*hdr
= (NMHDR
*)lParam
;
795 // the tooltips control created by the toolbar is sometimes Unicode, even
796 // in an ANSI application - this seems to be a bug in comctl32.dll v5
797 int code
= (int)hdr
->code
;
798 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
801 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
802 if ( toolTipWnd
!= hdr
->hwndFrom
)
805 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
806 int id
= (int)ttText
->hdr
.idFrom
;
808 wxToolBarToolBase
*tool
= FindById(id
);
812 const wxString
& help
= tool
->GetShortHelp();
814 if ( !help
.IsEmpty() )
816 if ( code
== TTN_NEEDTEXTA
)
818 ttText
->lpszText
= (wxChar
*)help
.c_str();
823 ttText
->lpszText
= (wxChar
*)help
.c_str();
825 // VZ: I don't know why it happens, but the versions of
826 // comctl32.dll starting from 4.70 sometimes send TTN_NEEDTEXTW
827 // even to ANSI programs (normally, this message is supposed
828 // to be sent to Unicode programs only) - hence we need to
829 // handle it as well, otherwise no tooltips will be shown in
832 size_t lenAnsi
= help
.Len();
833 #if defined( __MWERKS__ ) || defined( __CYGWIN__ )
834 // MetroWerks doesn't like calling mbstowcs with NULL argument
835 // neither Cygwin does
836 size_t lenUnicode
= 2*lenAnsi
;
838 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
841 // using the pointer of right type avoids us doing all sorts of
842 // pointer arithmetics ourselves
843 wchar_t *dst
= (wchar_t *)ttText
->szText
,
844 *pwz
= new wchar_t[lenUnicode
+ 1];
845 mbstowcs(pwz
, help
, lenAnsi
+ 1);
846 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
848 // put the terminating _wide_ NUL
859 // ----------------------------------------------------------------------------
861 // ----------------------------------------------------------------------------
863 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
865 wxToolBarBase::SetToolBitmapSize(size
);
867 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
870 void wxToolBar::SetRows(int nRows
)
872 if ( nRows
== m_maxRows
)
874 // avoid resizing the frame uselessly
878 // TRUE in wParam means to create at least as many rows, FALSE -
881 ::SendMessage(GetHwnd(), TB_SETROWS
,
882 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
890 // The button size is bigger than the bitmap size
891 wxSize
wxToolBar::GetToolSize() const
893 // TB_GETBUTTONSIZE is supported from version 4.70
894 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
895 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
896 if ( wxTheApp
->GetComCtl32Version() >= 470 )
898 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
900 return wxSize(LOWORD(dw
), HIWORD(dw
));
903 #endif // comctl32.dll 4.70+
906 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
911 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
914 wxToolBarToolsList::Node
* current
= tools
.GetFirst();
916 for ( ; current
!= 0; current
= current
->GetNext() )
919 return current
->GetData();
921 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
922 size_t separators
= tool
->GetSeparatorsCount();
924 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
925 // otherwise, skip as many items as the separator count, plus the
927 index
-= separators
? separators
+ 1 : 1;
933 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
938 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
939 // MBN: when the point ( x, y ) is close to the toolbar border
940 // TB_HITTEST returns m_nButtons ( not -1 )
941 if ( index
< 0 || (size_t)index
>= m_nButtons
)
943 // it's a separator or there is no tool at all there
944 return (wxToolBarToolBase
*)NULL
;
947 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
948 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
949 if ( wxTheApp
->GetComCtl32Version() >= 471 )
951 return m_tools
.Item((size_t)index
)->GetData();
956 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
960 void wxToolBar::UpdateSize()
962 // the toolbar size changed
963 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
965 // we must also refresh the frame after the toolbar size (possibly) changed
966 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
969 frame
->SendSizeEvent();
973 // ----------------------------------------------------------------------------
975 // ----------------------------------------------------------------------------
977 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
979 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
980 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
983 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
985 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
986 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
989 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
991 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
992 // without, so we really need to delete the button and recreate it here
993 wxFAIL_MSG( _T("not implemented") );
996 // ----------------------------------------------------------------------------
998 // ----------------------------------------------------------------------------
1000 // Responds to colour changes, and passes event on to children.
1001 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1003 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1005 // Remap the buttons
1008 // Relayout the toolbar
1009 int nrows
= m_maxRows
;
1010 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1015 // let the event propagate further
1019 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1021 if (event
.RightDown())
1023 // For now, we don't have an id. Later we could
1024 // try finding the tool.
1025 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1033 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1035 if ( nMsg
== WM_SIZE
)
1037 // calculate our minor dimenstion ourselves - we're confusing the
1038 // standard logic (TB_AUTOSIZE) with our horizontal toolbars and other
1041 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1045 if ( GetWindowStyle() & wxTB_VERTICAL
)
1047 w
= r
.right
- r
.left
;
1050 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1057 h
= r
.bottom
- r
.top
;
1060 h
+= 6; // FIXME: this is the separator line height...
1065 if ( MAKELPARAM(w
, h
) != lParam
)
1067 // size really changed
1071 // message processed
1075 else if ( nMsg
== WM_MOUSEMOVE
)
1077 wxCoord x
= GET_X_LPARAM(lParam
), y
= GET_Y_LPARAM(lParam
);
1078 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1080 // cursor left current tool
1081 if( tool
!= m_pInTool
&& !tool
)
1087 // cursor entered a tool
1088 if( tool
!= m_pInTool
&& tool
)
1091 OnMouseEnter( tool
->GetId() );
1094 // we don't handle mouse moves, so fall through
1095 // to wxControl::MSWWindowProc
1098 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1101 // ----------------------------------------------------------------------------
1102 // private functions
1103 // ----------------------------------------------------------------------------
1105 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1111 wxLogLastError(_T("CreateCompatibleDC"));
1116 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1120 wxLogLastError(_T("SelectObject"));
1125 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1127 for ( int i
= 0; i
< width
; i
++ )
1129 for ( int j
= 0; j
< height
; j
++ )
1131 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1133 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1135 COLORREF col
= cmap
[k
].from
;
1136 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1137 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1138 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1140 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1149 // VZ: I leave here my attempts to map the bitmap to the system colours
1150 // faster by using BitBlt() even though it's broken currently - but
1151 // maybe someone else can finish it? It should be faster than iterating
1152 // over all pixels...
1154 MemoryHDC hdcMask
, hdcDst
;
1155 if ( !hdcMask
|| !hdcDst
)
1157 wxLogLastError(_T("CreateCompatibleDC"));
1162 // create the target bitmap
1163 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1166 wxLogLastError(_T("CreateCompatibleBitmap"));
1171 // create the monochrome mask bitmap
1172 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1175 wxLogLastError(_T("CreateBitmap(mono)"));
1177 ::DeleteObject(hbmpDst
);
1182 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1183 bmpInMask(hdcMask
, hbmpMask
);
1186 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1188 // create the mask for this colour
1189 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1190 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1192 // replace this colour with the target one in the dst bitmap
1193 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1194 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1196 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1199 MAKEROP4(PATCOPY
, SRCCOPY
));
1201 (void)::SelectObject(hdcDst
, hbrOld
);
1202 ::DeleteObject(hbr
);
1205 ::DeleteObject((HBITMAP
)bitmap
);
1207 return (WXHBITMAP
)hbmpDst
;
1211 #endif // wxUSE_TOOLBAR && Win95