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 #ifdef __GNUWIN32_OLD__
54 #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
;
201 bool wxToolBar::Create(wxWindow
*parent
,
206 const wxString
& name
)
208 // common initialisation
209 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
213 DWORD msflags
= 0; // WS_VISIBLE | WS_CHILD always included
214 if (style
& wxBORDER
)
215 msflags
|= WS_BORDER
;
217 #ifdef TBSTYLE_TOOLTIPS
218 msflags
|= TBSTYLE_TOOLTIPS
;
221 if (style
& wxTB_FLAT
)
223 if (wxTheApp
->GetComCtl32Version() > 400)
224 msflags
|= TBSTYLE_FLAT
;
227 // MSW-specific initialisation
228 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME
, msflags
) )
231 // toolbar-specific post initialisation
232 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
234 // set up the colors and fonts
235 wxRGBToColour(m_backgroundColour
, GetSysColor(COLOR_BTNFACE
));
236 m_foregroundColour
= *wxBLACK
;
238 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
249 height
= m_defaultHeight
;
255 SetSize(x
, y
, width
, height
);
260 wxToolBar::~wxToolBar()
262 // we must refresh the frame size when the toolbar is deleted but the frame
263 // is not - otherwise toolbar leaves a hole in the place it used to occupy
265 // NB: a frame is being deleted only if it is not any longer in
266 // wxTopLevelWindows list
267 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
268 if ( frame
&& wxTopLevelWindows
.Find(frame
) )
270 frame
->SendSizeEvent();
275 ::DeleteObject((HBITMAP
) m_hBitmap
);
279 // ----------------------------------------------------------------------------
280 // adding/removing tools
281 // ----------------------------------------------------------------------------
283 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
284 wxToolBarToolBase
*tool
)
286 // nothing special to do here - we really create the toolbar buttons in
293 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
295 // the main difficulty we have here is with the controls in the toolbars:
296 // as we (sometimes) use several separators to cover up the space used by
297 // them, the indices are not the same for us and the toolbar
299 // first determine the position of the first button to delete: it may be
300 // different from pos if we use several separators to cover the space used
302 wxToolBarToolsList::Node
*node
;
303 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
305 wxToolBarToolBase
*tool2
= node
->GetData();
308 // let node point to the next node in the list
309 node
= node
->GetNext();
314 if ( tool2
->IsControl() )
316 pos
+= ((wxToolBarTool
*)tool2
)->GetSeparatorsCount();
320 // now determine the number of buttons to delete and the area taken by them
321 size_t nButtonsToDelete
= 1;
323 // get the size of the button we're going to delete
325 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
327 wxLogLastError(_T("TB_GETITEMRECT"));
330 int width
= r
.right
- r
.left
;
332 if ( tool
->IsControl() )
334 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
336 width
*= nButtonsToDelete
;
339 // do delete all buttons
340 m_nButtons
-= nButtonsToDelete
;
341 while ( nButtonsToDelete
-- > 0 )
343 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
345 wxLogLastError(wxT("TB_DELETEBUTTON"));
353 // and finally reposition all the controls after this button (the toolbar
354 // takes care of all normal items)
355 for ( /* node -> first after deleted */ ; node
; node
= node
->GetNext() )
357 wxToolBarToolBase
*tool2
= node
->GetData();
358 if ( tool2
->IsControl() )
361 wxControl
*control
= tool2
->GetControl();
362 control
->GetPosition(&x
, NULL
);
363 control
->Move(x
- width
, -1);
370 bool wxToolBar::Realize()
372 size_t nTools
= GetToolsCount();
379 bool isVertical
= (GetWindowStyle() & wxTB_VERTICAL
) != 0;
381 // First, add the bitmap: we use one bitmap for all toolbar buttons
382 // ----------------------------------------------------------------
384 // if we already have a bitmap, we'll replace the existing one - otherwise
385 // we'll install a new one
386 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
388 int totalBitmapWidth
= (int)(m_defaultWidth
* nTools
);
389 int totalBitmapHeight
= (int)m_defaultHeight
;
391 // Create a bitmap and copy all the tool bitmaps to it
393 wxMemoryDC dcAllButtons
;
394 wxBitmap
bitmap(totalBitmapWidth
, totalBitmapHeight
);
395 dcAllButtons
.SelectObject(bitmap
);
396 dcAllButtons
.SetBackground(*wxLIGHT_GREY_BRUSH
);
397 dcAllButtons
.Clear();
399 m_hBitmap
= bitmap
.GetHBITMAP();
400 HBITMAP hBitmap
= (HBITMAP
)m_hBitmap
;
401 #else // !USE_BITMAP_MASKS
402 HBITMAP hBitmap
= ::CreateCompatibleBitmap(ScreenHDC(),
407 wxLogLastError(_T("CreateCompatibleBitmap"));
412 m_hBitmap
= (WXHBITMAP
)hBitmap
;
414 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
415 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, hBitmap
);
417 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
418 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
420 // the button position
423 // the number of buttons (not separators)
426 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
429 wxToolBarToolBase
*tool
= node
->GetData();
430 if ( tool
->IsButton() )
432 const wxBitmap
& bmp
= tool
->GetBitmap1();
436 // notice the last parameter: do use mask
437 dcAllButtons
.DrawBitmap(tool
->GetBitmap1(), x
, 0, TRUE
);
438 #else // !USE_BITMAP_MASKS
439 HBITMAP hbmp
= GetHbitmapOf(bmp
);
440 HBITMAP oldBitmap2
= (HBITMAP
)::SelectObject(memoryDC2
, hbmp
);
441 if ( !BitBlt(memoryDC
, x
, 0, m_defaultWidth
, m_defaultHeight
,
442 memoryDC2
, 0, 0, SRCCOPY
) )
444 wxLogLastError(wxT("BitBlt"));
447 ::SelectObject(memoryDC2
, oldBitmap2
);
448 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
452 wxFAIL_MSG( _T("invalid tool button bitmap") );
455 // still inc width and number of buttons because otherwise the
456 // subsequent buttons will all be shifted which is rather confusing
457 // (and like this you'd see immediately which bitmap was bad)
462 node
= node
->GetNext();
466 dcAllButtons
.SelectObject(wxNullBitmap
);
468 // don't delete this HBITMAP!
469 bitmap
.SetHBITMAP(0);
470 #else // !USE_BITMAP_MASKS
471 ::SelectObject(memoryDC
, oldBitmap
);
472 ::DeleteDC(memoryDC
);
473 ::DeleteDC(memoryDC2
);
474 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
476 // Map to system colours
477 wxMapBitmap(hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
481 bool addBitmap
= TRUE
;
483 if ( oldToolBarBitmap
)
485 #ifdef TB_REPLACEBITMAP
486 if ( wxTheApp
->GetComCtl32Version() >= 400 )
488 TBREPLACEBITMAP replaceBitmap
;
489 replaceBitmap
.hInstOld
= NULL
;
490 replaceBitmap
.hInstNew
= NULL
;
491 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
492 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
493 replaceBitmap
.nButtons
= nButtons
;
494 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
495 0, (LPARAM
) &replaceBitmap
) )
497 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
500 ::DeleteObject(oldToolBarBitmap
);
506 #endif // TB_REPLACEBITMAP
508 // we can't replace the old bitmap, so we will add another one
509 // (awfully inefficient, but what else to do?) and shift the bitmap
510 // indices accordingly
513 bitmapId
= m_nButtons
;
516 // Now delete all the buttons
517 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
519 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
521 wxLogLastError(wxT("TB_DELETEBUTTON"));
527 if ( addBitmap
) // no old bitmap or we can't replace it
529 TBADDBITMAP addBitmap
;
531 addBitmap
.nID
= (UINT
) hBitmap
;
532 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
533 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
535 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
539 // Next add the buttons and separators
540 // -----------------------------------
542 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
544 // this array will hold the indices of all controls in the toolbar
545 wxArrayInt controlIds
;
548 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
550 wxToolBarToolBase
*tool
= node
->GetData();
552 // don't add separators to the vertical toolbar - looks ugly
553 if ( isVertical
&& tool
->IsSeparator() )
556 TBBUTTON
& button
= buttons
[i
];
558 wxZeroMemory(button
);
560 switch ( tool
->GetStyle() )
562 case wxTOOL_STYLE_CONTROL
:
563 button
.idCommand
= tool
->GetId();
564 // fall through: create just a separator too
566 case wxTOOL_STYLE_SEPARATOR
:
567 button
.fsState
= TBSTATE_ENABLED
;
568 button
.fsStyle
= TBSTYLE_SEP
;
571 case wxTOOL_STYLE_BUTTON
:
572 button
.iBitmap
= bitmapId
;
573 button
.idCommand
= tool
->GetId();
575 if ( tool
->IsEnabled() )
576 button
.fsState
|= TBSTATE_ENABLED
;
577 if ( tool
->IsToggled() )
578 button
.fsState
|= TBSTATE_CHECKED
;
580 button
.fsStyle
= tool
->CanBeToggled() ? TBSTYLE_CHECK
590 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
,
591 (WPARAM
)i
, (LPARAM
)buttons
) )
593 wxLogLastError(wxT("TB_ADDBUTTONS"));
598 // Deal with the controls finally
599 // ------------------------------
601 // adjust the controls size to fit nicely in the toolbar
603 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
605 wxToolBarToolBase
*tool
= node
->GetData();
606 if ( !tool
->IsControl() )
609 wxControl
*control
= tool
->GetControl();
611 wxSize size
= control
->GetSize();
613 // the position of the leftmost controls corner
616 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
617 // latter only appeared in v4.70 of comctl32.dll
619 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
620 index
, (LPARAM
)(LPRECT
)&r
) )
622 wxLogLastError(wxT("TB_GETITEMRECT"));
625 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
626 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
627 // available in headers, now check whether it is available now
629 if ( wxTheApp
->GetComCtl32Version() >= 471 )
631 // set the (underlying) separators width to be that of the
634 tbbi
.cbSize
= sizeof(tbbi
);
635 tbbi
.dwMask
= TBIF_SIZE
;
637 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
638 tool
->GetId(), (LPARAM
)&tbbi
) )
640 // the id is probably invalid?
641 wxLogLastError(wxT("TB_SETBUTTONINFO"));
645 #endif // comctl32.dll 4.71
646 // TB_SETBUTTONINFO unavailable
648 // try adding several separators to fit the controls width
649 int widthSep
= r
.right
- r
.left
;
655 tbb
.fsState
= TBSTATE_ENABLED
;
656 tbb
.fsStyle
= TBSTYLE_SEP
;
658 size_t nSeparators
= size
.x
/ widthSep
;
659 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
661 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
662 index
, (LPARAM
)&tbb
) )
664 wxLogLastError(wxT("TB_INSERTBUTTON"));
670 // remember the number of separators we used - we'd have to
671 // delete all of them later
672 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
674 // adjust the controls width to exactly cover the separators
675 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
678 // and position the control itself correctly vertically
679 int height
= r
.bottom
- r
.top
;
680 int diff
= height
- size
.y
;
683 // the control is too high, resize to fit
684 control
->SetSize(-1, height
- 2);
689 control
->Move(left
== -1 ? r
.left
: left
, r
.top
+ (diff
+ 1) / 2);
692 // the max index is the "real" number of buttons - i.e. counting even the
693 // separators which we added just for aligning the controls
698 if ( m_maxRows
== 0 )
700 // if not set yet, only one row
704 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
706 if ( m_maxRows
== 0 )
708 // if not set yet, have one column
716 // ----------------------------------------------------------------------------
718 // ----------------------------------------------------------------------------
720 bool wxToolBar::MSWCommand(WXUINT cmd
, WXWORD id
)
722 wxToolBarToolBase
*tool
= FindById((int)id
);
726 if ( tool
->CanBeToggled() )
728 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
729 tool
->Toggle((state
& TBSTATE_CHECKED
) != 0);
732 bool toggled
= tool
->IsToggled();
734 // OnLeftClick() can veto the button state change - for buttons which may
735 // be toggled only, of couse
736 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
740 tool
->SetToggle(toggled
);
742 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
748 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
752 // First check if this applies to us
753 NMHDR
*hdr
= (NMHDR
*)lParam
;
755 // the tooltips control created by the toolbar is sometimes Unicode, even
756 // in an ANSI application - this seems to be a bug in comctl32.dll v5
757 int code
= (int)hdr
->code
;
758 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
761 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
762 if ( toolTipWnd
!= hdr
->hwndFrom
)
765 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
766 int id
= (int)ttText
->hdr
.idFrom
;
768 wxToolBarToolBase
*tool
= FindById(id
);
772 const wxString
& help
= tool
->GetShortHelp();
774 if ( !help
.IsEmpty() )
776 if ( code
== TTN_NEEDTEXTA
)
778 ttText
->lpszText
= (wxChar
*)help
.c_str();
783 ttText
->lpszText
= (wxChar
*)help
.c_str();
785 // VZ: I don't know why it happens, but the versions of
786 // comctl32.dll starting from 4.70 sometimes send TTN_NEEDTEXTW
787 // even to ANSI programs (normally, this message is supposed
788 // to be sent to Unicode programs only) - hence we need to
789 // handle it as well, otherwise no tooltips will be shown in
792 size_t lenAnsi
= help
.Len();
794 // MetroWerks doesn't like calling mbstowcs with NULL argument
795 size_t lenUnicode
= 2*lenAnsi
;
797 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
800 // using the pointer of right type avoids us doing all sorts of
801 // pointer arithmetics ourselves
802 wchar_t *dst
= (wchar_t *)ttText
->szText
,
803 *pwz
= new wchar_t[lenUnicode
+ 1];
804 mbstowcs(pwz
, help
, lenAnsi
+ 1);
805 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
807 // put the terminating _wide_ NUL
815 // For backward compatibility...
816 OnMouseEnter(tool
->GetId());
821 // ----------------------------------------------------------------------------
823 // ----------------------------------------------------------------------------
825 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
827 wxToolBarBase::SetToolBitmapSize(size
);
829 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
832 void wxToolBar::SetRows(int nRows
)
834 if ( nRows
== m_maxRows
)
836 // avoid resizing the frame uselessly
840 // TRUE in wParam means to create at least as many rows, FALSE -
843 ::SendMessage(GetHwnd(), TB_SETROWS
,
844 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
852 // The button size is bigger than the bitmap size
853 wxSize
wxToolBar::GetToolSize() const
855 // TB_GETBUTTONSIZE is supported from version 4.70
856 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 )
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);
871 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
876 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
879 // it's a separator or there is no tool at all there
880 return (wxToolBarToolBase
*)NULL
;
883 return m_tools
.Item((size_t)index
)->GetData();
886 void wxToolBar::UpdateSize()
888 // the toolbar size changed
889 SendMessage(GetHwnd(), TB_AUTOSIZE
, 0, 0);
891 // we must also refresh the frame after the toolbar size (possibly) changed
892 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
895 frame
->SendSizeEvent();
899 // ----------------------------------------------------------------------------
901 // ----------------------------------------------------------------------------
903 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
905 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
906 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
909 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
911 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
912 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
915 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
)
917 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
918 // without, so we really need to delete the button and recreate it here
919 wxFAIL_MSG( _T("not implemented") );
922 // ----------------------------------------------------------------------------
924 // ----------------------------------------------------------------------------
926 // Responds to colour changes, and passes event on to children.
927 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
929 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
930 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
937 // Propagate the event to the non-top-level children
938 wxWindow::OnSysColourChanged(event
);
941 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
943 if (event
.RightDown())
945 // For now, we don't have an id. Later we could
946 // try finding the tool.
947 OnRightClick((int)-1, event
.GetX(), event
.GetY());
955 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
957 if ( nMsg
== WM_SIZE
)
959 // calculate our minor dimenstion ourselves - we're confusing the
960 // standard logic (TB_AUTOSIZE) with our horizontal toolbars and other
963 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
967 if ( GetWindowStyle() & wxTB_VERTICAL
)
969 w
= r
.right
- r
.left
;
972 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
979 h
= r
.bottom
- r
.top
;
982 h
+= 6; // FIXME: this is the separator line height...
987 if ( MAKELPARAM(w
, h
) != lParam
)
989 // size really changed
998 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1001 // ----------------------------------------------------------------------------
1002 // private functions
1003 // ----------------------------------------------------------------------------
1005 // These are the default colors used to map the bitmap colors to the current
1006 // system colors. Note that they are in BGR format because this is what Windows
1007 // wants (and not RGB)
1009 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
1010 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
1011 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
1012 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
1013 #define BGR_BACKGROUNDSEL (RGB(000,000,255)) // blue
1014 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
1016 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
1018 COLORMAP ColorMap
[] =
1020 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
1021 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
1022 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
1023 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
1024 /* {BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue */
1025 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
1028 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
1030 for ( n
= 0; n
< NUM_MAPS
; n
++)
1032 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
1036 HDC hdcMem
= CreateCompatibleDC(NULL
);
1040 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
1043 for ( i
= 0; i
< width
; i
++)
1045 for ( j
= 0; j
< height
; j
++)
1047 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
1049 BYTE red = GetRValue(pixel);
1050 BYTE green = GetGValue(pixel);
1051 BYTE blue = GetBValue(pixel);
1054 for ( k
= 0; k
< NUM_MAPS
; k
++)
1056 if ( ColorMap
[k
].from
== pixel
)
1058 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
1066 SelectObject(hdcMem
, hbmOld
);
1067 DeleteObject(hdcMem
);
1072 // Some experiments...
1074 // What we want to do is create another bitmap which has a depth of 4,
1075 // and set the bits. So probably we want to convert this HBITMAP into a
1076 // DIB, then call SetDIBits.
1077 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
1078 // the screen), then SetDIBits fails.
1079 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
1080 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
1081 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
1084 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
1086 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
1088 DWORD err
= GetLastError();
1090 ::ReleaseDC(NULL
, dc
);
1093 GlobalUnlock (newDIB
);
1094 GlobalFree (newDIB
);
1096 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
1097 // Substitute our new bitmap for the old one
1098 ::DeleteObject((HBITMAP
) m_hBitmap
);
1099 m_hBitmap
= (WXHBITMAP
) newBitmap
;
1103 #endif // wxUSE_TOOLBAR && Win95