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
99 #ifndef TBSTYLE_TRANSPARENT
100 #define TBSTYLE_TRANSPARENT 0x8000
105 #define TB_SETSTYLE (WM_USER + 56)
106 #define TB_GETSTYLE (WM_USER + 57)
110 #define TB_HITTEST (WM_USER + 69)
113 // these values correspond to those used by comctl32.dll
114 #define DEFAULTBITMAPX 16
115 #define DEFAULTBITMAPY 15
116 #define DEFAULTBUTTONX 24
117 #define DEFAULTBUTTONY 24
118 #define DEFAULTBARHEIGHT 27
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
126 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
127 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
128 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 class wxToolBarTool
: public wxToolBarToolBase
138 wxToolBarTool(wxToolBar
*tbar
,
140 const wxBitmap
& bitmap1
,
141 const wxBitmap
& bitmap2
,
143 wxObject
*clientData
,
144 const wxString
& shortHelpString
,
145 const wxString
& longHelpString
)
146 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
147 clientData
, shortHelpString
, longHelpString
)
152 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
153 : wxToolBarToolBase(tbar
, control
)
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
; }
168 // ============================================================================
170 // ============================================================================
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
177 const wxBitmap
& bitmap1
,
178 const wxBitmap
& bitmap2
,
180 wxObject
*clientData
,
181 const wxString
& shortHelpString
,
182 const wxString
& longHelpString
)
184 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
185 clientData
, shortHelpString
, longHelpString
);
188 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
190 return new wxToolBarTool(this, control
);
193 // ----------------------------------------------------------------------------
194 // wxToolBar construction
195 // ----------------------------------------------------------------------------
197 void wxToolBar::Init()
203 m_defaultWidth
= DEFAULTBITMAPX
;
204 m_defaultHeight
= DEFAULTBITMAPY
;
209 bool wxToolBar::Create(wxWindow
*parent
,
214 const wxString
& name
)
216 // toolbars never have border, giving one to them results in broken
218 style
&= ~wxBORDER_MASK
;
219 style
|= wxBORDER_NONE
;
221 // common initialisation
222 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
226 DWORD msflags
= 0; // WS_VISIBLE | WS_CHILD always included
228 if ( style
& wxCLIP_SIBLINGS
)
229 msflags
|= WS_CLIPSIBLINGS
;
231 #ifdef TBSTYLE_TOOLTIPS
232 msflags
|= TBSTYLE_TOOLTIPS
;
235 if (style
& wxTB_FLAT
)
237 // static as it doesn't change during the program lifetime
238 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
240 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
241 // style with 6.00 (part of Windows XP) leads to the toolbar with
242 // incorrect background colour - and not using it still results in the
243 // correct (flat) toolbar, so don't use it there
244 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
246 msflags
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
250 // MSW-specific initialisation
251 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME
, msflags
) )
254 // toolbar-specific post initialisation
255 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
257 // set up the colors and fonts
258 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
259 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
270 height
= m_defaultHeight
;
276 SetSize(x
, y
, width
, height
);
281 wxToolBar::~wxToolBar()
283 // we must refresh the frame size when the toolbar is deleted but the frame
284 // is not - otherwise toolbar leaves a hole in the place it used to occupy
285 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
286 if ( frame
&& !frame
->IsBeingDeleted() )
288 frame
->SendSizeEvent();
293 ::DeleteObject((HBITMAP
) m_hBitmap
);
297 // ----------------------------------------------------------------------------
298 // adding/removing tools
299 // ----------------------------------------------------------------------------
301 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
302 wxToolBarToolBase
*tool
)
304 // nothing special to do here - we really create the toolbar buttons in
311 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
313 // the main difficulty we have here is with the controls in the toolbars:
314 // as we (sometimes) use several separators to cover up the space used by
315 // them, the indices are not the same for us and the toolbar
317 // first determine the position of the first button to delete: it may be
318 // different from pos if we use several separators to cover the space used
320 wxToolBarToolsList::Node
*node
;
321 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
323 wxToolBarToolBase
*tool2
= node
->GetData();
326 // let node point to the next node in the list
327 node
= node
->GetNext();
332 if ( tool2
->IsControl() )
334 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
338 // now determine the number of buttons to delete and the area taken by them
339 size_t nButtonsToDelete
= 1;
341 // get the size of the button we're going to delete
343 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
345 wxLogLastError(_T("TB_GETITEMRECT"));
348 int width
= r
.right
- r
.left
;
350 if ( tool
->IsControl() )
352 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
354 width
*= nButtonsToDelete
;
357 // do delete all buttons
358 m_nButtons
-= nButtonsToDelete
;
359 while ( nButtonsToDelete
-- > 0 )
361 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
363 wxLogLastError(wxT("TB_DELETEBUTTON"));
371 // and finally reposition all the controls after this button (the toolbar
372 // takes care of all normal items)
373 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
375 wxToolBarToolBase
*tool2
= node
->GetData();
376 if ( tool2
->IsControl() )
379 wxControl
*control
= tool2
->GetControl();
380 control
->GetPosition(&x
, NULL
);
381 control
->Move(x
- width
, -1);
388 bool wxToolBar::Realize()
390 size_t nTools
= GetToolsCount();
397 bool isVertical
= (GetWindowStyle() & wxTB_VERTICAL
) != 0;
399 // First, add the bitmap: we use one bitmap for all toolbar buttons
400 // ----------------------------------------------------------------
402 // if we already have a bitmap, we'll replace the existing one - otherwise
403 // we'll install a new one
404 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
406 int totalBitmapWidth
= (int)(m_defaultWidth
* nTools
);
407 int totalBitmapHeight
= (int)m_defaultHeight
;
409 // Create a bitmap and copy all the tool bitmaps to it
411 wxMemoryDC dcAllButtons
;
412 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
413 dcAllButtons
.SelectObject(bitmap
);
414 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
415 dcAllButtons
.Clear();
417 m_hBitmap
= bitmap
.GetHBITMAP();
418 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
419 #else // !USE_BITMAP_MASKS
420 HBITMAP hBitmap
= ::CreateCompatibleBitmap(ScreenHDC(),
425 wxLogLastError(_T("CreateCompatibleBitmap"));
430 m_hBitmap
= (WXHBITMAP
)hBitmap
;
432 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
433 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, hBitmap
);
435 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
436 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
438 // the button position
441 // the number of buttons (not separators)
444 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
447 wxToolBarToolBase
*tool
= node
->GetData();
448 if ( tool
->IsButton() )
450 const wxBitmap
& bmp
= tool
->GetBitmap1();
454 // notice the last parameter: do use mask
455 dcAllButtons
.DrawBitmap(tool
->GetBitmap1(), x
, 0, TRUE
);
456 #else // !USE_BITMAP_MASKS
457 HBITMAP hbmp
= GetHbitmapOf(bmp
);
458 HBITMAP oldBitmap2
= (HBITMAP
)::SelectObject(memoryDC2
, hbmp
);
459 if ( !BitBlt(memoryDC
, x
, 0, m_defaultWidth
, m_defaultHeight
,
460 memoryDC2
, 0, 0, SRCCOPY
) )
462 wxLogLastError(wxT("BitBlt"));
465 ::SelectObject(memoryDC2
, oldBitmap2
);
466 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
470 wxFAIL_MSG( _T("invalid tool button bitmap") );
473 // still inc width and number of buttons because otherwise the
474 // subsequent buttons will all be shifted which is rather confusing
475 // (and like this you'd see immediately which bitmap was bad)
480 node
= node
->GetNext();
484 dcAllButtons
.SelectObject(wxNullBitmap
);
486 // don't delete this HBITMAP!
487 bitmap
.SetHBITMAP(0);
488 #else // !USE_BITMAP_MASKS
489 ::SelectObject(memoryDC
, oldBitmap
);
490 ::DeleteDC(memoryDC
);
491 ::DeleteDC(memoryDC2
);
492 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
494 // Map to system colours
495 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
496 totalBitmapWidth
, totalBitmapHeight
);
500 bool addBitmap
= TRUE
;
502 if ( oldToolBarBitmap
)
504 #ifdef TB_REPLACEBITMAP
505 if ( wxTheApp
->GetComCtl32Version() >= 400 )
507 TBREPLACEBITMAP replaceBitmap
;
508 replaceBitmap
.hInstOld
= NULL
;
509 replaceBitmap
.hInstNew
= NULL
;
510 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
511 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
512 replaceBitmap
.nButtons
= nButtons
;
513 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
514 0, (LPARAM
) &replaceBitmap
) )
516 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
519 ::DeleteObject(oldToolBarBitmap
);
525 #endif // TB_REPLACEBITMAP
527 // we can't replace the old bitmap, so we will add another one
528 // (awfully inefficient, but what else to do?) and shift the bitmap
529 // indices accordingly
532 bitmapId
= m_nButtons
;
535 // Now delete all the buttons
536 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
538 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
540 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
546 if ( addBitmap
) // no old bitmap or we can't replace it
548 TBADDBITMAP addBitmap
;
550 addBitmap
.nID
= (UINT
) hBitmap
;
551 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
552 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
554 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
558 // Next add the buttons and separators
559 // -----------------------------------
561 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
563 // this array will hold the indices of all controls in the toolbar
564 wxArrayInt controlIds
;
567 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
569 wxToolBarToolBase
*tool
= node
->GetData();
571 // don't add separators to the vertical toolbar - looks ugly
572 if ( isVertical
&& tool
->IsSeparator() )
575 TBBUTTON
& button
= buttons
[i
];
577 wxZeroMemory(button
);
579 switch ( tool
->GetStyle() )
581 case wxTOOL_STYLE_CONTROL
:
582 button
.idCommand
= tool
->GetId();
583 // fall through: create just a separator too
585 case wxTOOL_STYLE_SEPARATOR
:
586 button
.fsState
= TBSTATE_ENABLED
;
587 button
.fsStyle
= TBSTYLE_SEP
;
590 case wxTOOL_STYLE_BUTTON
:
591 button
.iBitmap
= bitmapId
;
592 button
.idCommand
= tool
->GetId();
594 if ( tool
->IsEnabled() )
595 button
.fsState
|= TBSTATE_ENABLED
;
596 if ( tool
->IsToggled() )
597 button
.fsState
|= TBSTATE_CHECKED
;
599 button
.fsStyle
= tool
->CanBeToggled() ? TBSTYLE_CHECK
609 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
,
610 (WPARAM
)i
, (LPARAM
)buttons
) )
612 wxLogLastError(wxT("TB_ADDBUTTONS"));
617 // Deal with the controls finally
618 // ------------------------------
620 // adjust the controls size to fit nicely in the toolbar
623 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
625 wxToolBarToolBase
*tool
= node
->GetData();
627 // we calculate the running y coord for vertical toolbars so we need to
628 // get the items size for all items but for the horizontal ones we
629 // don't need to deal with the non controls
630 bool isControl
= tool
->IsControl();
631 if ( !isControl
&& !isVertical
)
634 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
635 // latter only appeared in v4.70 of comctl32.dll
637 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
638 index
, (LPARAM
)(LPRECT
)&r
) )
640 wxLogLastError(wxT("TB_GETITEMRECT"));
645 // can only be control if isVertical
646 y
+= r
.bottom
- r
.top
;
651 wxControl
*control
= tool
->GetControl();
653 wxSize size
= control
->GetSize();
655 // the position of the leftmost controls corner
658 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
659 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
660 // available in headers, now check whether it is available now
662 if ( wxTheApp
->GetComCtl32Version() >= 471 )
664 // set the (underlying) separators width to be that of the
667 tbbi
.cbSize
= sizeof(tbbi
);
668 tbbi
.dwMask
= TBIF_SIZE
;
670 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
671 tool
->GetId(), (LPARAM
)&tbbi
) )
673 // the id is probably invalid?
674 wxLogLastError(wxT("TB_SETBUTTONINFO"));
678 #endif // comctl32.dll 4.71
679 // TB_SETBUTTONINFO unavailable
681 // try adding several separators to fit the controls width
682 int widthSep
= r
.right
- r
.left
;
688 tbb
.fsState
= TBSTATE_ENABLED
;
689 tbb
.fsStyle
= TBSTYLE_SEP
;
691 size_t nSeparators
= size
.x
/ widthSep
;
692 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
694 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
695 index
, (LPARAM
)&tbb
) )
697 wxLogLastError(wxT("TB_INSERTBUTTON"));
703 // remember the number of separators we used - we'd have to
704 // delete all of them later
705 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
707 // adjust the controls width to exactly cover the separators
708 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
711 // position the control itself correctly vertically
712 int height
= r
.bottom
- r
.top
;
713 int diff
= height
- size
.y
;
716 // the control is too high, resize to fit
717 control
->SetSize(-1, height
- 2);
728 y
+= height
+ 2*GetMargins().y
;
730 else // horizontal toolbar
738 control
->Move(left
, top
+ (diff
+ 1) / 2);
741 // the max index is the "real" number of buttons - i.e. counting even the
742 // separators which we added just for aligning the controls
747 if ( m_maxRows
== 0 )
749 // if not set yet, only one row
753 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
755 if ( m_maxRows
== 0 )
757 // if not set yet, have one column
765 // ----------------------------------------------------------------------------
767 // ----------------------------------------------------------------------------
769 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
771 wxToolBarToolBase
*tool
= FindById((int)id
);
775 if ( tool
->CanBeToggled() )
777 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
778 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
781 bool toggled
= tool
->IsToggled();
783 // OnLeftClick() can veto the button state change - for buttons which may
784 // be toggled only, of couse
785 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
789 tool
->SetToggle(toggled
);
791 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
797 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
799 WXLPARAM
*WXUNUSED(result
))
801 // First check if this applies to us
802 NMHDR
*hdr
= (NMHDR
*)lParam
;
804 // the tooltips control created by the toolbar is sometimes Unicode, even
805 // in an ANSI application - this seems to be a bug in comctl32.dll v5
806 int code
= (int)hdr
->code
;
807 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
810 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
811 if ( toolTipWnd
!= hdr
->hwndFrom
)
814 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
815 int id
= (int)ttText
->hdr
.idFrom
;
817 wxToolBarToolBase
*tool
= FindById(id
);
821 const wxString
& help
= tool
->GetShortHelp();
823 if ( !help
.IsEmpty() )
825 if ( code
== TTN_NEEDTEXTA
)
827 ttText
->lpszText
= (wxChar
*)help
.c_str();
832 ttText
->lpszText
= (wxChar
*)help
.c_str();
834 // VZ: I don't know why it happens, but the versions of
835 // comctl32.dll starting from 4.70 sometimes send TTN_NEEDTEXTW
836 // even to ANSI programs (normally, this message is supposed
837 // to be sent to Unicode programs only) - hence we need to
838 // handle it as well, otherwise no tooltips will be shown in
841 size_t lenAnsi
= help
.Len();
842 #if defined( __MWERKS__ ) || defined( __CYGWIN__ )
843 // MetroWerks doesn't like calling mbstowcs with NULL argument
844 // neither Cygwin does
845 size_t lenUnicode
= 2*lenAnsi
;
847 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
850 // using the pointer of right type avoids us doing all sorts of
851 // pointer arithmetics ourselves
852 wchar_t *dst
= (wchar_t *)ttText
->szText
,
853 *pwz
= new wchar_t[lenUnicode
+ 1];
854 mbstowcs(pwz
, help
, lenAnsi
+ 1);
855 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
857 // put the terminating _wide_ NUL
868 // ----------------------------------------------------------------------------
870 // ----------------------------------------------------------------------------
872 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
874 wxToolBarBase::SetToolBitmapSize(size
);
876 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
879 void wxToolBar::SetRows(int nRows
)
881 if ( nRows
== m_maxRows
)
883 // avoid resizing the frame uselessly
887 // TRUE in wParam means to create at least as many rows, FALSE -
890 ::SendMessage(GetHwnd(), TB_SETROWS
,
891 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
899 // The button size is bigger than the bitmap size
900 wxSize
wxToolBar::GetToolSize() const
902 // TB_GETBUTTONSIZE is supported from version 4.70
903 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
904 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
905 if ( wxTheApp
->GetComCtl32Version() >= 470 )
907 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
909 return wxSize(LOWORD(dw
), HIWORD(dw
));
912 #endif // comctl32.dll 4.70+
915 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
920 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
923 wxToolBarToolsList::Node
* current
= tools
.GetFirst();
925 for ( ; current
!= 0; current
= current
->GetNext() )
928 return current
->GetData();
930 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
931 size_t separators
= tool
->GetSeparatorsCount();
933 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
934 // otherwise, skip as many items as the separator count, plus the
936 index
-= separators
? separators
+ 1 : 1;
942 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
947 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
948 // MBN: when the point ( x, y ) is close to the toolbar border
949 // TB_HITTEST returns m_nButtons ( not -1 )
950 if ( index
< 0 || (size_t)index
>= m_nButtons
)
952 // it's a separator or there is no tool at all there
953 return (wxToolBarToolBase
*)NULL
;
956 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
957 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
958 if ( wxTheApp
->GetComCtl32Version() >= 471 )
960 return m_tools
.Item((size_t)index
)->GetData();
965 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
969 void wxToolBar::UpdateSize()
971 // the toolbar size changed
972 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
974 // we must also refresh the frame after the toolbar size (possibly) changed
975 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
978 frame
->SendSizeEvent();
982 // ----------------------------------------------------------------------------
984 // ----------------------------------------------------------------------------
986 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
988 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
989 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
992 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
994 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
995 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
998 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1000 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1001 // without, so we really need to delete the button and recreate it here
1002 wxFAIL_MSG( _T("not implemented") );
1005 // ----------------------------------------------------------------------------
1007 // ----------------------------------------------------------------------------
1009 // Responds to colour changes, and passes event on to children.
1010 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1012 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1014 // Remap the buttons
1017 // Relayout the toolbar
1018 int nrows
= m_maxRows
;
1019 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1024 // let the event propagate further
1028 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1030 if (event
.RightDown())
1032 // For now, we don't have an id. Later we could
1033 // try finding the tool.
1034 OnRightClick((int)-1, event
.GetX(), event
.GetY());
1042 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1044 if ( nMsg
== WM_SIZE
)
1046 // calculate our minor dimenstion ourselves - we're confusing the
1047 // standard logic (TB_AUTOSIZE) with our horizontal toolbars and other
1050 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1054 if ( GetWindowStyle() & wxTB_VERTICAL
)
1056 w
= r
.right
- r
.left
;
1059 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1066 h
= r
.bottom
- r
.top
;
1069 h
+= 6; // FIXME: this is the separator line height...
1074 if ( MAKELPARAM(w
, h
) != lParam
)
1076 // size really changed
1080 // message processed
1084 else if ( nMsg
== WM_MOUSEMOVE
)
1086 wxCoord x
= GET_X_LPARAM(lParam
), y
= GET_Y_LPARAM(lParam
);
1087 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1089 // cursor left current tool
1090 if( tool
!= m_pInTool
&& !tool
)
1096 // cursor entered a tool
1097 if( tool
!= m_pInTool
&& tool
)
1100 OnMouseEnter( tool
->GetId() );
1103 // we don't handle mouse moves, so fall through
1104 // to wxControl::MSWWindowProc
1107 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1110 // ----------------------------------------------------------------------------
1111 // private functions
1112 // ----------------------------------------------------------------------------
1114 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1120 wxLogLastError(_T("CreateCompatibleDC"));
1125 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1129 wxLogLastError(_T("SelectObject"));
1134 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1136 for ( int i
= 0; i
< width
; i
++ )
1138 for ( int j
= 0; j
< height
; j
++ )
1140 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1142 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1144 COLORREF col
= cmap
[k
].from
;
1145 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1146 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1147 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1149 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1158 // VZ: I leave here my attempts to map the bitmap to the system colours
1159 // faster by using BitBlt() even though it's broken currently - but
1160 // maybe someone else can finish it? It should be faster than iterating
1161 // over all pixels...
1163 MemoryHDC hdcMask
, hdcDst
;
1164 if ( !hdcMask
|| !hdcDst
)
1166 wxLogLastError(_T("CreateCompatibleDC"));
1171 // create the target bitmap
1172 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1175 wxLogLastError(_T("CreateCompatibleBitmap"));
1180 // create the monochrome mask bitmap
1181 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1184 wxLogLastError(_T("CreateBitmap(mono)"));
1186 ::DeleteObject(hbmpDst
);
1191 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1192 bmpInMask(hdcMask
, hbmpMask
);
1195 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1197 // create the mask for this colour
1198 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1199 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1201 // replace this colour with the target one in the dst bitmap
1202 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1203 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1205 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1208 MAKEROP4(PATCOPY
, SRCCOPY
));
1210 (void)::SelectObject(hdcDst
, hbrOld
);
1211 ::DeleteObject(hbr
);
1214 ::DeleteObject((HBITMAP
)bitmap
);
1216 return (WXHBITMAP
)hbmpDst
;
1220 #endif // wxUSE_TOOLBAR && Win95