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"
38 #if wxUSE_BUTTONBAR && wxUSE_TOOLBAR && defined(__WIN95__)
40 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
44 #include "wx/msw/private.h"
48 #ifdef __GNUWIN32_OLD__
49 #include "wx/msw/gnuwin32/extra.h"
56 #include "wx/msw/dib.h"
57 #include "wx/tbar95.h"
58 #include "wx/app.h" // for GetComCtl32Version
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // these standard constants are not always defined in compilers headers
68 #define TBSTYLE_LIST 0x1000
69 #define TBSTYLE_FLAT 0x0800
70 #define TBSTYLE_TRANSPARENT 0x8000
72 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
76 #define TB_GETSTYLE (WM_USER + 57)
77 #define TB_SETSTYLE (WM_USER + 56)
80 // these values correspond to those used by comctl32.dll
81 #define DEFAULTBITMAPX 16
82 #define DEFAULTBITMAPY 15
83 #define DEFAULTBUTTONX 24
84 #define DEFAULTBUTTONY 24
85 #define DEFAULTBARHEIGHT 27
87 // ----------------------------------------------------------------------------
88 // function prototypes
89 // ----------------------------------------------------------------------------
91 static void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 #if !USE_SHARED_LIBRARY
98 IMPLEMENT_DYNAMIC_CLASS(wxToolBar95
, wxToolBarBase
)
101 BEGIN_EVENT_TABLE(wxToolBar95
, wxToolBarBase
)
102 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent
)
103 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged
)
106 // ============================================================================
108 // ============================================================================
110 // ----------------------------------------------------------------------------
111 // wxToolBar95 construction
112 // ----------------------------------------------------------------------------
114 void wxToolBar95::Init()
119 m_defaultWidth
= DEFAULTBITMAPX
;
120 m_defaultHeight
= DEFAULTBITMAPY
;
123 bool wxToolBar95::Create(wxWindow
*parent
,
128 const wxString
& name
)
130 wxASSERT_MSG( (style
& wxTB_VERTICAL
) == 0,
131 wxT("Sorry, wxToolBar95 under Windows 95 only "
132 "supports horizontal orientation.") );
134 // common initialisation
135 if ( !CreateControl(parent
, id
, pos
, size
, style
, name
) )
139 DWORD msflags
= 0; // WS_VISIBLE | WS_CHILD always included
140 if (style
& wxBORDER
)
141 msflags
|= WS_BORDER
;
142 msflags
|= TBSTYLE_TOOLTIPS
;
144 if (style
& wxTB_FLAT
)
146 if (wxTheApp
->GetComCtl32Version() > 400)
147 msflags
|= TBSTYLE_FLAT
;
150 // MSW-specific initialisation
151 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME
, msflags
) )
154 // toolbar-specific post initialisation
155 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
157 // set up the colors and fonts
158 wxRGBToColour(m_backgroundColour
, GetSysColor(COLOR_BTNFACE
));
159 m_foregroundColour
= *wxBLACK
;
161 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
172 height
= m_defaultHeight
;
178 SetSize(x
, y
, width
, height
);
183 wxToolBar95::~wxToolBar95()
189 ::DeleteObject((HBITMAP
) m_hBitmap
);
193 void wxToolBar95::ClearTools()
195 // TODO: Don't know how to reset the toolbar bitmap, as yet.
196 // But adding tools and calling CreateTools should probably
197 // recreate a buttonbar OK.
198 wxToolBarBase::ClearTools();
201 bool wxToolBar95::AddControl(wxControl
*control
)
203 wxCHECK_MSG( control
, FALSE
, _T("toolbar: can't insert NULL control") );
205 wxCHECK_MSG( control
->GetParent() == this, FALSE
,
206 _T("control must have toolbar as parent") );
208 wxToolBarTool
*tool
= new wxToolBarTool(control
);
210 m_tools
.Append(control
->GetId(), tool
);
215 wxToolBarTool
*wxToolBar95::AddTool(int index
,
216 const wxBitmap
& bitmap
,
217 const wxBitmap
& pushedBitmap
,
219 long xPos
, long yPos
,
220 wxObject
*clientData
,
221 const wxString
& helpString1
,
222 const wxString
& helpString2
)
224 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, wxNullBitmap
,
226 helpString1
, helpString2
);
227 tool
->m_clientData
= clientData
;
232 tool
->m_x
= m_xMargin
;
237 tool
->m_y
= m_yMargin
;
239 tool
->SetSize(GetToolSize().x
, GetToolSize().y
);
241 m_tools
.Append((long)index
, tool
);
246 bool wxToolBar95::CreateTools()
248 size_t nTools
= m_tools
.GetCount();
252 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
254 int totalBitmapWidth
= (int)(m_defaultWidth
* nTools
);
255 int totalBitmapHeight
= (int)m_defaultHeight
;
257 // Create a bitmap for all the tool bitmaps
258 HDC dc
= ::GetDC(NULL
);
259 m_hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap(dc
,
262 ::ReleaseDC(NULL
, dc
);
264 // Now blit all the tools onto this bitmap
265 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
266 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, (HBITMAP
)m_hBitmap
);
268 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
270 // the button position
273 // the number of buttons (not separators)
276 wxNode
*node
= m_tools
.First();
279 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
280 if ( tool
->m_toolStyle
== wxTOOL_STYLE_BUTTON
&& tool
->m_bitmap1
.Ok() )
282 HBITMAP hbmp
= GetHbitmapOf(tool
->m_bitmap1
);
285 HBITMAP oldBitmap2
= (HBITMAP
)::SelectObject(memoryDC2
, hbmp
);
286 if ( !BitBlt(memoryDC
, x
, 0, m_defaultWidth
, m_defaultHeight
,
287 memoryDC2
, 0, 0, SRCCOPY
) )
289 wxLogLastError("BitBlt");
292 ::SelectObject(memoryDC2
, oldBitmap2
);
301 ::SelectObject(memoryDC
, oldBitmap
);
302 ::DeleteDC(memoryDC
);
303 ::DeleteDC(memoryDC2
);
305 // Map to system colours
306 wxMapBitmap((HBITMAP
) m_hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
308 if ( oldToolBarBitmap
)
310 TBREPLACEBITMAP replaceBitmap
;
311 replaceBitmap
.hInstOld
= NULL
;
312 replaceBitmap
.hInstNew
= NULL
;
313 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
314 replaceBitmap
.nIDNew
= (UINT
) (HBITMAP
) m_hBitmap
;
315 replaceBitmap
.nButtons
= noButtons
;
316 if ( ::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
317 0, (LPARAM
) &replaceBitmap
) == -1 )
319 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
322 ::DeleteObject((HBITMAP
) oldToolBarBitmap
);
324 // Now delete all the buttons
328 // TODO: What about separators???? They don't have an id!
329 if ( ! ::SendMessage( GetHwnd(), TB_DELETEBUTTON
, i
, 0 ) )
335 TBADDBITMAP addBitmap
;
337 addBitmap
.nID
= (UINT
)m_hBitmap
;
338 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
339 (WPARAM
) noButtons
, (LPARAM
)&addBitmap
) == -1 )
341 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
345 // Now add the buttons.
346 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
348 // this array will holds the indices of all controls in the toolbar
349 wxArrayInt controlIds
;
354 node
= m_tools
.First();
357 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
358 TBBUTTON
& button
= buttons
[i
];
360 wxZeroMemory(button
);
362 switch ( tool
->m_toolStyle
)
364 case wxTOOL_STYLE_CONTROL
:
366 button
.idCommand
= tool
->m_index
;
367 // fall through: create just a separator too
369 case wxTOOL_STYLE_SEPARATOR
:
370 button
.fsState
= TBSTATE_ENABLED
;
371 button
.fsStyle
= TBSTYLE_SEP
;
374 case wxTOOL_STYLE_BUTTON
:
375 button
.iBitmap
= bitmapId
;
376 button
.idCommand
= tool
->m_index
;
379 button
.fsState
|= TBSTATE_ENABLED
;
380 if (tool
->m_toggleState
)
381 button
.fsState
|= TBSTATE_CHECKED
;
382 button
.fsStyle
= tool
->m_isToggle
? TBSTYLE_CHECK
393 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
,
394 (WPARAM
)i
, (LPARAM
)buttons
) )
396 wxLogLastError("TB_ADDBUTTONS");
401 // TBBUTTONINFO struct declaration is missing from mingw32 headers
402 #if !defined(__GNUWIN32__) && !defined(__WATCOMC__)
403 // adjust the controls size to fit nicely in the toolbar
404 size_t nControls
= controlIds
.GetCount();
405 for ( size_t nCtrl
= 0; nCtrl
< nControls
; nCtrl
++ )
407 wxToolBarTool
*tool
= (wxToolBarTool
*)
408 m_tools
.Nth(controlIds
[nCtrl
])->Data();
409 wxControl
*control
= tool
->GetControl();
411 wxSize size
= control
->GetSize();
413 // set the (underlying) separators width to be that of the control
415 tbbi
.cbSize
= sizeof(tbbi
);
416 tbbi
.dwMask
= TBIF_SIZE
;
418 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
419 tool
->m_index
, (LPARAM
)&tbbi
) )
421 // the index is probably invalid
422 wxLogLastError("TB_SETBUTTONINFO");
425 // and position the control itself correctly vertically
427 if ( !SendMessage(GetHwnd(), TB_GETRECT
,
428 tool
->m_index
, (LPARAM
)(LPRECT
)&r
) )
430 wxLogLastError("TB_GETRECT");
433 int height
= r
.bottom
- r
.top
;
434 int diff
= height
- size
.y
;
437 // the control is too high, resize to fit
438 control
->SetSize(-1, height
- 2);
443 control
->Move(r
.left
, r
.top
+ diff
/ 2);
445 #endif // __GNUWIN32__
447 (void)::SendMessage(GetHwnd(), TB_AUTOSIZE
, (WPARAM
)0, (LPARAM
) 0);
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 bool wxToolBar95::MSWCommand(WXUINT cmd
, WXWORD id
)
460 wxNode
*node
= m_tools
.Find((long)id
);
464 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
465 if (tool
->m_isToggle
)
467 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
468 tool
->m_toggleState
= state
& TBSTATE_CHECKED
;
471 BOOL ret
= OnLeftClick((int)id
, tool
->m_toggleState
);
472 if ( ret
== FALSE
&& tool
->m_isToggle
)
474 tool
->m_toggleState
= !tool
->m_toggleState
;
475 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
476 (WPARAM
)id
, (LPARAM
)MAKELONG(tool
->m_toggleState
, 0));
482 bool wxToolBar95::MSWOnNotify(int WXUNUSED(idCtrl
),
486 // First check if this applies to us
487 NMHDR
*hdr
= (NMHDR
*)lParam
;
489 // the tooltips control created by the toolbar is sometimes Unicode, even
490 // in an ANSI application - this seems to be a bug in comctl32.dll v5
491 int code
= (int)hdr
->code
;
492 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
495 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
496 if ( toolTipWnd
!= hdr
->hwndFrom
)
499 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
500 int id
= (int)ttText
->hdr
.idFrom
;
501 wxNode
*node
= m_tools
.Find((long)id
);
505 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
507 const wxString
& help
= tool
->m_shortHelpString
;
509 if ( !help
.IsEmpty() )
511 if ( code
== TTN_NEEDTEXTA
)
513 ttText
->lpszText
= (wxChar
*)help
.c_str();
515 #if (_WIN32_IE >= 0x0300)
518 // FIXME this is a temp hack only until I understand better what
519 // must be done in both ANSI and Unicode builds
521 size_t lenAnsi
= help
.Len();
523 // MetroWerks doesn't like calling mbstowcs with NULL argument
524 size_t lenUnicode
= 2*lenAnsi
;
526 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
529 // using the pointer of right type avoids us doing all sorts of
530 // pointer arithmetics ourselves
531 wchar_t *dst
= (wchar_t *)ttText
->szText
,
532 *pwz
= new wchar_t[lenUnicode
+ 1];
533 mbstowcs(pwz
, help
, lenAnsi
+ 1);
534 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
536 // put the terminating _wide_ NUL
541 #endif // _WIN32_IE >= 0x0300
544 // For backward compatibility...
545 OnMouseEnter(tool
->m_index
);
550 // ----------------------------------------------------------------------------
552 // ----------------------------------------------------------------------------
554 void wxToolBar95::SetToolBitmapSize(const wxSize
& size
)
556 wxToolBarBase::SetToolBitmapSize(size
);
558 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
561 void wxToolBar95::SetRows(int nRows
)
563 // TRUE in wParam means to create at least as many rows
565 ::SendMessage(GetHwnd(), TB_SETROWS
,
566 MAKEWPARAM(nRows
, TRUE
), (LPARAM
) &rect
);
568 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
569 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
574 wxSize
wxToolBar95::GetMaxSize() const
576 if ( (m_maxWidth
== -1) || (m_maxHeight
== -1) )
578 // it has a side effect of filling m_maxWidth/Height variables
579 ((wxToolBar95
*)this)->SetRows(m_maxRows
); // const_cast
582 return wxSize(m_maxWidth
, m_maxHeight
);
585 // The button size is bigger than the bitmap size
586 wxSize
wxToolBar95::GetToolSize() const
588 // FIXME: this is completely bogus (VZ)
589 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
592 // ----------------------------------------------------------------------------
594 // ----------------------------------------------------------------------------
596 void wxToolBar95::EnableTool(int toolIndex
, bool enable
)
598 wxNode
*node
= m_tools
.Find((long)toolIndex
);
601 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
602 tool
->m_enabled
= enable
;
603 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
604 (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(enable
, 0));
608 void wxToolBar95::ToggleTool(int toolIndex
, bool toggle
)
610 wxNode
*node
= m_tools
.Find((long)toolIndex
);
613 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
614 if (tool
->m_isToggle
)
616 tool
->m_toggleState
= toggle
;
617 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
618 (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(toggle
, 0));
623 bool wxToolBar95::GetToolState(int toolIndex
) const
625 return (::SendMessage(GetHwnd(), TB_ISBUTTONCHECKED
, (WPARAM
)toolIndex
, (LPARAM
)0) != 0);
628 // ----------------------------------------------------------------------------
630 // ----------------------------------------------------------------------------
632 // Responds to colour changes, and passes event on to children.
633 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent
& event
)
635 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
636 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
643 // Propagate the event to the non-top-level children
644 wxWindow::OnSysColourChanged(event
);
647 void wxToolBar95::OnMouseEvent(wxMouseEvent
& event
)
649 if (event
.RightDown())
651 // For now, we don't have an id. Later we could
652 // try finding the tool.
653 OnRightClick((int)-1, event
.GetX(), event
.GetY());
661 // ----------------------------------------------------------------------------
663 // ----------------------------------------------------------------------------
665 // These are the default colors used to map the bitmap colors
666 // to the current system colors
668 // VZ: why are they BGR and not RGB? just to confuse the people or is there a
670 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
671 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
672 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
673 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
674 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
675 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
677 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
679 COLORMAP ColorMap
[] =
681 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
682 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
683 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
684 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
685 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
686 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
689 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
691 for ( n
= 0; n
< NUM_MAPS
; n
++)
693 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
697 HDC hdcMem
= CreateCompatibleDC(NULL
);
701 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
704 for ( i
= 0; i
< width
; i
++)
706 for ( j
= 0; j
< height
; j
++)
708 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
710 BYTE red = GetRValue(pixel);
711 BYTE green = GetGValue(pixel);
712 BYTE blue = GetBValue(pixel);
715 for ( k
= 0; k
< NUM_MAPS
; k
++)
717 if ( ColorMap
[k
].from
== pixel
)
719 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
727 SelectObject(hdcMem
, hbmOld
);
728 DeleteObject(hdcMem
);
733 // Some experiments...
735 // What we want to do is create another bitmap which has a depth of 4,
736 // and set the bits. So probably we want to convert this HBITMAP into a
737 // DIB, then call SetDIBits.
738 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
739 // the screen), then SetDIBits fails.
740 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
741 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
742 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
745 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
747 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
749 DWORD err
= GetLastError();
751 ::ReleaseDC(NULL
, dc
);
754 GlobalUnlock (newDIB
);
757 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
758 // Substitute our new bitmap for the old one
759 ::DeleteObject((HBITMAP
) m_hBitmap
);
760 m_hBitmap
= (WXHBITMAP
) newBitmap
;
764 #endif // !(wxUSE_TOOLBAR && Win95)