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 // ----------------------------------------------------------------------------
65 // conditional compilation
66 // ----------------------------------------------------------------------------
68 // wxWindows previously always considered that toolbar buttons have light grey
69 // (0xc0c0c0) background and so ignored any bitmap masks - however, this
70 // doesn't work with XPMs which then appear to have black background. To make
71 // this work, we must respect the bitmap masks - which we do now. This should
72 // be ok in any case, but to restore 100% compatible with the old version
73 // behaviour, you can set this to 0.
74 #define USE_BITMAP_MASKS 1
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // these standard constants are not always defined in compilers headers
84 #define TBSTYLE_LIST 0x1000
85 #define TBSTYLE_FLAT 0x0800
86 #define TBSTYLE_TRANSPARENT 0x8000
88 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
92 #define TB_SETSTYLE (WM_USER + 56)
93 #define TB_GETSTYLE (WM_USER + 57)
97 #define TB_HITTEST (WM_USER + 69)
100 // these values correspond to those used by comctl32.dll
101 #define DEFAULTBITMAPX 16
102 #define DEFAULTBITMAPY 15
103 #define DEFAULTBUTTONX 24
104 #define DEFAULTBUTTONY 24
105 #define DEFAULTBARHEIGHT 27
107 // ----------------------------------------------------------------------------
108 // private function prototypes
109 // ----------------------------------------------------------------------------
111 // adjust toolbar bitmap colours
112 // static void wxMapBitmap(HBITMAP hBitmap, int width, int height);
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
120 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
121 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
122 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 class wxToolBarTool
: public wxToolBarToolBase
132 wxToolBarTool(wxToolBar
*tbar
,
134 const wxBitmap
& bitmap1
,
135 const wxBitmap
& bitmap2
,
137 wxObject
*clientData
,
138 const wxString
& shortHelpString
,
139 const wxString
& longHelpString
)
140 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
141 clientData
, shortHelpString
, longHelpString
)
146 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
147 : wxToolBarToolBase(tbar
, control
)
152 // set/get the number of separators which we use to cover the space used by
153 // a control in the toolbar
154 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
155 size_t GetSeparatorsCount() const { return m_nSepCount
; }
162 // ============================================================================
164 // ============================================================================
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
171 const wxBitmap
& bitmap1
,
172 const wxBitmap
& bitmap2
,
174 wxObject
*clientData
,
175 const wxString
& shortHelpString
,
176 const wxString
& longHelpString
)
178 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
179 clientData
, shortHelpString
, longHelpString
);
182 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
184 return new wxToolBarTool(this, control
);
187 // ----------------------------------------------------------------------------
188 // wxToolBar construction
189 // ----------------------------------------------------------------------------
191 void wxToolBar::Init()
197 m_defaultWidth
= DEFAULTBITMAPX
;
198 m_defaultHeight
= DEFAULTBITMAPY
;
203 bool wxToolBar::Create(wxWindow
*parent
,
208 const wxString
& name
)
210 // common initialisation
211 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
215 DWORD msflags
= 0; // WS_VISIBLE | WS_CHILD always included
216 if (style
& wxBORDER
)
217 msflags
|= WS_BORDER
;
219 if ( style
& wxCLIP_SIBLINGS
)
220 msflags
|= WS_CLIPSIBLINGS
;
222 #ifdef TBSTYLE_TOOLTIPS
223 msflags
|= TBSTYLE_TOOLTIPS
;
226 if (style
& wxTB_FLAT
)
228 if (wxTheApp
->GetComCtl32Version() > 400)
229 msflags
|= TBSTYLE_FLAT
;
232 // MSW-specific initialisation
233 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME
, msflags
) )
236 // toolbar-specific post initialisation
237 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
239 // set up the colors and fonts
240 wxRGBToColour(m_backgroundColour
, GetSysColor(COLOR_BTNFACE
));
241 m_foregroundColour
= *wxBLACK
;
243 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
254 height
= m_defaultHeight
;
260 SetSize(x
, y
, width
, height
);
265 wxToolBar::~wxToolBar()
267 // we must refresh the frame size when the toolbar is deleted but the frame
268 // is not - otherwise toolbar leaves a hole in the place it used to occupy
269 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
270 if ( frame
&& !frame
->IsBeingDeleted() )
272 frame
->SendSizeEvent();
277 ::DeleteObject((HBITMAP
) m_hBitmap
);
281 // ----------------------------------------------------------------------------
282 // adding/removing tools
283 // ----------------------------------------------------------------------------
285 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
286 wxToolBarToolBase
*tool
)
288 // nothing special to do here - we really create the toolbar buttons in
295 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
297 // the main difficulty we have here is with the controls in the toolbars:
298 // as we (sometimes) use several separators to cover up the space used by
299 // them, the indices are not the same for us and the toolbar
301 // first determine the position of the first button to delete: it may be
302 // different from pos if we use several separators to cover the space used
304 wxToolBarToolsList::Node
*node
;
305 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
307 wxToolBarToolBase
*tool2
= node
->GetData();
310 // let node point to the next node in the list
311 node
= node
->GetNext();
316 if ( tool2
->IsControl() )
318 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
322 // now determine the number of buttons to delete and the area taken by them
323 size_t nButtonsToDelete
= 1;
325 // get the size of the button we're going to delete
327 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
329 wxLogLastError(_T("TB_GETITEMRECT"));
332 int width
= r
.right
- r
.left
;
334 if ( tool
->IsControl() )
336 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
338 width
*= nButtonsToDelete
;
341 // do delete all buttons
342 m_nButtons
-= nButtonsToDelete
;
343 while ( nButtonsToDelete
-- > 0 )
345 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
347 wxLogLastError(wxT("TB_DELETEBUTTON"));
355 // and finally reposition all the controls after this button (the toolbar
356 // takes care of all normal items)
357 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
359 wxToolBarToolBase
*tool2
= node
->GetData();
360 if ( tool2
->IsControl() )
363 wxControl
*control
= tool2
->GetControl();
364 control
->GetPosition(&x
, NULL
);
365 control
->Move(x
- width
, -1);
372 bool wxToolBar::Realize()
374 size_t nTools
= GetToolsCount();
381 bool isVertical
= (GetWindowStyle() & wxTB_VERTICAL
) != 0;
383 // First, add the bitmap: we use one bitmap for all toolbar buttons
384 // ----------------------------------------------------------------
386 // if we already have a bitmap, we'll replace the existing one - otherwise
387 // we'll install a new one
388 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
390 int totalBitmapWidth
= (int)(m_defaultWidth
* nTools
);
391 int totalBitmapHeight
= (int)m_defaultHeight
;
393 // Create a bitmap and copy all the tool bitmaps to it
395 wxMemoryDC dcAllButtons
;
396 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
397 dcAllButtons
.SelectObject(bitmap
);
398 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
399 dcAllButtons
.Clear();
401 m_hBitmap
= bitmap
.GetHBITMAP();
402 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
403 #else // !USE_BITMAP_MASKS
404 HBITMAP hBitmap
= ::CreateCompatibleBitmap(ScreenHDC(),
409 wxLogLastError(_T("CreateCompatibleBitmap"));
414 m_hBitmap
= (WXHBITMAP
)hBitmap
;
416 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
417 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, hBitmap
);
419 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
420 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
422 // the button position
425 // the number of buttons (not separators)
428 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
431 wxToolBarToolBase
*tool
= node
->GetData();
432 if ( tool
->IsButton() )
434 const wxBitmap
& bmp
= tool
->GetBitmap1();
438 // notice the last parameter: do use mask
439 dcAllButtons
.DrawBitmap(tool
->GetBitmap1(), x
, 0, TRUE
);
440 #else // !USE_BITMAP_MASKS
441 HBITMAP hbmp
= GetHbitmapOf(bmp
);
442 HBITMAP oldBitmap2
= (HBITMAP
)::SelectObject(memoryDC2
, hbmp
);
443 if ( !BitBlt(memoryDC
, x
, 0, m_defaultWidth
, m_defaultHeight
,
444 memoryDC2
, 0, 0, SRCCOPY
) )
446 wxLogLastError(wxT("BitBlt"));
449 ::SelectObject(memoryDC2
, oldBitmap2
);
450 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
454 wxFAIL_MSG( _T("invalid tool button bitmap") );
457 // still inc width and number of buttons because otherwise the
458 // subsequent buttons will all be shifted which is rather confusing
459 // (and like this you'd see immediately which bitmap was bad)
464 node
= node
->GetNext();
468 dcAllButtons
.SelectObject(wxNullBitmap
);
470 // don't delete this HBITMAP!
471 bitmap
.SetHBITMAP(0);
472 #else // !USE_BITMAP_MASKS
473 ::SelectObject(memoryDC
, oldBitmap
);
474 ::DeleteDC(memoryDC
);
475 ::DeleteDC(memoryDC2
);
476 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
478 // Map to system colours
479 MapBitmap((WXHBITMAP
) hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
483 bool addBitmap
= TRUE
;
485 if ( oldToolBarBitmap
)
487 #ifdef TB_REPLACEBITMAP
488 if ( wxTheApp
->GetComCtl32Version() >= 400 )
490 TBREPLACEBITMAP replaceBitmap
;
491 replaceBitmap
.hInstOld
= NULL
;
492 replaceBitmap
.hInstNew
= NULL
;
493 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
494 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
495 replaceBitmap
.nButtons
= nButtons
;
496 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
497 0, (LPARAM
) &replaceBitmap
) )
499 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
502 ::DeleteObject(oldToolBarBitmap
);
508 #endif // TB_REPLACEBITMAP
510 // we can't replace the old bitmap, so we will add another one
511 // (awfully inefficient, but what else to do?) and shift the bitmap
512 // indices accordingly
515 bitmapId
= m_nButtons
;
518 // Now delete all the buttons
519 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
521 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
523 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
529 if ( addBitmap
) // no old bitmap or we can't replace it
531 TBADDBITMAP addBitmap
;
533 addBitmap
.nID
= (UINT
) hBitmap
;
534 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
535 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
537 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
541 // Next add the buttons and separators
542 // -----------------------------------
544 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
546 // this array will hold the indices of all controls in the toolbar
547 wxArrayInt controlIds
;
550 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
552 wxToolBarToolBase
*tool
= node
->GetData();
554 // don't add separators to the vertical toolbar - looks ugly
555 if ( isVertical
&& tool
->IsSeparator() )
558 TBBUTTON
& button
= buttons
[i
];
560 wxZeroMemory(button
);
562 switch ( tool
->GetStyle() )
564 case wxTOOL_STYLE_CONTROL
:
565 button
.idCommand
= tool
->GetId();
566 // fall through: create just a separator too
568 case wxTOOL_STYLE_SEPARATOR
:
569 button
.fsState
= TBSTATE_ENABLED
;
570 button
.fsStyle
= TBSTYLE_SEP
;
573 case wxTOOL_STYLE_BUTTON
:
574 button
.iBitmap
= bitmapId
;
575 button
.idCommand
= tool
->GetId();
577 if ( tool
->IsEnabled() )
578 button
.fsState
|= TBSTATE_ENABLED
;
579 if ( tool
->IsToggled() )
580 button
.fsState
|= TBSTATE_CHECKED
;
582 button
.fsStyle
= tool
->CanBeToggled() ? TBSTYLE_CHECK
592 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
,
593 (WPARAM
)i
, (LPARAM
)buttons
) )
595 wxLogLastError(wxT("TB_ADDBUTTONS"));
600 // Deal with the controls finally
601 // ------------------------------
603 // adjust the controls size to fit nicely in the toolbar
605 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
607 wxToolBarToolBase
*tool
= node
->GetData();
608 if ( !tool
->IsControl() )
611 wxControl
*control
= tool
->GetControl();
613 wxSize size
= control
->GetSize();
615 // the position of the leftmost controls corner
618 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
619 // latter only appeared in v4.70 of comctl32.dll
621 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
622 index
, (LPARAM
)(LPRECT
)&r
) )
624 wxLogLastError(wxT("TB_GETITEMRECT"));
627 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
628 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
629 // available in headers, now check whether it is available now
631 if ( wxTheApp
->GetComCtl32Version() >= 471 )
633 // set the (underlying) separators width to be that of the
636 tbbi
.cbSize
= sizeof(tbbi
);
637 tbbi
.dwMask
= TBIF_SIZE
;
639 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
640 tool
->GetId(), (LPARAM
)&tbbi
) )
642 // the id is probably invalid?
643 wxLogLastError(wxT("TB_SETBUTTONINFO"));
647 #endif // comctl32.dll 4.71
648 // TB_SETBUTTONINFO unavailable
650 // try adding several separators to fit the controls width
651 int widthSep
= r
.right
- r
.left
;
657 tbb
.fsState
= TBSTATE_ENABLED
;
658 tbb
.fsStyle
= TBSTYLE_SEP
;
660 size_t nSeparators
= size
.x
/ widthSep
;
661 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
663 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
664 index
, (LPARAM
)&tbb
) )
666 wxLogLastError(wxT("TB_INSERTBUTTON"));
672 // remember the number of separators we used - we'd have to
673 // delete all of them later
674 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
676 // adjust the controls width to exactly cover the separators
677 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
680 // and position the control itself correctly vertically
681 int height
= r
.bottom
- r
.top
;
682 int diff
= height
- size
.y
;
685 // the control is too high, resize to fit
686 control
->SetSize(-1, height
- 2);
691 control
->Move(left
== -1 ? r
.left
: left
, r
.top
+ (diff
+ 1) / 2);
694 // the max index is the "real" number of buttons - i.e. counting even the
695 // separators which we added just for aligning the controls
700 if ( m_maxRows
== 0 )
702 // if not set yet, only one row
706 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
708 if ( m_maxRows
== 0 )
710 // if not set yet, have one column
718 // ----------------------------------------------------------------------------
720 // ----------------------------------------------------------------------------
722 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
724 wxToolBarToolBase
*tool
= FindById((int)id
);
728 if ( tool
->CanBeToggled() )
730 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
731 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
734 bool toggled
= tool
->IsToggled();
736 // OnLeftClick() can veto the button state change - for buttons which may
737 // be toggled only, of couse
738 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
742 tool
->SetToggle(toggled
);
744 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
750 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
752 WXLPARAM
*WXUNUSED(result
))
754 // First check if this applies to us
755 NMHDR
*hdr
= (NMHDR
*)lParam
;
757 // the tooltips control created by the toolbar is sometimes Unicode, even
758 // in an ANSI application - this seems to be a bug in comctl32.dll v5
759 int code
= (int)hdr
->code
;
760 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
763 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
764 if ( toolTipWnd
!= hdr
->hwndFrom
)
767 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
768 int id
= (int)ttText
->hdr
.idFrom
;
770 wxToolBarToolBase
*tool
= FindById(id
);
774 const wxString
& help
= tool
->GetShortHelp();
776 if ( !help
.IsEmpty() )
778 if ( code
== TTN_NEEDTEXTA
)
780 ttText
->lpszText
= (wxChar
*)help
.c_str();
785 ttText
->lpszText
= (wxChar
*)help
.c_str();
787 // VZ: I don't know why it happens, but the versions of
788 // comctl32.dll starting from 4.70 sometimes send TTN_NEEDTEXTW
789 // even to ANSI programs (normally, this message is supposed
790 // to be sent to Unicode programs only) - hence we need to
791 // handle it as well, otherwise no tooltips will be shown in
794 size_t lenAnsi
= help
.Len();
796 // MetroWerks doesn't like calling mbstowcs with NULL argument
797 size_t lenUnicode
= 2*lenAnsi
;
799 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
802 // using the pointer of right type avoids us doing all sorts of
803 // pointer arithmetics ourselves
804 wchar_t *dst
= (wchar_t *)ttText
->szText
,
805 *pwz
= new wchar_t[lenUnicode
+ 1];
806 mbstowcs(pwz
, help
, lenAnsi
+ 1);
807 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
809 // put the terminating _wide_ NUL
820 // ----------------------------------------------------------------------------
822 // ----------------------------------------------------------------------------
824 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
826 wxToolBarBase::SetToolBitmapSize(size
);
828 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
831 void wxToolBar::SetRows(int nRows
)
833 if ( nRows
== m_maxRows
)
835 // avoid resizing the frame uselessly
839 // TRUE in wParam means to create at least as many rows, FALSE -
842 ::SendMessage(GetHwnd(), TB_SETROWS
,
843 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
851 // The button size is bigger than the bitmap size
852 wxSize
wxToolBar::GetToolSize() const
854 // TB_GETBUTTONSIZE is supported from version 4.70
855 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
856 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
857 if ( wxTheApp
->GetComCtl32Version() >= 470 )
859 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
861 return wxSize(LOWORD(dw
), HIWORD(dw
));
864 #endif // comctl32.dll 4.70+
867 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
872 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
875 wxToolBarToolsList::Node
* current
= tools
.GetFirst();
877 for ( ; current
!= 0; current
= current
->GetNext() )
880 return current
->GetData();
882 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
883 size_t separators
= tool
->GetSeparatorsCount();
885 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
886 // otherwise, skip as many items as the separator count, plus the
888 index
-= separators
? separators
+ 1 : 1;
894 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
899 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
900 // MBN: when the point ( x, y ) is close to the toolbar border
901 // TB_HITTEST returns m_nButtons ( not -1 )
902 if ( index
< 0 || (size_t)index
>= m_nButtons
)
904 // it's a separator or there is no tool at all there
905 return (wxToolBarToolBase
*)NULL
;
908 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
909 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
910 if ( wxTheApp
->GetComCtl32Version() >= 471 )
912 return m_tools
.Item((size_t)index
)->GetData();
917 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
921 void wxToolBar::UpdateSize()
923 // the toolbar size changed
924 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
926 // we must also refresh the frame after the toolbar size (possibly) changed
927 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
930 frame
->SendSizeEvent();
934 // ----------------------------------------------------------------------------
936 // ----------------------------------------------------------------------------
938 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
940 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
941 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
944 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
946 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
947 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
950 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
952 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
953 // without, so we really need to delete the button and recreate it here
954 wxFAIL_MSG( _T("not implemented") );
957 // ----------------------------------------------------------------------------
959 // ----------------------------------------------------------------------------
961 // Responds to colour changes, and passes event on to children.
962 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
964 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
969 // Relayout the toolbar
970 int nrows
= m_maxRows
;
971 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
976 // let the event propagate further
980 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
982 if (event
.RightDown())
984 // For now, we don't have an id. Later we could
985 // try finding the tool.
986 OnRightClick((int)-1, event
.GetX(), event
.GetY());
994 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
996 if ( nMsg
== WM_SIZE
)
998 // calculate our minor dimenstion ourselves - we're confusing the
999 // standard logic (TB_AUTOSIZE) with our horizontal toolbars and other
1002 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1006 if ( GetWindowStyle() & wxTB_VERTICAL
)
1008 w
= r
.right
- r
.left
;
1011 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1018 h
= r
.bottom
- r
.top
;
1021 h
+= 6; // FIXME: this is the separator line height...
1026 if ( MAKELPARAM(w
, h
) != lParam
)
1028 // size really changed
1032 // message processed
1036 else if ( nMsg
== WM_MOUSEMOVE
)
1038 wxCoord x
= GET_X_LPARAM(lParam
), y
= GET_Y_LPARAM(lParam
);
1039 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1041 // cursor left current tool
1042 if( tool
!= m_pInTool
&& !tool
)
1048 // cursor entered a tool
1049 if( tool
!= m_pInTool
&& tool
)
1052 OnMouseEnter( tool
->GetId() );
1055 // we don't handle mouse moves, so fall through
1056 // to wxControl::MSWWindowProc
1059 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1062 // ----------------------------------------------------------------------------
1063 // private functions
1064 // ----------------------------------------------------------------------------
1066 bool wxToolBar::sm_coloursInit
= FALSE
;
1067 long wxToolBar::sm_stdColours
[6];
1069 void wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1071 if (!sm_coloursInit
)
1073 // When a bitmap is loaded, the RGB values can change. So we need to have a
1074 // reference bitmap which can tell us what the RGB values change to.
1075 wxBitmap
stdColourBitmap("wxBITMAP_STD_COLOURS", wxBITMAP_TYPE_RESOURCE
);
1076 if (stdColourBitmap
.Ok())
1079 memDC
.SelectObject(stdColourBitmap
);
1083 for (i
= 0; i
< 6; i
++)
1085 memDC
.GetPixel(i
, 0, & colour
);
1086 sm_stdColours
[i
] = RGB(colour
.Red(), colour
.Green(), colour
.Blue());
1088 sm_coloursInit
= TRUE
;
1089 memDC
.SelectObject(wxNullBitmap
);
1093 sm_stdColours
[0] = RGB(000,000,000) ;
1094 sm_stdColours
[1] = RGB(128,128,128) ;
1095 sm_stdColours
[2] = RGB(192,192,192) ;
1096 sm_stdColours
[3] = RGB(255,255,255) ;
1097 sm_stdColours
[4] = RGB(000,000,255) ;
1098 sm_stdColours
[5] = RGB(255,000,255) ;
1099 sm_coloursInit
= TRUE
;
1103 HBITMAP hBitmap
= (HBITMAP
) bitmap
;
1105 COLORMAP ColorMap
[5];
1107 ColorMap
[0].from
= sm_stdColours
[0]; ColorMap
[0].to
= COLOR_BTNTEXT
; // black (0, 0 0)
1108 ColorMap
[1].from
= sm_stdColours
[1]; ColorMap
[1].to
= COLOR_BTNSHADOW
; // dark grey (128, 128, 128)
1109 ColorMap
[2].from
= sm_stdColours
[2]; ColorMap
[2].to
= COLOR_BTNFACE
; // bright grey (192, 192, 192)
1110 ColorMap
[3].from
= sm_stdColours
[3]; ColorMap
[3].to
= COLOR_BTNHIGHLIGHT
; // white (255, 255, 255)
1111 // ColorMap[4].from = sm_stdColours[4]; ColorMap[4].to = COLOR_HIGHLIGHT; // blue (0, 0, 255)
1112 ColorMap
[4].from
= sm_stdColours
[5]; ColorMap
[4].to
= COLOR_WINDOW
; // magenta (255, 0, 255)
1114 for ( size_t n
= 0; n
< WXSIZEOF(ColorMap
); n
++)
1116 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
1120 HDC hdcMem
= CreateCompatibleDC(NULL
);
1124 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
1126 for ( int i
= 0; i
< width
; i
++ )
1128 for ( int j
= 0; j
< height
; j
++ )
1130 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1132 for ( size_t k
= 0; k
< WXSIZEOF(ColorMap
); k
++ )
1134 int distance
= abs( GetRValue( pixel
) - GetRValue( ColorMap
[k
].from
)) ;
1135 distance
= max( distance
, abs(GetGValue(pixel
) - GetGValue( ColorMap
[k
].from
))) ;
1136 distance
= max( distance
, abs(GetBValue(pixel
) - GetBValue( ColorMap
[k
].from
))) ;
1137 if ( distance
< 0x10 )
1139 ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
1147 SelectObject(hdcMem
, hbmOld
);
1148 DeleteObject(hdcMem
);
1152 // Some experiments...
1154 // What we want to do is create another bitmap which has a depth of 4,
1155 // and set the bits. So probably we want to convert this HBITMAP into a
1156 // DIB, then call SetDIBits.
1157 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
1158 // the screen), then SetDIBits fails.
1159 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
1160 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
1161 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
1164 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
1166 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
1168 DWORD err
= GetLastError();
1170 ::ReleaseDC(NULL
, dc
);
1173 GlobalUnlock (newDIB
);
1174 GlobalFree (newDIB
);
1176 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
1177 // Substitute our new bitmap for the old one
1178 ::DeleteObject((HBITMAP
) m_hBitmap
);
1179 m_hBitmap
= (WXHBITMAP
) newBitmap
;
1183 #endif // wxUSE_TOOLBAR && Win95