1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tbar95.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 && wxUSE_TOOLBAR_NATIVE && (!defined(_WIN32_WCE) || (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)))
44 #include "wx/toolbar.h"
45 #include "wx/sysopt.h"
47 #include "wx/msw/private.h"
50 #include "wx/msw/uxtheme.h"
53 // include <commctrl.h> "properly"
54 #include "wx/msw/wrapcctl.h"
56 #include "wx/app.h" // for GetComCtl32Version
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // these standard constants are not always defined in compilers headers
66 #define TBSTYLE_LIST 0x1000
67 #define TBSTYLE_FLAT 0x0800
70 #ifndef TBSTYLE_TRANSPARENT
71 #define TBSTYLE_TRANSPARENT 0x8000
74 #ifndef TBSTYLE_TOOLTIPS
75 #define TBSTYLE_TOOLTIPS 0x0100
80 #define TB_SETSTYLE (WM_USER + 56)
81 #define TB_GETSTYLE (WM_USER + 57)
85 #define TB_HITTEST (WM_USER + 69)
89 #define TB_GETMAXSIZE (WM_USER + 83)
92 // these values correspond to those used by comctl32.dll
93 #define DEFAULTBITMAPX 16
94 #define DEFAULTBITMAPY 15
95 #define DEFAULTBUTTONX 24
96 #define DEFAULTBUTTONY 24
97 #define DEFAULTBARHEIGHT 27
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
115 style ( wxNO_BORDER | wxTB_HORIZONTAL)
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 wxString
& label
,
139 const wxBitmap
& bmpNormal
,
140 const wxBitmap
& bmpDisabled
,
142 wxObject
*clientData
,
143 const wxString
& shortHelp
,
144 const wxString
& longHelp
)
145 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpDisabled
, kind
,
146 clientData
, shortHelp
, longHelp
)
151 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
152 : wxToolBarToolBase(tbar
, control
)
157 virtual void SetLabel(const wxString
& label
)
159 if ( label
== m_label
)
162 wxToolBarToolBase::SetLabel(label
);
164 // we need to update the label shown in the toolbar because it has a
165 // pointer to the internal buffer of the old label
167 // TODO: use TB_SETBUTTONINFO
170 // set/get the number of separators which we use to cover the space used by
171 // a control in the toolbar
172 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
173 size_t GetSeparatorsCount() const { return m_nSepCount
; }
178 DECLARE_NO_COPY_CLASS(wxToolBarTool
)
182 // ============================================================================
184 // ============================================================================
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
191 const wxString
& label
,
192 const wxBitmap
& bmpNormal
,
193 const wxBitmap
& bmpDisabled
,
195 wxObject
*clientData
,
196 const wxString
& shortHelp
,
197 const wxString
& longHelp
)
199 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpDisabled
, kind
,
200 clientData
, shortHelp
, longHelp
);
203 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
205 return new wxToolBarTool(this, control
);
208 // ----------------------------------------------------------------------------
209 // wxToolBar construction
210 // ----------------------------------------------------------------------------
212 void wxToolBar::Init()
218 m_defaultWidth
= DEFAULTBITMAPX
;
219 m_defaultHeight
= DEFAULTBITMAPY
;
224 bool wxToolBar::Create(wxWindow
*parent
,
229 const wxString
& name
)
231 // common initialisation
232 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
235 // MSW-specific initialisation
236 if ( !MSWCreateToolbar(pos
, size
) )
239 wxSetCCUnicodeFormat(GetHwnd());
241 // set up the colors and fonts
242 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
243 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
245 // workaround for flat toolbar on Windows XP classic style
247 if ( style
& wxTB_FLAT
)
249 wxUxThemeEngine
*p
= wxUxThemeEngine::Get();
250 if ( !p
|| !p
->IsThemeActive() )
252 DWORD dwToolbarStyle
;
254 dwToolbarStyle
= (DWORD
)::SendMessage(GetHwnd(), TB_GETSTYLE
, 0, 0L );
256 if ((dwToolbarStyle
& TBSTYLE_FLAT
) == 0)
258 dwToolbarStyle
|= TBSTYLE_FLAT
;
259 ::SendMessage(GetHwnd(), TB_SETSTYLE
, 0, (LPARAM
)dwToolbarStyle
);
268 bool wxToolBar::MSWCreateToolbar(const wxPoint
& pos
, const wxSize
& size
)
270 if ( !MSWCreateControl(TOOLBARCLASSNAME
, wxEmptyString
, pos
, size
) )
273 // toolbar-specific post initialisation
274 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
279 void wxToolBar::Recreate()
281 const HWND hwndOld
= GetHwnd();
284 // we haven't been created yet, no need to recreate
288 // get the position and size before unsubclassing the old toolbar
289 const wxPoint pos
= GetPosition();
290 const wxSize size
= GetSize();
294 if ( !MSWCreateToolbar(pos
, size
) )
297 wxFAIL_MSG( _T("recreating the toolbar failed") );
302 // reparent all our children under the new toolbar
303 for ( wxWindowList::compatibility_iterator node
= m_children
.GetFirst();
305 node
= node
->GetNext() )
307 wxWindow
*win
= node
->GetData();
308 if ( !win
->IsTopLevel() )
309 ::SetParent(GetHwndOf(win
), GetHwnd());
312 // only destroy the old toolbar now -- after all the children had been
314 ::DestroyWindow(hwndOld
);
316 // it is for the old bitmap control and can't be used with the new one
319 ::DeleteObject((HBITMAP
) m_hBitmap
);
327 wxToolBar::~wxToolBar()
329 // we must refresh the frame size when the toolbar is deleted but the frame
330 // is not - otherwise toolbar leaves a hole in the place it used to occupy
331 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
332 if ( frame
&& !frame
->IsBeingDeleted() )
334 frame
->SendSizeEvent();
339 ::DeleteObject((HBITMAP
) m_hBitmap
);
343 wxSize
wxToolBar::DoGetBestSize() const
348 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE
, 0, (LPARAM
)&size
) )
350 // maybe an old (< 0x400) Windows version? try to approximate the
351 // toolbar size ourselves
352 sizeBest
= GetToolSize();
353 sizeBest
.y
+= 2 * ::GetSystemMetrics(SM_CYBORDER
); // Add borders
354 sizeBest
.x
*= GetToolsCount();
356 // reverse horz and vertical components if necessary
357 if ( HasFlag(wxTB_VERTICAL
) )
360 sizeBest
.x
= sizeBest
.y
;
366 sizeBest
.x
= size
.cx
;
367 sizeBest
.y
= size
.cy
;
373 WXDWORD
wxToolBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
375 // toolbars never have border, giving one to them results in broken
377 WXDWORD msStyle
= wxControl::MSWGetStyle
379 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
382 // always include this one, it never hurts and setting it later only if we
383 // do have tooltips wouldn't work
384 msStyle
|= TBSTYLE_TOOLTIPS
;
386 if ( style
& (wxTB_FLAT
| wxTB_HORZ_LAYOUT
) )
388 // static as it doesn't change during the program lifetime
389 static int s_verComCtl
= wxTheApp
->GetComCtl32Version();
391 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
392 // style with 6.00 (part of Windows XP) leads to the toolbar with
393 // incorrect background colour - and not using it still results in the
394 // correct (flat) toolbar, so don't use it there
395 if ( s_verComCtl
> 400 && s_verComCtl
< 600 )
397 msStyle
|= TBSTYLE_FLAT
| TBSTYLE_TRANSPARENT
;
400 if ( s_verComCtl
>= 470 && style
& wxTB_HORZ_LAYOUT
)
402 msStyle
|= TBSTYLE_LIST
;
406 if ( style
& wxTB_NODIVIDER
)
407 msStyle
|= CCS_NODIVIDER
;
409 if ( style
& wxTB_NOALIGN
)
410 msStyle
|= CCS_NOPARENTALIGN
;
412 if ( style
& wxTB_VERTICAL
)
418 // ----------------------------------------------------------------------------
419 // adding/removing tools
420 // ----------------------------------------------------------------------------
422 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
424 // nothing special to do here - we really create the toolbar buttons in
428 InvalidateBestSize();
432 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
434 // the main difficulty we have here is with the controls in the toolbars:
435 // as we (sometimes) use several separators to cover up the space used by
436 // them, the indices are not the same for us and the toolbar
438 // first determine the position of the first button to delete: it may be
439 // different from pos if we use several separators to cover the space used
441 wxToolBarToolsList::compatibility_iterator node
;
442 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
444 wxToolBarToolBase
*tool2
= node
->GetData();
447 // let node point to the next node in the list
448 node
= node
->GetNext();
453 if ( tool2
->IsControl() )
455 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount() - 1;
459 // now determine the number of buttons to delete and the area taken by them
460 size_t nButtonsToDelete
= 1;
462 // get the size of the button we're going to delete
464 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
466 wxLogLastError(_T("TB_GETITEMRECT"));
469 int width
= r
.right
- r
.left
;
471 if ( tool
->IsControl() )
473 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
475 width
*= nButtonsToDelete
;
478 // do delete all buttons
479 m_nButtons
-= nButtonsToDelete
;
480 while ( nButtonsToDelete
-- > 0 )
482 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
484 wxLogLastError(wxT("TB_DELETEBUTTON"));
492 // and finally reposition all the controls after this button (the toolbar
493 // takes care of all normal items)
494 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
496 wxToolBarToolBase
*tool2
= node
->GetData();
497 if ( tool2
->IsControl() )
500 wxControl
*control
= tool2
->GetControl();
501 control
->GetPosition(&x
, NULL
);
502 control
->Move(x
- width
, wxDefaultCoord
);
506 InvalidateBestSize();
510 bool wxToolBar::Realize()
512 const size_t nTools
= GetToolsCount();
519 const bool isVertical
= HasFlag(wxTB_VERTICAL
);
521 bool doRemap
, doRemapBg
, doTransparent
;
522 if (wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 2)
524 doRemapBg
= doRemap
= false;
525 doTransparent
= true;
528 { doRemap
= !wxSystemOptions::HasOption(wxT("msw.remap"))
529 || wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 1;
530 doRemapBg
= !doRemap
;
531 doTransparent
= false;
534 // delete all old buttons, if any
535 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
537 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
539 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
543 // First, add the bitmap: we use one bitmap for all toolbar buttons
544 // ----------------------------------------------------------------
546 wxToolBarToolsList::compatibility_iterator node
;
550 if ( HasFlag(wxTB_NOICONS
) )
552 // no icons, don't leave space for them
556 else // do show icons
558 // if we already have a bitmap, we'll replace the existing one --
559 // otherwise we'll install a new one
560 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
562 sizeBmp
.x
= m_defaultWidth
;
563 sizeBmp
.y
= m_defaultHeight
;
565 const wxCoord totalBitmapWidth
= m_defaultWidth
* nTools
,
566 totalBitmapHeight
= m_defaultHeight
;
568 // Create a bitmap and copy all the tool bitmaps to it
569 wxMemoryDC dcAllButtons
;
570 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
571 dcAllButtons
.SelectObject(bitmap
);
573 dcAllButtons
.SetBackground(*wxTRANSPARENT_BRUSH
);
575 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
576 dcAllButtons
.Clear();
578 m_hBitmap
= bitmap
.GetHBITMAP();
579 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
583 dcAllButtons
.SelectObject(wxNullBitmap
);
585 // Even if we're not remapping the bitmap
586 // content, we still have to remap the background.
587 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
588 totalBitmapWidth
, totalBitmapHeight
);
590 dcAllButtons
.SelectObject(bitmap
);
593 // the button position
596 // the number of buttons (not separators)
599 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
601 wxToolBarToolBase
*tool
= node
->GetData();
602 if ( tool
->IsButton() )
604 const wxBitmap
& bmp
= tool
->GetNormalBitmap();
607 int xOffset
= wxMax(0, (m_defaultWidth
- bmp
.GetWidth())/2);
608 int yOffset
= wxMax(0, (m_defaultHeight
- bmp
.GetHeight())/2);
609 // notice the last parameter: do use mask
610 dcAllButtons
.DrawBitmap(bmp
, x
+xOffset
, yOffset
, true);
614 wxFAIL_MSG( _T("invalid tool button bitmap") );
617 // still inc width and number of buttons because otherwise the
618 // subsequent buttons will all be shifted which is rather confusing
619 // (and like this you'd see immediately which bitmap was bad)
625 dcAllButtons
.SelectObject(wxNullBitmap
);
627 // don't delete this HBITMAP!
628 bitmap
.SetHBITMAP(0);
632 // Map to system colours
633 hBitmap
= (HBITMAP
)MapBitmap((WXHBITMAP
) hBitmap
,
634 totalBitmapWidth
, totalBitmapHeight
);
637 bool addBitmap
= true;
639 if ( oldToolBarBitmap
)
641 #ifdef TB_REPLACEBITMAP
642 if ( wxTheApp
->GetComCtl32Version() >= 400 )
644 TBREPLACEBITMAP replaceBitmap
;
645 replaceBitmap
.hInstOld
= NULL
;
646 replaceBitmap
.hInstNew
= NULL
;
647 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
648 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
649 replaceBitmap
.nButtons
= nButtons
;
650 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
651 0, (LPARAM
) &replaceBitmap
) )
653 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
656 ::DeleteObject(oldToolBarBitmap
);
662 #endif // TB_REPLACEBITMAP
664 // we can't replace the old bitmap, so we will add another one
665 // (awfully inefficient, but what else to do?) and shift the bitmap
666 // indices accordingly
669 bitmapId
= m_nButtons
;
673 if ( addBitmap
) // no old bitmap or we can't replace it
675 TBADDBITMAP addBitmap
;
677 addBitmap
.nID
= (UINT
) hBitmap
;
678 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
679 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
681 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
686 // don't call SetToolBitmapSize() as we don't want to change the values of
687 // m_defaultWidth/Height
688 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0,
689 MAKELONG(sizeBmp
.x
, sizeBmp
.y
)) )
691 wxLogLastError(_T("TB_SETBITMAPSIZE"));
694 // Next add the buttons and separators
695 // -----------------------------------
697 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
699 // this array will hold the indices of all controls in the toolbar
700 wxArrayInt controlIds
;
702 bool lastWasRadio
= false;
704 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
706 wxToolBarToolBase
*tool
= node
->GetData();
708 // don't add separators to the vertical toolbar with old comctl32.dll
709 // versions as they didn't handle this properly
710 if ( isVertical
&& tool
->IsSeparator() &&
711 wxTheApp
->GetComCtl32Version() <= 472 )
717 TBBUTTON
& button
= buttons
[i
];
719 wxZeroMemory(button
);
721 bool isRadio
= false;
722 switch ( tool
->GetStyle() )
724 case wxTOOL_STYLE_CONTROL
:
725 button
.idCommand
= tool
->GetId();
726 // fall through: create just a separator too
728 case wxTOOL_STYLE_SEPARATOR
:
729 button
.fsState
= TBSTATE_ENABLED
;
730 button
.fsStyle
= TBSTYLE_SEP
;
733 case wxTOOL_STYLE_BUTTON
:
734 if ( !HasFlag(wxTB_NOICONS
) )
735 button
.iBitmap
= bitmapId
;
737 if ( HasFlag(wxTB_TEXT
) )
739 const wxString
& label
= tool
->GetLabel();
740 if ( !label
.empty() )
742 button
.iString
= (int)label
.c_str();
746 button
.idCommand
= tool
->GetId();
748 if ( tool
->IsEnabled() )
749 button
.fsState
|= TBSTATE_ENABLED
;
750 if ( tool
->IsToggled() )
751 button
.fsState
|= TBSTATE_CHECKED
;
753 switch ( tool
->GetKind() )
756 button
.fsStyle
= TBSTYLE_CHECKGROUP
;
760 // the first item in the radio group is checked by
761 // default to be consistent with wxGTK and the menu
763 button
.fsState
|= TBSTATE_CHECKED
;
772 button
.fsStyle
= TBSTYLE_CHECK
;
776 wxFAIL_MSG( _T("unexpected toolbar button kind") );
780 button
.fsStyle
= TBSTYLE_BUTTON
;
787 lastWasRadio
= isRadio
;
792 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)buttons
) )
794 wxLogLastError(wxT("TB_ADDBUTTONS"));
799 // Deal with the controls finally
800 // ------------------------------
802 // adjust the controls size to fit nicely in the toolbar
805 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
807 wxToolBarToolBase
*tool
= node
->GetData();
809 // we calculate the running y coord for vertical toolbars so we need to
810 // get the items size for all items but for the horizontal ones we
811 // don't need to deal with the non controls
812 bool isControl
= tool
->IsControl();
813 if ( !isControl
&& !isVertical
)
816 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
817 // latter only appeared in v4.70 of comctl32.dll
819 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
820 index
, (LPARAM
)(LPRECT
)&r
) )
822 wxLogLastError(wxT("TB_GETITEMRECT"));
827 // can only be control if isVertical
828 y
+= r
.bottom
- r
.top
;
833 wxControl
*control
= tool
->GetControl();
835 wxSize size
= control
->GetSize();
837 // the position of the leftmost controls corner
838 int left
= wxDefaultCoord
;
840 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
841 #ifdef TB_SETBUTTONINFO
842 // available in headers, now check whether it is available now
844 if ( wxTheApp
->GetComCtl32Version() >= 471 )
846 // set the (underlying) separators width to be that of the
849 tbbi
.cbSize
= sizeof(tbbi
);
850 tbbi
.dwMask
= TBIF_SIZE
;
851 tbbi
.cx
= (WORD
)size
.x
;
852 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
853 tool
->GetId(), (LPARAM
)&tbbi
) )
855 // the id is probably invalid?
856 wxLogLastError(wxT("TB_SETBUTTONINFO"));
860 #endif // comctl32.dll 4.71
861 // TB_SETBUTTONINFO unavailable
863 // try adding several separators to fit the controls width
864 int widthSep
= r
.right
- r
.left
;
870 tbb
.fsState
= TBSTATE_ENABLED
;
871 tbb
.fsStyle
= TBSTYLE_SEP
;
873 size_t nSeparators
= size
.x
/ widthSep
;
874 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
876 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON
,
877 index
, (LPARAM
)&tbb
) )
879 wxLogLastError(wxT("TB_INSERTBUTTON"));
885 // remember the number of separators we used - we'd have to
886 // delete all of them later
887 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
889 // adjust the controls width to exactly cover the separators
890 control
->SetSize((nSeparators
+ 1)*widthSep
, wxDefaultCoord
);
893 // position the control itself correctly vertically
894 int height
= r
.bottom
- r
.top
;
895 int diff
= height
- size
.y
;
898 // the control is too high, resize to fit
899 control
->SetSize(wxDefaultCoord
, height
- 2);
910 y
+= height
+ 2*GetMargins().y
;
912 else // horizontal toolbar
914 if ( left
== wxDefaultCoord
)
920 control
->Move(left
, top
+ (diff
+ 1) / 2);
923 // the max index is the "real" number of buttons - i.e. counting even the
924 // separators which we added just for aligning the controls
929 if ( m_maxRows
== 0 )
931 // if not set yet, only one row
935 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
937 if ( m_maxRows
== 0 )
939 // if not set yet, have one column
944 InvalidateBestSize();
948 // ----------------------------------------------------------------------------
950 // ----------------------------------------------------------------------------
952 bool wxToolBar::MSWCommand(WXUINT
WXUNUSED(cmd
), WXWORD id
)
954 wxToolBarToolBase
*tool
= FindById((int)id
);
958 bool toggled
= false; // just to suppress warnings
960 if ( tool
->CanBeToggled() )
962 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
963 toggled
= (state
& TBSTATE_CHECKED
) != 0;
965 // ignore the event when a radio button is released, as this doesn't seem to
966 // happen at all, and is handled otherwise
967 if ( tool
->GetKind() == wxITEM_RADIO
&& !toggled
)
970 tool
->Toggle(toggled
);
971 UnToggleRadioGroup(tool
);
974 // OnLeftClick() can veto the button state change - for buttons which
975 // may be toggled only, of couse
976 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
979 tool
->Toggle(!toggled
);
981 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
987 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
989 WXLPARAM
*WXUNUSED(result
))
992 // First check if this applies to us
993 NMHDR
*hdr
= (NMHDR
*)lParam
;
995 // the tooltips control created by the toolbar is sometimes Unicode, even
996 // in an ANSI application - this seems to be a bug in comctl32.dll v5
997 UINT code
= hdr
->code
;
998 if ( (code
!= (UINT
) TTN_NEEDTEXTA
) && (code
!= (UINT
) TTN_NEEDTEXTW
) )
1001 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
1002 if ( toolTipWnd
!= hdr
->hwndFrom
)
1005 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
1006 int id
= (int)ttText
->hdr
.idFrom
;
1008 wxToolBarToolBase
*tool
= FindById(id
);
1012 return HandleTooltipNotify(code
, lParam
, tool
->GetShortHelp());
1018 // ----------------------------------------------------------------------------
1020 // ----------------------------------------------------------------------------
1022 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
1024 wxToolBarBase::SetToolBitmapSize(size
);
1026 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
1029 void wxToolBar::SetRows(int nRows
)
1031 if ( nRows
== m_maxRows
)
1033 // avoid resizing the frame uselessly
1037 // TRUE in wParam means to create at least as many rows, FALSE -
1040 ::SendMessage(GetHwnd(), TB_SETROWS
,
1041 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
1049 // The button size is bigger than the bitmap size
1050 wxSize
wxToolBar::GetToolSize() const
1052 // TB_GETBUTTONSIZE is supported from version 4.70
1053 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
1054 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1055 && !defined (__DIGITALMARS__)
1056 if ( wxTheApp
->GetComCtl32Version() >= 470 )
1058 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
1060 return wxSize(LOWORD(dw
), HIWORD(dw
));
1063 #endif // comctl32.dll 4.70+
1066 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
1071 wxToolBarToolBase
*GetItemSkippingDummySpacers(const wxToolBarToolsList
& tools
,
1074 wxToolBarToolsList::compatibility_iterator current
= tools
.GetFirst();
1076 for ( ; current
!= 0; current
= current
->GetNext() )
1079 return current
->GetData();
1081 wxToolBarTool
*tool
= (wxToolBarTool
*)current
->GetData();
1082 size_t separators
= tool
->GetSeparatorsCount();
1084 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1085 // otherwise, skip as many items as the separator count, plus the
1087 index
-= separators
? separators
+ 1 : 1;
1093 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
1098 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
1099 // MBN: when the point ( x, y ) is close to the toolbar border
1100 // TB_HITTEST returns m_nButtons ( not -1 )
1101 if ( index
< 0 || (size_t)index
>= m_nButtons
)
1103 // it's a separator or there is no tool at all there
1104 return (wxToolBarToolBase
*)NULL
;
1107 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1108 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1109 if ( wxTheApp
->GetComCtl32Version() >= 471 )
1111 return m_tools
.Item((size_t)index
)->GetData();
1116 return GetItemSkippingDummySpacers( m_tools
, (size_t) index
);
1120 void wxToolBar::UpdateSize()
1122 // the toolbar size changed
1123 ::SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
1125 // we must also refresh the frame after the toolbar size (possibly) changed
1126 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
1129 frame
->SendSizeEvent();
1133 // ----------------------------------------------------------------------------
1135 // ---------------------------------------------------------------------------
1137 void wxToolBar::SetWindowStyleFlag(long style
)
1139 // the style bits whose changes force us to recreate the toolbar
1140 static const long MASK_NEEDS_RECREATE
= wxTB_TEXT
| wxTB_NOICONS
;
1142 const long styleOld
= GetWindowStyle();
1144 wxToolBarBase::SetWindowStyleFlag(style
);
1146 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1147 // also fatal as we'd then try to recreate the toolbar when it's just being
1149 if ( GetToolsCount() &&
1150 (style
& MASK_NEEDS_RECREATE
) != (styleOld
& MASK_NEEDS_RECREATE
) )
1152 // to remove the text labels, simply re-realizing the toolbar is enough
1153 // but I don't know of any way to add the text to an existing toolbar
1154 // other than by recreating it entirely
1159 // ----------------------------------------------------------------------------
1161 // ----------------------------------------------------------------------------
1163 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
1165 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
1166 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
1169 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
1171 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
1172 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
1175 void wxToolBar::DoSetToggle(wxToolBarToolBase
*WXUNUSED(tool
), bool WXUNUSED(toggle
))
1177 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1178 // without, so we really need to delete the button and recreate it here
1179 wxFAIL_MSG( _T("not implemented") );
1182 // ----------------------------------------------------------------------------
1184 // ----------------------------------------------------------------------------
1186 // Responds to colour changes, and passes event on to children.
1187 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1189 wxRGBToColour(m_backgroundColour
, ::GetSysColor(COLOR_BTNFACE
));
1191 // Remap the buttons
1194 // Relayout the toolbar
1195 int nrows
= m_maxRows
;
1196 m_maxRows
= 0; // otherwise SetRows() wouldn't do anything
1201 // let the event propagate further
1205 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
1207 if (event
.Leaving() && m_pInTool
)
1214 if ( event
.RightDown() )
1216 // find the tool under the mouse
1218 event
.GetPosition(&x
, &y
);
1220 wxToolBarToolBase
*tool
= FindToolForPosition(x
, y
);
1221 OnRightClick(tool
? tool
->GetId() : -1, x
, y
);
1229 bool wxToolBar::HandleSize(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1231 // calculate our minor dimension ourselves - we're confusing the standard
1232 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1234 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
1238 if ( GetWindowStyle() & wxTB_VERTICAL
)
1240 w
= r
.right
- r
.left
;
1243 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
1250 if (HasFlag( wxTB_FLAT
))
1251 h
= r
.bottom
- r
.top
- 3;
1253 h
= r
.bottom
- r
.top
;
1256 // FIXME: hardcoded separator line height...
1257 h
+= HasFlag(wxTB_NODIVIDER
) ? 4 : 6;
1262 if ( MAKELPARAM(w
, h
) != lParam
)
1264 // size really changed
1268 // message processed
1275 bool wxToolBar::HandlePaint(WXWPARAM wParam
, WXLPARAM lParam
)
1277 // erase any dummy separators which we used for aligning the controls if
1280 // first of all, do we have any controls at all?
1281 wxToolBarToolsList::compatibility_iterator node
;
1282 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1284 if ( node
->GetData()->IsControl() )
1290 // no controls, nothing to erase
1294 // prepare the DC on which we'll be drawing
1295 wxClientDC
dc(this);
1296 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
1297 dc
.SetPen(*wxTRANSPARENT_PEN
);
1300 if ( !::GetUpdateRect(GetHwnd(), &r
, FALSE
) )
1302 // nothing to redraw anyhow
1307 wxCopyRECTToRect(r
, rectUpdate
);
1309 dc
.SetClippingRegion(rectUpdate
);
1311 // draw the toolbar tools, separators &c normally
1312 wxControl::MSWWindowProc(WM_PAINT
, wParam
, lParam
);
1314 // for each control in the toolbar find all the separators intersecting it
1317 // NB: this is really the only way to do it as we don't know if a separator
1318 // corresponds to a control (i.e. is a dummy one) or a real one
1320 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
1322 wxToolBarToolBase
*tool
= node
->GetData();
1323 if ( tool
->IsControl() )
1325 // get the control rect in our client coords
1326 wxControl
*control
= tool
->GetControl();
1327 wxRect rectCtrl
= control
->GetRect();
1329 // iterate over all buttons
1331 int count
= ::SendMessage(GetHwnd(), TB_BUTTONCOUNT
, 0, 0);
1332 for ( int n
= 0; n
< count
; n
++ )
1334 // is it a separator?
1335 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON
,
1338 wxLogDebug(_T("TB_GETBUTTON failed?"));
1343 if ( tbb
.fsStyle
!= TBSTYLE_SEP
)
1346 // get the bounding rect of the separator
1348 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
,
1351 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1356 // does it intersect the control?
1358 wxCopyRECTToRect(r
, rectItem
);
1359 if ( rectCtrl
.Intersects(rectItem
) )
1361 // yes, do erase it!
1362 dc
.DrawRectangle(rectItem
);
1364 // Necessary in case we use a no-paint-on-size
1365 // style in the parent: the controls can disappear
1366 control
->Refresh(false);
1375 void wxToolBar::HandleMouseMove(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
1377 wxCoord x
= GET_X_LPARAM(lParam
),
1378 y
= GET_Y_LPARAM(lParam
);
1379 wxToolBarToolBase
* tool
= FindToolForPosition( x
, y
);
1381 // cursor left current tool
1382 if( tool
!= m_pInTool
&& !tool
)
1388 // cursor entered a tool
1389 if( tool
!= m_pInTool
&& tool
)
1392 OnMouseEnter( tool
->GetId() );
1396 WXLRESULT
wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1401 if ( HandleSize(wParam
, lParam
) )
1406 // we don't handle mouse moves, so always pass the message to
1407 // wxControl::MSWWindowProc
1408 HandleMouseMove(wParam
, lParam
);
1412 if ( HandlePaint(wParam
, lParam
) )
1416 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1419 // ----------------------------------------------------------------------------
1420 // private functions
1421 // ----------------------------------------------------------------------------
1423 WXHBITMAP
wxToolBar::MapBitmap(WXHBITMAP bitmap
, int width
, int height
)
1429 wxLogLastError(_T("CreateCompatibleDC"));
1434 SelectInHDC
bmpInHDC(hdcMem
, (HBITMAP
)bitmap
);
1438 wxLogLastError(_T("SelectObject"));
1443 wxCOLORMAP
*cmap
= wxGetStdColourMap();
1445 for ( int i
= 0; i
< width
; i
++ )
1447 for ( int j
= 0; j
< height
; j
++ )
1449 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1451 for ( size_t k
= 0; k
< wxSTD_COL_MAX
; k
++ )
1453 COLORREF col
= cmap
[k
].from
;
1454 if ( abs(GetRValue(pixel
) - GetRValue(col
)) < 10 &&
1455 abs(GetGValue(pixel
) - GetGValue(col
)) < 10 &&
1456 abs(GetBValue(pixel
) - GetBValue(col
)) < 10 )
1458 ::SetPixel(hdcMem
, i
, j
, cmap
[k
].to
);
1467 // VZ: I leave here my attempts to map the bitmap to the system colours
1468 // faster by using BitBlt() even though it's broken currently - but
1469 // maybe someone else can finish it? It should be faster than iterating
1470 // over all pixels...
1472 MemoryHDC hdcMask
, hdcDst
;
1473 if ( !hdcMask
|| !hdcDst
)
1475 wxLogLastError(_T("CreateCompatibleDC"));
1480 // create the target bitmap
1481 HBITMAP hbmpDst
= ::CreateCompatibleBitmap(hdcDst
, width
, height
);
1484 wxLogLastError(_T("CreateCompatibleBitmap"));
1489 // create the monochrome mask bitmap
1490 HBITMAP hbmpMask
= ::CreateBitmap(width
, height
, 1, 1, 0);
1493 wxLogLastError(_T("CreateBitmap(mono)"));
1495 ::DeleteObject(hbmpDst
);
1500 SelectInHDC
bmpInDst(hdcDst
, hbmpDst
),
1501 bmpInMask(hdcMask
, hbmpMask
);
1504 for ( n
= 0; n
< NUM_OF_MAPPED_COLOURS
; n
++ )
1506 // create the mask for this colour
1507 ::SetBkColor(hdcMem
, ColorMap
[n
].from
);
1508 ::BitBlt(hdcMask
, 0, 0, width
, height
, hdcMem
, 0, 0, SRCCOPY
);
1510 // replace this colour with the target one in the dst bitmap
1511 HBRUSH hbr
= ::CreateSolidBrush(ColorMap
[n
].to
);
1512 HGDIOBJ hbrOld
= ::SelectObject(hdcDst
, hbr
);
1514 ::MaskBlt(hdcDst
, 0, 0, width
, height
,
1517 MAKEROP4(PATCOPY
, SRCCOPY
));
1519 (void)::SelectObject(hdcDst
, hbrOld
);
1520 ::DeleteObject(hbr
);
1523 ::DeleteObject((HBITMAP
)bitmap
);
1525 return (WXHBITMAP
)hbmpDst
;
1529 #endif // wxUSE_TOOLBAR && Win95