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__)
41 #include "wx/msw/gnuwin32/extra.h"
45 #include "wx/msw/dib.h"
46 #include "wx/tbar95.h"
48 #include "wx/msw/private.h"
52 #define TBSTYLE_LIST 0x1000
53 #define TBSTYLE_FLAT 0x0800
54 #define TBSTYLE_TRANSPARENT 0x8000
56 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
60 #define TB_GETSTYLE (WM_USER + 57)
61 #define TB_SETSTYLE (WM_USER + 56)
64 /* Hint from a newsgroup for custom flatbar drawing:
65 Set the TBSTYLE_CUSTOMERASE style, then handle the
66 NM_CUSTOMDRAW message and do your custom drawing.
69 #define DEFAULTBITMAPX 16
70 #define DEFAULTBITMAPY 15
71 #define DEFAULTBUTTONX 24
72 #define DEFAULTBUTTONY 24
73 #define DEFAULTBARHEIGHT 27
75 #if !USE_SHARED_LIBRARY
76 IMPLEMENT_DYNAMIC_CLASS(wxToolBar95
, wxToolBarBase
)
79 BEGIN_EVENT_TABLE(wxToolBar95
, wxToolBarBase
)
80 EVT_SIZE(wxToolBar95::OnSize
)
81 EVT_PAINT(wxToolBar95::OnPaint
)
82 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent
)
83 EVT_KILL_FOCUS(wxToolBar95::OnKillFocus
)
84 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged
)
87 static void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
89 wxToolBar95::wxToolBar95()
94 m_defaultWidth
= DEFAULTBITMAPX
;
95 m_defaultHeight
= DEFAULTBITMAPY
;
98 bool wxToolBar95::Create(wxWindow
*parent
,
103 const wxString
& name
)
105 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
106 GetGValue(GetSysColor(COLOR_BTNFACE
)),
107 GetBValue(GetSysColor(COLOR_BTNFACE
)));
108 m_foregroundColour
= *wxBLACK
;
110 wxASSERT_MSG( (style
& wxTB_VERTICAL
) == 0,
111 "Sorry, wxToolBar95 under Windows 95 only "
112 "supports horizontal orientation." );
119 m_defaultWidth
= DEFAULTBITMAPX
;
120 m_defaultHeight
= DEFAULTBITMAPY
;
123 m_windowStyle
= style
;
125 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
142 m_windowId
= (id
< 0 ? NewControlId() : id
);
144 if (style
& wxBORDER
)
145 msflags
|= WS_BORDER
;
146 msflags
|= WS_CHILD
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
;
148 if (style
& wxTB_FLAT
)
150 if (wxTheApp
->GetComCtl32Version() > 400)
151 msflags
|= TBSTYLE_FLAT
;
155 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
157 // Even with extended styles, need to combine with WS_BORDER
158 // for them to look right.
159 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
160 msflags
|= WS_BORDER
;
162 // Create the toolbar control.
163 HWND hWndToolbar
= CreateWindowEx
165 exStyle
, // Extended styles.
166 TOOLBARCLASSNAME
, // Class name for the toolbar.
167 "", // No default text.
169 x
, y
, width
, height
, // Standard toolbar size and position.
170 (HWND
) parent
->GetHWND(), // Parent window of the toolbar.
171 (HMENU
)m_windowId
, // Toolbar ID.
172 wxGetInstance(), // Current instance.
173 NULL
// No class data.
176 wxCHECK_MSG( hWndToolbar
, FALSE
, "Toolbar creation failed" );
178 // Toolbar-specific initialisation
179 ::SendMessage(hWndToolbar
, TB_BUTTONSTRUCTSIZE
,
180 (WPARAM
)sizeof(TBBUTTON
), (LPARAM
)0);
182 m_hWnd
= (WXHWND
) hWndToolbar
;
184 parent
->AddChild(this);
186 SubclassWin((WXHWND
)hWndToolbar
);
191 wxToolBar95::~wxToolBar95()
197 ::DeleteObject((HBITMAP
) m_hBitmap
);
202 bool wxToolBar95::CreateTools()
204 if (m_tools
.Number() == 0)
207 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
209 int totalBitmapWidth
= (int)(m_defaultWidth
* m_tools
.Number());
210 int totalBitmapHeight
= (int)m_defaultHeight
;
212 // Create a bitmap for all the tool bitmaps
213 HDC dc
= ::GetDC(NULL
);
214 m_hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap(dc
, totalBitmapWidth
, totalBitmapHeight
);
215 ::ReleaseDC(NULL
, dc
);
217 // Now blit all the tools onto this bitmap
218 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
219 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, (HBITMAP
) m_hBitmap
);
221 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
223 wxNode
*node
= m_tools
.First();
227 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
228 if ((tool
->m_toolStyle
!= wxTOOL_STYLE_SEPARATOR
) && tool
->m_bitmap1
.Ok() && tool
->m_bitmap1
.GetHBITMAP())
230 // wxPalette *palette = tool->m_bitmap1->GetPalette();
232 HBITMAP oldBitmap2
= (HBITMAP
) ::SelectObject(memoryDC2
, (HBITMAP
) tool
->m_bitmap1
.GetHBITMAP());
233 /* int bltResult = */
234 BitBlt(memoryDC
, x
, 0, (int) m_defaultWidth
, (int) m_defaultHeight
, memoryDC2
,
236 ::SelectObject(memoryDC2
, oldBitmap2
);
237 x
+= (int)m_defaultWidth
;
242 ::SelectObject(memoryDC
, oldBitmap
);
243 ::DeleteDC(memoryDC
);
244 ::DeleteDC(memoryDC2
);
246 // Map to system colours
247 wxMapBitmap((HBITMAP
) m_hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
249 if ( oldToolBarBitmap
)
251 TBREPLACEBITMAP replaceBitmap
;
252 replaceBitmap
.hInstOld
= NULL
;
253 replaceBitmap
.hInstNew
= NULL
;
254 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
255 replaceBitmap
.nIDNew
= (UINT
) (HBITMAP
) m_hBitmap
;
256 replaceBitmap
.nButtons
= noButtons
;
257 if (::SendMessage((HWND
) GetHWND(), TB_REPLACEBITMAP
, (WPARAM
) 0, (LPARAM
) &replaceBitmap
) == -1)
258 wxFAIL_MSG("Could not add bitmap to toolbar");
260 ::DeleteObject((HBITMAP
) oldToolBarBitmap
);
262 // Now delete all the buttons
266 // TODO: What about separators???? They don't have an id!
267 if ( ! ::SendMessage( (HWND
) GetHWND(), TB_DELETEBUTTON
, i
, 0 ) )
273 TBADDBITMAP addBitmap
;
275 addBitmap
.nID
= (UINT
)m_hBitmap
;
276 if (::SendMessage((HWND
) GetHWND(), TB_ADDBITMAP
, (WPARAM
) noButtons
, (LPARAM
) &addBitmap
) == -1)
277 wxFAIL_MSG("Could not add bitmap to toolbar");
280 // Now add the buttons.
281 TBBUTTON buttons
[50];
283 node
= m_tools
.First();
288 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
289 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
291 buttons
[i
].iBitmap
= 0;
292 buttons
[i
].idCommand
= 0;
294 buttons
[i
].fsState
= TBSTATE_ENABLED
;
295 buttons
[i
].fsStyle
= TBSTYLE_SEP
;
296 buttons
[i
].dwData
= 0L;
297 buttons
[i
].iString
= 0;
301 buttons
[i
].iBitmap
= bitmapId
;
302 buttons
[i
].idCommand
= tool
->m_index
;
304 buttons
[i
].fsState
= 0;
306 buttons
[i
].fsState
|= TBSTATE_ENABLED
;
307 if (tool
->m_toggleState
)
308 buttons
[i
].fsState
|= TBSTATE_CHECKED
;
309 buttons
[i
].fsStyle
= tool
->m_isToggle
? TBSTYLE_CHECK
: TBSTYLE_BUTTON
;
310 buttons
[i
].dwData
= 0L;
311 buttons
[i
].iString
= 0;
320 long rc
= ::SendMessage((HWND
) GetHWND(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)& buttons
);
322 wxCHECK_MSG( rc
, FALSE
, "failed to add buttons to the toolbar" );
324 (void)::SendMessage((HWND
) GetHWND(), TB_AUTOSIZE
, (WPARAM
)0, (LPARAM
) 0);
331 bool wxToolBar95::MSWCommand(WXUINT cmd
, WXWORD id
)
333 wxNode
*node
= m_tools
.Find((long)id
);
336 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
337 if (tool
->m_isToggle
)
338 tool
->m_toggleState
= (1 == (1 & (int)::SendMessage((HWND
) GetHWND(), TB_GETSTATE
, (WPARAM
) id
, (LPARAM
) 0)));
340 BOOL ret
= OnLeftClick((int)id
, tool
->m_toggleState
);
341 if (ret
== FALSE
&& tool
->m_isToggle
)
343 tool
->m_toggleState
= !tool
->m_toggleState
;
344 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)id
, (LPARAM
)MAKELONG(tool
->m_toggleState
, 0));
349 bool wxToolBar95::MSWNotify(WXWPARAM
WXUNUSED(wParam
),
353 // First check if this applies to us
354 NMHDR
*hdr
= (NMHDR
*)lParam
;
356 // the tooltips control created by the toolbar is sometimes Unicode, even in
357 // an ANSI application
358 if ( (hdr
->code
!= TTN_NEEDTEXTA
) && (hdr
->code
!= TTN_NEEDTEXTW
) )
361 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
362 if ( toolTipWnd
!= hdr
->hwndFrom
)
365 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
366 int id
= (int)ttText
->hdr
.idFrom
;
367 wxNode
*node
= m_tools
.Find((long)id
);
371 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
373 const wxString
& help
= tool
->m_shortHelpString
;
375 if ( !help
.IsEmpty() )
377 if ( hdr
->code
== TTN_NEEDTEXTA
)
379 ttText
->lpszText
= (char *)help
.c_str();
381 #if (_WIN32_IE >= 0x0300)
384 // FIXME this is a temp hack only until I understand better what
385 // must be done in both ANSI and Unicode builds
387 size_t lenAnsi
= help
.Len();
389 // MetroWerks doesn't like calling mbstowcs with NULL argument
390 size_t lenUnicode
= 2*lenAnsi
;
392 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
395 // using the pointer of right type avoids us doing all sorts of
396 // pointer arithmetics ourselves
397 wchar_t *dst
= (wchar_t *)ttText
->szText
,
398 *pwz
= new wchar_t[lenUnicode
+ 1];
399 mbstowcs(pwz
, help
, lenAnsi
+ 1);
400 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
402 // put the terminating _wide_ NUL
407 #endif // _WIN32_IE >= 0x0300
410 // For backward compatibility...
411 OnMouseEnter(tool
->m_index
);
416 void wxToolBar95::SetToolBitmapSize(const wxSize
& size
)
418 m_defaultWidth
= size
.x
;
419 m_defaultHeight
= size
.y
;
420 ::SendMessage((HWND
) GetHWND(), TB_SETBITMAPSIZE
, 0, (LPARAM
) MAKELONG ((int)size
.x
, (int)size
.y
));
423 void wxToolBar95::SetRows(int nRows
)
426 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(nRows
, TRUE
), (LPARAM
) & rect
);
427 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
428 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
431 wxSize
wxToolBar95::GetMaxSize() const
433 if ((m_maxWidth
== -1) || (m_maxHeight
== -1))
436 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(m_maxRows
, TRUE
), (LPARAM
) & rect
);
437 ((wxToolBar95
*)this)->m_maxWidth
= (rect
.right
- rect
.left
+ 2); // ???
438 ((wxToolBar95
*)this)->m_maxHeight
= (rect
.bottom
- rect
.top
+ 2); // ???
440 return wxSize(m_maxWidth
, m_maxHeight
);
443 void wxToolBar95::GetSize(int *w
, int *h
) const
445 wxWindow::GetSize(w
, h
);
446 // For some reason, the returned height is several pixels bigger than that
448 // Taking this fudge factor out now, it seems fine without it.
452 // The button size is bigger than the bitmap size
453 wxSize
wxToolBar95::GetToolSize() const
455 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
458 void wxToolBar95::EnableTool(int toolIndex
, bool enable
)
460 wxNode
*node
= m_tools
.Find((long)toolIndex
);
463 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
464 tool
->m_enabled
= enable
;
465 ::SendMessage((HWND
) GetHWND(), TB_ENABLEBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(enable
, 0));
469 void wxToolBar95::ToggleTool(int toolIndex
, bool toggle
)
471 wxNode
*node
= m_tools
.Find((long)toolIndex
);
474 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
475 if (tool
->m_isToggle
)
477 tool
->m_toggleState
= toggle
;
478 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(toggle
, 0));
483 bool wxToolBar95::GetToolState(int toolIndex
) const
485 return (::SendMessage((HWND
) GetHWND(), TB_ISBUTTONCHECKED
, (WPARAM
)toolIndex
, (LPARAM
)0) != 0);
488 void wxToolBar95::ClearTools()
490 // TODO: Don't know how to reset the toolbar bitmap, as yet.
491 // But adding tools and calling CreateTools should probably
492 // recreate a buttonbar OK.
493 wxToolBarBase::ClearTools();
496 // If pushedBitmap is NULL, a reversed version of bitmap is
497 // created and used as the pushed/toggled image.
498 // If toggle is TRUE, the button toggles between the two states.
499 wxToolBarTool
*wxToolBar95::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
500 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
502 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, wxNullBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
503 tool
->m_clientData
= clientData
;
508 tool
->m_x
= m_xMargin
;
513 tool
->m_y
= m_yMargin
;
515 tool
->SetSize(GetToolSize().x
, GetToolSize().y
);
517 m_tools
.Append((long)index
, tool
);
521 // Responds to colour changes, and passes event on to children.
522 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent
& event
)
524 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
525 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
534 // Propagate the event to the non-top-level children
535 wxWindow::OnSysColourChanged(event
);
538 void wxToolBar95::OnMouseEvent(wxMouseEvent
& event
)
540 if (event
.RightDown())
542 // For now, we don't have an id. Later we could
543 // try finding the tool.
544 OnRightClick((int)-1, event
.GetX(), event
.GetY());
552 // These are the default colors used to map the bitmap colors
553 // to the current system colors
555 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
556 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
557 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
558 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
559 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
560 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
562 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
564 COLORMAP ColorMap
[] = {
565 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
566 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
567 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
568 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
569 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
570 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
573 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
575 for ( n
= 0; n
< NUM_MAPS
; n
++)
577 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
581 HDC hdcMem
= CreateCompatibleDC(NULL
);
585 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
588 for ( i
= 0; i
< width
; i
++)
590 for ( j
= 0; j
< height
; j
++)
592 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
594 BYTE red = GetRValue(pixel);
595 BYTE green = GetGValue(pixel);
596 BYTE blue = GetBValue(pixel);
599 for ( k
= 0; k
< NUM_MAPS
; k
++)
601 if ( ColorMap
[k
].from
== pixel
)
603 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
611 SelectObject(hdcMem
, hbmOld
);
612 DeleteObject(hdcMem
);
617 // Some experiments...
619 // What we want to do is create another bitmap which has a depth of 4,
620 // and set the bits. So probably we want to convert this HBITMAP into a
621 // DIB, then call SetDIBits.
622 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
623 // the screen), then SetDIBits fails.
624 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
625 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
626 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
629 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
631 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
633 DWORD err
= GetLastError();
635 ::ReleaseDC(NULL
, dc
);
638 GlobalUnlock (newDIB
);
641 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
642 // Substitute our new bitmap for the old one
643 ::DeleteObject((HBITMAP
) m_hBitmap
);
644 m_hBitmap
= (WXHBITMAP
) newBitmap
;