1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolBar95
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "tbar95.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if wxUSE_BUTTONBAR && wxUSE_TOOLBAR && defined(__WIN95__)
29 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
35 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
41 #ifndef wxUSE_NORLANDER_HEADERS
42 #include "wx/msw/gnuwin32/extra.h"
47 #include "wx/msw/dib.h"
48 #include "wx/tbar95.h"
50 #include "wx/msw/private.h"
54 #define TBSTYLE_LIST 0x1000
55 #define TBSTYLE_FLAT 0x0800
56 #define TBSTYLE_TRANSPARENT 0x8000
58 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
62 #define TB_GETSTYLE (WM_USER + 57)
63 #define TB_SETSTYLE (WM_USER + 56)
66 /* Hint from a newsgroup for custom flatbar drawing:
67 Set the TBSTYLE_CUSTOMERASE style, then handle the
68 NM_CUSTOMDRAW message and do your custom drawing.
71 #define DEFAULTBITMAPX 16
72 #define DEFAULTBITMAPY 15
73 #define DEFAULTBUTTONX 24
74 #define DEFAULTBUTTONY 24
75 #define DEFAULTBARHEIGHT 27
77 #if !USE_SHARED_LIBRARY
78 IMPLEMENT_DYNAMIC_CLASS(wxToolBar95
, wxToolBarBase
)
81 BEGIN_EVENT_TABLE(wxToolBar95
, wxToolBarBase
)
82 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent
)
83 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged
)
86 static void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
88 wxToolBar95::wxToolBar95()
93 m_defaultWidth
= DEFAULTBITMAPX
;
94 m_defaultHeight
= DEFAULTBITMAPY
;
97 bool wxToolBar95::Create(wxWindow
*parent
,
102 const wxString
& name
)
106 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
107 GetGValue(GetSysColor(COLOR_BTNFACE
)),
108 GetBValue(GetSysColor(COLOR_BTNFACE
)));
109 m_foregroundColour
= *wxBLACK
;
111 wxASSERT_MSG( (style
& wxTB_VERTICAL
) == 0,
112 wxT("Sorry, wxToolBar95 under Windows 95 only "
113 "supports horizontal orientation.") );
120 m_defaultWidth
= DEFAULTBITMAPX
;
121 m_defaultHeight
= DEFAULTBITMAPY
;
124 m_windowStyle
= style
;
126 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
143 m_windowId
= (id
< 0 ? NewControlId() : id
);
145 if (style
& wxBORDER
)
146 msflags
|= WS_BORDER
;
147 msflags
|= WS_CHILD
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
;
149 if (style
& wxTB_FLAT
)
151 if (wxTheApp
->GetComCtl32Version() > 400)
152 msflags
|= TBSTYLE_FLAT
;
156 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
158 // Even with extended styles, need to combine with WS_BORDER
159 // for them to look right.
160 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
161 msflags
|= WS_BORDER
;
163 // Create the toolbar control.
164 HWND hWndToolbar
= CreateWindowEx
166 exStyle
, // Extended styles.
167 TOOLBARCLASSNAME
, // Class name for the toolbar.
168 wxT(""), // No default text.
170 x
, y
, width
, height
, // Standard toolbar size and position.
171 (HWND
) parent
->GetHWND(), // Parent window of the toolbar.
172 (HMENU
)m_windowId
, // Toolbar ID.
173 wxGetInstance(), // Current instance.
174 NULL
// No class data.
177 wxCHECK_MSG( hWndToolbar
, FALSE
, wxT("Toolbar creation failed") );
179 // Toolbar-specific initialisation
180 ::SendMessage(hWndToolbar
, TB_BUTTONSTRUCTSIZE
,
181 (WPARAM
)sizeof(TBBUTTON
), (LPARAM
)0);
183 m_hWnd
= (WXHWND
) hWndToolbar
;
185 parent
->AddChild(this);
187 SubclassWin((WXHWND
)hWndToolbar
);
192 wxToolBar95::~wxToolBar95()
198 ::DeleteObject((HBITMAP
) m_hBitmap
);
203 bool wxToolBar95::CreateTools()
205 if (m_tools
.Number() == 0)
208 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
210 int totalBitmapWidth
= (int)(m_defaultWidth
* m_tools
.Number());
211 int totalBitmapHeight
= (int)m_defaultHeight
;
213 // Create a bitmap for all the tool bitmaps
214 HDC dc
= ::GetDC(NULL
);
215 m_hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap(dc
, totalBitmapWidth
, totalBitmapHeight
);
216 ::ReleaseDC(NULL
, dc
);
218 // Now blit all the tools onto this bitmap
219 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
220 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, (HBITMAP
) m_hBitmap
);
222 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
224 wxNode
*node
= m_tools
.First();
228 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
229 if ((tool
->m_toolStyle
!= wxTOOL_STYLE_SEPARATOR
) && tool
->m_bitmap1
.Ok() && tool
->m_bitmap1
.GetHBITMAP())
231 // wxPalette *palette = tool->m_bitmap1->GetPalette();
233 HBITMAP oldBitmap2
= (HBITMAP
) ::SelectObject(memoryDC2
, (HBITMAP
) tool
->m_bitmap1
.GetHBITMAP());
234 /* int bltResult = */
235 BitBlt(memoryDC
, x
, 0, (int) m_defaultWidth
, (int) m_defaultHeight
, memoryDC2
,
237 ::SelectObject(memoryDC2
, oldBitmap2
);
238 x
+= (int)m_defaultWidth
;
243 ::SelectObject(memoryDC
, oldBitmap
);
244 ::DeleteDC(memoryDC
);
245 ::DeleteDC(memoryDC2
);
247 // Map to system colours
248 wxMapBitmap((HBITMAP
) m_hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
250 if ( oldToolBarBitmap
)
252 TBREPLACEBITMAP replaceBitmap
;
253 replaceBitmap
.hInstOld
= NULL
;
254 replaceBitmap
.hInstNew
= NULL
;
255 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
256 replaceBitmap
.nIDNew
= (UINT
) (HBITMAP
) m_hBitmap
;
257 replaceBitmap
.nButtons
= noButtons
;
258 if (::SendMessage((HWND
) GetHWND(), TB_REPLACEBITMAP
, (WPARAM
) 0, (LPARAM
) &replaceBitmap
) == -1)
260 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
263 ::DeleteObject((HBITMAP
) oldToolBarBitmap
);
265 // Now delete all the buttons
269 // TODO: What about separators???? They don't have an id!
270 if ( ! ::SendMessage( (HWND
) GetHWND(), TB_DELETEBUTTON
, i
, 0 ) )
276 TBADDBITMAP addBitmap
;
278 addBitmap
.nID
= (UINT
)m_hBitmap
;
279 if (::SendMessage((HWND
) GetHWND(), TB_ADDBITMAP
, (WPARAM
) noButtons
, (LPARAM
) &addBitmap
) == -1)
281 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
285 // Now add the buttons.
286 TBBUTTON buttons
[50];
288 node
= m_tools
.First();
293 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
294 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
296 buttons
[i
].iBitmap
= 0;
297 buttons
[i
].idCommand
= 0;
299 buttons
[i
].fsState
= TBSTATE_ENABLED
;
300 buttons
[i
].fsStyle
= TBSTYLE_SEP
;
301 buttons
[i
].dwData
= 0L;
302 buttons
[i
].iString
= 0;
306 buttons
[i
].iBitmap
= bitmapId
;
307 buttons
[i
].idCommand
= tool
->m_index
;
309 buttons
[i
].fsState
= 0;
311 buttons
[i
].fsState
|= TBSTATE_ENABLED
;
312 if (tool
->m_toggleState
)
313 buttons
[i
].fsState
|= TBSTATE_CHECKED
;
314 buttons
[i
].fsStyle
= tool
->m_isToggle
? TBSTYLE_CHECK
: TBSTYLE_BUTTON
;
315 buttons
[i
].dwData
= 0L;
316 buttons
[i
].iString
= 0;
325 long rc
= ::SendMessage((HWND
) GetHWND(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)& buttons
);
327 wxCHECK_MSG( rc
, FALSE
, wxT("failed to add buttons to the toolbar") );
329 (void)::SendMessage((HWND
) GetHWND(), TB_AUTOSIZE
, (WPARAM
)0, (LPARAM
) 0);
336 bool wxToolBar95::MSWCommand(WXUINT cmd
, WXWORD id
)
338 wxNode
*node
= m_tools
.Find((long)id
);
341 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
342 if (tool
->m_isToggle
)
343 tool
->m_toggleState
= (1 == (1 & (int)::SendMessage((HWND
) GetHWND(), TB_GETSTATE
, (WPARAM
) id
, (LPARAM
) 0)));
345 BOOL ret
= OnLeftClick((int)id
, tool
->m_toggleState
);
346 if (ret
== FALSE
&& tool
->m_isToggle
)
348 tool
->m_toggleState
= !tool
->m_toggleState
;
349 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)id
, (LPARAM
)MAKELONG(tool
->m_toggleState
, 0));
354 bool wxToolBar95::MSWOnNotify(int WXUNUSED(idCtrl
),
358 // First check if this applies to us
359 NMHDR
*hdr
= (NMHDR
*)lParam
;
361 // the tooltips control created by the toolbar is sometimes Unicode, even in
362 // an ANSI application
363 int code
= (int)hdr
->code
;
364 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
367 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
368 if ( toolTipWnd
!= hdr
->hwndFrom
)
371 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
372 int id
= (int)ttText
->hdr
.idFrom
;
373 wxNode
*node
= m_tools
.Find((long)id
);
377 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
379 const wxString
& help
= tool
->m_shortHelpString
;
381 if ( !help
.IsEmpty() )
383 if ( code
== TTN_NEEDTEXTA
)
385 ttText
->lpszText
= (wxChar
*)help
.c_str();
387 #if (_WIN32_IE >= 0x0300)
390 // FIXME this is a temp hack only until I understand better what
391 // must be done in both ANSI and Unicode builds
393 size_t lenAnsi
= help
.Len();
395 // MetroWerks doesn't like calling mbstowcs with NULL argument
396 size_t lenUnicode
= 2*lenAnsi
;
398 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
401 // using the pointer of right type avoids us doing all sorts of
402 // pointer arithmetics ourselves
403 wchar_t *dst
= (wchar_t *)ttText
->szText
,
404 *pwz
= new wchar_t[lenUnicode
+ 1];
405 mbstowcs(pwz
, help
, lenAnsi
+ 1);
406 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
408 // put the terminating _wide_ NUL
413 #endif // _WIN32_IE >= 0x0300
416 // For backward compatibility...
417 OnMouseEnter(tool
->m_index
);
422 void wxToolBar95::SetToolBitmapSize(const wxSize
& size
)
424 m_defaultWidth
= size
.x
;
425 m_defaultHeight
= size
.y
;
426 ::SendMessage((HWND
) GetHWND(), TB_SETBITMAPSIZE
, 0, (LPARAM
) MAKELONG ((int)size
.x
, (int)size
.y
));
429 void wxToolBar95::SetRows(int nRows
)
432 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(nRows
, TRUE
), (LPARAM
) & rect
);
433 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
434 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
437 wxSize
wxToolBar95::GetMaxSize() const
439 if ((m_maxWidth
== -1) || (m_maxHeight
== -1))
442 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(m_maxRows
, TRUE
), (LPARAM
) & rect
);
443 ((wxToolBar95
*)this)->m_maxWidth
= (rect
.right
- rect
.left
+ 2); // ???
444 ((wxToolBar95
*)this)->m_maxHeight
= (rect
.bottom
- rect
.top
+ 2); // ???
446 return wxSize(m_maxWidth
, m_maxHeight
);
449 // The button size is bigger than the bitmap size
450 wxSize
wxToolBar95::GetToolSize() const
452 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
455 void wxToolBar95::EnableTool(int toolIndex
, bool enable
)
457 wxNode
*node
= m_tools
.Find((long)toolIndex
);
460 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
461 tool
->m_enabled
= enable
;
462 ::SendMessage((HWND
) GetHWND(), TB_ENABLEBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(enable
, 0));
466 void wxToolBar95::ToggleTool(int toolIndex
, bool toggle
)
468 wxNode
*node
= m_tools
.Find((long)toolIndex
);
471 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
472 if (tool
->m_isToggle
)
474 tool
->m_toggleState
= toggle
;
475 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(toggle
, 0));
480 bool wxToolBar95::GetToolState(int toolIndex
) const
482 return (::SendMessage((HWND
) GetHWND(), TB_ISBUTTONCHECKED
, (WPARAM
)toolIndex
, (LPARAM
)0) != 0);
485 void wxToolBar95::ClearTools()
487 // TODO: Don't know how to reset the toolbar bitmap, as yet.
488 // But adding tools and calling CreateTools should probably
489 // recreate a buttonbar OK.
490 wxToolBarBase::ClearTools();
493 // If pushedBitmap is NULL, a reversed version of bitmap is
494 // created and used as the pushed/toggled image.
495 // If toggle is TRUE, the button toggles between the two states.
496 wxToolBarTool
*wxToolBar95::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
497 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
499 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, wxNullBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
500 tool
->m_clientData
= clientData
;
505 tool
->m_x
= m_xMargin
;
510 tool
->m_y
= m_yMargin
;
512 tool
->SetSize(GetToolSize().x
, GetToolSize().y
);
514 m_tools
.Append((long)index
, tool
);
518 // Responds to colour changes, and passes event on to children.
519 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent
& event
)
521 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
522 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
529 // Propagate the event to the non-top-level children
530 wxWindow::OnSysColourChanged(event
);
533 void wxToolBar95::OnMouseEvent(wxMouseEvent
& event
)
535 if (event
.RightDown())
537 // For now, we don't have an id. Later we could
538 // try finding the tool.
539 OnRightClick((int)-1, event
.GetX(), event
.GetY());
547 // These are the default colors used to map the bitmap colors
548 // to the current system colors
550 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
551 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
552 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
553 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
554 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
555 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
557 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
559 COLORMAP ColorMap
[] = {
560 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
561 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
562 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
563 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
564 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
565 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
568 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
570 for ( n
= 0; n
< NUM_MAPS
; n
++)
572 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
576 HDC hdcMem
= CreateCompatibleDC(NULL
);
580 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
583 for ( i
= 0; i
< width
; i
++)
585 for ( j
= 0; j
< height
; j
++)
587 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
589 BYTE red = GetRValue(pixel);
590 BYTE green = GetGValue(pixel);
591 BYTE blue = GetBValue(pixel);
594 for ( k
= 0; k
< NUM_MAPS
; k
++)
596 if ( ColorMap
[k
].from
== pixel
)
598 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
606 SelectObject(hdcMem
, hbmOld
);
607 DeleteObject(hdcMem
);
612 // Some experiments...
614 // What we want to do is create another bitmap which has a depth of 4,
615 // and set the bits. So probably we want to convert this HBITMAP into a
616 // DIB, then call SetDIBits.
617 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
618 // the screen), then SetDIBits fails.
619 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
620 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
621 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
624 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
626 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
628 DWORD err
= GetLastError();
630 ::ReleaseDC(NULL
, dc
);
633 GlobalUnlock (newDIB
);
636 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
637 // Substitute our new bitmap for the old one
638 ::DeleteObject((HBITMAP
) m_hBitmap
);
639 m_hBitmap
= (WXHBITMAP
) newBitmap
;