1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tbar95.cpp
3 // Purpose: wxToolBar95
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"
34 #include "wx/dynarray.h"
35 #include "wx/settings.h"
36 #include "wx/bitmap.h"
39 #if wxUSE_BUTTONBAR && wxUSE_TOOLBAR && defined(__WIN95__)
41 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
45 #include "wx/msw/private.h"
49 #ifdef __GNUWIN32_OLD__
50 #include "wx/msw/gnuwin32/extra.h"
57 #include "wx/msw/dib.h"
58 #include "wx/tbar95.h"
59 #include "wx/app.h" // for GetComCtl32Version
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // these standard constants are not always defined in compilers headers
69 #define TBSTYLE_LIST 0x1000
70 #define TBSTYLE_FLAT 0x0800
71 #define TBSTYLE_TRANSPARENT 0x8000
73 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
77 #define TB_GETSTYLE (WM_USER + 57)
78 #define TB_SETSTYLE (WM_USER + 56)
81 // these values correspond to those used by comctl32.dll
82 #define DEFAULTBITMAPX 16
83 #define DEFAULTBITMAPY 15
84 #define DEFAULTBUTTONX 24
85 #define DEFAULTBUTTONY 24
86 #define DEFAULTBARHEIGHT 27
88 // ----------------------------------------------------------------------------
89 // function prototypes
90 // ----------------------------------------------------------------------------
92 static void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 #if !USE_SHARED_LIBRARY
99 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
102 BEGIN_EVENT_TABLE(wxToolBar95
, wxToolBarBase
)
103 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent
)
104 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged
)
107 // ============================================================================
109 // ============================================================================
111 // ----------------------------------------------------------------------------
112 // wxToolBar95 construction
113 // ----------------------------------------------------------------------------
115 void wxToolBar95::Init()
120 m_defaultWidth
= DEFAULTBITMAPX
;
121 m_defaultHeight
= DEFAULTBITMAPY
;
124 bool wxToolBar95::Create(wxWindow
*parent
,
129 const wxString
& name
)
131 wxASSERT_MSG( (style
& wxTB_VERTICAL
) == 0,
132 wxT("Sorry, wxToolBar95 under Windows 95 only "
133 "supports horizontal orientation.") );
135 // common initialisation
136 if ( !CreateControl(parent
, id
, pos
, size
, style
, name
) )
140 DWORD msflags
= 0; // WS_VISIBLE | WS_CHILD always included
141 if (style
& wxBORDER
)
142 msflags
|= WS_BORDER
;
143 msflags
|= TBSTYLE_TOOLTIPS
;
145 if (style
& wxTB_FLAT
)
147 if (wxTheApp
->GetComCtl32Version() > 400)
148 msflags
|= TBSTYLE_FLAT
;
151 // MSW-specific initialisation
152 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME
, msflags
) )
155 // toolbar-specific post initialisation
156 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
158 // set up the colors and fonts
159 wxRGBToColour(m_backgroundColour
, GetSysColor(COLOR_BTNFACE
));
160 m_foregroundColour
= *wxBLACK
;
162 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
173 height
= m_defaultHeight
;
179 SetSize(x
, y
, width
, height
);
184 wxToolBar95::~wxToolBar95()
190 ::DeleteObject((HBITMAP
) m_hBitmap
);
194 void wxToolBar95::ClearTools()
196 // TODO: Don't know how to reset the toolbar bitmap, as yet.
197 // But adding tools and calling CreateTools should probably
198 // recreate a buttonbar OK.
199 wxToolBarBase::ClearTools();
202 bool wxToolBar95::AddControl(wxControl
*control
)
204 wxCHECK_MSG( control
, FALSE
, _T("toolbar: can't insert NULL control") );
206 wxCHECK_MSG( control
->GetParent() == this, FALSE
,
207 _T("control must have toolbar as parent") );
209 wxToolBarTool
*tool
= new wxToolBarTool(control
);
211 m_tools
.Append(control
->GetId(), tool
);
216 wxToolBarTool
*wxToolBar95::AddTool(int index
,
217 const wxBitmap
& bitmap
,
218 const wxBitmap
& pushedBitmap
,
220 long xPos
, long yPos
,
221 wxObject
*clientData
,
222 const wxString
& helpString1
,
223 const wxString
& helpString2
)
225 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, wxNullBitmap
,
227 helpString1
, helpString2
);
228 tool
->m_clientData
= clientData
;
233 tool
->m_x
= m_xMargin
;
238 tool
->m_y
= m_yMargin
;
240 tool
->SetSize(GetToolSize().x
, GetToolSize().y
);
242 m_tools
.Append((long)index
, tool
);
247 bool wxToolBar95::CreateTools()
249 size_t nTools
= m_tools
.GetCount();
253 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
255 int totalBitmapWidth
= (int)(m_defaultWidth
* nTools
);
256 int totalBitmapHeight
= (int)m_defaultHeight
;
258 // Create a bitmap for all the tool bitmaps
259 HDC dc
= ::GetDC(NULL
);
260 m_hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap(dc
,
263 ::ReleaseDC(NULL
, dc
);
265 // Now blit all the tools onto this bitmap
266 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
267 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, (HBITMAP
)m_hBitmap
);
269 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
271 // the button position
274 // the number of buttons (not separators)
277 wxNode
*node
= m_tools
.First();
280 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
281 if ( tool
->m_toolStyle
== wxTOOL_STYLE_BUTTON
&& tool
->m_bitmap1
.Ok() )
283 HBITMAP hbmp
= GetHbitmapOf(tool
->m_bitmap1
);
286 HBITMAP oldBitmap2
= (HBITMAP
)::SelectObject(memoryDC2
, hbmp
);
287 if ( !BitBlt(memoryDC
, x
, 0, m_defaultWidth
, m_defaultHeight
,
288 memoryDC2
, 0, 0, SRCCOPY
) )
290 wxLogLastError("BitBlt");
293 ::SelectObject(memoryDC2
, oldBitmap2
);
302 ::SelectObject(memoryDC
, oldBitmap
);
303 ::DeleteDC(memoryDC
);
304 ::DeleteDC(memoryDC2
);
306 // Map to system colours
307 wxMapBitmap((HBITMAP
) m_hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
309 if ( oldToolBarBitmap
)
311 TBREPLACEBITMAP replaceBitmap
;
312 replaceBitmap
.hInstOld
= NULL
;
313 replaceBitmap
.hInstNew
= NULL
;
314 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
315 replaceBitmap
.nIDNew
= (UINT
) (HBITMAP
) m_hBitmap
;
316 replaceBitmap
.nButtons
= noButtons
;
317 if ( ::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
318 0, (LPARAM
) &replaceBitmap
) == -1 )
320 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
323 ::DeleteObject((HBITMAP
) oldToolBarBitmap
);
325 // Now delete all the buttons
329 // TODO: What about separators???? They don't have an id!
330 if ( ! ::SendMessage( GetHwnd(), TB_DELETEBUTTON
, i
, 0 ) )
336 TBADDBITMAP addBitmap
;
338 addBitmap
.nID
= (UINT
)m_hBitmap
;
339 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
340 (WPARAM
) noButtons
, (LPARAM
)&addBitmap
) == -1 )
342 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
346 // Now add the buttons.
347 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
349 // this array will holds the indices of all controls in the toolbar
350 wxArrayInt controlIds
;
355 node
= m_tools
.First();
358 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
359 TBBUTTON
& button
= buttons
[i
];
361 wxZeroMemory(button
);
363 switch ( tool
->m_toolStyle
)
365 case wxTOOL_STYLE_CONTROL
:
367 button
.idCommand
= tool
->m_index
;
368 // fall through: create just a separator too
370 case wxTOOL_STYLE_SEPARATOR
:
371 button
.fsState
= TBSTATE_ENABLED
;
372 button
.fsStyle
= TBSTYLE_SEP
;
375 case wxTOOL_STYLE_BUTTON
:
376 button
.iBitmap
= bitmapId
;
377 button
.idCommand
= tool
->m_index
;
380 button
.fsState
|= TBSTATE_ENABLED
;
381 if (tool
->m_toggleState
)
382 button
.fsState
|= TBSTATE_CHECKED
;
383 button
.fsStyle
= tool
->m_isToggle
? TBSTYLE_CHECK
394 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
,
395 (WPARAM
)i
, (LPARAM
)buttons
) )
397 wxLogLastError("TB_ADDBUTTONS");
402 // TBBUTTONINFO struct declaration is new (comctl32.dll 4.70+)
403 #if !defined(__GNUWIN32__) && !defined(__WATCOMC__) && !defined(__BORLANDC__)
404 // adjust the controls size to fit nicely in the toolbar
405 size_t nControls
= controlIds
.GetCount();
406 for ( size_t nCtrl
= 0; nCtrl
< nControls
; nCtrl
++ )
408 wxToolBarTool
*tool
= (wxToolBarTool
*)
409 m_tools
.Nth(controlIds
[nCtrl
])->Data();
410 wxControl
*control
= tool
->GetControl();
412 wxSize size
= control
->GetSize();
414 // set the (underlying) separators width to be that of the control
416 tbbi
.cbSize
= sizeof(tbbi
);
417 tbbi
.dwMask
= TBIF_SIZE
;
419 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
420 tool
->m_index
, (LPARAM
)&tbbi
) )
422 // the index is probably invalid
423 wxLogLastError("TB_SETBUTTONINFO");
426 // and position the control itself correctly vertically
428 if ( !SendMessage(GetHwnd(), TB_GETRECT
,
429 tool
->m_index
, (LPARAM
)(LPRECT
)&r
) )
431 wxLogLastError("TB_GETRECT");
434 int height
= r
.bottom
- r
.top
;
435 int diff
= height
- size
.y
;
438 // the control is too high, resize to fit
439 control
->SetSize(-1, height
- 2);
444 control
->Move(r
.left
, r
.top
+ diff
/ 2);
446 #endif // __GNUWIN32__
448 (void)::SendMessage(GetHwnd(), TB_AUTOSIZE
, (WPARAM
)0, (LPARAM
) 0);
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
459 bool wxToolBar95::MSWCommand(WXUINT cmd
, WXWORD id
)
461 wxNode
*node
= m_tools
.Find((long)id
);
465 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
466 if (tool
->m_isToggle
)
468 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
469 tool
->m_toggleState
= state
& TBSTATE_CHECKED
;
472 BOOL ret
= OnLeftClick((int)id
, tool
->m_toggleState
);
473 if ( ret
== FALSE
&& tool
->m_isToggle
)
475 tool
->m_toggleState
= !tool
->m_toggleState
;
476 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
477 (WPARAM
)id
, (LPARAM
)MAKELONG(tool
->m_toggleState
, 0));
483 bool wxToolBar95::MSWOnNotify(int WXUNUSED(idCtrl
),
487 // First check if this applies to us
488 NMHDR
*hdr
= (NMHDR
*)lParam
;
490 // the tooltips control created by the toolbar is sometimes Unicode, even
491 // in an ANSI application - this seems to be a bug in comctl32.dll v5
492 int code
= (int)hdr
->code
;
493 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
496 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
497 if ( toolTipWnd
!= hdr
->hwndFrom
)
500 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
501 int id
= (int)ttText
->hdr
.idFrom
;
502 wxNode
*node
= m_tools
.Find((long)id
);
506 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
508 const wxString
& help
= tool
->m_shortHelpString
;
510 if ( !help
.IsEmpty() )
512 if ( code
== TTN_NEEDTEXTA
)
514 ttText
->lpszText
= (wxChar
*)help
.c_str();
516 #if (_WIN32_IE >= 0x0300)
519 // FIXME this is a temp hack only until I understand better what
520 // must be done in both ANSI and Unicode builds
522 size_t lenAnsi
= help
.Len();
524 // MetroWerks doesn't like calling mbstowcs with NULL argument
525 size_t lenUnicode
= 2*lenAnsi
;
527 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
530 // using the pointer of right type avoids us doing all sorts of
531 // pointer arithmetics ourselves
532 wchar_t *dst
= (wchar_t *)ttText
->szText
,
533 *pwz
= new wchar_t[lenUnicode
+ 1];
534 mbstowcs(pwz
, help
, lenAnsi
+ 1);
535 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
537 // put the terminating _wide_ NUL
542 #endif // _WIN32_IE >= 0x0300
545 // For backward compatibility...
546 OnMouseEnter(tool
->m_index
);
551 // ----------------------------------------------------------------------------
553 // ----------------------------------------------------------------------------
555 void wxToolBar95::SetToolBitmapSize(const wxSize
& size
)
557 wxToolBarBase::SetToolBitmapSize(size
);
559 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
562 void wxToolBar95::SetRows(int nRows
)
564 // TRUE in wParam means to create at least as many rows
566 ::SendMessage(GetHwnd(), TB_SETROWS
,
567 MAKEWPARAM(nRows
, TRUE
), (LPARAM
) &rect
);
569 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
570 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
575 wxSize
wxToolBar95::GetMaxSize() const
577 if ( (m_maxWidth
== -1) || (m_maxHeight
== -1) )
579 // it has a side effect of filling m_maxWidth/Height variables
580 ((wxToolBar95
*)this)->SetRows(m_maxRows
); // const_cast
583 return wxSize(m_maxWidth
, m_maxHeight
);
586 // The button size is bigger than the bitmap size
587 wxSize
wxToolBar95::GetToolSize() const
589 // FIXME: this is completely bogus (VZ)
590 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
593 // ----------------------------------------------------------------------------
595 // ----------------------------------------------------------------------------
597 void wxToolBar95::EnableTool(int toolIndex
, bool enable
)
599 wxNode
*node
= m_tools
.Find((long)toolIndex
);
602 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
603 tool
->m_enabled
= enable
;
604 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
605 (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(enable
, 0));
609 void wxToolBar95::ToggleTool(int toolIndex
, bool toggle
)
611 wxNode
*node
= m_tools
.Find((long)toolIndex
);
614 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
615 if (tool
->m_isToggle
)
617 tool
->m_toggleState
= toggle
;
618 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
619 (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(toggle
, 0));
624 bool wxToolBar95::GetToolState(int toolIndex
) const
626 return (::SendMessage(GetHwnd(), TB_ISBUTTONCHECKED
, (WPARAM
)toolIndex
, (LPARAM
)0) != 0);
629 // ----------------------------------------------------------------------------
631 // ----------------------------------------------------------------------------
633 // Responds to colour changes, and passes event on to children.
634 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent
& event
)
636 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
637 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
644 // Propagate the event to the non-top-level children
645 wxWindow::OnSysColourChanged(event
);
648 void wxToolBar95::OnMouseEvent(wxMouseEvent
& event
)
650 if (event
.RightDown())
652 // For now, we don't have an id. Later we could
653 // try finding the tool.
654 OnRightClick((int)-1, event
.GetX(), event
.GetY());
662 // ----------------------------------------------------------------------------
664 // ----------------------------------------------------------------------------
666 // These are the default colors used to map the bitmap colors
667 // to the current system colors
669 // VZ: why are they BGR and not RGB? just to confuse the people or is there a
671 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
672 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
673 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
674 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
675 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
676 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
678 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
680 COLORMAP ColorMap
[] =
682 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
683 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
684 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
685 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
686 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
687 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
690 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
692 for ( n
= 0; n
< NUM_MAPS
; n
++)
694 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
698 HDC hdcMem
= CreateCompatibleDC(NULL
);
702 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
705 for ( i
= 0; i
< width
; i
++)
707 for ( j
= 0; j
< height
; j
++)
709 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
711 BYTE red = GetRValue(pixel);
712 BYTE green = GetGValue(pixel);
713 BYTE blue = GetBValue(pixel);
716 for ( k
= 0; k
< NUM_MAPS
; k
++)
718 if ( ColorMap
[k
].from
== pixel
)
720 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
728 SelectObject(hdcMem
, hbmOld
);
729 DeleteObject(hdcMem
);
734 // Some experiments...
736 // What we want to do is create another bitmap which has a depth of 4,
737 // and set the bits. So probably we want to convert this HBITMAP into a
738 // DIB, then call SetDIBits.
739 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
740 // the screen), then SetDIBits fails.
741 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
742 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
743 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
746 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
748 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
750 DWORD err
= GetLastError();
752 ::ReleaseDC(NULL
, dc
);
755 GlobalUnlock (newDIB
);
758 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
759 // Substitute our new bitmap for the old one
760 ::DeleteObject((HBITMAP
) m_hBitmap
);
761 m_hBitmap
= (WXHBITMAP
) newBitmap
;
765 #endif // !(wxUSE_TOOLBAR && Win95)