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"
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"
54 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
57 #include "wx/msw/gnuwin32/extra.h"
62 #include "wx/msw/dib.h"
63 #include "wx/app.h" // for GetComCtl32Version
65 #if defined(__MWERKS__) && defined(__WXMSW__)
66 // including <windef.h> for max definition doesn't seem
67 // to work using CodeWarrior 6 Windows. So we define it
68 // here. (Otherwise we get a undefined identifier 'max'
69 // later on in this file.) (Added by dimitri@shortcut.nl)
71 # define max(a,b) (((a) > (b)) ? (a) : (b))
76 // ----------------------------------------------------------------------------
77 // conditional compilation
78 // ----------------------------------------------------------------------------
80 // wxWindows previously always considered that toolbar buttons have light grey
81 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
82 // doesn't work with XPMs which then appear to have black background. To make
83 // this work, we must respect the bitmap masks - which we do now. This should
84 // be ok in any case, but to restore 100% compatible with the old version
85 // behaviour, you can set this to 0.
86 #define USE_BITMAP_MASKS 1
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 // these standard constants are not always defined in compilers headers
96 #define TBSTYLE_LIST 0x1000
97 #define TBSTYLE_FLAT 0x0800
100 #ifndef TBSTYLE_TRANSPARENT
101 #define TBSTYLE_TRANSPARENT 0x8000
106 #define TB_SETSTYLE (WM_USER + 56)
107 #define TB_GETSTYLE (WM_USER + 57)
111 #define TB_HITTEST (WM_USER + 69)
114 // these values correspond to those used by comctl32.dll
115 #define DEFAULTBITMAPX 16
116 #define DEFAULTBITMAPY 15
117 #define DEFAULTBUTTONX 24
118 #define DEFAULTBUTTONY 24
119 #define DEFAULTBARHEIGHT 27
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
127 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
128 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
129 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 class wxToolBarTool
: public wxToolBarToolBase
139 wxToolBarTool(wxToolBar
*tbar
,
141 const wxBitmap
& bitmap1
,
142 const wxBitmap
& bitmap2
,
144 wxObject
*clientData
,
145 const wxString
& shortHelpString
,
146 const wxString
& longHelpString
)
147 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
148 clientData
, shortHelpString
, longHelpString
)
153 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
154 : wxToolBarToolBase(tbar
, control
)
159 // set/get the number of separators which we use to cover the space used by
160 // a control in the toolbar
161 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
162 size_t GetSeparatorsCount() const { return m_nSepCount
; }
169 // ============================================================================
171 // ============================================================================
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
178 const wxBitmap
& bitmap1
,
179 const wxBitmap
& bitmap2
,
181 wxObject
*clientData
,
182 const wxString
& shortHelpString
,
183 const wxString
& longHelpString
)
185 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
186 clientData
, shortHelpString
, longHelpString
);
189 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
191 return new wxToolBarTool(this, control
);
194 // ----------------------------------------------------------------------------
195 // wxToolBar construction
196 // ----------------------------------------------------------------------------
198 void wxToolBar::Init()
204 m_defaultWidth
= DEFAULTBITMAPX
;
205 m_defaultHeight
= DEFAULTBITMAPY
;
210 bool wxToolBar::Create(wxWindow
*parent
,
215 const wxString
& name
)
217 // toolbars never have border, giving one to them results in broken
219 style
&= ~wxBORDER_MASK
;
220 style
|= wxBORDER_NONE
;
222 // common initialisation
223 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
227 DWORD msflags
= 0; // WS_VISIBLE | WS_CHILD always included
229 if ( style
& wxCLIP_SIBLINGS
)
230 msflags
|= WS_CLIPSIBLINGS
;
232 #ifdef TBSTYLE_TOOLTIPS
233 msflags
|= TBSTYLE_TOOLTIPS
;
236 if (style
& wxTB_FLAT
)
238 // static as it doesn't change during the program lifetime
239 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
241 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
242 // style with 6.00 (part of Windows XP) leads to the toolbar with
243 // incorrect background colour - and not using it still results in the
244 // correct (flat) toolbar, so don't use it there
245 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
247 msflags
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
251 // MSW-specific initialisation
252 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME
, msflags
) )
255 // toolbar-specific post initialisation
256 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
258 // set up the colors and fonts
259 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
260 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
271 height
= m_defaultHeight
;
277 SetSize(x
, y
, width
, height
);
282 wxToolBar::~wxToolBar()
284 // we must refresh the frame size when the toolbar is deleted but the frame
285 // is not - otherwise toolbar leaves a hole in the place it used to occupy
286 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
287 if ( frame
&& !frame
->IsBeingDeleted() )
289 frame
->SendSizeEvent();
294 ::DeleteObject((HBITMAP
) m_hBitmap
);
298 // ----------------------------------------------------------------------------
299 // adding/removing tools
300 // ----------------------------------------------------------------------------
302 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
303 wxToolBarToolBase
*tool
)
305 // nothing special to do here - we really create the toolbar buttons in
312 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
314 // the main difficulty we have here is with the controls in the toolbars:
315 // as we (sometimes) use several separators to cover up the space used by
316 // them, the indices are not the same for us and the toolbar
318 // first determine the position of the first button to delete: it may be
319 // different from pos if we use several separators to cover the space used
321 wxToolBarToolsList::Node
*node
;
322 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
324 wxToolBarToolBase
*tool2
= node
->GetData();
327 // let node point to the next node in the list
328 node
= node
->GetNext();
333 if ( tool2
->IsControl() )
335 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
339 // now determine the number of buttons to delete and the area taken by them
340 size_t nButtonsToDelete
= 1;
342 // get the size of the button we're going to delete
344 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
346 wxLogLastError(_T("TB_GETITEMRECT"));
349 int width
= r
.right
- r
.left
;
351 if ( tool
->IsControl() )
353 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
355 width
*= nButtonsToDelete
;
358 // do delete all buttons
359 m_nButtons
-= nButtonsToDelete
;
360 while ( nButtonsToDelete
-- > 0 )
362 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
364 wxLogLastError(wxT("TB_DELETEBUTTON"));
372 // and finally reposition all the controls after this button (the toolbar
373 // takes care of all normal items)
374 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
376 wxToolBarToolBase
*tool2
= node
->GetData();
377 if ( tool2
->IsControl() )
380 wxControl
*control
= tool2
->GetControl();
381 control
->GetPosition(&x
, NULL
);
382 control
->Move(x
- width
, -1);
389 bool wxToolBar::Realize()
391 size_t nTools
= GetToolsCount();
398 bool isVertical
= (GetWindowStyle() & wxTB_VERTICAL
) != 0;
400 // First, add the bitmap: we use one bitmap for all toolbar buttons
401 // ----------------------------------------------------------------
403 // if we already have a bitmap, we'll replace the existing one - otherwise
404 // we'll install a new one
405 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
407 int totalBitmapWidth
= (int)(m_defaultWidth
* nTools
);
408 int totalBitmapHeight
= (int)m_defaultHeight
;
410 // Create a bitmap and copy all the tool bitmaps to it
412 wxMemoryDC dcAllButtons
;
413 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
414 dcAllButtons
.SelectObject(bitmap
);
415 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
416 dcAllButtons
.Clear();
418 m_hBitmap
= bitmap
.GetHBITMAP();
419 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
420 #else // !USE_BITMAP_MASKS
421 HBITMAP hBitmap
= ::CreateCompatibleBitmap(ScreenHDC(),
426 wxLogLastError(_T("CreateCompatibleBitmap"));
431 m_hBitmap
= (WXHBITMAP
)hBitmap
;
433 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
434 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, hBitmap
);
436 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
437 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
439 // the button position
442 // the number of buttons (not separators)
445 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
448 wxToolBarToolBase
*tool
= node
->GetData();
449 if ( tool
->IsButton() )
451 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
455 // notice the last parameter: do use mask
456 dcAllButtons
.DrawBitmap(bmp
, x
, 0, TRUE
);
457 #else // !USE_BITMAP_MASKS
458 HBITMAP hbmp
= GetHbitmapOf(bmp
);
459 HBITMAP oldBitmap2
= (HBITMAP
)::SelectObject(memoryDC2
, hbmp
);
460 if ( !BitBlt(memoryDC
, x
, 0, m_defaultWidth
, m_defaultHeight
,
461 memoryDC2
, 0, 0, SRCCOPY
) )
463 wxLogLastError(wxT("BitBlt"));
466 ::SelectObject(memoryDC2
, oldBitmap2
);
467 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
471 wxFAIL_MSG( _T("invalid tool button bitmap") );
474 // still inc width and number of buttons because otherwise the
475 // subsequent buttons will all be shifted which is rather confusing
476 // (and like this you'd see immediately which bitmap was bad)
481 node
= node
->GetNext();
485 dcAllButtons
.SelectObject(wxNullBitmap
);
487 // don't delete this HBITMAP!
488 bitmap
.SetHBITMAP(0);
489 #else // !USE_BITMAP_MASKS
490 ::SelectObject(memoryDC
, oldBitmap
);
491 ::DeleteDC(memoryDC
);
492 ::DeleteDC(memoryDC2
);
493 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
495 // Map to system colours
496 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
497 totalBitmapWidth
, totalBitmapHeight
);
501 bool addBitmap
= TRUE
;
503 if ( oldToolBarBitmap
)
505 #ifdef TB_REPLACEBITMAP
506 if ( wxTheApp
->GetComCtl32Version() >= 400 )
508 TBREPLACEBITMAP replaceBitmap
;
509 replaceBitmap
.hInstOld
= NULL
;
510 replaceBitmap
.hInstNew
= NULL
;
511 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
512 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
513 replaceBitmap
.nButtons
= nButtons
;
514 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
515 0, (LPARAM
) &replaceBitmap
) )
517 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
520 ::DeleteObject(oldToolBarBitmap
);
526 #endif // TB_REPLACEBITMAP
528 // we can't replace the old bitmap, so we will add another one
529 // (awfully inefficient, but what else to do?) and shift the bitmap
530 // indices accordingly
533 bitmapId
= m_nButtons
;
536 // Now delete all the buttons
537 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
539 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
541 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
547 if ( addBitmap
) // no old bitmap or we can't replace it
549 TBADDBITMAP addBitmap
;
551 addBitmap
.nID
= (UINT
) hBitmap
;
552 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
553 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
555 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
559 // Next add the buttons and separators
560 // -----------------------------------
562 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
564 // this array will hold the indices of all controls in the toolbar
565 wxArrayInt controlIds
;
568 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
570 wxToolBarToolBase
*tool
= node
->GetData();
572 // don't add separators to the vertical toolbar - looks ugly
573 if ( isVertical
&& tool
->IsSeparator() )
576 TBBUTTON
& button
= buttons
[i
];
578 wxZeroMemory(button
);
580 switch ( tool
->GetStyle() )
582 case wxTOOL_STYLE_CONTROL
:
583 button
.idCommand
= tool
->GetId();
584 // fall through: create just a separator too
586 case wxTOOL_STYLE_SEPARATOR
:
587 button
.fsState
= TBSTATE_ENABLED
;
588 button
.fsStyle
= TBSTYLE_SEP
;
591 case wxTOOL_STYLE_BUTTON
:
592 button
.iBitmap
= bitmapId
;
593 button
.idCommand
= tool
->GetId();
595 if ( tool
->IsEnabled() )
596 button
.fsState
|= TBSTATE_ENABLED
;
597 if ( tool
->IsToggled() )
598 button
.fsState
|= TBSTATE_CHECKED
;
600 button
.fsStyle
= tool
->CanBeToggled() ? TBSTYLE_CHECK
610 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
,
611 (WPARAM
)i
, (LPARAM
)buttons
) )
613 wxLogLastError(wxT("TB_ADDBUTTONS"));
618 // Deal with the controls finally
619 // ------------------------------
621 // adjust the controls size to fit nicely in the toolbar
624 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
626 wxToolBarToolBase
*tool
= node
->GetData();
628 // we calculate the running y coord for vertical toolbars so we need to
629 // get the items size for all items but for the horizontal ones we
630 // don't need to deal with the non controls
631 bool isControl
= tool
->IsControl();
632 if ( !isControl
&& !isVertical
)
635 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
636 // latter only appeared in v4.70 of comctl32.dll
638 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
639 index
, (LPARAM
)(LPRECT
)&r
) )
641 wxLogLastError(wxT("TB_GETITEMRECT"));
646 // can only be control if isVertical
647 y
+= r
.bottom
- r
.top
;
652 wxControl
*control
= tool
->GetControl();
654 wxSize size
= control
->GetSize();
656 // the position of the leftmost controls corner
659 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
660 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
661 // available in headers, now check whether it is available now
663 if ( wxTheApp
->GetComCtl32Version() >= 471 )
665 // set the (underlying) separators width to be that of the
668 tbbi
.cbSize
= sizeof(tbbi
);
669 tbbi
.dwMask
= TBIF_SIZE
;
671 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
672 tool
->GetId(), (LPARAM
)&tbbi
) )
674 // the id is probably invalid?
675 wxLogLastError(wxT("TB_SETBUTTONINFO"));
679 #endif // comctl32.dll 4.71
680 // TB_SETBUTTONINFO unavailable
682 // try adding several separators to fit the controls width
683 int widthSep
= r
.right
- r
.left
;
689 tbb
.fsState
= TBSTATE_ENABLED
;
690 tbb
.fsStyle
= TBSTYLE_SEP
;
692 size_t nSeparators
= size
.x
/ widthSep
;
693 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
695 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
696 index
, (LPARAM
)&tbb
) )
698 wxLogLastError(wxT("TB_INSERTBUTTON"));
704 // remember the number of separators we used - we'd have to
705 // delete all of them later
706 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
708 // adjust the controls width to exactly cover the separators
709 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
712 // position the control itself correctly vertically
713 int height
= r
.bottom
- r
.top
;
714 int diff
= height
- size
.y
;
717 // the control is too high, resize to fit
718 control
->SetSize(-1, height
- 2);
729 y
+= height
+ 2*GetMargins().y
;
731 else // horizontal toolbar
739 control
->Move(left
, top
+ (diff
+ 1) / 2);
742 // the max index is the "real" number of buttons - i.e. counting even the
743 // separators which we added just for aligning the controls
748 if ( m_maxRows
== 0 )
750 // if not set yet, only one row
754 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
756 if ( m_maxRows
== 0 )
758 // if not set yet, have one column
766 // ----------------------------------------------------------------------------
768 // ----------------------------------------------------------------------------
770 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
772 wxToolBarToolBase
*tool
= FindById((int)id
);
776 if ( tool
->CanBeToggled() )
778 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
779 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
782 bool toggled
= tool
->IsToggled();
784 // OnLeftClick() can veto the button state change - for buttons which may
785 // be toggled only, of couse
786 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
790 tool
->SetToggle(toggled
);
792 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
798 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
800 WXLPARAM
*WXUNUSED(result
))
802 // First check if this applies to us
803 NMHDR
*hdr
= (NMHDR
*)lParam
;
805 // the tooltips control created by the toolbar is sometimes Unicode, even
806 // in an ANSI application - this seems to be a bug in comctl32.dll v5
807 int code
= (int)hdr
->code
;
808 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
811 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
812 if ( toolTipWnd
!= hdr
->hwndFrom
)
815 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
816 int id
= (int)ttText
->hdr
.idFrom
;
818 wxToolBarToolBase
*tool
= FindById(id
);
822 const wxString
& help
= tool
->GetShortHelp();
824 if ( !help
.IsEmpty() )
826 if ( code
== TTN_NEEDTEXTA
)
828 ttText
->lpszText
= (wxChar
*)help
.c_str();
833 ttText
->lpszText
= (wxChar
*)help
.c_str();
835 // VZ: I don't know why it happens, but the versions of
836 // comctl32.dll starting from 4.70 sometimes send TTN_NEEDTEXTW
837 // even to ANSI programs (normally, this message is supposed
838 // to be sent to Unicode programs only) - hence we need to
839 // handle it as well, otherwise no tooltips will be shown in
842 size_t lenAnsi
= help
.Len();
843 #if defined( __MWERKS__ ) || defined( __CYGWIN__ )
844 // MetroWerks doesn't like calling mbstowcs with NULL argument
845 // neither Cygwin does
846 size_t lenUnicode
= 2*lenAnsi
;
848 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
851 // using the pointer of right type avoids us doing all sorts of
852 // pointer arithmetics ourselves
853 wchar_t *dst
= (wchar_t *)ttText
->szText
,
854 *pwz
= new wchar_t[lenUnicode
+ 1];
855 mbstowcs(pwz
, help
, lenAnsi
+ 1);
856 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
858 // put the terminating _wide_ NUL
869 // ----------------------------------------------------------------------------
871 // ----------------------------------------------------------------------------
873 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
875 wxToolBarBase::SetToolBitmapSize(size
);
877 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
880 void wxToolBar::SetRows(int nRows
)
882 if ( nRows
== m_maxRows
)
884 // avoid resizing the frame uselessly
888 // TRUE in wParam means to create at least as many rows, FALSE -
891 ::SendMessage(GetHwnd(), TB_SETROWS
,
892 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
900 // The button size is bigger than the bitmap size
901 wxSize
wxToolBar::GetToolSize() const
903 // TB_GETBUTTONSIZE is supported from version 4.70
904 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
905 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
906 if ( wxTheApp
->GetComCtl32Version() >= 470 )
908 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
910 return wxSize(LOWORD(dw
), HIWORD(dw
));
913 #endif // comctl32.dll 4.70+
916 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
921 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
924 wxToolBarToolsList::Node
* current
= tools
.GetFirst();
926 for ( ; current
!= 0; current
= current
->GetNext() )
929 return current
->GetData();
931 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
932 size_t separators
= tool
->GetSeparatorsCount();
934 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
935 // otherwise, skip as many items as the separator count, plus the
937 index
-= separators
? separators
+ 1 : 1;
943 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
948 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
949 // MBN: when the point ( x, y ) is close to the toolbar border
950 // TB_HITTEST returns m_nButtons ( not -1 )
951 if ( index
< 0 || (size_t)index
>= m_nButtons
)
953 // it's a separator or there is no tool at all there
954 return (wxToolBarToolBase
*)NULL
;
957 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
958 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
959 if ( wxTheApp
->GetComCtl32Version() >= 471 )
961 return m_tools
.Item((size_t)index
)->GetData();
966 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
970 void wxToolBar::UpdateSize()
972 // the toolbar size changed
973 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
975 // we must also refresh the frame after the toolbar size (possibly) changed
976 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
979 frame
->SendSizeEvent();
983 // ----------------------------------------------------------------------------
985 // ----------------------------------------------------------------------------
987 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
989 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
990 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
993 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
995 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
996 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
999 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1001 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1002 // without, so we really need to delete the button and recreate it here
1003 wxFAIL_MSG( _T("not implemented") );
1006 // ----------------------------------------------------------------------------
1008 // ----------------------------------------------------------------------------
1010 // Responds to colour changes, and passes event on to children.
1011 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1013 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1015 // Remap the buttons
1018 // Relayout the toolbar
1019 int nrows
= m_maxRows
;
1020 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1025 // let the event propagate further
1029 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1031 if (event
.RightDown())
1033 // For now, we don't have an id. Later we could
1034 // try finding the tool.
1035 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1043 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1045 if ( nMsg
== WM_SIZE
)
1047 // calculate our minor dimenstion ourselves - we're confusing the
1048 // standard logic (TB_AUTOSIZE) with our horizontal toolbars and other
1051 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1055 if ( GetWindowStyle() & wxTB_VERTICAL
)
1057 w
= r
.right
- r
.left
;
1060 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1067 h
= r
.bottom
- r
.top
;
1070 h
+= 6; // FIXME: this is the separator line height...
1075 if ( MAKELPARAM(w
, h
) != lParam
)
1077 // size really changed
1081 // message processed
1085 else if ( nMsg
== WM_MOUSEMOVE
)
1087 wxCoord x
= GET_X_LPARAM(lParam
), y
= GET_Y_LPARAM(lParam
);
1088 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1090 // cursor left current tool
1091 if( tool
!= m_pInTool
&& !tool
)
1097 // cursor entered a tool
1098 if( tool
!= m_pInTool
&& tool
)
1101 OnMouseEnter( tool
->GetId() );
1104 // we don't handle mouse moves, so fall through
1105 // to wxControl::MSWWindowProc
1108 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1111 // ----------------------------------------------------------------------------
1112 // private functions
1113 // ----------------------------------------------------------------------------
1115 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1121 wxLogLastError(_T("CreateCompatibleDC"));
1126 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1130 wxLogLastError(_T("SelectObject"));
1135 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1137 for ( int i
= 0; i
< width
; i
++ )
1139 for ( int j
= 0; j
< height
; j
++ )
1141 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1143 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1145 COLORREF col
= cmap
[k
].from
;
1146 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1147 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1148 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1150 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1159 // VZ: I leave here my attempts to map the bitmap to the system colours
1160 // faster by using BitBlt() even though it's broken currently - but
1161 // maybe someone else can finish it? It should be faster than iterating
1162 // over all pixels...
1164 MemoryHDC hdcMask
, hdcDst
;
1165 if ( !hdcMask
|| !hdcDst
)
1167 wxLogLastError(_T("CreateCompatibleDC"));
1172 // create the target bitmap
1173 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1176 wxLogLastError(_T("CreateCompatibleBitmap"));
1181 // create the monochrome mask bitmap
1182 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1185 wxLogLastError(_T("CreateBitmap(mono)"));
1187 ::DeleteObject(hbmpDst
);
1192 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1193 bmpInMask(hdcMask
, hbmpMask
);
1196 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1198 // create the mask for this colour
1199 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1200 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1202 // replace this colour with the target one in the dst bitmap
1203 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1204 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1206 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1209 MAKEROP4(PATCOPY
, SRCCOPY
));
1211 (void)::SelectObject(hdcDst
, hbrOld
);
1212 ::DeleteObject(hbr
);
1215 ::DeleteObject((HBITMAP
)bitmap
);
1217 return (WXHBITMAP
)hbmpDst
;
1221 #endif // wxUSE_TOOLBAR && Win95