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"
37 #if wxUSE_BUTTONBAR && wxUSE_TOOLBAR && defined(__WIN95__)
39 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
43 #include "wx/msw/private.h"
47 #ifdef __GNUWIN32_OLD__
48 #include "wx/msw/gnuwin32/extra.h"
55 #include "wx/msw/dib.h"
56 #include "wx/tbar95.h"
57 #include "wx/app.h" // for GetComCtl32Version
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // these standard constants are not always defined in compilers headers
67 #define TBSTYLE_LIST 0x1000
68 #define TBSTYLE_FLAT 0x0800
69 #define TBSTYLE_TRANSPARENT 0x8000
71 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
75 #define TB_GETSTYLE (WM_USER + 57)
76 #define TB_SETSTYLE (WM_USER + 56)
79 // these values correspond to those used by comctl32.dll
80 #define DEFAULTBITMAPX 16
81 #define DEFAULTBITMAPY 15
82 #define DEFAULTBUTTONX 24
83 #define DEFAULTBUTTONY 24
84 #define DEFAULTBARHEIGHT 27
86 // ----------------------------------------------------------------------------
87 // function prototypes
88 // ----------------------------------------------------------------------------
90 static void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 #if !USE_SHARED_LIBRARY
97 IMPLEMENT_DYNAMIC_CLASS(wxToolBar95
, wxToolBarBase
)
100 BEGIN_EVENT_TABLE(wxToolBar95
, wxToolBarBase
)
101 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent
)
102 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged
)
105 // ============================================================================
107 // ============================================================================
109 // ----------------------------------------------------------------------------
110 // wxToolBar95 construction
111 // ----------------------------------------------------------------------------
113 void wxToolBar95::Init()
118 m_defaultWidth
= DEFAULTBITMAPX
;
119 m_defaultHeight
= DEFAULTBITMAPY
;
122 bool wxToolBar95::Create(wxWindow
*parent
,
127 const wxString
& name
)
129 wxASSERT_MSG( (style
& wxTB_VERTICAL
) == 0,
130 wxT("Sorry, wxToolBar95 under Windows 95 only "
131 "supports horizontal orientation.") );
133 // common initialisation
134 if ( !CreateControl(parent
, id
, pos
, size
, style
, name
) )
138 DWORD msflags
= 0; // WS_VISIBLE | WS_CHILD always included
139 if (style
& wxBORDER
)
140 msflags
|= WS_BORDER
;
141 msflags
|= TBSTYLE_TOOLTIPS
;
143 if (style
& wxTB_FLAT
)
145 if (wxTheApp
->GetComCtl32Version() > 400)
146 msflags
|= TBSTYLE_FLAT
;
149 // MSW-specific initialisation
150 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME
, msflags
) )
153 // toolbar-specific post initialisation
154 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
156 // set up the colors and fonts
157 wxRGBToColour(m_backgroundColour
, GetSysColor(COLOR_BTNFACE
));
158 m_foregroundColour
= *wxBLACK
;
160 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
171 height
= m_defaultHeight
;
177 SetSize(x
, y
, width
, height
);
182 wxToolBar95::~wxToolBar95()
188 ::DeleteObject((HBITMAP
) m_hBitmap
);
192 void wxToolBar95::ClearTools()
194 // TODO: Don't know how to reset the toolbar bitmap, as yet.
195 // But adding tools and calling CreateTools should probably
196 // recreate a buttonbar OK.
197 wxToolBarBase::ClearTools();
200 bool wxToolBar95::AddControl(wxControl
*control
)
202 wxCHECK_MSG( control
, FALSE
, _T("toolbar: can't insert NULL control") );
204 wxCHECK_MSG( control
->GetParent() == this, FALSE
,
205 _T("control must have toolbar as parent") );
207 wxToolBarTool
*tool
= new wxToolBarTool(control
);
209 m_tools
.Append(control
->GetId(), tool
);
214 wxToolBarTool
*wxToolBar95::AddTool(int index
,
215 const wxBitmap
& bitmap
,
216 const wxBitmap
& pushedBitmap
,
218 long xPos
, long yPos
,
219 wxObject
*clientData
,
220 const wxString
& helpString1
,
221 const wxString
& helpString2
)
223 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, wxNullBitmap
,
225 helpString1
, helpString2
);
226 tool
->m_clientData
= clientData
;
231 tool
->m_x
= m_xMargin
;
236 tool
->m_y
= m_yMargin
;
238 tool
->SetSize(GetToolSize().x
, GetToolSize().y
);
240 m_tools
.Append((long)index
, tool
);
245 bool wxToolBar95::CreateTools()
247 size_t nTools
= m_tools
.GetCount();
251 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
253 int totalBitmapWidth
= (int)(m_defaultWidth
* nTools
);
254 int totalBitmapHeight
= (int)m_defaultHeight
;
256 // Create a bitmap for all the tool bitmaps
257 HDC dc
= ::GetDC(NULL
);
258 m_hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap(dc
,
261 ::ReleaseDC(NULL
, dc
);
263 // Now blit all the tools onto this bitmap
264 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
265 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, (HBITMAP
)m_hBitmap
);
267 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
269 // the button position
272 // the number of buttons (not separators)
275 wxNode
*node
= m_tools
.First();
278 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
279 if ( tool
->m_toolStyle
== wxTOOL_STYLE_BUTTON
&& tool
->m_bitmap1
.Ok() )
281 HBITMAP hbmp
= GetHbitmapOf(tool
->m_bitmap1
);
284 HBITMAP oldBitmap2
= (HBITMAP
)::SelectObject(memoryDC2
, hbmp
);
285 if ( !BitBlt(memoryDC
, x
, 0, m_defaultWidth
, m_defaultHeight
,
286 memoryDC2
, 0, 0, SRCCOPY
) )
288 wxLogLastError("BitBlt");
291 ::SelectObject(memoryDC2
, oldBitmap2
);
300 ::SelectObject(memoryDC
, oldBitmap
);
301 ::DeleteDC(memoryDC
);
302 ::DeleteDC(memoryDC2
);
304 // Map to system colours
305 wxMapBitmap((HBITMAP
) m_hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
307 if ( oldToolBarBitmap
)
309 TBREPLACEBITMAP replaceBitmap
;
310 replaceBitmap
.hInstOld
= NULL
;
311 replaceBitmap
.hInstNew
= NULL
;
312 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
313 replaceBitmap
.nIDNew
= (UINT
) (HBITMAP
) m_hBitmap
;
314 replaceBitmap
.nButtons
= noButtons
;
315 if ( ::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
316 0, (LPARAM
) &replaceBitmap
) == -1 )
318 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
321 ::DeleteObject((HBITMAP
) oldToolBarBitmap
);
323 // Now delete all the buttons
327 // TODO: What about separators???? They don't have an id!
328 if ( ! ::SendMessage( GetHwnd(), TB_DELETEBUTTON
, i
, 0 ) )
334 TBADDBITMAP addBitmap
;
336 addBitmap
.nID
= (UINT
)m_hBitmap
;
337 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
338 (WPARAM
) noButtons
, (LPARAM
)&addBitmap
) == -1 )
340 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
344 // Now add the buttons.
345 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
347 // this array will holds the indices of all controls in the toolbar
348 wxArrayInt controlIds
;
353 node
= m_tools
.First();
356 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
357 TBBUTTON
& button
= buttons
[i
];
359 wxZeroMemory(button
);
361 switch ( tool
->m_toolStyle
)
363 case wxTOOL_STYLE_CONTROL
:
365 button
.idCommand
= tool
->m_index
;
366 // fall through: create just a separator too
368 case wxTOOL_STYLE_SEPARATOR
:
369 button
.fsState
= TBSTATE_ENABLED
;
370 button
.fsStyle
= TBSTYLE_SEP
;
373 case wxTOOL_STYLE_BUTTON
:
374 button
.iBitmap
= bitmapId
;
375 button
.idCommand
= tool
->m_index
;
378 button
.fsState
|= TBSTATE_ENABLED
;
379 if (tool
->m_toggleState
)
380 button
.fsState
|= TBSTATE_CHECKED
;
381 button
.fsStyle
= tool
->m_isToggle
? TBSTYLE_CHECK
392 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
,
393 (WPARAM
)i
, (LPARAM
)buttons
) )
395 wxLogLastError("TB_ADDBUTTONS");
400 // adjust the controls size to fit nicely in the toolbar
401 size_t nControls
= controlIds
.GetCount();
402 for ( size_t nCtrl
= 0; nCtrl
< nControls
; nCtrl
++ )
404 wxToolBarTool
*tool
= (wxToolBarTool
*)
405 m_tools
.Nth(controlIds
[nCtrl
])->Data();
406 wxControl
*control
= tool
->GetControl();
408 wxSize size
= control
->GetSize();
410 // set the (underlying) separators width to be that of the control
412 tbbi
.cbSize
= sizeof(tbbi
);
413 tbbi
.dwMask
= TBIF_SIZE
;
415 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
416 tool
->m_index
, (LPARAM
)&tbbi
) )
418 // the index is probably invalid
419 wxLogLastError("TB_SETBUTTONINFO");
422 // and position the control itself correctly vertically
424 if ( !SendMessage(GetHwnd(), TB_GETRECT
,
425 tool
->m_index
, (LPARAM
)(LPRECT
)&r
) )
427 wxLogLastError("TB_GETRECT");
430 int height
= r
.bottom
- r
.top
;
431 int diff
= height
- size
.y
;
434 // the control is too high, resize to fit
435 control
->SetSize(-1, height
- 2);
440 control
->Move(r
.left
, r
.top
+ diff
/ 2);
443 (void)::SendMessage(GetHwnd(), TB_AUTOSIZE
, (WPARAM
)0, (LPARAM
) 0);
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
454 bool wxToolBar95::MSWCommand(WXUINT cmd
, WXWORD id
)
456 wxNode
*node
= m_tools
.Find((long)id
);
460 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
461 if (tool
->m_isToggle
)
463 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
464 tool
->m_toggleState
= state
& TBSTATE_CHECKED
;
467 BOOL ret
= OnLeftClick((int)id
, tool
->m_toggleState
);
468 if ( ret
== FALSE
&& tool
->m_isToggle
)
470 tool
->m_toggleState
= !tool
->m_toggleState
;
471 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
472 (WPARAM
)id
, (LPARAM
)MAKELONG(tool
->m_toggleState
, 0));
478 bool wxToolBar95::MSWOnNotify(int WXUNUSED(idCtrl
),
482 // First check if this applies to us
483 NMHDR
*hdr
= (NMHDR
*)lParam
;
485 // the tooltips control created by the toolbar is sometimes Unicode, even
486 // in an ANSI application - this seems to be a bug in comctl32.dll v5
487 int code
= (int)hdr
->code
;
488 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
491 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
492 if ( toolTipWnd
!= hdr
->hwndFrom
)
495 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
496 int id
= (int)ttText
->hdr
.idFrom
;
497 wxNode
*node
= m_tools
.Find((long)id
);
501 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
503 const wxString
& help
= tool
->m_shortHelpString
;
505 if ( !help
.IsEmpty() )
507 if ( code
== TTN_NEEDTEXTA
)
509 ttText
->lpszText
= (wxChar
*)help
.c_str();
511 #if (_WIN32_IE >= 0x0300)
514 // FIXME this is a temp hack only until I understand better what
515 // must be done in both ANSI and Unicode builds
517 size_t lenAnsi
= help
.Len();
519 // MetroWerks doesn't like calling mbstowcs with NULL argument
520 size_t lenUnicode
= 2*lenAnsi
;
522 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
525 // using the pointer of right type avoids us doing all sorts of
526 // pointer arithmetics ourselves
527 wchar_t *dst
= (wchar_t *)ttText
->szText
,
528 *pwz
= new wchar_t[lenUnicode
+ 1];
529 mbstowcs(pwz
, help
, lenAnsi
+ 1);
530 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
532 // put the terminating _wide_ NUL
537 #endif // _WIN32_IE >= 0x0300
540 // For backward compatibility...
541 OnMouseEnter(tool
->m_index
);
546 // ----------------------------------------------------------------------------
548 // ----------------------------------------------------------------------------
550 void wxToolBar95::SetToolBitmapSize(const wxSize
& size
)
552 wxToolBarBase::SetToolBitmapSize(size
);
554 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
557 void wxToolBar95::SetRows(int nRows
)
559 // TRUE in wParam means to create at least as many rows
561 ::SendMessage(GetHwnd(), TB_SETROWS
,
562 MAKEWPARAM(nRows
, TRUE
), (LPARAM
) &rect
);
564 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
565 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
570 wxSize
wxToolBar95::GetMaxSize() const
572 if ( (m_maxWidth
== -1) || (m_maxHeight
== -1) )
574 // it has a side effect of filling m_maxWidth/Height variables
575 ((wxToolBar95
*)this)->SetRows(m_maxRows
); // const_cast
578 return wxSize(m_maxWidth
, m_maxHeight
);
581 // The button size is bigger than the bitmap size
582 wxSize
wxToolBar95::GetToolSize() const
584 // FIXME: this is completely bogus (VZ)
585 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
588 // ----------------------------------------------------------------------------
590 // ----------------------------------------------------------------------------
592 void wxToolBar95::EnableTool(int toolIndex
, bool enable
)
594 wxNode
*node
= m_tools
.Find((long)toolIndex
);
597 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
598 tool
->m_enabled
= enable
;
599 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
600 (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(enable
, 0));
604 void wxToolBar95::ToggleTool(int toolIndex
, bool toggle
)
606 wxNode
*node
= m_tools
.Find((long)toolIndex
);
609 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
610 if (tool
->m_isToggle
)
612 tool
->m_toggleState
= toggle
;
613 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
614 (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(toggle
, 0));
619 bool wxToolBar95::GetToolState(int toolIndex
) const
621 return (::SendMessage(GetHwnd(), TB_ISBUTTONCHECKED
, (WPARAM
)toolIndex
, (LPARAM
)0) != 0);
624 // ----------------------------------------------------------------------------
626 // ----------------------------------------------------------------------------
628 // Responds to colour changes, and passes event on to children.
629 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent
& event
)
631 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
632 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
639 // Propagate the event to the non-top-level children
640 wxWindow::OnSysColourChanged(event
);
643 void wxToolBar95::OnMouseEvent(wxMouseEvent
& event
)
645 if (event
.RightDown())
647 // For now, we don't have an id. Later we could
648 // try finding the tool.
649 OnRightClick((int)-1, event
.GetX(), event
.GetY());
657 // ----------------------------------------------------------------------------
659 // ----------------------------------------------------------------------------
661 // These are the default colors used to map the bitmap colors
662 // to the current system colors
664 // VZ: why are they BGR and not RGB? just to confuse the people or is there a
666 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
667 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
668 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
669 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
670 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
671 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
673 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
675 COLORMAP ColorMap
[] =
677 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
678 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
679 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
680 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
681 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
682 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
685 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
687 for ( n
= 0; n
< NUM_MAPS
; n
++)
689 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
693 HDC hdcMem
= CreateCompatibleDC(NULL
);
697 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
700 for ( i
= 0; i
< width
; i
++)
702 for ( j
= 0; j
< height
; j
++)
704 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
706 BYTE red = GetRValue(pixel);
707 BYTE green = GetGValue(pixel);
708 BYTE blue = GetBValue(pixel);
711 for ( k
= 0; k
< NUM_MAPS
; k
++)
713 if ( ColorMap
[k
].from
== pixel
)
715 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
723 SelectObject(hdcMem
, hbmOld
);
724 DeleteObject(hdcMem
);
729 // Some experiments...
731 // What we want to do is create another bitmap which has a depth of 4,
732 // and set the bits. So probably we want to convert this HBITMAP into a
733 // DIB, then call SetDIBits.
734 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
735 // the screen), then SetDIBits fails.
736 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
737 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
738 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
741 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
743 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
745 DWORD err
= GetLastError();
747 ::ReleaseDC(NULL
, dc
);
750 GlobalUnlock (newDIB
);
753 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
754 // Substitute our new bitmap for the old one
755 ::DeleteObject((HBITMAP
) m_hBitmap
);
756 m_hBitmap
= (WXHBITMAP
) newBitmap
;
760 #endif // !(wxUSE_TOOLBAR && Win95)